ValueFlow: Evaluate more calculations when operands have known values

This commit is contained in:
Daniel Marjamäki 2015-07-18 15:03:57 +02:00
parent 3daabbcc3b
commit 5aa0b284dc
2 changed files with 14 additions and 7 deletions

View File

@ -378,10 +378,14 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value)
// Calculations..
else if (parent->isArithmeticalOp() && parent->astOperand1() && parent->astOperand2()) {
const bool known = ((parent->astOperand1()->values.size() == 1U &&
parent->astOperand1()->values.front().valueKind == ValueFlow::Value::ValueKind::Known) ||
(parent->astOperand2()->values.size() == 1U &&
parent->astOperand2()->values.front().valueKind == ValueFlow::Value::ValueKind::Known));
std::list<ValueFlow::Value>::const_iterator value1, value2;
for (value1 = parent->astOperand1()->values.begin(); value1 != parent->astOperand1()->values.end(); ++value1) {
for (value2 = parent->astOperand2()->values.begin(); value2 != parent->astOperand2()->values.end(); ++value2) {
if (value1->varId == 0U || value2->varId == 0U ||
if (known || value1->varId == 0U || value2->varId == 0U ||
(value1->varId == value2->varId && value1->varvalue == value2->varvalue)) {
ValueFlow::Value result(0);
result.condition = value1->condition ? value1->condition : value2->condition;

View File

@ -218,12 +218,7 @@ private:
void valueFlowCalculations() {
const char *code;
/*
code = "void f() {\n"
" x = 123+456;\n"
"}";
ASSERT_EQUALS(579, valueOfTok(code, "+").intvalue);
*/
code = "void f(int x) {\n"
" a = x+456;\n"
" if (x==123) {}"
@ -252,6 +247,14 @@ private:
ASSERT_EQUALS(4, values.front().intvalue);
ASSERT_EQUALS(16, values.back().intvalue);
// addition of different variables with known values
code = "int f(int x) {\n"
" int a = 1;\n"
" while (x!=3) { x+=a; }\n"
" return x/a;\n"
"}\n";
ASSERT_EQUALS(3, valueOfTok(code, "/").intvalue);
// ? :
code = "x = y ? 2 : 3;\n";
values = tokenValues(code,"?");