@@ -1150,10 +1150,12 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
11501150{
11511151 bool ret = false ;
11521152 // (1-2)
1153- while (tok->tokAt (4 ) && tok->next ()->isNumber () && tok->tokAt (3 )->isNumber ()) { // %any% %num% %any% %num% %any%
1154- const Token *before = tok;
1155- const Token* op = tok->tokAt (2 );
1156- const Token* after = tok->tokAt (4 );
1153+ while (tok->tokAt (3 ) && tok->isNumber () && tok->tokAt (2 )->isNumber ()) { // %any% %num% %any% %num% %any%
1154+ const Token *before = tok->previous ();
1155+ if (!before)
1156+ break ;
1157+ const Token* op = tok->next ();
1158+ const Token* after = tok->tokAt (3 );
11571159 const std::string &num1 = op->previous ()->str ();
11581160 const std::string &num2 = op->next ()->str ();
11591161 if (Token::Match (before, " * %num% /" ) && (num2 != " 0" ) && num1 == MathLib::multiply (num2, MathLib::divide (num1, num2))) {
@@ -1169,25 +1171,23 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
11691171 (op->str () == " ||" && isLowerThanLogicalAnd (before) && isLowerThanLogicalAnd (after))))
11701172 break ;
11711173
1172- tok = tok->next ();
1173-
11741174 // Don't simplify "%num% / 0"
11751175 if (Token::Match (op, " [/%] 0" ))
1176- continue ;
1176+ break ;
11771177
11781178 // Integer operations
11791179 if (Token::Match (op, " >>|<<|&|^|%or%" )) {
11801180 // Don't simplify if operand is negative, shifting with negative
11811181 // operand is UB. Bitmasking with negative operand is implementation
11821182 // defined behaviour.
1183- if (MathLib::isNegative (tok-> str ()) || MathLib::isNegative (tok-> strAt ( 2 ) ))
1184- continue ;
1183+ if (MathLib::isNegative (num1) || MathLib::isNegative (num2 ))
1184+ break ;
11851185
11861186 const MathLib::value v1 (num1);
11871187 const MathLib::value v2 (num2);
11881188
11891189 if (!v1.isInt () || !v2.isInt ())
1190- continue ;
1190+ break ;
11911191
11921192 switch (op->str ()[0 ]) {
11931193 case ' <' :
@@ -1230,7 +1230,6 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
12301230 }
12311231
12321232 tok->deleteNext (2 );
1233- tok = tok->previous ();
12341233
12351234 ret = true ;
12361235 }
@@ -1269,17 +1268,13 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens)
12691268 }
12701269
12711270 if (tok->isNumber ()) {
1272- if (simplifyNumericCalculations (tok-> previous () )) {
1271+ if (simplifyNumericCalculations (tok)) {
12731272 ret = true ;
1274- tok = tok->previous ();
1275- while (Token::Match (tok->tokAt (-2 ), " %cop%|,|( %num% %cop% %num% %cop%|,|)" )) {
1276- Token *before = tok->tokAt (-2 );
1277- if (simplifyNumericCalculations (before))
1278- tok = before;
1279- else
1280- break ;
1273+ Token *prev = tok->tokAt (-2 );
1274+ while (prev && simplifyNumericCalculations (prev)) {
1275+ tok = prev;
1276+ prev = prev->tokAt (-2 );
12811277 }
1282- tok = tok->next ();
12831278 }
12841279
12851280 // Remove redundant conditions (0&&x) (1||x)
0 commit comments