Skip to content

Commit fd4f092

Browse files
author
Your Name
committed
Remove the while(true) loop
1 parent 96fc564 commit fd4f092

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

simplecpp.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,21 +2089,24 @@ namespace simplecpp {
20892089
return functionLike() ? parametertokens2.back()->next : nameTokInst->next;
20902090
}
20912091

2092-
const Token *recursiveExpandToken(TokenList &output, TokenList &temp, const Location &loc, const Token *tok, const MacroMap &macros, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> &parametertokens) const {
2093-
// Loop: the expansion result may itself end with the name of a function-like
2094-
// macro whose arguments are supplied by the tokens that follow it.
2095-
while (true) {
2096-
if (!temp.cback() || !temp.cback()->name || !tok->next || tok->next->op != '(' || !sameline(tok, tok->next))
2097-
break;
2098-
2099-
const MacroMap::const_iterator it = macros.find(temp.cback()->str());
2100-
if (it == macros.end() || expandedmacros.find(temp.cback()->str()) != expandedmacros.end())
2101-
break;
2102-
2103-
const Macro &calledMacro = it->second;
2104-
if (!calledMacro.functionLike() || temp.cback()->isExpandedFrom(&calledMacro))
2105-
break;
2092+
/** Returns the macro to expand when the last token of @p temp is the name of a
2093+
* function-like macro and the tokens after @p tok supply its arguments; nullptr otherwise */
2094+
static const Macro *rescanMacro(const TokenList &temp, const Token *tok, const MacroMap &macros, const std::set<TokenString> &expandedmacros) {
2095+
if (!temp.cback() || !temp.cback()->name || !sameline(tok, tok->next) || tok->next->op != '(')
2096+
return nullptr;
2097+
const MacroMap::const_iterator it = macros.find(temp.cback()->str());
2098+
if (it == macros.end() || expandedmacros.find(temp.cback()->str()) != expandedmacros.end())
2099+
return nullptr;
2100+
if (!it->second.functionLike() || temp.cback()->isExpandedFrom(&it->second))
2101+
return nullptr;
2102+
return &it->second;
2103+
}
21062104

2105+
const Token *recursiveExpandToken(TokenList &output, TokenList &temp, const Location &loc, const Token *tok, const MacroMap &macros, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> &parametertokens) const {
2106+
// Expand while the expansion result ends with the name of a function-like
2107+
// macro whose arguments are supplied by the tokens that follow it. Each round
2108+
// consumes that macro call from the token stream, so tok always advances.
2109+
while (const Macro *calledMacro = rescanMacro(temp, tok, macros, expandedmacros)) {
21072110
TokenList temp2(files);
21082111
temp2.push_back(new Token(temp.cback()->str(), tok->location));
21092112

@@ -2112,7 +2115,7 @@ namespace simplecpp {
21122115
break;
21132116
output.takeTokens(temp);
21142117
output.deleteToken(output.back());
2115-
calledMacro.expand(temp, loc, temp2.cfront(), macros, expandedmacros);
2118+
calledMacro->expand(temp, loc, temp2.cfront(), macros, expandedmacros);
21162119
tok = tok2;
21172120
}
21182121
output.takeTokens(temp);

0 commit comments

Comments
 (0)