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