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