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,18 +220,20 @@ 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()) {
const Token *parent = tok->astParent();
while (Token::Match(parent, "[?:]"))
parent = parent->astParent();
// no dereference, then array is not "used"
if (!Token::Match(parent, "*|["))
return false;
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());
if (tok->varId() != var.declarationId())
return false;
if (!var.isArray())
return true;
const Token *parent = tok->astParent();
while (Token::Match(parent, "[?:]"))
parent = parent->astParent();
// no dereference, then array is not "used"
if (!Token::Match(parent, "*|["))
return false;
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);
}
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"