Fixed #1147 (Improve check: return uninitialized variable)

This commit is contained in:
Daniel Marjamäki 2009-12-23 17:55:03 +01:00
parent b6c320de8a
commit a7d48f2eb4
2 changed files with 26 additions and 16 deletions

View File

@ -1335,24 +1335,27 @@ private:
const Token *parse(const Token &tok, bool &foundError, std::list<ExecutionPath *> &checks) const
{
// Variable declaration..
if (Token::Match(tok.previous(), "[;{}] %type% *| %var% ;"))
if (tok.str() != "return")
{
const Token * vartok = tok.next();
const bool p(vartok->str() == "*");
if (p)
vartok = vartok->next();
if ((p || tok.isStandardType()) && vartok->varId() != 0)
checks.push_back(new CheckUninitVar(owner, vartok->varId(), vartok->str(), p, false));
return vartok->next();
}
if (Token::Match(tok.previous(), "[;{}] %type% *| %var% ;"))
{
const Token * vartok = tok.next();
const bool p(vartok->str() == "*");
if (p)
vartok = vartok->next();
if ((p || tok.isStandardType()) && vartok->varId() != 0)
checks.push_back(new CheckUninitVar(owner, vartok->varId(), vartok->str(), p, false));
return vartok->next();
}
// Variable declaration for array..
if (Token::Match(tok.previous(), "[;{}] %type% %var% [ %num% ] ;"))
{
const Token * vartok = tok.next();
if (vartok->varId() != 0)
checks.push_back(new CheckUninitVar(owner, vartok->varId(), vartok->str(), false, true));
return vartok->next()->link()->next();
// Variable declaration for array..
if (Token::Match(tok.previous(), "[;{}] %type% %var% [ %num% ] ;"))
{
const Token * vartok = tok.next();
if (vartok->varId() != 0)
checks.push_back(new CheckUninitVar(owner, vartok->varId(), vartok->str(), false, true));
return vartok->next()->link()->next();
}
}
if (tok.varId())

View File

@ -991,6 +991,13 @@ private:
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: p\n", errout.str());
checkUninitVar("static int foo()\n"
"{\n"
" int ret;\n"
" return ret;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: ret\n", errout.str());
checkUninitVar("static void foo()\n"
"{\n"
" int x, y;\n"