@@ -1545,14 +1545,14 @@ void simplecpp::TokenList::constFoldQuestionOp(Token *&tok1)
15451545{
15461546 // ?: is right-associative, "a ? b : c ? d : e" means "a ? b : (c ? d : e)",
15471547 // so the last ? is folded first
1548- for (;;) {
1549- const Token *tok = nullptr ;
1550- for ( const Token *t = tok1; t && t-> op != ' ) ' ; t = t-> next ) {
1551- if (t-> str () == " ? " )
1552- tok = t;
1553- }
1554- if (! tok)
1555- return ;
1548+ std::vector< const Token *> questionTokens;
1549+ for ( const Token *t = tok1; t && t-> op != ' ) ' ; t = t-> next ) {
1550+ if (t-> str () == " ? " )
1551+ questionTokens. push_back (t);
1552+ }
1553+ while (!questionTokens. empty ()) {
1554+ const Token * const tok = questionTokens. back ();
1555+ questionTokens. pop_back () ;
15561556 if (!tok->previous || !tok->next || !tok->next ->next )
15571557 throw std::runtime_error (" invalid expression" );
15581558 Token * const condTok = tok->previous ;
@@ -1570,14 +1570,15 @@ void simplecpp::TokenList::constFoldQuestionOp(Token *&tok1)
15701570 deleteToken (trueTok->next ); // :
15711571 deleteToken (trueTok);
15721572 deleteToken (falseTok);
1573- continue ;
1573+ } else {
1574+ Token * const result = (condTok->str () != " 0" ? trueTok : falseTok);
1575+ if (condTok == tok1)
1576+ tok1 = result;
1577+ deleteToken (condTok->next ); // ?
1578+ deleteToken (trueTok->next ); // :
1579+ deleteToken (result == falseTok ? trueTok : falseTok); // the operand that is not evaluated
1580+ deleteToken (condTok);
15741581 }
1575- if (condTok == tok1)
1576- tok1 = (condTok->str () != " 0" ? trueTok : falseTok);
1577- deleteToken (condTok->next ); // ?
1578- deleteToken (trueTok->next ); // :
1579- deleteToken (condTok->str () == " 0" ? trueTok : falseTok); // the operand that is not evaluated
1580- deleteToken (condTok);
15811582 }
15821583}
15831584
0 commit comments