Fixed #1225 (false negative :: uninitialized variable not detected when using enums)
This commit is contained in:
parent
3fb420cbf0
commit
c5966bba59
|
@ -1533,8 +1533,24 @@ private:
|
||||||
if (vartok->varId() == 0)
|
if (vartok->varId() == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Suppress warnings if variable in inner scope has same name as variable in outer scope
|
bool isenum = false;
|
||||||
if (!tok.isStandardType())
|
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;
|
std::set<unsigned int> dup;
|
||||||
for (std::list<ExecutionPath *>::const_iterator it = checks.begin(); it != checks.end(); ++it)
|
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));
|
checks.push_back(new CheckUninitVar(owner, vartok->varId(), vartok->str(), p, a));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1560,6 +1576,9 @@ private:
|
||||||
// Variable declaration..
|
// Variable declaration..
|
||||||
if (tok.str() != "return")
|
if (tok.str() != "return")
|
||||||
{
|
{
|
||||||
|
if (Token::Match(&tok, "enum %type% {"))
|
||||||
|
return tok.tokAt(2)->link();
|
||||||
|
|
||||||
if (Token::Match(tok.previous(), "[;{}] %type% *| %var% ;"))
|
if (Token::Match(tok.previous(), "[;{}] %type% *| %var% ;"))
|
||||||
{
|
{
|
||||||
const Token * vartok = tok.next();
|
const Token * vartok = tok.next();
|
||||||
|
|
|
@ -1578,6 +1578,15 @@ private:
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
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..
|
// references..
|
||||||
checkUninitVar("void f()\n"
|
checkUninitVar("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
|
Loading…
Reference in New Issue