Wunused-var-15.c: Add -fno-common additional option on hppa*-*-hpux*.
[official-gcc.git] / libcpp / macro.c
blobde18c2210cfc94d6e211efaa0ae8e8e524961a12
1 /* Part of CPP library. (Macro and #define handling.)
2 Copyright (C) 1986-2017 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
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.
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.
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/>.
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! */
25 #include "config.h"
26 #include "system.h"
27 #include "cpplib.h"
28 #include "internal.h"
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
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. */
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 litteral, 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
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
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
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;
92 /* Macro expansion. */
94 static int enter_macro_context (cpp_reader *, cpp_hashnode *,
95 const cpp_token *, source_location);
96 static int builtin_macro (cpp_reader *, cpp_hashnode *,
97 source_location, source_location);
98 static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
99 const cpp_token **, unsigned int);
100 static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *,
101 _cpp_buff *, source_location *,
102 const cpp_token **, unsigned int);
103 static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *,
104 _cpp_buff **, unsigned *);
105 static cpp_context *next_context (cpp_reader *);
106 static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
107 static void expand_arg (cpp_reader *, macro_arg *);
108 static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
109 static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
110 static void paste_all_tokens (cpp_reader *, const cpp_token *);
111 static bool paste_tokens (cpp_reader *, source_location,
112 const cpp_token **, const cpp_token *);
113 static void alloc_expanded_arg_mem (cpp_reader *, macro_arg *, size_t);
114 static void ensure_expanded_arg_room (cpp_reader *, macro_arg *, size_t, size_t *);
115 static void delete_macro_args (_cpp_buff*, unsigned num_args);
116 static void set_arg_token (macro_arg *, const cpp_token *,
117 source_location, size_t,
118 enum macro_arg_token_kind,
119 bool);
120 static const source_location *get_arg_token_location (const macro_arg *,
121 enum macro_arg_token_kind);
122 static const cpp_token **arg_token_ptr_at (const macro_arg *,
123 size_t,
124 enum macro_arg_token_kind,
125 source_location **virt_location);
127 static void macro_arg_token_iter_init (macro_arg_token_iter *, bool,
128 enum macro_arg_token_kind,
129 const macro_arg *,
130 const cpp_token **);
131 static const cpp_token *macro_arg_token_iter_get_token
132 (const macro_arg_token_iter *it);
133 static source_location macro_arg_token_iter_get_location
134 (const macro_arg_token_iter *);
135 static void macro_arg_token_iter_forward (macro_arg_token_iter *);
136 static _cpp_buff *tokens_buff_new (cpp_reader *, size_t,
137 source_location **);
138 static size_t tokens_buff_count (_cpp_buff *);
139 static const cpp_token **tokens_buff_last_token_ptr (_cpp_buff *);
140 static inline const cpp_token **tokens_buff_put_token_to (const cpp_token **,
141 source_location *,
142 const cpp_token *,
143 source_location,
144 source_location,
145 const line_map_macro *,
146 unsigned int);
148 static const cpp_token **tokens_buff_add_token (_cpp_buff *,
149 source_location *,
150 const cpp_token *,
151 source_location,
152 source_location,
153 const line_map_macro *,
154 unsigned int);
155 static inline void tokens_buff_remove_last_token (_cpp_buff *);
156 static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
157 macro_arg *, source_location);
158 static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *,
159 _cpp_buff **, unsigned *);
160 static bool create_iso_definition (cpp_reader *, cpp_macro *);
162 /* #define directive parsing and handling. */
164 static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *);
165 static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *);
166 static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *,
167 const cpp_macro *);
168 static bool parse_params (cpp_reader *, cpp_macro *);
169 static void check_trad_stringification (cpp_reader *, const cpp_macro *,
170 const cpp_string *);
171 static bool reached_end_of_context (cpp_context *);
172 static void consume_next_token_from_context (cpp_reader *pfile,
173 const cpp_token **,
174 source_location *);
175 static const cpp_token* cpp_get_token_1 (cpp_reader *, source_location *);
177 static cpp_hashnode* macro_of_context (cpp_context *context);
179 static bool in_macro_expansion_p (cpp_reader *pfile);
181 /* Statistical counter tracking the number of macros that got
182 expanded. */
183 unsigned num_expanded_macros_counter = 0;
184 /* Statistical counter tracking the total number tokens resulting
185 from macro expansion. */
186 unsigned num_macro_tokens_counter = 0;
188 /* Emits a warning if NODE is a macro defined in the main file that
189 has not been used. */
191 _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
192 void *v ATTRIBUTE_UNUSED)
194 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
196 cpp_macro *macro = node->value.macro;
198 if (!macro->used
199 && MAIN_FILE_P (linemap_check_ordinary
200 (linemap_lookup (pfile->line_table,
201 macro->line))))
202 cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0,
203 "macro \"%s\" is not used", NODE_NAME (node));
206 return 1;
209 /* Allocates and returns a CPP_STRING token, containing TEXT of length
210 LEN, after null-terminating it. TEXT must be in permanent storage. */
211 static const cpp_token *
212 new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len)
214 cpp_token *token = _cpp_temp_token (pfile);
216 text[len] = '\0';
217 token->type = CPP_STRING;
218 token->val.str.len = len;
219 token->val.str.text = text;
220 token->flags = 0;
221 return token;
224 static const char * const monthnames[] =
226 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
227 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
230 /* Helper function for builtin_macro. Returns the text generated by
231 a builtin macro. */
232 const uchar *
233 _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node,
234 source_location loc)
236 const uchar *result = NULL;
237 linenum_type number = 1;
239 switch (node->value.builtin)
241 default:
242 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
243 NODE_NAME (node));
244 break;
246 case BT_TIMESTAMP:
248 if (CPP_OPTION (pfile, warn_date_time))
249 cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent "
250 "reproducible builds", NODE_NAME (node));
252 cpp_buffer *pbuffer = cpp_get_buffer (pfile);
253 if (pbuffer->timestamp == NULL)
255 /* Initialize timestamp value of the assotiated file. */
256 struct _cpp_file *file = cpp_get_file (pbuffer);
257 if (file)
259 /* Generate __TIMESTAMP__ string, that represents
260 the date and time of the last modification
261 of the current source file. The string constant
262 looks like "Sun Sep 16 01:03:52 1973". */
263 struct tm *tb = NULL;
264 struct stat *st = _cpp_get_file_stat (file);
265 if (st)
266 tb = localtime (&st->st_mtime);
267 if (tb)
269 char *str = asctime (tb);
270 size_t len = strlen (str);
271 unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2);
272 buf[0] = '"';
273 strcpy ((char *) buf + 1, str);
274 buf[len] = '"';
275 pbuffer->timestamp = buf;
277 else
279 cpp_errno (pfile, CPP_DL_WARNING,
280 "could not determine file timestamp");
281 pbuffer->timestamp = UC"\"??? ??? ?? ??:??:?? ????\"";
285 result = pbuffer->timestamp;
287 break;
288 case BT_FILE:
289 case BT_BASE_FILE:
291 unsigned int len;
292 const char *name;
293 uchar *buf;
295 if (node->value.builtin == BT_FILE)
296 name = linemap_get_expansion_filename (pfile->line_table,
297 pfile->line_table->highest_line);
298 else
300 name = _cpp_get_file_name (pfile->main_file);
301 if (!name)
302 abort ();
304 len = strlen (name);
305 buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
306 result = buf;
307 *buf = '"';
308 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
309 *buf++ = '"';
310 *buf = '\0';
312 break;
314 case BT_INCLUDE_LEVEL:
315 /* The line map depth counts the primary source as level 1, but
316 historically __INCLUDE_DEPTH__ has called the primary source
317 level 0. */
318 number = pfile->line_table->depth - 1;
319 break;
321 case BT_SPECLINE:
322 /* If __LINE__ is embedded in a macro, it must expand to the
323 line of the macro's invocation, not its definition.
324 Otherwise things like assert() will not work properly.
325 See WG14 N1911, WG21 N4220 sec 6.5, and PR 61861. */
326 if (CPP_OPTION (pfile, traditional))
327 loc = pfile->line_table->highest_line;
328 else
329 loc = linemap_resolve_location (pfile->line_table, loc,
330 LRK_MACRO_EXPANSION_POINT, NULL);
331 number = linemap_get_expansion_line (pfile->line_table, loc);
332 break;
334 /* __STDC__ has the value 1 under normal circumstances.
335 However, if (a) we are in a system header, (b) the option
336 stdc_0_in_system_headers is true (set by target config), and
337 (c) we are not in strictly conforming mode, then it has the
338 value 0. (b) and (c) are already checked in cpp_init_builtins. */
339 case BT_STDC:
340 if (cpp_in_system_header (pfile))
341 number = 0;
342 else
343 number = 1;
344 break;
346 case BT_DATE:
347 case BT_TIME:
348 if (CPP_OPTION (pfile, warn_date_time))
349 cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent "
350 "reproducible builds", NODE_NAME (node));
351 if (pfile->date == NULL)
353 /* Allocate __DATE__ and __TIME__ strings from permanent
354 storage. We only do this once, and don't generate them
355 at init time, because time() and localtime() are very
356 slow on some systems. */
357 time_t tt;
358 struct tm *tb = NULL;
360 /* Set a reproducible timestamp for __DATE__ and __TIME__ macro
361 if SOURCE_DATE_EPOCH is defined. */
362 if (pfile->source_date_epoch == (time_t) -2
363 && pfile->cb.get_source_date_epoch != NULL)
364 pfile->source_date_epoch = pfile->cb.get_source_date_epoch (pfile);
366 if (pfile->source_date_epoch >= (time_t) 0)
367 tb = gmtime (&pfile->source_date_epoch);
368 else
370 /* (time_t) -1 is a legitimate value for "number of seconds
371 since the Epoch", so we have to do a little dance to
372 distinguish that from a genuine error. */
373 errno = 0;
374 tt = time (NULL);
375 if (tt != (time_t)-1 || errno == 0)
376 tb = localtime (&tt);
379 if (tb)
381 pfile->date = _cpp_unaligned_alloc (pfile,
382 sizeof ("\"Oct 11 1347\""));
383 sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
384 monthnames[tb->tm_mon], tb->tm_mday,
385 tb->tm_year + 1900);
387 pfile->time = _cpp_unaligned_alloc (pfile,
388 sizeof ("\"12:34:56\""));
389 sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
390 tb->tm_hour, tb->tm_min, tb->tm_sec);
392 else
394 cpp_errno (pfile, CPP_DL_WARNING,
395 "could not determine date and time");
397 pfile->date = UC"\"??? ?? ????\"";
398 pfile->time = UC"\"??:??:??\"";
402 if (node->value.builtin == BT_DATE)
403 result = pfile->date;
404 else
405 result = pfile->time;
406 break;
408 case BT_COUNTER:
409 if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive)
410 cpp_error (pfile, CPP_DL_ERROR,
411 "__COUNTER__ expanded inside directive with -fdirectives-only");
412 number = pfile->counter++;
413 break;
415 case BT_HAS_ATTRIBUTE:
416 number = pfile->cb.has_attribute (pfile);
417 break;
420 if (result == NULL)
422 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
423 result = _cpp_unaligned_alloc (pfile, 21);
424 sprintf ((char *) result, "%u", number);
427 return result;
430 /* Convert builtin macros like __FILE__ to a token and push it on the
431 context stack. Also handles _Pragma, for which a new token may not
432 be created. Returns 1 if it generates a new token context, 0 to
433 return the token to the caller. LOC is the location of the expansion
434 point of the macro. */
435 static int
436 builtin_macro (cpp_reader *pfile, cpp_hashnode *node,
437 source_location loc, source_location expand_loc)
439 const uchar *buf;
440 size_t len;
441 char *nbuf;
443 if (node->value.builtin == BT_PRAGMA)
445 /* Don't interpret _Pragma within directives. The standard is
446 not clear on this, but to me this makes most sense. */
447 if (pfile->state.in_directive)
448 return 0;
450 return _cpp_do__Pragma (pfile, loc);
453 buf = _cpp_builtin_macro_text (pfile, node, expand_loc);
454 len = ustrlen (buf);
455 nbuf = (char *) alloca (len + 1);
456 memcpy (nbuf, buf, len);
457 nbuf[len]='\n';
459 cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
460 _cpp_clean_line (pfile);
462 /* Set pfile->cur_token as required by _cpp_lex_direct. */
463 pfile->cur_token = _cpp_temp_token (pfile);
464 cpp_token *token = _cpp_lex_direct (pfile);
465 /* We should point to the expansion point of the builtin macro. */
466 token->src_loc = loc;
467 if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
469 /* We are tracking tokens resulting from macro expansion.
470 Create a macro line map and generate a virtual location for
471 the token resulting from the expansion of the built-in
472 macro. */
473 source_location *virt_locs = NULL;
474 _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
475 const line_map_macro * map =
476 linemap_enter_macro (pfile->line_table, node, loc, 1);
477 tokens_buff_add_token (token_buf, virt_locs, token,
478 pfile->line_table->builtin_location,
479 pfile->line_table->builtin_location,
480 map, /*macro_token_index=*/0);
481 push_extended_tokens_context (pfile, node, token_buf, virt_locs,
482 (const cpp_token **)token_buf->base,
485 else
486 _cpp_push_token_context (pfile, NULL, token, 1);
487 if (pfile->buffer->cur != pfile->buffer->rlimit)
488 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
489 NODE_NAME (node));
490 _cpp_pop_buffer (pfile);
492 return 1;
495 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
496 backslashes and double quotes. DEST must be of sufficient size.
497 Returns a pointer to the end of the string. */
498 uchar *
499 cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
501 while (len--)
503 uchar c = *src++;
505 if (c == '\\' || c == '"')
507 *dest++ = '\\';
508 *dest++ = c;
510 else
511 *dest++ = c;
514 return dest;
517 /* Convert a token sequence ARG to a single string token according to
518 the rules of the ISO C #-operator. */
519 static const cpp_token *
520 stringify_arg (cpp_reader *pfile, macro_arg *arg)
522 unsigned char *dest;
523 unsigned int i, escape_it, backslash_count = 0;
524 const cpp_token *source = NULL;
525 size_t len;
527 if (BUFF_ROOM (pfile->u_buff) < 3)
528 _cpp_extend_buff (pfile, &pfile->u_buff, 3);
529 dest = BUFF_FRONT (pfile->u_buff);
530 *dest++ = '"';
532 /* Loop, reading in the argument's tokens. */
533 for (i = 0; i < arg->count; i++)
535 const cpp_token *token = arg->first[i];
537 if (token->type == CPP_PADDING)
539 if (source == NULL
540 || (!(source->flags & PREV_WHITE)
541 && token->val.source == NULL))
542 source = token->val.source;
543 continue;
546 escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR
547 || token->type == CPP_WSTRING || token->type == CPP_WCHAR
548 || token->type == CPP_STRING32 || token->type == CPP_CHAR32
549 || token->type == CPP_STRING16 || token->type == CPP_CHAR16
550 || token->type == CPP_UTF8STRING || token->type == CPP_UTF8CHAR
551 || cpp_userdef_string_p (token->type)
552 || cpp_userdef_char_p (token->type));
554 /* Room for each char being written in octal, initial space and
555 final quote and NUL. */
556 len = cpp_token_len (token);
557 if (escape_it)
558 len *= 4;
559 len += 3;
561 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
563 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
564 _cpp_extend_buff (pfile, &pfile->u_buff, len);
565 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
568 /* Leading white space? */
569 if (dest - 1 != BUFF_FRONT (pfile->u_buff))
571 if (source == NULL)
572 source = token;
573 if (source->flags & PREV_WHITE)
574 *dest++ = ' ';
576 source = NULL;
578 if (escape_it)
580 _cpp_buff *buff = _cpp_get_buff (pfile, len);
581 unsigned char *buf = BUFF_FRONT (buff);
582 len = cpp_spell_token (pfile, token, buf, true) - buf;
583 dest = cpp_quote_string (dest, buf, len);
584 _cpp_release_buff (pfile, buff);
586 else
587 dest = cpp_spell_token (pfile, token, dest, true);
589 if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
590 backslash_count++;
591 else
592 backslash_count = 0;
595 /* Ignore the final \ of invalid string literals. */
596 if (backslash_count & 1)
598 cpp_error (pfile, CPP_DL_WARNING,
599 "invalid string literal, ignoring final '\\'");
600 dest--;
603 /* Commit the memory, including NUL, and return the token. */
604 *dest++ = '"';
605 len = dest - BUFF_FRONT (pfile->u_buff);
606 BUFF_FRONT (pfile->u_buff) = dest + 1;
607 return new_string_token (pfile, dest - len, len);
610 /* Try to paste two tokens. On success, return nonzero. In any
611 case, PLHS is updated to point to the pasted token, which is
612 guaranteed to not have the PASTE_LEFT flag set. LOCATION is
613 the virtual location used for error reporting. */
614 static bool
615 paste_tokens (cpp_reader *pfile, source_location location,
616 const cpp_token **plhs, const cpp_token *rhs)
618 unsigned char *buf, *end, *lhsend;
619 cpp_token *lhs;
620 unsigned int len;
622 len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1;
623 buf = (unsigned char *) alloca (len);
624 end = lhsend = cpp_spell_token (pfile, *plhs, buf, true);
626 /* Avoid comment headers, since they are still processed in stage 3.
627 It is simpler to insert a space here, rather than modifying the
628 lexer to ignore comments in some circumstances. Simply returning
629 false doesn't work, since we want to clear the PASTE_LEFT flag. */
630 if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ)
631 *end++ = ' ';
632 /* In one obscure case we might see padding here. */
633 if (rhs->type != CPP_PADDING)
634 end = cpp_spell_token (pfile, rhs, end, true);
635 *end = '\n';
637 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
638 _cpp_clean_line (pfile);
640 /* Set pfile->cur_token as required by _cpp_lex_direct. */
641 pfile->cur_token = _cpp_temp_token (pfile);
642 lhs = _cpp_lex_direct (pfile);
643 if (pfile->buffer->cur != pfile->buffer->rlimit)
645 source_location saved_loc = lhs->src_loc;
647 _cpp_pop_buffer (pfile);
648 _cpp_backup_tokens (pfile, 1);
649 *lhsend = '\0';
651 /* We have to remove the PASTE_LEFT flag from the old lhs, but
652 we want to keep the new location. */
653 *lhs = **plhs;
654 *plhs = lhs;
655 lhs->src_loc = saved_loc;
656 lhs->flags &= ~PASTE_LEFT;
658 /* Mandatory error for all apart from assembler. */
659 if (CPP_OPTION (pfile, lang) != CLK_ASM)
660 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
661 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
662 buf, cpp_token_as_text (pfile, rhs));
663 return false;
666 *plhs = lhs;
667 _cpp_pop_buffer (pfile);
668 return true;
671 /* Handles an arbitrarily long sequence of ## operators, with initial
672 operand LHS. This implementation is left-associative,
673 non-recursive, and finishes a paste before handling succeeding
674 ones. If a paste fails, we back up to the RHS of the failing ##
675 operator before pushing the context containing the result of prior
676 successful pastes, with the effect that the RHS appears in the
677 output stream after the pasted LHS normally. */
678 static void
679 paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
681 const cpp_token *rhs = NULL;
682 cpp_context *context = pfile->context;
683 source_location virt_loc = 0;
685 /* We are expanding a macro and we must have been called on a token
686 that appears at the left hand side of a ## operator. */
687 if (macro_of_context (pfile->context) == NULL
688 || (!(lhs->flags & PASTE_LEFT)))
689 abort ();
691 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
692 /* The caller must have called consume_next_token_from_context
693 right before calling us. That has incremented the pointer to
694 the current virtual location. So it now points to the location
695 of the token that comes right after *LHS. We want the
696 resulting pasted token to have the location of the current
697 *LHS, though. */
698 virt_loc = context->c.mc->cur_virt_loc[-1];
699 else
700 /* We are not tracking macro expansion. So the best virtual
701 location we can get here is the expansion point of the macro we
702 are currently expanding. */
703 virt_loc = pfile->invocation_location;
707 /* Take the token directly from the current context. We can do
708 this, because we are in the replacement list of either an
709 object-like macro, or a function-like macro with arguments
710 inserted. In either case, the constraints to #define
711 guarantee we have at least one more token. */
712 if (context->tokens_kind == TOKENS_KIND_DIRECT)
713 rhs = FIRST (context).token++;
714 else if (context->tokens_kind == TOKENS_KIND_INDIRECT)
715 rhs = *FIRST (context).ptoken++;
716 else if (context->tokens_kind == TOKENS_KIND_EXTENDED)
718 /* So we are in presence of an extended token context, which
719 means that each token in this context has a virtual
720 location attached to it. So let's not forget to update
721 the pointer to the current virtual location of the
722 current token when we update the pointer to the current
723 token */
725 rhs = *FIRST (context).ptoken++;
726 /* context->c.mc must be non-null, as if we were not in a
727 macro context, context->tokens_kind could not be equal to
728 TOKENS_KIND_EXTENDED. */
729 context->c.mc->cur_virt_loc++;
732 if (rhs->type == CPP_PADDING)
734 if (rhs->flags & PASTE_LEFT)
735 abort ();
737 if (!paste_tokens (pfile, virt_loc, &lhs, rhs))
738 break;
740 while (rhs->flags & PASTE_LEFT);
742 /* Put the resulting token in its own context. */
743 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
745 source_location *virt_locs = NULL;
746 _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
747 tokens_buff_add_token (token_buf, virt_locs, lhs,
748 virt_loc, 0, NULL, 0);
749 push_extended_tokens_context (pfile, context->c.mc->macro_node,
750 token_buf, virt_locs,
751 (const cpp_token **)token_buf->base, 1);
753 else
754 _cpp_push_token_context (pfile, NULL, lhs, 1);
757 /* Returns TRUE if the number of arguments ARGC supplied in an
758 invocation of the MACRO referenced by NODE is valid. An empty
759 invocation to a macro with no parameters should pass ARGC as zero.
761 Note that MACRO cannot necessarily be deduced from NODE, in case
762 NODE was redefined whilst collecting arguments. */
763 bool
764 _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
766 if (argc == macro->paramc)
767 return true;
769 if (argc < macro->paramc)
771 /* As an extension, variadic arguments are allowed to not appear in
772 the invocation at all.
773 e.g. #define debug(format, args...) something
774 debug("string");
776 This is exactly the same as if an empty variadic list had been
777 supplied - debug("string", ). */
779 if (argc + 1 == macro->paramc && macro->variadic)
781 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
783 if (CPP_OPTION (pfile, cplusplus))
784 cpp_error (pfile, CPP_DL_PEDWARN,
785 "ISO C++11 requires at least one argument "
786 "for the \"...\" in a variadic macro");
787 else
788 cpp_error (pfile, CPP_DL_PEDWARN,
789 "ISO C99 requires at least one argument "
790 "for the \"...\" in a variadic macro");
792 return true;
795 cpp_error (pfile, CPP_DL_ERROR,
796 "macro \"%s\" requires %u arguments, but only %u given",
797 NODE_NAME (node), macro->paramc, argc);
799 else
800 cpp_error (pfile, CPP_DL_ERROR,
801 "macro \"%s\" passed %u arguments, but takes just %u",
802 NODE_NAME (node), argc, macro->paramc);
804 return false;
807 /* Reads and returns the arguments to a function-like macro
808 invocation. Assumes the opening parenthesis has been processed.
809 If there is an error, emits an appropriate diagnostic and returns
810 NULL. Each argument is terminated by a CPP_EOF token, for the
811 future benefit of expand_arg(). If there are any deferred
812 #pragma directives among macro arguments, store pointers to the
813 CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.
815 What is returned is the buffer that contains the memory allocated
816 to hold the macro arguments. NODE is the name of the macro this
817 function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is
818 set to the actual number of macro arguments allocated in the
819 returned buffer. */
820 static _cpp_buff *
821 collect_args (cpp_reader *pfile, const cpp_hashnode *node,
822 _cpp_buff **pragma_buff, unsigned *num_args)
824 _cpp_buff *buff, *base_buff;
825 cpp_macro *macro;
826 macro_arg *args, *arg;
827 const cpp_token *token;
828 unsigned int argc;
829 source_location virt_loc;
830 bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion);
831 unsigned num_args_alloced = 0;
833 macro = node->value.macro;
834 if (macro->paramc)
835 argc = macro->paramc;
836 else
837 argc = 1;
839 #define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50
840 #define ARG_TOKENS_EXTENT 1000
842 buff = _cpp_get_buff (pfile, argc * (DEFAULT_NUM_TOKENS_PER_MACRO_ARG
843 * sizeof (cpp_token *)
844 + sizeof (macro_arg)));
845 base_buff = buff;
846 args = (macro_arg *) buff->base;
847 memset (args, 0, argc * sizeof (macro_arg));
848 buff->cur = (unsigned char *) &args[argc];
849 arg = args, argc = 0;
851 /* Collect the tokens making up each argument. We don't yet know
852 how many arguments have been supplied, whether too many or too
853 few. Hence the slightly bizarre usage of "argc" and "arg". */
856 unsigned int paren_depth = 0;
857 unsigned int ntokens = 0;
858 unsigned virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
859 num_args_alloced++;
861 argc++;
862 arg->first = (const cpp_token **) buff->cur;
863 if (track_macro_expansion_p)
865 virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
866 arg->virt_locs = XNEWVEC (source_location,
867 virt_locs_capacity);
870 for (;;)
872 /* Require space for 2 new tokens (including a CPP_EOF). */
873 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
875 buff = _cpp_append_extend_buff (pfile, buff,
876 ARG_TOKENS_EXTENT
877 * sizeof (cpp_token *));
878 arg->first = (const cpp_token **) buff->cur;
880 if (track_macro_expansion_p
881 && (ntokens + 2 > virt_locs_capacity))
883 virt_locs_capacity += ARG_TOKENS_EXTENT;
884 arg->virt_locs = XRESIZEVEC (source_location,
885 arg->virt_locs,
886 virt_locs_capacity);
889 token = cpp_get_token_1 (pfile, &virt_loc);
891 if (token->type == CPP_PADDING)
893 /* Drop leading padding. */
894 if (ntokens == 0)
895 continue;
897 else if (token->type == CPP_OPEN_PAREN)
898 paren_depth++;
899 else if (token->type == CPP_CLOSE_PAREN)
901 if (paren_depth-- == 0)
902 break;
904 else if (token->type == CPP_COMMA)
906 /* A comma does not terminate an argument within
907 parentheses or as part of a variable argument. */
908 if (paren_depth == 0
909 && ! (macro->variadic && argc == macro->paramc))
910 break;
912 else if (token->type == CPP_EOF
913 || (token->type == CPP_HASH && token->flags & BOL))
914 break;
915 else if (token->type == CPP_PRAGMA)
917 cpp_token *newtok = _cpp_temp_token (pfile);
919 /* CPP_PRAGMA token lives in directive_result, which will
920 be overwritten on the next directive. */
921 *newtok = *token;
922 token = newtok;
925 if (*pragma_buff == NULL
926 || BUFF_ROOM (*pragma_buff) < sizeof (cpp_token *))
928 _cpp_buff *next;
929 if (*pragma_buff == NULL)
930 *pragma_buff
931 = _cpp_get_buff (pfile, 32 * sizeof (cpp_token *));
932 else
934 next = *pragma_buff;
935 *pragma_buff
936 = _cpp_get_buff (pfile,
937 (BUFF_FRONT (*pragma_buff)
938 - (*pragma_buff)->base) * 2);
939 (*pragma_buff)->next = next;
942 *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token;
943 BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *);
944 if (token->type == CPP_PRAGMA_EOL)
945 break;
946 token = cpp_get_token_1 (pfile, &virt_loc);
948 while (token->type != CPP_EOF);
950 /* In deferred pragmas parsing_args and prevent_expansion
951 had been changed, reset it. */
952 pfile->state.parsing_args = 2;
953 pfile->state.prevent_expansion = 1;
955 if (token->type == CPP_EOF)
956 break;
957 else
958 continue;
960 set_arg_token (arg, token, virt_loc,
961 ntokens, MACRO_ARG_TOKEN_NORMAL,
962 CPP_OPTION (pfile, track_macro_expansion));
963 ntokens++;
966 /* Drop trailing padding. */
967 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
968 ntokens--;
970 arg->count = ntokens;
971 set_arg_token (arg, &pfile->eof, pfile->eof.src_loc,
972 ntokens, MACRO_ARG_TOKEN_NORMAL,
973 CPP_OPTION (pfile, track_macro_expansion));
975 /* Terminate the argument. Excess arguments loop back and
976 overwrite the final legitimate argument, before failing. */
977 if (argc <= macro->paramc)
979 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
980 if (argc != macro->paramc)
981 arg++;
984 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
986 if (token->type == CPP_EOF)
988 /* We still need the CPP_EOF to end directives, and to end
989 pre-expansion of a macro argument. Step back is not
990 unconditional, since we don't want to return a CPP_EOF to our
991 callers at the end of an -include-d file. */
992 if (pfile->context->prev || pfile->state.in_directive)
993 _cpp_backup_tokens (pfile, 1);
994 cpp_error (pfile, CPP_DL_ERROR,
995 "unterminated argument list invoking macro \"%s\"",
996 NODE_NAME (node));
998 else
1000 /* A single empty argument is counted as no argument. */
1001 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
1002 argc = 0;
1003 if (_cpp_arguments_ok (pfile, macro, node, argc))
1005 /* GCC has special semantics for , ## b where b is a varargs
1006 parameter: we remove the comma if b was omitted entirely.
1007 If b was merely an empty argument, the comma is retained.
1008 If the macro takes just one (varargs) parameter, then we
1009 retain the comma only if we are standards conforming.
1011 If FIRST is NULL replace_args () swallows the comma. */
1012 if (macro->variadic && (argc < macro->paramc
1013 || (argc == 1 && args[0].count == 0
1014 && !CPP_OPTION (pfile, std))))
1015 args[macro->paramc - 1].first = NULL;
1016 if (num_args)
1017 *num_args = num_args_alloced;
1018 return base_buff;
1022 /* An error occurred. */
1023 _cpp_release_buff (pfile, base_buff);
1024 return NULL;
1027 /* Search for an opening parenthesis to the macro of NODE, in such a
1028 way that, if none is found, we don't lose the information in any
1029 intervening padding tokens. If we find the parenthesis, collect
1030 the arguments and return the buffer containing them. PRAGMA_BUFF
1031 argument is the same as in collect_args. If NUM_ARGS is non-NULL,
1032 *NUM_ARGS is set to the number of arguments contained in the
1033 returned buffer. */
1034 static _cpp_buff *
1035 funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node,
1036 _cpp_buff **pragma_buff, unsigned *num_args)
1038 const cpp_token *token, *padding = NULL;
1040 for (;;)
1042 token = cpp_get_token (pfile);
1043 if (token->type != CPP_PADDING)
1044 break;
1045 if (padding == NULL
1046 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
1047 padding = token;
1050 if (token->type == CPP_OPEN_PAREN)
1052 pfile->state.parsing_args = 2;
1053 return collect_args (pfile, node, pragma_buff, num_args);
1056 /* CPP_EOF can be the end of macro arguments, or the end of the
1057 file. We mustn't back up over the latter. Ugh. */
1058 if (token->type != CPP_EOF || token == &pfile->eof)
1060 /* Back up. We may have skipped padding, in which case backing
1061 up more than one token when expanding macros is in general
1062 too difficult. We re-insert it in its own context. */
1063 _cpp_backup_tokens (pfile, 1);
1064 if (padding)
1065 _cpp_push_token_context (pfile, NULL, padding, 1);
1068 return NULL;
1071 /* Return the real number of tokens in the expansion of MACRO. */
1072 static inline unsigned int
1073 macro_real_token_count (const cpp_macro *macro)
1075 unsigned int i;
1076 if (__builtin_expect (!macro->extra_tokens, true))
1077 return macro->count;
1078 for (i = 0; i < macro->count; i++)
1079 if (macro->exp.tokens[i].type == CPP_PASTE)
1080 return i;
1081 abort ();
1084 /* Push the context of a macro with hash entry NODE onto the context
1085 stack. If we can successfully expand the macro, we push a context
1086 containing its yet-to-be-rescanned replacement list and return one.
1087 If there were additionally any unexpanded deferred #pragma
1088 directives among macro arguments, push another context containing
1089 the pragma tokens before the yet-to-be-rescanned replacement list
1090 and return two. Otherwise, we don't push a context and return
1091 zero. LOCATION is the location of the expansion point of the
1092 macro. */
1093 static int
1094 enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
1095 const cpp_token *result, source_location location)
1097 /* The presence of a macro invalidates a file's controlling macro. */
1098 pfile->mi_valid = false;
1100 pfile->state.angled_headers = false;
1102 /* From here to when we push the context for the macro later down
1103 this function, we need to flag the fact that we are about to
1104 expand a macro. This is useful when -ftrack-macro-expansion is
1105 turned off. In that case, we need to record the location of the
1106 expansion point of the top-most macro we are about to to expand,
1107 into pfile->invocation_location. But we must not record any such
1108 location once the process of expanding the macro starts; that is,
1109 we must not do that recording between now and later down this
1110 function where set this flag to FALSE. */
1111 pfile->about_to_expand_macro_p = true;
1113 if ((node->flags & NODE_BUILTIN) && !(node->flags & NODE_USED))
1115 node->flags |= NODE_USED;
1116 if ((!pfile->cb.user_builtin_macro
1117 || !pfile->cb.user_builtin_macro (pfile, node))
1118 && pfile->cb.used_define)
1119 pfile->cb.used_define (pfile, pfile->directive_line, node);
1122 /* Handle standard macros. */
1123 if (! (node->flags & NODE_BUILTIN))
1125 cpp_macro *macro = node->value.macro;
1126 _cpp_buff *pragma_buff = NULL;
1128 if (macro->fun_like)
1130 _cpp_buff *buff;
1131 unsigned num_args = 0;
1133 pfile->state.prevent_expansion++;
1134 pfile->keep_tokens++;
1135 pfile->state.parsing_args = 1;
1136 buff = funlike_invocation_p (pfile, node, &pragma_buff,
1137 &num_args);
1138 pfile->state.parsing_args = 0;
1139 pfile->keep_tokens--;
1140 pfile->state.prevent_expansion--;
1142 if (buff == NULL)
1144 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
1145 cpp_warning (pfile, CPP_W_TRADITIONAL,
1146 "function-like macro \"%s\" must be used with arguments in traditional C",
1147 NODE_NAME (node));
1149 if (pragma_buff)
1150 _cpp_release_buff (pfile, pragma_buff);
1152 pfile->about_to_expand_macro_p = false;
1153 return 0;
1156 if (macro->paramc > 0)
1157 replace_args (pfile, node, macro,
1158 (macro_arg *) buff->base,
1159 location);
1160 /* Free the memory used by the arguments of this
1161 function-like macro. This memory has been allocated by
1162 funlike_invocation_p and by replace_args. */
1163 delete_macro_args (buff, num_args);
1166 /* Disable the macro within its expansion. */
1167 node->flags |= NODE_DISABLED;
1169 if (!(node->flags & NODE_USED))
1171 node->flags |= NODE_USED;
1172 if (pfile->cb.used_define)
1173 pfile->cb.used_define (pfile, pfile->directive_line, node);
1176 if (pfile->cb.used)
1177 pfile->cb.used (pfile, location, node);
1179 macro->used = 1;
1181 if (macro->paramc == 0)
1183 unsigned tokens_count = macro_real_token_count (macro);
1184 if (CPP_OPTION (pfile, track_macro_expansion))
1186 unsigned int i;
1187 const cpp_token *src = macro->exp.tokens;
1188 const line_map_macro *map;
1189 source_location *virt_locs = NULL;
1190 _cpp_buff *macro_tokens
1191 = tokens_buff_new (pfile, tokens_count, &virt_locs);
1193 /* Create a macro map to record the locations of the
1194 tokens that are involved in the expansion. LOCATION
1195 is the location of the macro expansion point. */
1196 map = linemap_enter_macro (pfile->line_table,
1197 node, location, tokens_count);
1198 for (i = 0; i < tokens_count; ++i)
1200 tokens_buff_add_token (macro_tokens, virt_locs,
1201 src, src->src_loc,
1202 src->src_loc, map, i);
1203 ++src;
1205 push_extended_tokens_context (pfile, node,
1206 macro_tokens,
1207 virt_locs,
1208 (const cpp_token **)
1209 macro_tokens->base,
1210 tokens_count);
1212 else
1213 _cpp_push_token_context (pfile, node, macro->exp.tokens,
1214 tokens_count);
1215 num_macro_tokens_counter += tokens_count;
1218 if (pragma_buff)
1220 if (!pfile->state.in_directive)
1221 _cpp_push_token_context (pfile, NULL,
1222 padding_token (pfile, result), 1);
1225 unsigned tokens_count;
1226 _cpp_buff *tail = pragma_buff->next;
1227 pragma_buff->next = NULL;
1228 tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff)
1229 - (const cpp_token **) pragma_buff->base);
1230 push_ptoken_context (pfile, NULL, pragma_buff,
1231 (const cpp_token **) pragma_buff->base,
1232 tokens_count);
1233 pragma_buff = tail;
1234 if (!CPP_OPTION (pfile, track_macro_expansion))
1235 num_macro_tokens_counter += tokens_count;
1238 while (pragma_buff != NULL);
1239 pfile->about_to_expand_macro_p = false;
1240 return 2;
1243 pfile->about_to_expand_macro_p = false;
1244 return 1;
1247 pfile->about_to_expand_macro_p = false;
1248 /* Handle built-in macros and the _Pragma operator. */
1250 source_location loc, expand_loc;
1252 if (/* The top-level macro invocation that triggered the expansion
1253 we are looking at is with a standard macro ...*/
1254 !(pfile->top_most_macro_node->flags & NODE_BUILTIN)
1255 /* ... and it's a function-like macro invocation. */
1256 && pfile->top_most_macro_node->value.macro->fun_like)
1258 /* Then the location of the end of the macro invocation is the
1259 location of the closing parenthesis. */
1260 loc = pfile->cur_token[-1].src_loc;
1261 expand_loc = loc;
1263 else
1265 /* Otherwise, the location of the end of the macro invocation is
1266 the location of the expansion point of that top-level macro
1267 invocation. */
1268 loc = location;
1269 expand_loc = pfile->invocation_location;
1272 return builtin_macro (pfile, node, loc, expand_loc);
1276 /* De-allocate the memory used by BUFF which is an array of instances
1277 of macro_arg. NUM_ARGS is the number of instances of macro_arg
1278 present in BUFF. */
1279 static void
1280 delete_macro_args (_cpp_buff *buff, unsigned num_args)
1282 macro_arg *macro_args;
1283 unsigned i;
1285 if (buff == NULL)
1286 return;
1288 macro_args = (macro_arg *) buff->base;
1290 /* Walk instances of macro_arg to free their expanded tokens as well
1291 as their macro_arg::virt_locs members. */
1292 for (i = 0; i < num_args; ++i)
1294 if (macro_args[i].expanded)
1296 free (macro_args[i].expanded);
1297 macro_args[i].expanded = NULL;
1299 if (macro_args[i].virt_locs)
1301 free (macro_args[i].virt_locs);
1302 macro_args[i].virt_locs = NULL;
1304 if (macro_args[i].expanded_virt_locs)
1306 free (macro_args[i].expanded_virt_locs);
1307 macro_args[i].expanded_virt_locs = NULL;
1310 _cpp_free_buff (buff);
1313 /* Set the INDEXth token of the macro argument ARG. TOKEN is the token
1314 to set, LOCATION is its virtual location. "Virtual" location means
1315 the location that encodes loci across macro expansion. Otherwise
1316 it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the
1317 argument ARG is supposed to contain. Note that ARG must be
1318 tailored so that it has enough room to contain INDEX + 1 numbers of
1319 tokens, at least. */
1320 static void
1321 set_arg_token (macro_arg *arg, const cpp_token *token,
1322 source_location location, size_t index,
1323 enum macro_arg_token_kind kind,
1324 bool track_macro_exp_p)
1326 const cpp_token **token_ptr;
1327 source_location *loc = NULL;
1329 token_ptr =
1330 arg_token_ptr_at (arg, index, kind,
1331 track_macro_exp_p ? &loc : NULL);
1332 *token_ptr = token;
1334 if (loc != NULL)
1336 /* We can't set the location of a stringified argument
1337 token and we can't set any location if we aren't tracking
1338 macro expansion locations. */
1339 gcc_checking_assert (kind != MACRO_ARG_TOKEN_STRINGIFIED
1340 && track_macro_exp_p);
1341 *loc = location;
1345 /* Get the pointer to the location of the argument token of the
1346 function-like macro argument ARG. This function must be called
1347 only when we -ftrack-macro-expansion is on. */
1348 static const source_location *
1349 get_arg_token_location (const macro_arg *arg,
1350 enum macro_arg_token_kind kind)
1352 const source_location *loc = NULL;
1353 const cpp_token **token_ptr =
1354 arg_token_ptr_at (arg, 0, kind, (source_location **) &loc);
1356 if (token_ptr == NULL)
1357 return NULL;
1359 return loc;
1362 /* Return the pointer to the INDEXth token of the macro argument ARG.
1363 KIND specifies the kind of token the macro argument ARG contains.
1364 If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
1365 of the virtual location of the returned token if the
1366 -ftrack-macro-expansion flag is on; otherwise, it's set to the
1367 spelling location of the returned token. */
1368 static const cpp_token **
1369 arg_token_ptr_at (const macro_arg *arg, size_t index,
1370 enum macro_arg_token_kind kind,
1371 source_location **virt_location)
1373 const cpp_token **tokens_ptr = NULL;
1375 switch (kind)
1377 case MACRO_ARG_TOKEN_NORMAL:
1378 tokens_ptr = arg->first;
1379 break;
1380 case MACRO_ARG_TOKEN_STRINGIFIED:
1381 tokens_ptr = (const cpp_token **) &arg->stringified;
1382 break;
1383 case MACRO_ARG_TOKEN_EXPANDED:
1384 tokens_ptr = arg->expanded;
1385 break;
1388 if (tokens_ptr == NULL)
1389 /* This can happen for e.g, an empty token argument to a
1390 funtion-like macro. */
1391 return tokens_ptr;
1393 if (virt_location)
1395 if (kind == MACRO_ARG_TOKEN_NORMAL)
1396 *virt_location = &arg->virt_locs[index];
1397 else if (kind == MACRO_ARG_TOKEN_EXPANDED)
1398 *virt_location = &arg->expanded_virt_locs[index];
1399 else if (kind == MACRO_ARG_TOKEN_STRINGIFIED)
1400 *virt_location =
1401 (source_location *) &tokens_ptr[index]->src_loc;
1403 return &tokens_ptr[index];
1406 /* Initialize an iterator so that it iterates over the tokens of a
1407 function-like macro argument. KIND is the kind of tokens we want
1408 ITER to iterate over. TOKEN_PTR points the first token ITER will
1409 iterate over. */
1410 static void
1411 macro_arg_token_iter_init (macro_arg_token_iter *iter,
1412 bool track_macro_exp_p,
1413 enum macro_arg_token_kind kind,
1414 const macro_arg *arg,
1415 const cpp_token **token_ptr)
1417 iter->track_macro_exp_p = track_macro_exp_p;
1418 iter->kind = kind;
1419 iter->token_ptr = token_ptr;
1420 /* Unconditionally initialize this so that the compiler doesn't warn
1421 about iter->location_ptr being possibly uninitialized later after
1422 this code has been inlined somewhere. */
1423 iter->location_ptr = NULL;
1424 if (track_macro_exp_p)
1425 iter->location_ptr = get_arg_token_location (arg, kind);
1426 #if CHECKING_P
1427 iter->num_forwards = 0;
1428 if (track_macro_exp_p
1429 && token_ptr != NULL
1430 && iter->location_ptr == NULL)
1431 abort ();
1432 #endif
1435 /* Move the iterator one token forward. Note that if IT was
1436 initialized on an argument that has a stringified token, moving it
1437 forward doesn't make sense as a stringified token is essentially one
1438 string. */
1439 static void
1440 macro_arg_token_iter_forward (macro_arg_token_iter *it)
1442 switch (it->kind)
1444 case MACRO_ARG_TOKEN_NORMAL:
1445 case MACRO_ARG_TOKEN_EXPANDED:
1446 it->token_ptr++;
1447 if (it->track_macro_exp_p)
1448 it->location_ptr++;
1449 break;
1450 case MACRO_ARG_TOKEN_STRINGIFIED:
1451 #if CHECKING_P
1452 if (it->num_forwards > 0)
1453 abort ();
1454 #endif
1455 break;
1458 #if CHECKING_P
1459 it->num_forwards++;
1460 #endif
1463 /* Return the token pointed to by the iterator. */
1464 static const cpp_token *
1465 macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
1467 #if CHECKING_P
1468 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1469 && it->num_forwards > 0)
1470 abort ();
1471 #endif
1472 if (it->token_ptr == NULL)
1473 return NULL;
1474 return *it->token_ptr;
1477 /* Return the location of the token pointed to by the iterator.*/
1478 static source_location
1479 macro_arg_token_iter_get_location (const macro_arg_token_iter *it)
1481 #if CHECKING_P
1482 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1483 && it->num_forwards > 0)
1484 abort ();
1485 #endif
1486 if (it->track_macro_exp_p)
1487 return *it->location_ptr;
1488 else
1489 return (*it->token_ptr)->src_loc;
1492 /* Return the index of a token [resulting from macro expansion] inside
1493 the total list of tokens resulting from a given macro
1494 expansion. The index can be different depending on whether if we
1495 want each tokens resulting from function-like macro arguments
1496 expansion to have a different location or not.
1498 E.g, consider this function-like macro:
1500 #define M(x) x - 3
1502 Then consider us "calling" it (and thus expanding it) like:
1504 M(1+4)
1506 It will be expanded into:
1508 1+4-3
1510 Let's consider the case of the token '4'.
1512 Its index can be 2 (it's the third token of the set of tokens
1513 resulting from the expansion) or it can be 0 if we consider that
1514 all tokens resulting from the expansion of the argument "1+2" have
1515 the same index, which is 0. In this later case, the index of token
1516 '-' would then be 1 and the index of token '3' would be 2.
1518 The later case is useful to use less memory e.g, for the case of
1519 the user using the option -ftrack-macro-expansion=1.
1521 ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
1522 are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro
1523 parameter (inside the macro replacement list) that corresponds to
1524 the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
1527 If we refer to the example above, for the '4' argument token,
1528 ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
1529 would be set to the token 'x', in the replacement list "x - 3" of
1530 macro M.
1532 This is a subroutine of replace_args. */
1533 inline static unsigned
1534 expanded_token_index (cpp_reader *pfile, cpp_macro *macro,
1535 const cpp_token *cur_replacement_token,
1536 unsigned absolute_token_index)
1538 if (CPP_OPTION (pfile, track_macro_expansion) > 1)
1539 return absolute_token_index;
1540 return cur_replacement_token - macro->exp.tokens;
1543 /* Replace the parameters in a function-like macro of NODE with the
1544 actual ARGS, and place the result in a newly pushed token context.
1545 Expand each argument before replacing, unless it is operated upon
1546 by the # or ## operators. EXPANSION_POINT_LOC is the location of
1547 the expansion point of the macro. E.g, the location of the
1548 function-like macro invocation. */
1549 static void
1550 replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
1551 macro_arg *args, source_location expansion_point_loc)
1553 unsigned int i, total;
1554 const cpp_token *src, *limit;
1555 const cpp_token **first = NULL;
1556 macro_arg *arg;
1557 _cpp_buff *buff = NULL;
1558 source_location *virt_locs = NULL;
1559 unsigned int exp_count;
1560 const line_map_macro *map = NULL;
1561 int track_macro_exp;
1563 /* First, fully macro-expand arguments, calculating the number of
1564 tokens in the final expansion as we go. The ordering of the if
1565 statements below is subtle; we must handle stringification before
1566 pasting. */
1568 /* EXP_COUNT is the number of tokens in the macro replacement
1569 list. TOTAL is the number of tokens /after/ macro parameters
1570 have been replaced by their arguments. */
1571 exp_count = macro_real_token_count (macro);
1572 total = exp_count;
1573 limit = macro->exp.tokens + exp_count;
1575 for (src = macro->exp.tokens; src < limit; src++)
1576 if (src->type == CPP_MACRO_ARG)
1578 /* Leading and trailing padding tokens. */
1579 total += 2;
1580 /* Account for leading and padding tokens in exp_count too.
1581 This is going to be important later down this function,
1582 when we want to handle the case of (track_macro_exp <
1583 2). */
1584 exp_count += 2;
1586 /* We have an argument. If it is not being stringified or
1587 pasted it is macro-replaced before insertion. */
1588 arg = &args[src->val.macro_arg.arg_no - 1];
1590 if (src->flags & STRINGIFY_ARG)
1592 if (!arg->stringified)
1593 arg->stringified = stringify_arg (pfile, arg);
1595 else if ((src->flags & PASTE_LEFT)
1596 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
1597 total += arg->count - 1;
1598 else
1600 if (!arg->expanded)
1601 expand_arg (pfile, arg);
1602 total += arg->expanded_count - 1;
1606 /* When the compiler is called with the -ftrack-macro-expansion
1607 flag, we need to keep track of the location of each token that
1608 results from macro expansion.
1610 A token resulting from macro expansion is not a new token. It is
1611 simply the same token as the token coming from the macro
1612 definition. The new things that are allocated are the buffer
1613 that holds the tokens resulting from macro expansion and a new
1614 location that records many things like the locus of the expansion
1615 point as well as the original locus inside the definition of the
1616 macro. This location is called a virtual location.
1618 So the buffer BUFF holds a set of cpp_token*, and the buffer
1619 VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
1621 Both of these two buffers are going to be hung off of the macro
1622 context, when the latter is pushed. The memory allocated to
1623 store the tokens and their locations is going to be freed once
1624 the context of macro expansion is popped.
1626 As far as tokens are concerned, the memory overhead of
1627 -ftrack-macro-expansion is proportional to the number of
1628 macros that get expanded multiplied by sizeof (source_location).
1629 The good news is that extra memory gets freed when the macro
1630 context is freed, i.e shortly after the macro got expanded. */
1632 /* Is the -ftrack-macro-expansion flag in effect? */
1633 track_macro_exp = CPP_OPTION (pfile, track_macro_expansion);
1635 /* Now allocate memory space for tokens and locations resulting from
1636 the macro expansion, copy the tokens and replace the arguments.
1637 This memory must be freed when the context of the macro MACRO is
1638 popped. */
1639 buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL);
1641 first = (const cpp_token **) buff->base;
1643 /* Create a macro map to record the locations of the tokens that are
1644 involved in the expansion. Note that the expansion point is set
1645 to the location of the closing parenthesis. Otherwise, the
1646 subsequent map created for the first token that comes after the
1647 macro map might have a wrong line number. That would lead to
1648 tokens with wrong line numbers after the macro expansion. This
1649 adds up to the memory overhead of the -ftrack-macro-expansion
1650 flag; for every macro that is expanded, a "macro map" is
1651 created. */
1652 if (track_macro_exp)
1654 int num_macro_tokens = total;
1655 if (track_macro_exp < 2)
1656 /* Then the number of macro tokens won't take in account the
1657 fact that function-like macro arguments can expand to
1658 multiple tokens. This is to save memory at the expense of
1659 accuracy.
1661 Suppose we have #define SQARE(A) A * A
1663 And then we do SQARE(2+3)
1665 Then the tokens 2, +, 3, will have the same location,
1666 saying they come from the expansion of the argument A. */
1667 num_macro_tokens = exp_count;
1668 map = linemap_enter_macro (pfile->line_table, node,
1669 expansion_point_loc,
1670 num_macro_tokens);
1672 i = 0;
1673 for (src = macro->exp.tokens; src < limit; src++)
1675 unsigned int arg_tokens_count;
1676 macro_arg_token_iter from;
1677 const cpp_token **paste_flag = NULL;
1678 const cpp_token **tmp_token_ptr;
1680 if (src->type != CPP_MACRO_ARG)
1682 /* Allocate a virtual location for token SRC, and add that
1683 token and its virtual location into the buffers BUFF and
1684 VIRT_LOCS. */
1685 unsigned index = expanded_token_index (pfile, macro, src, i);
1686 tokens_buff_add_token (buff, virt_locs, src,
1687 src->src_loc, src->src_loc,
1688 map, index);
1689 i += 1;
1690 continue;
1693 paste_flag = 0;
1694 arg = &args[src->val.macro_arg.arg_no - 1];
1695 /* SRC is a macro parameter that we need to replace with its
1696 corresponding argument. So at some point we'll need to
1697 iterate over the tokens of the macro argument and copy them
1698 into the "place" now holding the correspondig macro
1699 parameter. We are going to use the iterator type
1700 macro_argo_token_iter to handle that iterating. The 'if'
1701 below is to initialize the iterator depending on the type of
1702 tokens the macro argument has. It also does some adjustment
1703 related to padding tokens and some pasting corner cases. */
1704 if (src->flags & STRINGIFY_ARG)
1706 arg_tokens_count = 1;
1707 macro_arg_token_iter_init (&from,
1708 CPP_OPTION (pfile,
1709 track_macro_expansion),
1710 MACRO_ARG_TOKEN_STRINGIFIED,
1711 arg, &arg->stringified);
1713 else if (src->flags & PASTE_LEFT)
1715 arg_tokens_count = arg->count;
1716 macro_arg_token_iter_init (&from,
1717 CPP_OPTION (pfile,
1718 track_macro_expansion),
1719 MACRO_ARG_TOKEN_NORMAL,
1720 arg, arg->first);
1722 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
1724 int num_toks;
1725 arg_tokens_count = arg->count;
1726 macro_arg_token_iter_init (&from,
1727 CPP_OPTION (pfile,
1728 track_macro_expansion),
1729 MACRO_ARG_TOKEN_NORMAL,
1730 arg, arg->first);
1732 num_toks = tokens_buff_count (buff);
1734 if (num_toks != 0)
1736 /* So the current parameter token is pasted to the previous
1737 token in the replacement list. Let's look at what
1738 we have as previous and current arguments. */
1740 /* This is the previous argument's token ... */
1741 tmp_token_ptr = tokens_buff_last_token_ptr (buff);
1743 if ((*tmp_token_ptr)->type == CPP_COMMA
1744 && macro->variadic
1745 && src->val.macro_arg.arg_no == macro->paramc)
1747 /* ... which is a comma; and the current parameter
1748 is the last parameter of a variadic function-like
1749 macro. If the argument to the current last
1750 parameter is NULL, then swallow the comma,
1751 otherwise drop the paste flag. */
1752 if (macro_arg_token_iter_get_token (&from) == NULL)
1753 tokens_buff_remove_last_token (buff);
1754 else
1755 paste_flag = tmp_token_ptr;
1757 /* Remove the paste flag if the RHS is a placemarker. */
1758 else if (arg_tokens_count == 0)
1759 paste_flag = tmp_token_ptr;
1762 else
1764 arg_tokens_count = arg->expanded_count;
1765 macro_arg_token_iter_init (&from,
1766 CPP_OPTION (pfile,
1767 track_macro_expansion),
1768 MACRO_ARG_TOKEN_EXPANDED,
1769 arg, arg->expanded);
1772 /* Padding on the left of an argument (unless RHS of ##). */
1773 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
1774 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
1776 const cpp_token *t = padding_token (pfile, src);
1777 unsigned index = expanded_token_index (pfile, macro, src, i);
1778 /* Allocate a virtual location for the padding token and
1779 append the token and its location to BUFF and
1780 VIRT_LOCS. */
1781 tokens_buff_add_token (buff, virt_locs, t,
1782 t->src_loc, t->src_loc,
1783 map, index);
1786 if (arg_tokens_count)
1788 /* So now we've got the number of tokens that make up the
1789 argument that is going to replace the current parameter
1790 in the macro's replacement list. */
1791 unsigned int j;
1792 for (j = 0; j < arg_tokens_count; ++j)
1794 /* So if track_macro_exp is < 2, the user wants to
1795 save extra memory while tracking macro expansion
1796 locations. So in that case here is what we do:
1798 Suppose we have #define SQARE(A) A * A
1800 And then we do SQARE(2+3)
1802 Then the tokens 2, +, 3, will have the same location,
1803 saying they come from the expansion of the argument
1806 So that means we are going to ignore the COUNT tokens
1807 resulting from the expansion of the current macro
1808 arugment. In other words all the ARG_TOKENS_COUNT tokens
1809 resulting from the expansion of the macro argument will
1810 have the index I. Normally, each of those token should
1811 have index I+J. */
1812 unsigned token_index = i;
1813 unsigned index;
1814 if (track_macro_exp > 1)
1815 token_index += j;
1817 index = expanded_token_index (pfile, macro, src, token_index);
1818 tokens_buff_add_token (buff, virt_locs,
1819 macro_arg_token_iter_get_token (&from),
1820 macro_arg_token_iter_get_location (&from),
1821 src->src_loc, map, index);
1822 macro_arg_token_iter_forward (&from);
1825 /* With a non-empty argument on the LHS of ##, the last
1826 token should be flagged PASTE_LEFT. */
1827 if (src->flags & PASTE_LEFT)
1828 paste_flag =
1829 (const cpp_token **) tokens_buff_last_token_ptr (buff);
1831 else if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99)
1832 && ! macro->syshdr && ! cpp_in_system_header (pfile))
1834 if (CPP_OPTION (pfile, cplusplus))
1835 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
1836 "invoking macro %s argument %d: "
1837 "empty macro arguments are undefined"
1838 " in ISO C++98",
1839 NODE_NAME (node), src->val.macro_arg.arg_no);
1840 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat))
1841 cpp_pedwarning (pfile,
1842 CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
1843 ? CPP_W_C90_C99_COMPAT : CPP_W_PEDANTIC,
1844 "invoking macro %s argument %d: "
1845 "empty macro arguments are undefined"
1846 " in ISO C90",
1847 NODE_NAME (node), src->val.macro_arg.arg_no);
1849 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
1850 && ! CPP_OPTION (pfile, cplusplus)
1851 && ! macro->syshdr && ! cpp_in_system_header (pfile))
1852 cpp_warning (pfile, CPP_W_C90_C99_COMPAT,
1853 "invoking macro %s argument %d: "
1854 "empty macro arguments are undefined"
1855 " in ISO C90",
1856 NODE_NAME (node), src->val.macro_arg.arg_no);
1858 /* Avoid paste on RHS (even case count == 0). */
1859 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
1861 const cpp_token *t = &pfile->avoid_paste;
1862 tokens_buff_add_token (buff, virt_locs,
1863 t, t->src_loc, t->src_loc,
1864 NULL, 0);
1867 /* Add a new paste flag, or remove an unwanted one. */
1868 if (paste_flag)
1870 cpp_token *token = _cpp_temp_token (pfile);
1871 token->type = (*paste_flag)->type;
1872 token->val = (*paste_flag)->val;
1873 if (src->flags & PASTE_LEFT)
1874 token->flags = (*paste_flag)->flags | PASTE_LEFT;
1875 else
1876 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
1877 *paste_flag = token;
1880 i += arg_tokens_count;
1883 if (track_macro_exp)
1884 push_extended_tokens_context (pfile, node, buff, virt_locs, first,
1885 tokens_buff_count (buff));
1886 else
1887 push_ptoken_context (pfile, node, buff, first,
1888 tokens_buff_count (buff));
1890 num_macro_tokens_counter += tokens_buff_count (buff);
1893 /* Return a special padding token, with padding inherited from SOURCE. */
1894 static const cpp_token *
1895 padding_token (cpp_reader *pfile, const cpp_token *source)
1897 cpp_token *result = _cpp_temp_token (pfile);
1899 result->type = CPP_PADDING;
1901 /* Data in GCed data structures cannot be made const so far, so we
1902 need a cast here. */
1903 result->val.source = (cpp_token *) source;
1904 result->flags = 0;
1905 return result;
1908 /* Get a new uninitialized context. Create a new one if we cannot
1909 re-use an old one. */
1910 static cpp_context *
1911 next_context (cpp_reader *pfile)
1913 cpp_context *result = pfile->context->next;
1915 if (result == 0)
1917 result = XNEW (cpp_context);
1918 memset (result, 0, sizeof (cpp_context));
1919 result->prev = pfile->context;
1920 result->next = 0;
1921 pfile->context->next = result;
1924 pfile->context = result;
1925 return result;
1928 /* Push a list of pointers to tokens. */
1929 static void
1930 push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
1931 const cpp_token **first, unsigned int count)
1933 cpp_context *context = next_context (pfile);
1935 context->tokens_kind = TOKENS_KIND_INDIRECT;
1936 context->c.macro = macro;
1937 context->buff = buff;
1938 FIRST (context).ptoken = first;
1939 LAST (context).ptoken = first + count;
1942 /* Push a list of tokens.
1944 A NULL macro means that we should continue the current macro
1945 expansion, in essence. That means that if we are currently in a
1946 macro expansion context, we'll make the new pfile->context refer to
1947 the current macro. */
1948 void
1949 _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
1950 const cpp_token *first, unsigned int count)
1952 cpp_context *context;
1954 if (macro == NULL)
1955 macro = macro_of_context (pfile->context);
1957 context = next_context (pfile);
1958 context->tokens_kind = TOKENS_KIND_DIRECT;
1959 context->c.macro = macro;
1960 context->buff = NULL;
1961 FIRST (context).token = first;
1962 LAST (context).token = first + count;
1965 /* Build a context containing a list of tokens as well as their
1966 virtual locations and push it. TOKENS_BUFF is the buffer that
1967 contains the tokens pointed to by FIRST. If TOKENS_BUFF is
1968 non-NULL, it means that the context owns it, meaning that
1969 _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
1970 contains the virtual locations.
1972 A NULL macro means that we should continue the current macro
1973 expansion, in essence. That means that if we are currently in a
1974 macro expansion context, we'll make the new pfile->context refer to
1975 the current macro. */
1976 static void
1977 push_extended_tokens_context (cpp_reader *pfile,
1978 cpp_hashnode *macro,
1979 _cpp_buff *token_buff,
1980 source_location *virt_locs,
1981 const cpp_token **first,
1982 unsigned int count)
1984 cpp_context *context;
1985 macro_context *m;
1987 if (macro == NULL)
1988 macro = macro_of_context (pfile->context);
1990 context = next_context (pfile);
1991 context->tokens_kind = TOKENS_KIND_EXTENDED;
1992 context->buff = token_buff;
1994 m = XNEW (macro_context);
1995 m->macro_node = macro;
1996 m->virt_locs = virt_locs;
1997 m->cur_virt_loc = virt_locs;
1998 context->c.mc = m;
1999 FIRST (context).ptoken = first;
2000 LAST (context).ptoken = first + count;
2003 /* Push a traditional macro's replacement text. */
2004 void
2005 _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
2006 const uchar *start, size_t len)
2008 cpp_context *context = next_context (pfile);
2010 context->tokens_kind = TOKENS_KIND_DIRECT;
2011 context->c.macro = macro;
2012 context->buff = NULL;
2013 CUR (context) = start;
2014 RLIMIT (context) = start + len;
2015 macro->flags |= NODE_DISABLED;
2018 /* Creates a buffer that holds tokens a.k.a "token buffer", usually
2019 for the purpose of storing them on a cpp_context. If VIRT_LOCS is
2020 non-null (which means that -ftrack-macro-expansion is on),
2021 *VIRT_LOCS is set to a newly allocated buffer that is supposed to
2022 hold the virtual locations of the tokens resulting from macro
2023 expansion. */
2024 static _cpp_buff*
2025 tokens_buff_new (cpp_reader *pfile, size_t len,
2026 source_location **virt_locs)
2028 size_t tokens_size = len * sizeof (cpp_token *);
2029 size_t locs_size = len * sizeof (source_location);
2031 if (virt_locs != NULL)
2032 *virt_locs = XNEWVEC (source_location, locs_size);
2033 return _cpp_get_buff (pfile, tokens_size);
2036 /* Returns the number of tokens contained in a token buffer. The
2037 buffer holds a set of cpp_token*. */
2038 static size_t
2039 tokens_buff_count (_cpp_buff *buff)
2041 return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *);
2044 /* Return a pointer to the last token contained in the token buffer
2045 BUFF. */
2046 static const cpp_token **
2047 tokens_buff_last_token_ptr (_cpp_buff *buff)
2049 return &((const cpp_token **) BUFF_FRONT (buff))[-1];
2052 /* Remove the last token contained in the token buffer TOKENS_BUFF.
2053 If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer
2054 containing the virtual locations of the tokens in TOKENS_BUFF; in
2055 which case the function updates that buffer as well. */
2056 static inline void
2057 tokens_buff_remove_last_token (_cpp_buff *tokens_buff)
2060 if (BUFF_FRONT (tokens_buff) > tokens_buff->base)
2061 BUFF_FRONT (tokens_buff) =
2062 (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1];
2065 /* Insert a token into the token buffer at the position pointed to by
2066 DEST. Note that the buffer is not enlarged so the previous token
2067 that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null,
2068 means -ftrack-macro-expansion is effect; it then points to where to
2069 insert the virtual location of TOKEN. TOKEN is the token to
2070 insert. VIRT_LOC is the virtual location of the token, i.e, the
2071 location possibly encoding its locus across macro expansion. If
2072 TOKEN is an argument of a function-like macro (inside a macro
2073 replacement list), PARM_DEF_LOC is the spelling location of the
2074 macro parameter that TOKEN is replacing, in the replacement list of
2075 the macro. If TOKEN is not an argument of a function-like macro or
2076 if it doesn't come from a macro expansion, then VIRT_LOC can just
2077 be set to the same value as PARM_DEF_LOC. If MAP is non null, it
2078 means TOKEN comes from a macro expansion and MAP is the macro map
2079 associated to the macro. MACRO_TOKEN_INDEX points to the index of
2080 the token in the macro map; it is not considered if MAP is NULL.
2082 Upon successful completion this function returns the a pointer to
2083 the position of the token coming right after the insertion
2084 point. */
2085 static inline const cpp_token **
2086 tokens_buff_put_token_to (const cpp_token **dest,
2087 source_location *virt_loc_dest,
2088 const cpp_token *token,
2089 source_location virt_loc,
2090 source_location parm_def_loc,
2091 const line_map_macro *map,
2092 unsigned int macro_token_index)
2094 source_location macro_loc = virt_loc;
2095 const cpp_token **result;
2097 if (virt_loc_dest)
2099 /* -ftrack-macro-expansion is on. */
2100 if (map)
2101 macro_loc = linemap_add_macro_token (map, macro_token_index,
2102 virt_loc, parm_def_loc);
2103 *virt_loc_dest = macro_loc;
2105 *dest = token;
2106 result = &dest[1];
2108 return result;
2111 /* Adds a token at the end of the tokens contained in BUFFER. Note
2112 that this function doesn't enlarge BUFFER when the number of tokens
2113 reaches BUFFER's size; it aborts in that situation.
2115 TOKEN is the token to append. VIRT_LOC is the virtual location of
2116 the token, i.e, the location possibly encoding its locus across
2117 macro expansion. If TOKEN is an argument of a function-like macro
2118 (inside a macro replacement list), PARM_DEF_LOC is the location of
2119 the macro parameter that TOKEN is replacing. If TOKEN doesn't come
2120 from a macro expansion, then VIRT_LOC can just be set to the same
2121 value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes
2122 from a macro expansion and MAP is the macro map associated to the
2123 macro. MACRO_TOKEN_INDEX points to the index of the token in the
2124 macro map; It is not considered if MAP is NULL. If VIRT_LOCS is
2125 non-null, it means -ftrack-macro-expansion is on; in which case
2126 this function adds the virtual location DEF_LOC to the VIRT_LOCS
2127 array, at the same index as the one of TOKEN in BUFFER. Upon
2128 successful completion this function returns the a pointer to the
2129 position of the token coming right after the insertion point. */
2130 static const cpp_token **
2131 tokens_buff_add_token (_cpp_buff *buffer,
2132 source_location *virt_locs,
2133 const cpp_token *token,
2134 source_location virt_loc,
2135 source_location parm_def_loc,
2136 const line_map_macro *map,
2137 unsigned int macro_token_index)
2139 const cpp_token **result;
2140 source_location *virt_loc_dest = NULL;
2141 unsigned token_index =
2142 (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
2144 /* Abort if we pass the end the buffer. */
2145 if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer))
2146 abort ();
2148 if (virt_locs != NULL)
2149 virt_loc_dest = &virt_locs[token_index];
2151 result =
2152 tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer),
2153 virt_loc_dest, token, virt_loc, parm_def_loc,
2154 map, macro_token_index);
2156 BUFF_FRONT (buffer) = (unsigned char *) result;
2157 return result;
2160 /* Allocate space for the function-like macro argument ARG to store
2161 the tokens resulting from the macro-expansion of the tokens that
2162 make up ARG itself. That space is allocated in ARG->expanded and
2163 needs to be freed using free. */
2164 static void
2165 alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity)
2167 gcc_checking_assert (arg->expanded == NULL
2168 && arg->expanded_virt_locs == NULL);
2170 arg->expanded = XNEWVEC (const cpp_token *, capacity);
2171 if (CPP_OPTION (pfile, track_macro_expansion))
2172 arg->expanded_virt_locs = XNEWVEC (source_location, capacity);
2176 /* If necessary, enlarge ARG->expanded to so that it can contain SIZE
2177 tokens. */
2178 static void
2179 ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg,
2180 size_t size, size_t *expanded_capacity)
2182 if (size <= *expanded_capacity)
2183 return;
2185 size *= 2;
2187 arg->expanded =
2188 XRESIZEVEC (const cpp_token *, arg->expanded, size);
2189 *expanded_capacity = size;
2191 if (CPP_OPTION (pfile, track_macro_expansion))
2193 if (arg->expanded_virt_locs == NULL)
2194 arg->expanded_virt_locs = XNEWVEC (source_location, size);
2195 else
2196 arg->expanded_virt_locs = XRESIZEVEC (source_location,
2197 arg->expanded_virt_locs,
2198 size);
2202 /* Expand an argument ARG before replacing parameters in a
2203 function-like macro. This works by pushing a context with the
2204 argument's tokens, and then expanding that into a temporary buffer
2205 as if it were a normal part of the token stream. collect_args()
2206 has terminated the argument's tokens with a CPP_EOF so that we know
2207 when we have fully expanded the argument. */
2208 static void
2209 expand_arg (cpp_reader *pfile, macro_arg *arg)
2211 size_t capacity;
2212 bool saved_warn_trad;
2213 bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
2215 if (arg->count == 0
2216 || arg->expanded != NULL)
2217 return;
2219 /* Don't warn about funlike macros when pre-expanding. */
2220 saved_warn_trad = CPP_WTRADITIONAL (pfile);
2221 CPP_WTRADITIONAL (pfile) = 0;
2223 /* Loop, reading in the tokens of the argument. */
2224 capacity = 256;
2225 alloc_expanded_arg_mem (pfile, arg, capacity);
2227 if (track_macro_exp_p)
2228 push_extended_tokens_context (pfile, NULL, NULL,
2229 arg->virt_locs,
2230 arg->first,
2231 arg->count + 1);
2232 else
2233 push_ptoken_context (pfile, NULL, NULL,
2234 arg->first, arg->count + 1);
2236 for (;;)
2238 const cpp_token *token;
2239 source_location location;
2241 ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1,
2242 &capacity);
2244 token = cpp_get_token_1 (pfile, &location);
2246 if (token->type == CPP_EOF)
2247 break;
2249 set_arg_token (arg, token, location,
2250 arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED,
2251 CPP_OPTION (pfile, track_macro_expansion));
2252 arg->expanded_count++;
2255 _cpp_pop_context (pfile);
2257 CPP_WTRADITIONAL (pfile) = saved_warn_trad;
2260 /* Returns the macro associated to the current context if we are in
2261 the context a macro expansion, NULL otherwise. */
2262 static cpp_hashnode*
2263 macro_of_context (cpp_context *context)
2265 if (context == NULL)
2266 return NULL;
2268 return (context->tokens_kind == TOKENS_KIND_EXTENDED)
2269 ? context->c.mc->macro_node
2270 : context->c.macro;
2273 /* Return TRUE iff we are expanding a macro or are about to start
2274 expanding one. If we are effectively expanding a macro, the
2275 function macro_of_context returns a pointer to the macro being
2276 expanded. */
2277 static bool
2278 in_macro_expansion_p (cpp_reader *pfile)
2280 if (pfile == NULL)
2281 return false;
2283 return (pfile->about_to_expand_macro_p
2284 || macro_of_context (pfile->context));
2287 /* Pop the current context off the stack, re-enabling the macro if the
2288 context represented a macro's replacement list. Initially the
2289 context structure was not freed so that we can re-use it later, but
2290 now we do free it to reduce peak memory consumption. */
2291 void
2292 _cpp_pop_context (cpp_reader *pfile)
2294 cpp_context *context = pfile->context;
2296 /* We should not be popping the base context. */
2297 if (context == &pfile->base_context)
2298 abort ();
2300 if (context->c.macro)
2302 cpp_hashnode *macro;
2303 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
2305 macro_context *mc = context->c.mc;
2306 macro = mc->macro_node;
2307 /* If context->buff is set, it means the life time of tokens
2308 is bound to the life time of this context; so we must
2309 free the tokens; that means we must free the virtual
2310 locations of these tokens too. */
2311 if (context->buff && mc->virt_locs)
2313 free (mc->virt_locs);
2314 mc->virt_locs = NULL;
2316 free (mc);
2317 context->c.mc = NULL;
2319 else
2320 macro = context->c.macro;
2322 /* Beware that MACRO can be NULL in cases like when we are
2323 called from expand_arg. In those cases, a dummy context with
2324 tokens is pushed just for the purpose of walking them using
2325 cpp_get_token_1. In that case, no 'macro' field is set into
2326 the dummy context. */
2327 if (macro != NULL
2328 /* Several contiguous macro expansion contexts can be
2329 associated to the same macro; that means it's the same
2330 macro expansion that spans across all these (sub)
2331 contexts. So we should re-enable an expansion-disabled
2332 macro only when we are sure we are really out of that
2333 macro expansion. */
2334 && macro_of_context (context->prev) != macro)
2335 macro->flags &= ~NODE_DISABLED;
2337 if (macro == pfile->top_most_macro_node && context->prev == NULL)
2338 /* We are popping the context of the top-most macro node. */
2339 pfile->top_most_macro_node = NULL;
2342 if (context->buff)
2344 /* Decrease memory peak consumption by freeing the memory used
2345 by the context. */
2346 _cpp_free_buff (context->buff);
2349 pfile->context = context->prev;
2350 /* decrease peak memory consumption by feeing the context. */
2351 pfile->context->next = NULL;
2352 free (context);
2355 /* Return TRUE if we reached the end of the set of tokens stored in
2356 CONTEXT, FALSE otherwise. */
2357 static inline bool
2358 reached_end_of_context (cpp_context *context)
2360 if (context->tokens_kind == TOKENS_KIND_DIRECT)
2361 return FIRST (context).token == LAST (context).token;
2362 else if (context->tokens_kind == TOKENS_KIND_INDIRECT
2363 || context->tokens_kind == TOKENS_KIND_EXTENDED)
2364 return FIRST (context).ptoken == LAST (context).ptoken;
2365 else
2366 abort ();
2369 /* Consume the next token contained in the current context of PFILE,
2370 and return it in *TOKEN. It's "full location" is returned in
2371 *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
2372 means the location encoding the locus of the token across macro
2373 expansion; otherwise it's just is the "normal" location of the
2374 token which (*TOKEN)->src_loc. */
2375 static inline void
2376 consume_next_token_from_context (cpp_reader *pfile,
2377 const cpp_token ** token,
2378 source_location *location)
2380 cpp_context *c = pfile->context;
2382 if ((c)->tokens_kind == TOKENS_KIND_DIRECT)
2384 *token = FIRST (c).token;
2385 *location = (*token)->src_loc;
2386 FIRST (c).token++;
2388 else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT)
2390 *token = *FIRST (c).ptoken;
2391 *location = (*token)->src_loc;
2392 FIRST (c).ptoken++;
2394 else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED)
2396 macro_context *m = c->c.mc;
2397 *token = *FIRST (c).ptoken;
2398 if (m->virt_locs)
2400 *location = *m->cur_virt_loc;
2401 m->cur_virt_loc++;
2403 else
2404 *location = (*token)->src_loc;
2405 FIRST (c).ptoken++;
2407 else
2408 abort ();
2411 /* In the traditional mode of the preprocessor, if we are currently in
2412 a directive, the location of a token must be the location of the
2413 start of the directive line. This function returns the proper
2414 location if we are in the traditional mode, and just returns
2415 LOCATION otherwise. */
2417 static inline source_location
2418 maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, source_location location)
2420 if (CPP_OPTION (pfile, traditional))
2422 if (pfile->state.in_directive)
2423 return pfile->directive_line;
2425 return location;
2428 /* Routine to get a token as well as its location.
2430 Macro expansions and directives are transparently handled,
2431 including entering included files. Thus tokens are post-macro
2432 expansion, and after any intervening directives. External callers
2433 see CPP_EOF only at EOF. Internal callers also see it when meeting
2434 a directive inside a macro call, when at the end of a directive and
2435 state.in_directive is still 1, and at the end of argument
2436 pre-expansion.
2438 LOC is an out parameter; *LOC is set to the location "as expected
2439 by the user". Please read the comment of
2440 cpp_get_token_with_location to learn more about the meaning of this
2441 location. */
2442 static const cpp_token*
2443 cpp_get_token_1 (cpp_reader *pfile, source_location *location)
2445 const cpp_token *result;
2446 /* This token is a virtual token that either encodes a location
2447 related to macro expansion or a spelling location. */
2448 source_location virt_loc = 0;
2449 /* pfile->about_to_expand_macro_p can be overriden by indirect calls
2450 to functions that push macro contexts. So let's save it so that
2451 we can restore it when we are about to leave this routine. */
2452 bool saved_about_to_expand_macro = pfile->about_to_expand_macro_p;
2454 for (;;)
2456 cpp_hashnode *node;
2457 cpp_context *context = pfile->context;
2459 /* Context->prev == 0 <=> base context. */
2460 if (!context->prev)
2462 result = _cpp_lex_token (pfile);
2463 virt_loc = result->src_loc;
2465 else if (!reached_end_of_context (context))
2467 consume_next_token_from_context (pfile, &result,
2468 &virt_loc);
2469 if (result->flags & PASTE_LEFT)
2471 paste_all_tokens (pfile, result);
2472 if (pfile->state.in_directive)
2473 continue;
2474 result = padding_token (pfile, result);
2475 goto out;
2478 else
2480 if (pfile->context->c.macro)
2481 ++num_expanded_macros_counter;
2482 _cpp_pop_context (pfile);
2483 if (pfile->state.in_directive)
2484 continue;
2485 result = &pfile->avoid_paste;
2486 goto out;
2489 if (pfile->state.in_directive && result->type == CPP_COMMENT)
2490 continue;
2492 if (result->type != CPP_NAME)
2493 break;
2495 node = result->val.node.node;
2497 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
2498 break;
2500 if (!(node->flags & NODE_DISABLED))
2502 int ret = 0;
2503 /* If not in a macro context, and we're going to start an
2504 expansion, record the location and the top level macro
2505 about to be expanded. */
2506 if (!in_macro_expansion_p (pfile))
2508 pfile->invocation_location = result->src_loc;
2509 pfile->top_most_macro_node = node;
2511 if (pfile->state.prevent_expansion)
2512 break;
2514 /* Conditional macros require that a predicate be evaluated
2515 first. */
2516 if ((node->flags & NODE_CONDITIONAL) != 0)
2518 if (pfile->cb.macro_to_expand)
2520 bool whitespace_after;
2521 const cpp_token *peek_tok = cpp_peek_token (pfile, 0);
2523 whitespace_after = (peek_tok->type == CPP_PADDING
2524 || (peek_tok->flags & PREV_WHITE));
2525 node = pfile->cb.macro_to_expand (pfile, result);
2526 if (node)
2527 ret = enter_macro_context (pfile, node, result,
2528 virt_loc);
2529 else if (whitespace_after)
2531 /* If macro_to_expand hook returned NULL and it
2532 ate some tokens, see if we don't need to add
2533 a padding token in between this and the
2534 next token. */
2535 peek_tok = cpp_peek_token (pfile, 0);
2536 if (peek_tok->type != CPP_PADDING
2537 && (peek_tok->flags & PREV_WHITE) == 0)
2538 _cpp_push_token_context (pfile, NULL,
2539 padding_token (pfile,
2540 peek_tok), 1);
2544 else
2545 ret = enter_macro_context (pfile, node, result,
2546 virt_loc);
2547 if (ret)
2549 if (pfile->state.in_directive || ret == 2)
2550 continue;
2551 result = padding_token (pfile, result);
2552 goto out;
2555 else
2557 /* Flag this token as always unexpandable. FIXME: move this
2558 to collect_args()?. */
2559 cpp_token *t = _cpp_temp_token (pfile);
2560 t->type = result->type;
2561 t->flags = result->flags | NO_EXPAND;
2562 t->val = result->val;
2563 result = t;
2566 break;
2569 out:
2570 if (location != NULL)
2572 if (virt_loc == 0)
2573 virt_loc = result->src_loc;
2574 *location = virt_loc;
2576 if (!CPP_OPTION (pfile, track_macro_expansion)
2577 && macro_of_context (pfile->context) != NULL)
2578 /* We are in a macro expansion context, are not tracking
2579 virtual location, but were asked to report the location
2580 of the expansion point of the macro being expanded. */
2581 *location = pfile->invocation_location;
2583 *location = maybe_adjust_loc_for_trad_cpp (pfile, *location);
2586 pfile->about_to_expand_macro_p = saved_about_to_expand_macro;
2587 return result;
2590 /* External routine to get a token. Also used nearly everywhere
2591 internally, except for places where we know we can safely call
2592 _cpp_lex_token directly, such as lexing a directive name.
2594 Macro expansions and directives are transparently handled,
2595 including entering included files. Thus tokens are post-macro
2596 expansion, and after any intervening directives. External callers
2597 see CPP_EOF only at EOF. Internal callers also see it when meeting
2598 a directive inside a macro call, when at the end of a directive and
2599 state.in_directive is still 1, and at the end of argument
2600 pre-expansion. */
2601 const cpp_token *
2602 cpp_get_token (cpp_reader *pfile)
2604 return cpp_get_token_1 (pfile, NULL);
2607 /* Like cpp_get_token, but also returns a virtual token location
2608 separate from the spelling location carried by the returned token.
2610 LOC is an out parameter; *LOC is set to the location "as expected
2611 by the user". This matters when a token results from macro
2612 expansion; in that case the token's spelling location indicates the
2613 locus of the token in the definition of the macro but *LOC
2614 virtually encodes all the other meaningful locuses associated to
2615 the token.
2617 What? virtual location? Yes, virtual location.
2619 If the token results from macro expansion and if macro expansion
2620 location tracking is enabled its virtual location encodes (at the
2621 same time):
2623 - the spelling location of the token
2625 - the locus of the macro expansion point
2627 - the locus of the point where the token got instantiated as part
2628 of the macro expansion process.
2630 You have to use the linemap API to get the locus you are interested
2631 in from a given virtual location.
2633 Note however that virtual locations are not necessarily ordered for
2634 relations '<' and '>'. One must use the function
2635 linemap_location_before_p instead of using the relational operator
2636 '<'.
2638 If macro expansion tracking is off and if the token results from
2639 macro expansion the virtual location is the expansion point of the
2640 macro that got expanded.
2642 When the token doesn't result from macro expansion, the virtual
2643 location is just the same thing as its spelling location. */
2645 const cpp_token *
2646 cpp_get_token_with_location (cpp_reader *pfile, source_location *loc)
2648 return cpp_get_token_1 (pfile, loc);
2651 /* Returns true if we're expanding an object-like macro that was
2652 defined in a system header. Just checks the macro at the top of
2653 the stack. Used for diagnostic suppression. */
2655 cpp_sys_macro_p (cpp_reader *pfile)
2657 cpp_hashnode *node = NULL;
2659 if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2660 node = pfile->context->c.mc->macro_node;
2661 else
2662 node = pfile->context->c.macro;
2664 return node && node->value.macro && node->value.macro->syshdr;
2667 /* Read each token in, until end of the current file. Directives are
2668 transparently processed. */
2669 void
2670 cpp_scan_nooutput (cpp_reader *pfile)
2672 /* Request a CPP_EOF token at the end of this file, rather than
2673 transparently continuing with the including file. */
2674 pfile->buffer->return_at_eof = true;
2676 pfile->state.discarding_output++;
2677 pfile->state.prevent_expansion++;
2679 if (CPP_OPTION (pfile, traditional))
2680 while (_cpp_read_logical_line_trad (pfile))
2682 else
2683 while (cpp_get_token (pfile)->type != CPP_EOF)
2686 pfile->state.discarding_output--;
2687 pfile->state.prevent_expansion--;
2690 /* Step back one or more tokens obtained from the lexer. */
2691 void
2692 _cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count)
2694 pfile->lookaheads += count;
2695 while (count--)
2697 pfile->cur_token--;
2698 if (pfile->cur_token == pfile->cur_run->base
2699 /* Possible with -fpreprocessed and no leading #line. */
2700 && pfile->cur_run->prev != NULL)
2702 pfile->cur_run = pfile->cur_run->prev;
2703 pfile->cur_token = pfile->cur_run->limit;
2708 /* Step back one (or more) tokens. Can only step back more than 1 if
2709 they are from the lexer, and not from macro expansion. */
2710 void
2711 _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
2713 if (pfile->context->prev == NULL)
2714 _cpp_backup_tokens_direct (pfile, count);
2715 else
2717 if (count != 1)
2718 abort ();
2719 if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT)
2720 FIRST (pfile->context).token--;
2721 else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT)
2722 FIRST (pfile->context).ptoken--;
2723 else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2725 FIRST (pfile->context).ptoken--;
2726 if (pfile->context->c.macro)
2728 macro_context *m = pfile->context->c.mc;
2729 m->cur_virt_loc--;
2730 gcc_checking_assert (m->cur_virt_loc >= m->virt_locs);
2732 else
2733 abort ();
2735 else
2736 abort ();
2740 /* #define directive parsing and handling. */
2742 /* Returns nonzero if a macro redefinition warning is required. */
2743 static bool
2744 warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
2745 const cpp_macro *macro2)
2747 const cpp_macro *macro1;
2748 unsigned int i;
2750 /* Some redefinitions need to be warned about regardless. */
2751 if (node->flags & NODE_WARN)
2752 return true;
2754 /* Suppress warnings for builtins that lack the NODE_WARN flag,
2755 unless Wbuiltin-macro-redefined. */
2756 if (node->flags & NODE_BUILTIN
2757 && (!pfile->cb.user_builtin_macro
2758 || !pfile->cb.user_builtin_macro (pfile, node)))
2759 return CPP_OPTION (pfile, warn_builtin_macro_redefined);
2761 /* Redefinitions of conditional (context-sensitive) macros, on
2762 the other hand, must be allowed silently. */
2763 if (node->flags & NODE_CONDITIONAL)
2764 return false;
2766 /* Redefinition of a macro is allowed if and only if the old and new
2767 definitions are the same. (6.10.3 paragraph 2). */
2768 macro1 = node->value.macro;
2770 /* Don't check count here as it can be different in valid
2771 traditional redefinitions with just whitespace differences. */
2772 if (macro1->paramc != macro2->paramc
2773 || macro1->fun_like != macro2->fun_like
2774 || macro1->variadic != macro2->variadic)
2775 return true;
2777 /* Check parameter spellings. */
2778 for (i = 0; i < macro1->paramc; i++)
2779 if (macro1->params[i] != macro2->params[i])
2780 return true;
2782 /* Check the replacement text or tokens. */
2783 if (CPP_OPTION (pfile, traditional))
2784 return _cpp_expansions_different_trad (macro1, macro2);
2786 if (macro1->count != macro2->count)
2787 return true;
2789 for (i = 0; i < macro1->count; i++)
2790 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
2791 return true;
2793 return false;
2796 /* Free the definition of hashnode H. */
2797 void
2798 _cpp_free_definition (cpp_hashnode *h)
2800 /* Macros and assertions no longer have anything to free. */
2801 h->type = NT_VOID;
2802 /* Clear builtin flag in case of redefinition. */
2803 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED);
2806 /* Save parameter NODE (spelling SPELLING) to the parameter list of
2807 macro MACRO. Returns zero on success, nonzero if the parameter is
2808 a duplicate. */
2809 bool
2810 _cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node,
2811 cpp_hashnode *spelling)
2813 unsigned int len;
2814 /* Constraint 6.10.3.6 - duplicate parameter names. */
2815 if (node->flags & NODE_MACRO_ARG)
2817 cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
2818 NODE_NAME (node));
2819 return true;
2822 if (BUFF_ROOM (pfile->a_buff)
2823 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
2824 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
2826 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = spelling;
2827 node->flags |= NODE_MACRO_ARG;
2828 len = macro->paramc * sizeof (struct macro_arg_saved_data);
2829 if (len > pfile->macro_buffer_len)
2831 pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer,
2832 len);
2833 pfile->macro_buffer_len = len;
2835 struct macro_arg_saved_data save;
2836 save.value = node->value;
2837 save.canonical_node = node;
2838 ((struct macro_arg_saved_data *) pfile->macro_buffer)[macro->paramc - 1]
2839 = save;
2841 node->value.arg_index = macro->paramc;
2842 return false;
2845 /* Check the syntax of the parameters in a MACRO definition. Returns
2846 false if an error occurs. */
2847 static bool
2848 parse_params (cpp_reader *pfile, cpp_macro *macro)
2850 unsigned int prev_ident = 0;
2852 for (;;)
2854 const cpp_token *token = _cpp_lex_token (pfile);
2856 switch (token->type)
2858 default:
2859 /* Allow/ignore comments in parameter lists if we are
2860 preserving comments in macro expansions. */
2861 if (token->type == CPP_COMMENT
2862 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
2863 continue;
2865 cpp_error (pfile, CPP_DL_ERROR,
2866 "\"%s\" may not appear in macro parameter list",
2867 cpp_token_as_text (pfile, token));
2868 return false;
2870 case CPP_NAME:
2871 if (prev_ident)
2873 cpp_error (pfile, CPP_DL_ERROR,
2874 "macro parameters must be comma-separated");
2875 return false;
2877 prev_ident = 1;
2879 if (_cpp_save_parameter (pfile, macro, token->val.node.node,
2880 token->val.node.spelling))
2881 return false;
2882 continue;
2884 case CPP_CLOSE_PAREN:
2885 if (prev_ident || macro->paramc == 0)
2886 return true;
2888 /* Fall through to pick up the error. */
2889 /* FALLTHRU */
2890 case CPP_COMMA:
2891 if (!prev_ident)
2893 cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
2894 return false;
2896 prev_ident = 0;
2897 continue;
2899 case CPP_ELLIPSIS:
2900 macro->variadic = 1;
2901 if (!prev_ident)
2903 _cpp_save_parameter (pfile, macro,
2904 pfile->spec_nodes.n__VA_ARGS__,
2905 pfile->spec_nodes.n__VA_ARGS__);
2906 pfile->state.va_args_ok = 1;
2907 if (! CPP_OPTION (pfile, c99)
2908 && CPP_OPTION (pfile, cpp_pedantic)
2909 && CPP_OPTION (pfile, warn_variadic_macros))
2911 if (CPP_OPTION (pfile, cplusplus))
2912 cpp_pedwarning
2913 (pfile, CPP_W_VARIADIC_MACROS,
2914 "anonymous variadic macros were introduced in C++11");
2915 else
2916 cpp_pedwarning
2917 (pfile, CPP_W_VARIADIC_MACROS,
2918 "anonymous variadic macros were introduced in C99");
2920 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
2921 && ! CPP_OPTION (pfile, cplusplus))
2922 cpp_error (pfile, CPP_DL_WARNING,
2923 "anonymous variadic macros were introduced in C99");
2925 else if (CPP_OPTION (pfile, cpp_pedantic)
2926 && CPP_OPTION (pfile, warn_variadic_macros))
2928 if (CPP_OPTION (pfile, cplusplus))
2929 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
2930 "ISO C++ does not permit named variadic macros");
2931 else
2932 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
2933 "ISO C does not permit named variadic macros");
2936 /* We're at the end, and just expect a closing parenthesis. */
2937 token = _cpp_lex_token (pfile);
2938 if (token->type == CPP_CLOSE_PAREN)
2939 return true;
2940 /* Fall through. */
2942 case CPP_EOF:
2943 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
2944 return false;
2949 /* Allocate room for a token from a macro's replacement list. */
2950 static cpp_token *
2951 alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
2953 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
2954 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
2956 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
2959 /* Lex a token from the expansion of MACRO, but mark parameters as we
2960 find them and warn of traditional stringification. */
2961 static cpp_token *
2962 lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
2964 cpp_token *token, *saved_cur_token;
2966 saved_cur_token = pfile->cur_token;
2967 pfile->cur_token = alloc_expansion_token (pfile, macro);
2968 token = _cpp_lex_direct (pfile);
2969 pfile->cur_token = saved_cur_token;
2971 /* Is this a parameter? */
2972 if (token->type == CPP_NAME
2973 && (token->val.node.node->flags & NODE_MACRO_ARG) != 0)
2975 cpp_hashnode *spelling = token->val.node.spelling;
2976 token->type = CPP_MACRO_ARG;
2977 token->val.macro_arg.arg_no = token->val.node.node->value.arg_index;
2978 token->val.macro_arg.spelling = spelling;
2980 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
2981 && (token->type == CPP_STRING || token->type == CPP_CHAR))
2982 check_trad_stringification (pfile, macro, &token->val.str);
2984 return token;
2987 static bool
2988 create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
2990 cpp_token *token;
2991 const cpp_token *ctoken;
2992 bool following_paste_op = false;
2993 const char *paste_op_error_msg =
2994 N_("'##' cannot appear at either end of a macro expansion");
2995 unsigned int num_extra_tokens = 0;
2997 /* Get the first token of the expansion (or the '(' of a
2998 function-like macro). */
2999 ctoken = _cpp_lex_token (pfile);
3001 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
3003 bool ok = parse_params (pfile, macro);
3004 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
3005 if (!ok)
3006 return false;
3008 /* Success. Commit or allocate the parameter array. */
3009 if (pfile->hash_table->alloc_subobject)
3011 cpp_hashnode **params =
3012 (cpp_hashnode **) pfile->hash_table->alloc_subobject
3013 (sizeof (cpp_hashnode *) * macro->paramc);
3014 memcpy (params, macro->params,
3015 sizeof (cpp_hashnode *) * macro->paramc);
3016 macro->params = params;
3018 else
3019 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
3020 macro->fun_like = 1;
3022 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
3024 /* While ISO C99 requires whitespace before replacement text
3025 in a macro definition, ISO C90 with TC1 allows characters
3026 from the basic source character set there. */
3027 if (CPP_OPTION (pfile, c99))
3029 if (CPP_OPTION (pfile, cplusplus))
3030 cpp_error (pfile, CPP_DL_PEDWARN,
3031 "ISO C++11 requires whitespace after the macro name");
3032 else
3033 cpp_error (pfile, CPP_DL_PEDWARN,
3034 "ISO C99 requires whitespace after the macro name");
3036 else
3038 int warntype = CPP_DL_WARNING;
3039 switch (ctoken->type)
3041 case CPP_ATSIGN:
3042 case CPP_AT_NAME:
3043 case CPP_OBJC_STRING:
3044 /* '@' is not in basic character set. */
3045 warntype = CPP_DL_PEDWARN;
3046 break;
3047 case CPP_OTHER:
3048 /* Basic character set sans letters, digits and _. */
3049 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
3050 ctoken->val.str.text[0]) == NULL)
3051 warntype = CPP_DL_PEDWARN;
3052 break;
3053 default:
3054 /* All other tokens start with a character from basic
3055 character set. */
3056 break;
3058 cpp_error (pfile, warntype,
3059 "missing whitespace after the macro name");
3063 if (macro->fun_like)
3064 token = lex_expansion_token (pfile, macro);
3065 else
3067 token = alloc_expansion_token (pfile, macro);
3068 *token = *ctoken;
3071 for (;;)
3073 /* Check the stringifying # constraint 6.10.3.2.1 of
3074 function-like macros when lexing the subsequent token. */
3075 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
3077 if (token->type == CPP_MACRO_ARG)
3079 if (token->flags & PREV_WHITE)
3080 token->flags |= SP_PREV_WHITE;
3081 if (token[-1].flags & DIGRAPH)
3082 token->flags |= SP_DIGRAPH;
3083 token->flags &= ~PREV_WHITE;
3084 token->flags |= STRINGIFY_ARG;
3085 token->flags |= token[-1].flags & PREV_WHITE;
3086 token[-1] = token[0];
3087 macro->count--;
3089 /* Let assembler get away with murder. */
3090 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
3092 cpp_error (pfile, CPP_DL_ERROR,
3093 "'#' is not followed by a macro parameter");
3094 return false;
3098 if (token->type == CPP_EOF)
3100 /* Paste operator constraint 6.10.3.3.1:
3101 Token-paste ##, can appear in both object-like and
3102 function-like macros, but not at the end. */
3103 if (following_paste_op)
3105 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
3106 return false;
3108 break;
3111 /* Paste operator constraint 6.10.3.3.1. */
3112 if (token->type == CPP_PASTE)
3114 /* Token-paste ##, can appear in both object-like and
3115 function-like macros, but not at the beginning. */
3116 if (macro->count == 1)
3118 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
3119 return false;
3122 if (token[-1].flags & PASTE_LEFT)
3124 macro->extra_tokens = 1;
3125 num_extra_tokens++;
3126 token->val.token_no = macro->count - 1;
3128 else
3130 --macro->count;
3131 token[-1].flags |= PASTE_LEFT;
3132 if (token->flags & DIGRAPH)
3133 token[-1].flags |= SP_DIGRAPH;
3134 if (token->flags & PREV_WHITE)
3135 token[-1].flags |= SP_PREV_WHITE;
3139 following_paste_op = (token->type == CPP_PASTE);
3140 token = lex_expansion_token (pfile, macro);
3143 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
3144 macro->traditional = 0;
3146 /* Don't count the CPP_EOF. */
3147 macro->count--;
3149 /* Clear whitespace on first token for warn_of_redefinition(). */
3150 if (macro->count)
3151 macro->exp.tokens[0].flags &= ~PREV_WHITE;
3153 /* Commit or allocate the memory. */
3154 if (pfile->hash_table->alloc_subobject)
3156 cpp_token *tokns =
3157 (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token)
3158 * macro->count);
3159 if (num_extra_tokens)
3161 /* Place second and subsequent ## or %:%: tokens in
3162 sequences of consecutive such tokens at the end of the
3163 list to preserve information about where they appear, how
3164 they are spelt and whether they are preceded by
3165 whitespace without otherwise interfering with macro
3166 expansion. */
3167 cpp_token *normal_dest = tokns;
3168 cpp_token *extra_dest = tokns + macro->count - num_extra_tokens;
3169 unsigned int i;
3170 for (i = 0; i < macro->count; i++)
3172 if (macro->exp.tokens[i].type == CPP_PASTE)
3173 *extra_dest++ = macro->exp.tokens[i];
3174 else
3175 *normal_dest++ = macro->exp.tokens[i];
3178 else
3179 memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
3180 macro->exp.tokens = tokns;
3182 else
3183 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
3185 return true;
3188 /* Parse a macro and save its expansion. Returns nonzero on success. */
3189 bool
3190 _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
3192 cpp_macro *macro;
3193 unsigned int i;
3194 bool ok;
3196 if (pfile->hash_table->alloc_subobject)
3197 macro = (cpp_macro *) pfile->hash_table->alloc_subobject
3198 (sizeof (cpp_macro));
3199 else
3200 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
3201 macro->line = pfile->directive_line;
3202 macro->params = 0;
3203 macro->paramc = 0;
3204 macro->variadic = 0;
3205 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
3206 macro->count = 0;
3207 macro->fun_like = 0;
3208 macro->extra_tokens = 0;
3209 /* To suppress some diagnostics. */
3210 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
3212 if (CPP_OPTION (pfile, traditional))
3213 ok = _cpp_create_trad_definition (pfile, macro);
3214 else
3216 ok = create_iso_definition (pfile, macro);
3218 /* We set the type for SEEN_EOL() in directives.c.
3220 Longer term we should lex the whole line before coming here,
3221 and just copy the expansion. */
3223 /* Stop the lexer accepting __VA_ARGS__. */
3224 pfile->state.va_args_ok = 0;
3227 /* Clear the fast argument lookup indices. */
3228 for (i = macro->paramc; i-- > 0; )
3230 struct macro_arg_saved_data *save =
3231 &((struct macro_arg_saved_data *) pfile->macro_buffer)[i];
3232 struct cpp_hashnode *node = save->canonical_node;
3233 node->flags &= ~ NODE_MACRO_ARG;
3234 node->value = save->value;
3237 if (!ok)
3238 return ok;
3240 if (node->type == NT_MACRO)
3242 if (CPP_OPTION (pfile, warn_unused_macros))
3243 _cpp_warn_if_unused_macro (pfile, node, NULL);
3245 if (warn_of_redefinition (pfile, node, macro))
3247 const int reason = ((node->flags & NODE_BUILTIN)
3248 && !(node->flags & NODE_WARN))
3249 ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
3251 bool warned =
3252 cpp_pedwarning_with_line (pfile, reason,
3253 pfile->directive_line, 0,
3254 "\"%s\" redefined", NODE_NAME (node));
3256 if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
3257 cpp_error_with_line (pfile, CPP_DL_NOTE,
3258 node->value.macro->line, 0,
3259 "this is the location of the previous definition");
3263 if (node->type != NT_VOID)
3264 _cpp_free_definition (node);
3266 /* Enter definition in hash table. */
3267 node->type = NT_MACRO;
3268 node->value.macro = macro;
3269 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))
3270 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS")
3271 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
3272 in the C standard, as something that one must use in C++.
3273 However DR#593 and C++11 indicate that they play no role in C++.
3274 We special-case them anyway. */
3275 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS")
3276 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS"))
3277 node->flags |= NODE_WARN;
3279 /* If user defines one of the conditional macros, remove the
3280 conditional flag */
3281 node->flags &= ~NODE_CONDITIONAL;
3283 return ok;
3286 /* Warn if a token in STRING matches one of a function-like MACRO's
3287 parameters. */
3288 static void
3289 check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
3290 const cpp_string *string)
3292 unsigned int i, len;
3293 const uchar *p, *q, *limit;
3295 /* Loop over the string. */
3296 limit = string->text + string->len - 1;
3297 for (p = string->text + 1; p < limit; p = q)
3299 /* Find the start of an identifier. */
3300 while (p < limit && !is_idstart (*p))
3301 p++;
3303 /* Find the end of the identifier. */
3304 q = p;
3305 while (q < limit && is_idchar (*q))
3306 q++;
3308 len = q - p;
3310 /* Loop over the function macro arguments to see if the
3311 identifier inside the string matches one of them. */
3312 for (i = 0; i < macro->paramc; i++)
3314 const cpp_hashnode *node = macro->params[i];
3316 if (NODE_LEN (node) == len
3317 && !memcmp (p, NODE_NAME (node), len))
3319 cpp_error (pfile, CPP_DL_WARNING,
3320 "macro argument \"%s\" would be stringified in traditional C",
3321 NODE_NAME (node));
3322 break;
3328 /* Returns true of NODE is a function-like macro. */
3329 bool
3330 cpp_fun_like_macro_p (cpp_hashnode *node)
3332 return (node->type == NT_MACRO
3333 && (node->flags & (NODE_BUILTIN | NODE_MACRO_ARG)) == 0
3334 && node->value.macro->fun_like);
3337 /* Returns the name, arguments and expansion of a macro, in a format
3338 suitable to be read back in again, and therefore also for DWARF 2
3339 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
3340 Caller is expected to generate the "#define" bit if needed. The
3341 returned text is temporary, and automatically freed later. */
3342 const unsigned char *
3343 cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
3345 unsigned int i, len;
3346 const cpp_macro *macro;
3347 unsigned char *buffer;
3349 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
3351 if (node->type != NT_MACRO
3352 || !pfile->cb.user_builtin_macro
3353 || !pfile->cb.user_builtin_macro (pfile, node))
3355 cpp_error (pfile, CPP_DL_ICE,
3356 "invalid hash type %d in cpp_macro_definition",
3357 node->type);
3358 return 0;
3362 macro = node->value.macro;
3363 /* Calculate length. */
3364 len = NODE_LEN (node) * 10 + 2; /* ' ' and NUL. */
3365 if (macro->fun_like)
3367 len += 4; /* "()" plus possible final ".." of named
3368 varargs (we have + 1 below). */
3369 for (i = 0; i < macro->paramc; i++)
3370 len += NODE_LEN (macro->params[i]) + 1; /* "," */
3373 /* This should match below where we fill in the buffer. */
3374 if (CPP_OPTION (pfile, traditional))
3375 len += _cpp_replacement_text_len (macro);
3376 else
3378 unsigned int count = macro_real_token_count (macro);
3379 for (i = 0; i < count; i++)
3381 cpp_token *token = &macro->exp.tokens[i];
3383 if (token->type == CPP_MACRO_ARG)
3384 len += NODE_LEN (token->val.macro_arg.spelling);
3385 else
3386 len += cpp_token_len (token);
3388 if (token->flags & STRINGIFY_ARG)
3389 len++; /* "#" */
3390 if (token->flags & PASTE_LEFT)
3391 len += 3; /* " ##" */
3392 if (token->flags & PREV_WHITE)
3393 len++; /* " " */
3397 if (len > pfile->macro_buffer_len)
3399 pfile->macro_buffer = XRESIZEVEC (unsigned char,
3400 pfile->macro_buffer, len);
3401 pfile->macro_buffer_len = len;
3404 /* Fill in the buffer. Start with the macro name. */
3405 buffer = pfile->macro_buffer;
3406 buffer = _cpp_spell_ident_ucns (buffer, node);
3408 /* Parameter names. */
3409 if (macro->fun_like)
3411 *buffer++ = '(';
3412 for (i = 0; i < macro->paramc; i++)
3414 cpp_hashnode *param = macro->params[i];
3416 if (param != pfile->spec_nodes.n__VA_ARGS__)
3418 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
3419 buffer += NODE_LEN (param);
3422 if (i + 1 < macro->paramc)
3423 /* Don't emit a space after the comma here; we're trying
3424 to emit a Dwarf-friendly definition, and the Dwarf spec
3425 forbids spaces in the argument list. */
3426 *buffer++ = ',';
3427 else if (macro->variadic)
3428 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
3430 *buffer++ = ')';
3433 /* The Dwarf spec requires a space after the macro name, even if the
3434 definition is the empty string. */
3435 *buffer++ = ' ';
3437 if (CPP_OPTION (pfile, traditional))
3438 buffer = _cpp_copy_replacement_text (macro, buffer);
3439 else if (macro->count)
3440 /* Expansion tokens. */
3442 unsigned int count = macro_real_token_count (macro);
3443 for (i = 0; i < count; i++)
3445 cpp_token *token = &macro->exp.tokens[i];
3447 if (token->flags & PREV_WHITE)
3448 *buffer++ = ' ';
3449 if (token->flags & STRINGIFY_ARG)
3450 *buffer++ = '#';
3452 if (token->type == CPP_MACRO_ARG)
3454 memcpy (buffer,
3455 NODE_NAME (token->val.macro_arg.spelling),
3456 NODE_LEN (token->val.macro_arg.spelling));
3457 buffer += NODE_LEN (token->val.macro_arg.spelling);
3459 else
3460 buffer = cpp_spell_token (pfile, token, buffer, true);
3462 if (token->flags & PASTE_LEFT)
3464 *buffer++ = ' ';
3465 *buffer++ = '#';
3466 *buffer++ = '#';
3467 /* Next has PREV_WHITE; see _cpp_create_definition. */
3472 *buffer = '\0';
3473 return pfile->macro_buffer;