Uninitialized variables: Better handling of variable usage in condition

This commit is contained in:
Daniel Marjamäki 2011-12-26 18:56:40 +01:00
parent 59cd099ba7
commit 5aaec7adc5
3 changed files with 6 additions and 6 deletions

View File

@ -1114,7 +1114,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int
// Inner scope..
if (Token::Match(tok, "if (")) {
// initialization / usage in condition..
if (checkIfForWhileHead(tok->next(), varid, ispointer, suppressErrors))
if (checkIfForWhileHead(tok->next(), varid, ispointer, suppressErrors, bool(number_of_if == 0)))
return true;
// goto the {
@ -1185,7 +1185,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int
return true;
// is variable used / initialized in for-head
if (checkIfForWhileHead(tok->next(), varid, ispointer, suppressErrors))
if (checkIfForWhileHead(tok->next(), varid, ispointer, suppressErrors, bool(number_of_if == 0)))
return true;
}
@ -1219,7 +1219,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int
return ret;
}
bool CheckUninitVar::checkIfForWhileHead(const Token *startparanthesis, unsigned int varid, bool ispointer, bool suppressErrors)
bool CheckUninitVar::checkIfForWhileHead(const Token *startparanthesis, unsigned int varid, bool ispointer, bool suppressErrors, bool isuninit)
{
const Token * const endpar = startparanthesis->link();
for (const Token *tok = startparanthesis->next(); tok && tok != endpar; tok = tok->next()) {
@ -1230,7 +1230,7 @@ bool CheckUninitVar::checkIfForWhileHead(const Token *startparanthesis, unsigned
}
if (Token::Match(tok, "sizeof|decltype|offsetof ("))
tok = tok->next()->link();
if (tok->str() == "&&")
if (!isuninit && tok->str() == "&&")
suppressErrors = true;
}
return false;

View File

@ -61,7 +61,7 @@ public:
/** Check for uninitialized variables */
void check();
bool checkScopeForVariable(const Token *tok, const unsigned int varid, bool ispointer, bool * const possibleInit);
bool checkIfForWhileHead(const Token *startparanthesis, unsigned int varid, bool ispointer, bool suppressErrors);
bool checkIfForWhileHead(const Token *startparanthesis, unsigned int varid, bool ispointer, bool suppressErrors, bool isuninit);
bool isVariableUsage(const Token *vartok, bool ispointer) const;

View File

@ -1928,7 +1928,7 @@ private:
" int a;\n"
" if (x == 0 && (a == 1)) { }\n"
"}\n");
TODO_ASSERT_EQUALS("error", "", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
// asm
checkUninitVar2("void f() {\n"