ValueFlow: improved analysis in for loops to avoid fp

This commit is contained in:
Daniel Marjamäki 2014-08-01 16:12:57 +02:00
parent fbf09f11a0
commit f908959196
2 changed files with 37 additions and 0 deletions

View File

@ -1208,6 +1208,23 @@ static void valueFlowForLoopSimplify(Token * const bodyStart, const unsigned int
setTokenValue(tok2, value1);
}
if (Token::Match(tok2, "%oror%|&&")) {
const std::map<unsigned int, MathLib::bigint> programMemory(getProgramMemory(tok2->astTop(), varid, value));
if ((tok2->str() == "&&" && conditionIsFalse(tok2->astOperand1(), programMemory)) ||
(tok2->str() == "||" && conditionIsTrue(tok2->astOperand1(), programMemory))) {
// Skip second expression..
const Token *parent = tok2;
while (parent && parent->str() == tok2->str())
parent = parent->astParent();
if (parent && parent->str() == "(")
tok2 = parent->link();
}
}
if ((tok2->str() == "&&" && conditionIsFalse(tok2->astOperand1(), getProgramMemory(tok2->astTop(), varid, value))) ||
(tok2->str() == "||" && conditionIsTrue(tok2->astOperand1(), getProgramMemory(tok2->astTop(), varid, value))))
break;
else if (Token::simpleMatch(tok2, ") {") && Token::findmatch(tok2->link(), "%varid%", tok2, varid)) {
if (Token::findmatch(tok2, "continue|break|return", tok2->linkAt(1), varid)) {
if (settings->debugwarnings)

View File

@ -962,6 +962,26 @@ private:
" }\n"
"}\n";
ASSERT_EQUALS(false, testValueOfX(code, 3U, 0));
// &&
code = "void foo() {\n"
" for (int x = 0; x < 10; x++) {\n"
" if (x > 1\n"
" && x) {}" // <- x is not 0
" }\n"
"}\n";
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 == 0\n"
" || x) {}" // <- x is not 0
" }\n"
"}\n";
ASSERT_EQUALS(false, testValueOfX(code, 4U, 0));
ASSERT_EQUALS(true, testValueOfX(code, 4U, 9));
}
void valueFlowSubFunction() {