Fixed #7350 (ValueFlow: Result of 'x & 0' is always 0)

This commit is contained in:
Daniel Marjamäki 2016-01-26 10:07:42 +01:00
parent 5e10e680da
commit a391bca060
2 changed files with 13 additions and 0 deletions

View File

@ -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))

View File

@ -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());
} }
}; };