ExecutionPath: better handling of for/while/do/switch, it doesn't bail out everything
This commit is contained in:
parent
c5966bba59
commit
8be54b592d
|
@ -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
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in New Issue