diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 3456ea4bb..4de0e4013 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -734,6 +734,9 @@ void Tokenizer::setVarId() if (Token::Match(tok, "else|return|typedef|delete")) continue; + if (Token::simpleMatch(tok, "const")) + tok = tok->next(); + if (Token::simpleMatch(tok, "std ::")) tok = tok->tokAt(2); @@ -759,7 +762,7 @@ void Tokenizer::setVarId() { if (tok2->isName()) varname = tok2->strAt(0); - else if (tok2->str() != "*") + else if (tok2->str() != "*" && tok2->str() != "&") break; tok2 = tok2->next(); } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index fb7fcd6dd..72643a8a7 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -1291,6 +1291,46 @@ private: ASSERT_EQUALS(expected, actual); } + + { + const std::string code("void f1(int &p)\n" + "{\n" + " p = 0;\n" + "}\n" + "void f2(std::string &str)\n" + "{\n" + " str.clear();\n" + "}\n" + "void f3(const std::string &s)\n" + "{\n" + " s.size();\n" + "}\n"); + + // tokenize.. + Tokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + tokenizer.setVarId(); + + // result.. + const std::string actual(tokenizer.tokens()->stringifyList(true)); + const std::string expected("\n\n##file 0\n" + "1: void f1 ( int & p@1 )\n" + "2: {\n" + "3: p@1 = 0 ;\n" + "4: }\n" + "5: void f2 ( std :: string & str@2 )\n" + "6: {\n" + "7: str@2 . clear ( ) ;\n" + "8: }\n" + "9: void f3 ( const std :: string & s@3 )\n" + "10: {\n" + "11: s@3 . size ( ) ;\n" + "12: }\n"); + + ASSERT_EQUALS(expected, actual); + } + } void varidclass1()