Fixed #2680 (setVarId: variables with class qualification don't get varid)

This commit is contained in:
Daniel Marjamäki 2011-03-26 08:56:41 +01:00
parent c27ecf5aeb
commit 2b1277aa64
2 changed files with 33 additions and 3 deletions

View File

@ -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;

View File

@ -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"