From 529b255e9901c9eaba7febfb7ef54a9eb79c0b98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 25 Aug 2017 23:07:26 +0200 Subject: [PATCH] ValueFlow: Better errorpath for increment/decrement --- lib/valueflow.cpp | 10 ++++++++-- test/testvalueflow.cpp | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 8433b993c..753a2f6b9 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -959,15 +959,21 @@ static void valueFlowReverse(TokenList *tokenlist, } // increment/decrement + int inc = 0; if (Token::Match(tok2->previous(), "[;{}] %name% ++|-- ;")) - val.intvalue += (tok2->strAt(1)=="++") ? -1 : 1; + inc = (tok2->strAt(1)=="++") ? -1 : 1; else if (Token::Match(tok2->tokAt(-2), "[;{}] ++|-- %name% ;")) - val.intvalue += (tok2->strAt(-1)=="++") ? -1 : 1; + inc = (tok2->strAt(-1)=="++") ? -1 : 1; else if (Token::Match(tok2->previous(), "++|-- %name%") || Token::Match(tok2, "%name% ++|--")) { if (settings->debugwarnings) bailout(tokenlist, errorLogger, tok2, "increment/decrement of " + tok2->str()); break; } + if (inc != 0) { + val.intvalue += inc; + const std::string info(tok2->str() + " is " + std::string(inc==1 ? "decremented" : "incremented") + ", before this " + (inc==1?"decrement":"increment") + " the value is " + val.infoString()); + val.errorPath.push_back(ErrorPathItem(tok2, info)); + } // compound assignment if (Token::Match(tok2->previous(), "[;{}] %var% %assign%") && tok2->next()->str() != "=") { diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index beb96ea80..d190aa083 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -730,6 +730,9 @@ private: " if (x == 4);\n" "}"; ASSERT_EQUALS(true, testValueOfX(code, 2U, 3)); + ASSERT_EQUALS("4,Assuming that condition 'x==4' is not redundant\n" + "3,x is incremented, before this increment the value is 3\n", + getErrorPathForX(code, 2U)); // compound assignment += , -= , ... code = "void f(int x) {\n"