@@ -1151,19 +1151,22 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
11511151 bool ret = false ;
11521152 // (1-2)
11531153 while (tok->tokAt (4 ) && tok->next ()->isNumber () && tok->tokAt (3 )->isNumber ()) { // %any% %num% %any% %num% %any%
1154+ const Token *before = tok;
11541155 const Token* op = tok->tokAt (2 );
11551156 const Token* after = tok->tokAt (4 );
1156- if (Token::Match (tok, " * %num% /" ) && (op->strAt (1 ) != " 0" ) && tok->next ()->str () == MathLib::multiply (op->strAt (1 ), MathLib::divide (tok->next ()->str (), op->strAt (1 )))) {
1157+ const std::string &num1 = op->previous ()->str ();
1158+ const std::string &num2 = op->next ()->str ();
1159+ if (Token::Match (before, " * %num% /" ) && (num2 != " 0" ) && num1 == MathLib::multiply (num2, MathLib::divide (num1, num2))) {
11571160 // Division where result is a whole number
1158- } else if (!((op->str () == " *" && (isLowerThanMulDiv (tok ) || tok ->str () == " *" ) && isLowerEqualThanMulDiv (after)) || // associative
1159- (Token::Match (op, " [/%]" ) && isLowerThanMulDiv (tok ) && isLowerEqualThanMulDiv (after)) || // NOT associative
1160- (Token::Match (op, " [+-]" ) && isLowerThanMulDiv (tok ) && isLowerThanMulDiv (after)) || // Only partially (+) associative, but handled later
1161- (Token::Match (op, " >>|<<" ) && isLowerThanShift (tok ) && isLowerThanPlusMinus (after)) || // NOT associative
1162- (op->str () == " &" && isLowerThanShift (tok ) && isLowerThanShift (after)) || // associative
1163- (op->str () == " ^" && isLowerThanAnd (tok ) && isLowerThanAnd (after)) || // associative
1164- (op->str () == " |" && isLowerThanXor (tok ) && isLowerThanXor (after)) || // associative
1165- (op->str () == " &&" && isLowerThanOr (tok ) && isLowerThanOr (after)) ||
1166- (op->str () == " ||" && isLowerThanLogicalAnd (tok ) && isLowerThanLogicalAnd (after))))
1161+ } else if (!((op->str () == " *" && (isLowerThanMulDiv (before ) || before ->str () == " *" ) && isLowerEqualThanMulDiv (after)) || // associative
1162+ (Token::Match (op, " [/%]" ) && isLowerThanMulDiv (before ) && isLowerEqualThanMulDiv (after)) || // NOT associative
1163+ (Token::Match (op, " [+-]" ) && isLowerThanMulDiv (before ) && isLowerThanMulDiv (after)) || // Only partially (+) associative, but handled later
1164+ (Token::Match (op, " >>|<<" ) && isLowerThanShift (before ) && isLowerThanPlusMinus (after)) || // NOT associative
1165+ (op->str () == " &" && isLowerThanShift (before ) && isLowerThanShift (after)) || // associative
1166+ (op->str () == " ^" && isLowerThanAnd (before ) && isLowerThanAnd (after)) || // associative
1167+ (op->str () == " |" && isLowerThanXor (before ) && isLowerThanXor (after)) || // associative
1168+ (op->str () == " &&" && isLowerThanOr (before ) && isLowerThanOr (after)) ||
1169+ (op->str () == " ||" && isLowerThanLogicalAnd (before ) && isLowerThanLogicalAnd (after))))
11671170 break ;
11681171
11691172 tok = tok->next ();
@@ -1180,8 +1183,8 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
11801183 if (MathLib::isNegative (tok->str ()) || MathLib::isNegative (tok->strAt (2 )))
11811184 continue ;
11821185
1183- const MathLib::value v1 (tok-> str () );
1184- const MathLib::value v2 (tok-> strAt ( 2 ) );
1186+ const MathLib::value v1 (num1 );
1187+ const MathLib::value v2 (num2 );
11851188
11861189 if (!v1.isInt () || !v2.isInt ())
11871190 continue ;
@@ -1207,19 +1210,19 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
12071210
12081211 // Logical operations
12091212 else if (Token::Match (op, " %oror%|&&" )) {
1210- const bool op1 = !MathLib::isNullValue (tok-> str () );
1211- const bool op2 = !MathLib::isNullValue (tok-> strAt ( 2 ) );
1213+ const bool op1 = !MathLib::isNullValue (num1 );
1214+ const bool op2 = !MathLib::isNullValue (num2 );
12121215 const bool result = (op->str () == " ||" ) ? (op1 || op2) : (op1 && op2);
12131216 tok->str (result ? " 1" : " 0" );
12141217 }
12151218
12161219 else if (Token::Match (tok->previous (), " - %num% - %num%" ))
1217- tok->str (MathLib::add (tok-> str (), tok-> strAt ( 2 ) ));
1220+ tok->str (MathLib::add (num1, num2 ));
12181221 else if (Token::Match (tok->previous (), " - %num% + %num%" ))
1219- tok->str (MathLib::subtract (tok-> str (), tok-> strAt ( 2 ) ));
1222+ tok->str (MathLib::subtract (num1, num2 ));
12201223 else {
12211224 try {
1222- tok->str (MathLib::calculate (tok-> str (), tok-> strAt ( 2 ) , op->str ()[0 ]));
1225+ tok->str (MathLib::calculate (num1, num2 , op->str ()[0 ]));
12231226 } catch (InternalError &e) {
12241227 e.token = tok;
12251228 throw ;
0 commit comments