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