@@ -3025,6 +3025,18 @@ namespace simplecpp {
30253025 const Token *raw;
30263026 };
30273027
3028+ /* * What a macro call is replaced by depends on */
3029+ struct MacroCall {
3030+ /* * the hide set of the result */
3031+ std::set<TokenString> hideSet;
3032+ /* * the arguments as written in the call */
3033+ std::vector<TokenList> arguments;
3034+ /* * the arguments, macro expanded */
3035+ std::vector<TokenList> expandedArguments;
3036+ /* * there is whitespace after the call */
3037+ bool whitespaceAfter{};
3038+ };
3039+
30283040 static Macro::Error unsupported (const Location &loc, const std::string &what) {
30293041 return {loc, what + " is not supported by the hide-set expansion yet" };
30303042 }
@@ -3054,35 +3066,44 @@ namespace simplecpp {
30543066 continue ;
30553067 }
30563068
3057- std::set<TokenString> hideSet;
3058- std::vector<TokenList> arguments;
3059- std::vector<TokenList> expandedArguments;
3060- bool whitespaceAfter = tok->whitespaceahead ;
3061- if (macro.functionLike ()) {
3062- TokenList call (files);
3063- call.push_back (tok);
3064- readCall (call, input);
3065- const std::vector<const Token *> delimiters = macro.getMacroParameters (call.cfront (), false );
3066- // the hide set of the name and the ) have the macro calls that this call
3067- // was produced by, the arguments are not affected by them
3068- const std::set<TokenString> &hs1 = call.cfront ()->hideSet ();
3069- const std::set<TokenString> &hs2 = delimiters.back ()->hideSet ();
3070- std::set_intersection (hs1.begin (), hs1.end (), hs2.begin (), hs2.end (), std::inserter (hideSet, hideSet.begin ()));
3071- arguments = getArguments (macro, delimiters);
3072- expandedArguments = expandArguments (arguments);
3073- whitespaceAfter = delimiters.back ()->whitespaceahead ;
3074- } else {
3075- hideSet = tok->hideSet ();
3076- delete tok;
3077- }
3078- hideSet.insert (macro.name ());
3079-
3069+ const MacroCall call = readMacroCall (macro, tok, input);
30803070 TokenList result (files);
3081- substitute (result, macro, arguments, expandedArguments, hideSet, whitespaceAfter );
3071+ substitute (result, macro, call );
30823072 input.pushFront (result);
30833073 }
30843074 }
30853075
3076+ /* *
3077+ * Read a macro call: the name, and "( ... )" for a function-like macro.
3078+ * @param macro the macro
3079+ * @param nameTok the macro name, it is deleted
3080+ * @param input the input after the name
3081+ * @throws wrongNumberOfParameters if a function-like macro has the wrong number of arguments
3082+ */
3083+ MacroCall readMacroCall (const Macro ¯o, Token *nameTok, Input &input) {
3084+ MacroCall call;
3085+ if (!macro.functionLike ()) {
3086+ call.hideSet = nameTok->hideSet ();
3087+ call.whitespaceAfter = nameTok->whitespaceahead ;
3088+ delete nameTok;
3089+ } else {
3090+ TokenList tokens (files);
3091+ tokens.push_back (nameTok);
3092+ readCall (tokens, input);
3093+ const std::vector<const Token *> delimiters = macro.getMacroParameters (tokens.cfront (), false );
3094+ // the name and the ) have the hide sets of the macro calls that this call
3095+ // was produced by, the arguments are not affected by them
3096+ const std::set<TokenString> &hs1 = tokens.cfront ()->hideSet ();
3097+ const std::set<TokenString> &hs2 = delimiters.back ()->hideSet ();
3098+ std::set_intersection (hs1.begin (), hs1.end (), hs2.begin (), hs2.end (), std::inserter (call.hideSet , call.hideSet .begin ()));
3099+ call.arguments = getArguments (macro, delimiters);
3100+ call.expandedArguments = expandArguments (call.arguments );
3101+ call.whitespaceAfter = delimiters.back ()->whitespaceahead ;
3102+ }
3103+ call.hideSet .insert (macro.name ());
3104+ return call;
3105+ }
3106+
30863107 /* * Read "( ... )" after the macro name in call */
30873108 static void readCall (TokenList &call, Input &input) {
30883109 int par = 0 ;
@@ -3183,23 +3204,22 @@ namespace simplecpp {
31833204 * @param operand destination tokenlist
31843205 * @param macro the macro
31853206 * @param tok the token in the replacement list; for # it is moved to the parameter
3186- * @param arguments the arguments as written in the call
3187- * @param expandedArguments the arguments, macro expanded
3207+ * @param call the macro call
31883208 * @param expand use the expanded argument for a parameter
31893209 */
3190- void getOperand (TokenList &operand, const Macro ¯o, const Token *&tok, const std::vector<TokenList> &arguments, const std::vector<TokenList> &expandedArguments , bool expand) const {
3210+ void getOperand (TokenList &operand, const Macro ¯o, const Token *&tok, const MacroCall &call , bool expand) const {
31913211 const Token * const next = (tok->next != macro.endToken ) ? tok->next : nullptr ;
31923212 // # is an operator in a function-like macro when it is followed by a parameter
31933213 const unsigned int hashArg = (tok->op == ' #' ) ? getParameter (macro, next) : ~0U ;
3194- if (hashArg < arguments.size ()) {
3195- operand.push_back (new Token (stringify (arguments[hashArg], loc, macro.name (), outputList), loc, next->whitespaceahead ));
3214+ if (hashArg < call. arguments .size ()) {
3215+ operand.push_back (new Token (stringify (call. arguments [hashArg], loc, macro.name (), outputList), loc, next->whitespaceahead ));
31963216 operand.back ()->macro = macro.name ();
31973217 tok = next;
31983218 return ;
31993219 }
32003220 const unsigned int argnr = getParameter (macro, tok);
3201- if (argnr < arguments.size ()) {
3202- const TokenList &argument = expand ? expandedArguments[argnr] : arguments[argnr];
3221+ if (argnr < call. arguments .size ()) {
3222+ const TokenList &argument = expand ? call. expandedArguments [argnr] : call. arguments [argnr];
32033223 for (const Token *argtok = argument.cfront (); argtok; argtok = argtok->next )
32043224 operand.push_back (new Token (*argtok));
32053225 return ;
@@ -3210,14 +3230,11 @@ namespace simplecpp {
32103230
32113231 /* *
32123232 * The replacement list of the macro, with the parameters replaced by the arguments.
3213- * @param result destination tokenlist
3214- * @param macro the macro
3215- * @param arguments the arguments as written in the call, for #
3216- * @param expandedArguments the arguments, macro expanded
3217- * @param hideSet the hide set of the result
3218- * @param whitespaceAfter there is whitespace after the macro call
3233+ * @param result destination tokenlist
3234+ * @param macro the macro
3235+ * @param call the macro call
32193236 */
3220- void substitute (TokenList &result, const Macro ¯o, const std::vector<TokenList> &arguments, const std::vector<TokenList> &expandedArguments, const std::set<TokenString> &hideSet, bool whitespaceAfter ) const {
3237+ void substitute (TokenList &result, const Macro ¯o, const MacroCall &call ) const {
32213238 if (macro.variadicOpt )
32223239 throw unsupported (loc, " __VA_OPT__" );
32233240 const Token *hashHash = nullptr ; // the ## before the current operand
@@ -3233,11 +3250,11 @@ namespace simplecpp {
32333250 if (tok->str () == DEFINED )
32343251 throw unsupported (loc, " defined" );
32353252 const Token * const operandTok = tok;
3236- const bool parameter = getParameter (macro, tok) < arguments.size ();
3253+ const bool parameter = getParameter (macro, tok) < call. arguments .size ();
32373254 // an argument is not expanded when it is an operand of ##
32383255 const bool expand = !hashHash && !isHashHash (macro, tok->next );
32393256 TokenList operand (files);
3240- getOperand (operand, macro, tok, arguments, expandedArguments , expand);
3257+ getOperand (operand, macro, tok, call , expand);
32413258 const bool empty = operand.empty ();
32423259 if (hashHash)
32433260 paste (result, operand, previousEmpty, macro, hashHash, operandTok);
@@ -3253,13 +3270,13 @@ namespace simplecpp {
32533270 throw Macro::invalidHashHash::unexpectedNewline (hashHash->location , macro.name ());
32543271 for (Token *tok = result.front (); tok; tok = tok->next ) {
32553272 tok->location = loc;
3256- for (const TokenString &name : hideSet)
3273+ for (const TokenString &name : call. hideSet )
32573274 tok->addToHideSet (name);
32583275 }
32593276 // whitespace at the end of the replacement list does not count, the
32603277 // expansion is followed by the whitespace after the macro call
32613278 if (result.back ())
3262- result.back ()->whitespaceahead = whitespaceAfter;
3279+ result.back ()->whitespaceahead = call. whitespaceAfter ;
32633280 }
32643281
32653282 const MacroMap ¯os;
0 commit comments