From 6e2f2816de1da5b699cd42d843e934b8e4730220 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Sun, 6 Nov 2011 14:28:19 +0100 Subject: [PATCH] Improve same expression check: remove '%op%' pattern and add ',' for the same expressions as an argument inside a function. Improve compound assignment simplification: use already defined 'isAssignmentOp' and extend the adding parenthesis to a generic operator, not only to the arithmetical ones. See: http://en.cppreference.com/w/cpp/language/operator_precedence --- lib/checkother.cpp | 4 ++-- lib/tokenize.cpp | 6 +++--- test/testother.cpp | 2 +- test/testtokenize.cpp | 4 ++++ 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index ef4a14fe2..953a1ef82 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2346,7 +2346,7 @@ void CheckOther::checkDuplicateExpression() continue; for (const Token *tok = scope->classStart; tok && tok != scope->classStart->link(); tok = tok->next()) { - if (Token::Match(tok, "=|%op%|return|(|&&|%oror% %var% &&|%oror%|==|!=|<=|>=|<|>|-|%or% %var% )|&&|%oror%|;") && + if (Token::Match(tok, ",|=|return|(|&&|%oror% %var% &&|%oror%|==|!=|<=|>=|<|>|-|%or% %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()) { @@ -2358,7 +2358,7 @@ void CheckOther::checkDuplicateExpression() } duplicateExpressionError(tok->next(), tok->tokAt(3), tok->strAt(2)); - } else if (Token::Match(tok, "=|%op%|return|(|&&|%oror% %var% . %var% &&|%oror%|==|!=|<=|>=|<|>|-|%or% %var% . %var% )|&&|%oror%") && + } else if (Token::Match(tok, ",|=|return|(|&&|%oror% %var% . %var% &&|%oror%|==|!=|<=|>=|<|>|-|%or% %var% . %var% )|&&|%oror%") && tok->strAt(1) == tok->strAt(5) && tok->strAt(3) == tok->strAt(7)) { duplicateExpressionError(tok->next(), tok->tokAt(6), tok->strAt(4)); } diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 2034af759..a47bf43a4 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4994,9 +4994,9 @@ void Tokenizer::simplifyCompoundAssignment() // Is current token at a compound assignment: +=|-=|.. ? const std::string &str = tok->str(); std::string op; // operator used in assignment - if (str.size() == 2 && str[1] == '=' && str.find_first_of("+-*/%&|^")==0) + if (tok->isAssignmentOp() && str.size() == 2) op = str.substr(0, 1); - else if (str=="<<=" || str==">>=") + else if (tok->isAssignmentOp() && str.size() == 3) op = str.substr(0, 2); else { tok = tok1; @@ -5029,7 +5029,7 @@ void Tokenizer::simplifyCompoundAssignment() break; } - someOperator |= bool(tok2->isArithmeticalOp() || (tok2->str() == "?")); + someOperator |= (tok2->isOp() || (tok2->str() == "?")); } } diff --git a/test/testother.cpp b/test/testother.cpp index b0af1fca7..5fe2f708c 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3428,7 +3428,7 @@ private: ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '&&'.\n", errout.str()); check("void foo() {\n" - " a = a + b && b;\n" + " f(a,b && b);\n" "}"); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '&&'.\n", errout.str()); } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index bb5aab0c4..5597d66b6 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -5665,6 +5665,10 @@ private: // #2714 - wrong simplification of "a += b?c:d;" ASSERT_EQUALS("; a = a + ( b ? c : d ) ;", tokenizeAndStringify("; a+=b?c:d;")); ASSERT_EQUALS("; a = a * ( b + 1 ) ;", tokenizeAndStringify("; a*=b+1;")); + + ASSERT_EQUALS("; a = a + ( b && c ) ;", tokenizeAndStringify("; a+=b&&c;")); + ASSERT_EQUALS("; a = a * ( b || c ) ;", tokenizeAndStringify("; a*=b||c;")); + ASSERT_EQUALS("; a = a | ( b == c ) ;", tokenizeAndStringify("; a|=b==c;")); } void simplifyAssignmentInFunctionCall() {