From 3bc21b8138b9cba00a2a8d625007f164fc816eac Mon Sep 17 00:00:00 2001 From: Frank Zingsheim Date: Sun, 4 Oct 2015 10:33:43 +0200 Subject: [PATCH] Fixed #6996 (ValueFlow: known and possible values) --- lib/valueflow.cpp | 5 +++++ test/testvalueflow.cpp | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index c1ce9357d..298d6318e 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -358,6 +358,11 @@ static bool isVariableChanged(const Token *start, const Token *end, const unsign /** Add token value. Return true if value is added. */ static bool addValue(Token *tok, const ValueFlow::Value &value) { + if (value.isKnown()) { + // Clear all other values since value is known + tok->values.clear(); + } + // if value already exists, don't add it again std::list::iterator it; for (it = tok->values.begin(); it != tok->values.end(); ++it) { diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index c86effbb9..164d08d00 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1775,6 +1775,15 @@ private: value = valueOfTok(code, "+"); ASSERT_EQUALS(13, value.intvalue); ASSERT(value.isPossible()); + + // known and possible value + code = "void f() {\n" + " int x = 1;\n" + " int y = 2 + x;\n" // <- known value, don't care about condition + " if (x == 2) {}\n" + "}"; + ASSERT_EQUALS(true, testValueOfX(code, 3U, 1)); // value of x can be 1 + ASSERT_EQUALS(false, testValueOfX(code, 3U, 2)); // value of x can't be 2 } };