Tokenizer: simplify calculations with zero better

This commit is contained in:
Daniel Marjamäki 2010-11-05 20:35:31 +01:00
parent 4fdb420454
commit 52faadda89
3 changed files with 27 additions and 2 deletions

View File

@ -6270,6 +6270,29 @@ bool Tokenizer::simplifyCalculations()
if (tok->isNumber()) 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(), "* 1") || Token::simpleMatch(tok, "1 *"))
{ {
if (Token::simpleMatch(tok->previous(), "*")) if (Token::simpleMatch(tok->previous(), "*"))

View File

@ -1193,8 +1193,7 @@ private:
" data[(i-0)/2] = 0;\n" " data[(i-0)/2] = 0;\n"
" }\n" " }\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (error) Array 'data[8]' index 17 out of bounds\n", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Array index out of bounds\n", errout.str());
} }
void array_index_negative() void array_index_negative()

View File

@ -2507,6 +2507,9 @@ private:
ASSERT_EQUALS("int a [ 4 ] ;", tok("int a[(10)-1-5];")); 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("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 = 501 ;", tok("x = 1000 + 2 >> 1;"));
ASSERT_EQUALS("x = 125 ;", tok("x = 1000 / 2 >> 2;")); ASSERT_EQUALS("x = 125 ;", tok("x = 1000 / 2 >> 2;"));