diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 4ef13d3d8..23822eb4e 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1426,20 +1426,59 @@ void Tokenizer::setVarId() // Skip template arguments.. if (Token::Match(tok, "%type% <")) { + int level = 1; + bool again; Token *tok2 = tok->tokAt(2); - while (Token::Match(tok2, "%var% ::")) - tok2 = tok2->tokAt(2); + do // Look for start of templates or template arguments + { + again = false; - while (tok2 && (tok2->isName() || tok2->isNumber() || tok2->str() == "*" || tok2->str() == ",")) - tok2 = tok2->next(); + while (Token::Match(tok2, "%var% ::")) + tok2 = tok2->tokAt(2); - if (Token::Match(tok2, "> %var%")) - tok = tok2; - else if (Token::Match(tok2, "> ::|*|& %var%")) - tok = tok2->next(); - else - continue; // Not code that I understand / not a variable declaration + if (Token::Match(tok2, "%type% <")) + { + level++; + tok2 = tok2->tokAt(2); + again = true; + } + else if (Token::Match(tok2, "%type% ,")) + { + tok2 = tok2->tokAt(2); + again = true; + } + else + { + while (tok2 && (tok2->isName() || tok2->isNumber() || tok2->str() == "*" || tok2->str() == ",")) + tok2 = tok2->next(); + } + } + while (again); + + do // Look for end of templates + { + again = false; + + if (level == 1 && Token::Match(tok2, "> %var%")) + tok = tok2; + else if (level > 1 && tok2->str() == ">") + { + level--; + if (level == 0) + tok = tok2; + else + { + tok2 = tok2->tokAt(1); + again = true; + } + } + else if (level == 1 && Token::Match(tok2, "> ::|*|& %var%")) + tok = tok2->next(); + else + continue; // Not code that I understand / not a variable declaration + } + while (again); } // Determine name of declared variable.. diff --git a/test/testother.cpp b/test/testother.cpp index 478c432ff..abe032798 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1550,6 +1550,22 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:4]: (possible style) Pre-Decrementing variable 'it' is preferred to Post-Decrementing\n" "[test.cpp:6]: (possible style) Pre-Decrementing variable 'it' is preferred to Post-Decrementing\n", errout.str()); + + checkpostIncrementDecrement("void f1()\n" + "{\n" + " std::list >::iterator it;\n" + " for (it = ab.begin(); it != ab.end(); it++)\n" + " ;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (possible style) Pre-Incrementing variable 'it' is preferred to Post-Incrementing\n", errout.str()); + + checkpostIncrementDecrement("void f1()\n" + "{\n" + " std::map >::iterator it;\n" + " for (it = ab.begin(); it != ab.end(); it++)\n" + " ;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (possible style) Pre-Incrementing variable 'it' is preferred to Post-Incrementing\n", errout.str()); } void postIncrementDecrementClass()