value flow: condition in loop, bailout valueflow analysis before loop if there is assignment inside the loop

This commit is contained in:
Daniel Marjamäki 2014-01-12 11:58:10 +01:00
parent e3496080c8
commit d8262963d9
2 changed files with 43 additions and 10 deletions

View File

@ -136,11 +136,11 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
continue; continue;
} }
if (varid == 0U) if (varid == 0U || !var)
continue; continue;
// bailout: global non-const variables // bailout: global non-const variables
if (var && var->isGlobal() && !var->isConst()) { if (var->isGlobal() && !var->isConst()) {
if (settings->debugwarnings) if (settings->debugwarnings)
bailout(tokenlist, errorLogger, tok, "global variable " + var->nameToken()->str()); bailout(tokenlist, errorLogger, tok, "global variable " + var->nameToken()->str());
continue; continue;
@ -239,12 +239,29 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
} else { } else {
tok2 = tok2->link(); tok2 = tok2->link();
} }
} else if (var && var->isGlobal() && tok2->str() == "{") { } else if (tok2->str() == "{") {
if (!Token::Match(tok2->previous(), ")|else {")) // if variable is assigned in loop don't look before the loop
break; if (tok2->previous() &&
if (Token::Match(tok2->previous(), ") {") && (Token::Match(tok2->previous(), "do") ||
!Token::Match(tok2->linkAt(-1)->previous(), "if|for|while (")) (tok2->str() == ")" && Token::Match(tok2->linkAt(-1)->previous(), "for|while (")))) {
break;
const Token *start = tok2;
const Token *end = start->link();
if (Token::findmatch(start,"++|--| %varid% ++|--|=",end,varid)) {
if (settings->debugwarnings)
bailout(tokenlist, errorLogger, tok2, "variable " + var->nameToken()->str() + " is assigned in loop. so valueflow analysis bailout when start of loop is reached.");
break;
}
}
// Global variable : stop when leaving the function scope
if (var->isGlobal()) {
if (!Token::Match(tok2->previous(), ")|else {"))
break;
if (Token::Match(tok2->previous(), ") {") &&
!Token::Match(tok2->linkAt(-1)->previous(), "if|for|while ("))
break;
}
} else if (tok2->str() == "break") { } else if (tok2->str() == "break") {
if (settings->debugwarnings) if (settings->debugwarnings)
bailout(tokenlist, errorLogger, tok2, "variable " + var->nameToken()->str() + " stopping on break"); bailout(tokenlist, errorLogger, tok2, "variable " + var->nameToken()->str() + " stopping on break");

View File

@ -144,18 +144,34 @@ private:
ASSERT_EQUALS(false, testValueOfX(code, 2U, 1)); ASSERT_EQUALS(false, testValueOfX(code, 2U, 1));
// while, for, do-while // while, for, do-while
code = "void f(int x) {\n" code = "void f(int x) {\n" // loop condition, x is not assigned inside loop => use condition
" a = x;\n" // x can be 37 " a = x;\n" // x can be 37
" while (x == 37) {}\n" " while (x == 37) {}\n"
"}"; "}";
ASSERT_EQUALS(true, testValueOfX(code, 2U, 37)); ASSERT_EQUALS(true, testValueOfX(code, 2U, 37));
code = "void f(int x) {\n" code = "void f(int x) {\n" // loop condition, x is assigned inside loop => dont use condition
" a = x;\n" // don't assume that x can be 37 " a = x;\n" // don't assume that x can be 37
" while (x != 37) { x++; }\n" " while (x != 37) { x++; }\n"
"}"; "}";
ASSERT_EQUALS(false, testValueOfX(code, 2U, 37)); ASSERT_EQUALS(false, testValueOfX(code, 2U, 37));
code = "void f(int x) {\n" // condition inside loop, x is NOT assigned inside loop => use condition
" a = x;\n"
" do {\n"
" if (x==76) {}\n"
" } while (1);\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 2U, 76));
code = "void f(int x) {\n" // conditions inside loop, x is assigned inside do-while => dont use condition
" a = x;\n"
" do {\n"
" if (x!=76) { x=do_something(); }\n"
" } while (1);\n"
"}";
ASSERT_EQUALS(false, testValueOfX(code, 2U, 76));
// bailout: ?: // bailout: ?:
bailout("void f(int x) {\n" bailout("void f(int x) {\n"
" y = ((x<0) ? x : ((x==2)?3:4));\n" " y = ((x<0) ? x : ((x==2)?3:4));\n"