diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 89e82abbb..d790b2744 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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)); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 0dea4c791..99aa55dce 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -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];")); }