@@ -54,14 +54,26 @@ namespace {
5454}
5555
5656TemplateSimplifier::TokenAndName::TokenAndName (Token *tok, const std::string &s, const std::string &n, const Token *nt, const Token *pe) :
57- token(tok), scope(s), name(n), nameToken(nt), paramEnd(pe)
57+ token(tok), scope(s), name(n), nameToken(nt), paramEnd(pe), flags( 0 )
5858{
59+ // only set flags for declaration
60+ if (token && nameToken && paramEnd) {
61+ isClass (Token::Match (paramEnd->next (), " class|struct|union %name% <|{|:" ));
62+ isFunction (nameToken->strAt (1 ) == " (" ||
63+ (nameToken->strAt (1 ) == " <" && nameToken->next ()->findClosingBracket ()->strAt (1 ) == " (" ));
64+ isVariable (Token::Match (nameToken->next (), " =|;" ) ||
65+ (nameToken->strAt (1 ) == " <" && Token::Match (nameToken->next ()->findClosingBracket ()->next (), " =|;" )));
66+ isAlias (paramEnd->strAt (1 ) == " using" );
67+ isSpecialized (Token::Match (token, " template < >" ));
68+ }
69+
5970 if (token)
6071 token->templateSimplifierPointer (this );
6172}
6273
6374TemplateSimplifier::TokenAndName::TokenAndName (const TokenAndName& otherTok) :
64- token(otherTok.token), scope(otherTok.scope), name(otherTok.name), nameToken(otherTok.nameToken), paramEnd(otherTok.paramEnd)
75+ token(otherTok.token), scope(otherTok.scope), name(otherTok.name),
76+ nameToken(otherTok.nameToken), paramEnd(otherTok.paramEnd), flags(otherTok.flags)
6577{
6678 if (token)
6779 token->templateSimplifierPointer (this );
@@ -983,7 +995,7 @@ void TemplateSimplifier::addNamespace(const TokenAndName &templateDeclaration, c
983995 }
984996}
985997
986- bool TemplateSimplifier::alreadyHasNamespace (const TokenAndName &templateDeclaration, const Token *tok) const
998+ bool TemplateSimplifier::alreadyHasNamespace (const TokenAndName &templateDeclaration, const Token *tok)
987999{
9881000 std::string scope = templateDeclaration.scope ;
9891001
@@ -1012,17 +1024,16 @@ void TemplateSimplifier::expandTemplate(
10121024 const Token *endOfTemplateDefinition = nullptr ;
10131025 const Token * const templateDeclarationNameToken = templateDeclaration.nameToken ;
10141026 const Token * const templateDeclarationToken = templateDeclaration.paramEnd ;
1015- const bool isClass = Token::Match (templateDeclaration.paramEnd ->next (), " class|struct|union %name% <|{|:" );
1016- const bool isFunction = templateDeclarationNameToken->strAt (1 ) == " (" ||
1017- (templateDeclarationNameToken->strAt (1 ) == " <" && templateDeclarationNameToken->next ()->findClosingBracket ()->strAt (1 ) == " (" );
1018- const bool isSpecialization = Token::Match (templateDeclaration.token , " template < >" );
1027+ const bool isClass = templateDeclaration.isClass ();
1028+ const bool isFunction = templateDeclaration.isFunction ();
1029+ const bool isSpecialization = templateDeclaration.isSpecialized ();
10191030
10201031 // add forward declarations
10211032 if (copy && isClass) {
10221033 templateDeclaration.token ->insertToken (templateDeclarationToken->strAt (1 ), " " , true );
10231034 templateDeclaration.token ->insertToken (newName, " " , true );
10241035 templateDeclaration.token ->insertToken (" ;" , " " , true );
1025- } else if (isFunction && (copy || (!copy && isSpecialization) )) {
1036+ } else if (isFunction && (copy || isSpecialization)) {
10261037 Token * dst = templateDeclaration.token ;
10271038 bool isStatic = false ;
10281039 std::string scope;
@@ -1812,16 +1823,8 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
18121823 std::vector<const Token *> typeParametersInDeclaration;
18131824 getTemplateParametersInDeclaration (templateDeclaration.token ->tokAt (2 ), typeParametersInDeclaration);
18141825 const bool printDebug = mSettings ->debugwarnings ;
1815- const bool specialized = Token::simpleMatch (templateDeclaration.token , " template < >" );
1816- bool isfunc = false ;
1817-
1818- if (templateDeclaration.nameToken ->strAt (1 ) == " (" )
1819- isfunc = true ;
1820- else if (templateDeclaration.nameToken ->strAt (1 ) == " <" ) {
1821- const Token *tok1 = templateDeclaration.nameToken ->tokAt (1 )->findClosingBracket ();
1822- if (tok1 && tok1->strAt (1 ) == " (" )
1823- isfunc = true ;
1824- }
1826+ const bool specialized = templateDeclaration.isSpecialized ();
1827+ const bool isfunc = templateDeclaration.isFunction ();
18251828
18261829 // locate template usage..
18271830 std::string::size_type numberOfTemplateInstantiations = mTemplateInstantiations .size ();
@@ -2098,19 +2101,20 @@ void TemplateSimplifier::getUserDefinedSpecializations()
20982101{
20992102 // try to locate a matching declaration for each user defined specialization
21002103 for (auto & spec : mTemplateDeclarations ) {
2101- if (Token::Match ( spec.token , " template < > " )) {
2104+ if (spec.isSpecialized ( )) {
21022105 std::string specName = getPathName (spec);
21032106
21042107 bool found = false ;
21052108 for (auto & decl : mTemplateDeclarations ) {
2106- if (Token::Match ( decl.token , " template < > " ))
2109+ if (decl.isSpecialized ( ))
21072110 continue ;
21082111 std::string declName = getPathName (decl);
21092112
21102113 // make sure the scopes and names match
21112114 if (specName == declName) {
21122115 // @todo make sure function parameters also match
21132116 mTemplateUserSpecializationMap [spec.token ] = decl.token ;
2117+ found = true ;
21142118 }
21152119 }
21162120
@@ -2258,7 +2262,7 @@ void TemplateSimplifier::simplifyTemplates(
22582262 break ;
22592263 }
22602264 if (decl != mTemplateDeclarations .end ()) {
2261- if (Token::simpleMatch ( it->token , " template < > " )) {
2265+ if (it->isSpecialized ( )) {
22622266 // delete the "template < >"
22632267 Token * tok = it->token ;
22642268 tok->deleteNext (2 );
@@ -2296,7 +2300,7 @@ void TemplateSimplifier::simplifyTemplates(
22962300 end = end->next ();
22972301 if (start->previous ())
22982302 start = start->previous ();
2299- if (end->next ())
2303+ if (end && end ->next ())
23002304 end = end->next ();
23012305 eraseTokens (start, end);
23022306 }
0 commit comments