From f6d910ab3db5a8df74568e22def3140e2a532940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 9 Apr 2011 15:54:36 +0200 Subject: [PATCH] Refactoring: Use Token::isOp --- lib/checkbufferoverrun.cpp | 2 +- lib/checkmemoryleak.cpp | 2 +- lib/token.h | 14 +++++++++----- lib/tokenize.cpp | 10 ++-------- test/testsimplifytokens.cpp | 2 +- 5 files changed, 14 insertions(+), 16 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index b35f2eddb..9aa809b34 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -707,7 +707,7 @@ void CheckBufferOverrun::checkFunctionParameter(const Token &tok, unsigned int p Token::Match(ftok, "%var% --")) break; - if (Token::Match(ftok->previous(), "[=+-*/;{}] %var% [ %num% ]")) + if ((Token::Match(ftok->previous(), "[=;{}]") || ftok->previous()->isOp()) && Token::Match(ftok, "%var% [ %num% ]")) { const MathLib::bigint index = MathLib::toLongNumber(ftok->strAt(2)); if (index >= 0 && arrayInfo.num[0] > 0 && index >= arrayInfo.num[0]) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index b54d3e766..9aa5df0b6 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -1492,7 +1492,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listprevious(), "[;{}=(,+-*/] %varid% [", varid)) + else if ((Token::Match(tok->previous(), "[;{}=(,]") || tok->previous()->isOp()) && Token::Match(tok, "%varid% [", varid)) { // warning is written for "dealloc ; use_ ;". // but this use doesn't affect the leak-checking diff --git a/lib/token.h b/lib/token.h index 728e6f8d6..69d96e918 100644 --- a/lib/token.h +++ b/lib/token.h @@ -153,12 +153,17 @@ public: { _isNumber = number; } + bool isArithmeticalOp() const + { + return (this && (_str=="<<" || _str==">>" || (_str.size()==1 && _str.find_first_of("+-*/%") != std::string::npos))); + } bool isOp() const { if (!this) return false; - return (_str == "&&" || + return (isArithmeticalOp() || + _str == "&&" || _str == "||" || _str == "==" || _str == "!=" || @@ -166,13 +171,12 @@ public: _str == "<=" || _str == ">" || _str == ">=" || - _str == "<<" || - _str == ">>" || - Token::Match(this, "[+-*/%&|^~!]")); + (_str.size() == 1 && _str.find_first_of("&|^~!") != std::string::npos)); } bool isExtendedOp() const { - return isOp() || Match(this, "[,[]()?:]"); + return isOp() || + (this && _str.size() == 1 && _str.find_first_of(",[]()?:") != std::string::npos); } bool isBoolean() const { diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 747dc0c56..4c46e11ff 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2075,15 +2075,9 @@ bool Tokenizer::tokenize(std::istream &code, // Combine "- %num%" .. for (Token *tok = _tokens; tok; tok = tok->next()) { - if (Token::Match(tok, "[([+-*/=,] - %num%") && tok->strAt(2)[0] != '-') + if (Token::Match(tok, "%any% - %num%") && (tok->isOp() || Token::Match(tok, "?|:|,|(|[|=|return|case"))) { - tok->next()->str(std::string("-") + tok->strAt(2)); - tok->next()->deleteNext(); - } - - if (Token::Match(tok, "return|case - %num%") && tok->strAt(2)[0] != '-') - { - tok->next()->str(std::string("-") + tok->strAt(2)); + tok->next()->str("-" + tok->strAt(2)); tok->next()->deleteNext(); } } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index e24d094ae..53cdf121f 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -3772,7 +3772,7 @@ private: const char expected[] = "; " - "int a [ ice_or < is_int < int > :: value , is_int < UDT > :: value > :: value ? 1 : - 1 ] ; " + "int a [ ice_or < is_int < int > :: value , is_int < UDT > :: value > :: value ? 1 : -1 ] ; " "int a1 [ N ] ; " "int a2 [ N ] [ M ] ; " "int t ; "