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