diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 430480523..b45e3ded2 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6270,6 +6270,29 @@ bool Tokenizer::simplifyCalculations() if (tok->isNumber()) { + if (tok->str() == "0") + { + if (Token::Match(tok->previous(), "[+-] 0")) + { + tok = tok->previous(); + tok->deleteThis(); + tok->deleteThis(); + ret = true; + } + else if (Token::Match(tok->previous(), "[=([,] 0 +")) + { + tok->deleteThis(); + tok->deleteThis(); + ret = true; + } + else if (Token::Match(tok->previous(), "[=[(,] 0 * %any% [+-*/,]);]")) + { + tok->deleteNext(); + tok->deleteNext(); + ret = true; + } + } + if (Token::simpleMatch(tok->previous(), "* 1") || Token::simpleMatch(tok, "1 *")) { if (Token::simpleMatch(tok->previous(), "*")) diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index b052ef21c..7d4475b2e 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -1193,8 +1193,7 @@ private: " data[(i-0)/2] = 0;\n" " }\n" "}\n"); - ASSERT_EQUALS("", errout.str()); - TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Array index out of bounds\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5]: (error) Array 'data[8]' index 17 out of bounds\n", errout.str()); } void array_index_negative() diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 2a2461689..12ca7cbf8 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -2507,6 +2507,9 @@ private: ASSERT_EQUALS("int a [ 4 ] ;", tok("int a[(10)-1-5];")); ASSERT_EQUALS("int a [ i - 9 ] ;", tok("int a[i - 10 + 1];")); + ASSERT_EQUALS("x = y ;", tok("x=0+y+0-0;")); + ASSERT_EQUALS("x = 0 ;", tok("x=0*y;")); + ASSERT_EQUALS("x = 501 ;", tok("x = 1000 + 2 >> 1;")); ASSERT_EQUALS("x = 125 ;", tok("x = 1000 / 2 >> 2;"));