2016-05-20 Thomas Preud'homme <thomas.preudhomme@arm.com>
[official-gcc.git] / libcpp / macro.c
blobc2a83764660d1f8ceccfe3ce767cd349c151c818
1 /* Part of CPP library. (Macro and #define handling.)
2 Copyright (C) 1986-2016 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 usage if SOURCE_DATE_EPOCH is defined. */
362 if (pfile->source_date_epoch != (time_t) -1)
363 tb = gmtime (&pfile->source_date_epoch);
364 else
366 /* (time_t) -1 is a legitimate value for "number of seconds
367 since the Epoch", so we have to do a little dance to
368 distinguish that from a genuine error. */
369 errno = 0;
370 tt = time (NULL);
371 if (tt != (time_t)-1 || errno == 0)
372 tb = localtime (&tt);
375 if (tb)
377 pfile->date = _cpp_unaligned_alloc (pfile,
378 sizeof ("\"Oct 11 1347\""));
379 sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
380 monthnames[tb->tm_mon], tb->tm_mday,
381 tb->tm_year + 1900);
383 pfile->time = _cpp_unaligned_alloc (pfile,
384 sizeof ("\"12:34:56\""));
385 sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
386 tb->tm_hour, tb->tm_min, tb->tm_sec);
388 else
390 cpp_errno (pfile, CPP_DL_WARNING,
391 "could not determine date and time");
393 pfile->date = UC"\"??? ?? ????\"";
394 pfile->time = UC"\"??:??:??\"";
398 if (node->value.builtin == BT_DATE)
399 result = pfile->date;
400 else
401 result = pfile->time;
402 break;
404 case BT_COUNTER:
405 if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive)
406 cpp_error (pfile, CPP_DL_ERROR,
407 "__COUNTER__ expanded inside directive with -fdirectives-only");
408 number = pfile->counter++;
409 break;
411 case BT_HAS_ATTRIBUTE:
412 number = pfile->cb.has_attribute (pfile);
413 break;
416 if (result == NULL)
418 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
419 result = _cpp_unaligned_alloc (pfile, 21);
420 sprintf ((char *) result, "%u", number);
423 return result;
426 /* Convert builtin macros like __FILE__ to a token and push it on the
427 context stack. Also handles _Pragma, for which a new token may not
428 be created. Returns 1 if it generates a new token context, 0 to
429 return the token to the caller. LOC is the location of the expansion
430 point of the macro. */
431 static int
432 builtin_macro (cpp_reader *pfile, cpp_hashnode *node,
433 source_location loc, source_location expand_loc)
435 const uchar *buf;
436 size_t len;
437 char *nbuf;
439 if (node->value.builtin == BT_PRAGMA)
441 /* Don't interpret _Pragma within directives. The standard is
442 not clear on this, but to me this makes most sense. */
443 if (pfile->state.in_directive)
444 return 0;
446 return _cpp_do__Pragma (pfile, loc);
449 buf = _cpp_builtin_macro_text (pfile, node, expand_loc);
450 len = ustrlen (buf);
451 nbuf = (char *) alloca (len + 1);
452 memcpy (nbuf, buf, len);
453 nbuf[len]='\n';
455 cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
456 _cpp_clean_line (pfile);
458 /* Set pfile->cur_token as required by _cpp_lex_direct. */
459 pfile->cur_token = _cpp_temp_token (pfile);
460 cpp_token *token = _cpp_lex_direct (pfile);
461 /* We should point to the expansion point of the builtin macro. */
462 token->src_loc = loc;
463 if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
465 /* We are tracking tokens resulting from macro expansion.
466 Create a macro line map and generate a virtual location for
467 the token resulting from the expansion of the built-in
468 macro. */
469 source_location *virt_locs = NULL;
470 _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
471 const line_map_macro * map =
472 linemap_enter_macro (pfile->line_table, node, loc, 1);
473 tokens_buff_add_token (token_buf, virt_locs, token,
474 pfile->line_table->builtin_location,
475 pfile->line_table->builtin_location,
476 map, /*macro_token_index=*/0);
477 push_extended_tokens_context (pfile, node, token_buf, virt_locs,
478 (const cpp_token **)token_buf->base,
481 else
482 _cpp_push_token_context (pfile, NULL, token, 1);
483 if (pfile->buffer->cur != pfile->buffer->rlimit)
484 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
485 NODE_NAME (node));
486 _cpp_pop_buffer (pfile);
488 return 1;
491 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
492 backslashes and double quotes. DEST must be of sufficient size.
493 Returns a pointer to the end of the string. */
494 uchar *
495 cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
497 while (len--)
499 uchar c = *src++;
501 if (c == '\\' || c == '"')
503 *dest++ = '\\';
504 *dest++ = c;
506 else
507 *dest++ = c;
510 return dest;
513 /* Convert a token sequence ARG to a single string token according to
514 the rules of the ISO C #-operator. */
515 static const cpp_token *
516 stringify_arg (cpp_reader *pfile, macro_arg *arg)
518 unsigned char *dest;
519 unsigned int i, escape_it, backslash_count = 0;
520 const cpp_token *source = NULL;
521 size_t len;
523 if (BUFF_ROOM (pfile->u_buff) < 3)
524 _cpp_extend_buff (pfile, &pfile->u_buff, 3);
525 dest = BUFF_FRONT (pfile->u_buff);
526 *dest++ = '"';
528 /* Loop, reading in the argument's tokens. */
529 for (i = 0; i < arg->count; i++)
531 const cpp_token *token = arg->first[i];
533 if (token->type == CPP_PADDING)
535 if (source == NULL
536 || (!(source->flags & PREV_WHITE)
537 && token->val.source == NULL))
538 source = token->val.source;
539 continue;
542 escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR
543 || token->type == CPP_WSTRING || token->type == CPP_WCHAR
544 || token->type == CPP_STRING32 || token->type == CPP_CHAR32
545 || token->type == CPP_STRING16 || token->type == CPP_CHAR16
546 || token->type == CPP_UTF8STRING || token->type == CPP_UTF8CHAR
547 || cpp_userdef_string_p (token->type)
548 || cpp_userdef_char_p (token->type));
550 /* Room for each char being written in octal, initial space and
551 final quote and NUL. */
552 len = cpp_token_len (token);
553 if (escape_it)
554 len *= 4;
555 len += 3;
557 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
559 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
560 _cpp_extend_buff (pfile, &pfile->u_buff, len);
561 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
564 /* Leading white space? */
565 if (dest - 1 != BUFF_FRONT (pfile->u_buff))
567 if (source == NULL)
568 source = token;
569 if (source->flags & PREV_WHITE)
570 *dest++ = ' ';
572 source = NULL;
574 if (escape_it)
576 _cpp_buff *buff = _cpp_get_buff (pfile, len);
577 unsigned char *buf = BUFF_FRONT (buff);
578 len = cpp_spell_token (pfile, token, buf, true) - buf;
579 dest = cpp_quote_string (dest, buf, len);
580 _cpp_release_buff (pfile, buff);
582 else
583 dest = cpp_spell_token (pfile, token, dest, true);
585 if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
586 backslash_count++;
587 else
588 backslash_count = 0;
591 /* Ignore the final \ of invalid string literals. */
592 if (backslash_count & 1)
594 cpp_error (pfile, CPP_DL_WARNING,
595 "invalid string literal, ignoring final '\\'");
596 dest--;
599 /* Commit the memory, including NUL, and return the token. */
600 *dest++ = '"';
601 len = dest - BUFF_FRONT (pfile->u_buff);
602 BUFF_FRONT (pfile->u_buff) = dest + 1;
603 return new_string_token (pfile, dest - len, len);
606 /* Try to paste two tokens. On success, return nonzero. In any
607 case, PLHS is updated to point to the pasted token, which is
608 guaranteed to not have the PASTE_LEFT flag set. LOCATION is
609 the virtual location used for error reporting. */
610 static bool
611 paste_tokens (cpp_reader *pfile, source_location location,
612 const cpp_token **plhs, const cpp_token *rhs)
614 unsigned char *buf, *end, *lhsend;
615 cpp_token *lhs;
616 unsigned int len;
618 len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1;
619 buf = (unsigned char *) alloca (len);
620 end = lhsend = cpp_spell_token (pfile, *plhs, buf, true);
622 /* Avoid comment headers, since they are still processed in stage 3.
623 It is simpler to insert a space here, rather than modifying the
624 lexer to ignore comments in some circumstances. Simply returning
625 false doesn't work, since we want to clear the PASTE_LEFT flag. */
626 if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ)
627 *end++ = ' ';
628 /* In one obscure case we might see padding here. */
629 if (rhs->type != CPP_PADDING)
630 end = cpp_spell_token (pfile, rhs, end, true);
631 *end = '\n';
633 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
634 _cpp_clean_line (pfile);
636 /* Set pfile->cur_token as required by _cpp_lex_direct. */
637 pfile->cur_token = _cpp_temp_token (pfile);
638 lhs = _cpp_lex_direct (pfile);
639 if (pfile->buffer->cur != pfile->buffer->rlimit)
641 source_location saved_loc = lhs->src_loc;
643 _cpp_pop_buffer (pfile);
644 _cpp_backup_tokens (pfile, 1);
645 *lhsend = '\0';
647 /* We have to remove the PASTE_LEFT flag from the old lhs, but
648 we want to keep the new location. */
649 *lhs = **plhs;
650 *plhs = lhs;
651 lhs->src_loc = saved_loc;
652 lhs->flags &= ~PASTE_LEFT;
654 /* Mandatory error for all apart from assembler. */
655 if (CPP_OPTION (pfile, lang) != CLK_ASM)
656 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
657 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
658 buf, cpp_token_as_text (pfile, rhs));
659 return false;
662 *plhs = lhs;
663 _cpp_pop_buffer (pfile);
664 return true;
667 /* Handles an arbitrarily long sequence of ## operators, with initial
668 operand LHS. This implementation is left-associative,
669 non-recursive, and finishes a paste before handling succeeding
670 ones. If a paste fails, we back up to the RHS of the failing ##
671 operator before pushing the context containing the result of prior
672 successful pastes, with the effect that the RHS appears in the
673 output stream after the pasted LHS normally. */
674 static void
675 paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
677 const cpp_token *rhs = NULL;
678 cpp_context *context = pfile->context;
679 source_location virt_loc = 0;
681 /* We are expanding a macro and we must have been called on a token
682 that appears at the left hand side of a ## operator. */
683 if (macro_of_context (pfile->context) == NULL
684 || (!(lhs->flags & PASTE_LEFT)))
685 abort ();
687 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
688 /* The caller must have called consume_next_token_from_context
689 right before calling us. That has incremented the pointer to
690 the current virtual location. So it now points to the location
691 of the token that comes right after *LHS. We want the
692 resulting pasted token to have the location of the current
693 *LHS, though. */
694 virt_loc = context->c.mc->cur_virt_loc[-1];
695 else
696 /* We are not tracking macro expansion. So the best virtual
697 location we can get here is the expansion point of the macro we
698 are currently expanding. */
699 virt_loc = pfile->invocation_location;
703 /* Take the token directly from the current context. We can do
704 this, because we are in the replacement list of either an
705 object-like macro, or a function-like macro with arguments
706 inserted. In either case, the constraints to #define
707 guarantee we have at least one more token. */
708 if (context->tokens_kind == TOKENS_KIND_DIRECT)
709 rhs = FIRST (context).token++;
710 else if (context->tokens_kind == TOKENS_KIND_INDIRECT)
711 rhs = *FIRST (context).ptoken++;
712 else if (context->tokens_kind == TOKENS_KIND_EXTENDED)
714 /* So we are in presence of an extended token context, which
715 means that each token in this context has a virtual
716 location attached to it. So let's not forget to update
717 the pointer to the current virtual location of the
718 current token when we update the pointer to the current
719 token */
721 rhs = *FIRST (context).ptoken++;
722 /* context->c.mc must be non-null, as if we were not in a
723 macro context, context->tokens_kind could not be equal to
724 TOKENS_KIND_EXTENDED. */
725 context->c.mc->cur_virt_loc++;
728 if (rhs->type == CPP_PADDING)
730 if (rhs->flags & PASTE_LEFT)
731 abort ();
733 if (!paste_tokens (pfile, virt_loc, &lhs, rhs))
734 break;
736 while (rhs->flags & PASTE_LEFT);
738 /* Put the resulting token in its own context. */
739 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
741 source_location *virt_locs = NULL;
742 _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
743 tokens_buff_add_token (token_buf, virt_locs, lhs,
744 virt_loc, 0, NULL, 0);
745 push_extended_tokens_context (pfile, context->c.mc->macro_node,
746 token_buf, virt_locs,
747 (const cpp_token **)token_buf->base, 1);
749 else
750 _cpp_push_token_context (pfile, NULL, lhs, 1);
753 /* Returns TRUE if the number of arguments ARGC supplied in an
754 invocation of the MACRO referenced by NODE is valid. An empty
755 invocation to a macro with no parameters should pass ARGC as zero.
757 Note that MACRO cannot necessarily be deduced from NODE, in case
758 NODE was redefined whilst collecting arguments. */
759 bool
760 _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
762 if (argc == macro->paramc)
763 return true;
765 if (argc < macro->paramc)
767 /* As an extension, variadic arguments are allowed to not appear in
768 the invocation at all.
769 e.g. #define debug(format, args...) something
770 debug("string");
772 This is exactly the same as if an empty variadic list had been
773 supplied - debug("string", ). */
775 if (argc + 1 == macro->paramc && macro->variadic)
777 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
779 if (CPP_OPTION (pfile, cplusplus))
780 cpp_error (pfile, CPP_DL_PEDWARN,
781 "ISO C++11 requires at least one argument "
782 "for the \"...\" in a variadic macro");
783 else
784 cpp_error (pfile, CPP_DL_PEDWARN,
785 "ISO C99 requires at least one argument "
786 "for the \"...\" in a variadic macro");
788 return true;
791 cpp_error (pfile, CPP_DL_ERROR,
792 "macro \"%s\" requires %u arguments, but only %u given",
793 NODE_NAME (node), macro->paramc, argc);
795 else
796 cpp_error (pfile, CPP_DL_ERROR,
797 "macro \"%s\" passed %u arguments, but takes just %u",
798 NODE_NAME (node), argc, macro->paramc);
800 return false;
803 /* Reads and returns the arguments to a function-like macro
804 invocation. Assumes the opening parenthesis has been processed.
805 If there is an error, emits an appropriate diagnostic and returns
806 NULL. Each argument is terminated by a CPP_EOF token, for the
807 future benefit of expand_arg(). If there are any deferred
808 #pragma directives among macro arguments, store pointers to the
809 CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.
811 What is returned is the buffer that contains the memory allocated
812 to hold the macro arguments. NODE is the name of the macro this
813 function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is
814 set to the actual number of macro arguments allocated in the
815 returned buffer. */
816 static _cpp_buff *
817 collect_args (cpp_reader *pfile, const cpp_hashnode *node,
818 _cpp_buff **pragma_buff, unsigned *num_args)
820 _cpp_buff *buff, *base_buff;
821 cpp_macro *macro;
822 macro_arg *args, *arg;
823 const cpp_token *token;
824 unsigned int argc;
825 source_location virt_loc;
826 bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion);
827 unsigned num_args_alloced = 0;
829 macro = node->value.macro;
830 if (macro->paramc)
831 argc = macro->paramc;
832 else
833 argc = 1;
835 #define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50
836 #define ARG_TOKENS_EXTENT 1000
838 buff = _cpp_get_buff (pfile, argc * (DEFAULT_NUM_TOKENS_PER_MACRO_ARG
839 * sizeof (cpp_token *)
840 + sizeof (macro_arg)));
841 base_buff = buff;
842 args = (macro_arg *) buff->base;
843 memset (args, 0, argc * sizeof (macro_arg));
844 buff->cur = (unsigned char *) &args[argc];
845 arg = args, argc = 0;
847 /* Collect the tokens making up each argument. We don't yet know
848 how many arguments have been supplied, whether too many or too
849 few. Hence the slightly bizarre usage of "argc" and "arg". */
852 unsigned int paren_depth = 0;
853 unsigned int ntokens = 0;
854 unsigned virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
855 num_args_alloced++;
857 argc++;
858 arg->first = (const cpp_token **) buff->cur;
859 if (track_macro_expansion_p)
861 virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
862 arg->virt_locs = XNEWVEC (source_location,
863 virt_locs_capacity);
866 for (;;)
868 /* Require space for 2 new tokens (including a CPP_EOF). */
869 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
871 buff = _cpp_append_extend_buff (pfile, buff,
872 ARG_TOKENS_EXTENT
873 * sizeof (cpp_token *));
874 arg->first = (const cpp_token **) buff->cur;
876 if (track_macro_expansion_p
877 && (ntokens + 2 > virt_locs_capacity))
879 virt_locs_capacity += ARG_TOKENS_EXTENT;
880 arg->virt_locs = XRESIZEVEC (source_location,
881 arg->virt_locs,
882 virt_locs_capacity);
885 token = cpp_get_token_1 (pfile, &virt_loc);
887 if (token->type == CPP_PADDING)
889 /* Drop leading padding. */
890 if (ntokens == 0)
891 continue;
893 else if (token->type == CPP_OPEN_PAREN)
894 paren_depth++;
895 else if (token->type == CPP_CLOSE_PAREN)
897 if (paren_depth-- == 0)
898 break;
900 else if (token->type == CPP_COMMA)
902 /* A comma does not terminate an argument within
903 parentheses or as part of a variable argument. */
904 if (paren_depth == 0
905 && ! (macro->variadic && argc == macro->paramc))
906 break;
908 else if (token->type == CPP_EOF
909 || (token->type == CPP_HASH && token->flags & BOL))
910 break;
911 else if (token->type == CPP_PRAGMA)
913 cpp_token *newtok = _cpp_temp_token (pfile);
915 /* CPP_PRAGMA token lives in directive_result, which will
916 be overwritten on the next directive. */
917 *newtok = *token;
918 token = newtok;
921 if (*pragma_buff == NULL
922 || BUFF_ROOM (*pragma_buff) < sizeof (cpp_token *))
924 _cpp_buff *next;
925 if (*pragma_buff == NULL)
926 *pragma_buff
927 = _cpp_get_buff (pfile, 32 * sizeof (cpp_token *));
928 else
930 next = *pragma_buff;
931 *pragma_buff
932 = _cpp_get_buff (pfile,
933 (BUFF_FRONT (*pragma_buff)
934 - (*pragma_buff)->base) * 2);
935 (*pragma_buff)->next = next;
938 *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token;
939 BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *);
940 if (token->type == CPP_PRAGMA_EOL)
941 break;
942 token = cpp_get_token_1 (pfile, &virt_loc);
944 while (token->type != CPP_EOF);
946 /* In deferred pragmas parsing_args and prevent_expansion
947 had been changed, reset it. */
948 pfile->state.parsing_args = 2;
949 pfile->state.prevent_expansion = 1;
951 if (token->type == CPP_EOF)
952 break;
953 else
954 continue;
956 set_arg_token (arg, token, virt_loc,
957 ntokens, MACRO_ARG_TOKEN_NORMAL,
958 CPP_OPTION (pfile, track_macro_expansion));
959 ntokens++;
962 /* Drop trailing padding. */
963 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
964 ntokens--;
966 arg->count = ntokens;
967 set_arg_token (arg, &pfile->eof, pfile->eof.src_loc,
968 ntokens, MACRO_ARG_TOKEN_NORMAL,
969 CPP_OPTION (pfile, track_macro_expansion));
971 /* Terminate the argument. Excess arguments loop back and
972 overwrite the final legitimate argument, before failing. */
973 if (argc <= macro->paramc)
975 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
976 if (argc != macro->paramc)
977 arg++;
980 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
982 if (token->type == CPP_EOF)
984 /* We still need the CPP_EOF to end directives, and to end
985 pre-expansion of a macro argument. Step back is not
986 unconditional, since we don't want to return a CPP_EOF to our
987 callers at the end of an -include-d file. */
988 if (pfile->context->prev || pfile->state.in_directive)
989 _cpp_backup_tokens (pfile, 1);
990 cpp_error (pfile, CPP_DL_ERROR,
991 "unterminated argument list invoking macro \"%s\"",
992 NODE_NAME (node));
994 else
996 /* A single empty argument is counted as no argument. */
997 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
998 argc = 0;
999 if (_cpp_arguments_ok (pfile, macro, node, argc))
1001 /* GCC has special semantics for , ## b where b is a varargs
1002 parameter: we remove the comma if b was omitted entirely.
1003 If b was merely an empty argument, the comma is retained.
1004 If the macro takes just one (varargs) parameter, then we
1005 retain the comma only if we are standards conforming.
1007 If FIRST is NULL replace_args () swallows the comma. */
1008 if (macro->variadic && (argc < macro->paramc
1009 || (argc == 1 && args[0].count == 0
1010 && !CPP_OPTION (pfile, std))))
1011 args[macro->paramc - 1].first = NULL;
1012 if (num_args)
1013 *num_args = num_args_alloced;
1014 return base_buff;
1018 /* An error occurred. */
1019 _cpp_release_buff (pfile, base_buff);
1020 return NULL;
1023 /* Search for an opening parenthesis to the macro of NODE, in such a
1024 way that, if none is found, we don't lose the information in any
1025 intervening padding tokens. If we find the parenthesis, collect
1026 the arguments and return the buffer containing them. PRAGMA_BUFF
1027 argument is the same as in collect_args. If NUM_ARGS is non-NULL,
1028 *NUM_ARGS is set to the number of arguments contained in the
1029 returned buffer. */
1030 static _cpp_buff *
1031 funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node,
1032 _cpp_buff **pragma_buff, unsigned *num_args)
1034 const cpp_token *token, *padding = NULL;
1036 for (;;)
1038 token = cpp_get_token (pfile);
1039 if (token->type != CPP_PADDING)
1040 break;
1041 if (padding == NULL
1042 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
1043 padding = token;
1046 if (token->type == CPP_OPEN_PAREN)
1048 pfile->state.parsing_args = 2;
1049 return collect_args (pfile, node, pragma_buff, num_args);
1052 /* CPP_EOF can be the end of macro arguments, or the end of the
1053 file. We mustn't back up over the latter. Ugh. */
1054 if (token->type != CPP_EOF || token == &pfile->eof)
1056 /* Back up. We may have skipped padding, in which case backing
1057 up more than one token when expanding macros is in general
1058 too difficult. We re-insert it in its own context. */
1059 _cpp_backup_tokens (pfile, 1);
1060 if (padding)
1061 _cpp_push_token_context (pfile, NULL, padding, 1);
1064 return NULL;
1067 /* Return the real number of tokens in the expansion of MACRO. */
1068 static inline unsigned int
1069 macro_real_token_count (const cpp_macro *macro)
1071 unsigned int i;
1072 if (__builtin_expect (!macro->extra_tokens, true))
1073 return macro->count;
1074 for (i = 0; i < macro->count; i++)
1075 if (macro->exp.tokens[i].type == CPP_PASTE)
1076 return i;
1077 abort ();
1080 /* Push the context of a macro with hash entry NODE onto the context
1081 stack. If we can successfully expand the macro, we push a context
1082 containing its yet-to-be-rescanned replacement list and return one.
1083 If there were additionally any unexpanded deferred #pragma
1084 directives among macro arguments, push another context containing
1085 the pragma tokens before the yet-to-be-rescanned replacement list
1086 and return two. Otherwise, we don't push a context and return
1087 zero. LOCATION is the location of the expansion point of the
1088 macro. */
1089 static int
1090 enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
1091 const cpp_token *result, source_location location)
1093 /* The presence of a macro invalidates a file's controlling macro. */
1094 pfile->mi_valid = false;
1096 pfile->state.angled_headers = false;
1098 /* From here to when we push the context for the macro later down
1099 this function, we need to flag the fact that we are about to
1100 expand a macro. This is useful when -ftrack-macro-expansion is
1101 turned off. In that case, we need to record the location of the
1102 expansion point of the top-most macro we are about to to expand,
1103 into pfile->invocation_location. But we must not record any such
1104 location once the process of expanding the macro starts; that is,
1105 we must not do that recording between now and later down this
1106 function where set this flag to FALSE. */
1107 pfile->about_to_expand_macro_p = true;
1109 if ((node->flags & NODE_BUILTIN) && !(node->flags & NODE_USED))
1111 node->flags |= NODE_USED;
1112 if ((!pfile->cb.user_builtin_macro
1113 || !pfile->cb.user_builtin_macro (pfile, node))
1114 && pfile->cb.used_define)
1115 pfile->cb.used_define (pfile, pfile->directive_line, node);
1118 /* Handle standard macros. */
1119 if (! (node->flags & NODE_BUILTIN))
1121 cpp_macro *macro = node->value.macro;
1122 _cpp_buff *pragma_buff = NULL;
1124 if (macro->fun_like)
1126 _cpp_buff *buff;
1127 unsigned num_args = 0;
1129 pfile->state.prevent_expansion++;
1130 pfile->keep_tokens++;
1131 pfile->state.parsing_args = 1;
1132 buff = funlike_invocation_p (pfile, node, &pragma_buff,
1133 &num_args);
1134 pfile->state.parsing_args = 0;
1135 pfile->keep_tokens--;
1136 pfile->state.prevent_expansion--;
1138 if (buff == NULL)
1140 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
1141 cpp_warning (pfile, CPP_W_TRADITIONAL,
1142 "function-like macro \"%s\" must be used with arguments in traditional C",
1143 NODE_NAME (node));
1145 if (pragma_buff)
1146 _cpp_release_buff (pfile, pragma_buff);
1148 pfile->about_to_expand_macro_p = false;
1149 return 0;
1152 if (macro->paramc > 0)
1153 replace_args (pfile, node, macro,
1154 (macro_arg *) buff->base,
1155 location);
1156 /* Free the memory used by the arguments of this
1157 function-like macro. This memory has been allocated by
1158 funlike_invocation_p and by replace_args. */
1159 delete_macro_args (buff, num_args);
1162 /* Disable the macro within its expansion. */
1163 node->flags |= NODE_DISABLED;
1165 if (!(node->flags & NODE_USED))
1167 node->flags |= NODE_USED;
1168 if (pfile->cb.used_define)
1169 pfile->cb.used_define (pfile, pfile->directive_line, node);
1172 if (pfile->cb.used)
1173 pfile->cb.used (pfile, location, node);
1175 macro->used = 1;
1177 if (macro->paramc == 0)
1179 unsigned tokens_count = macro_real_token_count (macro);
1180 if (CPP_OPTION (pfile, track_macro_expansion))
1182 unsigned int i;
1183 const cpp_token *src = macro->exp.tokens;
1184 const line_map_macro *map;
1185 source_location *virt_locs = NULL;
1186 _cpp_buff *macro_tokens
1187 = tokens_buff_new (pfile, tokens_count, &virt_locs);
1189 /* Create a macro map to record the locations of the
1190 tokens that are involved in the expansion. LOCATION
1191 is the location of the macro expansion point. */
1192 map = linemap_enter_macro (pfile->line_table,
1193 node, location, tokens_count);
1194 for (i = 0; i < tokens_count; ++i)
1196 tokens_buff_add_token (macro_tokens, virt_locs,
1197 src, src->src_loc,
1198 src->src_loc, map, i);
1199 ++src;
1201 push_extended_tokens_context (pfile, node,
1202 macro_tokens,
1203 virt_locs,
1204 (const cpp_token **)
1205 macro_tokens->base,
1206 tokens_count);
1208 else
1209 _cpp_push_token_context (pfile, node, macro->exp.tokens,
1210 tokens_count);
1211 num_macro_tokens_counter += tokens_count;
1214 if (pragma_buff)
1216 if (!pfile->state.in_directive)
1217 _cpp_push_token_context (pfile, NULL,
1218 padding_token (pfile, result), 1);
1221 unsigned tokens_count;
1222 _cpp_buff *tail = pragma_buff->next;
1223 pragma_buff->next = NULL;
1224 tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff)
1225 - (const cpp_token **) pragma_buff->base);
1226 push_ptoken_context (pfile, NULL, pragma_buff,
1227 (const cpp_token **) pragma_buff->base,
1228 tokens_count);
1229 pragma_buff = tail;
1230 if (!CPP_OPTION (pfile, track_macro_expansion))
1231 num_macro_tokens_counter += tokens_count;
1234 while (pragma_buff != NULL);
1235 pfile->about_to_expand_macro_p = false;
1236 return 2;
1239 pfile->about_to_expand_macro_p = false;
1240 return 1;
1243 pfile->about_to_expand_macro_p = false;
1244 /* Handle built-in macros and the _Pragma operator. */
1246 source_location loc, expand_loc;
1248 if (/* The top-level macro invocation that triggered the expansion
1249 we are looking at is with a standard macro ...*/
1250 !(pfile->top_most_macro_node->flags & NODE_BUILTIN)
1251 /* ... and it's a function-like macro invocation. */
1252 && pfile->top_most_macro_node->value.macro->fun_like)
1254 /* Then the location of the end of the macro invocation is the
1255 location of the closing parenthesis. */
1256 loc = pfile->cur_token[-1].src_loc;
1257 expand_loc = loc;
1259 else
1261 /* Otherwise, the location of the end of the macro invocation is
1262 the location of the expansion point of that top-level macro
1263 invocation. */
1264 loc = location;
1265 expand_loc = pfile->invocation_location;
1268 return builtin_macro (pfile, node, loc, expand_loc);
1272 /* De-allocate the memory used by BUFF which is an array of instances
1273 of macro_arg. NUM_ARGS is the number of instances of macro_arg
1274 present in BUFF. */
1275 static void
1276 delete_macro_args (_cpp_buff *buff, unsigned num_args)
1278 macro_arg *macro_args;
1279 unsigned i;
1281 if (buff == NULL)
1282 return;
1284 macro_args = (macro_arg *) buff->base;
1286 /* Walk instances of macro_arg to free their expanded tokens as well
1287 as their macro_arg::virt_locs members. */
1288 for (i = 0; i < num_args; ++i)
1290 if (macro_args[i].expanded)
1292 free (macro_args[i].expanded);
1293 macro_args[i].expanded = NULL;
1295 if (macro_args[i].virt_locs)
1297 free (macro_args[i].virt_locs);
1298 macro_args[i].virt_locs = NULL;
1300 if (macro_args[i].expanded_virt_locs)
1302 free (macro_args[i].expanded_virt_locs);
1303 macro_args[i].expanded_virt_locs = NULL;
1306 _cpp_free_buff (buff);
1309 /* Set the INDEXth token of the macro argument ARG. TOKEN is the token
1310 to set, LOCATION is its virtual location. "Virtual" location means
1311 the location that encodes loci across macro expansion. Otherwise
1312 it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the
1313 argument ARG is supposed to contain. Note that ARG must be
1314 tailored so that it has enough room to contain INDEX + 1 numbers of
1315 tokens, at least. */
1316 static void
1317 set_arg_token (macro_arg *arg, const cpp_token *token,
1318 source_location location, size_t index,
1319 enum macro_arg_token_kind kind,
1320 bool track_macro_exp_p)
1322 const cpp_token **token_ptr;
1323 source_location *loc = NULL;
1325 token_ptr =
1326 arg_token_ptr_at (arg, index, kind,
1327 track_macro_exp_p ? &loc : NULL);
1328 *token_ptr = token;
1330 if (loc != NULL)
1332 /* We can't set the location of a stringified argument
1333 token and we can't set any location if we aren't tracking
1334 macro expansion locations. */
1335 gcc_checking_assert (kind != MACRO_ARG_TOKEN_STRINGIFIED
1336 && track_macro_exp_p);
1337 *loc = location;
1341 /* Get the pointer to the location of the argument token of the
1342 function-like macro argument ARG. This function must be called
1343 only when we -ftrack-macro-expansion is on. */
1344 static const source_location *
1345 get_arg_token_location (const macro_arg *arg,
1346 enum macro_arg_token_kind kind)
1348 const source_location *loc = NULL;
1349 const cpp_token **token_ptr =
1350 arg_token_ptr_at (arg, 0, kind, (source_location **) &loc);
1352 if (token_ptr == NULL)
1353 return NULL;
1355 return loc;
1358 /* Return the pointer to the INDEXth token of the macro argument ARG.
1359 KIND specifies the kind of token the macro argument ARG contains.
1360 If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
1361 of the virtual location of the returned token if the
1362 -ftrack-macro-expansion flag is on; otherwise, it's set to the
1363 spelling location of the returned token. */
1364 static const cpp_token **
1365 arg_token_ptr_at (const macro_arg *arg, size_t index,
1366 enum macro_arg_token_kind kind,
1367 source_location **virt_location)
1369 const cpp_token **tokens_ptr = NULL;
1371 switch (kind)
1373 case MACRO_ARG_TOKEN_NORMAL:
1374 tokens_ptr = arg->first;
1375 break;
1376 case MACRO_ARG_TOKEN_STRINGIFIED:
1377 tokens_ptr = (const cpp_token **) &arg->stringified;
1378 break;
1379 case MACRO_ARG_TOKEN_EXPANDED:
1380 tokens_ptr = arg->expanded;
1381 break;
1384 if (tokens_ptr == NULL)
1385 /* This can happen for e.g, an empty token argument to a
1386 funtion-like macro. */
1387 return tokens_ptr;
1389 if (virt_location)
1391 if (kind == MACRO_ARG_TOKEN_NORMAL)
1392 *virt_location = &arg->virt_locs[index];
1393 else if (kind == MACRO_ARG_TOKEN_EXPANDED)
1394 *virt_location = &arg->expanded_virt_locs[index];
1395 else if (kind == MACRO_ARG_TOKEN_STRINGIFIED)
1396 *virt_location =
1397 (source_location *) &tokens_ptr[index]->src_loc;
1399 return &tokens_ptr[index];
1402 /* Initialize an iterator so that it iterates over the tokens of a
1403 function-like macro argument. KIND is the kind of tokens we want
1404 ITER to iterate over. TOKEN_PTR points the first token ITER will
1405 iterate over. */
1406 static void
1407 macro_arg_token_iter_init (macro_arg_token_iter *iter,
1408 bool track_macro_exp_p,
1409 enum macro_arg_token_kind kind,
1410 const macro_arg *arg,
1411 const cpp_token **token_ptr)
1413 iter->track_macro_exp_p = track_macro_exp_p;
1414 iter->kind = kind;
1415 iter->token_ptr = token_ptr;
1416 /* Unconditionally initialize this so that the compiler doesn't warn
1417 about iter->location_ptr being possibly uninitialized later after
1418 this code has been inlined somewhere. */
1419 iter->location_ptr = NULL;
1420 if (track_macro_exp_p)
1421 iter->location_ptr = get_arg_token_location (arg, kind);
1422 #if CHECKING_P
1423 iter->num_forwards = 0;
1424 if (track_macro_exp_p
1425 && token_ptr != NULL
1426 && iter->location_ptr == NULL)
1427 abort ();
1428 #endif
1431 /* Move the iterator one token forward. Note that if IT was
1432 initialized on an argument that has a stringified token, moving it
1433 forward doesn't make sense as a stringified token is essentially one
1434 string. */
1435 static void
1436 macro_arg_token_iter_forward (macro_arg_token_iter *it)
1438 switch (it->kind)
1440 case MACRO_ARG_TOKEN_NORMAL:
1441 case MACRO_ARG_TOKEN_EXPANDED:
1442 it->token_ptr++;
1443 if (it->track_macro_exp_p)
1444 it->location_ptr++;
1445 break;
1446 case MACRO_ARG_TOKEN_STRINGIFIED:
1447 #if CHECKING_P
1448 if (it->num_forwards > 0)
1449 abort ();
1450 #endif
1451 break;
1454 #if CHECKING_P
1455 it->num_forwards++;
1456 #endif
1459 /* Return the token pointed to by the iterator. */
1460 static const cpp_token *
1461 macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
1463 #if CHECKING_P
1464 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1465 && it->num_forwards > 0)
1466 abort ();
1467 #endif
1468 if (it->token_ptr == NULL)
1469 return NULL;
1470 return *it->token_ptr;
1473 /* Return the location of the token pointed to by the iterator.*/
1474 static source_location
1475 macro_arg_token_iter_get_location (const macro_arg_token_iter *it)
1477 #if CHECKING_P
1478 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1479 && it->num_forwards > 0)
1480 abort ();
1481 #endif
1482 if (it->track_macro_exp_p)
1483 return *it->location_ptr;
1484 else
1485 return (*it->token_ptr)->src_loc;
1488 /* Return the index of a token [resulting from macro expansion] inside
1489 the total list of tokens resulting from a given macro
1490 expansion. The index can be different depending on whether if we
1491 want each tokens resulting from function-like macro arguments
1492 expansion to have a different location or not.
1494 E.g, consider this function-like macro:
1496 #define M(x) x - 3
1498 Then consider us "calling" it (and thus expanding it) like:
1500 M(1+4)
1502 It will be expanded into:
1504 1+4-3
1506 Let's consider the case of the token '4'.
1508 Its index can be 2 (it's the third token of the set of tokens
1509 resulting from the expansion) or it can be 0 if we consider that
1510 all tokens resulting from the expansion of the argument "1+2" have
1511 the same index, which is 0. In this later case, the index of token
1512 '-' would then be 1 and the index of token '3' would be 2.
1514 The later case is useful to use less memory e.g, for the case of
1515 the user using the option -ftrack-macro-expansion=1.
1517 ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
1518 are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro
1519 parameter (inside the macro replacement list) that corresponds to
1520 the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
1523 If we refer to the example above, for the '4' argument token,
1524 ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
1525 would be set to the token 'x', in the replacement list "x - 3" of
1526 macro M.
1528 This is a subroutine of replace_args. */
1529 inline static unsigned
1530 expanded_token_index (cpp_reader *pfile, cpp_macro *macro,
1531 const cpp_token *cur_replacement_token,
1532 unsigned absolute_token_index)
1534 if (CPP_OPTION (pfile, track_macro_expansion) > 1)
1535 return absolute_token_index;
1536 return cur_replacement_token - macro->exp.tokens;
1539 /* Replace the parameters in a function-like macro of NODE with the
1540 actual ARGS, and place the result in a newly pushed token context.
1541 Expand each argument before replacing, unless it is operated upon
1542 by the # or ## operators. EXPANSION_POINT_LOC is the location of
1543 the expansion point of the macro. E.g, the location of the
1544 function-like macro invocation. */
1545 static void
1546 replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
1547 macro_arg *args, source_location expansion_point_loc)
1549 unsigned int i, total;
1550 const cpp_token *src, *limit;
1551 const cpp_token **first = NULL;
1552 macro_arg *arg;
1553 _cpp_buff *buff = NULL;
1554 source_location *virt_locs = NULL;
1555 unsigned int exp_count;
1556 const line_map_macro *map = NULL;
1557 int track_macro_exp;
1559 /* First, fully macro-expand arguments, calculating the number of
1560 tokens in the final expansion as we go. The ordering of the if
1561 statements below is subtle; we must handle stringification before
1562 pasting. */
1564 /* EXP_COUNT is the number of tokens in the macro replacement
1565 list. TOTAL is the number of tokens /after/ macro parameters
1566 have been replaced by their arguments. */
1567 exp_count = macro_real_token_count (macro);
1568 total = exp_count;
1569 limit = macro->exp.tokens + exp_count;
1571 for (src = macro->exp.tokens; src < limit; src++)
1572 if (src->type == CPP_MACRO_ARG)
1574 /* Leading and trailing padding tokens. */
1575 total += 2;
1576 /* Account for leading and padding tokens in exp_count too.
1577 This is going to be important later down this function,
1578 when we want to handle the case of (track_macro_exp <
1579 2). */
1580 exp_count += 2;
1582 /* We have an argument. If it is not being stringified or
1583 pasted it is macro-replaced before insertion. */
1584 arg = &args[src->val.macro_arg.arg_no - 1];
1586 if (src->flags & STRINGIFY_ARG)
1588 if (!arg->stringified)
1589 arg->stringified = stringify_arg (pfile, arg);
1591 else if ((src->flags & PASTE_LEFT)
1592 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
1593 total += arg->count - 1;
1594 else
1596 if (!arg->expanded)
1597 expand_arg (pfile, arg);
1598 total += arg->expanded_count - 1;
1602 /* When the compiler is called with the -ftrack-macro-expansion
1603 flag, we need to keep track of the location of each token that
1604 results from macro expansion.
1606 A token resulting from macro expansion is not a new token. It is
1607 simply the same token as the token coming from the macro
1608 definition. The new things that are allocated are the buffer
1609 that holds the tokens resulting from macro expansion and a new
1610 location that records many things like the locus of the expansion
1611 point as well as the original locus inside the definition of the
1612 macro. This location is called a virtual location.
1614 So the buffer BUFF holds a set of cpp_token*, and the buffer
1615 VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
1617 Both of these two buffers are going to be hung off of the macro
1618 context, when the latter is pushed. The memory allocated to
1619 store the tokens and their locations is going to be freed once
1620 the context of macro expansion is popped.
1622 As far as tokens are concerned, the memory overhead of
1623 -ftrack-macro-expansion is proportional to the number of
1624 macros that get expanded multiplied by sizeof (source_location).
1625 The good news is that extra memory gets freed when the macro
1626 context is freed, i.e shortly after the macro got expanded. */
1628 /* Is the -ftrack-macro-expansion flag in effect? */
1629 track_macro_exp = CPP_OPTION (pfile, track_macro_expansion);
1631 /* Now allocate memory space for tokens and locations resulting from
1632 the macro expansion, copy the tokens and replace the arguments.
1633 This memory must be freed when the context of the macro MACRO is
1634 popped. */
1635 buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL);
1637 first = (const cpp_token **) buff->base;
1639 /* Create a macro map to record the locations of the tokens that are
1640 involved in the expansion. Note that the expansion point is set
1641 to the location of the closing parenthesis. Otherwise, the
1642 subsequent map created for the first token that comes after the
1643 macro map might have a wrong line number. That would lead to
1644 tokens with wrong line numbers after the macro expansion. This
1645 adds up to the memory overhead of the -ftrack-macro-expansion
1646 flag; for every macro that is expanded, a "macro map" is
1647 created. */
1648 if (track_macro_exp)
1650 int num_macro_tokens = total;
1651 if (track_macro_exp < 2)
1652 /* Then the number of macro tokens won't take in account the
1653 fact that function-like macro arguments can expand to
1654 multiple tokens. This is to save memory at the expense of
1655 accuracy.
1657 Suppose we have #define SQARE(A) A * A
1659 And then we do SQARE(2+3)
1661 Then the tokens 2, +, 3, will have the same location,
1662 saying they come from the expansion of the argument A. */
1663 num_macro_tokens = exp_count;
1664 map = linemap_enter_macro (pfile->line_table, node,
1665 expansion_point_loc,
1666 num_macro_tokens);
1668 i = 0;
1669 for (src = macro->exp.tokens; src < limit; src++)
1671 unsigned int arg_tokens_count;
1672 macro_arg_token_iter from;
1673 const cpp_token **paste_flag = NULL;
1674 const cpp_token **tmp_token_ptr;
1676 if (src->type != CPP_MACRO_ARG)
1678 /* Allocate a virtual location for token SRC, and add that
1679 token and its virtual location into the buffers BUFF and
1680 VIRT_LOCS. */
1681 unsigned index = expanded_token_index (pfile, macro, src, i);
1682 tokens_buff_add_token (buff, virt_locs, src,
1683 src->src_loc, src->src_loc,
1684 map, index);
1685 i += 1;
1686 continue;
1689 paste_flag = 0;
1690 arg = &args[src->val.macro_arg.arg_no - 1];
1691 /* SRC is a macro parameter that we need to replace with its
1692 corresponding argument. So at some point we'll need to
1693 iterate over the tokens of the macro argument and copy them
1694 into the "place" now holding the correspondig macro
1695 parameter. We are going to use the iterator type
1696 macro_argo_token_iter to handle that iterating. The 'if'
1697 below is to initialize the iterator depending on the type of
1698 tokens the macro argument has. It also does some adjustment
1699 related to padding tokens and some pasting corner cases. */
1700 if (src->flags & STRINGIFY_ARG)
1702 arg_tokens_count = 1;
1703 macro_arg_token_iter_init (&from,
1704 CPP_OPTION (pfile,
1705 track_macro_expansion),
1706 MACRO_ARG_TOKEN_STRINGIFIED,
1707 arg, &arg->stringified);
1709 else if (src->flags & PASTE_LEFT)
1711 arg_tokens_count = arg->count;
1712 macro_arg_token_iter_init (&from,
1713 CPP_OPTION (pfile,
1714 track_macro_expansion),
1715 MACRO_ARG_TOKEN_NORMAL,
1716 arg, arg->first);
1718 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
1720 int num_toks;
1721 arg_tokens_count = arg->count;
1722 macro_arg_token_iter_init (&from,
1723 CPP_OPTION (pfile,
1724 track_macro_expansion),
1725 MACRO_ARG_TOKEN_NORMAL,
1726 arg, arg->first);
1728 num_toks = tokens_buff_count (buff);
1730 if (num_toks != 0)
1732 /* So the current parameter token is pasted to the previous
1733 token in the replacement list. Let's look at what
1734 we have as previous and current arguments. */
1736 /* This is the previous argument's token ... */
1737 tmp_token_ptr = tokens_buff_last_token_ptr (buff);
1739 if ((*tmp_token_ptr)->type == CPP_COMMA
1740 && macro->variadic
1741 && src->val.macro_arg.arg_no == macro->paramc)
1743 /* ... which is a comma; and the current parameter
1744 is the last parameter of a variadic function-like
1745 macro. If the argument to the current last
1746 parameter is NULL, then swallow the comma,
1747 otherwise drop the paste flag. */
1748 if (macro_arg_token_iter_get_token (&from) == NULL)
1749 tokens_buff_remove_last_token (buff);
1750 else
1751 paste_flag = tmp_token_ptr;
1753 /* Remove the paste flag if the RHS is a placemarker. */
1754 else if (arg_tokens_count == 0)
1755 paste_flag = tmp_token_ptr;
1758 else
1760 arg_tokens_count = arg->expanded_count;
1761 macro_arg_token_iter_init (&from,
1762 CPP_OPTION (pfile,
1763 track_macro_expansion),
1764 MACRO_ARG_TOKEN_EXPANDED,
1765 arg, arg->expanded);
1768 /* Padding on the left of an argument (unless RHS of ##). */
1769 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
1770 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
1772 const cpp_token *t = padding_token (pfile, src);
1773 unsigned index = expanded_token_index (pfile, macro, src, i);
1774 /* Allocate a virtual location for the padding token and
1775 append the token and its location to BUFF and
1776 VIRT_LOCS. */
1777 tokens_buff_add_token (buff, virt_locs, t,
1778 t->src_loc, t->src_loc,
1779 map, index);
1782 if (arg_tokens_count)
1784 /* So now we've got the number of tokens that make up the
1785 argument that is going to replace the current parameter
1786 in the macro's replacement list. */
1787 unsigned int j;
1788 for (j = 0; j < arg_tokens_count; ++j)
1790 /* So if track_macro_exp is < 2, the user wants to
1791 save extra memory while tracking macro expansion
1792 locations. So in that case here is what we do:
1794 Suppose we have #define SQARE(A) A * A
1796 And then we do SQARE(2+3)
1798 Then the tokens 2, +, 3, will have the same location,
1799 saying they come from the expansion of the argument
1802 So that means we are going to ignore the COUNT tokens
1803 resulting from the expansion of the current macro
1804 arugment. In other words all the ARG_TOKENS_COUNT tokens
1805 resulting from the expansion of the macro argument will
1806 have the index I. Normally, each of those token should
1807 have index I+J. */
1808 unsigned token_index = i;
1809 unsigned index;
1810 if (track_macro_exp > 1)
1811 token_index += j;
1813 index = expanded_token_index (pfile, macro, src, token_index);
1814 tokens_buff_add_token (buff, virt_locs,
1815 macro_arg_token_iter_get_token (&from),
1816 macro_arg_token_iter_get_location (&from),
1817 src->src_loc, map, index);
1818 macro_arg_token_iter_forward (&from);
1821 /* With a non-empty argument on the LHS of ##, the last
1822 token should be flagged PASTE_LEFT. */
1823 if (src->flags & PASTE_LEFT)
1824 paste_flag =
1825 (const cpp_token **) tokens_buff_last_token_ptr (buff);
1827 else if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99)
1828 && ! macro->syshdr && ! cpp_in_system_header (pfile))
1830 if (CPP_OPTION (pfile, cplusplus))
1831 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
1832 "invoking macro %s argument %d: "
1833 "empty macro arguments are undefined"
1834 " in ISO C++98",
1835 NODE_NAME (node), src->val.macro_arg.arg_no);
1836 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat))
1837 cpp_pedwarning (pfile,
1838 CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
1839 ? CPP_W_C90_C99_COMPAT : CPP_W_PEDANTIC,
1840 "invoking macro %s argument %d: "
1841 "empty macro arguments are undefined"
1842 " in ISO C90",
1843 NODE_NAME (node), src->val.macro_arg.arg_no);
1845 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
1846 && ! CPP_OPTION (pfile, cplusplus)
1847 && ! macro->syshdr && ! cpp_in_system_header (pfile))
1848 cpp_warning (pfile, CPP_W_C90_C99_COMPAT,
1849 "invoking macro %s argument %d: "
1850 "empty macro arguments are undefined"
1851 " in ISO C90",
1852 NODE_NAME (node), src->val.macro_arg.arg_no);
1854 /* Avoid paste on RHS (even case count == 0). */
1855 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
1857 const cpp_token *t = &pfile->avoid_paste;
1858 tokens_buff_add_token (buff, virt_locs,
1859 t, t->src_loc, t->src_loc,
1860 NULL, 0);
1863 /* Add a new paste flag, or remove an unwanted one. */
1864 if (paste_flag)
1866 cpp_token *token = _cpp_temp_token (pfile);
1867 token->type = (*paste_flag)->type;
1868 token->val = (*paste_flag)->val;
1869 if (src->flags & PASTE_LEFT)
1870 token->flags = (*paste_flag)->flags | PASTE_LEFT;
1871 else
1872 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
1873 *paste_flag = token;
1876 i += arg_tokens_count;
1879 if (track_macro_exp)
1880 push_extended_tokens_context (pfile, node, buff, virt_locs, first,
1881 tokens_buff_count (buff));
1882 else
1883 push_ptoken_context (pfile, node, buff, first,
1884 tokens_buff_count (buff));
1886 num_macro_tokens_counter += tokens_buff_count (buff);
1889 /* Return a special padding token, with padding inherited from SOURCE. */
1890 static const cpp_token *
1891 padding_token (cpp_reader *pfile, const cpp_token *source)
1893 cpp_token *result = _cpp_temp_token (pfile);
1895 result->type = CPP_PADDING;
1897 /* Data in GCed data structures cannot be made const so far, so we
1898 need a cast here. */
1899 result->val.source = (cpp_token *) source;
1900 result->flags = 0;
1901 return result;
1904 /* Get a new uninitialized context. Create a new one if we cannot
1905 re-use an old one. */
1906 static cpp_context *
1907 next_context (cpp_reader *pfile)
1909 cpp_context *result = pfile->context->next;
1911 if (result == 0)
1913 result = XNEW (cpp_context);
1914 memset (result, 0, sizeof (cpp_context));
1915 result->prev = pfile->context;
1916 result->next = 0;
1917 pfile->context->next = result;
1920 pfile->context = result;
1921 return result;
1924 /* Push a list of pointers to tokens. */
1925 static void
1926 push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
1927 const cpp_token **first, unsigned int count)
1929 cpp_context *context = next_context (pfile);
1931 context->tokens_kind = TOKENS_KIND_INDIRECT;
1932 context->c.macro = macro;
1933 context->buff = buff;
1934 FIRST (context).ptoken = first;
1935 LAST (context).ptoken = first + count;
1938 /* Push a list of tokens.
1940 A NULL macro means that we should continue the current macro
1941 expansion, in essence. That means that if we are currently in a
1942 macro expansion context, we'll make the new pfile->context refer to
1943 the current macro. */
1944 void
1945 _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
1946 const cpp_token *first, unsigned int count)
1948 cpp_context *context;
1950 if (macro == NULL)
1951 macro = macro_of_context (pfile->context);
1953 context = next_context (pfile);
1954 context->tokens_kind = TOKENS_KIND_DIRECT;
1955 context->c.macro = macro;
1956 context->buff = NULL;
1957 FIRST (context).token = first;
1958 LAST (context).token = first + count;
1961 /* Build a context containing a list of tokens as well as their
1962 virtual locations and push it. TOKENS_BUFF is the buffer that
1963 contains the tokens pointed to by FIRST. If TOKENS_BUFF is
1964 non-NULL, it means that the context owns it, meaning that
1965 _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
1966 contains the virtual locations.
1968 A NULL macro means that we should continue the current macro
1969 expansion, in essence. That means that if we are currently in a
1970 macro expansion context, we'll make the new pfile->context refer to
1971 the current macro. */
1972 static void
1973 push_extended_tokens_context (cpp_reader *pfile,
1974 cpp_hashnode *macro,
1975 _cpp_buff *token_buff,
1976 source_location *virt_locs,
1977 const cpp_token **first,
1978 unsigned int count)
1980 cpp_context *context;
1981 macro_context *m;
1983 if (macro == NULL)
1984 macro = macro_of_context (pfile->context);
1986 context = next_context (pfile);
1987 context->tokens_kind = TOKENS_KIND_EXTENDED;
1988 context->buff = token_buff;
1990 m = XNEW (macro_context);
1991 m->macro_node = macro;
1992 m->virt_locs = virt_locs;
1993 m->cur_virt_loc = virt_locs;
1994 context->c.mc = m;
1995 FIRST (context).ptoken = first;
1996 LAST (context).ptoken = first + count;
1999 /* Push a traditional macro's replacement text. */
2000 void
2001 _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
2002 const uchar *start, size_t len)
2004 cpp_context *context = next_context (pfile);
2006 context->tokens_kind = TOKENS_KIND_DIRECT;
2007 context->c.macro = macro;
2008 context->buff = NULL;
2009 CUR (context) = start;
2010 RLIMIT (context) = start + len;
2011 macro->flags |= NODE_DISABLED;
2014 /* Creates a buffer that holds tokens a.k.a "token buffer", usually
2015 for the purpose of storing them on a cpp_context. If VIRT_LOCS is
2016 non-null (which means that -ftrack-macro-expansion is on),
2017 *VIRT_LOCS is set to a newly allocated buffer that is supposed to
2018 hold the virtual locations of the tokens resulting from macro
2019 expansion. */
2020 static _cpp_buff*
2021 tokens_buff_new (cpp_reader *pfile, size_t len,
2022 source_location **virt_locs)
2024 size_t tokens_size = len * sizeof (cpp_token *);
2025 size_t locs_size = len * sizeof (source_location);
2027 if (virt_locs != NULL)
2028 *virt_locs = XNEWVEC (source_location, locs_size);
2029 return _cpp_get_buff (pfile, tokens_size);
2032 /* Returns the number of tokens contained in a token buffer. The
2033 buffer holds a set of cpp_token*. */
2034 static size_t
2035 tokens_buff_count (_cpp_buff *buff)
2037 return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *);
2040 /* Return a pointer to the last token contained in the token buffer
2041 BUFF. */
2042 static const cpp_token **
2043 tokens_buff_last_token_ptr (_cpp_buff *buff)
2045 return &((const cpp_token **) BUFF_FRONT (buff))[-1];
2048 /* Remove the last token contained in the token buffer TOKENS_BUFF.
2049 If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer
2050 containing the virtual locations of the tokens in TOKENS_BUFF; in
2051 which case the function updates that buffer as well. */
2052 static inline void
2053 tokens_buff_remove_last_token (_cpp_buff *tokens_buff)
2056 if (BUFF_FRONT (tokens_buff) > tokens_buff->base)
2057 BUFF_FRONT (tokens_buff) =
2058 (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1];
2061 /* Insert a token into the token buffer at the position pointed to by
2062 DEST. Note that the buffer is not enlarged so the previous token
2063 that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null,
2064 means -ftrack-macro-expansion is effect; it then points to where to
2065 insert the virtual location of TOKEN. TOKEN is the token to
2066 insert. VIRT_LOC is the virtual location of the token, i.e, the
2067 location possibly encoding its locus across macro expansion. If
2068 TOKEN is an argument of a function-like macro (inside a macro
2069 replacement list), PARM_DEF_LOC is the spelling location of the
2070 macro parameter that TOKEN is replacing, in the replacement list of
2071 the macro. If TOKEN is not an argument of a function-like macro or
2072 if it doesn't come from a macro expansion, then VIRT_LOC can just
2073 be set to the same value as PARM_DEF_LOC. If MAP is non null, it
2074 means TOKEN comes from a macro expansion and MAP is the macro map
2075 associated to the macro. MACRO_TOKEN_INDEX points to the index of
2076 the token in the macro map; it is not considered if MAP is NULL.
2078 Upon successful completion this function returns the a pointer to
2079 the position of the token coming right after the insertion
2080 point. */
2081 static inline const cpp_token **
2082 tokens_buff_put_token_to (const cpp_token **dest,
2083 source_location *virt_loc_dest,
2084 const cpp_token *token,
2085 source_location virt_loc,
2086 source_location parm_def_loc,
2087 const line_map_macro *map,
2088 unsigned int macro_token_index)
2090 source_location macro_loc = virt_loc;
2091 const cpp_token **result;
2093 if (virt_loc_dest)
2095 /* -ftrack-macro-expansion is on. */
2096 if (map)
2097 macro_loc = linemap_add_macro_token (map, macro_token_index,
2098 virt_loc, parm_def_loc);
2099 *virt_loc_dest = macro_loc;
2101 *dest = token;
2102 result = &dest[1];
2104 return result;
2107 /* Adds a token at the end of the tokens contained in BUFFER. Note
2108 that this function doesn't enlarge BUFFER when the number of tokens
2109 reaches BUFFER's size; it aborts in that situation.
2111 TOKEN is the token to append. VIRT_LOC is the virtual location of
2112 the token, i.e, the location possibly encoding its locus across
2113 macro expansion. If TOKEN is an argument of a function-like macro
2114 (inside a macro replacement list), PARM_DEF_LOC is the location of
2115 the macro parameter that TOKEN is replacing. If TOKEN doesn't come
2116 from a macro expansion, then VIRT_LOC can just be set to the same
2117 value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes
2118 from a macro expansion and MAP is the macro map associated to the
2119 macro. MACRO_TOKEN_INDEX points to the index of the token in the
2120 macro map; It is not considered if MAP is NULL. If VIRT_LOCS is
2121 non-null, it means -ftrack-macro-expansion is on; in which case
2122 this function adds the virtual location DEF_LOC to the VIRT_LOCS
2123 array, at the same index as the one of TOKEN in BUFFER. Upon
2124 successful completion this function returns the a pointer to the
2125 position of the token coming right after the insertion point. */
2126 static const cpp_token **
2127 tokens_buff_add_token (_cpp_buff *buffer,
2128 source_location *virt_locs,
2129 const cpp_token *token,
2130 source_location virt_loc,
2131 source_location parm_def_loc,
2132 const line_map_macro *map,
2133 unsigned int macro_token_index)
2135 const cpp_token **result;
2136 source_location *virt_loc_dest = NULL;
2137 unsigned token_index =
2138 (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
2140 /* Abort if we pass the end the buffer. */
2141 if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer))
2142 abort ();
2144 if (virt_locs != NULL)
2145 virt_loc_dest = &virt_locs[token_index];
2147 result =
2148 tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer),
2149 virt_loc_dest, token, virt_loc, parm_def_loc,
2150 map, macro_token_index);
2152 BUFF_FRONT (buffer) = (unsigned char *) result;
2153 return result;
2156 /* Allocate space for the function-like macro argument ARG to store
2157 the tokens resulting from the macro-expansion of the tokens that
2158 make up ARG itself. That space is allocated in ARG->expanded and
2159 needs to be freed using free. */
2160 static void
2161 alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity)
2163 gcc_checking_assert (arg->expanded == NULL
2164 && arg->expanded_virt_locs == NULL);
2166 arg->expanded = XNEWVEC (const cpp_token *, capacity);
2167 if (CPP_OPTION (pfile, track_macro_expansion))
2168 arg->expanded_virt_locs = XNEWVEC (source_location, capacity);
2172 /* If necessary, enlarge ARG->expanded to so that it can contain SIZE
2173 tokens. */
2174 static void
2175 ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg,
2176 size_t size, size_t *expanded_capacity)
2178 if (size <= *expanded_capacity)
2179 return;
2181 size *= 2;
2183 arg->expanded =
2184 XRESIZEVEC (const cpp_token *, arg->expanded, size);
2185 *expanded_capacity = size;
2187 if (CPP_OPTION (pfile, track_macro_expansion))
2189 if (arg->expanded_virt_locs == NULL)
2190 arg->expanded_virt_locs = XNEWVEC (source_location, size);
2191 else
2192 arg->expanded_virt_locs = XRESIZEVEC (source_location,
2193 arg->expanded_virt_locs,
2194 size);
2198 /* Expand an argument ARG before replacing parameters in a
2199 function-like macro. This works by pushing a context with the
2200 argument's tokens, and then expanding that into a temporary buffer
2201 as if it were a normal part of the token stream. collect_args()
2202 has terminated the argument's tokens with a CPP_EOF so that we know
2203 when we have fully expanded the argument. */
2204 static void
2205 expand_arg (cpp_reader *pfile, macro_arg *arg)
2207 size_t capacity;
2208 bool saved_warn_trad;
2209 bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
2211 if (arg->count == 0
2212 || arg->expanded != NULL)
2213 return;
2215 /* Don't warn about funlike macros when pre-expanding. */
2216 saved_warn_trad = CPP_WTRADITIONAL (pfile);
2217 CPP_WTRADITIONAL (pfile) = 0;
2219 /* Loop, reading in the tokens of the argument. */
2220 capacity = 256;
2221 alloc_expanded_arg_mem (pfile, arg, capacity);
2223 if (track_macro_exp_p)
2224 push_extended_tokens_context (pfile, NULL, NULL,
2225 arg->virt_locs,
2226 arg->first,
2227 arg->count + 1);
2228 else
2229 push_ptoken_context (pfile, NULL, NULL,
2230 arg->first, arg->count + 1);
2232 for (;;)
2234 const cpp_token *token;
2235 source_location location;
2237 ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1,
2238 &capacity);
2240 token = cpp_get_token_1 (pfile, &location);
2242 if (token->type == CPP_EOF)
2243 break;
2245 set_arg_token (arg, token, location,
2246 arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED,
2247 CPP_OPTION (pfile, track_macro_expansion));
2248 arg->expanded_count++;
2251 _cpp_pop_context (pfile);
2253 CPP_WTRADITIONAL (pfile) = saved_warn_trad;
2256 /* Returns the macro associated to the current context if we are in
2257 the context a macro expansion, NULL otherwise. */
2258 static cpp_hashnode*
2259 macro_of_context (cpp_context *context)
2261 if (context == NULL)
2262 return NULL;
2264 return (context->tokens_kind == TOKENS_KIND_EXTENDED)
2265 ? context->c.mc->macro_node
2266 : context->c.macro;
2269 /* Return TRUE iff we are expanding a macro or are about to start
2270 expanding one. If we are effectively expanding a macro, the
2271 function macro_of_context returns a pointer to the macro being
2272 expanded. */
2273 static bool
2274 in_macro_expansion_p (cpp_reader *pfile)
2276 if (pfile == NULL)
2277 return false;
2279 return (pfile->about_to_expand_macro_p
2280 || macro_of_context (pfile->context));
2283 /* Pop the current context off the stack, re-enabling the macro if the
2284 context represented a macro's replacement list. Initially the
2285 context structure was not freed so that we can re-use it later, but
2286 now we do free it to reduce peak memory consumption. */
2287 void
2288 _cpp_pop_context (cpp_reader *pfile)
2290 cpp_context *context = pfile->context;
2292 /* We should not be popping the base context. */
2293 if (context == &pfile->base_context)
2294 abort ();
2296 if (context->c.macro)
2298 cpp_hashnode *macro;
2299 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
2301 macro_context *mc = context->c.mc;
2302 macro = mc->macro_node;
2303 /* If context->buff is set, it means the life time of tokens
2304 is bound to the life time of this context; so we must
2305 free the tokens; that means we must free the virtual
2306 locations of these tokens too. */
2307 if (context->buff && mc->virt_locs)
2309 free (mc->virt_locs);
2310 mc->virt_locs = NULL;
2312 free (mc);
2313 context->c.mc = NULL;
2315 else
2316 macro = context->c.macro;
2318 /* Beware that MACRO can be NULL in cases like when we are
2319 called from expand_arg. In those cases, a dummy context with
2320 tokens is pushed just for the purpose of walking them using
2321 cpp_get_token_1. In that case, no 'macro' field is set into
2322 the dummy context. */
2323 if (macro != NULL
2324 /* Several contiguous macro expansion contexts can be
2325 associated to the same macro; that means it's the same
2326 macro expansion that spans across all these (sub)
2327 contexts. So we should re-enable an expansion-disabled
2328 macro only when we are sure we are really out of that
2329 macro expansion. */
2330 && macro_of_context (context->prev) != macro)
2331 macro->flags &= ~NODE_DISABLED;
2333 if (macro == pfile->top_most_macro_node && context->prev == NULL)
2334 /* We are popping the context of the top-most macro node. */
2335 pfile->top_most_macro_node = NULL;
2338 if (context->buff)
2340 /* Decrease memory peak consumption by freeing the memory used
2341 by the context. */
2342 _cpp_free_buff (context->buff);
2345 pfile->context = context->prev;
2346 /* decrease peak memory consumption by feeing the context. */
2347 pfile->context->next = NULL;
2348 free (context);
2351 /* Return TRUE if we reached the end of the set of tokens stored in
2352 CONTEXT, FALSE otherwise. */
2353 static inline bool
2354 reached_end_of_context (cpp_context *context)
2356 if (context->tokens_kind == TOKENS_KIND_DIRECT)
2357 return FIRST (context).token == LAST (context).token;
2358 else if (context->tokens_kind == TOKENS_KIND_INDIRECT
2359 || context->tokens_kind == TOKENS_KIND_EXTENDED)
2360 return FIRST (context).ptoken == LAST (context).ptoken;
2361 else
2362 abort ();
2365 /* Consume the next token contained in the current context of PFILE,
2366 and return it in *TOKEN. It's "full location" is returned in
2367 *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
2368 means the location encoding the locus of the token across macro
2369 expansion; otherwise it's just is the "normal" location of the
2370 token which (*TOKEN)->src_loc. */
2371 static inline void
2372 consume_next_token_from_context (cpp_reader *pfile,
2373 const cpp_token ** token,
2374 source_location *location)
2376 cpp_context *c = pfile->context;
2378 if ((c)->tokens_kind == TOKENS_KIND_DIRECT)
2380 *token = FIRST (c).token;
2381 *location = (*token)->src_loc;
2382 FIRST (c).token++;
2384 else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT)
2386 *token = *FIRST (c).ptoken;
2387 *location = (*token)->src_loc;
2388 FIRST (c).ptoken++;
2390 else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED)
2392 macro_context *m = c->c.mc;
2393 *token = *FIRST (c).ptoken;
2394 if (m->virt_locs)
2396 *location = *m->cur_virt_loc;
2397 m->cur_virt_loc++;
2399 else
2400 *location = (*token)->src_loc;
2401 FIRST (c).ptoken++;
2403 else
2404 abort ();
2407 /* In the traditional mode of the preprocessor, if we are currently in
2408 a directive, the location of a token must be the location of the
2409 start of the directive line. This function returns the proper
2410 location if we are in the traditional mode, and just returns
2411 LOCATION otherwise. */
2413 static inline source_location
2414 maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, source_location location)
2416 if (CPP_OPTION (pfile, traditional))
2418 if (pfile->state.in_directive)
2419 return pfile->directive_line;
2421 return location;
2424 /* Routine to get a token as well as its location.
2426 Macro expansions and directives are transparently handled,
2427 including entering included files. Thus tokens are post-macro
2428 expansion, and after any intervening directives. External callers
2429 see CPP_EOF only at EOF. Internal callers also see it when meeting
2430 a directive inside a macro call, when at the end of a directive and
2431 state.in_directive is still 1, and at the end of argument
2432 pre-expansion.
2434 LOC is an out parameter; *LOC is set to the location "as expected
2435 by the user". Please read the comment of
2436 cpp_get_token_with_location to learn more about the meaning of this
2437 location. */
2438 static const cpp_token*
2439 cpp_get_token_1 (cpp_reader *pfile, source_location *location)
2441 const cpp_token *result;
2442 /* This token is a virtual token that either encodes a location
2443 related to macro expansion or a spelling location. */
2444 source_location virt_loc = 0;
2445 /* pfile->about_to_expand_macro_p can be overriden by indirect calls
2446 to functions that push macro contexts. So let's save it so that
2447 we can restore it when we are about to leave this routine. */
2448 bool saved_about_to_expand_macro = pfile->about_to_expand_macro_p;
2450 for (;;)
2452 cpp_hashnode *node;
2453 cpp_context *context = pfile->context;
2455 /* Context->prev == 0 <=> base context. */
2456 if (!context->prev)
2458 result = _cpp_lex_token (pfile);
2459 virt_loc = result->src_loc;
2461 else if (!reached_end_of_context (context))
2463 consume_next_token_from_context (pfile, &result,
2464 &virt_loc);
2465 if (result->flags & PASTE_LEFT)
2467 paste_all_tokens (pfile, result);
2468 if (pfile->state.in_directive)
2469 continue;
2470 result = padding_token (pfile, result);
2471 goto out;
2474 else
2476 if (pfile->context->c.macro)
2477 ++num_expanded_macros_counter;
2478 _cpp_pop_context (pfile);
2479 if (pfile->state.in_directive)
2480 continue;
2481 result = &pfile->avoid_paste;
2482 goto out;
2485 if (pfile->state.in_directive && result->type == CPP_COMMENT)
2486 continue;
2488 if (result->type != CPP_NAME)
2489 break;
2491 node = result->val.node.node;
2493 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
2494 break;
2496 if (!(node->flags & NODE_DISABLED))
2498 int ret = 0;
2499 /* If not in a macro context, and we're going to start an
2500 expansion, record the location and the top level macro
2501 about to be expanded. */
2502 if (!in_macro_expansion_p (pfile))
2504 pfile->invocation_location = result->src_loc;
2505 pfile->top_most_macro_node = node;
2507 if (pfile->state.prevent_expansion)
2508 break;
2510 /* Conditional macros require that a predicate be evaluated
2511 first. */
2512 if ((node->flags & NODE_CONDITIONAL) != 0)
2514 if (pfile->cb.macro_to_expand)
2516 bool whitespace_after;
2517 const cpp_token *peek_tok = cpp_peek_token (pfile, 0);
2519 whitespace_after = (peek_tok->type == CPP_PADDING
2520 || (peek_tok->flags & PREV_WHITE));
2521 node = pfile->cb.macro_to_expand (pfile, result);
2522 if (node)
2523 ret = enter_macro_context (pfile, node, result,
2524 virt_loc);
2525 else if (whitespace_after)
2527 /* If macro_to_expand hook returned NULL and it
2528 ate some tokens, see if we don't need to add
2529 a padding token in between this and the
2530 next token. */
2531 peek_tok = cpp_peek_token (pfile, 0);
2532 if (peek_tok->type != CPP_PADDING
2533 && (peek_tok->flags & PREV_WHITE) == 0)
2534 _cpp_push_token_context (pfile, NULL,
2535 padding_token (pfile,
2536 peek_tok), 1);
2540 else
2541 ret = enter_macro_context (pfile, node, result,
2542 virt_loc);
2543 if (ret)
2545 if (pfile->state.in_directive || ret == 2)
2546 continue;
2547 result = padding_token (pfile, result);
2548 goto out;
2551 else
2553 /* Flag this token as always unexpandable. FIXME: move this
2554 to collect_args()?. */
2555 cpp_token *t = _cpp_temp_token (pfile);
2556 t->type = result->type;
2557 t->flags = result->flags | NO_EXPAND;
2558 t->val = result->val;
2559 result = t;
2562 break;
2565 out:
2566 if (location != NULL)
2568 if (virt_loc == 0)
2569 virt_loc = result->src_loc;
2570 *location = virt_loc;
2572 if (!CPP_OPTION (pfile, track_macro_expansion)
2573 && macro_of_context (pfile->context) != NULL)
2574 /* We are in a macro expansion context, are not tracking
2575 virtual location, but were asked to report the location
2576 of the expansion point of the macro being expanded. */
2577 *location = pfile->invocation_location;
2579 *location = maybe_adjust_loc_for_trad_cpp (pfile, *location);
2582 pfile->about_to_expand_macro_p = saved_about_to_expand_macro;
2583 return result;
2586 /* External routine to get a token. Also used nearly everywhere
2587 internally, except for places where we know we can safely call
2588 _cpp_lex_token directly, such as lexing a directive name.
2590 Macro expansions and directives are transparently handled,
2591 including entering included files. Thus tokens are post-macro
2592 expansion, and after any intervening directives. External callers
2593 see CPP_EOF only at EOF. Internal callers also see it when meeting
2594 a directive inside a macro call, when at the end of a directive and
2595 state.in_directive is still 1, and at the end of argument
2596 pre-expansion. */
2597 const cpp_token *
2598 cpp_get_token (cpp_reader *pfile)
2600 return cpp_get_token_1 (pfile, NULL);
2603 /* Like cpp_get_token, but also returns a virtual token location
2604 separate from the spelling location carried by the returned token.
2606 LOC is an out parameter; *LOC is set to the location "as expected
2607 by the user". This matters when a token results from macro
2608 expansion; in that case the token's spelling location indicates the
2609 locus of the token in the definition of the macro but *LOC
2610 virtually encodes all the other meaningful locuses associated to
2611 the token.
2613 What? virtual location? Yes, virtual location.
2615 If the token results from macro expansion and if macro expansion
2616 location tracking is enabled its virtual location encodes (at the
2617 same time):
2619 - the spelling location of the token
2621 - the locus of the macro expansion point
2623 - the locus of the point where the token got instantiated as part
2624 of the macro expansion process.
2626 You have to use the linemap API to get the locus you are interested
2627 in from a given virtual location.
2629 Note however that virtual locations are not necessarily ordered for
2630 relations '<' and '>'. One must use the function
2631 linemap_location_before_p instead of using the relational operator
2632 '<'.
2634 If macro expansion tracking is off and if the token results from
2635 macro expansion the virtual location is the expansion point of the
2636 macro that got expanded.
2638 When the token doesn't result from macro expansion, the virtual
2639 location is just the same thing as its spelling location. */
2641 const cpp_token *
2642 cpp_get_token_with_location (cpp_reader *pfile, source_location *loc)
2644 return cpp_get_token_1 (pfile, loc);
2647 /* Returns true if we're expanding an object-like macro that was
2648 defined in a system header. Just checks the macro at the top of
2649 the stack. Used for diagnostic suppression. */
2651 cpp_sys_macro_p (cpp_reader *pfile)
2653 cpp_hashnode *node = NULL;
2655 if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2656 node = pfile->context->c.mc->macro_node;
2657 else
2658 node = pfile->context->c.macro;
2660 return node && node->value.macro && node->value.macro->syshdr;
2663 /* Read each token in, until end of the current file. Directives are
2664 transparently processed. */
2665 void
2666 cpp_scan_nooutput (cpp_reader *pfile)
2668 /* Request a CPP_EOF token at the end of this file, rather than
2669 transparently continuing with the including file. */
2670 pfile->buffer->return_at_eof = true;
2672 pfile->state.discarding_output++;
2673 pfile->state.prevent_expansion++;
2675 if (CPP_OPTION (pfile, traditional))
2676 while (_cpp_read_logical_line_trad (pfile))
2678 else
2679 while (cpp_get_token (pfile)->type != CPP_EOF)
2682 pfile->state.discarding_output--;
2683 pfile->state.prevent_expansion--;
2686 /* Step back one or more tokens obtained from the lexer. */
2687 void
2688 _cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count)
2690 pfile->lookaheads += count;
2691 while (count--)
2693 pfile->cur_token--;
2694 if (pfile->cur_token == pfile->cur_run->base
2695 /* Possible with -fpreprocessed and no leading #line. */
2696 && pfile->cur_run->prev != NULL)
2698 pfile->cur_run = pfile->cur_run->prev;
2699 pfile->cur_token = pfile->cur_run->limit;
2704 /* Step back one (or more) tokens. Can only step back more than 1 if
2705 they are from the lexer, and not from macro expansion. */
2706 void
2707 _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
2709 if (pfile->context->prev == NULL)
2710 _cpp_backup_tokens_direct (pfile, count);
2711 else
2713 if (count != 1)
2714 abort ();
2715 if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT)
2716 FIRST (pfile->context).token--;
2717 else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT)
2718 FIRST (pfile->context).ptoken--;
2719 else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2721 FIRST (pfile->context).ptoken--;
2722 if (pfile->context->c.macro)
2724 macro_context *m = pfile->context->c.mc;
2725 m->cur_virt_loc--;
2726 gcc_checking_assert (m->cur_virt_loc >= m->virt_locs);
2728 else
2729 abort ();
2731 else
2732 abort ();
2736 /* #define directive parsing and handling. */
2738 /* Returns nonzero if a macro redefinition warning is required. */
2739 static bool
2740 warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
2741 const cpp_macro *macro2)
2743 const cpp_macro *macro1;
2744 unsigned int i;
2746 /* Some redefinitions need to be warned about regardless. */
2747 if (node->flags & NODE_WARN)
2748 return true;
2750 /* Suppress warnings for builtins that lack the NODE_WARN flag,
2751 unless Wbuiltin-macro-redefined. */
2752 if (node->flags & NODE_BUILTIN
2753 && (!pfile->cb.user_builtin_macro
2754 || !pfile->cb.user_builtin_macro (pfile, node)))
2755 return CPP_OPTION (pfile, warn_builtin_macro_redefined);
2757 /* Redefinitions of conditional (context-sensitive) macros, on
2758 the other hand, must be allowed silently. */
2759 if (node->flags & NODE_CONDITIONAL)
2760 return false;
2762 /* Redefinition of a macro is allowed if and only if the old and new
2763 definitions are the same. (6.10.3 paragraph 2). */
2764 macro1 = node->value.macro;
2766 /* Don't check count here as it can be different in valid
2767 traditional redefinitions with just whitespace differences. */
2768 if (macro1->paramc != macro2->paramc
2769 || macro1->fun_like != macro2->fun_like
2770 || macro1->variadic != macro2->variadic)
2771 return true;
2773 /* Check parameter spellings. */
2774 for (i = 0; i < macro1->paramc; i++)
2775 if (macro1->params[i] != macro2->params[i])
2776 return true;
2778 /* Check the replacement text or tokens. */
2779 if (CPP_OPTION (pfile, traditional))
2780 return _cpp_expansions_different_trad (macro1, macro2);
2782 if (macro1->count != macro2->count)
2783 return true;
2785 for (i = 0; i < macro1->count; i++)
2786 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
2787 return true;
2789 return false;
2792 /* Free the definition of hashnode H. */
2793 void
2794 _cpp_free_definition (cpp_hashnode *h)
2796 /* Macros and assertions no longer have anything to free. */
2797 h->type = NT_VOID;
2798 /* Clear builtin flag in case of redefinition. */
2799 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED);
2802 /* Save parameter NODE (spelling SPELLING) to the parameter list of
2803 macro MACRO. Returns zero on success, nonzero if the parameter is
2804 a duplicate. */
2805 bool
2806 _cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node,
2807 cpp_hashnode *spelling)
2809 unsigned int len;
2810 /* Constraint 6.10.3.6 - duplicate parameter names. */
2811 if (node->flags & NODE_MACRO_ARG)
2813 cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
2814 NODE_NAME (node));
2815 return true;
2818 if (BUFF_ROOM (pfile->a_buff)
2819 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
2820 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
2822 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = spelling;
2823 node->flags |= NODE_MACRO_ARG;
2824 len = macro->paramc * sizeof (struct macro_arg_saved_data);
2825 if (len > pfile->macro_buffer_len)
2827 pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer,
2828 len);
2829 pfile->macro_buffer_len = len;
2831 struct macro_arg_saved_data save;
2832 save.value = node->value;
2833 save.canonical_node = node;
2834 ((struct macro_arg_saved_data *) pfile->macro_buffer)[macro->paramc - 1]
2835 = save;
2837 node->value.arg_index = macro->paramc;
2838 return false;
2841 /* Check the syntax of the parameters in a MACRO definition. Returns
2842 false if an error occurs. */
2843 static bool
2844 parse_params (cpp_reader *pfile, cpp_macro *macro)
2846 unsigned int prev_ident = 0;
2848 for (;;)
2850 const cpp_token *token = _cpp_lex_token (pfile);
2852 switch (token->type)
2854 default:
2855 /* Allow/ignore comments in parameter lists if we are
2856 preserving comments in macro expansions. */
2857 if (token->type == CPP_COMMENT
2858 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
2859 continue;
2861 cpp_error (pfile, CPP_DL_ERROR,
2862 "\"%s\" may not appear in macro parameter list",
2863 cpp_token_as_text (pfile, token));
2864 return false;
2866 case CPP_NAME:
2867 if (prev_ident)
2869 cpp_error (pfile, CPP_DL_ERROR,
2870 "macro parameters must be comma-separated");
2871 return false;
2873 prev_ident = 1;
2875 if (_cpp_save_parameter (pfile, macro, token->val.node.node,
2876 token->val.node.spelling))
2877 return false;
2878 continue;
2880 case CPP_CLOSE_PAREN:
2881 if (prev_ident || macro->paramc == 0)
2882 return true;
2884 /* Fall through to pick up the error. */
2885 case CPP_COMMA:
2886 if (!prev_ident)
2888 cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
2889 return false;
2891 prev_ident = 0;
2892 continue;
2894 case CPP_ELLIPSIS:
2895 macro->variadic = 1;
2896 if (!prev_ident)
2898 _cpp_save_parameter (pfile, macro,
2899 pfile->spec_nodes.n__VA_ARGS__,
2900 pfile->spec_nodes.n__VA_ARGS__);
2901 pfile->state.va_args_ok = 1;
2902 if (! CPP_OPTION (pfile, c99)
2903 && CPP_OPTION (pfile, cpp_pedantic)
2904 && CPP_OPTION (pfile, warn_variadic_macros))
2906 if (CPP_OPTION (pfile, cplusplus))
2907 cpp_pedwarning
2908 (pfile, CPP_W_VARIADIC_MACROS,
2909 "anonymous variadic macros were introduced in C++11");
2910 else
2911 cpp_pedwarning
2912 (pfile, CPP_W_VARIADIC_MACROS,
2913 "anonymous variadic macros were introduced in C99");
2915 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
2916 && ! CPP_OPTION (pfile, cplusplus))
2917 cpp_error (pfile, CPP_DL_WARNING,
2918 "anonymous variadic macros were introduced in C99");
2920 else if (CPP_OPTION (pfile, cpp_pedantic)
2921 && CPP_OPTION (pfile, warn_variadic_macros))
2923 if (CPP_OPTION (pfile, cplusplus))
2924 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
2925 "ISO C++ does not permit named variadic macros");
2926 else
2927 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
2928 "ISO C does not permit named variadic macros");
2931 /* We're at the end, and just expect a closing parenthesis. */
2932 token = _cpp_lex_token (pfile);
2933 if (token->type == CPP_CLOSE_PAREN)
2934 return true;
2935 /* Fall through. */
2937 case CPP_EOF:
2938 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
2939 return false;
2944 /* Allocate room for a token from a macro's replacement list. */
2945 static cpp_token *
2946 alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
2948 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
2949 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
2951 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
2954 /* Lex a token from the expansion of MACRO, but mark parameters as we
2955 find them and warn of traditional stringification. */
2956 static cpp_token *
2957 lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
2959 cpp_token *token, *saved_cur_token;
2961 saved_cur_token = pfile->cur_token;
2962 pfile->cur_token = alloc_expansion_token (pfile, macro);
2963 token = _cpp_lex_direct (pfile);
2964 pfile->cur_token = saved_cur_token;
2966 /* Is this a parameter? */
2967 if (token->type == CPP_NAME
2968 && (token->val.node.node->flags & NODE_MACRO_ARG) != 0)
2970 cpp_hashnode *spelling = token->val.node.spelling;
2971 token->type = CPP_MACRO_ARG;
2972 token->val.macro_arg.arg_no = token->val.node.node->value.arg_index;
2973 token->val.macro_arg.spelling = spelling;
2975 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
2976 && (token->type == CPP_STRING || token->type == CPP_CHAR))
2977 check_trad_stringification (pfile, macro, &token->val.str);
2979 return token;
2982 static bool
2983 create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
2985 cpp_token *token;
2986 const cpp_token *ctoken;
2987 bool following_paste_op = false;
2988 const char *paste_op_error_msg =
2989 N_("'##' cannot appear at either end of a macro expansion");
2990 unsigned int num_extra_tokens = 0;
2992 /* Get the first token of the expansion (or the '(' of a
2993 function-like macro). */
2994 ctoken = _cpp_lex_token (pfile);
2996 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
2998 bool ok = parse_params (pfile, macro);
2999 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
3000 if (!ok)
3001 return false;
3003 /* Success. Commit or allocate the parameter array. */
3004 if (pfile->hash_table->alloc_subobject)
3006 cpp_hashnode **params =
3007 (cpp_hashnode **) pfile->hash_table->alloc_subobject
3008 (sizeof (cpp_hashnode *) * macro->paramc);
3009 memcpy (params, macro->params,
3010 sizeof (cpp_hashnode *) * macro->paramc);
3011 macro->params = params;
3013 else
3014 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
3015 macro->fun_like = 1;
3017 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
3019 /* While ISO C99 requires whitespace before replacement text
3020 in a macro definition, ISO C90 with TC1 allows characters
3021 from the basic source character set there. */
3022 if (CPP_OPTION (pfile, c99))
3024 if (CPP_OPTION (pfile, cplusplus))
3025 cpp_error (pfile, CPP_DL_PEDWARN,
3026 "ISO C++11 requires whitespace after the macro name");
3027 else
3028 cpp_error (pfile, CPP_DL_PEDWARN,
3029 "ISO C99 requires whitespace after the macro name");
3031 else
3033 int warntype = CPP_DL_WARNING;
3034 switch (ctoken->type)
3036 case CPP_ATSIGN:
3037 case CPP_AT_NAME:
3038 case CPP_OBJC_STRING:
3039 /* '@' is not in basic character set. */
3040 warntype = CPP_DL_PEDWARN;
3041 break;
3042 case CPP_OTHER:
3043 /* Basic character set sans letters, digits and _. */
3044 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
3045 ctoken->val.str.text[0]) == NULL)
3046 warntype = CPP_DL_PEDWARN;
3047 break;
3048 default:
3049 /* All other tokens start with a character from basic
3050 character set. */
3051 break;
3053 cpp_error (pfile, warntype,
3054 "missing whitespace after the macro name");
3058 if (macro->fun_like)
3059 token = lex_expansion_token (pfile, macro);
3060 else
3062 token = alloc_expansion_token (pfile, macro);
3063 *token = *ctoken;
3066 for (;;)
3068 /* Check the stringifying # constraint 6.10.3.2.1 of
3069 function-like macros when lexing the subsequent token. */
3070 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
3072 if (token->type == CPP_MACRO_ARG)
3074 if (token->flags & PREV_WHITE)
3075 token->flags |= SP_PREV_WHITE;
3076 if (token[-1].flags & DIGRAPH)
3077 token->flags |= SP_DIGRAPH;
3078 token->flags &= ~PREV_WHITE;
3079 token->flags |= STRINGIFY_ARG;
3080 token->flags |= token[-1].flags & PREV_WHITE;
3081 token[-1] = token[0];
3082 macro->count--;
3084 /* Let assembler get away with murder. */
3085 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
3087 cpp_error (pfile, CPP_DL_ERROR,
3088 "'#' is not followed by a macro parameter");
3089 return false;
3093 if (token->type == CPP_EOF)
3095 /* Paste operator constraint 6.10.3.3.1:
3096 Token-paste ##, can appear in both object-like and
3097 function-like macros, but not at the end. */
3098 if (following_paste_op)
3100 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
3101 return false;
3103 break;
3106 /* Paste operator constraint 6.10.3.3.1. */
3107 if (token->type == CPP_PASTE)
3109 /* Token-paste ##, can appear in both object-like and
3110 function-like macros, but not at the beginning. */
3111 if (macro->count == 1)
3113 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
3114 return false;
3117 if (token[-1].flags & PASTE_LEFT)
3119 macro->extra_tokens = 1;
3120 num_extra_tokens++;
3121 token->val.token_no = macro->count - 1;
3123 else
3125 --macro->count;
3126 token[-1].flags |= PASTE_LEFT;
3127 if (token->flags & DIGRAPH)
3128 token[-1].flags |= SP_DIGRAPH;
3129 if (token->flags & PREV_WHITE)
3130 token[-1].flags |= SP_PREV_WHITE;
3134 following_paste_op = (token->type == CPP_PASTE);
3135 token = lex_expansion_token (pfile, macro);
3138 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
3139 macro->traditional = 0;
3141 /* Don't count the CPP_EOF. */
3142 macro->count--;
3144 /* Clear whitespace on first token for warn_of_redefinition(). */
3145 if (macro->count)
3146 macro->exp.tokens[0].flags &= ~PREV_WHITE;
3148 /* Commit or allocate the memory. */
3149 if (pfile->hash_table->alloc_subobject)
3151 cpp_token *tokns =
3152 (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token)
3153 * macro->count);
3154 if (num_extra_tokens)
3156 /* Place second and subsequent ## or %:%: tokens in
3157 sequences of consecutive such tokens at the end of the
3158 list to preserve information about where they appear, how
3159 they are spelt and whether they are preceded by
3160 whitespace without otherwise interfering with macro
3161 expansion. */
3162 cpp_token *normal_dest = tokns;
3163 cpp_token *extra_dest = tokns + macro->count - num_extra_tokens;
3164 unsigned int i;
3165 for (i = 0; i < macro->count; i++)
3167 if (macro->exp.tokens[i].type == CPP_PASTE)
3168 *extra_dest++ = macro->exp.tokens[i];
3169 else
3170 *normal_dest++ = macro->exp.tokens[i];
3173 else
3174 memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
3175 macro->exp.tokens = tokns;
3177 else
3178 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
3180 return true;
3183 /* Parse a macro and save its expansion. Returns nonzero on success. */
3184 bool
3185 _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
3187 cpp_macro *macro;
3188 unsigned int i;
3189 bool ok;
3191 if (pfile->hash_table->alloc_subobject)
3192 macro = (cpp_macro *) pfile->hash_table->alloc_subobject
3193 (sizeof (cpp_macro));
3194 else
3195 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
3196 macro->line = pfile->directive_line;
3197 macro->params = 0;
3198 macro->paramc = 0;
3199 macro->variadic = 0;
3200 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
3201 macro->count = 0;
3202 macro->fun_like = 0;
3203 macro->extra_tokens = 0;
3204 /* To suppress some diagnostics. */
3205 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
3207 if (CPP_OPTION (pfile, traditional))
3208 ok = _cpp_create_trad_definition (pfile, macro);
3209 else
3211 ok = create_iso_definition (pfile, macro);
3213 /* We set the type for SEEN_EOL() in directives.c.
3215 Longer term we should lex the whole line before coming here,
3216 and just copy the expansion. */
3218 /* Stop the lexer accepting __VA_ARGS__. */
3219 pfile->state.va_args_ok = 0;
3222 /* Clear the fast argument lookup indices. */
3223 for (i = macro->paramc; i-- > 0; )
3225 struct macro_arg_saved_data *save =
3226 &((struct macro_arg_saved_data *) pfile->macro_buffer)[i];
3227 struct cpp_hashnode *node = save->canonical_node;
3228 node->flags &= ~ NODE_MACRO_ARG;
3229 node->value = save->value;
3232 if (!ok)
3233 return ok;
3235 if (node->type == NT_MACRO)
3237 if (CPP_OPTION (pfile, warn_unused_macros))
3238 _cpp_warn_if_unused_macro (pfile, node, NULL);
3240 if (warn_of_redefinition (pfile, node, macro))
3242 const int reason = ((node->flags & NODE_BUILTIN)
3243 && !(node->flags & NODE_WARN))
3244 ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
3246 bool warned =
3247 cpp_pedwarning_with_line (pfile, reason,
3248 pfile->directive_line, 0,
3249 "\"%s\" redefined", NODE_NAME (node));
3251 if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
3252 cpp_error_with_line (pfile, CPP_DL_NOTE,
3253 node->value.macro->line, 0,
3254 "this is the location of the previous definition");
3258 if (node->type != NT_VOID)
3259 _cpp_free_definition (node);
3261 /* Enter definition in hash table. */
3262 node->type = NT_MACRO;
3263 node->value.macro = macro;
3264 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))
3265 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS")
3266 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
3267 in the C standard, as something that one must use in C++.
3268 However DR#593 and C++11 indicate that they play no role in C++.
3269 We special-case them anyway. */
3270 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS")
3271 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS"))
3272 node->flags |= NODE_WARN;
3274 /* If user defines one of the conditional macros, remove the
3275 conditional flag */
3276 node->flags &= ~NODE_CONDITIONAL;
3278 return ok;
3281 /* Warn if a token in STRING matches one of a function-like MACRO's
3282 parameters. */
3283 static void
3284 check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
3285 const cpp_string *string)
3287 unsigned int i, len;
3288 const uchar *p, *q, *limit;
3290 /* Loop over the string. */
3291 limit = string->text + string->len - 1;
3292 for (p = string->text + 1; p < limit; p = q)
3294 /* Find the start of an identifier. */
3295 while (p < limit && !is_idstart (*p))
3296 p++;
3298 /* Find the end of the identifier. */
3299 q = p;
3300 while (q < limit && is_idchar (*q))
3301 q++;
3303 len = q - p;
3305 /* Loop over the function macro arguments to see if the
3306 identifier inside the string matches one of them. */
3307 for (i = 0; i < macro->paramc; i++)
3309 const cpp_hashnode *node = macro->params[i];
3311 if (NODE_LEN (node) == len
3312 && !memcmp (p, NODE_NAME (node), len))
3314 cpp_error (pfile, CPP_DL_WARNING,
3315 "macro argument \"%s\" would be stringified in traditional C",
3316 NODE_NAME (node));
3317 break;
3323 /* Returns true of NODE is a function-like macro. */
3324 bool
3325 cpp_fun_like_macro_p (cpp_hashnode *node)
3327 return (node->type == NT_MACRO
3328 && (node->flags & (NODE_BUILTIN | NODE_MACRO_ARG)) == 0
3329 && node->value.macro->fun_like);
3332 /* Returns the name, arguments and expansion of a macro, in a format
3333 suitable to be read back in again, and therefore also for DWARF 2
3334 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
3335 Caller is expected to generate the "#define" bit if needed. The
3336 returned text is temporary, and automatically freed later. */
3337 const unsigned char *
3338 cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
3340 unsigned int i, len;
3341 const cpp_macro *macro;
3342 unsigned char *buffer;
3344 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
3346 if (node->type != NT_MACRO
3347 || !pfile->cb.user_builtin_macro
3348 || !pfile->cb.user_builtin_macro (pfile, node))
3350 cpp_error (pfile, CPP_DL_ICE,
3351 "invalid hash type %d in cpp_macro_definition",
3352 node->type);
3353 return 0;
3357 macro = node->value.macro;
3358 /* Calculate length. */
3359 len = NODE_LEN (node) * 10 + 2; /* ' ' and NUL. */
3360 if (macro->fun_like)
3362 len += 4; /* "()" plus possible final ".." of named
3363 varargs (we have + 1 below). */
3364 for (i = 0; i < macro->paramc; i++)
3365 len += NODE_LEN (macro->params[i]) + 1; /* "," */
3368 /* This should match below where we fill in the buffer. */
3369 if (CPP_OPTION (pfile, traditional))
3370 len += _cpp_replacement_text_len (macro);
3371 else
3373 unsigned int count = macro_real_token_count (macro);
3374 for (i = 0; i < count; i++)
3376 cpp_token *token = &macro->exp.tokens[i];
3378 if (token->type == CPP_MACRO_ARG)
3379 len += NODE_LEN (token->val.macro_arg.spelling);
3380 else
3381 len += cpp_token_len (token);
3383 if (token->flags & STRINGIFY_ARG)
3384 len++; /* "#" */
3385 if (token->flags & PASTE_LEFT)
3386 len += 3; /* " ##" */
3387 if (token->flags & PREV_WHITE)
3388 len++; /* " " */
3392 if (len > pfile->macro_buffer_len)
3394 pfile->macro_buffer = XRESIZEVEC (unsigned char,
3395 pfile->macro_buffer, len);
3396 pfile->macro_buffer_len = len;
3399 /* Fill in the buffer. Start with the macro name. */
3400 buffer = pfile->macro_buffer;
3401 buffer = _cpp_spell_ident_ucns (buffer, node);
3403 /* Parameter names. */
3404 if (macro->fun_like)
3406 *buffer++ = '(';
3407 for (i = 0; i < macro->paramc; i++)
3409 cpp_hashnode *param = macro->params[i];
3411 if (param != pfile->spec_nodes.n__VA_ARGS__)
3413 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
3414 buffer += NODE_LEN (param);
3417 if (i + 1 < macro->paramc)
3418 /* Don't emit a space after the comma here; we're trying
3419 to emit a Dwarf-friendly definition, and the Dwarf spec
3420 forbids spaces in the argument list. */
3421 *buffer++ = ',';
3422 else if (macro->variadic)
3423 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
3425 *buffer++ = ')';
3428 /* The Dwarf spec requires a space after the macro name, even if the
3429 definition is the empty string. */
3430 *buffer++ = ' ';
3432 if (CPP_OPTION (pfile, traditional))
3433 buffer = _cpp_copy_replacement_text (macro, buffer);
3434 else if (macro->count)
3435 /* Expansion tokens. */
3437 unsigned int count = macro_real_token_count (macro);
3438 for (i = 0; i < count; i++)
3440 cpp_token *token = &macro->exp.tokens[i];
3442 if (token->flags & PREV_WHITE)
3443 *buffer++ = ' ';
3444 if (token->flags & STRINGIFY_ARG)
3445 *buffer++ = '#';
3447 if (token->type == CPP_MACRO_ARG)
3449 memcpy (buffer,
3450 NODE_NAME (token->val.macro_arg.spelling),
3451 NODE_LEN (token->val.macro_arg.spelling));
3452 buffer += NODE_LEN (token->val.macro_arg.spelling);
3454 else
3455 buffer = cpp_spell_token (pfile, token, buffer, true);
3457 if (token->flags & PASTE_LEFT)
3459 *buffer++ = ' ';
3460 *buffer++ = '#';
3461 *buffer++ = '#';
3462 /* Next has PREV_WHITE; see _cpp_create_definition. */
3467 *buffer = '\0';
3468 return pfile->macro_buffer;