Fixed #1824 (false positive: unitialised variable)

This commit is contained in:
Daniel Marjamäki 2010-07-06 13:18:28 +02:00
parent 1799e4a575
commit 1b20f8d27d
2 changed files with 38 additions and 16 deletions

View File

@ -42,11 +42,14 @@ bool ExecutionPath::parseCondition(const Token &tok, std::list<ExecutionPath *>
break; break;
--parlevel; --parlevel;
} }
else if (Token::Match(tok2, ";{}")) else if (Token::Match(tok2, "[;{}]"))
break; break;
if (tok2->varId() != 0) if (tok2->varId() != 0)
{ {
bailOutVar(checks, tok2->varId()); bailOutVar(checks, tok2->varId());
}
}
for (std::list<ExecutionPath *>::iterator it = checks.begin(); it != checks.end();) for (std::list<ExecutionPath *>::iterator it = checks.begin(); it != checks.end();)
{ {
if ((*it)->varId > 0 && (*it)->numberOfIf >= 1) if ((*it)->varId > 0 && (*it)->numberOfIf >= 1)
@ -59,8 +62,7 @@ bool ExecutionPath::parseCondition(const Token &tok, std::list<ExecutionPath *>
++it; ++it;
} }
} }
}
}
return false; return false;
} }
@ -103,6 +105,7 @@ static void parseIfSwitchBody(const Token * const tok,
std::list<ExecutionPath *>::const_iterator it; std::list<ExecutionPath *>::const_iterator it;
for (it = checks.begin(); it != checks.end(); ++it) for (it = checks.begin(); it != checks.end(); ++it)
{ {
if ((*it)->numberOfIf == 0)
c.push_back((*it)->copy()); c.push_back((*it)->copy());
if ((*it)->varId != 0) if ((*it)->varId != 0)
countif2.insert((*it)->varId); countif2.insert((*it)->varId);

View File

@ -1174,6 +1174,7 @@ private:
Tokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
// Clear the error buffer.. // Clear the error buffer..
errout.str(""); errout.str("");
@ -1351,10 +1352,10 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void a()\n" checkUninitVar("int a()\n"
"{\n" "{\n"
" int x;\n" " int x;\n"
" int y = x;\n" " return x;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
@ -1617,6 +1618,24 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void foo(int a)\n"
"{\n"
" int n;\n"
" int condition;\n"
" if(a == 1) {\n"
" n=0;\n"
" condition=0;\n"
" }\n"
" else {\n"
" n=1;\n"
" }\n"
"\n"
" if( n == 0) {\n"
" a=condition;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void f()\n" checkUninitVar("void f()\n"
"{\n" "{\n"
" C *c;\n" " C *c;\n"