@@ -947,7 +947,7 @@ void simplecpp::TokenList::constFold()
947
947
constFoldQuestionOp (&tok);
948
948
949
949
// If there is no '(' we are done with the constant folding
950
- if (!tok || tok->op != ' (' )
950
+ if (tok->op != ' (' )
951
951
break ;
952
952
953
953
if (!tok->next || !tok->next ->next || tok->next ->next ->op != ' )' )
@@ -1157,7 +1157,10 @@ void simplecpp::TokenList::constFoldMulDivRem(Token *tok)
1157
1157
} else
1158
1158
continue ;
1159
1159
1160
- simpleSquash (tok, toString (result));
1160
+ tok = tok->previous ;
1161
+ tok->setstr (toString (result));
1162
+ deleteToken (tok->next );
1163
+ deleteToken (tok->next );
1161
1164
}
1162
1165
}
1163
1166
@@ -1177,7 +1180,10 @@ void simplecpp::TokenList::constFoldAddSub(Token *tok)
1177
1180
else
1178
1181
continue ;
1179
1182
1180
- simpleSquash (tok, toString (result));
1183
+ tok = tok->previous ;
1184
+ tok->setstr (toString (result));
1185
+ deleteToken (tok->next );
1186
+ deleteToken (tok->next );
1181
1187
}
1182
1188
}
1183
1189
@@ -1197,7 +1203,10 @@ void simplecpp::TokenList::constFoldShift(Token *tok)
1197
1203
else
1198
1204
continue ;
1199
1205
1200
- simpleSquash (tok, toString (result));
1206
+ tok = tok->previous ;
1207
+ tok->setstr (toString (result));
1208
+ deleteToken (tok->next );
1209
+ deleteToken (tok->next );
1201
1210
}
1202
1211
}
1203
1212
@@ -1231,7 +1240,10 @@ void simplecpp::TokenList::constFoldComparison(Token *tok)
1231
1240
else
1232
1241
continue ;
1233
1242
1234
- simpleSquash (tok, toString (result));
1243
+ tok = tok->previous ;
1244
+ tok->setstr (toString (result));
1245
+ deleteToken (tok->next );
1246
+ deleteToken (tok->next );
1235
1247
}
1236
1248
}
1237
1249
@@ -1263,51 +1275,12 @@ void simplecpp::TokenList::constFoldBitwise(Token *tok)
1263
1275
result = (stringToLL (tok->previous ->str ()) ^ stringToLL (tok->next ->str ()));
1264
1276
else /* if (*op == '|')*/
1265
1277
result = (stringToLL (tok->previous ->str ()) | stringToLL (tok->next ->str ()));
1266
- simpleSquash (tok, toString (result));
1267
- }
1268
- }
1269
- }
1270
-
1271
- void simplecpp::TokenList::simpleSquash (Token *&tok, const std::string & result)
1272
- {
1273
- tok = tok->previous ;
1274
- tok->setstr (result);
1275
- deleteToken (tok->next );
1276
- deleteToken (tok->next );
1277
- }
1278
-
1279
- void simplecpp::TokenList::squashTokens (Token *&tok, const std::set<std::string> & breakPoints, bool forwardDirection, const std::string & result)
1280
- {
1281
- const char * const brackets = forwardDirection ? " ()" : " )(" ;
1282
- Token* Token::* const step = forwardDirection ? &Token::next : &Token::previous;
1283
- int skip = 0 ;
1284
- const Token * const tok1 = tok->*step;
1285
- while (tok1 && tok1->*step) {
1286
- if ((tok1->*step)->op == brackets[1 ]){
1287
- if (skip) {
1288
- --skip;
1289
- deleteToken (tok1->*step);
1290
- } else
1291
- break ;
1292
- } else if ((tok1->*step)->op == brackets[0 ]) {
1293
- ++skip;
1294
- deleteToken (tok1->*step);
1295
- } else if (skip) {
1296
- deleteToken (tok1->*step);
1297
- } else if (breakPoints.count ((tok1->*step)->str ()) != 0 ) {
1298
- break ;
1299
- } else {
1300
- deleteToken (tok1->*step);
1278
+ tok = tok->previous ;
1279
+ tok->setstr (toString (result));
1280
+ deleteToken (tok->next );
1281
+ deleteToken (tok->next );
1301
1282
}
1302
1283
}
1303
- simpleSquash (tok, result);
1304
- }
1305
-
1306
- static simplecpp::Token * constFoldGetOperand (simplecpp::Token * tok, bool forwardDirection)
1307
- {
1308
- simplecpp::Token* simplecpp::Token::* const step = forwardDirection ? &simplecpp::Token::next : &simplecpp::Token::previous;
1309
- const char bracket = forwardDirection ? ' )' : ' (' ;
1310
- return tok->*step && (tok->*step)->number && (!((tok->*step)->*step) || (((tok->*step)->*step)->op == bracket)) ? tok->*step : nullptr ;
1311
1284
}
1312
1285
1313
1286
static const std::string AND (" and" );
@@ -1323,24 +1296,21 @@ void simplecpp::TokenList::constFoldLogicalOp(Token *tok)
1323
1296
}
1324
1297
if (tok->str () != " &&" && tok->str () != " ||" )
1325
1298
continue ;
1326
- const Token* const lhs = constFoldGetOperand ( tok, false );
1327
- const Token* const rhs = constFoldGetOperand (tok, true ) ;
1328
- if (!lhs) // if lhs is not a single number we don't need to fold
1299
+ if (! tok-> previous || !tok-> previous -> number )
1300
+ continue ;
1301
+ if (!tok-> next || !tok-> next -> number )
1329
1302
continue ;
1330
1303
1331
- std::set<std::string> breakPoints;
1332
- breakPoints.insert (" :" );
1333
- breakPoints.insert (" ?" );
1334
- if (tok->str () == " ||" ){
1335
- if (stringToLL (lhs->str ()) != 0LL || (rhs && stringToLL (rhs->str ()) != 0LL ))
1336
- squashTokens (tok, breakPoints, stringToLL (lhs->str ()) != 0LL , toString (1 ));
1337
- } else /* if (tok->str() == "&&")*/ {
1338
- breakPoints.insert (" ||" );
1339
- if (stringToLL (lhs->str ()) == 0LL || (rhs && stringToLL (rhs->str ()) == 0LL ))
1340
- squashTokens (tok, breakPoints, stringToLL (lhs->str ()) == 0LL , toString (0 ));
1341
- else if (rhs && stringToLL (lhs->str ()) && stringToLL (rhs->str ()))
1342
- simpleSquash (tok, " 1" );
1343
- }
1304
+ int result;
1305
+ if (tok->str () == " ||" )
1306
+ result = (stringToLL (tok->previous ->str ()) || stringToLL (tok->next ->str ()));
1307
+ else /* if (tok->str() == "&&")*/
1308
+ result = (stringToLL (tok->previous ->str ()) && stringToLL (tok->next ->str ()));
1309
+
1310
+ tok = tok->previous ;
1311
+ tok->setstr (toString (result));
1312
+ deleteToken (tok->next );
1313
+ deleteToken (tok->next );
1344
1314
}
1345
1315
}
1346
1316
0 commit comments