value flow: fixed fp when variable is used in for-loop condition

This commit is contained in:
Daniel Marjamäki 2014-01-14 17:57:50 +01:00
parent e45a2e2c61
commit c547c9a108
2 changed files with 22 additions and 3 deletions

View File

@ -162,8 +162,9 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
for (const Token *tok2 = tok; tok2; tok2 = tok2->previous()) { for (const Token *tok2 = tok; tok2; tok2 = tok2->previous()) {
if (tok2->str() == ")") if (tok2->str() == ")")
tok2 = tok2->link(); tok2 = tok2->link();
else if (tok2->str() == "(") {
if (Token::Match(tok2->previous(), "for|while (") && Token::Match(tok2->link(), ") {")) { else if (tok2->str() == "(" && Token::simpleMatch(tok2->link(), ") {")) {
if (Token::Match(tok2->previous(), "for|while (")) {
const Token *start = tok2->link()->next(); const Token *start = tok2->link()->next();
const Token *end = start->link(); const Token *end = start->link();
if (Token::findmatch(start,"++|--| %varid% ++|--|=",end,varid)) { if (Token::findmatch(start,"++|--| %varid% ++|--|=",end,varid)) {
@ -172,14 +173,19 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
bailout(tokenlist, errorLogger, tok, "variable " + var->nameToken()->str() + " used in loop"); bailout(tokenlist, errorLogger, tok, "variable " + var->nameToken()->str() + " used in loop");
} }
} }
// bailout..
// if,macro => bailout
else if (Token::simpleMatch(tok2->previous(), "if (") && tok2->previous()->isExpandedMacro()) { else if (Token::simpleMatch(tok2->previous(), "if (") && tok2->previous()->isExpandedMacro()) {
varid = 0U; varid = 0U;
if (settings->debugwarnings) if (settings->debugwarnings)
bailout(tokenlist, errorLogger, tok, "variable " + var->nameToken()->str() + ", condition is defined in macro"); bailout(tokenlist, errorLogger, tok, "variable " + var->nameToken()->str() + ", condition is defined in macro");
} }
break; break;
} }
else if (Token::Match(tok2, "[{}]"))
break;
} }
if (varid == 0U) if (varid == 0U)
continue; continue;

View File

@ -177,6 +177,19 @@ private:
"}"; "}";
ASSERT_EQUALS(false, testValueOfX(code, 2U, 37)); ASSERT_EQUALS(false, testValueOfX(code, 2U, 37));
code = "void f(menu *x) {\n"
" a = x->parent;\n"
" for (i=0;(i<10) && (x!=0); i++) { x = x->next; }\n"
"}";
ASSERT_EQUALS(false, testValueOfX(code, 2U, 0));
code = "void f() {\n" // loop condition, x is assigned inside loop => dont use condition
" vimmenu_T *x = pMenu->parent;\n"
" for (index = 1; (index != itemIndex) && (pMenu != NULL); index++)\n"
" pMenu = pMenu->next;\n"
"}";
ASSERT_EQUALS(false, testValueOfX(code, 2U, 0));
code = "void f(int x) {\n" // condition inside loop, x is NOT assigned inside loop => use condition code = "void f(int x) {\n" // condition inside loop, x is NOT assigned inside loop => use condition
" a = x;\n" " a = x;\n"
" do {\n" " do {\n"