* gimple-ssa-store-merging.c (struct store_immediate_info): Add
[official-gcc.git] / libcpp / macro.c
blobfab1cb051dc78569c3adf2661b22c043d8ed09c3
1 /* Part of CPP library. (Macro and #define handling.)
2 Copyright (C) 1986-2017 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding! */
25 #include "config.h"
26 #include "system.h"
27 #include "cpplib.h"
28 #include "internal.h"
30 typedef struct macro_arg macro_arg;
31 /* This structure represents the tokens of a macro argument. These
32 tokens can be macro themselves, in which case they can be either
33 expanded or unexpanded. When they are expanded, this data
34 structure keeps both the expanded and unexpanded forms. */
35 struct macro_arg
37 const cpp_token **first; /* First token in unexpanded argument. */
38 const cpp_token **expanded; /* Macro-expanded argument. */
39 const cpp_token *stringified; /* Stringified argument. */
40 unsigned int count; /* # of tokens in argument. */
41 unsigned int expanded_count; /* # of tokens in expanded argument. */
42 source_location *virt_locs; /* Where virtual locations for
43 unexpanded tokens are stored. */
44 source_location *expanded_virt_locs; /* Where virtual locations for
45 expanded tokens are
46 stored. */
49 /* The kind of macro tokens which the instance of
50 macro_arg_token_iter is supposed to iterate over. */
51 enum macro_arg_token_kind {
52 MACRO_ARG_TOKEN_NORMAL,
53 /* This is a macro argument token that got transformed into a string
54 litteral, e.g. #foo. */
55 MACRO_ARG_TOKEN_STRINGIFIED,
56 /* This is a token resulting from the expansion of a macro
57 argument that was itself a macro. */
58 MACRO_ARG_TOKEN_EXPANDED
61 /* An iterator over tokens coming from a function-like macro
62 argument. */
63 typedef struct macro_arg_token_iter macro_arg_token_iter;
64 struct macro_arg_token_iter
66 /* Whether or not -ftrack-macro-expansion is used. */
67 bool track_macro_exp_p;
68 /* The kind of token over which we are supposed to iterate. */
69 enum macro_arg_token_kind kind;
70 /* A pointer to the current token pointed to by the iterator. */
71 const cpp_token **token_ptr;
72 /* A pointer to the "full" location of the current token. If
73 -ftrack-macro-expansion is used this location tracks loci across
74 macro expansion. */
75 const source_location *location_ptr;
76 #if CHECKING_P
77 /* The number of times the iterator went forward. This useful only
78 when checking is enabled. */
79 size_t num_forwards;
80 #endif
83 /* Saved data about an identifier being used as a macro argument
84 name. */
85 struct macro_arg_saved_data {
86 /* The canonical (UTF-8) spelling of this identifier. */
87 cpp_hashnode *canonical_node;
88 /* The previous value of this identifier. */
89 union _cpp_hashnode_value value;
92 /* Macro expansion. */
94 static int enter_macro_context (cpp_reader *, cpp_hashnode *,
95 const cpp_token *, source_location);
96 static int builtin_macro (cpp_reader *, cpp_hashnode *,
97 source_location, source_location);
98 static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
99 const cpp_token **, unsigned int);
100 static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *,
101 _cpp_buff *, source_location *,
102 const cpp_token **, unsigned int);
103 static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *,
104 _cpp_buff **, unsigned *);
105 static cpp_context *next_context (cpp_reader *);
106 static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
107 static void expand_arg (cpp_reader *, macro_arg *);
108 static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
109 static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
110 static void paste_all_tokens (cpp_reader *, const cpp_token *);
111 static bool paste_tokens (cpp_reader *, source_location,
112 const cpp_token **, const cpp_token *);
113 static void alloc_expanded_arg_mem (cpp_reader *, macro_arg *, size_t);
114 static void ensure_expanded_arg_room (cpp_reader *, macro_arg *, size_t, size_t *);
115 static void delete_macro_args (_cpp_buff*, unsigned num_args);
116 static void set_arg_token (macro_arg *, const cpp_token *,
117 source_location, size_t,
118 enum macro_arg_token_kind,
119 bool);
120 static const source_location *get_arg_token_location (const macro_arg *,
121 enum macro_arg_token_kind);
122 static const cpp_token **arg_token_ptr_at (const macro_arg *,
123 size_t,
124 enum macro_arg_token_kind,
125 source_location **virt_location);
127 static void macro_arg_token_iter_init (macro_arg_token_iter *, bool,
128 enum macro_arg_token_kind,
129 const macro_arg *,
130 const cpp_token **);
131 static const cpp_token *macro_arg_token_iter_get_token
132 (const macro_arg_token_iter *it);
133 static source_location macro_arg_token_iter_get_location
134 (const macro_arg_token_iter *);
135 static void macro_arg_token_iter_forward (macro_arg_token_iter *);
136 static _cpp_buff *tokens_buff_new (cpp_reader *, size_t,
137 source_location **);
138 static size_t tokens_buff_count (_cpp_buff *);
139 static const cpp_token **tokens_buff_last_token_ptr (_cpp_buff *);
140 static inline const cpp_token **tokens_buff_put_token_to (const cpp_token **,
141 source_location *,
142 const cpp_token *,
143 source_location,
144 source_location,
145 const line_map_macro *,
146 unsigned int);
148 static const cpp_token **tokens_buff_add_token (_cpp_buff *,
149 source_location *,
150 const cpp_token *,
151 source_location,
152 source_location,
153 const line_map_macro *,
154 unsigned int);
155 static inline void tokens_buff_remove_last_token (_cpp_buff *);
156 static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
157 macro_arg *, source_location);
158 static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *,
159 _cpp_buff **, unsigned *);
160 static bool create_iso_definition (cpp_reader *, cpp_macro *);
162 /* #define directive parsing and handling. */
164 static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *);
165 static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *);
166 static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *,
167 const cpp_macro *);
168 static bool parse_params (cpp_reader *, cpp_macro *);
169 static void check_trad_stringification (cpp_reader *, const cpp_macro *,
170 const cpp_string *);
171 static bool reached_end_of_context (cpp_context *);
172 static void consume_next_token_from_context (cpp_reader *pfile,
173 const cpp_token **,
174 source_location *);
175 static const cpp_token* cpp_get_token_1 (cpp_reader *, source_location *);
177 static cpp_hashnode* macro_of_context (cpp_context *context);
179 static bool in_macro_expansion_p (cpp_reader *pfile);
181 /* Statistical counter tracking the number of macros that got
182 expanded. */
183 unsigned num_expanded_macros_counter = 0;
184 /* Statistical counter tracking the total number tokens resulting
185 from macro expansion. */
186 unsigned num_macro_tokens_counter = 0;
188 /* Emits a warning if NODE is a macro defined in the main file that
189 has not been used. */
191 _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
192 void *v ATTRIBUTE_UNUSED)
194 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
196 cpp_macro *macro = node->value.macro;
198 if (!macro->used
199 && MAIN_FILE_P (linemap_check_ordinary
200 (linemap_lookup (pfile->line_table,
201 macro->line))))
202 cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0,
203 "macro \"%s\" is not used", NODE_NAME (node));
206 return 1;
209 /* Allocates and returns a CPP_STRING token, containing TEXT of length
210 LEN, after null-terminating it. TEXT must be in permanent storage. */
211 static const cpp_token *
212 new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len)
214 cpp_token *token = _cpp_temp_token (pfile);
216 text[len] = '\0';
217 token->type = CPP_STRING;
218 token->val.str.len = len;
219 token->val.str.text = text;
220 token->flags = 0;
221 return token;
224 static const char * const monthnames[] =
226 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
227 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
230 /* Helper function for builtin_macro. Returns the text generated by
231 a builtin macro. */
232 const uchar *
233 _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node,
234 source_location loc)
236 const uchar *result = NULL;
237 linenum_type number = 1;
239 switch (node->value.builtin)
241 default:
242 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
243 NODE_NAME (node));
244 break;
246 case BT_TIMESTAMP:
248 if (CPP_OPTION (pfile, warn_date_time))
249 cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent "
250 "reproducible builds", NODE_NAME (node));
252 cpp_buffer *pbuffer = cpp_get_buffer (pfile);
253 if (pbuffer->timestamp == NULL)
255 /* Initialize timestamp value of the assotiated file. */
256 struct _cpp_file *file = cpp_get_file (pbuffer);
257 if (file)
259 /* Generate __TIMESTAMP__ string, that represents
260 the date and time of the last modification
261 of the current source file. The string constant
262 looks like "Sun Sep 16 01:03:52 1973". */
263 struct tm *tb = NULL;
264 struct stat *st = _cpp_get_file_stat (file);
265 if (st)
266 tb = localtime (&st->st_mtime);
267 if (tb)
269 char *str = asctime (tb);
270 size_t len = strlen (str);
271 unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2);
272 buf[0] = '"';
273 strcpy ((char *) buf + 1, str);
274 buf[len] = '"';
275 pbuffer->timestamp = buf;
277 else
279 cpp_errno (pfile, CPP_DL_WARNING,
280 "could not determine file timestamp");
281 pbuffer->timestamp = UC"\"??? ??? ?? ??:??:?? ????\"";
285 result = pbuffer->timestamp;
287 break;
288 case BT_FILE:
289 case BT_BASE_FILE:
291 unsigned int len;
292 const char *name;
293 uchar *buf;
295 if (node->value.builtin == BT_FILE)
296 name = linemap_get_expansion_filename (pfile->line_table,
297 pfile->line_table->highest_line);
298 else
300 name = _cpp_get_file_name (pfile->main_file);
301 if (!name)
302 abort ();
304 len = strlen (name);
305 buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
306 result = buf;
307 *buf = '"';
308 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
309 *buf++ = '"';
310 *buf = '\0';
312 break;
314 case BT_INCLUDE_LEVEL:
315 /* The line map depth counts the primary source as level 1, but
316 historically __INCLUDE_DEPTH__ has called the primary source
317 level 0. */
318 number = pfile->line_table->depth - 1;
319 break;
321 case BT_SPECLINE:
322 /* If __LINE__ is embedded in a macro, it must expand to the
323 line of the macro's invocation, not its definition.
324 Otherwise things like assert() will not work properly.
325 See WG14 N1911, WG21 N4220 sec 6.5, and PR 61861. */
326 if (CPP_OPTION (pfile, traditional))
327 loc = pfile->line_table->highest_line;
328 else
329 loc = linemap_resolve_location (pfile->line_table, loc,
330 LRK_MACRO_EXPANSION_POINT, NULL);
331 number = linemap_get_expansion_line (pfile->line_table, loc);
332 break;
334 /* __STDC__ has the value 1 under normal circumstances.
335 However, if (a) we are in a system header, (b) the option
336 stdc_0_in_system_headers is true (set by target config), and
337 (c) we are not in strictly conforming mode, then it has the
338 value 0. (b) and (c) are already checked in cpp_init_builtins. */
339 case BT_STDC:
340 if (cpp_in_system_header (pfile))
341 number = 0;
342 else
343 number = 1;
344 break;
346 case BT_DATE:
347 case BT_TIME:
348 if (CPP_OPTION (pfile, warn_date_time))
349 cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent "
350 "reproducible builds", NODE_NAME (node));
351 if (pfile->date == NULL)
353 /* Allocate __DATE__ and __TIME__ strings from permanent
354 storage. We only do this once, and don't generate them
355 at init time, because time() and localtime() are very
356 slow on some systems. */
357 time_t tt;
358 struct tm *tb = NULL;
360 /* Set a reproducible timestamp for __DATE__ and __TIME__ macro
361 if SOURCE_DATE_EPOCH is defined. */
362 if (pfile->source_date_epoch == (time_t) -2
363 && pfile->cb.get_source_date_epoch != NULL)
364 pfile->source_date_epoch = pfile->cb.get_source_date_epoch (pfile);
366 if (pfile->source_date_epoch >= (time_t) 0)
367 tb = gmtime (&pfile->source_date_epoch);
368 else
370 /* (time_t) -1 is a legitimate value for "number of seconds
371 since the Epoch", so we have to do a little dance to
372 distinguish that from a genuine error. */
373 errno = 0;
374 tt = time (NULL);
375 if (tt != (time_t)-1 || errno == 0)
376 tb = localtime (&tt);
379 if (tb)
381 pfile->date = _cpp_unaligned_alloc (pfile,
382 sizeof ("\"Oct 11 1347\""));
383 sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
384 monthnames[tb->tm_mon], tb->tm_mday,
385 tb->tm_year + 1900);
387 pfile->time = _cpp_unaligned_alloc (pfile,
388 sizeof ("\"12:34:56\""));
389 sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
390 tb->tm_hour, tb->tm_min, tb->tm_sec);
392 else
394 cpp_errno (pfile, CPP_DL_WARNING,
395 "could not determine date and time");
397 pfile->date = UC"\"??? ?? ????\"";
398 pfile->time = UC"\"??:??:??\"";
402 if (node->value.builtin == BT_DATE)
403 result = pfile->date;
404 else
405 result = pfile->time;
406 break;
408 case BT_COUNTER:
409 if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive)
410 cpp_error (pfile, CPP_DL_ERROR,
411 "__COUNTER__ expanded inside directive with -fdirectives-only");
412 number = pfile->counter++;
413 break;
415 case BT_HAS_ATTRIBUTE:
416 number = pfile->cb.has_attribute (pfile);
417 break;
420 if (result == NULL)
422 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
423 result = _cpp_unaligned_alloc (pfile, 21);
424 sprintf ((char *) result, "%u", number);
427 return result;
430 /* Convert builtin macros like __FILE__ to a token and push it on the
431 context stack. Also handles _Pragma, for which a new token may not
432 be created. Returns 1 if it generates a new token context, 0 to
433 return the token to the caller. LOC is the location of the expansion
434 point of the macro. */
435 static int
436 builtin_macro (cpp_reader *pfile, cpp_hashnode *node,
437 source_location loc, source_location expand_loc)
439 const uchar *buf;
440 size_t len;
441 char *nbuf;
443 if (node->value.builtin == BT_PRAGMA)
445 /* Don't interpret _Pragma within directives. The standard is
446 not clear on this, but to me this makes most sense. */
447 if (pfile->state.in_directive)
448 return 0;
450 return _cpp_do__Pragma (pfile, loc);
453 buf = _cpp_builtin_macro_text (pfile, node, expand_loc);
454 len = ustrlen (buf);
455 nbuf = (char *) alloca (len + 1);
456 memcpy (nbuf, buf, len);
457 nbuf[len]='\n';
459 cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
460 _cpp_clean_line (pfile);
462 /* Set pfile->cur_token as required by _cpp_lex_direct. */
463 pfile->cur_token = _cpp_temp_token (pfile);
464 cpp_token *token = _cpp_lex_direct (pfile);
465 /* We should point to the expansion point of the builtin macro. */
466 token->src_loc = loc;
467 if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
469 /* We are tracking tokens resulting from macro expansion.
470 Create a macro line map and generate a virtual location for
471 the token resulting from the expansion of the built-in
472 macro. */
473 source_location *virt_locs = NULL;
474 _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
475 const line_map_macro * map =
476 linemap_enter_macro (pfile->line_table, node, loc, 1);
477 tokens_buff_add_token (token_buf, virt_locs, token,
478 pfile->line_table->builtin_location,
479 pfile->line_table->builtin_location,
480 map, /*macro_token_index=*/0);
481 push_extended_tokens_context (pfile, node, token_buf, virt_locs,
482 (const cpp_token **)token_buf->base,
485 else
486 _cpp_push_token_context (pfile, NULL, token, 1);
487 if (pfile->buffer->cur != pfile->buffer->rlimit)
488 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
489 NODE_NAME (node));
490 _cpp_pop_buffer (pfile);
492 return 1;
495 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
496 backslashes and double quotes. DEST must be of sufficient size.
497 Returns a pointer to the end of the string. */
498 uchar *
499 cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
501 while (len--)
503 uchar c = *src++;
505 switch (c)
507 case '\n':
508 /* Naked LF can appear in raw string literals */
509 c = 'n';
510 /* FALLTHROUGH */
512 case '\\':
513 case '"':
514 *dest++ = '\\';
515 /* FALLTHROUGH */
517 default:
518 *dest++ = c;
522 return dest;
525 /* Convert a token sequence ARG to a single string token according to
526 the rules of the ISO C #-operator. */
527 static const cpp_token *
528 stringify_arg (cpp_reader *pfile, macro_arg *arg)
530 unsigned char *dest;
531 unsigned int i, escape_it, backslash_count = 0;
532 const cpp_token *source = NULL;
533 size_t len;
535 if (BUFF_ROOM (pfile->u_buff) < 3)
536 _cpp_extend_buff (pfile, &pfile->u_buff, 3);
537 dest = BUFF_FRONT (pfile->u_buff);
538 *dest++ = '"';
540 /* Loop, reading in the argument's tokens. */
541 for (i = 0; i < arg->count; i++)
543 const cpp_token *token = arg->first[i];
545 if (token->type == CPP_PADDING)
547 if (source == NULL
548 || (!(source->flags & PREV_WHITE)
549 && token->val.source == NULL))
550 source = token->val.source;
551 continue;
554 escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR
555 || token->type == CPP_WSTRING || token->type == CPP_WCHAR
556 || token->type == CPP_STRING32 || token->type == CPP_CHAR32
557 || token->type == CPP_STRING16 || token->type == CPP_CHAR16
558 || token->type == CPP_UTF8STRING || token->type == CPP_UTF8CHAR
559 || cpp_userdef_string_p (token->type)
560 || cpp_userdef_char_p (token->type));
562 /* Room for each char being written in octal, initial space and
563 final quote and NUL. */
564 len = cpp_token_len (token);
565 if (escape_it)
566 len *= 4;
567 len += 3;
569 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
571 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
572 _cpp_extend_buff (pfile, &pfile->u_buff, len);
573 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
576 /* Leading white space? */
577 if (dest - 1 != BUFF_FRONT (pfile->u_buff))
579 if (source == NULL)
580 source = token;
581 if (source->flags & PREV_WHITE)
582 *dest++ = ' ';
584 source = NULL;
586 if (escape_it)
588 _cpp_buff *buff = _cpp_get_buff (pfile, len);
589 unsigned char *buf = BUFF_FRONT (buff);
590 len = cpp_spell_token (pfile, token, buf, true) - buf;
591 dest = cpp_quote_string (dest, buf, len);
592 _cpp_release_buff (pfile, buff);
594 else
595 dest = cpp_spell_token (pfile, token, dest, true);
597 if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
598 backslash_count++;
599 else
600 backslash_count = 0;
603 /* Ignore the final \ of invalid string literals. */
604 if (backslash_count & 1)
606 cpp_error (pfile, CPP_DL_WARNING,
607 "invalid string literal, ignoring final '\\'");
608 dest--;
611 /* Commit the memory, including NUL, and return the token. */
612 *dest++ = '"';
613 len = dest - BUFF_FRONT (pfile->u_buff);
614 BUFF_FRONT (pfile->u_buff) = dest + 1;
615 return new_string_token (pfile, dest - len, len);
618 /* Try to paste two tokens. On success, return nonzero. In any
619 case, PLHS is updated to point to the pasted token, which is
620 guaranteed to not have the PASTE_LEFT flag set. LOCATION is
621 the virtual location used for error reporting. */
622 static bool
623 paste_tokens (cpp_reader *pfile, source_location location,
624 const cpp_token **plhs, const cpp_token *rhs)
626 unsigned char *buf, *end, *lhsend;
627 cpp_token *lhs;
628 unsigned int len;
630 len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1;
631 buf = (unsigned char *) alloca (len);
632 end = lhsend = cpp_spell_token (pfile, *plhs, buf, true);
634 /* Avoid comment headers, since they are still processed in stage 3.
635 It is simpler to insert a space here, rather than modifying the
636 lexer to ignore comments in some circumstances. Simply returning
637 false doesn't work, since we want to clear the PASTE_LEFT flag. */
638 if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ)
639 *end++ = ' ';
640 /* In one obscure case we might see padding here. */
641 if (rhs->type != CPP_PADDING)
642 end = cpp_spell_token (pfile, rhs, end, true);
643 *end = '\n';
645 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
646 _cpp_clean_line (pfile);
648 /* Set pfile->cur_token as required by _cpp_lex_direct. */
649 pfile->cur_token = _cpp_temp_token (pfile);
650 lhs = _cpp_lex_direct (pfile);
651 if (pfile->buffer->cur != pfile->buffer->rlimit)
653 source_location saved_loc = lhs->src_loc;
655 _cpp_pop_buffer (pfile);
656 _cpp_backup_tokens (pfile, 1);
657 *lhsend = '\0';
659 /* We have to remove the PASTE_LEFT flag from the old lhs, but
660 we want to keep the new location. */
661 *lhs = **plhs;
662 *plhs = lhs;
663 lhs->src_loc = saved_loc;
664 lhs->flags &= ~PASTE_LEFT;
666 /* Mandatory error for all apart from assembler. */
667 if (CPP_OPTION (pfile, lang) != CLK_ASM)
668 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
669 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
670 buf, cpp_token_as_text (pfile, rhs));
671 return false;
674 *plhs = lhs;
675 _cpp_pop_buffer (pfile);
676 return true;
679 /* Handles an arbitrarily long sequence of ## operators, with initial
680 operand LHS. This implementation is left-associative,
681 non-recursive, and finishes a paste before handling succeeding
682 ones. If a paste fails, we back up to the RHS of the failing ##
683 operator before pushing the context containing the result of prior
684 successful pastes, with the effect that the RHS appears in the
685 output stream after the pasted LHS normally. */
686 static void
687 paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
689 const cpp_token *rhs = NULL;
690 cpp_context *context = pfile->context;
691 source_location virt_loc = 0;
693 /* We are expanding a macro and we must have been called on a token
694 that appears at the left hand side of a ## operator. */
695 if (macro_of_context (pfile->context) == NULL
696 || (!(lhs->flags & PASTE_LEFT)))
697 abort ();
699 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
700 /* The caller must have called consume_next_token_from_context
701 right before calling us. That has incremented the pointer to
702 the current virtual location. So it now points to the location
703 of the token that comes right after *LHS. We want the
704 resulting pasted token to have the location of the current
705 *LHS, though. */
706 virt_loc = context->c.mc->cur_virt_loc[-1];
707 else
708 /* We are not tracking macro expansion. So the best virtual
709 location we can get here is the expansion point of the macro we
710 are currently expanding. */
711 virt_loc = pfile->invocation_location;
715 /* Take the token directly from the current context. We can do
716 this, because we are in the replacement list of either an
717 object-like macro, or a function-like macro with arguments
718 inserted. In either case, the constraints to #define
719 guarantee we have at least one more token. */
720 if (context->tokens_kind == TOKENS_KIND_DIRECT)
721 rhs = FIRST (context).token++;
722 else if (context->tokens_kind == TOKENS_KIND_INDIRECT)
723 rhs = *FIRST (context).ptoken++;
724 else if (context->tokens_kind == TOKENS_KIND_EXTENDED)
726 /* So we are in presence of an extended token context, which
727 means that each token in this context has a virtual
728 location attached to it. So let's not forget to update
729 the pointer to the current virtual location of the
730 current token when we update the pointer to the current
731 token */
733 rhs = *FIRST (context).ptoken++;
734 /* context->c.mc must be non-null, as if we were not in a
735 macro context, context->tokens_kind could not be equal to
736 TOKENS_KIND_EXTENDED. */
737 context->c.mc->cur_virt_loc++;
740 if (rhs->type == CPP_PADDING)
742 if (rhs->flags & PASTE_LEFT)
743 abort ();
745 if (!paste_tokens (pfile, virt_loc, &lhs, rhs))
746 break;
748 while (rhs->flags & PASTE_LEFT);
750 /* Put the resulting token in its own context. */
751 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
753 source_location *virt_locs = NULL;
754 _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
755 tokens_buff_add_token (token_buf, virt_locs, lhs,
756 virt_loc, 0, NULL, 0);
757 push_extended_tokens_context (pfile, context->c.mc->macro_node,
758 token_buf, virt_locs,
759 (const cpp_token **)token_buf->base, 1);
761 else
762 _cpp_push_token_context (pfile, NULL, lhs, 1);
765 /* Returns TRUE if the number of arguments ARGC supplied in an
766 invocation of the MACRO referenced by NODE is valid. An empty
767 invocation to a macro with no parameters should pass ARGC as zero.
769 Note that MACRO cannot necessarily be deduced from NODE, in case
770 NODE was redefined whilst collecting arguments. */
771 bool
772 _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
774 if (argc == macro->paramc)
775 return true;
777 if (argc < macro->paramc)
779 /* As an extension, variadic arguments are allowed to not appear in
780 the invocation at all.
781 e.g. #define debug(format, args...) something
782 debug("string");
784 This is exactly the same as if an empty variadic list had been
785 supplied - debug("string", ). */
787 if (argc + 1 == macro->paramc && macro->variadic)
789 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
791 if (CPP_OPTION (pfile, cplusplus))
792 cpp_error (pfile, CPP_DL_PEDWARN,
793 "ISO C++11 requires at least one argument "
794 "for the \"...\" in a variadic macro");
795 else
796 cpp_error (pfile, CPP_DL_PEDWARN,
797 "ISO C99 requires at least one argument "
798 "for the \"...\" in a variadic macro");
800 return true;
803 cpp_error (pfile, CPP_DL_ERROR,
804 "macro \"%s\" requires %u arguments, but only %u given",
805 NODE_NAME (node), macro->paramc, argc);
807 else
808 cpp_error (pfile, CPP_DL_ERROR,
809 "macro \"%s\" passed %u arguments, but takes just %u",
810 NODE_NAME (node), argc, macro->paramc);
812 return false;
815 /* Reads and returns the arguments to a function-like macro
816 invocation. Assumes the opening parenthesis has been processed.
817 If there is an error, emits an appropriate diagnostic and returns
818 NULL. Each argument is terminated by a CPP_EOF token, for the
819 future benefit of expand_arg(). If there are any deferred
820 #pragma directives among macro arguments, store pointers to the
821 CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.
823 What is returned is the buffer that contains the memory allocated
824 to hold the macro arguments. NODE is the name of the macro this
825 function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is
826 set to the actual number of macro arguments allocated in the
827 returned buffer. */
828 static _cpp_buff *
829 collect_args (cpp_reader *pfile, const cpp_hashnode *node,
830 _cpp_buff **pragma_buff, unsigned *num_args)
832 _cpp_buff *buff, *base_buff;
833 cpp_macro *macro;
834 macro_arg *args, *arg;
835 const cpp_token *token;
836 unsigned int argc;
837 source_location virt_loc;
838 bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion);
839 unsigned num_args_alloced = 0;
841 macro = node->value.macro;
842 if (macro->paramc)
843 argc = macro->paramc;
844 else
845 argc = 1;
847 #define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50
848 #define ARG_TOKENS_EXTENT 1000
850 buff = _cpp_get_buff (pfile, argc * (DEFAULT_NUM_TOKENS_PER_MACRO_ARG
851 * sizeof (cpp_token *)
852 + sizeof (macro_arg)));
853 base_buff = buff;
854 args = (macro_arg *) buff->base;
855 memset (args, 0, argc * sizeof (macro_arg));
856 buff->cur = (unsigned char *) &args[argc];
857 arg = args, argc = 0;
859 /* Collect the tokens making up each argument. We don't yet know
860 how many arguments have been supplied, whether too many or too
861 few. Hence the slightly bizarre usage of "argc" and "arg". */
864 unsigned int paren_depth = 0;
865 unsigned int ntokens = 0;
866 unsigned virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
867 num_args_alloced++;
869 argc++;
870 arg->first = (const cpp_token **) buff->cur;
871 if (track_macro_expansion_p)
873 virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
874 arg->virt_locs = XNEWVEC (source_location,
875 virt_locs_capacity);
878 for (;;)
880 /* Require space for 2 new tokens (including a CPP_EOF). */
881 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
883 buff = _cpp_append_extend_buff (pfile, buff,
884 ARG_TOKENS_EXTENT
885 * sizeof (cpp_token *));
886 arg->first = (const cpp_token **) buff->cur;
888 if (track_macro_expansion_p
889 && (ntokens + 2 > virt_locs_capacity))
891 virt_locs_capacity += ARG_TOKENS_EXTENT;
892 arg->virt_locs = XRESIZEVEC (source_location,
893 arg->virt_locs,
894 virt_locs_capacity);
897 token = cpp_get_token_1 (pfile, &virt_loc);
899 if (token->type == CPP_PADDING)
901 /* Drop leading padding. */
902 if (ntokens == 0)
903 continue;
905 else if (token->type == CPP_OPEN_PAREN)
906 paren_depth++;
907 else if (token->type == CPP_CLOSE_PAREN)
909 if (paren_depth-- == 0)
910 break;
912 else if (token->type == CPP_COMMA)
914 /* A comma does not terminate an argument within
915 parentheses or as part of a variable argument. */
916 if (paren_depth == 0
917 && ! (macro->variadic && argc == macro->paramc))
918 break;
920 else if (token->type == CPP_EOF
921 || (token->type == CPP_HASH && token->flags & BOL))
922 break;
923 else if (token->type == CPP_PRAGMA)
925 cpp_token *newtok = _cpp_temp_token (pfile);
927 /* CPP_PRAGMA token lives in directive_result, which will
928 be overwritten on the next directive. */
929 *newtok = *token;
930 token = newtok;
933 if (*pragma_buff == NULL
934 || BUFF_ROOM (*pragma_buff) < sizeof (cpp_token *))
936 _cpp_buff *next;
937 if (*pragma_buff == NULL)
938 *pragma_buff
939 = _cpp_get_buff (pfile, 32 * sizeof (cpp_token *));
940 else
942 next = *pragma_buff;
943 *pragma_buff
944 = _cpp_get_buff (pfile,
945 (BUFF_FRONT (*pragma_buff)
946 - (*pragma_buff)->base) * 2);
947 (*pragma_buff)->next = next;
950 *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token;
951 BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *);
952 if (token->type == CPP_PRAGMA_EOL)
953 break;
954 token = cpp_get_token_1 (pfile, &virt_loc);
956 while (token->type != CPP_EOF);
958 /* In deferred pragmas parsing_args and prevent_expansion
959 had been changed, reset it. */
960 pfile->state.parsing_args = 2;
961 pfile->state.prevent_expansion = 1;
963 if (token->type == CPP_EOF)
964 break;
965 else
966 continue;
968 set_arg_token (arg, token, virt_loc,
969 ntokens, MACRO_ARG_TOKEN_NORMAL,
970 CPP_OPTION (pfile, track_macro_expansion));
971 ntokens++;
974 /* Drop trailing padding. */
975 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
976 ntokens--;
978 arg->count = ntokens;
979 set_arg_token (arg, &pfile->eof, pfile->eof.src_loc,
980 ntokens, MACRO_ARG_TOKEN_NORMAL,
981 CPP_OPTION (pfile, track_macro_expansion));
983 /* Terminate the argument. Excess arguments loop back and
984 overwrite the final legitimate argument, before failing. */
985 if (argc <= macro->paramc)
987 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
988 if (argc != macro->paramc)
989 arg++;
992 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
994 if (token->type == CPP_EOF)
996 /* We still need the CPP_EOF to end directives, and to end
997 pre-expansion of a macro argument. Step back is not
998 unconditional, since we don't want to return a CPP_EOF to our
999 callers at the end of an -include-d file. */
1000 if (pfile->context->prev || pfile->state.in_directive)
1001 _cpp_backup_tokens (pfile, 1);
1002 cpp_error (pfile, CPP_DL_ERROR,
1003 "unterminated argument list invoking macro \"%s\"",
1004 NODE_NAME (node));
1006 else
1008 /* A single empty argument is counted as no argument. */
1009 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
1010 argc = 0;
1011 if (_cpp_arguments_ok (pfile, macro, node, argc))
1013 /* GCC has special semantics for , ## b where b is a varargs
1014 parameter: we remove the comma if b was omitted entirely.
1015 If b was merely an empty argument, the comma is retained.
1016 If the macro takes just one (varargs) parameter, then we
1017 retain the comma only if we are standards conforming.
1019 If FIRST is NULL replace_args () swallows the comma. */
1020 if (macro->variadic && (argc < macro->paramc
1021 || (argc == 1 && args[0].count == 0
1022 && !CPP_OPTION (pfile, std))))
1023 args[macro->paramc - 1].first = NULL;
1024 if (num_args)
1025 *num_args = num_args_alloced;
1026 return base_buff;
1030 /* An error occurred. */
1031 _cpp_release_buff (pfile, base_buff);
1032 return NULL;
1035 /* Search for an opening parenthesis to the macro of NODE, in such a
1036 way that, if none is found, we don't lose the information in any
1037 intervening padding tokens. If we find the parenthesis, collect
1038 the arguments and return the buffer containing them. PRAGMA_BUFF
1039 argument is the same as in collect_args. If NUM_ARGS is non-NULL,
1040 *NUM_ARGS is set to the number of arguments contained in the
1041 returned buffer. */
1042 static _cpp_buff *
1043 funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node,
1044 _cpp_buff **pragma_buff, unsigned *num_args)
1046 const cpp_token *token, *padding = NULL;
1048 for (;;)
1050 token = cpp_get_token (pfile);
1051 if (token->type != CPP_PADDING)
1052 break;
1053 if (padding == NULL
1054 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
1055 padding = token;
1058 if (token->type == CPP_OPEN_PAREN)
1060 pfile->state.parsing_args = 2;
1061 return collect_args (pfile, node, pragma_buff, num_args);
1064 /* CPP_EOF can be the end of macro arguments, or the end of the
1065 file. We mustn't back up over the latter. Ugh. */
1066 if (token->type != CPP_EOF || token == &pfile->eof)
1068 /* Back up. We may have skipped padding, in which case backing
1069 up more than one token when expanding macros is in general
1070 too difficult. We re-insert it in its own context. */
1071 _cpp_backup_tokens (pfile, 1);
1072 if (padding)
1073 _cpp_push_token_context (pfile, NULL, padding, 1);
1076 return NULL;
1079 /* Return the real number of tokens in the expansion of MACRO. */
1080 static inline unsigned int
1081 macro_real_token_count (const cpp_macro *macro)
1083 unsigned int i;
1084 if (__builtin_expect (!macro->extra_tokens, true))
1085 return macro->count;
1086 for (i = 0; i < macro->count; i++)
1087 if (macro->exp.tokens[i].type == CPP_PASTE)
1088 return i;
1089 abort ();
1092 /* Push the context of a macro with hash entry NODE onto the context
1093 stack. If we can successfully expand the macro, we push a context
1094 containing its yet-to-be-rescanned replacement list and return one.
1095 If there were additionally any unexpanded deferred #pragma
1096 directives among macro arguments, push another context containing
1097 the pragma tokens before the yet-to-be-rescanned replacement list
1098 and return two. Otherwise, we don't push a context and return
1099 zero. LOCATION is the location of the expansion point of the
1100 macro. */
1101 static int
1102 enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
1103 const cpp_token *result, source_location location)
1105 /* The presence of a macro invalidates a file's controlling macro. */
1106 pfile->mi_valid = false;
1108 pfile->state.angled_headers = false;
1110 /* From here to when we push the context for the macro later down
1111 this function, we need to flag the fact that we are about to
1112 expand a macro. This is useful when -ftrack-macro-expansion is
1113 turned off. In that case, we need to record the location of the
1114 expansion point of the top-most macro we are about to to expand,
1115 into pfile->invocation_location. But we must not record any such
1116 location once the process of expanding the macro starts; that is,
1117 we must not do that recording between now and later down this
1118 function where set this flag to FALSE. */
1119 pfile->about_to_expand_macro_p = true;
1121 if ((node->flags & NODE_BUILTIN) && !(node->flags & NODE_USED))
1123 node->flags |= NODE_USED;
1124 if ((!pfile->cb.user_builtin_macro
1125 || !pfile->cb.user_builtin_macro (pfile, node))
1126 && pfile->cb.used_define)
1127 pfile->cb.used_define (pfile, pfile->directive_line, node);
1130 /* Handle standard macros. */
1131 if (! (node->flags & NODE_BUILTIN))
1133 cpp_macro *macro = node->value.macro;
1134 _cpp_buff *pragma_buff = NULL;
1136 if (macro->fun_like)
1138 _cpp_buff *buff;
1139 unsigned num_args = 0;
1141 pfile->state.prevent_expansion++;
1142 pfile->keep_tokens++;
1143 pfile->state.parsing_args = 1;
1144 buff = funlike_invocation_p (pfile, node, &pragma_buff,
1145 &num_args);
1146 pfile->state.parsing_args = 0;
1147 pfile->keep_tokens--;
1148 pfile->state.prevent_expansion--;
1150 if (buff == NULL)
1152 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
1153 cpp_warning (pfile, CPP_W_TRADITIONAL,
1154 "function-like macro \"%s\" must be used with arguments in traditional C",
1155 NODE_NAME (node));
1157 if (pragma_buff)
1158 _cpp_release_buff (pfile, pragma_buff);
1160 pfile->about_to_expand_macro_p = false;
1161 return 0;
1164 if (macro->paramc > 0)
1165 replace_args (pfile, node, macro,
1166 (macro_arg *) buff->base,
1167 location);
1168 /* Free the memory used by the arguments of this
1169 function-like macro. This memory has been allocated by
1170 funlike_invocation_p and by replace_args. */
1171 delete_macro_args (buff, num_args);
1174 /* Disable the macro within its expansion. */
1175 node->flags |= NODE_DISABLED;
1177 if (!(node->flags & NODE_USED))
1179 node->flags |= NODE_USED;
1180 if (pfile->cb.used_define)
1181 pfile->cb.used_define (pfile, pfile->directive_line, node);
1184 if (pfile->cb.used)
1185 pfile->cb.used (pfile, location, node);
1187 macro->used = 1;
1189 if (macro->paramc == 0)
1191 unsigned tokens_count = macro_real_token_count (macro);
1192 if (CPP_OPTION (pfile, track_macro_expansion))
1194 unsigned int i;
1195 const cpp_token *src = macro->exp.tokens;
1196 const line_map_macro *map;
1197 source_location *virt_locs = NULL;
1198 _cpp_buff *macro_tokens
1199 = tokens_buff_new (pfile, tokens_count, &virt_locs);
1201 /* Create a macro map to record the locations of the
1202 tokens that are involved in the expansion. LOCATION
1203 is the location of the macro expansion point. */
1204 map = linemap_enter_macro (pfile->line_table,
1205 node, location, tokens_count);
1206 for (i = 0; i < tokens_count; ++i)
1208 tokens_buff_add_token (macro_tokens, virt_locs,
1209 src, src->src_loc,
1210 src->src_loc, map, i);
1211 ++src;
1213 push_extended_tokens_context (pfile, node,
1214 macro_tokens,
1215 virt_locs,
1216 (const cpp_token **)
1217 macro_tokens->base,
1218 tokens_count);
1220 else
1221 _cpp_push_token_context (pfile, node, macro->exp.tokens,
1222 tokens_count);
1223 num_macro_tokens_counter += tokens_count;
1226 if (pragma_buff)
1228 if (!pfile->state.in_directive)
1229 _cpp_push_token_context (pfile, NULL,
1230 padding_token (pfile, result), 1);
1233 unsigned tokens_count;
1234 _cpp_buff *tail = pragma_buff->next;
1235 pragma_buff->next = NULL;
1236 tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff)
1237 - (const cpp_token **) pragma_buff->base);
1238 push_ptoken_context (pfile, NULL, pragma_buff,
1239 (const cpp_token **) pragma_buff->base,
1240 tokens_count);
1241 pragma_buff = tail;
1242 if (!CPP_OPTION (pfile, track_macro_expansion))
1243 num_macro_tokens_counter += tokens_count;
1246 while (pragma_buff != NULL);
1247 pfile->about_to_expand_macro_p = false;
1248 return 2;
1251 pfile->about_to_expand_macro_p = false;
1252 return 1;
1255 pfile->about_to_expand_macro_p = false;
1256 /* Handle built-in macros and the _Pragma operator. */
1258 source_location loc, expand_loc;
1260 if (/* The top-level macro invocation that triggered the expansion
1261 we are looking at is with a standard macro ...*/
1262 !(pfile->top_most_macro_node->flags & NODE_BUILTIN)
1263 /* ... and it's a function-like macro invocation. */
1264 && pfile->top_most_macro_node->value.macro->fun_like)
1266 /* Then the location of the end of the macro invocation is the
1267 location of the closing parenthesis. */
1268 loc = pfile->cur_token[-1].src_loc;
1269 expand_loc = loc;
1271 else
1273 /* Otherwise, the location of the end of the macro invocation is
1274 the location of the expansion point of that top-level macro
1275 invocation. */
1276 loc = location;
1277 expand_loc = pfile->invocation_location;
1280 return builtin_macro (pfile, node, loc, expand_loc);
1284 /* De-allocate the memory used by BUFF which is an array of instances
1285 of macro_arg. NUM_ARGS is the number of instances of macro_arg
1286 present in BUFF. */
1287 static void
1288 delete_macro_args (_cpp_buff *buff, unsigned num_args)
1290 macro_arg *macro_args;
1291 unsigned i;
1293 if (buff == NULL)
1294 return;
1296 macro_args = (macro_arg *) buff->base;
1298 /* Walk instances of macro_arg to free their expanded tokens as well
1299 as their macro_arg::virt_locs members. */
1300 for (i = 0; i < num_args; ++i)
1302 if (macro_args[i].expanded)
1304 free (macro_args[i].expanded);
1305 macro_args[i].expanded = NULL;
1307 if (macro_args[i].virt_locs)
1309 free (macro_args[i].virt_locs);
1310 macro_args[i].virt_locs = NULL;
1312 if (macro_args[i].expanded_virt_locs)
1314 free (macro_args[i].expanded_virt_locs);
1315 macro_args[i].expanded_virt_locs = NULL;
1318 _cpp_free_buff (buff);
1321 /* Set the INDEXth token of the macro argument ARG. TOKEN is the token
1322 to set, LOCATION is its virtual location. "Virtual" location means
1323 the location that encodes loci across macro expansion. Otherwise
1324 it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the
1325 argument ARG is supposed to contain. Note that ARG must be
1326 tailored so that it has enough room to contain INDEX + 1 numbers of
1327 tokens, at least. */
1328 static void
1329 set_arg_token (macro_arg *arg, const cpp_token *token,
1330 source_location location, size_t index,
1331 enum macro_arg_token_kind kind,
1332 bool track_macro_exp_p)
1334 const cpp_token **token_ptr;
1335 source_location *loc = NULL;
1337 token_ptr =
1338 arg_token_ptr_at (arg, index, kind,
1339 track_macro_exp_p ? &loc : NULL);
1340 *token_ptr = token;
1342 if (loc != NULL)
1344 /* We can't set the location of a stringified argument
1345 token and we can't set any location if we aren't tracking
1346 macro expansion locations. */
1347 gcc_checking_assert (kind != MACRO_ARG_TOKEN_STRINGIFIED
1348 && track_macro_exp_p);
1349 *loc = location;
1353 /* Get the pointer to the location of the argument token of the
1354 function-like macro argument ARG. This function must be called
1355 only when we -ftrack-macro-expansion is on. */
1356 static const source_location *
1357 get_arg_token_location (const macro_arg *arg,
1358 enum macro_arg_token_kind kind)
1360 const source_location *loc = NULL;
1361 const cpp_token **token_ptr =
1362 arg_token_ptr_at (arg, 0, kind, (source_location **) &loc);
1364 if (token_ptr == NULL)
1365 return NULL;
1367 return loc;
1370 /* Return the pointer to the INDEXth token of the macro argument ARG.
1371 KIND specifies the kind of token the macro argument ARG contains.
1372 If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
1373 of the virtual location of the returned token if the
1374 -ftrack-macro-expansion flag is on; otherwise, it's set to the
1375 spelling location of the returned token. */
1376 static const cpp_token **
1377 arg_token_ptr_at (const macro_arg *arg, size_t index,
1378 enum macro_arg_token_kind kind,
1379 source_location **virt_location)
1381 const cpp_token **tokens_ptr = NULL;
1383 switch (kind)
1385 case MACRO_ARG_TOKEN_NORMAL:
1386 tokens_ptr = arg->first;
1387 break;
1388 case MACRO_ARG_TOKEN_STRINGIFIED:
1389 tokens_ptr = (const cpp_token **) &arg->stringified;
1390 break;
1391 case MACRO_ARG_TOKEN_EXPANDED:
1392 tokens_ptr = arg->expanded;
1393 break;
1396 if (tokens_ptr == NULL)
1397 /* This can happen for e.g, an empty token argument to a
1398 funtion-like macro. */
1399 return tokens_ptr;
1401 if (virt_location)
1403 if (kind == MACRO_ARG_TOKEN_NORMAL)
1404 *virt_location = &arg->virt_locs[index];
1405 else if (kind == MACRO_ARG_TOKEN_EXPANDED)
1406 *virt_location = &arg->expanded_virt_locs[index];
1407 else if (kind == MACRO_ARG_TOKEN_STRINGIFIED)
1408 *virt_location =
1409 (source_location *) &tokens_ptr[index]->src_loc;
1411 return &tokens_ptr[index];
1414 /* Initialize an iterator so that it iterates over the tokens of a
1415 function-like macro argument. KIND is the kind of tokens we want
1416 ITER to iterate over. TOKEN_PTR points the first token ITER will
1417 iterate over. */
1418 static void
1419 macro_arg_token_iter_init (macro_arg_token_iter *iter,
1420 bool track_macro_exp_p,
1421 enum macro_arg_token_kind kind,
1422 const macro_arg *arg,
1423 const cpp_token **token_ptr)
1425 iter->track_macro_exp_p = track_macro_exp_p;
1426 iter->kind = kind;
1427 iter->token_ptr = token_ptr;
1428 /* Unconditionally initialize this so that the compiler doesn't warn
1429 about iter->location_ptr being possibly uninitialized later after
1430 this code has been inlined somewhere. */
1431 iter->location_ptr = NULL;
1432 if (track_macro_exp_p)
1433 iter->location_ptr = get_arg_token_location (arg, kind);
1434 #if CHECKING_P
1435 iter->num_forwards = 0;
1436 if (track_macro_exp_p
1437 && token_ptr != NULL
1438 && iter->location_ptr == NULL)
1439 abort ();
1440 #endif
1443 /* Move the iterator one token forward. Note that if IT was
1444 initialized on an argument that has a stringified token, moving it
1445 forward doesn't make sense as a stringified token is essentially one
1446 string. */
1447 static void
1448 macro_arg_token_iter_forward (macro_arg_token_iter *it)
1450 switch (it->kind)
1452 case MACRO_ARG_TOKEN_NORMAL:
1453 case MACRO_ARG_TOKEN_EXPANDED:
1454 it->token_ptr++;
1455 if (it->track_macro_exp_p)
1456 it->location_ptr++;
1457 break;
1458 case MACRO_ARG_TOKEN_STRINGIFIED:
1459 #if CHECKING_P
1460 if (it->num_forwards > 0)
1461 abort ();
1462 #endif
1463 break;
1466 #if CHECKING_P
1467 it->num_forwards++;
1468 #endif
1471 /* Return the token pointed to by the iterator. */
1472 static const cpp_token *
1473 macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
1475 #if CHECKING_P
1476 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1477 && it->num_forwards > 0)
1478 abort ();
1479 #endif
1480 if (it->token_ptr == NULL)
1481 return NULL;
1482 return *it->token_ptr;
1485 /* Return the location of the token pointed to by the iterator.*/
1486 static source_location
1487 macro_arg_token_iter_get_location (const macro_arg_token_iter *it)
1489 #if CHECKING_P
1490 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1491 && it->num_forwards > 0)
1492 abort ();
1493 #endif
1494 if (it->track_macro_exp_p)
1495 return *it->location_ptr;
1496 else
1497 return (*it->token_ptr)->src_loc;
1500 /* Return the index of a token [resulting from macro expansion] inside
1501 the total list of tokens resulting from a given macro
1502 expansion. The index can be different depending on whether if we
1503 want each tokens resulting from function-like macro arguments
1504 expansion to have a different location or not.
1506 E.g, consider this function-like macro:
1508 #define M(x) x - 3
1510 Then consider us "calling" it (and thus expanding it) like:
1512 M(1+4)
1514 It will be expanded into:
1516 1+4-3
1518 Let's consider the case of the token '4'.
1520 Its index can be 2 (it's the third token of the set of tokens
1521 resulting from the expansion) or it can be 0 if we consider that
1522 all tokens resulting from the expansion of the argument "1+2" have
1523 the same index, which is 0. In this later case, the index of token
1524 '-' would then be 1 and the index of token '3' would be 2.
1526 The later case is useful to use less memory e.g, for the case of
1527 the user using the option -ftrack-macro-expansion=1.
1529 ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
1530 are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro
1531 parameter (inside the macro replacement list) that corresponds to
1532 the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
1535 If we refer to the example above, for the '4' argument token,
1536 ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
1537 would be set to the token 'x', in the replacement list "x - 3" of
1538 macro M.
1540 This is a subroutine of replace_args. */
1541 inline static unsigned
1542 expanded_token_index (cpp_reader *pfile, cpp_macro *macro,
1543 const cpp_token *cur_replacement_token,
1544 unsigned absolute_token_index)
1546 if (CPP_OPTION (pfile, track_macro_expansion) > 1)
1547 return absolute_token_index;
1548 return cur_replacement_token - macro->exp.tokens;
1551 /* Replace the parameters in a function-like macro of NODE with the
1552 actual ARGS, and place the result in a newly pushed token context.
1553 Expand each argument before replacing, unless it is operated upon
1554 by the # or ## operators. EXPANSION_POINT_LOC is the location of
1555 the expansion point of the macro. E.g, the location of the
1556 function-like macro invocation. */
1557 static void
1558 replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
1559 macro_arg *args, source_location expansion_point_loc)
1561 unsigned int i, total;
1562 const cpp_token *src, *limit;
1563 const cpp_token **first = NULL;
1564 macro_arg *arg;
1565 _cpp_buff *buff = NULL;
1566 source_location *virt_locs = NULL;
1567 unsigned int exp_count;
1568 const line_map_macro *map = NULL;
1569 int track_macro_exp;
1571 /* First, fully macro-expand arguments, calculating the number of
1572 tokens in the final expansion as we go. The ordering of the if
1573 statements below is subtle; we must handle stringification before
1574 pasting. */
1576 /* EXP_COUNT is the number of tokens in the macro replacement
1577 list. TOTAL is the number of tokens /after/ macro parameters
1578 have been replaced by their arguments. */
1579 exp_count = macro_real_token_count (macro);
1580 total = exp_count;
1581 limit = macro->exp.tokens + exp_count;
1583 for (src = macro->exp.tokens; src < limit; src++)
1584 if (src->type == CPP_MACRO_ARG)
1586 /* Leading and trailing padding tokens. */
1587 total += 2;
1588 /* Account for leading and padding tokens in exp_count too.
1589 This is going to be important later down this function,
1590 when we want to handle the case of (track_macro_exp <
1591 2). */
1592 exp_count += 2;
1594 /* We have an argument. If it is not being stringified or
1595 pasted it is macro-replaced before insertion. */
1596 arg = &args[src->val.macro_arg.arg_no - 1];
1598 if (src->flags & STRINGIFY_ARG)
1600 if (!arg->stringified)
1601 arg->stringified = stringify_arg (pfile, arg);
1603 else if ((src->flags & PASTE_LEFT)
1604 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
1605 total += arg->count - 1;
1606 else
1608 if (!arg->expanded)
1609 expand_arg (pfile, arg);
1610 total += arg->expanded_count - 1;
1614 /* When the compiler is called with the -ftrack-macro-expansion
1615 flag, we need to keep track of the location of each token that
1616 results from macro expansion.
1618 A token resulting from macro expansion is not a new token. It is
1619 simply the same token as the token coming from the macro
1620 definition. The new things that are allocated are the buffer
1621 that holds the tokens resulting from macro expansion and a new
1622 location that records many things like the locus of the expansion
1623 point as well as the original locus inside the definition of the
1624 macro. This location is called a virtual location.
1626 So the buffer BUFF holds a set of cpp_token*, and the buffer
1627 VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
1629 Both of these two buffers are going to be hung off of the macro
1630 context, when the latter is pushed. The memory allocated to
1631 store the tokens and their locations is going to be freed once
1632 the context of macro expansion is popped.
1634 As far as tokens are concerned, the memory overhead of
1635 -ftrack-macro-expansion is proportional to the number of
1636 macros that get expanded multiplied by sizeof (source_location).
1637 The good news is that extra memory gets freed when the macro
1638 context is freed, i.e shortly after the macro got expanded. */
1640 /* Is the -ftrack-macro-expansion flag in effect? */
1641 track_macro_exp = CPP_OPTION (pfile, track_macro_expansion);
1643 /* Now allocate memory space for tokens and locations resulting from
1644 the macro expansion, copy the tokens and replace the arguments.
1645 This memory must be freed when the context of the macro MACRO is
1646 popped. */
1647 buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL);
1649 first = (const cpp_token **) buff->base;
1651 /* Create a macro map to record the locations of the tokens that are
1652 involved in the expansion. Note that the expansion point is set
1653 to the location of the closing parenthesis. Otherwise, the
1654 subsequent map created for the first token that comes after the
1655 macro map might have a wrong line number. That would lead to
1656 tokens with wrong line numbers after the macro expansion. This
1657 adds up to the memory overhead of the -ftrack-macro-expansion
1658 flag; for every macro that is expanded, a "macro map" is
1659 created. */
1660 if (track_macro_exp)
1662 int num_macro_tokens = total;
1663 if (track_macro_exp < 2)
1664 /* Then the number of macro tokens won't take in account the
1665 fact that function-like macro arguments can expand to
1666 multiple tokens. This is to save memory at the expense of
1667 accuracy.
1669 Suppose we have #define SQARE(A) A * A
1671 And then we do SQARE(2+3)
1673 Then the tokens 2, +, 3, will have the same location,
1674 saying they come from the expansion of the argument A. */
1675 num_macro_tokens = exp_count;
1676 map = linemap_enter_macro (pfile->line_table, node,
1677 expansion_point_loc,
1678 num_macro_tokens);
1680 i = 0;
1681 for (src = macro->exp.tokens; src < limit; src++)
1683 unsigned int arg_tokens_count;
1684 macro_arg_token_iter from;
1685 const cpp_token **paste_flag = NULL;
1686 const cpp_token **tmp_token_ptr;
1688 if (src->type != CPP_MACRO_ARG)
1690 /* Allocate a virtual location for token SRC, and add that
1691 token and its virtual location into the buffers BUFF and
1692 VIRT_LOCS. */
1693 unsigned index = expanded_token_index (pfile, macro, src, i);
1694 tokens_buff_add_token (buff, virt_locs, src,
1695 src->src_loc, src->src_loc,
1696 map, index);
1697 i += 1;
1698 continue;
1701 paste_flag = 0;
1702 arg = &args[src->val.macro_arg.arg_no - 1];
1703 /* SRC is a macro parameter that we need to replace with its
1704 corresponding argument. So at some point we'll need to
1705 iterate over the tokens of the macro argument and copy them
1706 into the "place" now holding the correspondig macro
1707 parameter. We are going to use the iterator type
1708 macro_argo_token_iter to handle that iterating. The 'if'
1709 below is to initialize the iterator depending on the type of
1710 tokens the macro argument has. It also does some adjustment
1711 related to padding tokens and some pasting corner cases. */
1712 if (src->flags & STRINGIFY_ARG)
1714 arg_tokens_count = 1;
1715 macro_arg_token_iter_init (&from,
1716 CPP_OPTION (pfile,
1717 track_macro_expansion),
1718 MACRO_ARG_TOKEN_STRINGIFIED,
1719 arg, &arg->stringified);
1721 else if (src->flags & PASTE_LEFT)
1723 arg_tokens_count = arg->count;
1724 macro_arg_token_iter_init (&from,
1725 CPP_OPTION (pfile,
1726 track_macro_expansion),
1727 MACRO_ARG_TOKEN_NORMAL,
1728 arg, arg->first);
1730 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
1732 int num_toks;
1733 arg_tokens_count = arg->count;
1734 macro_arg_token_iter_init (&from,
1735 CPP_OPTION (pfile,
1736 track_macro_expansion),
1737 MACRO_ARG_TOKEN_NORMAL,
1738 arg, arg->first);
1740 num_toks = tokens_buff_count (buff);
1742 if (num_toks != 0)
1744 /* So the current parameter token is pasted to the previous
1745 token in the replacement list. Let's look at what
1746 we have as previous and current arguments. */
1748 /* This is the previous argument's token ... */
1749 tmp_token_ptr = tokens_buff_last_token_ptr (buff);
1751 if ((*tmp_token_ptr)->type == CPP_COMMA
1752 && macro->variadic
1753 && src->val.macro_arg.arg_no == macro->paramc)
1755 /* ... which is a comma; and the current parameter
1756 is the last parameter of a variadic function-like
1757 macro. If the argument to the current last
1758 parameter is NULL, then swallow the comma,
1759 otherwise drop the paste flag. */
1760 if (macro_arg_token_iter_get_token (&from) == NULL)
1761 tokens_buff_remove_last_token (buff);
1762 else
1763 paste_flag = tmp_token_ptr;
1765 /* Remove the paste flag if the RHS is a placemarker. */
1766 else if (arg_tokens_count == 0)
1767 paste_flag = tmp_token_ptr;
1770 else
1772 arg_tokens_count = arg->expanded_count;
1773 macro_arg_token_iter_init (&from,
1774 CPP_OPTION (pfile,
1775 track_macro_expansion),
1776 MACRO_ARG_TOKEN_EXPANDED,
1777 arg, arg->expanded);
1780 /* Padding on the left of an argument (unless RHS of ##). */
1781 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
1782 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
1784 const cpp_token *t = padding_token (pfile, src);
1785 unsigned index = expanded_token_index (pfile, macro, src, i);
1786 /* Allocate a virtual location for the padding token and
1787 append the token and its location to BUFF and
1788 VIRT_LOCS. */
1789 tokens_buff_add_token (buff, virt_locs, t,
1790 t->src_loc, t->src_loc,
1791 map, index);
1794 if (arg_tokens_count)
1796 /* So now we've got the number of tokens that make up the
1797 argument that is going to replace the current parameter
1798 in the macro's replacement list. */
1799 unsigned int j;
1800 for (j = 0; j < arg_tokens_count; ++j)
1802 /* So if track_macro_exp is < 2, the user wants to
1803 save extra memory while tracking macro expansion
1804 locations. So in that case here is what we do:
1806 Suppose we have #define SQARE(A) A * A
1808 And then we do SQARE(2+3)
1810 Then the tokens 2, +, 3, will have the same location,
1811 saying they come from the expansion of the argument
1814 So that means we are going to ignore the COUNT tokens
1815 resulting from the expansion of the current macro
1816 arugment. In other words all the ARG_TOKENS_COUNT tokens
1817 resulting from the expansion of the macro argument will
1818 have the index I. Normally, each of those token should
1819 have index I+J. */
1820 unsigned token_index = i;
1821 unsigned index;
1822 if (track_macro_exp > 1)
1823 token_index += j;
1825 index = expanded_token_index (pfile, macro, src, token_index);
1826 tokens_buff_add_token (buff, virt_locs,
1827 macro_arg_token_iter_get_token (&from),
1828 macro_arg_token_iter_get_location (&from),
1829 src->src_loc, map, index);
1830 macro_arg_token_iter_forward (&from);
1833 /* With a non-empty argument on the LHS of ##, the last
1834 token should be flagged PASTE_LEFT. */
1835 if (src->flags & PASTE_LEFT)
1836 paste_flag =
1837 (const cpp_token **) tokens_buff_last_token_ptr (buff);
1839 else if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99)
1840 && ! macro->syshdr && ! cpp_in_system_header (pfile))
1842 if (CPP_OPTION (pfile, cplusplus))
1843 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
1844 "invoking macro %s argument %d: "
1845 "empty macro arguments are undefined"
1846 " in ISO C++98",
1847 NODE_NAME (node), src->val.macro_arg.arg_no);
1848 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat))
1849 cpp_pedwarning (pfile,
1850 CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
1851 ? CPP_W_C90_C99_COMPAT : CPP_W_PEDANTIC,
1852 "invoking macro %s argument %d: "
1853 "empty macro arguments are undefined"
1854 " in ISO C90",
1855 NODE_NAME (node), src->val.macro_arg.arg_no);
1857 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
1858 && ! CPP_OPTION (pfile, cplusplus)
1859 && ! macro->syshdr && ! cpp_in_system_header (pfile))
1860 cpp_warning (pfile, CPP_W_C90_C99_COMPAT,
1861 "invoking macro %s argument %d: "
1862 "empty macro arguments are undefined"
1863 " in ISO C90",
1864 NODE_NAME (node), src->val.macro_arg.arg_no);
1866 /* Avoid paste on RHS (even case count == 0). */
1867 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
1869 const cpp_token *t = &pfile->avoid_paste;
1870 tokens_buff_add_token (buff, virt_locs,
1871 t, t->src_loc, t->src_loc,
1872 NULL, 0);
1875 /* Add a new paste flag, or remove an unwanted one. */
1876 if (paste_flag)
1878 cpp_token *token = _cpp_temp_token (pfile);
1879 token->type = (*paste_flag)->type;
1880 token->val = (*paste_flag)->val;
1881 if (src->flags & PASTE_LEFT)
1882 token->flags = (*paste_flag)->flags | PASTE_LEFT;
1883 else
1884 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
1885 *paste_flag = token;
1888 i += arg_tokens_count;
1891 if (track_macro_exp)
1892 push_extended_tokens_context (pfile, node, buff, virt_locs, first,
1893 tokens_buff_count (buff));
1894 else
1895 push_ptoken_context (pfile, node, buff, first,
1896 tokens_buff_count (buff));
1898 num_macro_tokens_counter += tokens_buff_count (buff);
1901 /* Return a special padding token, with padding inherited from SOURCE. */
1902 static const cpp_token *
1903 padding_token (cpp_reader *pfile, const cpp_token *source)
1905 cpp_token *result = _cpp_temp_token (pfile);
1907 result->type = CPP_PADDING;
1909 /* Data in GCed data structures cannot be made const so far, so we
1910 need a cast here. */
1911 result->val.source = (cpp_token *) source;
1912 result->flags = 0;
1913 return result;
1916 /* Get a new uninitialized context. Create a new one if we cannot
1917 re-use an old one. */
1918 static cpp_context *
1919 next_context (cpp_reader *pfile)
1921 cpp_context *result = pfile->context->next;
1923 if (result == 0)
1925 result = XNEW (cpp_context);
1926 memset (result, 0, sizeof (cpp_context));
1927 result->prev = pfile->context;
1928 result->next = 0;
1929 pfile->context->next = result;
1932 pfile->context = result;
1933 return result;
1936 /* Push a list of pointers to tokens. */
1937 static void
1938 push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
1939 const cpp_token **first, unsigned int count)
1941 cpp_context *context = next_context (pfile);
1943 context->tokens_kind = TOKENS_KIND_INDIRECT;
1944 context->c.macro = macro;
1945 context->buff = buff;
1946 FIRST (context).ptoken = first;
1947 LAST (context).ptoken = first + count;
1950 /* Push a list of tokens.
1952 A NULL macro means that we should continue the current macro
1953 expansion, in essence. That means that if we are currently in a
1954 macro expansion context, we'll make the new pfile->context refer to
1955 the current macro. */
1956 void
1957 _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
1958 const cpp_token *first, unsigned int count)
1960 cpp_context *context;
1962 if (macro == NULL)
1963 macro = macro_of_context (pfile->context);
1965 context = next_context (pfile);
1966 context->tokens_kind = TOKENS_KIND_DIRECT;
1967 context->c.macro = macro;
1968 context->buff = NULL;
1969 FIRST (context).token = first;
1970 LAST (context).token = first + count;
1973 /* Build a context containing a list of tokens as well as their
1974 virtual locations and push it. TOKENS_BUFF is the buffer that
1975 contains the tokens pointed to by FIRST. If TOKENS_BUFF is
1976 non-NULL, it means that the context owns it, meaning that
1977 _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
1978 contains the virtual locations.
1980 A NULL macro means that we should continue the current macro
1981 expansion, in essence. That means that if we are currently in a
1982 macro expansion context, we'll make the new pfile->context refer to
1983 the current macro. */
1984 static void
1985 push_extended_tokens_context (cpp_reader *pfile,
1986 cpp_hashnode *macro,
1987 _cpp_buff *token_buff,
1988 source_location *virt_locs,
1989 const cpp_token **first,
1990 unsigned int count)
1992 cpp_context *context;
1993 macro_context *m;
1995 if (macro == NULL)
1996 macro = macro_of_context (pfile->context);
1998 context = next_context (pfile);
1999 context->tokens_kind = TOKENS_KIND_EXTENDED;
2000 context->buff = token_buff;
2002 m = XNEW (macro_context);
2003 m->macro_node = macro;
2004 m->virt_locs = virt_locs;
2005 m->cur_virt_loc = virt_locs;
2006 context->c.mc = m;
2007 FIRST (context).ptoken = first;
2008 LAST (context).ptoken = first + count;
2011 /* Push a traditional macro's replacement text. */
2012 void
2013 _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
2014 const uchar *start, size_t len)
2016 cpp_context *context = next_context (pfile);
2018 context->tokens_kind = TOKENS_KIND_DIRECT;
2019 context->c.macro = macro;
2020 context->buff = NULL;
2021 CUR (context) = start;
2022 RLIMIT (context) = start + len;
2023 macro->flags |= NODE_DISABLED;
2026 /* Creates a buffer that holds tokens a.k.a "token buffer", usually
2027 for the purpose of storing them on a cpp_context. If VIRT_LOCS is
2028 non-null (which means that -ftrack-macro-expansion is on),
2029 *VIRT_LOCS is set to a newly allocated buffer that is supposed to
2030 hold the virtual locations of the tokens resulting from macro
2031 expansion. */
2032 static _cpp_buff*
2033 tokens_buff_new (cpp_reader *pfile, size_t len,
2034 source_location **virt_locs)
2036 size_t tokens_size = len * sizeof (cpp_token *);
2037 size_t locs_size = len * sizeof (source_location);
2039 if (virt_locs != NULL)
2040 *virt_locs = XNEWVEC (source_location, locs_size);
2041 return _cpp_get_buff (pfile, tokens_size);
2044 /* Returns the number of tokens contained in a token buffer. The
2045 buffer holds a set of cpp_token*. */
2046 static size_t
2047 tokens_buff_count (_cpp_buff *buff)
2049 return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *);
2052 /* Return a pointer to the last token contained in the token buffer
2053 BUFF. */
2054 static const cpp_token **
2055 tokens_buff_last_token_ptr (_cpp_buff *buff)
2057 return &((const cpp_token **) BUFF_FRONT (buff))[-1];
2060 /* Remove the last token contained in the token buffer TOKENS_BUFF.
2061 If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer
2062 containing the virtual locations of the tokens in TOKENS_BUFF; in
2063 which case the function updates that buffer as well. */
2064 static inline void
2065 tokens_buff_remove_last_token (_cpp_buff *tokens_buff)
2068 if (BUFF_FRONT (tokens_buff) > tokens_buff->base)
2069 BUFF_FRONT (tokens_buff) =
2070 (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1];
2073 /* Insert a token into the token buffer at the position pointed to by
2074 DEST. Note that the buffer is not enlarged so the previous token
2075 that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null,
2076 means -ftrack-macro-expansion is effect; it then points to where to
2077 insert the virtual location of TOKEN. TOKEN is the token to
2078 insert. VIRT_LOC is the virtual location of the token, i.e, the
2079 location possibly encoding its locus across macro expansion. If
2080 TOKEN is an argument of a function-like macro (inside a macro
2081 replacement list), PARM_DEF_LOC is the spelling location of the
2082 macro parameter that TOKEN is replacing, in the replacement list of
2083 the macro. If TOKEN is not an argument of a function-like macro or
2084 if it doesn't come from a macro expansion, then VIRT_LOC can just
2085 be set to the same value as PARM_DEF_LOC. If MAP is non null, it
2086 means TOKEN comes from a macro expansion and MAP is the macro map
2087 associated to the macro. MACRO_TOKEN_INDEX points to the index of
2088 the token in the macro map; it is not considered if MAP is NULL.
2090 Upon successful completion this function returns the a pointer to
2091 the position of the token coming right after the insertion
2092 point. */
2093 static inline const cpp_token **
2094 tokens_buff_put_token_to (const cpp_token **dest,
2095 source_location *virt_loc_dest,
2096 const cpp_token *token,
2097 source_location virt_loc,
2098 source_location parm_def_loc,
2099 const line_map_macro *map,
2100 unsigned int macro_token_index)
2102 source_location macro_loc = virt_loc;
2103 const cpp_token **result;
2105 if (virt_loc_dest)
2107 /* -ftrack-macro-expansion is on. */
2108 if (map)
2109 macro_loc = linemap_add_macro_token (map, macro_token_index,
2110 virt_loc, parm_def_loc);
2111 *virt_loc_dest = macro_loc;
2113 *dest = token;
2114 result = &dest[1];
2116 return result;
2119 /* Adds a token at the end of the tokens contained in BUFFER. Note
2120 that this function doesn't enlarge BUFFER when the number of tokens
2121 reaches BUFFER's size; it aborts in that situation.
2123 TOKEN is the token to append. VIRT_LOC is the virtual location of
2124 the token, i.e, the location possibly encoding its locus across
2125 macro expansion. If TOKEN is an argument of a function-like macro
2126 (inside a macro replacement list), PARM_DEF_LOC is the location of
2127 the macro parameter that TOKEN is replacing. If TOKEN doesn't come
2128 from a macro expansion, then VIRT_LOC can just be set to the same
2129 value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes
2130 from a macro expansion and MAP is the macro map associated to the
2131 macro. MACRO_TOKEN_INDEX points to the index of the token in the
2132 macro map; It is not considered if MAP is NULL. If VIRT_LOCS is
2133 non-null, it means -ftrack-macro-expansion is on; in which case
2134 this function adds the virtual location DEF_LOC to the VIRT_LOCS
2135 array, at the same index as the one of TOKEN in BUFFER. Upon
2136 successful completion this function returns the a pointer to the
2137 position of the token coming right after the insertion point. */
2138 static const cpp_token **
2139 tokens_buff_add_token (_cpp_buff *buffer,
2140 source_location *virt_locs,
2141 const cpp_token *token,
2142 source_location virt_loc,
2143 source_location parm_def_loc,
2144 const line_map_macro *map,
2145 unsigned int macro_token_index)
2147 const cpp_token **result;
2148 source_location *virt_loc_dest = NULL;
2149 unsigned token_index =
2150 (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
2152 /* Abort if we pass the end the buffer. */
2153 if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer))
2154 abort ();
2156 if (virt_locs != NULL)
2157 virt_loc_dest = &virt_locs[token_index];
2159 result =
2160 tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer),
2161 virt_loc_dest, token, virt_loc, parm_def_loc,
2162 map, macro_token_index);
2164 BUFF_FRONT (buffer) = (unsigned char *) result;
2165 return result;
2168 /* Allocate space for the function-like macro argument ARG to store
2169 the tokens resulting from the macro-expansion of the tokens that
2170 make up ARG itself. That space is allocated in ARG->expanded and
2171 needs to be freed using free. */
2172 static void
2173 alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity)
2175 gcc_checking_assert (arg->expanded == NULL
2176 && arg->expanded_virt_locs == NULL);
2178 arg->expanded = XNEWVEC (const cpp_token *, capacity);
2179 if (CPP_OPTION (pfile, track_macro_expansion))
2180 arg->expanded_virt_locs = XNEWVEC (source_location, capacity);
2184 /* If necessary, enlarge ARG->expanded to so that it can contain SIZE
2185 tokens. */
2186 static void
2187 ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg,
2188 size_t size, size_t *expanded_capacity)
2190 if (size <= *expanded_capacity)
2191 return;
2193 size *= 2;
2195 arg->expanded =
2196 XRESIZEVEC (const cpp_token *, arg->expanded, size);
2197 *expanded_capacity = size;
2199 if (CPP_OPTION (pfile, track_macro_expansion))
2201 if (arg->expanded_virt_locs == NULL)
2202 arg->expanded_virt_locs = XNEWVEC (source_location, size);
2203 else
2204 arg->expanded_virt_locs = XRESIZEVEC (source_location,
2205 arg->expanded_virt_locs,
2206 size);
2210 /* Expand an argument ARG before replacing parameters in a
2211 function-like macro. This works by pushing a context with the
2212 argument's tokens, and then expanding that into a temporary buffer
2213 as if it were a normal part of the token stream. collect_args()
2214 has terminated the argument's tokens with a CPP_EOF so that we know
2215 when we have fully expanded the argument. */
2216 static void
2217 expand_arg (cpp_reader *pfile, macro_arg *arg)
2219 size_t capacity;
2220 bool saved_warn_trad;
2221 bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
2223 if (arg->count == 0
2224 || arg->expanded != NULL)
2225 return;
2227 /* Don't warn about funlike macros when pre-expanding. */
2228 saved_warn_trad = CPP_WTRADITIONAL (pfile);
2229 CPP_WTRADITIONAL (pfile) = 0;
2231 /* Loop, reading in the tokens of the argument. */
2232 capacity = 256;
2233 alloc_expanded_arg_mem (pfile, arg, capacity);
2235 if (track_macro_exp_p)
2236 push_extended_tokens_context (pfile, NULL, NULL,
2237 arg->virt_locs,
2238 arg->first,
2239 arg->count + 1);
2240 else
2241 push_ptoken_context (pfile, NULL, NULL,
2242 arg->first, arg->count + 1);
2244 for (;;)
2246 const cpp_token *token;
2247 source_location location;
2249 ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1,
2250 &capacity);
2252 token = cpp_get_token_1 (pfile, &location);
2254 if (token->type == CPP_EOF)
2255 break;
2257 set_arg_token (arg, token, location,
2258 arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED,
2259 CPP_OPTION (pfile, track_macro_expansion));
2260 arg->expanded_count++;
2263 _cpp_pop_context (pfile);
2265 CPP_WTRADITIONAL (pfile) = saved_warn_trad;
2268 /* Returns the macro associated to the current context if we are in
2269 the context a macro expansion, NULL otherwise. */
2270 static cpp_hashnode*
2271 macro_of_context (cpp_context *context)
2273 if (context == NULL)
2274 return NULL;
2276 return (context->tokens_kind == TOKENS_KIND_EXTENDED)
2277 ? context->c.mc->macro_node
2278 : context->c.macro;
2281 /* Return TRUE iff we are expanding a macro or are about to start
2282 expanding one. If we are effectively expanding a macro, the
2283 function macro_of_context returns a pointer to the macro being
2284 expanded. */
2285 static bool
2286 in_macro_expansion_p (cpp_reader *pfile)
2288 if (pfile == NULL)
2289 return false;
2291 return (pfile->about_to_expand_macro_p
2292 || macro_of_context (pfile->context));
2295 /* Pop the current context off the stack, re-enabling the macro if the
2296 context represented a macro's replacement list. Initially the
2297 context structure was not freed so that we can re-use it later, but
2298 now we do free it to reduce peak memory consumption. */
2299 void
2300 _cpp_pop_context (cpp_reader *pfile)
2302 cpp_context *context = pfile->context;
2304 /* We should not be popping the base context. */
2305 if (context == &pfile->base_context)
2306 abort ();
2308 if (context->c.macro)
2310 cpp_hashnode *macro;
2311 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
2313 macro_context *mc = context->c.mc;
2314 macro = mc->macro_node;
2315 /* If context->buff is set, it means the life time of tokens
2316 is bound to the life time of this context; so we must
2317 free the tokens; that means we must free the virtual
2318 locations of these tokens too. */
2319 if (context->buff && mc->virt_locs)
2321 free (mc->virt_locs);
2322 mc->virt_locs = NULL;
2324 free (mc);
2325 context->c.mc = NULL;
2327 else
2328 macro = context->c.macro;
2330 /* Beware that MACRO can be NULL in cases like when we are
2331 called from expand_arg. In those cases, a dummy context with
2332 tokens is pushed just for the purpose of walking them using
2333 cpp_get_token_1. In that case, no 'macro' field is set into
2334 the dummy context. */
2335 if (macro != NULL
2336 /* Several contiguous macro expansion contexts can be
2337 associated to the same macro; that means it's the same
2338 macro expansion that spans across all these (sub)
2339 contexts. So we should re-enable an expansion-disabled
2340 macro only when we are sure we are really out of that
2341 macro expansion. */
2342 && macro_of_context (context->prev) != macro)
2343 macro->flags &= ~NODE_DISABLED;
2345 if (macro == pfile->top_most_macro_node && context->prev == NULL)
2346 /* We are popping the context of the top-most macro node. */
2347 pfile->top_most_macro_node = NULL;
2350 if (context->buff)
2352 /* Decrease memory peak consumption by freeing the memory used
2353 by the context. */
2354 _cpp_free_buff (context->buff);
2357 pfile->context = context->prev;
2358 /* decrease peak memory consumption by feeing the context. */
2359 pfile->context->next = NULL;
2360 free (context);
2363 /* Return TRUE if we reached the end of the set of tokens stored in
2364 CONTEXT, FALSE otherwise. */
2365 static inline bool
2366 reached_end_of_context (cpp_context *context)
2368 if (context->tokens_kind == TOKENS_KIND_DIRECT)
2369 return FIRST (context).token == LAST (context).token;
2370 else if (context->tokens_kind == TOKENS_KIND_INDIRECT
2371 || context->tokens_kind == TOKENS_KIND_EXTENDED)
2372 return FIRST (context).ptoken == LAST (context).ptoken;
2373 else
2374 abort ();
2377 /* Consume the next token contained in the current context of PFILE,
2378 and return it in *TOKEN. It's "full location" is returned in
2379 *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
2380 means the location encoding the locus of the token across macro
2381 expansion; otherwise it's just is the "normal" location of the
2382 token which (*TOKEN)->src_loc. */
2383 static inline void
2384 consume_next_token_from_context (cpp_reader *pfile,
2385 const cpp_token ** token,
2386 source_location *location)
2388 cpp_context *c = pfile->context;
2390 if ((c)->tokens_kind == TOKENS_KIND_DIRECT)
2392 *token = FIRST (c).token;
2393 *location = (*token)->src_loc;
2394 FIRST (c).token++;
2396 else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT)
2398 *token = *FIRST (c).ptoken;
2399 *location = (*token)->src_loc;
2400 FIRST (c).ptoken++;
2402 else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED)
2404 macro_context *m = c->c.mc;
2405 *token = *FIRST (c).ptoken;
2406 if (m->virt_locs)
2408 *location = *m->cur_virt_loc;
2409 m->cur_virt_loc++;
2411 else
2412 *location = (*token)->src_loc;
2413 FIRST (c).ptoken++;
2415 else
2416 abort ();
2419 /* In the traditional mode of the preprocessor, if we are currently in
2420 a directive, the location of a token must be the location of the
2421 start of the directive line. This function returns the proper
2422 location if we are in the traditional mode, and just returns
2423 LOCATION otherwise. */
2425 static inline source_location
2426 maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, source_location location)
2428 if (CPP_OPTION (pfile, traditional))
2430 if (pfile->state.in_directive)
2431 return pfile->directive_line;
2433 return location;
2436 /* Routine to get a token as well as its location.
2438 Macro expansions and directives are transparently handled,
2439 including entering included files. Thus tokens are post-macro
2440 expansion, and after any intervening directives. External callers
2441 see CPP_EOF only at EOF. Internal callers also see it when meeting
2442 a directive inside a macro call, when at the end of a directive and
2443 state.in_directive is still 1, and at the end of argument
2444 pre-expansion.
2446 LOC is an out parameter; *LOC is set to the location "as expected
2447 by the user". Please read the comment of
2448 cpp_get_token_with_location to learn more about the meaning of this
2449 location. */
2450 static const cpp_token*
2451 cpp_get_token_1 (cpp_reader *pfile, source_location *location)
2453 const cpp_token *result;
2454 /* This token is a virtual token that either encodes a location
2455 related to macro expansion or a spelling location. */
2456 source_location virt_loc = 0;
2457 /* pfile->about_to_expand_macro_p can be overriden by indirect calls
2458 to functions that push macro contexts. So let's save it so that
2459 we can restore it when we are about to leave this routine. */
2460 bool saved_about_to_expand_macro = pfile->about_to_expand_macro_p;
2462 for (;;)
2464 cpp_hashnode *node;
2465 cpp_context *context = pfile->context;
2467 /* Context->prev == 0 <=> base context. */
2468 if (!context->prev)
2470 result = _cpp_lex_token (pfile);
2471 virt_loc = result->src_loc;
2473 else if (!reached_end_of_context (context))
2475 consume_next_token_from_context (pfile, &result,
2476 &virt_loc);
2477 if (result->flags & PASTE_LEFT)
2479 paste_all_tokens (pfile, result);
2480 if (pfile->state.in_directive)
2481 continue;
2482 result = padding_token (pfile, result);
2483 goto out;
2486 else
2488 if (pfile->context->c.macro)
2489 ++num_expanded_macros_counter;
2490 _cpp_pop_context (pfile);
2491 if (pfile->state.in_directive)
2492 continue;
2493 result = &pfile->avoid_paste;
2494 goto out;
2497 if (pfile->state.in_directive && result->type == CPP_COMMENT)
2498 continue;
2500 if (result->type != CPP_NAME)
2501 break;
2503 node = result->val.node.node;
2505 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
2506 break;
2508 if (!(node->flags & NODE_DISABLED))
2510 int ret = 0;
2511 /* If not in a macro context, and we're going to start an
2512 expansion, record the location and the top level macro
2513 about to be expanded. */
2514 if (!in_macro_expansion_p (pfile))
2516 pfile->invocation_location = result->src_loc;
2517 pfile->top_most_macro_node = node;
2519 if (pfile->state.prevent_expansion)
2520 break;
2522 /* Conditional macros require that a predicate be evaluated
2523 first. */
2524 if ((node->flags & NODE_CONDITIONAL) != 0)
2526 if (pfile->cb.macro_to_expand)
2528 bool whitespace_after;
2529 const cpp_token *peek_tok = cpp_peek_token (pfile, 0);
2531 whitespace_after = (peek_tok->type == CPP_PADDING
2532 || (peek_tok->flags & PREV_WHITE));
2533 node = pfile->cb.macro_to_expand (pfile, result);
2534 if (node)
2535 ret = enter_macro_context (pfile, node, result,
2536 virt_loc);
2537 else if (whitespace_after)
2539 /* If macro_to_expand hook returned NULL and it
2540 ate some tokens, see if we don't need to add
2541 a padding token in between this and the
2542 next token. */
2543 peek_tok = cpp_peek_token (pfile, 0);
2544 if (peek_tok->type != CPP_PADDING
2545 && (peek_tok->flags & PREV_WHITE) == 0)
2546 _cpp_push_token_context (pfile, NULL,
2547 padding_token (pfile,
2548 peek_tok), 1);
2552 else
2553 ret = enter_macro_context (pfile, node, result,
2554 virt_loc);
2555 if (ret)
2557 if (pfile->state.in_directive || ret == 2)
2558 continue;
2559 result = padding_token (pfile, result);
2560 goto out;
2563 else
2565 /* Flag this token as always unexpandable. FIXME: move this
2566 to collect_args()?. */
2567 cpp_token *t = _cpp_temp_token (pfile);
2568 t->type = result->type;
2569 t->flags = result->flags | NO_EXPAND;
2570 t->val = result->val;
2571 result = t;
2574 break;
2577 out:
2578 if (location != NULL)
2580 if (virt_loc == 0)
2581 virt_loc = result->src_loc;
2582 *location = virt_loc;
2584 if (!CPP_OPTION (pfile, track_macro_expansion)
2585 && macro_of_context (pfile->context) != NULL)
2586 /* We are in a macro expansion context, are not tracking
2587 virtual location, but were asked to report the location
2588 of the expansion point of the macro being expanded. */
2589 *location = pfile->invocation_location;
2591 *location = maybe_adjust_loc_for_trad_cpp (pfile, *location);
2594 pfile->about_to_expand_macro_p = saved_about_to_expand_macro;
2595 return result;
2598 /* External routine to get a token. Also used nearly everywhere
2599 internally, except for places where we know we can safely call
2600 _cpp_lex_token directly, such as lexing a directive name.
2602 Macro expansions and directives are transparently handled,
2603 including entering included files. Thus tokens are post-macro
2604 expansion, and after any intervening directives. External callers
2605 see CPP_EOF only at EOF. Internal callers also see it when meeting
2606 a directive inside a macro call, when at the end of a directive and
2607 state.in_directive is still 1, and at the end of argument
2608 pre-expansion. */
2609 const cpp_token *
2610 cpp_get_token (cpp_reader *pfile)
2612 return cpp_get_token_1 (pfile, NULL);
2615 /* Like cpp_get_token, but also returns a virtual token location
2616 separate from the spelling location carried by the returned token.
2618 LOC is an out parameter; *LOC is set to the location "as expected
2619 by the user". This matters when a token results from macro
2620 expansion; in that case the token's spelling location indicates the
2621 locus of the token in the definition of the macro but *LOC
2622 virtually encodes all the other meaningful locuses associated to
2623 the token.
2625 What? virtual location? Yes, virtual location.
2627 If the token results from macro expansion and if macro expansion
2628 location tracking is enabled its virtual location encodes (at the
2629 same time):
2631 - the spelling location of the token
2633 - the locus of the macro expansion point
2635 - the locus of the point where the token got instantiated as part
2636 of the macro expansion process.
2638 You have to use the linemap API to get the locus you are interested
2639 in from a given virtual location.
2641 Note however that virtual locations are not necessarily ordered for
2642 relations '<' and '>'. One must use the function
2643 linemap_location_before_p instead of using the relational operator
2644 '<'.
2646 If macro expansion tracking is off and if the token results from
2647 macro expansion the virtual location is the expansion point of the
2648 macro that got expanded.
2650 When the token doesn't result from macro expansion, the virtual
2651 location is just the same thing as its spelling location. */
2653 const cpp_token *
2654 cpp_get_token_with_location (cpp_reader *pfile, source_location *loc)
2656 return cpp_get_token_1 (pfile, loc);
2659 /* Returns true if we're expanding an object-like macro that was
2660 defined in a system header. Just checks the macro at the top of
2661 the stack. Used for diagnostic suppression. */
2663 cpp_sys_macro_p (cpp_reader *pfile)
2665 cpp_hashnode *node = NULL;
2667 if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2668 node = pfile->context->c.mc->macro_node;
2669 else
2670 node = pfile->context->c.macro;
2672 return node && node->value.macro && node->value.macro->syshdr;
2675 /* Read each token in, until end of the current file. Directives are
2676 transparently processed. */
2677 void
2678 cpp_scan_nooutput (cpp_reader *pfile)
2680 /* Request a CPP_EOF token at the end of this file, rather than
2681 transparently continuing with the including file. */
2682 pfile->buffer->return_at_eof = true;
2684 pfile->state.discarding_output++;
2685 pfile->state.prevent_expansion++;
2687 if (CPP_OPTION (pfile, traditional))
2688 while (_cpp_read_logical_line_trad (pfile))
2690 else
2691 while (cpp_get_token (pfile)->type != CPP_EOF)
2694 pfile->state.discarding_output--;
2695 pfile->state.prevent_expansion--;
2698 /* Step back one or more tokens obtained from the lexer. */
2699 void
2700 _cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count)
2702 pfile->lookaheads += count;
2703 while (count--)
2705 pfile->cur_token--;
2706 if (pfile->cur_token == pfile->cur_run->base
2707 /* Possible with -fpreprocessed and no leading #line. */
2708 && pfile->cur_run->prev != NULL)
2710 pfile->cur_run = pfile->cur_run->prev;
2711 pfile->cur_token = pfile->cur_run->limit;
2716 /* Step back one (or more) tokens. Can only step back more than 1 if
2717 they are from the lexer, and not from macro expansion. */
2718 void
2719 _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
2721 if (pfile->context->prev == NULL)
2722 _cpp_backup_tokens_direct (pfile, count);
2723 else
2725 if (count != 1)
2726 abort ();
2727 if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT)
2728 FIRST (pfile->context).token--;
2729 else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT)
2730 FIRST (pfile->context).ptoken--;
2731 else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2733 FIRST (pfile->context).ptoken--;
2734 if (pfile->context->c.macro)
2736 macro_context *m = pfile->context->c.mc;
2737 m->cur_virt_loc--;
2738 gcc_checking_assert (m->cur_virt_loc >= m->virt_locs);
2740 else
2741 abort ();
2743 else
2744 abort ();
2748 /* #define directive parsing and handling. */
2750 /* Returns nonzero if a macro redefinition warning is required. */
2751 static bool
2752 warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
2753 const cpp_macro *macro2)
2755 const cpp_macro *macro1;
2756 unsigned int i;
2758 /* Some redefinitions need to be warned about regardless. */
2759 if (node->flags & NODE_WARN)
2760 return true;
2762 /* Suppress warnings for builtins that lack the NODE_WARN flag,
2763 unless Wbuiltin-macro-redefined. */
2764 if (node->flags & NODE_BUILTIN
2765 && (!pfile->cb.user_builtin_macro
2766 || !pfile->cb.user_builtin_macro (pfile, node)))
2767 return CPP_OPTION (pfile, warn_builtin_macro_redefined);
2769 /* Redefinitions of conditional (context-sensitive) macros, on
2770 the other hand, must be allowed silently. */
2771 if (node->flags & NODE_CONDITIONAL)
2772 return false;
2774 /* Redefinition of a macro is allowed if and only if the old and new
2775 definitions are the same. (6.10.3 paragraph 2). */
2776 macro1 = node->value.macro;
2778 /* Don't check count here as it can be different in valid
2779 traditional redefinitions with just whitespace differences. */
2780 if (macro1->paramc != macro2->paramc
2781 || macro1->fun_like != macro2->fun_like
2782 || macro1->variadic != macro2->variadic)
2783 return true;
2785 /* Check parameter spellings. */
2786 for (i = 0; i < macro1->paramc; i++)
2787 if (macro1->params[i] != macro2->params[i])
2788 return true;
2790 /* Check the replacement text or tokens. */
2791 if (CPP_OPTION (pfile, traditional))
2792 return _cpp_expansions_different_trad (macro1, macro2);
2794 if (macro1->count != macro2->count)
2795 return true;
2797 for (i = 0; i < macro1->count; i++)
2798 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
2799 return true;
2801 return false;
2804 /* Free the definition of hashnode H. */
2805 void
2806 _cpp_free_definition (cpp_hashnode *h)
2808 /* Macros and assertions no longer have anything to free. */
2809 h->type = NT_VOID;
2810 /* Clear builtin flag in case of redefinition. */
2811 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED);
2814 /* Save parameter NODE (spelling SPELLING) to the parameter list of
2815 macro MACRO. Returns zero on success, nonzero if the parameter is
2816 a duplicate. */
2817 bool
2818 _cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node,
2819 cpp_hashnode *spelling)
2821 unsigned int len;
2822 /* Constraint 6.10.3.6 - duplicate parameter names. */
2823 if (node->flags & NODE_MACRO_ARG)
2825 cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
2826 NODE_NAME (node));
2827 return true;
2830 if (BUFF_ROOM (pfile->a_buff)
2831 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
2832 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
2834 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = spelling;
2835 node->flags |= NODE_MACRO_ARG;
2836 len = macro->paramc * sizeof (struct macro_arg_saved_data);
2837 if (len > pfile->macro_buffer_len)
2839 pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer,
2840 len);
2841 pfile->macro_buffer_len = len;
2843 struct macro_arg_saved_data save;
2844 save.value = node->value;
2845 save.canonical_node = node;
2846 ((struct macro_arg_saved_data *) pfile->macro_buffer)[macro->paramc - 1]
2847 = save;
2849 node->value.arg_index = macro->paramc;
2850 return false;
2853 /* Check the syntax of the parameters in a MACRO definition. Returns
2854 false if an error occurs. */
2855 static bool
2856 parse_params (cpp_reader *pfile, cpp_macro *macro)
2858 unsigned int prev_ident = 0;
2860 for (;;)
2862 const cpp_token *token = _cpp_lex_token (pfile);
2864 switch (token->type)
2866 default:
2867 /* Allow/ignore comments in parameter lists if we are
2868 preserving comments in macro expansions. */
2869 if (token->type == CPP_COMMENT
2870 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
2871 continue;
2873 cpp_error (pfile, CPP_DL_ERROR,
2874 "\"%s\" may not appear in macro parameter list",
2875 cpp_token_as_text (pfile, token));
2876 return false;
2878 case CPP_NAME:
2879 if (prev_ident)
2881 cpp_error (pfile, CPP_DL_ERROR,
2882 "macro parameters must be comma-separated");
2883 return false;
2885 prev_ident = 1;
2887 if (_cpp_save_parameter (pfile, macro, token->val.node.node,
2888 token->val.node.spelling))
2889 return false;
2890 continue;
2892 case CPP_CLOSE_PAREN:
2893 if (prev_ident || macro->paramc == 0)
2894 return true;
2896 /* Fall through to pick up the error. */
2897 /* FALLTHRU */
2898 case CPP_COMMA:
2899 if (!prev_ident)
2901 cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
2902 return false;
2904 prev_ident = 0;
2905 continue;
2907 case CPP_ELLIPSIS:
2908 macro->variadic = 1;
2909 if (!prev_ident)
2911 _cpp_save_parameter (pfile, macro,
2912 pfile->spec_nodes.n__VA_ARGS__,
2913 pfile->spec_nodes.n__VA_ARGS__);
2914 pfile->state.va_args_ok = 1;
2915 if (! CPP_OPTION (pfile, c99)
2916 && CPP_OPTION (pfile, cpp_pedantic)
2917 && CPP_OPTION (pfile, warn_variadic_macros))
2919 if (CPP_OPTION (pfile, cplusplus))
2920 cpp_pedwarning
2921 (pfile, CPP_W_VARIADIC_MACROS,
2922 "anonymous variadic macros were introduced in C++11");
2923 else
2924 cpp_pedwarning
2925 (pfile, CPP_W_VARIADIC_MACROS,
2926 "anonymous variadic macros were introduced in C99");
2928 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
2929 && ! CPP_OPTION (pfile, cplusplus))
2930 cpp_error (pfile, CPP_DL_WARNING,
2931 "anonymous variadic macros were introduced in C99");
2933 else if (CPP_OPTION (pfile, cpp_pedantic)
2934 && CPP_OPTION (pfile, warn_variadic_macros))
2936 if (CPP_OPTION (pfile, cplusplus))
2937 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
2938 "ISO C++ does not permit named variadic macros");
2939 else
2940 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
2941 "ISO C does not permit named variadic macros");
2944 /* We're at the end, and just expect a closing parenthesis. */
2945 token = _cpp_lex_token (pfile);
2946 if (token->type == CPP_CLOSE_PAREN)
2947 return true;
2948 /* Fall through. */
2950 case CPP_EOF:
2951 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
2952 return false;
2957 /* Allocate room for a token from a macro's replacement list. */
2958 static cpp_token *
2959 alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
2961 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
2962 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
2964 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
2967 /* Lex a token from the expansion of MACRO, but mark parameters as we
2968 find them and warn of traditional stringification. */
2969 static cpp_token *
2970 lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
2972 cpp_token *token, *saved_cur_token;
2974 saved_cur_token = pfile->cur_token;
2975 pfile->cur_token = alloc_expansion_token (pfile, macro);
2976 token = _cpp_lex_direct (pfile);
2977 pfile->cur_token = saved_cur_token;
2979 /* Is this a parameter? */
2980 if (token->type == CPP_NAME
2981 && (token->val.node.node->flags & NODE_MACRO_ARG) != 0)
2983 cpp_hashnode *spelling = token->val.node.spelling;
2984 token->type = CPP_MACRO_ARG;
2985 token->val.macro_arg.arg_no = token->val.node.node->value.arg_index;
2986 token->val.macro_arg.spelling = spelling;
2988 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
2989 && (token->type == CPP_STRING || token->type == CPP_CHAR))
2990 check_trad_stringification (pfile, macro, &token->val.str);
2992 return token;
2995 static bool
2996 create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
2998 cpp_token *token;
2999 const cpp_token *ctoken;
3000 bool following_paste_op = false;
3001 const char *paste_op_error_msg =
3002 N_("'##' cannot appear at either end of a macro expansion");
3003 unsigned int num_extra_tokens = 0;
3005 /* Get the first token of the expansion (or the '(' of a
3006 function-like macro). */
3007 ctoken = _cpp_lex_token (pfile);
3009 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
3011 bool ok = parse_params (pfile, macro);
3012 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
3013 if (!ok)
3014 return false;
3016 /* Success. Commit or allocate the parameter array. */
3017 if (pfile->hash_table->alloc_subobject)
3019 cpp_hashnode **params =
3020 (cpp_hashnode **) pfile->hash_table->alloc_subobject
3021 (sizeof (cpp_hashnode *) * macro->paramc);
3022 memcpy (params, macro->params,
3023 sizeof (cpp_hashnode *) * macro->paramc);
3024 macro->params = params;
3026 else
3027 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
3028 macro->fun_like = 1;
3030 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
3032 /* While ISO C99 requires whitespace before replacement text
3033 in a macro definition, ISO C90 with TC1 allows characters
3034 from the basic source character set there. */
3035 if (CPP_OPTION (pfile, c99))
3037 if (CPP_OPTION (pfile, cplusplus))
3038 cpp_error (pfile, CPP_DL_PEDWARN,
3039 "ISO C++11 requires whitespace after the macro name");
3040 else
3041 cpp_error (pfile, CPP_DL_PEDWARN,
3042 "ISO C99 requires whitespace after the macro name");
3044 else
3046 int warntype = CPP_DL_WARNING;
3047 switch (ctoken->type)
3049 case CPP_ATSIGN:
3050 case CPP_AT_NAME:
3051 case CPP_OBJC_STRING:
3052 /* '@' is not in basic character set. */
3053 warntype = CPP_DL_PEDWARN;
3054 break;
3055 case CPP_OTHER:
3056 /* Basic character set sans letters, digits and _. */
3057 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
3058 ctoken->val.str.text[0]) == NULL)
3059 warntype = CPP_DL_PEDWARN;
3060 break;
3061 default:
3062 /* All other tokens start with a character from basic
3063 character set. */
3064 break;
3066 cpp_error (pfile, warntype,
3067 "missing whitespace after the macro name");
3071 if (macro->fun_like)
3072 token = lex_expansion_token (pfile, macro);
3073 else
3075 token = alloc_expansion_token (pfile, macro);
3076 *token = *ctoken;
3079 for (;;)
3081 /* Check the stringifying # constraint 6.10.3.2.1 of
3082 function-like macros when lexing the subsequent token. */
3083 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
3085 if (token->type == CPP_MACRO_ARG)
3087 if (token->flags & PREV_WHITE)
3088 token->flags |= SP_PREV_WHITE;
3089 if (token[-1].flags & DIGRAPH)
3090 token->flags |= SP_DIGRAPH;
3091 token->flags &= ~PREV_WHITE;
3092 token->flags |= STRINGIFY_ARG;
3093 token->flags |= token[-1].flags & PREV_WHITE;
3094 token[-1] = token[0];
3095 macro->count--;
3097 /* Let assembler get away with murder. */
3098 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
3100 cpp_error (pfile, CPP_DL_ERROR,
3101 "'#' is not followed by a macro parameter");
3102 return false;
3106 if (token->type == CPP_EOF)
3108 /* Paste operator constraint 6.10.3.3.1:
3109 Token-paste ##, can appear in both object-like and
3110 function-like macros, but not at the end. */
3111 if (following_paste_op)
3113 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
3114 return false;
3116 break;
3119 /* Paste operator constraint 6.10.3.3.1. */
3120 if (token->type == CPP_PASTE)
3122 /* Token-paste ##, can appear in both object-like and
3123 function-like macros, but not at the beginning. */
3124 if (macro->count == 1)
3126 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
3127 return false;
3130 if (token[-1].flags & PASTE_LEFT)
3132 macro->extra_tokens = 1;
3133 num_extra_tokens++;
3134 token->val.token_no = macro->count - 1;
3136 else
3138 --macro->count;
3139 token[-1].flags |= PASTE_LEFT;
3140 if (token->flags & DIGRAPH)
3141 token[-1].flags |= SP_DIGRAPH;
3142 if (token->flags & PREV_WHITE)
3143 token[-1].flags |= SP_PREV_WHITE;
3147 following_paste_op = (token->type == CPP_PASTE);
3148 token = lex_expansion_token (pfile, macro);
3151 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
3152 macro->traditional = 0;
3154 /* Don't count the CPP_EOF. */
3155 macro->count--;
3157 /* Clear whitespace on first token for warn_of_redefinition(). */
3158 if (macro->count)
3159 macro->exp.tokens[0].flags &= ~PREV_WHITE;
3161 /* Commit or allocate the memory. */
3162 if (pfile->hash_table->alloc_subobject)
3164 cpp_token *tokns =
3165 (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token)
3166 * macro->count);
3167 if (num_extra_tokens)
3169 /* Place second and subsequent ## or %:%: tokens in
3170 sequences of consecutive such tokens at the end of the
3171 list to preserve information about where they appear, how
3172 they are spelt and whether they are preceded by
3173 whitespace without otherwise interfering with macro
3174 expansion. */
3175 cpp_token *normal_dest = tokns;
3176 cpp_token *extra_dest = tokns + macro->count - num_extra_tokens;
3177 unsigned int i;
3178 for (i = 0; i < macro->count; i++)
3180 if (macro->exp.tokens[i].type == CPP_PASTE)
3181 *extra_dest++ = macro->exp.tokens[i];
3182 else
3183 *normal_dest++ = macro->exp.tokens[i];
3186 else
3187 memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
3188 macro->exp.tokens = tokns;
3190 else
3191 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
3193 return true;
3196 /* Parse a macro and save its expansion. Returns nonzero on success. */
3197 bool
3198 _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
3200 cpp_macro *macro;
3201 unsigned int i;
3202 bool ok;
3204 if (pfile->hash_table->alloc_subobject)
3205 macro = (cpp_macro *) pfile->hash_table->alloc_subobject
3206 (sizeof (cpp_macro));
3207 else
3208 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
3209 macro->line = pfile->directive_line;
3210 macro->params = 0;
3211 macro->paramc = 0;
3212 macro->variadic = 0;
3213 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
3214 macro->count = 0;
3215 macro->fun_like = 0;
3216 macro->extra_tokens = 0;
3217 /* To suppress some diagnostics. */
3218 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
3220 if (CPP_OPTION (pfile, traditional))
3221 ok = _cpp_create_trad_definition (pfile, macro);
3222 else
3224 ok = create_iso_definition (pfile, macro);
3226 /* We set the type for SEEN_EOL() in directives.c.
3228 Longer term we should lex the whole line before coming here,
3229 and just copy the expansion. */
3231 /* Stop the lexer accepting __VA_ARGS__. */
3232 pfile->state.va_args_ok = 0;
3235 /* Clear the fast argument lookup indices. */
3236 for (i = macro->paramc; i-- > 0; )
3238 struct macro_arg_saved_data *save =
3239 &((struct macro_arg_saved_data *) pfile->macro_buffer)[i];
3240 struct cpp_hashnode *node = save->canonical_node;
3241 node->flags &= ~ NODE_MACRO_ARG;
3242 node->value = save->value;
3245 if (!ok)
3246 return ok;
3248 if (node->type == NT_MACRO)
3250 if (CPP_OPTION (pfile, warn_unused_macros))
3251 _cpp_warn_if_unused_macro (pfile, node, NULL);
3253 if (warn_of_redefinition (pfile, node, macro))
3255 const int reason = ((node->flags & NODE_BUILTIN)
3256 && !(node->flags & NODE_WARN))
3257 ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
3259 bool warned =
3260 cpp_pedwarning_with_line (pfile, reason,
3261 pfile->directive_line, 0,
3262 "\"%s\" redefined", NODE_NAME (node));
3264 if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
3265 cpp_error_with_line (pfile, CPP_DL_NOTE,
3266 node->value.macro->line, 0,
3267 "this is the location of the previous definition");
3271 if (node->type != NT_VOID)
3272 _cpp_free_definition (node);
3274 /* Enter definition in hash table. */
3275 node->type = NT_MACRO;
3276 node->value.macro = macro;
3277 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))
3278 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS")
3279 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
3280 in the C standard, as something that one must use in C++.
3281 However DR#593 and C++11 indicate that they play no role in C++.
3282 We special-case them anyway. */
3283 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS")
3284 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS"))
3285 node->flags |= NODE_WARN;
3287 /* If user defines one of the conditional macros, remove the
3288 conditional flag */
3289 node->flags &= ~NODE_CONDITIONAL;
3291 return ok;
3294 /* Warn if a token in STRING matches one of a function-like MACRO's
3295 parameters. */
3296 static void
3297 check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
3298 const cpp_string *string)
3300 unsigned int i, len;
3301 const uchar *p, *q, *limit;
3303 /* Loop over the string. */
3304 limit = string->text + string->len - 1;
3305 for (p = string->text + 1; p < limit; p = q)
3307 /* Find the start of an identifier. */
3308 while (p < limit && !is_idstart (*p))
3309 p++;
3311 /* Find the end of the identifier. */
3312 q = p;
3313 while (q < limit && is_idchar (*q))
3314 q++;
3316 len = q - p;
3318 /* Loop over the function macro arguments to see if the
3319 identifier inside the string matches one of them. */
3320 for (i = 0; i < macro->paramc; i++)
3322 const cpp_hashnode *node = macro->params[i];
3324 if (NODE_LEN (node) == len
3325 && !memcmp (p, NODE_NAME (node), len))
3327 cpp_error (pfile, CPP_DL_WARNING,
3328 "macro argument \"%s\" would be stringified in traditional C",
3329 NODE_NAME (node));
3330 break;
3336 /* Returns true of NODE is a function-like macro. */
3337 bool
3338 cpp_fun_like_macro_p (cpp_hashnode *node)
3340 return (node->type == NT_MACRO
3341 && (node->flags & (NODE_BUILTIN | NODE_MACRO_ARG)) == 0
3342 && node->value.macro->fun_like);
3345 /* Returns the name, arguments and expansion of a macro, in a format
3346 suitable to be read back in again, and therefore also for DWARF 2
3347 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
3348 Caller is expected to generate the "#define" bit if needed. The
3349 returned text is temporary, and automatically freed later. */
3350 const unsigned char *
3351 cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
3353 unsigned int i, len;
3354 const cpp_macro *macro;
3355 unsigned char *buffer;
3357 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
3359 if (node->type != NT_MACRO
3360 || !pfile->cb.user_builtin_macro
3361 || !pfile->cb.user_builtin_macro (pfile, node))
3363 cpp_error (pfile, CPP_DL_ICE,
3364 "invalid hash type %d in cpp_macro_definition",
3365 node->type);
3366 return 0;
3370 macro = node->value.macro;
3371 /* Calculate length. */
3372 len = NODE_LEN (node) * 10 + 2; /* ' ' and NUL. */
3373 if (macro->fun_like)
3375 len += 4; /* "()" plus possible final ".." of named
3376 varargs (we have + 1 below). */
3377 for (i = 0; i < macro->paramc; i++)
3378 len += NODE_LEN (macro->params[i]) + 1; /* "," */
3381 /* This should match below where we fill in the buffer. */
3382 if (CPP_OPTION (pfile, traditional))
3383 len += _cpp_replacement_text_len (macro);
3384 else
3386 unsigned int count = macro_real_token_count (macro);
3387 for (i = 0; i < count; i++)
3389 cpp_token *token = &macro->exp.tokens[i];
3391 if (token->type == CPP_MACRO_ARG)
3392 len += NODE_LEN (token->val.macro_arg.spelling);
3393 else
3394 len += cpp_token_len (token);
3396 if (token->flags & STRINGIFY_ARG)
3397 len++; /* "#" */
3398 if (token->flags & PASTE_LEFT)
3399 len += 3; /* " ##" */
3400 if (token->flags & PREV_WHITE)
3401 len++; /* " " */
3405 if (len > pfile->macro_buffer_len)
3407 pfile->macro_buffer = XRESIZEVEC (unsigned char,
3408 pfile->macro_buffer, len);
3409 pfile->macro_buffer_len = len;
3412 /* Fill in the buffer. Start with the macro name. */
3413 buffer = pfile->macro_buffer;
3414 buffer = _cpp_spell_ident_ucns (buffer, node);
3416 /* Parameter names. */
3417 if (macro->fun_like)
3419 *buffer++ = '(';
3420 for (i = 0; i < macro->paramc; i++)
3422 cpp_hashnode *param = macro->params[i];
3424 if (param != pfile->spec_nodes.n__VA_ARGS__)
3426 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
3427 buffer += NODE_LEN (param);
3430 if (i + 1 < macro->paramc)
3431 /* Don't emit a space after the comma here; we're trying
3432 to emit a Dwarf-friendly definition, and the Dwarf spec
3433 forbids spaces in the argument list. */
3434 *buffer++ = ',';
3435 else if (macro->variadic)
3436 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
3438 *buffer++ = ')';
3441 /* The Dwarf spec requires a space after the macro name, even if the
3442 definition is the empty string. */
3443 *buffer++ = ' ';
3445 if (CPP_OPTION (pfile, traditional))
3446 buffer = _cpp_copy_replacement_text (macro, buffer);
3447 else if (macro->count)
3448 /* Expansion tokens. */
3450 unsigned int count = macro_real_token_count (macro);
3451 for (i = 0; i < count; i++)
3453 cpp_token *token = &macro->exp.tokens[i];
3455 if (token->flags & PREV_WHITE)
3456 *buffer++ = ' ';
3457 if (token->flags & STRINGIFY_ARG)
3458 *buffer++ = '#';
3460 if (token->type == CPP_MACRO_ARG)
3462 memcpy (buffer,
3463 NODE_NAME (token->val.macro_arg.spelling),
3464 NODE_LEN (token->val.macro_arg.spelling));
3465 buffer += NODE_LEN (token->val.macro_arg.spelling);
3467 else
3468 buffer = cpp_spell_token (pfile, token, buffer, true);
3470 if (token->flags & PASTE_LEFT)
3472 *buffer++ = ' ';
3473 *buffer++ = '#';
3474 *buffer++ = '#';
3475 /* Next has PREV_WHITE; see _cpp_create_definition. */
3480 *buffer = '\0';
3481 return pfile->macro_buffer;