@@ -2177,26 +2177,37 @@ namespace simplecpp {
21772177 return &it->second ;
21782178 }
21792179
2180- const Token *recursiveExpandToken (TokenList &output, TokenList &temp, const Location &loc, const Token *tok, const MacroMap ¯os, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> ¶metertokens) const {
2181- // Expand while the expansion result ends with the name of a function-like
2182- // macro whose arguments are supplied by the tokens that follow it. Each round
2183- // consumes that macro call from the token stream, so tok always advances.
2180+ /* * Expand while the expansion result in @p temp ends with the name of a
2181+ * function-like macro whose arguments are supplied by the tokens after @p tok.
2182+ * Each round consumes that macro call from the token stream. The @p gatherCall
2183+ * callback copies the "( ... )" tokens that follow @p lpar into its output list
2184+ * and returns the matching ')', or nullptr when no complete call is available.
2185+ * Returns the first token that was not consumed (tok->next when no call was
2186+ * expanded). */
2187+ template <class GatherCall >
2188+ const Token *expandTrailingCalls (TokenList &output, TokenList &temp, const Location &loc, const Token *tok, const MacroMap ¯os, const std::set<TokenString> &expandedmacros, GatherCall gatherCall) const {
21842189 while (const Macro * const calledMacro = rescanMacro (temp, tok, macros, expandedmacros)) {
21852190 TokenList temp2 (files);
2186- temp2.push_back (new Token (temp.cback ()->str (), tok->location ));
2191+ temp2.push_back (new Token (temp.cback ()->str (), tok->next -> location ));
21872192
2188- const Token * const tok2 = appendTokens (temp2, loc, tok->next , macros, expandedmacros, parametertokens );
2189- if (!tok2 )
2193+ const Token * const closingPar = gatherCall (temp2, tok->next );
2194+ if (!closingPar )
21902195 break ;
21912196 output.takeTokens (temp);
21922197 output.deleteToken (output.back ());
21932198 calledMacro->expand (temp, loc, temp2.cfront (), macros, expandedmacros);
2194- tok = tok2 ;
2199+ tok = closingPar ;
21952200 }
21962201 output.takeTokens (temp);
21972202 return tok->next ;
21982203 }
21992204
2205+ const Token *recursiveExpandToken (TokenList &output, TokenList &temp, const Location &loc, const Token *tok, const MacroMap ¯os, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> ¶metertokens) const {
2206+ return expandTrailingCalls (output, temp, loc, tok, macros, expandedmacros, [&](TokenList &temp2, const Token *lpar) {
2207+ return appendTokens (temp2, loc, lpar, macros, expandedmacros, parametertokens);
2208+ });
2209+ }
2210+
22002211 const Token *expandToken (TokenList &output, const Location &loc, const Token *tok, const MacroMap ¯os, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> ¶metertokens) const {
22012212 // Not name..
22022213 if (!tok->name ) {
@@ -2313,12 +2324,35 @@ namespace simplecpp {
23132324 return false ;
23142325 if (variadic && argnr + 1U >= parametertokens.size ()) // empty variadic parameter
23152326 return true ;
2316- for (const Token *partok = parametertokens[argnr]->next ; partok != parametertokens[argnr + 1U ];) {
2327+ const Token * const argEnd = parametertokens[argnr + 1U ];
2328+ for (const Token *partok = parametertokens[argnr]->next ; partok != argEnd;) {
23172329 const MacroMap::const_iterator it = macros.find (partok->str ());
23182330 if (it != macros.end () && !partok->isExpandedFrom (&it->second ) && (partok->str () == name () || expandedmacros.find (partok->str ()) == expandedmacros.end ())) {
23192331 std::set<TokenString> expandedmacros2 (expandedmacros); // temporary amnesia to allow reexpansion of currently expanding macros during argument evaluation
23202332 expandedmacros2.erase (name ());
2321- partok = it->second .expand (output, loc, partok, macros, std::move (expandedmacros2));
2333+ TokenList temp (files);
2334+ partok = it->second .expand (temp, loc, partok, macros, std::move (expandedmacros2));
2335+ if (partok->op == ' (' && temp.cback () && temp.cback ()->name ) {
2336+ // the expansion result may end with the name of a function-like
2337+ // macro whose arguments are supplied by the remaining argument tokens
2338+ std::set<TokenString> expandedmacros3 (expandedmacros);
2339+ expandedmacros3.erase (name ());
2340+ partok = expandTrailingCalls (output, temp, loc, partok->previous , macros, expandedmacros3, [&](TokenList &temp2, const Token *lpar) -> const Token * {
2341+ unsigned int par = 0 ;
2342+ for (const Token *tok2 = lpar; tok2 != argEnd; tok2 = tok2->next ) {
2343+ temp2.push_back (new Token (*tok2));
2344+ if (tok2->op == ' (' ) {
2345+ ++par;
2346+ } else if (tok2->op == ' )' ) {
2347+ if (--par == 0U )
2348+ return tok2;
2349+ }
2350+ }
2351+ return nullptr ;
2352+ });
2353+ } else {
2354+ output.takeTokens (temp);
2355+ }
23222356 } else {
23232357 output.push_back (newMacroToken (partok->str (), loc, isReplaced (expandedmacros), partok));
23242358 output.back ()->macro = partok->macro ;
0 commit comments