Tokenizer: removed redundant assignments such as 'x+=0;'. Ticket: #2173

This commit is contained in:
Daniel Marjamäki 2010-11-06 07:23:35 +01:00
parent 431201dd67
commit 314e5b838b
2 changed files with 28 additions and 12 deletions

View File

@ -4386,7 +4386,7 @@ void Tokenizer::simplifyCompoundAssignment()
}
// backup current token..
const Token * const tok1 = tok;
Token * const tok1 = tok;
if (tok->strAt(1) == "*")
tok = tok->next();
@ -4424,18 +4424,28 @@ void Tokenizer::simplifyCompoundAssignment()
else
continue;
// modify the token list..
tok->str("=");
tok->insertToken(op);
Token *tokend = 0;
for (const Token *tok2 = tok->previous(); tok2 && tok2 != tok1; tok2 = tok2->previous())
// 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->insertToken(tok2->str());
tok->next()->varId(tok2->varId());
if (Token::simpleMatch(tok->next(), "]"))
tokend = tok->next();
else if (Token::simpleMatch(tok->next(), "["))
Token::createMutualLinks(tok->next(), tokend);
tok = tok1;
while (tok->next()->str() != ";")
tok->deleteNext();
}
else
{
// simplify the compound assignment..
tok->str("=");
tok->insertToken(op);
Token *tokend = 0;
for (const Token *tok2 = tok->previous(); tok2 && tok2 != tok1; tok2 = tok2->previous())
{
tok->insertToken(tok2->str());
tok->next()->varId(tok2->varId());
if (Token::simpleMatch(tok->next(), "]"))
tokend = tok->next();
else if (Token::simpleMatch(tok->next(), "["))
Token::createMutualLinks(tok->next(), tokend);
}
}
}
}

View File

@ -4648,6 +4648,12 @@ private:
ASSERT_EQUALS("; x [ 0 ] = x [ 0 ] + 1 ;", tokenizeAndStringify("; x[0] += 1;"));
ASSERT_EQUALS("; x [ y - 1 ] = x [ y - 1 ] + 1 ;", tokenizeAndStringify("; x[y-1] += 1;"));
ASSERT_EQUALS(";", tokenizeAndStringify(";x += 0;"));
ASSERT_EQUALS(";", tokenizeAndStringify(";x -= 0;"));
ASSERT_EQUALS(";", tokenizeAndStringify(";x |= 0;"));
ASSERT_EQUALS(";", tokenizeAndStringify(";x *= 1;"));
ASSERT_EQUALS(";", tokenizeAndStringify(";x /= 1;"));
}
void simplifyAssignmentInFunctionCall()