@@ -2252,35 +2252,53 @@ namespace simplecpp {
22522252
22532253 /* * Gather the call of the function-like macro named by @p nameTok whose
22542254 * arguments follow the replacement-list token @p tok into @p call.
2255- * Parameters are substituted before the rescan, so parameters that expand
2256- * to nothing are skipped and the arguments can come from a parameter whose
2257- * expansion starts with '('; that expansion is copied whole since it may
2258- * hold more than the arguments. Returns the ')' from the replacement list,
2259- * the parameter that supplied the arguments, or nullptr when there is no
2260- * call. */
2255+ * Parameters are substituted before the rescan, so the parentheses are
2256+ * counted in the substituted tokens: parameters that expand to nothing are
2257+ * skipped, and the '(' and the ')' of the call can each come from the
2258+ * replacement list or from a parameter. Returns the replacement-list token
2259+ * that supplied the ')', or nullptr when there is no complete call. When
2260+ * that token is a parameter, its expansion is copied whole since it may
2261+ * hold more than the end of the call. */
22612262 const Token *gatherCallAfter (TokenList &call, const Token *nameTok, const Location &loc, const Token *tok, const MacroMap ¯os, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> ¶metertokens, OutputList *outputList) const {
22622263 TokenList callArgs (files);
2263- const Token *last = nullptr ;
2264- for (const Token *after = tok->next ; sameline (tok, after); after = after->next ) {
2265- if (after->op == ' (' ) {
2266- last = appendTokens (callArgs, loc, after, macros, expandedmacros, parametertokens, outputList);
2267- break ;
2264+ unsigned int par = 0 ;
2265+ for (const Token *after = tok->next ; sameline (tok, after);) {
2266+ if (par > 0U && after->op == ' #' && sameline (after, after->next ) && (after->next ->op != ' #' || sameline (after, after->next ->next ))) {
2267+ // # and ## keep the parentheses balanced
2268+ if (after->next ->op == ' #' )
2269+ after = expandHashHash (callArgs, loc, after, macros, expandedmacros, parametertokens, outputList, false );
2270+ else
2271+ after = expandHash (callArgs, loc, after, expandedmacros, parametertokens, outputList);
2272+ continue ;
22682273 }
2269- if (!expandArg (callArgs, after, loc, macros, expandedmacros, parametertokens, outputList))
2270- break ;
2271- // a parameter that expands to nothing is substituted before the rescan,
2272- // so it does not separate the macro name from its '('
2273- if (callArgs.cfront ()) {
2274- if (callArgs.cfront ()->op == ' (' )
2275- last = after;
2276- break ;
2274+ const Token * const end = callArgs.cback ();
2275+ if (!expandArg (callArgs, after, loc, macros, expandedmacros, parametertokens, outputList)) {
2276+ // only parameters, which can expand to nothing, can come between
2277+ // the macro name and its '('
2278+ if (par == 0U && after->op != ' (' )
2279+ return nullptr ;
2280+ callArgs.push_back (new Token (*after));
2281+ if (after->macro .empty () && par > 0U )
2282+ callArgs.back ()->macro = name ();
22772283 }
2284+ for (const Token *tok2 = end ? end->next : callArgs.cfront (); tok2; tok2 = tok2->next ) {
2285+ if (tok2->op == ' (' ) {
2286+ ++par;
2287+ } else if (par == 0U ) {
2288+ // the parameter does not start with '('
2289+ return nullptr ;
2290+ } else if (tok2->op == ' )' && --par == 0U ) {
2291+ // the tokens of the call must be on the same line
2292+ for (Token *tok3 = callArgs.front (); tok3 != tok2->next ; tok3 = tok3->next )
2293+ tok3->location = callArgs.cfront ()->location ;
2294+ call.push_back (new Token (nameTok->str (), callArgs.cfront ()->location ));
2295+ call.takeTokens (callArgs);
2296+ return after;
2297+ }
2298+ }
2299+ after = after->next ;
22782300 }
2279- if (!last)
2280- return nullptr ;
2281- call.push_back (new Token (nameTok->str (), callArgs.cfront ()->location ));
2282- call.takeTokens (callArgs);
2283- return last;
2301+ return nullptr ;
22842302 }
22852303
22862304 /* * Expand while the expansion result in @p temp ends with the name of a
@@ -2300,7 +2318,7 @@ namespace simplecpp {
23002318 output.takeTokens (temp);
23012319 output.deleteToken (output.back ());
23022320 if (last->name ) {
2303- // the arguments came from a parameter that may hold more than the call
2321+ // the call ended in a parameter that may hold more than the call
23042322 temp.takeTokens (call);
23052323 rescanArg (temp, loc, macros, expandedmacros, outputList);
23062324 } else {
0 commit comments