From a76ba698c2cc4c03504d39666487e1210cd6428a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 9 Jun 2022 07:38:27 +0200 Subject: [PATCH] Tokenizer: remove simplifyCompoundAssignment from simplifyTokenList2 --- lib/tokenize.cpp | 114 ------------------------------------ lib/tokenize.h | 6 -- test/testsimplifytokens.cpp | 5 +- 3 files changed, 1 insertion(+), 124 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index da9f97465..8d07384c5 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5275,9 +5275,6 @@ bool Tokenizer::simplifyTokenList2() tok->clearValueFlow(); } - // ";a+=b;" => ";a=a+b;" - simplifyCompoundAssignment(); - simplifyCharAt(); // simplify references @@ -6450,117 +6447,6 @@ Token *Tokenizer::simplifyAddBracesPair(Token *tok, bool commandWithCondition) return tokBracesEnd; } -void Tokenizer::simplifyCompoundAssignment() -{ - // Simplify compound assignments: - // "a+=b" => "a = a + b" - for (Token *tok = list.front(); tok; tok = tok->next()) { - if (!Token::Match(tok, "[;{}] (| *| (| %name%")) - continue; - if (tok->next()->str() == "return") - continue; - // backup current token.. - Token * const tok1 = tok; - - if (tok->next()->str() == "*") - tok = tok->next(); - - if (tok->next() && tok->next()->str() == "(") { - tok = tok->next()->link()->next(); - } else { - // variable.. - tok = tok->tokAt(2); - while (Token::Match(tok, ". %name%") || - Token::Match(tok, "[|(")) { - if (tok->str() == ".") - tok = tok->tokAt(2); - else { - // goto "]" or ")" - tok = tok->link(); - - // goto next token.. - tok = tok ? tok->next() : nullptr; - } - } - } - if (!tok) - break; - - // Is current token at a compound assignment: +=|-=|.. ? - const std::string &str = tok->str(); - std::string op; // operator used in assignment - if (tok->isAssignmentOp() && str.size() == 2) - op = str.substr(0, 1); - else if (tok->isAssignmentOp() && str.size() == 3) - op = str.substr(0, 2); - else { - tok = tok1; - continue; - } - - // Remove the whole statement if it says: "+=0;", "-=0;", "*=1;" or "/=1;" - if (Token::Match(tok, "+=|-= 0 ;") || - Token::simpleMatch(tok, "|= 0 ;") || - Token::Match(tok, "*=|/= 1 ;")) { - tok = tok1; - while (tok->next()->str() != ";") - tok->deleteNext(); - } else { - // Enclose the rhs in parentheses.. - if (!Token::Match(tok->tokAt(2), "[;)]")) { - // Only enclose rhs in parentheses if there is some operator - bool someOperator = false; - for (Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) { - if (tok2->link() && Token::Match(tok2, "{|[|(")) - tok2 = tok2->link(); - - if (Token::Match(tok2->next(), "[;)]")) { - if (someOperator) { - tok->insertToken("("); - tok2->insertToken(")"); - Token::createMutualLinks(tok->next(), tok2->next()); - } - break; - } - - someOperator |= (tok2->isOp() || tok2->str() == "?"); - } - } - - // simplify the compound assignment.. - tok->str("="); - tok->insertToken(op); - - std::stack tokend; - 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->tokType() == Token::eIncDecOp) { - // pre increment/decrement => don't copy - if (tok2->next()->isName()) { - continue; - } - - // post increment/decrement => move from lhs to rhs - tok->insertToken(tok2->str()); - tok2->deleteThis(); - continue; - } - - // Copy token from lhs to rhs - tok->insertToken(tok2->str()); - tok->next()->varId(tok2->varId()); - if (Token::Match(tok->next(), "]|)|}")) - tokend.push(tok->next()); - else if (Token::Match(tok->next(), "(|[|{")) { - Token::createMutualLinks(tok->next(), tokend.top()); - tokend.pop(); - } - } - } - } -} - bool Tokenizer::simplifyConditions() { bool ret = false; diff --git a/lib/tokenize.h b/lib/tokenize.h index 82866854d..ddd25342d 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -310,12 +310,6 @@ public: */ bool simplifyConstTernaryOp(); - /** - * Simplify compound assignments - * Example: ";a+=b;" => ";a=a+b;" - */ - void simplifyCompoundAssignment(); - /** * Simplify the location of "static" and "const" qualifiers in * a variable declaration or definition. diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 98f02e083..04a780c18 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -60,9 +60,6 @@ private: TEST_CASE(test1); // array access. replace "*(p+1)" => "p[1]" - // ";a+=b;" => ";a=a+b;" - TEST_CASE(simplifyCompoundAssignment); - TEST_CASE(cast); TEST_CASE(iftruefalse); @@ -497,7 +494,7 @@ private: // "(x-y)" => "(x-y)" and "(x+y)" => "(x+y)" ASSERT_EQUALS("; a = b * ( x - y ) ;", tok("; a = b * (x - y);")); ASSERT_EQUALS("; a = b * x [ - y ] ;", tok("; a = b * *(x - y);")); - ASSERT_EQUALS("; a = a * ( x - y ) ;", tok("; a *= (x - y);")); + ASSERT_EQUALS("; a *= ( x - y ) ;", tok("; a *= (x - y);")); ASSERT_EQUALS("; z = a ++ * ( x - y ) ;", tok("; z = a++ * (x - y);")); ASSERT_EQUALS("; z = a ++ * ( x + y ) ;", tok("; z = a++ * (x + y);")); ASSERT_EQUALS("; z = a -- * ( x - y ) ;", tok("; z = a-- * (x - y);"));