Fix ticket #416 (False positive: Redundant code)

http://sourceforge.net/apps/trac/cppcheck/ticket/416
This commit is contained in:
Reijo Tomperi 2009-06-19 19:03:09 +03:00
parent 6e1281ec34
commit 5f7d88b36c
2 changed files with 24 additions and 4 deletions

View File

@ -1384,7 +1384,7 @@ void Tokenizer::simplifyTokenList()
{
if (Token::Match(tok, "const %type% %var% = %num% ;"))
{
const char *sym = tok->strAt(2);
unsigned int varId = tok->tokAt(2)->varId();
const char *num = tok->strAt(4);
int indent = 1;
for (Token *tok2 = tok->tokAt(6); tok2; tok2 = tok2->next())
@ -1401,9 +1401,7 @@ void Tokenizer::simplifyTokenList()
}
// Compare constants, but don't touch members of other structures
else if (tok2->str() == sym &&
tok2->previous() &&
tok2->previous()->str() != ".")
else if (tok2->varId() == varId)
{
tok2->str(num);
}

View File

@ -92,6 +92,7 @@ private:
TEST_CASE(simplifyKnownVariables8);
TEST_CASE(simplifyKnownVariables9);
TEST_CASE(simplifyKnownVariables10);
TEST_CASE(simplifyKnownVariables11);
TEST_CASE(match1);
@ -796,6 +797,27 @@ private:
}
}
void simplifyKnownVariables11()
{
const char code[] = "const int foo = 0;\n"
"int main()\n"
"{\n"
" int foo=0;\n"
"}\n";
// tokenize..
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" const int foo = 0 ; int main ( ) { int foo ; foo = 0 ; }", ostr.str());
}
void match1()
{
// Match "%var% | %var%"