Fixed #6917 (False positive uninitvar - unused array declaration and ternary expression)

This commit is contained in:
Daniel Marjamäki 2015-08-29 13:19:28 +02:00
parent d7d11332cd
commit 6ddd0a953f
2 changed files with 20 additions and 12 deletions

View File

@ -220,7 +220,11 @@ static bool isVariableUsed(const Token *tok, const Variable& var)
return false;
if (tok->isConstOp())
return isVariableUsed(tok->astOperand1(),var) || isVariableUsed(tok->astOperand2(),var);
if (var.isArray()) {
if (tok->varId() != var.declarationId())
return false;
if (!var.isArray())
return true;
const Token *parent = tok->astParent();
while (Token::Match(parent, "[?:]"))
parent = parent->astParent();
@ -230,8 +234,6 @@ static bool isVariableUsed(const Token *tok, const Variable& var)
const Token *parent2 = parent->astParent();
// TODO: handle function calls. There is a TODO assertion in TestUninitVar::uninitvar_arrays
return !parent2 || parent2->isConstOp() || (parent2->str() == "=" && parent2->astOperand2() == parent);
}
return (tok->varId() == var.declarationId());
}
bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var, bool * const possibleInit, bool * const noreturn, Alloc* const alloc, const std::string &membervar)

View File

@ -1281,6 +1281,12 @@ private:
"}");
TODO_ASSERT_EQUALS("error", "", errout.str());
checkUninitVar("void f(int x) {\n"
" int a[2];\n"
" y *= (x ? 1 : 2);\n"
"}");
ASSERT_EQUALS("", errout.str());
// passing array to library functions
checkUninitVar("void f()\n"
"{\n"