Fixed #1824 (false positive: unitialised variable)

This commit is contained in:
Daniel Marjamäki 2010-07-06 13:18:28 +02:00
parent 1799e4a575
commit 1b20f8d27d
2 changed files with 38 additions and 16 deletions

View File

@ -42,26 +42,28 @@ bool ExecutionPath::parseCondition(const Token &tok, std::list<ExecutionPath *>
break;
--parlevel;
}
else if (Token::Match(tok2, ";{}"))
else if (Token::Match(tok2, "[;{}]"))
break;
if (tok2->varId() != 0)
{
bailOutVar(checks, tok2->varId());
for (std::list<ExecutionPath *>::iterator it = checks.begin(); it != checks.end();)
{
if ((*it)->varId > 0 && (*it)->numberOfIf >= 1)
{
delete *it;
checks.erase(it++);
}
else
{
++it;
}
}
}
}
for (std::list<ExecutionPath *>::iterator it = checks.begin(); it != checks.end();)
{
if ((*it)->varId > 0 && (*it)->numberOfIf >= 1)
{
delete *it;
checks.erase(it++);
}
else
{
++it;
}
}
return false;
}
@ -103,7 +105,8 @@ static void parseIfSwitchBody(const Token * const tok,
std::list<ExecutionPath *>::const_iterator it;
for (it = checks.begin(); it != checks.end(); ++it)
{
c.push_back((*it)->copy());
if ((*it)->numberOfIf == 0)
c.push_back((*it)->copy());
if ((*it)->varId != 0)
countif2.insert((*it)->varId);
}

View File

@ -1174,6 +1174,7 @@ private:
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
// Clear the error buffer..
errout.str("");
@ -1351,10 +1352,10 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void a()\n"
checkUninitVar("int a()\n"
"{\n"
" int x;\n"
" int y = x;\n"
" return x;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
@ -1617,6 +1618,24 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void foo(int a)\n"
"{\n"
" int n;\n"
" int condition;\n"
" if(a == 1) {\n"
" n=0;\n"
" condition=0;\n"
" }\n"
" else {\n"
" n=1;\n"
" }\n"
"\n"
" if( n == 0) {\n"
" a=condition;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void f()\n"
"{\n"
" C *c;\n"