Skip to content

Commit 08c7c43

Browse files
committed
Fix nesting
1 parent 05d017c commit 08c7c43

1 file changed

Lines changed: 44 additions & 21 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,19 +2209,29 @@ namespace simplecpp {
22092209
return callEnd->next;
22102210
}
22112211

2212-
/** Returns the macro when the last token of @p temp is the name of a
2213-
* function-like macro that the rescan can still expand; nullptr otherwise */
2214-
static const Macro *trailingMacro(const TokenList &temp, const MacroMap &macros, const std::set<TokenString> &expandedmacros) {
2215-
if (!temp.cback() || !temp.cback()->name || temp.cback()->skippedByRescan)
2212+
/** Returns the macro when @p tok is the name of a function-like macro
2213+
* that the rescan can still expand; nullptr otherwise */
2214+
static const Macro *callableMacro(const Token *tok, const MacroMap &macros, const std::set<TokenString> &expandedmacros) {
2215+
if (!tok || !tok->name || tok->skippedByRescan)
22162216
return nullptr;
2217-
const MacroMap::const_iterator it = macros.find(temp.cback()->str());
2218-
if (it == macros.end() || expandedmacros.find(temp.cback()->str()) != expandedmacros.end())
2217+
const MacroMap::const_iterator it = macros.find(tok->str());
2218+
if (it == macros.end() || expandedmacros.find(tok->str()) != expandedmacros.end())
22192219
return nullptr;
2220-
if (!it->second.functionLike() || temp.cback()->isExpandedFrom(&it->second))
2220+
if (!it->second.functionLike() || tok->isExpandedFrom(&it->second))
22212221
return nullptr;
22222222
return &it->second;
22232223
}
22242224

2225+
/** Mark @p tok when it is the name of a macro that is being expanded, so
2226+
* that it is never expanded later */
2227+
static void markExpanding(Token *tok, const MacroMap &macros, const std::set<TokenString> &expandedmacros) {
2228+
if (!tok->name || expandedmacros.find(tok->str()) == expandedmacros.end())
2229+
return;
2230+
const MacroMap::const_iterator it = macros.find(tok->str());
2231+
if (it != macros.end())
2232+
tok->markExpandedFrom(&it->second);
2233+
}
2234+
22252235
/** A macro call that expanded to nothing came after the last token of
22262236
* @p output: the rescan has passed that token, so when it is the name of
22272237
* a function-like macro, a '(' that comes later does not start a call. */
@@ -2310,7 +2320,7 @@ namespace simplecpp {
23102320
* consumed (tok->next when no call was expanded). */
23112321
template<class GatherCall>
23122322
const Token *expandTrailingCalls(TokenList &output, TokenList &temp, const Location &loc, const Token *tok, const MacroMap &macros, const std::set<TokenString> &expandedmacros, OutputList *outputList, GatherCall gatherCall) const {
2313-
while (const Macro * const calledMacro = trailingMacro(temp, macros, expandedmacros)) {
2323+
while (const Macro * const calledMacro = callableMacro(temp.cback(), macros, expandedmacros)) {
23142324
TokenList call(files);
23152325
const Token * const last = gatherCall(call, temp.cback(), tok);
23162326
if (!last)
@@ -2350,18 +2360,28 @@ namespace simplecpp {
23502360
* when the macro name and its '(' were separated by tokens that expanded to
23512361
* nothing, as in "f EMPTY() (1)". The result of each call is rescanned
23522362
* against the tokens that follow it. Names of macros that are being expanded
2353-
* are marked so that they are never expanded later. */
2363+
* are marked so that they are never expanded later. The marks that the
2364+
* pre-expansion left on the tokens do not apply to this rescan. */
23542365
void rescanArg(TokenList &tokens, const Location &loc, const MacroMap &macros, const std::set<TokenString> &expandedmacros, OutputList *outputList) const {
2366+
// Usually the pre-expansion has left no call behind. Then the tokens are
2367+
// only changed in place, which avoids copying them at each nesting level.
2368+
bool hasCall = false;
2369+
for (Token *tok = tokens.front(); tok; tok = tok->next) {
2370+
tok->skippedByRescan = false;
2371+
if (hasCall)
2372+
continue;
2373+
hasCall = sameline(tok, tok->next) && tok->next->op == '(' && callableMacro(tok, macros, expandedmacros) != nullptr;
2374+
if (!hasCall)
2375+
markExpanding(tok, macros, expandedmacros);
2376+
}
2377+
if (!hasCall)
2378+
return;
23552379
TokenList input(files);
23562380
input.takeTokens(tokens);
23572381
for (const Token *tok = input.cfront(); tok;) {
23582382
TokenList temp(files);
23592383
temp.push_back(new Token(*tok));
2360-
if (tok->name && expandedmacros.find(tok->str()) != expandedmacros.end()) {
2361-
const MacroMap::const_iterator it = macros.find(tok->str());
2362-
if (it != macros.end())
2363-
temp.back()->markExpandedFrom(&it->second);
2364-
}
2384+
markExpanding(temp.back(), macros, expandedmacros);
23652385
tok = expandTrailingCalls(tokens, temp, loc, tok, macros, expandedmacros, outputList, [](TokenList &call, const Token *nameTok, const Token *tok2) {
23662386
return gatherCall(call, nameTok, tok2);
23672387
});
@@ -2470,8 +2490,9 @@ namespace simplecpp {
24702490

24712491
/** Append the fully macro-expanded argument for parameter @p tok to @p output.
24722492
* @p hasMacroName, when given, is set when the argument held a macro name
2473-
* (otherwise the output cannot hold one). Returns false when @p tok is not
2474-
* a parameter. */
2493+
* (otherwise the output cannot hold one); the caller then passes the output
2494+
* to rescanArg, which clears the marks that the pre-expansion left on it.
2495+
* Returns false when @p tok is not a parameter. */
24752496
bool expandArg(TokenList &output, const Token *tok, const Location &loc, const MacroMap &macros, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> &parametertokens, OutputList *outputList, bool *hasMacroName = nullptr) const {
24762497
if (!tok->name)
24772498
return false;
@@ -2515,12 +2536,14 @@ namespace simplecpp {
25152536
if (tok->whitespaceahead && output.back())
25162537
output.back()->whitespaceahead = true;
25172538
if (macroName) {
2518-
// the substituted tokens are rescanned again, so the marks that the
2519-
// expansions above left on them do not apply
2520-
for (Token *tok2 = outputEnd ? outputEnd->next : output.front(); tok2; tok2 = tok2->next)
2521-
tok2->skippedByRescan = false;
2522-
if (hasMacroName)
2539+
if (hasMacroName) {
25232540
*hasMacroName = true;
2541+
} else {
2542+
// the substituted tokens are rescanned again, so the marks that the
2543+
// expansions above left on them do not apply
2544+
for (Token *tok2 = outputEnd ? outputEnd->next : output.front(); tok2; tok2 = tok2->next)
2545+
tok2->skippedByRescan = false;
2546+
}
25242547
}
25252548
return true;
25262549
}

0 commit comments

Comments
 (0)