Merge pull request #2689 from Ken-Patrick/9769

9769: Improve value flow for ternary operator
This commit is contained in:
Daniel Marjamäki 2020-06-21 13:04:43 +02:00 committed by GitHub
commit 8ec5605bac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -524,6 +524,16 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value, const Setti
}
}
else if (parent->str() == "?" && value.isIntValue() && tok == parent->astOperand1() && value.isKnown() &&
parent->astOperand2() && parent->astOperand2()->astOperand1() && parent->astOperand2()->astOperand2()) {
const std::list<ValueFlow::Value> &values = (value.intvalue == 0
? parent->astOperand2()->astOperand2()->values()
: parent->astOperand2()->astOperand1()->values());
for (const ValueFlow::Value &v : values)
setTokenValue(parent, v, settings);
}
// Calculations..
else if ((parent->isArithmeticalOp() || parent->isComparisonOp() || (parent->tokType() == Token::eBitOp) || (parent->tokType() == Token::eLogicalOp)) &&
parent->astOperand1() &&

View File

@ -674,6 +674,13 @@ private:
ASSERT_EQUALS(1U, values.size());
ASSERT_EQUALS(123, values.empty() ? 0 : values.front().intvalue);
code = "int f() {\n"
" const int i = 1;\n"
" int x = i < 0 ? 0 : 1;\n"
" return x;\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 4U, 1));
// ~
code = "x = ~0U;";
settings.platform(cppcheck::Platform::Native); // ensure platform is native