[NDS32] Add missing RTX_FRAME_RELATED_P to set fp_adjust_insn rtx.
[official-gcc.git] / libcpp / macro.c
blob678bf2b625342724e58bb3d580b4ee8d3d6beb86
1 /* Part of CPP library. (Macro and #define handling.)
2 Copyright (C) 1986-2014 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 #ifdef ENABLE_CHECKING
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 *, source_location);
97 static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
98 const cpp_token **, unsigned int);
99 static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *,
100 _cpp_buff *, source_location *,
101 const cpp_token **, unsigned int);
102 static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *,
103 _cpp_buff **, unsigned *);
104 static cpp_context *next_context (cpp_reader *);
105 static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
106 static void expand_arg (cpp_reader *, macro_arg *);
107 static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
108 static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
109 static void paste_all_tokens (cpp_reader *, const cpp_token *);
110 static bool paste_tokens (cpp_reader *, source_location,
111 const cpp_token **, const cpp_token *);
112 static void alloc_expanded_arg_mem (cpp_reader *, macro_arg *, size_t);
113 static void ensure_expanded_arg_room (cpp_reader *, macro_arg *, size_t, size_t *);
114 static void delete_macro_args (_cpp_buff*, unsigned num_args);
115 static void set_arg_token (macro_arg *, const cpp_token *,
116 source_location, size_t,
117 enum macro_arg_token_kind,
118 bool);
119 static const source_location *get_arg_token_location (const macro_arg *,
120 enum macro_arg_token_kind);
121 static const cpp_token **arg_token_ptr_at (const macro_arg *,
122 size_t,
123 enum macro_arg_token_kind,
124 source_location **virt_location);
126 static void macro_arg_token_iter_init (macro_arg_token_iter *, bool,
127 enum macro_arg_token_kind,
128 const macro_arg *,
129 const cpp_token **);
130 static const cpp_token *macro_arg_token_iter_get_token
131 (const macro_arg_token_iter *it);
132 static source_location macro_arg_token_iter_get_location
133 (const macro_arg_token_iter *);
134 static void macro_arg_token_iter_forward (macro_arg_token_iter *);
135 static _cpp_buff *tokens_buff_new (cpp_reader *, size_t,
136 source_location **);
137 static size_t tokens_buff_count (_cpp_buff *);
138 static const cpp_token **tokens_buff_last_token_ptr (_cpp_buff *);
139 static inline const cpp_token **tokens_buff_put_token_to (const cpp_token **,
140 source_location *,
141 const cpp_token *,
142 source_location,
143 source_location,
144 const struct line_map *,
145 unsigned int);
147 static const cpp_token **tokens_buff_add_token (_cpp_buff *,
148 source_location *,
149 const cpp_token *,
150 source_location,
151 source_location,
152 const struct line_map *,
153 unsigned int);
154 static inline void tokens_buff_remove_last_token (_cpp_buff *);
155 static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
156 macro_arg *, source_location);
157 static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *,
158 _cpp_buff **, unsigned *);
159 static bool create_iso_definition (cpp_reader *, cpp_macro *);
161 /* #define directive parsing and handling. */
163 static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *);
164 static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *);
165 static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *,
166 const cpp_macro *);
167 static bool parse_params (cpp_reader *, cpp_macro *);
168 static void check_trad_stringification (cpp_reader *, const cpp_macro *,
169 const cpp_string *);
170 static bool reached_end_of_context (cpp_context *);
171 static void consume_next_token_from_context (cpp_reader *pfile,
172 const cpp_token **,
173 source_location *);
174 static const cpp_token* cpp_get_token_1 (cpp_reader *, source_location *);
176 static cpp_hashnode* macro_of_context (cpp_context *context);
178 static bool in_macro_expansion_p (cpp_reader *pfile);
180 /* Statistical counter tracking the number of macros that got
181 expanded. */
182 unsigned num_expanded_macros_counter = 0;
183 /* Statistical counter tracking the total number tokens resulting
184 from macro expansion. */
185 unsigned num_macro_tokens_counter = 0;
187 /* Emits a warning if NODE is a macro defined in the main file that
188 has not been used. */
190 _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
191 void *v ATTRIBUTE_UNUSED)
193 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
195 cpp_macro *macro = node->value.macro;
197 if (!macro->used
198 && MAIN_FILE_P (linemap_lookup (pfile->line_table, macro->line)))
199 cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0,
200 "macro \"%s\" is not used", NODE_NAME (node));
203 return 1;
206 /* Allocates and returns a CPP_STRING token, containing TEXT of length
207 LEN, after null-terminating it. TEXT must be in permanent storage. */
208 static const cpp_token *
209 new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len)
211 cpp_token *token = _cpp_temp_token (pfile);
213 text[len] = '\0';
214 token->type = CPP_STRING;
215 token->val.str.len = len;
216 token->val.str.text = text;
217 token->flags = 0;
218 return token;
221 static const char * const monthnames[] =
223 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
224 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
227 /* Helper function for builtin_macro. Returns the text generated by
228 a builtin macro. */
229 const uchar *
230 _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
232 const uchar *result = NULL;
233 linenum_type number = 1;
235 switch (node->value.builtin)
237 default:
238 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
239 NODE_NAME (node));
240 break;
242 case BT_TIMESTAMP:
244 if (CPP_OPTION (pfile, warn_date_time))
245 cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent "
246 "reproducible builds", NODE_NAME (node));
248 cpp_buffer *pbuffer = cpp_get_buffer (pfile);
249 if (pbuffer->timestamp == NULL)
251 /* Initialize timestamp value of the assotiated file. */
252 struct _cpp_file *file = cpp_get_file (pbuffer);
253 if (file)
255 /* Generate __TIMESTAMP__ string, that represents
256 the date and time of the last modification
257 of the current source file. The string constant
258 looks like "Sun Sep 16 01:03:52 1973". */
259 struct tm *tb = NULL;
260 struct stat *st = _cpp_get_file_stat (file);
261 if (st)
262 tb = localtime (&st->st_mtime);
263 if (tb)
265 char *str = asctime (tb);
266 size_t len = strlen (str);
267 unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2);
268 buf[0] = '"';
269 strcpy ((char *) buf + 1, str);
270 buf[len] = '"';
271 pbuffer->timestamp = buf;
273 else
275 cpp_errno (pfile, CPP_DL_WARNING,
276 "could not determine file timestamp");
277 pbuffer->timestamp = UC"\"??? ??? ?? ??:??:?? ????\"";
281 result = pbuffer->timestamp;
283 break;
284 case BT_FILE:
285 case BT_BASE_FILE:
287 unsigned int len;
288 const char *name;
289 uchar *buf;
291 if (node->value.builtin == BT_FILE)
292 name = linemap_get_expansion_filename (pfile->line_table,
293 pfile->line_table->highest_line);
294 else
296 name = _cpp_get_file_name (pfile->main_file);
297 if (!name)
298 abort ();
300 len = strlen (name);
301 buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
302 result = buf;
303 *buf = '"';
304 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
305 *buf++ = '"';
306 *buf = '\0';
308 break;
310 case BT_INCLUDE_LEVEL:
311 /* The line map depth counts the primary source as level 1, but
312 historically __INCLUDE_DEPTH__ has called the primary source
313 level 0. */
314 number = pfile->line_table->depth - 1;
315 break;
317 case BT_SPECLINE:
318 /* If __LINE__ is embedded in a macro, it must expand to the
319 line of the macro's invocation, not its definition.
320 Otherwise things like assert() will not work properly. */
321 number = linemap_get_expansion_line (pfile->line_table,
322 CPP_OPTION (pfile, traditional)
323 ? pfile->line_table->highest_line
324 : pfile->cur_token[-1].src_loc);
325 break;
327 /* __STDC__ has the value 1 under normal circumstances.
328 However, if (a) we are in a system header, (b) the option
329 stdc_0_in_system_headers is true (set by target config), and
330 (c) we are not in strictly conforming mode, then it has the
331 value 0. (b) and (c) are already checked in cpp_init_builtins. */
332 case BT_STDC:
333 if (cpp_in_system_header (pfile))
334 number = 0;
335 else
336 number = 1;
337 break;
339 case BT_DATE:
340 case BT_TIME:
341 if (CPP_OPTION (pfile, warn_date_time))
342 cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent "
343 "reproducible builds", NODE_NAME (node));
344 if (pfile->date == NULL)
346 /* Allocate __DATE__ and __TIME__ strings from permanent
347 storage. We only do this once, and don't generate them
348 at init time, because time() and localtime() are very
349 slow on some systems. */
350 time_t tt;
351 struct tm *tb = NULL;
353 /* (time_t) -1 is a legitimate value for "number of seconds
354 since the Epoch", so we have to do a little dance to
355 distinguish that from a genuine error. */
356 errno = 0;
357 tt = time(NULL);
358 if (tt != (time_t)-1 || errno == 0)
359 tb = localtime (&tt);
361 if (tb)
363 pfile->date = _cpp_unaligned_alloc (pfile,
364 sizeof ("\"Oct 11 1347\""));
365 sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
366 monthnames[tb->tm_mon], tb->tm_mday,
367 tb->tm_year + 1900);
369 pfile->time = _cpp_unaligned_alloc (pfile,
370 sizeof ("\"12:34:56\""));
371 sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
372 tb->tm_hour, tb->tm_min, tb->tm_sec);
374 else
376 cpp_errno (pfile, CPP_DL_WARNING,
377 "could not determine date and time");
379 pfile->date = UC"\"??? ?? ????\"";
380 pfile->time = UC"\"??:??:??\"";
384 if (node->value.builtin == BT_DATE)
385 result = pfile->date;
386 else
387 result = pfile->time;
388 break;
390 case BT_COUNTER:
391 if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive)
392 cpp_error (pfile, CPP_DL_ERROR,
393 "__COUNTER__ expanded inside directive with -fdirectives-only");
394 number = pfile->counter++;
395 break;
398 if (result == NULL)
400 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
401 result = _cpp_unaligned_alloc (pfile, 21);
402 sprintf ((char *) result, "%u", number);
405 return result;
408 /* Convert builtin macros like __FILE__ to a token and push it on the
409 context stack. Also handles _Pragma, for which a new token may not
410 be created. Returns 1 if it generates a new token context, 0 to
411 return the token to the caller. LOC is the location of the expansion
412 point of the macro. */
413 static int
414 builtin_macro (cpp_reader *pfile, cpp_hashnode *node, source_location loc)
416 const uchar *buf;
417 size_t len;
418 char *nbuf;
420 if (node->value.builtin == BT_PRAGMA)
422 /* Don't interpret _Pragma within directives. The standard is
423 not clear on this, but to me this makes most sense. */
424 if (pfile->state.in_directive)
425 return 0;
427 return _cpp_do__Pragma (pfile);
430 buf = _cpp_builtin_macro_text (pfile, node);
431 len = ustrlen (buf);
432 nbuf = (char *) alloca (len + 1);
433 memcpy (nbuf, buf, len);
434 nbuf[len]='\n';
436 cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
437 _cpp_clean_line (pfile);
439 /* Set pfile->cur_token as required by _cpp_lex_direct. */
440 pfile->cur_token = _cpp_temp_token (pfile);
441 cpp_token *token = _cpp_lex_direct (pfile);
442 /* We should point to the expansion point of the builtin macro. */
443 token->src_loc = loc;
444 if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
446 /* We are tracking tokens resulting from macro expansion.
447 Create a macro line map and generate a virtual location for
448 the token resulting from the expansion of the built-in
449 macro. */
450 source_location *virt_locs = NULL;
451 _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
452 const line_map * map =
453 linemap_enter_macro (pfile->line_table, node,
454 token->src_loc, 1);
455 tokens_buff_add_token (token_buf, virt_locs, token,
456 pfile->line_table->builtin_location,
457 pfile->line_table->builtin_location,
458 map, /*macro_token_index=*/0);
459 push_extended_tokens_context (pfile, node, token_buf, virt_locs,
460 (const cpp_token **)token_buf->base,
463 else
464 _cpp_push_token_context (pfile, NULL, token, 1);
465 if (pfile->buffer->cur != pfile->buffer->rlimit)
466 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
467 NODE_NAME (node));
468 _cpp_pop_buffer (pfile);
470 return 1;
473 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
474 backslashes and double quotes. DEST must be of sufficient size.
475 Returns a pointer to the end of the string. */
476 uchar *
477 cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
479 while (len--)
481 uchar c = *src++;
483 if (c == '\\' || c == '"')
485 *dest++ = '\\';
486 *dest++ = c;
488 else
489 *dest++ = c;
492 return dest;
495 /* Convert a token sequence ARG to a single string token according to
496 the rules of the ISO C #-operator. */
497 static const cpp_token *
498 stringify_arg (cpp_reader *pfile, macro_arg *arg)
500 unsigned char *dest;
501 unsigned int i, escape_it, backslash_count = 0;
502 const cpp_token *source = NULL;
503 size_t len;
505 if (BUFF_ROOM (pfile->u_buff) < 3)
506 _cpp_extend_buff (pfile, &pfile->u_buff, 3);
507 dest = BUFF_FRONT (pfile->u_buff);
508 *dest++ = '"';
510 /* Loop, reading in the argument's tokens. */
511 for (i = 0; i < arg->count; i++)
513 const cpp_token *token = arg->first[i];
515 if (token->type == CPP_PADDING)
517 if (source == NULL
518 || (!(source->flags & PREV_WHITE)
519 && token->val.source == NULL))
520 source = token->val.source;
521 continue;
524 escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR
525 || token->type == CPP_WSTRING || token->type == CPP_WCHAR
526 || token->type == CPP_STRING32 || token->type == CPP_CHAR32
527 || token->type == CPP_STRING16 || token->type == CPP_CHAR16
528 || token->type == CPP_UTF8STRING
529 || cpp_userdef_string_p (token->type)
530 || cpp_userdef_char_p (token->type));
532 /* Room for each char being written in octal, initial space and
533 final quote and NUL. */
534 len = cpp_token_len (token);
535 if (escape_it)
536 len *= 4;
537 len += 3;
539 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
541 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
542 _cpp_extend_buff (pfile, &pfile->u_buff, len);
543 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
546 /* Leading white space? */
547 if (dest - 1 != BUFF_FRONT (pfile->u_buff))
549 if (source == NULL)
550 source = token;
551 if (source->flags & PREV_WHITE)
552 *dest++ = ' ';
554 source = NULL;
556 if (escape_it)
558 _cpp_buff *buff = _cpp_get_buff (pfile, len);
559 unsigned char *buf = BUFF_FRONT (buff);
560 len = cpp_spell_token (pfile, token, buf, true) - buf;
561 dest = cpp_quote_string (dest, buf, len);
562 _cpp_release_buff (pfile, buff);
564 else
565 dest = cpp_spell_token (pfile, token, dest, true);
567 if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
568 backslash_count++;
569 else
570 backslash_count = 0;
573 /* Ignore the final \ of invalid string literals. */
574 if (backslash_count & 1)
576 cpp_error (pfile, CPP_DL_WARNING,
577 "invalid string literal, ignoring final '\\'");
578 dest--;
581 /* Commit the memory, including NUL, and return the token. */
582 *dest++ = '"';
583 len = dest - BUFF_FRONT (pfile->u_buff);
584 BUFF_FRONT (pfile->u_buff) = dest + 1;
585 return new_string_token (pfile, dest - len, len);
588 /* Try to paste two tokens. On success, return nonzero. In any
589 case, PLHS is updated to point to the pasted token, which is
590 guaranteed to not have the PASTE_LEFT flag set. LOCATION is
591 the virtual location used for error reporting. */
592 static bool
593 paste_tokens (cpp_reader *pfile, source_location location,
594 const cpp_token **plhs, const cpp_token *rhs)
596 unsigned char *buf, *end, *lhsend;
597 cpp_token *lhs;
598 unsigned int len;
600 len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1;
601 buf = (unsigned char *) alloca (len);
602 end = lhsend = cpp_spell_token (pfile, *plhs, buf, true);
604 /* Avoid comment headers, since they are still processed in stage 3.
605 It is simpler to insert a space here, rather than modifying the
606 lexer to ignore comments in some circumstances. Simply returning
607 false doesn't work, since we want to clear the PASTE_LEFT flag. */
608 if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ)
609 *end++ = ' ';
610 /* In one obscure case we might see padding here. */
611 if (rhs->type != CPP_PADDING)
612 end = cpp_spell_token (pfile, rhs, end, true);
613 *end = '\n';
615 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
616 _cpp_clean_line (pfile);
618 /* Set pfile->cur_token as required by _cpp_lex_direct. */
619 pfile->cur_token = _cpp_temp_token (pfile);
620 lhs = _cpp_lex_direct (pfile);
621 if (pfile->buffer->cur != pfile->buffer->rlimit)
623 source_location saved_loc = lhs->src_loc;
625 _cpp_pop_buffer (pfile);
626 _cpp_backup_tokens (pfile, 1);
627 *lhsend = '\0';
629 /* We have to remove the PASTE_LEFT flag from the old lhs, but
630 we want to keep the new location. */
631 *lhs = **plhs;
632 *plhs = lhs;
633 lhs->src_loc = saved_loc;
634 lhs->flags &= ~PASTE_LEFT;
636 /* Mandatory error for all apart from assembler. */
637 if (CPP_OPTION (pfile, lang) != CLK_ASM)
638 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
639 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
640 buf, cpp_token_as_text (pfile, rhs));
641 return false;
644 *plhs = lhs;
645 _cpp_pop_buffer (pfile);
646 return true;
649 /* Handles an arbitrarily long sequence of ## operators, with initial
650 operand LHS. This implementation is left-associative,
651 non-recursive, and finishes a paste before handling succeeding
652 ones. If a paste fails, we back up to the RHS of the failing ##
653 operator before pushing the context containing the result of prior
654 successful pastes, with the effect that the RHS appears in the
655 output stream after the pasted LHS normally. */
656 static void
657 paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
659 const cpp_token *rhs = NULL;
660 cpp_context *context = pfile->context;
661 source_location virt_loc = 0;
663 /* We are expanding a macro and we must have been called on a token
664 that appears at the left hand side of a ## operator. */
665 if (macro_of_context (pfile->context) == NULL
666 || (!(lhs->flags & PASTE_LEFT)))
667 abort ();
669 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
670 /* The caller must have called consume_next_token_from_context
671 right before calling us. That has incremented the pointer to
672 the current virtual location. So it now points to the location
673 of the token that comes right after *LHS. We want the
674 resulting pasted token to have the location of the current
675 *LHS, though. */
676 virt_loc = context->c.mc->cur_virt_loc[-1];
677 else
678 /* We are not tracking macro expansion. So the best virtual
679 location we can get here is the expansion point of the macro we
680 are currently expanding. */
681 virt_loc = pfile->invocation_location;
685 /* Take the token directly from the current context. We can do
686 this, because we are in the replacement list of either an
687 object-like macro, or a function-like macro with arguments
688 inserted. In either case, the constraints to #define
689 guarantee we have at least one more token. */
690 if (context->tokens_kind == TOKENS_KIND_DIRECT)
691 rhs = FIRST (context).token++;
692 else if (context->tokens_kind == TOKENS_KIND_INDIRECT)
693 rhs = *FIRST (context).ptoken++;
694 else if (context->tokens_kind == TOKENS_KIND_EXTENDED)
696 /* So we are in presence of an extended token context, which
697 means that each token in this context has a virtual
698 location attached to it. So let's not forget to update
699 the pointer to the current virtual location of the
700 current token when we update the pointer to the current
701 token */
703 rhs = *FIRST (context).ptoken++;
704 /* context->c.mc must be non-null, as if we were not in a
705 macro context, context->tokens_kind could not be equal to
706 TOKENS_KIND_EXTENDED. */
707 context->c.mc->cur_virt_loc++;
710 if (rhs->type == CPP_PADDING)
712 if (rhs->flags & PASTE_LEFT)
713 abort ();
715 if (!paste_tokens (pfile, virt_loc, &lhs, rhs))
716 break;
718 while (rhs->flags & PASTE_LEFT);
720 /* Put the resulting token in its own context. */
721 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
723 source_location *virt_locs = NULL;
724 _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
725 tokens_buff_add_token (token_buf, virt_locs, lhs,
726 virt_loc, 0, NULL, 0);
727 push_extended_tokens_context (pfile, context->c.mc->macro_node,
728 token_buf, virt_locs,
729 (const cpp_token **)token_buf->base, 1);
731 else
732 _cpp_push_token_context (pfile, NULL, lhs, 1);
735 /* Returns TRUE if the number of arguments ARGC supplied in an
736 invocation of the MACRO referenced by NODE is valid. An empty
737 invocation to a macro with no parameters should pass ARGC as zero.
739 Note that MACRO cannot necessarily be deduced from NODE, in case
740 NODE was redefined whilst collecting arguments. */
741 bool
742 _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
744 if (argc == macro->paramc)
745 return true;
747 if (argc < macro->paramc)
749 /* As an extension, variadic arguments are allowed to not appear in
750 the invocation at all.
751 e.g. #define debug(format, args...) something
752 debug("string");
754 This is exactly the same as if an empty variadic list had been
755 supplied - debug("string", ). */
757 if (argc + 1 == macro->paramc && macro->variadic)
759 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
761 if (CPP_OPTION (pfile, cplusplus))
762 cpp_error (pfile, CPP_DL_PEDWARN,
763 "ISO C++11 requires at least one argument "
764 "for the \"...\" in a variadic macro");
765 else
766 cpp_error (pfile, CPP_DL_PEDWARN,
767 "ISO C99 requires at least one argument "
768 "for the \"...\" in a variadic macro");
770 return true;
773 cpp_error (pfile, CPP_DL_ERROR,
774 "macro \"%s\" requires %u arguments, but only %u given",
775 NODE_NAME (node), macro->paramc, argc);
777 else
778 cpp_error (pfile, CPP_DL_ERROR,
779 "macro \"%s\" passed %u arguments, but takes just %u",
780 NODE_NAME (node), argc, macro->paramc);
782 return false;
785 /* Reads and returns the arguments to a function-like macro
786 invocation. Assumes the opening parenthesis has been processed.
787 If there is an error, emits an appropriate diagnostic and returns
788 NULL. Each argument is terminated by a CPP_EOF token, for the
789 future benefit of expand_arg(). If there are any deferred
790 #pragma directives among macro arguments, store pointers to the
791 CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.
793 What is returned is the buffer that contains the memory allocated
794 to hold the macro arguments. NODE is the name of the macro this
795 function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is
796 set to the actual number of macro arguments allocated in the
797 returned buffer. */
798 static _cpp_buff *
799 collect_args (cpp_reader *pfile, const cpp_hashnode *node,
800 _cpp_buff **pragma_buff, unsigned *num_args)
802 _cpp_buff *buff, *base_buff;
803 cpp_macro *macro;
804 macro_arg *args, *arg;
805 const cpp_token *token;
806 unsigned int argc;
807 source_location virt_loc;
808 bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion);
809 unsigned num_args_alloced = 0;
811 macro = node->value.macro;
812 if (macro->paramc)
813 argc = macro->paramc;
814 else
815 argc = 1;
817 #define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50
818 #define ARG_TOKENS_EXTENT 1000
820 buff = _cpp_get_buff (pfile, argc * (DEFAULT_NUM_TOKENS_PER_MACRO_ARG
821 * sizeof (cpp_token *)
822 + sizeof (macro_arg)));
823 base_buff = buff;
824 args = (macro_arg *) buff->base;
825 memset (args, 0, argc * sizeof (macro_arg));
826 buff->cur = (unsigned char *) &args[argc];
827 arg = args, argc = 0;
829 /* Collect the tokens making up each argument. We don't yet know
830 how many arguments have been supplied, whether too many or too
831 few. Hence the slightly bizarre usage of "argc" and "arg". */
834 unsigned int paren_depth = 0;
835 unsigned int ntokens = 0;
836 unsigned virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
837 num_args_alloced++;
839 argc++;
840 arg->first = (const cpp_token **) buff->cur;
841 if (track_macro_expansion_p)
843 virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
844 arg->virt_locs = XNEWVEC (source_location,
845 virt_locs_capacity);
848 for (;;)
850 /* Require space for 2 new tokens (including a CPP_EOF). */
851 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
853 buff = _cpp_append_extend_buff (pfile, buff,
854 ARG_TOKENS_EXTENT
855 * sizeof (cpp_token *));
856 arg->first = (const cpp_token **) buff->cur;
858 if (track_macro_expansion_p
859 && (ntokens + 2 > virt_locs_capacity))
861 virt_locs_capacity += ARG_TOKENS_EXTENT;
862 arg->virt_locs = XRESIZEVEC (source_location,
863 arg->virt_locs,
864 virt_locs_capacity);
867 token = cpp_get_token_1 (pfile, &virt_loc);
869 if (token->type == CPP_PADDING)
871 /* Drop leading padding. */
872 if (ntokens == 0)
873 continue;
875 else if (token->type == CPP_OPEN_PAREN)
876 paren_depth++;
877 else if (token->type == CPP_CLOSE_PAREN)
879 if (paren_depth-- == 0)
880 break;
882 else if (token->type == CPP_COMMA)
884 /* A comma does not terminate an argument within
885 parentheses or as part of a variable argument. */
886 if (paren_depth == 0
887 && ! (macro->variadic && argc == macro->paramc))
888 break;
890 else if (token->type == CPP_EOF
891 || (token->type == CPP_HASH && token->flags & BOL))
892 break;
893 else if (token->type == CPP_PRAGMA)
895 cpp_token *newtok = _cpp_temp_token (pfile);
897 /* CPP_PRAGMA token lives in directive_result, which will
898 be overwritten on the next directive. */
899 *newtok = *token;
900 token = newtok;
903 if (*pragma_buff == NULL
904 || BUFF_ROOM (*pragma_buff) < sizeof (cpp_token *))
906 _cpp_buff *next;
907 if (*pragma_buff == NULL)
908 *pragma_buff
909 = _cpp_get_buff (pfile, 32 * sizeof (cpp_token *));
910 else
912 next = *pragma_buff;
913 *pragma_buff
914 = _cpp_get_buff (pfile,
915 (BUFF_FRONT (*pragma_buff)
916 - (*pragma_buff)->base) * 2);
917 (*pragma_buff)->next = next;
920 *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token;
921 BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *);
922 if (token->type == CPP_PRAGMA_EOL)
923 break;
924 token = cpp_get_token_1 (pfile, &virt_loc);
926 while (token->type != CPP_EOF);
928 /* In deferred pragmas parsing_args and prevent_expansion
929 had been changed, reset it. */
930 pfile->state.parsing_args = 2;
931 pfile->state.prevent_expansion = 1;
933 if (token->type == CPP_EOF)
934 break;
935 else
936 continue;
938 set_arg_token (arg, token, virt_loc,
939 ntokens, MACRO_ARG_TOKEN_NORMAL,
940 CPP_OPTION (pfile, track_macro_expansion));
941 ntokens++;
944 /* Drop trailing padding. */
945 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
946 ntokens--;
948 arg->count = ntokens;
949 set_arg_token (arg, &pfile->eof, pfile->eof.src_loc,
950 ntokens, MACRO_ARG_TOKEN_NORMAL,
951 CPP_OPTION (pfile, track_macro_expansion));
953 /* Terminate the argument. Excess arguments loop back and
954 overwrite the final legitimate argument, before failing. */
955 if (argc <= macro->paramc)
957 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
958 if (argc != macro->paramc)
959 arg++;
962 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
964 if (token->type == CPP_EOF)
966 /* We still need the CPP_EOF to end directives, and to end
967 pre-expansion of a macro argument. Step back is not
968 unconditional, since we don't want to return a CPP_EOF to our
969 callers at the end of an -include-d file. */
970 if (pfile->context->prev || pfile->state.in_directive)
971 _cpp_backup_tokens (pfile, 1);
972 cpp_error (pfile, CPP_DL_ERROR,
973 "unterminated argument list invoking macro \"%s\"",
974 NODE_NAME (node));
976 else
978 /* A single empty argument is counted as no argument. */
979 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
980 argc = 0;
981 if (_cpp_arguments_ok (pfile, macro, node, argc))
983 /* GCC has special semantics for , ## b where b is a varargs
984 parameter: we remove the comma if b was omitted entirely.
985 If b was merely an empty argument, the comma is retained.
986 If the macro takes just one (varargs) parameter, then we
987 retain the comma only if we are standards conforming.
989 If FIRST is NULL replace_args () swallows the comma. */
990 if (macro->variadic && (argc < macro->paramc
991 || (argc == 1 && args[0].count == 0
992 && !CPP_OPTION (pfile, std))))
993 args[macro->paramc - 1].first = NULL;
994 if (num_args)
995 *num_args = num_args_alloced;
996 return base_buff;
1000 /* An error occurred. */
1001 _cpp_release_buff (pfile, base_buff);
1002 return NULL;
1005 /* Search for an opening parenthesis to the macro of NODE, in such a
1006 way that, if none is found, we don't lose the information in any
1007 intervening padding tokens. If we find the parenthesis, collect
1008 the arguments and return the buffer containing them. PRAGMA_BUFF
1009 argument is the same as in collect_args. If NUM_ARGS is non-NULL,
1010 *NUM_ARGS is set to the number of arguments contained in the
1011 returned buffer. */
1012 static _cpp_buff *
1013 funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node,
1014 _cpp_buff **pragma_buff, unsigned *num_args)
1016 const cpp_token *token, *padding = NULL;
1018 for (;;)
1020 token = cpp_get_token (pfile);
1021 if (token->type != CPP_PADDING)
1022 break;
1023 if (padding == NULL
1024 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
1025 padding = token;
1028 if (token->type == CPP_OPEN_PAREN)
1030 pfile->state.parsing_args = 2;
1031 return collect_args (pfile, node, pragma_buff, num_args);
1034 /* CPP_EOF can be the end of macro arguments, or the end of the
1035 file. We mustn't back up over the latter. Ugh. */
1036 if (token->type != CPP_EOF || token == &pfile->eof)
1038 /* Back up. We may have skipped padding, in which case backing
1039 up more than one token when expanding macros is in general
1040 too difficult. We re-insert it in its own context. */
1041 _cpp_backup_tokens (pfile, 1);
1042 if (padding)
1043 _cpp_push_token_context (pfile, NULL, padding, 1);
1046 return NULL;
1049 /* Return the real number of tokens in the expansion of MACRO. */
1050 static inline unsigned int
1051 macro_real_token_count (const cpp_macro *macro)
1053 unsigned int i;
1054 if (__builtin_expect (!macro->extra_tokens, true))
1055 return macro->count;
1056 for (i = 0; i < macro->count; i++)
1057 if (macro->exp.tokens[i].type == CPP_PASTE)
1058 return i;
1059 abort ();
1062 /* Push the context of a macro with hash entry NODE onto the context
1063 stack. If we can successfully expand the macro, we push a context
1064 containing its yet-to-be-rescanned replacement list and return one.
1065 If there were additionally any unexpanded deferred #pragma
1066 directives among macro arguments, push another context containing
1067 the pragma tokens before the yet-to-be-rescanned replacement list
1068 and return two. Otherwise, we don't push a context and return
1069 zero. LOCATION is the location of the expansion point of the
1070 macro. */
1071 static int
1072 enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
1073 const cpp_token *result, source_location location)
1075 /* The presence of a macro invalidates a file's controlling macro. */
1076 pfile->mi_valid = false;
1078 pfile->state.angled_headers = false;
1080 /* From here to when we push the context for the macro later down
1081 this function, we need to flag the fact that we are about to
1082 expand a macro. This is useful when -ftrack-macro-expansion is
1083 turned off. In that case, we need to record the location of the
1084 expansion point of the top-most macro we are about to to expand,
1085 into pfile->invocation_location. But we must not record any such
1086 location once the process of expanding the macro starts; that is,
1087 we must not do that recording between now and later down this
1088 function where set this flag to FALSE. */
1089 pfile->about_to_expand_macro_p = true;
1091 if ((node->flags & NODE_BUILTIN) && !(node->flags & NODE_USED))
1093 node->flags |= NODE_USED;
1094 if ((!pfile->cb.user_builtin_macro
1095 || !pfile->cb.user_builtin_macro (pfile, node))
1096 && pfile->cb.used_define)
1097 pfile->cb.used_define (pfile, pfile->directive_line, node);
1100 /* Handle standard macros. */
1101 if (! (node->flags & NODE_BUILTIN))
1103 cpp_macro *macro = node->value.macro;
1104 _cpp_buff *pragma_buff = NULL;
1106 if (macro->fun_like)
1108 _cpp_buff *buff;
1109 unsigned num_args = 0;
1111 pfile->state.prevent_expansion++;
1112 pfile->keep_tokens++;
1113 pfile->state.parsing_args = 1;
1114 buff = funlike_invocation_p (pfile, node, &pragma_buff,
1115 &num_args);
1116 pfile->state.parsing_args = 0;
1117 pfile->keep_tokens--;
1118 pfile->state.prevent_expansion--;
1120 if (buff == NULL)
1122 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
1123 cpp_warning (pfile, CPP_W_TRADITIONAL,
1124 "function-like macro \"%s\" must be used with arguments in traditional C",
1125 NODE_NAME (node));
1127 if (pragma_buff)
1128 _cpp_release_buff (pfile, pragma_buff);
1130 pfile->about_to_expand_macro_p = false;
1131 return 0;
1134 if (macro->paramc > 0)
1135 replace_args (pfile, node, macro,
1136 (macro_arg *) buff->base,
1137 location);
1138 /* Free the memory used by the arguments of this
1139 function-like macro. This memory has been allocated by
1140 funlike_invocation_p and by replace_args. */
1141 delete_macro_args (buff, num_args);
1144 /* Disable the macro within its expansion. */
1145 node->flags |= NODE_DISABLED;
1147 if (!(node->flags & NODE_USED))
1149 node->flags |= NODE_USED;
1150 if (pfile->cb.used_define)
1151 pfile->cb.used_define (pfile, pfile->directive_line, node);
1154 if (pfile->cb.used)
1155 pfile->cb.used (pfile, location, node);
1157 macro->used = 1;
1159 if (macro->paramc == 0)
1161 unsigned tokens_count = macro_real_token_count (macro);
1162 if (CPP_OPTION (pfile, track_macro_expansion))
1164 unsigned int i;
1165 const cpp_token *src = macro->exp.tokens;
1166 const struct line_map *map;
1167 source_location *virt_locs = NULL;
1168 _cpp_buff *macro_tokens
1169 = tokens_buff_new (pfile, tokens_count, &virt_locs);
1171 /* Create a macro map to record the locations of the
1172 tokens that are involved in the expansion. LOCATION
1173 is the location of the macro expansion point. */
1174 map = linemap_enter_macro (pfile->line_table,
1175 node, location, tokens_count);
1176 for (i = 0; i < tokens_count; ++i)
1178 tokens_buff_add_token (macro_tokens, virt_locs,
1179 src, src->src_loc,
1180 src->src_loc, map, i);
1181 ++src;
1183 push_extended_tokens_context (pfile, node,
1184 macro_tokens,
1185 virt_locs,
1186 (const cpp_token **)
1187 macro_tokens->base,
1188 tokens_count);
1190 else
1191 _cpp_push_token_context (pfile, node, macro->exp.tokens,
1192 tokens_count);
1193 num_macro_tokens_counter += tokens_count;
1196 if (pragma_buff)
1198 if (!pfile->state.in_directive)
1199 _cpp_push_token_context (pfile, NULL,
1200 padding_token (pfile, result), 1);
1203 unsigned tokens_count;
1204 _cpp_buff *tail = pragma_buff->next;
1205 pragma_buff->next = NULL;
1206 tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff)
1207 - (const cpp_token **) pragma_buff->base);
1208 push_ptoken_context (pfile, NULL, pragma_buff,
1209 (const cpp_token **) pragma_buff->base,
1210 tokens_count);
1211 pragma_buff = tail;
1212 if (!CPP_OPTION (pfile, track_macro_expansion))
1213 num_macro_tokens_counter += tokens_count;
1216 while (pragma_buff != NULL);
1217 pfile->about_to_expand_macro_p = false;
1218 return 2;
1221 pfile->about_to_expand_macro_p = false;
1222 return 1;
1225 pfile->about_to_expand_macro_p = false;
1226 /* Handle built-in macros and the _Pragma operator. */
1227 return builtin_macro (pfile, node, location);
1230 /* De-allocate the memory used by BUFF which is an array of instances
1231 of macro_arg. NUM_ARGS is the number of instances of macro_arg
1232 present in BUFF. */
1233 static void
1234 delete_macro_args (_cpp_buff *buff, unsigned num_args)
1236 macro_arg *macro_args;
1237 unsigned i;
1239 if (buff == NULL)
1240 return;
1242 macro_args = (macro_arg *) buff->base;
1244 /* Walk instances of macro_arg to free their expanded tokens as well
1245 as their macro_arg::virt_locs members. */
1246 for (i = 0; i < num_args; ++i)
1248 if (macro_args[i].expanded)
1250 free (macro_args[i].expanded);
1251 macro_args[i].expanded = NULL;
1253 if (macro_args[i].virt_locs)
1255 free (macro_args[i].virt_locs);
1256 macro_args[i].virt_locs = NULL;
1258 if (macro_args[i].expanded_virt_locs)
1260 free (macro_args[i].expanded_virt_locs);
1261 macro_args[i].expanded_virt_locs = NULL;
1264 _cpp_free_buff (buff);
1267 /* Set the INDEXth token of the macro argument ARG. TOKEN is the token
1268 to set, LOCATION is its virtual location. "Virtual" location means
1269 the location that encodes loci across macro expansion. Otherwise
1270 it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the
1271 argument ARG is supposed to contain. Note that ARG must be
1272 tailored so that it has enough room to contain INDEX + 1 numbers of
1273 tokens, at least. */
1274 static void
1275 set_arg_token (macro_arg *arg, const cpp_token *token,
1276 source_location location, size_t index,
1277 enum macro_arg_token_kind kind,
1278 bool track_macro_exp_p)
1280 const cpp_token **token_ptr;
1281 source_location *loc = NULL;
1283 token_ptr =
1284 arg_token_ptr_at (arg, index, kind,
1285 track_macro_exp_p ? &loc : NULL);
1286 *token_ptr = token;
1288 if (loc != NULL)
1290 #ifdef ENABLE_CHECKING
1291 if (kind == MACRO_ARG_TOKEN_STRINGIFIED
1292 || !track_macro_exp_p)
1293 /* We can't set the location of a stringified argument
1294 token and we can't set any location if we aren't tracking
1295 macro expansion locations. */
1296 abort ();
1297 #endif
1298 *loc = location;
1302 /* Get the pointer to the location of the argument token of the
1303 function-like macro argument ARG. This function must be called
1304 only when we -ftrack-macro-expansion is on. */
1305 static const source_location *
1306 get_arg_token_location (const macro_arg *arg,
1307 enum macro_arg_token_kind kind)
1309 const source_location *loc = NULL;
1310 const cpp_token **token_ptr =
1311 arg_token_ptr_at (arg, 0, kind, (source_location **) &loc);
1313 if (token_ptr == NULL)
1314 return NULL;
1316 return loc;
1319 /* Return the pointer to the INDEXth token of the macro argument ARG.
1320 KIND specifies the kind of token the macro argument ARG contains.
1321 If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
1322 of the virtual location of the returned token if the
1323 -ftrack-macro-expansion flag is on; otherwise, it's set to the
1324 spelling location of the returned token. */
1325 static const cpp_token **
1326 arg_token_ptr_at (const macro_arg *arg, size_t index,
1327 enum macro_arg_token_kind kind,
1328 source_location **virt_location)
1330 const cpp_token **tokens_ptr = NULL;
1332 switch (kind)
1334 case MACRO_ARG_TOKEN_NORMAL:
1335 tokens_ptr = arg->first;
1336 break;
1337 case MACRO_ARG_TOKEN_STRINGIFIED:
1338 tokens_ptr = (const cpp_token **) &arg->stringified;
1339 break;
1340 case MACRO_ARG_TOKEN_EXPANDED:
1341 tokens_ptr = arg->expanded;
1342 break;
1345 if (tokens_ptr == NULL)
1346 /* This can happen for e.g, an empty token argument to a
1347 funtion-like macro. */
1348 return tokens_ptr;
1350 if (virt_location)
1352 if (kind == MACRO_ARG_TOKEN_NORMAL)
1353 *virt_location = &arg->virt_locs[index];
1354 else if (kind == MACRO_ARG_TOKEN_EXPANDED)
1355 *virt_location = &arg->expanded_virt_locs[index];
1356 else if (kind == MACRO_ARG_TOKEN_STRINGIFIED)
1357 *virt_location =
1358 (source_location *) &tokens_ptr[index]->src_loc;
1360 return &tokens_ptr[index];
1363 /* Initialize an iterator so that it iterates over the tokens of a
1364 function-like macro argument. KIND is the kind of tokens we want
1365 ITER to iterate over. TOKEN_PTR points the first token ITER will
1366 iterate over. */
1367 static void
1368 macro_arg_token_iter_init (macro_arg_token_iter *iter,
1369 bool track_macro_exp_p,
1370 enum macro_arg_token_kind kind,
1371 const macro_arg *arg,
1372 const cpp_token **token_ptr)
1374 iter->track_macro_exp_p = track_macro_exp_p;
1375 iter->kind = kind;
1376 iter->token_ptr = token_ptr;
1377 /* Unconditionally initialize this so that the compiler doesn't warn
1378 about iter->location_ptr being possibly uninitialized later after
1379 this code has been inlined somewhere. */
1380 iter->location_ptr = NULL;
1381 if (track_macro_exp_p)
1382 iter->location_ptr = get_arg_token_location (arg, kind);
1383 #ifdef ENABLE_CHECKING
1384 iter->num_forwards = 0;
1385 if (track_macro_exp_p
1386 && token_ptr != NULL
1387 && iter->location_ptr == NULL)
1388 abort ();
1389 #endif
1392 /* Move the iterator one token forward. Note that if IT was
1393 initialized on an argument that has a stringified token, moving it
1394 forward doesn't make sense as a stringified token is essentially one
1395 string. */
1396 static void
1397 macro_arg_token_iter_forward (macro_arg_token_iter *it)
1399 switch (it->kind)
1401 case MACRO_ARG_TOKEN_NORMAL:
1402 case MACRO_ARG_TOKEN_EXPANDED:
1403 it->token_ptr++;
1404 if (it->track_macro_exp_p)
1405 it->location_ptr++;
1406 break;
1407 case MACRO_ARG_TOKEN_STRINGIFIED:
1408 #ifdef ENABLE_CHECKING
1409 if (it->num_forwards > 0)
1410 abort ();
1411 #endif
1412 break;
1415 #ifdef ENABLE_CHECKING
1416 it->num_forwards++;
1417 #endif
1420 /* Return the token pointed to by the iterator. */
1421 static const cpp_token *
1422 macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
1424 #ifdef ENABLE_CHECKING
1425 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1426 && it->num_forwards > 0)
1427 abort ();
1428 #endif
1429 if (it->token_ptr == NULL)
1430 return NULL;
1431 return *it->token_ptr;
1434 /* Return the location of the token pointed to by the iterator.*/
1435 static source_location
1436 macro_arg_token_iter_get_location (const macro_arg_token_iter *it)
1438 #ifdef ENABLE_CHECKING
1439 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1440 && it->num_forwards > 0)
1441 abort ();
1442 #endif
1443 if (it->track_macro_exp_p)
1444 return *it->location_ptr;
1445 else
1446 return (*it->token_ptr)->src_loc;
1449 /* Return the index of a token [resulting from macro expansion] inside
1450 the total list of tokens resulting from a given macro
1451 expansion. The index can be different depending on whether if we
1452 want each tokens resulting from function-like macro arguments
1453 expansion to have a different location or not.
1455 E.g, consider this function-like macro:
1457 #define M(x) x - 3
1459 Then consider us "calling" it (and thus expanding it) like:
1461 M(1+4)
1463 It will be expanded into:
1465 1+4-3
1467 Let's consider the case of the token '4'.
1469 Its index can be 2 (it's the third token of the set of tokens
1470 resulting from the expansion) or it can be 0 if we consider that
1471 all tokens resulting from the expansion of the argument "1+2" have
1472 the same index, which is 0. In this later case, the index of token
1473 '-' would then be 1 and the index of token '3' would be 2.
1475 The later case is useful to use less memory e.g, for the case of
1476 the user using the option -ftrack-macro-expansion=1.
1478 ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
1479 are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro
1480 parameter (inside the macro replacement list) that corresponds to
1481 the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
1484 If we refer to the example above, for the '4' argument token,
1485 ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
1486 would be set to the token 'x', in the replacement list "x - 3" of
1487 macro M.
1489 This is a subroutine of replace_args. */
1490 inline static unsigned
1491 expanded_token_index (cpp_reader *pfile, cpp_macro *macro,
1492 const cpp_token *cur_replacement_token,
1493 unsigned absolute_token_index)
1495 if (CPP_OPTION (pfile, track_macro_expansion) > 1)
1496 return absolute_token_index;
1497 return cur_replacement_token - macro->exp.tokens;
1500 /* Replace the parameters in a function-like macro of NODE with the
1501 actual ARGS, and place the result in a newly pushed token context.
1502 Expand each argument before replacing, unless it is operated upon
1503 by the # or ## operators. EXPANSION_POINT_LOC is the location of
1504 the expansion point of the macro. E.g, the location of the
1505 function-like macro invocation. */
1506 static void
1507 replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
1508 macro_arg *args, source_location expansion_point_loc)
1510 unsigned int i, total;
1511 const cpp_token *src, *limit;
1512 const cpp_token **first = NULL;
1513 macro_arg *arg;
1514 _cpp_buff *buff = NULL;
1515 source_location *virt_locs = NULL;
1516 unsigned int exp_count;
1517 const struct line_map *map = NULL;
1518 int track_macro_exp;
1520 /* First, fully macro-expand arguments, calculating the number of
1521 tokens in the final expansion as we go. The ordering of the if
1522 statements below is subtle; we must handle stringification before
1523 pasting. */
1525 /* EXP_COUNT is the number of tokens in the macro replacement
1526 list. TOTAL is the number of tokens /after/ macro parameters
1527 have been replaced by their arguments. */
1528 exp_count = macro_real_token_count (macro);
1529 total = exp_count;
1530 limit = macro->exp.tokens + exp_count;
1532 for (src = macro->exp.tokens; src < limit; src++)
1533 if (src->type == CPP_MACRO_ARG)
1535 /* Leading and trailing padding tokens. */
1536 total += 2;
1537 /* Account for leading and padding tokens in exp_count too.
1538 This is going to be important later down this function,
1539 when we want to handle the case of (track_macro_exp <
1540 2). */
1541 exp_count += 2;
1543 /* We have an argument. If it is not being stringified or
1544 pasted it is macro-replaced before insertion. */
1545 arg = &args[src->val.macro_arg.arg_no - 1];
1547 if (src->flags & STRINGIFY_ARG)
1549 if (!arg->stringified)
1550 arg->stringified = stringify_arg (pfile, arg);
1552 else if ((src->flags & PASTE_LEFT)
1553 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
1554 total += arg->count - 1;
1555 else
1557 if (!arg->expanded)
1558 expand_arg (pfile, arg);
1559 total += arg->expanded_count - 1;
1563 /* When the compiler is called with the -ftrack-macro-expansion
1564 flag, we need to keep track of the location of each token that
1565 results from macro expansion.
1567 A token resulting from macro expansion is not a new token. It is
1568 simply the same token as the token coming from the macro
1569 definition. The new things that are allocated are the buffer
1570 that holds the tokens resulting from macro expansion and a new
1571 location that records many things like the locus of the expansion
1572 point as well as the original locus inside the definition of the
1573 macro. This location is called a virtual location.
1575 So the buffer BUFF holds a set of cpp_token*, and the buffer
1576 VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
1578 Both of these two buffers are going to be hung off of the macro
1579 context, when the latter is pushed. The memory allocated to
1580 store the tokens and their locations is going to be freed once
1581 the context of macro expansion is popped.
1583 As far as tokens are concerned, the memory overhead of
1584 -ftrack-macro-expansion is proportional to the number of
1585 macros that get expanded multiplied by sizeof (source_location).
1586 The good news is that extra memory gets freed when the macro
1587 context is freed, i.e shortly after the macro got expanded. */
1589 /* Is the -ftrack-macro-expansion flag in effect? */
1590 track_macro_exp = CPP_OPTION (pfile, track_macro_expansion);
1592 /* Now allocate memory space for tokens and locations resulting from
1593 the macro expansion, copy the tokens and replace the arguments.
1594 This memory must be freed when the context of the macro MACRO is
1595 popped. */
1596 buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL);
1598 first = (const cpp_token **) buff->base;
1600 /* Create a macro map to record the locations of the tokens that are
1601 involved in the expansion. Note that the expansion point is set
1602 to the location of the closing parenthesis. Otherwise, the
1603 subsequent map created for the first token that comes after the
1604 macro map might have a wrong line number. That would lead to
1605 tokens with wrong line numbers after the macro expansion. This
1606 adds up to the memory overhead of the -ftrack-macro-expansion
1607 flag; for every macro that is expanded, a "macro map" is
1608 created. */
1609 if (track_macro_exp)
1611 int num_macro_tokens = total;
1612 if (track_macro_exp < 2)
1613 /* Then the number of macro tokens won't take in account the
1614 fact that function-like macro arguments can expand to
1615 multiple tokens. This is to save memory at the expense of
1616 accuracy.
1618 Suppose we have #define SQARE(A) A * A
1620 And then we do SQARE(2+3)
1622 Then the tokens 2, +, 3, will have the same location,
1623 saying they come from the expansion of the argument A. */
1624 num_macro_tokens = exp_count;
1625 map = linemap_enter_macro (pfile->line_table, node,
1626 expansion_point_loc,
1627 num_macro_tokens);
1629 i = 0;
1630 for (src = macro->exp.tokens; src < limit; src++)
1632 unsigned int arg_tokens_count;
1633 macro_arg_token_iter from;
1634 const cpp_token **paste_flag = NULL;
1635 const cpp_token **tmp_token_ptr;
1637 if (src->type != CPP_MACRO_ARG)
1639 /* Allocate a virtual location for token SRC, and add that
1640 token and its virtual location into the buffers BUFF and
1641 VIRT_LOCS. */
1642 unsigned index = expanded_token_index (pfile, macro, src, i);
1643 tokens_buff_add_token (buff, virt_locs, src,
1644 src->src_loc, src->src_loc,
1645 map, index);
1646 i += 1;
1647 continue;
1650 paste_flag = 0;
1651 arg = &args[src->val.macro_arg.arg_no - 1];
1652 /* SRC is a macro parameter that we need to replace with its
1653 corresponding argument. So at some point we'll need to
1654 iterate over the tokens of the macro argument and copy them
1655 into the "place" now holding the correspondig macro
1656 parameter. We are going to use the iterator type
1657 macro_argo_token_iter to handle that iterating. The 'if'
1658 below is to initialize the iterator depending on the type of
1659 tokens the macro argument has. It also does some adjustment
1660 related to padding tokens and some pasting corner cases. */
1661 if (src->flags & STRINGIFY_ARG)
1663 arg_tokens_count = 1;
1664 macro_arg_token_iter_init (&from,
1665 CPP_OPTION (pfile,
1666 track_macro_expansion),
1667 MACRO_ARG_TOKEN_STRINGIFIED,
1668 arg, &arg->stringified);
1670 else if (src->flags & PASTE_LEFT)
1672 arg_tokens_count = arg->count;
1673 macro_arg_token_iter_init (&from,
1674 CPP_OPTION (pfile,
1675 track_macro_expansion),
1676 MACRO_ARG_TOKEN_NORMAL,
1677 arg, arg->first);
1679 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
1681 int num_toks;
1682 arg_tokens_count = arg->count;
1683 macro_arg_token_iter_init (&from,
1684 CPP_OPTION (pfile,
1685 track_macro_expansion),
1686 MACRO_ARG_TOKEN_NORMAL,
1687 arg, arg->first);
1689 num_toks = tokens_buff_count (buff);
1691 if (num_toks != 0)
1693 /* So the current parameter token is pasted to the previous
1694 token in the replacement list. Let's look at what
1695 we have as previous and current arguments. */
1697 /* This is the previous argument's token ... */
1698 tmp_token_ptr = tokens_buff_last_token_ptr (buff);
1700 if ((*tmp_token_ptr)->type == CPP_COMMA
1701 && macro->variadic
1702 && src->val.macro_arg.arg_no == macro->paramc)
1704 /* ... which is a comma; and the current parameter
1705 is the last parameter of a variadic function-like
1706 macro. If the argument to the current last
1707 parameter is NULL, then swallow the comma,
1708 otherwise drop the paste flag. */
1709 if (macro_arg_token_iter_get_token (&from) == NULL)
1710 tokens_buff_remove_last_token (buff);
1711 else
1712 paste_flag = tmp_token_ptr;
1714 /* Remove the paste flag if the RHS is a placemarker. */
1715 else if (arg_tokens_count == 0)
1716 paste_flag = tmp_token_ptr;
1719 else
1721 arg_tokens_count = arg->expanded_count;
1722 macro_arg_token_iter_init (&from,
1723 CPP_OPTION (pfile,
1724 track_macro_expansion),
1725 MACRO_ARG_TOKEN_EXPANDED,
1726 arg, arg->expanded);
1729 /* Padding on the left of an argument (unless RHS of ##). */
1730 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
1731 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
1733 const cpp_token *t = padding_token (pfile, src);
1734 unsigned index = expanded_token_index (pfile, macro, src, i);
1735 /* Allocate a virtual location for the padding token and
1736 append the token and its location to BUFF and
1737 VIRT_LOCS. */
1738 tokens_buff_add_token (buff, virt_locs, t,
1739 t->src_loc, t->src_loc,
1740 map, index);
1743 if (arg_tokens_count)
1745 /* So now we've got the number of tokens that make up the
1746 argument that is going to replace the current parameter
1747 in the macro's replacement list. */
1748 unsigned int j;
1749 for (j = 0; j < arg_tokens_count; ++j)
1751 /* So if track_macro_exp is < 2, the user wants to
1752 save extra memory while tracking macro expansion
1753 locations. So in that case here is what we do:
1755 Suppose we have #define SQARE(A) A * A
1757 And then we do SQARE(2+3)
1759 Then the tokens 2, +, 3, will have the same location,
1760 saying they come from the expansion of the argument
1763 So that means we are going to ignore the COUNT tokens
1764 resulting from the expansion of the current macro
1765 arugment. In other words all the ARG_TOKENS_COUNT tokens
1766 resulting from the expansion of the macro argument will
1767 have the index I. Normally, each of those token should
1768 have index I+J. */
1769 unsigned token_index = i;
1770 unsigned index;
1771 if (track_macro_exp > 1)
1772 token_index += j;
1774 index = expanded_token_index (pfile, macro, src, token_index);
1775 tokens_buff_add_token (buff, virt_locs,
1776 macro_arg_token_iter_get_token (&from),
1777 macro_arg_token_iter_get_location (&from),
1778 src->src_loc, map, index);
1779 macro_arg_token_iter_forward (&from);
1782 /* With a non-empty argument on the LHS of ##, the last
1783 token should be flagged PASTE_LEFT. */
1784 if (src->flags & PASTE_LEFT)
1785 paste_flag =
1786 (const cpp_token **) tokens_buff_last_token_ptr (buff);
1788 else if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99)
1789 && ! macro->syshdr && ! cpp_in_system_header (pfile))
1791 if (CPP_OPTION (pfile, cplusplus))
1792 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
1793 "invoking macro %s argument %d: "
1794 "empty macro arguments are undefined"
1795 " in ISO C++98",
1796 NODE_NAME (node), src->val.macro_arg.arg_no);
1797 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat))
1798 cpp_pedwarning (pfile,
1799 CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
1800 ? CPP_W_C90_C99_COMPAT : CPP_W_PEDANTIC,
1801 "invoking macro %s argument %d: "
1802 "empty macro arguments are undefined"
1803 " in ISO C90",
1804 NODE_NAME (node), src->val.macro_arg.arg_no);
1806 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
1807 && ! CPP_OPTION (pfile, cplusplus)
1808 && ! macro->syshdr && ! cpp_in_system_header (pfile))
1809 cpp_warning (pfile, CPP_W_C90_C99_COMPAT,
1810 "invoking macro %s argument %d: "
1811 "empty macro arguments are undefined"
1812 " in ISO C90",
1813 NODE_NAME (node), src->val.macro_arg.arg_no);
1815 /* Avoid paste on RHS (even case count == 0). */
1816 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
1818 const cpp_token *t = &pfile->avoid_paste;
1819 tokens_buff_add_token (buff, virt_locs,
1820 t, t->src_loc, t->src_loc,
1821 NULL, 0);
1824 /* Add a new paste flag, or remove an unwanted one. */
1825 if (paste_flag)
1827 cpp_token *token = _cpp_temp_token (pfile);
1828 token->type = (*paste_flag)->type;
1829 token->val = (*paste_flag)->val;
1830 if (src->flags & PASTE_LEFT)
1831 token->flags = (*paste_flag)->flags | PASTE_LEFT;
1832 else
1833 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
1834 *paste_flag = token;
1837 i += arg_tokens_count;
1840 if (track_macro_exp)
1841 push_extended_tokens_context (pfile, node, buff, virt_locs, first,
1842 tokens_buff_count (buff));
1843 else
1844 push_ptoken_context (pfile, node, buff, first,
1845 tokens_buff_count (buff));
1847 num_macro_tokens_counter += tokens_buff_count (buff);
1850 /* Return a special padding token, with padding inherited from SOURCE. */
1851 static const cpp_token *
1852 padding_token (cpp_reader *pfile, const cpp_token *source)
1854 cpp_token *result = _cpp_temp_token (pfile);
1856 result->type = CPP_PADDING;
1858 /* Data in GCed data structures cannot be made const so far, so we
1859 need a cast here. */
1860 result->val.source = (cpp_token *) source;
1861 result->flags = 0;
1862 return result;
1865 /* Get a new uninitialized context. Create a new one if we cannot
1866 re-use an old one. */
1867 static cpp_context *
1868 next_context (cpp_reader *pfile)
1870 cpp_context *result = pfile->context->next;
1872 if (result == 0)
1874 result = XNEW (cpp_context);
1875 memset (result, 0, sizeof (cpp_context));
1876 result->prev = pfile->context;
1877 result->next = 0;
1878 pfile->context->next = result;
1881 pfile->context = result;
1882 return result;
1885 /* Push a list of pointers to tokens. */
1886 static void
1887 push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
1888 const cpp_token **first, unsigned int count)
1890 cpp_context *context = next_context (pfile);
1892 context->tokens_kind = TOKENS_KIND_INDIRECT;
1893 context->c.macro = macro;
1894 context->buff = buff;
1895 FIRST (context).ptoken = first;
1896 LAST (context).ptoken = first + count;
1899 /* Push a list of tokens.
1901 A NULL macro means that we should continue the current macro
1902 expansion, in essence. That means that if we are currently in a
1903 macro expansion context, we'll make the new pfile->context refer to
1904 the current macro. */
1905 void
1906 _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
1907 const cpp_token *first, unsigned int count)
1909 cpp_context *context;
1911 if (macro == NULL)
1912 macro = macro_of_context (pfile->context);
1914 context = next_context (pfile);
1915 context->tokens_kind = TOKENS_KIND_DIRECT;
1916 context->c.macro = macro;
1917 context->buff = NULL;
1918 FIRST (context).token = first;
1919 LAST (context).token = first + count;
1922 /* Build a context containing a list of tokens as well as their
1923 virtual locations and push it. TOKENS_BUFF is the buffer that
1924 contains the tokens pointed to by FIRST. If TOKENS_BUFF is
1925 non-NULL, it means that the context owns it, meaning that
1926 _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
1927 contains the virtual locations.
1929 A NULL macro means that we should continue the current macro
1930 expansion, in essence. That means that if we are currently in a
1931 macro expansion context, we'll make the new pfile->context refer to
1932 the current macro. */
1933 static void
1934 push_extended_tokens_context (cpp_reader *pfile,
1935 cpp_hashnode *macro,
1936 _cpp_buff *token_buff,
1937 source_location *virt_locs,
1938 const cpp_token **first,
1939 unsigned int count)
1941 cpp_context *context;
1942 macro_context *m;
1944 if (macro == NULL)
1945 macro = macro_of_context (pfile->context);
1947 context = next_context (pfile);
1948 context->tokens_kind = TOKENS_KIND_EXTENDED;
1949 context->buff = token_buff;
1951 m = XNEW (macro_context);
1952 m->macro_node = macro;
1953 m->virt_locs = virt_locs;
1954 m->cur_virt_loc = virt_locs;
1955 context->c.mc = m;
1956 FIRST (context).ptoken = first;
1957 LAST (context).ptoken = first + count;
1960 /* Push a traditional macro's replacement text. */
1961 void
1962 _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
1963 const uchar *start, size_t len)
1965 cpp_context *context = next_context (pfile);
1967 context->tokens_kind = TOKENS_KIND_DIRECT;
1968 context->c.macro = macro;
1969 context->buff = NULL;
1970 CUR (context) = start;
1971 RLIMIT (context) = start + len;
1972 macro->flags |= NODE_DISABLED;
1975 /* Creates a buffer that holds tokens a.k.a "token buffer", usually
1976 for the purpose of storing them on a cpp_context. If VIRT_LOCS is
1977 non-null (which means that -ftrack-macro-expansion is on),
1978 *VIRT_LOCS is set to a newly allocated buffer that is supposed to
1979 hold the virtual locations of the tokens resulting from macro
1980 expansion. */
1981 static _cpp_buff*
1982 tokens_buff_new (cpp_reader *pfile, size_t len,
1983 source_location **virt_locs)
1985 size_t tokens_size = len * sizeof (cpp_token *);
1986 size_t locs_size = len * sizeof (source_location);
1988 if (virt_locs != NULL)
1989 *virt_locs = XNEWVEC (source_location, locs_size);
1990 return _cpp_get_buff (pfile, tokens_size);
1993 /* Returns the number of tokens contained in a token buffer. The
1994 buffer holds a set of cpp_token*. */
1995 static size_t
1996 tokens_buff_count (_cpp_buff *buff)
1998 return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *);
2001 /* Return a pointer to the last token contained in the token buffer
2002 BUFF. */
2003 static const cpp_token **
2004 tokens_buff_last_token_ptr (_cpp_buff *buff)
2006 return &((const cpp_token **) BUFF_FRONT (buff))[-1];
2009 /* Remove the last token contained in the token buffer TOKENS_BUFF.
2010 If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer
2011 containing the virtual locations of the tokens in TOKENS_BUFF; in
2012 which case the function updates that buffer as well. */
2013 static inline void
2014 tokens_buff_remove_last_token (_cpp_buff *tokens_buff)
2017 if (BUFF_FRONT (tokens_buff) > tokens_buff->base)
2018 BUFF_FRONT (tokens_buff) =
2019 (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1];
2022 /* Insert a token into the token buffer at the position pointed to by
2023 DEST. Note that the buffer is not enlarged so the previous token
2024 that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null,
2025 means -ftrack-macro-expansion is effect; it then points to where to
2026 insert the virtual location of TOKEN. TOKEN is the token to
2027 insert. VIRT_LOC is the virtual location of the token, i.e, the
2028 location possibly encoding its locus across macro expansion. If
2029 TOKEN is an argument of a function-like macro (inside a macro
2030 replacement list), PARM_DEF_LOC is the spelling location of the
2031 macro parameter that TOKEN is replacing, in the replacement list of
2032 the macro. If TOKEN is not an argument of a function-like macro or
2033 if it doesn't come from a macro expansion, then VIRT_LOC can just
2034 be set to the same value as PARM_DEF_LOC. If MAP is non null, it
2035 means TOKEN comes from a macro expansion and MAP is the macro map
2036 associated to the macro. MACRO_TOKEN_INDEX points to the index of
2037 the token in the macro map; it is not considered if MAP is NULL.
2039 Upon successful completion this function returns the a pointer to
2040 the position of the token coming right after the insertion
2041 point. */
2042 static inline const cpp_token **
2043 tokens_buff_put_token_to (const cpp_token **dest,
2044 source_location *virt_loc_dest,
2045 const cpp_token *token,
2046 source_location virt_loc,
2047 source_location parm_def_loc,
2048 const struct line_map *map,
2049 unsigned int macro_token_index)
2051 source_location macro_loc = virt_loc;
2052 const cpp_token **result;
2054 if (virt_loc_dest)
2056 /* -ftrack-macro-expansion is on. */
2057 if (map)
2058 macro_loc = linemap_add_macro_token (map, macro_token_index,
2059 virt_loc, parm_def_loc);
2060 *virt_loc_dest = macro_loc;
2062 *dest = token;
2063 result = &dest[1];
2065 return result;
2068 /* Adds a token at the end of the tokens contained in BUFFER. Note
2069 that this function doesn't enlarge BUFFER when the number of tokens
2070 reaches BUFFER's size; it aborts in that situation.
2072 TOKEN is the token to append. VIRT_LOC is the virtual location of
2073 the token, i.e, the location possibly encoding its locus across
2074 macro expansion. If TOKEN is an argument of a function-like macro
2075 (inside a macro replacement list), PARM_DEF_LOC is the location of
2076 the macro parameter that TOKEN is replacing. If TOKEN doesn't come
2077 from a macro expansion, then VIRT_LOC can just be set to the same
2078 value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes
2079 from a macro expansion and MAP is the macro map associated to the
2080 macro. MACRO_TOKEN_INDEX points to the index of the token in the
2081 macro map; It is not considered if MAP is NULL. If VIRT_LOCS is
2082 non-null, it means -ftrack-macro-expansion is on; in which case
2083 this function adds the virtual location DEF_LOC to the VIRT_LOCS
2084 array, at the same index as the one of TOKEN in BUFFER. Upon
2085 successful completion this function returns the a pointer to the
2086 position of the token coming right after the insertion point. */
2087 static const cpp_token **
2088 tokens_buff_add_token (_cpp_buff *buffer,
2089 source_location *virt_locs,
2090 const cpp_token *token,
2091 source_location virt_loc,
2092 source_location parm_def_loc,
2093 const struct line_map *map,
2094 unsigned int macro_token_index)
2096 const cpp_token **result;
2097 source_location *virt_loc_dest = NULL;
2098 unsigned token_index =
2099 (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
2101 /* Abort if we pass the end the buffer. */
2102 if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer))
2103 abort ();
2105 if (virt_locs != NULL)
2106 virt_loc_dest = &virt_locs[token_index];
2108 result =
2109 tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer),
2110 virt_loc_dest, token, virt_loc, parm_def_loc,
2111 map, macro_token_index);
2113 BUFF_FRONT (buffer) = (unsigned char *) result;
2114 return result;
2117 /* Allocate space for the function-like macro argument ARG to store
2118 the tokens resulting from the macro-expansion of the tokens that
2119 make up ARG itself. That space is allocated in ARG->expanded and
2120 needs to be freed using free. */
2121 static void
2122 alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity)
2124 #ifdef ENABLE_CHECKING
2125 if (arg->expanded != NULL
2126 || arg->expanded_virt_locs != NULL)
2127 abort ();
2128 #endif
2129 arg->expanded = XNEWVEC (const cpp_token *, capacity);
2130 if (CPP_OPTION (pfile, track_macro_expansion))
2131 arg->expanded_virt_locs = XNEWVEC (source_location, capacity);
2135 /* If necessary, enlarge ARG->expanded to so that it can contain SIZE
2136 tokens. */
2137 static void
2138 ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg,
2139 size_t size, size_t *expanded_capacity)
2141 if (size <= *expanded_capacity)
2142 return;
2144 size *= 2;
2146 arg->expanded =
2147 XRESIZEVEC (const cpp_token *, arg->expanded, size);
2148 *expanded_capacity = size;
2150 if (CPP_OPTION (pfile, track_macro_expansion))
2152 if (arg->expanded_virt_locs == NULL)
2153 arg->expanded_virt_locs = XNEWVEC (source_location, size);
2154 else
2155 arg->expanded_virt_locs = XRESIZEVEC (source_location,
2156 arg->expanded_virt_locs,
2157 size);
2161 /* Expand an argument ARG before replacing parameters in a
2162 function-like macro. This works by pushing a context with the
2163 argument's tokens, and then expanding that into a temporary buffer
2164 as if it were a normal part of the token stream. collect_args()
2165 has terminated the argument's tokens with a CPP_EOF so that we know
2166 when we have fully expanded the argument. */
2167 static void
2168 expand_arg (cpp_reader *pfile, macro_arg *arg)
2170 size_t capacity;
2171 bool saved_warn_trad;
2172 bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
2174 if (arg->count == 0
2175 || arg->expanded != NULL)
2176 return;
2178 /* Don't warn about funlike macros when pre-expanding. */
2179 saved_warn_trad = CPP_WTRADITIONAL (pfile);
2180 CPP_WTRADITIONAL (pfile) = 0;
2182 /* Loop, reading in the tokens of the argument. */
2183 capacity = 256;
2184 alloc_expanded_arg_mem (pfile, arg, capacity);
2186 if (track_macro_exp_p)
2187 push_extended_tokens_context (pfile, NULL, NULL,
2188 arg->virt_locs,
2189 arg->first,
2190 arg->count + 1);
2191 else
2192 push_ptoken_context (pfile, NULL, NULL,
2193 arg->first, arg->count + 1);
2195 for (;;)
2197 const cpp_token *token;
2198 source_location location;
2200 ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1,
2201 &capacity);
2203 token = cpp_get_token_1 (pfile, &location);
2205 if (token->type == CPP_EOF)
2206 break;
2208 set_arg_token (arg, token, location,
2209 arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED,
2210 CPP_OPTION (pfile, track_macro_expansion));
2211 arg->expanded_count++;
2214 _cpp_pop_context (pfile);
2216 CPP_WTRADITIONAL (pfile) = saved_warn_trad;
2219 /* Returns the macro associated to the current context if we are in
2220 the context a macro expansion, NULL otherwise. */
2221 static cpp_hashnode*
2222 macro_of_context (cpp_context *context)
2224 if (context == NULL)
2225 return NULL;
2227 return (context->tokens_kind == TOKENS_KIND_EXTENDED)
2228 ? context->c.mc->macro_node
2229 : context->c.macro;
2232 /* Return TRUE iff we are expanding a macro or are about to start
2233 expanding one. If we are effectively expanding a macro, the
2234 function macro_of_context returns a pointer to the macro being
2235 expanded. */
2236 static bool
2237 in_macro_expansion_p (cpp_reader *pfile)
2239 if (pfile == NULL)
2240 return false;
2242 return (pfile->about_to_expand_macro_p
2243 || macro_of_context (pfile->context));
2246 /* Pop the current context off the stack, re-enabling the macro if the
2247 context represented a macro's replacement list. Initially the
2248 context structure was not freed so that we can re-use it later, but
2249 now we do free it to reduce peak memory consumption. */
2250 void
2251 _cpp_pop_context (cpp_reader *pfile)
2253 cpp_context *context = pfile->context;
2255 /* We should not be popping the base context. */
2256 if (context == &pfile->base_context)
2257 abort ();
2259 if (context->c.macro)
2261 cpp_hashnode *macro;
2262 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
2264 macro_context *mc = context->c.mc;
2265 macro = mc->macro_node;
2266 /* If context->buff is set, it means the life time of tokens
2267 is bound to the life time of this context; so we must
2268 free the tokens; that means we must free the virtual
2269 locations of these tokens too. */
2270 if (context->buff && mc->virt_locs)
2272 free (mc->virt_locs);
2273 mc->virt_locs = NULL;
2275 free (mc);
2276 context->c.mc = NULL;
2278 else
2279 macro = context->c.macro;
2281 /* Beware that MACRO can be NULL in cases like when we are
2282 called from expand_arg. In those cases, a dummy context with
2283 tokens is pushed just for the purpose of walking them using
2284 cpp_get_token_1. In that case, no 'macro' field is set into
2285 the dummy context. */
2286 if (macro != NULL
2287 /* Several contiguous macro expansion contexts can be
2288 associated to the same macro; that means it's the same
2289 macro expansion that spans across all these (sub)
2290 contexts. So we should re-enable an expansion-disabled
2291 macro only when we are sure we are really out of that
2292 macro expansion. */
2293 && macro_of_context (context->prev) != macro)
2294 macro->flags &= ~NODE_DISABLED;
2297 if (context->buff)
2299 /* Decrease memory peak consumption by freeing the memory used
2300 by the context. */
2301 _cpp_free_buff (context->buff);
2304 pfile->context = context->prev;
2305 /* decrease peak memory consumption by feeing the context. */
2306 pfile->context->next = NULL;
2307 free (context);
2310 /* Return TRUE if we reached the end of the set of tokens stored in
2311 CONTEXT, FALSE otherwise. */
2312 static inline bool
2313 reached_end_of_context (cpp_context *context)
2315 if (context->tokens_kind == TOKENS_KIND_DIRECT)
2316 return FIRST (context).token == LAST (context).token;
2317 else if (context->tokens_kind == TOKENS_KIND_INDIRECT
2318 || context->tokens_kind == TOKENS_KIND_EXTENDED)
2319 return FIRST (context).ptoken == LAST (context).ptoken;
2320 else
2321 abort ();
2324 /* Consume the next token contained in the current context of PFILE,
2325 and return it in *TOKEN. It's "full location" is returned in
2326 *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
2327 means the location encoding the locus of the token across macro
2328 expansion; otherwise it's just is the "normal" location of the
2329 token which (*TOKEN)->src_loc. */
2330 static inline void
2331 consume_next_token_from_context (cpp_reader *pfile,
2332 const cpp_token ** token,
2333 source_location *location)
2335 cpp_context *c = pfile->context;
2337 if ((c)->tokens_kind == TOKENS_KIND_DIRECT)
2339 *token = FIRST (c).token;
2340 *location = (*token)->src_loc;
2341 FIRST (c).token++;
2343 else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT)
2345 *token = *FIRST (c).ptoken;
2346 *location = (*token)->src_loc;
2347 FIRST (c).ptoken++;
2349 else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED)
2351 macro_context *m = c->c.mc;
2352 *token = *FIRST (c).ptoken;
2353 if (m->virt_locs)
2355 *location = *m->cur_virt_loc;
2356 m->cur_virt_loc++;
2358 else
2359 *location = (*token)->src_loc;
2360 FIRST (c).ptoken++;
2362 else
2363 abort ();
2366 /* In the traditional mode of the preprocessor, if we are currently in
2367 a directive, the location of a token must be the location of the
2368 start of the directive line. This function returns the proper
2369 location if we are in the traditional mode, and just returns
2370 LOCATION otherwise. */
2372 static inline source_location
2373 maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, source_location location)
2375 if (CPP_OPTION (pfile, traditional))
2377 if (pfile->state.in_directive)
2378 return pfile->directive_line;
2380 return location;
2383 /* Routine to get a token as well as its location.
2385 Macro expansions and directives are transparently handled,
2386 including entering included files. Thus tokens are post-macro
2387 expansion, and after any intervening directives. External callers
2388 see CPP_EOF only at EOF. Internal callers also see it when meeting
2389 a directive inside a macro call, when at the end of a directive and
2390 state.in_directive is still 1, and at the end of argument
2391 pre-expansion.
2393 LOC is an out parameter; *LOC is set to the location "as expected
2394 by the user". Please read the comment of
2395 cpp_get_token_with_location to learn more about the meaning of this
2396 location. */
2397 static const cpp_token*
2398 cpp_get_token_1 (cpp_reader *pfile, source_location *location)
2400 const cpp_token *result;
2401 /* This token is a virtual token that either encodes a location
2402 related to macro expansion or a spelling location. */
2403 source_location virt_loc = 0;
2404 /* pfile->about_to_expand_macro_p can be overriden by indirect calls
2405 to functions that push macro contexts. So let's save it so that
2406 we can restore it when we are about to leave this routine. */
2407 bool saved_about_to_expand_macro = pfile->about_to_expand_macro_p;
2409 for (;;)
2411 cpp_hashnode *node;
2412 cpp_context *context = pfile->context;
2414 /* Context->prev == 0 <=> base context. */
2415 if (!context->prev)
2417 result = _cpp_lex_token (pfile);
2418 virt_loc = result->src_loc;
2420 else if (!reached_end_of_context (context))
2422 consume_next_token_from_context (pfile, &result,
2423 &virt_loc);
2424 if (result->flags & PASTE_LEFT)
2426 paste_all_tokens (pfile, result);
2427 if (pfile->state.in_directive)
2428 continue;
2429 result = padding_token (pfile, result);
2430 goto out;
2433 else
2435 if (pfile->context->c.macro)
2436 ++num_expanded_macros_counter;
2437 _cpp_pop_context (pfile);
2438 if (pfile->state.in_directive)
2439 continue;
2440 result = &pfile->avoid_paste;
2441 goto out;
2444 if (pfile->state.in_directive && result->type == CPP_COMMENT)
2445 continue;
2447 if (result->type != CPP_NAME)
2448 break;
2450 node = result->val.node.node;
2452 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
2453 break;
2455 if (!(node->flags & NODE_DISABLED))
2457 int ret = 0;
2458 /* If not in a macro context, and we're going to start an
2459 expansion, record the location. */
2460 if (!in_macro_expansion_p (pfile))
2461 pfile->invocation_location = result->src_loc;
2462 if (pfile->state.prevent_expansion)
2463 break;
2465 /* Conditional macros require that a predicate be evaluated
2466 first. */
2467 if ((node->flags & NODE_CONDITIONAL) != 0)
2469 if (pfile->cb.macro_to_expand)
2471 bool whitespace_after;
2472 const cpp_token *peek_tok = cpp_peek_token (pfile, 0);
2474 whitespace_after = (peek_tok->type == CPP_PADDING
2475 || (peek_tok->flags & PREV_WHITE));
2476 node = pfile->cb.macro_to_expand (pfile, result);
2477 if (node)
2478 ret = enter_macro_context (pfile, node, result,
2479 virt_loc);
2480 else if (whitespace_after)
2482 /* If macro_to_expand hook returned NULL and it
2483 ate some tokens, see if we don't need to add
2484 a padding token in between this and the
2485 next token. */
2486 peek_tok = cpp_peek_token (pfile, 0);
2487 if (peek_tok->type != CPP_PADDING
2488 && (peek_tok->flags & PREV_WHITE) == 0)
2489 _cpp_push_token_context (pfile, NULL,
2490 padding_token (pfile,
2491 peek_tok), 1);
2495 else
2496 ret = enter_macro_context (pfile, node, result,
2497 virt_loc);
2498 if (ret)
2500 if (pfile->state.in_directive || ret == 2)
2501 continue;
2502 result = padding_token (pfile, result);
2503 goto out;
2506 else
2508 /* Flag this token as always unexpandable. FIXME: move this
2509 to collect_args()?. */
2510 cpp_token *t = _cpp_temp_token (pfile);
2511 t->type = result->type;
2512 t->flags = result->flags | NO_EXPAND;
2513 t->val = result->val;
2514 result = t;
2517 break;
2520 out:
2521 if (location != NULL)
2523 if (virt_loc == 0)
2524 virt_loc = result->src_loc;
2525 *location = virt_loc;
2527 if (!CPP_OPTION (pfile, track_macro_expansion)
2528 && macro_of_context (pfile->context) != NULL)
2529 /* We are in a macro expansion context, are not tracking
2530 virtual location, but were asked to report the location
2531 of the expansion point of the macro being expanded. */
2532 *location = pfile->invocation_location;
2534 *location = maybe_adjust_loc_for_trad_cpp (pfile, *location);
2537 pfile->about_to_expand_macro_p = saved_about_to_expand_macro;
2538 return result;
2541 /* External routine to get a token. Also used nearly everywhere
2542 internally, except for places where we know we can safely call
2543 _cpp_lex_token directly, such as lexing a directive name.
2545 Macro expansions and directives are transparently handled,
2546 including entering included files. Thus tokens are post-macro
2547 expansion, and after any intervening directives. External callers
2548 see CPP_EOF only at EOF. Internal callers also see it when meeting
2549 a directive inside a macro call, when at the end of a directive and
2550 state.in_directive is still 1, and at the end of argument
2551 pre-expansion. */
2552 const cpp_token *
2553 cpp_get_token (cpp_reader *pfile)
2555 return cpp_get_token_1 (pfile, NULL);
2558 /* Like cpp_get_token, but also returns a virtual token location
2559 separate from the spelling location carried by the returned token.
2561 LOC is an out parameter; *LOC is set to the location "as expected
2562 by the user". This matters when a token results from macro
2563 expansion; in that case the token's spelling location indicates the
2564 locus of the token in the definition of the macro but *LOC
2565 virtually encodes all the other meaningful locuses associated to
2566 the token.
2568 What? virtual location? Yes, virtual location.
2570 If the token results from macro expansion and if macro expansion
2571 location tracking is enabled its virtual location encodes (at the
2572 same time):
2574 - the spelling location of the token
2576 - the locus of the macro expansion point
2578 - the locus of the point where the token got instantiated as part
2579 of the macro expansion process.
2581 You have to use the linemap API to get the locus you are interested
2582 in from a given virtual location.
2584 Note however that virtual locations are not necessarily ordered for
2585 relations '<' and '>'. One must use the function
2586 linemap_location_before_p instead of using the relational operator
2587 '<'.
2589 If macro expansion tracking is off and if the token results from
2590 macro expansion the virtual location is the expansion point of the
2591 macro that got expanded.
2593 When the token doesn't result from macro expansion, the virtual
2594 location is just the same thing as its spelling location. */
2596 const cpp_token *
2597 cpp_get_token_with_location (cpp_reader *pfile, source_location *loc)
2599 return cpp_get_token_1 (pfile, loc);
2602 /* Returns true if we're expanding an object-like macro that was
2603 defined in a system header. Just checks the macro at the top of
2604 the stack. Used for diagnostic suppression. */
2606 cpp_sys_macro_p (cpp_reader *pfile)
2608 cpp_hashnode *node = NULL;
2610 if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2611 node = pfile->context->c.mc->macro_node;
2612 else
2613 node = pfile->context->c.macro;
2615 return node && node->value.macro && node->value.macro->syshdr;
2618 /* Read each token in, until end of the current file. Directives are
2619 transparently processed. */
2620 void
2621 cpp_scan_nooutput (cpp_reader *pfile)
2623 /* Request a CPP_EOF token at the end of this file, rather than
2624 transparently continuing with the including file. */
2625 pfile->buffer->return_at_eof = true;
2627 pfile->state.discarding_output++;
2628 pfile->state.prevent_expansion++;
2630 if (CPP_OPTION (pfile, traditional))
2631 while (_cpp_read_logical_line_trad (pfile))
2633 else
2634 while (cpp_get_token (pfile)->type != CPP_EOF)
2637 pfile->state.discarding_output--;
2638 pfile->state.prevent_expansion--;
2641 /* Step back one or more tokens obtained from the lexer. */
2642 void
2643 _cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count)
2645 pfile->lookaheads += count;
2646 while (count--)
2648 pfile->cur_token--;
2649 if (pfile->cur_token == pfile->cur_run->base
2650 /* Possible with -fpreprocessed and no leading #line. */
2651 && pfile->cur_run->prev != NULL)
2653 pfile->cur_run = pfile->cur_run->prev;
2654 pfile->cur_token = pfile->cur_run->limit;
2659 /* Step back one (or more) tokens. Can only step back more than 1 if
2660 they are from the lexer, and not from macro expansion. */
2661 void
2662 _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
2664 if (pfile->context->prev == NULL)
2665 _cpp_backup_tokens_direct (pfile, count);
2666 else
2668 if (count != 1)
2669 abort ();
2670 if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT)
2671 FIRST (pfile->context).token--;
2672 else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT)
2673 FIRST (pfile->context).ptoken--;
2674 else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2676 FIRST (pfile->context).ptoken--;
2677 if (pfile->context->c.macro)
2679 macro_context *m = pfile->context->c.mc;
2680 m->cur_virt_loc--;
2681 #ifdef ENABLE_CHECKING
2682 if (m->cur_virt_loc < m->virt_locs)
2683 abort ();
2684 #endif
2686 else
2687 abort ();
2689 else
2690 abort ();
2694 /* #define directive parsing and handling. */
2696 /* Returns nonzero if a macro redefinition warning is required. */
2697 static bool
2698 warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
2699 const cpp_macro *macro2)
2701 const cpp_macro *macro1;
2702 unsigned int i;
2704 /* Some redefinitions need to be warned about regardless. */
2705 if (node->flags & NODE_WARN)
2706 return true;
2708 /* Suppress warnings for builtins that lack the NODE_WARN flag,
2709 unless Wbuiltin-macro-redefined. */
2710 if (node->flags & NODE_BUILTIN
2711 && (!pfile->cb.user_builtin_macro
2712 || !pfile->cb.user_builtin_macro (pfile, node)))
2713 return CPP_OPTION (pfile, warn_builtin_macro_redefined);
2715 /* Redefinitions of conditional (context-sensitive) macros, on
2716 the other hand, must be allowed silently. */
2717 if (node->flags & NODE_CONDITIONAL)
2718 return false;
2720 /* Redefinition of a macro is allowed if and only if the old and new
2721 definitions are the same. (6.10.3 paragraph 2). */
2722 macro1 = node->value.macro;
2724 /* Don't check count here as it can be different in valid
2725 traditional redefinitions with just whitespace differences. */
2726 if (macro1->paramc != macro2->paramc
2727 || macro1->fun_like != macro2->fun_like
2728 || macro1->variadic != macro2->variadic)
2729 return true;
2731 /* Check parameter spellings. */
2732 for (i = 0; i < macro1->paramc; i++)
2733 if (macro1->params[i] != macro2->params[i])
2734 return true;
2736 /* Check the replacement text or tokens. */
2737 if (CPP_OPTION (pfile, traditional))
2738 return _cpp_expansions_different_trad (macro1, macro2);
2740 if (macro1->count != macro2->count)
2741 return true;
2743 for (i = 0; i < macro1->count; i++)
2744 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
2745 return true;
2747 return false;
2750 /* Free the definition of hashnode H. */
2751 void
2752 _cpp_free_definition (cpp_hashnode *h)
2754 /* Macros and assertions no longer have anything to free. */
2755 h->type = NT_VOID;
2756 /* Clear builtin flag in case of redefinition. */
2757 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED);
2760 /* Save parameter NODE (spelling SPELLING) to the parameter list of
2761 macro MACRO. Returns zero on success, nonzero if the parameter is
2762 a duplicate. */
2763 bool
2764 _cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node,
2765 cpp_hashnode *spelling)
2767 unsigned int len;
2768 /* Constraint 6.10.3.6 - duplicate parameter names. */
2769 if (node->flags & NODE_MACRO_ARG)
2771 cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
2772 NODE_NAME (node));
2773 return true;
2776 if (BUFF_ROOM (pfile->a_buff)
2777 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
2778 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
2780 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = spelling;
2781 node->flags |= NODE_MACRO_ARG;
2782 len = macro->paramc * sizeof (struct macro_arg_saved_data);
2783 if (len > pfile->macro_buffer_len)
2785 pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer,
2786 len);
2787 pfile->macro_buffer_len = len;
2789 struct macro_arg_saved_data save;
2790 save.value = node->value;
2791 save.canonical_node = node;
2792 ((struct macro_arg_saved_data *) pfile->macro_buffer)[macro->paramc - 1]
2793 = save;
2795 node->value.arg_index = macro->paramc;
2796 return false;
2799 /* Check the syntax of the parameters in a MACRO definition. Returns
2800 false if an error occurs. */
2801 static bool
2802 parse_params (cpp_reader *pfile, cpp_macro *macro)
2804 unsigned int prev_ident = 0;
2806 for (;;)
2808 const cpp_token *token = _cpp_lex_token (pfile);
2810 switch (token->type)
2812 default:
2813 /* Allow/ignore comments in parameter lists if we are
2814 preserving comments in macro expansions. */
2815 if (token->type == CPP_COMMENT
2816 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
2817 continue;
2819 cpp_error (pfile, CPP_DL_ERROR,
2820 "\"%s\" may not appear in macro parameter list",
2821 cpp_token_as_text (pfile, token));
2822 return false;
2824 case CPP_NAME:
2825 if (prev_ident)
2827 cpp_error (pfile, CPP_DL_ERROR,
2828 "macro parameters must be comma-separated");
2829 return false;
2831 prev_ident = 1;
2833 if (_cpp_save_parameter (pfile, macro, token->val.node.node,
2834 token->val.node.spelling))
2835 return false;
2836 continue;
2838 case CPP_CLOSE_PAREN:
2839 if (prev_ident || macro->paramc == 0)
2840 return true;
2842 /* Fall through to pick up the error. */
2843 case CPP_COMMA:
2844 if (!prev_ident)
2846 cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
2847 return false;
2849 prev_ident = 0;
2850 continue;
2852 case CPP_ELLIPSIS:
2853 macro->variadic = 1;
2854 if (!prev_ident)
2856 _cpp_save_parameter (pfile, macro,
2857 pfile->spec_nodes.n__VA_ARGS__,
2858 pfile->spec_nodes.n__VA_ARGS__);
2859 pfile->state.va_args_ok = 1;
2860 if (! CPP_OPTION (pfile, c99)
2861 && CPP_OPTION (pfile, cpp_pedantic)
2862 && CPP_OPTION (pfile, warn_variadic_macros))
2864 if (CPP_OPTION (pfile, cplusplus))
2865 cpp_pedwarning
2866 (pfile, CPP_W_VARIADIC_MACROS,
2867 "anonymous variadic macros were introduced in C++11");
2868 else
2869 cpp_pedwarning
2870 (pfile, CPP_W_VARIADIC_MACROS,
2871 "anonymous variadic macros were introduced in C99");
2873 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
2874 && ! CPP_OPTION (pfile, cplusplus))
2875 cpp_error (pfile, CPP_DL_WARNING,
2876 "anonymous variadic macros were introduced in C99");
2878 else if (CPP_OPTION (pfile, cpp_pedantic)
2879 && CPP_OPTION (pfile, warn_variadic_macros))
2881 if (CPP_OPTION (pfile, cplusplus))
2882 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
2883 "ISO C++ does not permit named variadic macros");
2884 else
2885 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
2886 "ISO C does not permit named variadic macros");
2889 /* We're at the end, and just expect a closing parenthesis. */
2890 token = _cpp_lex_token (pfile);
2891 if (token->type == CPP_CLOSE_PAREN)
2892 return true;
2893 /* Fall through. */
2895 case CPP_EOF:
2896 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
2897 return false;
2902 /* Allocate room for a token from a macro's replacement list. */
2903 static cpp_token *
2904 alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
2906 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
2907 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
2909 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
2912 /* Lex a token from the expansion of MACRO, but mark parameters as we
2913 find them and warn of traditional stringification. */
2914 static cpp_token *
2915 lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
2917 cpp_token *token, *saved_cur_token;
2919 saved_cur_token = pfile->cur_token;
2920 pfile->cur_token = alloc_expansion_token (pfile, macro);
2921 token = _cpp_lex_direct (pfile);
2922 pfile->cur_token = saved_cur_token;
2924 /* Is this a parameter? */
2925 if (token->type == CPP_NAME
2926 && (token->val.node.node->flags & NODE_MACRO_ARG) != 0)
2928 cpp_hashnode *spelling = token->val.node.spelling;
2929 token->type = CPP_MACRO_ARG;
2930 token->val.macro_arg.arg_no = token->val.node.node->value.arg_index;
2931 token->val.macro_arg.spelling = spelling;
2933 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
2934 && (token->type == CPP_STRING || token->type == CPP_CHAR))
2935 check_trad_stringification (pfile, macro, &token->val.str);
2937 return token;
2940 static bool
2941 create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
2943 cpp_token *token;
2944 const cpp_token *ctoken;
2945 bool following_paste_op = false;
2946 const char *paste_op_error_msg =
2947 N_("'##' cannot appear at either end of a macro expansion");
2948 unsigned int num_extra_tokens = 0;
2950 /* Get the first token of the expansion (or the '(' of a
2951 function-like macro). */
2952 ctoken = _cpp_lex_token (pfile);
2954 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
2956 bool ok = parse_params (pfile, macro);
2957 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
2958 if (!ok)
2959 return false;
2961 /* Success. Commit or allocate the parameter array. */
2962 if (pfile->hash_table->alloc_subobject)
2964 cpp_hashnode **params =
2965 (cpp_hashnode **) pfile->hash_table->alloc_subobject
2966 (sizeof (cpp_hashnode *) * macro->paramc);
2967 memcpy (params, macro->params,
2968 sizeof (cpp_hashnode *) * macro->paramc);
2969 macro->params = params;
2971 else
2972 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
2973 macro->fun_like = 1;
2975 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
2977 /* While ISO C99 requires whitespace before replacement text
2978 in a macro definition, ISO C90 with TC1 allows characters
2979 from the basic source character set there. */
2980 if (CPP_OPTION (pfile, c99))
2982 if (CPP_OPTION (pfile, cplusplus))
2983 cpp_error (pfile, CPP_DL_PEDWARN,
2984 "ISO C++11 requires whitespace after the macro name");
2985 else
2986 cpp_error (pfile, CPP_DL_PEDWARN,
2987 "ISO C99 requires whitespace after the macro name");
2989 else
2991 int warntype = CPP_DL_WARNING;
2992 switch (ctoken->type)
2994 case CPP_ATSIGN:
2995 case CPP_AT_NAME:
2996 case CPP_OBJC_STRING:
2997 /* '@' is not in basic character set. */
2998 warntype = CPP_DL_PEDWARN;
2999 break;
3000 case CPP_OTHER:
3001 /* Basic character set sans letters, digits and _. */
3002 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
3003 ctoken->val.str.text[0]) == NULL)
3004 warntype = CPP_DL_PEDWARN;
3005 break;
3006 default:
3007 /* All other tokens start with a character from basic
3008 character set. */
3009 break;
3011 cpp_error (pfile, warntype,
3012 "missing whitespace after the macro name");
3016 if (macro->fun_like)
3017 token = lex_expansion_token (pfile, macro);
3018 else
3020 token = alloc_expansion_token (pfile, macro);
3021 *token = *ctoken;
3024 for (;;)
3026 /* Check the stringifying # constraint 6.10.3.2.1 of
3027 function-like macros when lexing the subsequent token. */
3028 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
3030 if (token->type == CPP_MACRO_ARG)
3032 if (token->flags & PREV_WHITE)
3033 token->flags |= SP_PREV_WHITE;
3034 if (token[-1].flags & DIGRAPH)
3035 token->flags |= SP_DIGRAPH;
3036 token->flags &= ~PREV_WHITE;
3037 token->flags |= STRINGIFY_ARG;
3038 token->flags |= token[-1].flags & PREV_WHITE;
3039 token[-1] = token[0];
3040 macro->count--;
3042 /* Let assembler get away with murder. */
3043 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
3045 cpp_error (pfile, CPP_DL_ERROR,
3046 "'#' is not followed by a macro parameter");
3047 return false;
3051 if (token->type == CPP_EOF)
3053 /* Paste operator constraint 6.10.3.3.1:
3054 Token-paste ##, can appear in both object-like and
3055 function-like macros, but not at the end. */
3056 if (following_paste_op)
3058 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
3059 return false;
3061 break;
3064 /* Paste operator constraint 6.10.3.3.1. */
3065 if (token->type == CPP_PASTE)
3067 /* Token-paste ##, can appear in both object-like and
3068 function-like macros, but not at the beginning. */
3069 if (macro->count == 1)
3071 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
3072 return false;
3075 if (token[-1].flags & PASTE_LEFT)
3077 macro->extra_tokens = 1;
3078 num_extra_tokens++;
3079 token->val.token_no = macro->count - 1;
3081 else
3083 --macro->count;
3084 token[-1].flags |= PASTE_LEFT;
3085 if (token->flags & DIGRAPH)
3086 token[-1].flags |= SP_DIGRAPH;
3087 if (token->flags & PREV_WHITE)
3088 token[-1].flags |= SP_PREV_WHITE;
3092 following_paste_op = (token->type == CPP_PASTE);
3093 token = lex_expansion_token (pfile, macro);
3096 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
3097 macro->traditional = 0;
3099 /* Don't count the CPP_EOF. */
3100 macro->count--;
3102 /* Clear whitespace on first token for warn_of_redefinition(). */
3103 if (macro->count)
3104 macro->exp.tokens[0].flags &= ~PREV_WHITE;
3106 /* Commit or allocate the memory. */
3107 if (pfile->hash_table->alloc_subobject)
3109 cpp_token *tokns =
3110 (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token)
3111 * macro->count);
3112 if (num_extra_tokens)
3114 /* Place second and subsequent ## or %:%: tokens in
3115 sequences of consecutive such tokens at the end of the
3116 list to preserve information about where they appear, how
3117 they are spelt and whether they are preceded by
3118 whitespace without otherwise interfering with macro
3119 expansion. */
3120 cpp_token *normal_dest = tokns;
3121 cpp_token *extra_dest = tokns + macro->count - num_extra_tokens;
3122 unsigned int i;
3123 for (i = 0; i < macro->count; i++)
3125 if (macro->exp.tokens[i].type == CPP_PASTE)
3126 *extra_dest++ = macro->exp.tokens[i];
3127 else
3128 *normal_dest++ = macro->exp.tokens[i];
3131 else
3132 memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
3133 macro->exp.tokens = tokns;
3135 else
3136 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
3138 return true;
3141 /* Parse a macro and save its expansion. Returns nonzero on success. */
3142 bool
3143 _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
3145 cpp_macro *macro;
3146 unsigned int i;
3147 bool ok;
3149 if (pfile->hash_table->alloc_subobject)
3150 macro = (cpp_macro *) pfile->hash_table->alloc_subobject
3151 (sizeof (cpp_macro));
3152 else
3153 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
3154 macro->line = pfile->directive_line;
3155 macro->params = 0;
3156 macro->paramc = 0;
3157 macro->variadic = 0;
3158 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
3159 macro->count = 0;
3160 macro->fun_like = 0;
3161 macro->extra_tokens = 0;
3162 /* To suppress some diagnostics. */
3163 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
3165 if (CPP_OPTION (pfile, traditional))
3166 ok = _cpp_create_trad_definition (pfile, macro);
3167 else
3169 ok = create_iso_definition (pfile, macro);
3171 /* We set the type for SEEN_EOL() in directives.c.
3173 Longer term we should lex the whole line before coming here,
3174 and just copy the expansion. */
3176 /* Stop the lexer accepting __VA_ARGS__. */
3177 pfile->state.va_args_ok = 0;
3180 /* Clear the fast argument lookup indices. */
3181 for (i = macro->paramc; i-- > 0; )
3183 struct macro_arg_saved_data *save =
3184 &((struct macro_arg_saved_data *) pfile->macro_buffer)[i];
3185 struct cpp_hashnode *node = save->canonical_node;
3186 node->flags &= ~ NODE_MACRO_ARG;
3187 node->value = save->value;
3190 if (!ok)
3191 return ok;
3193 if (node->type == NT_MACRO)
3195 if (CPP_OPTION (pfile, warn_unused_macros))
3196 _cpp_warn_if_unused_macro (pfile, node, NULL);
3198 if (warn_of_redefinition (pfile, node, macro))
3200 const int reason = ((node->flags & NODE_BUILTIN)
3201 && !(node->flags & NODE_WARN))
3202 ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
3204 bool warned =
3205 cpp_pedwarning_with_line (pfile, reason,
3206 pfile->directive_line, 0,
3207 "\"%s\" redefined", NODE_NAME (node));
3209 if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
3210 cpp_error_with_line (pfile, CPP_DL_NOTE,
3211 node->value.macro->line, 0,
3212 "this is the location of the previous definition");
3216 if (node->type != NT_VOID)
3217 _cpp_free_definition (node);
3219 /* Enter definition in hash table. */
3220 node->type = NT_MACRO;
3221 node->value.macro = macro;
3222 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))
3223 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS")
3224 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
3225 in the C standard, as something that one must use in C++.
3226 However DR#593 and C++11 indicate that they play no role in C++.
3227 We special-case them anyway. */
3228 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS")
3229 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS"))
3230 node->flags |= NODE_WARN;
3232 /* If user defines one of the conditional macros, remove the
3233 conditional flag */
3234 node->flags &= ~NODE_CONDITIONAL;
3236 return ok;
3239 /* Warn if a token in STRING matches one of a function-like MACRO's
3240 parameters. */
3241 static void
3242 check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
3243 const cpp_string *string)
3245 unsigned int i, len;
3246 const uchar *p, *q, *limit;
3248 /* Loop over the string. */
3249 limit = string->text + string->len - 1;
3250 for (p = string->text + 1; p < limit; p = q)
3252 /* Find the start of an identifier. */
3253 while (p < limit && !is_idstart (*p))
3254 p++;
3256 /* Find the end of the identifier. */
3257 q = p;
3258 while (q < limit && is_idchar (*q))
3259 q++;
3261 len = q - p;
3263 /* Loop over the function macro arguments to see if the
3264 identifier inside the string matches one of them. */
3265 for (i = 0; i < macro->paramc; i++)
3267 const cpp_hashnode *node = macro->params[i];
3269 if (NODE_LEN (node) == len
3270 && !memcmp (p, NODE_NAME (node), len))
3272 cpp_error (pfile, CPP_DL_WARNING,
3273 "macro argument \"%s\" would be stringified in traditional C",
3274 NODE_NAME (node));
3275 break;
3281 /* Returns the name, arguments and expansion of a macro, in a format
3282 suitable to be read back in again, and therefore also for DWARF 2
3283 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
3284 Caller is expected to generate the "#define" bit if needed. The
3285 returned text is temporary, and automatically freed later. */
3286 const unsigned char *
3287 cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
3289 unsigned int i, len;
3290 const cpp_macro *macro;
3291 unsigned char *buffer;
3293 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
3295 if (node->type != NT_MACRO
3296 || !pfile->cb.user_builtin_macro
3297 || !pfile->cb.user_builtin_macro (pfile, node))
3299 cpp_error (pfile, CPP_DL_ICE,
3300 "invalid hash type %d in cpp_macro_definition",
3301 node->type);
3302 return 0;
3306 macro = node->value.macro;
3307 /* Calculate length. */
3308 len = NODE_LEN (node) * 10 + 2; /* ' ' and NUL. */
3309 if (macro->fun_like)
3311 len += 4; /* "()" plus possible final ".." of named
3312 varargs (we have + 1 below). */
3313 for (i = 0; i < macro->paramc; i++)
3314 len += NODE_LEN (macro->params[i]) + 1; /* "," */
3317 /* This should match below where we fill in the buffer. */
3318 if (CPP_OPTION (pfile, traditional))
3319 len += _cpp_replacement_text_len (macro);
3320 else
3322 unsigned int count = macro_real_token_count (macro);
3323 for (i = 0; i < count; i++)
3325 cpp_token *token = &macro->exp.tokens[i];
3327 if (token->type == CPP_MACRO_ARG)
3328 len += NODE_LEN (token->val.macro_arg.spelling);
3329 else
3330 len += cpp_token_len (token);
3332 if (token->flags & STRINGIFY_ARG)
3333 len++; /* "#" */
3334 if (token->flags & PASTE_LEFT)
3335 len += 3; /* " ##" */
3336 if (token->flags & PREV_WHITE)
3337 len++; /* " " */
3341 if (len > pfile->macro_buffer_len)
3343 pfile->macro_buffer = XRESIZEVEC (unsigned char,
3344 pfile->macro_buffer, len);
3345 pfile->macro_buffer_len = len;
3348 /* Fill in the buffer. Start with the macro name. */
3349 buffer = pfile->macro_buffer;
3350 buffer = _cpp_spell_ident_ucns (buffer, node);
3352 /* Parameter names. */
3353 if (macro->fun_like)
3355 *buffer++ = '(';
3356 for (i = 0; i < macro->paramc; i++)
3358 cpp_hashnode *param = macro->params[i];
3360 if (param != pfile->spec_nodes.n__VA_ARGS__)
3362 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
3363 buffer += NODE_LEN (param);
3366 if (i + 1 < macro->paramc)
3367 /* Don't emit a space after the comma here; we're trying
3368 to emit a Dwarf-friendly definition, and the Dwarf spec
3369 forbids spaces in the argument list. */
3370 *buffer++ = ',';
3371 else if (macro->variadic)
3372 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
3374 *buffer++ = ')';
3377 /* The Dwarf spec requires a space after the macro name, even if the
3378 definition is the empty string. */
3379 *buffer++ = ' ';
3381 if (CPP_OPTION (pfile, traditional))
3382 buffer = _cpp_copy_replacement_text (macro, buffer);
3383 else if (macro->count)
3384 /* Expansion tokens. */
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->flags & PREV_WHITE)
3392 *buffer++ = ' ';
3393 if (token->flags & STRINGIFY_ARG)
3394 *buffer++ = '#';
3396 if (token->type == CPP_MACRO_ARG)
3398 memcpy (buffer,
3399 NODE_NAME (token->val.macro_arg.spelling),
3400 NODE_LEN (token->val.macro_arg.spelling));
3401 buffer += NODE_LEN (token->val.macro_arg.spelling);
3403 else
3404 buffer = cpp_spell_token (pfile, token, buffer, true);
3406 if (token->flags & PASTE_LEFT)
3408 *buffer++ = ' ';
3409 *buffer++ = '#';
3410 *buffer++ = '#';
3411 /* Next has PREV_WHITE; see _cpp_create_definition. */
3416 *buffer = '\0';
3417 return pfile->macro_buffer;