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

source code of libcpp/macro.cc