Skip to content

Commit

Permalink
Refactorization: Make use of do-loops to avoid redundant Token::Match…
Browse files Browse the repository at this point in the history
…() calls
  • Loading branch information
PKEuS committed May 13, 2015
1 parent b946b74 commit 3ce4e68
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/checkbufferoverrun.cpp
Expand Up @@ -683,14 +683,14 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
std::size_t charactersAppend = 0;
const Token *tok2 = tok;

while (Token::Match(tok2, strcatPattern.c_str(), declarationId)) {
do {
charactersAppend += Token::getStrLength(tok2->tokAt(4 + varcount));
if (charactersAppend >= static_cast<std::size_t>(total_size)) {
bufferOverrunError(tok2);
break;
}
tok2 = tok2->tokAt(7 + varcount);
}
} while (Token::Match(tok2, strcatPattern.c_str(), declarationId));
}

// Check function call..
Expand Down
3 changes: 2 additions & 1 deletion lib/checkuninitvar.cpp
Expand Up @@ -1704,8 +1704,9 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al
// - should only return false if struct member is (or might be) array.
// - should only return false if function argument is (or might be) non-const pointer or reference
const Token *tok2 = vartok->next();
while (Token::Match(tok2,". %name%"))
do {
tok2 = tok2->tokAt(2);
} while (Token::Match(tok2, ". %name%"));
if (Token::Match(tok2, "[,)]"))
return false;
} else if (pointer && alloc != CTOR_CALL && Token::Match(vartok, "%name% . %name% (")) {
Expand Down
19 changes: 10 additions & 9 deletions lib/tokenize.cpp
Expand Up @@ -2855,12 +2855,12 @@ void Tokenizer::setVarId()
// constructor with initializer list
if (Token::Match(tok2, ") : %name% (")) {
Token *tok3 = tok2;
while (Token::Match(tok3, ") [:,] %name% (")) {
do {
Token *vartok = tok3->tokAt(2);
if (varlist[classname].find(vartok->str()) != varlist[classname].end())
vartok->varId(varlist[classname][vartok->str()]);
tok3 = tok3->linkAt(3);
}
} while (Token::Match(tok3, ") [:,] %name% ("));
if (Token::simpleMatch(tok3, ") {")) {
setVarIdClassFunction(classname, tok2, tok3->next()->link(), varlist[classname], &structMembers, &_varId);
}
Expand Down Expand Up @@ -3022,10 +3022,10 @@ bool Tokenizer::simplifySizeof()
continue;

Token* tok2 = tok->next();
while (Token::Match(tok2, "[ %num% ]")) {
do {
size *= static_cast<unsigned int>(MathLib::toLongNumber(tok2->strAt(1)));
tok2 = tok2->tokAt(3);
}
} while (Token::Match(tok2, "[ %num% ]"));
if (Token::Match(tok2, "[;=]")) {
sizeOfVar[varId] = size;
declTokOfVar[varId] = tok;
Expand Down Expand Up @@ -6456,8 +6456,9 @@ bool Tokenizer::simplifyKnownVariables()
// skip loop variable
if (Token::Match(tok2->tokAt(-2), "(|:: %type%")) {
const Token *tok3 = tok2->previous();
while (Token::Match(tok3->previous(), ":: %type%"))
do {
tok3 = tok3->tokAt(-2);
} while (Token::Match(tok3->previous(), ":: %type%"));
if (Token::Match(tok3->tokAt(-2), "for ( %type%"))
continue;
}
Expand Down Expand Up @@ -7302,9 +7303,8 @@ void Tokenizer::simplifyNestedStrcat()

// find inner strcat call
Token *tok2 = tok->tokAt(3);
while (Token::simpleMatch(tok2, "strcat ( strcat")) {
while (Token::simpleMatch(tok2, "strcat ( strcat"))
tok2 = tok2->tokAt(2);
}

if (tok2->strAt(3) != ",")
continue;
Expand Down Expand Up @@ -7748,9 +7748,10 @@ void Tokenizer::simplifyEnum()
if (Token::Match(arg->previous(), "%type%|*|& %type% [,)=]") &&
enumValues.find(arg->str()) != enumValues.end()) {
// is this a variable declaration
const Token *prev = arg;
while (Token::Match(prev,"%type%|*|&"))
const Token *prev = arg->previous();
do {
prev = prev->previous();
} while (Token::Match(prev, "%type%|*|&"));
if (!Token::Match(prev,"[,(] %type%"))
continue;
if (prev->str() == "(" && (!Token::Match(prev->tokAt(-2), "%type%|::|*|& %type% (") || prev->strAt(-2) == "else"))
Expand Down
3 changes: 2 additions & 1 deletion lib/valueflow.cpp
Expand Up @@ -1046,8 +1046,9 @@ static bool valueFlowForward(Token * const startToken,
// bailout: possible assignment using >>
if (Token::Match(tok2->previous(), ">> %name% >>|;")) {
const Token *parent = tok2->previous();
while (Token::simpleMatch(parent,">>"))
do {
parent = parent->astParent();
} while (Token::simpleMatch(parent, ">>"));
if (!parent) {
if (settings->debugwarnings)
bailout(tokenlist, errorLogger, tok2, "Possible assignment of " + tok2->str() + " using >>");
Expand Down

0 comments on commit 3ce4e68

Please sign in to comment.