Refactorization: Make use of do-loops to avoid redundant Token::Match() calls

This commit is contained in:
PKEuS 2015-05-13 13:40:40 +02:00
parent b946b74e09
commit 3ce4e68ca6
4 changed files with 16 additions and 13 deletions

View File

@ -683,14 +683,14 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
std::size_t charactersAppend = 0; std::size_t charactersAppend = 0;
const Token *tok2 = tok; const Token *tok2 = tok;
while (Token::Match(tok2, strcatPattern.c_str(), declarationId)) { do {
charactersAppend += Token::getStrLength(tok2->tokAt(4 + varcount)); charactersAppend += Token::getStrLength(tok2->tokAt(4 + varcount));
if (charactersAppend >= static_cast<std::size_t>(total_size)) { if (charactersAppend >= static_cast<std::size_t>(total_size)) {
bufferOverrunError(tok2); bufferOverrunError(tok2);
break; break;
} }
tok2 = tok2->tokAt(7 + varcount); tok2 = tok2->tokAt(7 + varcount);
} } while (Token::Match(tok2, strcatPattern.c_str(), declarationId));
} }
// Check function call.. // Check function call..

View File

@ -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 struct member is (or might be) array.
// - should only return false if function argument is (or might be) non-const pointer or reference // - should only return false if function argument is (or might be) non-const pointer or reference
const Token *tok2 = vartok->next(); const Token *tok2 = vartok->next();
while (Token::Match(tok2,". %name%")) do {
tok2 = tok2->tokAt(2); tok2 = tok2->tokAt(2);
} while (Token::Match(tok2, ". %name%"));
if (Token::Match(tok2, "[,)]")) if (Token::Match(tok2, "[,)]"))
return false; return false;
} else if (pointer && alloc != CTOR_CALL && Token::Match(vartok, "%name% . %name% (")) { } else if (pointer && alloc != CTOR_CALL && Token::Match(vartok, "%name% . %name% (")) {

View File

@ -2855,12 +2855,12 @@ void Tokenizer::setVarId()
// constructor with initializer list // constructor with initializer list
if (Token::Match(tok2, ") : %name% (")) { if (Token::Match(tok2, ") : %name% (")) {
Token *tok3 = tok2; Token *tok3 = tok2;
while (Token::Match(tok3, ") [:,] %name% (")) { do {
Token *vartok = tok3->tokAt(2); Token *vartok = tok3->tokAt(2);
if (varlist[classname].find(vartok->str()) != varlist[classname].end()) if (varlist[classname].find(vartok->str()) != varlist[classname].end())
vartok->varId(varlist[classname][vartok->str()]); vartok->varId(varlist[classname][vartok->str()]);
tok3 = tok3->linkAt(3); tok3 = tok3->linkAt(3);
} } while (Token::Match(tok3, ") [:,] %name% ("));
if (Token::simpleMatch(tok3, ") {")) { if (Token::simpleMatch(tok3, ") {")) {
setVarIdClassFunction(classname, tok2, tok3->next()->link(), varlist[classname], &structMembers, &_varId); setVarIdClassFunction(classname, tok2, tok3->next()->link(), varlist[classname], &structMembers, &_varId);
} }
@ -3022,10 +3022,10 @@ bool Tokenizer::simplifySizeof()
continue; continue;
Token* tok2 = tok->next(); Token* tok2 = tok->next();
while (Token::Match(tok2, "[ %num% ]")) { do {
size *= static_cast<unsigned int>(MathLib::toLongNumber(tok2->strAt(1))); size *= static_cast<unsigned int>(MathLib::toLongNumber(tok2->strAt(1)));
tok2 = tok2->tokAt(3); tok2 = tok2->tokAt(3);
} } while (Token::Match(tok2, "[ %num% ]"));
if (Token::Match(tok2, "[;=]")) { if (Token::Match(tok2, "[;=]")) {
sizeOfVar[varId] = size; sizeOfVar[varId] = size;
declTokOfVar[varId] = tok; declTokOfVar[varId] = tok;
@ -6456,8 +6456,9 @@ bool Tokenizer::simplifyKnownVariables()
// skip loop variable // skip loop variable
if (Token::Match(tok2->tokAt(-2), "(|:: %type%")) { if (Token::Match(tok2->tokAt(-2), "(|:: %type%")) {
const Token *tok3 = tok2->previous(); const Token *tok3 = tok2->previous();
while (Token::Match(tok3->previous(), ":: %type%")) do {
tok3 = tok3->tokAt(-2); tok3 = tok3->tokAt(-2);
} while (Token::Match(tok3->previous(), ":: %type%"));
if (Token::Match(tok3->tokAt(-2), "for ( %type%")) if (Token::Match(tok3->tokAt(-2), "for ( %type%"))
continue; continue;
} }
@ -7302,9 +7303,8 @@ void Tokenizer::simplifyNestedStrcat()
// find inner strcat call // find inner strcat call
Token *tok2 = tok->tokAt(3); Token *tok2 = tok->tokAt(3);
while (Token::simpleMatch(tok2, "strcat ( strcat")) { while (Token::simpleMatch(tok2, "strcat ( strcat"))
tok2 = tok2->tokAt(2); tok2 = tok2->tokAt(2);
}
if (tok2->strAt(3) != ",") if (tok2->strAt(3) != ",")
continue; continue;
@ -7748,9 +7748,10 @@ void Tokenizer::simplifyEnum()
if (Token::Match(arg->previous(), "%type%|*|& %type% [,)=]") && if (Token::Match(arg->previous(), "%type%|*|& %type% [,)=]") &&
enumValues.find(arg->str()) != enumValues.end()) { enumValues.find(arg->str()) != enumValues.end()) {
// is this a variable declaration // is this a variable declaration
const Token *prev = arg; const Token *prev = arg->previous();
while (Token::Match(prev,"%type%|*|&")) do {
prev = prev->previous(); prev = prev->previous();
} while (Token::Match(prev, "%type%|*|&"));
if (!Token::Match(prev,"[,(] %type%")) if (!Token::Match(prev,"[,(] %type%"))
continue; continue;
if (prev->str() == "(" && (!Token::Match(prev->tokAt(-2), "%type%|::|*|& %type% (") || prev->strAt(-2) == "else")) if (prev->str() == "(" && (!Token::Match(prev->tokAt(-2), "%type%|::|*|& %type% (") || prev->strAt(-2) == "else"))

View File

@ -1046,8 +1046,9 @@ static bool valueFlowForward(Token * const startToken,
// bailout: possible assignment using >> // bailout: possible assignment using >>
if (Token::Match(tok2->previous(), ">> %name% >>|;")) { if (Token::Match(tok2->previous(), ">> %name% >>|;")) {
const Token *parent = tok2->previous(); const Token *parent = tok2->previous();
while (Token::simpleMatch(parent,">>")) do {
parent = parent->astParent(); parent = parent->astParent();
} while (Token::simpleMatch(parent, ">>"));
if (!parent) { if (!parent) {
if (settings->debugwarnings) if (settings->debugwarnings)
bailout(tokenlist, errorLogger, tok2, "Possible assignment of " + tok2->str() + " using >>"); bailout(tokenlist, errorLogger, tok2, "Possible assignment of " + tok2->str() + " using >>");