Fixed #2870 (Tokenizer: simplify '0 |')

This commit is contained in:
Simon Martin 2011-08-06 06:50:29 +02:00 committed by Daniel Marjamäki
parent fa82d43562
commit 13360c2a66
2 changed files with 23 additions and 5 deletions

View File

@ -7613,10 +7613,10 @@ bool Tokenizer::simplifyCalculations()
{ {
if (tok->str() == "0") if (tok->str() == "0")
{ {
if (Token::Match(tok->previous(), "[+-] 0")) if (Token::Match(tok->previous(), "[+-|] 0"))
{ {
tok = tok->previous(); tok = tok->previous();
if (Token::Match(tok->tokAt(-4), "[;{}] %var% = %var% [+-] 0 ;") && if (Token::Match(tok->tokAt(-4), "[;{}] %var% = %var% [+-|] 0 ;") &&
tok->strAt(-3) == tok->strAt(-1)) tok->strAt(-3) == tok->strAt(-1))
{ {
tok = tok->previous()->previous()->previous(); tok = tok->previous()->previous()->previous();
@ -7628,7 +7628,7 @@ bool Tokenizer::simplifyCalculations()
tok->deleteThis(); tok->deleteThis();
ret = true; ret = true;
} }
else if (Token::Match(tok->previous(), "[=([,] 0 +")) else if (Token::Match(tok->previous(), "[=([,] 0 [+|]"))
{ {
tok->deleteThis(); tok->deleteThis();
tok->deleteThis(); tok->deleteThis();
@ -7646,7 +7646,7 @@ bool Tokenizer::simplifyCalculations()
if (Token::simpleMatch(tok->previous(), "* 1") || Token::simpleMatch(tok, "1 *")) if (Token::simpleMatch(tok->previous(), "* 1") || Token::simpleMatch(tok, "1 *"))
{ {
if (Token::simpleMatch(tok->previous(), "*")) if (tok->previous()->isOp())
tok = tok->previous(); tok = tok->previous();
tok->deleteThis(); tok->deleteThis();
tok->deleteThis(); tok->deleteThis();
@ -7664,10 +7664,12 @@ bool Tokenizer::simplifyCalculations()
if (Token::simpleMatch(tok->previous(), "( 0 ||") || if (Token::simpleMatch(tok->previous(), "( 0 ||") ||
Token::simpleMatch(tok->previous(), "|| 0 )") || Token::simpleMatch(tok->previous(), "|| 0 )") ||
Token::simpleMatch(tok->previous(), "( 0 |") ||
Token::simpleMatch(tok->previous(), "| 0 )") ||
Token::simpleMatch(tok->previous(), "( 1 &&") || Token::simpleMatch(tok->previous(), "( 1 &&") ||
Token::simpleMatch(tok->previous(), "&& 1 )")) Token::simpleMatch(tok->previous(), "&& 1 )"))
{ {
if (!Token::simpleMatch(tok->previous(), "(")) if (tok->previous()->isOp())
tok = tok->previous(); tok = tok->previous();
tok->deleteThis(); tok->deleteThis();
tok->deleteThis(); tok->deleteThis();

View File

@ -323,6 +323,8 @@ private:
TEST_CASE(simplifyLogicalOperators); TEST_CASE(simplifyLogicalOperators);
TEST_CASE(simplifyCalculations); // ticket #2870
// foo(p = new char[10]); => p = new char[10]; foo(p); // foo(p = new char[10]); => p = new char[10]; foo(p);
TEST_CASE(simplifyAssignmentInFunctionCall); TEST_CASE(simplifyAssignmentInFunctionCall);
@ -5574,6 +5576,20 @@ private:
ASSERT_EQUALS("if ( a != b )", tokenizeAndStringify("if (a not_eq b)")); ASSERT_EQUALS("if ( a != b )", tokenizeAndStringify("if (a not_eq b)"));
} }
void simplifyCalculations()
{
ASSERT_EQUALS("void foo ( char str [ ] ) { char x ; x = * str ; }",
tokenizeAndStringify("void foo ( char str [ ] ) { char x = 0 | ( * str ) ; }", true));
ASSERT_EQUALS("void foo ( ) { if ( b ) { } }",
tokenizeAndStringify("void foo ( ) { if (b | 0) { } }", true));
ASSERT_EQUALS("void foo ( ) { if ( b ) { } }",
tokenizeAndStringify("void foo ( ) { if (0 | b) { } }", true));
ASSERT_EQUALS("void foo ( int b ) { int a ; a = b ; bar ( a ) ; }",
tokenizeAndStringify("void foo ( int b ) { int a = b | 0 ; bar ( a ) ; }", true));
ASSERT_EQUALS("void foo ( int b ) { int a ; a = b ; bar ( a ) ; }",
tokenizeAndStringify("void foo ( int b ) { int a = 0 | b ; bar ( a ) ; }", true));
}
void simplifyCompoundAssignment() void simplifyCompoundAssignment()
{ {
ASSERT_EQUALS("; x = x + y ;", tokenizeAndStringify("; x += y;")); ASSERT_EQUALS("; x = x + y ;", tokenizeAndStringify("; x += y;"));