Skip to content

Commit 3bc21b8

Browse files
zingsheimdanmar
authored andcommitted
Fixed cppcheck-opensource#6996 (ValueFlow: known and possible values)
1 parent f5bd00f commit 3bc21b8

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

‎lib/valueflow.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ static bool isVariableChanged(const Token *start, const Token *end, const unsign
358358
/** Add token value. Return true if value is added. */
359359
static bool addValue(Token *tok, const ValueFlow::Value &value)
360360
{
361+
if (value.isKnown()) {
362+
// Clear all other values since value is known
363+
tok->values.clear();
364+
}
365+
361366
// if value already exists, don't add it again
362367
std::list<ValueFlow::Value>::iterator it;
363368
for (it = tok->values.begin(); it != tok->values.end(); ++it) {

‎test/testvalueflow.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,15 @@ class TestValueFlow : public TestFixture {
17751775
value = valueOfTok(code, "+");
17761776
ASSERT_EQUALS(13, value.intvalue);
17771777
ASSERT(value.isPossible());
1778+
1779+
// known and possible value
1780+
code = "void f() {\n"
1781+
" int x = 1;\n"
1782+
" int y = 2 + x;\n" // <- known value, don't care about condition
1783+
" if (x == 2) {}\n"
1784+
"}";
1785+
ASSERT_EQUALS(true, testValueOfX(code, 3U, 1)); // value of x can be 1
1786+
ASSERT_EQUALS(false, testValueOfX(code, 3U, 2)); // value of x can't be 2
17781787
}
17791788
};
17801789

0 commit comments

Comments
 (0)