Uninitialized variables: Fixed TODO assertion (related with ticket #3106)

This commit is contained in:
Daniel Marjamäki 2012-06-23 16:06:20 +02:00
parent d3ce3e78bd
commit b6f4dbd067
2 changed files with 20 additions and 13 deletions

View File

@ -430,10 +430,16 @@ private:
// is there assignment or ternary operator in the return statement? // is there assignment or ternary operator in the return statement?
bool assignment = false; bool assignment = false;
for (const Token *tok2 = tok.next(); tok2 && tok2->str() != ";"; tok2 = tok2->next()) { for (const Token *tok2 = tok.next(); tok2 && tok2->str() != ";"; tok2 = tok2->next()) {
if (tok2->str() == "=" || (!isC && tok2->str() == ">>") || tok2->str() == "?" || Token::Match(tok2, "(|, &")) { if (tok2->str() == "=" || (!isC && tok2->str() == ">>") || Token::Match(tok2, "(|, &")) {
assignment = true; assignment = true;
break; break;
} }
if (Token::Match(tok2, "[(,] &| %var% [,)]")) {
tok2 = tok2->next();
if (!tok2->isName())
tok2 = tok2->next();
ExecutionPath::bailOutVar(checks, tok2->varId());
}
} }
if (!assignment) { if (!assignment) {

View File

@ -575,19 +575,20 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: y\n", errout.str()); ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: y\n", errout.str());
// Ticket #3106 - False positive {
checkUninitVar("int f() {\n" // Ticket #3106 - False positive
" int i;\n" checkUninitVar("int f() {\n"
" return x(&i) ? i : 0;\n" " int i;\n"
"}\n"); " return x(&i) ? i : 0;\n"
ASSERT_EQUALS("", errout.str()); "}\n");
ASSERT_EQUALS("", errout.str());
// Ticket #3106 - False negative checkUninitVar("int f() {\n"
checkUninitVar("int f() {\n" " int i;\n"
" int i;\n" " return x() ? i : 0;\n"
" return x() ? i : 0;\n" "}\n");
"}\n"); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: i\n", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: i\n", "", errout.str()); }
// Ticket #3480 - Don't crash garbage code // Ticket #3480 - Don't crash garbage code
checkUninitVar("int f()\n" checkUninitVar("int f()\n"