Tokenizer: remove simplifyCompoundAssignment from simplifyTokenList2

This commit is contained in:
Daniel Marjamäki 2022-06-09 07:38:27 +02:00
parent c0f3d5b2fb
commit a76ba698c2
3 changed files with 1 additions and 124 deletions

View File

@ -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<Token *> 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;

View File

@ -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.

View File

@ -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);"));