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).
This commit is contained in:
Edoardo Prezioso 2011-11-20 15:09:57 +01:00
parent b92959809c
commit a32b05197d
8 changed files with 31 additions and 31 deletions

View File

@ -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

View File

@ -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;

View File

@ -870,12 +870,12 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
continue;
const Token *tok3 = tok->previous();
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)

View File

@ -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<Function>::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) {

View File

@ -694,7 +694,7 @@ const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::list<co
if (!ftok)
return 0;
Token *func = getcode(ftok->tokAt(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::list<co
ftok = ftok->next();
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));
}
}
}

View File

@ -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);
}
}
}

View File

@ -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.

View File

@ -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()))