ValueFlow: improve handling of unary minus

This commit is contained in:
Daniel Marjamäki 2016-08-14 22:19:06 +02:00
parent f2e49b4db4
commit 62362cd06d
2 changed files with 21 additions and 0 deletions

View File

@ -521,6 +521,18 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value)
} }
} }
// unary minus
else if (parent->str() == "-" && !parent->astOperand2()) {
std::list<ValueFlow::Value>::const_iterator it;
for (it = tok->values.begin(); it != tok->values.end(); ++it) {
if (it->tokvalue)
continue;
ValueFlow::Value v(*it);
v.intvalue = -v.intvalue;
setTokenValue(parent, v);
}
}
// Array element // Array element
else if (parent->str() == "[" && parent->astOperand1() && parent->astOperand2()) { else if (parent->str() == "[" && parent->astOperand1() && parent->astOperand2()) {
std::list<ValueFlow::Value>::const_iterator value1, value2; std::list<ValueFlow::Value>::const_iterator value1, value2;

View File

@ -336,6 +336,15 @@ private:
ASSERT_EQUALS(1U, values.size()); ASSERT_EQUALS(1U, values.size());
ASSERT_EQUALS(1, values.back().intvalue); ASSERT_EQUALS(1, values.back().intvalue);
// unary minus
code = "void f(int x) {\n"
" a = -x;\n"
" if (x==10) {}\n"
"}";
values = tokenValues(code,"-");
ASSERT_EQUALS(1U, values.size());
ASSERT_EQUALS(-10, values.back().intvalue);
// function call => calculation // function call => calculation
code = "void f(int x) {\n" code = "void f(int x) {\n"
" a = x + 8;\n" " a = x + 8;\n"