ValueFlow: better handling of ? when condition result is known
This commit is contained in:
parent
453b5577cd
commit
1f98af654a
|
@ -363,6 +363,14 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value)
|
|||
}
|
||||
|
||||
else if (parent->str() == "?" && tok->str() == ":" && tok == parent->astOperand2()) {
|
||||
// is condition always true/false?
|
||||
if (parent->astOperand1()->values.size() == 1U && parent->astOperand1()->values.front().isKnown()) {
|
||||
const ValueFlow::Value &condvalue = parent->astOperand1()->values.front();
|
||||
const bool cond(condvalue.tokvalue || condvalue.intvalue != 0);
|
||||
const std::list<ValueFlow::Value> &values = cond ? tok->astOperand1()->values : tok->astOperand2()->values;
|
||||
if (std::find(values.begin(), values.end(), value) != values.end())
|
||||
setTokenValue(parent, value);
|
||||
} else {
|
||||
// is condition only depending on 1 variable?
|
||||
std::stack<const Token*> tokens;
|
||||
tokens.push(parent->astOperand1());
|
||||
|
@ -391,6 +399,7 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value)
|
|||
|
||||
setTokenValue(parent, v);
|
||||
}
|
||||
}
|
||||
|
||||
// Calculations..
|
||||
else if ((parent->isArithmeticalOp() || parent->isComparisonOp() || (parent->tokType() == Token::eBitOp)) &&
|
||||
|
|
|
@ -36,6 +36,18 @@ namespace ValueFlow {
|
|||
explicit Value(long long val = 0) : intvalue(val), tokvalue(nullptr), varvalue(val), condition(0), varId(0U), conditional(false), inconclusive(false), defaultArg(false), valueKind(ValueKind::Possible) {}
|
||||
Value(const Token *c, long long val) : intvalue(val), tokvalue(nullptr), varvalue(val), condition(c), varId(0U), conditional(false), inconclusive(false), defaultArg(false), valueKind(ValueKind::Possible) {}
|
||||
|
||||
bool operator==(const Value &rhs) const {
|
||||
return intvalue == rhs.intvalue &&
|
||||
tokvalue == rhs.tokvalue &&
|
||||
varvalue == rhs.varvalue &&
|
||||
condition == rhs.condition &&
|
||||
varId == rhs.varId &&
|
||||
conditional == rhs.conditional &&
|
||||
inconclusive == rhs.inconclusive &&
|
||||
defaultArg == rhs.defaultArg &&
|
||||
valueKind == rhs.valueKind;
|
||||
}
|
||||
|
||||
/** int value */
|
||||
long long intvalue;
|
||||
|
||||
|
|
|
@ -327,6 +327,11 @@ private:
|
|||
ASSERT_EQUALS(2, values.front().intvalue);
|
||||
ASSERT_EQUALS(3, values.back().intvalue);
|
||||
|
||||
code = "x = (2<5) ? 2 : 3;\n";
|
||||
values = tokenValues(code, "?");
|
||||
ASSERT_EQUALS(1U, values.size());
|
||||
ASSERT_EQUALS(2, values.front().intvalue);
|
||||
|
||||
// !
|
||||
code = "void f(int x) {\n"
|
||||
" a = !x;\n"
|
||||
|
|
Loading…
Reference in New Issue