@@ -4365,7 +4365,7 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
43654365
43664366 reportUnknownMacros ();
43674367
4368- simplifyHeaders ();
4368+ simplifyHeadersAndUnusedTemplates ();
43694369
43704370 // Remove __asm..
43714371 simplifyAsm ();
@@ -5027,7 +5027,7 @@ void Tokenizer::dump(std::ostream &out) const
50275027 list.front ()->printValueFlow (true , out);
50285028}
50295029
5030- void Tokenizer::simplifyHeaders ()
5030+ void Tokenizer::simplifyHeadersAndUnusedTemplates ()
50315031{
50325032 if (mSettings ->checkHeaders && mSettings ->checkUnusedTemplates )
50335033 // Full analysis. All information in the headers are kept.
@@ -5053,7 +5053,13 @@ void Tokenizer::simplifyHeaders()
50535053 // functions and types to keep
50545054 std::set<std::string> keep;
50555055 for (const Token *tok = list.front (); tok; tok = tok->next ()) {
5056- if (!tok->isName ())
5056+ if (isCPP () && Token::simpleMatch (tok, " template <" )) {
5057+ const Token *closingBracket = tok->next ()->findClosingBracket ();
5058+ if (Token::Match (closingBracket, " > class|struct %name% {" ))
5059+ tok = closingBracket->linkAt (3 );
5060+ }
5061+
5062+ if (!tok->isName () || tok->isKeyword ())
50575063 continue ;
50585064
50595065 if (!checkHeaders && tok->fileIndex () != 0 )
@@ -5088,61 +5094,57 @@ void Tokenizer::simplifyHeaders()
50885094 }
50895095 }
50905096
5091- if (Token::Match (tok, " [;{}]" )) {
5097+ if (!tok-> previous () || Token::Match (tok-> previous () , " [;{}]" )) {
50925098 // Remove unused function declarations
50935099 if (isIncluded && removeUnusedIncludedFunctions) {
50945100 while (1 ) {
5095- Token *start = tok-> next () ;
5101+ Token *start = tok;
50965102 while (start && functionStart.find (start->str ()) != functionStart.end ())
50975103 start = start->next ();
5098- if (Token::Match (start, " %name% (" ) && Token::Match (start->linkAt (1 ), " ) const| ;" ) && keep.find (start->str ()) == keep.end ())
5104+ if (Token::Match (start, " %name% (" ) && Token::Match (start->linkAt (1 ), " ) const| ;" ) && keep.find (start->str ()) == keep.end ()) {
50995105 Token::eraseTokens (tok, start->linkAt (1 )->tokAt (2 ));
5100- else
5106+ tok->deleteThis ();
5107+ } else
51015108 break ;
51025109 }
51035110 }
51045111
51055112 if (isIncluded && removeUnusedIncludedClasses) {
5106- if (Token::Match (tok, " [;{}] class|struct %name% [:{]" ) && keep.find (tok->strAt (2 )) == keep.end ()) {
5113+ if (Token::Match (tok, " class|struct %name% [:{]" ) && keep.find (tok->strAt (1 )) == keep.end ()) {
51075114 // Remove this class/struct
5108- const Token *endToken = tok->tokAt (3 );
5115+ const Token *endToken = tok->tokAt (2 );
51095116 if (endToken->str () == " :" ) {
51105117 endToken = endToken->next ();
51115118 while (Token::Match (endToken, " %name%|," ))
51125119 endToken = endToken->next ();
51135120 }
5114- if (endToken && endToken->str () == " {" && Token::simpleMatch (endToken->link (), " } ;" ))
5121+ if (endToken && endToken->str () == " {" && Token::simpleMatch (endToken->link (), " } ;" )) {
51155122 Token::eraseTokens (tok, endToken->link ()->next ());
5123+ tok->deleteThis ();
5124+ }
51165125 }
51175126 }
51185127
51195128 if (removeUnusedTemplates || (isIncluded && removeUnusedIncludedTemplates)) {
5120- if (Token::Match (tok->next (), " template < %name%" )) {
5121- const Token *tok2 = tok->tokAt (3 );
5122- while (Token::Match (tok2, " %name% %name% [,=>]" ) || Token::Match (tok2, " typename|class ... %name% [,>]" )) {
5123- if (Token::Match (tok2, " typename|class ..." ))
5124- tok2 = tok2->tokAt (3 );
5125- else
5126- tok2 = tok2->tokAt (2 );
5127- if (Token::Match (tok2, " = %name% [,>]" ))
5128- tok2 = tok2->tokAt (2 );
5129- if (tok2->str () == " ," )
5130- tok2 = tok2->next ();
5131- }
5132- if (Token::Match (tok2, " > class|struct %name% [;:{]" ) && keep.find (tok2->strAt (2 )) == keep.end ()) {
5133- const Token *endToken = tok2->tokAt (3 );
5129+ if (Token::Match (tok, " template < %name%" )) {
5130+ const Token *closingBracket = tok->next ()->findClosingBracket ();
5131+ if (Token::Match (closingBracket, " > class|struct %name% [;:{]" ) && keep.find (closingBracket->strAt (2 )) == keep.end ()) {
5132+ const Token *endToken = closingBracket->tokAt (3 );
51345133 if (endToken->str () == " :" ) {
51355134 endToken = endToken->next ();
51365135 while (Token::Match (endToken, " %name%|," ))
51375136 endToken = endToken->next ();
51385137 }
51395138 if (endToken && endToken->str () == " {" )
51405139 endToken = endToken->link ()->next ();
5141- if (endToken && endToken->str () == " ;" )
5140+ if (endToken && endToken->str () == " ;" ) {
51425141 Token::eraseTokens (tok, endToken);
5143- } else if (Token::Match (tok2, " > %type% %name% (" ) && Token::simpleMatch (tok2->linkAt (3 ), " ) {" ) && keep.find (tok2->strAt (2 )) == keep.end ()) {
5144- const Token *endToken = tok2->linkAt (3 )->linkAt (1 )->next ();
5142+ tok->deleteThis ();
5143+ }
5144+ } else if (Token::Match (closingBracket, " > %type% %name% (" ) && Token::simpleMatch (closingBracket->linkAt (3 ), " ) {" ) && keep.find (closingBracket->strAt (2 )) == keep.end ()) {
5145+ const Token *endToken = closingBracket->linkAt (3 )->linkAt (1 )->next ();
51455146 Token::eraseTokens (tok, endToken);
5147+ tok->deleteThis ();
51465148 }
51475149 }
51485150 }
0 commit comments