value flow: if unsigned variable is compared against 1 with either >= or <= then assume it can have the value 0
This commit is contained in:
parent
1e3c43e708
commit
6d22c9deaa
|
@ -86,6 +86,9 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
|
|||
}
|
||||
|
||||
const ValueFlow::Value val(tok, num);
|
||||
ValueFlow::Value val2;
|
||||
if (num==1 && var->typeStartToken()->isUnsigned() && Token::Match(tok,"<=|>="))
|
||||
val2 = ValueFlow::Value(tok,0);
|
||||
|
||||
for (Token *tok2 = tok->previous(); ; tok2 = tok2->previous()) {
|
||||
if (!tok2) {
|
||||
|
@ -117,6 +120,8 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
|
|||
}
|
||||
|
||||
tok2->values.push_back(val);
|
||||
if (val2.condition)
|
||||
tok2->values.push_back(val2);
|
||||
if (var && tok2 == var->nameToken())
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -210,6 +210,7 @@ private:
|
|||
settings->inconclusive = inconclusive;
|
||||
settings->experimental = experimental;
|
||||
settings->standards.posix = posix;
|
||||
settings->valueFlow = true;
|
||||
|
||||
if (posix) {
|
||||
const char cfg[] = "<?xml version=\"1.0\"?>\n"
|
||||
|
|
|
@ -84,6 +84,13 @@ private:
|
|||
"}";
|
||||
ASSERT_EQUALS(true, testValueOfX(code, 2U, 123));
|
||||
|
||||
code = "void f(unsigned int x) {\n"
|
||||
" int a = x;\n"
|
||||
" if (x >= 1) {}\n"
|
||||
"}";
|
||||
ASSERT_EQUALS(true, testValueOfX(code, 2U, 1));
|
||||
ASSERT_EQUALS(true, testValueOfX(code, 2U, 0));
|
||||
|
||||
// bailout: ?:
|
||||
bailout("void f(int x) {\n"
|
||||
" y = ((x<0) ? x : ((x==2)?3:4));\n"
|
||||
|
|
Loading…
Reference in New Issue