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