From 2b1277aa64a6928f42ea7d6278aa81afdd72f73e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 26 Mar 2011 08:56:41 +0100 Subject: [PATCH] Fixed #2680 (setVarId: variables with class qualification don't get varid) --- lib/tokenize.cpp | 19 ++++++++++++++++--- test/testtokenize.cpp | 17 +++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index fbeed2752..1bd13af87 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3603,6 +3603,11 @@ void Tokenizer::setVarId() } } + /** @todo better handling when classes in different scopes have the same name */ + std::string className; + if (Token::Match(tok2, "class|struct %type% [:{]")) + className = tok2->strAt(1); + // Set start token if (Token::Match(tok2, "class|struct")) { @@ -3621,9 +3626,17 @@ void Tokenizer::setVarId() const char c = tok2->str()[0]; if (c == varname[0]) { - const std::string &prev = tok2->strAt(-1); - if (tok2->str() == varname && prev != "struct" && prev != "union" && prev != "::" && prev != "." && tok2->strAt(1) != "::") - tok2->varId(_varId); + if (tok2->str() == varname) + { + const std::string &prev = tok2->previous()->str(); + + /** @todo better handling when classes in different scopes have the same name */ + if (!className.empty() && Token::simpleMatch(tok2->tokAt(-2), className.c_str()) && prev == "::") + tok2->varId(_varId); + + else if (tok2->str() == varname && prev != "struct" && prev != "union" && prev != "::" && prev != "." && tok2->strAt(1) != "::") + tok2->varId(_varId); + } } else if (c == '{') ++indentlevel; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 7febb17a9..3dfb24f33 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -196,6 +196,7 @@ private: TEST_CASE(varidclass9); TEST_CASE(varidclass10); // variable declaration below usage TEST_CASE(varidclass11); // variable declaration below usage + TEST_CASE(varidclass12); TEST_CASE(file1); TEST_CASE(file2); @@ -3481,6 +3482,22 @@ private: ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } + void varidclass12() + { + const std::string code("class Fred {\n" + " int a;\n" + " void f() { Fred::a = 0; }\n" + "};\n"); + + const std::string expected("\n\n##file 0\n" + "1: class Fred {\n" + "2: int a@1 ;\n" + "3: void f ( ) { Fred :: a@1 = 0 ; }\n" + "4: } ;\n"); + + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); + } + void file1() { const char code[] = "a1\n"