Tokenizer::simplifyCalculations: simplify division result if it's a whole number, even though it doesn't have precedence

This commit is contained in:
Daniel Marjamäki 2011-04-02 12:52:11 +02:00
parent 855b01cd5a
commit ea57e10c7f
2 changed files with 9 additions and 6 deletions

View File

@ -7377,14 +7377,13 @@ bool Tokenizer::simplifyCalculations()
else if (tok->next() && tok->next()->isNumber()) else if (tok->next() && tok->next()->isNumber())
{ {
// (1-2) // (1-2)
while (Token::Match(tok, "[[,(=<>+-*|&^] %num% [+-*/] %num% [],);=<>+-*/|&^]") || while (Token::Match(tok, "[[,(=<>+-*|&^] %num% [+-*/] %num% [],);=<>+-*/|&^]") ||
Token::Match(tok, "<< %num% [+-*/] %num% [],);=<>+-*/|&^]") || Token::Match(tok, "<< %num% [+-*/] %num% [],);=<>+-*/|&^]") ||
Token::Match(tok, "[[,(=<>+-*|&^] %num% [+-*/] %num% <<|>>") || Token::Match(tok, "[[,(=<>+-*|&^] %num% [+-*/] %num% <<|>>") ||
Token::Match(tok, "<< %num% [+-*/] %num% <<") || Token::Match(tok, "<< %num% [+-*/] %num% <<") ||
Token::Match(tok, "[(,[] %num% [|&^] %num% [];,);]") || Token::Match(tok, "[(,[] %num% [|&^] %num% [];,);]") ||
Token::Match(tok, "(|==|!=|<=|>=|<|>|+ %num% [+-*/] %num% ==|!=|<=|>=|<|>|)")) Token::Match(tok, "(|==|!=|<=|>=|<|>|+|-|* %num% [+-*/] %num% ==|!=|<=|>=|<|>|)"))
{ {
tok = tok->next(); tok = tok->next();
@ -7415,8 +7414,14 @@ bool Tokenizer::simplifyCalculations()
} }
} }
// Division where result is a whole number
if (Token::Match(tok->previous(), "* %num% /") &&
tok->str() == MathLib::multiply(tok->strAt(2), MathLib::divide(tok->str(), tok->strAt(2))))
{
}
// + and - are calculated after * and / // + and - are calculated after * and /
if (Token::Match(tok->next(), "[+-/]")) else if (Token::Match(tok->next(), "[+-/]"))
{ {
if (tok->previous()->str() == "*") if (tok->previous()->str() == "*")
continue; continue;

View File

@ -2722,9 +2722,7 @@ private:
ASSERT_EQUALS("if ( a == 2 )", tok("if (a==1+1)")); ASSERT_EQUALS("if ( a == 2 )", tok("if (a==1+1)"));
ASSERT_EQUALS("if ( a + 2 != 6 )", tok("if (a+1+1!=1+2+3)")); ASSERT_EQUALS("if ( a + 2 != 6 )", tok("if (a+1+1!=1+2+3)"));
ASSERT_EQUALS("if ( 4 < a )", tok("if (14-2*5<a*4/(2*2))"));
// TODO: "4/4" should be simplified
TODO_ASSERT_EQUALS("if ( 4 < a )", "if ( 4 < a * 4 / 4 )", tok("if (14-2*5<a*4/(2*2))"));
} }