diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index aa646360d..d76757589 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7613,10 +7613,10 @@ bool Tokenizer::simplifyCalculations() { if (tok->str() == "0") { - if (Token::Match(tok->previous(), "[+-] 0")) + if (Token::Match(tok->previous(), "[+-|] 0")) { 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 = tok->previous()->previous()->previous(); @@ -7628,7 +7628,7 @@ bool Tokenizer::simplifyCalculations() tok->deleteThis(); ret = true; } - else if (Token::Match(tok->previous(), "[=([,] 0 +")) + else if (Token::Match(tok->previous(), "[=([,] 0 [+|]")) { 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(), "*")) + if (tok->previous()->isOp()) tok = tok->previous(); tok->deleteThis(); tok->deleteThis(); @@ -7664,10 +7664,12 @@ bool Tokenizer::simplifyCalculations() 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(), "( 1 &&") || Token::simpleMatch(tok->previous(), "&& 1 )")) { - if (!Token::simpleMatch(tok->previous(), "(")) + if (tok->previous()->isOp()) tok = tok->previous(); tok->deleteThis(); tok->deleteThis(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 06fd80ac8..4cdeb213c 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -323,6 +323,8 @@ private: TEST_CASE(simplifyLogicalOperators); + TEST_CASE(simplifyCalculations); // ticket #2870 + // foo(p = new char[10]); => p = new char[10]; foo(p); TEST_CASE(simplifyAssignmentInFunctionCall); @@ -5574,6 +5576,20 @@ private: 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() { ASSERT_EQUALS("; x = x + y ;", tokenizeAndStringify("; x += y;"));