Merge pull request #2689 from Ken-Patrick/9769
9769: Improve value flow for ternary operator
This commit is contained in:
commit
8ec5605bac
|
@ -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() &&
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue