@@ -1526,14 +1526,14 @@ void simplecpp::TokenList::constFoldQuestionOp(Token *&tok1)
15261526{
15271527 // ?: is right-associative, "a ? b : c ? d : e" means "a ? b : (c ? d : e)",
15281528 // so the last ? is folded first
1529- for (;;) {
1530- const Token *tok = nullptr ;
1531- for ( const Token *t = tok1; t && t-> op != ' ) ' ; t = t-> next ) {
1532- if (t-> str () == " ? " )
1533- tok = t;
1534- }
1535- if (! tok)
1536- return ;
1529+ std::vector< const Token *> questionTokens;
1530+ for ( const Token *t = tok1; t && t-> op != ' ) ' ; t = t-> next ) {
1531+ if (t-> str () == " ? " )
1532+ questionTokens. push_back (t);
1533+ }
1534+ while (!questionTokens. empty ()) {
1535+ const Token * const tok = questionTokens. back ();
1536+ questionTokens. pop_back () ;
15371537 if (!tok->previous || !tok->next || !tok->next ->next )
15381538 throw std::runtime_error (" invalid expression" );
15391539 Token * const condTok = tok->previous ;
@@ -1551,14 +1551,15 @@ void simplecpp::TokenList::constFoldQuestionOp(Token *&tok1)
15511551 deleteToken (trueTok->next ); // :
15521552 deleteToken (trueTok);
15531553 deleteToken (falseTok);
1554- continue ;
1554+ } else {
1555+ Token * const result = (condTok->str () != " 0" ? trueTok : falseTok);
1556+ if (condTok == tok1)
1557+ tok1 = result;
1558+ deleteToken (condTok->next ); // ?
1559+ deleteToken (trueTok->next ); // :
1560+ deleteToken (result == falseTok ? trueTok : falseTok); // the operand that is not evaluated
1561+ deleteToken (condTok);
15551562 }
1556- if (condTok == tok1)
1557- tok1 = (condTok->str () != " 0" ? trueTok : falseTok);
1558- deleteToken (condTok->next ); // ?
1559- deleteToken (trueTok->next ); // :
1560- deleteToken (condTok->str () == " 0" ? trueTok : falseTok); // the operand that is not evaluated
1561- deleteToken (condTok);
15621563 }
15631564}
15641565
0 commit comments