25f4a361189d13b55f0bd2e31b48758b973dc2c7
[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 ((node->flags & NODE_BUILTIN) && !(node->flags & NODE_USED))
1277 {
1278 node->flags |= NODE_USED;
1279 if ((!pfile->cb.user_builtin_macro
1280 || !pfile->cb.user_builtin_macro (pfile, node))
1281 && pfile->cb.used_define)
1282 pfile->cb.used_define (pfile, pfile->directive_line, node);
1283 }
1284
1285 if (cpp_user_macro_p (node))
1286 {
1287 cpp_macro *macro = node->value.macro;
1288 _cpp_buff *pragma_buff = NULL;
1289
1290 if (macro->fun_like)
1291 {
1292 _cpp_buff *buff;
1293 unsigned num_args = 0;
1294
1295 pfile->state.prevent_expansion++;
1296 pfile->keep_tokens++;
1297 pfile->state.parsing_args = 1;
1298 buff = funlike_invocation_p (pfile, node, &pragma_buff,
1299 &num_args);
1300 pfile->state.parsing_args = 0;
1301 pfile->keep_tokens--;
1302 pfile->state.prevent_expansion--;
1303
1304 if (buff == NULL)
1305 {
1306 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
1307 cpp_warning (pfile, CPP_W_TRADITIONAL,
1308 "function-like macro \"%s\" must be used with arguments in traditional C",
1309 NODE_NAME (node));
1310
1311 if (pragma_buff)
1312 _cpp_release_buff (pfile, pragma_buff);
1313
1314 pfile->about_to_expand_macro_p = false;
1315 return 0;
1316 }
1317
1318 if (macro->paramc > 0)
1319 replace_args (pfile, node, macro,
1320 (macro_arg *) buff->base,
1321 location);
1322 /* Free the memory used by the arguments of this
1323 function-like macro. This memory has been allocated by
1324 funlike_invocation_p and by replace_args. */
1325 delete_macro_args (buff, num_args);
1326 }
1327
1328 /* Disable the macro within its expansion. */
1329 node->flags |= NODE_DISABLED;
1330
1331 if (!(node->flags & NODE_USED))
1332 {
1333 node->flags |= NODE_USED;
1334 if (pfile->cb.used_define)
1335 pfile->cb.used_define (pfile, pfile->directive_line, node);
1336 }
1337
1338 if (pfile->cb.used)
1339 pfile->cb.used (pfile, location, node);
1340
1341 macro->used = 1;
1342
1343 if (macro->paramc == 0)
1344 {
1345 unsigned tokens_count = macro_real_token_count (macro);
1346 if (CPP_OPTION (pfile, track_macro_expansion))
1347 {
1348 unsigned int i;
1349 const cpp_token *src = macro->exp.tokens;
1350 const line_map_macro *map;
1351 source_location *virt_locs = NULL;
1352 _cpp_buff *macro_tokens
1353 = tokens_buff_new (pfile, tokens_count, &virt_locs);
1354
1355 /* Create a macro map to record the locations of the
1356 tokens that are involved in the expansion. LOCATION
1357 is the location of the macro expansion point. */
1358 map = linemap_enter_macro (pfile->line_table,
1359 node, location, tokens_count);
1360 for (i = 0; i < tokens_count; ++i)
1361 {
1362 tokens_buff_add_token (macro_tokens, virt_locs,
1363 src, src->src_loc,
1364 src->src_loc, map, i);
1365 ++src;
1366 }
1367 push_extended_tokens_context (pfile, node,
1368 macro_tokens,
1369 virt_locs,
1370 (const cpp_token **)
1371 macro_tokens->base,
1372 tokens_count);
1373 }
1374 else
1375 _cpp_push_token_context (pfile, node, macro->exp.tokens,
1376 tokens_count);
1377 num_macro_tokens_counter += tokens_count;
1378 }
1379
1380 if (pragma_buff)
1381 {
1382 if (!pfile->state.in_directive)
1383 _cpp_push_token_context (pfile, NULL,
1384 padding_token (pfile, result), 1);
1385 do
1386 {
1387 unsigned tokens_count;
1388 _cpp_buff *tail = pragma_buff->next;
1389 pragma_buff->next = NULL;
1390 tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff)
1391 - (const cpp_token **) pragma_buff->base);
1392 push_ptoken_context (pfile, NULL, pragma_buff,
1393 (const cpp_token **) pragma_buff->base,
1394 tokens_count);
1395 pragma_buff = tail;
1396 if (!CPP_OPTION (pfile, track_macro_expansion))
1397 num_macro_tokens_counter += tokens_count;
1398
1399 }
1400 while (pragma_buff != NULL);
1401 pfile->about_to_expand_macro_p = false;
1402 return 2;
1403 }
1404
1405 pfile->about_to_expand_macro_p = false;
1406 return 1;
1407 }
1408
1409 pfile->about_to_expand_macro_p = false;
1410 /* Handle built-in macros and the _Pragma operator. */
1411 {
1412 source_location expand_loc;
1413
1414 if (/* The top-level macro invocation that triggered the expansion
1415 we are looking at is with a function-like user macro ... */
1416 cpp_fun_like_macro_p (pfile->top_most_macro_node)
1417 /* ... and we are tracking the macro expansion. */
1418 && CPP_OPTION (pfile, track_macro_expansion))
1419 /* Then the location of the end of the macro invocation is the
1420 location of the expansion point of this macro. */
1421 expand_loc = location;
1422 else
1423 /* Otherwise, the location of the end of the macro invocation is
1424 the location of the expansion point of that top-level macro
1425 invocation. */
1426 expand_loc = pfile->invocation_location;
1427
1428 return builtin_macro (pfile, node, location, expand_loc);
1429 }
1430 }
1431
1432 /* De-allocate the memory used by BUFF which is an array of instances
1433 of macro_arg. NUM_ARGS is the number of instances of macro_arg
1434 present in BUFF. */
1435 static void
1436 delete_macro_args (_cpp_buff *buff, unsigned num_args)
1437 {
1438 macro_arg *macro_args;
1439 unsigned i;
1440
1441 if (buff == NULL)
1442 return;
1443
1444 macro_args = (macro_arg *) buff->base;
1445
1446 /* Walk instances of macro_arg to free their expanded tokens as well
1447 as their macro_arg::virt_locs members. */
1448 for (i = 0; i < num_args; ++i)
1449 {
1450 if (macro_args[i].expanded)
1451 {
1452 free (macro_args[i].expanded);
1453 macro_args[i].expanded = NULL;
1454 }
1455 if (macro_args[i].virt_locs)
1456 {
1457 free (macro_args[i].virt_locs);
1458 macro_args[i].virt_locs = NULL;
1459 }
1460 if (macro_args[i].expanded_virt_locs)
1461 {
1462 free (macro_args[i].expanded_virt_locs);
1463 macro_args[i].expanded_virt_locs = NULL;
1464 }
1465 }
1466 _cpp_free_buff (buff);
1467 }
1468
1469 /* Set the INDEXth token of the macro argument ARG. TOKEN is the token
1470 to set, LOCATION is its virtual location. "Virtual" location means
1471 the location that encodes loci across macro expansion. Otherwise
1472 it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the
1473 argument ARG is supposed to contain. Note that ARG must be
1474 tailored so that it has enough room to contain INDEX + 1 numbers of
1475 tokens, at least. */
1476 static void
1477 set_arg_token (macro_arg *arg, const cpp_token *token,
1478 source_location location, size_t index,
1479 enum macro_arg_token_kind kind,
1480 bool track_macro_exp_p)
1481 {
1482 const cpp_token **token_ptr;
1483 source_location *loc = NULL;
1484
1485 token_ptr =
1486 arg_token_ptr_at (arg, index, kind,
1487 track_macro_exp_p ? &loc : NULL);
1488 *token_ptr = token;
1489
1490 if (loc != NULL)
1491 {
1492 /* We can't set the location of a stringified argument
1493 token and we can't set any location if we aren't tracking
1494 macro expansion locations. */
1495 gcc_checking_assert (kind != MACRO_ARG_TOKEN_STRINGIFIED
1496 && track_macro_exp_p);
1497 *loc = location;
1498 }
1499 }
1500
1501 /* Get the pointer to the location of the argument token of the
1502 function-like macro argument ARG. This function must be called
1503 only when we -ftrack-macro-expansion is on. */
1504 static const source_location *
1505 get_arg_token_location (const macro_arg *arg,
1506 enum macro_arg_token_kind kind)
1507 {
1508 const source_location *loc = NULL;
1509 const cpp_token **token_ptr =
1510 arg_token_ptr_at (arg, 0, kind, (source_location **) &loc);
1511
1512 if (token_ptr == NULL)
1513 return NULL;
1514
1515 return loc;
1516 }
1517
1518 /* Return the pointer to the INDEXth token of the macro argument ARG.
1519 KIND specifies the kind of token the macro argument ARG contains.
1520 If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
1521 of the virtual location of the returned token if the
1522 -ftrack-macro-expansion flag is on; otherwise, it's set to the
1523 spelling location of the returned token. */
1524 static const cpp_token **
1525 arg_token_ptr_at (const macro_arg *arg, size_t index,
1526 enum macro_arg_token_kind kind,
1527 source_location **virt_location)
1528 {
1529 const cpp_token **tokens_ptr = NULL;
1530
1531 switch (kind)
1532 {
1533 case MACRO_ARG_TOKEN_NORMAL:
1534 tokens_ptr = arg->first;
1535 break;
1536 case MACRO_ARG_TOKEN_STRINGIFIED:
1537 tokens_ptr = (const cpp_token **) &arg->stringified;
1538 break;
1539 case MACRO_ARG_TOKEN_EXPANDED:
1540 tokens_ptr = arg->expanded;
1541 break;
1542 }
1543
1544 if (tokens_ptr == NULL)
1545 /* This can happen for e.g, an empty token argument to a
1546 funtion-like macro. */
1547 return tokens_ptr;
1548
1549 if (virt_location)
1550 {
1551 if (kind == MACRO_ARG_TOKEN_NORMAL)
1552 *virt_location = &arg->virt_locs[index];
1553 else if (kind == MACRO_ARG_TOKEN_EXPANDED)
1554 *virt_location = &arg->expanded_virt_locs[index];
1555 else if (kind == MACRO_ARG_TOKEN_STRINGIFIED)
1556 *virt_location =
1557 (source_location *) &tokens_ptr[index]->src_loc;
1558 }
1559 return &tokens_ptr[index];
1560 }
1561
1562 /* Initialize an iterator so that it iterates over the tokens of a
1563 function-like macro argument. KIND is the kind of tokens we want
1564 ITER to iterate over. TOKEN_PTR points the first token ITER will
1565 iterate over. */
1566 static void
1567 macro_arg_token_iter_init (macro_arg_token_iter *iter,
1568 bool track_macro_exp_p,
1569 enum macro_arg_token_kind kind,
1570 const macro_arg *arg,
1571 const cpp_token **token_ptr)
1572 {
1573 iter->track_macro_exp_p = track_macro_exp_p;
1574 iter->kind = kind;
1575 iter->token_ptr = token_ptr;
1576 /* Unconditionally initialize this so that the compiler doesn't warn
1577 about iter->location_ptr being possibly uninitialized later after
1578 this code has been inlined somewhere. */
1579 iter->location_ptr = NULL;
1580 if (track_macro_exp_p)
1581 iter->location_ptr = get_arg_token_location (arg, kind);
1582 #if CHECKING_P
1583 iter->num_forwards = 0;
1584 if (track_macro_exp_p
1585 && token_ptr != NULL
1586 && iter->location_ptr == NULL)
1587 abort ();
1588 #endif
1589 }
1590
1591 /* Move the iterator one token forward. Note that if IT was
1592 initialized on an argument that has a stringified token, moving it
1593 forward doesn't make sense as a stringified token is essentially one
1594 string. */
1595 static void
1596 macro_arg_token_iter_forward (macro_arg_token_iter *it)
1597 {
1598 switch (it->kind)
1599 {
1600 case MACRO_ARG_TOKEN_NORMAL:
1601 case MACRO_ARG_TOKEN_EXPANDED:
1602 it->token_ptr++;
1603 if (it->track_macro_exp_p)
1604 it->location_ptr++;
1605 break;
1606 case MACRO_ARG_TOKEN_STRINGIFIED:
1607 #if CHECKING_P
1608 if (it->num_forwards > 0)
1609 abort ();
1610 #endif
1611 break;
1612 }
1613
1614 #if CHECKING_P
1615 it->num_forwards++;
1616 #endif
1617 }
1618
1619 /* Return the token pointed to by the iterator. */
1620 static const cpp_token *
1621 macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
1622 {
1623 #if CHECKING_P
1624 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1625 && it->num_forwards > 0)
1626 abort ();
1627 #endif
1628 if (it->token_ptr == NULL)
1629 return NULL;
1630 return *it->token_ptr;
1631 }
1632
1633 /* Return the location of the token pointed to by the iterator.*/
1634 static source_location
1635 macro_arg_token_iter_get_location (const macro_arg_token_iter *it)
1636 {
1637 #if CHECKING_P
1638 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1639 && it->num_forwards > 0)
1640 abort ();
1641 #endif
1642 if (it->track_macro_exp_p)
1643 return *it->location_ptr;
1644 else
1645 return (*it->token_ptr)->src_loc;
1646 }
1647
1648 /* Return the index of a token [resulting from macro expansion] inside
1649 the total list of tokens resulting from a given macro
1650 expansion. The index can be different depending on whether if we
1651 want each tokens resulting from function-like macro arguments
1652 expansion to have a different location or not.
1653
1654 E.g, consider this function-like macro:
1655
1656 #define M(x) x - 3
1657
1658 Then consider us "calling" it (and thus expanding it) like:
1659
1660 M(1+4)
1661
1662 It will be expanded into:
1663
1664 1+4-3
1665
1666 Let's consider the case of the token '4'.
1667
1668 Its index can be 2 (it's the third token of the set of tokens
1669 resulting from the expansion) or it can be 0 if we consider that
1670 all tokens resulting from the expansion of the argument "1+2" have
1671 the same index, which is 0. In this later case, the index of token
1672 '-' would then be 1 and the index of token '3' would be 2.
1673
1674 The later case is useful to use less memory e.g, for the case of
1675 the user using the option -ftrack-macro-expansion=1.
1676
1677 ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
1678 are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro
1679 parameter (inside the macro replacement list) that corresponds to
1680 the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
1681 of.
1682
1683 If we refer to the example above, for the '4' argument token,
1684 ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
1685 would be set to the token 'x', in the replacement list "x - 3" of
1686 macro M.
1687
1688 This is a subroutine of replace_args. */
1689 inline static unsigned
1690 expanded_token_index (cpp_reader *pfile, cpp_macro *macro,
1691 const cpp_token *cur_replacement_token,
1692 unsigned absolute_token_index)
1693 {
1694 if (CPP_OPTION (pfile, track_macro_expansion) > 1)
1695 return absolute_token_index;
1696 return cur_replacement_token - macro->exp.tokens;
1697 }
1698
1699 /* Copy whether PASTE_LEFT is set from SRC to *PASTE_FLAG. */
1700
1701 static void
1702 copy_paste_flag (cpp_reader *pfile, const cpp_token **paste_flag,
1703 const cpp_token *src)
1704 {
1705 cpp_token *token = _cpp_temp_token (pfile);
1706 token->type = (*paste_flag)->type;
1707 token->val = (*paste_flag)->val;
1708 if (src->flags & PASTE_LEFT)
1709 token->flags = (*paste_flag)->flags | PASTE_LEFT;
1710 else
1711 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
1712 *paste_flag = token;
1713 }
1714
1715 /* True IFF the last token emitted into BUFF (if any) is PTR. */
1716
1717 static bool
1718 last_token_is (_cpp_buff *buff, const cpp_token **ptr)
1719 {
1720 return (ptr && tokens_buff_last_token_ptr (buff) == ptr);
1721 }
1722
1723 /* Replace the parameters in a function-like macro of NODE with the
1724 actual ARGS, and place the result in a newly pushed token context.
1725 Expand each argument before replacing, unless it is operated upon
1726 by the # or ## operators. EXPANSION_POINT_LOC is the location of
1727 the expansion point of the macro. E.g, the location of the
1728 function-like macro invocation. */
1729 static void
1730 replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
1731 macro_arg *args, source_location expansion_point_loc)
1732 {
1733 unsigned int i, total;
1734 const cpp_token *src, *limit;
1735 const cpp_token **first = NULL;
1736 macro_arg *arg;
1737 _cpp_buff *buff = NULL;
1738 source_location *virt_locs = NULL;
1739 unsigned int exp_count;
1740 const line_map_macro *map = NULL;
1741 int track_macro_exp;
1742
1743 /* First, fully macro-expand arguments, calculating the number of
1744 tokens in the final expansion as we go. The ordering of the if
1745 statements below is subtle; we must handle stringification before
1746 pasting. */
1747
1748 /* EXP_COUNT is the number of tokens in the macro replacement
1749 list. TOTAL is the number of tokens /after/ macro parameters
1750 have been replaced by their arguments. */
1751 exp_count = macro_real_token_count (macro);
1752 total = exp_count;
1753 limit = macro->exp.tokens + exp_count;
1754
1755 for (src = macro->exp.tokens; src < limit; src++)
1756 if (src->type == CPP_MACRO_ARG)
1757 {
1758 /* Leading and trailing padding tokens. */
1759 total += 2;
1760 /* Account for leading and padding tokens in exp_count too.
1761 This is going to be important later down this function,
1762 when we want to handle the case of (track_macro_exp <
1763 2). */
1764 exp_count += 2;
1765
1766 /* We have an argument. If it is not being stringified or
1767 pasted it is macro-replaced before insertion. */
1768 arg = &args[src->val.macro_arg.arg_no - 1];
1769
1770 if (src->flags & STRINGIFY_ARG)
1771 {
1772 if (!arg->stringified)
1773 arg->stringified = stringify_arg (pfile, arg);
1774 }
1775 else if ((src->flags & PASTE_LEFT)
1776 || (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
1777 total += arg->count - 1;
1778 else
1779 {
1780 if (!arg->expanded)
1781 expand_arg (pfile, arg);
1782 total += arg->expanded_count - 1;
1783 }
1784 }
1785
1786 /* When the compiler is called with the -ftrack-macro-expansion
1787 flag, we need to keep track of the location of each token that
1788 results from macro expansion.
1789
1790 A token resulting from macro expansion is not a new token. It is
1791 simply the same token as the token coming from the macro
1792 definition. The new things that are allocated are the buffer
1793 that holds the tokens resulting from macro expansion and a new
1794 location that records many things like the locus of the expansion
1795 point as well as the original locus inside the definition of the
1796 macro. This location is called a virtual location.
1797
1798 So the buffer BUFF holds a set of cpp_token*, and the buffer
1799 VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
1800
1801 Both of these two buffers are going to be hung off of the macro
1802 context, when the latter is pushed. The memory allocated to
1803 store the tokens and their locations is going to be freed once
1804 the context of macro expansion is popped.
1805
1806 As far as tokens are concerned, the memory overhead of
1807 -ftrack-macro-expansion is proportional to the number of
1808 macros that get expanded multiplied by sizeof (source_location).
1809 The good news is that extra memory gets freed when the macro
1810 context is freed, i.e shortly after the macro got expanded. */
1811
1812 /* Is the -ftrack-macro-expansion flag in effect? */
1813 track_macro_exp = CPP_OPTION (pfile, track_macro_expansion);
1814
1815 /* Now allocate memory space for tokens and locations resulting from
1816 the macro expansion, copy the tokens and replace the arguments.
1817 This memory must be freed when the context of the macro MACRO is
1818 popped. */
1819 buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL);
1820
1821 first = (const cpp_token **) buff->base;
1822
1823 /* Create a macro map to record the locations of the tokens that are
1824 involved in the expansion. Note that the expansion point is set
1825 to the location of the closing parenthesis. Otherwise, the
1826 subsequent map created for the first token that comes after the
1827 macro map might have a wrong line number. That would lead to
1828 tokens with wrong line numbers after the macro expansion. This
1829 adds up to the memory overhead of the -ftrack-macro-expansion
1830 flag; for every macro that is expanded, a "macro map" is
1831 created. */
1832 if (track_macro_exp)
1833 {
1834 int num_macro_tokens = total;
1835 if (track_macro_exp < 2)
1836 /* Then the number of macro tokens won't take in account the
1837 fact that function-like macro arguments can expand to
1838 multiple tokens. This is to save memory at the expense of
1839 accuracy.
1840
1841 Suppose we have #define SQUARE(A) A * A
1842
1843 And then we do SQUARE(2+3)
1844
1845 Then the tokens 2, +, 3, will have the same location,
1846 saying they come from the expansion of the argument A. */
1847 num_macro_tokens = exp_count;
1848 map = linemap_enter_macro (pfile->line_table, node,
1849 expansion_point_loc,
1850 num_macro_tokens);
1851 }
1852 i = 0;
1853 vaopt_state vaopt_tracker (pfile, macro->variadic,
1854 args[macro->paramc - 1].count > 0);
1855 const cpp_token **vaopt_start = NULL;
1856 for (src = macro->exp.tokens; src < limit; src++)
1857 {
1858 unsigned int arg_tokens_count;
1859 macro_arg_token_iter from;
1860 const cpp_token **paste_flag = NULL;
1861 const cpp_token **tmp_token_ptr;
1862
1863 /* __VA_OPT__ handling. */
1864 vaopt_state::update_type vostate = vaopt_tracker.update (src);
1865 if (vostate != vaopt_state::INCLUDE)
1866 {
1867 if (vostate == vaopt_state::BEGIN)
1868 {
1869 /* Padding on the left of __VA_OPT__ (unless RHS of ##). */
1870 if (src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
1871 {
1872 const cpp_token *t = padding_token (pfile, src);
1873 unsigned index = expanded_token_index (pfile, macro, src, i);
1874 /* Allocate a virtual location for the padding token and
1875 append the token and its location to BUFF and
1876 VIRT_LOCS. */
1877 tokens_buff_add_token (buff, virt_locs, t,
1878 t->src_loc, t->src_loc,
1879 map, index);
1880 }
1881 vaopt_start = tokens_buff_last_token_ptr (buff);
1882 }
1883 else if (vostate == vaopt_state::END)
1884 {
1885 const cpp_token **start = vaopt_start;
1886 vaopt_start = NULL;
1887
1888 /* Remove any tail padding from inside the __VA_OPT__. */
1889 paste_flag = tokens_buff_last_token_ptr (buff);
1890 while (paste_flag && paste_flag != start
1891 && (*paste_flag)->type == CPP_PADDING)
1892 {
1893 tokens_buff_remove_last_token (buff);
1894 paste_flag = tokens_buff_last_token_ptr (buff);
1895 }
1896
1897 if (src->flags & PASTE_LEFT)
1898 {
1899 /* With a non-empty __VA_OPT__ on the LHS of ##, the last
1900 token should be flagged PASTE_LEFT. */
1901 if (paste_flag && (*paste_flag)->type != CPP_PADDING)
1902 copy_paste_flag (pfile, paste_flag, src);
1903 }
1904 else
1905 {
1906 /* Otherwise, avoid paste on RHS, __VA_OPT__(c)d or
1907 __VA_OPT__(c)__VA_OPT__(d). */
1908 const cpp_token *t = &pfile->avoid_paste;
1909 tokens_buff_add_token (buff, virt_locs,
1910 t, t->src_loc, t->src_loc,
1911 NULL, 0);
1912 }
1913 }
1914 continue;
1915 }
1916
1917 if (src->type != CPP_MACRO_ARG)
1918 {
1919 /* Allocate a virtual location for token SRC, and add that
1920 token and its virtual location into the buffers BUFF and
1921 VIRT_LOCS. */
1922 unsigned index = expanded_token_index (pfile, macro, src, i);
1923 tokens_buff_add_token (buff, virt_locs, src,
1924 src->src_loc, src->src_loc,
1925 map, index);
1926 i += 1;
1927 continue;
1928 }
1929
1930 paste_flag = 0;
1931 arg = &args[src->val.macro_arg.arg_no - 1];
1932 /* SRC is a macro parameter that we need to replace with its
1933 corresponding argument. So at some point we'll need to
1934 iterate over the tokens of the macro argument and copy them
1935 into the "place" now holding the correspondig macro
1936 parameter. We are going to use the iterator type
1937 macro_argo_token_iter to handle that iterating. The 'if'
1938 below is to initialize the iterator depending on the type of
1939 tokens the macro argument has. It also does some adjustment
1940 related to padding tokens and some pasting corner cases. */
1941 if (src->flags & STRINGIFY_ARG)
1942 {
1943 arg_tokens_count = 1;
1944 macro_arg_token_iter_init (&from,
1945 CPP_OPTION (pfile,
1946 track_macro_expansion),
1947 MACRO_ARG_TOKEN_STRINGIFIED,
1948 arg, &arg->stringified);
1949 }
1950 else if (src->flags & PASTE_LEFT)
1951 {
1952 arg_tokens_count = arg->count;
1953 macro_arg_token_iter_init (&from,
1954 CPP_OPTION (pfile,
1955 track_macro_expansion),
1956 MACRO_ARG_TOKEN_NORMAL,
1957 arg, arg->first);
1958 }
1959 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
1960 {
1961 int num_toks;
1962 arg_tokens_count = arg->count;
1963 macro_arg_token_iter_init (&from,
1964 CPP_OPTION (pfile,
1965 track_macro_expansion),
1966 MACRO_ARG_TOKEN_NORMAL,
1967 arg, arg->first);
1968
1969 num_toks = tokens_buff_count (buff);
1970
1971 if (num_toks != 0)
1972 {
1973 /* So the current parameter token is pasted to the previous
1974 token in the replacement list. Let's look at what
1975 we have as previous and current arguments. */
1976
1977 /* This is the previous argument's token ... */
1978 tmp_token_ptr = tokens_buff_last_token_ptr (buff);
1979
1980 if ((*tmp_token_ptr)->type == CPP_COMMA
1981 && macro->variadic
1982 && src->val.macro_arg.arg_no == macro->paramc)
1983 {
1984 /* ... which is a comma; and the current parameter
1985 is the last parameter of a variadic function-like
1986 macro. If the argument to the current last
1987 parameter is NULL, then swallow the comma,
1988 otherwise drop the paste flag. */
1989 if (macro_arg_token_iter_get_token (&from) == NULL)
1990 tokens_buff_remove_last_token (buff);
1991 else
1992 paste_flag = tmp_token_ptr;
1993 }
1994 /* Remove the paste flag if the RHS is a placemarker, unless the
1995 previous emitted token is at the beginning of __VA_OPT__;
1996 placemarkers within __VA_OPT__ are ignored in that case. */
1997 else if (arg_tokens_count == 0
1998 && tmp_token_ptr != vaopt_start)
1999 paste_flag = tmp_token_ptr;
2000 }
2001 }
2002 else
2003 {
2004 arg_tokens_count = arg->expanded_count;
2005 macro_arg_token_iter_init (&from,
2006 CPP_OPTION (pfile,
2007 track_macro_expansion),
2008 MACRO_ARG_TOKEN_EXPANDED,
2009 arg, arg->expanded);
2010
2011 if (last_token_is (buff, vaopt_start))
2012 {
2013 /* We're expanding an arg at the beginning of __VA_OPT__.
2014 Skip padding. */
2015 while (arg_tokens_count)
2016 {
2017 const cpp_token *t = macro_arg_token_iter_get_token (&from);
2018 if (t->type != CPP_PADDING)
2019 break;
2020 macro_arg_token_iter_forward (&from);
2021 --arg_tokens_count;
2022 }
2023 }
2024 }
2025
2026 /* Padding on the left of an argument (unless RHS of ##). */
2027 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
2028 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT)
2029 && !last_token_is (buff, vaopt_start))
2030 {
2031 const cpp_token *t = padding_token (pfile, src);
2032 unsigned index = expanded_token_index (pfile, macro, src, i);
2033 /* Allocate a virtual location for the padding token and
2034 append the token and its location to BUFF and
2035 VIRT_LOCS. */
2036 tokens_buff_add_token (buff, virt_locs, t,
2037 t->src_loc, t->src_loc,
2038 map, index);
2039 }
2040
2041 if (arg_tokens_count)
2042 {
2043 /* So now we've got the number of tokens that make up the
2044 argument that is going to replace the current parameter
2045 in the macro's replacement list. */
2046 unsigned int j;
2047 for (j = 0; j < arg_tokens_count; ++j)
2048 {
2049 /* So if track_macro_exp is < 2, the user wants to
2050 save extra memory while tracking macro expansion
2051 locations. So in that case here is what we do:
2052
2053 Suppose we have #define SQUARE(A) A * A
2054
2055 And then we do SQUARE(2+3)
2056
2057 Then the tokens 2, +, 3, will have the same location,
2058 saying they come from the expansion of the argument
2059 A.
2060
2061 So that means we are going to ignore the COUNT tokens
2062 resulting from the expansion of the current macro
2063 argument. In other words all the ARG_TOKENS_COUNT tokens
2064 resulting from the expansion of the macro argument will
2065 have the index I. Normally, each of those tokens should
2066 have index I+J. */
2067 unsigned token_index = i;
2068 unsigned index;
2069 if (track_macro_exp > 1)
2070 token_index += j;
2071
2072 index = expanded_token_index (pfile, macro, src, token_index);
2073 tokens_buff_add_token (buff, virt_locs,
2074 macro_arg_token_iter_get_token (&from),
2075 macro_arg_token_iter_get_location (&from),
2076 src->src_loc, map, index);
2077 macro_arg_token_iter_forward (&from);
2078 }
2079
2080 /* With a non-empty argument on the LHS of ##, the last
2081 token should be flagged PASTE_LEFT. */
2082 if (src->flags & PASTE_LEFT)
2083 paste_flag
2084 = (const cpp_token **) tokens_buff_last_token_ptr (buff);
2085 }
2086 else if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99)
2087 && ! macro->syshdr && ! cpp_in_system_header (pfile))
2088 {
2089 if (CPP_OPTION (pfile, cplusplus))
2090 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
2091 "invoking macro %s argument %d: "
2092 "empty macro arguments are undefined"
2093 " in ISO C++98",
2094 NODE_NAME (node), src->val.macro_arg.arg_no);
2095 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat))
2096 cpp_pedwarning (pfile,
2097 CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
2098 ? CPP_W_C90_C99_COMPAT : CPP_W_PEDANTIC,
2099 "invoking macro %s argument %d: "
2100 "empty macro arguments are undefined"
2101 " in ISO C90",
2102 NODE_NAME (node), src->val.macro_arg.arg_no);
2103 }
2104 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
2105 && ! CPP_OPTION (pfile, cplusplus)
2106 && ! macro->syshdr && ! cpp_in_system_header (pfile))
2107 cpp_warning (pfile, CPP_W_C90_C99_COMPAT,
2108 "invoking macro %s argument %d: "
2109 "empty macro arguments are undefined"
2110 " in ISO C90",
2111 NODE_NAME (node), src->val.macro_arg.arg_no);
2112
2113 /* Avoid paste on RHS (even case count == 0). */
2114 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT)
2115 && !last_token_is (buff, vaopt_start))
2116 {
2117 const cpp_token *t = &pfile->avoid_paste;
2118 tokens_buff_add_token (buff, virt_locs,
2119 t, t->src_loc, t->src_loc,
2120 NULL, 0);
2121 }
2122
2123 /* Add a new paste flag, or remove an unwanted one. */
2124 if (paste_flag)
2125 copy_paste_flag (pfile, paste_flag, src);
2126
2127 i += arg_tokens_count;
2128 }
2129
2130 if (track_macro_exp)
2131 push_extended_tokens_context (pfile, node, buff, virt_locs, first,
2132 tokens_buff_count (buff));
2133 else
2134 push_ptoken_context (pfile, node, buff, first,
2135 tokens_buff_count (buff));
2136
2137 num_macro_tokens_counter += tokens_buff_count (buff);
2138 }
2139
2140 /* Return a special padding token, with padding inherited from SOURCE. */
2141 static const cpp_token *
2142 padding_token (cpp_reader *pfile, const cpp_token *source)
2143 {
2144 cpp_token *result = _cpp_temp_token (pfile);
2145
2146 result->type = CPP_PADDING;
2147
2148 /* Data in GCed data structures cannot be made const so far, so we
2149 need a cast here. */
2150 result->val.source = (cpp_token *) source;
2151 result->flags = 0;
2152 return result;
2153 }
2154
2155 /* Get a new uninitialized context. Create a new one if we cannot
2156 re-use an old one. */
2157 static cpp_context *
2158 next_context (cpp_reader *pfile)
2159 {
2160 cpp_context *result = pfile->context->next;
2161
2162 if (result == 0)
2163 {
2164 result = XNEW (cpp_context);
2165 memset (result, 0, sizeof (cpp_context));
2166 result->prev = pfile->context;
2167 result->next = 0;
2168 pfile->context->next = result;
2169 }
2170
2171 pfile->context = result;
2172 return result;
2173 }
2174
2175 /* Push a list of pointers to tokens. */
2176 static void
2177 push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
2178 const cpp_token **first, unsigned int count)
2179 {
2180 cpp_context *context = next_context (pfile);
2181
2182 context->tokens_kind = TOKENS_KIND_INDIRECT;
2183 context->c.macro = macro;
2184 context->buff = buff;
2185 FIRST (context).ptoken = first;
2186 LAST (context).ptoken = first + count;
2187 }
2188
2189 /* Push a list of tokens.
2190
2191 A NULL macro means that we should continue the current macro
2192 expansion, in essence. That means that if we are currently in a
2193 macro expansion context, we'll make the new pfile->context refer to
2194 the current macro. */
2195 void
2196 _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
2197 const cpp_token *first, unsigned int count)
2198 {
2199 cpp_context *context;
2200
2201 if (macro == NULL)
2202 macro = macro_of_context (pfile->context);
2203
2204 context = next_context (pfile);
2205 context->tokens_kind = TOKENS_KIND_DIRECT;
2206 context->c.macro = macro;
2207 context->buff = NULL;
2208 FIRST (context).token = first;
2209 LAST (context).token = first + count;
2210 }
2211
2212 /* Build a context containing a list of tokens as well as their
2213 virtual locations and push it. TOKENS_BUFF is the buffer that
2214 contains the tokens pointed to by FIRST. If TOKENS_BUFF is
2215 non-NULL, it means that the context owns it, meaning that
2216 _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
2217 contains the virtual locations.
2218
2219 A NULL macro means that we should continue the current macro
2220 expansion, in essence. That means that if we are currently in a
2221 macro expansion context, we'll make the new pfile->context refer to
2222 the current macro. */
2223 static void
2224 push_extended_tokens_context (cpp_reader *pfile,
2225 cpp_hashnode *macro,
2226 _cpp_buff *token_buff,
2227 source_location *virt_locs,
2228 const cpp_token **first,
2229 unsigned int count)
2230 {
2231 cpp_context *context;
2232 macro_context *m;
2233
2234 if (macro == NULL)
2235 macro = macro_of_context (pfile->context);
2236
2237 context = next_context (pfile);
2238 context->tokens_kind = TOKENS_KIND_EXTENDED;
2239 context->buff = token_buff;
2240
2241 m = XNEW (macro_context);
2242 m->macro_node = macro;
2243 m->virt_locs = virt_locs;
2244 m->cur_virt_loc = virt_locs;
2245 context->c.mc = m;
2246 FIRST (context).ptoken = first;
2247 LAST (context).ptoken = first + count;
2248 }
2249
2250 /* Push a traditional macro's replacement text. */
2251 void
2252 _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
2253 const uchar *start, size_t len)
2254 {
2255 cpp_context *context = next_context (pfile);
2256
2257 context->tokens_kind = TOKENS_KIND_DIRECT;
2258 context->c.macro = macro;
2259 context->buff = NULL;
2260 CUR (context) = start;
2261 RLIMIT (context) = start + len;
2262 macro->flags |= NODE_DISABLED;
2263 }
2264
2265 /* Creates a buffer that holds tokens a.k.a "token buffer", usually
2266 for the purpose of storing them on a cpp_context. If VIRT_LOCS is
2267 non-null (which means that -ftrack-macro-expansion is on),
2268 *VIRT_LOCS is set to a newly allocated buffer that is supposed to
2269 hold the virtual locations of the tokens resulting from macro
2270 expansion. */
2271 static _cpp_buff*
2272 tokens_buff_new (cpp_reader *pfile, size_t len,
2273 source_location **virt_locs)
2274 {
2275 size_t tokens_size = len * sizeof (cpp_token *);
2276 size_t locs_size = len * sizeof (source_location);
2277
2278 if (virt_locs != NULL)
2279 *virt_locs = XNEWVEC (source_location, locs_size);
2280 return _cpp_get_buff (pfile, tokens_size);
2281 }
2282
2283 /* Returns the number of tokens contained in a token buffer. The
2284 buffer holds a set of cpp_token*. */
2285 static size_t
2286 tokens_buff_count (_cpp_buff *buff)
2287 {
2288 return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *);
2289 }
2290
2291 /* Return a pointer to the last token contained in the token buffer
2292 BUFF. */
2293 static const cpp_token **
2294 tokens_buff_last_token_ptr (_cpp_buff *buff)
2295 {
2296 if (BUFF_FRONT (buff) == buff->base)
2297 return NULL;
2298 return &((const cpp_token **) BUFF_FRONT (buff))[-1];
2299 }
2300
2301 /* Remove the last token contained in the token buffer TOKENS_BUFF.
2302 If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer
2303 containing the virtual locations of the tokens in TOKENS_BUFF; in
2304 which case the function updates that buffer as well. */
2305 static inline void
2306 tokens_buff_remove_last_token (_cpp_buff *tokens_buff)
2307
2308 {
2309 if (BUFF_FRONT (tokens_buff) > tokens_buff->base)
2310 BUFF_FRONT (tokens_buff) =
2311 (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1];
2312 }
2313
2314 /* Insert a token into the token buffer at the position pointed to by
2315 DEST. Note that the buffer is not enlarged so the previous token
2316 that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null,
2317 means -ftrack-macro-expansion is effect; it then points to where to
2318 insert the virtual location of TOKEN. TOKEN is the token to
2319 insert. VIRT_LOC is the virtual location of the token, i.e, the
2320 location possibly encoding its locus across macro expansion. If
2321 TOKEN is an argument of a function-like macro (inside a macro
2322 replacement list), PARM_DEF_LOC is the spelling location of the
2323 macro parameter that TOKEN is replacing, in the replacement list of
2324 the macro. If TOKEN is not an argument of a function-like macro or
2325 if it doesn't come from a macro expansion, then VIRT_LOC can just
2326 be set to the same value as PARM_DEF_LOC. If MAP is non null, it
2327 means TOKEN comes from a macro expansion and MAP is the macro map
2328 associated to the macro. MACRO_TOKEN_INDEX points to the index of
2329 the token in the macro map; it is not considered if MAP is NULL.
2330
2331 Upon successful completion this function returns the a pointer to
2332 the position of the token coming right after the insertion
2333 point. */
2334 static inline const cpp_token **
2335 tokens_buff_put_token_to (const cpp_token **dest,
2336 source_location *virt_loc_dest,
2337 const cpp_token *token,
2338 source_location virt_loc,
2339 source_location parm_def_loc,
2340 const line_map_macro *map,
2341 unsigned int macro_token_index)
2342 {
2343 source_location macro_loc = virt_loc;
2344 const cpp_token **result;
2345
2346 if (virt_loc_dest)
2347 {
2348 /* -ftrack-macro-expansion is on. */
2349 if (map)
2350 macro_loc = linemap_add_macro_token (map, macro_token_index,
2351 virt_loc, parm_def_loc);
2352 *virt_loc_dest = macro_loc;
2353 }
2354 *dest = token;
2355 result = &dest[1];
2356
2357 return result;
2358 }
2359
2360 /* Adds a token at the end of the tokens contained in BUFFER. Note
2361 that this function doesn't enlarge BUFFER when the number of tokens
2362 reaches BUFFER's size; it aborts in that situation.
2363
2364 TOKEN is the token to append. VIRT_LOC is the virtual location of
2365 the token, i.e, the location possibly encoding its locus across
2366 macro expansion. If TOKEN is an argument of a function-like macro
2367 (inside a macro replacement list), PARM_DEF_LOC is the location of
2368 the macro parameter that TOKEN is replacing. If TOKEN doesn't come
2369 from a macro expansion, then VIRT_LOC can just be set to the same
2370 value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes
2371 from a macro expansion and MAP is the macro map associated to the
2372 macro. MACRO_TOKEN_INDEX points to the index of the token in the
2373 macro map; It is not considered if MAP is NULL. If VIRT_LOCS is
2374 non-null, it means -ftrack-macro-expansion is on; in which case
2375 this function adds the virtual location DEF_LOC to the VIRT_LOCS
2376 array, at the same index as the one of TOKEN in BUFFER. Upon
2377 successful completion this function returns the a pointer to the
2378 position of the token coming right after the insertion point. */
2379 static const cpp_token **
2380 tokens_buff_add_token (_cpp_buff *buffer,
2381 source_location *virt_locs,
2382 const cpp_token *token,
2383 source_location virt_loc,
2384 source_location parm_def_loc,
2385 const line_map_macro *map,
2386 unsigned int macro_token_index)
2387 {
2388 const cpp_token **result;
2389 source_location *virt_loc_dest = NULL;
2390 unsigned token_index =
2391 (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
2392
2393 /* Abort if we pass the end the buffer. */
2394 if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer))
2395 abort ();
2396
2397 if (virt_locs != NULL)
2398 virt_loc_dest = &virt_locs[token_index];
2399
2400 result =
2401 tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer),
2402 virt_loc_dest, token, virt_loc, parm_def_loc,
2403 map, macro_token_index);
2404
2405 BUFF_FRONT (buffer) = (unsigned char *) result;
2406 return result;
2407 }
2408
2409 /* Allocate space for the function-like macro argument ARG to store
2410 the tokens resulting from the macro-expansion of the tokens that
2411 make up ARG itself. That space is allocated in ARG->expanded and
2412 needs to be freed using free. */
2413 static void
2414 alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity)
2415 {
2416 gcc_checking_assert (arg->expanded == NULL
2417 && arg->expanded_virt_locs == NULL);
2418
2419 arg->expanded = XNEWVEC (const cpp_token *, capacity);
2420 if (CPP_OPTION (pfile, track_macro_expansion))
2421 arg->expanded_virt_locs = XNEWVEC (source_location, capacity);
2422
2423 }
2424
2425 /* If necessary, enlarge ARG->expanded to so that it can contain SIZE
2426 tokens. */
2427 static void
2428 ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg,
2429 size_t size, size_t *expanded_capacity)
2430 {
2431 if (size <= *expanded_capacity)
2432 return;
2433
2434 size *= 2;
2435
2436 arg->expanded =
2437 XRESIZEVEC (const cpp_token *, arg->expanded, size);
2438 *expanded_capacity = size;
2439
2440 if (CPP_OPTION (pfile, track_macro_expansion))
2441 {
2442 if (arg->expanded_virt_locs == NULL)
2443 arg->expanded_virt_locs = XNEWVEC (source_location, size);
2444 else
2445 arg->expanded_virt_locs = XRESIZEVEC (source_location,
2446 arg->expanded_virt_locs,
2447 size);
2448 }
2449 }
2450
2451 /* Expand an argument ARG before replacing parameters in a
2452 function-like macro. This works by pushing a context with the
2453 argument's tokens, and then expanding that into a temporary buffer
2454 as if it were a normal part of the token stream. collect_args()
2455 has terminated the argument's tokens with a CPP_EOF so that we know
2456 when we have fully expanded the argument. */
2457 static void
2458 expand_arg (cpp_reader *pfile, macro_arg *arg)
2459 {
2460 size_t capacity;
2461 bool saved_warn_trad;
2462 bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
2463
2464 if (arg->count == 0
2465 || arg->expanded != NULL)
2466 return;
2467
2468 /* Don't warn about funlike macros when pre-expanding. */
2469 saved_warn_trad = CPP_WTRADITIONAL (pfile);
2470 CPP_WTRADITIONAL (pfile) = 0;
2471
2472 /* Loop, reading in the tokens of the argument. */
2473 capacity = 256;
2474 alloc_expanded_arg_mem (pfile, arg, capacity);
2475
2476 if (track_macro_exp_p)
2477 push_extended_tokens_context (pfile, NULL, NULL,
2478 arg->virt_locs,
2479 arg->first,
2480 arg->count + 1);
2481 else
2482 push_ptoken_context (pfile, NULL, NULL,
2483 arg->first, arg->count + 1);
2484
2485 for (;;)
2486 {
2487 const cpp_token *token;
2488 source_location location;
2489
2490 ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1,
2491 &capacity);
2492
2493 token = cpp_get_token_1 (pfile, &location);
2494
2495 if (token->type == CPP_EOF)
2496 break;
2497
2498 set_arg_token (arg, token, location,
2499 arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED,
2500 CPP_OPTION (pfile, track_macro_expansion));
2501 arg->expanded_count++;
2502 }
2503
2504 _cpp_pop_context (pfile);
2505
2506 CPP_WTRADITIONAL (pfile) = saved_warn_trad;
2507 }
2508
2509 /* Returns the macro associated to the current context if we are in
2510 the context a macro expansion, NULL otherwise. */
2511 static cpp_hashnode*
2512 macro_of_context (cpp_context *context)
2513 {
2514 if (context == NULL)
2515 return NULL;
2516
2517 return (context->tokens_kind == TOKENS_KIND_EXTENDED)
2518 ? context->c.mc->macro_node
2519 : context->c.macro;
2520 }
2521
2522 /* Return TRUE iff we are expanding a macro or are about to start
2523 expanding one. If we are effectively expanding a macro, the
2524 function macro_of_context returns a pointer to the macro being
2525 expanded. */
2526 static bool
2527 in_macro_expansion_p (cpp_reader *pfile)
2528 {
2529 if (pfile == NULL)
2530 return false;
2531
2532 return (pfile->about_to_expand_macro_p
2533 || macro_of_context (pfile->context));
2534 }
2535
2536 /* Pop the current context off the stack, re-enabling the macro if the
2537 context represented a macro's replacement list. Initially the
2538 context structure was not freed so that we can re-use it later, but
2539 now we do free it to reduce peak memory consumption. */
2540 void
2541 _cpp_pop_context (cpp_reader *pfile)
2542 {
2543 cpp_context *context = pfile->context;
2544
2545 /* We should not be popping the base context. */
2546 if (context == &pfile->base_context)
2547 abort ();
2548
2549 if (context->c.macro)
2550 {
2551 cpp_hashnode *macro;
2552 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
2553 {
2554 macro_context *mc = context->c.mc;
2555 macro = mc->macro_node;
2556 /* If context->buff is set, it means the life time of tokens
2557 is bound to the life time of this context; so we must
2558 free the tokens; that means we must free the virtual
2559 locations of these tokens too. */
2560 if (context->buff && mc->virt_locs)
2561 {
2562 free (mc->virt_locs);
2563 mc->virt_locs = NULL;
2564 }
2565 free (mc);
2566 context->c.mc = NULL;
2567 }
2568 else
2569 macro = context->c.macro;
2570
2571 /* Beware that MACRO can be NULL in cases like when we are
2572 called from expand_arg. In those cases, a dummy context with
2573 tokens is pushed just for the purpose of walking them using
2574 cpp_get_token_1. In that case, no 'macro' field is set into
2575 the dummy context. */
2576 if (macro != NULL
2577 /* Several contiguous macro expansion contexts can be
2578 associated to the same macro; that means it's the same
2579 macro expansion that spans across all these (sub)
2580 contexts. So we should re-enable an expansion-disabled
2581 macro only when we are sure we are really out of that
2582 macro expansion. */
2583 && macro_of_context (context->prev) != macro)
2584 macro->flags &= ~NODE_DISABLED;
2585
2586 if (macro == pfile->top_most_macro_node && context->prev == NULL)
2587 /* We are popping the context of the top-most macro node. */
2588 pfile->top_most_macro_node = NULL;
2589 }
2590
2591 if (context->buff)
2592 {
2593 /* Decrease memory peak consumption by freeing the memory used
2594 by the context. */
2595 _cpp_free_buff (context->buff);
2596 }
2597
2598 pfile->context = context->prev;
2599 /* decrease peak memory consumption by feeing the context. */
2600 pfile->context->next = NULL;
2601 free (context);
2602 }
2603
2604 /* Return TRUE if we reached the end of the set of tokens stored in
2605 CONTEXT, FALSE otherwise. */
2606 static inline bool
2607 reached_end_of_context (cpp_context *context)
2608 {
2609 if (context->tokens_kind == TOKENS_KIND_DIRECT)
2610 return FIRST (context).token == LAST (context).token;
2611 else if (context->tokens_kind == TOKENS_KIND_INDIRECT
2612 || context->tokens_kind == TOKENS_KIND_EXTENDED)
2613 return FIRST (context).ptoken == LAST (context).ptoken;
2614 else
2615 abort ();
2616 }
2617
2618 /* Consume the next token contained in the current context of PFILE,
2619 and return it in *TOKEN. It's "full location" is returned in
2620 *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
2621 means the location encoding the locus of the token across macro
2622 expansion; otherwise it's just is the "normal" location of the
2623 token which (*TOKEN)->src_loc. */
2624 static inline void
2625 consume_next_token_from_context (cpp_reader *pfile,
2626 const cpp_token ** token,
2627 source_location *location)
2628 {
2629 cpp_context *c = pfile->context;
2630
2631 if ((c)->tokens_kind == TOKENS_KIND_DIRECT)
2632 {
2633 *token = FIRST (c).token;
2634 *location = (*token)->src_loc;
2635 FIRST (c).token++;
2636 }
2637 else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT)
2638 {
2639 *token = *FIRST (c).ptoken;
2640 *location = (*token)->src_loc;
2641 FIRST (c).ptoken++;
2642 }
2643 else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED)
2644 {
2645 macro_context *m = c->c.mc;
2646 *token = *FIRST (c).ptoken;
2647 if (m->virt_locs)
2648 {
2649 *location = *m->cur_virt_loc;
2650 m->cur_virt_loc++;
2651 }
2652 else
2653 *location = (*token)->src_loc;
2654 FIRST (c).ptoken++;
2655 }
2656 else
2657 abort ();
2658 }
2659
2660 /* In the traditional mode of the preprocessor, if we are currently in
2661 a directive, the location of a token must be the location of the
2662 start of the directive line. This function returns the proper
2663 location if we are in the traditional mode, and just returns
2664 LOCATION otherwise. */
2665
2666 static inline source_location
2667 maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, source_location location)
2668 {
2669 if (CPP_OPTION (pfile, traditional))
2670 {
2671 if (pfile->state.in_directive)
2672 return pfile->directive_line;
2673 }
2674 return location;
2675 }
2676
2677 /* Routine to get a token as well as its location.
2678
2679 Macro expansions and directives are transparently handled,
2680 including entering included files. Thus tokens are post-macro
2681 expansion, and after any intervening directives. External callers
2682 see CPP_EOF only at EOF. Internal callers also see it when meeting
2683 a directive inside a macro call, when at the end of a directive and
2684 state.in_directive is still 1, and at the end of argument
2685 pre-expansion.
2686
2687 LOC is an out parameter; *LOC is set to the location "as expected
2688 by the user". Please read the comment of
2689 cpp_get_token_with_location to learn more about the meaning of this
2690 location. */
2691 static const cpp_token*
2692 cpp_get_token_1 (cpp_reader *pfile, source_location *location)
2693 {
2694 const cpp_token *result;
2695 /* This token is a virtual token that either encodes a location
2696 related to macro expansion or a spelling location. */
2697 source_location virt_loc = 0;
2698 /* pfile->about_to_expand_macro_p can be overriden by indirect calls
2699 to functions that push macro contexts. So let's save it so that
2700 we can restore it when we are about to leave this routine. */
2701 bool saved_about_to_expand_macro = pfile->about_to_expand_macro_p;
2702
2703 for (;;)
2704 {
2705 cpp_hashnode *node;
2706 cpp_context *context = pfile->context;
2707
2708 /* Context->prev == 0 <=> base context. */
2709 if (!context->prev)
2710 {
2711 result = _cpp_lex_token (pfile);
2712 virt_loc = result->src_loc;
2713 }
2714 else if (!reached_end_of_context (context))
2715 {
2716 consume_next_token_from_context (pfile, &result,
2717 &virt_loc);
2718 if (result->flags & PASTE_LEFT)
2719 {
2720 paste_all_tokens (pfile, result);
2721 if (pfile->state.in_directive)
2722 continue;
2723 result = padding_token (pfile, result);
2724 goto out;
2725 }
2726 }
2727 else
2728 {
2729 if (pfile->context->c.macro)
2730 ++num_expanded_macros_counter;
2731 _cpp_pop_context (pfile);
2732 if (pfile->state.in_directive)
2733 continue;
2734 result = &pfile->avoid_paste;
2735 goto out;
2736 }
2737
2738 if (pfile->state.in_directive && result->type == CPP_COMMENT)
2739 continue;
2740
2741 if (result->type != CPP_NAME)
2742 break;
2743
2744 node = result->val.node.node;
2745
2746 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
2747 break;
2748
2749 if (!(node->flags & NODE_DISABLED))
2750 {
2751 int ret = 0;
2752 /* If not in a macro context, and we're going to start an
2753 expansion, record the location and the top level macro
2754 about to be expanded. */
2755 if (!in_macro_expansion_p (pfile))
2756 {
2757 pfile->invocation_location = result->src_loc;
2758 pfile->top_most_macro_node = node;
2759 }
2760 if (pfile->state.prevent_expansion)
2761 break;
2762
2763 /* Conditional macros require that a predicate be evaluated
2764 first. */
2765 if ((node->flags & NODE_CONDITIONAL) != 0)
2766 {
2767 if (pfile->cb.macro_to_expand)
2768 {
2769 bool whitespace_after;
2770 const cpp_token *peek_tok = cpp_peek_token (pfile, 0);
2771
2772 whitespace_after = (peek_tok->type == CPP_PADDING
2773 || (peek_tok->flags & PREV_WHITE));
2774 node = pfile->cb.macro_to_expand (pfile, result);
2775 if (node)
2776 ret = enter_macro_context (pfile, node, result,
2777 virt_loc);
2778 else if (whitespace_after)
2779 {
2780 /* If macro_to_expand hook returned NULL and it
2781 ate some tokens, see if we don't need to add
2782 a padding token in between this and the
2783 next token. */
2784 peek_tok = cpp_peek_token (pfile, 0);
2785 if (peek_tok->type != CPP_PADDING
2786 && (peek_tok->flags & PREV_WHITE) == 0)
2787 _cpp_push_token_context (pfile, NULL,
2788 padding_token (pfile,
2789 peek_tok), 1);
2790 }
2791 }
2792 }
2793 else
2794 ret = enter_macro_context (pfile, node, result,
2795 virt_loc);
2796 if (ret)
2797 {
2798 if (pfile->state.in_directive || ret == 2)
2799 continue;
2800 result = padding_token (pfile, result);
2801 goto out;
2802 }
2803 }
2804 else
2805 {
2806 /* Flag this token as always unexpandable. FIXME: move this
2807 to collect_args()?. */
2808 cpp_token *t = _cpp_temp_token (pfile);
2809 t->type = result->type;
2810 t->flags = result->flags | NO_EXPAND;
2811 t->val = result->val;
2812 result = t;
2813 }
2814
2815 break;
2816 }
2817
2818 out:
2819 if (location != NULL)
2820 {
2821 if (virt_loc == 0)
2822 virt_loc = result->src_loc;
2823 *location = virt_loc;
2824
2825 if (!CPP_OPTION (pfile, track_macro_expansion)
2826 && macro_of_context (pfile->context) != NULL)
2827 /* We are in a macro expansion context, are not tracking
2828 virtual location, but were asked to report the location
2829 of the expansion point of the macro being expanded. */
2830 *location = pfile->invocation_location;
2831
2832 *location = maybe_adjust_loc_for_trad_cpp (pfile, *location);
2833 }
2834
2835 pfile->about_to_expand_macro_p = saved_about_to_expand_macro;
2836 return result;
2837 }
2838
2839 /* External routine to get a token. Also used nearly everywhere
2840 internally, except for places where we know we can safely call
2841 _cpp_lex_token directly, such as lexing a directive name.
2842
2843 Macro expansions and directives are transparently handled,
2844 including entering included files. Thus tokens are post-macro
2845 expansion, and after any intervening directives. External callers
2846 see CPP_EOF only at EOF. Internal callers also see it when meeting
2847 a directive inside a macro call, when at the end of a directive and
2848 state.in_directive is still 1, and at the end of argument
2849 pre-expansion. */
2850 const cpp_token *
2851 cpp_get_token (cpp_reader *pfile)
2852 {
2853 return cpp_get_token_1 (pfile, NULL);
2854 }
2855
2856 /* Like cpp_get_token, but also returns a virtual token location
2857 separate from the spelling location carried by the returned token.
2858
2859 LOC is an out parameter; *LOC is set to the location "as expected
2860 by the user". This matters when a token results from macro
2861 expansion; in that case the token's spelling location indicates the
2862 locus of the token in the definition of the macro but *LOC
2863 virtually encodes all the other meaningful locuses associated to
2864 the token.
2865
2866 What? virtual location? Yes, virtual location.
2867
2868 If the token results from macro expansion and if macro expansion
2869 location tracking is enabled its virtual location encodes (at the
2870 same time):
2871
2872 - the spelling location of the token
2873
2874 - the locus of the macro expansion point
2875
2876 - the locus of the point where the token got instantiated as part
2877 of the macro expansion process.
2878
2879 You have to use the linemap API to get the locus you are interested
2880 in from a given virtual location.
2881
2882 Note however that virtual locations are not necessarily ordered for
2883 relations '<' and '>'. One must use the function
2884 linemap_location_before_p instead of using the relational operator
2885 '<'.
2886
2887 If macro expansion tracking is off and if the token results from
2888 macro expansion the virtual location is the expansion point of the
2889 macro that got expanded.
2890
2891 When the token doesn't result from macro expansion, the virtual
2892 location is just the same thing as its spelling location. */
2893
2894 const cpp_token *
2895 cpp_get_token_with_location (cpp_reader *pfile, source_location *loc)
2896 {
2897 return cpp_get_token_1 (pfile, loc);
2898 }
2899
2900 /* Returns true if we're expanding an object-like macro that was
2901 defined in a system header. Just checks the macro at the top of
2902 the stack. Used for diagnostic suppression. */
2903 int
2904 cpp_sys_macro_p (cpp_reader *pfile)
2905 {
2906 cpp_hashnode *node = NULL;
2907
2908 if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2909 node = pfile->context->c.mc->macro_node;
2910 else
2911 node = pfile->context->c.macro;
2912
2913 return node && node->value.macro && node->value.macro->syshdr;
2914 }
2915
2916 /* Read each token in, until end of the current file. Directives are
2917 transparently processed. */
2918 void
2919 cpp_scan_nooutput (cpp_reader *pfile)
2920 {
2921 /* Request a CPP_EOF token at the end of this file, rather than
2922 transparently continuing with the including file. */
2923 pfile->buffer->return_at_eof = true;
2924
2925 pfile->state.discarding_output++;
2926 pfile->state.prevent_expansion++;
2927
2928 if (CPP_OPTION (pfile, traditional))
2929 while (_cpp_read_logical_line_trad (pfile))
2930 ;
2931 else
2932 while (cpp_get_token (pfile)->type != CPP_EOF)
2933 ;
2934
2935 pfile->state.discarding_output--;
2936 pfile->state.prevent_expansion--;
2937 }
2938
2939 /* Step back one or more tokens obtained from the lexer. */
2940 void
2941 _cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count)
2942 {
2943 pfile->lookaheads += count;
2944 while (count--)
2945 {
2946 pfile->cur_token--;
2947 if (pfile->cur_token == pfile->cur_run->base
2948 /* Possible with -fpreprocessed and no leading #line. */
2949 && pfile->cur_run->prev != NULL)
2950 {
2951 pfile->cur_run = pfile->cur_run->prev;
2952 pfile->cur_token = pfile->cur_run->limit;
2953 }
2954 }
2955 }
2956
2957 /* Step back one (or more) tokens. Can only step back more than 1 if
2958 they are from the lexer, and not from macro expansion. */
2959 void
2960 _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
2961 {
2962 if (pfile->context->prev == NULL)
2963 _cpp_backup_tokens_direct (pfile, count);
2964 else
2965 {
2966 if (count != 1)
2967 abort ();
2968 if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT)
2969 FIRST (pfile->context).token--;
2970 else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT)
2971 FIRST (pfile->context).ptoken--;
2972 else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2973 {
2974 FIRST (pfile->context).ptoken--;
2975 if (pfile->context->c.macro)
2976 {
2977 macro_context *m = pfile->context->c.mc;
2978 m->cur_virt_loc--;
2979 gcc_checking_assert (m->cur_virt_loc >= m->virt_locs);
2980 }
2981 else
2982 abort ();
2983 }
2984 else
2985 abort ();
2986 }
2987 }
2988
2989 /* #define directive parsing and handling. */
2990
2991 /* Returns nonzero if a macro redefinition warning is required. */
2992 static bool
2993 warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
2994 const cpp_macro *macro2)
2995 {
2996 const cpp_macro *macro1;
2997 unsigned int i;
2998
2999 /* Some redefinitions need to be warned about regardless. */
3000 if (node->flags & NODE_WARN)
3001 return true;
3002
3003 /* Suppress warnings for builtins that lack the NODE_WARN flag,
3004 unless Wbuiltin-macro-redefined. */
3005 if (node->flags & NODE_BUILTIN
3006 && (!pfile->cb.user_builtin_macro
3007 || !pfile->cb.user_builtin_macro (pfile, node)))
3008 return CPP_OPTION (pfile, warn_builtin_macro_redefined);
3009
3010 /* Redefinitions of conditional (context-sensitive) macros, on
3011 the other hand, must be allowed silently. */
3012 if (node->flags & NODE_CONDITIONAL)
3013 return false;
3014
3015 /* Redefinition of a macro is allowed if and only if the old and new
3016 definitions are the same. (6.10.3 paragraph 2). */
3017 macro1 = node->value.macro;
3018
3019 /* Don't check count here as it can be different in valid
3020 traditional redefinitions with just whitespace differences. */
3021 if (macro1->paramc != macro2->paramc
3022 || macro1->fun_like != macro2->fun_like
3023 || macro1->variadic != macro2->variadic)
3024 return true;
3025
3026 /* Check parameter spellings. */
3027 for (i = 0; i < macro1->paramc; i++)
3028 if (macro1->params[i] != macro2->params[i])
3029 return true;
3030
3031 /* Check the replacement text or tokens. */
3032 if (CPP_OPTION (pfile, traditional))
3033 return _cpp_expansions_different_trad (macro1, macro2);
3034
3035 if (macro1->count != macro2->count)
3036 return true;
3037
3038 for (i = 0; i < macro1->count; i++)
3039 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
3040 return true;
3041
3042 return false;
3043 }
3044
3045 /* Free the definition of hashnode H. */
3046 void
3047 _cpp_free_definition (cpp_hashnode *h)
3048 {
3049 /* Macros and assertions no longer have anything to free. */
3050 h->type = NT_VOID;
3051 /* Clear builtin flag in case of redefinition. */
3052 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED);
3053 }
3054
3055 /* Save parameter NODE (spelling SPELLING) to the parameter list of
3056 macro MACRO. Returns true on success, false on failure. */
3057 bool
3058 _cpp_save_parameter (cpp_reader *pfile, unsigned n, cpp_hashnode *node,
3059 cpp_hashnode *spelling)
3060 {
3061 /* Constraint 6.10.3.6 - duplicate parameter names. */
3062 if (node->flags & NODE_MACRO_ARG)
3063 {
3064 cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
3065 NODE_NAME (node));
3066 return false;
3067 }
3068
3069 unsigned len = (n + 1) * sizeof (struct macro_arg_saved_data);
3070 if (len > pfile->macro_buffer_len)
3071 {
3072 pfile->macro_buffer
3073 = XRESIZEVEC (unsigned char, pfile->macro_buffer, len);
3074 pfile->macro_buffer_len = len;
3075 }
3076
3077 macro_arg_saved_data *saved = (macro_arg_saved_data *)pfile->macro_buffer;
3078 saved[n].canonical_node = node;
3079 saved[n].value = node->value;
3080
3081 void *base = _cpp_reserve_room (pfile, n * sizeof (cpp_hashnode *),
3082 sizeof (cpp_hashnode *));
3083 ((cpp_hashnode **)base)[n] = spelling;
3084
3085 /* Morph into a macro arg. */
3086 node->flags |= NODE_MACRO_ARG;
3087 /* Index is 1 based. */
3088 node->value.arg_index = n + 1;
3089
3090 return true;
3091 }
3092
3093 /* Restore the parameters to their previous state. */
3094 void
3095 _cpp_unsave_parameters (cpp_reader *pfile, unsigned n)
3096 {
3097 /* Clear the fast argument lookup indices. */
3098 while (n--)
3099 {
3100 struct macro_arg_saved_data *save =
3101 &((struct macro_arg_saved_data *) pfile->macro_buffer)[n];
3102
3103 struct cpp_hashnode *node = save->canonical_node;
3104 node->value = save->value;
3105 node->flags &= ~NODE_MACRO_ARG;
3106 }
3107 }
3108
3109 /* Check the syntax of the parameters in a MACRO definition. Return
3110 false on failure. Set *N_PTR and *VARADIC_PTR as appropriate.
3111 '(' ')'
3112 '(' parm-list ',' last-parm ')'
3113 '(' last-parm ')'
3114 parm-list: name
3115 | parm-list, name
3116 last-parm: name
3117 | name '...'
3118 | '...'
3119 */
3120 static bool
3121 parse_params (cpp_reader *pfile, unsigned *n_ptr, bool *varadic_ptr)
3122 {
3123 unsigned nparms = 0;
3124 bool ok = false;
3125
3126 for (bool prev_ident = false;;)
3127 {
3128 const cpp_token *token = _cpp_lex_token (pfile);
3129
3130 switch (token->type)
3131 {
3132 case CPP_COMMENT:
3133 /* Allow/ignore comments in parameter lists if we are
3134 preserving comments in macro expansions. */
3135 if (!CPP_OPTION (pfile, discard_comments_in_macro_exp))
3136 break;
3137
3138 /* FALLTHRU */
3139 default:
3140 bad:
3141 {
3142 const char *const msgs[5] =
3143 {
3144 N_("expected parameter name, found \"%s\""),
3145 N_("expected ',' or ')', found \"%s\""),
3146 N_("expected parameter name before end of line"),
3147 N_("expected ')' before end of line"),
3148 N_("expected ')' after \"...\"")
3149 };
3150 unsigned ix = prev_ident;
3151 const unsigned char *as_text = NULL;
3152 if (*varadic_ptr)
3153 ix = 4;
3154 else if (token->type == CPP_EOF)
3155 ix += 2;
3156 else
3157 as_text = cpp_token_as_text (pfile, token);
3158 cpp_error (pfile, CPP_DL_ERROR, msgs[ix], as_text);
3159 }
3160 goto out;
3161
3162 case CPP_NAME:
3163 if (prev_ident || *varadic_ptr)
3164 goto bad;
3165 prev_ident = true;
3166
3167 if (!_cpp_save_parameter (pfile, nparms, token->val.node.node,
3168 token->val.node.spelling))
3169 goto out;
3170 nparms++;
3171 break;
3172
3173 case CPP_CLOSE_PAREN:
3174 if (prev_ident || !nparms || *varadic_ptr)
3175 {
3176 ok = true;
3177 goto out;
3178 }
3179
3180 /* FALLTHRU */
3181 case CPP_COMMA:
3182 if (!prev_ident || *varadic_ptr)
3183 goto bad;
3184 prev_ident = false;
3185 break;
3186
3187 case CPP_ELLIPSIS:
3188 if (*varadic_ptr)
3189 goto bad;
3190 *varadic_ptr = true;
3191 if (!prev_ident)
3192 {
3193 /* An ISO bare ellipsis. */
3194 _cpp_save_parameter (pfile, nparms,
3195 pfile->spec_nodes.n__VA_ARGS__,
3196 pfile->spec_nodes.n__VA_ARGS__);
3197 nparms++;
3198 pfile->state.va_args_ok = 1;
3199 if (! CPP_OPTION (pfile, c99)
3200 && CPP_OPTION (pfile, cpp_pedantic)
3201 && CPP_OPTION (pfile, warn_variadic_macros))
3202 cpp_pedwarning
3203 (pfile, CPP_W_VARIADIC_MACROS,
3204 CPP_OPTION (pfile, cplusplus)
3205 ? N_("anonymous variadic macros were introduced in C++11")
3206 : N_("anonymous variadic macros were introduced in C99"));
3207 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
3208 && ! CPP_OPTION (pfile, cplusplus))
3209 cpp_error (pfile, CPP_DL_WARNING,
3210 "anonymous variadic macros were introduced in C99");
3211 }
3212 else if (CPP_OPTION (pfile, cpp_pedantic)
3213 && CPP_OPTION (pfile, warn_variadic_macros))
3214 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
3215 CPP_OPTION (pfile, cplusplus)
3216 ? N_("ISO C++ does not permit named variadic macros")
3217 : N_("ISO C does not permit named variadic macros"));
3218 break;
3219 }
3220 }
3221
3222 out:
3223 *n_ptr = nparms;
3224
3225 return ok;
3226 }
3227
3228 /* Lex a token from the expansion of MACRO, but mark parameters as we
3229 find them and warn of traditional stringification. */
3230 static cpp_macro *
3231 lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
3232 {
3233 macro = (cpp_macro *)_cpp_reserve_room (pfile,
3234 sizeof (cpp_macro) - sizeof (cpp_token)
3235 + macro->count * sizeof (cpp_token),
3236 sizeof (cpp_token));
3237 cpp_token *saved_cur_token = pfile->cur_token;
3238 pfile->cur_token = &macro->exp.tokens[macro->count];
3239 cpp_token *token = _cpp_lex_direct (pfile);
3240 pfile->cur_token = saved_cur_token;
3241
3242 /* Is this a parameter? */
3243 if (token->type == CPP_NAME
3244 && (token->val.node.node->flags & NODE_MACRO_ARG) != 0)
3245 {
3246 cpp_hashnode *spelling = token->val.node.spelling;
3247 token->type = CPP_MACRO_ARG;
3248 token->val.macro_arg.arg_no = token->val.node.node->value.arg_index;
3249 token->val.macro_arg.spelling = spelling;
3250 }
3251 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
3252 && (token->type == CPP_STRING || token->type == CPP_CHAR))
3253 check_trad_stringification (pfile, macro, &token->val.str);
3254
3255 return macro;
3256 }
3257
3258 static cpp_macro *
3259 create_iso_definition (cpp_reader *pfile)
3260 {
3261 bool following_paste_op = false;
3262 const char *paste_op_error_msg =
3263 N_("'##' cannot appear at either end of a macro expansion");
3264 unsigned int num_extra_tokens = 0;
3265 unsigned nparms = 0;
3266 cpp_hashnode **params = NULL;
3267 bool varadic = false;
3268 bool ok = false;
3269 cpp_macro *macro = NULL;
3270
3271 /* Look at the first token, to see if this is a function-like
3272 macro. */
3273 cpp_token first;
3274 cpp_token *saved_cur_token = pfile->cur_token;
3275 pfile->cur_token = &first;
3276 cpp_token *token = _cpp_lex_direct (pfile);
3277 pfile->cur_token = saved_cur_token;
3278
3279 if (token->flags & PREV_WHITE)
3280 /* Preceeded by space, must be part of expansion. */;
3281 else if (token->type == CPP_OPEN_PAREN)
3282 {
3283 /* An open-paren, get a parameter list. */
3284 if (!parse_params (pfile, &nparms, &varadic))
3285 goto out;
3286
3287 params = (cpp_hashnode **)_cpp_commit_buff
3288 (pfile, sizeof (cpp_hashnode *) * nparms);
3289 token = NULL;
3290 }
3291 else if (token->type != CPP_EOF
3292 && !(token->type == CPP_COMMENT
3293 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp)))
3294 {
3295 /* While ISO C99 requires whitespace before replacement text
3296 in a macro definition, ISO C90 with TC1 allows characters
3297 from the basic source character set there. */
3298 if (CPP_OPTION (pfile, c99))
3299 cpp_error (pfile, CPP_DL_PEDWARN,
3300 CPP_OPTION (pfile, cplusplus)
3301 ? N_("ISO C++11 requires whitespace after the macro name")
3302 : N_("ISO C99 requires whitespace after the macro name"));
3303 else
3304 {
3305 int warntype = CPP_DL_WARNING;
3306 switch (token->type)
3307 {
3308 case CPP_ATSIGN:
3309 case CPP_AT_NAME:
3310 case CPP_OBJC_STRING:
3311 /* '@' is not in basic character set. */
3312 warntype = CPP_DL_PEDWARN;
3313 break;
3314 case CPP_OTHER:
3315 /* Basic character set sans letters, digits and _. */
3316 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
3317 token->val.str.text[0]) == NULL)
3318 warntype = CPP_DL_PEDWARN;
3319 break;
3320 default:
3321 /* All other tokens start with a character from basic
3322 character set. */
3323 break;
3324 }
3325 cpp_error (pfile, warntype,
3326 "missing whitespace after the macro name");
3327 }
3328 }
3329
3330 macro = _cpp_new_macro (pfile, cmk_macro,
3331 _cpp_reserve_room (pfile, 0, sizeof (cpp_macro)));
3332
3333 if (!token)
3334 {
3335 macro->variadic = varadic;
3336 macro->paramc = nparms;
3337 macro->params = params;
3338 macro->fun_like = true;
3339 }
3340 else
3341 {
3342 /* Preserve the token we peeked, there is already a single slot for it. */
3343 macro->exp.tokens[0] = *token;
3344 token = &macro->exp.tokens[0];
3345 macro->count = 1;
3346 }
3347
3348 for (vaopt_state vaopt_tracker (pfile, macro->variadic, true);; token = NULL)
3349 {
3350 if (!token)
3351 {
3352 macro = lex_expansion_token (pfile, macro);
3353 token = &macro->exp.tokens[macro->count++];
3354 }
3355
3356 /* Check the stringifying # constraint 6.10.3.2.1 of
3357 function-like macros when lexing the subsequent token. */
3358 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
3359 {
3360 if (token->type == CPP_MACRO_ARG)
3361 {
3362 if (token->flags & PREV_WHITE)
3363 token->flags |= SP_PREV_WHITE;
3364 if (token[-1].flags & DIGRAPH)
3365 token->flags |= SP_DIGRAPH;
3366 token->flags &= ~PREV_WHITE;
3367 token->flags |= STRINGIFY_ARG;
3368 token->flags |= token[-1].flags & PREV_WHITE;
3369 token[-1] = token[0];
3370 macro->count--;
3371 }
3372 /* Let assembler get away with murder. */
3373 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
3374 {
3375 cpp_error (pfile, CPP_DL_ERROR,
3376 "'#' is not followed by a macro parameter");
3377 goto out;
3378 }
3379 }
3380
3381 if (token->type == CPP_EOF)
3382 {
3383 /* Paste operator constraint 6.10.3.3.1:
3384 Token-paste ##, can appear in both object-like and
3385 function-like macros, but not at the end. */
3386 if (following_paste_op)
3387 {
3388 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
3389 goto out;
3390 }
3391 if (!vaopt_tracker.completed ())
3392 goto out;
3393 break;
3394 }
3395
3396 /* Paste operator constraint 6.10.3.3.1. */
3397 if (token->type == CPP_PASTE)
3398 {
3399 /* Token-paste ##, can appear in both object-like and
3400 function-like macros, but not at the beginning. */
3401 if (macro->count == 1)
3402 {
3403 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
3404 goto out;
3405 }
3406
3407 if (following_paste_op)
3408 {
3409 /* Consecutive paste operators. This one will be moved
3410 to the end. */
3411 num_extra_tokens++;
3412 token->val.token_no = macro->count - 1;
3413 }
3414 else
3415 {
3416 /* Drop the paste operator. */
3417 --macro->count;
3418 token[-1].flags |= PASTE_LEFT;
3419 if (token->flags & DIGRAPH)
3420 token[-1].flags |= SP_DIGRAPH;
3421 if (token->flags & PREV_WHITE)
3422 token[-1].flags |= SP_PREV_WHITE;
3423 }
3424 following_paste_op = true;
3425 }
3426 else
3427 following_paste_op = false;
3428
3429 if (vaopt_tracker.update (token) == vaopt_state::ERROR)
3430 goto out;
3431 }
3432
3433 /* We're committed to winning now. */
3434 ok = true;
3435
3436 /* Don't count the CPP_EOF. */
3437 macro->count--;
3438
3439 macro = (cpp_macro *)_cpp_commit_buff
3440 (pfile, sizeof (cpp_macro) - sizeof (cpp_token)
3441 + sizeof (cpp_token) * macro->count);
3442
3443 /* Clear whitespace on first token for warn_of_redefinition(). */
3444 if (macro->count)
3445 macro->exp.tokens[0].flags &= ~PREV_WHITE;
3446
3447 if (num_extra_tokens)
3448 {
3449 /* Place second and subsequent ## or %:%: tokens in sequences of
3450 consecutive such tokens at the end of the list to preserve
3451 information about where they appear, how they are spelt and
3452 whether they are preceded by whitespace without otherwise
3453 interfering with macro expansion. Remember, this is
3454 extremely rare, so efficiency is not a priority. */
3455 cpp_token *temp = (cpp_token *)_cpp_reserve_room
3456 (pfile, 0, num_extra_tokens * sizeof (cpp_token));
3457 unsigned extra_ix = 0, norm_ix = 0;
3458 cpp_token *exp = macro->exp.tokens;
3459 for (unsigned ix = 0; ix != macro->count; ix++)
3460 if (exp[ix].type == CPP_PASTE)
3461 temp[extra_ix++] = exp[ix];
3462 else
3463 exp[norm_ix++] = exp[ix];
3464 memcpy (&exp[norm_ix], temp, num_extra_tokens * sizeof (cpp_token));
3465
3466 /* Record there are extra tokens. */
3467 macro->extra_tokens = 1;
3468 }
3469
3470 out:
3471 pfile->state.va_args_ok = 0;
3472 _cpp_unsave_parameters (pfile, nparms);
3473
3474 return ok ? macro : NULL;
3475 }
3476
3477 cpp_macro *
3478 _cpp_new_macro (cpp_reader *pfile, cpp_macro_kind kind, void *placement)
3479 {
3480 cpp_macro *macro = (cpp_macro *) placement;
3481
3482 macro->line = pfile->directive_line;
3483 macro->params = 0;
3484 macro->paramc = 0;
3485 macro->variadic = 0;
3486 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
3487 macro->count = 0;
3488 macro->fun_like = 0;
3489 macro->extra_tokens = 0;
3490 /* To suppress some diagnostics. */
3491 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
3492
3493 macro->kind = kind;
3494
3495 return macro;
3496 }
3497
3498 /* Parse a macro and save its expansion. Returns nonzero on success. */
3499 bool
3500 _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
3501 {
3502 cpp_macro *macro;
3503
3504 if (CPP_OPTION (pfile, traditional))
3505 macro = _cpp_create_trad_definition (pfile);
3506 else
3507 macro = create_iso_definition (pfile);
3508
3509 if (!macro)
3510 return false;
3511
3512 if (cpp_macro_p (node))
3513 {
3514 if (CPP_OPTION (pfile, warn_unused_macros))
3515 _cpp_warn_if_unused_macro (pfile, node, NULL);
3516
3517 if (warn_of_redefinition (pfile, node, macro))
3518 {
3519 const int reason
3520 = (cpp_builtin_macro_p (node) && !(node->flags & NODE_WARN))
3521 ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
3522
3523 bool warned =
3524 cpp_pedwarning_with_line (pfile, reason,
3525 pfile->directive_line, 0,
3526 "\"%s\" redefined", NODE_NAME (node));
3527
3528 if (warned && cpp_user_macro_p (node))
3529 cpp_error_with_line (pfile, CPP_DL_NOTE,
3530 node->value.macro->line, 0,
3531 "this is the location of the previous definition");
3532 }
3533 _cpp_free_definition (node);
3534 }
3535
3536 /* Enter definition in hash table. */
3537 node->type = NT_MACRO;
3538 node->value.macro = macro;
3539 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))
3540 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS")
3541 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
3542 in the C standard, as something that one must use in C++.
3543 However DR#593 and C++11 indicate that they play no role in C++.
3544 We special-case them anyway. */
3545 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS")
3546 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS"))
3547 node->flags |= NODE_WARN;
3548
3549 /* If user defines one of the conditional macros, remove the
3550 conditional flag */
3551 node->flags &= ~NODE_CONDITIONAL;
3552
3553 return true;
3554 }
3555
3556 /* Notify the use of NODE in a macro-aware context (i.e. expanding it,
3557 or testing its existance). Also applies any lazy definition. */
3558
3559 extern void
3560 _cpp_notify_macro_use (cpp_reader *pfile, cpp_hashnode *node)
3561 {
3562 node->flags |= NODE_USED;
3563 switch (node->type)
3564 {
3565 case NT_MACRO:
3566 if ((node->flags & NODE_BUILTIN)
3567 && pfile->cb.user_builtin_macro)
3568 pfile->cb.user_builtin_macro (pfile, node);
3569
3570 if (pfile->cb.used_define)
3571 pfile->cb.used_define (pfile, pfile->directive_line, node);
3572 break;
3573
3574 case NT_VOID:
3575 if (pfile->cb.used_undef)
3576 pfile->cb.used_undef (pfile, pfile->directive_line, node);
3577 break;
3578
3579 default:
3580 abort ();
3581 }
3582 }
3583
3584 /* Warn if a token in STRING matches one of a function-like MACRO's
3585 parameters. */
3586 static void
3587 check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
3588 const cpp_string *string)
3589 {
3590 unsigned int i, len;
3591 const uchar *p, *q, *limit;
3592
3593 /* Loop over the string. */
3594 limit = string->text + string->len - 1;
3595 for (p = string->text + 1; p < limit; p = q)
3596 {
3597 /* Find the start of an identifier. */
3598 while (p < limit && !is_idstart (*p))
3599 p++;
3600
3601 /* Find the end of the identifier. */
3602 q = p;
3603 while (q < limit && is_idchar (*q))
3604 q++;
3605
3606 len = q - p;
3607
3608 /* Loop over the function macro arguments to see if the
3609 identifier inside the string matches one of them. */
3610 for (i = 0; i < macro->paramc; i++)
3611 {
3612 const cpp_hashnode *node = macro->params[i];
3613
3614 if (NODE_LEN (node) == len
3615 && !memcmp (p, NODE_NAME (node), len))
3616 {
3617 cpp_warning (pfile, CPP_W_TRADITIONAL,
3618 "macro argument \"%s\" would be stringified in traditional C",
3619 NODE_NAME (node));
3620 break;
3621 }
3622 }
3623 }
3624 }
3625
3626 /* Returns true of NODE is a function-like macro. */
3627 bool
3628 cpp_fun_like_macro_p (cpp_hashnode *node)
3629 {
3630 return (node->type == NT_MACRO
3631 && (node->flags & (NODE_BUILTIN | NODE_MACRO_ARG)) == 0
3632 && node->value.macro->fun_like);
3633 }
3634
3635 /* Returns the name, arguments and expansion of a macro, in a format
3636 suitable to be read back in again, and therefore also for DWARF 2
3637 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
3638 Caller is expected to generate the "#define" bit if needed. The
3639 returned text is temporary, and automatically freed later. */
3640 const unsigned char *
3641 cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
3642 {
3643 unsigned int i, len;
3644 const cpp_macro *macro;
3645 unsigned char *buffer;
3646
3647 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
3648 {
3649 if (node->type != NT_MACRO
3650 || !pfile->cb.user_builtin_macro
3651 || !pfile->cb.user_builtin_macro (pfile, node))
3652 {
3653 cpp_error (pfile, CPP_DL_ICE,
3654 "invalid hash type %d in cpp_macro_definition",
3655 node->type);
3656 return 0;
3657 }
3658 }
3659
3660 macro = node->value.macro;
3661 /* Calculate length. */
3662 len = NODE_LEN (node) * 10 + 2; /* ' ' and NUL. */
3663 if (macro->fun_like)
3664 {
3665 len += 4; /* "()" plus possible final ".." of named
3666 varargs (we have + 1 below). */
3667 for (i = 0; i < macro->paramc; i++)
3668 len += NODE_LEN (macro->params[i]) + 1; /* "," */
3669 }
3670
3671 /* This should match below where we fill in the buffer. */
3672 if (CPP_OPTION (pfile, traditional))
3673 len += _cpp_replacement_text_len (macro);
3674 else
3675 {
3676 unsigned int count = macro_real_token_count (macro);
3677 for (i = 0; i < count; i++)
3678 {
3679 const cpp_token *token = &macro->exp.tokens[i];
3680
3681 if (token->type == CPP_MACRO_ARG)
3682 len += NODE_LEN (token->val.macro_arg.spelling);
3683 else
3684 len += cpp_token_len (token);
3685
3686 if (token->flags & STRINGIFY_ARG)
3687 len++; /* "#" */
3688 if (token->flags & PASTE_LEFT)
3689 len += 3; /* " ##" */
3690 if (token->flags & PREV_WHITE)
3691 len++; /* " " */
3692 }
3693 }
3694
3695 if (len > pfile->macro_buffer_len)
3696 {
3697 pfile->macro_buffer = XRESIZEVEC (unsigned char,
3698 pfile->macro_buffer, len);
3699 pfile->macro_buffer_len = len;
3700 }
3701
3702 /* Fill in the buffer. Start with the macro name. */
3703 buffer = pfile->macro_buffer;
3704 buffer = _cpp_spell_ident_ucns (buffer, node);
3705
3706 /* Parameter names. */
3707 if (macro->fun_like)
3708 {
3709 *buffer++ = '(';
3710 for (i = 0; i < macro->paramc; i++)
3711 {
3712 cpp_hashnode *param = macro->params[i];
3713
3714 if (param != pfile->spec_nodes.n__VA_ARGS__)
3715 {
3716 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
3717 buffer += NODE_LEN (param);
3718 }
3719
3720 if (i + 1 < macro->paramc)
3721 /* Don't emit a space after the comma here; we're trying
3722 to emit a Dwarf-friendly definition, and the Dwarf spec
3723 forbids spaces in the argument list. */
3724 *buffer++ = ',';
3725 else if (macro->variadic)
3726 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
3727 }
3728 *buffer++ = ')';
3729 }
3730
3731 /* The Dwarf spec requires a space after the macro name, even if the
3732 definition is the empty string. */
3733 *buffer++ = ' ';
3734
3735 if (CPP_OPTION (pfile, traditional))
3736 buffer = _cpp_copy_replacement_text (macro, buffer);
3737 else if (macro->count)
3738 /* Expansion tokens. */
3739 {
3740 unsigned int count = macro_real_token_count (macro);
3741 for (i = 0; i < count; i++)
3742 {
3743 const cpp_token *token = &macro->exp.tokens[i];
3744
3745 if (token->flags & PREV_WHITE)
3746 *buffer++ = ' ';
3747 if (token->flags & STRINGIFY_ARG)
3748 *buffer++ = '#';
3749
3750 if (token->type == CPP_MACRO_ARG)
3751 {
3752 memcpy (buffer,
3753 NODE_NAME (token->val.macro_arg.spelling),
3754 NODE_LEN (token->val.macro_arg.spelling));
3755 buffer += NODE_LEN (token->val.macro_arg.spelling);
3756 }
3757 else
3758 buffer = cpp_spell_token (pfile, token, buffer, true);
3759
3760 if (token->flags & PASTE_LEFT)
3761 {
3762 *buffer++ = ' ';
3763 *buffer++ = '#';
3764 *buffer++ = '#';
3765 /* Next has PREV_WHITE; see _cpp_create_definition. */
3766 }
3767 }
3768 }
3769
3770 *buffer = '\0';
3771 return pfile->macro_buffer;
3772 }
3773
3774 /* Get the line at which the macro was defined. */
3775
3776 source_location
3777 cpp_macro_definition_location (cpp_hashnode *node)
3778 {
3779 return node->value.macro->line;
3780 }