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