Fixed #1141 (Tokenizer: Wrong simplification of calculations)

This commit is contained in:
Daniel Marjamäki 2009-12-25 14:47:15 +01:00
parent 9b05850e97
commit 44a629ec74
2 changed files with 10 additions and 3 deletions

View File

@ -3909,16 +3909,21 @@ bool Tokenizer::simplifyCalculations()
if (Token::simpleMatch(tok->next(), "/ 0"))
continue;
// + and - are calculated after *
// + and - are calculated after * and /
if (Token::Match(tok->next(), "[+-/]"))
{
if (tok->previous()->str() == "*")
continue;
if (Token::simpleMatch(tok->tokAt(3), "*"))
if (Token::Match(tok->tokAt(3), "[*/]"))
continue;
}
tok->str(MathLib::calculate(tok->str(), tok->tokAt(2)->str(), *(tok->strAt(1))));
if (Token::Match(tok->previous(), "- %num% - %num%"))
tok->str(MathLib::calculate(tok->str(), tok->tokAt(2)->str(), '+'));
else if (Token::Match(tok->previous(), "- %num% + %num%"))
tok->str(MathLib::calculate(tok->tokAt(2)->str(), tok->str(), '-'));
else
tok->str(MathLib::calculate(tok->str(), tok->tokAt(2)->str(), *(tok->strAt(1))));
Token::eraseTokens(tok, tok->tokAt(3));

View File

@ -1914,6 +1914,8 @@ private:
ASSERT_EQUALS("x = 7 ;", tok("x=1+2*3;"));
ASSERT_EQUALS("x = 47185 ;", tok("x=(65536*72/100);"));
ASSERT_EQUALS("x = 900 ;", tok("x = 1500000 / ((145000 - 55000) * 1000 / 54000);"));
ASSERT_EQUALS("int a [ 8 ] ;", tok("int a[5+6/2];"));
ASSERT_EQUALS("int a [ 4 ] ;", tok("int a[(10)-1-5];"));
}