uninitialized variables: refactoring handling of loop bodies

This commit is contained in:
Daniel Marjamäki 2010-10-24 18:26:59 +02:00
parent b66d3c8858
commit 6601de7681
3 changed files with 10 additions and 64 deletions

View File

@ -3647,70 +3647,15 @@ private:
return ExecutionPath::parseCondition(tok, checks);
}
void parseLoopBody(const Token *tok_, std::list<ExecutionPath *> &checks) const
void parseLoopBody(const Token *tok, std::list<ExecutionPath *> &checks) const
{
for (; tok_; tok_ = tok_->next())
while (tok)
{
if (tok_->str() == "{")
if (tok->str() == "{")
return;
const Token &tok = *tok_;
if (Token::Match(tok.previous(), "[;{}] %var% [=[.]"))
{
if (tok.next()->str() == ".")
{
if (use_dead_pointer(checks, &tok))
{
return;
}
}
else
{
// check variable usages in rhs/index
for (const Token *tok2 = tok.tokAt(2); tok2; tok2 = tok2->next())
{
if (Token::Match(tok2, "[;)=?]"))
break;
if (Token::Match(tok2, "%var% ("))
break;
if (tok2->varId() &&
!Token::Match(tok2->previous(), "&|::") &&
!Token::simpleMatch(tok2->next(), "="))
{
// Multiple assignments..
if (Token::simpleMatch(tok2->next(), "["))
{
const Token * tok3 = tok2;
while (Token::simpleMatch(tok3->next(), "["))
tok3 = tok3->next()->link();
if (Token::simpleMatch(tok3, "] ="))
continue;
}
bool foundError;
if (tok2->previous()->str() == "*" || tok2->next()->str() == "[")
foundError = use_array_or_pointer_data(checks, tok2);
else
foundError = use(checks, tok2);
// prevent duplicate error messages
if (foundError)
{
bailOutVar(checks, tok2->varId());
}
}
}
}
// pointer aliasing?
if (Token::Match(tok.tokAt(2), "%var% ;"))
{
pointer_assignment(checks, &tok, tok.tokAt(2));
}
}
const Token *next = parse(*tok, checks);
if (next)
tok = tok->next();
}
}

View File

@ -1452,8 +1452,7 @@ private:
" *p = 0;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: p\n" // <-- duplicate
"[test.cpp:5]: (error) Uninitialized variable: p\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: p\n", errout.str());
// +=
checkUninitVar("void f()\n"

View File

@ -271,5 +271,7 @@ void TestFixture::reportOut(const std::string & outmsg)
void TestFixture::reportErr(const ErrorLogger::ErrorMessage &msg)
{
errout << msg.toString() << std::endl;
const std::string errormessage(msg.toString());
if (errout.str().find(errormessage) == std::string::npos)
errout << errormessage << std::endl;
}