Tokenizer::setVarId: Prevent that wrong varid is given when uncertain. Ticket: #2680

This commit is contained in:
Daniel Marjamäki 2011-03-26 10:05:07 +01:00
parent 0ff5320d19
commit 7d9872a134
2 changed files with 18 additions and 1 deletions

View File

@ -3631,7 +3631,7 @@ void Tokenizer::setVarId()
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 == "::")
if (!className.empty() && Token::Match(tok2->tokAt(-3), ("!!:: " + className + " ::").c_str()))
tok2->varId(_varId);
else if (tok2->str() == varname && prev != "struct" && prev != "union" && prev != "::" && prev != "." && tok2->strAt(1) != "::")

View File

@ -197,6 +197,7 @@ private:
TEST_CASE(varidclass10); // variable declaration below usage
TEST_CASE(varidclass11); // variable declaration below usage
TEST_CASE(varidclass12);
TEST_CASE(varidclass13);
TEST_CASE(file1);
TEST_CASE(file2);
@ -3498,6 +3499,22 @@ private:
ASSERT_EQUALS(expected, tokenizeDebugListing(code));
}
void varidclass13()
{
const std::string code("class Fred {\n"
" int a;\n"
" void f() { Foo::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 ( ) { Foo :: Fred :: a = 0 ; }\n"
"4: } ;\n");
ASSERT_EQUALS(expected, tokenizeDebugListing(code));
}
void file1()
{
const char code[] = "a1\n"