Uninitialized variables: Detect more errors. Ticket: #3369

This commit is contained in:
Daniel Marjamäki 2011-12-14 18:28:30 +01:00
parent 419ae2a135
commit 8e3e634ce0
3 changed files with 34 additions and 5 deletions

View File

@ -1078,7 +1078,8 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int
const Token * const endToken = tok->next()->link(); const Token * const endToken = tok->next()->link();
for (const Token *tok2 = tok->tokAt(2); tok2 != endToken; tok2 = tok2->next()) { for (const Token *tok2 = tok->tokAt(2); tok2 != endToken; tok2 = tok2->next()) {
if (tok2->varId() == varid) { if (tok2->varId() == varid) {
// TODO: better checks if this is initialization or usage if (isVariableUsage(tok2))
uninitvarError(tok2, tok2->str());
return true; return true;
} }
} }
@ -1124,10 +1125,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int
return true; return true;
// Use variable // Use variable
if (tok->previous()->str() == "return") if (isVariableUsage(tok))
uninitvarError(tok, tok->str());
else if (Token::Match(tok->next(), "++|--|%op%"))
uninitvarError(tok, tok->str()); uninitvarError(tok, tok->str());
else else
@ -1139,6 +1137,23 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int
return ret; return ret;
} }
bool CheckUninitVar::isVariableUsage(const Token *vartok) const
{
if (vartok->previous()->str() == "return")
return true;
if (Token::Match(vartok->next(), "++|--|%op%"))
return true;
if (Token::Match(vartok->previous(), "++|--|%op%")) {
if (vartok->previous()->str() != "&" || !Token::Match(vartok->tokAt(-2), "[(,]")) {
return true;
}
}
return false;
}
void CheckUninitVar::uninitstringError(const Token *tok, const std::string &varname, bool strncpy_) void CheckUninitVar::uninitstringError(const Token *tok, const std::string &varname, bool strncpy_)
{ {

View File

@ -61,6 +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 checkScopeForVariable(const Token *tok, const unsigned int varid);
bool isVariableUsage(const Token *vartok) const;
/** /**
* @brief Uninitialized variables: analyse functions to see how they work with uninitialized variables * @brief Uninitialized variables: analyse functions to see how they work with uninitialized variables

View File

@ -1713,6 +1713,19 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
checkUninitVar2("void f() {\n"
" int x;\n"
" int y = 3 & x;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
// using uninit var in condition
checkUninitVar2("void f() {\n"
" int x;\n"
" if (1 == (3 & x)) { }\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
// conditional initialization // conditional initialization
checkUninitVar2("void f() {\n" checkUninitVar2("void f() {\n"
" int x;\n" " int x;\n"