diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index e20253b39..c149d9470 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -396,7 +396,7 @@ static bool for_bailout(const Token * const tok1, unsigned int varid) if (loopTok->varId() == varid) { // Counter variable used inside loop if (Token::Match(loopTok->next(), "+=|-=|++|--|=") || - Token::Match(loopTok->previous(), "++|--")) { + (loopTok->previous()->type() == Token::eIncDecOp)) { return true; } } @@ -558,7 +558,7 @@ void CheckBufferOverrun::checkFunctionParameter(const Token &tok, unsigned int p } } - else if (Token::Match(tok2->next(), ",|)") && tok2->str()[0] == '\'') { + else if (Token::Match(tok2->next(), ",|)") && tok2->type() == Token::eChar) { sizeArgumentAsCharError(tok2); } } else if (arg == 1001) { // special code. This parameter multiplied with the next must not exceed total_size @@ -1574,13 +1574,13 @@ MathLib::bigint CheckBufferOverrun::countSprintfLength(const std::string &input_ break; case 'd': i_d_x_f_found = true; - if (paramIter != parameters.end() && *paramIter && (*paramIter)->str()[0] != '"') + if (paramIter != parameters.end() && *paramIter && (*paramIter)->type() != Token::eString) parameterLength = (*paramIter)->str().length(); handleNextParameter = true; break; case 's': - if (paramIter != parameters.end() && *paramIter && (*paramIter)->str()[0] == '"') + if (paramIter != parameters.end() && *paramIter && (*paramIter)->type() == Token::eString) parameterLength = Token::getStrLength(*paramIter); handleNextParameter = true; @@ -1646,7 +1646,7 @@ void CheckBufferOverrun::checkSprintfCall(const Token *tok, const MathLib::bigin const Token* vaArg = tok->tokAt(2)->nextArgument()->nextArgument(); while (vaArg) { if (Token::Match(vaArg->next(), "[,)]")) { - if (vaArg->str()[0] == '"') // %str% + if (vaArg->type() == Token::eString) parameters.push_back(vaArg); else if (vaArg->isNumber()) @@ -2064,7 +2064,7 @@ void CheckBufferOverrun::arrayIndexThenCheck() return; // skip comparison - if (Token::Match(tok, "==|!=|<|<=|>|>= %any% &&")) + if (tok->type() == Token::eComparisionOp && tok->strAt(2) == "&&") tok = tok->tokAt(2); // check if array index is ok diff --git a/lib/checkinternal.cpp b/lib/checkinternal.cpp index e7cc8beff..55ab3de25 100644 --- a/lib/checkinternal.cpp +++ b/lib/checkinternal.cpp @@ -39,7 +39,7 @@ void CheckInternal::checkTokenMatchPatterns() // Get pattern string const Token *pattern_tok = tok->tokAt(4)->nextArgument(); - if (!pattern_tok || !Token::Match(pattern_tok, "%str%")) + if (!pattern_tok || pattern_tok->type() != Token::eString) continue; const std::string pattern = pattern_tok->strValue(); @@ -68,7 +68,7 @@ void CheckInternal::checkTokenSimpleMatchPatterns() // Get pattern string const Token *pattern_tok = tok->tokAt(4)->nextArgument(); - if (!pattern_tok || !Token::Match(pattern_tok, "%str%")) + if (!pattern_tok || pattern_tok->type() != Token::eString) continue; const std::string pattern = pattern_tok->strValue(); @@ -140,7 +140,7 @@ void CheckInternal::checkMissingPercentCharacter() // Get pattern string const Token *pattern_tok = tok->tokAt(4)->nextArgument(); - if (!pattern_tok || !Token::Match(pattern_tok, "%str%")) + if (!pattern_tok || pattern_tok->type() != Token::eString) continue; const std::string pattern = pattern_tok->strValue(); diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 4baad6893..52c337370 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -152,7 +152,7 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::liststr()[0] == '"') { + if (formatStringTok && formatStringTok->type() == Token::eString) { argListTok = formatStringTok->nextArgument(); // Find third parameter (first argument of va_args) formatString = formatStringTok->strValue(); } @@ -161,7 +161,7 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::listnextArgument(); // Find third parameter (format string) } - if (formatStringTok && formatStringTok->str()[0] == '"') { + if (formatStringTok && formatStringTok->type() == Token::eString) { argListTok = formatStringTok->nextArgument(); // Find fourth parameter (first argument of va_args) formatString = formatStringTok->strValue(); } @@ -616,7 +616,7 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec() continue; // name of struct pointer - const std::string varname(tok1->str()); + const std::string& varname(tok1->str()); // is pointer local? bool isLocal = false; diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 0e621eef2..6b89e7696 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -153,7 +153,7 @@ void CheckOther::clarifyCondition() for (const Token *tok2 = tok->tokAt(3); tok2; tok2 = tok2->next()) { if (tok2->str() == "(" || tok2->str() == "[") tok2 = tok2->link(); - else if (Token::Match(tok2, "<|<=|==|!=|>|>=")) { + else if (tok2->type() == Token::eComparisionOp) { // This might be a template if (!_tokenizer->isC() && Token::Match(tok2->previous(), "%var% <")) break; @@ -964,7 +964,7 @@ void CheckOther::checkAssignmentInAssert() while (tok && endTok) { for (tok = tok->tokAt(2); tok != endTok; tok = tok->next()) { - if (tok->isName() && (tok->next()->isAssignmentOp() || tok->next()->str() == "++" || tok->next()->str() == "--")) + if (tok->isName() && (tok->next()->isAssignmentOp() || tok->next()->type() == Token::eIncDecOp)) assignmentInAssertError(tok, tok->str()); else if (Token::Match(tok, "--|++ %var%")) assignmentInAssertError(tok, tok->strAt(1)); @@ -1350,7 +1350,7 @@ void CheckOther::invalidScanf() formatToken = tok->tokAt(2); else if (Token::Match(tok, "sscanf|vsscanf|fscanf|vfscanf (")) { const Token* nextArg = tok->tokAt(2)->nextArgument(); - if (nextArg && Token::Match(nextArg, "%str%")) + if (nextArg && nextArg->type() == Token::eString) formatToken = nextArg; else continue; @@ -1460,7 +1460,7 @@ void CheckOther::checkWrongPrintfScanfArguments() } else { continue; } - } else if (Token::Match(tok, "snprintf|fnprintf ( %any%")) { + } else if (Token::Match(tok, "snprintf|fnprintf (")) { const Token* formatStringTok = tok->tokAt(2); for (int i = 0; i < 2 && formatStringTok; i++) { formatStringTok = formatStringTok->nextArgument(); // Find third parameter (format string) @@ -1536,11 +1536,11 @@ void CheckOther::checkWrongPrintfScanfArguments() } else if (!scan) { switch (*i) { case 's': - if (variableInfo && !Token::Match(argListTok, "%str%") && isKnownType(variableInfo, varTypeTok) && (!variableInfo->isPointer() && !variableInfo->isArray())) + if (variableInfo && argListTok->type() != Token::eString && isKnownType(variableInfo, varTypeTok) && (!variableInfo->isPointer() && !variableInfo->isArray())) invalidPrintfArgTypeError_s(tok, numFormat); break; case 'n': - if ((varTypeTok && isKnownType(variableInfo, varTypeTok) && ((!variableInfo->isPointer() && !variableInfo->isArray()) || varTypeTok->str() == "const")) || Token::Match(argListTok, "%str%")) + if ((varTypeTok && isKnownType(variableInfo, varTypeTok) && ((!variableInfo->isPointer() && !variableInfo->isArray()) || varTypeTok->str() == "const")) || argListTok->type() == Token::eString) invalidPrintfArgTypeError_n(tok, numFormat); break; case 'c': @@ -1554,7 +1554,7 @@ void CheckOther::checkWrongPrintfScanfArguments() varTypeTok = varTypeTok->next(); if ((varTypeTok && isKnownType(variableInfo, varTypeTok) && !Token::Match(varTypeTok, "unsigned|signed| bool|short|long|int|char|size_t|unsigned|signed") && !variableInfo->isPointer() && !variableInfo->isArray())) invalidPrintfArgTypeError_int(tok, numFormat, *i); - else if (Token::Match(argListTok, "%str%")) + else if (argListTok->type() == Token::eString) invalidPrintfArgTypeError_int(tok, numFormat, *i); break; case 'p': @@ -1562,7 +1562,7 @@ void CheckOther::checkWrongPrintfScanfArguments() varTypeTok = varTypeTok->next(); if (varTypeTok && isKnownType(variableInfo, varTypeTok) && !Token::Match(varTypeTok, "unsigned|signed| short|long|int|size_t|unsigned|signed") && !variableInfo->isPointer() && !variableInfo->isArray()) invalidPrintfArgTypeError_p(tok, numFormat); - else if (Token::Match(argListTok, "%str%")) + else if (argListTok->type() == Token::eString) invalidPrintfArgTypeError_p(tok, numFormat); break; case 'e': @@ -1574,7 +1574,7 @@ void CheckOther::checkWrongPrintfScanfArguments() varTypeTok = varTypeTok->next(); if (varTypeTok && ((isKnownType(variableInfo, varTypeTok) && !Token::Match(varTypeTok, "float|double")) || variableInfo->isPointer() || variableInfo->isArray())) invalidPrintfArgTypeError_float(tok, numFormat, *i); - else if (Token::Match(argListTok, "%str%")) + else if (argListTok->type() == Token::eString) invalidPrintfArgTypeError_float(tok, numFormat, *i); break; default: @@ -1680,7 +1680,7 @@ void CheckOther::checkComparisonOfBoolWithInt() const SymbolDatabase* const symbolDatabase = _tokenizer->getSymbolDatabase(); for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { - if (Token::Match(tok->next(), ">|>=|==|!=|<=|<") && (!tok->previous() || !tok->previous()->isArithmeticalOp()) && (!tok->tokAt(3) || !tok->tokAt(3)->isArithmeticalOp())) { + if (tok->next() && tok->next()->type() == Token::eComparisionOp && (!tok->previous() || !tok->previous()->isArithmeticalOp()) && (!tok->tokAt(3) || !tok->tokAt(3)->isArithmeticalOp())) { const Token* const right = tok->tokAt(2); if ((tok->varId() && right->isNumber()) || (tok->isNumber() && right->varId())) { // Comparing variable with number const Token* varTok = tok; @@ -2167,7 +2167,7 @@ void CheckOther::checkCharVariable() charArrayIndexError(tok->next()); } - else if (Token::Match(tok, "[;{}] %var% = %any% [&|] %any% ;")) { + else if (Token::Match(tok, "[;{}] %var% = %any% [&^|] %any% ;")) { // is a char variable used in the calculation? if (!isSignedChar(symbolDatabase->getVariableFromVarId(tok->tokAt(3)->varId())) && !isSignedChar(symbolDatabase->getVariableFromVarId(tok->tokAt(5)->varId()))) @@ -2187,7 +2187,7 @@ void CheckOther::checkCharVariable() charBitOpError(tok->tokAt(4)); // This is an error.. } - else if (Token::Match(tok, "[;{}] %var% = %any% [&|] ( * %var% ) ;")) { + else if (Token::Match(tok, "[;{}] %var% = %any% [&^|] ( * %var% ) ;")) { const Variable* var = symbolDatabase->getVariableFromVarId(tok->tokAt(7)->varId()); if (!var || !var->isPointer()) continue; @@ -2530,7 +2530,7 @@ static bool expressionHasSideEffects(const Token *first, const Token *last) return true; // check for inc/dec - else if (Token::Match(tok, "++|--")) + else if (tok->type() == Token::eIncDecOp) return true; // check for function call @@ -2960,7 +2960,7 @@ void CheckOther::checkExpressionRange(const std::list &constFunctions, break; } --brackets; - } else if (tok->str() == "++" || tok->str() == "--") { + } else if (tok->type() == Token::eIncDecOp) { valid = false; break; } diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 3f510a6df..4c6988bfc 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -921,9 +921,8 @@ void CheckStl::size() } // check for using as boolean expression - else if (tok->strAt(-1) == "!" || - (Token::Match(tok->tokAt(-2), "if|while (") && end->str() == ")") || - (Token::Match(tok->previous(), "&&|%oror%") && Token::Match(end, "&&|)|%oror%"))) { + else if ((Token::Match(tok->tokAt(-2), "if|while (") && end->str() == ")") || + (tok->previous()->type() == Token::eLogicalOp && Token::Match(end, "&&|)|,|;|%oror%"))) { if (isStlContainer(varid)) sizeError(tok1); } diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 3e09f9028..810c51239 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -509,7 +509,7 @@ private: return &tok; } - if (Token::Match(tok.previous(), "++|--") || Token::Match(tok.next(), "++|--")) { + if ((tok.previous() && tok.previous()->type() == Token::eIncDecOp) || (tok.next() && tok.next()->type() == Token::eIncDecOp)) { use(checks, &tok); return &tok; } @@ -961,8 +961,8 @@ public: } else if (indentlevel == 0 && tok3->str() == ";") break; else if (indentlevel >= 1 && tok3->varId() == varid) { - if (Token::Match(tok3->previous(), "++|--") || - Token::Match(tok3->next(), "++|--")) { + if (tok3->previous()->type() == Token::eIncDecOp || + tok3->next()->type() == Token::eIncDecOp) { r = true; } diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 19c8f4c38..5dbef87df 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -738,12 +738,12 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const else if (tok->str() == "(") tok = tok->next(); - if (Token::Match(tok, "++|--")) { + if (tok->type() == Token::eIncDecOp) { pre = true; tok = tok->next(); } - if (Token::Match(tok->next(), "++|--")) + if (tok->next()->type() == Token::eIncDecOp) post = true; const unsigned int varid1 = tok->varId(); diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 898878b07..800eff0e0 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -731,7 +731,7 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens) ret = true; } - if (tok->str()[0] == '\'' && tok->str().size() == 3 && + if (tok->type() == Token::eChar && Token::Match(tok->previous(), "(|&&|%oror% %any% ==|!=|<=|<|>=|> %num% &&|%oror%|)")) { tok->str(MathLib::toString(tok->str()[1] & 0xff)); } diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 047e38592..e44b36f8d 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -172,7 +172,7 @@ unsigned int Tokenizer::sizeOfType(const Token *type) const if (!type || type->str().empty()) return 0; - if (type->str()[0] == '"') + if (type->type() == Token::eString) return static_cast(Token::getStrLength(type) + 1); std::map::const_iterator it = _typeSize.find(type->str()); @@ -3315,7 +3315,7 @@ void Tokenizer::simplifySizeof() } // sizeof 'x' - if (tok->next() && tok->next()->str()[0] == '\'') { + if (tok->next()->type() == Token::eChar) { tok->deleteThis(); std::ostringstream sz; sz << sizeof 'x'; @@ -3335,7 +3335,7 @@ void Tokenizer::simplifySizeof() } // sizeof "text" - if (Token::Match(tok->next(), "%str%")) { + if (tok->next()->type() == Token::eString) { tok->deleteThis(); std::ostringstream ostr; ostr << (Token::getStrLength(tok) + 1); @@ -3396,7 +3396,7 @@ void Tokenizer::simplifySizeof() // We are checking a class or struct, search next varname tempToken = tempToken->next(); continue; - } else if (Token::Match(tempToken->next(), "++|--")) { + } else if (tempToken->next()->type() == Token::eIncDecOp) { // We have variable++ or variable--, there should be // nothing after this tempToken = tempToken->tokAt(2); @@ -3497,7 +3497,7 @@ bool Tokenizer::simplifyTokenList() // Combine wide strings for (Token *tok = _tokens; tok; tok = tok->next()) { - while (tok->str() == "L" && tok->next() && tok->next()->str()[0] == '"') { + while (tok->str() == "L" && tok->next() && tok->next()->type() == Token::eString) { // Combine 'L "string"' tok->str(tok->next()->str()); tok->deleteNext(); @@ -3510,7 +3510,7 @@ bool Tokenizer::simplifyTokenList() continue; tok->str(simplifyString(tok->str())); - while (tok->next() && tok->next()->str()[0] == '"') { + while (tok->next() && tok->next()->type() == Token::eString) { tok->next()->str(simplifyString(tok->next()->str())); // Two strings after each other, combine them @@ -4301,7 +4301,7 @@ void Tokenizer::simplifyCompoundAssignment() for (Token *tok2 = tok->previous(); tok2 && tok2 != tok1; tok2 = tok2->previous()) { // Don't duplicate ++ and --. Put preincrement in lhs. Put // postincrement in rhs. - if (tok2->str() == "++" || tok2->str() == "--") { + if (tok2->type() == Token::eIncDecOp) { // pre increment/decrement => don't copy if (tok2->next()->isName()) { continue;