@@ -1422,30 +1422,36 @@ void simplecpp::TokenList::constFoldBitwise(Token *tok)
14221422static const std::string OR (" or" );
14231423void simplecpp::TokenList::constFoldLogicalOp (Token *tok)
14241424{
1425- for (; tok && tok ->op != ' )' ; tok = tok ->next ) {
1426- if (tok ->name ) {
1427- if (isAlternativeBinaryOp (tok ,AND ))
1428- tok ->setstr (" &&" );
1429- else if (isAlternativeBinaryOp (tok ,OR ))
1430- tok ->setstr (" ||" );
1425+ for (Token *t = tok; t && t ->op != ' )' ; t = t ->next ) {
1426+ if (t ->name ) {
1427+ if (isAlternativeBinaryOp (t ,AND ))
1428+ t ->setstr (" &&" );
1429+ else if (isAlternativeBinaryOp (t ,OR ))
1430+ t ->setstr (" ||" );
14311431 }
1432- if (tok->str () != " &&" && tok->str () != " ||" )
1433- continue ;
1434- if (!tok->previous || !tok->previous ->number )
1435- continue ;
1436- if (!tok->next || !tok->next ->number )
1437- continue ;
1432+ }
14381433
1439- int result;
1440- if (tok->str () == " ||" )
1441- result = (stringToLL (tok->previous ->str ()) || stringToLL (tok->next ->str ()));
1442- else /* if (tok->str() == "&&")*/
1443- result = (stringToLL (tok->previous ->str ()) && stringToLL (tok->next ->str ()));
1434+ // && has higher precedence than ||
1435+ for (const char *op : {" &&" , " ||" }) {
1436+ for (Token *t = tok; t && t->op != ' )' ; t = t->next ) {
1437+ if (t->str () != op)
1438+ continue ;
1439+ if (!t->previous || !t->previous ->number )
1440+ continue ;
1441+ if (!t->next || !t->next ->number )
1442+ continue ;
14441443
1445- tok = tok->previous ;
1446- tok->setstr (toString (result));
1447- deleteToken (tok->next );
1448- deleteToken (tok->next );
1444+ int result;
1445+ if (t->str () == " ||" )
1446+ result = (stringToLL (t->previous ->str ()) || stringToLL (t->next ->str ()));
1447+ else /* if (t->str() == "&&")*/
1448+ result = (stringToLL (t->previous ->str ()) && stringToLL (t->next ->str ()));
1449+
1450+ t = t->previous ;
1451+ t->setstr (toString (result));
1452+ deleteToken (t->next );
1453+ deleteToken (t->next );
1454+ }
14491455 }
14501456}
14511457
0 commit comments