Use Token::simpleMatch where no special patterns are needed
This commit is contained in:
parent
45e5dc20a2
commit
f8e2d50e6f
|
@ -828,7 +828,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
|
||||||
const MathLib::bigint index = MathLib::toLongNumber(tok->strAt(3));
|
const MathLib::bigint index = MathLib::toLongNumber(tok->strAt(3));
|
||||||
if (index < 0 || index >= size)
|
if (index < 0 || index >= size)
|
||||||
{
|
{
|
||||||
if (index > size || !Token::Match(tok->previous(), "& ("))
|
if (index > size || !Token::simpleMatch(tok->previous(), "& ("))
|
||||||
{
|
{
|
||||||
arrayIndexOutOfBounds(tok->next(), size, index);
|
arrayIndexOutOfBounds(tok->next(), size, index);
|
||||||
}
|
}
|
||||||
|
|
|
@ -799,7 +799,7 @@ void CheckClass::noMemset()
|
||||||
if (Token::Match(tstruct->next(), "std :: %type% %var% ;"))
|
if (Token::Match(tstruct->next(), "std :: %type% %var% ;"))
|
||||||
memsetStructError(tok, tok->str(), tstruct->strAt(3));
|
memsetStructError(tok, tok->str(), tstruct->strAt(3));
|
||||||
|
|
||||||
else if (Token::Match(tstruct->next(), "std :: %type% < "))
|
else if (Token::Match(tstruct->next(), "std :: %type% <"))
|
||||||
{
|
{
|
||||||
// backup the type
|
// backup the type
|
||||||
const std::string typestr(tstruct->strAt(3));
|
const std::string typestr(tstruct->strAt(3));
|
||||||
|
@ -1132,7 +1132,7 @@ bool CheckClass::hasAssignSelf(const Token *first, const Token *last, const Toke
|
||||||
{
|
{
|
||||||
for (const Token *tok = first; tok && tok != last; tok = tok->next())
|
for (const Token *tok = first; tok && tok != last; tok = tok->next())
|
||||||
{
|
{
|
||||||
if (Token::Match(tok, "if ("))
|
if (Token::simpleMatch(tok, "if ("))
|
||||||
{
|
{
|
||||||
const Token *tok1 = tok->tokAt(2);
|
const Token *tok1 = tok->tokAt(2);
|
||||||
const Token *tok2 = tok->tokAt(1)->link();
|
const Token *tok2 = tok->tokAt(1)->link();
|
||||||
|
@ -1412,7 +1412,7 @@ bool CheckClass::isMemberVar(const Scope *scope, const Token *tok)
|
||||||
|
|
||||||
while (tok->previous() && !Token::Match(tok->previous(), "}|{|;|public:|protected:|private:|return|:|?"))
|
while (tok->previous() && !Token::Match(tok->previous(), "}|{|;|public:|protected:|private:|return|:|?"))
|
||||||
{
|
{
|
||||||
if (Token::Match(tok->previous(), "* this"))
|
if (Token::simpleMatch(tok->previous(), "* this"))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
tok = tok->previous();
|
tok = tok->previous();
|
||||||
|
|
|
@ -1445,7 +1445,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
|
||||||
// Assignment..
|
// Assignment..
|
||||||
if (varid)
|
if (varid)
|
||||||
{
|
{
|
||||||
if (Token::Match(tok, "= {"))
|
if (Token::simpleMatch(tok, "= {"))
|
||||||
{
|
{
|
||||||
unsigned int indentlevel2 = 0;
|
unsigned int indentlevel2 = 0;
|
||||||
bool use = false;
|
bool use = false;
|
||||||
|
@ -1754,7 +1754,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Token::Match(tok2, "while1 { if { dealloc ; return ; } }"))
|
if (Token::simpleMatch(tok2, "while1 { if { dealloc ; return ; } }"))
|
||||||
{
|
{
|
||||||
tok2->str(";");
|
tok2->str(";");
|
||||||
Token::eraseTokens(tok2, tok2->tokAt(4));
|
Token::eraseTokens(tok2, tok2->tokAt(4));
|
||||||
|
@ -1820,7 +1820,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reduce "if continue ; if continue ;" => "if continue ;"
|
// Reduce "if continue ; if continue ;" => "if continue ;"
|
||||||
else if (Token::Match(tok2->next(), "if continue ; if continue ;"))
|
else if (Token::simpleMatch(tok2->next(), "if continue ; if continue ;"))
|
||||||
{
|
{
|
||||||
Token::eraseTokens(tok2, tok2->tokAt(4));
|
Token::eraseTokens(tok2, tok2->tokAt(4));
|
||||||
done = false;
|
done = false;
|
||||||
|
|
|
@ -617,7 +617,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
|
||||||
if (null && indentlevel == 0)
|
if (null && indentlevel == 0)
|
||||||
{
|
{
|
||||||
// skip all "else" blocks because they are not executed in this execution path
|
// skip all "else" blocks because they are not executed in this execution path
|
||||||
while (Token::Match(tok2, "} else {"))
|
while (Token::simpleMatch(tok2, "} else {"))
|
||||||
tok2 = tok2->tokAt(2)->link();
|
tok2 = tok2->tokAt(2)->link();
|
||||||
null = false;
|
null = false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2705,7 +2705,7 @@ void CheckOther::checkMisusedScopedObject()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Token::Match(tok, "[;{}] %var% (")
|
if (Token::Match(tok, "[;{}] %var% (")
|
||||||
&& Token::Match(tok->tokAt(2)->link(), ") ;")
|
&& Token::simpleMatch(tok->tokAt(2)->link(), ") ;")
|
||||||
&& symbolDatabase->isClassOrStruct(tok->next()->str())
|
&& symbolDatabase->isClassOrStruct(tok->next()->str())
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1038,7 +1038,7 @@ void CheckStl::string_c_str()
|
||||||
string_c_strError(tok);
|
string_c_strError(tok);
|
||||||
}
|
}
|
||||||
else if (Token::Match(tok, "[;{}] %var% = %var% (") &&
|
else if (Token::Match(tok, "[;{}] %var% = %var% (") &&
|
||||||
Token::Match(tok->tokAt(4)->link(), ") . c_str ( ) ;") &&
|
Token::simpleMatch(tok->tokAt(4)->link(), ") . c_str ( ) ;") &&
|
||||||
tok->next()->varId() > 0 &&
|
tok->next()->varId() > 0 &&
|
||||||
pointers.find(tok->next()->varId()) != pointers.end() &&
|
pointers.find(tok->next()->varId()) != pointers.end() &&
|
||||||
Token::findmatch(_tokenizer->tokens(), ("std :: string " + tok->strAt(3) + " (").c_str()))
|
Token::findmatch(_tokenizer->tokens(), ("std :: string " + tok->strAt(3) + " (").c_str()))
|
||||||
|
|
|
@ -1238,7 +1238,7 @@ bool Scope::isVariableDeclaration(const Token* tok, const Token*& vartok, const
|
||||||
const Token* localTypeTok = skipScopeIdentifiers(tok);
|
const Token* localTypeTok = skipScopeIdentifiers(tok);
|
||||||
const Token* localVarTok = NULL;
|
const Token* localVarTok = NULL;
|
||||||
|
|
||||||
if (Token::Match(localTypeTok, "%type% < "))
|
if (Token::Match(localTypeTok, "%type% <"))
|
||||||
{
|
{
|
||||||
const Token* closeTok = NULL;
|
const Token* closeTok = NULL;
|
||||||
bool found = findClosingBracket(localTypeTok->next(), closeTok);
|
bool found = findClosingBracket(localTypeTok->next(), closeTok);
|
||||||
|
|
|
@ -588,7 +588,7 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name)
|
||||||
(tok->previous()->str() == "*" && tok->next()->str() != "(") ||
|
(tok->previous()->str() == "*" && tok->next()->str() != "(") ||
|
||||||
(Token::Match(tok->previous(), "%type%") &&
|
(Token::Match(tok->previous(), "%type%") &&
|
||||||
(!Token::Match(tok->previous(), "return|new|const|friend|public|private|protected|throw|extern") &&
|
(!Token::Match(tok->previous(), "return|new|const|friend|public|private|protected|throw|extern") &&
|
||||||
!Token::Match(tok->tokAt(-2), "friend class"))))
|
!Token::simpleMatch(tok->tokAt(-2), "friend class"))))
|
||||||
{
|
{
|
||||||
// scan backwards for the end of the previous statement
|
// scan backwards for the end of the previous statement
|
||||||
int level = (tok->previous()->str() == "}") ? 1 : 0;
|
int level = (tok->previous()->str() == "}") ? 1 : 0;
|
||||||
|
@ -926,7 +926,7 @@ void Tokenizer::simplifyTypedef()
|
||||||
Token *namespaceStart = 0;
|
Token *namespaceStart = 0;
|
||||||
Token *namespaceEnd = 0;
|
Token *namespaceEnd = 0;
|
||||||
|
|
||||||
if (Token::Match(tok->next(), "::") ||
|
if (Token::simpleMatch(tok->next(), "::") ||
|
||||||
Token::Match(tok->next(), "%type%"))
|
Token::Match(tok->next(), "%type%"))
|
||||||
{
|
{
|
||||||
typeStart = tok->next();
|
typeStart = tok->next();
|
||||||
|
@ -940,13 +940,13 @@ void Tokenizer::simplifyTypedef()
|
||||||
bool atEnd = false;
|
bool atEnd = false;
|
||||||
while (!atEnd)
|
while (!atEnd)
|
||||||
{
|
{
|
||||||
if (Token::Match(tok->tokAt(offset), "::"))
|
if (Token::simpleMatch(tok->tokAt(offset), "::"))
|
||||||
typeEnd = tok->tokAt(offset++);
|
typeEnd = tok->tokAt(offset++);
|
||||||
|
|
||||||
if (Token::Match(tok->tokAt(offset), "%type%") &&
|
if (Token::Match(tok->tokAt(offset), "%type%") &&
|
||||||
tok->tokAt(offset + 1) && !Token::Match(tok->tokAt(offset + 1), "[|;|,|("))
|
tok->tokAt(offset + 1) && !Token::Match(tok->tokAt(offset + 1), "[|;|,|("))
|
||||||
typeEnd = tok->tokAt(offset++);
|
typeEnd = tok->tokAt(offset++);
|
||||||
else if (Token::Match(tok->tokAt(offset), "const ("))
|
else if (Token::simpleMatch(tok->tokAt(offset), "const ("))
|
||||||
{
|
{
|
||||||
typeEnd = tok->tokAt(offset++);
|
typeEnd = tok->tokAt(offset++);
|
||||||
atEnd = true;
|
atEnd = true;
|
||||||
|
@ -1040,7 +1040,7 @@ void Tokenizer::simplifyTypedef()
|
||||||
tok = tok->tokAt(offset);
|
tok = tok->tokAt(offset);
|
||||||
|
|
||||||
// or a function typedef
|
// or a function typedef
|
||||||
else if (Token::Match(tok->tokAt(offset), "("))
|
else if (Token::simpleMatch(tok->tokAt(offset), "("))
|
||||||
{
|
{
|
||||||
// unhandled typedef, skip it and continue
|
// unhandled typedef, skip it and continue
|
||||||
if (typeName->str() == "void")
|
if (typeName->str() == "void")
|
||||||
|
@ -1143,7 +1143,7 @@ void Tokenizer::simplifyTypedef()
|
||||||
|
|
||||||
// function returning pointer to function
|
// function returning pointer to function
|
||||||
else if (Token::Match(tok->tokAt(offset), "( * %type% (") &&
|
else if (Token::Match(tok->tokAt(offset), "( * %type% (") &&
|
||||||
Token::Match(tok->tokAt(offset + 3)->link(), ") ) ("))
|
Token::simpleMatch(tok->tokAt(offset + 3)->link(), ") ) ("))
|
||||||
{
|
{
|
||||||
functionRetFuncPtr = true;
|
functionRetFuncPtr = true;
|
||||||
|
|
||||||
|
@ -1397,8 +1397,8 @@ void Tokenizer::simplifyTypedef()
|
||||||
inTemplate = true;
|
inTemplate = true;
|
||||||
|
|
||||||
// check for operator
|
// check for operator
|
||||||
if (Token::Match(tok2->previous(), "operator") ||
|
if (Token::simpleMatch(tok2->previous(), "operator") ||
|
||||||
Token::Match(tok2->tokAt(-2), "operator const"))
|
Token::simpleMatch(tok2->tokAt(-2), "operator const"))
|
||||||
inOperator = true;
|
inOperator = true;
|
||||||
|
|
||||||
// skip over class or struct in derived class declaration
|
// skip over class or struct in derived class declaration
|
||||||
|
@ -2717,7 +2717,7 @@ void Tokenizer::simplifyTemplates()
|
||||||
ostr << " ";
|
ostr << " ";
|
||||||
ostr << tok3->str();
|
ostr << tok3->str();
|
||||||
}
|
}
|
||||||
if (!Token::Match(tok3, "> ("))
|
if (!Token::simpleMatch(tok3, "> ("))
|
||||||
continue;
|
continue;
|
||||||
s = ostr.str();
|
s = ostr.str();
|
||||||
}
|
}
|
||||||
|
@ -3999,7 +3999,7 @@ void Tokenizer::simplifySizeof()
|
||||||
tok->next()->deleteNext();
|
tok->next()->deleteNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Token::Match(tok->next(), "( * )"))
|
if (Token::simpleMatch(tok->next(), "( * )"))
|
||||||
{
|
{
|
||||||
tok->str(MathLib::toString<unsigned long>(sizeOfType(tok->tokAt(2))));
|
tok->str(MathLib::toString<unsigned long>(sizeOfType(tok->tokAt(2))));
|
||||||
Token::eraseTokens(tok, tok->tokAt(4));
|
Token::eraseTokens(tok, tok->tokAt(4));
|
||||||
|
@ -4711,7 +4711,7 @@ void Tokenizer::simplifyIfAddBraces()
|
||||||
if (!innerIf)
|
if (!innerIf)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (Token::Match(tempToken, "; else if"))
|
if (Token::simpleMatch(tempToken, "; else if"))
|
||||||
;
|
;
|
||||||
else if (Token::Match(tempToken, "; else"))
|
else if (Token::Match(tempToken, "; else"))
|
||||||
innerIf = false;
|
innerIf = false;
|
||||||
|
@ -4754,7 +4754,7 @@ bool Tokenizer::simplifyDoWhileAddBracesHelper(Token *tok)
|
||||||
tok2 = tok3;
|
tok2 = tok3;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (Token::Match(tok3, "do {"))
|
else if (Token::simpleMatch(tok3, "do {"))
|
||||||
{
|
{
|
||||||
// Skip do{}while inside the current "do"
|
// Skip do{}while inside the current "do"
|
||||||
tok3 = tok3->next()->link();
|
tok3 = tok3->next()->link();
|
||||||
|
@ -5758,7 +5758,7 @@ void Tokenizer::simplifyStdType()
|
||||||
tok->isSigned(!isUnsigned);
|
tok->isSigned(!isUnsigned);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Token::Match(tok, "__int8"))
|
if (Token::simpleMatch(tok, "__int8"))
|
||||||
tok->str("char");
|
tok->str("char");
|
||||||
else if (Token::Match(tok, "__int16"))
|
else if (Token::Match(tok, "__int16"))
|
||||||
tok->str("short");
|
tok->str("short");
|
||||||
|
@ -7492,7 +7492,7 @@ bool Tokenizer::duplicateDefinition(Token ** tokPtr, const Token * name)
|
||||||
|
|
||||||
if (end)
|
if (end)
|
||||||
{
|
{
|
||||||
if (Token::Match(end, ") {")) // function parameter ?
|
if (Token::simpleMatch(end, ") {")) // function parameter ?
|
||||||
{
|
{
|
||||||
// look backwards
|
// look backwards
|
||||||
if (tok->previous()->str() == "enum" ||
|
if (tok->previous()->str() == "enum" ||
|
||||||
|
@ -7908,7 +7908,7 @@ void Tokenizer::simplifyEnum()
|
||||||
}
|
}
|
||||||
else if (tok2->str() == "{")
|
else if (tok2->str() == "{")
|
||||||
++level;
|
++level;
|
||||||
else if (!pattern.empty() && ((Token::Match(tok2, "enum") && Token::Match(tok2->next(), pattern.c_str())) || Token::Match(tok2, pattern.c_str())))
|
else if (!pattern.empty() && ((Token::simpleMatch(tok2, "enum") && Token::Match(tok2->next(), pattern.c_str())) || Token::Match(tok2, pattern.c_str())))
|
||||||
{
|
{
|
||||||
simplify = true;
|
simplify = true;
|
||||||
hasClass = true;
|
hasClass = true;
|
||||||
|
@ -8243,7 +8243,7 @@ void Tokenizer::simplifyComma()
|
||||||
|
|
||||||
// We must not accept just any keyword, e.g. accepting int
|
// We must not accept just any keyword, e.g. accepting int
|
||||||
// would cause function parameters to corrupt.
|
// would cause function parameters to corrupt.
|
||||||
if (Token::Match(tok->next(), "delete"))
|
if (Token::simpleMatch(tok->next(), "delete"))
|
||||||
{
|
{
|
||||||
// Handle "delete a, delete b;"
|
// Handle "delete a, delete b;"
|
||||||
tok->str(";");
|
tok->str(";");
|
||||||
|
@ -8566,7 +8566,7 @@ void Tokenizer::simplifyStructInit()
|
||||||
if (Token::simpleMatch(tok2, ", ."))
|
if (Token::simpleMatch(tok2, ", ."))
|
||||||
tok2 = tok2->next();
|
tok2 = tok2->next();
|
||||||
}
|
}
|
||||||
if (!Token::Match(tok2, "} ;"))
|
if (!Token::simpleMatch(tok2, "} ;"))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Known expression format => Perform simplification
|
// Known expression format => Perform simplification
|
||||||
|
|
Loading…
Reference in New Issue