Skip to content

Commit 62362cd

Browse files
committed
ValueFlow: improve handling of unary minus
1 parent f2e49b4 commit 62362cd

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

‎lib/valueflow.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,18 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value)
521521
}
522522
}
523523

524+
// unary minus
525+
else if (parent->str() == "-" && !parent->astOperand2()) {
526+
std::list<ValueFlow::Value>::const_iterator it;
527+
for (it = tok->values.begin(); it != tok->values.end(); ++it) {
528+
if (it->tokvalue)
529+
continue;
530+
ValueFlow::Value v(*it);
531+
v.intvalue = -v.intvalue;
532+
setTokenValue(parent, v);
533+
}
534+
}
535+
524536
// Array element
525537
else if (parent->str() == "[" && parent->astOperand1() && parent->astOperand2()) {
526538
std::list<ValueFlow::Value>::const_iterator value1, value2;

‎test/testvalueflow.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,15 @@ class TestValueFlow : public TestFixture {
336336
ASSERT_EQUALS(1U, values.size());
337337
ASSERT_EQUALS(1, values.back().intvalue);
338338

339+
// unary minus
340+
code = "void f(int x) {\n"
341+
" a = -x;\n"
342+
" if (x==10) {}\n"
343+
"}";
344+
values = tokenValues(code,"-");
345+
ASSERT_EQUALS(1U, values.size());
346+
ASSERT_EQUALS(-10, values.back().intvalue);
347+
339348
// function call => calculation
340349
code = "void f(int x) {\n"
341350
" a = x + 8;\n"

0 commit comments

Comments
 (0)