Uninitialized variables: Better handling of variable usage in condition
This commit is contained in:
parent
59cd099ba7
commit
5aaec7adc5
|
@ -1114,7 +1114,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int
|
||||||
// Inner scope..
|
// Inner scope..
|
||||||
if (Token::Match(tok, "if (")) {
|
if (Token::Match(tok, "if (")) {
|
||||||
// initialization / usage in condition..
|
// 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;
|
return true;
|
||||||
|
|
||||||
// goto the {
|
// goto the {
|
||||||
|
@ -1185,7 +1185,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
// is variable used / initialized in for-head
|
// 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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1219,7 +1219,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int
|
||||||
return ret;
|
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();
|
const Token * const endpar = startparanthesis->link();
|
||||||
for (const Token *tok = startparanthesis->next(); tok && tok != endpar; tok = tok->next()) {
|
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 ("))
|
if (Token::Match(tok, "sizeof|decltype|offsetof ("))
|
||||||
tok = tok->next()->link();
|
tok = tok->next()->link();
|
||||||
if (tok->str() == "&&")
|
if (!isuninit && tok->str() == "&&")
|
||||||
suppressErrors = true;
|
suppressErrors = true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -61,7 +61,7 @@ public:
|
||||||
/** Check for uninitialized variables */
|
/** Check for uninitialized variables */
|
||||||
void check();
|
void check();
|
||||||
bool checkScopeForVariable(const Token *tok, const unsigned int varid, bool ispointer, bool * const possibleInit);
|
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;
|
bool isVariableUsage(const Token *vartok, bool ispointer) const;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1928,7 +1928,7 @@ private:
|
||||||
" int a;\n"
|
" int a;\n"
|
||||||
" if (x == 0 && (a == 1)) { }\n"
|
" if (x == 0 && (a == 1)) { }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
TODO_ASSERT_EQUALS("error", "", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
||||||
|
|
||||||
// asm
|
// asm
|
||||||
checkUninitVar2("void f() {\n"
|
checkUninitVar2("void f() {\n"
|
||||||
|
|
Loading…
Reference in New Issue