136ac495dSmrg /* Part of CPP library. (Macro and #define handling.)
2*8feb0f0bSmrg Copyright (C) 1986-2020 Free Software Foundation, Inc.
336ac495dSmrg Written by Per Bothner, 1994.
436ac495dSmrg Based on CCCP program by Paul Rubin, June 1986
536ac495dSmrg Adapted to ANSI C, Richard Stallman, Jan 1987
636ac495dSmrg
736ac495dSmrg This program is free software; you can redistribute it and/or modify it
836ac495dSmrg under the terms of the GNU General Public License as published by the
936ac495dSmrg Free Software Foundation; either version 3, or (at your option) any
1036ac495dSmrg later version.
1136ac495dSmrg
1236ac495dSmrg This program is distributed in the hope that it will be useful,
1336ac495dSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of
1436ac495dSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1536ac495dSmrg GNU General Public License for more details.
1636ac495dSmrg
1736ac495dSmrg You should have received a copy of the GNU General Public License
1836ac495dSmrg along with this program; see the file COPYING3. If not see
1936ac495dSmrg <http://www.gnu.org/licenses/>.
2036ac495dSmrg
2136ac495dSmrg In other words, you are welcome to use, share and improve this program.
2236ac495dSmrg You are forbidden to forbid anyone else to use, share and improve
2336ac495dSmrg what you give them. Help stamp out software-hoarding! */
2436ac495dSmrg
2536ac495dSmrg #include "config.h"
2636ac495dSmrg #include "system.h"
2736ac495dSmrg #include "cpplib.h"
2836ac495dSmrg #include "internal.h"
2936ac495dSmrg
3036ac495dSmrg typedef struct macro_arg macro_arg;
3136ac495dSmrg /* This structure represents the tokens of a macro argument. These
3236ac495dSmrg tokens can be macro themselves, in which case they can be either
3336ac495dSmrg expanded or unexpanded. When they are expanded, this data
3436ac495dSmrg structure keeps both the expanded and unexpanded forms. */
3536ac495dSmrg struct macro_arg
3636ac495dSmrg {
3736ac495dSmrg const cpp_token **first; /* First token in unexpanded argument. */
3836ac495dSmrg const cpp_token **expanded; /* Macro-expanded argument. */
3936ac495dSmrg const cpp_token *stringified; /* Stringified argument. */
4036ac495dSmrg unsigned int count; /* # of tokens in argument. */
4136ac495dSmrg unsigned int expanded_count; /* # of tokens in expanded argument. */
42c0a68be4Smrg location_t *virt_locs; /* Where virtual locations for
4336ac495dSmrg unexpanded tokens are stored. */
44c0a68be4Smrg location_t *expanded_virt_locs; /* Where virtual locations for
4536ac495dSmrg expanded tokens are
4636ac495dSmrg stored. */
4736ac495dSmrg };
4836ac495dSmrg
4936ac495dSmrg /* The kind of macro tokens which the instance of
5036ac495dSmrg macro_arg_token_iter is supposed to iterate over. */
5136ac495dSmrg enum macro_arg_token_kind {
5236ac495dSmrg MACRO_ARG_TOKEN_NORMAL,
5336ac495dSmrg /* This is a macro argument token that got transformed into a string
54a2dc1f3fSmrg literal, e.g. #foo. */
5536ac495dSmrg MACRO_ARG_TOKEN_STRINGIFIED,
5636ac495dSmrg /* This is a token resulting from the expansion of a macro
5736ac495dSmrg argument that was itself a macro. */
5836ac495dSmrg MACRO_ARG_TOKEN_EXPANDED
5936ac495dSmrg };
6036ac495dSmrg
6136ac495dSmrg /* An iterator over tokens coming from a function-like macro
6236ac495dSmrg argument. */
6336ac495dSmrg typedef struct macro_arg_token_iter macro_arg_token_iter;
6436ac495dSmrg struct macro_arg_token_iter
6536ac495dSmrg {
6636ac495dSmrg /* Whether or not -ftrack-macro-expansion is used. */
6736ac495dSmrg bool track_macro_exp_p;
6836ac495dSmrg /* The kind of token over which we are supposed to iterate. */
6936ac495dSmrg enum macro_arg_token_kind kind;
7036ac495dSmrg /* A pointer to the current token pointed to by the iterator. */
7136ac495dSmrg const cpp_token **token_ptr;
7236ac495dSmrg /* A pointer to the "full" location of the current token. If
7336ac495dSmrg -ftrack-macro-expansion is used this location tracks loci across
7436ac495dSmrg macro expansion. */
75c0a68be4Smrg const location_t *location_ptr;
7636ac495dSmrg #if CHECKING_P
7736ac495dSmrg /* The number of times the iterator went forward. This useful only
7836ac495dSmrg when checking is enabled. */
7936ac495dSmrg size_t num_forwards;
8036ac495dSmrg #endif
8136ac495dSmrg };
8236ac495dSmrg
8336ac495dSmrg /* Saved data about an identifier being used as a macro argument
8436ac495dSmrg name. */
8536ac495dSmrg struct macro_arg_saved_data {
8636ac495dSmrg /* The canonical (UTF-8) spelling of this identifier. */
8736ac495dSmrg cpp_hashnode *canonical_node;
88c0a68be4Smrg /* The previous value & type of this identifier. */
8936ac495dSmrg union _cpp_hashnode_value value;
90c0a68be4Smrg node_type type;
9136ac495dSmrg };
9236ac495dSmrg
93a2dc1f3fSmrg static const char *vaopt_paste_error =
94a2dc1f3fSmrg N_("'##' cannot appear at either end of __VA_OPT__");
95a2dc1f3fSmrg
96*8feb0f0bSmrg static void expand_arg (cpp_reader *, macro_arg *);
97*8feb0f0bSmrg
98a2dc1f3fSmrg /* A class for tracking __VA_OPT__ state while iterating over a
99a2dc1f3fSmrg sequence of tokens. This is used during both macro definition and
100a2dc1f3fSmrg expansion. */
101a2dc1f3fSmrg class vaopt_state {
102a2dc1f3fSmrg
103a2dc1f3fSmrg public:
104a2dc1f3fSmrg
105a2dc1f3fSmrg enum update_type
106a2dc1f3fSmrg {
107a2dc1f3fSmrg ERROR,
108a2dc1f3fSmrg DROP,
109a2dc1f3fSmrg INCLUDE,
110a2dc1f3fSmrg BEGIN,
111a2dc1f3fSmrg END
112a2dc1f3fSmrg };
113a2dc1f3fSmrg
114*8feb0f0bSmrg /* Initialize the state tracker. ANY_ARGS is true if variable
115*8feb0f0bSmrg arguments were provided to the macro invocation. */
vaopt_state(cpp_reader * pfile,bool is_variadic,macro_arg * arg)116*8feb0f0bSmrg vaopt_state (cpp_reader *pfile, bool is_variadic, macro_arg *arg)
117*8feb0f0bSmrg : m_pfile (pfile),
118*8feb0f0bSmrg m_arg (arg),
119*8feb0f0bSmrg m_variadic (is_variadic),
120*8feb0f0bSmrg m_last_was_paste (false),
121*8feb0f0bSmrg m_state (0),
122*8feb0f0bSmrg m_paste_location (0),
123*8feb0f0bSmrg m_location (0),
124*8feb0f0bSmrg m_update (ERROR)
125*8feb0f0bSmrg {
126*8feb0f0bSmrg }
127*8feb0f0bSmrg
128a2dc1f3fSmrg /* Given a token, update the state of this tracker and return a
129a2dc1f3fSmrg boolean indicating whether the token should be be included in the
130a2dc1f3fSmrg expansion. */
update(const cpp_token * token)131a2dc1f3fSmrg update_type update (const cpp_token *token)
132a2dc1f3fSmrg {
133a2dc1f3fSmrg /* If the macro isn't variadic, just don't bother. */
134a2dc1f3fSmrg if (!m_variadic)
135a2dc1f3fSmrg return INCLUDE;
136a2dc1f3fSmrg
137a2dc1f3fSmrg if (token->type == CPP_NAME
138a2dc1f3fSmrg && token->val.node.node == m_pfile->spec_nodes.n__VA_OPT__)
139a2dc1f3fSmrg {
140a2dc1f3fSmrg if (m_state > 0)
141a2dc1f3fSmrg {
142a2dc1f3fSmrg cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc,
143a2dc1f3fSmrg "__VA_OPT__ may not appear in a __VA_OPT__");
144a2dc1f3fSmrg return ERROR;
145a2dc1f3fSmrg }
146a2dc1f3fSmrg ++m_state;
147a2dc1f3fSmrg m_location = token->src_loc;
148a2dc1f3fSmrg return BEGIN;
149a2dc1f3fSmrg }
150a2dc1f3fSmrg else if (m_state == 1)
151a2dc1f3fSmrg {
152a2dc1f3fSmrg if (token->type != CPP_OPEN_PAREN)
153a2dc1f3fSmrg {
154a2dc1f3fSmrg cpp_error_at (m_pfile, CPP_DL_ERROR, m_location,
155a2dc1f3fSmrg "__VA_OPT__ must be followed by an "
156a2dc1f3fSmrg "open parenthesis");
157a2dc1f3fSmrg return ERROR;
158a2dc1f3fSmrg }
159a2dc1f3fSmrg ++m_state;
160*8feb0f0bSmrg if (m_update == ERROR)
161*8feb0f0bSmrg {
162*8feb0f0bSmrg if (m_arg == NULL)
163*8feb0f0bSmrg m_update = INCLUDE;
164*8feb0f0bSmrg else
165*8feb0f0bSmrg {
166*8feb0f0bSmrg m_update = DROP;
167*8feb0f0bSmrg if (!m_arg->expanded)
168*8feb0f0bSmrg expand_arg (m_pfile, m_arg);
169*8feb0f0bSmrg for (unsigned idx = 0; idx < m_arg->expanded_count; ++idx)
170*8feb0f0bSmrg if (m_arg->expanded[idx]->type != CPP_PADDING)
171*8feb0f0bSmrg {
172*8feb0f0bSmrg m_update = INCLUDE;
173*8feb0f0bSmrg break;
174*8feb0f0bSmrg }
175*8feb0f0bSmrg }
176*8feb0f0bSmrg }
177a2dc1f3fSmrg return DROP;
178a2dc1f3fSmrg }
179a2dc1f3fSmrg else if (m_state >= 2)
180a2dc1f3fSmrg {
181a2dc1f3fSmrg if (m_state == 2 && token->type == CPP_PASTE)
182a2dc1f3fSmrg {
183a2dc1f3fSmrg cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc,
184a2dc1f3fSmrg vaopt_paste_error);
185a2dc1f3fSmrg return ERROR;
186a2dc1f3fSmrg }
187a2dc1f3fSmrg /* Advance states before further considering this token, in
188a2dc1f3fSmrg case we see a close paren immediately after the open
189a2dc1f3fSmrg paren. */
190a2dc1f3fSmrg if (m_state == 2)
191a2dc1f3fSmrg ++m_state;
192a2dc1f3fSmrg
193a2dc1f3fSmrg bool was_paste = m_last_was_paste;
194a2dc1f3fSmrg m_last_was_paste = false;
195a2dc1f3fSmrg if (token->type == CPP_PASTE)
196a2dc1f3fSmrg {
197a2dc1f3fSmrg m_last_was_paste = true;
198a2dc1f3fSmrg m_paste_location = token->src_loc;
199a2dc1f3fSmrg }
200a2dc1f3fSmrg else if (token->type == CPP_OPEN_PAREN)
201a2dc1f3fSmrg ++m_state;
202a2dc1f3fSmrg else if (token->type == CPP_CLOSE_PAREN)
203a2dc1f3fSmrg {
204a2dc1f3fSmrg --m_state;
205a2dc1f3fSmrg if (m_state == 2)
206a2dc1f3fSmrg {
207a2dc1f3fSmrg /* Saw the final paren. */
208a2dc1f3fSmrg m_state = 0;
209a2dc1f3fSmrg
210a2dc1f3fSmrg if (was_paste)
211a2dc1f3fSmrg {
212a2dc1f3fSmrg cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc,
213a2dc1f3fSmrg vaopt_paste_error);
214a2dc1f3fSmrg return ERROR;
215a2dc1f3fSmrg }
216a2dc1f3fSmrg
217a2dc1f3fSmrg return END;
218a2dc1f3fSmrg }
219a2dc1f3fSmrg }
220*8feb0f0bSmrg return m_update;
221a2dc1f3fSmrg }
222a2dc1f3fSmrg
223a2dc1f3fSmrg /* Nothing to do with __VA_OPT__. */
224a2dc1f3fSmrg return INCLUDE;
225a2dc1f3fSmrg }
226a2dc1f3fSmrg
227a2dc1f3fSmrg /* Ensure that any __VA_OPT__ was completed. If ok, return true.
228a2dc1f3fSmrg Otherwise, issue an error and return false. */
completed()229a2dc1f3fSmrg bool completed ()
230a2dc1f3fSmrg {
231a2dc1f3fSmrg if (m_variadic && m_state != 0)
232a2dc1f3fSmrg cpp_error_at (m_pfile, CPP_DL_ERROR, m_location,
233a2dc1f3fSmrg "unterminated __VA_OPT__");
234a2dc1f3fSmrg return m_state == 0;
235a2dc1f3fSmrg }
236a2dc1f3fSmrg
237a2dc1f3fSmrg private:
238a2dc1f3fSmrg
239a2dc1f3fSmrg /* The cpp_reader. */
240a2dc1f3fSmrg cpp_reader *m_pfile;
241a2dc1f3fSmrg
242*8feb0f0bSmrg /* The __VA_ARGS__ argument. */
243*8feb0f0bSmrg macro_arg *m_arg;
244*8feb0f0bSmrg
245a2dc1f3fSmrg /* True if the macro is variadic. */
246a2dc1f3fSmrg bool m_variadic;
247a2dc1f3fSmrg /* If true, the previous token was ##. This is used to detect when
248a2dc1f3fSmrg a paste occurs at the end of the sequence. */
249a2dc1f3fSmrg bool m_last_was_paste;
250a2dc1f3fSmrg
251a2dc1f3fSmrg /* The state variable:
252a2dc1f3fSmrg 0 means not parsing
253a2dc1f3fSmrg 1 means __VA_OPT__ seen, looking for "("
254a2dc1f3fSmrg 2 means "(" seen (so the next token can't be "##")
255a2dc1f3fSmrg >= 3 means looking for ")", the number encodes the paren depth. */
256a2dc1f3fSmrg int m_state;
257a2dc1f3fSmrg
258a2dc1f3fSmrg /* The location of the paste token. */
259c0a68be4Smrg location_t m_paste_location;
260a2dc1f3fSmrg
261a2dc1f3fSmrg /* Location of the __VA_OPT__ token. */
262c0a68be4Smrg location_t m_location;
263*8feb0f0bSmrg
264*8feb0f0bSmrg /* If __VA_ARGS__ substitutes to no preprocessing tokens,
265*8feb0f0bSmrg INCLUDE, otherwise DROP. ERROR when unknown yet. */
266*8feb0f0bSmrg update_type m_update;
267a2dc1f3fSmrg };
268a2dc1f3fSmrg
26936ac495dSmrg /* Macro expansion. */
27036ac495dSmrg
27136ac495dSmrg static int enter_macro_context (cpp_reader *, cpp_hashnode *,
272c0a68be4Smrg const cpp_token *, location_t);
27336ac495dSmrg static int builtin_macro (cpp_reader *, cpp_hashnode *,
274c0a68be4Smrg location_t, location_t);
27536ac495dSmrg static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
27636ac495dSmrg const cpp_token **, unsigned int);
27736ac495dSmrg static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *,
278c0a68be4Smrg _cpp_buff *, location_t *,
27936ac495dSmrg const cpp_token **, unsigned int);
28036ac495dSmrg static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *,
28136ac495dSmrg _cpp_buff **, unsigned *);
28236ac495dSmrg static cpp_context *next_context (cpp_reader *);
28336ac495dSmrg static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
28436ac495dSmrg static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
28536ac495dSmrg static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
28636ac495dSmrg static void paste_all_tokens (cpp_reader *, const cpp_token *);
287c0a68be4Smrg static bool paste_tokens (cpp_reader *, location_t,
28836ac495dSmrg const cpp_token **, const cpp_token *);
28936ac495dSmrg static void alloc_expanded_arg_mem (cpp_reader *, macro_arg *, size_t);
29036ac495dSmrg static void ensure_expanded_arg_room (cpp_reader *, macro_arg *, size_t, size_t *);
29136ac495dSmrg static void delete_macro_args (_cpp_buff*, unsigned num_args);
29236ac495dSmrg static void set_arg_token (macro_arg *, const cpp_token *,
293c0a68be4Smrg location_t, size_t,
29436ac495dSmrg enum macro_arg_token_kind,
29536ac495dSmrg bool);
296c0a68be4Smrg static const location_t *get_arg_token_location (const macro_arg *,
29736ac495dSmrg enum macro_arg_token_kind);
29836ac495dSmrg static const cpp_token **arg_token_ptr_at (const macro_arg *,
29936ac495dSmrg size_t,
30036ac495dSmrg enum macro_arg_token_kind,
301c0a68be4Smrg location_t **virt_location);
30236ac495dSmrg
30336ac495dSmrg static void macro_arg_token_iter_init (macro_arg_token_iter *, bool,
30436ac495dSmrg enum macro_arg_token_kind,
30536ac495dSmrg const macro_arg *,
30636ac495dSmrg const cpp_token **);
30736ac495dSmrg static const cpp_token *macro_arg_token_iter_get_token
30836ac495dSmrg (const macro_arg_token_iter *it);
309c0a68be4Smrg static location_t macro_arg_token_iter_get_location
31036ac495dSmrg (const macro_arg_token_iter *);
31136ac495dSmrg static void macro_arg_token_iter_forward (macro_arg_token_iter *);
31236ac495dSmrg static _cpp_buff *tokens_buff_new (cpp_reader *, size_t,
313c0a68be4Smrg location_t **);
31436ac495dSmrg static size_t tokens_buff_count (_cpp_buff *);
31536ac495dSmrg static const cpp_token **tokens_buff_last_token_ptr (_cpp_buff *);
31636ac495dSmrg static inline const cpp_token **tokens_buff_put_token_to (const cpp_token **,
317c0a68be4Smrg location_t *,
31836ac495dSmrg const cpp_token *,
319c0a68be4Smrg location_t,
320c0a68be4Smrg location_t,
32136ac495dSmrg const line_map_macro *,
32236ac495dSmrg unsigned int);
32336ac495dSmrg
32436ac495dSmrg static const cpp_token **tokens_buff_add_token (_cpp_buff *,
325c0a68be4Smrg location_t *,
32636ac495dSmrg const cpp_token *,
327c0a68be4Smrg location_t,
328c0a68be4Smrg location_t,
32936ac495dSmrg const line_map_macro *,
33036ac495dSmrg unsigned int);
33136ac495dSmrg static inline void tokens_buff_remove_last_token (_cpp_buff *);
33236ac495dSmrg static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
333c0a68be4Smrg macro_arg *, location_t);
33436ac495dSmrg static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *,
33536ac495dSmrg _cpp_buff **, unsigned *);
336c0a68be4Smrg static cpp_macro *create_iso_definition (cpp_reader *);
33736ac495dSmrg
33836ac495dSmrg /* #define directive parsing and handling. */
33936ac495dSmrg
340c0a68be4Smrg static cpp_macro *lex_expansion_token (cpp_reader *, cpp_macro *);
34136ac495dSmrg static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *,
34236ac495dSmrg const cpp_macro *);
343c0a68be4Smrg static bool parse_params (cpp_reader *, unsigned *, bool *);
34436ac495dSmrg static void check_trad_stringification (cpp_reader *, const cpp_macro *,
34536ac495dSmrg const cpp_string *);
34636ac495dSmrg static bool reached_end_of_context (cpp_context *);
34736ac495dSmrg static void consume_next_token_from_context (cpp_reader *pfile,
34836ac495dSmrg const cpp_token **,
349c0a68be4Smrg location_t *);
350c0a68be4Smrg static const cpp_token* cpp_get_token_1 (cpp_reader *, location_t *);
35136ac495dSmrg
35236ac495dSmrg static cpp_hashnode* macro_of_context (cpp_context *context);
35336ac495dSmrg
35436ac495dSmrg static bool in_macro_expansion_p (cpp_reader *pfile);
35536ac495dSmrg
35636ac495dSmrg /* Statistical counter tracking the number of macros that got
35736ac495dSmrg expanded. */
35836ac495dSmrg unsigned num_expanded_macros_counter = 0;
35936ac495dSmrg /* Statistical counter tracking the total number tokens resulting
36036ac495dSmrg from macro expansion. */
36136ac495dSmrg unsigned num_macro_tokens_counter = 0;
36236ac495dSmrg
363*8feb0f0bSmrg /* Wrapper around cpp_get_token to skip CPP_PADDING tokens
364*8feb0f0bSmrg and not consume CPP_EOF. */
365*8feb0f0bSmrg static const cpp_token *
cpp_get_token_no_padding(cpp_reader * pfile)366*8feb0f0bSmrg cpp_get_token_no_padding (cpp_reader *pfile)
367*8feb0f0bSmrg {
368*8feb0f0bSmrg for (;;)
369*8feb0f0bSmrg {
370*8feb0f0bSmrg const cpp_token *ret = cpp_peek_token (pfile, 0);
371*8feb0f0bSmrg if (ret->type == CPP_EOF)
372*8feb0f0bSmrg return ret;
373*8feb0f0bSmrg ret = cpp_get_token (pfile);
374*8feb0f0bSmrg if (ret->type != CPP_PADDING)
375*8feb0f0bSmrg return ret;
376*8feb0f0bSmrg }
377*8feb0f0bSmrg }
378*8feb0f0bSmrg
379*8feb0f0bSmrg /* Handle meeting "__has_include" builtin macro. */
380*8feb0f0bSmrg
381*8feb0f0bSmrg static int
builtin_has_include(cpp_reader * pfile,cpp_hashnode * op,bool has_next)382*8feb0f0bSmrg builtin_has_include (cpp_reader *pfile, cpp_hashnode *op, bool has_next)
383*8feb0f0bSmrg {
384*8feb0f0bSmrg int result = 0;
385*8feb0f0bSmrg
386*8feb0f0bSmrg if (!pfile->state.in_directive)
387*8feb0f0bSmrg cpp_error (pfile, CPP_DL_ERROR,
388*8feb0f0bSmrg "\"%s\" used outside of preprocessing directive",
389*8feb0f0bSmrg NODE_NAME (op));
390*8feb0f0bSmrg
391*8feb0f0bSmrg pfile->state.angled_headers = true;
392*8feb0f0bSmrg const cpp_token *token = cpp_get_token_no_padding (pfile);
393*8feb0f0bSmrg bool paren = token->type == CPP_OPEN_PAREN;
394*8feb0f0bSmrg if (paren)
395*8feb0f0bSmrg token = cpp_get_token_no_padding (pfile);
396*8feb0f0bSmrg else
397*8feb0f0bSmrg cpp_error (pfile, CPP_DL_ERROR,
398*8feb0f0bSmrg "missing '(' before \"%s\" operand", NODE_NAME (op));
399*8feb0f0bSmrg pfile->state.angled_headers = false;
400*8feb0f0bSmrg
401*8feb0f0bSmrg bool bracket = token->type != CPP_STRING;
402*8feb0f0bSmrg char *fname = NULL;
403*8feb0f0bSmrg if (token->type == CPP_STRING || token->type == CPP_HEADER_NAME)
404*8feb0f0bSmrg {
405*8feb0f0bSmrg fname = XNEWVEC (char, token->val.str.len - 1);
406*8feb0f0bSmrg memcpy (fname, token->val.str.text + 1, token->val.str.len - 2);
407*8feb0f0bSmrg fname[token->val.str.len - 2] = '\0';
408*8feb0f0bSmrg }
409*8feb0f0bSmrg else if (token->type == CPP_LESS)
410*8feb0f0bSmrg fname = _cpp_bracket_include (pfile);
411*8feb0f0bSmrg else
412*8feb0f0bSmrg cpp_error (pfile, CPP_DL_ERROR,
413*8feb0f0bSmrg "operator \"%s\" requires a header-name", NODE_NAME (op));
414*8feb0f0bSmrg
415*8feb0f0bSmrg if (fname)
416*8feb0f0bSmrg {
417*8feb0f0bSmrg /* Do not do the lookup if we're skipping, that's unnecessary
418*8feb0f0bSmrg IO. */
419*8feb0f0bSmrg if (!pfile->state.skip_eval
420*8feb0f0bSmrg && _cpp_has_header (pfile, fname, bracket,
421*8feb0f0bSmrg has_next ? IT_INCLUDE_NEXT : IT_INCLUDE))
422*8feb0f0bSmrg result = 1;
423*8feb0f0bSmrg
424*8feb0f0bSmrg XDELETEVEC (fname);
425*8feb0f0bSmrg }
426*8feb0f0bSmrg
427*8feb0f0bSmrg if (paren
428*8feb0f0bSmrg && cpp_get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
429*8feb0f0bSmrg cpp_error (pfile, CPP_DL_ERROR,
430*8feb0f0bSmrg "missing ')' after \"%s\" operand", NODE_NAME (op));
431*8feb0f0bSmrg
432*8feb0f0bSmrg return result;
433*8feb0f0bSmrg }
434*8feb0f0bSmrg
43536ac495dSmrg /* Emits a warning if NODE is a macro defined in the main file that
43636ac495dSmrg has not been used. */
43736ac495dSmrg int
_cpp_warn_if_unused_macro(cpp_reader * pfile,cpp_hashnode * node,void * v ATTRIBUTE_UNUSED)43836ac495dSmrg _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
43936ac495dSmrg void *v ATTRIBUTE_UNUSED)
44036ac495dSmrg {
441c0a68be4Smrg if (cpp_user_macro_p (node))
44236ac495dSmrg {
44336ac495dSmrg cpp_macro *macro = node->value.macro;
44436ac495dSmrg
44536ac495dSmrg if (!macro->used
44636ac495dSmrg && MAIN_FILE_P (linemap_check_ordinary
44736ac495dSmrg (linemap_lookup (pfile->line_table,
44836ac495dSmrg macro->line))))
44936ac495dSmrg cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0,
45036ac495dSmrg "macro \"%s\" is not used", NODE_NAME (node));
45136ac495dSmrg }
45236ac495dSmrg
45336ac495dSmrg return 1;
45436ac495dSmrg }
45536ac495dSmrg
45636ac495dSmrg /* Allocates and returns a CPP_STRING token, containing TEXT of length
45736ac495dSmrg LEN, after null-terminating it. TEXT must be in permanent storage. */
45836ac495dSmrg static const cpp_token *
new_string_token(cpp_reader * pfile,unsigned char * text,unsigned int len)45936ac495dSmrg new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len)
46036ac495dSmrg {
46136ac495dSmrg cpp_token *token = _cpp_temp_token (pfile);
46236ac495dSmrg
46336ac495dSmrg text[len] = '\0';
46436ac495dSmrg token->type = CPP_STRING;
46536ac495dSmrg token->val.str.len = len;
46636ac495dSmrg token->val.str.text = text;
46736ac495dSmrg token->flags = 0;
46836ac495dSmrg return token;
46936ac495dSmrg }
47036ac495dSmrg
47136ac495dSmrg static const char * const monthnames[] =
47236ac495dSmrg {
47336ac495dSmrg "Jan", "Feb", "Mar", "Apr", "May", "Jun",
47436ac495dSmrg "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
47536ac495dSmrg };
47636ac495dSmrg
47736ac495dSmrg static size_t remap_pairs;
47836ac495dSmrg static char **remap_src;
47936ac495dSmrg static char **remap_dst;
48036ac495dSmrg
48136ac495dSmrg void
add_cpp_remap_path(const char * arg)48236ac495dSmrg add_cpp_remap_path (const char *arg)
48336ac495dSmrg {
48436ac495dSmrg const char *arg_dst;
48536ac495dSmrg size_t len;
48636ac495dSmrg
48736ac495dSmrg arg_dst = strchr(arg, ':');
48836ac495dSmrg if (arg_dst == NULL) {
48936ac495dSmrg fprintf(stderr, "Invalid argument for -iremap");
49036ac495dSmrg exit(1);
49136ac495dSmrg }
49236ac495dSmrg len = arg_dst - arg;
49336ac495dSmrg ++arg_dst;
49436ac495dSmrg
49536ac495dSmrg remap_src = (char **) xrealloc(remap_src, sizeof(char *) * (remap_pairs + 1));
49636ac495dSmrg remap_dst = (char **) xrealloc(remap_dst, sizeof(char *) * (remap_pairs + 1));
49736ac495dSmrg
49836ac495dSmrg remap_src[remap_pairs] = (char *) xmalloc(len + 1);
49936ac495dSmrg memcpy(remap_src[remap_pairs], arg, len);
50036ac495dSmrg remap_src[remap_pairs][len] = '\0';
50136ac495dSmrg remap_dst[remap_pairs] = xstrdup(arg_dst);
50236ac495dSmrg ++remap_pairs;
50336ac495dSmrg }
50436ac495dSmrg
505407a19b7Schristos static const char *
cpp_remap_file(const char * arg,char ** tmp_name)506407a19b7Schristos cpp_remap_file (const char *arg, char **tmp_name)
507407a19b7Schristos {
508407a19b7Schristos char *result;
509407a19b7Schristos size_t i, len;
510407a19b7Schristos
511407a19b7Schristos for (i = 0; i < remap_pairs; ++i) {
512407a19b7Schristos len = strlen (remap_src[i]);
513407a19b7Schristos if (strncmp (remap_src[i], arg, len))
514407a19b7Schristos continue;
515407a19b7Schristos if (arg[len] == '\0')
516407a19b7Schristos return remap_dst[i];
517407a19b7Schristos if (arg[len] != '/')
518407a19b7Schristos continue;
519407a19b7Schristos arg += len;
520407a19b7Schristos len = strlen (remap_dst[i]);
521407a19b7Schristos result = (char *) xmalloc (len + strlen (arg) + 1);
522407a19b7Schristos memcpy(result, remap_dst[i], len);
523407a19b7Schristos strcpy(result + len, arg);
524407a19b7Schristos *tmp_name = result;
525407a19b7Schristos
526407a19b7Schristos return result;
527407a19b7Schristos }
528407a19b7Schristos
529407a19b7Schristos return arg;
530407a19b7Schristos }
531407a19b7Schristos
53236ac495dSmrg /* Helper function for builtin_macro. Returns the text generated by
53336ac495dSmrg a builtin macro. */
53436ac495dSmrg const uchar *
_cpp_builtin_macro_text(cpp_reader * pfile,cpp_hashnode * node,location_t loc)53536ac495dSmrg _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node,
536c0a68be4Smrg location_t loc)
53736ac495dSmrg {
53836ac495dSmrg const uchar *result = NULL;
53936ac495dSmrg linenum_type number = 1;
54036ac495dSmrg
54136ac495dSmrg switch (node->value.builtin)
54236ac495dSmrg {
54336ac495dSmrg default:
54436ac495dSmrg cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
54536ac495dSmrg NODE_NAME (node));
54636ac495dSmrg break;
54736ac495dSmrg
54836ac495dSmrg case BT_TIMESTAMP:
54936ac495dSmrg {
55036ac495dSmrg if (CPP_OPTION (pfile, warn_date_time))
55136ac495dSmrg cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent "
55236ac495dSmrg "reproducible builds", NODE_NAME (node));
55336ac495dSmrg
55436ac495dSmrg cpp_buffer *pbuffer = cpp_get_buffer (pfile);
55536ac495dSmrg if (pbuffer->timestamp == NULL)
55636ac495dSmrg {
55736ac495dSmrg /* Initialize timestamp value of the assotiated file. */
55836ac495dSmrg struct _cpp_file *file = cpp_get_file (pbuffer);
55936ac495dSmrg if (file)
56036ac495dSmrg {
56136ac495dSmrg /* Generate __TIMESTAMP__ string, that represents
56236ac495dSmrg the date and time of the last modification
56336ac495dSmrg of the current source file. The string constant
56436ac495dSmrg looks like "Sun Sep 16 01:03:52 1973". */
56536ac495dSmrg struct tm *tb = NULL;
56636ac495dSmrg struct stat *st = _cpp_get_file_stat (file);
56736ac495dSmrg if (st)
56836ac495dSmrg tb = localtime (&st->st_mtime);
56936ac495dSmrg if (tb)
57036ac495dSmrg {
57136ac495dSmrg char *str = asctime (tb);
57236ac495dSmrg size_t len = strlen (str);
57336ac495dSmrg unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2);
57436ac495dSmrg buf[0] = '"';
57536ac495dSmrg strcpy ((char *) buf + 1, str);
57636ac495dSmrg buf[len] = '"';
57736ac495dSmrg pbuffer->timestamp = buf;
57836ac495dSmrg }
57936ac495dSmrg else
58036ac495dSmrg {
58136ac495dSmrg cpp_errno (pfile, CPP_DL_WARNING,
58236ac495dSmrg "could not determine file timestamp");
58336ac495dSmrg pbuffer->timestamp = UC"\"??? ??? ?? ??:??:?? ????\"";
58436ac495dSmrg }
58536ac495dSmrg }
58636ac495dSmrg }
58736ac495dSmrg result = pbuffer->timestamp;
58836ac495dSmrg }
58936ac495dSmrg break;
59036ac495dSmrg case BT_FILE:
59136ac495dSmrg case BT_BASE_FILE:
59236ac495dSmrg {
59336ac495dSmrg unsigned int len;
59436ac495dSmrg const char *name;
595407a19b7Schristos char *tmp_name;
59636ac495dSmrg uchar *buf;
59736ac495dSmrg
59836ac495dSmrg if (node->value.builtin == BT_FILE)
59936ac495dSmrg name = linemap_get_expansion_filename (pfile->line_table,
60036ac495dSmrg pfile->line_table->highest_line);
60136ac495dSmrg else
60236ac495dSmrg {
60336ac495dSmrg name = _cpp_get_file_name (pfile->main_file);
60436ac495dSmrg if (!name)
60536ac495dSmrg abort ();
60636ac495dSmrg }
607a2dc1f3fSmrg if (pfile->cb.remap_filename)
608a2dc1f3fSmrg name = pfile->cb.remap_filename (name);
609407a19b7Schristos tmp_name = NULL;
610407a19b7Schristos name = cpp_remap_file (name, &tmp_name);
61136ac495dSmrg len = strlen (name);
61236ac495dSmrg buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
61336ac495dSmrg result = buf;
61436ac495dSmrg *buf = '"';
61536ac495dSmrg buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
616407a19b7Schristos free (tmp_name);
61736ac495dSmrg *buf++ = '"';
61836ac495dSmrg *buf = '\0';
61936ac495dSmrg }
62036ac495dSmrg break;
62136ac495dSmrg
62236ac495dSmrg case BT_INCLUDE_LEVEL:
62336ac495dSmrg /* The line map depth counts the primary source as level 1, but
62436ac495dSmrg historically __INCLUDE_DEPTH__ has called the primary source
62536ac495dSmrg level 0. */
62636ac495dSmrg number = pfile->line_table->depth - 1;
62736ac495dSmrg break;
62836ac495dSmrg
62936ac495dSmrg case BT_SPECLINE:
63036ac495dSmrg /* If __LINE__ is embedded in a macro, it must expand to the
63136ac495dSmrg line of the macro's invocation, not its definition.
63236ac495dSmrg Otherwise things like assert() will not work properly.
63336ac495dSmrg See WG14 N1911, WG21 N4220 sec 6.5, and PR 61861. */
63436ac495dSmrg if (CPP_OPTION (pfile, traditional))
63536ac495dSmrg loc = pfile->line_table->highest_line;
63636ac495dSmrg else
63736ac495dSmrg loc = linemap_resolve_location (pfile->line_table, loc,
63836ac495dSmrg LRK_MACRO_EXPANSION_POINT, NULL);
63936ac495dSmrg number = linemap_get_expansion_line (pfile->line_table, loc);
64036ac495dSmrg break;
64136ac495dSmrg
64236ac495dSmrg /* __STDC__ has the value 1 under normal circumstances.
64336ac495dSmrg However, if (a) we are in a system header, (b) the option
64436ac495dSmrg stdc_0_in_system_headers is true (set by target config), and
64536ac495dSmrg (c) we are not in strictly conforming mode, then it has the
64636ac495dSmrg value 0. (b) and (c) are already checked in cpp_init_builtins. */
64736ac495dSmrg case BT_STDC:
64836ac495dSmrg if (cpp_in_system_header (pfile))
64936ac495dSmrg number = 0;
65036ac495dSmrg else
65136ac495dSmrg number = 1;
65236ac495dSmrg break;
65336ac495dSmrg
65436ac495dSmrg case BT_DATE:
65536ac495dSmrg case BT_TIME:
65636ac495dSmrg if (CPP_OPTION (pfile, warn_date_time))
65736ac495dSmrg cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent "
65836ac495dSmrg "reproducible builds", NODE_NAME (node));
65936ac495dSmrg if (pfile->date == NULL)
66036ac495dSmrg {
66136ac495dSmrg /* Allocate __DATE__ and __TIME__ strings from permanent
66236ac495dSmrg storage. We only do this once, and don't generate them
66336ac495dSmrg at init time, because time() and localtime() are very
66436ac495dSmrg slow on some systems. */
66536ac495dSmrg time_t tt;
66636ac495dSmrg struct tm *tb = NULL;
66736ac495dSmrg
66836ac495dSmrg /* Set a reproducible timestamp for __DATE__ and __TIME__ macro
66936ac495dSmrg if SOURCE_DATE_EPOCH is defined. */
67036ac495dSmrg if (pfile->source_date_epoch == (time_t) -2
67136ac495dSmrg && pfile->cb.get_source_date_epoch != NULL)
67236ac495dSmrg pfile->source_date_epoch = pfile->cb.get_source_date_epoch (pfile);
67336ac495dSmrg
67436ac495dSmrg if (pfile->source_date_epoch >= (time_t) 0)
67536ac495dSmrg tb = gmtime (&pfile->source_date_epoch);
67636ac495dSmrg else
67736ac495dSmrg {
67836ac495dSmrg /* (time_t) -1 is a legitimate value for "number of seconds
67936ac495dSmrg since the Epoch", so we have to do a little dance to
68036ac495dSmrg distinguish that from a genuine error. */
68136ac495dSmrg errno = 0;
68236ac495dSmrg tt = time (NULL);
68336ac495dSmrg if (tt != (time_t)-1 || errno == 0)
68436ac495dSmrg tb = localtime (&tt);
68536ac495dSmrg }
68636ac495dSmrg
68736ac495dSmrg if (tb)
68836ac495dSmrg {
68936ac495dSmrg pfile->date = _cpp_unaligned_alloc (pfile,
69036ac495dSmrg sizeof ("\"Oct 11 1347\""));
69136ac495dSmrg sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
69236ac495dSmrg monthnames[tb->tm_mon], tb->tm_mday,
69336ac495dSmrg tb->tm_year + 1900);
69436ac495dSmrg
69536ac495dSmrg pfile->time = _cpp_unaligned_alloc (pfile,
69636ac495dSmrg sizeof ("\"12:34:56\""));
69736ac495dSmrg sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
69836ac495dSmrg tb->tm_hour, tb->tm_min, tb->tm_sec);
69936ac495dSmrg }
70036ac495dSmrg else
70136ac495dSmrg {
70236ac495dSmrg cpp_errno (pfile, CPP_DL_WARNING,
70336ac495dSmrg "could not determine date and time");
70436ac495dSmrg
70536ac495dSmrg pfile->date = UC"\"??? ?? ????\"";
70636ac495dSmrg pfile->time = UC"\"??:??:??\"";
70736ac495dSmrg }
70836ac495dSmrg }
70936ac495dSmrg
71036ac495dSmrg if (node->value.builtin == BT_DATE)
71136ac495dSmrg result = pfile->date;
71236ac495dSmrg else
71336ac495dSmrg result = pfile->time;
71436ac495dSmrg break;
71536ac495dSmrg
71636ac495dSmrg case BT_COUNTER:
71736ac495dSmrg if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive)
71836ac495dSmrg cpp_error (pfile, CPP_DL_ERROR,
71936ac495dSmrg "__COUNTER__ expanded inside directive with -fdirectives-only");
72036ac495dSmrg number = pfile->counter++;
72136ac495dSmrg break;
72236ac495dSmrg
72336ac495dSmrg case BT_HAS_ATTRIBUTE:
72436ac495dSmrg number = pfile->cb.has_attribute (pfile);
72536ac495dSmrg break;
726*8feb0f0bSmrg
727*8feb0f0bSmrg case BT_HAS_BUILTIN:
728*8feb0f0bSmrg number = pfile->cb.has_builtin (pfile);
729*8feb0f0bSmrg break;
730*8feb0f0bSmrg
731*8feb0f0bSmrg case BT_HAS_INCLUDE:
732*8feb0f0bSmrg case BT_HAS_INCLUDE_NEXT:
733*8feb0f0bSmrg number = builtin_has_include (pfile, node,
734*8feb0f0bSmrg node->value.builtin == BT_HAS_INCLUDE_NEXT);
735*8feb0f0bSmrg break;
73636ac495dSmrg }
73736ac495dSmrg
73836ac495dSmrg if (result == NULL)
73936ac495dSmrg {
74036ac495dSmrg /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
74136ac495dSmrg result = _cpp_unaligned_alloc (pfile, 21);
74236ac495dSmrg sprintf ((char *) result, "%u", number);
74336ac495dSmrg }
74436ac495dSmrg
74536ac495dSmrg return result;
74636ac495dSmrg }
74736ac495dSmrg
74836ac495dSmrg /* Convert builtin macros like __FILE__ to a token and push it on the
74936ac495dSmrg context stack. Also handles _Pragma, for which a new token may not
75036ac495dSmrg be created. Returns 1 if it generates a new token context, 0 to
75136ac495dSmrg return the token to the caller. LOC is the location of the expansion
75236ac495dSmrg point of the macro. */
75336ac495dSmrg static int
builtin_macro(cpp_reader * pfile,cpp_hashnode * node,location_t loc,location_t expand_loc)75436ac495dSmrg builtin_macro (cpp_reader *pfile, cpp_hashnode *node,
755c0a68be4Smrg location_t loc, location_t expand_loc)
75636ac495dSmrg {
75736ac495dSmrg const uchar *buf;
75836ac495dSmrg size_t len;
75936ac495dSmrg char *nbuf;
76036ac495dSmrg
76136ac495dSmrg if (node->value.builtin == BT_PRAGMA)
76236ac495dSmrg {
76336ac495dSmrg /* Don't interpret _Pragma within directives. The standard is
76436ac495dSmrg not clear on this, but to me this makes most sense. */
76536ac495dSmrg if (pfile->state.in_directive)
76636ac495dSmrg return 0;
76736ac495dSmrg
76836ac495dSmrg return _cpp_do__Pragma (pfile, loc);
76936ac495dSmrg }
77036ac495dSmrg
77136ac495dSmrg buf = _cpp_builtin_macro_text (pfile, node, expand_loc);
77236ac495dSmrg len = ustrlen (buf);
77336ac495dSmrg nbuf = (char *) alloca (len + 1);
77436ac495dSmrg memcpy (nbuf, buf, len);
77536ac495dSmrg nbuf[len]='\n';
77636ac495dSmrg
77736ac495dSmrg cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
77836ac495dSmrg _cpp_clean_line (pfile);
77936ac495dSmrg
78036ac495dSmrg /* Set pfile->cur_token as required by _cpp_lex_direct. */
78136ac495dSmrg pfile->cur_token = _cpp_temp_token (pfile);
78236ac495dSmrg cpp_token *token = _cpp_lex_direct (pfile);
78336ac495dSmrg /* We should point to the expansion point of the builtin macro. */
78436ac495dSmrg token->src_loc = loc;
78536ac495dSmrg if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
78636ac495dSmrg {
78736ac495dSmrg /* We are tracking tokens resulting from macro expansion.
78836ac495dSmrg Create a macro line map and generate a virtual location for
78936ac495dSmrg the token resulting from the expansion of the built-in
79036ac495dSmrg macro. */
791c0a68be4Smrg location_t *virt_locs = NULL;
79236ac495dSmrg _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
79336ac495dSmrg const line_map_macro * map =
79436ac495dSmrg linemap_enter_macro (pfile->line_table, node, loc, 1);
79536ac495dSmrg tokens_buff_add_token (token_buf, virt_locs, token,
79636ac495dSmrg pfile->line_table->builtin_location,
79736ac495dSmrg pfile->line_table->builtin_location,
79836ac495dSmrg map, /*macro_token_index=*/0);
79936ac495dSmrg push_extended_tokens_context (pfile, node, token_buf, virt_locs,
80036ac495dSmrg (const cpp_token **)token_buf->base,
80136ac495dSmrg 1);
80236ac495dSmrg }
80336ac495dSmrg else
80436ac495dSmrg _cpp_push_token_context (pfile, NULL, token, 1);
80536ac495dSmrg if (pfile->buffer->cur != pfile->buffer->rlimit)
80636ac495dSmrg cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
80736ac495dSmrg NODE_NAME (node));
80836ac495dSmrg _cpp_pop_buffer (pfile);
80936ac495dSmrg
81036ac495dSmrg return 1;
81136ac495dSmrg }
81236ac495dSmrg
81336ac495dSmrg /* Copies SRC, of length LEN, to DEST, adding backslashes before all
81436ac495dSmrg backslashes and double quotes. DEST must be of sufficient size.
81536ac495dSmrg Returns a pointer to the end of the string. */
81636ac495dSmrg uchar *
cpp_quote_string(uchar * dest,const uchar * src,unsigned int len)81736ac495dSmrg cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
81836ac495dSmrg {
81936ac495dSmrg while (len--)
82036ac495dSmrg {
82136ac495dSmrg uchar c = *src++;
82236ac495dSmrg
823a2dc1f3fSmrg switch (c)
82436ac495dSmrg {
825a2dc1f3fSmrg case '\n':
826a2dc1f3fSmrg /* Naked LF can appear in raw string literals */
827a2dc1f3fSmrg c = 'n';
828a2dc1f3fSmrg /* FALLTHROUGH */
829a2dc1f3fSmrg
830a2dc1f3fSmrg case '\\':
831a2dc1f3fSmrg case '"':
83236ac495dSmrg *dest++ = '\\';
833a2dc1f3fSmrg /* FALLTHROUGH */
834a2dc1f3fSmrg
835a2dc1f3fSmrg default:
83636ac495dSmrg *dest++ = c;
83736ac495dSmrg }
83836ac495dSmrg }
83936ac495dSmrg
84036ac495dSmrg return dest;
84136ac495dSmrg }
84236ac495dSmrg
84336ac495dSmrg /* Convert a token sequence ARG to a single string token according to
84436ac495dSmrg the rules of the ISO C #-operator. */
84536ac495dSmrg static const cpp_token *
stringify_arg(cpp_reader * pfile,macro_arg * arg)84636ac495dSmrg stringify_arg (cpp_reader *pfile, macro_arg *arg)
84736ac495dSmrg {
84836ac495dSmrg unsigned char *dest;
84936ac495dSmrg unsigned int i, escape_it, backslash_count = 0;
85036ac495dSmrg const cpp_token *source = NULL;
85136ac495dSmrg size_t len;
85236ac495dSmrg
85336ac495dSmrg if (BUFF_ROOM (pfile->u_buff) < 3)
85436ac495dSmrg _cpp_extend_buff (pfile, &pfile->u_buff, 3);
85536ac495dSmrg dest = BUFF_FRONT (pfile->u_buff);
85636ac495dSmrg *dest++ = '"';
85736ac495dSmrg
85836ac495dSmrg /* Loop, reading in the argument's tokens. */
85936ac495dSmrg for (i = 0; i < arg->count; i++)
86036ac495dSmrg {
86136ac495dSmrg const cpp_token *token = arg->first[i];
86236ac495dSmrg
86336ac495dSmrg if (token->type == CPP_PADDING)
86436ac495dSmrg {
86536ac495dSmrg if (source == NULL
86636ac495dSmrg || (!(source->flags & PREV_WHITE)
86736ac495dSmrg && token->val.source == NULL))
86836ac495dSmrg source = token->val.source;
86936ac495dSmrg continue;
87036ac495dSmrg }
87136ac495dSmrg
87236ac495dSmrg escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR
87336ac495dSmrg || token->type == CPP_WSTRING || token->type == CPP_WCHAR
87436ac495dSmrg || token->type == CPP_STRING32 || token->type == CPP_CHAR32
87536ac495dSmrg || token->type == CPP_STRING16 || token->type == CPP_CHAR16
87636ac495dSmrg || token->type == CPP_UTF8STRING || token->type == CPP_UTF8CHAR
87736ac495dSmrg || cpp_userdef_string_p (token->type)
87836ac495dSmrg || cpp_userdef_char_p (token->type));
87936ac495dSmrg
88036ac495dSmrg /* Room for each char being written in octal, initial space and
88136ac495dSmrg final quote and NUL. */
88236ac495dSmrg len = cpp_token_len (token);
88336ac495dSmrg if (escape_it)
88436ac495dSmrg len *= 4;
88536ac495dSmrg len += 3;
88636ac495dSmrg
88736ac495dSmrg if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
88836ac495dSmrg {
88936ac495dSmrg size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
89036ac495dSmrg _cpp_extend_buff (pfile, &pfile->u_buff, len);
89136ac495dSmrg dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
89236ac495dSmrg }
89336ac495dSmrg
89436ac495dSmrg /* Leading white space? */
89536ac495dSmrg if (dest - 1 != BUFF_FRONT (pfile->u_buff))
89636ac495dSmrg {
89736ac495dSmrg if (source == NULL)
89836ac495dSmrg source = token;
89936ac495dSmrg if (source->flags & PREV_WHITE)
90036ac495dSmrg *dest++ = ' ';
90136ac495dSmrg }
90236ac495dSmrg source = NULL;
90336ac495dSmrg
90436ac495dSmrg if (escape_it)
90536ac495dSmrg {
90636ac495dSmrg _cpp_buff *buff = _cpp_get_buff (pfile, len);
90736ac495dSmrg unsigned char *buf = BUFF_FRONT (buff);
90836ac495dSmrg len = cpp_spell_token (pfile, token, buf, true) - buf;
90936ac495dSmrg dest = cpp_quote_string (dest, buf, len);
91036ac495dSmrg _cpp_release_buff (pfile, buff);
91136ac495dSmrg }
91236ac495dSmrg else
91336ac495dSmrg dest = cpp_spell_token (pfile, token, dest, true);
91436ac495dSmrg
91536ac495dSmrg if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
91636ac495dSmrg backslash_count++;
91736ac495dSmrg else
91836ac495dSmrg backslash_count = 0;
91936ac495dSmrg }
92036ac495dSmrg
92136ac495dSmrg /* Ignore the final \ of invalid string literals. */
92236ac495dSmrg if (backslash_count & 1)
92336ac495dSmrg {
92436ac495dSmrg cpp_error (pfile, CPP_DL_WARNING,
92536ac495dSmrg "invalid string literal, ignoring final '\\'");
92636ac495dSmrg dest--;
92736ac495dSmrg }
92836ac495dSmrg
92936ac495dSmrg /* Commit the memory, including NUL, and return the token. */
93036ac495dSmrg *dest++ = '"';
93136ac495dSmrg len = dest - BUFF_FRONT (pfile->u_buff);
93236ac495dSmrg BUFF_FRONT (pfile->u_buff) = dest + 1;
93336ac495dSmrg return new_string_token (pfile, dest - len, len);
93436ac495dSmrg }
93536ac495dSmrg
93636ac495dSmrg /* Try to paste two tokens. On success, return nonzero. In any
93736ac495dSmrg case, PLHS is updated to point to the pasted token, which is
93836ac495dSmrg guaranteed to not have the PASTE_LEFT flag set. LOCATION is
93936ac495dSmrg the virtual location used for error reporting. */
94036ac495dSmrg static bool
paste_tokens(cpp_reader * pfile,location_t location,const cpp_token ** plhs,const cpp_token * rhs)941c0a68be4Smrg paste_tokens (cpp_reader *pfile, location_t location,
94236ac495dSmrg const cpp_token **plhs, const cpp_token *rhs)
94336ac495dSmrg {
94436ac495dSmrg unsigned char *buf, *end, *lhsend;
94536ac495dSmrg cpp_token *lhs;
94636ac495dSmrg unsigned int len;
94736ac495dSmrg
94836ac495dSmrg len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1;
94936ac495dSmrg buf = (unsigned char *) alloca (len);
95036ac495dSmrg end = lhsend = cpp_spell_token (pfile, *plhs, buf, true);
95136ac495dSmrg
95236ac495dSmrg /* Avoid comment headers, since they are still processed in stage 3.
95336ac495dSmrg It is simpler to insert a space here, rather than modifying the
95436ac495dSmrg lexer to ignore comments in some circumstances. Simply returning
95536ac495dSmrg false doesn't work, since we want to clear the PASTE_LEFT flag. */
95636ac495dSmrg if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ)
95736ac495dSmrg *end++ = ' ';
95836ac495dSmrg /* In one obscure case we might see padding here. */
95936ac495dSmrg if (rhs->type != CPP_PADDING)
96036ac495dSmrg end = cpp_spell_token (pfile, rhs, end, true);
96136ac495dSmrg *end = '\n';
96236ac495dSmrg
96336ac495dSmrg cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
96436ac495dSmrg _cpp_clean_line (pfile);
96536ac495dSmrg
96636ac495dSmrg /* Set pfile->cur_token as required by _cpp_lex_direct. */
96736ac495dSmrg pfile->cur_token = _cpp_temp_token (pfile);
96836ac495dSmrg lhs = _cpp_lex_direct (pfile);
96936ac495dSmrg if (pfile->buffer->cur != pfile->buffer->rlimit)
97036ac495dSmrg {
971c0a68be4Smrg location_t saved_loc = lhs->src_loc;
97236ac495dSmrg
97336ac495dSmrg _cpp_pop_buffer (pfile);
97436ac495dSmrg _cpp_backup_tokens (pfile, 1);
97536ac495dSmrg *lhsend = '\0';
97636ac495dSmrg
97736ac495dSmrg /* We have to remove the PASTE_LEFT flag from the old lhs, but
97836ac495dSmrg we want to keep the new location. */
97936ac495dSmrg *lhs = **plhs;
98036ac495dSmrg *plhs = lhs;
98136ac495dSmrg lhs->src_loc = saved_loc;
98236ac495dSmrg lhs->flags &= ~PASTE_LEFT;
98336ac495dSmrg
98436ac495dSmrg /* Mandatory error for all apart from assembler. */
98536ac495dSmrg if (CPP_OPTION (pfile, lang) != CLK_ASM)
98636ac495dSmrg cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
98736ac495dSmrg "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
98836ac495dSmrg buf, cpp_token_as_text (pfile, rhs));
98936ac495dSmrg return false;
99036ac495dSmrg }
99136ac495dSmrg
99236ac495dSmrg *plhs = lhs;
99336ac495dSmrg _cpp_pop_buffer (pfile);
99436ac495dSmrg return true;
99536ac495dSmrg }
99636ac495dSmrg
99736ac495dSmrg /* Handles an arbitrarily long sequence of ## operators, with initial
99836ac495dSmrg operand LHS. This implementation is left-associative,
99936ac495dSmrg non-recursive, and finishes a paste before handling succeeding
100036ac495dSmrg ones. If a paste fails, we back up to the RHS of the failing ##
100136ac495dSmrg operator before pushing the context containing the result of prior
100236ac495dSmrg successful pastes, with the effect that the RHS appears in the
100336ac495dSmrg output stream after the pasted LHS normally. */
100436ac495dSmrg static void
paste_all_tokens(cpp_reader * pfile,const cpp_token * lhs)100536ac495dSmrg paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
100636ac495dSmrg {
100736ac495dSmrg const cpp_token *rhs = NULL;
100836ac495dSmrg cpp_context *context = pfile->context;
1009c0a68be4Smrg location_t virt_loc = 0;
101036ac495dSmrg
101136ac495dSmrg /* We are expanding a macro and we must have been called on a token
101236ac495dSmrg that appears at the left hand side of a ## operator. */
101336ac495dSmrg if (macro_of_context (pfile->context) == NULL
101436ac495dSmrg || (!(lhs->flags & PASTE_LEFT)))
101536ac495dSmrg abort ();
101636ac495dSmrg
101736ac495dSmrg if (context->tokens_kind == TOKENS_KIND_EXTENDED)
101836ac495dSmrg /* The caller must have called consume_next_token_from_context
101936ac495dSmrg right before calling us. That has incremented the pointer to
102036ac495dSmrg the current virtual location. So it now points to the location
102136ac495dSmrg of the token that comes right after *LHS. We want the
102236ac495dSmrg resulting pasted token to have the location of the current
102336ac495dSmrg *LHS, though. */
102436ac495dSmrg virt_loc = context->c.mc->cur_virt_loc[-1];
102536ac495dSmrg else
102636ac495dSmrg /* We are not tracking macro expansion. So the best virtual
102736ac495dSmrg location we can get here is the expansion point of the macro we
102836ac495dSmrg are currently expanding. */
102936ac495dSmrg virt_loc = pfile->invocation_location;
103036ac495dSmrg
103136ac495dSmrg do
103236ac495dSmrg {
103336ac495dSmrg /* Take the token directly from the current context. We can do
103436ac495dSmrg this, because we are in the replacement list of either an
103536ac495dSmrg object-like macro, or a function-like macro with arguments
103636ac495dSmrg inserted. In either case, the constraints to #define
103736ac495dSmrg guarantee we have at least one more token. */
103836ac495dSmrg if (context->tokens_kind == TOKENS_KIND_DIRECT)
103936ac495dSmrg rhs = FIRST (context).token++;
104036ac495dSmrg else if (context->tokens_kind == TOKENS_KIND_INDIRECT)
104136ac495dSmrg rhs = *FIRST (context).ptoken++;
104236ac495dSmrg else if (context->tokens_kind == TOKENS_KIND_EXTENDED)
104336ac495dSmrg {
104436ac495dSmrg /* So we are in presence of an extended token context, which
104536ac495dSmrg means that each token in this context has a virtual
104636ac495dSmrg location attached to it. So let's not forget to update
104736ac495dSmrg the pointer to the current virtual location of the
104836ac495dSmrg current token when we update the pointer to the current
104936ac495dSmrg token */
105036ac495dSmrg
105136ac495dSmrg rhs = *FIRST (context).ptoken++;
105236ac495dSmrg /* context->c.mc must be non-null, as if we were not in a
105336ac495dSmrg macro context, context->tokens_kind could not be equal to
105436ac495dSmrg TOKENS_KIND_EXTENDED. */
105536ac495dSmrg context->c.mc->cur_virt_loc++;
105636ac495dSmrg }
105736ac495dSmrg
105836ac495dSmrg if (rhs->type == CPP_PADDING)
105936ac495dSmrg {
106036ac495dSmrg if (rhs->flags & PASTE_LEFT)
106136ac495dSmrg abort ();
106236ac495dSmrg }
106336ac495dSmrg if (!paste_tokens (pfile, virt_loc, &lhs, rhs))
106436ac495dSmrg break;
106536ac495dSmrg }
106636ac495dSmrg while (rhs->flags & PASTE_LEFT);
106736ac495dSmrg
106836ac495dSmrg /* Put the resulting token in its own context. */
106936ac495dSmrg if (context->tokens_kind == TOKENS_KIND_EXTENDED)
107036ac495dSmrg {
1071c0a68be4Smrg location_t *virt_locs = NULL;
107236ac495dSmrg _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
107336ac495dSmrg tokens_buff_add_token (token_buf, virt_locs, lhs,
107436ac495dSmrg virt_loc, 0, NULL, 0);
107536ac495dSmrg push_extended_tokens_context (pfile, context->c.mc->macro_node,
107636ac495dSmrg token_buf, virt_locs,
107736ac495dSmrg (const cpp_token **)token_buf->base, 1);
107836ac495dSmrg }
107936ac495dSmrg else
108036ac495dSmrg _cpp_push_token_context (pfile, NULL, lhs, 1);
108136ac495dSmrg }
108236ac495dSmrg
108336ac495dSmrg /* Returns TRUE if the number of arguments ARGC supplied in an
108436ac495dSmrg invocation of the MACRO referenced by NODE is valid. An empty
108536ac495dSmrg invocation to a macro with no parameters should pass ARGC as zero.
108636ac495dSmrg
108736ac495dSmrg Note that MACRO cannot necessarily be deduced from NODE, in case
108836ac495dSmrg NODE was redefined whilst collecting arguments. */
108936ac495dSmrg bool
_cpp_arguments_ok(cpp_reader * pfile,cpp_macro * macro,const cpp_hashnode * node,unsigned int argc)109036ac495dSmrg _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
109136ac495dSmrg {
109236ac495dSmrg if (argc == macro->paramc)
109336ac495dSmrg return true;
109436ac495dSmrg
109536ac495dSmrg if (argc < macro->paramc)
109636ac495dSmrg {
1097a2dc1f3fSmrg /* In C++2a (here the va_opt flag is used), and also as a GNU
1098a2dc1f3fSmrg extension, variadic arguments are allowed to not appear in
109936ac495dSmrg the invocation at all.
110036ac495dSmrg e.g. #define debug(format, args...) something
110136ac495dSmrg debug("string");
110236ac495dSmrg
110336ac495dSmrg This is exactly the same as if an empty variadic list had been
110436ac495dSmrg supplied - debug("string", ). */
110536ac495dSmrg
110636ac495dSmrg if (argc + 1 == macro->paramc && macro->variadic)
110736ac495dSmrg {
1108a2dc1f3fSmrg if (CPP_PEDANTIC (pfile) && ! macro->syshdr
1109a2dc1f3fSmrg && ! CPP_OPTION (pfile, va_opt))
111036ac495dSmrg {
111136ac495dSmrg if (CPP_OPTION (pfile, cplusplus))
111236ac495dSmrg cpp_error (pfile, CPP_DL_PEDWARN,
111336ac495dSmrg "ISO C++11 requires at least one argument "
111436ac495dSmrg "for the \"...\" in a variadic macro");
111536ac495dSmrg else
111636ac495dSmrg cpp_error (pfile, CPP_DL_PEDWARN,
111736ac495dSmrg "ISO C99 requires at least one argument "
111836ac495dSmrg "for the \"...\" in a variadic macro");
111936ac495dSmrg }
112036ac495dSmrg return true;
112136ac495dSmrg }
112236ac495dSmrg
112336ac495dSmrg cpp_error (pfile, CPP_DL_ERROR,
112436ac495dSmrg "macro \"%s\" requires %u arguments, but only %u given",
112536ac495dSmrg NODE_NAME (node), macro->paramc, argc);
112636ac495dSmrg }
112736ac495dSmrg else
112836ac495dSmrg cpp_error (pfile, CPP_DL_ERROR,
112936ac495dSmrg "macro \"%s\" passed %u arguments, but takes just %u",
113036ac495dSmrg NODE_NAME (node), argc, macro->paramc);
113136ac495dSmrg
1132c0a68be4Smrg if (macro->line > RESERVED_LOCATION_COUNT)
1133c0a68be4Smrg cpp_error_at (pfile, CPP_DL_NOTE, macro->line, "macro \"%s\" defined here",
1134c0a68be4Smrg NODE_NAME (node));
1135c0a68be4Smrg
113636ac495dSmrg return false;
113736ac495dSmrg }
113836ac495dSmrg
113936ac495dSmrg /* Reads and returns the arguments to a function-like macro
114036ac495dSmrg invocation. Assumes the opening parenthesis has been processed.
114136ac495dSmrg If there is an error, emits an appropriate diagnostic and returns
114236ac495dSmrg NULL. Each argument is terminated by a CPP_EOF token, for the
114336ac495dSmrg future benefit of expand_arg(). If there are any deferred
114436ac495dSmrg #pragma directives among macro arguments, store pointers to the
114536ac495dSmrg CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.
114636ac495dSmrg
114736ac495dSmrg What is returned is the buffer that contains the memory allocated
114836ac495dSmrg to hold the macro arguments. NODE is the name of the macro this
114936ac495dSmrg function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is
115036ac495dSmrg set to the actual number of macro arguments allocated in the
115136ac495dSmrg returned buffer. */
115236ac495dSmrg static _cpp_buff *
collect_args(cpp_reader * pfile,const cpp_hashnode * node,_cpp_buff ** pragma_buff,unsigned * num_args)115336ac495dSmrg collect_args (cpp_reader *pfile, const cpp_hashnode *node,
115436ac495dSmrg _cpp_buff **pragma_buff, unsigned *num_args)
115536ac495dSmrg {
115636ac495dSmrg _cpp_buff *buff, *base_buff;
115736ac495dSmrg cpp_macro *macro;
115836ac495dSmrg macro_arg *args, *arg;
115936ac495dSmrg const cpp_token *token;
116036ac495dSmrg unsigned int argc;
1161c0a68be4Smrg location_t virt_loc;
116236ac495dSmrg bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion);
116336ac495dSmrg unsigned num_args_alloced = 0;
116436ac495dSmrg
116536ac495dSmrg macro = node->value.macro;
116636ac495dSmrg if (macro->paramc)
116736ac495dSmrg argc = macro->paramc;
116836ac495dSmrg else
116936ac495dSmrg argc = 1;
117036ac495dSmrg
117136ac495dSmrg #define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50
117236ac495dSmrg #define ARG_TOKENS_EXTENT 1000
117336ac495dSmrg
117436ac495dSmrg buff = _cpp_get_buff (pfile, argc * (DEFAULT_NUM_TOKENS_PER_MACRO_ARG
117536ac495dSmrg * sizeof (cpp_token *)
117636ac495dSmrg + sizeof (macro_arg)));
117736ac495dSmrg base_buff = buff;
117836ac495dSmrg args = (macro_arg *) buff->base;
117936ac495dSmrg memset (args, 0, argc * sizeof (macro_arg));
118036ac495dSmrg buff->cur = (unsigned char *) &args[argc];
118136ac495dSmrg arg = args, argc = 0;
118236ac495dSmrg
118336ac495dSmrg /* Collect the tokens making up each argument. We don't yet know
118436ac495dSmrg how many arguments have been supplied, whether too many or too
118536ac495dSmrg few. Hence the slightly bizarre usage of "argc" and "arg". */
118636ac495dSmrg do
118736ac495dSmrg {
118836ac495dSmrg unsigned int paren_depth = 0;
118936ac495dSmrg unsigned int ntokens = 0;
119036ac495dSmrg unsigned virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
119136ac495dSmrg num_args_alloced++;
119236ac495dSmrg
119336ac495dSmrg argc++;
119436ac495dSmrg arg->first = (const cpp_token **) buff->cur;
119536ac495dSmrg if (track_macro_expansion_p)
119636ac495dSmrg {
119736ac495dSmrg virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
1198c0a68be4Smrg arg->virt_locs = XNEWVEC (location_t,
119936ac495dSmrg virt_locs_capacity);
120036ac495dSmrg }
120136ac495dSmrg
120236ac495dSmrg for (;;)
120336ac495dSmrg {
120436ac495dSmrg /* Require space for 2 new tokens (including a CPP_EOF). */
120536ac495dSmrg if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
120636ac495dSmrg {
120736ac495dSmrg buff = _cpp_append_extend_buff (pfile, buff,
120836ac495dSmrg ARG_TOKENS_EXTENT
120936ac495dSmrg * sizeof (cpp_token *));
121036ac495dSmrg arg->first = (const cpp_token **) buff->cur;
121136ac495dSmrg }
121236ac495dSmrg if (track_macro_expansion_p
121336ac495dSmrg && (ntokens + 2 > virt_locs_capacity))
121436ac495dSmrg {
121536ac495dSmrg virt_locs_capacity += ARG_TOKENS_EXTENT;
1216c0a68be4Smrg arg->virt_locs = XRESIZEVEC (location_t,
121736ac495dSmrg arg->virt_locs,
121836ac495dSmrg virt_locs_capacity);
121936ac495dSmrg }
122036ac495dSmrg
122136ac495dSmrg token = cpp_get_token_1 (pfile, &virt_loc);
122236ac495dSmrg
122336ac495dSmrg if (token->type == CPP_PADDING)
122436ac495dSmrg {
122536ac495dSmrg /* Drop leading padding. */
122636ac495dSmrg if (ntokens == 0)
122736ac495dSmrg continue;
122836ac495dSmrg }
122936ac495dSmrg else if (token->type == CPP_OPEN_PAREN)
123036ac495dSmrg paren_depth++;
123136ac495dSmrg else if (token->type == CPP_CLOSE_PAREN)
123236ac495dSmrg {
123336ac495dSmrg if (paren_depth-- == 0)
123436ac495dSmrg break;
123536ac495dSmrg }
123636ac495dSmrg else if (token->type == CPP_COMMA)
123736ac495dSmrg {
123836ac495dSmrg /* A comma does not terminate an argument within
123936ac495dSmrg parentheses or as part of a variable argument. */
124036ac495dSmrg if (paren_depth == 0
124136ac495dSmrg && ! (macro->variadic && argc == macro->paramc))
124236ac495dSmrg break;
124336ac495dSmrg }
124436ac495dSmrg else if (token->type == CPP_EOF
124536ac495dSmrg || (token->type == CPP_HASH && token->flags & BOL))
124636ac495dSmrg break;
124736ac495dSmrg else if (token->type == CPP_PRAGMA)
124836ac495dSmrg {
124936ac495dSmrg cpp_token *newtok = _cpp_temp_token (pfile);
125036ac495dSmrg
125136ac495dSmrg /* CPP_PRAGMA token lives in directive_result, which will
125236ac495dSmrg be overwritten on the next directive. */
125336ac495dSmrg *newtok = *token;
125436ac495dSmrg token = newtok;
125536ac495dSmrg do
125636ac495dSmrg {
125736ac495dSmrg if (*pragma_buff == NULL
125836ac495dSmrg || BUFF_ROOM (*pragma_buff) < sizeof (cpp_token *))
125936ac495dSmrg {
126036ac495dSmrg _cpp_buff *next;
126136ac495dSmrg if (*pragma_buff == NULL)
126236ac495dSmrg *pragma_buff
126336ac495dSmrg = _cpp_get_buff (pfile, 32 * sizeof (cpp_token *));
126436ac495dSmrg else
126536ac495dSmrg {
126636ac495dSmrg next = *pragma_buff;
126736ac495dSmrg *pragma_buff
126836ac495dSmrg = _cpp_get_buff (pfile,
126936ac495dSmrg (BUFF_FRONT (*pragma_buff)
127036ac495dSmrg - (*pragma_buff)->base) * 2);
127136ac495dSmrg (*pragma_buff)->next = next;
127236ac495dSmrg }
127336ac495dSmrg }
127436ac495dSmrg *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token;
127536ac495dSmrg BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *);
127636ac495dSmrg if (token->type == CPP_PRAGMA_EOL)
127736ac495dSmrg break;
127836ac495dSmrg token = cpp_get_token_1 (pfile, &virt_loc);
127936ac495dSmrg }
128036ac495dSmrg while (token->type != CPP_EOF);
128136ac495dSmrg
128236ac495dSmrg /* In deferred pragmas parsing_args and prevent_expansion
128336ac495dSmrg had been changed, reset it. */
128436ac495dSmrg pfile->state.parsing_args = 2;
128536ac495dSmrg pfile->state.prevent_expansion = 1;
128636ac495dSmrg
128736ac495dSmrg if (token->type == CPP_EOF)
128836ac495dSmrg break;
128936ac495dSmrg else
129036ac495dSmrg continue;
129136ac495dSmrg }
129236ac495dSmrg set_arg_token (arg, token, virt_loc,
129336ac495dSmrg ntokens, MACRO_ARG_TOKEN_NORMAL,
129436ac495dSmrg CPP_OPTION (pfile, track_macro_expansion));
129536ac495dSmrg ntokens++;
129636ac495dSmrg }
129736ac495dSmrg
129836ac495dSmrg /* Drop trailing padding. */
129936ac495dSmrg while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
130036ac495dSmrg ntokens--;
130136ac495dSmrg
130236ac495dSmrg arg->count = ntokens;
130336ac495dSmrg set_arg_token (arg, &pfile->eof, pfile->eof.src_loc,
130436ac495dSmrg ntokens, MACRO_ARG_TOKEN_NORMAL,
130536ac495dSmrg CPP_OPTION (pfile, track_macro_expansion));
130636ac495dSmrg
130736ac495dSmrg /* Terminate the argument. Excess arguments loop back and
130836ac495dSmrg overwrite the final legitimate argument, before failing. */
130936ac495dSmrg if (argc <= macro->paramc)
131036ac495dSmrg {
131136ac495dSmrg buff->cur = (unsigned char *) &arg->first[ntokens + 1];
131236ac495dSmrg if (argc != macro->paramc)
131336ac495dSmrg arg++;
131436ac495dSmrg }
131536ac495dSmrg }
131636ac495dSmrg while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
131736ac495dSmrg
131836ac495dSmrg if (token->type == CPP_EOF)
131936ac495dSmrg {
132036ac495dSmrg /* We still need the CPP_EOF to end directives, and to end
132136ac495dSmrg pre-expansion of a macro argument. Step back is not
132236ac495dSmrg unconditional, since we don't want to return a CPP_EOF to our
132336ac495dSmrg callers at the end of an -include-d file. */
132436ac495dSmrg if (pfile->context->prev || pfile->state.in_directive)
132536ac495dSmrg _cpp_backup_tokens (pfile, 1);
132636ac495dSmrg cpp_error (pfile, CPP_DL_ERROR,
132736ac495dSmrg "unterminated argument list invoking macro \"%s\"",
132836ac495dSmrg NODE_NAME (node));
132936ac495dSmrg }
133036ac495dSmrg else
133136ac495dSmrg {
133236ac495dSmrg /* A single empty argument is counted as no argument. */
133336ac495dSmrg if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
133436ac495dSmrg argc = 0;
133536ac495dSmrg if (_cpp_arguments_ok (pfile, macro, node, argc))
133636ac495dSmrg {
133736ac495dSmrg /* GCC has special semantics for , ## b where b is a varargs
133836ac495dSmrg parameter: we remove the comma if b was omitted entirely.
133936ac495dSmrg If b was merely an empty argument, the comma is retained.
134036ac495dSmrg If the macro takes just one (varargs) parameter, then we
134136ac495dSmrg retain the comma only if we are standards conforming.
134236ac495dSmrg
134336ac495dSmrg If FIRST is NULL replace_args () swallows the comma. */
134436ac495dSmrg if (macro->variadic && (argc < macro->paramc
134536ac495dSmrg || (argc == 1 && args[0].count == 0
134636ac495dSmrg && !CPP_OPTION (pfile, std))))
134736ac495dSmrg args[macro->paramc - 1].first = NULL;
134836ac495dSmrg if (num_args)
134936ac495dSmrg *num_args = num_args_alloced;
135036ac495dSmrg return base_buff;
135136ac495dSmrg }
135236ac495dSmrg }
135336ac495dSmrg
135436ac495dSmrg /* An error occurred. */
135536ac495dSmrg _cpp_release_buff (pfile, base_buff);
135636ac495dSmrg return NULL;
135736ac495dSmrg }
135836ac495dSmrg
135936ac495dSmrg /* Search for an opening parenthesis to the macro of NODE, in such a
136036ac495dSmrg way that, if none is found, we don't lose the information in any
136136ac495dSmrg intervening padding tokens. If we find the parenthesis, collect
136236ac495dSmrg the arguments and return the buffer containing them. PRAGMA_BUFF
136336ac495dSmrg argument is the same as in collect_args. If NUM_ARGS is non-NULL,
136436ac495dSmrg *NUM_ARGS is set to the number of arguments contained in the
136536ac495dSmrg returned buffer. */
136636ac495dSmrg static _cpp_buff *
funlike_invocation_p(cpp_reader * pfile,cpp_hashnode * node,_cpp_buff ** pragma_buff,unsigned * num_args)136736ac495dSmrg funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node,
136836ac495dSmrg _cpp_buff **pragma_buff, unsigned *num_args)
136936ac495dSmrg {
137036ac495dSmrg const cpp_token *token, *padding = NULL;
137136ac495dSmrg
137236ac495dSmrg for (;;)
137336ac495dSmrg {
137436ac495dSmrg token = cpp_get_token (pfile);
137536ac495dSmrg if (token->type != CPP_PADDING)
137636ac495dSmrg break;
1377*8feb0f0bSmrg gcc_assert ((token->flags & PREV_WHITE) == 0);
137836ac495dSmrg if (padding == NULL
1379*8feb0f0bSmrg || padding->val.source == NULL
1380*8feb0f0bSmrg || (!(padding->val.source->flags & PREV_WHITE)
1381*8feb0f0bSmrg && token->val.source == NULL))
138236ac495dSmrg padding = token;
138336ac495dSmrg }
138436ac495dSmrg
138536ac495dSmrg if (token->type == CPP_OPEN_PAREN)
138636ac495dSmrg {
138736ac495dSmrg pfile->state.parsing_args = 2;
138836ac495dSmrg return collect_args (pfile, node, pragma_buff, num_args);
138936ac495dSmrg }
139036ac495dSmrg
139136ac495dSmrg /* CPP_EOF can be the end of macro arguments, or the end of the
139236ac495dSmrg file. We mustn't back up over the latter. Ugh. */
139336ac495dSmrg if (token->type != CPP_EOF || token == &pfile->eof)
139436ac495dSmrg {
139536ac495dSmrg /* Back up. We may have skipped padding, in which case backing
139636ac495dSmrg up more than one token when expanding macros is in general
139736ac495dSmrg too difficult. We re-insert it in its own context. */
139836ac495dSmrg _cpp_backup_tokens (pfile, 1);
139936ac495dSmrg if (padding)
140036ac495dSmrg _cpp_push_token_context (pfile, NULL, padding, 1);
140136ac495dSmrg }
140236ac495dSmrg
140336ac495dSmrg return NULL;
140436ac495dSmrg }
140536ac495dSmrg
140636ac495dSmrg /* Return the real number of tokens in the expansion of MACRO. */
140736ac495dSmrg static inline unsigned int
macro_real_token_count(const cpp_macro * macro)140836ac495dSmrg macro_real_token_count (const cpp_macro *macro)
140936ac495dSmrg {
141036ac495dSmrg if (__builtin_expect (!macro->extra_tokens, true))
141136ac495dSmrg return macro->count;
1412c0a68be4Smrg
1413c0a68be4Smrg for (unsigned i = macro->count; i--;)
1414c0a68be4Smrg if (macro->exp.tokens[i].type != CPP_PASTE)
1415c0a68be4Smrg return i + 1;
1416c0a68be4Smrg
1417c0a68be4Smrg return 0;
141836ac495dSmrg }
141936ac495dSmrg
142036ac495dSmrg /* Push the context of a macro with hash entry NODE onto the context
142136ac495dSmrg stack. If we can successfully expand the macro, we push a context
142236ac495dSmrg containing its yet-to-be-rescanned replacement list and return one.
142336ac495dSmrg If there were additionally any unexpanded deferred #pragma
142436ac495dSmrg directives among macro arguments, push another context containing
142536ac495dSmrg the pragma tokens before the yet-to-be-rescanned replacement list
142636ac495dSmrg and return two. Otherwise, we don't push a context and return
142736ac495dSmrg zero. LOCATION is the location of the expansion point of the
142836ac495dSmrg macro. */
142936ac495dSmrg static int
enter_macro_context(cpp_reader * pfile,cpp_hashnode * node,const cpp_token * result,location_t location)143036ac495dSmrg enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
1431c0a68be4Smrg const cpp_token *result, location_t location)
143236ac495dSmrg {
143336ac495dSmrg /* The presence of a macro invalidates a file's controlling macro. */
143436ac495dSmrg pfile->mi_valid = false;
143536ac495dSmrg
143636ac495dSmrg pfile->state.angled_headers = false;
143736ac495dSmrg
143836ac495dSmrg /* From here to when we push the context for the macro later down
143936ac495dSmrg this function, we need to flag the fact that we are about to
144036ac495dSmrg expand a macro. This is useful when -ftrack-macro-expansion is
144136ac495dSmrg turned off. In that case, we need to record the location of the
144236ac495dSmrg expansion point of the top-most macro we are about to to expand,
144336ac495dSmrg into pfile->invocation_location. But we must not record any such
144436ac495dSmrg location once the process of expanding the macro starts; that is,
144536ac495dSmrg we must not do that recording between now and later down this
144636ac495dSmrg function where set this flag to FALSE. */
144736ac495dSmrg pfile->about_to_expand_macro_p = true;
144836ac495dSmrg
1449c0a68be4Smrg if (cpp_user_macro_p (node))
145036ac495dSmrg {
145136ac495dSmrg cpp_macro *macro = node->value.macro;
145236ac495dSmrg _cpp_buff *pragma_buff = NULL;
145336ac495dSmrg
145436ac495dSmrg if (macro->fun_like)
145536ac495dSmrg {
145636ac495dSmrg _cpp_buff *buff;
145736ac495dSmrg unsigned num_args = 0;
145836ac495dSmrg
145936ac495dSmrg pfile->state.prevent_expansion++;
146036ac495dSmrg pfile->keep_tokens++;
146136ac495dSmrg pfile->state.parsing_args = 1;
146236ac495dSmrg buff = funlike_invocation_p (pfile, node, &pragma_buff,
146336ac495dSmrg &num_args);
146436ac495dSmrg pfile->state.parsing_args = 0;
146536ac495dSmrg pfile->keep_tokens--;
146636ac495dSmrg pfile->state.prevent_expansion--;
146736ac495dSmrg
146836ac495dSmrg if (buff == NULL)
146936ac495dSmrg {
147036ac495dSmrg if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
147136ac495dSmrg cpp_warning (pfile, CPP_W_TRADITIONAL,
147236ac495dSmrg "function-like macro \"%s\" must be used with arguments in traditional C",
147336ac495dSmrg NODE_NAME (node));
147436ac495dSmrg
147536ac495dSmrg if (pragma_buff)
147636ac495dSmrg _cpp_release_buff (pfile, pragma_buff);
147736ac495dSmrg
147836ac495dSmrg pfile->about_to_expand_macro_p = false;
147936ac495dSmrg return 0;
148036ac495dSmrg }
148136ac495dSmrg
148236ac495dSmrg if (macro->paramc > 0)
148336ac495dSmrg replace_args (pfile, node, macro,
148436ac495dSmrg (macro_arg *) buff->base,
148536ac495dSmrg location);
148636ac495dSmrg /* Free the memory used by the arguments of this
148736ac495dSmrg function-like macro. This memory has been allocated by
148836ac495dSmrg funlike_invocation_p and by replace_args. */
148936ac495dSmrg delete_macro_args (buff, num_args);
149036ac495dSmrg }
149136ac495dSmrg
149236ac495dSmrg /* Disable the macro within its expansion. */
149336ac495dSmrg node->flags |= NODE_DISABLED;
149436ac495dSmrg
1495c0a68be4Smrg /* Laziness can only affect the expansion tokens of the macro,
1496c0a68be4Smrg not its fun-likeness or parameters. */
1497c0a68be4Smrg _cpp_maybe_notify_macro_use (pfile, node);
149836ac495dSmrg if (pfile->cb.used)
149936ac495dSmrg pfile->cb.used (pfile, location, node);
150036ac495dSmrg
150136ac495dSmrg macro->used = 1;
150236ac495dSmrg
150336ac495dSmrg if (macro->paramc == 0)
150436ac495dSmrg {
150536ac495dSmrg unsigned tokens_count = macro_real_token_count (macro);
150636ac495dSmrg if (CPP_OPTION (pfile, track_macro_expansion))
150736ac495dSmrg {
150836ac495dSmrg unsigned int i;
150936ac495dSmrg const cpp_token *src = macro->exp.tokens;
151036ac495dSmrg const line_map_macro *map;
1511c0a68be4Smrg location_t *virt_locs = NULL;
151236ac495dSmrg _cpp_buff *macro_tokens
151336ac495dSmrg = tokens_buff_new (pfile, tokens_count, &virt_locs);
151436ac495dSmrg
151536ac495dSmrg /* Create a macro map to record the locations of the
151636ac495dSmrg tokens that are involved in the expansion. LOCATION
151736ac495dSmrg is the location of the macro expansion point. */
151836ac495dSmrg map = linemap_enter_macro (pfile->line_table,
151936ac495dSmrg node, location, tokens_count);
152036ac495dSmrg for (i = 0; i < tokens_count; ++i)
152136ac495dSmrg {
152236ac495dSmrg tokens_buff_add_token (macro_tokens, virt_locs,
152336ac495dSmrg src, src->src_loc,
152436ac495dSmrg src->src_loc, map, i);
152536ac495dSmrg ++src;
152636ac495dSmrg }
152736ac495dSmrg push_extended_tokens_context (pfile, node,
152836ac495dSmrg macro_tokens,
152936ac495dSmrg virt_locs,
153036ac495dSmrg (const cpp_token **)
153136ac495dSmrg macro_tokens->base,
153236ac495dSmrg tokens_count);
153336ac495dSmrg }
153436ac495dSmrg else
153536ac495dSmrg _cpp_push_token_context (pfile, node, macro->exp.tokens,
153636ac495dSmrg tokens_count);
153736ac495dSmrg num_macro_tokens_counter += tokens_count;
153836ac495dSmrg }
153936ac495dSmrg
154036ac495dSmrg if (pragma_buff)
154136ac495dSmrg {
154236ac495dSmrg if (!pfile->state.in_directive)
154336ac495dSmrg _cpp_push_token_context (pfile, NULL,
154436ac495dSmrg padding_token (pfile, result), 1);
154536ac495dSmrg do
154636ac495dSmrg {
154736ac495dSmrg unsigned tokens_count;
154836ac495dSmrg _cpp_buff *tail = pragma_buff->next;
154936ac495dSmrg pragma_buff->next = NULL;
155036ac495dSmrg tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff)
155136ac495dSmrg - (const cpp_token **) pragma_buff->base);
155236ac495dSmrg push_ptoken_context (pfile, NULL, pragma_buff,
155336ac495dSmrg (const cpp_token **) pragma_buff->base,
155436ac495dSmrg tokens_count);
155536ac495dSmrg pragma_buff = tail;
155636ac495dSmrg if (!CPP_OPTION (pfile, track_macro_expansion))
155736ac495dSmrg num_macro_tokens_counter += tokens_count;
155836ac495dSmrg
155936ac495dSmrg }
156036ac495dSmrg while (pragma_buff != NULL);
156136ac495dSmrg pfile->about_to_expand_macro_p = false;
156236ac495dSmrg return 2;
156336ac495dSmrg }
156436ac495dSmrg
156536ac495dSmrg pfile->about_to_expand_macro_p = false;
156636ac495dSmrg return 1;
156736ac495dSmrg }
156836ac495dSmrg
156936ac495dSmrg pfile->about_to_expand_macro_p = false;
157036ac495dSmrg /* Handle built-in macros and the _Pragma operator. */
157136ac495dSmrg {
1572c0a68be4Smrg location_t expand_loc;
157336ac495dSmrg
157436ac495dSmrg if (/* The top-level macro invocation that triggered the expansion
1575c0a68be4Smrg we are looking at is with a function-like user macro ... */
1576c0a68be4Smrg cpp_fun_like_macro_p (pfile->top_most_macro_node)
1577c0a68be4Smrg /* ... and we are tracking the macro expansion. */
1578c0a68be4Smrg && CPP_OPTION (pfile, track_macro_expansion))
157936ac495dSmrg /* Then the location of the end of the macro invocation is the
1580c0a68be4Smrg location of the expansion point of this macro. */
1581c0a68be4Smrg expand_loc = location;
158236ac495dSmrg else
158336ac495dSmrg /* Otherwise, the location of the end of the macro invocation is
158436ac495dSmrg the location of the expansion point of that top-level macro
158536ac495dSmrg invocation. */
158636ac495dSmrg expand_loc = pfile->invocation_location;
158736ac495dSmrg
1588c0a68be4Smrg return builtin_macro (pfile, node, location, expand_loc);
158936ac495dSmrg }
159036ac495dSmrg }
159136ac495dSmrg
159236ac495dSmrg /* De-allocate the memory used by BUFF which is an array of instances
159336ac495dSmrg of macro_arg. NUM_ARGS is the number of instances of macro_arg
159436ac495dSmrg present in BUFF. */
159536ac495dSmrg static void
delete_macro_args(_cpp_buff * buff,unsigned num_args)159636ac495dSmrg delete_macro_args (_cpp_buff *buff, unsigned num_args)
159736ac495dSmrg {
159836ac495dSmrg macro_arg *macro_args;
159936ac495dSmrg unsigned i;
160036ac495dSmrg
160136ac495dSmrg if (buff == NULL)
160236ac495dSmrg return;
160336ac495dSmrg
160436ac495dSmrg macro_args = (macro_arg *) buff->base;
160536ac495dSmrg
160636ac495dSmrg /* Walk instances of macro_arg to free their expanded tokens as well
160736ac495dSmrg as their macro_arg::virt_locs members. */
160836ac495dSmrg for (i = 0; i < num_args; ++i)
160936ac495dSmrg {
161036ac495dSmrg if (macro_args[i].expanded)
161136ac495dSmrg {
161236ac495dSmrg free (macro_args[i].expanded);
161336ac495dSmrg macro_args[i].expanded = NULL;
161436ac495dSmrg }
161536ac495dSmrg if (macro_args[i].virt_locs)
161636ac495dSmrg {
161736ac495dSmrg free (macro_args[i].virt_locs);
161836ac495dSmrg macro_args[i].virt_locs = NULL;
161936ac495dSmrg }
162036ac495dSmrg if (macro_args[i].expanded_virt_locs)
162136ac495dSmrg {
162236ac495dSmrg free (macro_args[i].expanded_virt_locs);
162336ac495dSmrg macro_args[i].expanded_virt_locs = NULL;
162436ac495dSmrg }
162536ac495dSmrg }
162636ac495dSmrg _cpp_free_buff (buff);
162736ac495dSmrg }
162836ac495dSmrg
162936ac495dSmrg /* Set the INDEXth token of the macro argument ARG. TOKEN is the token
163036ac495dSmrg to set, LOCATION is its virtual location. "Virtual" location means
163136ac495dSmrg the location that encodes loci across macro expansion. Otherwise
163236ac495dSmrg it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the
163336ac495dSmrg argument ARG is supposed to contain. Note that ARG must be
163436ac495dSmrg tailored so that it has enough room to contain INDEX + 1 numbers of
163536ac495dSmrg tokens, at least. */
163636ac495dSmrg static void
set_arg_token(macro_arg * arg,const cpp_token * token,location_t location,size_t index,enum macro_arg_token_kind kind,bool track_macro_exp_p)163736ac495dSmrg set_arg_token (macro_arg *arg, const cpp_token *token,
1638c0a68be4Smrg location_t location, size_t index,
163936ac495dSmrg enum macro_arg_token_kind kind,
164036ac495dSmrg bool track_macro_exp_p)
164136ac495dSmrg {
164236ac495dSmrg const cpp_token **token_ptr;
1643c0a68be4Smrg location_t *loc = NULL;
164436ac495dSmrg
164536ac495dSmrg token_ptr =
164636ac495dSmrg arg_token_ptr_at (arg, index, kind,
164736ac495dSmrg track_macro_exp_p ? &loc : NULL);
164836ac495dSmrg *token_ptr = token;
164936ac495dSmrg
165036ac495dSmrg if (loc != NULL)
165136ac495dSmrg {
165236ac495dSmrg /* We can't set the location of a stringified argument
165336ac495dSmrg token and we can't set any location if we aren't tracking
165436ac495dSmrg macro expansion locations. */
165536ac495dSmrg gcc_checking_assert (kind != MACRO_ARG_TOKEN_STRINGIFIED
165636ac495dSmrg && track_macro_exp_p);
165736ac495dSmrg *loc = location;
165836ac495dSmrg }
165936ac495dSmrg }
166036ac495dSmrg
166136ac495dSmrg /* Get the pointer to the location of the argument token of the
166236ac495dSmrg function-like macro argument ARG. This function must be called
166336ac495dSmrg only when we -ftrack-macro-expansion is on. */
1664c0a68be4Smrg static const location_t *
get_arg_token_location(const macro_arg * arg,enum macro_arg_token_kind kind)166536ac495dSmrg get_arg_token_location (const macro_arg *arg,
166636ac495dSmrg enum macro_arg_token_kind kind)
166736ac495dSmrg {
1668c0a68be4Smrg const location_t *loc = NULL;
166936ac495dSmrg const cpp_token **token_ptr =
1670c0a68be4Smrg arg_token_ptr_at (arg, 0, kind, (location_t **) &loc);
167136ac495dSmrg
167236ac495dSmrg if (token_ptr == NULL)
167336ac495dSmrg return NULL;
167436ac495dSmrg
167536ac495dSmrg return loc;
167636ac495dSmrg }
167736ac495dSmrg
167836ac495dSmrg /* Return the pointer to the INDEXth token of the macro argument ARG.
167936ac495dSmrg KIND specifies the kind of token the macro argument ARG contains.
168036ac495dSmrg If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
168136ac495dSmrg of the virtual location of the returned token if the
168236ac495dSmrg -ftrack-macro-expansion flag is on; otherwise, it's set to the
168336ac495dSmrg spelling location of the returned token. */
168436ac495dSmrg static const cpp_token **
arg_token_ptr_at(const macro_arg * arg,size_t index,enum macro_arg_token_kind kind,location_t ** virt_location)168536ac495dSmrg arg_token_ptr_at (const macro_arg *arg, size_t index,
168636ac495dSmrg enum macro_arg_token_kind kind,
1687c0a68be4Smrg location_t **virt_location)
168836ac495dSmrg {
168936ac495dSmrg const cpp_token **tokens_ptr = NULL;
169036ac495dSmrg
169136ac495dSmrg switch (kind)
169236ac495dSmrg {
169336ac495dSmrg case MACRO_ARG_TOKEN_NORMAL:
169436ac495dSmrg tokens_ptr = arg->first;
169536ac495dSmrg break;
169636ac495dSmrg case MACRO_ARG_TOKEN_STRINGIFIED:
169736ac495dSmrg tokens_ptr = (const cpp_token **) &arg->stringified;
169836ac495dSmrg break;
169936ac495dSmrg case MACRO_ARG_TOKEN_EXPANDED:
170036ac495dSmrg tokens_ptr = arg->expanded;
170136ac495dSmrg break;
170236ac495dSmrg }
170336ac495dSmrg
170436ac495dSmrg if (tokens_ptr == NULL)
170536ac495dSmrg /* This can happen for e.g, an empty token argument to a
170636ac495dSmrg funtion-like macro. */
170736ac495dSmrg return tokens_ptr;
170836ac495dSmrg
170936ac495dSmrg if (virt_location)
171036ac495dSmrg {
171136ac495dSmrg if (kind == MACRO_ARG_TOKEN_NORMAL)
171236ac495dSmrg *virt_location = &arg->virt_locs[index];
171336ac495dSmrg else if (kind == MACRO_ARG_TOKEN_EXPANDED)
171436ac495dSmrg *virt_location = &arg->expanded_virt_locs[index];
171536ac495dSmrg else if (kind == MACRO_ARG_TOKEN_STRINGIFIED)
171636ac495dSmrg *virt_location =
1717c0a68be4Smrg (location_t *) &tokens_ptr[index]->src_loc;
171836ac495dSmrg }
171936ac495dSmrg return &tokens_ptr[index];
172036ac495dSmrg }
172136ac495dSmrg
172236ac495dSmrg /* Initialize an iterator so that it iterates over the tokens of a
172336ac495dSmrg function-like macro argument. KIND is the kind of tokens we want
172436ac495dSmrg ITER to iterate over. TOKEN_PTR points the first token ITER will
172536ac495dSmrg iterate over. */
172636ac495dSmrg 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)172736ac495dSmrg macro_arg_token_iter_init (macro_arg_token_iter *iter,
172836ac495dSmrg bool track_macro_exp_p,
172936ac495dSmrg enum macro_arg_token_kind kind,
173036ac495dSmrg const macro_arg *arg,
173136ac495dSmrg const cpp_token **token_ptr)
173236ac495dSmrg {
173336ac495dSmrg iter->track_macro_exp_p = track_macro_exp_p;
173436ac495dSmrg iter->kind = kind;
173536ac495dSmrg iter->token_ptr = token_ptr;
173636ac495dSmrg /* Unconditionally initialize this so that the compiler doesn't warn
173736ac495dSmrg about iter->location_ptr being possibly uninitialized later after
173836ac495dSmrg this code has been inlined somewhere. */
173936ac495dSmrg iter->location_ptr = NULL;
174036ac495dSmrg if (track_macro_exp_p)
174136ac495dSmrg iter->location_ptr = get_arg_token_location (arg, kind);
174236ac495dSmrg #if CHECKING_P
174336ac495dSmrg iter->num_forwards = 0;
174436ac495dSmrg if (track_macro_exp_p
174536ac495dSmrg && token_ptr != NULL
174636ac495dSmrg && iter->location_ptr == NULL)
174736ac495dSmrg abort ();
174836ac495dSmrg #endif
174936ac495dSmrg }
175036ac495dSmrg
175136ac495dSmrg /* Move the iterator one token forward. Note that if IT was
175236ac495dSmrg initialized on an argument that has a stringified token, moving it
175336ac495dSmrg forward doesn't make sense as a stringified token is essentially one
175436ac495dSmrg string. */
175536ac495dSmrg static void
macro_arg_token_iter_forward(macro_arg_token_iter * it)175636ac495dSmrg macro_arg_token_iter_forward (macro_arg_token_iter *it)
175736ac495dSmrg {
175836ac495dSmrg switch (it->kind)
175936ac495dSmrg {
176036ac495dSmrg case MACRO_ARG_TOKEN_NORMAL:
176136ac495dSmrg case MACRO_ARG_TOKEN_EXPANDED:
176236ac495dSmrg it->token_ptr++;
176336ac495dSmrg if (it->track_macro_exp_p)
176436ac495dSmrg it->location_ptr++;
176536ac495dSmrg break;
176636ac495dSmrg case MACRO_ARG_TOKEN_STRINGIFIED:
176736ac495dSmrg #if CHECKING_P
176836ac495dSmrg if (it->num_forwards > 0)
176936ac495dSmrg abort ();
177036ac495dSmrg #endif
177136ac495dSmrg break;
177236ac495dSmrg }
177336ac495dSmrg
177436ac495dSmrg #if CHECKING_P
177536ac495dSmrg it->num_forwards++;
177636ac495dSmrg #endif
177736ac495dSmrg }
177836ac495dSmrg
177936ac495dSmrg /* Return the token pointed to by the iterator. */
178036ac495dSmrg static const cpp_token *
macro_arg_token_iter_get_token(const macro_arg_token_iter * it)178136ac495dSmrg macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
178236ac495dSmrg {
178336ac495dSmrg #if CHECKING_P
178436ac495dSmrg if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
178536ac495dSmrg && it->num_forwards > 0)
178636ac495dSmrg abort ();
178736ac495dSmrg #endif
178836ac495dSmrg if (it->token_ptr == NULL)
178936ac495dSmrg return NULL;
179036ac495dSmrg return *it->token_ptr;
179136ac495dSmrg }
179236ac495dSmrg
179336ac495dSmrg /* Return the location of the token pointed to by the iterator.*/
1794c0a68be4Smrg static location_t
macro_arg_token_iter_get_location(const macro_arg_token_iter * it)179536ac495dSmrg macro_arg_token_iter_get_location (const macro_arg_token_iter *it)
179636ac495dSmrg {
179736ac495dSmrg #if CHECKING_P
179836ac495dSmrg if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
179936ac495dSmrg && it->num_forwards > 0)
180036ac495dSmrg abort ();
180136ac495dSmrg #endif
180236ac495dSmrg if (it->track_macro_exp_p)
180336ac495dSmrg return *it->location_ptr;
180436ac495dSmrg else
180536ac495dSmrg return (*it->token_ptr)->src_loc;
180636ac495dSmrg }
180736ac495dSmrg
180836ac495dSmrg /* Return the index of a token [resulting from macro expansion] inside
180936ac495dSmrg the total list of tokens resulting from a given macro
181036ac495dSmrg expansion. The index can be different depending on whether if we
181136ac495dSmrg want each tokens resulting from function-like macro arguments
181236ac495dSmrg expansion to have a different location or not.
181336ac495dSmrg
181436ac495dSmrg E.g, consider this function-like macro:
181536ac495dSmrg
181636ac495dSmrg #define M(x) x - 3
181736ac495dSmrg
181836ac495dSmrg Then consider us "calling" it (and thus expanding it) like:
181936ac495dSmrg
182036ac495dSmrg M(1+4)
182136ac495dSmrg
182236ac495dSmrg It will be expanded into:
182336ac495dSmrg
182436ac495dSmrg 1+4-3
182536ac495dSmrg
182636ac495dSmrg Let's consider the case of the token '4'.
182736ac495dSmrg
182836ac495dSmrg Its index can be 2 (it's the third token of the set of tokens
182936ac495dSmrg resulting from the expansion) or it can be 0 if we consider that
183036ac495dSmrg all tokens resulting from the expansion of the argument "1+2" have
183136ac495dSmrg the same index, which is 0. In this later case, the index of token
183236ac495dSmrg '-' would then be 1 and the index of token '3' would be 2.
183336ac495dSmrg
183436ac495dSmrg The later case is useful to use less memory e.g, for the case of
183536ac495dSmrg the user using the option -ftrack-macro-expansion=1.
183636ac495dSmrg
183736ac495dSmrg ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
183836ac495dSmrg are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro
183936ac495dSmrg parameter (inside the macro replacement list) that corresponds to
184036ac495dSmrg the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
184136ac495dSmrg of.
184236ac495dSmrg
184336ac495dSmrg If we refer to the example above, for the '4' argument token,
184436ac495dSmrg ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
184536ac495dSmrg would be set to the token 'x', in the replacement list "x - 3" of
184636ac495dSmrg macro M.
184736ac495dSmrg
184836ac495dSmrg This is a subroutine of replace_args. */
184936ac495dSmrg inline static unsigned
expanded_token_index(cpp_reader * pfile,cpp_macro * macro,const cpp_token * cur_replacement_token,unsigned absolute_token_index)185036ac495dSmrg expanded_token_index (cpp_reader *pfile, cpp_macro *macro,
185136ac495dSmrg const cpp_token *cur_replacement_token,
185236ac495dSmrg unsigned absolute_token_index)
185336ac495dSmrg {
185436ac495dSmrg if (CPP_OPTION (pfile, track_macro_expansion) > 1)
185536ac495dSmrg return absolute_token_index;
185636ac495dSmrg return cur_replacement_token - macro->exp.tokens;
185736ac495dSmrg }
185836ac495dSmrg
1859a2dc1f3fSmrg /* Copy whether PASTE_LEFT is set from SRC to *PASTE_FLAG. */
1860a2dc1f3fSmrg
1861a2dc1f3fSmrg static void
copy_paste_flag(cpp_reader * pfile,const cpp_token ** paste_flag,const cpp_token * src)1862a2dc1f3fSmrg copy_paste_flag (cpp_reader *pfile, const cpp_token **paste_flag,
1863a2dc1f3fSmrg const cpp_token *src)
1864a2dc1f3fSmrg {
1865a2dc1f3fSmrg cpp_token *token = _cpp_temp_token (pfile);
1866a2dc1f3fSmrg token->type = (*paste_flag)->type;
1867a2dc1f3fSmrg token->val = (*paste_flag)->val;
1868a2dc1f3fSmrg if (src->flags & PASTE_LEFT)
1869a2dc1f3fSmrg token->flags = (*paste_flag)->flags | PASTE_LEFT;
1870a2dc1f3fSmrg else
1871a2dc1f3fSmrg token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
1872a2dc1f3fSmrg *paste_flag = token;
1873a2dc1f3fSmrg }
1874a2dc1f3fSmrg
1875a2dc1f3fSmrg /* True IFF the last token emitted into BUFF (if any) is PTR. */
1876a2dc1f3fSmrg
1877a2dc1f3fSmrg static bool
last_token_is(_cpp_buff * buff,const cpp_token ** ptr)1878a2dc1f3fSmrg last_token_is (_cpp_buff *buff, const cpp_token **ptr)
1879a2dc1f3fSmrg {
1880a2dc1f3fSmrg return (ptr && tokens_buff_last_token_ptr (buff) == ptr);
1881a2dc1f3fSmrg }
1882a2dc1f3fSmrg
188336ac495dSmrg /* Replace the parameters in a function-like macro of NODE with the
188436ac495dSmrg actual ARGS, and place the result in a newly pushed token context.
188536ac495dSmrg Expand each argument before replacing, unless it is operated upon
188636ac495dSmrg by the # or ## operators. EXPANSION_POINT_LOC is the location of
188736ac495dSmrg the expansion point of the macro. E.g, the location of the
188836ac495dSmrg function-like macro invocation. */
188936ac495dSmrg static void
replace_args(cpp_reader * pfile,cpp_hashnode * node,cpp_macro * macro,macro_arg * args,location_t expansion_point_loc)189036ac495dSmrg replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
1891c0a68be4Smrg macro_arg *args, location_t expansion_point_loc)
189236ac495dSmrg {
189336ac495dSmrg unsigned int i, total;
189436ac495dSmrg const cpp_token *src, *limit;
189536ac495dSmrg const cpp_token **first = NULL;
189636ac495dSmrg macro_arg *arg;
189736ac495dSmrg _cpp_buff *buff = NULL;
1898c0a68be4Smrg location_t *virt_locs = NULL;
189936ac495dSmrg unsigned int exp_count;
190036ac495dSmrg const line_map_macro *map = NULL;
190136ac495dSmrg int track_macro_exp;
190236ac495dSmrg
190336ac495dSmrg /* First, fully macro-expand arguments, calculating the number of
190436ac495dSmrg tokens in the final expansion as we go. The ordering of the if
190536ac495dSmrg statements below is subtle; we must handle stringification before
190636ac495dSmrg pasting. */
190736ac495dSmrg
190836ac495dSmrg /* EXP_COUNT is the number of tokens in the macro replacement
190936ac495dSmrg list. TOTAL is the number of tokens /after/ macro parameters
191036ac495dSmrg have been replaced by their arguments. */
191136ac495dSmrg exp_count = macro_real_token_count (macro);
191236ac495dSmrg total = exp_count;
191336ac495dSmrg limit = macro->exp.tokens + exp_count;
191436ac495dSmrg
191536ac495dSmrg for (src = macro->exp.tokens; src < limit; src++)
191636ac495dSmrg if (src->type == CPP_MACRO_ARG)
191736ac495dSmrg {
191836ac495dSmrg /* Leading and trailing padding tokens. */
191936ac495dSmrg total += 2;
192036ac495dSmrg /* Account for leading and padding tokens in exp_count too.
192136ac495dSmrg This is going to be important later down this function,
192236ac495dSmrg when we want to handle the case of (track_macro_exp <
192336ac495dSmrg 2). */
192436ac495dSmrg exp_count += 2;
192536ac495dSmrg
192636ac495dSmrg /* We have an argument. If it is not being stringified or
192736ac495dSmrg pasted it is macro-replaced before insertion. */
192836ac495dSmrg arg = &args[src->val.macro_arg.arg_no - 1];
192936ac495dSmrg
193036ac495dSmrg if (src->flags & STRINGIFY_ARG)
193136ac495dSmrg {
193236ac495dSmrg if (!arg->stringified)
193336ac495dSmrg arg->stringified = stringify_arg (pfile, arg);
193436ac495dSmrg }
193536ac495dSmrg else if ((src->flags & PASTE_LEFT)
1936c0a68be4Smrg || (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
193736ac495dSmrg total += arg->count - 1;
193836ac495dSmrg else
193936ac495dSmrg {
194036ac495dSmrg if (!arg->expanded)
194136ac495dSmrg expand_arg (pfile, arg);
194236ac495dSmrg total += arg->expanded_count - 1;
194336ac495dSmrg }
194436ac495dSmrg }
194536ac495dSmrg
194636ac495dSmrg /* When the compiler is called with the -ftrack-macro-expansion
194736ac495dSmrg flag, we need to keep track of the location of each token that
194836ac495dSmrg results from macro expansion.
194936ac495dSmrg
195036ac495dSmrg A token resulting from macro expansion is not a new token. It is
195136ac495dSmrg simply the same token as the token coming from the macro
195236ac495dSmrg definition. The new things that are allocated are the buffer
195336ac495dSmrg that holds the tokens resulting from macro expansion and a new
195436ac495dSmrg location that records many things like the locus of the expansion
195536ac495dSmrg point as well as the original locus inside the definition of the
195636ac495dSmrg macro. This location is called a virtual location.
195736ac495dSmrg
195836ac495dSmrg So the buffer BUFF holds a set of cpp_token*, and the buffer
195936ac495dSmrg VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
196036ac495dSmrg
196136ac495dSmrg Both of these two buffers are going to be hung off of the macro
196236ac495dSmrg context, when the latter is pushed. The memory allocated to
196336ac495dSmrg store the tokens and their locations is going to be freed once
196436ac495dSmrg the context of macro expansion is popped.
196536ac495dSmrg
196636ac495dSmrg As far as tokens are concerned, the memory overhead of
196736ac495dSmrg -ftrack-macro-expansion is proportional to the number of
1968c0a68be4Smrg macros that get expanded multiplied by sizeof (location_t).
196936ac495dSmrg The good news is that extra memory gets freed when the macro
197036ac495dSmrg context is freed, i.e shortly after the macro got expanded. */
197136ac495dSmrg
197236ac495dSmrg /* Is the -ftrack-macro-expansion flag in effect? */
197336ac495dSmrg track_macro_exp = CPP_OPTION (pfile, track_macro_expansion);
197436ac495dSmrg
197536ac495dSmrg /* Now allocate memory space for tokens and locations resulting from
197636ac495dSmrg the macro expansion, copy the tokens and replace the arguments.
197736ac495dSmrg This memory must be freed when the context of the macro MACRO is
197836ac495dSmrg popped. */
197936ac495dSmrg buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL);
198036ac495dSmrg
198136ac495dSmrg first = (const cpp_token **) buff->base;
198236ac495dSmrg
198336ac495dSmrg /* Create a macro map to record the locations of the tokens that are
198436ac495dSmrg involved in the expansion. Note that the expansion point is set
198536ac495dSmrg to the location of the closing parenthesis. Otherwise, the
198636ac495dSmrg subsequent map created for the first token that comes after the
198736ac495dSmrg macro map might have a wrong line number. That would lead to
198836ac495dSmrg tokens with wrong line numbers after the macro expansion. This
198936ac495dSmrg adds up to the memory overhead of the -ftrack-macro-expansion
199036ac495dSmrg flag; for every macro that is expanded, a "macro map" is
199136ac495dSmrg created. */
199236ac495dSmrg if (track_macro_exp)
199336ac495dSmrg {
199436ac495dSmrg int num_macro_tokens = total;
199536ac495dSmrg if (track_macro_exp < 2)
199636ac495dSmrg /* Then the number of macro tokens won't take in account the
199736ac495dSmrg fact that function-like macro arguments can expand to
199836ac495dSmrg multiple tokens. This is to save memory at the expense of
199936ac495dSmrg accuracy.
200036ac495dSmrg
2001a2dc1f3fSmrg Suppose we have #define SQUARE(A) A * A
200236ac495dSmrg
2003a2dc1f3fSmrg And then we do SQUARE(2+3)
200436ac495dSmrg
200536ac495dSmrg Then the tokens 2, +, 3, will have the same location,
200636ac495dSmrg saying they come from the expansion of the argument A. */
200736ac495dSmrg num_macro_tokens = exp_count;
200836ac495dSmrg map = linemap_enter_macro (pfile->line_table, node,
200936ac495dSmrg expansion_point_loc,
201036ac495dSmrg num_macro_tokens);
201136ac495dSmrg }
201236ac495dSmrg i = 0;
2013*8feb0f0bSmrg vaopt_state vaopt_tracker (pfile, macro->variadic, &args[macro->paramc - 1]);
2014a2dc1f3fSmrg const cpp_token **vaopt_start = NULL;
201536ac495dSmrg for (src = macro->exp.tokens; src < limit; src++)
201636ac495dSmrg {
201736ac495dSmrg unsigned int arg_tokens_count;
201836ac495dSmrg macro_arg_token_iter from;
201936ac495dSmrg const cpp_token **paste_flag = NULL;
202036ac495dSmrg const cpp_token **tmp_token_ptr;
202136ac495dSmrg
2022a2dc1f3fSmrg /* __VA_OPT__ handling. */
2023a2dc1f3fSmrg vaopt_state::update_type vostate = vaopt_tracker.update (src);
2024a2dc1f3fSmrg if (vostate != vaopt_state::INCLUDE)
2025a2dc1f3fSmrg {
2026a2dc1f3fSmrg if (vostate == vaopt_state::BEGIN)
2027a2dc1f3fSmrg {
2028a2dc1f3fSmrg /* Padding on the left of __VA_OPT__ (unless RHS of ##). */
2029a2dc1f3fSmrg if (src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
2030a2dc1f3fSmrg {
2031a2dc1f3fSmrg const cpp_token *t = padding_token (pfile, src);
2032a2dc1f3fSmrg unsigned index = expanded_token_index (pfile, macro, src, i);
2033a2dc1f3fSmrg /* Allocate a virtual location for the padding token and
2034a2dc1f3fSmrg append the token and its location to BUFF and
2035a2dc1f3fSmrg VIRT_LOCS. */
2036a2dc1f3fSmrg tokens_buff_add_token (buff, virt_locs, t,
2037a2dc1f3fSmrg t->src_loc, t->src_loc,
2038a2dc1f3fSmrg map, index);
2039a2dc1f3fSmrg }
2040a2dc1f3fSmrg vaopt_start = tokens_buff_last_token_ptr (buff);
2041a2dc1f3fSmrg }
2042a2dc1f3fSmrg else if (vostate == vaopt_state::END)
2043a2dc1f3fSmrg {
2044a2dc1f3fSmrg const cpp_token **start = vaopt_start;
2045a2dc1f3fSmrg vaopt_start = NULL;
2046a2dc1f3fSmrg
2047a2dc1f3fSmrg /* Remove any tail padding from inside the __VA_OPT__. */
2048a2dc1f3fSmrg paste_flag = tokens_buff_last_token_ptr (buff);
2049a2dc1f3fSmrg while (paste_flag && paste_flag != start
2050a2dc1f3fSmrg && (*paste_flag)->type == CPP_PADDING)
2051a2dc1f3fSmrg {
2052a2dc1f3fSmrg tokens_buff_remove_last_token (buff);
2053a2dc1f3fSmrg paste_flag = tokens_buff_last_token_ptr (buff);
2054a2dc1f3fSmrg }
2055a2dc1f3fSmrg
2056*8feb0f0bSmrg if (start && paste_flag == start && (*start)->flags & PASTE_LEFT)
2057*8feb0f0bSmrg /* If __VA_OPT__ expands to nothing (either because __VA_ARGS__
2058*8feb0f0bSmrg is empty or because it is __VA_OPT__() ), drop PASTE_LEFT
2059*8feb0f0bSmrg flag from previous token. */
2060*8feb0f0bSmrg copy_paste_flag (pfile, start, &pfile->avoid_paste);
2061a2dc1f3fSmrg if (src->flags & PASTE_LEFT)
2062a2dc1f3fSmrg {
2063a2dc1f3fSmrg /* With a non-empty __VA_OPT__ on the LHS of ##, the last
2064a2dc1f3fSmrg token should be flagged PASTE_LEFT. */
2065a2dc1f3fSmrg if (paste_flag && (*paste_flag)->type != CPP_PADDING)
2066a2dc1f3fSmrg copy_paste_flag (pfile, paste_flag, src);
2067a2dc1f3fSmrg }
2068a2dc1f3fSmrg else
2069a2dc1f3fSmrg {
2070a2dc1f3fSmrg /* Otherwise, avoid paste on RHS, __VA_OPT__(c)d or
2071a2dc1f3fSmrg __VA_OPT__(c)__VA_OPT__(d). */
2072a2dc1f3fSmrg const cpp_token *t = &pfile->avoid_paste;
2073a2dc1f3fSmrg tokens_buff_add_token (buff, virt_locs,
2074a2dc1f3fSmrg t, t->src_loc, t->src_loc,
2075a2dc1f3fSmrg NULL, 0);
2076a2dc1f3fSmrg }
2077a2dc1f3fSmrg }
2078a2dc1f3fSmrg continue;
2079a2dc1f3fSmrg }
2080a2dc1f3fSmrg
208136ac495dSmrg if (src->type != CPP_MACRO_ARG)
208236ac495dSmrg {
208336ac495dSmrg /* Allocate a virtual location for token SRC, and add that
208436ac495dSmrg token and its virtual location into the buffers BUFF and
208536ac495dSmrg VIRT_LOCS. */
208636ac495dSmrg unsigned index = expanded_token_index (pfile, macro, src, i);
208736ac495dSmrg tokens_buff_add_token (buff, virt_locs, src,
208836ac495dSmrg src->src_loc, src->src_loc,
208936ac495dSmrg map, index);
209036ac495dSmrg i += 1;
209136ac495dSmrg continue;
209236ac495dSmrg }
209336ac495dSmrg
209436ac495dSmrg paste_flag = 0;
209536ac495dSmrg arg = &args[src->val.macro_arg.arg_no - 1];
209636ac495dSmrg /* SRC is a macro parameter that we need to replace with its
209736ac495dSmrg corresponding argument. So at some point we'll need to
209836ac495dSmrg iterate over the tokens of the macro argument and copy them
209936ac495dSmrg into the "place" now holding the correspondig macro
210036ac495dSmrg parameter. We are going to use the iterator type
210136ac495dSmrg macro_argo_token_iter to handle that iterating. The 'if'
210236ac495dSmrg below is to initialize the iterator depending on the type of
210336ac495dSmrg tokens the macro argument has. It also does some adjustment
210436ac495dSmrg related to padding tokens and some pasting corner cases. */
210536ac495dSmrg if (src->flags & STRINGIFY_ARG)
210636ac495dSmrg {
210736ac495dSmrg arg_tokens_count = 1;
210836ac495dSmrg macro_arg_token_iter_init (&from,
210936ac495dSmrg CPP_OPTION (pfile,
211036ac495dSmrg track_macro_expansion),
211136ac495dSmrg MACRO_ARG_TOKEN_STRINGIFIED,
211236ac495dSmrg arg, &arg->stringified);
211336ac495dSmrg }
211436ac495dSmrg else if (src->flags & PASTE_LEFT)
211536ac495dSmrg {
211636ac495dSmrg arg_tokens_count = arg->count;
211736ac495dSmrg macro_arg_token_iter_init (&from,
211836ac495dSmrg CPP_OPTION (pfile,
211936ac495dSmrg track_macro_expansion),
212036ac495dSmrg MACRO_ARG_TOKEN_NORMAL,
212136ac495dSmrg arg, arg->first);
212236ac495dSmrg }
212336ac495dSmrg else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
212436ac495dSmrg {
212536ac495dSmrg int num_toks;
212636ac495dSmrg arg_tokens_count = arg->count;
212736ac495dSmrg macro_arg_token_iter_init (&from,
212836ac495dSmrg CPP_OPTION (pfile,
212936ac495dSmrg track_macro_expansion),
213036ac495dSmrg MACRO_ARG_TOKEN_NORMAL,
213136ac495dSmrg arg, arg->first);
213236ac495dSmrg
213336ac495dSmrg num_toks = tokens_buff_count (buff);
213436ac495dSmrg
213536ac495dSmrg if (num_toks != 0)
213636ac495dSmrg {
213736ac495dSmrg /* So the current parameter token is pasted to the previous
213836ac495dSmrg token in the replacement list. Let's look at what
213936ac495dSmrg we have as previous and current arguments. */
214036ac495dSmrg
214136ac495dSmrg /* This is the previous argument's token ... */
214236ac495dSmrg tmp_token_ptr = tokens_buff_last_token_ptr (buff);
214336ac495dSmrg
214436ac495dSmrg if ((*tmp_token_ptr)->type == CPP_COMMA
214536ac495dSmrg && macro->variadic
214636ac495dSmrg && src->val.macro_arg.arg_no == macro->paramc)
214736ac495dSmrg {
214836ac495dSmrg /* ... which is a comma; and the current parameter
214936ac495dSmrg is the last parameter of a variadic function-like
215036ac495dSmrg macro. If the argument to the current last
215136ac495dSmrg parameter is NULL, then swallow the comma,
215236ac495dSmrg otherwise drop the paste flag. */
215336ac495dSmrg if (macro_arg_token_iter_get_token (&from) == NULL)
215436ac495dSmrg tokens_buff_remove_last_token (buff);
215536ac495dSmrg else
215636ac495dSmrg paste_flag = tmp_token_ptr;
215736ac495dSmrg }
2158a2dc1f3fSmrg /* Remove the paste flag if the RHS is a placemarker, unless the
2159a2dc1f3fSmrg previous emitted token is at the beginning of __VA_OPT__;
2160a2dc1f3fSmrg placemarkers within __VA_OPT__ are ignored in that case. */
2161a2dc1f3fSmrg else if (arg_tokens_count == 0
2162a2dc1f3fSmrg && tmp_token_ptr != vaopt_start)
216336ac495dSmrg paste_flag = tmp_token_ptr;
216436ac495dSmrg }
216536ac495dSmrg }
216636ac495dSmrg else
216736ac495dSmrg {
216836ac495dSmrg arg_tokens_count = arg->expanded_count;
216936ac495dSmrg macro_arg_token_iter_init (&from,
217036ac495dSmrg CPP_OPTION (pfile,
217136ac495dSmrg track_macro_expansion),
217236ac495dSmrg MACRO_ARG_TOKEN_EXPANDED,
217336ac495dSmrg arg, arg->expanded);
2174a2dc1f3fSmrg
2175a2dc1f3fSmrg if (last_token_is (buff, vaopt_start))
2176a2dc1f3fSmrg {
2177a2dc1f3fSmrg /* We're expanding an arg at the beginning of __VA_OPT__.
2178a2dc1f3fSmrg Skip padding. */
2179a2dc1f3fSmrg while (arg_tokens_count)
2180a2dc1f3fSmrg {
2181a2dc1f3fSmrg const cpp_token *t = macro_arg_token_iter_get_token (&from);
2182a2dc1f3fSmrg if (t->type != CPP_PADDING)
2183a2dc1f3fSmrg break;
2184a2dc1f3fSmrg macro_arg_token_iter_forward (&from);
2185a2dc1f3fSmrg --arg_tokens_count;
2186a2dc1f3fSmrg }
2187a2dc1f3fSmrg }
218836ac495dSmrg }
218936ac495dSmrg
219036ac495dSmrg /* Padding on the left of an argument (unless RHS of ##). */
219136ac495dSmrg if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
2192a2dc1f3fSmrg && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT)
2193a2dc1f3fSmrg && !last_token_is (buff, vaopt_start))
219436ac495dSmrg {
219536ac495dSmrg const cpp_token *t = padding_token (pfile, src);
219636ac495dSmrg unsigned index = expanded_token_index (pfile, macro, src, i);
219736ac495dSmrg /* Allocate a virtual location for the padding token and
219836ac495dSmrg append the token and its location to BUFF and
219936ac495dSmrg VIRT_LOCS. */
220036ac495dSmrg tokens_buff_add_token (buff, virt_locs, t,
220136ac495dSmrg t->src_loc, t->src_loc,
220236ac495dSmrg map, index);
220336ac495dSmrg }
220436ac495dSmrg
220536ac495dSmrg if (arg_tokens_count)
220636ac495dSmrg {
220736ac495dSmrg /* So now we've got the number of tokens that make up the
220836ac495dSmrg argument that is going to replace the current parameter
220936ac495dSmrg in the macro's replacement list. */
221036ac495dSmrg unsigned int j;
221136ac495dSmrg for (j = 0; j < arg_tokens_count; ++j)
221236ac495dSmrg {
221336ac495dSmrg /* So if track_macro_exp is < 2, the user wants to
221436ac495dSmrg save extra memory while tracking macro expansion
221536ac495dSmrg locations. So in that case here is what we do:
221636ac495dSmrg
2217a2dc1f3fSmrg Suppose we have #define SQUARE(A) A * A
221836ac495dSmrg
2219a2dc1f3fSmrg And then we do SQUARE(2+3)
222036ac495dSmrg
222136ac495dSmrg Then the tokens 2, +, 3, will have the same location,
222236ac495dSmrg saying they come from the expansion of the argument
222336ac495dSmrg A.
222436ac495dSmrg
222536ac495dSmrg So that means we are going to ignore the COUNT tokens
222636ac495dSmrg resulting from the expansion of the current macro
2227a2dc1f3fSmrg argument. In other words all the ARG_TOKENS_COUNT tokens
222836ac495dSmrg resulting from the expansion of the macro argument will
2229a2dc1f3fSmrg have the index I. Normally, each of those tokens should
223036ac495dSmrg have index I+J. */
223136ac495dSmrg unsigned token_index = i;
223236ac495dSmrg unsigned index;
223336ac495dSmrg if (track_macro_exp > 1)
223436ac495dSmrg token_index += j;
223536ac495dSmrg
223636ac495dSmrg index = expanded_token_index (pfile, macro, src, token_index);
223736ac495dSmrg tokens_buff_add_token (buff, virt_locs,
223836ac495dSmrg macro_arg_token_iter_get_token (&from),
223936ac495dSmrg macro_arg_token_iter_get_location (&from),
224036ac495dSmrg src->src_loc, map, index);
224136ac495dSmrg macro_arg_token_iter_forward (&from);
224236ac495dSmrg }
224336ac495dSmrg
224436ac495dSmrg /* With a non-empty argument on the LHS of ##, the last
224536ac495dSmrg token should be flagged PASTE_LEFT. */
224636ac495dSmrg if (src->flags & PASTE_LEFT)
2247a2dc1f3fSmrg paste_flag
2248a2dc1f3fSmrg = (const cpp_token **) tokens_buff_last_token_ptr (buff);
224936ac495dSmrg }
225036ac495dSmrg else if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99)
225136ac495dSmrg && ! macro->syshdr && ! cpp_in_system_header (pfile))
225236ac495dSmrg {
225336ac495dSmrg if (CPP_OPTION (pfile, cplusplus))
225436ac495dSmrg cpp_pedwarning (pfile, CPP_W_PEDANTIC,
225536ac495dSmrg "invoking macro %s argument %d: "
225636ac495dSmrg "empty macro arguments are undefined"
225736ac495dSmrg " in ISO C++98",
225836ac495dSmrg NODE_NAME (node), src->val.macro_arg.arg_no);
225936ac495dSmrg else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat))
226036ac495dSmrg cpp_pedwarning (pfile,
226136ac495dSmrg CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
226236ac495dSmrg ? CPP_W_C90_C99_COMPAT : CPP_W_PEDANTIC,
226336ac495dSmrg "invoking macro %s argument %d: "
226436ac495dSmrg "empty macro arguments are undefined"
226536ac495dSmrg " in ISO C90",
226636ac495dSmrg NODE_NAME (node), src->val.macro_arg.arg_no);
226736ac495dSmrg }
226836ac495dSmrg else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
226936ac495dSmrg && ! CPP_OPTION (pfile, cplusplus)
227036ac495dSmrg && ! macro->syshdr && ! cpp_in_system_header (pfile))
227136ac495dSmrg cpp_warning (pfile, CPP_W_C90_C99_COMPAT,
227236ac495dSmrg "invoking macro %s argument %d: "
227336ac495dSmrg "empty macro arguments are undefined"
227436ac495dSmrg " in ISO C90",
227536ac495dSmrg NODE_NAME (node), src->val.macro_arg.arg_no);
227636ac495dSmrg
227736ac495dSmrg /* Avoid paste on RHS (even case count == 0). */
2278a2dc1f3fSmrg if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT)
2279a2dc1f3fSmrg && !last_token_is (buff, vaopt_start))
228036ac495dSmrg {
228136ac495dSmrg const cpp_token *t = &pfile->avoid_paste;
228236ac495dSmrg tokens_buff_add_token (buff, virt_locs,
228336ac495dSmrg t, t->src_loc, t->src_loc,
228436ac495dSmrg NULL, 0);
228536ac495dSmrg }
228636ac495dSmrg
228736ac495dSmrg /* Add a new paste flag, or remove an unwanted one. */
228836ac495dSmrg if (paste_flag)
2289a2dc1f3fSmrg copy_paste_flag (pfile, paste_flag, src);
229036ac495dSmrg
229136ac495dSmrg i += arg_tokens_count;
229236ac495dSmrg }
229336ac495dSmrg
229436ac495dSmrg if (track_macro_exp)
229536ac495dSmrg push_extended_tokens_context (pfile, node, buff, virt_locs, first,
229636ac495dSmrg tokens_buff_count (buff));
229736ac495dSmrg else
229836ac495dSmrg push_ptoken_context (pfile, node, buff, first,
229936ac495dSmrg tokens_buff_count (buff));
230036ac495dSmrg
230136ac495dSmrg num_macro_tokens_counter += tokens_buff_count (buff);
230236ac495dSmrg }
230336ac495dSmrg
230436ac495dSmrg /* Return a special padding token, with padding inherited from SOURCE. */
230536ac495dSmrg static const cpp_token *
padding_token(cpp_reader * pfile,const cpp_token * source)230636ac495dSmrg padding_token (cpp_reader *pfile, const cpp_token *source)
230736ac495dSmrg {
230836ac495dSmrg cpp_token *result = _cpp_temp_token (pfile);
230936ac495dSmrg
231036ac495dSmrg result->type = CPP_PADDING;
231136ac495dSmrg
231236ac495dSmrg /* Data in GCed data structures cannot be made const so far, so we
231336ac495dSmrg need a cast here. */
231436ac495dSmrg result->val.source = (cpp_token *) source;
231536ac495dSmrg result->flags = 0;
231636ac495dSmrg return result;
231736ac495dSmrg }
231836ac495dSmrg
231936ac495dSmrg /* Get a new uninitialized context. Create a new one if we cannot
232036ac495dSmrg re-use an old one. */
232136ac495dSmrg static cpp_context *
next_context(cpp_reader * pfile)232236ac495dSmrg next_context (cpp_reader *pfile)
232336ac495dSmrg {
232436ac495dSmrg cpp_context *result = pfile->context->next;
232536ac495dSmrg
232636ac495dSmrg if (result == 0)
232736ac495dSmrg {
232836ac495dSmrg result = XNEW (cpp_context);
232936ac495dSmrg memset (result, 0, sizeof (cpp_context));
233036ac495dSmrg result->prev = pfile->context;
233136ac495dSmrg result->next = 0;
233236ac495dSmrg pfile->context->next = result;
233336ac495dSmrg }
233436ac495dSmrg
233536ac495dSmrg pfile->context = result;
233636ac495dSmrg return result;
233736ac495dSmrg }
233836ac495dSmrg
233936ac495dSmrg /* Push a list of pointers to tokens. */
234036ac495dSmrg static void
push_ptoken_context(cpp_reader * pfile,cpp_hashnode * macro,_cpp_buff * buff,const cpp_token ** first,unsigned int count)234136ac495dSmrg push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
234236ac495dSmrg const cpp_token **first, unsigned int count)
234336ac495dSmrg {
234436ac495dSmrg cpp_context *context = next_context (pfile);
234536ac495dSmrg
234636ac495dSmrg context->tokens_kind = TOKENS_KIND_INDIRECT;
234736ac495dSmrg context->c.macro = macro;
234836ac495dSmrg context->buff = buff;
234936ac495dSmrg FIRST (context).ptoken = first;
235036ac495dSmrg LAST (context).ptoken = first + count;
235136ac495dSmrg }
235236ac495dSmrg
235336ac495dSmrg /* Push a list of tokens.
235436ac495dSmrg
235536ac495dSmrg A NULL macro means that we should continue the current macro
235636ac495dSmrg expansion, in essence. That means that if we are currently in a
235736ac495dSmrg macro expansion context, we'll make the new pfile->context refer to
235836ac495dSmrg the current macro. */
235936ac495dSmrg void
_cpp_push_token_context(cpp_reader * pfile,cpp_hashnode * macro,const cpp_token * first,unsigned int count)236036ac495dSmrg _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
236136ac495dSmrg const cpp_token *first, unsigned int count)
236236ac495dSmrg {
236336ac495dSmrg cpp_context *context;
236436ac495dSmrg
236536ac495dSmrg if (macro == NULL)
236636ac495dSmrg macro = macro_of_context (pfile->context);
236736ac495dSmrg
236836ac495dSmrg context = next_context (pfile);
236936ac495dSmrg context->tokens_kind = TOKENS_KIND_DIRECT;
237036ac495dSmrg context->c.macro = macro;
237136ac495dSmrg context->buff = NULL;
237236ac495dSmrg FIRST (context).token = first;
237336ac495dSmrg LAST (context).token = first + count;
237436ac495dSmrg }
237536ac495dSmrg
237636ac495dSmrg /* Build a context containing a list of tokens as well as their
237736ac495dSmrg virtual locations and push it. TOKENS_BUFF is the buffer that
237836ac495dSmrg contains the tokens pointed to by FIRST. If TOKENS_BUFF is
237936ac495dSmrg non-NULL, it means that the context owns it, meaning that
238036ac495dSmrg _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
238136ac495dSmrg contains the virtual locations.
238236ac495dSmrg
238336ac495dSmrg A NULL macro means that we should continue the current macro
238436ac495dSmrg expansion, in essence. That means that if we are currently in a
238536ac495dSmrg macro expansion context, we'll make the new pfile->context refer to
238636ac495dSmrg the current macro. */
238736ac495dSmrg static void
push_extended_tokens_context(cpp_reader * pfile,cpp_hashnode * macro,_cpp_buff * token_buff,location_t * virt_locs,const cpp_token ** first,unsigned int count)238836ac495dSmrg push_extended_tokens_context (cpp_reader *pfile,
238936ac495dSmrg cpp_hashnode *macro,
239036ac495dSmrg _cpp_buff *token_buff,
2391c0a68be4Smrg location_t *virt_locs,
239236ac495dSmrg const cpp_token **first,
239336ac495dSmrg unsigned int count)
239436ac495dSmrg {
239536ac495dSmrg cpp_context *context;
239636ac495dSmrg macro_context *m;
239736ac495dSmrg
239836ac495dSmrg if (macro == NULL)
239936ac495dSmrg macro = macro_of_context (pfile->context);
240036ac495dSmrg
240136ac495dSmrg context = next_context (pfile);
240236ac495dSmrg context->tokens_kind = TOKENS_KIND_EXTENDED;
240336ac495dSmrg context->buff = token_buff;
240436ac495dSmrg
240536ac495dSmrg m = XNEW (macro_context);
240636ac495dSmrg m->macro_node = macro;
240736ac495dSmrg m->virt_locs = virt_locs;
240836ac495dSmrg m->cur_virt_loc = virt_locs;
240936ac495dSmrg context->c.mc = m;
241036ac495dSmrg FIRST (context).ptoken = first;
241136ac495dSmrg LAST (context).ptoken = first + count;
241236ac495dSmrg }
241336ac495dSmrg
241436ac495dSmrg /* Push a traditional macro's replacement text. */
241536ac495dSmrg void
_cpp_push_text_context(cpp_reader * pfile,cpp_hashnode * macro,const uchar * start,size_t len)241636ac495dSmrg _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
241736ac495dSmrg const uchar *start, size_t len)
241836ac495dSmrg {
241936ac495dSmrg cpp_context *context = next_context (pfile);
242036ac495dSmrg
242136ac495dSmrg context->tokens_kind = TOKENS_KIND_DIRECT;
242236ac495dSmrg context->c.macro = macro;
242336ac495dSmrg context->buff = NULL;
242436ac495dSmrg CUR (context) = start;
242536ac495dSmrg RLIMIT (context) = start + len;
242636ac495dSmrg macro->flags |= NODE_DISABLED;
242736ac495dSmrg }
242836ac495dSmrg
242936ac495dSmrg /* Creates a buffer that holds tokens a.k.a "token buffer", usually
243036ac495dSmrg for the purpose of storing them on a cpp_context. If VIRT_LOCS is
243136ac495dSmrg non-null (which means that -ftrack-macro-expansion is on),
243236ac495dSmrg *VIRT_LOCS is set to a newly allocated buffer that is supposed to
243336ac495dSmrg hold the virtual locations of the tokens resulting from macro
243436ac495dSmrg expansion. */
243536ac495dSmrg static _cpp_buff*
tokens_buff_new(cpp_reader * pfile,size_t len,location_t ** virt_locs)243636ac495dSmrg tokens_buff_new (cpp_reader *pfile, size_t len,
2437c0a68be4Smrg location_t **virt_locs)
243836ac495dSmrg {
243936ac495dSmrg size_t tokens_size = len * sizeof (cpp_token *);
2440c0a68be4Smrg size_t locs_size = len * sizeof (location_t);
244136ac495dSmrg
244236ac495dSmrg if (virt_locs != NULL)
2443c0a68be4Smrg *virt_locs = XNEWVEC (location_t, locs_size);
244436ac495dSmrg return _cpp_get_buff (pfile, tokens_size);
244536ac495dSmrg }
244636ac495dSmrg
244736ac495dSmrg /* Returns the number of tokens contained in a token buffer. The
244836ac495dSmrg buffer holds a set of cpp_token*. */
244936ac495dSmrg static size_t
tokens_buff_count(_cpp_buff * buff)245036ac495dSmrg tokens_buff_count (_cpp_buff *buff)
245136ac495dSmrg {
245236ac495dSmrg return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *);
245336ac495dSmrg }
245436ac495dSmrg
245536ac495dSmrg /* Return a pointer to the last token contained in the token buffer
245636ac495dSmrg BUFF. */
245736ac495dSmrg static const cpp_token **
tokens_buff_last_token_ptr(_cpp_buff * buff)245836ac495dSmrg tokens_buff_last_token_ptr (_cpp_buff *buff)
245936ac495dSmrg {
2460a2dc1f3fSmrg if (BUFF_FRONT (buff) == buff->base)
2461a2dc1f3fSmrg return NULL;
246236ac495dSmrg return &((const cpp_token **) BUFF_FRONT (buff))[-1];
246336ac495dSmrg }
246436ac495dSmrg
246536ac495dSmrg /* Remove the last token contained in the token buffer TOKENS_BUFF.
246636ac495dSmrg If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer
246736ac495dSmrg containing the virtual locations of the tokens in TOKENS_BUFF; in
246836ac495dSmrg which case the function updates that buffer as well. */
246936ac495dSmrg static inline void
tokens_buff_remove_last_token(_cpp_buff * tokens_buff)247036ac495dSmrg tokens_buff_remove_last_token (_cpp_buff *tokens_buff)
247136ac495dSmrg
247236ac495dSmrg {
247336ac495dSmrg if (BUFF_FRONT (tokens_buff) > tokens_buff->base)
247436ac495dSmrg BUFF_FRONT (tokens_buff) =
247536ac495dSmrg (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1];
247636ac495dSmrg }
247736ac495dSmrg
247836ac495dSmrg /* Insert a token into the token buffer at the position pointed to by
247936ac495dSmrg DEST. Note that the buffer is not enlarged so the previous token
248036ac495dSmrg that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null,
248136ac495dSmrg means -ftrack-macro-expansion is effect; it then points to where to
248236ac495dSmrg insert the virtual location of TOKEN. TOKEN is the token to
248336ac495dSmrg insert. VIRT_LOC is the virtual location of the token, i.e, the
248436ac495dSmrg location possibly encoding its locus across macro expansion. If
248536ac495dSmrg TOKEN is an argument of a function-like macro (inside a macro
248636ac495dSmrg replacement list), PARM_DEF_LOC is the spelling location of the
248736ac495dSmrg macro parameter that TOKEN is replacing, in the replacement list of
248836ac495dSmrg the macro. If TOKEN is not an argument of a function-like macro or
248936ac495dSmrg if it doesn't come from a macro expansion, then VIRT_LOC can just
249036ac495dSmrg be set to the same value as PARM_DEF_LOC. If MAP is non null, it
249136ac495dSmrg means TOKEN comes from a macro expansion and MAP is the macro map
249236ac495dSmrg associated to the macro. MACRO_TOKEN_INDEX points to the index of
249336ac495dSmrg the token in the macro map; it is not considered if MAP is NULL.
249436ac495dSmrg
249536ac495dSmrg Upon successful completion this function returns the a pointer to
249636ac495dSmrg the position of the token coming right after the insertion
249736ac495dSmrg point. */
249836ac495dSmrg static inline const cpp_token **
tokens_buff_put_token_to(const cpp_token ** dest,location_t * virt_loc_dest,const cpp_token * token,location_t virt_loc,location_t parm_def_loc,const line_map_macro * map,unsigned int macro_token_index)249936ac495dSmrg tokens_buff_put_token_to (const cpp_token **dest,
2500c0a68be4Smrg location_t *virt_loc_dest,
250136ac495dSmrg const cpp_token *token,
2502c0a68be4Smrg location_t virt_loc,
2503c0a68be4Smrg location_t parm_def_loc,
250436ac495dSmrg const line_map_macro *map,
250536ac495dSmrg unsigned int macro_token_index)
250636ac495dSmrg {
2507c0a68be4Smrg location_t macro_loc = virt_loc;
250836ac495dSmrg const cpp_token **result;
250936ac495dSmrg
251036ac495dSmrg if (virt_loc_dest)
251136ac495dSmrg {
251236ac495dSmrg /* -ftrack-macro-expansion is on. */
251336ac495dSmrg if (map)
251436ac495dSmrg macro_loc = linemap_add_macro_token (map, macro_token_index,
251536ac495dSmrg virt_loc, parm_def_loc);
251636ac495dSmrg *virt_loc_dest = macro_loc;
251736ac495dSmrg }
251836ac495dSmrg *dest = token;
251936ac495dSmrg result = &dest[1];
252036ac495dSmrg
252136ac495dSmrg return result;
252236ac495dSmrg }
252336ac495dSmrg
252436ac495dSmrg /* Adds a token at the end of the tokens contained in BUFFER. Note
252536ac495dSmrg that this function doesn't enlarge BUFFER when the number of tokens
252636ac495dSmrg reaches BUFFER's size; it aborts in that situation.
252736ac495dSmrg
252836ac495dSmrg TOKEN is the token to append. VIRT_LOC is the virtual location of
252936ac495dSmrg the token, i.e, the location possibly encoding its locus across
253036ac495dSmrg macro expansion. If TOKEN is an argument of a function-like macro
253136ac495dSmrg (inside a macro replacement list), PARM_DEF_LOC is the location of
253236ac495dSmrg the macro parameter that TOKEN is replacing. If TOKEN doesn't come
253336ac495dSmrg from a macro expansion, then VIRT_LOC can just be set to the same
253436ac495dSmrg value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes
253536ac495dSmrg from a macro expansion and MAP is the macro map associated to the
253636ac495dSmrg macro. MACRO_TOKEN_INDEX points to the index of the token in the
253736ac495dSmrg macro map; It is not considered if MAP is NULL. If VIRT_LOCS is
253836ac495dSmrg non-null, it means -ftrack-macro-expansion is on; in which case
253936ac495dSmrg this function adds the virtual location DEF_LOC to the VIRT_LOCS
254036ac495dSmrg array, at the same index as the one of TOKEN in BUFFER. Upon
254136ac495dSmrg successful completion this function returns the a pointer to the
254236ac495dSmrg position of the token coming right after the insertion point. */
254336ac495dSmrg static const cpp_token **
tokens_buff_add_token(_cpp_buff * buffer,location_t * virt_locs,const cpp_token * token,location_t virt_loc,location_t parm_def_loc,const line_map_macro * map,unsigned int macro_token_index)254436ac495dSmrg tokens_buff_add_token (_cpp_buff *buffer,
2545c0a68be4Smrg location_t *virt_locs,
254636ac495dSmrg const cpp_token *token,
2547c0a68be4Smrg location_t virt_loc,
2548c0a68be4Smrg location_t parm_def_loc,
254936ac495dSmrg const line_map_macro *map,
255036ac495dSmrg unsigned int macro_token_index)
255136ac495dSmrg {
255236ac495dSmrg const cpp_token **result;
2553c0a68be4Smrg location_t *virt_loc_dest = NULL;
255436ac495dSmrg unsigned token_index =
255536ac495dSmrg (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
255636ac495dSmrg
255736ac495dSmrg /* Abort if we pass the end the buffer. */
255836ac495dSmrg if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer))
255936ac495dSmrg abort ();
256036ac495dSmrg
256136ac495dSmrg if (virt_locs != NULL)
256236ac495dSmrg virt_loc_dest = &virt_locs[token_index];
256336ac495dSmrg
256436ac495dSmrg result =
256536ac495dSmrg tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer),
256636ac495dSmrg virt_loc_dest, token, virt_loc, parm_def_loc,
256736ac495dSmrg map, macro_token_index);
256836ac495dSmrg
256936ac495dSmrg BUFF_FRONT (buffer) = (unsigned char *) result;
257036ac495dSmrg return result;
257136ac495dSmrg }
257236ac495dSmrg
257336ac495dSmrg /* Allocate space for the function-like macro argument ARG to store
257436ac495dSmrg the tokens resulting from the macro-expansion of the tokens that
257536ac495dSmrg make up ARG itself. That space is allocated in ARG->expanded and
257636ac495dSmrg needs to be freed using free. */
257736ac495dSmrg static void
alloc_expanded_arg_mem(cpp_reader * pfile,macro_arg * arg,size_t capacity)257836ac495dSmrg alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity)
257936ac495dSmrg {
258036ac495dSmrg gcc_checking_assert (arg->expanded == NULL
258136ac495dSmrg && arg->expanded_virt_locs == NULL);
258236ac495dSmrg
258336ac495dSmrg arg->expanded = XNEWVEC (const cpp_token *, capacity);
258436ac495dSmrg if (CPP_OPTION (pfile, track_macro_expansion))
2585c0a68be4Smrg arg->expanded_virt_locs = XNEWVEC (location_t, capacity);
258636ac495dSmrg
258736ac495dSmrg }
258836ac495dSmrg
258936ac495dSmrg /* If necessary, enlarge ARG->expanded to so that it can contain SIZE
259036ac495dSmrg tokens. */
259136ac495dSmrg static void
ensure_expanded_arg_room(cpp_reader * pfile,macro_arg * arg,size_t size,size_t * expanded_capacity)259236ac495dSmrg ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg,
259336ac495dSmrg size_t size, size_t *expanded_capacity)
259436ac495dSmrg {
259536ac495dSmrg if (size <= *expanded_capacity)
259636ac495dSmrg return;
259736ac495dSmrg
259836ac495dSmrg size *= 2;
259936ac495dSmrg
260036ac495dSmrg arg->expanded =
260136ac495dSmrg XRESIZEVEC (const cpp_token *, arg->expanded, size);
260236ac495dSmrg *expanded_capacity = size;
260336ac495dSmrg
260436ac495dSmrg if (CPP_OPTION (pfile, track_macro_expansion))
260536ac495dSmrg {
260636ac495dSmrg if (arg->expanded_virt_locs == NULL)
2607c0a68be4Smrg arg->expanded_virt_locs = XNEWVEC (location_t, size);
260836ac495dSmrg else
2609c0a68be4Smrg arg->expanded_virt_locs = XRESIZEVEC (location_t,
261036ac495dSmrg arg->expanded_virt_locs,
261136ac495dSmrg size);
261236ac495dSmrg }
261336ac495dSmrg }
261436ac495dSmrg
261536ac495dSmrg /* Expand an argument ARG before replacing parameters in a
261636ac495dSmrg function-like macro. This works by pushing a context with the
261736ac495dSmrg argument's tokens, and then expanding that into a temporary buffer
261836ac495dSmrg as if it were a normal part of the token stream. collect_args()
261936ac495dSmrg has terminated the argument's tokens with a CPP_EOF so that we know
262036ac495dSmrg when we have fully expanded the argument. */
262136ac495dSmrg static void
expand_arg(cpp_reader * pfile,macro_arg * arg)262236ac495dSmrg expand_arg (cpp_reader *pfile, macro_arg *arg)
262336ac495dSmrg {
262436ac495dSmrg size_t capacity;
262536ac495dSmrg bool saved_warn_trad;
262636ac495dSmrg bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
262736ac495dSmrg
262836ac495dSmrg if (arg->count == 0
262936ac495dSmrg || arg->expanded != NULL)
263036ac495dSmrg return;
263136ac495dSmrg
263236ac495dSmrg /* Don't warn about funlike macros when pre-expanding. */
263336ac495dSmrg saved_warn_trad = CPP_WTRADITIONAL (pfile);
263436ac495dSmrg CPP_WTRADITIONAL (pfile) = 0;
263536ac495dSmrg
263636ac495dSmrg /* Loop, reading in the tokens of the argument. */
263736ac495dSmrg capacity = 256;
263836ac495dSmrg alloc_expanded_arg_mem (pfile, arg, capacity);
263936ac495dSmrg
264036ac495dSmrg if (track_macro_exp_p)
264136ac495dSmrg push_extended_tokens_context (pfile, NULL, NULL,
264236ac495dSmrg arg->virt_locs,
264336ac495dSmrg arg->first,
264436ac495dSmrg arg->count + 1);
264536ac495dSmrg else
264636ac495dSmrg push_ptoken_context (pfile, NULL, NULL,
264736ac495dSmrg arg->first, arg->count + 1);
264836ac495dSmrg
264936ac495dSmrg for (;;)
265036ac495dSmrg {
265136ac495dSmrg const cpp_token *token;
2652c0a68be4Smrg location_t location;
265336ac495dSmrg
265436ac495dSmrg ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1,
265536ac495dSmrg &capacity);
265636ac495dSmrg
265736ac495dSmrg token = cpp_get_token_1 (pfile, &location);
265836ac495dSmrg
265936ac495dSmrg if (token->type == CPP_EOF)
266036ac495dSmrg break;
266136ac495dSmrg
266236ac495dSmrg set_arg_token (arg, token, location,
266336ac495dSmrg arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED,
266436ac495dSmrg CPP_OPTION (pfile, track_macro_expansion));
266536ac495dSmrg arg->expanded_count++;
266636ac495dSmrg }
266736ac495dSmrg
266836ac495dSmrg _cpp_pop_context (pfile);
266936ac495dSmrg
267036ac495dSmrg CPP_WTRADITIONAL (pfile) = saved_warn_trad;
267136ac495dSmrg }
267236ac495dSmrg
267336ac495dSmrg /* Returns the macro associated to the current context if we are in
267436ac495dSmrg the context a macro expansion, NULL otherwise. */
267536ac495dSmrg static cpp_hashnode*
macro_of_context(cpp_context * context)267636ac495dSmrg macro_of_context (cpp_context *context)
267736ac495dSmrg {
267836ac495dSmrg if (context == NULL)
267936ac495dSmrg return NULL;
268036ac495dSmrg
268136ac495dSmrg return (context->tokens_kind == TOKENS_KIND_EXTENDED)
268236ac495dSmrg ? context->c.mc->macro_node
268336ac495dSmrg : context->c.macro;
268436ac495dSmrg }
268536ac495dSmrg
268636ac495dSmrg /* Return TRUE iff we are expanding a macro or are about to start
268736ac495dSmrg expanding one. If we are effectively expanding a macro, the
268836ac495dSmrg function macro_of_context returns a pointer to the macro being
268936ac495dSmrg expanded. */
269036ac495dSmrg static bool
in_macro_expansion_p(cpp_reader * pfile)269136ac495dSmrg in_macro_expansion_p (cpp_reader *pfile)
269236ac495dSmrg {
269336ac495dSmrg if (pfile == NULL)
269436ac495dSmrg return false;
269536ac495dSmrg
269636ac495dSmrg return (pfile->about_to_expand_macro_p
269736ac495dSmrg || macro_of_context (pfile->context));
269836ac495dSmrg }
269936ac495dSmrg
270036ac495dSmrg /* Pop the current context off the stack, re-enabling the macro if the
270136ac495dSmrg context represented a macro's replacement list. Initially the
270236ac495dSmrg context structure was not freed so that we can re-use it later, but
270336ac495dSmrg now we do free it to reduce peak memory consumption. */
270436ac495dSmrg void
_cpp_pop_context(cpp_reader * pfile)270536ac495dSmrg _cpp_pop_context (cpp_reader *pfile)
270636ac495dSmrg {
270736ac495dSmrg cpp_context *context = pfile->context;
270836ac495dSmrg
270936ac495dSmrg /* We should not be popping the base context. */
271036ac495dSmrg if (context == &pfile->base_context)
271136ac495dSmrg abort ();
271236ac495dSmrg
271336ac495dSmrg if (context->c.macro)
271436ac495dSmrg {
271536ac495dSmrg cpp_hashnode *macro;
271636ac495dSmrg if (context->tokens_kind == TOKENS_KIND_EXTENDED)
271736ac495dSmrg {
271836ac495dSmrg macro_context *mc = context->c.mc;
271936ac495dSmrg macro = mc->macro_node;
272036ac495dSmrg /* If context->buff is set, it means the life time of tokens
272136ac495dSmrg is bound to the life time of this context; so we must
272236ac495dSmrg free the tokens; that means we must free the virtual
272336ac495dSmrg locations of these tokens too. */
272436ac495dSmrg if (context->buff && mc->virt_locs)
272536ac495dSmrg {
272636ac495dSmrg free (mc->virt_locs);
272736ac495dSmrg mc->virt_locs = NULL;
272836ac495dSmrg }
272936ac495dSmrg free (mc);
273036ac495dSmrg context->c.mc = NULL;
273136ac495dSmrg }
273236ac495dSmrg else
273336ac495dSmrg macro = context->c.macro;
273436ac495dSmrg
273536ac495dSmrg /* Beware that MACRO can be NULL in cases like when we are
273636ac495dSmrg called from expand_arg. In those cases, a dummy context with
273736ac495dSmrg tokens is pushed just for the purpose of walking them using
273836ac495dSmrg cpp_get_token_1. In that case, no 'macro' field is set into
273936ac495dSmrg the dummy context. */
274036ac495dSmrg if (macro != NULL
274136ac495dSmrg /* Several contiguous macro expansion contexts can be
274236ac495dSmrg associated to the same macro; that means it's the same
274336ac495dSmrg macro expansion that spans across all these (sub)
274436ac495dSmrg contexts. So we should re-enable an expansion-disabled
274536ac495dSmrg macro only when we are sure we are really out of that
274636ac495dSmrg macro expansion. */
274736ac495dSmrg && macro_of_context (context->prev) != macro)
274836ac495dSmrg macro->flags &= ~NODE_DISABLED;
274936ac495dSmrg
275036ac495dSmrg if (macro == pfile->top_most_macro_node && context->prev == NULL)
275136ac495dSmrg /* We are popping the context of the top-most macro node. */
275236ac495dSmrg pfile->top_most_macro_node = NULL;
275336ac495dSmrg }
275436ac495dSmrg
275536ac495dSmrg if (context->buff)
275636ac495dSmrg {
275736ac495dSmrg /* Decrease memory peak consumption by freeing the memory used
275836ac495dSmrg by the context. */
275936ac495dSmrg _cpp_free_buff (context->buff);
276036ac495dSmrg }
276136ac495dSmrg
276236ac495dSmrg pfile->context = context->prev;
276336ac495dSmrg /* decrease peak memory consumption by feeing the context. */
276436ac495dSmrg pfile->context->next = NULL;
276536ac495dSmrg free (context);
276636ac495dSmrg }
276736ac495dSmrg
276836ac495dSmrg /* Return TRUE if we reached the end of the set of tokens stored in
276936ac495dSmrg CONTEXT, FALSE otherwise. */
277036ac495dSmrg static inline bool
reached_end_of_context(cpp_context * context)277136ac495dSmrg reached_end_of_context (cpp_context *context)
277236ac495dSmrg {
277336ac495dSmrg if (context->tokens_kind == TOKENS_KIND_DIRECT)
277436ac495dSmrg return FIRST (context).token == LAST (context).token;
277536ac495dSmrg else if (context->tokens_kind == TOKENS_KIND_INDIRECT
277636ac495dSmrg || context->tokens_kind == TOKENS_KIND_EXTENDED)
277736ac495dSmrg return FIRST (context).ptoken == LAST (context).ptoken;
277836ac495dSmrg else
277936ac495dSmrg abort ();
278036ac495dSmrg }
278136ac495dSmrg
278236ac495dSmrg /* Consume the next token contained in the current context of PFILE,
278336ac495dSmrg and return it in *TOKEN. It's "full location" is returned in
278436ac495dSmrg *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
278536ac495dSmrg means the location encoding the locus of the token across macro
278636ac495dSmrg expansion; otherwise it's just is the "normal" location of the
278736ac495dSmrg token which (*TOKEN)->src_loc. */
278836ac495dSmrg static inline void
consume_next_token_from_context(cpp_reader * pfile,const cpp_token ** token,location_t * location)278936ac495dSmrg consume_next_token_from_context (cpp_reader *pfile,
279036ac495dSmrg const cpp_token ** token,
2791c0a68be4Smrg location_t *location)
279236ac495dSmrg {
279336ac495dSmrg cpp_context *c = pfile->context;
279436ac495dSmrg
279536ac495dSmrg if ((c)->tokens_kind == TOKENS_KIND_DIRECT)
279636ac495dSmrg {
279736ac495dSmrg *token = FIRST (c).token;
279836ac495dSmrg *location = (*token)->src_loc;
279936ac495dSmrg FIRST (c).token++;
280036ac495dSmrg }
280136ac495dSmrg else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT)
280236ac495dSmrg {
280336ac495dSmrg *token = *FIRST (c).ptoken;
280436ac495dSmrg *location = (*token)->src_loc;
280536ac495dSmrg FIRST (c).ptoken++;
280636ac495dSmrg }
280736ac495dSmrg else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED)
280836ac495dSmrg {
280936ac495dSmrg macro_context *m = c->c.mc;
281036ac495dSmrg *token = *FIRST (c).ptoken;
281136ac495dSmrg if (m->virt_locs)
281236ac495dSmrg {
281336ac495dSmrg *location = *m->cur_virt_loc;
281436ac495dSmrg m->cur_virt_loc++;
281536ac495dSmrg }
281636ac495dSmrg else
281736ac495dSmrg *location = (*token)->src_loc;
281836ac495dSmrg FIRST (c).ptoken++;
281936ac495dSmrg }
282036ac495dSmrg else
282136ac495dSmrg abort ();
282236ac495dSmrg }
282336ac495dSmrg
282436ac495dSmrg /* In the traditional mode of the preprocessor, if we are currently in
282536ac495dSmrg a directive, the location of a token must be the location of the
282636ac495dSmrg start of the directive line. This function returns the proper
282736ac495dSmrg location if we are in the traditional mode, and just returns
282836ac495dSmrg LOCATION otherwise. */
282936ac495dSmrg
2830c0a68be4Smrg static inline location_t
maybe_adjust_loc_for_trad_cpp(cpp_reader * pfile,location_t location)2831c0a68be4Smrg maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, location_t location)
283236ac495dSmrg {
283336ac495dSmrg if (CPP_OPTION (pfile, traditional))
283436ac495dSmrg {
283536ac495dSmrg if (pfile->state.in_directive)
283636ac495dSmrg return pfile->directive_line;
283736ac495dSmrg }
283836ac495dSmrg return location;
283936ac495dSmrg }
284036ac495dSmrg
284136ac495dSmrg /* Routine to get a token as well as its location.
284236ac495dSmrg
284336ac495dSmrg Macro expansions and directives are transparently handled,
284436ac495dSmrg including entering included files. Thus tokens are post-macro
284536ac495dSmrg expansion, and after any intervening directives. External callers
284636ac495dSmrg see CPP_EOF only at EOF. Internal callers also see it when meeting
284736ac495dSmrg a directive inside a macro call, when at the end of a directive and
284836ac495dSmrg state.in_directive is still 1, and at the end of argument
284936ac495dSmrg pre-expansion.
285036ac495dSmrg
285136ac495dSmrg LOC is an out parameter; *LOC is set to the location "as expected
285236ac495dSmrg by the user". Please read the comment of
285336ac495dSmrg cpp_get_token_with_location to learn more about the meaning of this
285436ac495dSmrg location. */
285536ac495dSmrg static const cpp_token*
cpp_get_token_1(cpp_reader * pfile,location_t * location)2856c0a68be4Smrg cpp_get_token_1 (cpp_reader *pfile, location_t *location)
285736ac495dSmrg {
285836ac495dSmrg const cpp_token *result;
285936ac495dSmrg /* This token is a virtual token that either encodes a location
286036ac495dSmrg related to macro expansion or a spelling location. */
2861c0a68be4Smrg location_t virt_loc = 0;
286236ac495dSmrg /* pfile->about_to_expand_macro_p can be overriden by indirect calls
286336ac495dSmrg to functions that push macro contexts. So let's save it so that
286436ac495dSmrg we can restore it when we are about to leave this routine. */
286536ac495dSmrg bool saved_about_to_expand_macro = pfile->about_to_expand_macro_p;
286636ac495dSmrg
286736ac495dSmrg for (;;)
286836ac495dSmrg {
286936ac495dSmrg cpp_hashnode *node;
287036ac495dSmrg cpp_context *context = pfile->context;
287136ac495dSmrg
287236ac495dSmrg /* Context->prev == 0 <=> base context. */
287336ac495dSmrg if (!context->prev)
287436ac495dSmrg {
287536ac495dSmrg result = _cpp_lex_token (pfile);
287636ac495dSmrg virt_loc = result->src_loc;
287736ac495dSmrg }
287836ac495dSmrg else if (!reached_end_of_context (context))
287936ac495dSmrg {
288036ac495dSmrg consume_next_token_from_context (pfile, &result,
288136ac495dSmrg &virt_loc);
288236ac495dSmrg if (result->flags & PASTE_LEFT)
288336ac495dSmrg {
288436ac495dSmrg paste_all_tokens (pfile, result);
288536ac495dSmrg if (pfile->state.in_directive)
288636ac495dSmrg continue;
288736ac495dSmrg result = padding_token (pfile, result);
288836ac495dSmrg goto out;
288936ac495dSmrg }
289036ac495dSmrg }
289136ac495dSmrg else
289236ac495dSmrg {
289336ac495dSmrg if (pfile->context->c.macro)
289436ac495dSmrg ++num_expanded_macros_counter;
289536ac495dSmrg _cpp_pop_context (pfile);
289636ac495dSmrg if (pfile->state.in_directive)
289736ac495dSmrg continue;
289836ac495dSmrg result = &pfile->avoid_paste;
289936ac495dSmrg goto out;
290036ac495dSmrg }
290136ac495dSmrg
290236ac495dSmrg if (pfile->state.in_directive && result->type == CPP_COMMENT)
290336ac495dSmrg continue;
290436ac495dSmrg
290536ac495dSmrg if (result->type != CPP_NAME)
290636ac495dSmrg break;
290736ac495dSmrg
290836ac495dSmrg node = result->val.node.node;
290936ac495dSmrg
2910c0a68be4Smrg if (node->type == NT_VOID || (result->flags & NO_EXPAND))
291136ac495dSmrg break;
291236ac495dSmrg
291336ac495dSmrg if (!(node->flags & NODE_DISABLED))
291436ac495dSmrg {
291536ac495dSmrg int ret = 0;
291636ac495dSmrg /* If not in a macro context, and we're going to start an
291736ac495dSmrg expansion, record the location and the top level macro
291836ac495dSmrg about to be expanded. */
291936ac495dSmrg if (!in_macro_expansion_p (pfile))
292036ac495dSmrg {
292136ac495dSmrg pfile->invocation_location = result->src_loc;
292236ac495dSmrg pfile->top_most_macro_node = node;
292336ac495dSmrg }
292436ac495dSmrg if (pfile->state.prevent_expansion)
292536ac495dSmrg break;
292636ac495dSmrg
292736ac495dSmrg /* Conditional macros require that a predicate be evaluated
292836ac495dSmrg first. */
292936ac495dSmrg if ((node->flags & NODE_CONDITIONAL) != 0)
293036ac495dSmrg {
293136ac495dSmrg if (pfile->cb.macro_to_expand)
293236ac495dSmrg {
293336ac495dSmrg bool whitespace_after;
293436ac495dSmrg const cpp_token *peek_tok = cpp_peek_token (pfile, 0);
293536ac495dSmrg
293636ac495dSmrg whitespace_after = (peek_tok->type == CPP_PADDING
293736ac495dSmrg || (peek_tok->flags & PREV_WHITE));
293836ac495dSmrg node = pfile->cb.macro_to_expand (pfile, result);
293936ac495dSmrg if (node)
294036ac495dSmrg ret = enter_macro_context (pfile, node, result,
294136ac495dSmrg virt_loc);
294236ac495dSmrg else if (whitespace_after)
294336ac495dSmrg {
294436ac495dSmrg /* If macro_to_expand hook returned NULL and it
294536ac495dSmrg ate some tokens, see if we don't need to add
294636ac495dSmrg a padding token in between this and the
294736ac495dSmrg next token. */
294836ac495dSmrg peek_tok = cpp_peek_token (pfile, 0);
294936ac495dSmrg if (peek_tok->type != CPP_PADDING
295036ac495dSmrg && (peek_tok->flags & PREV_WHITE) == 0)
295136ac495dSmrg _cpp_push_token_context (pfile, NULL,
295236ac495dSmrg padding_token (pfile,
295336ac495dSmrg peek_tok), 1);
295436ac495dSmrg }
295536ac495dSmrg }
295636ac495dSmrg }
295736ac495dSmrg else
295836ac495dSmrg ret = enter_macro_context (pfile, node, result,
295936ac495dSmrg virt_loc);
296036ac495dSmrg if (ret)
296136ac495dSmrg {
296236ac495dSmrg if (pfile->state.in_directive || ret == 2)
296336ac495dSmrg continue;
296436ac495dSmrg result = padding_token (pfile, result);
296536ac495dSmrg goto out;
296636ac495dSmrg }
296736ac495dSmrg }
296836ac495dSmrg else
296936ac495dSmrg {
297036ac495dSmrg /* Flag this token as always unexpandable. FIXME: move this
297136ac495dSmrg to collect_args()?. */
297236ac495dSmrg cpp_token *t = _cpp_temp_token (pfile);
297336ac495dSmrg t->type = result->type;
297436ac495dSmrg t->flags = result->flags | NO_EXPAND;
297536ac495dSmrg t->val = result->val;
297636ac495dSmrg result = t;
297736ac495dSmrg }
297836ac495dSmrg
297936ac495dSmrg break;
298036ac495dSmrg }
298136ac495dSmrg
298236ac495dSmrg out:
298336ac495dSmrg if (location != NULL)
298436ac495dSmrg {
298536ac495dSmrg if (virt_loc == 0)
298636ac495dSmrg virt_loc = result->src_loc;
298736ac495dSmrg *location = virt_loc;
298836ac495dSmrg
298936ac495dSmrg if (!CPP_OPTION (pfile, track_macro_expansion)
299036ac495dSmrg && macro_of_context (pfile->context) != NULL)
299136ac495dSmrg /* We are in a macro expansion context, are not tracking
299236ac495dSmrg virtual location, but were asked to report the location
299336ac495dSmrg of the expansion point of the macro being expanded. */
299436ac495dSmrg *location = pfile->invocation_location;
299536ac495dSmrg
299636ac495dSmrg *location = maybe_adjust_loc_for_trad_cpp (pfile, *location);
299736ac495dSmrg }
299836ac495dSmrg
299936ac495dSmrg pfile->about_to_expand_macro_p = saved_about_to_expand_macro;
300036ac495dSmrg return result;
300136ac495dSmrg }
300236ac495dSmrg
300336ac495dSmrg /* External routine to get a token. Also used nearly everywhere
300436ac495dSmrg internally, except for places where we know we can safely call
300536ac495dSmrg _cpp_lex_token directly, such as lexing a directive name.
300636ac495dSmrg
300736ac495dSmrg Macro expansions and directives are transparently handled,
300836ac495dSmrg including entering included files. Thus tokens are post-macro
300936ac495dSmrg expansion, and after any intervening directives. External callers
301036ac495dSmrg see CPP_EOF only at EOF. Internal callers also see it when meeting
301136ac495dSmrg a directive inside a macro call, when at the end of a directive and
301236ac495dSmrg state.in_directive is still 1, and at the end of argument
301336ac495dSmrg pre-expansion. */
301436ac495dSmrg const cpp_token *
cpp_get_token(cpp_reader * pfile)301536ac495dSmrg cpp_get_token (cpp_reader *pfile)
301636ac495dSmrg {
301736ac495dSmrg return cpp_get_token_1 (pfile, NULL);
301836ac495dSmrg }
301936ac495dSmrg
302036ac495dSmrg /* Like cpp_get_token, but also returns a virtual token location
302136ac495dSmrg separate from the spelling location carried by the returned token.
302236ac495dSmrg
302336ac495dSmrg LOC is an out parameter; *LOC is set to the location "as expected
302436ac495dSmrg by the user". This matters when a token results from macro
302536ac495dSmrg expansion; in that case the token's spelling location indicates the
302636ac495dSmrg locus of the token in the definition of the macro but *LOC
302736ac495dSmrg virtually encodes all the other meaningful locuses associated to
302836ac495dSmrg the token.
302936ac495dSmrg
303036ac495dSmrg What? virtual location? Yes, virtual location.
303136ac495dSmrg
303236ac495dSmrg If the token results from macro expansion and if macro expansion
303336ac495dSmrg location tracking is enabled its virtual location encodes (at the
303436ac495dSmrg same time):
303536ac495dSmrg
303636ac495dSmrg - the spelling location of the token
303736ac495dSmrg
303836ac495dSmrg - the locus of the macro expansion point
303936ac495dSmrg
304036ac495dSmrg - the locus of the point where the token got instantiated as part
304136ac495dSmrg of the macro expansion process.
304236ac495dSmrg
304336ac495dSmrg You have to use the linemap API to get the locus you are interested
304436ac495dSmrg in from a given virtual location.
304536ac495dSmrg
304636ac495dSmrg Note however that virtual locations are not necessarily ordered for
304736ac495dSmrg relations '<' and '>'. One must use the function
304836ac495dSmrg linemap_location_before_p instead of using the relational operator
304936ac495dSmrg '<'.
305036ac495dSmrg
305136ac495dSmrg If macro expansion tracking is off and if the token results from
305236ac495dSmrg macro expansion the virtual location is the expansion point of the
305336ac495dSmrg macro that got expanded.
305436ac495dSmrg
305536ac495dSmrg When the token doesn't result from macro expansion, the virtual
305636ac495dSmrg location is just the same thing as its spelling location. */
305736ac495dSmrg
305836ac495dSmrg const cpp_token *
cpp_get_token_with_location(cpp_reader * pfile,location_t * loc)3059c0a68be4Smrg cpp_get_token_with_location (cpp_reader *pfile, location_t *loc)
306036ac495dSmrg {
306136ac495dSmrg return cpp_get_token_1 (pfile, loc);
306236ac495dSmrg }
306336ac495dSmrg
306436ac495dSmrg /* Returns true if we're expanding an object-like macro that was
306536ac495dSmrg defined in a system header. Just checks the macro at the top of
306636ac495dSmrg the stack. Used for diagnostic suppression. */
306736ac495dSmrg int
cpp_sys_macro_p(cpp_reader * pfile)306836ac495dSmrg cpp_sys_macro_p (cpp_reader *pfile)
306936ac495dSmrg {
307036ac495dSmrg cpp_hashnode *node = NULL;
307136ac495dSmrg
307236ac495dSmrg if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
307336ac495dSmrg node = pfile->context->c.mc->macro_node;
307436ac495dSmrg else
307536ac495dSmrg node = pfile->context->c.macro;
307636ac495dSmrg
307736ac495dSmrg return node && node->value.macro && node->value.macro->syshdr;
307836ac495dSmrg }
307936ac495dSmrg
308036ac495dSmrg /* Read each token in, until end of the current file. Directives are
308136ac495dSmrg transparently processed. */
308236ac495dSmrg void
cpp_scan_nooutput(cpp_reader * pfile)308336ac495dSmrg cpp_scan_nooutput (cpp_reader *pfile)
308436ac495dSmrg {
308536ac495dSmrg /* Request a CPP_EOF token at the end of this file, rather than
308636ac495dSmrg transparently continuing with the including file. */
308736ac495dSmrg pfile->buffer->return_at_eof = true;
308836ac495dSmrg
308936ac495dSmrg pfile->state.discarding_output++;
309036ac495dSmrg pfile->state.prevent_expansion++;
309136ac495dSmrg
309236ac495dSmrg if (CPP_OPTION (pfile, traditional))
309336ac495dSmrg while (_cpp_read_logical_line_trad (pfile))
309436ac495dSmrg ;
309536ac495dSmrg else
309636ac495dSmrg while (cpp_get_token (pfile)->type != CPP_EOF)
309736ac495dSmrg ;
309836ac495dSmrg
309936ac495dSmrg pfile->state.discarding_output--;
310036ac495dSmrg pfile->state.prevent_expansion--;
310136ac495dSmrg }
310236ac495dSmrg
310336ac495dSmrg /* Step back one or more tokens obtained from the lexer. */
310436ac495dSmrg void
_cpp_backup_tokens_direct(cpp_reader * pfile,unsigned int count)310536ac495dSmrg _cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count)
310636ac495dSmrg {
310736ac495dSmrg pfile->lookaheads += count;
310836ac495dSmrg while (count--)
310936ac495dSmrg {
311036ac495dSmrg pfile->cur_token--;
311136ac495dSmrg if (pfile->cur_token == pfile->cur_run->base
311236ac495dSmrg /* Possible with -fpreprocessed and no leading #line. */
311336ac495dSmrg && pfile->cur_run->prev != NULL)
311436ac495dSmrg {
311536ac495dSmrg pfile->cur_run = pfile->cur_run->prev;
311636ac495dSmrg pfile->cur_token = pfile->cur_run->limit;
311736ac495dSmrg }
311836ac495dSmrg }
311936ac495dSmrg }
312036ac495dSmrg
312136ac495dSmrg /* Step back one (or more) tokens. Can only step back more than 1 if
312236ac495dSmrg they are from the lexer, and not from macro expansion. */
312336ac495dSmrg void
_cpp_backup_tokens(cpp_reader * pfile,unsigned int count)312436ac495dSmrg _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
312536ac495dSmrg {
312636ac495dSmrg if (pfile->context->prev == NULL)
312736ac495dSmrg _cpp_backup_tokens_direct (pfile, count);
312836ac495dSmrg else
312936ac495dSmrg {
313036ac495dSmrg if (count != 1)
313136ac495dSmrg abort ();
313236ac495dSmrg if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT)
313336ac495dSmrg FIRST (pfile->context).token--;
313436ac495dSmrg else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT)
313536ac495dSmrg FIRST (pfile->context).ptoken--;
313636ac495dSmrg else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
313736ac495dSmrg {
313836ac495dSmrg FIRST (pfile->context).ptoken--;
313936ac495dSmrg if (pfile->context->c.macro)
314036ac495dSmrg {
314136ac495dSmrg macro_context *m = pfile->context->c.mc;
314236ac495dSmrg m->cur_virt_loc--;
314336ac495dSmrg gcc_checking_assert (m->cur_virt_loc >= m->virt_locs);
314436ac495dSmrg }
314536ac495dSmrg else
314636ac495dSmrg abort ();
314736ac495dSmrg }
314836ac495dSmrg else
314936ac495dSmrg abort ();
315036ac495dSmrg }
315136ac495dSmrg }
315236ac495dSmrg
315336ac495dSmrg /* #define directive parsing and handling. */
315436ac495dSmrg
3155c0a68be4Smrg /* Returns true if a macro redefinition warning is required. */
315636ac495dSmrg static bool
warn_of_redefinition(cpp_reader * pfile,cpp_hashnode * node,const cpp_macro * macro2)315736ac495dSmrg warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
315836ac495dSmrg const cpp_macro *macro2)
315936ac495dSmrg {
316036ac495dSmrg /* Some redefinitions need to be warned about regardless. */
316136ac495dSmrg if (node->flags & NODE_WARN)
316236ac495dSmrg return true;
316336ac495dSmrg
316436ac495dSmrg /* Suppress warnings for builtins that lack the NODE_WARN flag,
316536ac495dSmrg unless Wbuiltin-macro-redefined. */
3166c0a68be4Smrg if (cpp_builtin_macro_p (node))
316736ac495dSmrg return CPP_OPTION (pfile, warn_builtin_macro_redefined);
316836ac495dSmrg
316936ac495dSmrg /* Redefinitions of conditional (context-sensitive) macros, on
317036ac495dSmrg the other hand, must be allowed silently. */
317136ac495dSmrg if (node->flags & NODE_CONDITIONAL)
317236ac495dSmrg return false;
317336ac495dSmrg
3174c0a68be4Smrg cpp_macro *macro1 = node->value.macro;
3175c0a68be4Smrg if (macro1->lazy)
3176c0a68be4Smrg {
3177c0a68be4Smrg /* We don't want to mark MACRO as used, but do need to finalize
3178c0a68be4Smrg its laziness. */
3179c0a68be4Smrg pfile->cb.user_lazy_macro (pfile, macro1, macro1->lazy - 1);
3180c0a68be4Smrg macro1->lazy = 0;
3181c0a68be4Smrg }
3182c0a68be4Smrg
318336ac495dSmrg /* Redefinition of a macro is allowed if and only if the old and new
318436ac495dSmrg definitions are the same. (6.10.3 paragraph 2). */
318536ac495dSmrg
318636ac495dSmrg /* Don't check count here as it can be different in valid
318736ac495dSmrg traditional redefinitions with just whitespace differences. */
318836ac495dSmrg if (macro1->paramc != macro2->paramc
318936ac495dSmrg || macro1->fun_like != macro2->fun_like
319036ac495dSmrg || macro1->variadic != macro2->variadic)
319136ac495dSmrg return true;
319236ac495dSmrg
319336ac495dSmrg /* Check parameter spellings. */
3194c0a68be4Smrg for (unsigned i = macro1->paramc; i--; )
3195c0a68be4Smrg if (macro1->parm.params[i] != macro2->parm.params[i])
319636ac495dSmrg return true;
319736ac495dSmrg
319836ac495dSmrg /* Check the replacement text or tokens. */
3199c0a68be4Smrg if (macro1->kind == cmk_traditional)
320036ac495dSmrg return _cpp_expansions_different_trad (macro1, macro2);
320136ac495dSmrg
320236ac495dSmrg if (macro1->count != macro2->count)
320336ac495dSmrg return true;
320436ac495dSmrg
3205c0a68be4Smrg for (unsigned i= macro1->count; i--; )
320636ac495dSmrg if (!_cpp_equiv_tokens (¯o1->exp.tokens[i], ¯o2->exp.tokens[i]))
320736ac495dSmrg return true;
320836ac495dSmrg
320936ac495dSmrg return false;
321036ac495dSmrg }
321136ac495dSmrg
321236ac495dSmrg /* Free the definition of hashnode H. */
321336ac495dSmrg void
_cpp_free_definition(cpp_hashnode * h)321436ac495dSmrg _cpp_free_definition (cpp_hashnode *h)
321536ac495dSmrg {
321636ac495dSmrg /* Macros and assertions no longer have anything to free. */
321736ac495dSmrg h->type = NT_VOID;
3218c0a68be4Smrg h->value.answers = NULL;
3219c0a68be4Smrg h->flags &= ~(NODE_DISABLED | NODE_USED);
322036ac495dSmrg }
322136ac495dSmrg
322236ac495dSmrg /* Save parameter NODE (spelling SPELLING) to the parameter list of
3223c0a68be4Smrg macro MACRO. Returns true on success, false on failure. */
322436ac495dSmrg bool
_cpp_save_parameter(cpp_reader * pfile,unsigned n,cpp_hashnode * node,cpp_hashnode * spelling)3225c0a68be4Smrg _cpp_save_parameter (cpp_reader *pfile, unsigned n, cpp_hashnode *node,
322636ac495dSmrg cpp_hashnode *spelling)
322736ac495dSmrg {
322836ac495dSmrg /* Constraint 6.10.3.6 - duplicate parameter names. */
3229c0a68be4Smrg if (node->type == NT_MACRO_ARG)
323036ac495dSmrg {
323136ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
323236ac495dSmrg NODE_NAME (node));
323336ac495dSmrg return false;
323436ac495dSmrg }
323536ac495dSmrg
3236c0a68be4Smrg unsigned len = (n + 1) * sizeof (struct macro_arg_saved_data);
3237c0a68be4Smrg if (len > pfile->macro_buffer_len)
323836ac495dSmrg {
3239c0a68be4Smrg pfile->macro_buffer
3240c0a68be4Smrg = XRESIZEVEC (unsigned char, pfile->macro_buffer, len);
3241c0a68be4Smrg pfile->macro_buffer_len = len;
3242c0a68be4Smrg }
324336ac495dSmrg
3244c0a68be4Smrg macro_arg_saved_data *saved = (macro_arg_saved_data *)pfile->macro_buffer;
3245c0a68be4Smrg saved[n].canonical_node = node;
3246c0a68be4Smrg saved[n].value = node->value;
3247c0a68be4Smrg saved[n].type = node->type;
3248c0a68be4Smrg
3249c0a68be4Smrg void *base = _cpp_reserve_room (pfile, n * sizeof (cpp_hashnode *),
3250c0a68be4Smrg sizeof (cpp_hashnode *));
3251c0a68be4Smrg ((cpp_hashnode **)base)[n] = spelling;
3252c0a68be4Smrg
3253c0a68be4Smrg /* Morph into a macro arg. */
3254c0a68be4Smrg node->type = NT_MACRO_ARG;
3255c0a68be4Smrg /* Index is 1 based. */
3256c0a68be4Smrg node->value.arg_index = n + 1;
3257c0a68be4Smrg
3258c0a68be4Smrg return true;
3259c0a68be4Smrg }
3260c0a68be4Smrg
3261c0a68be4Smrg /* Restore the parameters to their previous state. */
3262c0a68be4Smrg void
_cpp_unsave_parameters(cpp_reader * pfile,unsigned n)3263c0a68be4Smrg _cpp_unsave_parameters (cpp_reader *pfile, unsigned n)
3264c0a68be4Smrg {
3265c0a68be4Smrg /* Clear the fast argument lookup indices. */
3266c0a68be4Smrg while (n--)
3267c0a68be4Smrg {
3268c0a68be4Smrg struct macro_arg_saved_data *save =
3269c0a68be4Smrg &((struct macro_arg_saved_data *) pfile->macro_buffer)[n];
3270c0a68be4Smrg
3271c0a68be4Smrg struct cpp_hashnode *node = save->canonical_node;
3272c0a68be4Smrg node->type = save->type;
3273c0a68be4Smrg node->value = save->value;
3274c0a68be4Smrg }
3275c0a68be4Smrg }
3276c0a68be4Smrg
3277c0a68be4Smrg /* Check the syntax of the parameters in a MACRO definition. Return
3278c0a68be4Smrg false on failure. Set *N_PTR and *VARADIC_PTR as appropriate.
3279c0a68be4Smrg '(' ')'
3280c0a68be4Smrg '(' parm-list ',' last-parm ')'
3281c0a68be4Smrg '(' last-parm ')'
3282c0a68be4Smrg parm-list: name
3283c0a68be4Smrg | parm-list, name
3284c0a68be4Smrg last-parm: name
3285c0a68be4Smrg | name '...'
3286c0a68be4Smrg | '...'
3287c0a68be4Smrg */
3288c0a68be4Smrg
3289c0a68be4Smrg static bool
parse_params(cpp_reader * pfile,unsigned * n_ptr,bool * varadic_ptr)3290c0a68be4Smrg parse_params (cpp_reader *pfile, unsigned *n_ptr, bool *varadic_ptr)
3291c0a68be4Smrg {
3292c0a68be4Smrg unsigned nparms = 0;
3293c0a68be4Smrg bool ok = false;
3294c0a68be4Smrg
3295c0a68be4Smrg for (bool prev_ident = false;;)
329636ac495dSmrg {
329736ac495dSmrg const cpp_token *token = _cpp_lex_token (pfile);
329836ac495dSmrg
329936ac495dSmrg switch (token->type)
330036ac495dSmrg {
3301c0a68be4Smrg case CPP_COMMENT:
330236ac495dSmrg /* Allow/ignore comments in parameter lists if we are
330336ac495dSmrg preserving comments in macro expansions. */
3304c0a68be4Smrg if (!CPP_OPTION (pfile, discard_comments_in_macro_exp))
3305c0a68be4Smrg break;
330636ac495dSmrg
3307c0a68be4Smrg /* FALLTHRU */
3308c0a68be4Smrg default:
3309c0a68be4Smrg bad:
3310c0a68be4Smrg {
3311c0a68be4Smrg const char *const msgs[5] =
3312c0a68be4Smrg {
3313c0a68be4Smrg N_("expected parameter name, found \"%s\""),
3314c0a68be4Smrg N_("expected ',' or ')', found \"%s\""),
3315c0a68be4Smrg N_("expected parameter name before end of line"),
3316c0a68be4Smrg N_("expected ')' before end of line"),
3317c0a68be4Smrg N_("expected ')' after \"...\"")
3318c0a68be4Smrg };
3319c0a68be4Smrg unsigned ix = prev_ident;
3320c0a68be4Smrg const unsigned char *as_text = NULL;
3321c0a68be4Smrg if (*varadic_ptr)
3322c0a68be4Smrg ix = 4;
3323c0a68be4Smrg else if (token->type == CPP_EOF)
3324c0a68be4Smrg ix += 2;
3325c0a68be4Smrg else
3326c0a68be4Smrg as_text = cpp_token_as_text (pfile, token);
3327c0a68be4Smrg cpp_error (pfile, CPP_DL_ERROR, msgs[ix], as_text);
3328c0a68be4Smrg }
3329c0a68be4Smrg goto out;
333036ac495dSmrg
333136ac495dSmrg case CPP_NAME:
3332c0a68be4Smrg if (prev_ident || *varadic_ptr)
3333c0a68be4Smrg goto bad;
3334c0a68be4Smrg prev_ident = true;
333536ac495dSmrg
3336c0a68be4Smrg if (!_cpp_save_parameter (pfile, nparms, token->val.node.node,
333736ac495dSmrg token->val.node.spelling))
3338c0a68be4Smrg goto out;
3339c0a68be4Smrg nparms++;
3340c0a68be4Smrg break;
334136ac495dSmrg
334236ac495dSmrg case CPP_CLOSE_PAREN:
3343c0a68be4Smrg if (prev_ident || !nparms || *varadic_ptr)
3344c0a68be4Smrg {
3345c0a68be4Smrg ok = true;
3346c0a68be4Smrg goto out;
3347c0a68be4Smrg }
334836ac495dSmrg
334936ac495dSmrg /* FALLTHRU */
335036ac495dSmrg case CPP_COMMA:
3351c0a68be4Smrg if (!prev_ident || *varadic_ptr)
3352c0a68be4Smrg goto bad;
3353c0a68be4Smrg prev_ident = false;
3354c0a68be4Smrg break;
335536ac495dSmrg
335636ac495dSmrg case CPP_ELLIPSIS:
3357c0a68be4Smrg if (*varadic_ptr)
3358c0a68be4Smrg goto bad;
3359c0a68be4Smrg *varadic_ptr = true;
336036ac495dSmrg if (!prev_ident)
336136ac495dSmrg {
3362c0a68be4Smrg /* An ISO bare ellipsis. */
3363c0a68be4Smrg _cpp_save_parameter (pfile, nparms,
336436ac495dSmrg pfile->spec_nodes.n__VA_ARGS__,
336536ac495dSmrg pfile->spec_nodes.n__VA_ARGS__);
3366c0a68be4Smrg nparms++;
336736ac495dSmrg pfile->state.va_args_ok = 1;
336836ac495dSmrg if (! CPP_OPTION (pfile, c99)
336936ac495dSmrg && CPP_OPTION (pfile, cpp_pedantic)
337036ac495dSmrg && CPP_OPTION (pfile, warn_variadic_macros))
337136ac495dSmrg cpp_pedwarning
337236ac495dSmrg (pfile, CPP_W_VARIADIC_MACROS,
3373c0a68be4Smrg CPP_OPTION (pfile, cplusplus)
3374c0a68be4Smrg ? N_("anonymous variadic macros were introduced in C++11")
3375c0a68be4Smrg : N_("anonymous variadic macros were introduced in C99"));
337636ac495dSmrg else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
337736ac495dSmrg && ! CPP_OPTION (pfile, cplusplus))
337836ac495dSmrg cpp_error (pfile, CPP_DL_WARNING,
337936ac495dSmrg "anonymous variadic macros were introduced in C99");
338036ac495dSmrg }
338136ac495dSmrg else if (CPP_OPTION (pfile, cpp_pedantic)
338236ac495dSmrg && CPP_OPTION (pfile, warn_variadic_macros))
338336ac495dSmrg cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
3384c0a68be4Smrg CPP_OPTION (pfile, cplusplus)
3385c0a68be4Smrg ? N_("ISO C++ does not permit named variadic macros")
3386c0a68be4Smrg : N_("ISO C does not permit named variadic macros"));
3387c0a68be4Smrg break;
338836ac495dSmrg }
338936ac495dSmrg }
339036ac495dSmrg
3391c0a68be4Smrg out:
3392c0a68be4Smrg *n_ptr = nparms;
339336ac495dSmrg
3394c0a68be4Smrg return ok;
339536ac495dSmrg }
339636ac495dSmrg
339736ac495dSmrg /* Lex a token from the expansion of MACRO, but mark parameters as we
339836ac495dSmrg find them and warn of traditional stringification. */
3399c0a68be4Smrg static cpp_macro *
lex_expansion_token(cpp_reader * pfile,cpp_macro * macro)340036ac495dSmrg lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
340136ac495dSmrg {
3402c0a68be4Smrg macro = (cpp_macro *)_cpp_reserve_room (pfile,
3403c0a68be4Smrg sizeof (cpp_macro) - sizeof (cpp_token)
3404c0a68be4Smrg + macro->count * sizeof (cpp_token),
3405c0a68be4Smrg sizeof (cpp_token));
3406c0a68be4Smrg cpp_token *saved_cur_token = pfile->cur_token;
3407c0a68be4Smrg pfile->cur_token = ¯o->exp.tokens[macro->count];
3408c0a68be4Smrg cpp_token *token = _cpp_lex_direct (pfile);
340936ac495dSmrg pfile->cur_token = saved_cur_token;
341036ac495dSmrg
341136ac495dSmrg /* Is this a parameter? */
3412c0a68be4Smrg if (token->type == CPP_NAME && token->val.node.node->type == NT_MACRO_ARG)
341336ac495dSmrg {
3414c0a68be4Smrg /* Morph into a parameter reference. */
341536ac495dSmrg cpp_hashnode *spelling = token->val.node.spelling;
341636ac495dSmrg token->type = CPP_MACRO_ARG;
341736ac495dSmrg token->val.macro_arg.arg_no = token->val.node.node->value.arg_index;
341836ac495dSmrg token->val.macro_arg.spelling = spelling;
341936ac495dSmrg }
342036ac495dSmrg else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
342136ac495dSmrg && (token->type == CPP_STRING || token->type == CPP_CHAR))
342236ac495dSmrg check_trad_stringification (pfile, macro, &token->val.str);
342336ac495dSmrg
3424c0a68be4Smrg return macro;
342536ac495dSmrg }
342636ac495dSmrg
3427c0a68be4Smrg static cpp_macro *
create_iso_definition(cpp_reader * pfile)3428c0a68be4Smrg create_iso_definition (cpp_reader *pfile)
342936ac495dSmrg {
343036ac495dSmrg bool following_paste_op = false;
343136ac495dSmrg const char *paste_op_error_msg =
343236ac495dSmrg N_("'##' cannot appear at either end of a macro expansion");
343336ac495dSmrg unsigned int num_extra_tokens = 0;
3434c0a68be4Smrg unsigned nparms = 0;
3435c0a68be4Smrg cpp_hashnode **params = NULL;
3436c0a68be4Smrg bool varadic = false;
3437c0a68be4Smrg bool ok = false;
3438c0a68be4Smrg cpp_macro *macro = NULL;
343936ac495dSmrg
3440c0a68be4Smrg /* Look at the first token, to see if this is a function-like
3441c0a68be4Smrg macro. */
3442c0a68be4Smrg cpp_token first;
3443c0a68be4Smrg cpp_token *saved_cur_token = pfile->cur_token;
3444c0a68be4Smrg pfile->cur_token = &first;
3445c0a68be4Smrg cpp_token *token = _cpp_lex_direct (pfile);
3446c0a68be4Smrg pfile->cur_token = saved_cur_token;
344736ac495dSmrg
3448c0a68be4Smrg if (token->flags & PREV_WHITE)
3449c0a68be4Smrg /* Preceeded by space, must be part of expansion. */;
3450c0a68be4Smrg else if (token->type == CPP_OPEN_PAREN)
345136ac495dSmrg {
3452c0a68be4Smrg /* An open-paren, get a parameter list. */
3453c0a68be4Smrg if (!parse_params (pfile, &nparms, &varadic))
3454c0a68be4Smrg goto out;
345536ac495dSmrg
3456c0a68be4Smrg params = (cpp_hashnode **)_cpp_commit_buff
3457c0a68be4Smrg (pfile, sizeof (cpp_hashnode *) * nparms);
3458c0a68be4Smrg token = NULL;
345936ac495dSmrg }
3460c0a68be4Smrg else if (token->type != CPP_EOF
3461c0a68be4Smrg && !(token->type == CPP_COMMENT
3462c0a68be4Smrg && ! CPP_OPTION (pfile, discard_comments_in_macro_exp)))
346336ac495dSmrg {
346436ac495dSmrg /* While ISO C99 requires whitespace before replacement text
346536ac495dSmrg in a macro definition, ISO C90 with TC1 allows characters
346636ac495dSmrg from the basic source character set there. */
346736ac495dSmrg if (CPP_OPTION (pfile, c99))
346836ac495dSmrg cpp_error (pfile, CPP_DL_PEDWARN,
3469c0a68be4Smrg CPP_OPTION (pfile, cplusplus)
3470c0a68be4Smrg ? N_("ISO C++11 requires whitespace after the macro name")
3471c0a68be4Smrg : N_("ISO C99 requires whitespace after the macro name"));
347236ac495dSmrg else
347336ac495dSmrg {
3474c0a68be4Smrg enum cpp_diagnostic_level warntype = CPP_DL_WARNING;
3475c0a68be4Smrg switch (token->type)
347636ac495dSmrg {
347736ac495dSmrg case CPP_ATSIGN:
347836ac495dSmrg case CPP_AT_NAME:
347936ac495dSmrg case CPP_OBJC_STRING:
348036ac495dSmrg /* '@' is not in basic character set. */
348136ac495dSmrg warntype = CPP_DL_PEDWARN;
348236ac495dSmrg break;
348336ac495dSmrg case CPP_OTHER:
348436ac495dSmrg /* Basic character set sans letters, digits and _. */
348536ac495dSmrg if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
3486c0a68be4Smrg token->val.str.text[0]) == NULL)
348736ac495dSmrg warntype = CPP_DL_PEDWARN;
348836ac495dSmrg break;
348936ac495dSmrg default:
349036ac495dSmrg /* All other tokens start with a character from basic
349136ac495dSmrg character set. */
349236ac495dSmrg break;
349336ac495dSmrg }
349436ac495dSmrg cpp_error (pfile, warntype,
349536ac495dSmrg "missing whitespace after the macro name");
349636ac495dSmrg }
349736ac495dSmrg }
349836ac495dSmrg
3499c0a68be4Smrg macro = _cpp_new_macro (pfile, cmk_macro,
3500c0a68be4Smrg _cpp_reserve_room (pfile, 0, sizeof (cpp_macro)));
3501c0a68be4Smrg
3502c0a68be4Smrg if (!token)
3503c0a68be4Smrg {
3504c0a68be4Smrg macro->variadic = varadic;
3505c0a68be4Smrg macro->paramc = nparms;
3506c0a68be4Smrg macro->parm.params = params;
3507c0a68be4Smrg macro->fun_like = true;
3508c0a68be4Smrg }
350936ac495dSmrg else
351036ac495dSmrg {
3511c0a68be4Smrg /* Preserve the token we peeked, there is already a single slot for it. */
3512c0a68be4Smrg macro->exp.tokens[0] = *token;
3513c0a68be4Smrg token = ¯o->exp.tokens[0];
3514c0a68be4Smrg macro->count = 1;
351536ac495dSmrg }
351636ac495dSmrg
3517*8feb0f0bSmrg for (vaopt_state vaopt_tracker (pfile, macro->variadic, NULL);; token = NULL)
351836ac495dSmrg {
3519c0a68be4Smrg if (!token)
3520c0a68be4Smrg {
3521c0a68be4Smrg macro = lex_expansion_token (pfile, macro);
3522c0a68be4Smrg token = ¯o->exp.tokens[macro->count++];
3523c0a68be4Smrg }
3524c0a68be4Smrg
352536ac495dSmrg /* Check the stringifying # constraint 6.10.3.2.1 of
352636ac495dSmrg function-like macros when lexing the subsequent token. */
352736ac495dSmrg if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
352836ac495dSmrg {
352936ac495dSmrg if (token->type == CPP_MACRO_ARG)
353036ac495dSmrg {
353136ac495dSmrg if (token->flags & PREV_WHITE)
353236ac495dSmrg token->flags |= SP_PREV_WHITE;
353336ac495dSmrg if (token[-1].flags & DIGRAPH)
353436ac495dSmrg token->flags |= SP_DIGRAPH;
353536ac495dSmrg token->flags &= ~PREV_WHITE;
353636ac495dSmrg token->flags |= STRINGIFY_ARG;
353736ac495dSmrg token->flags |= token[-1].flags & PREV_WHITE;
353836ac495dSmrg token[-1] = token[0];
353936ac495dSmrg macro->count--;
354036ac495dSmrg }
354136ac495dSmrg /* Let assembler get away with murder. */
354236ac495dSmrg else if (CPP_OPTION (pfile, lang) != CLK_ASM)
354336ac495dSmrg {
354436ac495dSmrg cpp_error (pfile, CPP_DL_ERROR,
354536ac495dSmrg "'#' is not followed by a macro parameter");
3546c0a68be4Smrg goto out;
354736ac495dSmrg }
354836ac495dSmrg }
354936ac495dSmrg
355036ac495dSmrg if (token->type == CPP_EOF)
355136ac495dSmrg {
355236ac495dSmrg /* Paste operator constraint 6.10.3.3.1:
355336ac495dSmrg Token-paste ##, can appear in both object-like and
355436ac495dSmrg function-like macros, but not at the end. */
355536ac495dSmrg if (following_paste_op)
355636ac495dSmrg {
355736ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
3558c0a68be4Smrg goto out;
355936ac495dSmrg }
3560c0a68be4Smrg if (!vaopt_tracker.completed ())
3561c0a68be4Smrg goto out;
356236ac495dSmrg break;
356336ac495dSmrg }
356436ac495dSmrg
356536ac495dSmrg /* Paste operator constraint 6.10.3.3.1. */
356636ac495dSmrg if (token->type == CPP_PASTE)
356736ac495dSmrg {
356836ac495dSmrg /* Token-paste ##, can appear in both object-like and
356936ac495dSmrg function-like macros, but not at the beginning. */
357036ac495dSmrg if (macro->count == 1)
357136ac495dSmrg {
357236ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
3573c0a68be4Smrg goto out;
357436ac495dSmrg }
357536ac495dSmrg
3576c0a68be4Smrg if (following_paste_op)
357736ac495dSmrg {
3578c0a68be4Smrg /* Consecutive paste operators. This one will be moved
3579c0a68be4Smrg to the end. */
358036ac495dSmrg num_extra_tokens++;
358136ac495dSmrg token->val.token_no = macro->count - 1;
358236ac495dSmrg }
358336ac495dSmrg else
358436ac495dSmrg {
3585c0a68be4Smrg /* Drop the paste operator. */
358636ac495dSmrg --macro->count;
358736ac495dSmrg token[-1].flags |= PASTE_LEFT;
358836ac495dSmrg if (token->flags & DIGRAPH)
358936ac495dSmrg token[-1].flags |= SP_DIGRAPH;
359036ac495dSmrg if (token->flags & PREV_WHITE)
359136ac495dSmrg token[-1].flags |= SP_PREV_WHITE;
359236ac495dSmrg }
3593c0a68be4Smrg following_paste_op = true;
359436ac495dSmrg }
3595c0a68be4Smrg else
3596c0a68be4Smrg following_paste_op = false;
359736ac495dSmrg
3598a2dc1f3fSmrg if (vaopt_tracker.update (token) == vaopt_state::ERROR)
3599c0a68be4Smrg goto out;
360036ac495dSmrg }
360136ac495dSmrg
3602c0a68be4Smrg /* We're committed to winning now. */
3603c0a68be4Smrg ok = true;
360436ac495dSmrg
360536ac495dSmrg /* Don't count the CPP_EOF. */
360636ac495dSmrg macro->count--;
360736ac495dSmrg
3608c0a68be4Smrg macro = (cpp_macro *)_cpp_commit_buff
3609c0a68be4Smrg (pfile, sizeof (cpp_macro) - sizeof (cpp_token)
3610c0a68be4Smrg + sizeof (cpp_token) * macro->count);
3611c0a68be4Smrg
3612c0a68be4Smrg /* Clear whitespace on first token. */
361336ac495dSmrg if (macro->count)
361436ac495dSmrg macro->exp.tokens[0].flags &= ~PREV_WHITE;
361536ac495dSmrg
361636ac495dSmrg if (num_extra_tokens)
361736ac495dSmrg {
3618c0a68be4Smrg /* Place second and subsequent ## or %:%: tokens in sequences of
3619c0a68be4Smrg consecutive such tokens at the end of the list to preserve
3620c0a68be4Smrg information about where they appear, how they are spelt and
3621c0a68be4Smrg whether they are preceded by whitespace without otherwise
3622c0a68be4Smrg interfering with macro expansion. Remember, this is
3623c0a68be4Smrg extremely rare, so efficiency is not a priority. */
3624c0a68be4Smrg cpp_token *temp = (cpp_token *)_cpp_reserve_room
3625c0a68be4Smrg (pfile, 0, num_extra_tokens * sizeof (cpp_token));
3626c0a68be4Smrg unsigned extra_ix = 0, norm_ix = 0;
3627c0a68be4Smrg cpp_token *exp = macro->exp.tokens;
3628c0a68be4Smrg for (unsigned ix = 0; ix != macro->count; ix++)
3629c0a68be4Smrg if (exp[ix].type == CPP_PASTE)
3630c0a68be4Smrg temp[extra_ix++] = exp[ix];
3631c0a68be4Smrg else
3632c0a68be4Smrg exp[norm_ix++] = exp[ix];
3633c0a68be4Smrg memcpy (&exp[norm_ix], temp, num_extra_tokens * sizeof (cpp_token));
3634c0a68be4Smrg
3635c0a68be4Smrg /* Record there are extra tokens. */
3636c0a68be4Smrg macro->extra_tokens = 1;
3637c0a68be4Smrg }
3638c0a68be4Smrg
3639c0a68be4Smrg out:
3640c0a68be4Smrg pfile->state.va_args_ok = 0;
3641c0a68be4Smrg _cpp_unsave_parameters (pfile, nparms);
3642c0a68be4Smrg
3643c0a68be4Smrg return ok ? macro : NULL;
3644c0a68be4Smrg }
3645c0a68be4Smrg
3646c0a68be4Smrg cpp_macro *
_cpp_new_macro(cpp_reader * pfile,cpp_macro_kind kind,void * placement)3647c0a68be4Smrg _cpp_new_macro (cpp_reader *pfile, cpp_macro_kind kind, void *placement)
364836ac495dSmrg {
3649c0a68be4Smrg cpp_macro *macro = (cpp_macro *) placement;
365036ac495dSmrg
365136ac495dSmrg macro->line = pfile->directive_line;
3652c0a68be4Smrg macro->parm.params = 0;
3653c0a68be4Smrg macro->lazy = 0;
365436ac495dSmrg macro->paramc = 0;
365536ac495dSmrg macro->variadic = 0;
365636ac495dSmrg macro->used = !CPP_OPTION (pfile, warn_unused_macros);
365736ac495dSmrg macro->count = 0;
365836ac495dSmrg macro->fun_like = 0;
365936ac495dSmrg macro->extra_tokens = 0;
366036ac495dSmrg /* To suppress some diagnostics. */
366136ac495dSmrg macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
366236ac495dSmrg
3663c0a68be4Smrg macro->kind = kind;
3664c0a68be4Smrg
3665c0a68be4Smrg return macro;
3666c0a68be4Smrg }
3667c0a68be4Smrg
3668c0a68be4Smrg /* Parse a macro and save its expansion. Returns nonzero on success. */
3669c0a68be4Smrg bool
_cpp_create_definition(cpp_reader * pfile,cpp_hashnode * node)3670c0a68be4Smrg _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
3671c0a68be4Smrg {
3672c0a68be4Smrg cpp_macro *macro;
3673c0a68be4Smrg
367436ac495dSmrg if (CPP_OPTION (pfile, traditional))
3675c0a68be4Smrg macro = _cpp_create_trad_definition (pfile);
367636ac495dSmrg else
3677c0a68be4Smrg macro = create_iso_definition (pfile);
367836ac495dSmrg
3679c0a68be4Smrg if (!macro)
3680c0a68be4Smrg return false;
368136ac495dSmrg
3682c0a68be4Smrg if (cpp_macro_p (node))
368336ac495dSmrg {
368436ac495dSmrg if (CPP_OPTION (pfile, warn_unused_macros))
368536ac495dSmrg _cpp_warn_if_unused_macro (pfile, node, NULL);
368636ac495dSmrg
368736ac495dSmrg if (warn_of_redefinition (pfile, node, macro))
368836ac495dSmrg {
3689c0a68be4Smrg const enum cpp_warning_reason reason
3690c0a68be4Smrg = (cpp_builtin_macro_p (node) && !(node->flags & NODE_WARN))
369136ac495dSmrg ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
369236ac495dSmrg
369336ac495dSmrg bool warned =
369436ac495dSmrg cpp_pedwarning_with_line (pfile, reason,
369536ac495dSmrg pfile->directive_line, 0,
369636ac495dSmrg "\"%s\" redefined", NODE_NAME (node));
369736ac495dSmrg
3698c0a68be4Smrg if (warned && cpp_user_macro_p (node))
369936ac495dSmrg cpp_error_with_line (pfile, CPP_DL_NOTE,
370036ac495dSmrg node->value.macro->line, 0,
370136ac495dSmrg "this is the location of the previous definition");
370236ac495dSmrg }
3703c0a68be4Smrg _cpp_free_definition (node);
370436ac495dSmrg }
370536ac495dSmrg
370636ac495dSmrg /* Enter definition in hash table. */
3707c0a68be4Smrg node->type = NT_USER_MACRO;
370836ac495dSmrg node->value.macro = macro;
370936ac495dSmrg if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))
371036ac495dSmrg && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS")
371136ac495dSmrg /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
371236ac495dSmrg in the C standard, as something that one must use in C++.
371336ac495dSmrg However DR#593 and C++11 indicate that they play no role in C++.
371436ac495dSmrg We special-case them anyway. */
371536ac495dSmrg && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS")
371636ac495dSmrg && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS"))
371736ac495dSmrg node->flags |= NODE_WARN;
371836ac495dSmrg
371936ac495dSmrg /* If user defines one of the conditional macros, remove the
372036ac495dSmrg conditional flag */
372136ac495dSmrg node->flags &= ~NODE_CONDITIONAL;
372236ac495dSmrg
3723c0a68be4Smrg return true;
3724c0a68be4Smrg }
3725c0a68be4Smrg
3726c0a68be4Smrg extern void
cpp_define_lazily(cpp_reader * pfile,cpp_hashnode * node,unsigned num)3727c0a68be4Smrg cpp_define_lazily (cpp_reader *pfile, cpp_hashnode *node, unsigned num)
3728c0a68be4Smrg {
3729c0a68be4Smrg cpp_macro *macro = node->value.macro;
3730c0a68be4Smrg
3731c0a68be4Smrg gcc_checking_assert (pfile->cb.user_lazy_macro && macro && num < UCHAR_MAX);
3732c0a68be4Smrg
3733c0a68be4Smrg macro->lazy = num + 1;
3734c0a68be4Smrg }
3735c0a68be4Smrg
3736c0a68be4Smrg /* Notify the use of NODE in a macro-aware context (i.e. expanding it,
3737c0a68be4Smrg or testing its existance). Also applies any lazy definition. */
3738c0a68be4Smrg
3739c0a68be4Smrg extern void
_cpp_notify_macro_use(cpp_reader * pfile,cpp_hashnode * node)3740c0a68be4Smrg _cpp_notify_macro_use (cpp_reader *pfile, cpp_hashnode *node)
3741c0a68be4Smrg {
3742c0a68be4Smrg node->flags |= NODE_USED;
3743c0a68be4Smrg switch (node->type)
3744c0a68be4Smrg {
3745c0a68be4Smrg case NT_USER_MACRO:
3746c0a68be4Smrg {
3747c0a68be4Smrg cpp_macro *macro = node->value.macro;
3748c0a68be4Smrg if (macro->lazy)
3749c0a68be4Smrg {
3750c0a68be4Smrg pfile->cb.user_lazy_macro (pfile, macro, macro->lazy - 1);
3751c0a68be4Smrg macro->lazy = 0;
3752c0a68be4Smrg }
3753c0a68be4Smrg }
3754c0a68be4Smrg /* FALLTHROUGH. */
3755c0a68be4Smrg
3756c0a68be4Smrg case NT_BUILTIN_MACRO:
3757c0a68be4Smrg if (pfile->cb.used_define)
3758c0a68be4Smrg pfile->cb.used_define (pfile, pfile->directive_line, node);
3759c0a68be4Smrg break;
3760c0a68be4Smrg
3761c0a68be4Smrg case NT_VOID:
3762c0a68be4Smrg if (pfile->cb.used_undef)
3763c0a68be4Smrg pfile->cb.used_undef (pfile, pfile->directive_line, node);
3764c0a68be4Smrg break;
3765c0a68be4Smrg
3766c0a68be4Smrg default:
3767c0a68be4Smrg abort ();
3768c0a68be4Smrg }
376936ac495dSmrg }
377036ac495dSmrg
377136ac495dSmrg /* Warn if a token in STRING matches one of a function-like MACRO's
377236ac495dSmrg parameters. */
377336ac495dSmrg static void
check_trad_stringification(cpp_reader * pfile,const cpp_macro * macro,const cpp_string * string)377436ac495dSmrg check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
377536ac495dSmrg const cpp_string *string)
377636ac495dSmrg {
377736ac495dSmrg unsigned int i, len;
377836ac495dSmrg const uchar *p, *q, *limit;
377936ac495dSmrg
378036ac495dSmrg /* Loop over the string. */
378136ac495dSmrg limit = string->text + string->len - 1;
378236ac495dSmrg for (p = string->text + 1; p < limit; p = q)
378336ac495dSmrg {
378436ac495dSmrg /* Find the start of an identifier. */
378536ac495dSmrg while (p < limit && !is_idstart (*p))
378636ac495dSmrg p++;
378736ac495dSmrg
378836ac495dSmrg /* Find the end of the identifier. */
378936ac495dSmrg q = p;
379036ac495dSmrg while (q < limit && is_idchar (*q))
379136ac495dSmrg q++;
379236ac495dSmrg
379336ac495dSmrg len = q - p;
379436ac495dSmrg
379536ac495dSmrg /* Loop over the function macro arguments to see if the
379636ac495dSmrg identifier inside the string matches one of them. */
379736ac495dSmrg for (i = 0; i < macro->paramc; i++)
379836ac495dSmrg {
3799c0a68be4Smrg const cpp_hashnode *node = macro->parm.params[i];
380036ac495dSmrg
380136ac495dSmrg if (NODE_LEN (node) == len
380236ac495dSmrg && !memcmp (p, NODE_NAME (node), len))
380336ac495dSmrg {
3804a2dc1f3fSmrg cpp_warning (pfile, CPP_W_TRADITIONAL,
380536ac495dSmrg "macro argument \"%s\" would be stringified in traditional C",
380636ac495dSmrg NODE_NAME (node));
380736ac495dSmrg break;
380836ac495dSmrg }
380936ac495dSmrg }
381036ac495dSmrg }
381136ac495dSmrg }
381236ac495dSmrg
381336ac495dSmrg /* Returns the name, arguments and expansion of a macro, in a format
381436ac495dSmrg suitable to be read back in again, and therefore also for DWARF 2
381536ac495dSmrg debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
381636ac495dSmrg Caller is expected to generate the "#define" bit if needed. The
381736ac495dSmrg returned text is temporary, and automatically freed later. */
381836ac495dSmrg const unsigned char *
cpp_macro_definition(cpp_reader * pfile,cpp_hashnode * node)381936ac495dSmrg cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
382036ac495dSmrg {
382136ac495dSmrg unsigned int i, len;
382236ac495dSmrg unsigned char *buffer;
382336ac495dSmrg
3824c0a68be4Smrg gcc_checking_assert (cpp_user_macro_p (node));
382536ac495dSmrg
3826c0a68be4Smrg const cpp_macro *macro = node->value.macro;
3827c0a68be4Smrg
382836ac495dSmrg /* Calculate length. */
382936ac495dSmrg len = NODE_LEN (node) * 10 + 2; /* ' ' and NUL. */
383036ac495dSmrg if (macro->fun_like)
383136ac495dSmrg {
383236ac495dSmrg len += 4; /* "()" plus possible final ".." of named
383336ac495dSmrg varargs (we have + 1 below). */
383436ac495dSmrg for (i = 0; i < macro->paramc; i++)
3835c0a68be4Smrg len += NODE_LEN (macro->parm.params[i]) + 1; /* "," */
383636ac495dSmrg }
383736ac495dSmrg
383836ac495dSmrg /* This should match below where we fill in the buffer. */
383936ac495dSmrg if (CPP_OPTION (pfile, traditional))
384036ac495dSmrg len += _cpp_replacement_text_len (macro);
384136ac495dSmrg else
384236ac495dSmrg {
384336ac495dSmrg unsigned int count = macro_real_token_count (macro);
384436ac495dSmrg for (i = 0; i < count; i++)
384536ac495dSmrg {
3846c0a68be4Smrg const cpp_token *token = ¯o->exp.tokens[i];
384736ac495dSmrg
384836ac495dSmrg if (token->type == CPP_MACRO_ARG)
384936ac495dSmrg len += NODE_LEN (token->val.macro_arg.spelling);
385036ac495dSmrg else
385136ac495dSmrg len += cpp_token_len (token);
385236ac495dSmrg
385336ac495dSmrg if (token->flags & STRINGIFY_ARG)
385436ac495dSmrg len++; /* "#" */
385536ac495dSmrg if (token->flags & PASTE_LEFT)
385636ac495dSmrg len += 3; /* " ##" */
385736ac495dSmrg if (token->flags & PREV_WHITE)
385836ac495dSmrg len++; /* " " */
385936ac495dSmrg }
386036ac495dSmrg }
386136ac495dSmrg
386236ac495dSmrg if (len > pfile->macro_buffer_len)
386336ac495dSmrg {
386436ac495dSmrg pfile->macro_buffer = XRESIZEVEC (unsigned char,
386536ac495dSmrg pfile->macro_buffer, len);
386636ac495dSmrg pfile->macro_buffer_len = len;
386736ac495dSmrg }
386836ac495dSmrg
386936ac495dSmrg /* Fill in the buffer. Start with the macro name. */
387036ac495dSmrg buffer = pfile->macro_buffer;
387136ac495dSmrg buffer = _cpp_spell_ident_ucns (buffer, node);
387236ac495dSmrg
387336ac495dSmrg /* Parameter names. */
387436ac495dSmrg if (macro->fun_like)
387536ac495dSmrg {
387636ac495dSmrg *buffer++ = '(';
387736ac495dSmrg for (i = 0; i < macro->paramc; i++)
387836ac495dSmrg {
3879c0a68be4Smrg cpp_hashnode *param = macro->parm.params[i];
388036ac495dSmrg
388136ac495dSmrg if (param != pfile->spec_nodes.n__VA_ARGS__)
388236ac495dSmrg {
388336ac495dSmrg memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
388436ac495dSmrg buffer += NODE_LEN (param);
388536ac495dSmrg }
388636ac495dSmrg
388736ac495dSmrg if (i + 1 < macro->paramc)
388836ac495dSmrg /* Don't emit a space after the comma here; we're trying
388936ac495dSmrg to emit a Dwarf-friendly definition, and the Dwarf spec
389036ac495dSmrg forbids spaces in the argument list. */
389136ac495dSmrg *buffer++ = ',';
389236ac495dSmrg else if (macro->variadic)
389336ac495dSmrg *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
389436ac495dSmrg }
389536ac495dSmrg *buffer++ = ')';
389636ac495dSmrg }
389736ac495dSmrg
389836ac495dSmrg /* The Dwarf spec requires a space after the macro name, even if the
389936ac495dSmrg definition is the empty string. */
390036ac495dSmrg *buffer++ = ' ';
390136ac495dSmrg
390236ac495dSmrg if (CPP_OPTION (pfile, traditional))
390336ac495dSmrg buffer = _cpp_copy_replacement_text (macro, buffer);
390436ac495dSmrg else if (macro->count)
390536ac495dSmrg /* Expansion tokens. */
390636ac495dSmrg {
390736ac495dSmrg unsigned int count = macro_real_token_count (macro);
390836ac495dSmrg for (i = 0; i < count; i++)
390936ac495dSmrg {
3910c0a68be4Smrg const cpp_token *token = ¯o->exp.tokens[i];
391136ac495dSmrg
391236ac495dSmrg if (token->flags & PREV_WHITE)
391336ac495dSmrg *buffer++ = ' ';
391436ac495dSmrg if (token->flags & STRINGIFY_ARG)
391536ac495dSmrg *buffer++ = '#';
391636ac495dSmrg
391736ac495dSmrg if (token->type == CPP_MACRO_ARG)
391836ac495dSmrg {
391936ac495dSmrg memcpy (buffer,
392036ac495dSmrg NODE_NAME (token->val.macro_arg.spelling),
392136ac495dSmrg NODE_LEN (token->val.macro_arg.spelling));
392236ac495dSmrg buffer += NODE_LEN (token->val.macro_arg.spelling);
392336ac495dSmrg }
392436ac495dSmrg else
392536ac495dSmrg buffer = cpp_spell_token (pfile, token, buffer, true);
392636ac495dSmrg
392736ac495dSmrg if (token->flags & PASTE_LEFT)
392836ac495dSmrg {
392936ac495dSmrg *buffer++ = ' ';
393036ac495dSmrg *buffer++ = '#';
393136ac495dSmrg *buffer++ = '#';
393236ac495dSmrg /* Next has PREV_WHITE; see _cpp_create_definition. */
393336ac495dSmrg }
393436ac495dSmrg }
393536ac495dSmrg }
393636ac495dSmrg
393736ac495dSmrg *buffer = '\0';
393836ac495dSmrg return pfile->macro_buffer;
393936ac495dSmrg }
3940