Refactoring checking for unused variables (avoid FN when not taking address)

This commit is contained in:
Daniel Marjamäki 2017-07-01 22:45:51 +02:00
parent d6f066482e
commit 211d8c6020
2 changed files with 9 additions and 6 deletions

View File

@ -691,7 +691,10 @@ static void useFunctionArgs(const Token *tok, Variables& variables)
// TODO: Match function args to see if they are const or not. Assume that const data is not written.
if (!tok)
return;
if (Token::Match(tok, "[,+?:]")) {
if (tok->str() == ",") {
useFunctionArgs(tok->astOperand1(), variables);
useFunctionArgs(tok->astOperand2(), variables);
} else if (Token::Match(tok, "[+:]") && (!tok->valueType() || tok->valueType()->pointer)) {
useFunctionArgs(tok->astOperand1(), variables);
useFunctionArgs(tok->astOperand2(), variables);
} else if (tok->variable() && tok->variable()->isArray()) {
@ -1147,10 +1150,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
variables.use(tok->varId(), tok); // use = read + write
}
else if (Token::Match(tok, "[?:]")) {
// TODO: This is to avoid FP for 'p = c ? buf1 : buf2;'.
// Check if the address is taken. If not this is just a read.
// maybe handle struct members better
else if (tok->str() == ":" && (!tok->valueType() || tok->valueType()->pointer)) {
if (tok->astOperand1())
variables.use(tok->astOperand1()->varId(), tok->astOperand1());
if (tok->astOperand2())

View File

@ -4795,8 +4795,11 @@ void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype)
return;
bool ternary = parent->str() == ":" && parent->astParent() && parent->astParent()->str() == "?";
if (ternary)
if (ternary) {
if (vt1->pointer == vt2->pointer && vt1->type == vt2->type && vt1->sign == vt2->sign)
setValueType(parent, *vt2);
parent = const_cast<Token*>(parent->astParent());
}
if (ternary || parent->isArithmeticalOp() || parent->tokType() == Token::eIncDecOp) {
if (vt1->pointer != 0U && vt2 && vt2->pointer == 0U) {