value flow: improved handling of loops

This commit is contained in:
Daniel Marjamäki 2014-01-18 11:54:00 +01:00
parent 498cdb8610
commit c7032a8867
2 changed files with 10 additions and 1 deletions

View File

@ -276,7 +276,7 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
// if variable is assigned in loop don't look before the loop // if variable is assigned in loop don't look before the loop
if (tok2->previous() && if (tok2->previous() &&
(Token::Match(tok2->previous(), "do") || (Token::Match(tok2->previous(), "do") ||
(tok2->str() == ")" && Token::Match(tok2->linkAt(-1)->previous(), "for|while (")))) { (tok2->strAt(-1) == ")" && Token::Match(tok2->linkAt(-1)->previous(), "for|while (")))) {
const Token *start = tok2; const Token *start = tok2;
const Token *end = start->link(); const Token *end = start->link();

View File

@ -240,6 +240,15 @@ private:
" } while (1);\n" " } while (1);\n"
"}"; "}";
ASSERT_EQUALS(false, testValueOfX(code, 2U, 76)); ASSERT_EQUALS(false, testValueOfX(code, 2U, 76));
code = "void f(X x) {\n" // conditions inside loop, x is assigned inside do-while => dont use condition
" a = x;\n"
" for (i=1;i<=count;i++) {\n"
" BUGON(x==0)\n"
" x = x.next;\n"
" }\n"
"}";
ASSERT_EQUALS(false, testValueOfX(code, 2U, 0));
} }
void valueFlowBeforeConditionTernaryOp() { // bailout: ?: void valueFlowBeforeConditionTernaryOp() { // bailout: ?: