Fixed #7350 (ValueFlow: Result of 'x & 0' is always 0)
This commit is contained in:
parent
5e10e680da
commit
a391bca060
|
@ -398,6 +398,13 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value)
|
||||||
parent->astOperand1()->values.front().isKnown()) ||
|
parent->astOperand1()->values.front().isKnown()) ||
|
||||||
(parent->astOperand2()->values.size() == 1U &&
|
(parent->astOperand2()->values.size() == 1U &&
|
||||||
parent->astOperand2()->values.front().isKnown()));
|
parent->astOperand2()->values.front().isKnown()));
|
||||||
|
|
||||||
|
// known result when a operand is 0.
|
||||||
|
if (Token::Match(parent, "[&*]") && value.isKnown() && value.tokvalue==nullptr && value.intvalue==0) {
|
||||||
|
setTokenValue(parent, value);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
std::list<ValueFlow::Value>::const_iterator value1, value2;
|
std::list<ValueFlow::Value>::const_iterator value1, value2;
|
||||||
for (value1 = parent->astOperand1()->values.begin(); value1 != parent->astOperand1()->values.end(); ++value1) {
|
for (value1 = parent->astOperand1()->values.begin(); value1 != parent->astOperand1()->values.end(); ++value1) {
|
||||||
if (value1->tokvalue && (!parent->isComparisonOp() || value1->tokvalue->tokType() != Token::eString))
|
if (value1->tokvalue && (!parent->isComparisonOp() || value1->tokvalue->tokType() != Token::eString))
|
||||||
|
|
|
@ -1883,6 +1883,12 @@ private:
|
||||||
"}";
|
"}";
|
||||||
ASSERT_EQUALS(true, testValueOfX(code, 3U, 1)); // value of x can be 1
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 1)); // value of x can be 1
|
||||||
ASSERT_EQUALS(false, testValueOfX(code, 3U, 2)); // value of x can't be 2
|
ASSERT_EQUALS(false, testValueOfX(code, 3U, 2)); // value of x can't be 2
|
||||||
|
|
||||||
|
// calculation with known result
|
||||||
|
code = "int f(int x) { a = x & 0; }"; // <- & is 0
|
||||||
|
value = valueOfTok(code, "&");
|
||||||
|
ASSERT_EQUALS(0, value.intvalue);
|
||||||
|
ASSERT(value.isKnown());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue