ValueFlow: improve handling of unary minus
This commit is contained in:
parent
f2e49b4db4
commit
62362cd06d
|
@ -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
|
||||
else if (parent->str() == "[" && parent->astOperand1() && parent->astOperand2()) {
|
||||
std::list<ValueFlow::Value>::const_iterator value1, value2;
|
||||
|
|
|
@ -336,6 +336,15 @@ private:
|
|||
ASSERT_EQUALS(1U, values.size());
|
||||
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
|
||||
code = "void f(int x) {\n"
|
||||
" a = x + 8;\n"
|
||||
|
|
Loading…
Reference in New Issue