Uninitialized variables: handle unsimplified labels better

This commit is contained in:
Daniel Marjamäki 2012-12-25 10:37:21 +01:00
parent db2a129557
commit 299c1bb208
2 changed files with 32 additions and 7 deletions

View File

@ -1097,8 +1097,6 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
if (possibleInit)
*possibleInit = false;
bool ret = false;
unsigned int number_of_if = 0;
// variable values
@ -1292,10 +1290,24 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
if (Token::Match(tok, "return|break|continue|throw|goto")) {
if (noreturn)
*noreturn = true;
else
ret = true;
} else if (tok->str() == "goto")
return true;
while (tok && tok->str() != ";") {
// variable is seen..
if (tok->varId() == var.varId()) {
// Use variable
if (!suppressErrors && isVariableUsage(scope, tok, var.isPointer()))
uninitvarError(tok, tok->str());
else
// assume that variable is assigned
return true;
}
tok = tok->next();
}
return bool(noreturn==NULL);
}
// variable is seen..
if (tok->varId() == var.varId()) {
@ -1309,7 +1321,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
}
}
return ret;
return false;
}
bool CheckUninitVar::checkIfForWhileHead(const Scope *scope, const Token *startparanthesis, const Variable& var, bool suppressErrors, bool isuninit)

View File

@ -2106,6 +2106,19 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar2("int f(int x) {\n"
" int ret;\n"
" if (!x) {\n"
" ret = -123;\n"
" goto out1;\n"
" }\n"
" return 0;\n"
"out1:\n"
"out2:\n"
" return ret;\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar2("void f() {\n"
" int i;\n"
" if (x) {\n"