Uninitialized variables: better handling of initialization with >>. Ticket: #3369

This commit is contained in:
Daniel Marjamäki 2011-12-15 16:55:55 +01:00
parent b3c35d4b32
commit 3a432fa959
2 changed files with 21 additions and 6 deletions

View File

@ -1130,10 +1130,6 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int
// variable is seen.. // variable is seen..
if (tok->varId() == varid) { if (tok->varId() == varid) {
// Assign variable
if (tok->previous()->str() == ">>" || tok->next()->str() == "=")
return true;
// Use variable // Use variable
if (isVariableUsage(tok)) if (isVariableUsage(tok))
uninitvarError(tok, tok->str()); uninitvarError(tok, tok->str());
@ -1156,6 +1152,11 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok) const
return true; return true;
if (Token::Match(vartok->previous(), "++|--|%op%")) { if (Token::Match(vartok->previous(), "++|--|%op%")) {
if (vartok->previous()->str() == ">>" && _tokenizer->isCPP()) {
// assume that variable is initialized
return false;
}
if (vartok->previous()->str() != "&" || !Token::Match(vartok->tokAt(-2), "[(,]")) { if (vartok->previous()->str() != "&" || !Token::Match(vartok->tokAt(-2), "[(,]")) {
return true; return true;
} }

View File

@ -1682,7 +1682,7 @@ private:
/** New checking that doesn't rely on ExecutionPath */ /** New checking that doesn't rely on ExecutionPath */
void checkUninitVar2(const char code[]) { void checkUninitVar2(const char code[], const char fname[] = "test.cpp") {
// Clear the error buffer.. // Clear the error buffer..
errout.str(""); errout.str("");
@ -1691,7 +1691,7 @@ private:
// Tokenize.. // Tokenize..
Tokenizer tokenizer(&settings, this); Tokenizer tokenizer(&settings, this);
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, fname);
tokenizer.simplifyTokenList(); tokenizer.simplifyTokenList();
// Check for redundant code.. // Check for redundant code..
@ -1732,6 +1732,20 @@ 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());
// >> => initialization / usage
{
const char code[] = "void f() {\n"
" int x;\n"
" if (i >> x) { }\n"
"}";
checkUninitVar2(code, "test.cpp");
ASSERT_EQUALS("", errout.str());
checkUninitVar2(code, "test.c");
ASSERT_EQUALS("[test.c: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"