Simplified several Token::Match/simpleMatch calls when match string consists of a single pattern

Fixed two CheckInternal error messages
This commit is contained in:
PKEuS 2013-03-01 03:42:04 -08:00
parent 2ad38c8701
commit 0105f8223c
10 changed files with 37 additions and 39 deletions

View File

@ -610,7 +610,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
else if (Token::Match(ftok, "%var% [|.")) { else if (Token::Match(ftok, "%var% [|.")) {
const Token *tok2 = ftok; const Token *tok2 = ftok;
while (tok2) { while (tok2) {
if (Token::simpleMatch(tok2->next(), "[")) if (tok2->strAt(1) == "[")
tok2 = tok2->next()->link(); tok2 = tok2->next()->link();
else if (Token::Match(tok2->next(), ". %var%")) else if (Token::Match(tok2->next(), ". %var%"))
tok2 = tok2->tokAt(2); tok2 = tok2->tokAt(2);

View File

@ -247,7 +247,7 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
deallocUseError(tok, tok->str()); deallocUseError(tok, tok->str());
} else if (Token::simpleMatch(tok->tokAt(-2), "= &")) { } else if (Token::simpleMatch(tok->tokAt(-2), "= &")) {
varInfo->erase(tok->varId()); varInfo->erase(tok->varId());
} else if (Token::simpleMatch(tok->previous(), "=")) { } else if (tok->strAt(-1) == "=") {
varInfo->erase(tok->varId()); varInfo->erase(tok->varId());
} }
} else if (Token::Match(tok->previous(), "& %var% = %var% ;")) { } else if (Token::Match(tok->previous(), "& %var% = %var% ;")) {

View File

@ -1232,7 +1232,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
if (tok2->varId() == varid) { if (tok2->varId() == varid) {
// Read data.. // Read data..
if (!Token::Match(tok2->previous(), "&|(") && if (!Token::Match(tok2->previous(), "&|(") &&
Token::simpleMatch(tok2->next(), "[")) { tok2->strAt(1) == "[") {
} else if (f.empty() || } else if (f.empty() ||
!test_white_list(f.top()->str()) || !test_white_list(f.top()->str()) ||
getDeallocationType(f.top(),varid)) { getDeallocationType(f.top(),varid)) {

View File

@ -239,7 +239,7 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::list<const Token
} }
// 2nd parameter.. // 2nd parameter..
if (secondParam && ((value == 0 && secondParam->str() == "0") || (Token::Match(secondParam, "%var%") && secondParam->varId() > 0))) { if (secondParam && ((value == 0 && secondParam->str() == "0") || (secondParam->varId() > 0))) {
if (functionNames2_all.find(tok.str()) != functionNames2_all.end()) if (functionNames2_all.find(tok.str()) != functionNames2_all.end())
var.push_back(secondParam); var.push_back(secondParam);
else if (value == 0 && functionNames2_nullptr.find(tok.str()) != functionNames2_nullptr.end()) else if (value == 0 && functionNames2_nullptr.find(tok.str()) != functionNames2_nullptr.end())

View File

@ -61,7 +61,7 @@ void CheckOther::checkCastIntToCharAndBack()
const Variable *var = tok->variable(); const Variable *var = tok->variable();
if (var && var->typeEndToken()->str() == "char" && !var->typeEndToken()->isSigned()) { if (var && var->typeEndToken()->str() == "char" && !var->typeEndToken()->isSigned()) {
uiVarId = tok->varId(); uiVarId = tok->varId();
strFunctionName = tok->tokAt(2)->str(); strFunctionName = tok->strAt(2);
} }
} }
} else if (Token::Match(tok, "EOF %comp% ( %var% = fclose|fflush|fputc|fputs|fscanf|getchar|getc|fgetc|putchar|putc|puts|scanf|sscanf|ungetc (")) { } else if (Token::Match(tok, "EOF %comp% ( %var% = fclose|fflush|fputc|fputs|fscanf|getchar|getc|fgetc|putchar|putc|puts|scanf|sscanf|ungetc (")) {
@ -69,7 +69,7 @@ void CheckOther::checkCastIntToCharAndBack()
if (tok && tok->varId()) { if (tok && tok->varId()) {
const Variable *var = tok->variable(); const Variable *var = tok->variable();
if (var && var->typeEndToken()->str() == "char" && !var->typeEndToken()->isSigned()) { if (var && var->typeEndToken()->str() == "char" && !var->typeEndToken()->isSigned()) {
checkCastIntToCharAndBackError(tok, tok->tokAt(2)->str()); checkCastIntToCharAndBackError(tok, tok->strAt(2));
} }
} }
} }
@ -2548,7 +2548,7 @@ void CheckOther::checkComparisonOfFuncReturningBool()
continue; continue;
const Token *first_token; const Token *first_token;
bool first_token_func_of_type_bool = false; bool first_token_func_of_type_bool = false;
if (Token::simpleMatch(tok->previous(), ")")) { if (tok->strAt(-1) == ")") {
first_token = tok->previous()->link()->previous(); first_token = tok->previous()->link()->previous();
} else { } else {
first_token = tok->previous(); first_token = tok->previous();
@ -2996,12 +2996,10 @@ void CheckOther::checkDoubleFree()
// If it is a function call, then clear those variables in its argument list // If it is a function call, then clear those variables in its argument list
else if (Token::simpleMatch(tok->next()->link(), ") ;")) { else if (Token::simpleMatch(tok->next()->link(), ") ;")) {
for (const Token* tok2 = tok->tokAt(2); tok2 != tok->linkAt(1); tok2 = tok2->next()) { for (const Token* tok2 = tok->tokAt(2); tok2 != tok->linkAt(1); tok2 = tok2->next()) {
if (Token::Match(tok2, "%var%")) { if (tok2->varId()) {
unsigned int var = tok2->varId(); unsigned int var = tok2->varId();
if (var) { freedVariables.erase(var);
freedVariables.erase(var); closeDirVariables.erase(var);
closeDirVariables.erase(var);
}
} }
} }
} }

View File

@ -370,7 +370,7 @@ private:
if (tok2->varId() && if (tok2->varId() &&
!Token::Match(tok2->previous(), "&|::") && !Token::Match(tok2->previous(), "&|::") &&
!Token::simpleMatch(tok2->tokAt(-2), "& (") && !Token::simpleMatch(tok2->tokAt(-2), "& (") &&
!Token::simpleMatch(tok2->next(), "=")) { tok2->strAt(1) != "=") {
// Multiple assignments.. // Multiple assignments..
if (Token::Match(tok2->next(), ".|[")) { if (Token::Match(tok2->next(), ".|[")) {
const Token * tok3 = tok2; const Token * tok3 = tok2;
@ -547,12 +547,12 @@ private:
} }
} }
if (Token::simpleMatch(tok.next(), "(")) { if (tok.strAt(1) == "(") {
use_pointer(checks, &tok); use_pointer(checks, &tok);
} }
if (Token::Match(tok.tokAt(-2), "[;{}] *")) { if (Token::Match(tok.tokAt(-2), "[;{}] *")) {
if (Token::simpleMatch(tok.next(), "=")) { if (tok.strAt(1) == "=") {
// is the pointer used in the rhs? // is the pointer used in the rhs?
bool used = false; bool used = false;
for (const Token *tok2 = tok.tokAt(2); tok2; tok2 = tok2->next()) { for (const Token *tok2 = tok.tokAt(2); tok2; tok2 = tok2->next()) {
@ -580,22 +580,22 @@ private:
} }
else if ((!isC && (Token::Match(tok.previous(), "<<|>>") || Token::Match(tok.previous(), "[;{}] %var% <<"))) || else if ((!isC && (Token::Match(tok.previous(), "<<|>>") || Token::Match(tok.previous(), "[;{}] %var% <<"))) ||
Token::simpleMatch(tok.next(), "=")) { tok.strAt(1) == "=") {
// TODO: Don't bail out for "<<" and ">>" if these are // TODO: Don't bail out for "<<" and ">>" if these are
// just computations // just computations
ExecutionPath::bailOutVar(checks, tok.varId()); ExecutionPath::bailOutVar(checks, tok.varId());
return &tok; return &tok;
} }
if (Token::simpleMatch(tok.next(), "[")) { if (tok.strAt(1) == "[" && tok.next()->link()) {
const Token *tok2 = tok.next()->link(); const Token *tok2 = tok.next()->link();
if (Token::simpleMatch(tok2 ? tok2->next() : 0, "=")) { if (tok2->strAt(1) == "=") {
ExecutionPath::bailOutVar(checks, tok.varId()); ExecutionPath::bailOutVar(checks, tok.varId());
return &tok; return &tok;
} }
} }
if (Token::simpleMatch(tok.previous(), "delete") || if (tok.strAt(-1) == "delete" ||
Token::simpleMatch(tok.tokAt(-3), "delete [ ]")) { Token::simpleMatch(tok.tokAt(-3), "delete [ ]")) {
dealloc_pointer(checks, &tok); dealloc_pointer(checks, &tok);
return &tok; return &tok;
@ -764,7 +764,7 @@ private:
} }
if (tok.varId()) { if (tok.varId()) {
if (Token::simpleMatch(tok.previous(), "=")) { if (tok.strAt(-1) == "=") {
if (Token::Match(tok.tokAt(-3), "& %var% =")) { if (Token::Match(tok.tokAt(-3), "& %var% =")) {
bailOutVar(checks, tok.varId()); bailOutVar(checks, tok.varId());
return &tok; return &tok;
@ -786,12 +786,12 @@ private:
} }
} }
if (Token::simpleMatch(tok.next(), ".")) { if (tok.strAt(1) == ".") {
bailOutVar(checks, tok.varId()); bailOutVar(checks, tok.varId());
return &tok; return &tok;
} }
if (Token::simpleMatch(tok.next(), "[")) { if (tok.strAt(1) == "[") {
ExecutionPath::bailOutVar(checks, tok.varId()); ExecutionPath::bailOutVar(checks, tok.varId());
return &tok; return &tok;
} }
@ -801,7 +801,7 @@ private:
return &tok; return &tok;
} }
if (Token::simpleMatch(tok.previous(), "&")) { if (tok.strAt(-1) == "&") {
ExecutionPath::bailOutVar(checks, tok.varId()); ExecutionPath::bailOutVar(checks, tok.varId());
} }
} }
@ -1532,7 +1532,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool cpp
const Token *tok2 = vartok->tokAt(-2); const Token *tok2 = vartok->tokAt(-2);
if (tok2 && (tok2->isConstOp() || tok2->str() == "(")) if (tok2 && (tok2->isConstOp() || tok2->str() == "("))
return false; // address of return false; // address of
if (Token::simpleMatch(tok2,")")) if (tok2 && tok2->str() == ")")
tok2 = tok2->link()->previous(); tok2 = tok2->link()->previous();
while (tok2 && tok2->str() == "(") while (tok2 && tok2->str() == "(")
tok2 = tok2->previous(); tok2 = tok2->previous();

View File

@ -593,7 +593,7 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de
// check for alias to struct member // check for alias to struct member
// char c[10]; a.b = c; // char c[10]; a.b = c;
else if (Token::Match(tok->tokAt(-2), "%var% .")) { else if (Token::Match(tok->tokAt(-2), "%var% .")) {
if (Token::Match(tok->tokAt(2), "%var%")) { if (tok->tokAt(2)->varId()) {
unsigned int varid2 = tok->tokAt(2)->varId(); unsigned int varid2 = tok->tokAt(2)->varId();
Variables::VariableUsage *var2 = variables.find(varid2); Variables::VariableUsage *var2 = variables.find(varid2);
@ -717,7 +717,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
if (defValTok && defValTok->str() == "=") { if (defValTok && defValTok->str() == "=") {
if (defValTok->next() && defValTok->next()->str() == "{") { if (defValTok->next() && defValTok->next()->str() == "{") {
for (const Token* tok = defValTok; tok && tok != defValTok->linkAt(1); tok = tok->next()) for (const Token* tok = defValTok; tok && tok != defValTok->linkAt(1); tok = tok->next())
if (Token::Match(tok, "%var%")) // Variables used to initialize the array read. if (tok->varId()) // Variables used to initialize the array read.
variables.read(tok->varId(), i->nameToken()); variables.read(tok->varId(), i->nameToken());
} else } else
doAssignment(variables, i->nameToken(), false, scope); doAssignment(variables, i->nameToken(), false, scope);
@ -873,7 +873,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
variables.read(varid1, tok); variables.read(varid1, tok);
} else { } else {
Variables::VariableUsage *var = variables.find(varid1); Variables::VariableUsage *var = variables.find(varid1);
if (var && Token::simpleMatch(start->previous(), ",")) { if (var && start->strAt(-1) == ",") {
variables.use(varid1, tok); variables.use(varid1, tok);
} else if (var && var->_type == Variables::reference) { } else if (var && var->_type == Variables::reference) {
variables.writeAliases(varid1, tok); variables.writeAliases(varid1, tok);
@ -1007,12 +1007,12 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
variables.use(tok->varId(), tok); // use = read + write variables.use(tok->varId(), tok); // use = read + write
} }
else if (tok->isExtendedOp() && else if (tok->isExtendedOp() && tok->next() &&
Token::Match(tok->next(), "%var%") && !Token::Match(tok->next(), "true|false|new") && tok->strAt(2) != "=") { tok->next()->varId() && !Token::Match(tok->next(), "true|false|new") && tok->strAt(2) != "=") {
variables.readAll(tok->next()->varId(), tok); variables.readAll(tok->next()->varId(), tok);
} }
else if (Token::Match(tok, "%var%") && tok->next() && (tok->next()->str() == ")" || tok->next()->isExtendedOp())) { else if (tok->varId() && tok->next() && (tok->next()->str() == ")" || tok->next()->isExtendedOp())) {
variables.readAll(tok->varId(), tok); variables.readAll(tok->varId(), tok);
} }

View File

@ -2269,7 +2269,7 @@ const Variable *Scope::getVariable(const std::string &varname) const
static const Token* skipScopeIdentifiers(const Token* tok) static const Token* skipScopeIdentifiers(const Token* tok)
{ {
if (Token::simpleMatch(tok, "::")) { if (tok && tok->str() == "::") {
tok = tok->next(); tok = tok->next();
} }
while (Token::Match(tok, "%type% ::")) { while (Token::Match(tok, "%type% ::")) {

View File

@ -798,7 +798,7 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens)
!tok->isName() && !tok->isName() &&
tok->str() != ">" && tok->str() != ">" &&
tok->str() != "]" && tok->str() != "]" &&
!Token::simpleMatch(tok->previous(), "operator") && tok->strAt(-1) != "operator" &&
!Token::simpleMatch(tok->previous(), "* )") && !Token::simpleMatch(tok->previous(), "* )") &&
!Token::simpleMatch(tok->previous(), ") )") && !Token::simpleMatch(tok->previous(), ") )") &&
!Token::Match(tok->tokAt(-2), "* %var% )") && !Token::Match(tok->tokAt(-2), "* %var% )") &&

View File

@ -2616,7 +2616,7 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::map<std::stri
if (executableScope && ref) { if (executableScope && ref) {
if (tok2->str() == "(" || tok2->str() == "=") if (tok2->str() == "(" || tok2->str() == "=")
; // reference is assigned => ok ; // reference is assigned => ok
else if (tok2->str() != ")" || !Token::simpleMatch(tok2->link()->previous(), "catch")) else if (tok2->str() != ")" || tok2->link()->strAt(-1) != "catch")
return false; // not catching by reference => not declaration return false; // not catching by reference => not declaration
} }
} }
@ -2790,9 +2790,9 @@ void Tokenizer::setVarId()
} else if (tok->str() == "{") { } else if (tok->str() == "{") {
initlist = false; initlist = false;
// parse anonymous unions as part of the current scope // parse anonymous unions as part of the current scope
if (!(Token::simpleMatch(tok->previous(), "union") && Token::simpleMatch(tok->link(), "} ;"))) { if (!(tok->strAt(-1) == "union" && Token::simpleMatch(tok->link(), "} ;"))) {
scopestartvarid.push(_varId); scopestartvarid.push(_varId);
if (Token::simpleMatch(tok->previous(), ")") || Token::Match(tok->tokAt(-2), ") %type%")) { if (tok->strAt(-1) == ")" || Token::Match(tok->tokAt(-2), ") %type%")) {
executableScope.push(true); executableScope.push(true);
} else { } else {
executableScope.push(executableScope.top()); executableScope.push(executableScope.top());
@ -5055,7 +5055,7 @@ void Tokenizer::simplifyVarDecl(bool only_k_r_fpar)
else else
break; break;
if (Token::simpleMatch(tok2->next(), "*")) if (tok2->strAt(1) == "*")
break; break;
tok2 = tok2->next(); tok2 = tok2->next();
@ -6139,7 +6139,7 @@ bool Tokenizer::simplifyKnownVariablesGetData(unsigned int varid, Token **_tok2,
} else { } else {
value = tok2->strAt(2); value = tok2->strAt(2);
valueVarId = tok2->tokAt(2)->varId(); valueVarId = tok2->tokAt(2)->varId();
if (Token::simpleMatch(tok2->next(), "[")) { if (tok2->strAt(1) == "[") {
value = tok2->next()->link()->strAt(2); value = tok2->next()->link()->strAt(2);
valueVarId = 0; valueVarId = 0;
} else if (value == "&") { } else if (value == "&") {
@ -6717,7 +6717,7 @@ bool Tokenizer::simplifyRedundantParentheses()
// Simplify "!!operator !!(%var%|)) ( %num%|%bool% ) %op%|;|,|)" // Simplify "!!operator !!(%var%|)) ( %num%|%bool% ) %op%|;|,|)"
if (Token::Match(tok, "( %bool%|%num% ) %cop%|;|,|)") && if (Token::Match(tok, "( %bool%|%num% ) %cop%|;|,|)") &&
!Token::simpleMatch(tok->tokAt(-2), "operator") && tok->strAt(-2) != "operator" &&
tok->previous() && tok->previous() &&
!tok->previous()->isName() && !tok->previous()->isName() &&
tok->previous()->str() != ")" && tok->previous()->str() != ")" &&
@ -8587,7 +8587,7 @@ void Tokenizer::simplifyKeyword()
if (_settings->standards.c >= Standards::C99) { if (_settings->standards.c >= Standards::C99) {
for (Token *tok = list.front(); tok; tok = tok->next()) { for (Token *tok = list.front(); tok; tok = tok->next()) {
while (Token::simpleMatch(tok, "restrict")) { while (tok && tok->str() == "restrict") {
tok->deleteThis(); tok->deleteThis();
} }
} }