From a32b05197d4498db4cb2897bef9fb988afc1c5d4 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Sun, 20 Nov 2011 15:09:57 +0100 Subject: [PATCH] Change every 'tokAt(1)' to 'next()' and every 'tokAt(-1)' to 'previous()'. Added a safety check to ensure that a 'previous()' call doesn't crash (not sure if it's needed or not). --- lib/checkautovariables.cpp | 2 +- lib/checkboost.cpp | 2 +- lib/checkbufferoverrun.cpp | 10 +++++----- lib/checkclass.cpp | 20 ++++++++++---------- lib/checkmemoryleak.cpp | 10 +++++----- lib/checkobsoletefunctions.cpp | 6 +++--- lib/checkother.cpp | 10 +++++----- lib/checkunusedfunctions.cpp | 2 +- 8 files changed, 31 insertions(+), 31 deletions(-) diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index f5e320133..732c59213 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -132,7 +132,7 @@ void CheckAutoVariables::autoVariables() errorAutoVariableAssignment(tok->next(), false); } tok = tok->tokAt(4); - } else if (Token::Match(tok, "[;{}] %var% [ %any% ] = & %var%") && errorAv(tok->tokAt(1), tok->tokAt(7))) { + } else if (Token::Match(tok, "[;{}] %var% [ %any% ] = & %var%") && errorAv(tok->next(), tok->tokAt(7))) { errorAutoVariableAssignment(tok->next(), false); } // Critical return diff --git a/lib/checkboost.cpp b/lib/checkboost.cpp index 2d3d24257..6007f33a7 100644 --- a/lib/checkboost.cpp +++ b/lib/checkboost.cpp @@ -31,7 +31,7 @@ void CheckBoost::checkBoostForeachModification() if (!Token::simpleMatch(tok, "BOOST_FOREACH (")) continue; - const Token *container_tok = tok->next()->link()->tokAt(-1); + const Token *container_tok = tok->next()->link()->previous(); if (!Token::Match(container_tok, "%var% ) {")) continue; diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 407d76346..b44d5137b 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -870,12 +870,12 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vectorprevious(); - while (tok3 && Token::Match(tok3->tokAt(-1), "%var% .")) + while (tok3 && Token::Match(tok3->previous(), "%var% .")) tok3 = tok3->tokAt(-2); // just taking the address? - const bool addr(Token::simpleMatch(tok3, "&") || - Token::simpleMatch(tok3->tokAt(-1), "& (")); + const bool addr(tok3 && (Token::simpleMatch(tok3, "&") || + Token::simpleMatch(tok3->previous(), "& ("))); // taking address of 1 past end? if (addr && totalIndex == totalElements) @@ -1061,12 +1061,12 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo continue; const Token *tok2 = tok->previous(); - while (tok2 && Token::Match(tok2->tokAt(-1), "%var% .")) + while (tok2 && Token::Match(tok2->previous(), "%var% .")) tok2 = tok2->tokAt(-2); // just taking the address? const bool addr(Token::simpleMatch(tok2, "&") || - Token::simpleMatch(tok2->tokAt(-1), "& (")); + Token::simpleMatch(tok2->previous(), "& (")); // taking address of 1 past end? if (addr && totalIndex == totalElements) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index c8fc867e6..bb2538301 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -129,7 +129,7 @@ void CheckClass::constructors() // It's non-static and it's not initialized => error if (func->type == Function::eOperatorEqual) { - const Token *operStart = func->token->tokAt(1); + const Token *operStart = func->token->next(); bool classNameUsed = false; for (const Token *operTok = operStart; operTok != operStart->link(); operTok = operTok->next()) { @@ -755,9 +755,9 @@ void CheckClass::operatorEq() if (Token::Match(func->tokenDef->tokAt(2), "const| %var% &")) { if (func->tokenDef->strAt(2) == "const" && func->tokenDef->strAt(3) == scope->className) - operatorEqReturnError(func->tokenDef->tokAt(-1), scope->className); + operatorEqReturnError(func->tokenDef->previous(), scope->className); else if (func->tokenDef->strAt(2) == scope->className) - operatorEqReturnError(func->tokenDef->tokAt(-1), scope->className); + operatorEqReturnError(func->tokenDef->previous(), scope->className); } } } @@ -818,7 +818,7 @@ void CheckClass::checkReturnPtrThis(const Scope *scope, const Function *func, co tok = tok->tokAt(4); // check if a function is called - if (Token::Match(tok->tokAt(1), "%any% (") && + if (Token::Match(tok->next(), "%any% (") && tok->linkAt(2)->next()->str() == ";") { std::list::const_iterator it; @@ -847,11 +847,11 @@ void CheckClass::checkReturnPtrThis(const Scope *scope, const Function *func, co } // check if *this is returned - else if (!(Token::Match(tok->tokAt(1), "(| * this ;|=") || - Token::Match(tok->tokAt(1), "(| * this +=") || - Token::simpleMatch(tok->tokAt(1), "operator= (") || - Token::simpleMatch(tok->tokAt(1), "this . operator= (") || - (Token::Match(tok->tokAt(1), "%type% :: operator= (") && + else if (!(Token::Match(tok->next(), "(| * this ;|=") || + Token::Match(tok->next(), "(| * this +=") || + Token::simpleMatch(tok->next(), "operator= (") || + Token::simpleMatch(tok->next(), "this . operator= (") || + (Token::Match(tok->next(), "%type% :: operator= (") && tok->next()->str() == scope->className))) operatorEqRetRefThisError(func->token); } @@ -1041,7 +1041,7 @@ void CheckClass::virtualDestructor() continue; const Token *derived = scope->classDef; - const Token *derivedClass = derived->tokAt(1); + const Token *derivedClass = derived->next(); // Iterate through each base class... for (unsigned int j = 0; j < scope->derivedFrom.size(); ++j) { diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index a20cd9890..4f63b45bb 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -694,7 +694,7 @@ const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::listtokAt(1), callstack, 0, alloctype, dealloctype, false, 1); + Token *func = getcode(ftok->next(), callstack, 0, alloctype, dealloctype, false, 1); simplifycode(func); const char *ret = 0; if (Token::simpleMatch(func, "; alloc ; }")) @@ -766,7 +766,7 @@ const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::listnext(); if (!ftok) return 0; - Token *func = getcode(ftok->tokAt(1), callstack, parameterVarid, alloctype, dealloctype, false, sz); + Token *func = getcode(ftok->next(), callstack, parameterVarid, alloctype, dealloctype, false, sz); //simplifycode(func, all); const Token *func_ = func; while (func_ && func_->str() == ";") @@ -2267,7 +2267,7 @@ void CheckMemoryLeakInFunction::checkReallocUsage() const Token* tokEndRealloc = tok->linkAt(3); // Check that the allocation isn't followed immediately by an 'if (!var) { error(); }' that might handle failure - if (Token::Match(tokEndRealloc->tokAt(1), "; if ( ! %varid% ) {", tok->varId())) { + if (Token::Match(tokEndRealloc->next(), "; if ( ! %varid% ) {", tok->varId())) { const Token* tokEndBrace = tokEndRealloc->linkAt(7); if (tokEndBrace && Token::simpleMatch(tokEndBrace->tokAt(-2), ") ;") && Token::Match(tokEndBrace->linkAt(-2)->tokAt(-2), "{|}|; %var% (")) @@ -2286,13 +2286,13 @@ void CheckMemoryLeakInFunction::checkReallocUsage() const Token* tokEndRealloc = tok->linkAt(4); // Check that the allocation isn't followed immediately by an 'if (!var) { error(); }' that might handle failure - if (Token::Match(tokEndRealloc->tokAt(1), "; if ( ! * %varid% ) {", tok->next()->varId())) { + if (Token::Match(tokEndRealloc->next(), "; if ( ! * %varid% ) {", tok->next()->varId())) { const Token* tokEndBrace = tokEndRealloc->linkAt(8); if (tokEndBrace && Token::simpleMatch(tokEndBrace->tokAt(-2), ") ;") && Token::Match(tokEndBrace->linkAt(-2)->tokAt(-2), "{|}|; %var% (")) continue; } - memleakUponReallocFailureError(tok->tokAt(1), tok->strAt(1)); + memleakUponReallocFailureError(tok->next(), tok->strAt(1)); } } } diff --git a/lib/checkobsoletefunctions.cpp b/lib/checkobsoletefunctions.cpp index b1c4598f4..af529fe34 100644 --- a/lib/checkobsoletefunctions.cpp +++ b/lib/checkobsoletefunctions.cpp @@ -63,20 +63,20 @@ void CheckObsoleteFunctions::obsoleteFunctions() if (it != _obsoleteStandardFunctions.end()) { // If checking an old code base it might be uninteresting to update obsolete functions. // Therefore this is "information" - reportError(tok->tokAt(1), Severity::style, "obsoleteFunctions"+it->first, it->second); + reportError(tok->next(), Severity::style, "obsoleteFunctions"+it->first, it->second); } else { if (_settings->standards.posix) { it = _obsoletePosixFunctions.find(tok->str()); if (it != _obsoletePosixFunctions.end()) { // If checking an old code base it might be uninteresting to update obsolete functions. // Therefore this is "information" - reportError(tok->tokAt(1), Severity::style, "obsoleteFunctions"+it->first, it->second); + reportError(tok->next(), Severity::style, "obsoleteFunctions"+it->first, it->second); } } if (_settings->standards.c99) { it = _obsoleteC99Functions.find(tok->str()); if (it != _obsoleteC99Functions.end()) { - reportError(tok->tokAt(1), Severity::style, "obsoleteFunctions"+it->first, it->second); + reportError(tok->next(), Severity::style, "obsoleteFunctions"+it->first, it->second); } } } diff --git a/lib/checkother.cpp b/lib/checkother.cpp index e90d03380..dfaa8aa32 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -612,7 +612,7 @@ void CheckOther::checkSwitchCaseFallThrough() } justbreak = false; } else if (Token::simpleMatch(tok2, "do {")) { - tok2 = tok2->tokAt(1); + tok2 = tok2->next(); if (tok2->link() == NULL) { std::ostringstream errmsg; errmsg << "unmatched do in switch: " << tok2->linenr(); @@ -869,7 +869,7 @@ void CheckOther::checkIncorrectLogicOperator() } else if (NULL != (logicTok = Token::findmatch(tok, "%any% !=|==|<|>|>=|<= %any% &&|%oror% %any% !=|==|<|>|>=|<= %any% %any%", endTok))) { term1Tok = logicTok; term2Tok = logicTok->tokAt(4); - op1Tok = logicTok->tokAt(1); + op1Tok = logicTok->next(); op2Tok = logicTok->tokAt(3); op3Tok = logicTok->tokAt(5); nextTok = logicTok->tokAt(7); @@ -1381,7 +1381,7 @@ void CheckOther::checkComparisonOfBoolWithInt() comparisonOfBoolWithIntError(numTok, "!"+tok->strAt(2)); } } else if (Token::Match(tok, "( %num% ==|!= ! %var% )")) { - const Token *numTok = tok->tokAt(1); + const Token *numTok = tok->next(); if (numTok && numTok->str() != "0" && numTok->str() != "1") { comparisonOfBoolWithIntError(numTok, "!"+tok->strAt(4)); } @@ -1482,7 +1482,7 @@ void CheckOther::checkMemsetZeroBytes() { for (const Token* tok = _tokenizer->tokens(); tok; tok = tok->next()) { if (Token::simpleMatch(tok, "memset (")) { - const Token* lastParamTok = tok->next()->link()->tokAt(-1); + const Token* lastParamTok = tok->next()->link()->previous(); if (lastParamTok->str() == "0") memsetZeroBytesError(tok, tok->strAt(2)); } @@ -2148,7 +2148,7 @@ void CheckOther::checkIncorrectStringCompare() // assert(condition && "debug message") would be considered a fp. if (tok->str() == "&&" && tok->strAt(2) == ")" && tok->linkAt(2)->previous()->str() == "assert") continue; - incorrectStringBooleanError(tok->tokAt(1), tok->strAt(1)); + incorrectStringBooleanError(tok->next(), tok->strAt(1)); } if (Token::Match(tok, "if|while|assert ( %str% &&|%oror%|)")) { // assert("debug message" && condition) would be considered a fp. diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index deee3fd31..663ad3790 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -54,7 +54,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer) const Token *funcname = 0; if (Token::Match(tok, "%type% %var% (")) - funcname = tok->tokAt(1); + funcname = tok->next(); else if (Token::Match(tok, "%type% * %var% (")) funcname = tok->tokAt(2); else if (Token::Match(tok, "%type% :: %var% (") && !Token::Match(tok, tok->strAt(2).c_str()))