@@ -1702,7 +1702,7 @@ namespace simplecpp {
17021702 } else if (rawtok->op == ' (' ) {
17031703 macro2tok = output2.back ();
17041704 }
1705- if (!macro2tok || !macro2tok->name )
1705+ if (!macro2tok || !macro2tok->name || macro2tok-> isSkippedByRescan () )
17061706 break ;
17071707 if (output2.cfront () != output2.cback () && macro2tok->str () == this ->name ())
17081708 break ;
@@ -1732,8 +1732,11 @@ namespace simplecpp {
17321732 }
17331733 if (!rawtok2 || par != 1U )
17341734 break ;
1735+ const Token * const outputBack = output2.cback ();
17351736 if (macro->second .expand (output2, rawtok->location , rawtokens2.cfront (), macros, expandedmacros) != nullptr )
17361737 break ;
1738+ if (output2.cback () == outputBack)
1739+ skipTrailingName (output2);
17371740 rawtok = rawtok2->next ;
17381741 }
17391742 output.takeTokens (output2);
@@ -2164,10 +2167,10 @@ namespace simplecpp {
21642167 return functionLike () ? parametertokens2.back ()->next : nameTokInst->next ;
21652168 }
21662169
2167- /* * Returns the macro to expand when the last token of @p temp is the name of a
2168- * function-like macro and the tokens after @p tok supply its arguments ; nullptr otherwise */
2169- static const Macro *rescanMacro (const TokenList &temp, const Token *tok , const MacroMap ¯os, const std::set<TokenString> &expandedmacros) {
2170- if (!temp.cback () || !temp.cback ()->name || ! sameline (tok, tok-> next ) || tok-> next -> op != ' ( ' )
2170+ /* * Returns the macro when the last token of @p temp is the name of a
2171+ * function-like macro that the rescan can still expand ; nullptr otherwise */
2172+ static const Macro *trailingMacro (const TokenList &temp, const MacroMap ¯os, const std::set<TokenString> &expandedmacros) {
2173+ if (!temp.cback () || !temp.cback ()->name || temp. cback ()-> isSkippedByRescan () )
21712174 return nullptr ;
21722175 const MacroMap::const_iterator it = macros.find (temp.cback ()->str ());
21732176 if (it == macros.end () || expandedmacros.find (temp.cback ()->str ()) != expandedmacros.end ())
@@ -2177,6 +2180,22 @@ namespace simplecpp {
21772180 return &it->second ;
21782181 }
21792182
2183+ /* * Returns the macro to expand when the last token of @p temp is the name of a
2184+ * function-like macro and the tokens after @p tok supply its arguments; nullptr otherwise */
2185+ static const Macro *rescanMacro (const TokenList &temp, const Token *tok, const MacroMap ¯os, const std::set<TokenString> &expandedmacros) {
2186+ if (!sameline (tok, tok->next ) || tok->next ->op != ' (' )
2187+ return nullptr ;
2188+ return trailingMacro (temp, macros, expandedmacros);
2189+ }
2190+
2191+ /* * A macro call that expanded to nothing came after the last token of
2192+ * @p output: the rescan has passed that token, so when it is the name of
2193+ * a function-like macro, a '(' that comes later does not start a call. */
2194+ static void skipTrailingName (TokenList &output) {
2195+ if (output.back () && output.back ()->name )
2196+ output.back ()->setSkippedByRescan (true );
2197+ }
2198+
21802199 /* * Expand while the expansion result in @p temp ends with the name of a
21812200 * function-like macro whose arguments are supplied by the tokens after @p tok.
21822201 * Each round consumes that macro call from the token stream. The @p gatherCall
@@ -2196,18 +2215,144 @@ namespace simplecpp {
21962215 output.takeTokens (temp);
21972216 output.deleteToken (output.back ());
21982217 calledMacro->expand (temp, loc, temp2.cfront (), macros, expandedmacros);
2218+ if (temp.empty ())
2219+ skipTrailingName (output);
21992220 tok = closingPar;
22002221 }
22012222 output.takeTokens (temp);
22022223 return tok->next ;
22032224 }
22042225
2226+ /* * Expand the call of the function-like macro @p calledMacro whose name is the
2227+ * only token in @p tokens and whose arguments follow the replacement-list
2228+ * token @p tok. Parameters are substituted before the rescan, so parameters
2229+ * that expand to nothing are skipped and the arguments may come from a
2230+ * parameter whose expansion starts with '(' (that expansion may hold more
2231+ * than the arguments, so the macro name is rescanned together with it).
2232+ * The expansion result is rescanned against the tokens that follow.
2233+ * Returns false when there is no call; otherwise @p next is set to the
2234+ * first token that was not consumed. */
2235+ bool expandCallAfter (TokenList &output, TokenList &tokens, const Macro &calledMacro, const Location &loc, const Token *tok, const MacroMap ¯os, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> ¶metertokens, const Token *&next) const {
2236+ const Token *tok2 = nullptr ;
2237+ bool argumentsFromParameter = false ;
2238+ for (const Token *after = tok->next ; sameline (tok, after); after = after->next ) {
2239+ if (after->op == ' (' ) {
2240+ tok2 = appendTokens (tokens, loc, after, macros, expandedmacros, parametertokens);
2241+ break ;
2242+ }
2243+ if (!expandArg (tokens, after, loc, macros, expandedmacros, parametertokens))
2244+ break ;
2245+ if (tokens.cfront ()->next ) {
2246+ if (tokens.cfront ()->next ->op == ' (' ) {
2247+ tok2 = after;
2248+ argumentsFromParameter = true ;
2249+ }
2250+ break ;
2251+ }
2252+ }
2253+ if (!tok2)
2254+ return false ;
2255+ const Token * const outputBack = output.cback ();
2256+ TokenList temp (files);
2257+ if (argumentsFromParameter)
2258+ rescanArg (temp, tokens, loc, macros, expandedmacros);
2259+ else
2260+ calledMacro.expand (temp, loc, tokens.cfront (), macros, expandedmacros);
2261+ next = recursiveExpandToken (output, temp, loc, tok2, macros, expandedmacros, parametertokens);
2262+ if (output.cback () == outputBack)
2263+ skipTrailingName (output);
2264+ return true ;
2265+ }
2266+
22052267 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 {
2268+ if (sameline (tok, tok->next ) && tok->next ->op != ' (' ) {
2269+ // the arguments of a trailing macro name can come from the parameters that follow
2270+ if (const Macro * const calledMacro = trailingMacro (temp, macros, expandedmacros)) {
2271+ TokenList tokens (files);
2272+ tokens.push_back (new Token (*temp.cback ()));
2273+ TokenList output2 (files);
2274+ const Token *next = nullptr ;
2275+ if (expandCallAfter (output2, tokens, *calledMacro, loc, tok, macros, expandedmacros, parametertokens, next)) {
2276+ output.takeTokens (temp);
2277+ output.deleteToken (output.back ());
2278+ if (output2.empty ())
2279+ skipTrailingName (output);
2280+ output.takeTokens (output2);
2281+ return next;
2282+ }
2283+ }
2284+ }
22062285 return expandTrailingCalls (output, temp, loc, tok, macros, expandedmacros, [&](TokenList &temp2, const Token *lpar) {
22072286 return appendTokens (temp2, loc, lpar, macros, expandedmacros, parametertokens);
22082287 });
22092288 }
22102289
2290+ /* * Rescan a macro argument after it has been substituted into the replacement
2291+ * list: move @p tokens to @p output, expanding each call of a function-like
2292+ * macro that is complete within @p tokens. The argument pre-expansion leaves
2293+ * such a call behind when the macro name and its '(' were separated by tokens
2294+ * that expanded to nothing, as in "f EMPTY() (1)". The result of each call is
2295+ * rescanned against the tokens that follow it. Names of macros that are being
2296+ * expanded are marked so that they are never expanded later. */
2297+ void rescanArg (TokenList &output, TokenList &tokens, const Location &loc, const MacroMap ¯os, const std::set<TokenString> &expandedmacros) const {
2298+ bool hasMacroName = false ;
2299+ for (Token *tok = tokens.front (); tok; tok = tok->next ) {
2300+ tok->setSkippedByRescan (false );
2301+ if (tok->name && macros.find (tok->str ()) != macros.end ())
2302+ hasMacroName = true ;
2303+ }
2304+ if (!hasMacroName) {
2305+ output.takeTokens (tokens);
2306+ return ;
2307+ }
2308+
2309+ const auto gatherCall = [](TokenList &temp2, const Token *lpar) -> const Token * {
2310+ unsigned int par = 0 ;
2311+ for (const Token *tok2 = lpar; tok2; tok2 = tok2->next ) {
2312+ temp2.push_back (new Token (*tok2));
2313+ if (tok2->op == ' (' ) {
2314+ ++par;
2315+ } else if (tok2->op == ' )' ) {
2316+ if (--par == 0U )
2317+ return tok2;
2318+ }
2319+ }
2320+ return nullptr ;
2321+ };
2322+
2323+ for (const Token *tok = tokens.cfront (); tok;) {
2324+ const MacroMap::const_iterator it = tok->name ? macros.find (tok->str ()) : macros.end ();
2325+ if (it == macros.end ()) {
2326+ output.push_back (new Token (*tok));
2327+ tok = tok->next ;
2328+ continue ;
2329+ }
2330+ const Macro &calledMacro = it->second ;
2331+ if (expandedmacros.find (tok->str ()) != expandedmacros.end ()) {
2332+ output.push_back (new Token (*tok));
2333+ output.back ()->markExpandedFrom (&calledMacro);
2334+ tok = tok->next ;
2335+ continue ;
2336+ }
2337+ TokenList call (files);
2338+ call.push_back (new Token (tok->str (), loc));
2339+ const Token *closingPar = nullptr ;
2340+ if (calledMacro.functionLike () && !tok->isExpandedFrom (&calledMacro) && tok->next && tok->next ->op == ' (' )
2341+ closingPar = gatherCall (call, tok->next );
2342+ if (!closingPar) {
2343+ output.push_back (new Token (*tok));
2344+ tok = tok->next ;
2345+ continue ;
2346+ }
2347+ TokenList temp (files);
2348+ calledMacro.expand (temp, loc, call.cfront (), macros, expandedmacros);
2349+ if (temp.empty ())
2350+ skipTrailingName (output);
2351+ tok = expandTrailingCalls (output, temp, loc, closingPar, macros, expandedmacros, gatherCall);
2352+ }
2353+ tokens.clear ();
2354+ }
2355+
22112356 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 {
22122357 // Not name..
22132358 if (!tok->name ) {
@@ -2222,7 +2367,14 @@ namespace simplecpp {
22222367 if (tok->str () == " __VA_ARGS__" && temp.empty () && output.cback () && output.cback ()->str () == " ," &&
22232368 tok->nextSkipComments () && tok->nextSkipComments ()->str () == " )" )
22242369 output.deleteToken (output.back ());
2225- return recursiveExpandToken (output, temp, loc, tok, macros, expandedmacros, parametertokens);
2370+ const bool substituted = !temp.empty ();
2371+ TokenList rescanned (files);
2372+ rescanArg (rescanned, temp, loc, macros, expandedmacros);
2373+ // the argument held only macro calls that expanded to nothing:
2374+ // the rescan has passed the token before it
2375+ if (substituted && rescanned.empty ())
2376+ skipTrailingName (output);
2377+ return recursiveExpandToken (output, rescanned, loc, tok, macros, expandedmacros, parametertokens);
22262378 }
22272379 }
22282380
@@ -2236,6 +2388,8 @@ namespace simplecpp {
22362388 if (!calledMacro.functionLike ()) {
22372389 TokenList temp (files);
22382390 calledMacro.expand (temp, loc, tok, macros, expandedmacros);
2391+ if (temp.empty ())
2392+ skipTrailingName (output);
22392393 return recursiveExpandToken (output, temp, loc, tok, macros, expandedmacros2, parametertokens);
22402394 }
22412395 if (!sameline (tok, tok->next )) {
@@ -2244,22 +2398,12 @@ namespace simplecpp {
22442398 }
22452399 TokenList tokens (files);
22462400 tokens.push_back (new Token (*tok));
2247- const Token * tok2 = nullptr ;
2248- if (tok->next ->op == ' (' ) {
2249- tok2 = appendTokens (tokens, loc, tok->next , macros, expandedmacros, parametertokens);
2250- }
2251- else if (expandArg (tokens, tok->next , loc, macros, expandedmacros, parametertokens)) {
2252- tokens.front ()->location = loc;
2253- if (tokens.cfront ()->next && tokens.cfront ()->next ->op == ' (' )
2254- tok2 = tok->next ;
2255- }
2256- if (!tok2) {
2401+ const Token *next = nullptr ;
2402+ if (!expandCallAfter (output, tokens, calledMacro, loc, tok, macros, expandedmacros, parametertokens, next)) {
22572403 output.push_back (newMacroToken (tok->str (), loc, true , tok));
22582404 return tok->next ;
22592405 }
2260- TokenList temp (files);
2261- calledMacro.expand (temp, loc, tokens.cfront (), macros, expandedmacros);
2262- return recursiveExpandToken (output, temp, loc, tok2, macros, expandedmacros, parametertokens);
2406+ return next;
22632407 }
22642408
22652409 if (tok->str () == DEFINED ) {
@@ -2310,8 +2454,11 @@ namespace simplecpp {
23102454 if (variadic && argnr + 1U >= parametertokens.size ())
23112455 return true ;
23122456
2313- for (const Token *partok = parametertokens[argnr]->next ; partok != parametertokens[argnr + 1U ]; partok = partok->next )
2457+ for (const Token *partok = parametertokens[argnr]->next ; partok != parametertokens[argnr + 1U ]; partok = partok->next ) {
23142458 output.push_back (new Token (*partok));
2459+ // the substituted tokens are rescanned again
2460+ output.back ()->setSkippedByRescan (false );
2461+ }
23152462
23162463 return true ;
23172464 }
0 commit comments