ValueFlow: Better handling of && and || in for loop to avoid FP

This commit is contained in:
Daniel Marjamäki 2016-12-11 21:19:24 +01:00
parent d4f2512421
commit 31337dda27
2 changed files with 10 additions and 2 deletions

View File

@ -2301,8 +2301,8 @@ static void valueFlowForLoopSimplify(Token * const bodyStart, const unsigned int
if (Token::Match(tok2, "%oror%|&&")) {
const ProgramMemory programMemory(getProgramMemory(tok2->astTop(), varid, ValueFlow::Value(value)));
if ((tok2->str() == "&&" && conditionIsFalse(tok2->astOperand1(), programMemory)) ||
(tok2->str() == "||" && conditionIsTrue(tok2->astOperand1(), programMemory))) {
if ((tok2->str() == "&&" && !conditionIsTrue(tok2->astOperand1(), programMemory)) ||
(tok2->str() == "||" && !conditionIsFalse(tok2->astOperand1(), programMemory))) {
// Skip second expression..
const Token *parent = tok2;
while (parent && parent->str() == tok2->str())

View File

@ -1731,6 +1731,14 @@ private:
ASSERT_EQUALS(false, testValueOfX(code, 4U, 0));
ASSERT_EQUALS(true, testValueOfX(code, 4U, 9));
code = "void foo() {\n"
" for (int x = 0; x < 10; x++) {\n"
" if (x < value\n"
" && x) {}" // <- maybe x is not 9
" }\n"
"}\n";
ASSERT_EQUALS(false, testValueOfX(code, 4U, 9));
// ||
code = "void foo() {\n"
" for (int x = 0; x < 10; x++) {\n"