From 5101f3c029907e97363bef58984d9c3cd698536a Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Sat, 1 Dec 2012 01:31:35 +0100 Subject: [PATCH] Use the new pattern: '%comp%' where possible. Change also the description comment of the Token::Match by adding the new pattern and the forgotten '%op%'. --- lib/checkbufferoverrun.cpp | 2 +- lib/checkother.cpp | 28 ++++++++++++---------------- lib/templatesimplifier.cpp | 7 +++---- lib/token.h | 2 ++ lib/tokenize.cpp | 6 +++--- 5 files changed, 21 insertions(+), 24 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 0918fccc3..633c579b7 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -436,7 +436,7 @@ void CheckBufferOverrun::parse_for_body(const Token *tok, const ArrayInfo &array // does condition check counter variable? bool usesCounter = false; const Token *tok3 = tok2->previous(); - while (Token::Match(tok3, "%var%|%num%|)|>=|>|<=|<|==|!=")) { + while (Token::Match(tok3, "%comp%|%num%|%var%|)")) { if (tok3->str() == strindex) { usesCounter = true; break; diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 4edcca8c5..5a9a7c7b4 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -184,7 +184,7 @@ void CheckOther::clarifyCondition() for (std::size_t i = 0; i < functions; ++i) { const Scope * scope = symbolDatabase->functionScopes[i]; for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { - if (Token::Match(tok, "!|<|<=|==|!=|>|>=")) { + if (Token::Match(tok, "%comp%|!")) { if (tok->link()) // don't write false positives when templates are used continue; @@ -1226,25 +1226,21 @@ void CheckOther::checkIncorrectLogicOperator() const Token *term1Tok = NULL, *term2Tok = NULL; const Token *op1Tok = NULL, *op2Tok = NULL, *op3Tok = NULL, *nextTok = NULL; - if (Token::Match(tok, "( %any% %op% %any% ) &&|%oror%") && - tok->tokAt(2)->isComparisonOp()) { + if (Token::Match(tok, "( %any% %comp% %any% ) &&|%oror%")) { term1Tok = tok->next(); op1Tok = tok->tokAt(2); op2Tok = tok->tokAt(5); - } else if (Token::Match(tok, "%any% %op% %any% &&|%oror%") && - tok->tokAt(1)->isComparisonOp()) { + } else if (Token::Match(tok, "%any% %comp% %any% &&|%oror%")) { term1Tok = tok; op1Tok = tok->next(); op2Tok = tok->tokAt(3); } if (op2Tok) { - if (Token::Match(op2Tok->next(), "( %any% %op% %any% ) %any%") && - op2Tok->tokAt(3)->isComparisonOp()) { + if (Token::Match(op2Tok->next(), "( %any% %comp% %any% ) %any%")) { term2Tok = op2Tok->tokAt(2); op3Tok = op2Tok->tokAt(3); nextTok = op2Tok->tokAt(6); - } else if (Token::Match(op2Tok->next(), "%any% %op% %any% %any%") && - op2Tok->tokAt(2)->isComparisonOp()) { + } else if (Token::Match(op2Tok->next(), "%any% %comp% %any% %any%")) { term2Tok = op2Tok->next(); op3Tok = op2Tok->tokAt(2); nextTok = op2Tok->tokAt(4); @@ -3061,7 +3057,7 @@ void CheckOther::checkDuplicateExpression() complexDuplicateExpressionCheck(constFunctions, scope->classStart, "&&", "%oror%|%or%"); for (const Token *tok = scope->classStart; tok && tok != scope->classStart->link(); tok = tok->next()) { - if (Token::Match(tok, ",|=|return|(|&&|%oror% %var% ==|!=|<=|>=|<|>|- %var% )|&&|%oror%|;|,") && + if (Token::Match(tok, ",|=|return|(|&&|%oror% %var% %comp%|- %var% )|&&|%oror%|;|,") && tok->strAt(1) == tok->strAt(3)) { // float == float and float != float are valid NaN checks if (Token::Match(tok->tokAt(2), "==|!=") && tok->next()->varId()) { @@ -3078,7 +3074,7 @@ void CheckOther::checkDuplicateExpression() continue; duplicateExpressionError(tok->next(), tok->tokAt(3), tok->strAt(2)); - } else if (Token::Match(tok, ",|=|return|(|&&|%oror% %var% . %var% ==|!=|<=|>=|<|>|- %var% . %var% )|&&|%oror%|;|,") && + } else if (Token::Match(tok, ",|=|return|(|&&|%oror% %var% . %var% %comp%|- %var% . %var% )|&&|%oror%|;|,") && tok->strAt(1) == tok->strAt(5) && tok->strAt(3) == tok->strAt(7)) { // If either variable token is an expanded macro then @@ -3224,7 +3220,7 @@ void CheckOther::checkModuloAlwaysTrueFalse() for (std::size_t i = 0; i < functions; ++i) { const Scope * scope = symbolDatabase->functionScopes[i]; for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { - if ((Token::Match(tok, "% %num% %op% %num%") && tok->tokAt(2)->isComparisonOp()) && + if ((Token::Match(tok, "% %num% %comp% %num%")) && (!tok->tokAt(4) || !tok->tokAt(4)->isArithmeticalOp())) { if (MathLib::isLessEqual(tok->strAt(1), tok->strAt(3))) moduloAlwaysTrueFalseError(tok, tok->strAt(1)); @@ -3376,24 +3372,24 @@ void CheckOther::checkComparisonOfBoolExpressionWithInt() const Token* numTok = 0; const Token* opTok = 0; char op = 0; - if (Token::Match(tok, "&&|%oror% %any% ) %op% %any%") && tok->tokAt(3)->isComparisonOp()) { + if (Token::Match(tok, "&&|%oror% %any% ) %comp% %any%")) { numTok = tok->tokAt(4); opTok = tok->tokAt(3); if (Token::Match(opTok, "<|>")) op = opTok->str()[0]; - } else if (Token::Match(tok, "%any% %op% ( %any% &&|%oror%") && tok->next()->isComparisonOp()) { + } else if (Token::Match(tok, "%any% %comp% ( %any% &&|%oror%")) { numTok = tok; opTok = tok->next(); if (Token::Match(opTok, "<|>")) op = opTok->str()[0]=='>'?'<':'>'; } - else if (Token::Match(tok, "! %var% %op% %any%") && tok->tokAt(2)->isComparisonOp()) { + else if (Token::Match(tok, "! %var% %comp% %any%")) { numTok = tok->tokAt(3); opTok = tok->tokAt(2); if (Token::Match(opTok, "<|>")) op = opTok->str()[0]; - } else if (Token::Match(tok, "%any% %op% ! %var%") && tok->next()->isComparisonOp()) { + } else if (Token::Match(tok, "%any% %comp% ! %var%")) { numTok = tok; opTok = tok->next(); if (Token::Match(opTok, "<|>")) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index d80c68c97..cb1d66a6a 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -694,7 +694,7 @@ static bool isLowerThanAnd(const Token* lower) } static bool isLowerThanShift(const Token* lower) { - return isLowerThanAnd(lower) || Token::Match(lower, "&|<|<=|>|>=|==|!="); + return isLowerThanAnd(lower) || Token::Match(lower, "%comp%|&"); } static bool isLowerThanPlusMinus(const Token* lower) { @@ -808,8 +808,7 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens) ret = true; } - if (tok->type() == Token::eChar && - Token::Match(tok->previous(), "(|&&|%oror% %any% ==|!=|<=|<|>=|> %num% &&|%oror%|)")) { + if (Token::Match(tok->previous(), "(|&&|%oror% %char% %comp% %num% &&|%oror%|)")) { tok->str(MathLib::longToString(tok->str()[1] & 0xff)); } @@ -921,7 +920,7 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens) ret = true; } - if (Token::Match(tok, "%num% ==|!=|<=|>=|<|> %num%") && + if (Token::Match(tok, "%num% %comp% %num%") && MathLib::isInt(tok->str()) && MathLib::isInt(tok->strAt(2))) { if (Token::Match(tok->previous(), "(|&&|%oror%") && Token::Match(tok->tokAt(3), ")|&&|%oror%")) { diff --git a/lib/token.h b/lib/token.h index 14037ad92..d6c89460c 100644 --- a/lib/token.h +++ b/lib/token.h @@ -124,8 +124,10 @@ public: * - "%num%" Any numeric token, e.g. "23" * - "%bool%" true or false * - "%char%" Any token enclosed in '-character. + * - "%comp%" Any token such that isComparisonOp() returns true. * - "%str%" Any token starting with "-character (C-string). * - "%varid%" Match with parameter varid + * - "%op%" Any token such that isOp() returns true. * - "%or%" A bitwise-or operator '|' * - "%oror%" A logical-or operator '||' * - "[abc]" Any of the characters 'a' or 'b' or 'c' diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index b9b86202b..b4eeb831b 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6260,9 +6260,9 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign } // Using the variable in condition.. - if (Token::Match(tok3->previous(), ("if ( " + structname + " %varid% ==|!=|<|<=|>|>=|)").c_str(), varid) || - Token::Match(tok3, ("( " + structname + " %varid% ==|!=|<|<=|>|>=").c_str(), varid) || - Token::Match(tok3, ("!|==|!=|<|<=|>|>= " + structname + " %varid% ==|!=|<|<=|>|>=|)|;").c_str(), varid) || + if (Token::Match(tok3->previous(), ("if ( " + structname + " %varid% %comp%|)").c_str(), varid) || + Token::Match(tok3, ("( " + structname + " %varid% %comp%").c_str(), varid) || + Token::Match(tok3, ("%comp%|! " + structname + " %varid% %comp%|)|;").c_str(), varid) || Token::Match(tok3->previous(), "strlen|free ( %varid% )", varid)) { if (value[0] == '\"' && tok3->previous()->str() != "strlen") { // bail out if value is a string unless if it's just given