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

source code of libcpp/macro.cc