@@ -1652,6 +1652,41 @@ namespace simplecpp {
16521652 return functionLike () ? parametertokens2.back ()->next : nameTokInst->next ;
16531653 }
16541654
1655+ const Token *recursiveExpandToken (TokenList *output, TokenList &temp, const Location &loc, const Token *tok, const std::map<TokenString,Macro> ¯os, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> ¶metertokens) const {
1656+ if (!(temp.cback () && temp.cback ()->name && tok->next && tok->next ->op == ' (' )) {
1657+ output->takeTokens (temp);
1658+ return tok->next ;
1659+ }
1660+
1661+ if (!sameline (tok, tok->next )) {
1662+ output->takeTokens (temp);
1663+ return tok->next ;
1664+ }
1665+
1666+ const std::map<TokenString, Macro>::const_iterator it = macros.find (temp.cback ()->str ());
1667+ if (it == macros.end () || expandedmacros.find (temp.cback ()->str ()) != expandedmacros.end ()) {
1668+ output->takeTokens (temp);
1669+ return tok->next ;
1670+ }
1671+
1672+ const Macro &calledMacro = it->second ;
1673+ if (!calledMacro.functionLike ()) {
1674+ output->takeTokens (temp);
1675+ return tok->next ;
1676+ }
1677+
1678+ TokenList temp2 (files);
1679+ temp2.push_back (new Token (temp.cback ()->str (), tok->location ));
1680+
1681+ const Token *tok2 = appendTokens (&temp2, loc, tok->next , macros, expandedmacros, parametertokens);
1682+ if (!tok2)
1683+ return tok->next ;
1684+ output->takeTokens (temp);
1685+ output->deleteToken (output->back ());
1686+ calledMacro.expand (output, loc, temp2.cfront (), macros, expandedmacros);
1687+ return tok2->next ;
1688+ }
1689+
16551690 const Token *expandToken (TokenList *output, const Location &loc, const Token *tok, const std::map<TokenString,Macro> ¯os, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> ¶metertokens) const {
16561691 // Not name..
16571692 if (!tok->name ) {
@@ -1662,50 +1697,22 @@ namespace simplecpp {
16621697 // Macro parameter..
16631698 {
16641699 TokenList temp (files);
1665- if (expandArg (&temp, tok, loc, macros, expandedmacros, parametertokens)) {
1666- if (!(temp.cback () && temp.cback ()->name && tok->next && tok->next ->op == ' (' )) {
1667- output->takeTokens (temp);
1668- return tok->next ;
1669- }
1670-
1671- if (!sameline (tok, tok->next )) {
1672- output->takeTokens (temp);
1673- return tok->next ;
1674- }
1675-
1676- const std::map<TokenString, Macro>::const_iterator it = macros.find (temp.cback ()->str ());
1677- if (it == macros.end () || expandedmacros.find (temp.cback ()->str ()) != expandedmacros.end ()) {
1678- output->takeTokens (temp);
1679- return tok->next ;
1680- }
1681-
1682- const Macro &calledMacro = it->second ;
1683- if (!calledMacro.functionLike ()) {
1684- output->takeTokens (temp);
1685- return tok->next ;
1686- }
1687-
1688- TokenList temp2 (files);
1689- temp2.push_back (new Token (temp.cback ()->str (), tok->location ));
1690-
1691- const Token *tok2 = appendTokens (&temp2, loc, tok->next , macros, expandedmacros, parametertokens);
1692- if (!tok2)
1693- return tok->next ;
1694-
1695- output->takeTokens (temp);
1696- output->deleteToken (output->back ());
1697- calledMacro.expand (output, loc, temp2.cfront (), macros, expandedmacros);
1698-
1699- return tok2->next ;
1700- }
1700+ if (expandArg (&temp, tok, loc, macros, expandedmacros, parametertokens))
1701+ return recursiveExpandToken (output, temp, loc, tok, macros, expandedmacros, parametertokens);
17011702 }
17021703
17031704 // Macro..
17041705 const std::map<TokenString, Macro>::const_iterator it = macros.find (tok->str ());
17051706 if (it != macros.end () && expandedmacros.find (tok->str ()) == expandedmacros.end ()) {
1707+ std::set<std::string> expandedmacros2 (expandedmacros);
1708+ expandedmacros2.insert (tok->str ());
1709+
17061710 const Macro &calledMacro = it->second ;
1707- if (!calledMacro.functionLike ())
1708- return calledMacro.expand (output, loc, tok, macros, expandedmacros);
1711+ if (!calledMacro.functionLike ()) {
1712+ TokenList temp (files);
1713+ calledMacro.expand (&temp, loc, tok, macros, expandedmacros);
1714+ return recursiveExpandToken (output, temp, loc, tok, macros, expandedmacros2, parametertokens);
1715+ }
17091716 if (!sameline (tok, tok->next ) || tok->next ->op != ' (' ) {
17101717 output->push_back (newMacroToken (tok->str (), loc, true ));
17111718 return tok->next ;
@@ -1717,8 +1724,9 @@ namespace simplecpp {
17171724 output->push_back (newMacroToken (tok->str (), loc, true ));
17181725 return tok->next ;
17191726 }
1720- calledMacro.expand (output, loc, tokens.cfront (), macros, expandedmacros);
1721- return tok2->next ;
1727+ TokenList temp (files);
1728+ calledMacro.expand (&temp, loc, tokens.cfront (), macros, expandedmacros);
1729+ return recursiveExpandToken (output, temp, loc, tok2, macros, expandedmacros2, parametertokens);
17221730 }
17231731
17241732 else if (tok->str () == DEFINED ) {
0 commit comments