Fix #759 (Tokenizer: Incorrect var id when two variables with same name)

http://sourceforge.net/apps/trac/cppcheck/ticket/759
This commit is contained in:
Reijo Tomperi 2009-09-30 14:40:10 +03:00
parent 7852d38f54
commit 6ed727564c
2 changed files with 68 additions and 26 deletions

View File

@ -1078,6 +1078,7 @@ void Tokenizer::setVarId()
int indentlevel = 0; int indentlevel = 0;
int parlevel = 0; int parlevel = 0;
bool dot = false; bool dot = false;
bool funcDeclaration = false;
for (tok2 = tok->next(); tok2; tok2 = tok2->next()) for (tok2 = tok->next(); tok2; tok2 = tok2->next())
{ {
if (!dot && tok2->str() == varname && !Token::Match(tok2->previous(), "struct|union")) if (!dot && tok2->str() == varname && !Token::Match(tok2->previous(), "struct|union"))
@ -1089,6 +1090,10 @@ void Tokenizer::setVarId()
--indentlevel; --indentlevel;
if (indentlevel < 0) if (indentlevel < 0)
break; break;
// We have reached the end of a loop: "for( int i;;) { }"
if (funcDeclaration && indentlevel <= 0)
break;
} }
else if (tok2->str() == "(") else if (tok2->str() == "(")
++parlevel; ++parlevel;
@ -1096,7 +1101,7 @@ void Tokenizer::setVarId()
{ {
// Is this a function parameter or a variable declared in for example a for loop? // Is this a function parameter or a variable declared in for example a for loop?
if (parlevel == 0 && indentlevel == 0 && Token::Match(tok2, ") const| {")) if (parlevel == 0 && indentlevel == 0 && Token::Match(tok2, ") const| {"))
; funcDeclaration = true;
else else
--parlevel; --parlevel;
} }

View File

@ -930,34 +930,71 @@ private:
void varid1() void varid1()
{ {
const std::string code("static int i = 1;\n" {
"void f()\n" const std::string code("static int i = 1;\n"
"{\n" "void f()\n"
" int i = 2;\n" "{\n"
" for (int i = 0; i < 10; ++i)\n" " int i = 2;\n"
" i = 3;\n" " for (int i = 0; i < 10; ++i)\n"
" i = 4;\n" " i = 3;\n"
"}\n"); " i = 4;\n"
"}\n");
// tokenize.. // tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId(); tokenizer.setVarId();
// result.. // result..
const std::string actual(tokenizer.tokens()->stringifyList(true)); const std::string actual(tokenizer.tokens()->stringifyList(true));
const std::string expected("\n\n##file 0\n" const std::string expected("\n\n##file 0\n"
"1: static int i@1 = 1 ;\n" "1: static int i@1 = 1 ;\n"
"2: void f ( )\n" "2: void f ( )\n"
"3: {\n" "3: {\n"
"4: int i@2 ; i@2 = 2 ;\n" "4: int i@2 ; i@2 = 2 ;\n"
"5: for ( int i@3 = 0 ; i@3 < 10 ; ++ i@3 )\n" "5: for ( int i@3 = 0 ; i@3 < 10 ; ++ i@3 )\n"
"6: i@3 = 3 ;\n" "6: i@3 = 3 ;\n"
"7: i@2 = 4 ;\n" "7: i@2 = 4 ;\n"
"8: }\n"); "8: }\n");
ASSERT_EQUALS(expected, actual); ASSERT_EQUALS(expected, actual);
}
{
const std::string code("static int i = 1;\n"
"void f()\n"
"{\n"
" int i = 2;\n"
" for (int i = 0; i < 10; ++i)\n"
" {\n"
" i = 3;\n"
" }\n"
" i = 4;\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: static int i@1 = 1 ;\n"
"2: void f ( )\n"
"3: {\n"
"4: int i@2 ; i@2 = 2 ;\n"
"5: for ( int i@3 = 0 ; i@3 < 10 ; ++ i@3 )\n"
"6: {\n"
"7: i@3 = 3 ;\n"
"8: }\n"
"9: i@2 = 4 ;\n"
"10: }\n");
ASSERT_EQUALS(expected, actual);
}
} }
void varid2() void varid2()