ExecutionPath: better handling of for/while/do/switch, it doesn't bail out everything

This commit is contained in:
Daniel Marjamäki 2010-01-10 22:05:51 +01:00
parent c5966bba59
commit 8be54b592d
2 changed files with 37 additions and 3 deletions

View File

@ -91,11 +91,36 @@ static const Token *checkExecutionPaths_(const Token *tok, std::list<ExecutionPa
}
}
// todo: handle for/while
// for/while/switch/do .. bail out
if (Token::Match(tok, "for|while|switch|do"))
{
ExecutionPath::bailOut(checks);
return 0;
// goto {
const Token *tok2 = tok->next();
if (tok2 && tok2->str() == "(")
tok2 = tok2->link();
if (tok2 && tok2->str() == ")")
tok2 = tok2->next();
if (!tok2 || tok2->str() != "{")
{
ExecutionPath::bailOut(checks);
return 0;
}
// skip { .. }
tok2 = tok2->link();
// if "do { .. } while ( .." , goto end of while..
if (Token::simpleMatch(tok, "do {") && Token::simpleMatch(tok2, "} while ("))
tok2 = tok2->tokAt(2)->link();
// bail out all variables used in this for/while/switch/do
for (; tok && tok != tok2; tok = tok->next())
{
if (tok->varId())
ExecutionPath::bailOutVar(checks, tok->varId());
}
continue;
}
// .. ) { ... } => bail out

View File

@ -1377,6 +1377,15 @@ private:
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: i\n", errout.str());
checkUninitVar("void f(int i)\n"
"{\n"
" int a;\n"
" while (i < 10)\n"
" i++;\n"
" a++;"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: a\n", errout.str());
// member variables..
checkUninitVar("class Fred\n"
"{\n"