diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 429cf9762..94bd2fcc6 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -738,7 +738,7 @@ void Tokenizer::setVarId() { const std::string &classname(tok->next()->str()); - int indentlevel = 0; + unsigned int indentlevel = 0; // What member variables are there in this class? for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) @@ -748,9 +748,9 @@ void Tokenizer::setVarId() ++indentlevel; else if (tok2->str() == "}") { - --indentlevel; - if (indentlevel == 0) + if (indentlevel <= 1) break; + --indentlevel; } // If a variable id is found in the class declaration, start @@ -765,16 +765,16 @@ void Tokenizer::setVarId() // Found a class function.. if (Token::Match(tok3, (classname + " :: %var% (").c_str())) { - int indentlevel2 = 0; + unsigned int indentlevel2 = 0; while (tok3) { if (tok3->str() == "{") ++indentlevel2; else if (tok3->str() == "}") { - --indentlevel2; if (indentlevel2 <= 1) break; + --indentlevel2; } else if (indentlevel2 == 0 && tok3->str() == ";") break; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index fc229bd8b..6c25fb68f 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -110,7 +110,11 @@ private: TEST_CASE(varid9); TEST_CASE(varidStl); TEST_CASE(varid_delete); - TEST_CASE(varid_class); + + TEST_CASE(varidclass1); + TEST_CASE(varidclass2); + TEST_CASE(varidclass3); + TEST_CASE(varidclass4); TEST_CASE(file1); TEST_CASE(file2); @@ -1143,7 +1147,7 @@ private: } - void varid_class() + void varidclass1() { const std::string code("class Fred\n" "{\n" @@ -1192,6 +1196,121 @@ private: } + void varidclass2() + { + const std::string code("class Fred\n" + "{ void f(); };\n" + "\n" + "void A::foo1()\n" + "{\n" + " int i = 0;\n" + "}\n" + "\n" + "void Fred::f()\n" + "{\n" + " i = 0;\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: class Fred\n" + "2: { void f@1 ( ) ; } ;\n" + "3:\n" + "4: void A :: foo1 ( )\n" + "5: {\n" + "6: int i@2 ; i@2 = 0 ;\n" + "7: }\n" + "8:\n" + "9: void Fred :: f ( )\n" + "10: {\n" + "11: i = 0 ;\n" + "12: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + + void varidclass3() + { + const std::string code("class Fred\n" + "{ int i; void f(); };\n" + "\n" + "void Fred::f()\n" + "{\n" + " i = 0;\n" + "}\n" + "\n" + "void A::f()\n" + "{\n" + " i = 0;\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: class Fred\n" + "2: { int i@1 ; void f@2 ( ) ; } ;\n" + "3:\n" + "4: void Fred :: f ( )\n" + "5: {\n" + "6: i@1 = 0 ;\n" + "7: }\n" + "8:\n" + "9: void A :: f ( )\n" + "10: {\n" + "11: i = 0 ;\n" + "12: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + + void varidclass4() + { + const std::string code("class Fred\n" + "{ int i; void f(); };\n" + "\n" + "void Fred::f()\n" + "{\n" + " if (i) { }\n" + " i = 0;\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: class Fred\n" + "2: { int i@1 ; void f@2 ( ) ; } ;\n" + "3:\n" + "4: void Fred :: f ( )\n" + "5: {\n" + "6: if ( i@1 ) { }\n" + "7: i@1 = 0 ;\n" + "8: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + void file1() {