1*38fd1498Szrj /* Part of CPP library. (Macro and #define handling.)
2*38fd1498Szrj Copyright (C) 1986-2018 Free Software Foundation, Inc.
3*38fd1498Szrj Written by Per Bothner, 1994.
4*38fd1498Szrj Based on CCCP program by Paul Rubin, June 1986
5*38fd1498Szrj Adapted to ANSI C, Richard Stallman, Jan 1987
6*38fd1498Szrj
7*38fd1498Szrj This program is free software; you can redistribute it and/or modify it
8*38fd1498Szrj under the terms of the GNU General Public License as published by the
9*38fd1498Szrj Free Software Foundation; either version 3, or (at your option) any
10*38fd1498Szrj later version.
11*38fd1498Szrj
12*38fd1498Szrj This program is distributed in the hope that it will be useful,
13*38fd1498Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
14*38fd1498Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*38fd1498Szrj GNU General Public License for more details.
16*38fd1498Szrj
17*38fd1498Szrj You should have received a copy of the GNU General Public License
18*38fd1498Szrj along with this program; see the file COPYING3. If not see
19*38fd1498Szrj <http://www.gnu.org/licenses/>.
20*38fd1498Szrj
21*38fd1498Szrj In other words, you are welcome to use, share and improve this program.
22*38fd1498Szrj You are forbidden to forbid anyone else to use, share and improve
23*38fd1498Szrj what you give them. Help stamp out software-hoarding! */
24*38fd1498Szrj
25*38fd1498Szrj #include "config.h"
26*38fd1498Szrj #include "system.h"
27*38fd1498Szrj #include "cpplib.h"
28*38fd1498Szrj #include "internal.h"
29*38fd1498Szrj
30*38fd1498Szrj typedef struct macro_arg macro_arg;
31*38fd1498Szrj /* This structure represents the tokens of a macro argument. These
32*38fd1498Szrj tokens can be macro themselves, in which case they can be either
33*38fd1498Szrj expanded or unexpanded. When they are expanded, this data
34*38fd1498Szrj structure keeps both the expanded and unexpanded forms. */
35*38fd1498Szrj struct macro_arg
36*38fd1498Szrj {
37*38fd1498Szrj const cpp_token **first; /* First token in unexpanded argument. */
38*38fd1498Szrj const cpp_token **expanded; /* Macro-expanded argument. */
39*38fd1498Szrj const cpp_token *stringified; /* Stringified argument. */
40*38fd1498Szrj unsigned int count; /* # of tokens in argument. */
41*38fd1498Szrj unsigned int expanded_count; /* # of tokens in expanded argument. */
42*38fd1498Szrj source_location *virt_locs; /* Where virtual locations for
43*38fd1498Szrj unexpanded tokens are stored. */
44*38fd1498Szrj source_location *expanded_virt_locs; /* Where virtual locations for
45*38fd1498Szrj expanded tokens are
46*38fd1498Szrj stored. */
47*38fd1498Szrj };
48*38fd1498Szrj
49*38fd1498Szrj /* The kind of macro tokens which the instance of
50*38fd1498Szrj macro_arg_token_iter is supposed to iterate over. */
51*38fd1498Szrj enum macro_arg_token_kind {
52*38fd1498Szrj MACRO_ARG_TOKEN_NORMAL,
53*38fd1498Szrj /* This is a macro argument token that got transformed into a string
54*38fd1498Szrj literal, e.g. #foo. */
55*38fd1498Szrj MACRO_ARG_TOKEN_STRINGIFIED,
56*38fd1498Szrj /* This is a token resulting from the expansion of a macro
57*38fd1498Szrj argument that was itself a macro. */
58*38fd1498Szrj MACRO_ARG_TOKEN_EXPANDED
59*38fd1498Szrj };
60*38fd1498Szrj
61*38fd1498Szrj /* An iterator over tokens coming from a function-like macro
62*38fd1498Szrj argument. */
63*38fd1498Szrj typedef struct macro_arg_token_iter macro_arg_token_iter;
64*38fd1498Szrj struct macro_arg_token_iter
65*38fd1498Szrj {
66*38fd1498Szrj /* Whether or not -ftrack-macro-expansion is used. */
67*38fd1498Szrj bool track_macro_exp_p;
68*38fd1498Szrj /* The kind of token over which we are supposed to iterate. */
69*38fd1498Szrj enum macro_arg_token_kind kind;
70*38fd1498Szrj /* A pointer to the current token pointed to by the iterator. */
71*38fd1498Szrj const cpp_token **token_ptr;
72*38fd1498Szrj /* A pointer to the "full" location of the current token. If
73*38fd1498Szrj -ftrack-macro-expansion is used this location tracks loci across
74*38fd1498Szrj macro expansion. */
75*38fd1498Szrj const source_location *location_ptr;
76*38fd1498Szrj #if CHECKING_P
77*38fd1498Szrj /* The number of times the iterator went forward. This useful only
78*38fd1498Szrj when checking is enabled. */
79*38fd1498Szrj size_t num_forwards;
80*38fd1498Szrj #endif
81*38fd1498Szrj };
82*38fd1498Szrj
83*38fd1498Szrj /* Saved data about an identifier being used as a macro argument
84*38fd1498Szrj name. */
85*38fd1498Szrj struct macro_arg_saved_data {
86*38fd1498Szrj /* The canonical (UTF-8) spelling of this identifier. */
87*38fd1498Szrj cpp_hashnode *canonical_node;
88*38fd1498Szrj /* The previous value of this identifier. */
89*38fd1498Szrj union _cpp_hashnode_value value;
90*38fd1498Szrj };
91*38fd1498Szrj
92*38fd1498Szrj static const char *vaopt_paste_error =
93*38fd1498Szrj N_("'##' cannot appear at either end of __VA_OPT__");
94*38fd1498Szrj
95*38fd1498Szrj /* A class for tracking __VA_OPT__ state while iterating over a
96*38fd1498Szrj sequence of tokens. This is used during both macro definition and
97*38fd1498Szrj expansion. */
98*38fd1498Szrj class vaopt_state {
99*38fd1498Szrj
100*38fd1498Szrj public:
101*38fd1498Szrj
102*38fd1498Szrj /* Initialize the state tracker. ANY_ARGS is true if variable
103*38fd1498Szrj arguments were provided to the macro invocation. */
vaopt_state(cpp_reader * pfile,bool is_variadic,bool any_args)104*38fd1498Szrj vaopt_state (cpp_reader *pfile, bool is_variadic, bool any_args)
105*38fd1498Szrj : m_pfile (pfile),
106*38fd1498Szrj m_allowed (any_args),
107*38fd1498Szrj m_variadic (is_variadic),
108*38fd1498Szrj m_last_was_paste (false),
109*38fd1498Szrj m_state (0),
110*38fd1498Szrj m_paste_location (0),
111*38fd1498Szrj m_location (0)
112*38fd1498Szrj {
113*38fd1498Szrj }
114*38fd1498Szrj
115*38fd1498Szrj enum update_type
116*38fd1498Szrj {
117*38fd1498Szrj ERROR,
118*38fd1498Szrj DROP,
119*38fd1498Szrj INCLUDE,
120*38fd1498Szrj BEGIN,
121*38fd1498Szrj END
122*38fd1498Szrj };
123*38fd1498Szrj
124*38fd1498Szrj /* Given a token, update the state of this tracker and return a
125*38fd1498Szrj boolean indicating whether the token should be be included in the
126*38fd1498Szrj expansion. */
update(const cpp_token * token)127*38fd1498Szrj update_type update (const cpp_token *token)
128*38fd1498Szrj {
129*38fd1498Szrj /* If the macro isn't variadic, just don't bother. */
130*38fd1498Szrj if (!m_variadic)
131*38fd1498Szrj return INCLUDE;
132*38fd1498Szrj
133*38fd1498Szrj if (token->type == CPP_NAME
134*38fd1498Szrj && token->val.node.node == m_pfile->spec_nodes.n__VA_OPT__)
135*38fd1498Szrj {
136*38fd1498Szrj if (m_state > 0)
137*38fd1498Szrj {
138*38fd1498Szrj cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc,
139*38fd1498Szrj "__VA_OPT__ may not appear in a __VA_OPT__");
140*38fd1498Szrj return ERROR;
141*38fd1498Szrj }
142*38fd1498Szrj ++m_state;
143*38fd1498Szrj m_location = token->src_loc;
144*38fd1498Szrj return BEGIN;
145*38fd1498Szrj }
146*38fd1498Szrj else if (m_state == 1)
147*38fd1498Szrj {
148*38fd1498Szrj if (token->type != CPP_OPEN_PAREN)
149*38fd1498Szrj {
150*38fd1498Szrj cpp_error_at (m_pfile, CPP_DL_ERROR, m_location,
151*38fd1498Szrj "__VA_OPT__ must be followed by an "
152*38fd1498Szrj "open parenthesis");
153*38fd1498Szrj return ERROR;
154*38fd1498Szrj }
155*38fd1498Szrj ++m_state;
156*38fd1498Szrj return DROP;
157*38fd1498Szrj }
158*38fd1498Szrj else if (m_state >= 2)
159*38fd1498Szrj {
160*38fd1498Szrj if (m_state == 2 && token->type == CPP_PASTE)
161*38fd1498Szrj {
162*38fd1498Szrj cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc,
163*38fd1498Szrj vaopt_paste_error);
164*38fd1498Szrj return ERROR;
165*38fd1498Szrj }
166*38fd1498Szrj /* Advance states before further considering this token, in
167*38fd1498Szrj case we see a close paren immediately after the open
168*38fd1498Szrj paren. */
169*38fd1498Szrj if (m_state == 2)
170*38fd1498Szrj ++m_state;
171*38fd1498Szrj
172*38fd1498Szrj bool was_paste = m_last_was_paste;
173*38fd1498Szrj m_last_was_paste = false;
174*38fd1498Szrj if (token->type == CPP_PASTE)
175*38fd1498Szrj {
176*38fd1498Szrj m_last_was_paste = true;
177*38fd1498Szrj m_paste_location = token->src_loc;
178*38fd1498Szrj }
179*38fd1498Szrj else if (token->type == CPP_OPEN_PAREN)
180*38fd1498Szrj ++m_state;
181*38fd1498Szrj else if (token->type == CPP_CLOSE_PAREN)
182*38fd1498Szrj {
183*38fd1498Szrj --m_state;
184*38fd1498Szrj if (m_state == 2)
185*38fd1498Szrj {
186*38fd1498Szrj /* Saw the final paren. */
187*38fd1498Szrj m_state = 0;
188*38fd1498Szrj
189*38fd1498Szrj if (was_paste)
190*38fd1498Szrj {
191*38fd1498Szrj cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc,
192*38fd1498Szrj vaopt_paste_error);
193*38fd1498Szrj return ERROR;
194*38fd1498Szrj }
195*38fd1498Szrj
196*38fd1498Szrj return END;
197*38fd1498Szrj }
198*38fd1498Szrj }
199*38fd1498Szrj return m_allowed ? INCLUDE : DROP;
200*38fd1498Szrj }
201*38fd1498Szrj
202*38fd1498Szrj /* Nothing to do with __VA_OPT__. */
203*38fd1498Szrj return INCLUDE;
204*38fd1498Szrj }
205*38fd1498Szrj
206*38fd1498Szrj /* Ensure that any __VA_OPT__ was completed. If ok, return true.
207*38fd1498Szrj Otherwise, issue an error and return false. */
completed()208*38fd1498Szrj bool completed ()
209*38fd1498Szrj {
210*38fd1498Szrj if (m_variadic && m_state != 0)
211*38fd1498Szrj cpp_error_at (m_pfile, CPP_DL_ERROR, m_location,
212*38fd1498Szrj "unterminated __VA_OPT__");
213*38fd1498Szrj return m_state == 0;
214*38fd1498Szrj }
215*38fd1498Szrj
216*38fd1498Szrj private:
217*38fd1498Szrj
218*38fd1498Szrj /* The cpp_reader. */
219*38fd1498Szrj cpp_reader *m_pfile;
220*38fd1498Szrj
221*38fd1498Szrj /* True if there were varargs. */
222*38fd1498Szrj bool m_allowed;
223*38fd1498Szrj /* True if the macro is variadic. */
224*38fd1498Szrj bool m_variadic;
225*38fd1498Szrj /* If true, the previous token was ##. This is used to detect when
226*38fd1498Szrj a paste occurs at the end of the sequence. */
227*38fd1498Szrj bool m_last_was_paste;
228*38fd1498Szrj
229*38fd1498Szrj /* The state variable:
230*38fd1498Szrj 0 means not parsing
231*38fd1498Szrj 1 means __VA_OPT__ seen, looking for "("
232*38fd1498Szrj 2 means "(" seen (so the next token can't be "##")
233*38fd1498Szrj >= 3 means looking for ")", the number encodes the paren depth. */
234*38fd1498Szrj int m_state;
235*38fd1498Szrj
236*38fd1498Szrj /* The location of the paste token. */
237*38fd1498Szrj source_location m_paste_location;
238*38fd1498Szrj
239*38fd1498Szrj /* Location of the __VA_OPT__ token. */
240*38fd1498Szrj source_location m_location;
241*38fd1498Szrj };
242*38fd1498Szrj
243*38fd1498Szrj /* Macro expansion. */
244*38fd1498Szrj
245*38fd1498Szrj static int enter_macro_context (cpp_reader *, cpp_hashnode *,
246*38fd1498Szrj const cpp_token *, source_location);
247*38fd1498Szrj static int builtin_macro (cpp_reader *, cpp_hashnode *,
248*38fd1498Szrj source_location, source_location);
249*38fd1498Szrj static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
250*38fd1498Szrj const cpp_token **, unsigned int);
251*38fd1498Szrj static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *,
252*38fd1498Szrj _cpp_buff *, source_location *,
253*38fd1498Szrj const cpp_token **, unsigned int);
254*38fd1498Szrj static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *,
255*38fd1498Szrj _cpp_buff **, unsigned *);
256*38fd1498Szrj static cpp_context *next_context (cpp_reader *);
257*38fd1498Szrj static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
258*38fd1498Szrj static void expand_arg (cpp_reader *, macro_arg *);
259*38fd1498Szrj static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
260*38fd1498Szrj static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
261*38fd1498Szrj static void paste_all_tokens (cpp_reader *, const cpp_token *);
262*38fd1498Szrj static bool paste_tokens (cpp_reader *, source_location,
263*38fd1498Szrj const cpp_token **, const cpp_token *);
264*38fd1498Szrj static void alloc_expanded_arg_mem (cpp_reader *, macro_arg *, size_t);
265*38fd1498Szrj static void ensure_expanded_arg_room (cpp_reader *, macro_arg *, size_t, size_t *);
266*38fd1498Szrj static void delete_macro_args (_cpp_buff*, unsigned num_args);
267*38fd1498Szrj static void set_arg_token (macro_arg *, const cpp_token *,
268*38fd1498Szrj source_location, size_t,
269*38fd1498Szrj enum macro_arg_token_kind,
270*38fd1498Szrj bool);
271*38fd1498Szrj static const source_location *get_arg_token_location (const macro_arg *,
272*38fd1498Szrj enum macro_arg_token_kind);
273*38fd1498Szrj static const cpp_token **arg_token_ptr_at (const macro_arg *,
274*38fd1498Szrj size_t,
275*38fd1498Szrj enum macro_arg_token_kind,
276*38fd1498Szrj source_location **virt_location);
277*38fd1498Szrj
278*38fd1498Szrj static void macro_arg_token_iter_init (macro_arg_token_iter *, bool,
279*38fd1498Szrj enum macro_arg_token_kind,
280*38fd1498Szrj const macro_arg *,
281*38fd1498Szrj const cpp_token **);
282*38fd1498Szrj static const cpp_token *macro_arg_token_iter_get_token
283*38fd1498Szrj (const macro_arg_token_iter *it);
284*38fd1498Szrj static source_location macro_arg_token_iter_get_location
285*38fd1498Szrj (const macro_arg_token_iter *);
286*38fd1498Szrj static void macro_arg_token_iter_forward (macro_arg_token_iter *);
287*38fd1498Szrj static _cpp_buff *tokens_buff_new (cpp_reader *, size_t,
288*38fd1498Szrj source_location **);
289*38fd1498Szrj static size_t tokens_buff_count (_cpp_buff *);
290*38fd1498Szrj static const cpp_token **tokens_buff_last_token_ptr (_cpp_buff *);
291*38fd1498Szrj static inline const cpp_token **tokens_buff_put_token_to (const cpp_token **,
292*38fd1498Szrj source_location *,
293*38fd1498Szrj const cpp_token *,
294*38fd1498Szrj source_location,
295*38fd1498Szrj source_location,
296*38fd1498Szrj const line_map_macro *,
297*38fd1498Szrj unsigned int);
298*38fd1498Szrj
299*38fd1498Szrj static const cpp_token **tokens_buff_add_token (_cpp_buff *,
300*38fd1498Szrj source_location *,
301*38fd1498Szrj const cpp_token *,
302*38fd1498Szrj source_location,
303*38fd1498Szrj source_location,
304*38fd1498Szrj const line_map_macro *,
305*38fd1498Szrj unsigned int);
306*38fd1498Szrj static inline void tokens_buff_remove_last_token (_cpp_buff *);
307*38fd1498Szrj static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
308*38fd1498Szrj macro_arg *, source_location);
309*38fd1498Szrj static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *,
310*38fd1498Szrj _cpp_buff **, unsigned *);
311*38fd1498Szrj static bool create_iso_definition (cpp_reader *, cpp_macro *);
312*38fd1498Szrj
313*38fd1498Szrj /* #define directive parsing and handling. */
314*38fd1498Szrj
315*38fd1498Szrj static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *);
316*38fd1498Szrj static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *);
317*38fd1498Szrj static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *,
318*38fd1498Szrj const cpp_macro *);
319*38fd1498Szrj static bool parse_params (cpp_reader *, cpp_macro *);
320*38fd1498Szrj static void check_trad_stringification (cpp_reader *, const cpp_macro *,
321*38fd1498Szrj const cpp_string *);
322*38fd1498Szrj static bool reached_end_of_context (cpp_context *);
323*38fd1498Szrj static void consume_next_token_from_context (cpp_reader *pfile,
324*38fd1498Szrj const cpp_token **,
325*38fd1498Szrj source_location *);
326*38fd1498Szrj static const cpp_token* cpp_get_token_1 (cpp_reader *, source_location *);
327*38fd1498Szrj
328*38fd1498Szrj static cpp_hashnode* macro_of_context (cpp_context *context);
329*38fd1498Szrj
330*38fd1498Szrj static bool in_macro_expansion_p (cpp_reader *pfile);
331*38fd1498Szrj
332*38fd1498Szrj /* Statistical counter tracking the number of macros that got
333*38fd1498Szrj expanded. */
334*38fd1498Szrj unsigned num_expanded_macros_counter = 0;
335*38fd1498Szrj /* Statistical counter tracking the total number tokens resulting
336*38fd1498Szrj from macro expansion. */
337*38fd1498Szrj unsigned num_macro_tokens_counter = 0;
338*38fd1498Szrj
339*38fd1498Szrj /* Emits a warning if NODE is a macro defined in the main file that
340*38fd1498Szrj has not been used. */
341*38fd1498Szrj int
_cpp_warn_if_unused_macro(cpp_reader * pfile,cpp_hashnode * node,void * v ATTRIBUTE_UNUSED)342*38fd1498Szrj _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
343*38fd1498Szrj void *v ATTRIBUTE_UNUSED)
344*38fd1498Szrj {
345*38fd1498Szrj if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
346*38fd1498Szrj {
347*38fd1498Szrj cpp_macro *macro = node->value.macro;
348*38fd1498Szrj
349*38fd1498Szrj if (!macro->used
350*38fd1498Szrj && MAIN_FILE_P (linemap_check_ordinary
351*38fd1498Szrj (linemap_lookup (pfile->line_table,
352*38fd1498Szrj macro->line))))
353*38fd1498Szrj cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0,
354*38fd1498Szrj "macro \"%s\" is not used", NODE_NAME (node));
355*38fd1498Szrj }
356*38fd1498Szrj
357*38fd1498Szrj return 1;
358*38fd1498Szrj }
359*38fd1498Szrj
360*38fd1498Szrj /* Allocates and returns a CPP_STRING token, containing TEXT of length
361*38fd1498Szrj LEN, after null-terminating it. TEXT must be in permanent storage. */
362*38fd1498Szrj static const cpp_token *
new_string_token(cpp_reader * pfile,unsigned char * text,unsigned int len)363*38fd1498Szrj new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len)
364*38fd1498Szrj {
365*38fd1498Szrj cpp_token *token = _cpp_temp_token (pfile);
366*38fd1498Szrj
367*38fd1498Szrj text[len] = '\0';
368*38fd1498Szrj token->type = CPP_STRING;
369*38fd1498Szrj token->val.str.len = len;
370*38fd1498Szrj token->val.str.text = text;
371*38fd1498Szrj token->flags = 0;
372*38fd1498Szrj return token;
373*38fd1498Szrj }
374*38fd1498Szrj
375*38fd1498Szrj static const char * const monthnames[] =
376*38fd1498Szrj {
377*38fd1498Szrj "Jan", "Feb", "Mar", "Apr", "May", "Jun",
378*38fd1498Szrj "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
379*38fd1498Szrj };
380*38fd1498Szrj
381*38fd1498Szrj /* Helper function for builtin_macro. Returns the text generated by
382*38fd1498Szrj a builtin macro. */
383*38fd1498Szrj const uchar *
_cpp_builtin_macro_text(cpp_reader * pfile,cpp_hashnode * node,source_location loc)384*38fd1498Szrj _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node,
385*38fd1498Szrj source_location loc)
386*38fd1498Szrj {
387*38fd1498Szrj const uchar *result = NULL;
388*38fd1498Szrj linenum_type number = 1;
389*38fd1498Szrj
390*38fd1498Szrj switch (node->value.builtin)
391*38fd1498Szrj {
392*38fd1498Szrj default:
393*38fd1498Szrj cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
394*38fd1498Szrj NODE_NAME (node));
395*38fd1498Szrj break;
396*38fd1498Szrj
397*38fd1498Szrj case BT_TIMESTAMP:
398*38fd1498Szrj {
399*38fd1498Szrj if (CPP_OPTION (pfile, warn_date_time))
400*38fd1498Szrj cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent "
401*38fd1498Szrj "reproducible builds", NODE_NAME (node));
402*38fd1498Szrj
403*38fd1498Szrj cpp_buffer *pbuffer = cpp_get_buffer (pfile);
404*38fd1498Szrj if (pbuffer->timestamp == NULL)
405*38fd1498Szrj {
406*38fd1498Szrj /* Initialize timestamp value of the assotiated file. */
407*38fd1498Szrj struct _cpp_file *file = cpp_get_file (pbuffer);
408*38fd1498Szrj if (file)
409*38fd1498Szrj {
410*38fd1498Szrj /* Generate __TIMESTAMP__ string, that represents
411*38fd1498Szrj the date and time of the last modification
412*38fd1498Szrj of the current source file. The string constant
413*38fd1498Szrj looks like "Sun Sep 16 01:03:52 1973". */
414*38fd1498Szrj struct tm *tb = NULL;
415*38fd1498Szrj struct stat *st = _cpp_get_file_stat (file);
416*38fd1498Szrj if (st)
417*38fd1498Szrj tb = localtime (&st->st_mtime);
418*38fd1498Szrj if (tb)
419*38fd1498Szrj {
420*38fd1498Szrj char *str = asctime (tb);
421*38fd1498Szrj size_t len = strlen (str);
422*38fd1498Szrj unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2);
423*38fd1498Szrj buf[0] = '"';
424*38fd1498Szrj strcpy ((char *) buf + 1, str);
425*38fd1498Szrj buf[len] = '"';
426*38fd1498Szrj pbuffer->timestamp = buf;
427*38fd1498Szrj }
428*38fd1498Szrj else
429*38fd1498Szrj {
430*38fd1498Szrj cpp_errno (pfile, CPP_DL_WARNING,
431*38fd1498Szrj "could not determine file timestamp");
432*38fd1498Szrj pbuffer->timestamp = UC"\"??? ??? ?? ??:??:?? ????\"";
433*38fd1498Szrj }
434*38fd1498Szrj }
435*38fd1498Szrj }
436*38fd1498Szrj result = pbuffer->timestamp;
437*38fd1498Szrj }
438*38fd1498Szrj break;
439*38fd1498Szrj case BT_FILE:
440*38fd1498Szrj case BT_BASE_FILE:
441*38fd1498Szrj {
442*38fd1498Szrj unsigned int len;
443*38fd1498Szrj const char *name;
444*38fd1498Szrj uchar *buf;
445*38fd1498Szrj
446*38fd1498Szrj if (node->value.builtin == BT_FILE)
447*38fd1498Szrj name = linemap_get_expansion_filename (pfile->line_table,
448*38fd1498Szrj pfile->line_table->highest_line);
449*38fd1498Szrj else
450*38fd1498Szrj {
451*38fd1498Szrj name = _cpp_get_file_name (pfile->main_file);
452*38fd1498Szrj if (!name)
453*38fd1498Szrj abort ();
454*38fd1498Szrj }
455*38fd1498Szrj if (pfile->cb.remap_filename)
456*38fd1498Szrj name = pfile->cb.remap_filename (name);
457*38fd1498Szrj len = strlen (name);
458*38fd1498Szrj buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
459*38fd1498Szrj result = buf;
460*38fd1498Szrj *buf = '"';
461*38fd1498Szrj buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
462*38fd1498Szrj *buf++ = '"';
463*38fd1498Szrj *buf = '\0';
464*38fd1498Szrj }
465*38fd1498Szrj break;
466*38fd1498Szrj
467*38fd1498Szrj case BT_INCLUDE_LEVEL:
468*38fd1498Szrj /* The line map depth counts the primary source as level 1, but
469*38fd1498Szrj historically __INCLUDE_DEPTH__ has called the primary source
470*38fd1498Szrj level 0. */
471*38fd1498Szrj number = pfile->line_table->depth - 1;
472*38fd1498Szrj break;
473*38fd1498Szrj
474*38fd1498Szrj case BT_SPECLINE:
475*38fd1498Szrj /* If __LINE__ is embedded in a macro, it must expand to the
476*38fd1498Szrj line of the macro's invocation, not its definition.
477*38fd1498Szrj Otherwise things like assert() will not work properly.
478*38fd1498Szrj See WG14 N1911, WG21 N4220 sec 6.5, and PR 61861. */
479*38fd1498Szrj if (CPP_OPTION (pfile, traditional))
480*38fd1498Szrj loc = pfile->line_table->highest_line;
481*38fd1498Szrj else
482*38fd1498Szrj loc = linemap_resolve_location (pfile->line_table, loc,
483*38fd1498Szrj LRK_MACRO_EXPANSION_POINT, NULL);
484*38fd1498Szrj number = linemap_get_expansion_line (pfile->line_table, loc);
485*38fd1498Szrj break;
486*38fd1498Szrj
487*38fd1498Szrj /* __STDC__ has the value 1 under normal circumstances.
488*38fd1498Szrj However, if (a) we are in a system header, (b) the option
489*38fd1498Szrj stdc_0_in_system_headers is true (set by target config), and
490*38fd1498Szrj (c) we are not in strictly conforming mode, then it has the
491*38fd1498Szrj value 0. (b) and (c) are already checked in cpp_init_builtins. */
492*38fd1498Szrj case BT_STDC:
493*38fd1498Szrj if (cpp_in_system_header (pfile))
494*38fd1498Szrj number = 0;
495*38fd1498Szrj else
496*38fd1498Szrj number = 1;
497*38fd1498Szrj break;
498*38fd1498Szrj
499*38fd1498Szrj case BT_DATE:
500*38fd1498Szrj case BT_TIME:
501*38fd1498Szrj if (CPP_OPTION (pfile, warn_date_time))
502*38fd1498Szrj cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent "
503*38fd1498Szrj "reproducible builds", NODE_NAME (node));
504*38fd1498Szrj if (pfile->date == NULL)
505*38fd1498Szrj {
506*38fd1498Szrj /* Allocate __DATE__ and __TIME__ strings from permanent
507*38fd1498Szrj storage. We only do this once, and don't generate them
508*38fd1498Szrj at init time, because time() and localtime() are very
509*38fd1498Szrj slow on some systems. */
510*38fd1498Szrj time_t tt;
511*38fd1498Szrj struct tm *tb = NULL;
512*38fd1498Szrj
513*38fd1498Szrj /* Set a reproducible timestamp for __DATE__ and __TIME__ macro
514*38fd1498Szrj if SOURCE_DATE_EPOCH is defined. */
515*38fd1498Szrj if (pfile->source_date_epoch == (time_t) -2
516*38fd1498Szrj && pfile->cb.get_source_date_epoch != NULL)
517*38fd1498Szrj pfile->source_date_epoch = pfile->cb.get_source_date_epoch (pfile);
518*38fd1498Szrj
519*38fd1498Szrj if (pfile->source_date_epoch >= (time_t) 0)
520*38fd1498Szrj tb = gmtime (&pfile->source_date_epoch);
521*38fd1498Szrj else
522*38fd1498Szrj {
523*38fd1498Szrj /* (time_t) -1 is a legitimate value for "number of seconds
524*38fd1498Szrj since the Epoch", so we have to do a little dance to
525*38fd1498Szrj distinguish that from a genuine error. */
526*38fd1498Szrj errno = 0;
527*38fd1498Szrj tt = time (NULL);
528*38fd1498Szrj if (tt != (time_t)-1 || errno == 0)
529*38fd1498Szrj tb = localtime (&tt);
530*38fd1498Szrj }
531*38fd1498Szrj
532*38fd1498Szrj if (tb)
533*38fd1498Szrj {
534*38fd1498Szrj pfile->date = _cpp_unaligned_alloc (pfile,
535*38fd1498Szrj sizeof ("\"Oct 11 1347\""));
536*38fd1498Szrj sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
537*38fd1498Szrj monthnames[tb->tm_mon], tb->tm_mday,
538*38fd1498Szrj tb->tm_year + 1900);
539*38fd1498Szrj
540*38fd1498Szrj pfile->time = _cpp_unaligned_alloc (pfile,
541*38fd1498Szrj sizeof ("\"12:34:56\""));
542*38fd1498Szrj sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
543*38fd1498Szrj tb->tm_hour, tb->tm_min, tb->tm_sec);
544*38fd1498Szrj }
545*38fd1498Szrj else
546*38fd1498Szrj {
547*38fd1498Szrj cpp_errno (pfile, CPP_DL_WARNING,
548*38fd1498Szrj "could not determine date and time");
549*38fd1498Szrj
550*38fd1498Szrj pfile->date = UC"\"??? ?? ????\"";
551*38fd1498Szrj pfile->time = UC"\"??:??:??\"";
552*38fd1498Szrj }
553*38fd1498Szrj }
554*38fd1498Szrj
555*38fd1498Szrj if (node->value.builtin == BT_DATE)
556*38fd1498Szrj result = pfile->date;
557*38fd1498Szrj else
558*38fd1498Szrj result = pfile->time;
559*38fd1498Szrj break;
560*38fd1498Szrj
561*38fd1498Szrj case BT_COUNTER:
562*38fd1498Szrj if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive)
563*38fd1498Szrj cpp_error (pfile, CPP_DL_ERROR,
564*38fd1498Szrj "__COUNTER__ expanded inside directive with -fdirectives-only");
565*38fd1498Szrj number = pfile->counter++;
566*38fd1498Szrj break;
567*38fd1498Szrj
568*38fd1498Szrj case BT_HAS_ATTRIBUTE:
569*38fd1498Szrj number = pfile->cb.has_attribute (pfile);
570*38fd1498Szrj break;
571*38fd1498Szrj }
572*38fd1498Szrj
573*38fd1498Szrj if (result == NULL)
574*38fd1498Szrj {
575*38fd1498Szrj /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
576*38fd1498Szrj result = _cpp_unaligned_alloc (pfile, 21);
577*38fd1498Szrj sprintf ((char *) result, "%u", number);
578*38fd1498Szrj }
579*38fd1498Szrj
580*38fd1498Szrj return result;
581*38fd1498Szrj }
582*38fd1498Szrj
583*38fd1498Szrj /* Convert builtin macros like __FILE__ to a token and push it on the
584*38fd1498Szrj context stack. Also handles _Pragma, for which a new token may not
585*38fd1498Szrj be created. Returns 1 if it generates a new token context, 0 to
586*38fd1498Szrj return the token to the caller. LOC is the location of the expansion
587*38fd1498Szrj point of the macro. */
588*38fd1498Szrj static int
builtin_macro(cpp_reader * pfile,cpp_hashnode * node,source_location loc,source_location expand_loc)589*38fd1498Szrj builtin_macro (cpp_reader *pfile, cpp_hashnode *node,
590*38fd1498Szrj source_location loc, source_location expand_loc)
591*38fd1498Szrj {
592*38fd1498Szrj const uchar *buf;
593*38fd1498Szrj size_t len;
594*38fd1498Szrj char *nbuf;
595*38fd1498Szrj
596*38fd1498Szrj if (node->value.builtin == BT_PRAGMA)
597*38fd1498Szrj {
598*38fd1498Szrj /* Don't interpret _Pragma within directives. The standard is
599*38fd1498Szrj not clear on this, but to me this makes most sense. */
600*38fd1498Szrj if (pfile->state.in_directive)
601*38fd1498Szrj return 0;
602*38fd1498Szrj
603*38fd1498Szrj return _cpp_do__Pragma (pfile, loc);
604*38fd1498Szrj }
605*38fd1498Szrj
606*38fd1498Szrj buf = _cpp_builtin_macro_text (pfile, node, expand_loc);
607*38fd1498Szrj len = ustrlen (buf);
608*38fd1498Szrj nbuf = (char *) alloca (len + 1);
609*38fd1498Szrj memcpy (nbuf, buf, len);
610*38fd1498Szrj nbuf[len]='\n';
611*38fd1498Szrj
612*38fd1498Szrj cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
613*38fd1498Szrj _cpp_clean_line (pfile);
614*38fd1498Szrj
615*38fd1498Szrj /* Set pfile->cur_token as required by _cpp_lex_direct. */
616*38fd1498Szrj pfile->cur_token = _cpp_temp_token (pfile);
617*38fd1498Szrj cpp_token *token = _cpp_lex_direct (pfile);
618*38fd1498Szrj /* We should point to the expansion point of the builtin macro. */
619*38fd1498Szrj token->src_loc = loc;
620*38fd1498Szrj if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
621*38fd1498Szrj {
622*38fd1498Szrj /* We are tracking tokens resulting from macro expansion.
623*38fd1498Szrj Create a macro line map and generate a virtual location for
624*38fd1498Szrj the token resulting from the expansion of the built-in
625*38fd1498Szrj macro. */
626*38fd1498Szrj source_location *virt_locs = NULL;
627*38fd1498Szrj _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
628*38fd1498Szrj const line_map_macro * map =
629*38fd1498Szrj linemap_enter_macro (pfile->line_table, node, loc, 1);
630*38fd1498Szrj tokens_buff_add_token (token_buf, virt_locs, token,
631*38fd1498Szrj pfile->line_table->builtin_location,
632*38fd1498Szrj pfile->line_table->builtin_location,
633*38fd1498Szrj map, /*macro_token_index=*/0);
634*38fd1498Szrj push_extended_tokens_context (pfile, node, token_buf, virt_locs,
635*38fd1498Szrj (const cpp_token **)token_buf->base,
636*38fd1498Szrj 1);
637*38fd1498Szrj }
638*38fd1498Szrj else
639*38fd1498Szrj _cpp_push_token_context (pfile, NULL, token, 1);
640*38fd1498Szrj if (pfile->buffer->cur != pfile->buffer->rlimit)
641*38fd1498Szrj cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
642*38fd1498Szrj NODE_NAME (node));
643*38fd1498Szrj _cpp_pop_buffer (pfile);
644*38fd1498Szrj
645*38fd1498Szrj return 1;
646*38fd1498Szrj }
647*38fd1498Szrj
648*38fd1498Szrj /* Copies SRC, of length LEN, to DEST, adding backslashes before all
649*38fd1498Szrj backslashes and double quotes. DEST must be of sufficient size.
650*38fd1498Szrj Returns a pointer to the end of the string. */
651*38fd1498Szrj uchar *
cpp_quote_string(uchar * dest,const uchar * src,unsigned int len)652*38fd1498Szrj cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
653*38fd1498Szrj {
654*38fd1498Szrj while (len--)
655*38fd1498Szrj {
656*38fd1498Szrj uchar c = *src++;
657*38fd1498Szrj
658*38fd1498Szrj switch (c)
659*38fd1498Szrj {
660*38fd1498Szrj case '\n':
661*38fd1498Szrj /* Naked LF can appear in raw string literals */
662*38fd1498Szrj c = 'n';
663*38fd1498Szrj /* FALLTHROUGH */
664*38fd1498Szrj
665*38fd1498Szrj case '\\':
666*38fd1498Szrj case '"':
667*38fd1498Szrj *dest++ = '\\';
668*38fd1498Szrj /* FALLTHROUGH */
669*38fd1498Szrj
670*38fd1498Szrj default:
671*38fd1498Szrj *dest++ = c;
672*38fd1498Szrj }
673*38fd1498Szrj }
674*38fd1498Szrj
675*38fd1498Szrj return dest;
676*38fd1498Szrj }
677*38fd1498Szrj
678*38fd1498Szrj /* Convert a token sequence ARG to a single string token according to
679*38fd1498Szrj the rules of the ISO C #-operator. */
680*38fd1498Szrj static const cpp_token *
stringify_arg(cpp_reader * pfile,macro_arg * arg)681*38fd1498Szrj stringify_arg (cpp_reader *pfile, macro_arg *arg)
682*38fd1498Szrj {
683*38fd1498Szrj unsigned char *dest;
684*38fd1498Szrj unsigned int i, escape_it, backslash_count = 0;
685*38fd1498Szrj const cpp_token *source = NULL;
686*38fd1498Szrj size_t len;
687*38fd1498Szrj
688*38fd1498Szrj if (BUFF_ROOM (pfile->u_buff) < 3)
689*38fd1498Szrj _cpp_extend_buff (pfile, &pfile->u_buff, 3);
690*38fd1498Szrj dest = BUFF_FRONT (pfile->u_buff);
691*38fd1498Szrj *dest++ = '"';
692*38fd1498Szrj
693*38fd1498Szrj /* Loop, reading in the argument's tokens. */
694*38fd1498Szrj for (i = 0; i < arg->count; i++)
695*38fd1498Szrj {
696*38fd1498Szrj const cpp_token *token = arg->first[i];
697*38fd1498Szrj
698*38fd1498Szrj if (token->type == CPP_PADDING)
699*38fd1498Szrj {
700*38fd1498Szrj if (source == NULL
701*38fd1498Szrj || (!(source->flags & PREV_WHITE)
702*38fd1498Szrj && token->val.source == NULL))
703*38fd1498Szrj source = token->val.source;
704*38fd1498Szrj continue;
705*38fd1498Szrj }
706*38fd1498Szrj
707*38fd1498Szrj escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR
708*38fd1498Szrj || token->type == CPP_WSTRING || token->type == CPP_WCHAR
709*38fd1498Szrj || token->type == CPP_STRING32 || token->type == CPP_CHAR32
710*38fd1498Szrj || token->type == CPP_STRING16 || token->type == CPP_CHAR16
711*38fd1498Szrj || token->type == CPP_UTF8STRING || token->type == CPP_UTF8CHAR
712*38fd1498Szrj || cpp_userdef_string_p (token->type)
713*38fd1498Szrj || cpp_userdef_char_p (token->type));
714*38fd1498Szrj
715*38fd1498Szrj /* Room for each char being written in octal, initial space and
716*38fd1498Szrj final quote and NUL. */
717*38fd1498Szrj len = cpp_token_len (token);
718*38fd1498Szrj if (escape_it)
719*38fd1498Szrj len *= 4;
720*38fd1498Szrj len += 3;
721*38fd1498Szrj
722*38fd1498Szrj if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
723*38fd1498Szrj {
724*38fd1498Szrj size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
725*38fd1498Szrj _cpp_extend_buff (pfile, &pfile->u_buff, len);
726*38fd1498Szrj dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
727*38fd1498Szrj }
728*38fd1498Szrj
729*38fd1498Szrj /* Leading white space? */
730*38fd1498Szrj if (dest - 1 != BUFF_FRONT (pfile->u_buff))
731*38fd1498Szrj {
732*38fd1498Szrj if (source == NULL)
733*38fd1498Szrj source = token;
734*38fd1498Szrj if (source->flags & PREV_WHITE)
735*38fd1498Szrj *dest++ = ' ';
736*38fd1498Szrj }
737*38fd1498Szrj source = NULL;
738*38fd1498Szrj
739*38fd1498Szrj if (escape_it)
740*38fd1498Szrj {
741*38fd1498Szrj _cpp_buff *buff = _cpp_get_buff (pfile, len);
742*38fd1498Szrj unsigned char *buf = BUFF_FRONT (buff);
743*38fd1498Szrj len = cpp_spell_token (pfile, token, buf, true) - buf;
744*38fd1498Szrj dest = cpp_quote_string (dest, buf, len);
745*38fd1498Szrj _cpp_release_buff (pfile, buff);
746*38fd1498Szrj }
747*38fd1498Szrj else
748*38fd1498Szrj dest = cpp_spell_token (pfile, token, dest, true);
749*38fd1498Szrj
750*38fd1498Szrj if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
751*38fd1498Szrj backslash_count++;
752*38fd1498Szrj else
753*38fd1498Szrj backslash_count = 0;
754*38fd1498Szrj }
755*38fd1498Szrj
756*38fd1498Szrj /* Ignore the final \ of invalid string literals. */
757*38fd1498Szrj if (backslash_count & 1)
758*38fd1498Szrj {
759*38fd1498Szrj cpp_error (pfile, CPP_DL_WARNING,
760*38fd1498Szrj "invalid string literal, ignoring final '\\'");
761*38fd1498Szrj dest--;
762*38fd1498Szrj }
763*38fd1498Szrj
764*38fd1498Szrj /* Commit the memory, including NUL, and return the token. */
765*38fd1498Szrj *dest++ = '"';
766*38fd1498Szrj len = dest - BUFF_FRONT (pfile->u_buff);
767*38fd1498Szrj BUFF_FRONT (pfile->u_buff) = dest + 1;
768*38fd1498Szrj return new_string_token (pfile, dest - len, len);
769*38fd1498Szrj }
770*38fd1498Szrj
771*38fd1498Szrj /* Try to paste two tokens. On success, return nonzero. In any
772*38fd1498Szrj case, PLHS is updated to point to the pasted token, which is
773*38fd1498Szrj guaranteed to not have the PASTE_LEFT flag set. LOCATION is
774*38fd1498Szrj the virtual location used for error reporting. */
775*38fd1498Szrj static bool
paste_tokens(cpp_reader * pfile,source_location location,const cpp_token ** plhs,const cpp_token * rhs)776*38fd1498Szrj paste_tokens (cpp_reader *pfile, source_location location,
777*38fd1498Szrj const cpp_token **plhs, const cpp_token *rhs)
778*38fd1498Szrj {
779*38fd1498Szrj unsigned char *buf, *end, *lhsend;
780*38fd1498Szrj cpp_token *lhs;
781*38fd1498Szrj unsigned int len;
782*38fd1498Szrj
783*38fd1498Szrj len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1;
784*38fd1498Szrj buf = (unsigned char *) alloca (len);
785*38fd1498Szrj end = lhsend = cpp_spell_token (pfile, *plhs, buf, true);
786*38fd1498Szrj
787*38fd1498Szrj /* Avoid comment headers, since they are still processed in stage 3.
788*38fd1498Szrj It is simpler to insert a space here, rather than modifying the
789*38fd1498Szrj lexer to ignore comments in some circumstances. Simply returning
790*38fd1498Szrj false doesn't work, since we want to clear the PASTE_LEFT flag. */
791*38fd1498Szrj if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ)
792*38fd1498Szrj *end++ = ' ';
793*38fd1498Szrj /* In one obscure case we might see padding here. */
794*38fd1498Szrj if (rhs->type != CPP_PADDING)
795*38fd1498Szrj end = cpp_spell_token (pfile, rhs, end, true);
796*38fd1498Szrj *end = '\n';
797*38fd1498Szrj
798*38fd1498Szrj cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
799*38fd1498Szrj _cpp_clean_line (pfile);
800*38fd1498Szrj
801*38fd1498Szrj /* Set pfile->cur_token as required by _cpp_lex_direct. */
802*38fd1498Szrj pfile->cur_token = _cpp_temp_token (pfile);
803*38fd1498Szrj lhs = _cpp_lex_direct (pfile);
804*38fd1498Szrj if (pfile->buffer->cur != pfile->buffer->rlimit)
805*38fd1498Szrj {
806*38fd1498Szrj source_location saved_loc = lhs->src_loc;
807*38fd1498Szrj
808*38fd1498Szrj _cpp_pop_buffer (pfile);
809*38fd1498Szrj _cpp_backup_tokens (pfile, 1);
810*38fd1498Szrj *lhsend = '\0';
811*38fd1498Szrj
812*38fd1498Szrj /* We have to remove the PASTE_LEFT flag from the old lhs, but
813*38fd1498Szrj we want to keep the new location. */
814*38fd1498Szrj *lhs = **plhs;
815*38fd1498Szrj *plhs = lhs;
816*38fd1498Szrj lhs->src_loc = saved_loc;
817*38fd1498Szrj lhs->flags &= ~PASTE_LEFT;
818*38fd1498Szrj
819*38fd1498Szrj /* Mandatory error for all apart from assembler. */
820*38fd1498Szrj if (CPP_OPTION (pfile, lang) != CLK_ASM)
821*38fd1498Szrj cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
822*38fd1498Szrj "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
823*38fd1498Szrj buf, cpp_token_as_text (pfile, rhs));
824*38fd1498Szrj return false;
825*38fd1498Szrj }
826*38fd1498Szrj
827*38fd1498Szrj *plhs = lhs;
828*38fd1498Szrj _cpp_pop_buffer (pfile);
829*38fd1498Szrj return true;
830*38fd1498Szrj }
831*38fd1498Szrj
832*38fd1498Szrj /* Handles an arbitrarily long sequence of ## operators, with initial
833*38fd1498Szrj operand LHS. This implementation is left-associative,
834*38fd1498Szrj non-recursive, and finishes a paste before handling succeeding
835*38fd1498Szrj ones. If a paste fails, we back up to the RHS of the failing ##
836*38fd1498Szrj operator before pushing the context containing the result of prior
837*38fd1498Szrj successful pastes, with the effect that the RHS appears in the
838*38fd1498Szrj output stream after the pasted LHS normally. */
839*38fd1498Szrj static void
paste_all_tokens(cpp_reader * pfile,const cpp_token * lhs)840*38fd1498Szrj paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
841*38fd1498Szrj {
842*38fd1498Szrj const cpp_token *rhs = NULL;
843*38fd1498Szrj cpp_context *context = pfile->context;
844*38fd1498Szrj source_location virt_loc = 0;
845*38fd1498Szrj
846*38fd1498Szrj /* We are expanding a macro and we must have been called on a token
847*38fd1498Szrj that appears at the left hand side of a ## operator. */
848*38fd1498Szrj if (macro_of_context (pfile->context) == NULL
849*38fd1498Szrj || (!(lhs->flags & PASTE_LEFT)))
850*38fd1498Szrj abort ();
851*38fd1498Szrj
852*38fd1498Szrj if (context->tokens_kind == TOKENS_KIND_EXTENDED)
853*38fd1498Szrj /* The caller must have called consume_next_token_from_context
854*38fd1498Szrj right before calling us. That has incremented the pointer to
855*38fd1498Szrj the current virtual location. So it now points to the location
856*38fd1498Szrj of the token that comes right after *LHS. We want the
857*38fd1498Szrj resulting pasted token to have the location of the current
858*38fd1498Szrj *LHS, though. */
859*38fd1498Szrj virt_loc = context->c.mc->cur_virt_loc[-1];
860*38fd1498Szrj else
861*38fd1498Szrj /* We are not tracking macro expansion. So the best virtual
862*38fd1498Szrj location we can get here is the expansion point of the macro we
863*38fd1498Szrj are currently expanding. */
864*38fd1498Szrj virt_loc = pfile->invocation_location;
865*38fd1498Szrj
866*38fd1498Szrj do
867*38fd1498Szrj {
868*38fd1498Szrj /* Take the token directly from the current context. We can do
869*38fd1498Szrj this, because we are in the replacement list of either an
870*38fd1498Szrj object-like macro, or a function-like macro with arguments
871*38fd1498Szrj inserted. In either case, the constraints to #define
872*38fd1498Szrj guarantee we have at least one more token. */
873*38fd1498Szrj if (context->tokens_kind == TOKENS_KIND_DIRECT)
874*38fd1498Szrj rhs = FIRST (context).token++;
875*38fd1498Szrj else if (context->tokens_kind == TOKENS_KIND_INDIRECT)
876*38fd1498Szrj rhs = *FIRST (context).ptoken++;
877*38fd1498Szrj else if (context->tokens_kind == TOKENS_KIND_EXTENDED)
878*38fd1498Szrj {
879*38fd1498Szrj /* So we are in presence of an extended token context, which
880*38fd1498Szrj means that each token in this context has a virtual
881*38fd1498Szrj location attached to it. So let's not forget to update
882*38fd1498Szrj the pointer to the current virtual location of the
883*38fd1498Szrj current token when we update the pointer to the current
884*38fd1498Szrj token */
885*38fd1498Szrj
886*38fd1498Szrj rhs = *FIRST (context).ptoken++;
887*38fd1498Szrj /* context->c.mc must be non-null, as if we were not in a
888*38fd1498Szrj macro context, context->tokens_kind could not be equal to
889*38fd1498Szrj TOKENS_KIND_EXTENDED. */
890*38fd1498Szrj context->c.mc->cur_virt_loc++;
891*38fd1498Szrj }
892*38fd1498Szrj
893*38fd1498Szrj if (rhs->type == CPP_PADDING)
894*38fd1498Szrj {
895*38fd1498Szrj if (rhs->flags & PASTE_LEFT)
896*38fd1498Szrj abort ();
897*38fd1498Szrj }
898*38fd1498Szrj if (!paste_tokens (pfile, virt_loc, &lhs, rhs))
899*38fd1498Szrj break;
900*38fd1498Szrj }
901*38fd1498Szrj while (rhs->flags & PASTE_LEFT);
902*38fd1498Szrj
903*38fd1498Szrj /* Put the resulting token in its own context. */
904*38fd1498Szrj if (context->tokens_kind == TOKENS_KIND_EXTENDED)
905*38fd1498Szrj {
906*38fd1498Szrj source_location *virt_locs = NULL;
907*38fd1498Szrj _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
908*38fd1498Szrj tokens_buff_add_token (token_buf, virt_locs, lhs,
909*38fd1498Szrj virt_loc, 0, NULL, 0);
910*38fd1498Szrj push_extended_tokens_context (pfile, context->c.mc->macro_node,
911*38fd1498Szrj token_buf, virt_locs,
912*38fd1498Szrj (const cpp_token **)token_buf->base, 1);
913*38fd1498Szrj }
914*38fd1498Szrj else
915*38fd1498Szrj _cpp_push_token_context (pfile, NULL, lhs, 1);
916*38fd1498Szrj }
917*38fd1498Szrj
918*38fd1498Szrj /* Returns TRUE if the number of arguments ARGC supplied in an
919*38fd1498Szrj invocation of the MACRO referenced by NODE is valid. An empty
920*38fd1498Szrj invocation to a macro with no parameters should pass ARGC as zero.
921*38fd1498Szrj
922*38fd1498Szrj Note that MACRO cannot necessarily be deduced from NODE, in case
923*38fd1498Szrj NODE was redefined whilst collecting arguments. */
924*38fd1498Szrj bool
_cpp_arguments_ok(cpp_reader * pfile,cpp_macro * macro,const cpp_hashnode * node,unsigned int argc)925*38fd1498Szrj _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
926*38fd1498Szrj {
927*38fd1498Szrj if (argc == macro->paramc)
928*38fd1498Szrj return true;
929*38fd1498Szrj
930*38fd1498Szrj if (argc < macro->paramc)
931*38fd1498Szrj {
932*38fd1498Szrj /* In C++2a (here the va_opt flag is used), and also as a GNU
933*38fd1498Szrj extension, variadic arguments are allowed to not appear in
934*38fd1498Szrj the invocation at all.
935*38fd1498Szrj e.g. #define debug(format, args...) something
936*38fd1498Szrj debug("string");
937*38fd1498Szrj
938*38fd1498Szrj This is exactly the same as if an empty variadic list had been
939*38fd1498Szrj supplied - debug("string", ). */
940*38fd1498Szrj
941*38fd1498Szrj if (argc + 1 == macro->paramc && macro->variadic)
942*38fd1498Szrj {
943*38fd1498Szrj if (CPP_PEDANTIC (pfile) && ! macro->syshdr
944*38fd1498Szrj && ! CPP_OPTION (pfile, va_opt))
945*38fd1498Szrj {
946*38fd1498Szrj if (CPP_OPTION (pfile, cplusplus))
947*38fd1498Szrj cpp_error (pfile, CPP_DL_PEDWARN,
948*38fd1498Szrj "ISO C++11 requires at least one argument "
949*38fd1498Szrj "for the \"...\" in a variadic macro");
950*38fd1498Szrj else
951*38fd1498Szrj cpp_error (pfile, CPP_DL_PEDWARN,
952*38fd1498Szrj "ISO C99 requires at least one argument "
953*38fd1498Szrj "for the \"...\" in a variadic macro");
954*38fd1498Szrj }
955*38fd1498Szrj return true;
956*38fd1498Szrj }
957*38fd1498Szrj
958*38fd1498Szrj cpp_error (pfile, CPP_DL_ERROR,
959*38fd1498Szrj "macro \"%s\" requires %u arguments, but only %u given",
960*38fd1498Szrj NODE_NAME (node), macro->paramc, argc);
961*38fd1498Szrj }
962*38fd1498Szrj else
963*38fd1498Szrj cpp_error (pfile, CPP_DL_ERROR,
964*38fd1498Szrj "macro \"%s\" passed %u arguments, but takes just %u",
965*38fd1498Szrj NODE_NAME (node), argc, macro->paramc);
966*38fd1498Szrj
967*38fd1498Szrj return false;
968*38fd1498Szrj }
969*38fd1498Szrj
970*38fd1498Szrj /* Reads and returns the arguments to a function-like macro
971*38fd1498Szrj invocation. Assumes the opening parenthesis has been processed.
972*38fd1498Szrj If there is an error, emits an appropriate diagnostic and returns
973*38fd1498Szrj NULL. Each argument is terminated by a CPP_EOF token, for the
974*38fd1498Szrj future benefit of expand_arg(). If there are any deferred
975*38fd1498Szrj #pragma directives among macro arguments, store pointers to the
976*38fd1498Szrj CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.
977*38fd1498Szrj
978*38fd1498Szrj What is returned is the buffer that contains the memory allocated
979*38fd1498Szrj to hold the macro arguments. NODE is the name of the macro this
980*38fd1498Szrj function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is
981*38fd1498Szrj set to the actual number of macro arguments allocated in the
982*38fd1498Szrj returned buffer. */
983*38fd1498Szrj static _cpp_buff *
collect_args(cpp_reader * pfile,const cpp_hashnode * node,_cpp_buff ** pragma_buff,unsigned * num_args)984*38fd1498Szrj collect_args (cpp_reader *pfile, const cpp_hashnode *node,
985*38fd1498Szrj _cpp_buff **pragma_buff, unsigned *num_args)
986*38fd1498Szrj {
987*38fd1498Szrj _cpp_buff *buff, *base_buff;
988*38fd1498Szrj cpp_macro *macro;
989*38fd1498Szrj macro_arg *args, *arg;
990*38fd1498Szrj const cpp_token *token;
991*38fd1498Szrj unsigned int argc;
992*38fd1498Szrj source_location virt_loc;
993*38fd1498Szrj bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion);
994*38fd1498Szrj unsigned num_args_alloced = 0;
995*38fd1498Szrj
996*38fd1498Szrj macro = node->value.macro;
997*38fd1498Szrj if (macro->paramc)
998*38fd1498Szrj argc = macro->paramc;
999*38fd1498Szrj else
1000*38fd1498Szrj argc = 1;
1001*38fd1498Szrj
1002*38fd1498Szrj #define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50
1003*38fd1498Szrj #define ARG_TOKENS_EXTENT 1000
1004*38fd1498Szrj
1005*38fd1498Szrj buff = _cpp_get_buff (pfile, argc * (DEFAULT_NUM_TOKENS_PER_MACRO_ARG
1006*38fd1498Szrj * sizeof (cpp_token *)
1007*38fd1498Szrj + sizeof (macro_arg)));
1008*38fd1498Szrj base_buff = buff;
1009*38fd1498Szrj args = (macro_arg *) buff->base;
1010*38fd1498Szrj memset (args, 0, argc * sizeof (macro_arg));
1011*38fd1498Szrj buff->cur = (unsigned char *) &args[argc];
1012*38fd1498Szrj arg = args, argc = 0;
1013*38fd1498Szrj
1014*38fd1498Szrj /* Collect the tokens making up each argument. We don't yet know
1015*38fd1498Szrj how many arguments have been supplied, whether too many or too
1016*38fd1498Szrj few. Hence the slightly bizarre usage of "argc" and "arg". */
1017*38fd1498Szrj do
1018*38fd1498Szrj {
1019*38fd1498Szrj unsigned int paren_depth = 0;
1020*38fd1498Szrj unsigned int ntokens = 0;
1021*38fd1498Szrj unsigned virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
1022*38fd1498Szrj num_args_alloced++;
1023*38fd1498Szrj
1024*38fd1498Szrj argc++;
1025*38fd1498Szrj arg->first = (const cpp_token **) buff->cur;
1026*38fd1498Szrj if (track_macro_expansion_p)
1027*38fd1498Szrj {
1028*38fd1498Szrj virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
1029*38fd1498Szrj arg->virt_locs = XNEWVEC (source_location,
1030*38fd1498Szrj virt_locs_capacity);
1031*38fd1498Szrj }
1032*38fd1498Szrj
1033*38fd1498Szrj for (;;)
1034*38fd1498Szrj {
1035*38fd1498Szrj /* Require space for 2 new tokens (including a CPP_EOF). */
1036*38fd1498Szrj if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
1037*38fd1498Szrj {
1038*38fd1498Szrj buff = _cpp_append_extend_buff (pfile, buff,
1039*38fd1498Szrj ARG_TOKENS_EXTENT
1040*38fd1498Szrj * sizeof (cpp_token *));
1041*38fd1498Szrj arg->first = (const cpp_token **) buff->cur;
1042*38fd1498Szrj }
1043*38fd1498Szrj if (track_macro_expansion_p
1044*38fd1498Szrj && (ntokens + 2 > virt_locs_capacity))
1045*38fd1498Szrj {
1046*38fd1498Szrj virt_locs_capacity += ARG_TOKENS_EXTENT;
1047*38fd1498Szrj arg->virt_locs = XRESIZEVEC (source_location,
1048*38fd1498Szrj arg->virt_locs,
1049*38fd1498Szrj virt_locs_capacity);
1050*38fd1498Szrj }
1051*38fd1498Szrj
1052*38fd1498Szrj token = cpp_get_token_1 (pfile, &virt_loc);
1053*38fd1498Szrj
1054*38fd1498Szrj if (token->type == CPP_PADDING)
1055*38fd1498Szrj {
1056*38fd1498Szrj /* Drop leading padding. */
1057*38fd1498Szrj if (ntokens == 0)
1058*38fd1498Szrj continue;
1059*38fd1498Szrj }
1060*38fd1498Szrj else if (token->type == CPP_OPEN_PAREN)
1061*38fd1498Szrj paren_depth++;
1062*38fd1498Szrj else if (token->type == CPP_CLOSE_PAREN)
1063*38fd1498Szrj {
1064*38fd1498Szrj if (paren_depth-- == 0)
1065*38fd1498Szrj break;
1066*38fd1498Szrj }
1067*38fd1498Szrj else if (token->type == CPP_COMMA)
1068*38fd1498Szrj {
1069*38fd1498Szrj /* A comma does not terminate an argument within
1070*38fd1498Szrj parentheses or as part of a variable argument. */
1071*38fd1498Szrj if (paren_depth == 0
1072*38fd1498Szrj && ! (macro->variadic && argc == macro->paramc))
1073*38fd1498Szrj break;
1074*38fd1498Szrj }
1075*38fd1498Szrj else if (token->type == CPP_EOF
1076*38fd1498Szrj || (token->type == CPP_HASH && token->flags & BOL))
1077*38fd1498Szrj break;
1078*38fd1498Szrj else if (token->type == CPP_PRAGMA)
1079*38fd1498Szrj {
1080*38fd1498Szrj cpp_token *newtok = _cpp_temp_token (pfile);
1081*38fd1498Szrj
1082*38fd1498Szrj /* CPP_PRAGMA token lives in directive_result, which will
1083*38fd1498Szrj be overwritten on the next directive. */
1084*38fd1498Szrj *newtok = *token;
1085*38fd1498Szrj token = newtok;
1086*38fd1498Szrj do
1087*38fd1498Szrj {
1088*38fd1498Szrj if (*pragma_buff == NULL
1089*38fd1498Szrj || BUFF_ROOM (*pragma_buff) < sizeof (cpp_token *))
1090*38fd1498Szrj {
1091*38fd1498Szrj _cpp_buff *next;
1092*38fd1498Szrj if (*pragma_buff == NULL)
1093*38fd1498Szrj *pragma_buff
1094*38fd1498Szrj = _cpp_get_buff (pfile, 32 * sizeof (cpp_token *));
1095*38fd1498Szrj else
1096*38fd1498Szrj {
1097*38fd1498Szrj next = *pragma_buff;
1098*38fd1498Szrj *pragma_buff
1099*38fd1498Szrj = _cpp_get_buff (pfile,
1100*38fd1498Szrj (BUFF_FRONT (*pragma_buff)
1101*38fd1498Szrj - (*pragma_buff)->base) * 2);
1102*38fd1498Szrj (*pragma_buff)->next = next;
1103*38fd1498Szrj }
1104*38fd1498Szrj }
1105*38fd1498Szrj *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token;
1106*38fd1498Szrj BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *);
1107*38fd1498Szrj if (token->type == CPP_PRAGMA_EOL)
1108*38fd1498Szrj break;
1109*38fd1498Szrj token = cpp_get_token_1 (pfile, &virt_loc);
1110*38fd1498Szrj }
1111*38fd1498Szrj while (token->type != CPP_EOF);
1112*38fd1498Szrj
1113*38fd1498Szrj /* In deferred pragmas parsing_args and prevent_expansion
1114*38fd1498Szrj had been changed, reset it. */
1115*38fd1498Szrj pfile->state.parsing_args = 2;
1116*38fd1498Szrj pfile->state.prevent_expansion = 1;
1117*38fd1498Szrj
1118*38fd1498Szrj if (token->type == CPP_EOF)
1119*38fd1498Szrj break;
1120*38fd1498Szrj else
1121*38fd1498Szrj continue;
1122*38fd1498Szrj }
1123*38fd1498Szrj set_arg_token (arg, token, virt_loc,
1124*38fd1498Szrj ntokens, MACRO_ARG_TOKEN_NORMAL,
1125*38fd1498Szrj CPP_OPTION (pfile, track_macro_expansion));
1126*38fd1498Szrj ntokens++;
1127*38fd1498Szrj }
1128*38fd1498Szrj
1129*38fd1498Szrj /* Drop trailing padding. */
1130*38fd1498Szrj while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
1131*38fd1498Szrj ntokens--;
1132*38fd1498Szrj
1133*38fd1498Szrj arg->count = ntokens;
1134*38fd1498Szrj set_arg_token (arg, &pfile->eof, pfile->eof.src_loc,
1135*38fd1498Szrj ntokens, MACRO_ARG_TOKEN_NORMAL,
1136*38fd1498Szrj CPP_OPTION (pfile, track_macro_expansion));
1137*38fd1498Szrj
1138*38fd1498Szrj /* Terminate the argument. Excess arguments loop back and
1139*38fd1498Szrj overwrite the final legitimate argument, before failing. */
1140*38fd1498Szrj if (argc <= macro->paramc)
1141*38fd1498Szrj {
1142*38fd1498Szrj buff->cur = (unsigned char *) &arg->first[ntokens + 1];
1143*38fd1498Szrj if (argc != macro->paramc)
1144*38fd1498Szrj arg++;
1145*38fd1498Szrj }
1146*38fd1498Szrj }
1147*38fd1498Szrj while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
1148*38fd1498Szrj
1149*38fd1498Szrj if (token->type == CPP_EOF)
1150*38fd1498Szrj {
1151*38fd1498Szrj /* We still need the CPP_EOF to end directives, and to end
1152*38fd1498Szrj pre-expansion of a macro argument. Step back is not
1153*38fd1498Szrj unconditional, since we don't want to return a CPP_EOF to our
1154*38fd1498Szrj callers at the end of an -include-d file. */
1155*38fd1498Szrj if (pfile->context->prev || pfile->state.in_directive)
1156*38fd1498Szrj _cpp_backup_tokens (pfile, 1);
1157*38fd1498Szrj cpp_error (pfile, CPP_DL_ERROR,
1158*38fd1498Szrj "unterminated argument list invoking macro \"%s\"",
1159*38fd1498Szrj NODE_NAME (node));
1160*38fd1498Szrj }
1161*38fd1498Szrj else
1162*38fd1498Szrj {
1163*38fd1498Szrj /* A single empty argument is counted as no argument. */
1164*38fd1498Szrj if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
1165*38fd1498Szrj argc = 0;
1166*38fd1498Szrj if (_cpp_arguments_ok (pfile, macro, node, argc))
1167*38fd1498Szrj {
1168*38fd1498Szrj /* GCC has special semantics for , ## b where b is a varargs
1169*38fd1498Szrj parameter: we remove the comma if b was omitted entirely.
1170*38fd1498Szrj If b was merely an empty argument, the comma is retained.
1171*38fd1498Szrj If the macro takes just one (varargs) parameter, then we
1172*38fd1498Szrj retain the comma only if we are standards conforming.
1173*38fd1498Szrj
1174*38fd1498Szrj If FIRST is NULL replace_args () swallows the comma. */
1175*38fd1498Szrj if (macro->variadic && (argc < macro->paramc
1176*38fd1498Szrj || (argc == 1 && args[0].count == 0
1177*38fd1498Szrj && !CPP_OPTION (pfile, std))))
1178*38fd1498Szrj args[macro->paramc - 1].first = NULL;
1179*38fd1498Szrj if (num_args)
1180*38fd1498Szrj *num_args = num_args_alloced;
1181*38fd1498Szrj return base_buff;
1182*38fd1498Szrj }
1183*38fd1498Szrj }
1184*38fd1498Szrj
1185*38fd1498Szrj /* An error occurred. */
1186*38fd1498Szrj _cpp_release_buff (pfile, base_buff);
1187*38fd1498Szrj return NULL;
1188*38fd1498Szrj }
1189*38fd1498Szrj
1190*38fd1498Szrj /* Search for an opening parenthesis to the macro of NODE, in such a
1191*38fd1498Szrj way that, if none is found, we don't lose the information in any
1192*38fd1498Szrj intervening padding tokens. If we find the parenthesis, collect
1193*38fd1498Szrj the arguments and return the buffer containing them. PRAGMA_BUFF
1194*38fd1498Szrj argument is the same as in collect_args. If NUM_ARGS is non-NULL,
1195*38fd1498Szrj *NUM_ARGS is set to the number of arguments contained in the
1196*38fd1498Szrj returned buffer. */
1197*38fd1498Szrj static _cpp_buff *
funlike_invocation_p(cpp_reader * pfile,cpp_hashnode * node,_cpp_buff ** pragma_buff,unsigned * num_args)1198*38fd1498Szrj funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node,
1199*38fd1498Szrj _cpp_buff **pragma_buff, unsigned *num_args)
1200*38fd1498Szrj {
1201*38fd1498Szrj const cpp_token *token, *padding = NULL;
1202*38fd1498Szrj
1203*38fd1498Szrj for (;;)
1204*38fd1498Szrj {
1205*38fd1498Szrj token = cpp_get_token (pfile);
1206*38fd1498Szrj if (token->type != CPP_PADDING)
1207*38fd1498Szrj break;
1208*38fd1498Szrj if (padding == NULL
1209*38fd1498Szrj || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
1210*38fd1498Szrj padding = token;
1211*38fd1498Szrj }
1212*38fd1498Szrj
1213*38fd1498Szrj if (token->type == CPP_OPEN_PAREN)
1214*38fd1498Szrj {
1215*38fd1498Szrj pfile->state.parsing_args = 2;
1216*38fd1498Szrj return collect_args (pfile, node, pragma_buff, num_args);
1217*38fd1498Szrj }
1218*38fd1498Szrj
1219*38fd1498Szrj /* CPP_EOF can be the end of macro arguments, or the end of the
1220*38fd1498Szrj file. We mustn't back up over the latter. Ugh. */
1221*38fd1498Szrj if (token->type != CPP_EOF || token == &pfile->eof)
1222*38fd1498Szrj {
1223*38fd1498Szrj /* Back up. We may have skipped padding, in which case backing
1224*38fd1498Szrj up more than one token when expanding macros is in general
1225*38fd1498Szrj too difficult. We re-insert it in its own context. */
1226*38fd1498Szrj _cpp_backup_tokens (pfile, 1);
1227*38fd1498Szrj if (padding)
1228*38fd1498Szrj _cpp_push_token_context (pfile, NULL, padding, 1);
1229*38fd1498Szrj }
1230*38fd1498Szrj
1231*38fd1498Szrj return NULL;
1232*38fd1498Szrj }
1233*38fd1498Szrj
1234*38fd1498Szrj /* Return the real number of tokens in the expansion of MACRO. */
1235*38fd1498Szrj static inline unsigned int
macro_real_token_count(const cpp_macro * macro)1236*38fd1498Szrj macro_real_token_count (const cpp_macro *macro)
1237*38fd1498Szrj {
1238*38fd1498Szrj unsigned int i;
1239*38fd1498Szrj if (__builtin_expect (!macro->extra_tokens, true))
1240*38fd1498Szrj return macro->count;
1241*38fd1498Szrj for (i = 0; i < macro->count; i++)
1242*38fd1498Szrj if (macro->exp.tokens[i].type == CPP_PASTE)
1243*38fd1498Szrj return i;
1244*38fd1498Szrj abort ();
1245*38fd1498Szrj }
1246*38fd1498Szrj
1247*38fd1498Szrj /* Push the context of a macro with hash entry NODE onto the context
1248*38fd1498Szrj stack. If we can successfully expand the macro, we push a context
1249*38fd1498Szrj containing its yet-to-be-rescanned replacement list and return one.
1250*38fd1498Szrj If there were additionally any unexpanded deferred #pragma
1251*38fd1498Szrj directives among macro arguments, push another context containing
1252*38fd1498Szrj the pragma tokens before the yet-to-be-rescanned replacement list
1253*38fd1498Szrj and return two. Otherwise, we don't push a context and return
1254*38fd1498Szrj zero. LOCATION is the location of the expansion point of the
1255*38fd1498Szrj macro. */
1256*38fd1498Szrj static int
enter_macro_context(cpp_reader * pfile,cpp_hashnode * node,const cpp_token * result,source_location location)1257*38fd1498Szrj enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
1258*38fd1498Szrj const cpp_token *result, source_location location)
1259*38fd1498Szrj {
1260*38fd1498Szrj /* The presence of a macro invalidates a file's controlling macro. */
1261*38fd1498Szrj pfile->mi_valid = false;
1262*38fd1498Szrj
1263*38fd1498Szrj pfile->state.angled_headers = false;
1264*38fd1498Szrj
1265*38fd1498Szrj /* From here to when we push the context for the macro later down
1266*38fd1498Szrj this function, we need to flag the fact that we are about to
1267*38fd1498Szrj expand a macro. This is useful when -ftrack-macro-expansion is
1268*38fd1498Szrj turned off. In that case, we need to record the location of the
1269*38fd1498Szrj expansion point of the top-most macro we are about to to expand,
1270*38fd1498Szrj into pfile->invocation_location. But we must not record any such
1271*38fd1498Szrj location once the process of expanding the macro starts; that is,
1272*38fd1498Szrj we must not do that recording between now and later down this
1273*38fd1498Szrj function where set this flag to FALSE. */
1274*38fd1498Szrj pfile->about_to_expand_macro_p = true;
1275*38fd1498Szrj
1276*38fd1498Szrj if ((node->flags & NODE_BUILTIN) && !(node->flags & NODE_USED))
1277*38fd1498Szrj {
1278*38fd1498Szrj node->flags |= NODE_USED;
1279*38fd1498Szrj if ((!pfile->cb.user_builtin_macro
1280*38fd1498Szrj || !pfile->cb.user_builtin_macro (pfile, node))
1281*38fd1498Szrj && pfile->cb.used_define)
1282*38fd1498Szrj pfile->cb.used_define (pfile, pfile->directive_line, node);
1283*38fd1498Szrj }
1284*38fd1498Szrj
1285*38fd1498Szrj /* Handle standard macros. */
1286*38fd1498Szrj if (! (node->flags & NODE_BUILTIN))
1287*38fd1498Szrj {
1288*38fd1498Szrj cpp_macro *macro = node->value.macro;
1289*38fd1498Szrj _cpp_buff *pragma_buff = NULL;
1290*38fd1498Szrj
1291*38fd1498Szrj if (macro->fun_like)
1292*38fd1498Szrj {
1293*38fd1498Szrj _cpp_buff *buff;
1294*38fd1498Szrj unsigned num_args = 0;
1295*38fd1498Szrj
1296*38fd1498Szrj pfile->state.prevent_expansion++;
1297*38fd1498Szrj pfile->keep_tokens++;
1298*38fd1498Szrj pfile->state.parsing_args = 1;
1299*38fd1498Szrj buff = funlike_invocation_p (pfile, node, &pragma_buff,
1300*38fd1498Szrj &num_args);
1301*38fd1498Szrj pfile->state.parsing_args = 0;
1302*38fd1498Szrj pfile->keep_tokens--;
1303*38fd1498Szrj pfile->state.prevent_expansion--;
1304*38fd1498Szrj
1305*38fd1498Szrj if (buff == NULL)
1306*38fd1498Szrj {
1307*38fd1498Szrj if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
1308*38fd1498Szrj cpp_warning (pfile, CPP_W_TRADITIONAL,
1309*38fd1498Szrj "function-like macro \"%s\" must be used with arguments in traditional C",
1310*38fd1498Szrj NODE_NAME (node));
1311*38fd1498Szrj
1312*38fd1498Szrj if (pragma_buff)
1313*38fd1498Szrj _cpp_release_buff (pfile, pragma_buff);
1314*38fd1498Szrj
1315*38fd1498Szrj pfile->about_to_expand_macro_p = false;
1316*38fd1498Szrj return 0;
1317*38fd1498Szrj }
1318*38fd1498Szrj
1319*38fd1498Szrj if (macro->paramc > 0)
1320*38fd1498Szrj replace_args (pfile, node, macro,
1321*38fd1498Szrj (macro_arg *) buff->base,
1322*38fd1498Szrj location);
1323*38fd1498Szrj /* Free the memory used by the arguments of this
1324*38fd1498Szrj function-like macro. This memory has been allocated by
1325*38fd1498Szrj funlike_invocation_p and by replace_args. */
1326*38fd1498Szrj delete_macro_args (buff, num_args);
1327*38fd1498Szrj }
1328*38fd1498Szrj
1329*38fd1498Szrj /* Disable the macro within its expansion. */
1330*38fd1498Szrj node->flags |= NODE_DISABLED;
1331*38fd1498Szrj
1332*38fd1498Szrj if (!(node->flags & NODE_USED))
1333*38fd1498Szrj {
1334*38fd1498Szrj node->flags |= NODE_USED;
1335*38fd1498Szrj if (pfile->cb.used_define)
1336*38fd1498Szrj pfile->cb.used_define (pfile, pfile->directive_line, node);
1337*38fd1498Szrj }
1338*38fd1498Szrj
1339*38fd1498Szrj if (pfile->cb.used)
1340*38fd1498Szrj pfile->cb.used (pfile, location, node);
1341*38fd1498Szrj
1342*38fd1498Szrj macro->used = 1;
1343*38fd1498Szrj
1344*38fd1498Szrj if (macro->paramc == 0)
1345*38fd1498Szrj {
1346*38fd1498Szrj unsigned tokens_count = macro_real_token_count (macro);
1347*38fd1498Szrj if (CPP_OPTION (pfile, track_macro_expansion))
1348*38fd1498Szrj {
1349*38fd1498Szrj unsigned int i;
1350*38fd1498Szrj const cpp_token *src = macro->exp.tokens;
1351*38fd1498Szrj const line_map_macro *map;
1352*38fd1498Szrj source_location *virt_locs = NULL;
1353*38fd1498Szrj _cpp_buff *macro_tokens
1354*38fd1498Szrj = tokens_buff_new (pfile, tokens_count, &virt_locs);
1355*38fd1498Szrj
1356*38fd1498Szrj /* Create a macro map to record the locations of the
1357*38fd1498Szrj tokens that are involved in the expansion. LOCATION
1358*38fd1498Szrj is the location of the macro expansion point. */
1359*38fd1498Szrj map = linemap_enter_macro (pfile->line_table,
1360*38fd1498Szrj node, location, tokens_count);
1361*38fd1498Szrj for (i = 0; i < tokens_count; ++i)
1362*38fd1498Szrj {
1363*38fd1498Szrj tokens_buff_add_token (macro_tokens, virt_locs,
1364*38fd1498Szrj src, src->src_loc,
1365*38fd1498Szrj src->src_loc, map, i);
1366*38fd1498Szrj ++src;
1367*38fd1498Szrj }
1368*38fd1498Szrj push_extended_tokens_context (pfile, node,
1369*38fd1498Szrj macro_tokens,
1370*38fd1498Szrj virt_locs,
1371*38fd1498Szrj (const cpp_token **)
1372*38fd1498Szrj macro_tokens->base,
1373*38fd1498Szrj tokens_count);
1374*38fd1498Szrj }
1375*38fd1498Szrj else
1376*38fd1498Szrj _cpp_push_token_context (pfile, node, macro->exp.tokens,
1377*38fd1498Szrj tokens_count);
1378*38fd1498Szrj num_macro_tokens_counter += tokens_count;
1379*38fd1498Szrj }
1380*38fd1498Szrj
1381*38fd1498Szrj if (pragma_buff)
1382*38fd1498Szrj {
1383*38fd1498Szrj if (!pfile->state.in_directive)
1384*38fd1498Szrj _cpp_push_token_context (pfile, NULL,
1385*38fd1498Szrj padding_token (pfile, result), 1);
1386*38fd1498Szrj do
1387*38fd1498Szrj {
1388*38fd1498Szrj unsigned tokens_count;
1389*38fd1498Szrj _cpp_buff *tail = pragma_buff->next;
1390*38fd1498Szrj pragma_buff->next = NULL;
1391*38fd1498Szrj tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff)
1392*38fd1498Szrj - (const cpp_token **) pragma_buff->base);
1393*38fd1498Szrj push_ptoken_context (pfile, NULL, pragma_buff,
1394*38fd1498Szrj (const cpp_token **) pragma_buff->base,
1395*38fd1498Szrj tokens_count);
1396*38fd1498Szrj pragma_buff = tail;
1397*38fd1498Szrj if (!CPP_OPTION (pfile, track_macro_expansion))
1398*38fd1498Szrj num_macro_tokens_counter += tokens_count;
1399*38fd1498Szrj
1400*38fd1498Szrj }
1401*38fd1498Szrj while (pragma_buff != NULL);
1402*38fd1498Szrj pfile->about_to_expand_macro_p = false;
1403*38fd1498Szrj return 2;
1404*38fd1498Szrj }
1405*38fd1498Szrj
1406*38fd1498Szrj pfile->about_to_expand_macro_p = false;
1407*38fd1498Szrj return 1;
1408*38fd1498Szrj }
1409*38fd1498Szrj
1410*38fd1498Szrj pfile->about_to_expand_macro_p = false;
1411*38fd1498Szrj /* Handle built-in macros and the _Pragma operator. */
1412*38fd1498Szrj {
1413*38fd1498Szrj source_location loc, expand_loc;
1414*38fd1498Szrj
1415*38fd1498Szrj if (/* The top-level macro invocation that triggered the expansion
1416*38fd1498Szrj we are looking at is with a standard macro ...*/
1417*38fd1498Szrj !(pfile->top_most_macro_node->flags & NODE_BUILTIN)
1418*38fd1498Szrj /* ... and it's a function-like macro invocation. */
1419*38fd1498Szrj && pfile->top_most_macro_node->value.macro->fun_like)
1420*38fd1498Szrj {
1421*38fd1498Szrj /* Then the location of the end of the macro invocation is the
1422*38fd1498Szrj location of the closing parenthesis. */
1423*38fd1498Szrj loc = pfile->cur_token[-1].src_loc;
1424*38fd1498Szrj expand_loc = loc;
1425*38fd1498Szrj }
1426*38fd1498Szrj else
1427*38fd1498Szrj {
1428*38fd1498Szrj /* Otherwise, the location of the end of the macro invocation is
1429*38fd1498Szrj the location of the expansion point of that top-level macro
1430*38fd1498Szrj invocation. */
1431*38fd1498Szrj loc = location;
1432*38fd1498Szrj expand_loc = pfile->invocation_location;
1433*38fd1498Szrj }
1434*38fd1498Szrj
1435*38fd1498Szrj return builtin_macro (pfile, node, loc, expand_loc);
1436*38fd1498Szrj }
1437*38fd1498Szrj }
1438*38fd1498Szrj
1439*38fd1498Szrj /* De-allocate the memory used by BUFF which is an array of instances
1440*38fd1498Szrj of macro_arg. NUM_ARGS is the number of instances of macro_arg
1441*38fd1498Szrj present in BUFF. */
1442*38fd1498Szrj static void
delete_macro_args(_cpp_buff * buff,unsigned num_args)1443*38fd1498Szrj delete_macro_args (_cpp_buff *buff, unsigned num_args)
1444*38fd1498Szrj {
1445*38fd1498Szrj macro_arg *macro_args;
1446*38fd1498Szrj unsigned i;
1447*38fd1498Szrj
1448*38fd1498Szrj if (buff == NULL)
1449*38fd1498Szrj return;
1450*38fd1498Szrj
1451*38fd1498Szrj macro_args = (macro_arg *) buff->base;
1452*38fd1498Szrj
1453*38fd1498Szrj /* Walk instances of macro_arg to free their expanded tokens as well
1454*38fd1498Szrj as their macro_arg::virt_locs members. */
1455*38fd1498Szrj for (i = 0; i < num_args; ++i)
1456*38fd1498Szrj {
1457*38fd1498Szrj if (macro_args[i].expanded)
1458*38fd1498Szrj {
1459*38fd1498Szrj free (macro_args[i].expanded);
1460*38fd1498Szrj macro_args[i].expanded = NULL;
1461*38fd1498Szrj }
1462*38fd1498Szrj if (macro_args[i].virt_locs)
1463*38fd1498Szrj {
1464*38fd1498Szrj free (macro_args[i].virt_locs);
1465*38fd1498Szrj macro_args[i].virt_locs = NULL;
1466*38fd1498Szrj }
1467*38fd1498Szrj if (macro_args[i].expanded_virt_locs)
1468*38fd1498Szrj {
1469*38fd1498Szrj free (macro_args[i].expanded_virt_locs);
1470*38fd1498Szrj macro_args[i].expanded_virt_locs = NULL;
1471*38fd1498Szrj }
1472*38fd1498Szrj }
1473*38fd1498Szrj _cpp_free_buff (buff);
1474*38fd1498Szrj }
1475*38fd1498Szrj
1476*38fd1498Szrj /* Set the INDEXth token of the macro argument ARG. TOKEN is the token
1477*38fd1498Szrj to set, LOCATION is its virtual location. "Virtual" location means
1478*38fd1498Szrj the location that encodes loci across macro expansion. Otherwise
1479*38fd1498Szrj it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the
1480*38fd1498Szrj argument ARG is supposed to contain. Note that ARG must be
1481*38fd1498Szrj tailored so that it has enough room to contain INDEX + 1 numbers of
1482*38fd1498Szrj tokens, at least. */
1483*38fd1498Szrj static void
set_arg_token(macro_arg * arg,const cpp_token * token,source_location location,size_t index,enum macro_arg_token_kind kind,bool track_macro_exp_p)1484*38fd1498Szrj set_arg_token (macro_arg *arg, const cpp_token *token,
1485*38fd1498Szrj source_location location, size_t index,
1486*38fd1498Szrj enum macro_arg_token_kind kind,
1487*38fd1498Szrj bool track_macro_exp_p)
1488*38fd1498Szrj {
1489*38fd1498Szrj const cpp_token **token_ptr;
1490*38fd1498Szrj source_location *loc = NULL;
1491*38fd1498Szrj
1492*38fd1498Szrj token_ptr =
1493*38fd1498Szrj arg_token_ptr_at (arg, index, kind,
1494*38fd1498Szrj track_macro_exp_p ? &loc : NULL);
1495*38fd1498Szrj *token_ptr = token;
1496*38fd1498Szrj
1497*38fd1498Szrj if (loc != NULL)
1498*38fd1498Szrj {
1499*38fd1498Szrj /* We can't set the location of a stringified argument
1500*38fd1498Szrj token and we can't set any location if we aren't tracking
1501*38fd1498Szrj macro expansion locations. */
1502*38fd1498Szrj gcc_checking_assert (kind != MACRO_ARG_TOKEN_STRINGIFIED
1503*38fd1498Szrj && track_macro_exp_p);
1504*38fd1498Szrj *loc = location;
1505*38fd1498Szrj }
1506*38fd1498Szrj }
1507*38fd1498Szrj
1508*38fd1498Szrj /* Get the pointer to the location of the argument token of the
1509*38fd1498Szrj function-like macro argument ARG. This function must be called
1510*38fd1498Szrj only when we -ftrack-macro-expansion is on. */
1511*38fd1498Szrj static const source_location *
get_arg_token_location(const macro_arg * arg,enum macro_arg_token_kind kind)1512*38fd1498Szrj get_arg_token_location (const macro_arg *arg,
1513*38fd1498Szrj enum macro_arg_token_kind kind)
1514*38fd1498Szrj {
1515*38fd1498Szrj const source_location *loc = NULL;
1516*38fd1498Szrj const cpp_token **token_ptr =
1517*38fd1498Szrj arg_token_ptr_at (arg, 0, kind, (source_location **) &loc);
1518*38fd1498Szrj
1519*38fd1498Szrj if (token_ptr == NULL)
1520*38fd1498Szrj return NULL;
1521*38fd1498Szrj
1522*38fd1498Szrj return loc;
1523*38fd1498Szrj }
1524*38fd1498Szrj
1525*38fd1498Szrj /* Return the pointer to the INDEXth token of the macro argument ARG.
1526*38fd1498Szrj KIND specifies the kind of token the macro argument ARG contains.
1527*38fd1498Szrj If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
1528*38fd1498Szrj of the virtual location of the returned token if the
1529*38fd1498Szrj -ftrack-macro-expansion flag is on; otherwise, it's set to the
1530*38fd1498Szrj spelling location of the returned token. */
1531*38fd1498Szrj static const cpp_token **
arg_token_ptr_at(const macro_arg * arg,size_t index,enum macro_arg_token_kind kind,source_location ** virt_location)1532*38fd1498Szrj arg_token_ptr_at (const macro_arg *arg, size_t index,
1533*38fd1498Szrj enum macro_arg_token_kind kind,
1534*38fd1498Szrj source_location **virt_location)
1535*38fd1498Szrj {
1536*38fd1498Szrj const cpp_token **tokens_ptr = NULL;
1537*38fd1498Szrj
1538*38fd1498Szrj switch (kind)
1539*38fd1498Szrj {
1540*38fd1498Szrj case MACRO_ARG_TOKEN_NORMAL:
1541*38fd1498Szrj tokens_ptr = arg->first;
1542*38fd1498Szrj break;
1543*38fd1498Szrj case MACRO_ARG_TOKEN_STRINGIFIED:
1544*38fd1498Szrj tokens_ptr = (const cpp_token **) &arg->stringified;
1545*38fd1498Szrj break;
1546*38fd1498Szrj case MACRO_ARG_TOKEN_EXPANDED:
1547*38fd1498Szrj tokens_ptr = arg->expanded;
1548*38fd1498Szrj break;
1549*38fd1498Szrj }
1550*38fd1498Szrj
1551*38fd1498Szrj if (tokens_ptr == NULL)
1552*38fd1498Szrj /* This can happen for e.g, an empty token argument to a
1553*38fd1498Szrj funtion-like macro. */
1554*38fd1498Szrj return tokens_ptr;
1555*38fd1498Szrj
1556*38fd1498Szrj if (virt_location)
1557*38fd1498Szrj {
1558*38fd1498Szrj if (kind == MACRO_ARG_TOKEN_NORMAL)
1559*38fd1498Szrj *virt_location = &arg->virt_locs[index];
1560*38fd1498Szrj else if (kind == MACRO_ARG_TOKEN_EXPANDED)
1561*38fd1498Szrj *virt_location = &arg->expanded_virt_locs[index];
1562*38fd1498Szrj else if (kind == MACRO_ARG_TOKEN_STRINGIFIED)
1563*38fd1498Szrj *virt_location =
1564*38fd1498Szrj (source_location *) &tokens_ptr[index]->src_loc;
1565*38fd1498Szrj }
1566*38fd1498Szrj return &tokens_ptr[index];
1567*38fd1498Szrj }
1568*38fd1498Szrj
1569*38fd1498Szrj /* Initialize an iterator so that it iterates over the tokens of a
1570*38fd1498Szrj function-like macro argument. KIND is the kind of tokens we want
1571*38fd1498Szrj ITER to iterate over. TOKEN_PTR points the first token ITER will
1572*38fd1498Szrj iterate over. */
1573*38fd1498Szrj 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)1574*38fd1498Szrj macro_arg_token_iter_init (macro_arg_token_iter *iter,
1575*38fd1498Szrj bool track_macro_exp_p,
1576*38fd1498Szrj enum macro_arg_token_kind kind,
1577*38fd1498Szrj const macro_arg *arg,
1578*38fd1498Szrj const cpp_token **token_ptr)
1579*38fd1498Szrj {
1580*38fd1498Szrj iter->track_macro_exp_p = track_macro_exp_p;
1581*38fd1498Szrj iter->kind = kind;
1582*38fd1498Szrj iter->token_ptr = token_ptr;
1583*38fd1498Szrj /* Unconditionally initialize this so that the compiler doesn't warn
1584*38fd1498Szrj about iter->location_ptr being possibly uninitialized later after
1585*38fd1498Szrj this code has been inlined somewhere. */
1586*38fd1498Szrj iter->location_ptr = NULL;
1587*38fd1498Szrj if (track_macro_exp_p)
1588*38fd1498Szrj iter->location_ptr = get_arg_token_location (arg, kind);
1589*38fd1498Szrj #if CHECKING_P
1590*38fd1498Szrj iter->num_forwards = 0;
1591*38fd1498Szrj if (track_macro_exp_p
1592*38fd1498Szrj && token_ptr != NULL
1593*38fd1498Szrj && iter->location_ptr == NULL)
1594*38fd1498Szrj abort ();
1595*38fd1498Szrj #endif
1596*38fd1498Szrj }
1597*38fd1498Szrj
1598*38fd1498Szrj /* Move the iterator one token forward. Note that if IT was
1599*38fd1498Szrj initialized on an argument that has a stringified token, moving it
1600*38fd1498Szrj forward doesn't make sense as a stringified token is essentially one
1601*38fd1498Szrj string. */
1602*38fd1498Szrj static void
macro_arg_token_iter_forward(macro_arg_token_iter * it)1603*38fd1498Szrj macro_arg_token_iter_forward (macro_arg_token_iter *it)
1604*38fd1498Szrj {
1605*38fd1498Szrj switch (it->kind)
1606*38fd1498Szrj {
1607*38fd1498Szrj case MACRO_ARG_TOKEN_NORMAL:
1608*38fd1498Szrj case MACRO_ARG_TOKEN_EXPANDED:
1609*38fd1498Szrj it->token_ptr++;
1610*38fd1498Szrj if (it->track_macro_exp_p)
1611*38fd1498Szrj it->location_ptr++;
1612*38fd1498Szrj break;
1613*38fd1498Szrj case MACRO_ARG_TOKEN_STRINGIFIED:
1614*38fd1498Szrj #if CHECKING_P
1615*38fd1498Szrj if (it->num_forwards > 0)
1616*38fd1498Szrj abort ();
1617*38fd1498Szrj #endif
1618*38fd1498Szrj break;
1619*38fd1498Szrj }
1620*38fd1498Szrj
1621*38fd1498Szrj #if CHECKING_P
1622*38fd1498Szrj it->num_forwards++;
1623*38fd1498Szrj #endif
1624*38fd1498Szrj }
1625*38fd1498Szrj
1626*38fd1498Szrj /* Return the token pointed to by the iterator. */
1627*38fd1498Szrj static const cpp_token *
macro_arg_token_iter_get_token(const macro_arg_token_iter * it)1628*38fd1498Szrj macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
1629*38fd1498Szrj {
1630*38fd1498Szrj #if CHECKING_P
1631*38fd1498Szrj if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1632*38fd1498Szrj && it->num_forwards > 0)
1633*38fd1498Szrj abort ();
1634*38fd1498Szrj #endif
1635*38fd1498Szrj if (it->token_ptr == NULL)
1636*38fd1498Szrj return NULL;
1637*38fd1498Szrj return *it->token_ptr;
1638*38fd1498Szrj }
1639*38fd1498Szrj
1640*38fd1498Szrj /* Return the location of the token pointed to by the iterator.*/
1641*38fd1498Szrj static source_location
macro_arg_token_iter_get_location(const macro_arg_token_iter * it)1642*38fd1498Szrj macro_arg_token_iter_get_location (const macro_arg_token_iter *it)
1643*38fd1498Szrj {
1644*38fd1498Szrj #if CHECKING_P
1645*38fd1498Szrj if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1646*38fd1498Szrj && it->num_forwards > 0)
1647*38fd1498Szrj abort ();
1648*38fd1498Szrj #endif
1649*38fd1498Szrj if (it->track_macro_exp_p)
1650*38fd1498Szrj return *it->location_ptr;
1651*38fd1498Szrj else
1652*38fd1498Szrj return (*it->token_ptr)->src_loc;
1653*38fd1498Szrj }
1654*38fd1498Szrj
1655*38fd1498Szrj /* Return the index of a token [resulting from macro expansion] inside
1656*38fd1498Szrj the total list of tokens resulting from a given macro
1657*38fd1498Szrj expansion. The index can be different depending on whether if we
1658*38fd1498Szrj want each tokens resulting from function-like macro arguments
1659*38fd1498Szrj expansion to have a different location or not.
1660*38fd1498Szrj
1661*38fd1498Szrj E.g, consider this function-like macro:
1662*38fd1498Szrj
1663*38fd1498Szrj #define M(x) x - 3
1664*38fd1498Szrj
1665*38fd1498Szrj Then consider us "calling" it (and thus expanding it) like:
1666*38fd1498Szrj
1667*38fd1498Szrj M(1+4)
1668*38fd1498Szrj
1669*38fd1498Szrj It will be expanded into:
1670*38fd1498Szrj
1671*38fd1498Szrj 1+4-3
1672*38fd1498Szrj
1673*38fd1498Szrj Let's consider the case of the token '4'.
1674*38fd1498Szrj
1675*38fd1498Szrj Its index can be 2 (it's the third token of the set of tokens
1676*38fd1498Szrj resulting from the expansion) or it can be 0 if we consider that
1677*38fd1498Szrj all tokens resulting from the expansion of the argument "1+2" have
1678*38fd1498Szrj the same index, which is 0. In this later case, the index of token
1679*38fd1498Szrj '-' would then be 1 and the index of token '3' would be 2.
1680*38fd1498Szrj
1681*38fd1498Szrj The later case is useful to use less memory e.g, for the case of
1682*38fd1498Szrj the user using the option -ftrack-macro-expansion=1.
1683*38fd1498Szrj
1684*38fd1498Szrj ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
1685*38fd1498Szrj are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro
1686*38fd1498Szrj parameter (inside the macro replacement list) that corresponds to
1687*38fd1498Szrj the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
1688*38fd1498Szrj of.
1689*38fd1498Szrj
1690*38fd1498Szrj If we refer to the example above, for the '4' argument token,
1691*38fd1498Szrj ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
1692*38fd1498Szrj would be set to the token 'x', in the replacement list "x - 3" of
1693*38fd1498Szrj macro M.
1694*38fd1498Szrj
1695*38fd1498Szrj This is a subroutine of replace_args. */
1696*38fd1498Szrj inline static unsigned
expanded_token_index(cpp_reader * pfile,cpp_macro * macro,const cpp_token * cur_replacement_token,unsigned absolute_token_index)1697*38fd1498Szrj expanded_token_index (cpp_reader *pfile, cpp_macro *macro,
1698*38fd1498Szrj const cpp_token *cur_replacement_token,
1699*38fd1498Szrj unsigned absolute_token_index)
1700*38fd1498Szrj {
1701*38fd1498Szrj if (CPP_OPTION (pfile, track_macro_expansion) > 1)
1702*38fd1498Szrj return absolute_token_index;
1703*38fd1498Szrj return cur_replacement_token - macro->exp.tokens;
1704*38fd1498Szrj }
1705*38fd1498Szrj
1706*38fd1498Szrj /* Copy whether PASTE_LEFT is set from SRC to *PASTE_FLAG. */
1707*38fd1498Szrj
1708*38fd1498Szrj static void
copy_paste_flag(cpp_reader * pfile,const cpp_token ** paste_flag,const cpp_token * src)1709*38fd1498Szrj copy_paste_flag (cpp_reader *pfile, const cpp_token **paste_flag,
1710*38fd1498Szrj const cpp_token *src)
1711*38fd1498Szrj {
1712*38fd1498Szrj cpp_token *token = _cpp_temp_token (pfile);
1713*38fd1498Szrj token->type = (*paste_flag)->type;
1714*38fd1498Szrj token->val = (*paste_flag)->val;
1715*38fd1498Szrj if (src->flags & PASTE_LEFT)
1716*38fd1498Szrj token->flags = (*paste_flag)->flags | PASTE_LEFT;
1717*38fd1498Szrj else
1718*38fd1498Szrj token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
1719*38fd1498Szrj *paste_flag = token;
1720*38fd1498Szrj }
1721*38fd1498Szrj
1722*38fd1498Szrj /* True IFF the last token emitted into BUFF (if any) is PTR. */
1723*38fd1498Szrj
1724*38fd1498Szrj static bool
last_token_is(_cpp_buff * buff,const cpp_token ** ptr)1725*38fd1498Szrj last_token_is (_cpp_buff *buff, const cpp_token **ptr)
1726*38fd1498Szrj {
1727*38fd1498Szrj return (ptr && tokens_buff_last_token_ptr (buff) == ptr);
1728*38fd1498Szrj }
1729*38fd1498Szrj
1730*38fd1498Szrj /* Replace the parameters in a function-like macro of NODE with the
1731*38fd1498Szrj actual ARGS, and place the result in a newly pushed token context.
1732*38fd1498Szrj Expand each argument before replacing, unless it is operated upon
1733*38fd1498Szrj by the # or ## operators. EXPANSION_POINT_LOC is the location of
1734*38fd1498Szrj the expansion point of the macro. E.g, the location of the
1735*38fd1498Szrj function-like macro invocation. */
1736*38fd1498Szrj static void
replace_args(cpp_reader * pfile,cpp_hashnode * node,cpp_macro * macro,macro_arg * args,source_location expansion_point_loc)1737*38fd1498Szrj replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
1738*38fd1498Szrj macro_arg *args, source_location expansion_point_loc)
1739*38fd1498Szrj {
1740*38fd1498Szrj unsigned int i, total;
1741*38fd1498Szrj const cpp_token *src, *limit;
1742*38fd1498Szrj const cpp_token **first = NULL;
1743*38fd1498Szrj macro_arg *arg;
1744*38fd1498Szrj _cpp_buff *buff = NULL;
1745*38fd1498Szrj source_location *virt_locs = NULL;
1746*38fd1498Szrj unsigned int exp_count;
1747*38fd1498Szrj const line_map_macro *map = NULL;
1748*38fd1498Szrj int track_macro_exp;
1749*38fd1498Szrj
1750*38fd1498Szrj /* First, fully macro-expand arguments, calculating the number of
1751*38fd1498Szrj tokens in the final expansion as we go. The ordering of the if
1752*38fd1498Szrj statements below is subtle; we must handle stringification before
1753*38fd1498Szrj pasting. */
1754*38fd1498Szrj
1755*38fd1498Szrj /* EXP_COUNT is the number of tokens in the macro replacement
1756*38fd1498Szrj list. TOTAL is the number of tokens /after/ macro parameters
1757*38fd1498Szrj have been replaced by their arguments. */
1758*38fd1498Szrj exp_count = macro_real_token_count (macro);
1759*38fd1498Szrj total = exp_count;
1760*38fd1498Szrj limit = macro->exp.tokens + exp_count;
1761*38fd1498Szrj
1762*38fd1498Szrj for (src = macro->exp.tokens; src < limit; src++)
1763*38fd1498Szrj if (src->type == CPP_MACRO_ARG)
1764*38fd1498Szrj {
1765*38fd1498Szrj /* Leading and trailing padding tokens. */
1766*38fd1498Szrj total += 2;
1767*38fd1498Szrj /* Account for leading and padding tokens in exp_count too.
1768*38fd1498Szrj This is going to be important later down this function,
1769*38fd1498Szrj when we want to handle the case of (track_macro_exp <
1770*38fd1498Szrj 2). */
1771*38fd1498Szrj exp_count += 2;
1772*38fd1498Szrj
1773*38fd1498Szrj /* We have an argument. If it is not being stringified or
1774*38fd1498Szrj pasted it is macro-replaced before insertion. */
1775*38fd1498Szrj arg = &args[src->val.macro_arg.arg_no - 1];
1776*38fd1498Szrj
1777*38fd1498Szrj if (src->flags & STRINGIFY_ARG)
1778*38fd1498Szrj {
1779*38fd1498Szrj if (!arg->stringified)
1780*38fd1498Szrj arg->stringified = stringify_arg (pfile, arg);
1781*38fd1498Szrj }
1782*38fd1498Szrj else if ((src->flags & PASTE_LEFT)
1783*38fd1498Szrj || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
1784*38fd1498Szrj total += arg->count - 1;
1785*38fd1498Szrj else
1786*38fd1498Szrj {
1787*38fd1498Szrj if (!arg->expanded)
1788*38fd1498Szrj expand_arg (pfile, arg);
1789*38fd1498Szrj total += arg->expanded_count - 1;
1790*38fd1498Szrj }
1791*38fd1498Szrj }
1792*38fd1498Szrj
1793*38fd1498Szrj /* When the compiler is called with the -ftrack-macro-expansion
1794*38fd1498Szrj flag, we need to keep track of the location of each token that
1795*38fd1498Szrj results from macro expansion.
1796*38fd1498Szrj
1797*38fd1498Szrj A token resulting from macro expansion is not a new token. It is
1798*38fd1498Szrj simply the same token as the token coming from the macro
1799*38fd1498Szrj definition. The new things that are allocated are the buffer
1800*38fd1498Szrj that holds the tokens resulting from macro expansion and a new
1801*38fd1498Szrj location that records many things like the locus of the expansion
1802*38fd1498Szrj point as well as the original locus inside the definition of the
1803*38fd1498Szrj macro. This location is called a virtual location.
1804*38fd1498Szrj
1805*38fd1498Szrj So the buffer BUFF holds a set of cpp_token*, and the buffer
1806*38fd1498Szrj VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
1807*38fd1498Szrj
1808*38fd1498Szrj Both of these two buffers are going to be hung off of the macro
1809*38fd1498Szrj context, when the latter is pushed. The memory allocated to
1810*38fd1498Szrj store the tokens and their locations is going to be freed once
1811*38fd1498Szrj the context of macro expansion is popped.
1812*38fd1498Szrj
1813*38fd1498Szrj As far as tokens are concerned, the memory overhead of
1814*38fd1498Szrj -ftrack-macro-expansion is proportional to the number of
1815*38fd1498Szrj macros that get expanded multiplied by sizeof (source_location).
1816*38fd1498Szrj The good news is that extra memory gets freed when the macro
1817*38fd1498Szrj context is freed, i.e shortly after the macro got expanded. */
1818*38fd1498Szrj
1819*38fd1498Szrj /* Is the -ftrack-macro-expansion flag in effect? */
1820*38fd1498Szrj track_macro_exp = CPP_OPTION (pfile, track_macro_expansion);
1821*38fd1498Szrj
1822*38fd1498Szrj /* Now allocate memory space for tokens and locations resulting from
1823*38fd1498Szrj the macro expansion, copy the tokens and replace the arguments.
1824*38fd1498Szrj This memory must be freed when the context of the macro MACRO is
1825*38fd1498Szrj popped. */
1826*38fd1498Szrj buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL);
1827*38fd1498Szrj
1828*38fd1498Szrj first = (const cpp_token **) buff->base;
1829*38fd1498Szrj
1830*38fd1498Szrj /* Create a macro map to record the locations of the tokens that are
1831*38fd1498Szrj involved in the expansion. Note that the expansion point is set
1832*38fd1498Szrj to the location of the closing parenthesis. Otherwise, the
1833*38fd1498Szrj subsequent map created for the first token that comes after the
1834*38fd1498Szrj macro map might have a wrong line number. That would lead to
1835*38fd1498Szrj tokens with wrong line numbers after the macro expansion. This
1836*38fd1498Szrj adds up to the memory overhead of the -ftrack-macro-expansion
1837*38fd1498Szrj flag; for every macro that is expanded, a "macro map" is
1838*38fd1498Szrj created. */
1839*38fd1498Szrj if (track_macro_exp)
1840*38fd1498Szrj {
1841*38fd1498Szrj int num_macro_tokens = total;
1842*38fd1498Szrj if (track_macro_exp < 2)
1843*38fd1498Szrj /* Then the number of macro tokens won't take in account the
1844*38fd1498Szrj fact that function-like macro arguments can expand to
1845*38fd1498Szrj multiple tokens. This is to save memory at the expense of
1846*38fd1498Szrj accuracy.
1847*38fd1498Szrj
1848*38fd1498Szrj Suppose we have #define SQUARE(A) A * A
1849*38fd1498Szrj
1850*38fd1498Szrj And then we do SQUARE(2+3)
1851*38fd1498Szrj
1852*38fd1498Szrj Then the tokens 2, +, 3, will have the same location,
1853*38fd1498Szrj saying they come from the expansion of the argument A. */
1854*38fd1498Szrj num_macro_tokens = exp_count;
1855*38fd1498Szrj map = linemap_enter_macro (pfile->line_table, node,
1856*38fd1498Szrj expansion_point_loc,
1857*38fd1498Szrj num_macro_tokens);
1858*38fd1498Szrj }
1859*38fd1498Szrj i = 0;
1860*38fd1498Szrj vaopt_state vaopt_tracker (pfile, macro->variadic,
1861*38fd1498Szrj args[macro->paramc - 1].count > 0);
1862*38fd1498Szrj const cpp_token **vaopt_start = NULL;
1863*38fd1498Szrj for (src = macro->exp.tokens; src < limit; src++)
1864*38fd1498Szrj {
1865*38fd1498Szrj unsigned int arg_tokens_count;
1866*38fd1498Szrj macro_arg_token_iter from;
1867*38fd1498Szrj const cpp_token **paste_flag = NULL;
1868*38fd1498Szrj const cpp_token **tmp_token_ptr;
1869*38fd1498Szrj
1870*38fd1498Szrj /* __VA_OPT__ handling. */
1871*38fd1498Szrj vaopt_state::update_type vostate = vaopt_tracker.update (src);
1872*38fd1498Szrj if (vostate != vaopt_state::INCLUDE)
1873*38fd1498Szrj {
1874*38fd1498Szrj if (vostate == vaopt_state::BEGIN)
1875*38fd1498Szrj {
1876*38fd1498Szrj /* Padding on the left of __VA_OPT__ (unless RHS of ##). */
1877*38fd1498Szrj if (src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
1878*38fd1498Szrj {
1879*38fd1498Szrj const cpp_token *t = padding_token (pfile, src);
1880*38fd1498Szrj unsigned index = expanded_token_index (pfile, macro, src, i);
1881*38fd1498Szrj /* Allocate a virtual location for the padding token and
1882*38fd1498Szrj append the token and its location to BUFF and
1883*38fd1498Szrj VIRT_LOCS. */
1884*38fd1498Szrj tokens_buff_add_token (buff, virt_locs, t,
1885*38fd1498Szrj t->src_loc, t->src_loc,
1886*38fd1498Szrj map, index);
1887*38fd1498Szrj }
1888*38fd1498Szrj vaopt_start = tokens_buff_last_token_ptr (buff);
1889*38fd1498Szrj }
1890*38fd1498Szrj else if (vostate == vaopt_state::END)
1891*38fd1498Szrj {
1892*38fd1498Szrj const cpp_token **start = vaopt_start;
1893*38fd1498Szrj vaopt_start = NULL;
1894*38fd1498Szrj
1895*38fd1498Szrj /* Remove any tail padding from inside the __VA_OPT__. */
1896*38fd1498Szrj paste_flag = tokens_buff_last_token_ptr (buff);
1897*38fd1498Szrj while (paste_flag && paste_flag != start
1898*38fd1498Szrj && (*paste_flag)->type == CPP_PADDING)
1899*38fd1498Szrj {
1900*38fd1498Szrj tokens_buff_remove_last_token (buff);
1901*38fd1498Szrj paste_flag = tokens_buff_last_token_ptr (buff);
1902*38fd1498Szrj }
1903*38fd1498Szrj
1904*38fd1498Szrj if (src->flags & PASTE_LEFT)
1905*38fd1498Szrj {
1906*38fd1498Szrj /* With a non-empty __VA_OPT__ on the LHS of ##, the last
1907*38fd1498Szrj token should be flagged PASTE_LEFT. */
1908*38fd1498Szrj if (paste_flag && (*paste_flag)->type != CPP_PADDING)
1909*38fd1498Szrj copy_paste_flag (pfile, paste_flag, src);
1910*38fd1498Szrj }
1911*38fd1498Szrj else
1912*38fd1498Szrj {
1913*38fd1498Szrj /* Otherwise, avoid paste on RHS, __VA_OPT__(c)d or
1914*38fd1498Szrj __VA_OPT__(c)__VA_OPT__(d). */
1915*38fd1498Szrj const cpp_token *t = &pfile->avoid_paste;
1916*38fd1498Szrj tokens_buff_add_token (buff, virt_locs,
1917*38fd1498Szrj t, t->src_loc, t->src_loc,
1918*38fd1498Szrj NULL, 0);
1919*38fd1498Szrj }
1920*38fd1498Szrj }
1921*38fd1498Szrj continue;
1922*38fd1498Szrj }
1923*38fd1498Szrj
1924*38fd1498Szrj if (src->type != CPP_MACRO_ARG)
1925*38fd1498Szrj {
1926*38fd1498Szrj /* Allocate a virtual location for token SRC, and add that
1927*38fd1498Szrj token and its virtual location into the buffers BUFF and
1928*38fd1498Szrj VIRT_LOCS. */
1929*38fd1498Szrj unsigned index = expanded_token_index (pfile, macro, src, i);
1930*38fd1498Szrj tokens_buff_add_token (buff, virt_locs, src,
1931*38fd1498Szrj src->src_loc, src->src_loc,
1932*38fd1498Szrj map, index);
1933*38fd1498Szrj i += 1;
1934*38fd1498Szrj continue;
1935*38fd1498Szrj }
1936*38fd1498Szrj
1937*38fd1498Szrj paste_flag = 0;
1938*38fd1498Szrj arg = &args[src->val.macro_arg.arg_no - 1];
1939*38fd1498Szrj /* SRC is a macro parameter that we need to replace with its
1940*38fd1498Szrj corresponding argument. So at some point we'll need to
1941*38fd1498Szrj iterate over the tokens of the macro argument and copy them
1942*38fd1498Szrj into the "place" now holding the correspondig macro
1943*38fd1498Szrj parameter. We are going to use the iterator type
1944*38fd1498Szrj macro_argo_token_iter to handle that iterating. The 'if'
1945*38fd1498Szrj below is to initialize the iterator depending on the type of
1946*38fd1498Szrj tokens the macro argument has. It also does some adjustment
1947*38fd1498Szrj related to padding tokens and some pasting corner cases. */
1948*38fd1498Szrj if (src->flags & STRINGIFY_ARG)
1949*38fd1498Szrj {
1950*38fd1498Szrj arg_tokens_count = 1;
1951*38fd1498Szrj macro_arg_token_iter_init (&from,
1952*38fd1498Szrj CPP_OPTION (pfile,
1953*38fd1498Szrj track_macro_expansion),
1954*38fd1498Szrj MACRO_ARG_TOKEN_STRINGIFIED,
1955*38fd1498Szrj arg, &arg->stringified);
1956*38fd1498Szrj }
1957*38fd1498Szrj else if (src->flags & PASTE_LEFT)
1958*38fd1498Szrj {
1959*38fd1498Szrj arg_tokens_count = arg->count;
1960*38fd1498Szrj macro_arg_token_iter_init (&from,
1961*38fd1498Szrj CPP_OPTION (pfile,
1962*38fd1498Szrj track_macro_expansion),
1963*38fd1498Szrj MACRO_ARG_TOKEN_NORMAL,
1964*38fd1498Szrj arg, arg->first);
1965*38fd1498Szrj }
1966*38fd1498Szrj else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
1967*38fd1498Szrj {
1968*38fd1498Szrj int num_toks;
1969*38fd1498Szrj arg_tokens_count = arg->count;
1970*38fd1498Szrj macro_arg_token_iter_init (&from,
1971*38fd1498Szrj CPP_OPTION (pfile,
1972*38fd1498Szrj track_macro_expansion),
1973*38fd1498Szrj MACRO_ARG_TOKEN_NORMAL,
1974*38fd1498Szrj arg, arg->first);
1975*38fd1498Szrj
1976*38fd1498Szrj num_toks = tokens_buff_count (buff);
1977*38fd1498Szrj
1978*38fd1498Szrj if (num_toks != 0)
1979*38fd1498Szrj {
1980*38fd1498Szrj /* So the current parameter token is pasted to the previous
1981*38fd1498Szrj token in the replacement list. Let's look at what
1982*38fd1498Szrj we have as previous and current arguments. */
1983*38fd1498Szrj
1984*38fd1498Szrj /* This is the previous argument's token ... */
1985*38fd1498Szrj tmp_token_ptr = tokens_buff_last_token_ptr (buff);
1986*38fd1498Szrj
1987*38fd1498Szrj if ((*tmp_token_ptr)->type == CPP_COMMA
1988*38fd1498Szrj && macro->variadic
1989*38fd1498Szrj && src->val.macro_arg.arg_no == macro->paramc)
1990*38fd1498Szrj {
1991*38fd1498Szrj /* ... which is a comma; and the current parameter
1992*38fd1498Szrj is the last parameter of a variadic function-like
1993*38fd1498Szrj macro. If the argument to the current last
1994*38fd1498Szrj parameter is NULL, then swallow the comma,
1995*38fd1498Szrj otherwise drop the paste flag. */
1996*38fd1498Szrj if (macro_arg_token_iter_get_token (&from) == NULL)
1997*38fd1498Szrj tokens_buff_remove_last_token (buff);
1998*38fd1498Szrj else
1999*38fd1498Szrj paste_flag = tmp_token_ptr;
2000*38fd1498Szrj }
2001*38fd1498Szrj /* Remove the paste flag if the RHS is a placemarker, unless the
2002*38fd1498Szrj previous emitted token is at the beginning of __VA_OPT__;
2003*38fd1498Szrj placemarkers within __VA_OPT__ are ignored in that case. */
2004*38fd1498Szrj else if (arg_tokens_count == 0
2005*38fd1498Szrj && tmp_token_ptr != vaopt_start)
2006*38fd1498Szrj paste_flag = tmp_token_ptr;
2007*38fd1498Szrj }
2008*38fd1498Szrj }
2009*38fd1498Szrj else
2010*38fd1498Szrj {
2011*38fd1498Szrj arg_tokens_count = arg->expanded_count;
2012*38fd1498Szrj macro_arg_token_iter_init (&from,
2013*38fd1498Szrj CPP_OPTION (pfile,
2014*38fd1498Szrj track_macro_expansion),
2015*38fd1498Szrj MACRO_ARG_TOKEN_EXPANDED,
2016*38fd1498Szrj arg, arg->expanded);
2017*38fd1498Szrj
2018*38fd1498Szrj if (last_token_is (buff, vaopt_start))
2019*38fd1498Szrj {
2020*38fd1498Szrj /* We're expanding an arg at the beginning of __VA_OPT__.
2021*38fd1498Szrj Skip padding. */
2022*38fd1498Szrj while (arg_tokens_count)
2023*38fd1498Szrj {
2024*38fd1498Szrj const cpp_token *t = macro_arg_token_iter_get_token (&from);
2025*38fd1498Szrj if (t->type != CPP_PADDING)
2026*38fd1498Szrj break;
2027*38fd1498Szrj macro_arg_token_iter_forward (&from);
2028*38fd1498Szrj --arg_tokens_count;
2029*38fd1498Szrj }
2030*38fd1498Szrj }
2031*38fd1498Szrj }
2032*38fd1498Szrj
2033*38fd1498Szrj /* Padding on the left of an argument (unless RHS of ##). */
2034*38fd1498Szrj if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
2035*38fd1498Szrj && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT)
2036*38fd1498Szrj && !last_token_is (buff, vaopt_start))
2037*38fd1498Szrj {
2038*38fd1498Szrj const cpp_token *t = padding_token (pfile, src);
2039*38fd1498Szrj unsigned index = expanded_token_index (pfile, macro, src, i);
2040*38fd1498Szrj /* Allocate a virtual location for the padding token and
2041*38fd1498Szrj append the token and its location to BUFF and
2042*38fd1498Szrj VIRT_LOCS. */
2043*38fd1498Szrj tokens_buff_add_token (buff, virt_locs, t,
2044*38fd1498Szrj t->src_loc, t->src_loc,
2045*38fd1498Szrj map, index);
2046*38fd1498Szrj }
2047*38fd1498Szrj
2048*38fd1498Szrj if (arg_tokens_count)
2049*38fd1498Szrj {
2050*38fd1498Szrj /* So now we've got the number of tokens that make up the
2051*38fd1498Szrj argument that is going to replace the current parameter
2052*38fd1498Szrj in the macro's replacement list. */
2053*38fd1498Szrj unsigned int j;
2054*38fd1498Szrj for (j = 0; j < arg_tokens_count; ++j)
2055*38fd1498Szrj {
2056*38fd1498Szrj /* So if track_macro_exp is < 2, the user wants to
2057*38fd1498Szrj save extra memory while tracking macro expansion
2058*38fd1498Szrj locations. So in that case here is what we do:
2059*38fd1498Szrj
2060*38fd1498Szrj Suppose we have #define SQUARE(A) A * A
2061*38fd1498Szrj
2062*38fd1498Szrj And then we do SQUARE(2+3)
2063*38fd1498Szrj
2064*38fd1498Szrj Then the tokens 2, +, 3, will have the same location,
2065*38fd1498Szrj saying they come from the expansion of the argument
2066*38fd1498Szrj A.
2067*38fd1498Szrj
2068*38fd1498Szrj So that means we are going to ignore the COUNT tokens
2069*38fd1498Szrj resulting from the expansion of the current macro
2070*38fd1498Szrj argument. In other words all the ARG_TOKENS_COUNT tokens
2071*38fd1498Szrj resulting from the expansion of the macro argument will
2072*38fd1498Szrj have the index I. Normally, each of those tokens should
2073*38fd1498Szrj have index I+J. */
2074*38fd1498Szrj unsigned token_index = i;
2075*38fd1498Szrj unsigned index;
2076*38fd1498Szrj if (track_macro_exp > 1)
2077*38fd1498Szrj token_index += j;
2078*38fd1498Szrj
2079*38fd1498Szrj index = expanded_token_index (pfile, macro, src, token_index);
2080*38fd1498Szrj tokens_buff_add_token (buff, virt_locs,
2081*38fd1498Szrj macro_arg_token_iter_get_token (&from),
2082*38fd1498Szrj macro_arg_token_iter_get_location (&from),
2083*38fd1498Szrj src->src_loc, map, index);
2084*38fd1498Szrj macro_arg_token_iter_forward (&from);
2085*38fd1498Szrj }
2086*38fd1498Szrj
2087*38fd1498Szrj /* With a non-empty argument on the LHS of ##, the last
2088*38fd1498Szrj token should be flagged PASTE_LEFT. */
2089*38fd1498Szrj if (src->flags & PASTE_LEFT)
2090*38fd1498Szrj paste_flag
2091*38fd1498Szrj = (const cpp_token **) tokens_buff_last_token_ptr (buff);
2092*38fd1498Szrj }
2093*38fd1498Szrj else if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99)
2094*38fd1498Szrj && ! macro->syshdr && ! cpp_in_system_header (pfile))
2095*38fd1498Szrj {
2096*38fd1498Szrj if (CPP_OPTION (pfile, cplusplus))
2097*38fd1498Szrj cpp_pedwarning (pfile, CPP_W_PEDANTIC,
2098*38fd1498Szrj "invoking macro %s argument %d: "
2099*38fd1498Szrj "empty macro arguments are undefined"
2100*38fd1498Szrj " in ISO C++98",
2101*38fd1498Szrj NODE_NAME (node), src->val.macro_arg.arg_no);
2102*38fd1498Szrj else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat))
2103*38fd1498Szrj cpp_pedwarning (pfile,
2104*38fd1498Szrj CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
2105*38fd1498Szrj ? CPP_W_C90_C99_COMPAT : CPP_W_PEDANTIC,
2106*38fd1498Szrj "invoking macro %s argument %d: "
2107*38fd1498Szrj "empty macro arguments are undefined"
2108*38fd1498Szrj " in ISO C90",
2109*38fd1498Szrj NODE_NAME (node), src->val.macro_arg.arg_no);
2110*38fd1498Szrj }
2111*38fd1498Szrj else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
2112*38fd1498Szrj && ! CPP_OPTION (pfile, cplusplus)
2113*38fd1498Szrj && ! macro->syshdr && ! cpp_in_system_header (pfile))
2114*38fd1498Szrj cpp_warning (pfile, CPP_W_C90_C99_COMPAT,
2115*38fd1498Szrj "invoking macro %s argument %d: "
2116*38fd1498Szrj "empty macro arguments are undefined"
2117*38fd1498Szrj " in ISO C90",
2118*38fd1498Szrj NODE_NAME (node), src->val.macro_arg.arg_no);
2119*38fd1498Szrj
2120*38fd1498Szrj /* Avoid paste on RHS (even case count == 0). */
2121*38fd1498Szrj if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT)
2122*38fd1498Szrj && !last_token_is (buff, vaopt_start))
2123*38fd1498Szrj {
2124*38fd1498Szrj const cpp_token *t = &pfile->avoid_paste;
2125*38fd1498Szrj tokens_buff_add_token (buff, virt_locs,
2126*38fd1498Szrj t, t->src_loc, t->src_loc,
2127*38fd1498Szrj NULL, 0);
2128*38fd1498Szrj }
2129*38fd1498Szrj
2130*38fd1498Szrj /* Add a new paste flag, or remove an unwanted one. */
2131*38fd1498Szrj if (paste_flag)
2132*38fd1498Szrj copy_paste_flag (pfile, paste_flag, src);
2133*38fd1498Szrj
2134*38fd1498Szrj i += arg_tokens_count;
2135*38fd1498Szrj }
2136*38fd1498Szrj
2137*38fd1498Szrj if (track_macro_exp)
2138*38fd1498Szrj push_extended_tokens_context (pfile, node, buff, virt_locs, first,
2139*38fd1498Szrj tokens_buff_count (buff));
2140*38fd1498Szrj else
2141*38fd1498Szrj push_ptoken_context (pfile, node, buff, first,
2142*38fd1498Szrj tokens_buff_count (buff));
2143*38fd1498Szrj
2144*38fd1498Szrj num_macro_tokens_counter += tokens_buff_count (buff);
2145*38fd1498Szrj }
2146*38fd1498Szrj
2147*38fd1498Szrj /* Return a special padding token, with padding inherited from SOURCE. */
2148*38fd1498Szrj static const cpp_token *
padding_token(cpp_reader * pfile,const cpp_token * source)2149*38fd1498Szrj padding_token (cpp_reader *pfile, const cpp_token *source)
2150*38fd1498Szrj {
2151*38fd1498Szrj cpp_token *result = _cpp_temp_token (pfile);
2152*38fd1498Szrj
2153*38fd1498Szrj result->type = CPP_PADDING;
2154*38fd1498Szrj
2155*38fd1498Szrj /* Data in GCed data structures cannot be made const so far, so we
2156*38fd1498Szrj need a cast here. */
2157*38fd1498Szrj result->val.source = (cpp_token *) source;
2158*38fd1498Szrj result->flags = 0;
2159*38fd1498Szrj return result;
2160*38fd1498Szrj }
2161*38fd1498Szrj
2162*38fd1498Szrj /* Get a new uninitialized context. Create a new one if we cannot
2163*38fd1498Szrj re-use an old one. */
2164*38fd1498Szrj static cpp_context *
next_context(cpp_reader * pfile)2165*38fd1498Szrj next_context (cpp_reader *pfile)
2166*38fd1498Szrj {
2167*38fd1498Szrj cpp_context *result = pfile->context->next;
2168*38fd1498Szrj
2169*38fd1498Szrj if (result == 0)
2170*38fd1498Szrj {
2171*38fd1498Szrj result = XNEW (cpp_context);
2172*38fd1498Szrj memset (result, 0, sizeof (cpp_context));
2173*38fd1498Szrj result->prev = pfile->context;
2174*38fd1498Szrj result->next = 0;
2175*38fd1498Szrj pfile->context->next = result;
2176*38fd1498Szrj }
2177*38fd1498Szrj
2178*38fd1498Szrj pfile->context = result;
2179*38fd1498Szrj return result;
2180*38fd1498Szrj }
2181*38fd1498Szrj
2182*38fd1498Szrj /* Push a list of pointers to tokens. */
2183*38fd1498Szrj static void
push_ptoken_context(cpp_reader * pfile,cpp_hashnode * macro,_cpp_buff * buff,const cpp_token ** first,unsigned int count)2184*38fd1498Szrj push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
2185*38fd1498Szrj const cpp_token **first, unsigned int count)
2186*38fd1498Szrj {
2187*38fd1498Szrj cpp_context *context = next_context (pfile);
2188*38fd1498Szrj
2189*38fd1498Szrj context->tokens_kind = TOKENS_KIND_INDIRECT;
2190*38fd1498Szrj context->c.macro = macro;
2191*38fd1498Szrj context->buff = buff;
2192*38fd1498Szrj FIRST (context).ptoken = first;
2193*38fd1498Szrj LAST (context).ptoken = first + count;
2194*38fd1498Szrj }
2195*38fd1498Szrj
2196*38fd1498Szrj /* Push a list of tokens.
2197*38fd1498Szrj
2198*38fd1498Szrj A NULL macro means that we should continue the current macro
2199*38fd1498Szrj expansion, in essence. That means that if we are currently in a
2200*38fd1498Szrj macro expansion context, we'll make the new pfile->context refer to
2201*38fd1498Szrj the current macro. */
2202*38fd1498Szrj void
_cpp_push_token_context(cpp_reader * pfile,cpp_hashnode * macro,const cpp_token * first,unsigned int count)2203*38fd1498Szrj _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
2204*38fd1498Szrj const cpp_token *first, unsigned int count)
2205*38fd1498Szrj {
2206*38fd1498Szrj cpp_context *context;
2207*38fd1498Szrj
2208*38fd1498Szrj if (macro == NULL)
2209*38fd1498Szrj macro = macro_of_context (pfile->context);
2210*38fd1498Szrj
2211*38fd1498Szrj context = next_context (pfile);
2212*38fd1498Szrj context->tokens_kind = TOKENS_KIND_DIRECT;
2213*38fd1498Szrj context->c.macro = macro;
2214*38fd1498Szrj context->buff = NULL;
2215*38fd1498Szrj FIRST (context).token = first;
2216*38fd1498Szrj LAST (context).token = first + count;
2217*38fd1498Szrj }
2218*38fd1498Szrj
2219*38fd1498Szrj /* Build a context containing a list of tokens as well as their
2220*38fd1498Szrj virtual locations and push it. TOKENS_BUFF is the buffer that
2221*38fd1498Szrj contains the tokens pointed to by FIRST. If TOKENS_BUFF is
2222*38fd1498Szrj non-NULL, it means that the context owns it, meaning that
2223*38fd1498Szrj _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
2224*38fd1498Szrj contains the virtual locations.
2225*38fd1498Szrj
2226*38fd1498Szrj A NULL macro means that we should continue the current macro
2227*38fd1498Szrj expansion, in essence. That means that if we are currently in a
2228*38fd1498Szrj macro expansion context, we'll make the new pfile->context refer to
2229*38fd1498Szrj the current macro. */
2230*38fd1498Szrj static void
push_extended_tokens_context(cpp_reader * pfile,cpp_hashnode * macro,_cpp_buff * token_buff,source_location * virt_locs,const cpp_token ** first,unsigned int count)2231*38fd1498Szrj push_extended_tokens_context (cpp_reader *pfile,
2232*38fd1498Szrj cpp_hashnode *macro,
2233*38fd1498Szrj _cpp_buff *token_buff,
2234*38fd1498Szrj source_location *virt_locs,
2235*38fd1498Szrj const cpp_token **first,
2236*38fd1498Szrj unsigned int count)
2237*38fd1498Szrj {
2238*38fd1498Szrj cpp_context *context;
2239*38fd1498Szrj macro_context *m;
2240*38fd1498Szrj
2241*38fd1498Szrj if (macro == NULL)
2242*38fd1498Szrj macro = macro_of_context (pfile->context);
2243*38fd1498Szrj
2244*38fd1498Szrj context = next_context (pfile);
2245*38fd1498Szrj context->tokens_kind = TOKENS_KIND_EXTENDED;
2246*38fd1498Szrj context->buff = token_buff;
2247*38fd1498Szrj
2248*38fd1498Szrj m = XNEW (macro_context);
2249*38fd1498Szrj m->macro_node = macro;
2250*38fd1498Szrj m->virt_locs = virt_locs;
2251*38fd1498Szrj m->cur_virt_loc = virt_locs;
2252*38fd1498Szrj context->c.mc = m;
2253*38fd1498Szrj FIRST (context).ptoken = first;
2254*38fd1498Szrj LAST (context).ptoken = first + count;
2255*38fd1498Szrj }
2256*38fd1498Szrj
2257*38fd1498Szrj /* Push a traditional macro's replacement text. */
2258*38fd1498Szrj void
_cpp_push_text_context(cpp_reader * pfile,cpp_hashnode * macro,const uchar * start,size_t len)2259*38fd1498Szrj _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
2260*38fd1498Szrj const uchar *start, size_t len)
2261*38fd1498Szrj {
2262*38fd1498Szrj cpp_context *context = next_context (pfile);
2263*38fd1498Szrj
2264*38fd1498Szrj context->tokens_kind = TOKENS_KIND_DIRECT;
2265*38fd1498Szrj context->c.macro = macro;
2266*38fd1498Szrj context->buff = NULL;
2267*38fd1498Szrj CUR (context) = start;
2268*38fd1498Szrj RLIMIT (context) = start + len;
2269*38fd1498Szrj macro->flags |= NODE_DISABLED;
2270*38fd1498Szrj }
2271*38fd1498Szrj
2272*38fd1498Szrj /* Creates a buffer that holds tokens a.k.a "token buffer", usually
2273*38fd1498Szrj for the purpose of storing them on a cpp_context. If VIRT_LOCS is
2274*38fd1498Szrj non-null (which means that -ftrack-macro-expansion is on),
2275*38fd1498Szrj *VIRT_LOCS is set to a newly allocated buffer that is supposed to
2276*38fd1498Szrj hold the virtual locations of the tokens resulting from macro
2277*38fd1498Szrj expansion. */
2278*38fd1498Szrj static _cpp_buff*
tokens_buff_new(cpp_reader * pfile,size_t len,source_location ** virt_locs)2279*38fd1498Szrj tokens_buff_new (cpp_reader *pfile, size_t len,
2280*38fd1498Szrj source_location **virt_locs)
2281*38fd1498Szrj {
2282*38fd1498Szrj size_t tokens_size = len * sizeof (cpp_token *);
2283*38fd1498Szrj size_t locs_size = len * sizeof (source_location);
2284*38fd1498Szrj
2285*38fd1498Szrj if (virt_locs != NULL)
2286*38fd1498Szrj *virt_locs = XNEWVEC (source_location, locs_size);
2287*38fd1498Szrj return _cpp_get_buff (pfile, tokens_size);
2288*38fd1498Szrj }
2289*38fd1498Szrj
2290*38fd1498Szrj /* Returns the number of tokens contained in a token buffer. The
2291*38fd1498Szrj buffer holds a set of cpp_token*. */
2292*38fd1498Szrj static size_t
tokens_buff_count(_cpp_buff * buff)2293*38fd1498Szrj tokens_buff_count (_cpp_buff *buff)
2294*38fd1498Szrj {
2295*38fd1498Szrj return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *);
2296*38fd1498Szrj }
2297*38fd1498Szrj
2298*38fd1498Szrj /* Return a pointer to the last token contained in the token buffer
2299*38fd1498Szrj BUFF. */
2300*38fd1498Szrj static const cpp_token **
tokens_buff_last_token_ptr(_cpp_buff * buff)2301*38fd1498Szrj tokens_buff_last_token_ptr (_cpp_buff *buff)
2302*38fd1498Szrj {
2303*38fd1498Szrj if (BUFF_FRONT (buff) == buff->base)
2304*38fd1498Szrj return NULL;
2305*38fd1498Szrj return &((const cpp_token **) BUFF_FRONT (buff))[-1];
2306*38fd1498Szrj }
2307*38fd1498Szrj
2308*38fd1498Szrj /* Remove the last token contained in the token buffer TOKENS_BUFF.
2309*38fd1498Szrj If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer
2310*38fd1498Szrj containing the virtual locations of the tokens in TOKENS_BUFF; in
2311*38fd1498Szrj which case the function updates that buffer as well. */
2312*38fd1498Szrj static inline void
tokens_buff_remove_last_token(_cpp_buff * tokens_buff)2313*38fd1498Szrj tokens_buff_remove_last_token (_cpp_buff *tokens_buff)
2314*38fd1498Szrj
2315*38fd1498Szrj {
2316*38fd1498Szrj if (BUFF_FRONT (tokens_buff) > tokens_buff->base)
2317*38fd1498Szrj BUFF_FRONT (tokens_buff) =
2318*38fd1498Szrj (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1];
2319*38fd1498Szrj }
2320*38fd1498Szrj
2321*38fd1498Szrj /* Insert a token into the token buffer at the position pointed to by
2322*38fd1498Szrj DEST. Note that the buffer is not enlarged so the previous token
2323*38fd1498Szrj that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null,
2324*38fd1498Szrj means -ftrack-macro-expansion is effect; it then points to where to
2325*38fd1498Szrj insert the virtual location of TOKEN. TOKEN is the token to
2326*38fd1498Szrj insert. VIRT_LOC is the virtual location of the token, i.e, the
2327*38fd1498Szrj location possibly encoding its locus across macro expansion. If
2328*38fd1498Szrj TOKEN is an argument of a function-like macro (inside a macro
2329*38fd1498Szrj replacement list), PARM_DEF_LOC is the spelling location of the
2330*38fd1498Szrj macro parameter that TOKEN is replacing, in the replacement list of
2331*38fd1498Szrj the macro. If TOKEN is not an argument of a function-like macro or
2332*38fd1498Szrj if it doesn't come from a macro expansion, then VIRT_LOC can just
2333*38fd1498Szrj be set to the same value as PARM_DEF_LOC. If MAP is non null, it
2334*38fd1498Szrj means TOKEN comes from a macro expansion and MAP is the macro map
2335*38fd1498Szrj associated to the macro. MACRO_TOKEN_INDEX points to the index of
2336*38fd1498Szrj the token in the macro map; it is not considered if MAP is NULL.
2337*38fd1498Szrj
2338*38fd1498Szrj Upon successful completion this function returns the a pointer to
2339*38fd1498Szrj the position of the token coming right after the insertion
2340*38fd1498Szrj point. */
2341*38fd1498Szrj static inline const cpp_token **
tokens_buff_put_token_to(const cpp_token ** dest,source_location * virt_loc_dest,const cpp_token * token,source_location virt_loc,source_location parm_def_loc,const line_map_macro * map,unsigned int macro_token_index)2342*38fd1498Szrj tokens_buff_put_token_to (const cpp_token **dest,
2343*38fd1498Szrj source_location *virt_loc_dest,
2344*38fd1498Szrj const cpp_token *token,
2345*38fd1498Szrj source_location virt_loc,
2346*38fd1498Szrj source_location parm_def_loc,
2347*38fd1498Szrj const line_map_macro *map,
2348*38fd1498Szrj unsigned int macro_token_index)
2349*38fd1498Szrj {
2350*38fd1498Szrj source_location macro_loc = virt_loc;
2351*38fd1498Szrj const cpp_token **result;
2352*38fd1498Szrj
2353*38fd1498Szrj if (virt_loc_dest)
2354*38fd1498Szrj {
2355*38fd1498Szrj /* -ftrack-macro-expansion is on. */
2356*38fd1498Szrj if (map)
2357*38fd1498Szrj macro_loc = linemap_add_macro_token (map, macro_token_index,
2358*38fd1498Szrj virt_loc, parm_def_loc);
2359*38fd1498Szrj *virt_loc_dest = macro_loc;
2360*38fd1498Szrj }
2361*38fd1498Szrj *dest = token;
2362*38fd1498Szrj result = &dest[1];
2363*38fd1498Szrj
2364*38fd1498Szrj return result;
2365*38fd1498Szrj }
2366*38fd1498Szrj
2367*38fd1498Szrj /* Adds a token at the end of the tokens contained in BUFFER. Note
2368*38fd1498Szrj that this function doesn't enlarge BUFFER when the number of tokens
2369*38fd1498Szrj reaches BUFFER's size; it aborts in that situation.
2370*38fd1498Szrj
2371*38fd1498Szrj TOKEN is the token to append. VIRT_LOC is the virtual location of
2372*38fd1498Szrj the token, i.e, the location possibly encoding its locus across
2373*38fd1498Szrj macro expansion. If TOKEN is an argument of a function-like macro
2374*38fd1498Szrj (inside a macro replacement list), PARM_DEF_LOC is the location of
2375*38fd1498Szrj the macro parameter that TOKEN is replacing. If TOKEN doesn't come
2376*38fd1498Szrj from a macro expansion, then VIRT_LOC can just be set to the same
2377*38fd1498Szrj value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes
2378*38fd1498Szrj from a macro expansion and MAP is the macro map associated to the
2379*38fd1498Szrj macro. MACRO_TOKEN_INDEX points to the index of the token in the
2380*38fd1498Szrj macro map; It is not considered if MAP is NULL. If VIRT_LOCS is
2381*38fd1498Szrj non-null, it means -ftrack-macro-expansion is on; in which case
2382*38fd1498Szrj this function adds the virtual location DEF_LOC to the VIRT_LOCS
2383*38fd1498Szrj array, at the same index as the one of TOKEN in BUFFER. Upon
2384*38fd1498Szrj successful completion this function returns the a pointer to the
2385*38fd1498Szrj position of the token coming right after the insertion point. */
2386*38fd1498Szrj static const cpp_token **
tokens_buff_add_token(_cpp_buff * buffer,source_location * virt_locs,const cpp_token * token,source_location virt_loc,source_location parm_def_loc,const line_map_macro * map,unsigned int macro_token_index)2387*38fd1498Szrj tokens_buff_add_token (_cpp_buff *buffer,
2388*38fd1498Szrj source_location *virt_locs,
2389*38fd1498Szrj const cpp_token *token,
2390*38fd1498Szrj source_location virt_loc,
2391*38fd1498Szrj source_location parm_def_loc,
2392*38fd1498Szrj const line_map_macro *map,
2393*38fd1498Szrj unsigned int macro_token_index)
2394*38fd1498Szrj {
2395*38fd1498Szrj const cpp_token **result;
2396*38fd1498Szrj source_location *virt_loc_dest = NULL;
2397*38fd1498Szrj unsigned token_index =
2398*38fd1498Szrj (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
2399*38fd1498Szrj
2400*38fd1498Szrj /* Abort if we pass the end the buffer. */
2401*38fd1498Szrj if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer))
2402*38fd1498Szrj abort ();
2403*38fd1498Szrj
2404*38fd1498Szrj if (virt_locs != NULL)
2405*38fd1498Szrj virt_loc_dest = &virt_locs[token_index];
2406*38fd1498Szrj
2407*38fd1498Szrj result =
2408*38fd1498Szrj tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer),
2409*38fd1498Szrj virt_loc_dest, token, virt_loc, parm_def_loc,
2410*38fd1498Szrj map, macro_token_index);
2411*38fd1498Szrj
2412*38fd1498Szrj BUFF_FRONT (buffer) = (unsigned char *) result;
2413*38fd1498Szrj return result;
2414*38fd1498Szrj }
2415*38fd1498Szrj
2416*38fd1498Szrj /* Allocate space for the function-like macro argument ARG to store
2417*38fd1498Szrj the tokens resulting from the macro-expansion of the tokens that
2418*38fd1498Szrj make up ARG itself. That space is allocated in ARG->expanded and
2419*38fd1498Szrj needs to be freed using free. */
2420*38fd1498Szrj static void
alloc_expanded_arg_mem(cpp_reader * pfile,macro_arg * arg,size_t capacity)2421*38fd1498Szrj alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity)
2422*38fd1498Szrj {
2423*38fd1498Szrj gcc_checking_assert (arg->expanded == NULL
2424*38fd1498Szrj && arg->expanded_virt_locs == NULL);
2425*38fd1498Szrj
2426*38fd1498Szrj arg->expanded = XNEWVEC (const cpp_token *, capacity);
2427*38fd1498Szrj if (CPP_OPTION (pfile, track_macro_expansion))
2428*38fd1498Szrj arg->expanded_virt_locs = XNEWVEC (source_location, capacity);
2429*38fd1498Szrj
2430*38fd1498Szrj }
2431*38fd1498Szrj
2432*38fd1498Szrj /* If necessary, enlarge ARG->expanded to so that it can contain SIZE
2433*38fd1498Szrj tokens. */
2434*38fd1498Szrj static void
ensure_expanded_arg_room(cpp_reader * pfile,macro_arg * arg,size_t size,size_t * expanded_capacity)2435*38fd1498Szrj ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg,
2436*38fd1498Szrj size_t size, size_t *expanded_capacity)
2437*38fd1498Szrj {
2438*38fd1498Szrj if (size <= *expanded_capacity)
2439*38fd1498Szrj return;
2440*38fd1498Szrj
2441*38fd1498Szrj size *= 2;
2442*38fd1498Szrj
2443*38fd1498Szrj arg->expanded =
2444*38fd1498Szrj XRESIZEVEC (const cpp_token *, arg->expanded, size);
2445*38fd1498Szrj *expanded_capacity = size;
2446*38fd1498Szrj
2447*38fd1498Szrj if (CPP_OPTION (pfile, track_macro_expansion))
2448*38fd1498Szrj {
2449*38fd1498Szrj if (arg->expanded_virt_locs == NULL)
2450*38fd1498Szrj arg->expanded_virt_locs = XNEWVEC (source_location, size);
2451*38fd1498Szrj else
2452*38fd1498Szrj arg->expanded_virt_locs = XRESIZEVEC (source_location,
2453*38fd1498Szrj arg->expanded_virt_locs,
2454*38fd1498Szrj size);
2455*38fd1498Szrj }
2456*38fd1498Szrj }
2457*38fd1498Szrj
2458*38fd1498Szrj /* Expand an argument ARG before replacing parameters in a
2459*38fd1498Szrj function-like macro. This works by pushing a context with the
2460*38fd1498Szrj argument's tokens, and then expanding that into a temporary buffer
2461*38fd1498Szrj as if it were a normal part of the token stream. collect_args()
2462*38fd1498Szrj has terminated the argument's tokens with a CPP_EOF so that we know
2463*38fd1498Szrj when we have fully expanded the argument. */
2464*38fd1498Szrj static void
expand_arg(cpp_reader * pfile,macro_arg * arg)2465*38fd1498Szrj expand_arg (cpp_reader *pfile, macro_arg *arg)
2466*38fd1498Szrj {
2467*38fd1498Szrj size_t capacity;
2468*38fd1498Szrj bool saved_warn_trad;
2469*38fd1498Szrj bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
2470*38fd1498Szrj
2471*38fd1498Szrj if (arg->count == 0
2472*38fd1498Szrj || arg->expanded != NULL)
2473*38fd1498Szrj return;
2474*38fd1498Szrj
2475*38fd1498Szrj /* Don't warn about funlike macros when pre-expanding. */
2476*38fd1498Szrj saved_warn_trad = CPP_WTRADITIONAL (pfile);
2477*38fd1498Szrj CPP_WTRADITIONAL (pfile) = 0;
2478*38fd1498Szrj
2479*38fd1498Szrj /* Loop, reading in the tokens of the argument. */
2480*38fd1498Szrj capacity = 256;
2481*38fd1498Szrj alloc_expanded_arg_mem (pfile, arg, capacity);
2482*38fd1498Szrj
2483*38fd1498Szrj if (track_macro_exp_p)
2484*38fd1498Szrj push_extended_tokens_context (pfile, NULL, NULL,
2485*38fd1498Szrj arg->virt_locs,
2486*38fd1498Szrj arg->first,
2487*38fd1498Szrj arg->count + 1);
2488*38fd1498Szrj else
2489*38fd1498Szrj push_ptoken_context (pfile, NULL, NULL,
2490*38fd1498Szrj arg->first, arg->count + 1);
2491*38fd1498Szrj
2492*38fd1498Szrj for (;;)
2493*38fd1498Szrj {
2494*38fd1498Szrj const cpp_token *token;
2495*38fd1498Szrj source_location location;
2496*38fd1498Szrj
2497*38fd1498Szrj ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1,
2498*38fd1498Szrj &capacity);
2499*38fd1498Szrj
2500*38fd1498Szrj token = cpp_get_token_1 (pfile, &location);
2501*38fd1498Szrj
2502*38fd1498Szrj if (token->type == CPP_EOF)
2503*38fd1498Szrj break;
2504*38fd1498Szrj
2505*38fd1498Szrj set_arg_token (arg, token, location,
2506*38fd1498Szrj arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED,
2507*38fd1498Szrj CPP_OPTION (pfile, track_macro_expansion));
2508*38fd1498Szrj arg->expanded_count++;
2509*38fd1498Szrj }
2510*38fd1498Szrj
2511*38fd1498Szrj _cpp_pop_context (pfile);
2512*38fd1498Szrj
2513*38fd1498Szrj CPP_WTRADITIONAL (pfile) = saved_warn_trad;
2514*38fd1498Szrj }
2515*38fd1498Szrj
2516*38fd1498Szrj /* Returns the macro associated to the current context if we are in
2517*38fd1498Szrj the context a macro expansion, NULL otherwise. */
2518*38fd1498Szrj static cpp_hashnode*
macro_of_context(cpp_context * context)2519*38fd1498Szrj macro_of_context (cpp_context *context)
2520*38fd1498Szrj {
2521*38fd1498Szrj if (context == NULL)
2522*38fd1498Szrj return NULL;
2523*38fd1498Szrj
2524*38fd1498Szrj return (context->tokens_kind == TOKENS_KIND_EXTENDED)
2525*38fd1498Szrj ? context->c.mc->macro_node
2526*38fd1498Szrj : context->c.macro;
2527*38fd1498Szrj }
2528*38fd1498Szrj
2529*38fd1498Szrj /* Return TRUE iff we are expanding a macro or are about to start
2530*38fd1498Szrj expanding one. If we are effectively expanding a macro, the
2531*38fd1498Szrj function macro_of_context returns a pointer to the macro being
2532*38fd1498Szrj expanded. */
2533*38fd1498Szrj static bool
in_macro_expansion_p(cpp_reader * pfile)2534*38fd1498Szrj in_macro_expansion_p (cpp_reader *pfile)
2535*38fd1498Szrj {
2536*38fd1498Szrj if (pfile == NULL)
2537*38fd1498Szrj return false;
2538*38fd1498Szrj
2539*38fd1498Szrj return (pfile->about_to_expand_macro_p
2540*38fd1498Szrj || macro_of_context (pfile->context));
2541*38fd1498Szrj }
2542*38fd1498Szrj
2543*38fd1498Szrj /* Pop the current context off the stack, re-enabling the macro if the
2544*38fd1498Szrj context represented a macro's replacement list. Initially the
2545*38fd1498Szrj context structure was not freed so that we can re-use it later, but
2546*38fd1498Szrj now we do free it to reduce peak memory consumption. */
2547*38fd1498Szrj void
_cpp_pop_context(cpp_reader * pfile)2548*38fd1498Szrj _cpp_pop_context (cpp_reader *pfile)
2549*38fd1498Szrj {
2550*38fd1498Szrj cpp_context *context = pfile->context;
2551*38fd1498Szrj
2552*38fd1498Szrj /* We should not be popping the base context. */
2553*38fd1498Szrj if (context == &pfile->base_context)
2554*38fd1498Szrj abort ();
2555*38fd1498Szrj
2556*38fd1498Szrj if (context->c.macro)
2557*38fd1498Szrj {
2558*38fd1498Szrj cpp_hashnode *macro;
2559*38fd1498Szrj if (context->tokens_kind == TOKENS_KIND_EXTENDED)
2560*38fd1498Szrj {
2561*38fd1498Szrj macro_context *mc = context->c.mc;
2562*38fd1498Szrj macro = mc->macro_node;
2563*38fd1498Szrj /* If context->buff is set, it means the life time of tokens
2564*38fd1498Szrj is bound to the life time of this context; so we must
2565*38fd1498Szrj free the tokens; that means we must free the virtual
2566*38fd1498Szrj locations of these tokens too. */
2567*38fd1498Szrj if (context->buff && mc->virt_locs)
2568*38fd1498Szrj {
2569*38fd1498Szrj free (mc->virt_locs);
2570*38fd1498Szrj mc->virt_locs = NULL;
2571*38fd1498Szrj }
2572*38fd1498Szrj free (mc);
2573*38fd1498Szrj context->c.mc = NULL;
2574*38fd1498Szrj }
2575*38fd1498Szrj else
2576*38fd1498Szrj macro = context->c.macro;
2577*38fd1498Szrj
2578*38fd1498Szrj /* Beware that MACRO can be NULL in cases like when we are
2579*38fd1498Szrj called from expand_arg. In those cases, a dummy context with
2580*38fd1498Szrj tokens is pushed just for the purpose of walking them using
2581*38fd1498Szrj cpp_get_token_1. In that case, no 'macro' field is set into
2582*38fd1498Szrj the dummy context. */
2583*38fd1498Szrj if (macro != NULL
2584*38fd1498Szrj /* Several contiguous macro expansion contexts can be
2585*38fd1498Szrj associated to the same macro; that means it's the same
2586*38fd1498Szrj macro expansion that spans across all these (sub)
2587*38fd1498Szrj contexts. So we should re-enable an expansion-disabled
2588*38fd1498Szrj macro only when we are sure we are really out of that
2589*38fd1498Szrj macro expansion. */
2590*38fd1498Szrj && macro_of_context (context->prev) != macro)
2591*38fd1498Szrj macro->flags &= ~NODE_DISABLED;
2592*38fd1498Szrj
2593*38fd1498Szrj if (macro == pfile->top_most_macro_node && context->prev == NULL)
2594*38fd1498Szrj /* We are popping the context of the top-most macro node. */
2595*38fd1498Szrj pfile->top_most_macro_node = NULL;
2596*38fd1498Szrj }
2597*38fd1498Szrj
2598*38fd1498Szrj if (context->buff)
2599*38fd1498Szrj {
2600*38fd1498Szrj /* Decrease memory peak consumption by freeing the memory used
2601*38fd1498Szrj by the context. */
2602*38fd1498Szrj _cpp_free_buff (context->buff);
2603*38fd1498Szrj }
2604*38fd1498Szrj
2605*38fd1498Szrj pfile->context = context->prev;
2606*38fd1498Szrj /* decrease peak memory consumption by feeing the context. */
2607*38fd1498Szrj pfile->context->next = NULL;
2608*38fd1498Szrj free (context);
2609*38fd1498Szrj }
2610*38fd1498Szrj
2611*38fd1498Szrj /* Return TRUE if we reached the end of the set of tokens stored in
2612*38fd1498Szrj CONTEXT, FALSE otherwise. */
2613*38fd1498Szrj static inline bool
reached_end_of_context(cpp_context * context)2614*38fd1498Szrj reached_end_of_context (cpp_context *context)
2615*38fd1498Szrj {
2616*38fd1498Szrj if (context->tokens_kind == TOKENS_KIND_DIRECT)
2617*38fd1498Szrj return FIRST (context).token == LAST (context).token;
2618*38fd1498Szrj else if (context->tokens_kind == TOKENS_KIND_INDIRECT
2619*38fd1498Szrj || context->tokens_kind == TOKENS_KIND_EXTENDED)
2620*38fd1498Szrj return FIRST (context).ptoken == LAST (context).ptoken;
2621*38fd1498Szrj else
2622*38fd1498Szrj abort ();
2623*38fd1498Szrj }
2624*38fd1498Szrj
2625*38fd1498Szrj /* Consume the next token contained in the current context of PFILE,
2626*38fd1498Szrj and return it in *TOKEN. It's "full location" is returned in
2627*38fd1498Szrj *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
2628*38fd1498Szrj means the location encoding the locus of the token across macro
2629*38fd1498Szrj expansion; otherwise it's just is the "normal" location of the
2630*38fd1498Szrj token which (*TOKEN)->src_loc. */
2631*38fd1498Szrj static inline void
consume_next_token_from_context(cpp_reader * pfile,const cpp_token ** token,source_location * location)2632*38fd1498Szrj consume_next_token_from_context (cpp_reader *pfile,
2633*38fd1498Szrj const cpp_token ** token,
2634*38fd1498Szrj source_location *location)
2635*38fd1498Szrj {
2636*38fd1498Szrj cpp_context *c = pfile->context;
2637*38fd1498Szrj
2638*38fd1498Szrj if ((c)->tokens_kind == TOKENS_KIND_DIRECT)
2639*38fd1498Szrj {
2640*38fd1498Szrj *token = FIRST (c).token;
2641*38fd1498Szrj *location = (*token)->src_loc;
2642*38fd1498Szrj FIRST (c).token++;
2643*38fd1498Szrj }
2644*38fd1498Szrj else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT)
2645*38fd1498Szrj {
2646*38fd1498Szrj *token = *FIRST (c).ptoken;
2647*38fd1498Szrj *location = (*token)->src_loc;
2648*38fd1498Szrj FIRST (c).ptoken++;
2649*38fd1498Szrj }
2650*38fd1498Szrj else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED)
2651*38fd1498Szrj {
2652*38fd1498Szrj macro_context *m = c->c.mc;
2653*38fd1498Szrj *token = *FIRST (c).ptoken;
2654*38fd1498Szrj if (m->virt_locs)
2655*38fd1498Szrj {
2656*38fd1498Szrj *location = *m->cur_virt_loc;
2657*38fd1498Szrj m->cur_virt_loc++;
2658*38fd1498Szrj }
2659*38fd1498Szrj else
2660*38fd1498Szrj *location = (*token)->src_loc;
2661*38fd1498Szrj FIRST (c).ptoken++;
2662*38fd1498Szrj }
2663*38fd1498Szrj else
2664*38fd1498Szrj abort ();
2665*38fd1498Szrj }
2666*38fd1498Szrj
2667*38fd1498Szrj /* In the traditional mode of the preprocessor, if we are currently in
2668*38fd1498Szrj a directive, the location of a token must be the location of the
2669*38fd1498Szrj start of the directive line. This function returns the proper
2670*38fd1498Szrj location if we are in the traditional mode, and just returns
2671*38fd1498Szrj LOCATION otherwise. */
2672*38fd1498Szrj
2673*38fd1498Szrj static inline source_location
maybe_adjust_loc_for_trad_cpp(cpp_reader * pfile,source_location location)2674*38fd1498Szrj maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, source_location location)
2675*38fd1498Szrj {
2676*38fd1498Szrj if (CPP_OPTION (pfile, traditional))
2677*38fd1498Szrj {
2678*38fd1498Szrj if (pfile->state.in_directive)
2679*38fd1498Szrj return pfile->directive_line;
2680*38fd1498Szrj }
2681*38fd1498Szrj return location;
2682*38fd1498Szrj }
2683*38fd1498Szrj
2684*38fd1498Szrj /* Routine to get a token as well as its location.
2685*38fd1498Szrj
2686*38fd1498Szrj Macro expansions and directives are transparently handled,
2687*38fd1498Szrj including entering included files. Thus tokens are post-macro
2688*38fd1498Szrj expansion, and after any intervening directives. External callers
2689*38fd1498Szrj see CPP_EOF only at EOF. Internal callers also see it when meeting
2690*38fd1498Szrj a directive inside a macro call, when at the end of a directive and
2691*38fd1498Szrj state.in_directive is still 1, and at the end of argument
2692*38fd1498Szrj pre-expansion.
2693*38fd1498Szrj
2694*38fd1498Szrj LOC is an out parameter; *LOC is set to the location "as expected
2695*38fd1498Szrj by the user". Please read the comment of
2696*38fd1498Szrj cpp_get_token_with_location to learn more about the meaning of this
2697*38fd1498Szrj location. */
2698*38fd1498Szrj static const cpp_token*
cpp_get_token_1(cpp_reader * pfile,source_location * location)2699*38fd1498Szrj cpp_get_token_1 (cpp_reader *pfile, source_location *location)
2700*38fd1498Szrj {
2701*38fd1498Szrj const cpp_token *result;
2702*38fd1498Szrj /* This token is a virtual token that either encodes a location
2703*38fd1498Szrj related to macro expansion or a spelling location. */
2704*38fd1498Szrj source_location virt_loc = 0;
2705*38fd1498Szrj /* pfile->about_to_expand_macro_p can be overriden by indirect calls
2706*38fd1498Szrj to functions that push macro contexts. So let's save it so that
2707*38fd1498Szrj we can restore it when we are about to leave this routine. */
2708*38fd1498Szrj bool saved_about_to_expand_macro = pfile->about_to_expand_macro_p;
2709*38fd1498Szrj
2710*38fd1498Szrj for (;;)
2711*38fd1498Szrj {
2712*38fd1498Szrj cpp_hashnode *node;
2713*38fd1498Szrj cpp_context *context = pfile->context;
2714*38fd1498Szrj
2715*38fd1498Szrj /* Context->prev == 0 <=> base context. */
2716*38fd1498Szrj if (!context->prev)
2717*38fd1498Szrj {
2718*38fd1498Szrj result = _cpp_lex_token (pfile);
2719*38fd1498Szrj virt_loc = result->src_loc;
2720*38fd1498Szrj }
2721*38fd1498Szrj else if (!reached_end_of_context (context))
2722*38fd1498Szrj {
2723*38fd1498Szrj consume_next_token_from_context (pfile, &result,
2724*38fd1498Szrj &virt_loc);
2725*38fd1498Szrj if (result->flags & PASTE_LEFT)
2726*38fd1498Szrj {
2727*38fd1498Szrj paste_all_tokens (pfile, result);
2728*38fd1498Szrj if (pfile->state.in_directive)
2729*38fd1498Szrj continue;
2730*38fd1498Szrj result = padding_token (pfile, result);
2731*38fd1498Szrj goto out;
2732*38fd1498Szrj }
2733*38fd1498Szrj }
2734*38fd1498Szrj else
2735*38fd1498Szrj {
2736*38fd1498Szrj if (pfile->context->c.macro)
2737*38fd1498Szrj ++num_expanded_macros_counter;
2738*38fd1498Szrj _cpp_pop_context (pfile);
2739*38fd1498Szrj if (pfile->state.in_directive)
2740*38fd1498Szrj continue;
2741*38fd1498Szrj result = &pfile->avoid_paste;
2742*38fd1498Szrj goto out;
2743*38fd1498Szrj }
2744*38fd1498Szrj
2745*38fd1498Szrj if (pfile->state.in_directive && result->type == CPP_COMMENT)
2746*38fd1498Szrj continue;
2747*38fd1498Szrj
2748*38fd1498Szrj if (result->type != CPP_NAME)
2749*38fd1498Szrj break;
2750*38fd1498Szrj
2751*38fd1498Szrj node = result->val.node.node;
2752*38fd1498Szrj
2753*38fd1498Szrj if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
2754*38fd1498Szrj break;
2755*38fd1498Szrj
2756*38fd1498Szrj if (!(node->flags & NODE_DISABLED))
2757*38fd1498Szrj {
2758*38fd1498Szrj int ret = 0;
2759*38fd1498Szrj /* If not in a macro context, and we're going to start an
2760*38fd1498Szrj expansion, record the location and the top level macro
2761*38fd1498Szrj about to be expanded. */
2762*38fd1498Szrj if (!in_macro_expansion_p (pfile))
2763*38fd1498Szrj {
2764*38fd1498Szrj pfile->invocation_location = result->src_loc;
2765*38fd1498Szrj pfile->top_most_macro_node = node;
2766*38fd1498Szrj }
2767*38fd1498Szrj if (pfile->state.prevent_expansion)
2768*38fd1498Szrj break;
2769*38fd1498Szrj
2770*38fd1498Szrj /* Conditional macros require that a predicate be evaluated
2771*38fd1498Szrj first. */
2772*38fd1498Szrj if ((node->flags & NODE_CONDITIONAL) != 0)
2773*38fd1498Szrj {
2774*38fd1498Szrj if (pfile->cb.macro_to_expand)
2775*38fd1498Szrj {
2776*38fd1498Szrj bool whitespace_after;
2777*38fd1498Szrj const cpp_token *peek_tok = cpp_peek_token (pfile, 0);
2778*38fd1498Szrj
2779*38fd1498Szrj whitespace_after = (peek_tok->type == CPP_PADDING
2780*38fd1498Szrj || (peek_tok->flags & PREV_WHITE));
2781*38fd1498Szrj node = pfile->cb.macro_to_expand (pfile, result);
2782*38fd1498Szrj if (node)
2783*38fd1498Szrj ret = enter_macro_context (pfile, node, result,
2784*38fd1498Szrj virt_loc);
2785*38fd1498Szrj else if (whitespace_after)
2786*38fd1498Szrj {
2787*38fd1498Szrj /* If macro_to_expand hook returned NULL and it
2788*38fd1498Szrj ate some tokens, see if we don't need to add
2789*38fd1498Szrj a padding token in between this and the
2790*38fd1498Szrj next token. */
2791*38fd1498Szrj peek_tok = cpp_peek_token (pfile, 0);
2792*38fd1498Szrj if (peek_tok->type != CPP_PADDING
2793*38fd1498Szrj && (peek_tok->flags & PREV_WHITE) == 0)
2794*38fd1498Szrj _cpp_push_token_context (pfile, NULL,
2795*38fd1498Szrj padding_token (pfile,
2796*38fd1498Szrj peek_tok), 1);
2797*38fd1498Szrj }
2798*38fd1498Szrj }
2799*38fd1498Szrj }
2800*38fd1498Szrj else
2801*38fd1498Szrj ret = enter_macro_context (pfile, node, result,
2802*38fd1498Szrj virt_loc);
2803*38fd1498Szrj if (ret)
2804*38fd1498Szrj {
2805*38fd1498Szrj if (pfile->state.in_directive || ret == 2)
2806*38fd1498Szrj continue;
2807*38fd1498Szrj result = padding_token (pfile, result);
2808*38fd1498Szrj goto out;
2809*38fd1498Szrj }
2810*38fd1498Szrj }
2811*38fd1498Szrj else
2812*38fd1498Szrj {
2813*38fd1498Szrj /* Flag this token as always unexpandable. FIXME: move this
2814*38fd1498Szrj to collect_args()?. */
2815*38fd1498Szrj cpp_token *t = _cpp_temp_token (pfile);
2816*38fd1498Szrj t->type = result->type;
2817*38fd1498Szrj t->flags = result->flags | NO_EXPAND;
2818*38fd1498Szrj t->val = result->val;
2819*38fd1498Szrj result = t;
2820*38fd1498Szrj }
2821*38fd1498Szrj
2822*38fd1498Szrj break;
2823*38fd1498Szrj }
2824*38fd1498Szrj
2825*38fd1498Szrj out:
2826*38fd1498Szrj if (location != NULL)
2827*38fd1498Szrj {
2828*38fd1498Szrj if (virt_loc == 0)
2829*38fd1498Szrj virt_loc = result->src_loc;
2830*38fd1498Szrj *location = virt_loc;
2831*38fd1498Szrj
2832*38fd1498Szrj if (!CPP_OPTION (pfile, track_macro_expansion)
2833*38fd1498Szrj && macro_of_context (pfile->context) != NULL)
2834*38fd1498Szrj /* We are in a macro expansion context, are not tracking
2835*38fd1498Szrj virtual location, but were asked to report the location
2836*38fd1498Szrj of the expansion point of the macro being expanded. */
2837*38fd1498Szrj *location = pfile->invocation_location;
2838*38fd1498Szrj
2839*38fd1498Szrj *location = maybe_adjust_loc_for_trad_cpp (pfile, *location);
2840*38fd1498Szrj }
2841*38fd1498Szrj
2842*38fd1498Szrj pfile->about_to_expand_macro_p = saved_about_to_expand_macro;
2843*38fd1498Szrj return result;
2844*38fd1498Szrj }
2845*38fd1498Szrj
2846*38fd1498Szrj /* External routine to get a token. Also used nearly everywhere
2847*38fd1498Szrj internally, except for places where we know we can safely call
2848*38fd1498Szrj _cpp_lex_token directly, such as lexing a directive name.
2849*38fd1498Szrj
2850*38fd1498Szrj Macro expansions and directives are transparently handled,
2851*38fd1498Szrj including entering included files. Thus tokens are post-macro
2852*38fd1498Szrj expansion, and after any intervening directives. External callers
2853*38fd1498Szrj see CPP_EOF only at EOF. Internal callers also see it when meeting
2854*38fd1498Szrj a directive inside a macro call, when at the end of a directive and
2855*38fd1498Szrj state.in_directive is still 1, and at the end of argument
2856*38fd1498Szrj pre-expansion. */
2857*38fd1498Szrj const cpp_token *
cpp_get_token(cpp_reader * pfile)2858*38fd1498Szrj cpp_get_token (cpp_reader *pfile)
2859*38fd1498Szrj {
2860*38fd1498Szrj return cpp_get_token_1 (pfile, NULL);
2861*38fd1498Szrj }
2862*38fd1498Szrj
2863*38fd1498Szrj /* Like cpp_get_token, but also returns a virtual token location
2864*38fd1498Szrj separate from the spelling location carried by the returned token.
2865*38fd1498Szrj
2866*38fd1498Szrj LOC is an out parameter; *LOC is set to the location "as expected
2867*38fd1498Szrj by the user". This matters when a token results from macro
2868*38fd1498Szrj expansion; in that case the token's spelling location indicates the
2869*38fd1498Szrj locus of the token in the definition of the macro but *LOC
2870*38fd1498Szrj virtually encodes all the other meaningful locuses associated to
2871*38fd1498Szrj the token.
2872*38fd1498Szrj
2873*38fd1498Szrj What? virtual location? Yes, virtual location.
2874*38fd1498Szrj
2875*38fd1498Szrj If the token results from macro expansion and if macro expansion
2876*38fd1498Szrj location tracking is enabled its virtual location encodes (at the
2877*38fd1498Szrj same time):
2878*38fd1498Szrj
2879*38fd1498Szrj - the spelling location of the token
2880*38fd1498Szrj
2881*38fd1498Szrj - the locus of the macro expansion point
2882*38fd1498Szrj
2883*38fd1498Szrj - the locus of the point where the token got instantiated as part
2884*38fd1498Szrj of the macro expansion process.
2885*38fd1498Szrj
2886*38fd1498Szrj You have to use the linemap API to get the locus you are interested
2887*38fd1498Szrj in from a given virtual location.
2888*38fd1498Szrj
2889*38fd1498Szrj Note however that virtual locations are not necessarily ordered for
2890*38fd1498Szrj relations '<' and '>'. One must use the function
2891*38fd1498Szrj linemap_location_before_p instead of using the relational operator
2892*38fd1498Szrj '<'.
2893*38fd1498Szrj
2894*38fd1498Szrj If macro expansion tracking is off and if the token results from
2895*38fd1498Szrj macro expansion the virtual location is the expansion point of the
2896*38fd1498Szrj macro that got expanded.
2897*38fd1498Szrj
2898*38fd1498Szrj When the token doesn't result from macro expansion, the virtual
2899*38fd1498Szrj location is just the same thing as its spelling location. */
2900*38fd1498Szrj
2901*38fd1498Szrj const cpp_token *
cpp_get_token_with_location(cpp_reader * pfile,source_location * loc)2902*38fd1498Szrj cpp_get_token_with_location (cpp_reader *pfile, source_location *loc)
2903*38fd1498Szrj {
2904*38fd1498Szrj return cpp_get_token_1 (pfile, loc);
2905*38fd1498Szrj }
2906*38fd1498Szrj
2907*38fd1498Szrj /* Returns true if we're expanding an object-like macro that was
2908*38fd1498Szrj defined in a system header. Just checks the macro at the top of
2909*38fd1498Szrj the stack. Used for diagnostic suppression. */
2910*38fd1498Szrj int
cpp_sys_macro_p(cpp_reader * pfile)2911*38fd1498Szrj cpp_sys_macro_p (cpp_reader *pfile)
2912*38fd1498Szrj {
2913*38fd1498Szrj cpp_hashnode *node = NULL;
2914*38fd1498Szrj
2915*38fd1498Szrj if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2916*38fd1498Szrj node = pfile->context->c.mc->macro_node;
2917*38fd1498Szrj else
2918*38fd1498Szrj node = pfile->context->c.macro;
2919*38fd1498Szrj
2920*38fd1498Szrj return node && node->value.macro && node->value.macro->syshdr;
2921*38fd1498Szrj }
2922*38fd1498Szrj
2923*38fd1498Szrj /* Read each token in, until end of the current file. Directives are
2924*38fd1498Szrj transparently processed. */
2925*38fd1498Szrj void
cpp_scan_nooutput(cpp_reader * pfile)2926*38fd1498Szrj cpp_scan_nooutput (cpp_reader *pfile)
2927*38fd1498Szrj {
2928*38fd1498Szrj /* Request a CPP_EOF token at the end of this file, rather than
2929*38fd1498Szrj transparently continuing with the including file. */
2930*38fd1498Szrj pfile->buffer->return_at_eof = true;
2931*38fd1498Szrj
2932*38fd1498Szrj pfile->state.discarding_output++;
2933*38fd1498Szrj pfile->state.prevent_expansion++;
2934*38fd1498Szrj
2935*38fd1498Szrj if (CPP_OPTION (pfile, traditional))
2936*38fd1498Szrj while (_cpp_read_logical_line_trad (pfile))
2937*38fd1498Szrj ;
2938*38fd1498Szrj else
2939*38fd1498Szrj while (cpp_get_token (pfile)->type != CPP_EOF)
2940*38fd1498Szrj ;
2941*38fd1498Szrj
2942*38fd1498Szrj pfile->state.discarding_output--;
2943*38fd1498Szrj pfile->state.prevent_expansion--;
2944*38fd1498Szrj }
2945*38fd1498Szrj
2946*38fd1498Szrj /* Step back one or more tokens obtained from the lexer. */
2947*38fd1498Szrj void
_cpp_backup_tokens_direct(cpp_reader * pfile,unsigned int count)2948*38fd1498Szrj _cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count)
2949*38fd1498Szrj {
2950*38fd1498Szrj pfile->lookaheads += count;
2951*38fd1498Szrj while (count--)
2952*38fd1498Szrj {
2953*38fd1498Szrj pfile->cur_token--;
2954*38fd1498Szrj if (pfile->cur_token == pfile->cur_run->base
2955*38fd1498Szrj /* Possible with -fpreprocessed and no leading #line. */
2956*38fd1498Szrj && pfile->cur_run->prev != NULL)
2957*38fd1498Szrj {
2958*38fd1498Szrj pfile->cur_run = pfile->cur_run->prev;
2959*38fd1498Szrj pfile->cur_token = pfile->cur_run->limit;
2960*38fd1498Szrj }
2961*38fd1498Szrj }
2962*38fd1498Szrj }
2963*38fd1498Szrj
2964*38fd1498Szrj /* Step back one (or more) tokens. Can only step back more than 1 if
2965*38fd1498Szrj they are from the lexer, and not from macro expansion. */
2966*38fd1498Szrj void
_cpp_backup_tokens(cpp_reader * pfile,unsigned int count)2967*38fd1498Szrj _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
2968*38fd1498Szrj {
2969*38fd1498Szrj if (pfile->context->prev == NULL)
2970*38fd1498Szrj _cpp_backup_tokens_direct (pfile, count);
2971*38fd1498Szrj else
2972*38fd1498Szrj {
2973*38fd1498Szrj if (count != 1)
2974*38fd1498Szrj abort ();
2975*38fd1498Szrj if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT)
2976*38fd1498Szrj FIRST (pfile->context).token--;
2977*38fd1498Szrj else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT)
2978*38fd1498Szrj FIRST (pfile->context).ptoken--;
2979*38fd1498Szrj else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2980*38fd1498Szrj {
2981*38fd1498Szrj FIRST (pfile->context).ptoken--;
2982*38fd1498Szrj if (pfile->context->c.macro)
2983*38fd1498Szrj {
2984*38fd1498Szrj macro_context *m = pfile->context->c.mc;
2985*38fd1498Szrj m->cur_virt_loc--;
2986*38fd1498Szrj gcc_checking_assert (m->cur_virt_loc >= m->virt_locs);
2987*38fd1498Szrj }
2988*38fd1498Szrj else
2989*38fd1498Szrj abort ();
2990*38fd1498Szrj }
2991*38fd1498Szrj else
2992*38fd1498Szrj abort ();
2993*38fd1498Szrj }
2994*38fd1498Szrj }
2995*38fd1498Szrj
2996*38fd1498Szrj /* #define directive parsing and handling. */
2997*38fd1498Szrj
2998*38fd1498Szrj /* Returns nonzero if a macro redefinition warning is required. */
2999*38fd1498Szrj static bool
warn_of_redefinition(cpp_reader * pfile,cpp_hashnode * node,const cpp_macro * macro2)3000*38fd1498Szrj warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
3001*38fd1498Szrj const cpp_macro *macro2)
3002*38fd1498Szrj {
3003*38fd1498Szrj const cpp_macro *macro1;
3004*38fd1498Szrj unsigned int i;
3005*38fd1498Szrj
3006*38fd1498Szrj /* Some redefinitions need to be warned about regardless. */
3007*38fd1498Szrj if (node->flags & NODE_WARN)
3008*38fd1498Szrj return true;
3009*38fd1498Szrj
3010*38fd1498Szrj /* Suppress warnings for builtins that lack the NODE_WARN flag,
3011*38fd1498Szrj unless Wbuiltin-macro-redefined. */
3012*38fd1498Szrj if (node->flags & NODE_BUILTIN
3013*38fd1498Szrj && (!pfile->cb.user_builtin_macro
3014*38fd1498Szrj || !pfile->cb.user_builtin_macro (pfile, node)))
3015*38fd1498Szrj return CPP_OPTION (pfile, warn_builtin_macro_redefined);
3016*38fd1498Szrj
3017*38fd1498Szrj /* Redefinitions of conditional (context-sensitive) macros, on
3018*38fd1498Szrj the other hand, must be allowed silently. */
3019*38fd1498Szrj if (node->flags & NODE_CONDITIONAL)
3020*38fd1498Szrj return false;
3021*38fd1498Szrj
3022*38fd1498Szrj /* Redefinition of a macro is allowed if and only if the old and new
3023*38fd1498Szrj definitions are the same. (6.10.3 paragraph 2). */
3024*38fd1498Szrj macro1 = node->value.macro;
3025*38fd1498Szrj
3026*38fd1498Szrj /* Don't check count here as it can be different in valid
3027*38fd1498Szrj traditional redefinitions with just whitespace differences. */
3028*38fd1498Szrj if (macro1->paramc != macro2->paramc
3029*38fd1498Szrj || macro1->fun_like != macro2->fun_like
3030*38fd1498Szrj || macro1->variadic != macro2->variadic)
3031*38fd1498Szrj return true;
3032*38fd1498Szrj
3033*38fd1498Szrj /* Check parameter spellings. */
3034*38fd1498Szrj for (i = 0; i < macro1->paramc; i++)
3035*38fd1498Szrj if (macro1->params[i] != macro2->params[i])
3036*38fd1498Szrj return true;
3037*38fd1498Szrj
3038*38fd1498Szrj /* Check the replacement text or tokens. */
3039*38fd1498Szrj if (CPP_OPTION (pfile, traditional))
3040*38fd1498Szrj return _cpp_expansions_different_trad (macro1, macro2);
3041*38fd1498Szrj
3042*38fd1498Szrj if (macro1->count != macro2->count)
3043*38fd1498Szrj return true;
3044*38fd1498Szrj
3045*38fd1498Szrj for (i = 0; i < macro1->count; i++)
3046*38fd1498Szrj if (!_cpp_equiv_tokens (¯o1->exp.tokens[i], ¯o2->exp.tokens[i]))
3047*38fd1498Szrj return true;
3048*38fd1498Szrj
3049*38fd1498Szrj return false;
3050*38fd1498Szrj }
3051*38fd1498Szrj
3052*38fd1498Szrj /* Free the definition of hashnode H. */
3053*38fd1498Szrj void
_cpp_free_definition(cpp_hashnode * h)3054*38fd1498Szrj _cpp_free_definition (cpp_hashnode *h)
3055*38fd1498Szrj {
3056*38fd1498Szrj /* Macros and assertions no longer have anything to free. */
3057*38fd1498Szrj h->type = NT_VOID;
3058*38fd1498Szrj /* Clear builtin flag in case of redefinition. */
3059*38fd1498Szrj h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED);
3060*38fd1498Szrj }
3061*38fd1498Szrj
3062*38fd1498Szrj /* Save parameter NODE (spelling SPELLING) to the parameter list of
3063*38fd1498Szrj macro MACRO. Returns zero on success, nonzero if the parameter is
3064*38fd1498Szrj a duplicate. */
3065*38fd1498Szrj bool
_cpp_save_parameter(cpp_reader * pfile,cpp_macro * macro,cpp_hashnode * node,cpp_hashnode * spelling)3066*38fd1498Szrj _cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node,
3067*38fd1498Szrj cpp_hashnode *spelling)
3068*38fd1498Szrj {
3069*38fd1498Szrj unsigned int len;
3070*38fd1498Szrj /* Constraint 6.10.3.6 - duplicate parameter names. */
3071*38fd1498Szrj if (node->flags & NODE_MACRO_ARG)
3072*38fd1498Szrj {
3073*38fd1498Szrj cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
3074*38fd1498Szrj NODE_NAME (node));
3075*38fd1498Szrj return true;
3076*38fd1498Szrj }
3077*38fd1498Szrj
3078*38fd1498Szrj if (BUFF_ROOM (pfile->a_buff)
3079*38fd1498Szrj < (macro->paramc + 1) * sizeof (cpp_hashnode *))
3080*38fd1498Szrj _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
3081*38fd1498Szrj
3082*38fd1498Szrj ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = spelling;
3083*38fd1498Szrj node->flags |= NODE_MACRO_ARG;
3084*38fd1498Szrj len = macro->paramc * sizeof (struct macro_arg_saved_data);
3085*38fd1498Szrj if (len > pfile->macro_buffer_len)
3086*38fd1498Szrj {
3087*38fd1498Szrj pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer,
3088*38fd1498Szrj len);
3089*38fd1498Szrj pfile->macro_buffer_len = len;
3090*38fd1498Szrj }
3091*38fd1498Szrj struct macro_arg_saved_data save;
3092*38fd1498Szrj save.value = node->value;
3093*38fd1498Szrj save.canonical_node = node;
3094*38fd1498Szrj ((struct macro_arg_saved_data *) pfile->macro_buffer)[macro->paramc - 1]
3095*38fd1498Szrj = save;
3096*38fd1498Szrj
3097*38fd1498Szrj node->value.arg_index = macro->paramc;
3098*38fd1498Szrj return false;
3099*38fd1498Szrj }
3100*38fd1498Szrj
3101*38fd1498Szrj /* Check the syntax of the parameters in a MACRO definition. Returns
3102*38fd1498Szrj false if an error occurs. */
3103*38fd1498Szrj static bool
parse_params(cpp_reader * pfile,cpp_macro * macro)3104*38fd1498Szrj parse_params (cpp_reader *pfile, cpp_macro *macro)
3105*38fd1498Szrj {
3106*38fd1498Szrj unsigned int prev_ident = 0;
3107*38fd1498Szrj
3108*38fd1498Szrj for (;;)
3109*38fd1498Szrj {
3110*38fd1498Szrj const cpp_token *token = _cpp_lex_token (pfile);
3111*38fd1498Szrj
3112*38fd1498Szrj switch (token->type)
3113*38fd1498Szrj {
3114*38fd1498Szrj default:
3115*38fd1498Szrj /* Allow/ignore comments in parameter lists if we are
3116*38fd1498Szrj preserving comments in macro expansions. */
3117*38fd1498Szrj if (token->type == CPP_COMMENT
3118*38fd1498Szrj && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
3119*38fd1498Szrj continue;
3120*38fd1498Szrj
3121*38fd1498Szrj cpp_error (pfile, CPP_DL_ERROR,
3122*38fd1498Szrj "\"%s\" may not appear in macro parameter list",
3123*38fd1498Szrj cpp_token_as_text (pfile, token));
3124*38fd1498Szrj return false;
3125*38fd1498Szrj
3126*38fd1498Szrj case CPP_NAME:
3127*38fd1498Szrj if (prev_ident)
3128*38fd1498Szrj {
3129*38fd1498Szrj cpp_error (pfile, CPP_DL_ERROR,
3130*38fd1498Szrj "macro parameters must be comma-separated");
3131*38fd1498Szrj return false;
3132*38fd1498Szrj }
3133*38fd1498Szrj prev_ident = 1;
3134*38fd1498Szrj
3135*38fd1498Szrj if (_cpp_save_parameter (pfile, macro, token->val.node.node,
3136*38fd1498Szrj token->val.node.spelling))
3137*38fd1498Szrj return false;
3138*38fd1498Szrj continue;
3139*38fd1498Szrj
3140*38fd1498Szrj case CPP_CLOSE_PAREN:
3141*38fd1498Szrj if (prev_ident || macro->paramc == 0)
3142*38fd1498Szrj return true;
3143*38fd1498Szrj
3144*38fd1498Szrj /* Fall through to pick up the error. */
3145*38fd1498Szrj /* FALLTHRU */
3146*38fd1498Szrj case CPP_COMMA:
3147*38fd1498Szrj if (!prev_ident)
3148*38fd1498Szrj {
3149*38fd1498Szrj cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
3150*38fd1498Szrj return false;
3151*38fd1498Szrj }
3152*38fd1498Szrj prev_ident = 0;
3153*38fd1498Szrj continue;
3154*38fd1498Szrj
3155*38fd1498Szrj case CPP_ELLIPSIS:
3156*38fd1498Szrj macro->variadic = 1;
3157*38fd1498Szrj if (!prev_ident)
3158*38fd1498Szrj {
3159*38fd1498Szrj _cpp_save_parameter (pfile, macro,
3160*38fd1498Szrj pfile->spec_nodes.n__VA_ARGS__,
3161*38fd1498Szrj pfile->spec_nodes.n__VA_ARGS__);
3162*38fd1498Szrj pfile->state.va_args_ok = 1;
3163*38fd1498Szrj if (! CPP_OPTION (pfile, c99)
3164*38fd1498Szrj && CPP_OPTION (pfile, cpp_pedantic)
3165*38fd1498Szrj && CPP_OPTION (pfile, warn_variadic_macros))
3166*38fd1498Szrj {
3167*38fd1498Szrj if (CPP_OPTION (pfile, cplusplus))
3168*38fd1498Szrj cpp_pedwarning
3169*38fd1498Szrj (pfile, CPP_W_VARIADIC_MACROS,
3170*38fd1498Szrj "anonymous variadic macros were introduced in C++11");
3171*38fd1498Szrj else
3172*38fd1498Szrj cpp_pedwarning
3173*38fd1498Szrj (pfile, CPP_W_VARIADIC_MACROS,
3174*38fd1498Szrj "anonymous variadic macros were introduced in C99");
3175*38fd1498Szrj }
3176*38fd1498Szrj else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
3177*38fd1498Szrj && ! CPP_OPTION (pfile, cplusplus))
3178*38fd1498Szrj cpp_error (pfile, CPP_DL_WARNING,
3179*38fd1498Szrj "anonymous variadic macros were introduced in C99");
3180*38fd1498Szrj }
3181*38fd1498Szrj else if (CPP_OPTION (pfile, cpp_pedantic)
3182*38fd1498Szrj && CPP_OPTION (pfile, warn_variadic_macros))
3183*38fd1498Szrj {
3184*38fd1498Szrj if (CPP_OPTION (pfile, cplusplus))
3185*38fd1498Szrj cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
3186*38fd1498Szrj "ISO C++ does not permit named variadic macros");
3187*38fd1498Szrj else
3188*38fd1498Szrj cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
3189*38fd1498Szrj "ISO C does not permit named variadic macros");
3190*38fd1498Szrj }
3191*38fd1498Szrj
3192*38fd1498Szrj /* We're at the end, and just expect a closing parenthesis. */
3193*38fd1498Szrj token = _cpp_lex_token (pfile);
3194*38fd1498Szrj if (token->type == CPP_CLOSE_PAREN)
3195*38fd1498Szrj return true;
3196*38fd1498Szrj /* Fall through. */
3197*38fd1498Szrj
3198*38fd1498Szrj case CPP_EOF:
3199*38fd1498Szrj cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
3200*38fd1498Szrj return false;
3201*38fd1498Szrj }
3202*38fd1498Szrj }
3203*38fd1498Szrj }
3204*38fd1498Szrj
3205*38fd1498Szrj /* Allocate room for a token from a macro's replacement list. */
3206*38fd1498Szrj static cpp_token *
alloc_expansion_token(cpp_reader * pfile,cpp_macro * macro)3207*38fd1498Szrj alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
3208*38fd1498Szrj {
3209*38fd1498Szrj if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
3210*38fd1498Szrj _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
3211*38fd1498Szrj
3212*38fd1498Szrj return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
3213*38fd1498Szrj }
3214*38fd1498Szrj
3215*38fd1498Szrj /* Lex a token from the expansion of MACRO, but mark parameters as we
3216*38fd1498Szrj find them and warn of traditional stringification. */
3217*38fd1498Szrj static cpp_token *
lex_expansion_token(cpp_reader * pfile,cpp_macro * macro)3218*38fd1498Szrj lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
3219*38fd1498Szrj {
3220*38fd1498Szrj cpp_token *token, *saved_cur_token;
3221*38fd1498Szrj
3222*38fd1498Szrj saved_cur_token = pfile->cur_token;
3223*38fd1498Szrj pfile->cur_token = alloc_expansion_token (pfile, macro);
3224*38fd1498Szrj token = _cpp_lex_direct (pfile);
3225*38fd1498Szrj pfile->cur_token = saved_cur_token;
3226*38fd1498Szrj
3227*38fd1498Szrj /* Is this a parameter? */
3228*38fd1498Szrj if (token->type == CPP_NAME
3229*38fd1498Szrj && (token->val.node.node->flags & NODE_MACRO_ARG) != 0)
3230*38fd1498Szrj {
3231*38fd1498Szrj cpp_hashnode *spelling = token->val.node.spelling;
3232*38fd1498Szrj token->type = CPP_MACRO_ARG;
3233*38fd1498Szrj token->val.macro_arg.arg_no = token->val.node.node->value.arg_index;
3234*38fd1498Szrj token->val.macro_arg.spelling = spelling;
3235*38fd1498Szrj }
3236*38fd1498Szrj else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
3237*38fd1498Szrj && (token->type == CPP_STRING || token->type == CPP_CHAR))
3238*38fd1498Szrj check_trad_stringification (pfile, macro, &token->val.str);
3239*38fd1498Szrj
3240*38fd1498Szrj return token;
3241*38fd1498Szrj }
3242*38fd1498Szrj
3243*38fd1498Szrj static bool
create_iso_definition(cpp_reader * pfile,cpp_macro * macro)3244*38fd1498Szrj create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
3245*38fd1498Szrj {
3246*38fd1498Szrj cpp_token *token;
3247*38fd1498Szrj const cpp_token *ctoken;
3248*38fd1498Szrj bool following_paste_op = false;
3249*38fd1498Szrj const char *paste_op_error_msg =
3250*38fd1498Szrj N_("'##' cannot appear at either end of a macro expansion");
3251*38fd1498Szrj unsigned int num_extra_tokens = 0;
3252*38fd1498Szrj
3253*38fd1498Szrj /* Get the first token of the expansion (or the '(' of a
3254*38fd1498Szrj function-like macro). */
3255*38fd1498Szrj ctoken = _cpp_lex_token (pfile);
3256*38fd1498Szrj
3257*38fd1498Szrj if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
3258*38fd1498Szrj {
3259*38fd1498Szrj bool ok = parse_params (pfile, macro);
3260*38fd1498Szrj macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
3261*38fd1498Szrj if (!ok)
3262*38fd1498Szrj return false;
3263*38fd1498Szrj
3264*38fd1498Szrj /* Success. Commit or allocate the parameter array. */
3265*38fd1498Szrj if (pfile->hash_table->alloc_subobject)
3266*38fd1498Szrj {
3267*38fd1498Szrj cpp_hashnode **params =
3268*38fd1498Szrj (cpp_hashnode **) pfile->hash_table->alloc_subobject
3269*38fd1498Szrj (sizeof (cpp_hashnode *) * macro->paramc);
3270*38fd1498Szrj memcpy (params, macro->params,
3271*38fd1498Szrj sizeof (cpp_hashnode *) * macro->paramc);
3272*38fd1498Szrj macro->params = params;
3273*38fd1498Szrj }
3274*38fd1498Szrj else
3275*38fd1498Szrj BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->params[macro->paramc];
3276*38fd1498Szrj macro->fun_like = 1;
3277*38fd1498Szrj }
3278*38fd1498Szrj else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
3279*38fd1498Szrj {
3280*38fd1498Szrj /* While ISO C99 requires whitespace before replacement text
3281*38fd1498Szrj in a macro definition, ISO C90 with TC1 allows characters
3282*38fd1498Szrj from the basic source character set there. */
3283*38fd1498Szrj if (CPP_OPTION (pfile, c99))
3284*38fd1498Szrj {
3285*38fd1498Szrj if (CPP_OPTION (pfile, cplusplus))
3286*38fd1498Szrj cpp_error (pfile, CPP_DL_PEDWARN,
3287*38fd1498Szrj "ISO C++11 requires whitespace after the macro name");
3288*38fd1498Szrj else
3289*38fd1498Szrj cpp_error (pfile, CPP_DL_PEDWARN,
3290*38fd1498Szrj "ISO C99 requires whitespace after the macro name");
3291*38fd1498Szrj }
3292*38fd1498Szrj else
3293*38fd1498Szrj {
3294*38fd1498Szrj int warntype = CPP_DL_WARNING;
3295*38fd1498Szrj switch (ctoken->type)
3296*38fd1498Szrj {
3297*38fd1498Szrj case CPP_ATSIGN:
3298*38fd1498Szrj case CPP_AT_NAME:
3299*38fd1498Szrj case CPP_OBJC_STRING:
3300*38fd1498Szrj /* '@' is not in basic character set. */
3301*38fd1498Szrj warntype = CPP_DL_PEDWARN;
3302*38fd1498Szrj break;
3303*38fd1498Szrj case CPP_OTHER:
3304*38fd1498Szrj /* Basic character set sans letters, digits and _. */
3305*38fd1498Szrj if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
3306*38fd1498Szrj ctoken->val.str.text[0]) == NULL)
3307*38fd1498Szrj warntype = CPP_DL_PEDWARN;
3308*38fd1498Szrj break;
3309*38fd1498Szrj default:
3310*38fd1498Szrj /* All other tokens start with a character from basic
3311*38fd1498Szrj character set. */
3312*38fd1498Szrj break;
3313*38fd1498Szrj }
3314*38fd1498Szrj cpp_error (pfile, warntype,
3315*38fd1498Szrj "missing whitespace after the macro name");
3316*38fd1498Szrj }
3317*38fd1498Szrj }
3318*38fd1498Szrj
3319*38fd1498Szrj if (macro->fun_like)
3320*38fd1498Szrj token = lex_expansion_token (pfile, macro);
3321*38fd1498Szrj else
3322*38fd1498Szrj {
3323*38fd1498Szrj token = alloc_expansion_token (pfile, macro);
3324*38fd1498Szrj *token = *ctoken;
3325*38fd1498Szrj }
3326*38fd1498Szrj
3327*38fd1498Szrj /* The argument doesn't matter here. */
3328*38fd1498Szrj vaopt_state vaopt_tracker (pfile, macro->variadic, true);
3329*38fd1498Szrj
3330*38fd1498Szrj for (;;)
3331*38fd1498Szrj {
3332*38fd1498Szrj /* Check the stringifying # constraint 6.10.3.2.1 of
3333*38fd1498Szrj function-like macros when lexing the subsequent token. */
3334*38fd1498Szrj if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
3335*38fd1498Szrj {
3336*38fd1498Szrj if (token->type == CPP_MACRO_ARG)
3337*38fd1498Szrj {
3338*38fd1498Szrj if (token->flags & PREV_WHITE)
3339*38fd1498Szrj token->flags |= SP_PREV_WHITE;
3340*38fd1498Szrj if (token[-1].flags & DIGRAPH)
3341*38fd1498Szrj token->flags |= SP_DIGRAPH;
3342*38fd1498Szrj token->flags &= ~PREV_WHITE;
3343*38fd1498Szrj token->flags |= STRINGIFY_ARG;
3344*38fd1498Szrj token->flags |= token[-1].flags & PREV_WHITE;
3345*38fd1498Szrj token[-1] = token[0];
3346*38fd1498Szrj macro->count--;
3347*38fd1498Szrj }
3348*38fd1498Szrj /* Let assembler get away with murder. */
3349*38fd1498Szrj else if (CPP_OPTION (pfile, lang) != CLK_ASM)
3350*38fd1498Szrj {
3351*38fd1498Szrj cpp_error (pfile, CPP_DL_ERROR,
3352*38fd1498Szrj "'#' is not followed by a macro parameter");
3353*38fd1498Szrj return false;
3354*38fd1498Szrj }
3355*38fd1498Szrj }
3356*38fd1498Szrj
3357*38fd1498Szrj if (token->type == CPP_EOF)
3358*38fd1498Szrj {
3359*38fd1498Szrj /* Paste operator constraint 6.10.3.3.1:
3360*38fd1498Szrj Token-paste ##, can appear in both object-like and
3361*38fd1498Szrj function-like macros, but not at the end. */
3362*38fd1498Szrj if (following_paste_op)
3363*38fd1498Szrj {
3364*38fd1498Szrj cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
3365*38fd1498Szrj return false;
3366*38fd1498Szrj }
3367*38fd1498Szrj break;
3368*38fd1498Szrj }
3369*38fd1498Szrj
3370*38fd1498Szrj /* Paste operator constraint 6.10.3.3.1. */
3371*38fd1498Szrj if (token->type == CPP_PASTE)
3372*38fd1498Szrj {
3373*38fd1498Szrj /* Token-paste ##, can appear in both object-like and
3374*38fd1498Szrj function-like macros, but not at the beginning. */
3375*38fd1498Szrj if (macro->count == 1)
3376*38fd1498Szrj {
3377*38fd1498Szrj cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
3378*38fd1498Szrj return false;
3379*38fd1498Szrj }
3380*38fd1498Szrj
3381*38fd1498Szrj if (token[-1].flags & PASTE_LEFT)
3382*38fd1498Szrj {
3383*38fd1498Szrj macro->extra_tokens = 1;
3384*38fd1498Szrj num_extra_tokens++;
3385*38fd1498Szrj token->val.token_no = macro->count - 1;
3386*38fd1498Szrj }
3387*38fd1498Szrj else
3388*38fd1498Szrj {
3389*38fd1498Szrj --macro->count;
3390*38fd1498Szrj token[-1].flags |= PASTE_LEFT;
3391*38fd1498Szrj if (token->flags & DIGRAPH)
3392*38fd1498Szrj token[-1].flags |= SP_DIGRAPH;
3393*38fd1498Szrj if (token->flags & PREV_WHITE)
3394*38fd1498Szrj token[-1].flags |= SP_PREV_WHITE;
3395*38fd1498Szrj }
3396*38fd1498Szrj }
3397*38fd1498Szrj
3398*38fd1498Szrj if (vaopt_tracker.update (token) == vaopt_state::ERROR)
3399*38fd1498Szrj return false;
3400*38fd1498Szrj
3401*38fd1498Szrj following_paste_op = (token->type == CPP_PASTE);
3402*38fd1498Szrj token = lex_expansion_token (pfile, macro);
3403*38fd1498Szrj }
3404*38fd1498Szrj
3405*38fd1498Szrj if (!vaopt_tracker.completed ())
3406*38fd1498Szrj return false;
3407*38fd1498Szrj
3408*38fd1498Szrj macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
3409*38fd1498Szrj macro->traditional = 0;
3410*38fd1498Szrj
3411*38fd1498Szrj /* Don't count the CPP_EOF. */
3412*38fd1498Szrj macro->count--;
3413*38fd1498Szrj
3414*38fd1498Szrj /* Clear whitespace on first token for warn_of_redefinition(). */
3415*38fd1498Szrj if (macro->count)
3416*38fd1498Szrj macro->exp.tokens[0].flags &= ~PREV_WHITE;
3417*38fd1498Szrj
3418*38fd1498Szrj /* Commit or allocate the memory. */
3419*38fd1498Szrj if (pfile->hash_table->alloc_subobject)
3420*38fd1498Szrj {
3421*38fd1498Szrj cpp_token *tokns =
3422*38fd1498Szrj (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token)
3423*38fd1498Szrj * macro->count);
3424*38fd1498Szrj if (num_extra_tokens)
3425*38fd1498Szrj {
3426*38fd1498Szrj /* Place second and subsequent ## or %:%: tokens in
3427*38fd1498Szrj sequences of consecutive such tokens at the end of the
3428*38fd1498Szrj list to preserve information about where they appear, how
3429*38fd1498Szrj they are spelt and whether they are preceded by
3430*38fd1498Szrj whitespace without otherwise interfering with macro
3431*38fd1498Szrj expansion. */
3432*38fd1498Szrj cpp_token *normal_dest = tokns;
3433*38fd1498Szrj cpp_token *extra_dest = tokns + macro->count - num_extra_tokens;
3434*38fd1498Szrj unsigned int i;
3435*38fd1498Szrj for (i = 0; i < macro->count; i++)
3436*38fd1498Szrj {
3437*38fd1498Szrj if (macro->exp.tokens[i].type == CPP_PASTE)
3438*38fd1498Szrj *extra_dest++ = macro->exp.tokens[i];
3439*38fd1498Szrj else
3440*38fd1498Szrj *normal_dest++ = macro->exp.tokens[i];
3441*38fd1498Szrj }
3442*38fd1498Szrj }
3443*38fd1498Szrj else
3444*38fd1498Szrj memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
3445*38fd1498Szrj macro->exp.tokens = tokns;
3446*38fd1498Szrj }
3447*38fd1498Szrj else
3448*38fd1498Szrj BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->exp.tokens[macro->count];
3449*38fd1498Szrj
3450*38fd1498Szrj return true;
3451*38fd1498Szrj }
3452*38fd1498Szrj
3453*38fd1498Szrj /* Parse a macro and save its expansion. Returns nonzero on success. */
3454*38fd1498Szrj bool
_cpp_create_definition(cpp_reader * pfile,cpp_hashnode * node)3455*38fd1498Szrj _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
3456*38fd1498Szrj {
3457*38fd1498Szrj cpp_macro *macro;
3458*38fd1498Szrj unsigned int i;
3459*38fd1498Szrj bool ok;
3460*38fd1498Szrj
3461*38fd1498Szrj if (pfile->hash_table->alloc_subobject)
3462*38fd1498Szrj macro = (cpp_macro *) pfile->hash_table->alloc_subobject
3463*38fd1498Szrj (sizeof (cpp_macro));
3464*38fd1498Szrj else
3465*38fd1498Szrj macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
3466*38fd1498Szrj macro->line = pfile->directive_line;
3467*38fd1498Szrj macro->params = 0;
3468*38fd1498Szrj macro->paramc = 0;
3469*38fd1498Szrj macro->variadic = 0;
3470*38fd1498Szrj macro->used = !CPP_OPTION (pfile, warn_unused_macros);
3471*38fd1498Szrj macro->count = 0;
3472*38fd1498Szrj macro->fun_like = 0;
3473*38fd1498Szrj macro->extra_tokens = 0;
3474*38fd1498Szrj /* To suppress some diagnostics. */
3475*38fd1498Szrj macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
3476*38fd1498Szrj
3477*38fd1498Szrj if (CPP_OPTION (pfile, traditional))
3478*38fd1498Szrj ok = _cpp_create_trad_definition (pfile, macro);
3479*38fd1498Szrj else
3480*38fd1498Szrj {
3481*38fd1498Szrj ok = create_iso_definition (pfile, macro);
3482*38fd1498Szrj
3483*38fd1498Szrj /* We set the type for SEEN_EOL() in directives.c.
3484*38fd1498Szrj
3485*38fd1498Szrj Longer term we should lex the whole line before coming here,
3486*38fd1498Szrj and just copy the expansion. */
3487*38fd1498Szrj
3488*38fd1498Szrj /* Stop the lexer accepting __VA_ARGS__. */
3489*38fd1498Szrj pfile->state.va_args_ok = 0;
3490*38fd1498Szrj }
3491*38fd1498Szrj
3492*38fd1498Szrj /* Clear the fast argument lookup indices. */
3493*38fd1498Szrj for (i = macro->paramc; i-- > 0; )
3494*38fd1498Szrj {
3495*38fd1498Szrj struct macro_arg_saved_data *save =
3496*38fd1498Szrj &((struct macro_arg_saved_data *) pfile->macro_buffer)[i];
3497*38fd1498Szrj struct cpp_hashnode *node = save->canonical_node;
3498*38fd1498Szrj node->flags &= ~ NODE_MACRO_ARG;
3499*38fd1498Szrj node->value = save->value;
3500*38fd1498Szrj }
3501*38fd1498Szrj
3502*38fd1498Szrj if (!ok)
3503*38fd1498Szrj return ok;
3504*38fd1498Szrj
3505*38fd1498Szrj if (node->type == NT_MACRO)
3506*38fd1498Szrj {
3507*38fd1498Szrj if (CPP_OPTION (pfile, warn_unused_macros))
3508*38fd1498Szrj _cpp_warn_if_unused_macro (pfile, node, NULL);
3509*38fd1498Szrj
3510*38fd1498Szrj if (warn_of_redefinition (pfile, node, macro))
3511*38fd1498Szrj {
3512*38fd1498Szrj const int reason = ((node->flags & NODE_BUILTIN)
3513*38fd1498Szrj && !(node->flags & NODE_WARN))
3514*38fd1498Szrj ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
3515*38fd1498Szrj
3516*38fd1498Szrj bool warned =
3517*38fd1498Szrj cpp_pedwarning_with_line (pfile, reason,
3518*38fd1498Szrj pfile->directive_line, 0,
3519*38fd1498Szrj "\"%s\" redefined", NODE_NAME (node));
3520*38fd1498Szrj
3521*38fd1498Szrj if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
3522*38fd1498Szrj cpp_error_with_line (pfile, CPP_DL_NOTE,
3523*38fd1498Szrj node->value.macro->line, 0,
3524*38fd1498Szrj "this is the location of the previous definition");
3525*38fd1498Szrj }
3526*38fd1498Szrj }
3527*38fd1498Szrj
3528*38fd1498Szrj if (node->type != NT_VOID)
3529*38fd1498Szrj _cpp_free_definition (node);
3530*38fd1498Szrj
3531*38fd1498Szrj /* Enter definition in hash table. */
3532*38fd1498Szrj node->type = NT_MACRO;
3533*38fd1498Szrj node->value.macro = macro;
3534*38fd1498Szrj if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))
3535*38fd1498Szrj && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS")
3536*38fd1498Szrj /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
3537*38fd1498Szrj in the C standard, as something that one must use in C++.
3538*38fd1498Szrj However DR#593 and C++11 indicate that they play no role in C++.
3539*38fd1498Szrj We special-case them anyway. */
3540*38fd1498Szrj && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS")
3541*38fd1498Szrj && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS"))
3542*38fd1498Szrj node->flags |= NODE_WARN;
3543*38fd1498Szrj
3544*38fd1498Szrj /* If user defines one of the conditional macros, remove the
3545*38fd1498Szrj conditional flag */
3546*38fd1498Szrj node->flags &= ~NODE_CONDITIONAL;
3547*38fd1498Szrj
3548*38fd1498Szrj return ok;
3549*38fd1498Szrj }
3550*38fd1498Szrj
3551*38fd1498Szrj /* Warn if a token in STRING matches one of a function-like MACRO's
3552*38fd1498Szrj parameters. */
3553*38fd1498Szrj static void
check_trad_stringification(cpp_reader * pfile,const cpp_macro * macro,const cpp_string * string)3554*38fd1498Szrj check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
3555*38fd1498Szrj const cpp_string *string)
3556*38fd1498Szrj {
3557*38fd1498Szrj unsigned int i, len;
3558*38fd1498Szrj const uchar *p, *q, *limit;
3559*38fd1498Szrj
3560*38fd1498Szrj /* Loop over the string. */
3561*38fd1498Szrj limit = string->text + string->len - 1;
3562*38fd1498Szrj for (p = string->text + 1; p < limit; p = q)
3563*38fd1498Szrj {
3564*38fd1498Szrj /* Find the start of an identifier. */
3565*38fd1498Szrj while (p < limit && !is_idstart (*p))
3566*38fd1498Szrj p++;
3567*38fd1498Szrj
3568*38fd1498Szrj /* Find the end of the identifier. */
3569*38fd1498Szrj q = p;
3570*38fd1498Szrj while (q < limit && is_idchar (*q))
3571*38fd1498Szrj q++;
3572*38fd1498Szrj
3573*38fd1498Szrj len = q - p;
3574*38fd1498Szrj
3575*38fd1498Szrj /* Loop over the function macro arguments to see if the
3576*38fd1498Szrj identifier inside the string matches one of them. */
3577*38fd1498Szrj for (i = 0; i < macro->paramc; i++)
3578*38fd1498Szrj {
3579*38fd1498Szrj const cpp_hashnode *node = macro->params[i];
3580*38fd1498Szrj
3581*38fd1498Szrj if (NODE_LEN (node) == len
3582*38fd1498Szrj && !memcmp (p, NODE_NAME (node), len))
3583*38fd1498Szrj {
3584*38fd1498Szrj cpp_warning (pfile, CPP_W_TRADITIONAL,
3585*38fd1498Szrj "macro argument \"%s\" would be stringified in traditional C",
3586*38fd1498Szrj NODE_NAME (node));
3587*38fd1498Szrj break;
3588*38fd1498Szrj }
3589*38fd1498Szrj }
3590*38fd1498Szrj }
3591*38fd1498Szrj }
3592*38fd1498Szrj
3593*38fd1498Szrj /* Returns true of NODE is a function-like macro. */
3594*38fd1498Szrj bool
cpp_fun_like_macro_p(cpp_hashnode * node)3595*38fd1498Szrj cpp_fun_like_macro_p (cpp_hashnode *node)
3596*38fd1498Szrj {
3597*38fd1498Szrj return (node->type == NT_MACRO
3598*38fd1498Szrj && (node->flags & (NODE_BUILTIN | NODE_MACRO_ARG)) == 0
3599*38fd1498Szrj && node->value.macro->fun_like);
3600*38fd1498Szrj }
3601*38fd1498Szrj
3602*38fd1498Szrj /* Returns the name, arguments and expansion of a macro, in a format
3603*38fd1498Szrj suitable to be read back in again, and therefore also for DWARF 2
3604*38fd1498Szrj debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
3605*38fd1498Szrj Caller is expected to generate the "#define" bit if needed. The
3606*38fd1498Szrj returned text is temporary, and automatically freed later. */
3607*38fd1498Szrj const unsigned char *
cpp_macro_definition(cpp_reader * pfile,cpp_hashnode * node)3608*38fd1498Szrj cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
3609*38fd1498Szrj {
3610*38fd1498Szrj unsigned int i, len;
3611*38fd1498Szrj const cpp_macro *macro;
3612*38fd1498Szrj unsigned char *buffer;
3613*38fd1498Szrj
3614*38fd1498Szrj if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
3615*38fd1498Szrj {
3616*38fd1498Szrj if (node->type != NT_MACRO
3617*38fd1498Szrj || !pfile->cb.user_builtin_macro
3618*38fd1498Szrj || !pfile->cb.user_builtin_macro (pfile, node))
3619*38fd1498Szrj {
3620*38fd1498Szrj cpp_error (pfile, CPP_DL_ICE,
3621*38fd1498Szrj "invalid hash type %d in cpp_macro_definition",
3622*38fd1498Szrj node->type);
3623*38fd1498Szrj return 0;
3624*38fd1498Szrj }
3625*38fd1498Szrj }
3626*38fd1498Szrj
3627*38fd1498Szrj macro = node->value.macro;
3628*38fd1498Szrj /* Calculate length. */
3629*38fd1498Szrj len = NODE_LEN (node) * 10 + 2; /* ' ' and NUL. */
3630*38fd1498Szrj if (macro->fun_like)
3631*38fd1498Szrj {
3632*38fd1498Szrj len += 4; /* "()" plus possible final ".." of named
3633*38fd1498Szrj varargs (we have + 1 below). */
3634*38fd1498Szrj for (i = 0; i < macro->paramc; i++)
3635*38fd1498Szrj len += NODE_LEN (macro->params[i]) + 1; /* "," */
3636*38fd1498Szrj }
3637*38fd1498Szrj
3638*38fd1498Szrj /* This should match below where we fill in the buffer. */
3639*38fd1498Szrj if (CPP_OPTION (pfile, traditional))
3640*38fd1498Szrj len += _cpp_replacement_text_len (macro);
3641*38fd1498Szrj else
3642*38fd1498Szrj {
3643*38fd1498Szrj unsigned int count = macro_real_token_count (macro);
3644*38fd1498Szrj for (i = 0; i < count; i++)
3645*38fd1498Szrj {
3646*38fd1498Szrj cpp_token *token = ¯o->exp.tokens[i];
3647*38fd1498Szrj
3648*38fd1498Szrj if (token->type == CPP_MACRO_ARG)
3649*38fd1498Szrj len += NODE_LEN (token->val.macro_arg.spelling);
3650*38fd1498Szrj else
3651*38fd1498Szrj len += cpp_token_len (token);
3652*38fd1498Szrj
3653*38fd1498Szrj if (token->flags & STRINGIFY_ARG)
3654*38fd1498Szrj len++; /* "#" */
3655*38fd1498Szrj if (token->flags & PASTE_LEFT)
3656*38fd1498Szrj len += 3; /* " ##" */
3657*38fd1498Szrj if (token->flags & PREV_WHITE)
3658*38fd1498Szrj len++; /* " " */
3659*38fd1498Szrj }
3660*38fd1498Szrj }
3661*38fd1498Szrj
3662*38fd1498Szrj if (len > pfile->macro_buffer_len)
3663*38fd1498Szrj {
3664*38fd1498Szrj pfile->macro_buffer = XRESIZEVEC (unsigned char,
3665*38fd1498Szrj pfile->macro_buffer, len);
3666*38fd1498Szrj pfile->macro_buffer_len = len;
3667*38fd1498Szrj }
3668*38fd1498Szrj
3669*38fd1498Szrj /* Fill in the buffer. Start with the macro name. */
3670*38fd1498Szrj buffer = pfile->macro_buffer;
3671*38fd1498Szrj buffer = _cpp_spell_ident_ucns (buffer, node);
3672*38fd1498Szrj
3673*38fd1498Szrj /* Parameter names. */
3674*38fd1498Szrj if (macro->fun_like)
3675*38fd1498Szrj {
3676*38fd1498Szrj *buffer++ = '(';
3677*38fd1498Szrj for (i = 0; i < macro->paramc; i++)
3678*38fd1498Szrj {
3679*38fd1498Szrj cpp_hashnode *param = macro->params[i];
3680*38fd1498Szrj
3681*38fd1498Szrj if (param != pfile->spec_nodes.n__VA_ARGS__)
3682*38fd1498Szrj {
3683*38fd1498Szrj memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
3684*38fd1498Szrj buffer += NODE_LEN (param);
3685*38fd1498Szrj }
3686*38fd1498Szrj
3687*38fd1498Szrj if (i + 1 < macro->paramc)
3688*38fd1498Szrj /* Don't emit a space after the comma here; we're trying
3689*38fd1498Szrj to emit a Dwarf-friendly definition, and the Dwarf spec
3690*38fd1498Szrj forbids spaces in the argument list. */
3691*38fd1498Szrj *buffer++ = ',';
3692*38fd1498Szrj else if (macro->variadic)
3693*38fd1498Szrj *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
3694*38fd1498Szrj }
3695*38fd1498Szrj *buffer++ = ')';
3696*38fd1498Szrj }
3697*38fd1498Szrj
3698*38fd1498Szrj /* The Dwarf spec requires a space after the macro name, even if the
3699*38fd1498Szrj definition is the empty string. */
3700*38fd1498Szrj *buffer++ = ' ';
3701*38fd1498Szrj
3702*38fd1498Szrj if (CPP_OPTION (pfile, traditional))
3703*38fd1498Szrj buffer = _cpp_copy_replacement_text (macro, buffer);
3704*38fd1498Szrj else if (macro->count)
3705*38fd1498Szrj /* Expansion tokens. */
3706*38fd1498Szrj {
3707*38fd1498Szrj unsigned int count = macro_real_token_count (macro);
3708*38fd1498Szrj for (i = 0; i < count; i++)
3709*38fd1498Szrj {
3710*38fd1498Szrj cpp_token *token = ¯o->exp.tokens[i];
3711*38fd1498Szrj
3712*38fd1498Szrj if (token->flags & PREV_WHITE)
3713*38fd1498Szrj *buffer++ = ' ';
3714*38fd1498Szrj if (token->flags & STRINGIFY_ARG)
3715*38fd1498Szrj *buffer++ = '#';
3716*38fd1498Szrj
3717*38fd1498Szrj if (token->type == CPP_MACRO_ARG)
3718*38fd1498Szrj {
3719*38fd1498Szrj memcpy (buffer,
3720*38fd1498Szrj NODE_NAME (token->val.macro_arg.spelling),
3721*38fd1498Szrj NODE_LEN (token->val.macro_arg.spelling));
3722*38fd1498Szrj buffer += NODE_LEN (token->val.macro_arg.spelling);
3723*38fd1498Szrj }
3724*38fd1498Szrj else
3725*38fd1498Szrj buffer = cpp_spell_token (pfile, token, buffer, true);
3726*38fd1498Szrj
3727*38fd1498Szrj if (token->flags & PASTE_LEFT)
3728*38fd1498Szrj {
3729*38fd1498Szrj *buffer++ = ' ';
3730*38fd1498Szrj *buffer++ = '#';
3731*38fd1498Szrj *buffer++ = '#';
3732*38fd1498Szrj /* Next has PREV_WHITE; see _cpp_create_definition. */
3733*38fd1498Szrj }
3734*38fd1498Szrj }
3735*38fd1498Szrj }
3736*38fd1498Szrj
3737*38fd1498Szrj *buffer = '\0';
3738*38fd1498Szrj return pfile->macro_buffer;
3739*38fd1498Szrj }
3740*38fd1498Szrj
3741*38fd1498Szrj /* Get the line at which the macro was defined. */
3742*38fd1498Szrj
3743*38fd1498Szrj source_location
cpp_macro_definition_location(cpp_hashnode * node)3744*38fd1498Szrj cpp_macro_definition_location (cpp_hashnode *node)
3745*38fd1498Szrj {
3746*38fd1498Szrj return node->value.macro->line;
3747*38fd1498Szrj }
3748