ValueFlow: Refactoring. Created utility function isVariableChanged()

This commit is contained in:
Daniel Marjamäki 2014-04-20 14:21:43 +02:00
parent ea016d4d73
commit 5fc43f85b1
1 changed files with 21 additions and 15 deletions

View File

@ -158,6 +158,23 @@ static bool bailoutSelfAssignment(const Token * const tok)
return false; return false;
} }
static bool isVariableChanged(const Token *start, const Token *end, const unsigned int varid)
{
for (const Token *tok = start; tok != end; tok = tok->next()) {
if (tok->varId() == varid) {
if (Token::Match(tok, "%var% ="))
return true;
const Token *parent = tok->astParent();
while (parent && parent->str() == ".")
parent = parent->astParent();
if (Token::Match(parent, "++|--"))
return true;
}
}
return false;
}
/** set ValueFlow value and perform calculations if possible */ /** set ValueFlow value and perform calculations if possible */
static void setTokenValue(Token* tok, const ValueFlow::Value &value) static void setTokenValue(Token* tok, const ValueFlow::Value &value)
{ {
@ -316,8 +333,7 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
const Token * const start = tok2->link()->next(); const Token * const start = tok2->link()->next();
const Token * const end = start->link(); const Token * const end = start->link();
if (Token::findmatch(start,"++|-- %varid%",end,varid) || if (isVariableChanged(start,end,varid)) {
Token::findmatch(start,"%varid% ++|--|=",end,varid)) {
varid = 0U; varid = 0U;
if (settings->debugwarnings) if (settings->debugwarnings)
bailout(tokenlist, errorLogger, tok, "variable " + var->nameToken()->str() + " used in loop"); bailout(tokenlist, errorLogger, tok, "variable " + var->nameToken()->str() + " used in loop");
@ -445,7 +461,7 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
const Token *start = tok2; const Token *start = tok2;
const Token *end = start->link(); const Token *end = start->link();
if (Token::findmatch(start,"++|--| %varid% ++|--|=",end,varid)) { if (isVariableChanged(start,end,varid)) {
if (settings->debugwarnings) 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."); bailout(tokenlist, errorLogger, tok2, "variable " + var->nameToken()->str() + " is assigned in loop. so valueflow analysis bailout when start of loop is reached.");
break; break;
@ -554,8 +570,7 @@ static void valueFlowAfterAssign(TokenList *tokenlist, ErrorLogger *errorLogger,
break; break;
} }
if (Token::findmatch(start, "++|-- %varid%", end, varid) || if (isVariableChanged(start, end, varid)) {
Token::findmatch(start, "%varid% ++|--|=", end, varid)) {
if (number_of_if == 0 && if (number_of_if == 0 &&
Token::simpleMatch(tok2, "if (") && Token::simpleMatch(tok2, "if (") &&
!(Token::simpleMatch(end, "} else {") && !(Token::simpleMatch(end, "} else {") &&
@ -836,16 +851,7 @@ static void valueFlowForLoopSimplify(Token * const bodyStart, const unsigned int
const Token * const bodyEnd = bodyStart->link(); const Token * const bodyEnd = bodyStart->link();
// Is variable modified inside for loop // Is variable modified inside for loop
bool modified = false; if (isVariableChanged(bodyStart, bodyEnd, varid))
for (const Token *tok = bodyStart->next(); tok != bodyEnd; tok = tok->next()) {
if (tok->varId() == varid &&
(Token::Match(tok, "%varid% =|++|--", varid) ||
Token::Match(tok->previous(), "++|-- %varid%", varid))) {
modified = true;
break;
}
}
if (modified)
return; return;
for (Token *tok2 = bodyStart->next(); tok2 != bodyEnd; tok2 = tok2->next()) { for (Token *tok2 = bodyStart->next(); tok2 != bodyEnd; tok2 = tok2->next()) {