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
This commit is contained in:
Edoardo Prezioso 2011-11-06 14:28:19 +01:00
parent f092779a4d
commit 6e2f2816de
4 changed files with 10 additions and 6 deletions

View File

@ -2346,7 +2346,7 @@ void CheckOther::checkDuplicateExpression()
continue; continue;
for (const Token *tok = scope->classStart; tok && tok != scope->classStart->link(); tok = tok->next()) { 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)) { tok->strAt(1) == tok->strAt(3)) {
// float == float and float != float are valid NaN checks // float == float and float != float are valid NaN checks
if (Token::Match(tok->tokAt(2), "==|!=") && tok->next()->varId()) { 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)); 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)) { tok->strAt(1) == tok->strAt(5) && tok->strAt(3) == tok->strAt(7)) {
duplicateExpressionError(tok->next(), tok->tokAt(6), tok->strAt(4)); duplicateExpressionError(tok->next(), tok->tokAt(6), tok->strAt(4));
} }

View File

@ -4994,9 +4994,9 @@ void Tokenizer::simplifyCompoundAssignment()
// Is current token at a compound assignment: +=|-=|.. ? // Is current token at a compound assignment: +=|-=|.. ?
const std::string &str = tok->str(); const std::string &str = tok->str();
std::string op; // operator used in assignment 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); op = str.substr(0, 1);
else if (str=="<<=" || str==">>=") else if (tok->isAssignmentOp() && str.size() == 3)
op = str.substr(0, 2); op = str.substr(0, 2);
else { else {
tok = tok1; tok = tok1;
@ -5029,7 +5029,7 @@ void Tokenizer::simplifyCompoundAssignment()
break; break;
} }
someOperator |= bool(tok2->isArithmeticalOp() || (tok2->str() == "?")); someOperator |= (tok2->isOp() || (tok2->str() == "?"));
} }
} }

View File

@ -3428,7 +3428,7 @@ private:
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '&&'.\n", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '&&'.\n", errout.str());
check("void foo() {\n" 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()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '&&'.\n", errout.str());
} }

View File

@ -5665,6 +5665,10 @@ private:
// #2714 - wrong simplification of "a += b?c:d;" // #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 ? c : d ) ;", tokenizeAndStringify("; a+=b?c:d;"));
ASSERT_EQUALS("; a = a * ( b + 1 ) ;", tokenizeAndStringify("; a*=b+1;")); 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() { void simplifyAssignmentInFunctionCall() {