Fix issue 9898: false positive: knownConditionTrueFalse (#2806)

This commit is contained in:
Paul Fultz II 2020-09-14 11:43:11 -05:00 committed by GitHub
parent 566e74fae0
commit a42976d656
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -313,11 +313,13 @@ struct ForwardTraversal {
if (!scope)
return Progress::Break;
if (Token::Match(tok->link()->previous(), ")|else {")) {
const bool inElse = Token::simpleMatch(tok->link()->previous(), "else {");
const Token* tok2 = tok->link()->previous();
const bool inElse = Token::simpleMatch(tok2, "else {");
const bool inLoop = inElse ? false : Token::Match(tok2->link()->previous(), "while|for (");
Token* condTok = getCondTokFromEnd(tok);
if (!condTok)
return Progress::Break;
if (!condTok->hasKnownIntValue()) {
if (!condTok->hasKnownIntValue() || inLoop) {
if (!analyzer->lowerToPossible())
return Progress::Break;
} else if (condTok->values().front().intvalue == inElse) {

View File

@ -2479,6 +2479,19 @@ private:
"}\n";
ASSERT_EQUALS(false, testValueOfXImpossible(code, 8U, 0));
code = "int foo(int n) {\n"
" if( n>= 8 ) {\n"
" while(true) {\n"
" n -= 8;\n"
" if( n < 8 )\n"
" break;\n"
" }\n"
" int x = n == 0;\n"
" return x;\n"
" }\n"
"}\n";
ASSERT_EQUALS(false, testValueOfXKnown(code, 9U, 0));
code = "bool c();\n"
"long f() {\n"
" bool stop = false;\n"