+ {
+ source_location loc;
+ if (/* The top-level macro invocation that triggered the expansion
+ we are looking at is with a standard macro ...*/
+ !(pfile->top_most_macro_node->flags & NODE_BUILTIN)
+ /* ... and it's a function-like macro invocation. */
+ && pfile->top_most_macro_node->value.macro->fun_like)
+ /* Then the location of the end of the macro invocation is the
+ location of the closing parenthesis. */
+ loc = pfile->cur_token[-1].src_loc;
+ else
+ /* Otherwise, the location of the end of the macro invocation is
+ the location of the expansion point of that top-level macro
+ invocation. */
+ loc = location;
+
+ return builtin_macro (pfile, node, loc);
+ }
+}
+
+/* De-allocate the memory used by BUFF which is an array of instances
+ of macro_arg. NUM_ARGS is the number of instances of macro_arg
+ present in BUFF. */
+static void
+delete_macro_args (_cpp_buff *buff, unsigned num_args)
+{
+ macro_arg *macro_args;
+ unsigned i;
+
+ if (buff == NULL)
+ return;
+
+ macro_args = (macro_arg *) buff->base;
+
+ /* Walk instances of macro_arg to free their expanded tokens as well
+ as their macro_arg::virt_locs members. */
+ for (i = 0; i < num_args; ++i)
+ {
+ if (macro_args[i].expanded)
+ {
+ free (macro_args[i].expanded);
+ macro_args[i].expanded = NULL;
+ }
+ if (macro_args[i].virt_locs)
+ {
+ free (macro_args[i].virt_locs);
+ macro_args[i].virt_locs = NULL;
+ }
+ if (macro_args[i].expanded_virt_locs)
+ {
+ free (macro_args[i].expanded_virt_locs);
+ macro_args[i].expanded_virt_locs = NULL;
+ }
+ }
+ _cpp_free_buff (buff);
+}
+
+/* Set the INDEXth token of the macro argument ARG. TOKEN is the token
+ to set, LOCATION is its virtual location. "Virtual" location means
+ the location that encodes loci across macro expansion. Otherwise
+ it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the
+ argument ARG is supposed to contain. Note that ARG must be
+ tailored so that it has enough room to contain INDEX + 1 numbers of
+ tokens, at least. */
+static void
+set_arg_token (macro_arg *arg, const cpp_token *token,
+ source_location location, size_t index,
+ enum macro_arg_token_kind kind,
+ bool track_macro_exp_p)
+{
+ const cpp_token **token_ptr;
+ source_location *loc = NULL;
+
+ token_ptr =
+ arg_token_ptr_at (arg, index, kind,
+ track_macro_exp_p ? &loc : NULL);
+ *token_ptr = token;
+
+ if (loc != NULL)
+ {
+#ifdef ENABLE_CHECKING
+ if (kind == MACRO_ARG_TOKEN_STRINGIFIED
+ || !track_macro_exp_p)
+ /* We can't set the location of a stringified argument
+ token and we can't set any location if we aren't tracking
+ macro expansion locations. */
+ abort ();
+#endif
+ *loc = location;
+ }
+}
+
+/* Get the pointer to the location of the argument token of the
+ function-like macro argument ARG. This function must be called
+ only when we -ftrack-macro-expansion is on. */
+static const source_location *
+get_arg_token_location (const macro_arg *arg,
+ enum macro_arg_token_kind kind)
+{
+ const source_location *loc = NULL;
+ const cpp_token **token_ptr =
+ arg_token_ptr_at (arg, 0, kind, (source_location **) &loc);
+
+ if (token_ptr == NULL)
+ return NULL;
+
+ return loc;
+}
+
+/* Return the pointer to the INDEXth token of the macro argument ARG.
+ KIND specifies the kind of token the macro argument ARG contains.
+ If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
+ of the virtual location of the returned token if the
+ -ftrack-macro-expansion flag is on; otherwise, it's set to the
+ spelling location of the returned token. */
+static const cpp_token **
+arg_token_ptr_at (const macro_arg *arg, size_t index,
+ enum macro_arg_token_kind kind,
+ source_location **virt_location)
+{
+ const cpp_token **tokens_ptr = NULL;
+
+ switch (kind)
+ {
+ case MACRO_ARG_TOKEN_NORMAL:
+ tokens_ptr = arg->first;
+ break;
+ case MACRO_ARG_TOKEN_STRINGIFIED:
+ tokens_ptr = (const cpp_token **) &arg->stringified;
+ break;
+ case MACRO_ARG_TOKEN_EXPANDED:
+ tokens_ptr = arg->expanded;
+ break;
+ }
+
+ if (tokens_ptr == NULL)
+ /* This can happen for e.g, an empty token argument to a
+ funtion-like macro. */
+ return tokens_ptr;
+
+ if (virt_location)
+ {
+ if (kind == MACRO_ARG_TOKEN_NORMAL)
+ *virt_location = &arg->virt_locs[index];
+ else if (kind == MACRO_ARG_TOKEN_EXPANDED)
+ *virt_location = &arg->expanded_virt_locs[index];
+ else if (kind == MACRO_ARG_TOKEN_STRINGIFIED)
+ *virt_location =
+ (source_location *) &tokens_ptr[index]->src_loc;
+ }
+ return &tokens_ptr[index];
+}
+
+/* Initialize an iterator so that it iterates over the tokens of a
+ function-like macro argument. KIND is the kind of tokens we want
+ ITER to iterate over. TOKEN_PTR points the first token ITER will
+ iterate over. */
+static void
+macro_arg_token_iter_init (macro_arg_token_iter *iter,
+ bool track_macro_exp_p,
+ enum macro_arg_token_kind kind,
+ const macro_arg *arg,
+ const cpp_token **token_ptr)
+{
+ iter->track_macro_exp_p = track_macro_exp_p;
+ iter->kind = kind;
+ iter->token_ptr = token_ptr;
+ /* Unconditionally initialize this so that the compiler doesn't warn
+ about iter->location_ptr being possibly uninitialized later after
+ this code has been inlined somewhere. */
+ iter->location_ptr = NULL;
+ if (track_macro_exp_p)
+ iter->location_ptr = get_arg_token_location (arg, kind);
+#ifdef ENABLE_CHECKING
+ iter->num_forwards = 0;
+ if (track_macro_exp_p
+ && token_ptr != NULL
+ && iter->location_ptr == NULL)
+ abort ();
+#endif
+}
+
+/* Move the iterator one token forward. Note that if IT was
+ initialized on an argument that has a stringified token, moving it
+ forward doesn't make sense as a stringified token is essentially one
+ string. */
+static void
+macro_arg_token_iter_forward (macro_arg_token_iter *it)
+{
+ switch (it->kind)
+ {
+ case MACRO_ARG_TOKEN_NORMAL:
+ case MACRO_ARG_TOKEN_EXPANDED:
+ it->token_ptr++;
+ if (it->track_macro_exp_p)
+ it->location_ptr++;
+ break;
+ case MACRO_ARG_TOKEN_STRINGIFIED:
+#ifdef ENABLE_CHECKING
+ if (it->num_forwards > 0)
+ abort ();
+#endif
+ break;
+ }
+
+#ifdef ENABLE_CHECKING
+ it->num_forwards++;
+#endif
+}
+
+/* Return the token pointed to by the iterator. */
+static const cpp_token *
+macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
+{
+#ifdef ENABLE_CHECKING
+ if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
+ && it->num_forwards > 0)
+ abort ();
+#endif
+ if (it->token_ptr == NULL)
+ return NULL;
+ return *it->token_ptr;
+}
+
+/* Return the location of the token pointed to by the iterator.*/
+static source_location
+macro_arg_token_iter_get_location (const macro_arg_token_iter *it)
+{
+#ifdef ENABLE_CHECKING
+ if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
+ && it->num_forwards > 0)
+ abort ();
+#endif
+ if (it->track_macro_exp_p)
+ return *it->location_ptr;
+ else
+ return (*it->token_ptr)->src_loc;
+}
+
+/* Return the index of a token [resulting from macro expansion] inside
+ the total list of tokens resulting from a given macro
+ expansion. The index can be different depending on whether if we
+ want each tokens resulting from function-like macro arguments
+ expansion to have a different location or not.
+
+ E.g, consider this function-like macro:
+
+ #define M(x) x - 3
+
+ Then consider us "calling" it (and thus expanding it) like:
+
+ M(1+4)
+
+ It will be expanded into:
+
+ 1+4-3
+
+ Let's consider the case of the token '4'.
+
+ Its index can be 2 (it's the third token of the set of tokens
+ resulting from the expansion) or it can be 0 if we consider that
+ all tokens resulting from the expansion of the argument "1+2" have
+ the same index, which is 0. In this later case, the index of token
+ '-' would then be 1 and the index of token '3' would be 2.
+
+ The later case is useful to use less memory e.g, for the case of
+ the user using the option -ftrack-macro-expansion=1.
+
+ ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
+ are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro
+ parameter (inside the macro replacement list) that corresponds to
+ the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
+ of.
+
+ If we refer to the example above, for the '4' argument token,
+ ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
+ would be set to the token 'x', in the replacement list "x - 3" of
+ macro M.
+
+ This is a subroutine of replace_args. */
+inline static unsigned
+expanded_token_index (cpp_reader *pfile, cpp_macro *macro,
+ const cpp_token *cur_replacement_token,
+ unsigned absolute_token_index)
+{
+ if (CPP_OPTION (pfile, track_macro_expansion) > 1)
+ return absolute_token_index;
+ return cur_replacement_token - macro->exp.tokens;