Fixed #1225 (false negative :: uninitialized variable not detected when using enums)

This commit is contained in:
Daniel Marjamäki 2010-01-10 20:36:15 +01:00
parent 3fb420cbf0
commit c5966bba59
2 changed files with 30 additions and 2 deletions

View File

@ -1533,8 +1533,24 @@ private:
if (vartok->varId() == 0)
return;
// Suppress warnings if variable in inner scope has same name as variable in outer scope
bool isenum = false;
if (!tok.isStandardType())
{
const std::string pattern("enum " + tok.str());
for (const Token *tok2 = tok.previous(); tok2; tok2 = tok2->previous())
{
if (tok2->str() != "{")
continue;
if (Token::simpleMatch(tok2->tokAt(-2), pattern.c_str()))
{
isenum = true;
break;
}
}
}
// Suppress warnings if variable in inner scope has same name as variable in outer scope
if (!tok.isStandardType() && !isenum)
{
std::set<unsigned int> dup;
for (std::list<ExecutionPath *>::const_iterator it = checks.begin(); it != checks.end(); ++it)
@ -1551,7 +1567,7 @@ private:
}
}
if (a || p || tok.isStandardType())
if (a || p || tok.isStandardType() || isenum)
checks.push_back(new CheckUninitVar(owner, vartok->varId(), vartok->str(), p, a));
}
@ -1560,6 +1576,9 @@ private:
// Variable declaration..
if (tok.str() != "return")
{
if (Token::Match(&tok, "enum %type% {"))
return tok.tokAt(2)->link();
if (Token::Match(tok.previous(), "[;{}] %type% *| %var% ;"))
{
const Token * vartok = tok.next();

View File

@ -1578,6 +1578,15 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
// enum..
checkUninitVar("void f()\n"
"{\n"
" enum AB { a, b };\n"
" AB ab;\n"
" if (ab);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: ab\n", errout.str());
// references..
checkUninitVar("void f()\n"
"{\n"