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