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:
parent
b92959809c
commit
a32b05197d
|
@ -132,7 +132,7 @@ void CheckAutoVariables::autoVariables()
|
||||||
errorAutoVariableAssignment(tok->next(), false);
|
errorAutoVariableAssignment(tok->next(), false);
|
||||||
}
|
}
|
||||||
tok = tok->tokAt(4);
|
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);
|
errorAutoVariableAssignment(tok->next(), false);
|
||||||
}
|
}
|
||||||
// Critical return
|
// Critical return
|
||||||
|
|
|
@ -31,7 +31,7 @@ void CheckBoost::checkBoostForeachModification()
|
||||||
if (!Token::simpleMatch(tok, "BOOST_FOREACH ("))
|
if (!Token::simpleMatch(tok, "BOOST_FOREACH ("))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const Token *container_tok = tok->next()->link()->tokAt(-1);
|
const Token *container_tok = tok->next()->link()->previous();
|
||||||
if (!Token::Match(container_tok, "%var% ) {"))
|
if (!Token::Match(container_tok, "%var% ) {"))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|
|
@ -870,12 +870,12 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const Token *tok3 = tok->previous();
|
const Token *tok3 = tok->previous();
|
||||||
while (tok3 && Token::Match(tok3->tokAt(-1), "%var% ."))
|
while (tok3 && Token::Match(tok3->previous(), "%var% ."))
|
||||||
tok3 = tok3->tokAt(-2);
|
tok3 = tok3->tokAt(-2);
|
||||||
|
|
||||||
// just taking the address?
|
// just taking the address?
|
||||||
const bool addr(Token::simpleMatch(tok3, "&") ||
|
const bool addr(tok3 && (Token::simpleMatch(tok3, "&") ||
|
||||||
Token::simpleMatch(tok3->tokAt(-1), "& ("));
|
Token::simpleMatch(tok3->previous(), "& (")));
|
||||||
|
|
||||||
// taking address of 1 past end?
|
// taking address of 1 past end?
|
||||||
if (addr && totalIndex == totalElements)
|
if (addr && totalIndex == totalElements)
|
||||||
|
@ -1061,12 +1061,12 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const Token *tok2 = tok->previous();
|
const Token *tok2 = tok->previous();
|
||||||
while (tok2 && Token::Match(tok2->tokAt(-1), "%var% ."))
|
while (tok2 && Token::Match(tok2->previous(), "%var% ."))
|
||||||
tok2 = tok2->tokAt(-2);
|
tok2 = tok2->tokAt(-2);
|
||||||
|
|
||||||
// just taking the address?
|
// just taking the address?
|
||||||
const bool addr(Token::simpleMatch(tok2, "&") ||
|
const bool addr(Token::simpleMatch(tok2, "&") ||
|
||||||
Token::simpleMatch(tok2->tokAt(-1), "& ("));
|
Token::simpleMatch(tok2->previous(), "& ("));
|
||||||
|
|
||||||
// taking address of 1 past end?
|
// taking address of 1 past end?
|
||||||
if (addr && totalIndex == totalElements)
|
if (addr && totalIndex == totalElements)
|
||||||
|
|
|
@ -129,7 +129,7 @@ void CheckClass::constructors()
|
||||||
|
|
||||||
// It's non-static and it's not initialized => error
|
// It's non-static and it's not initialized => error
|
||||||
if (func->type == Function::eOperatorEqual) {
|
if (func->type == Function::eOperatorEqual) {
|
||||||
const Token *operStart = func->token->tokAt(1);
|
const Token *operStart = func->token->next();
|
||||||
|
|
||||||
bool classNameUsed = false;
|
bool classNameUsed = false;
|
||||||
for (const Token *operTok = operStart; operTok != operStart->link(); operTok = operTok->next()) {
|
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 (Token::Match(func->tokenDef->tokAt(2), "const| %var% &")) {
|
||||||
if (func->tokenDef->strAt(2) == "const" &&
|
if (func->tokenDef->strAt(2) == "const" &&
|
||||||
func->tokenDef->strAt(3) == scope->className)
|
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)
|
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);
|
tok = tok->tokAt(4);
|
||||||
|
|
||||||
// check if a function is called
|
// check if a function is called
|
||||||
if (Token::Match(tok->tokAt(1), "%any% (") &&
|
if (Token::Match(tok->next(), "%any% (") &&
|
||||||
tok->linkAt(2)->next()->str() == ";") {
|
tok->linkAt(2)->next()->str() == ";") {
|
||||||
std::list<Function>::const_iterator it;
|
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
|
// check if *this is returned
|
||||||
else if (!(Token::Match(tok->tokAt(1), "(| * this ;|=") ||
|
else if (!(Token::Match(tok->next(), "(| * this ;|=") ||
|
||||||
Token::Match(tok->tokAt(1), "(| * this +=") ||
|
Token::Match(tok->next(), "(| * this +=") ||
|
||||||
Token::simpleMatch(tok->tokAt(1), "operator= (") ||
|
Token::simpleMatch(tok->next(), "operator= (") ||
|
||||||
Token::simpleMatch(tok->tokAt(1), "this . operator= (") ||
|
Token::simpleMatch(tok->next(), "this . operator= (") ||
|
||||||
(Token::Match(tok->tokAt(1), "%type% :: operator= (") &&
|
(Token::Match(tok->next(), "%type% :: operator= (") &&
|
||||||
tok->next()->str() == scope->className)))
|
tok->next()->str() == scope->className)))
|
||||||
operatorEqRetRefThisError(func->token);
|
operatorEqRetRefThisError(func->token);
|
||||||
}
|
}
|
||||||
|
@ -1041,7 +1041,7 @@ void CheckClass::virtualDestructor()
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const Token *derived = scope->classDef;
|
const Token *derived = scope->classDef;
|
||||||
const Token *derivedClass = derived->tokAt(1);
|
const Token *derivedClass = derived->next();
|
||||||
|
|
||||||
// Iterate through each base class...
|
// Iterate through each base class...
|
||||||
for (unsigned int j = 0; j < scope->derivedFrom.size(); ++j) {
|
for (unsigned int j = 0; j < scope->derivedFrom.size(); ++j) {
|
||||||
|
|
|
@ -694,7 +694,7 @@ const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::list<co
|
||||||
if (!ftok)
|
if (!ftok)
|
||||||
return 0;
|
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);
|
simplifycode(func);
|
||||||
const char *ret = 0;
|
const char *ret = 0;
|
||||||
if (Token::simpleMatch(func, "; alloc ; }"))
|
if (Token::simpleMatch(func, "; alloc ; }"))
|
||||||
|
@ -766,7 +766,7 @@ const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::list<co
|
||||||
ftok = ftok->next();
|
ftok = ftok->next();
|
||||||
if (!ftok)
|
if (!ftok)
|
||||||
return 0;
|
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);
|
//simplifycode(func, all);
|
||||||
const Token *func_ = func;
|
const Token *func_ = func;
|
||||||
while (func_ && func_->str() == ";")
|
while (func_ && func_->str() == ";")
|
||||||
|
@ -2267,7 +2267,7 @@ void CheckMemoryLeakInFunction::checkReallocUsage()
|
||||||
|
|
||||||
const Token* tokEndRealloc = tok->linkAt(3);
|
const Token* tokEndRealloc = tok->linkAt(3);
|
||||||
// Check that the allocation isn't followed immediately by an 'if (!var) { error(); }' that might handle failure
|
// 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);
|
const Token* tokEndBrace = tokEndRealloc->linkAt(7);
|
||||||
if (tokEndBrace && Token::simpleMatch(tokEndBrace->tokAt(-2), ") ;") &&
|
if (tokEndBrace && Token::simpleMatch(tokEndBrace->tokAt(-2), ") ;") &&
|
||||||
Token::Match(tokEndBrace->linkAt(-2)->tokAt(-2), "{|}|; %var% ("))
|
Token::Match(tokEndBrace->linkAt(-2)->tokAt(-2), "{|}|; %var% ("))
|
||||||
|
@ -2286,13 +2286,13 @@ void CheckMemoryLeakInFunction::checkReallocUsage()
|
||||||
|
|
||||||
const Token* tokEndRealloc = tok->linkAt(4);
|
const Token* tokEndRealloc = tok->linkAt(4);
|
||||||
// Check that the allocation isn't followed immediately by an 'if (!var) { error(); }' that might handle failure
|
// 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);
|
const Token* tokEndBrace = tokEndRealloc->linkAt(8);
|
||||||
if (tokEndBrace && Token::simpleMatch(tokEndBrace->tokAt(-2), ") ;") &&
|
if (tokEndBrace && Token::simpleMatch(tokEndBrace->tokAt(-2), ") ;") &&
|
||||||
Token::Match(tokEndBrace->linkAt(-2)->tokAt(-2), "{|}|; %var% ("))
|
Token::Match(tokEndBrace->linkAt(-2)->tokAt(-2), "{|}|; %var% ("))
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
memleakUponReallocFailureError(tok->tokAt(1), tok->strAt(1));
|
memleakUponReallocFailureError(tok->next(), tok->strAt(1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,20 +63,20 @@ void CheckObsoleteFunctions::obsoleteFunctions()
|
||||||
if (it != _obsoleteStandardFunctions.end()) {
|
if (it != _obsoleteStandardFunctions.end()) {
|
||||||
// If checking an old code base it might be uninteresting to update obsolete functions.
|
// If checking an old code base it might be uninteresting to update obsolete functions.
|
||||||
// Therefore this is "information"
|
// 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 {
|
} else {
|
||||||
if (_settings->standards.posix) {
|
if (_settings->standards.posix) {
|
||||||
it = _obsoletePosixFunctions.find(tok->str());
|
it = _obsoletePosixFunctions.find(tok->str());
|
||||||
if (it != _obsoletePosixFunctions.end()) {
|
if (it != _obsoletePosixFunctions.end()) {
|
||||||
// If checking an old code base it might be uninteresting to update obsolete functions.
|
// If checking an old code base it might be uninteresting to update obsolete functions.
|
||||||
// Therefore this is "information"
|
// 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) {
|
if (_settings->standards.c99) {
|
||||||
it = _obsoleteC99Functions.find(tok->str());
|
it = _obsoleteC99Functions.find(tok->str());
|
||||||
if (it != _obsoleteC99Functions.end()) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -612,7 +612,7 @@ void CheckOther::checkSwitchCaseFallThrough()
|
||||||
}
|
}
|
||||||
justbreak = false;
|
justbreak = false;
|
||||||
} else if (Token::simpleMatch(tok2, "do {")) {
|
} else if (Token::simpleMatch(tok2, "do {")) {
|
||||||
tok2 = tok2->tokAt(1);
|
tok2 = tok2->next();
|
||||||
if (tok2->link() == NULL) {
|
if (tok2->link() == NULL) {
|
||||||
std::ostringstream errmsg;
|
std::ostringstream errmsg;
|
||||||
errmsg << "unmatched do in switch: " << tok2->linenr();
|
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))) {
|
} else if (NULL != (logicTok = Token::findmatch(tok, "%any% !=|==|<|>|>=|<= %any% &&|%oror% %any% !=|==|<|>|>=|<= %any% %any%", endTok))) {
|
||||||
term1Tok = logicTok;
|
term1Tok = logicTok;
|
||||||
term2Tok = logicTok->tokAt(4);
|
term2Tok = logicTok->tokAt(4);
|
||||||
op1Tok = logicTok->tokAt(1);
|
op1Tok = logicTok->next();
|
||||||
op2Tok = logicTok->tokAt(3);
|
op2Tok = logicTok->tokAt(3);
|
||||||
op3Tok = logicTok->tokAt(5);
|
op3Tok = logicTok->tokAt(5);
|
||||||
nextTok = logicTok->tokAt(7);
|
nextTok = logicTok->tokAt(7);
|
||||||
|
@ -1381,7 +1381,7 @@ void CheckOther::checkComparisonOfBoolWithInt()
|
||||||
comparisonOfBoolWithIntError(numTok, "!"+tok->strAt(2));
|
comparisonOfBoolWithIntError(numTok, "!"+tok->strAt(2));
|
||||||
}
|
}
|
||||||
} else if (Token::Match(tok, "( %num% ==|!= ! %var% )")) {
|
} 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") {
|
if (numTok && numTok->str() != "0" && numTok->str() != "1") {
|
||||||
comparisonOfBoolWithIntError(numTok, "!"+tok->strAt(4));
|
comparisonOfBoolWithIntError(numTok, "!"+tok->strAt(4));
|
||||||
}
|
}
|
||||||
|
@ -1482,7 +1482,7 @@ void CheckOther::checkMemsetZeroBytes()
|
||||||
{
|
{
|
||||||
for (const Token* tok = _tokenizer->tokens(); tok; tok = tok->next()) {
|
for (const Token* tok = _tokenizer->tokens(); tok; tok = tok->next()) {
|
||||||
if (Token::simpleMatch(tok, "memset (")) {
|
if (Token::simpleMatch(tok, "memset (")) {
|
||||||
const Token* lastParamTok = tok->next()->link()->tokAt(-1);
|
const Token* lastParamTok = tok->next()->link()->previous();
|
||||||
if (lastParamTok->str() == "0")
|
if (lastParamTok->str() == "0")
|
||||||
memsetZeroBytesError(tok, tok->strAt(2));
|
memsetZeroBytesError(tok, tok->strAt(2));
|
||||||
}
|
}
|
||||||
|
@ -2148,7 +2148,7 @@ void CheckOther::checkIncorrectStringCompare()
|
||||||
// assert(condition && "debug message") would be considered a fp.
|
// assert(condition && "debug message") would be considered a fp.
|
||||||
if (tok->str() == "&&" && tok->strAt(2) == ")" && tok->linkAt(2)->previous()->str() == "assert")
|
if (tok->str() == "&&" && tok->strAt(2) == ")" && tok->linkAt(2)->previous()->str() == "assert")
|
||||||
continue;
|
continue;
|
||||||
incorrectStringBooleanError(tok->tokAt(1), tok->strAt(1));
|
incorrectStringBooleanError(tok->next(), tok->strAt(1));
|
||||||
}
|
}
|
||||||
if (Token::Match(tok, "if|while|assert ( %str% &&|%oror%|)")) {
|
if (Token::Match(tok, "if|while|assert ( %str% &&|%oror%|)")) {
|
||||||
// assert("debug message" && condition) would be considered a fp.
|
// assert("debug message" && condition) would be considered a fp.
|
||||||
|
|
|
@ -54,7 +54,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer)
|
||||||
const Token *funcname = 0;
|
const Token *funcname = 0;
|
||||||
|
|
||||||
if (Token::Match(tok, "%type% %var% ("))
|
if (Token::Match(tok, "%type% %var% ("))
|
||||||
funcname = tok->tokAt(1);
|
funcname = tok->next();
|
||||||
else if (Token::Match(tok, "%type% * %var% ("))
|
else if (Token::Match(tok, "%type% * %var% ("))
|
||||||
funcname = tok->tokAt(2);
|
funcname = tok->tokAt(2);
|
||||||
else if (Token::Match(tok, "%type% :: %var% (") && !Token::Match(tok, tok->strAt(2).c_str()))
|
else if (Token::Match(tok, "%type% :: %var% (") && !Token::Match(tok, tok->strAt(2).c_str()))
|
||||||
|
|
Loading…
Reference in New Issue