From 62362cd06d42e464e571434cc0939938df9e4e56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 14 Aug 2016 22:19:06 +0200 Subject: [PATCH] ValueFlow: improve handling of unary minus --- lib/valueflow.cpp | 12 ++++++++++++ test/testvalueflow.cpp | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 3e7b243b5..064a110df 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -521,6 +521,18 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value) } } + // unary minus + else if (parent->str() == "-" && !parent->astOperand2()) { + std::list::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::const_iterator value1, value2; diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 18e9f6f3c..9d85f9a37 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -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"