diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 8b313945a..a73e8e7a9 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3601,8 +3601,13 @@ void Tokenizer::setVarId() } } - // Start token - if (!Token::Match(tok2, "class|struct")) + // Set start token + if (Token::Match(tok2, "class|struct")) + { + while (tok2->str() != "{") + tok2 = tok2->next(); + } + else tok2 = tok; ++_varId; @@ -3646,29 +3651,6 @@ void Tokenizer::setVarId() } } - // Struct/Class members - for (Token *tok = _tokens; tok; tok = tok->next()) - { - // str.clear is a variable - // str.clear() is a member function - if (tok->varId() != 0 && - Token::Match(tok->next(), ". %var% !!(") && - tok->tokAt(2)->varId() == 0) - { - ++_varId; - - const std::string pattern(std::string(". ") + tok->strAt(2)); - for (Token *tok2 = tok; tok2; tok2 = tok2->next()) - { - if (tok2->varId() == tok->varId()) - { - if (Token::Match(tok2->next(), pattern.c_str())) - tok2->tokAt(2)->varId(_varId); - } - } - } - } - // Member functions and variables in this source std::list allMemberFunctions; std::list allMemberVars; @@ -3783,6 +3765,29 @@ void Tokenizer::setVarId() } } + + // Struct/Class members + for (Token *tok = _tokens; tok; tok = tok->next()) + { + // str.clear is a variable + // str.clear() is a member function + if (tok->varId() != 0 && + Token::Match(tok->next(), ". %var% !!(") && + tok->tokAt(2)->varId() == 0) + { + ++_varId; + + const std::string pattern(std::string(". ") + tok->strAt(2)); + for (Token *tok2 = tok; tok2; tok2 = tok2->next()) + { + if (tok2->varId() == tok->varId()) + { + if (Token::Match(tok2->next(), pattern.c_str())) + tok2->tokAt(2)->varId(_varId); + } + } + } + } } bool Tokenizer::createLinks() diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index cdbbe8e73..f1ce4876e 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -194,6 +194,7 @@ private: TEST_CASE(varidclass8); TEST_CASE(varidclass9); TEST_CASE(varidclass10); // variable declaration below usage + TEST_CASE(varidclass11); // variable declaration below usage TEST_CASE(file1); TEST_CASE(file2); @@ -3262,7 +3263,7 @@ private: "8:\n" "9: void A :: f ( )\n" "10: {\n" - "11: i@1 = 0 ;\n" + "11: i = 0 ;\n" "12: }\n"); ASSERT_EQUALS(expected, actual); @@ -3423,7 +3424,7 @@ private: " a = 3;\n" " }\n" " int a;\n" - "};\n;"); + "};\n"); const std::string expected("\n\n##file 0\n" "1: class A {\n" @@ -3435,6 +3436,33 @@ private: ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } + void varidclass11() + { + const std::string code("class Fred {\n" + " int a;\n" + " void f();\n" + "};\n" + "class Wilma {\n" + " int a;\n" + " void f();\n" + "};\n" + "void Fred::f() { a = 0; }\n" + "void Wilma::f() { a = 0; }\n"); + + const std::string expected("\n\n##file 0\n" + "1: class Fred {\n" + "2: int a@1 ;\n" + "3: void f ( ) ;\n" + "4: } ;\n" + "5: class Wilma {\n" + "6: int a@2 ;\n" + "7: void f ( ) ;\n" + "8: } ;\n" + "9: void Fred :: f ( ) { a@1 = 0 ; }\n" + "10: void Wilma :: f ( ) { a@2 = 0 ; }\n"); + + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); + } void file1() {