Merge pull request #264 from thomasjfox/simplematch

Use simple match where possible
This commit is contained in:
Daniel Marjamäki 2014-03-14 17:51:45 +01:00
commit b2708987c3
12 changed files with 32 additions and 32 deletions

View File

@ -632,7 +632,7 @@ void CheckBufferOverrun::checkFunctionParameter(const Token &tok, unsigned int p
} }
} }
if (Token::Match(tok.previous(), ".") || Token::Match(tok.tokAt(-2), "!!std ::")) if (Token::simpleMatch(tok.previous(), ".") || Token::Match(tok.tokAt(-2), "!!std ::"))
total_size.clear(); total_size.clear();
std::map<std::string, unsigned int>::const_iterator it = total_size.find(tok.str()); std::map<std::string, unsigned int>::const_iterator it = total_size.find(tok.str());
@ -1394,7 +1394,7 @@ bool CheckBufferOverrun::isArrayOfStruct(const Token* tok, int &position)
} else } else
break; break;
} }
if (Token::Match(tok->next(),";")) { if (Token::simpleMatch(tok->next(),";")) {
position = i; position = i;
return true; return true;
} }

View File

@ -969,7 +969,7 @@ void CheckClass::checkMemset()
if (var && arg1->strAt(1) == ",") { if (var && arg1->strAt(1) == ",") {
if (var->isPointer()) { if (var->isPointer()) {
derefs--; derefs--;
if (var->typeEndToken() && Token::Match(var->typeEndToken()->previous(), "* *")) // Check if it's a pointer to pointer if (var->typeEndToken() && Token::simpleMatch(var->typeEndToken()->previous(), "* *")) // Check if it's a pointer to pointer
derefs--; derefs--;
} }

View File

@ -1333,7 +1333,7 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * tok, const Settings *settings)
} else if (tok->str() == "&" || tok->type() == Token::eVariable || } else if (tok->str() == "&" || tok->type() == Token::eVariable ||
tok->type() == Token::eFunction || Token::Match(tok, "%type% ::") || tok->type() == Token::eFunction || Token::Match(tok, "%type% ::") ||
(Token::Match(tok, "static_cast|reinterpret_cast|const_cast <") && (Token::Match(tok, "static_cast|reinterpret_cast|const_cast <") &&
Token::Match(tok->linkAt(1), "> (") && Token::simpleMatch(tok->linkAt(1), "> (") &&
Token::Match(tok->linkAt(1)->linkAt(1), ") ,|)"))) { Token::Match(tok->linkAt(1)->linkAt(1), ") ,|)"))) {
if (Token::Match(tok, "static_cast|reinterpret_cast|const_cast")) { if (Token::Match(tok, "static_cast|reinterpret_cast|const_cast")) {
typeToken = tok->tokAt(2); typeToken = tok->tokAt(2);

View File

@ -410,7 +410,7 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown)
return false; return false;
// OK to delete a null // OK to delete a null
if (Token::Match(prev, "delete") || Token::Match(prev->tokAt(-2), "delete [ ]")) if (Token::simpleMatch(prev, "delete") || Token::Match(prev->tokAt(-2), "delete [ ]"))
return false; return false;
// OK to check if pointer is null // OK to check if pointer is null

View File

@ -83,7 +83,7 @@ static bool isSameExpression(const Token *tok1, const Token *tok2, const std::se
(Token::Match(tok2, "%var% <") && tok2->next()->link())) { (Token::Match(tok2, "%var% <") && tok2->next()->link())) {
// non-const template function that is not a dynamic_cast => return false // non-const template function that is not a dynamic_cast => return false
if (Token::Match(tok1->next()->link(), "> (") && if (Token::simpleMatch(tok1->next()->link(), "> (") &&
!(tok1->function() && tok1->function()->isConst) && !(tok1->function() && tok1->function()->isConst) &&
tok1->str() != "dynamic_cast") tok1->str() != "dynamic_cast")
return false; return false;
@ -2177,7 +2177,7 @@ void CheckOther::checkIncompleteStatement()
tok = tok->linkAt(2); tok = tok->linkAt(2);
// C++11 initialize set in initalizer list : [,:] std::set<int>{1} [{,] // C++11 initialize set in initalizer list : [,:] std::set<int>{1} [{,]
else if (Token::Match(tok,"> {") && tok->link()) else if (Token::simpleMatch(tok,"> {") && tok->link())
tok = tok->next()->link(); tok = tok->next()->link();
else if (Token::Match(tok, "[;{}] %str%") || Token::Match(tok, "[;{}] %num%")) { else if (Token::Match(tok, "[;{}] %str%") || Token::Match(tok, "[;{}] %num%")) {

View File

@ -296,11 +296,11 @@ void CheckSizeof::sizeofVoid()
const Token* tok2 = tok->tokAt(index); const Token* tok2 = tok->tokAt(index);
if (index == 0) { if (index == 0) {
bool isMember = false; bool isMember = false;
while (Token::Match(tok2->previous(), ".")) { while (Token::simpleMatch(tok2->previous(), ".")) {
isMember = true; isMember = true;
if (Token::Match(tok2->tokAt(-2), ")")) if (Token::simpleMatch(tok2->tokAt(-2), ")"))
tok2 = tok2->tokAt(-2)->link(); tok2 = tok2->tokAt(-2)->link();
else if (Token::Match(tok2->tokAt(-2), "]")) else if (Token::simpleMatch(tok2->tokAt(-2), "]"))
tok2 = tok2->tokAt(-2)->link()->previous(); tok2 = tok2->tokAt(-2)->link()->previous();
else else
tok2 = tok2->tokAt(-2); tok2 = tok2->tokAt(-2);
@ -315,7 +315,7 @@ void CheckSizeof::sizeofVoid()
// Check for cast on operations with '+|-' // Check for cast on operations with '+|-'
if (Token::Match(tok, "%var% +|-")) { if (Token::Match(tok, "%var% +|-")) {
// Check for cast expression // Check for cast expression
if (Token::Match(tok2->previous(), ")") && !Token::Match(tok2->previous()->link(), "( const| void *")) if (Token::simpleMatch(tok2->previous(), ")") && !Token::Match(tok2->previous()->link(), "( const| void *"))
continue; continue;
} }
arithOperationsOnVoidPointerError(tok, varname, arithOperationsOnVoidPointerError(tok, varname,

View File

@ -1506,10 +1506,10 @@ void CheckStl::checkDereferenceInvalidIterator()
// For "for" loops, only search between the two semicolons // For "for" loops, only search between the two semicolons
if (i->type == Scope::eFor) { if (i->type == Scope::eFor) {
startOfCondition = Token::findmatch(tok->tokAt(2), ";", endOfCondition); startOfCondition = Token::findsimplematch(tok->tokAt(2), ";", endOfCondition);
if (!startOfCondition) if (!startOfCondition)
continue; continue;
endOfCondition = Token::findmatch(startOfCondition->next(), ";", endOfCondition); endOfCondition = Token::findsimplematch(startOfCondition->next(), ";", endOfCondition);
if (!endOfCondition) if (!endOfCondition)
continue; continue;
} }
@ -1591,11 +1591,11 @@ void CheckStl::readingEmptyStlContainer()
if (Token::Match(tok, "%var% =|[")) { if (Token::Match(tok, "%var% =|[")) {
const Token *tok2; const Token *tok2;
if (Token::Match(tok->next(), "=")) if (Token::simpleMatch(tok->next(), "="))
tok2 = tok->tokAt(2); tok2 = tok->tokAt(2);
// to check cases like Cmap[1]; or i = Cmap[1] -- the right wld evaluate true when token reaches it. // to check cases like Cmap[1]; or i = Cmap[1] -- the right wld evaluate true when token reaches it.
else if (Token::Match(tok->next()," [") && Token::Match(tok->linkAt(1),"] =")) else if (Token::Match(tok->next()," [") && Token::simpleMatch(tok->linkAt(1),"] ="))
tok2 = tok->next()->link()->tokAt(2); tok2 = tok->next()->link()->tokAt(2);
else else

View File

@ -373,7 +373,7 @@ private:
break; break;
if (Token::Match(tok2, "%var% (")) if (Token::Match(tok2, "%var% ("))
break; break;
if (Token::Match(tok2, "%var% <") && Token::Match(tok2->linkAt(1), "> (")) if (Token::Match(tok2, "%var% <") && Token::simpleMatch(tok2->linkAt(1), "> ("))
break; break;
if (tok2->varId() && if (tok2->varId() &&
!Token::Match(tok2->previous(), "&|::") && !Token::Match(tok2->previous(), "&|::") &&

View File

@ -752,7 +752,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
variables.clear(); variables.clear();
break; break;
} }
if (Token::Match(tok, "goto")) { // https://sourceforge.net/apps/trac/cppcheck/ticket/4447 if (Token::simpleMatch(tok, "goto")) { // https://sourceforge.net/apps/trac/cppcheck/ticket/4447
variables.clear(); variables.clear();
break; break;
} }
@ -790,7 +790,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
for (const Token *body = end->linkAt(-1); body != end; body = body->next()) { for (const Token *body = end->linkAt(-1); body != end; body = body->next()) {
if (body->varId() == 0U) if (body->varId() == 0U)
continue; continue;
if (!Token::Match(body->next(),"=")) if (!Token::simpleMatch(body->next(),"="))
readvar.insert(body->varId()); readvar.insert(body->varId());
else if (readvar.find(body->varId()) != readvar.end()) else if (readvar.find(body->varId()) != readvar.end())
variables.erase(body->varId()); variables.erase(body->varId());

View File

@ -77,7 +77,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
if (tok2 && tok2->next()) { if (tok2 && tok2->next()) {
if (tok2->next()->str() == ";") if (tok2->next()->str() == ";")
tok = tok2->next(); tok = tok2->next();
else if (Token::Match(tok2->next(), "= {") && else if (Token::simpleMatch(tok2->next(), "= {") &&
tok2->linkAt(2)->next()->str() == ";") tok2->linkAt(2)->next()->str() == ";")
tok = tok2->linkAt(2)->next(); tok = tok2->linkAt(2)->next();
else if (Token::Match(tok2->next(), "(|{") && else if (Token::Match(tok2->next(), "(|{") &&
@ -1066,7 +1066,7 @@ void Variable::evaluate()
setFlag(fIsArray, arrayDimensions(_dimensions, _name->next())); setFlag(fIsArray, arrayDimensions(_dimensions, _name->next()));
if (_start) { if (_start) {
setFlag(fIsClass, !_start->isStandardType() && !isPointer() && !isReference()); setFlag(fIsClass, !_start->isStandardType() && !isPointer() && !isReference());
_stlType = Token::Match(_start, "std ::"); _stlType = Token::simpleMatch(_start, "std ::");
} }
if (_access == Argument) { if (_access == Argument) {
tok = _name; tok = _name;

View File

@ -2204,7 +2204,7 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::map<std::stri
++typeCount; ++typeCount;
} }
} else if ((TemplateSimplifier::templateParameters(tok2) > 0) || } else if ((TemplateSimplifier::templateParameters(tok2) > 0) ||
Token::Match(tok2, "< >") /* Ticket #4764 */) { Token::simpleMatch(tok2, "< >") /* Ticket #4764 */) {
tok2 = tok2->findClosingBracket(); tok2 = tok2->findClosingBracket();
if (!Token::Match(tok2, ">|>>")) if (!Token::Match(tok2, ">|>>"))
break; break;
@ -2727,7 +2727,7 @@ void Tokenizer::createLinks2()
} }
// if > is followed by ; .. "new a<b>;" is expected // if > is followed by ; .. "new a<b>;" is expected
if (Token::Match(token->next(), ";")) { if (Token::simpleMatch(token->next(), ";")) {
Token *prev = type.top()->previous(); Token *prev = type.top()->previous();
while (prev && Token::Match(prev->previous(), ":: %var%")) while (prev && Token::Match(prev->previous(), ":: %var%"))
prev = prev->tokAt(-2); prev = prev->tokAt(-2);
@ -4234,8 +4234,8 @@ Token *Tokenizer::simplifyAddBracesPair(Token *tok, bool commandWithCondition)
if (tokAfterCondition->str()=="{") { if (tokAfterCondition->str()=="{") {
// already surrounded by braces // already surrounded by braces
tokBracesEnd=tokAfterCondition->link(); tokBracesEnd=tokAfterCondition->link();
} else if (Token::Match(tokAfterCondition, "try {") && } else if (Token::simpleMatch(tokAfterCondition, "try {") &&
Token::Match(tokAfterCondition->linkAt(1), "} catch (")) { Token::simpleMatch(tokAfterCondition->linkAt(1), "} catch (")) {
tokAfterCondition->previous()->insertToken("{"); tokAfterCondition->previous()->insertToken("{");
Token * tokOpenBrace = tokAfterCondition->previous(); Token * tokOpenBrace = tokAfterCondition->previous();
Token * tokEnd = tokAfterCondition->linkAt(1)->linkAt(2)->linkAt(1); Token * tokEnd = tokAfterCondition->linkAt(1)->linkAt(2)->linkAt(1);
@ -5500,7 +5500,7 @@ void Tokenizer::simplifyPlatformTypes()
} else if (Token::Match(tok, "HRESULT|LONG")) { } else if (Token::Match(tok, "HRESULT|LONG")) {
tok->originalName(tok->str()); tok->originalName(tok->str());
tok->str("long"); tok->str("long");
} else if (Token::Match(tok, "INT8")) { } else if (tok->str() == "INT8") {
tok->originalName(tok->str()); tok->originalName(tok->str());
tok->str("char"); tok->str("char");
tok->isSigned(true); tok->isSigned(true);
@ -5752,7 +5752,7 @@ void Tokenizer::simplifyIfAndWhileAssign()
const bool iswhile(tok->next()->str() == "while"); const bool iswhile(tok->next()->str() == "while");
// simplifying a "do { } while(cond);" condition ? // simplifying a "do { } while(cond);" condition ?
const bool isDoWhile = iswhile && Token::Match(tok, "}") && Token::Match(tok->link()->previous(), "do"); const bool isDoWhile = iswhile && Token::simpleMatch(tok, "}") && Token::simpleMatch(tok->link()->previous(), "do");
Token* openBraceTok = tok->link(); Token* openBraceTok = tok->link();
// delete the "if|while" // delete the "if|while"

View File

@ -51,7 +51,7 @@ static bool bailoutFunctionPar(const Token *tok, const ValueFlow::Value &value,
return false; return false;
// address of variable // address of variable
const bool addressOf = tok && Token::Match(tok->previous(), "&"); const bool addressOf = tok && Token::simpleMatch(tok->previous(), "&");
// passing variable to subfunction? // passing variable to subfunction?
if (Token::Match(tok->tokAt(-2), ") & %var% [,)]") && Token::Match(tok->linkAt(-2)->previous(), "[,(] (")) if (Token::Match(tok->tokAt(-2), ") & %var% [,)]") && Token::Match(tok->linkAt(-2)->previous(), "[,(] ("))
@ -406,7 +406,7 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
} else if (tok2->str() == "{") { } else if (tok2->str() == "{") {
// if variable is assigned in loop don't look before the loop // if variable is assigned in loop don't look before the loop
if (tok2->previous() && if (tok2->previous() &&
(Token::Match(tok2->previous(), "do") || (Token::simpleMatch(tok2->previous(), "do") ||
(tok2->strAt(-1) == ")" && Token::Match(tok2->linkAt(-1)->previous(), "for|while (")))) { (tok2->strAt(-1) == ")" && Token::Match(tok2->linkAt(-1)->previous(), "for|while (")))) {
const Token *start = tok2; const Token *start = tok2;
@ -422,7 +422,7 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
if (!var->isLocal()) { if (!var->isLocal()) {
if (!Token::Match(tok2->previous(), ")|else|do {")) if (!Token::Match(tok2->previous(), ")|else|do {"))
break; break;
if (Token::Match(tok2->previous(), ") {") && if (Token::simpleMatch(tok2->previous(), ") {") &&
!Token::Match(tok2->linkAt(-1)->previous(), "if|for|while (")) !Token::Match(tok2->linkAt(-1)->previous(), "if|for|while ("))
break; break;
} }
@ -484,7 +484,7 @@ static void valueFlowAfterAssign(TokenList *tokenlist, ErrorLogger *errorLogger,
tok2 = tok2->linkAt(1); tok2 = tok2->linkAt(1);
// conditional block of code that assigns variable.. // conditional block of code that assigns variable..
if (Token::Match(tok2, "%var% (") && Token::Match(tok2->linkAt(1), ") {")) { if (Token::Match(tok2, "%var% (") && Token::simpleMatch(tok2->linkAt(1), ") {")) {
Token * const start = tok2->linkAt(1)->next(); Token * const start = tok2->linkAt(1)->next();
Token * const end = start->link(); Token * const end = start->link();
if (Token::findmatch(start, "%varid%", end, varid)) { if (Token::findmatch(start, "%varid%", end, varid)) {
@ -508,7 +508,7 @@ static void valueFlowAfterAssign(TokenList *tokenlist, ErrorLogger *errorLogger,
// noreturn scopes.. // noreturn scopes..
if (number_of_if > 0 && if (number_of_if > 0 &&
(Token::findmatch(start, "return|continue|break", end) || (Token::findmatch(start, "return|continue|break", end) ||
(Token::Match(end,"} else {") && Token::findmatch(end, "return|continue|break", end->linkAt(2))))) { (Token::simpleMatch(end,"} else {") && Token::findmatch(end, "return|continue|break", end->linkAt(2))))) {
if (settings->debugwarnings) if (settings->debugwarnings)
bailout(tokenlist, errorLogger, tok2, "variable " + var->nameToken()->str() + ". noreturn conditional scope."); bailout(tokenlist, errorLogger, tok2, "variable " + var->nameToken()->str() + ". noreturn conditional scope.");
break; break;
@ -612,7 +612,7 @@ static void valueFlowAfterAssign(TokenList *tokenlist, ErrorLogger *errorLogger,
static void valueFlowForLoop(TokenList *tokenlist, ErrorLogger *errorLogger, const Settings *settings) static void valueFlowForLoop(TokenList *tokenlist, ErrorLogger *errorLogger, const Settings *settings)
{ {
for (Token *tok = tokenlist->front(); tok; tok = tok->next()) { for (Token *tok = tokenlist->front(); tok; tok = tok->next()) {
if (!Token::Match(tok, "for (")) if (!Token::simpleMatch(tok, "for ("))
continue; continue;
tok = tok->tokAt(2); tok = tok->tokAt(2);