1 /* Part of CPP library. (Macro and #define handling.)
2 Copyright (C) 1986-2016 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
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! */
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. */
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
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
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
75 const source_location
*location_ptr
;
77 /* The number of times the iterator went forward. This useful only
78 when checking is enabled. */
83 /* Saved data about an identifier being used as a macro argument
85 struct macro_arg_saved_data
{
86 /* The canonical (UTF-8) spelling of this identifier. */
87 cpp_hashnode
*canonical_node
;
88 /* The previous value of this identifier. */
89 union _cpp_hashnode_value value
;
92 /* Macro expansion. */
94 static int enter_macro_context (cpp_reader
*, cpp_hashnode
*,
95 const cpp_token
*, source_location
);
96 static int builtin_macro (cpp_reader
*, cpp_hashnode
*,
97 source_location
, source_location
);
98 static void push_ptoken_context (cpp_reader
*, cpp_hashnode
*, _cpp_buff
*,
99 const cpp_token
**, unsigned int);
100 static void push_extended_tokens_context (cpp_reader
*, cpp_hashnode
*,
101 _cpp_buff
*, source_location
*,
102 const cpp_token
**, unsigned int);
103 static _cpp_buff
*collect_args (cpp_reader
*, const cpp_hashnode
*,
104 _cpp_buff
**, unsigned *);
105 static cpp_context
*next_context (cpp_reader
*);
106 static const cpp_token
*padding_token (cpp_reader
*, const cpp_token
*);
107 static void expand_arg (cpp_reader
*, macro_arg
*);
108 static const cpp_token
*new_string_token (cpp_reader
*, uchar
*, unsigned int);
109 static const cpp_token
*stringify_arg (cpp_reader
*, macro_arg
*);
110 static void paste_all_tokens (cpp_reader
*, const cpp_token
*);
111 static bool paste_tokens (cpp_reader
*, source_location
,
112 const cpp_token
**, const cpp_token
*);
113 static void alloc_expanded_arg_mem (cpp_reader
*, macro_arg
*, size_t);
114 static void ensure_expanded_arg_room (cpp_reader
*, macro_arg
*, size_t, size_t *);
115 static void delete_macro_args (_cpp_buff
*, unsigned num_args
);
116 static void set_arg_token (macro_arg
*, const cpp_token
*,
117 source_location
, size_t,
118 enum macro_arg_token_kind
,
120 static const source_location
*get_arg_token_location (const macro_arg
*,
121 enum macro_arg_token_kind
);
122 static const cpp_token
**arg_token_ptr_at (const macro_arg
*,
124 enum macro_arg_token_kind
,
125 source_location
**virt_location
);
127 static void macro_arg_token_iter_init (macro_arg_token_iter
*, bool,
128 enum macro_arg_token_kind
,
131 static const cpp_token
*macro_arg_token_iter_get_token
132 (const macro_arg_token_iter
*it
);
133 static source_location macro_arg_token_iter_get_location
134 (const macro_arg_token_iter
*);
135 static void macro_arg_token_iter_forward (macro_arg_token_iter
*);
136 static _cpp_buff
*tokens_buff_new (cpp_reader
*, size_t,
138 static size_t tokens_buff_count (_cpp_buff
*);
139 static const cpp_token
**tokens_buff_last_token_ptr (_cpp_buff
*);
140 static inline const cpp_token
**tokens_buff_put_token_to (const cpp_token
**,
145 const line_map_macro
*,
148 static const cpp_token
**tokens_buff_add_token (_cpp_buff
*,
153 const line_map_macro
*,
155 static inline void tokens_buff_remove_last_token (_cpp_buff
*);
156 static void replace_args (cpp_reader
*, cpp_hashnode
*, cpp_macro
*,
157 macro_arg
*, source_location
);
158 static _cpp_buff
*funlike_invocation_p (cpp_reader
*, cpp_hashnode
*,
159 _cpp_buff
**, unsigned *);
160 static bool create_iso_definition (cpp_reader
*, cpp_macro
*);
162 /* #define directive parsing and handling. */
164 static cpp_token
*alloc_expansion_token (cpp_reader
*, cpp_macro
*);
165 static cpp_token
*lex_expansion_token (cpp_reader
*, cpp_macro
*);
166 static bool warn_of_redefinition (cpp_reader
*, cpp_hashnode
*,
168 static bool parse_params (cpp_reader
*, cpp_macro
*);
169 static void check_trad_stringification (cpp_reader
*, const cpp_macro
*,
171 static bool reached_end_of_context (cpp_context
*);
172 static void consume_next_token_from_context (cpp_reader
*pfile
,
175 static const cpp_token
* cpp_get_token_1 (cpp_reader
*, source_location
*);
177 static cpp_hashnode
* macro_of_context (cpp_context
*context
);
179 static bool in_macro_expansion_p (cpp_reader
*pfile
);
181 /* Statistical counter tracking the number of macros that got
183 unsigned num_expanded_macros_counter
= 0;
184 /* Statistical counter tracking the total number tokens resulting
185 from macro expansion. */
186 unsigned num_macro_tokens_counter
= 0;
188 /* Emits a warning if NODE is a macro defined in the main file that
189 has not been used. */
191 _cpp_warn_if_unused_macro (cpp_reader
*pfile
, cpp_hashnode
*node
,
192 void *v ATTRIBUTE_UNUSED
)
194 if (node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
))
196 cpp_macro
*macro
= node
->value
.macro
;
199 && MAIN_FILE_P (linemap_check_ordinary
200 (linemap_lookup (pfile
->line_table
,
202 cpp_warning_with_line (pfile
, CPP_W_UNUSED_MACROS
, macro
->line
, 0,
203 "macro \"%s\" is not used", NODE_NAME (node
));
209 /* Allocates and returns a CPP_STRING token, containing TEXT of length
210 LEN, after null-terminating it. TEXT must be in permanent storage. */
211 static const cpp_token
*
212 new_string_token (cpp_reader
*pfile
, unsigned char *text
, unsigned int len
)
214 cpp_token
*token
= _cpp_temp_token (pfile
);
217 token
->type
= CPP_STRING
;
218 token
->val
.str
.len
= len
;
219 token
->val
.str
.text
= text
;
224 static const char * const monthnames
[] =
226 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
227 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
230 /* Helper function for builtin_macro. Returns the text generated by
233 _cpp_builtin_macro_text (cpp_reader
*pfile
, cpp_hashnode
*node
,
236 const uchar
*result
= NULL
;
237 linenum_type number
= 1;
239 switch (node
->value
.builtin
)
242 cpp_error (pfile
, CPP_DL_ICE
, "invalid built-in macro \"%s\"",
248 if (CPP_OPTION (pfile
, warn_date_time
))
249 cpp_warning (pfile
, CPP_W_DATE_TIME
, "macro \"%s\" might prevent "
250 "reproducible builds", NODE_NAME (node
));
252 cpp_buffer
*pbuffer
= cpp_get_buffer (pfile
);
253 if (pbuffer
->timestamp
== NULL
)
255 /* Initialize timestamp value of the assotiated file. */
256 struct _cpp_file
*file
= cpp_get_file (pbuffer
);
259 /* Generate __TIMESTAMP__ string, that represents
260 the date and time of the last modification
261 of the current source file. The string constant
262 looks like "Sun Sep 16 01:03:52 1973". */
263 struct tm
*tb
= NULL
;
264 struct stat
*st
= _cpp_get_file_stat (file
);
266 tb
= localtime (&st
->st_mtime
);
269 char *str
= asctime (tb
);
270 size_t len
= strlen (str
);
271 unsigned char *buf
= _cpp_unaligned_alloc (pfile
, len
+ 2);
273 strcpy ((char *) buf
+ 1, str
);
275 pbuffer
->timestamp
= buf
;
279 cpp_errno (pfile
, CPP_DL_WARNING
,
280 "could not determine file timestamp");
281 pbuffer
->timestamp
= UC
"\"??? ??? ?? ??:??:?? ????\"";
285 result
= pbuffer
->timestamp
;
295 if (node
->value
.builtin
== BT_FILE
)
296 name
= linemap_get_expansion_filename (pfile
->line_table
,
297 pfile
->line_table
->highest_line
);
300 name
= _cpp_get_file_name (pfile
->main_file
);
305 buf
= _cpp_unaligned_alloc (pfile
, len
* 2 + 3);
308 buf
= cpp_quote_string (buf
+ 1, (const unsigned char *) name
, len
);
314 case BT_INCLUDE_LEVEL
:
315 /* The line map depth counts the primary source as level 1, but
316 historically __INCLUDE_DEPTH__ has called the primary source
318 number
= pfile
->line_table
->depth
- 1;
322 /* If __LINE__ is embedded in a macro, it must expand to the
323 line of the macro's invocation, not its definition.
324 Otherwise things like assert() will not work properly.
325 See WG14 N1911, WG21 N4220 sec 6.5, and PR 61861. */
326 if (CPP_OPTION (pfile
, traditional
))
327 loc
= pfile
->line_table
->highest_line
;
329 loc
= linemap_resolve_location (pfile
->line_table
, loc
,
330 LRK_MACRO_EXPANSION_POINT
, NULL
);
331 number
= linemap_get_expansion_line (pfile
->line_table
, loc
);
334 /* __STDC__ has the value 1 under normal circumstances.
335 However, if (a) we are in a system header, (b) the option
336 stdc_0_in_system_headers is true (set by target config), and
337 (c) we are not in strictly conforming mode, then it has the
338 value 0. (b) and (c) are already checked in cpp_init_builtins. */
340 if (cpp_in_system_header (pfile
))
348 if (CPP_OPTION (pfile
, warn_date_time
))
349 cpp_warning (pfile
, CPP_W_DATE_TIME
, "macro \"%s\" might prevent "
350 "reproducible builds", NODE_NAME (node
));
351 if (pfile
->date
== NULL
)
353 /* Allocate __DATE__ and __TIME__ strings from permanent
354 storage. We only do this once, and don't generate them
355 at init time, because time() and localtime() are very
356 slow on some systems. */
358 struct tm
*tb
= NULL
;
360 /* Set a reproducible timestamp for __DATE__ and __TIME__ macro
361 if SOURCE_DATE_EPOCH is defined. */
362 if (pfile
->source_date_epoch
== (time_t) -2
363 && pfile
->cb
.get_source_date_epoch
!= NULL
)
364 pfile
->source_date_epoch
= pfile
->cb
.get_source_date_epoch (pfile
);
366 if (pfile
->source_date_epoch
>= (time_t) 0)
367 tb
= gmtime (&pfile
->source_date_epoch
);
370 /* (time_t) -1 is a legitimate value for "number of seconds
371 since the Epoch", so we have to do a little dance to
372 distinguish that from a genuine error. */
375 if (tt
!= (time_t)-1 || errno
== 0)
376 tb
= localtime (&tt
);
381 pfile
->date
= _cpp_unaligned_alloc (pfile
,
382 sizeof ("\"Oct 11 1347\""));
383 sprintf ((char *) pfile
->date
, "\"%s %2d %4d\"",
384 monthnames
[tb
->tm_mon
], tb
->tm_mday
,
387 pfile
->time
= _cpp_unaligned_alloc (pfile
,
388 sizeof ("\"12:34:56\""));
389 sprintf ((char *) pfile
->time
, "\"%02d:%02d:%02d\"",
390 tb
->tm_hour
, tb
->tm_min
, tb
->tm_sec
);
394 cpp_errno (pfile
, CPP_DL_WARNING
,
395 "could not determine date and time");
397 pfile
->date
= UC
"\"??? ?? ????\"";
398 pfile
->time
= UC
"\"??:??:??\"";
402 if (node
->value
.builtin
== BT_DATE
)
403 result
= pfile
->date
;
405 result
= pfile
->time
;
409 if (CPP_OPTION (pfile
, directives_only
) && pfile
->state
.in_directive
)
410 cpp_error (pfile
, CPP_DL_ERROR
,
411 "__COUNTER__ expanded inside directive with -fdirectives-only");
412 number
= pfile
->counter
++;
415 case BT_HAS_ATTRIBUTE
:
416 number
= pfile
->cb
.has_attribute (pfile
);
422 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
423 result
= _cpp_unaligned_alloc (pfile
, 21);
424 sprintf ((char *) result
, "%u", number
);
430 /* Convert builtin macros like __FILE__ to a token and push it on the
431 context stack. Also handles _Pragma, for which a new token may not
432 be created. Returns 1 if it generates a new token context, 0 to
433 return the token to the caller. LOC is the location of the expansion
434 point of the macro. */
436 builtin_macro (cpp_reader
*pfile
, cpp_hashnode
*node
,
437 source_location loc
, source_location expand_loc
)
443 if (node
->value
.builtin
== BT_PRAGMA
)
445 /* Don't interpret _Pragma within directives. The standard is
446 not clear on this, but to me this makes most sense. */
447 if (pfile
->state
.in_directive
)
450 return _cpp_do__Pragma (pfile
, loc
);
453 buf
= _cpp_builtin_macro_text (pfile
, node
, expand_loc
);
455 nbuf
= (char *) alloca (len
+ 1);
456 memcpy (nbuf
, buf
, len
);
459 cpp_push_buffer (pfile
, (uchar
*) nbuf
, len
, /* from_stage3 */ true);
460 _cpp_clean_line (pfile
);
462 /* Set pfile->cur_token as required by _cpp_lex_direct. */
463 pfile
->cur_token
= _cpp_temp_token (pfile
);
464 cpp_token
*token
= _cpp_lex_direct (pfile
);
465 /* We should point to the expansion point of the builtin macro. */
466 token
->src_loc
= loc
;
467 if (pfile
->context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
469 /* We are tracking tokens resulting from macro expansion.
470 Create a macro line map and generate a virtual location for
471 the token resulting from the expansion of the built-in
473 source_location
*virt_locs
= NULL
;
474 _cpp_buff
*token_buf
= tokens_buff_new (pfile
, 1, &virt_locs
);
475 const line_map_macro
* map
=
476 linemap_enter_macro (pfile
->line_table
, node
, loc
, 1);
477 tokens_buff_add_token (token_buf
, virt_locs
, token
,
478 pfile
->line_table
->builtin_location
,
479 pfile
->line_table
->builtin_location
,
480 map
, /*macro_token_index=*/0);
481 push_extended_tokens_context (pfile
, node
, token_buf
, virt_locs
,
482 (const cpp_token
**)token_buf
->base
,
486 _cpp_push_token_context (pfile
, NULL
, token
, 1);
487 if (pfile
->buffer
->cur
!= pfile
->buffer
->rlimit
)
488 cpp_error (pfile
, CPP_DL_ICE
, "invalid built-in macro \"%s\"",
490 _cpp_pop_buffer (pfile
);
495 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
496 backslashes and double quotes. DEST must be of sufficient size.
497 Returns a pointer to the end of the string. */
499 cpp_quote_string (uchar
*dest
, const uchar
*src
, unsigned int len
)
505 if (c
== '\\' || c
== '"')
517 /* Convert a token sequence ARG to a single string token according to
518 the rules of the ISO C #-operator. */
519 static const cpp_token
*
520 stringify_arg (cpp_reader
*pfile
, macro_arg
*arg
)
523 unsigned int i
, escape_it
, backslash_count
= 0;
524 const cpp_token
*source
= NULL
;
527 if (BUFF_ROOM (pfile
->u_buff
) < 3)
528 _cpp_extend_buff (pfile
, &pfile
->u_buff
, 3);
529 dest
= BUFF_FRONT (pfile
->u_buff
);
532 /* Loop, reading in the argument's tokens. */
533 for (i
= 0; i
< arg
->count
; i
++)
535 const cpp_token
*token
= arg
->first
[i
];
537 if (token
->type
== CPP_PADDING
)
540 || (!(source
->flags
& PREV_WHITE
)
541 && token
->val
.source
== NULL
))
542 source
= token
->val
.source
;
546 escape_it
= (token
->type
== CPP_STRING
|| token
->type
== CPP_CHAR
547 || token
->type
== CPP_WSTRING
|| token
->type
== CPP_WCHAR
548 || token
->type
== CPP_STRING32
|| token
->type
== CPP_CHAR32
549 || token
->type
== CPP_STRING16
|| token
->type
== CPP_CHAR16
550 || token
->type
== CPP_UTF8STRING
|| token
->type
== CPP_UTF8CHAR
551 || cpp_userdef_string_p (token
->type
)
552 || cpp_userdef_char_p (token
->type
));
554 /* Room for each char being written in octal, initial space and
555 final quote and NUL. */
556 len
= cpp_token_len (token
);
561 if ((size_t) (BUFF_LIMIT (pfile
->u_buff
) - dest
) < len
)
563 size_t len_so_far
= dest
- BUFF_FRONT (pfile
->u_buff
);
564 _cpp_extend_buff (pfile
, &pfile
->u_buff
, len
);
565 dest
= BUFF_FRONT (pfile
->u_buff
) + len_so_far
;
568 /* Leading white space? */
569 if (dest
- 1 != BUFF_FRONT (pfile
->u_buff
))
573 if (source
->flags
& PREV_WHITE
)
580 _cpp_buff
*buff
= _cpp_get_buff (pfile
, len
);
581 unsigned char *buf
= BUFF_FRONT (buff
);
582 len
= cpp_spell_token (pfile
, token
, buf
, true) - buf
;
583 dest
= cpp_quote_string (dest
, buf
, len
);
584 _cpp_release_buff (pfile
, buff
);
587 dest
= cpp_spell_token (pfile
, token
, dest
, true);
589 if (token
->type
== CPP_OTHER
&& token
->val
.str
.text
[0] == '\\')
595 /* Ignore the final \ of invalid string literals. */
596 if (backslash_count
& 1)
598 cpp_error (pfile
, CPP_DL_WARNING
,
599 "invalid string literal, ignoring final '\\'");
603 /* Commit the memory, including NUL, and return the token. */
605 len
= dest
- BUFF_FRONT (pfile
->u_buff
);
606 BUFF_FRONT (pfile
->u_buff
) = dest
+ 1;
607 return new_string_token (pfile
, dest
- len
, len
);
610 /* Try to paste two tokens. On success, return nonzero. In any
611 case, PLHS is updated to point to the pasted token, which is
612 guaranteed to not have the PASTE_LEFT flag set. LOCATION is
613 the virtual location used for error reporting. */
615 paste_tokens (cpp_reader
*pfile
, source_location location
,
616 const cpp_token
**plhs
, const cpp_token
*rhs
)
618 unsigned char *buf
, *end
, *lhsend
;
622 len
= cpp_token_len (*plhs
) + cpp_token_len (rhs
) + 1;
623 buf
= (unsigned char *) alloca (len
);
624 end
= lhsend
= cpp_spell_token (pfile
, *plhs
, buf
, true);
626 /* Avoid comment headers, since they are still processed in stage 3.
627 It is simpler to insert a space here, rather than modifying the
628 lexer to ignore comments in some circumstances. Simply returning
629 false doesn't work, since we want to clear the PASTE_LEFT flag. */
630 if ((*plhs
)->type
== CPP_DIV
&& rhs
->type
!= CPP_EQ
)
632 /* In one obscure case we might see padding here. */
633 if (rhs
->type
!= CPP_PADDING
)
634 end
= cpp_spell_token (pfile
, rhs
, end
, true);
637 cpp_push_buffer (pfile
, buf
, end
- buf
, /* from_stage3 */ true);
638 _cpp_clean_line (pfile
);
640 /* Set pfile->cur_token as required by _cpp_lex_direct. */
641 pfile
->cur_token
= _cpp_temp_token (pfile
);
642 lhs
= _cpp_lex_direct (pfile
);
643 if (pfile
->buffer
->cur
!= pfile
->buffer
->rlimit
)
645 source_location saved_loc
= lhs
->src_loc
;
647 _cpp_pop_buffer (pfile
);
648 _cpp_backup_tokens (pfile
, 1);
651 /* We have to remove the PASTE_LEFT flag from the old lhs, but
652 we want to keep the new location. */
655 lhs
->src_loc
= saved_loc
;
656 lhs
->flags
&= ~PASTE_LEFT
;
658 /* Mandatory error for all apart from assembler. */
659 if (CPP_OPTION (pfile
, lang
) != CLK_ASM
)
660 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
661 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
662 buf
, cpp_token_as_text (pfile
, rhs
));
667 _cpp_pop_buffer (pfile
);
671 /* Handles an arbitrarily long sequence of ## operators, with initial
672 operand LHS. This implementation is left-associative,
673 non-recursive, and finishes a paste before handling succeeding
674 ones. If a paste fails, we back up to the RHS of the failing ##
675 operator before pushing the context containing the result of prior
676 successful pastes, with the effect that the RHS appears in the
677 output stream after the pasted LHS normally. */
679 paste_all_tokens (cpp_reader
*pfile
, const cpp_token
*lhs
)
681 const cpp_token
*rhs
= NULL
;
682 cpp_context
*context
= pfile
->context
;
683 source_location virt_loc
= 0;
685 /* We are expanding a macro and we must have been called on a token
686 that appears at the left hand side of a ## operator. */
687 if (macro_of_context (pfile
->context
) == NULL
688 || (!(lhs
->flags
& PASTE_LEFT
)))
691 if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
692 /* The caller must have called consume_next_token_from_context
693 right before calling us. That has incremented the pointer to
694 the current virtual location. So it now points to the location
695 of the token that comes right after *LHS. We want the
696 resulting pasted token to have the location of the current
698 virt_loc
= context
->c
.mc
->cur_virt_loc
[-1];
700 /* We are not tracking macro expansion. So the best virtual
701 location we can get here is the expansion point of the macro we
702 are currently expanding. */
703 virt_loc
= pfile
->invocation_location
;
707 /* Take the token directly from the current context. We can do
708 this, because we are in the replacement list of either an
709 object-like macro, or a function-like macro with arguments
710 inserted. In either case, the constraints to #define
711 guarantee we have at least one more token. */
712 if (context
->tokens_kind
== TOKENS_KIND_DIRECT
)
713 rhs
= FIRST (context
).token
++;
714 else if (context
->tokens_kind
== TOKENS_KIND_INDIRECT
)
715 rhs
= *FIRST (context
).ptoken
++;
716 else if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
718 /* So we are in presence of an extended token context, which
719 means that each token in this context has a virtual
720 location attached to it. So let's not forget to update
721 the pointer to the current virtual location of the
722 current token when we update the pointer to the current
725 rhs
= *FIRST (context
).ptoken
++;
726 /* context->c.mc must be non-null, as if we were not in a
727 macro context, context->tokens_kind could not be equal to
728 TOKENS_KIND_EXTENDED. */
729 context
->c
.mc
->cur_virt_loc
++;
732 if (rhs
->type
== CPP_PADDING
)
734 if (rhs
->flags
& PASTE_LEFT
)
737 if (!paste_tokens (pfile
, virt_loc
, &lhs
, rhs
))
740 while (rhs
->flags
& PASTE_LEFT
);
742 /* Put the resulting token in its own context. */
743 if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
745 source_location
*virt_locs
= NULL
;
746 _cpp_buff
*token_buf
= tokens_buff_new (pfile
, 1, &virt_locs
);
747 tokens_buff_add_token (token_buf
, virt_locs
, lhs
,
748 virt_loc
, 0, NULL
, 0);
749 push_extended_tokens_context (pfile
, context
->c
.mc
->macro_node
,
750 token_buf
, virt_locs
,
751 (const cpp_token
**)token_buf
->base
, 1);
754 _cpp_push_token_context (pfile
, NULL
, lhs
, 1);
757 /* Returns TRUE if the number of arguments ARGC supplied in an
758 invocation of the MACRO referenced by NODE is valid. An empty
759 invocation to a macro with no parameters should pass ARGC as zero.
761 Note that MACRO cannot necessarily be deduced from NODE, in case
762 NODE was redefined whilst collecting arguments. */
764 _cpp_arguments_ok (cpp_reader
*pfile
, cpp_macro
*macro
, const cpp_hashnode
*node
, unsigned int argc
)
766 if (argc
== macro
->paramc
)
769 if (argc
< macro
->paramc
)
771 /* As an extension, variadic arguments are allowed to not appear in
772 the invocation at all.
773 e.g. #define debug(format, args...) something
776 This is exactly the same as if an empty variadic list had been
777 supplied - debug("string", ). */
779 if (argc
+ 1 == macro
->paramc
&& macro
->variadic
)
781 if (CPP_PEDANTIC (pfile
) && ! macro
->syshdr
)
783 if (CPP_OPTION (pfile
, cplusplus
))
784 cpp_error (pfile
, CPP_DL_PEDWARN
,
785 "ISO C++11 requires at least one argument "
786 "for the \"...\" in a variadic macro");
788 cpp_error (pfile
, CPP_DL_PEDWARN
,
789 "ISO C99 requires at least one argument "
790 "for the \"...\" in a variadic macro");
795 cpp_error (pfile
, CPP_DL_ERROR
,
796 "macro \"%s\" requires %u arguments, but only %u given",
797 NODE_NAME (node
), macro
->paramc
, argc
);
800 cpp_error (pfile
, CPP_DL_ERROR
,
801 "macro \"%s\" passed %u arguments, but takes just %u",
802 NODE_NAME (node
), argc
, macro
->paramc
);
807 /* Reads and returns the arguments to a function-like macro
808 invocation. Assumes the opening parenthesis has been processed.
809 If there is an error, emits an appropriate diagnostic and returns
810 NULL. Each argument is terminated by a CPP_EOF token, for the
811 future benefit of expand_arg(). If there are any deferred
812 #pragma directives among macro arguments, store pointers to the
813 CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.
815 What is returned is the buffer that contains the memory allocated
816 to hold the macro arguments. NODE is the name of the macro this
817 function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is
818 set to the actual number of macro arguments allocated in the
821 collect_args (cpp_reader
*pfile
, const cpp_hashnode
*node
,
822 _cpp_buff
**pragma_buff
, unsigned *num_args
)
824 _cpp_buff
*buff
, *base_buff
;
826 macro_arg
*args
, *arg
;
827 const cpp_token
*token
;
829 source_location virt_loc
;
830 bool track_macro_expansion_p
= CPP_OPTION (pfile
, track_macro_expansion
);
831 unsigned num_args_alloced
= 0;
833 macro
= node
->value
.macro
;
835 argc
= macro
->paramc
;
839 #define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50
840 #define ARG_TOKENS_EXTENT 1000
842 buff
= _cpp_get_buff (pfile
, argc
* (DEFAULT_NUM_TOKENS_PER_MACRO_ARG
843 * sizeof (cpp_token
*)
844 + sizeof (macro_arg
)));
846 args
= (macro_arg
*) buff
->base
;
847 memset (args
, 0, argc
* sizeof (macro_arg
));
848 buff
->cur
= (unsigned char *) &args
[argc
];
849 arg
= args
, argc
= 0;
851 /* Collect the tokens making up each argument. We don't yet know
852 how many arguments have been supplied, whether too many or too
853 few. Hence the slightly bizarre usage of "argc" and "arg". */
856 unsigned int paren_depth
= 0;
857 unsigned int ntokens
= 0;
858 unsigned virt_locs_capacity
= DEFAULT_NUM_TOKENS_PER_MACRO_ARG
;
862 arg
->first
= (const cpp_token
**) buff
->cur
;
863 if (track_macro_expansion_p
)
865 virt_locs_capacity
= DEFAULT_NUM_TOKENS_PER_MACRO_ARG
;
866 arg
->virt_locs
= XNEWVEC (source_location
,
872 /* Require space for 2 new tokens (including a CPP_EOF). */
873 if ((unsigned char *) &arg
->first
[ntokens
+ 2] > buff
->limit
)
875 buff
= _cpp_append_extend_buff (pfile
, buff
,
877 * sizeof (cpp_token
*));
878 arg
->first
= (const cpp_token
**) buff
->cur
;
880 if (track_macro_expansion_p
881 && (ntokens
+ 2 > virt_locs_capacity
))
883 virt_locs_capacity
+= ARG_TOKENS_EXTENT
;
884 arg
->virt_locs
= XRESIZEVEC (source_location
,
889 token
= cpp_get_token_1 (pfile
, &virt_loc
);
891 if (token
->type
== CPP_PADDING
)
893 /* Drop leading padding. */
897 else if (token
->type
== CPP_OPEN_PAREN
)
899 else if (token
->type
== CPP_CLOSE_PAREN
)
901 if (paren_depth
-- == 0)
904 else if (token
->type
== CPP_COMMA
)
906 /* A comma does not terminate an argument within
907 parentheses or as part of a variable argument. */
909 && ! (macro
->variadic
&& argc
== macro
->paramc
))
912 else if (token
->type
== CPP_EOF
913 || (token
->type
== CPP_HASH
&& token
->flags
& BOL
))
915 else if (token
->type
== CPP_PRAGMA
)
917 cpp_token
*newtok
= _cpp_temp_token (pfile
);
919 /* CPP_PRAGMA token lives in directive_result, which will
920 be overwritten on the next directive. */
925 if (*pragma_buff
== NULL
926 || BUFF_ROOM (*pragma_buff
) < sizeof (cpp_token
*))
929 if (*pragma_buff
== NULL
)
931 = _cpp_get_buff (pfile
, 32 * sizeof (cpp_token
*));
936 = _cpp_get_buff (pfile
,
937 (BUFF_FRONT (*pragma_buff
)
938 - (*pragma_buff
)->base
) * 2);
939 (*pragma_buff
)->next
= next
;
942 *(const cpp_token
**) BUFF_FRONT (*pragma_buff
) = token
;
943 BUFF_FRONT (*pragma_buff
) += sizeof (cpp_token
*);
944 if (token
->type
== CPP_PRAGMA_EOL
)
946 token
= cpp_get_token_1 (pfile
, &virt_loc
);
948 while (token
->type
!= CPP_EOF
);
950 /* In deferred pragmas parsing_args and prevent_expansion
951 had been changed, reset it. */
952 pfile
->state
.parsing_args
= 2;
953 pfile
->state
.prevent_expansion
= 1;
955 if (token
->type
== CPP_EOF
)
960 set_arg_token (arg
, token
, virt_loc
,
961 ntokens
, MACRO_ARG_TOKEN_NORMAL
,
962 CPP_OPTION (pfile
, track_macro_expansion
));
966 /* Drop trailing padding. */
967 while (ntokens
> 0 && arg
->first
[ntokens
- 1]->type
== CPP_PADDING
)
970 arg
->count
= ntokens
;
971 set_arg_token (arg
, &pfile
->eof
, pfile
->eof
.src_loc
,
972 ntokens
, MACRO_ARG_TOKEN_NORMAL
,
973 CPP_OPTION (pfile
, track_macro_expansion
));
975 /* Terminate the argument. Excess arguments loop back and
976 overwrite the final legitimate argument, before failing. */
977 if (argc
<= macro
->paramc
)
979 buff
->cur
= (unsigned char *) &arg
->first
[ntokens
+ 1];
980 if (argc
!= macro
->paramc
)
984 while (token
->type
!= CPP_CLOSE_PAREN
&& token
->type
!= CPP_EOF
);
986 if (token
->type
== CPP_EOF
)
988 /* We still need the CPP_EOF to end directives, and to end
989 pre-expansion of a macro argument. Step back is not
990 unconditional, since we don't want to return a CPP_EOF to our
991 callers at the end of an -include-d file. */
992 if (pfile
->context
->prev
|| pfile
->state
.in_directive
)
993 _cpp_backup_tokens (pfile
, 1);
994 cpp_error (pfile
, CPP_DL_ERROR
,
995 "unterminated argument list invoking macro \"%s\"",
1000 /* A single empty argument is counted as no argument. */
1001 if (argc
== 1 && macro
->paramc
== 0 && args
[0].count
== 0)
1003 if (_cpp_arguments_ok (pfile
, macro
, node
, argc
))
1005 /* GCC has special semantics for , ## b where b is a varargs
1006 parameter: we remove the comma if b was omitted entirely.
1007 If b was merely an empty argument, the comma is retained.
1008 If the macro takes just one (varargs) parameter, then we
1009 retain the comma only if we are standards conforming.
1011 If FIRST is NULL replace_args () swallows the comma. */
1012 if (macro
->variadic
&& (argc
< macro
->paramc
1013 || (argc
== 1 && args
[0].count
== 0
1014 && !CPP_OPTION (pfile
, std
))))
1015 args
[macro
->paramc
- 1].first
= NULL
;
1017 *num_args
= num_args_alloced
;
1022 /* An error occurred. */
1023 _cpp_release_buff (pfile
, base_buff
);
1027 /* Search for an opening parenthesis to the macro of NODE, in such a
1028 way that, if none is found, we don't lose the information in any
1029 intervening padding tokens. If we find the parenthesis, collect
1030 the arguments and return the buffer containing them. PRAGMA_BUFF
1031 argument is the same as in collect_args. If NUM_ARGS is non-NULL,
1032 *NUM_ARGS is set to the number of arguments contained in the
1035 funlike_invocation_p (cpp_reader
*pfile
, cpp_hashnode
*node
,
1036 _cpp_buff
**pragma_buff
, unsigned *num_args
)
1038 const cpp_token
*token
, *padding
= NULL
;
1042 token
= cpp_get_token (pfile
);
1043 if (token
->type
!= CPP_PADDING
)
1046 || (!(padding
->flags
& PREV_WHITE
) && token
->val
.source
== NULL
))
1050 if (token
->type
== CPP_OPEN_PAREN
)
1052 pfile
->state
.parsing_args
= 2;
1053 return collect_args (pfile
, node
, pragma_buff
, num_args
);
1056 /* CPP_EOF can be the end of macro arguments, or the end of the
1057 file. We mustn't back up over the latter. Ugh. */
1058 if (token
->type
!= CPP_EOF
|| token
== &pfile
->eof
)
1060 /* Back up. We may have skipped padding, in which case backing
1061 up more than one token when expanding macros is in general
1062 too difficult. We re-insert it in its own context. */
1063 _cpp_backup_tokens (pfile
, 1);
1065 _cpp_push_token_context (pfile
, NULL
, padding
, 1);
1071 /* Return the real number of tokens in the expansion of MACRO. */
1072 static inline unsigned int
1073 macro_real_token_count (const cpp_macro
*macro
)
1076 if (__builtin_expect (!macro
->extra_tokens
, true))
1077 return macro
->count
;
1078 for (i
= 0; i
< macro
->count
; i
++)
1079 if (macro
->exp
.tokens
[i
].type
== CPP_PASTE
)
1084 /* Push the context of a macro with hash entry NODE onto the context
1085 stack. If we can successfully expand the macro, we push a context
1086 containing its yet-to-be-rescanned replacement list and return one.
1087 If there were additionally any unexpanded deferred #pragma
1088 directives among macro arguments, push another context containing
1089 the pragma tokens before the yet-to-be-rescanned replacement list
1090 and return two. Otherwise, we don't push a context and return
1091 zero. LOCATION is the location of the expansion point of the
1094 enter_macro_context (cpp_reader
*pfile
, cpp_hashnode
*node
,
1095 const cpp_token
*result
, source_location location
)
1097 /* The presence of a macro invalidates a file's controlling macro. */
1098 pfile
->mi_valid
= false;
1100 pfile
->state
.angled_headers
= false;
1102 /* From here to when we push the context for the macro later down
1103 this function, we need to flag the fact that we are about to
1104 expand a macro. This is useful when -ftrack-macro-expansion is
1105 turned off. In that case, we need to record the location of the
1106 expansion point of the top-most macro we are about to to expand,
1107 into pfile->invocation_location. But we must not record any such
1108 location once the process of expanding the macro starts; that is,
1109 we must not do that recording between now and later down this
1110 function where set this flag to FALSE. */
1111 pfile
->about_to_expand_macro_p
= true;
1113 if ((node
->flags
& NODE_BUILTIN
) && !(node
->flags
& NODE_USED
))
1115 node
->flags
|= NODE_USED
;
1116 if ((!pfile
->cb
.user_builtin_macro
1117 || !pfile
->cb
.user_builtin_macro (pfile
, node
))
1118 && pfile
->cb
.used_define
)
1119 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
1122 /* Handle standard macros. */
1123 if (! (node
->flags
& NODE_BUILTIN
))
1125 cpp_macro
*macro
= node
->value
.macro
;
1126 _cpp_buff
*pragma_buff
= NULL
;
1128 if (macro
->fun_like
)
1131 unsigned num_args
= 0;
1133 pfile
->state
.prevent_expansion
++;
1134 pfile
->keep_tokens
++;
1135 pfile
->state
.parsing_args
= 1;
1136 buff
= funlike_invocation_p (pfile
, node
, &pragma_buff
,
1138 pfile
->state
.parsing_args
= 0;
1139 pfile
->keep_tokens
--;
1140 pfile
->state
.prevent_expansion
--;
1144 if (CPP_WTRADITIONAL (pfile
) && ! node
->value
.macro
->syshdr
)
1145 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
1146 "function-like macro \"%s\" must be used with arguments in traditional C",
1150 _cpp_release_buff (pfile
, pragma_buff
);
1152 pfile
->about_to_expand_macro_p
= false;
1156 if (macro
->paramc
> 0)
1157 replace_args (pfile
, node
, macro
,
1158 (macro_arg
*) buff
->base
,
1160 /* Free the memory used by the arguments of this
1161 function-like macro. This memory has been allocated by
1162 funlike_invocation_p and by replace_args. */
1163 delete_macro_args (buff
, num_args
);
1166 /* Disable the macro within its expansion. */
1167 node
->flags
|= NODE_DISABLED
;
1169 if (!(node
->flags
& NODE_USED
))
1171 node
->flags
|= NODE_USED
;
1172 if (pfile
->cb
.used_define
)
1173 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
1177 pfile
->cb
.used (pfile
, location
, node
);
1181 if (macro
->paramc
== 0)
1183 unsigned tokens_count
= macro_real_token_count (macro
);
1184 if (CPP_OPTION (pfile
, track_macro_expansion
))
1187 const cpp_token
*src
= macro
->exp
.tokens
;
1188 const line_map_macro
*map
;
1189 source_location
*virt_locs
= NULL
;
1190 _cpp_buff
*macro_tokens
1191 = tokens_buff_new (pfile
, tokens_count
, &virt_locs
);
1193 /* Create a macro map to record the locations of the
1194 tokens that are involved in the expansion. LOCATION
1195 is the location of the macro expansion point. */
1196 map
= linemap_enter_macro (pfile
->line_table
,
1197 node
, location
, tokens_count
);
1198 for (i
= 0; i
< tokens_count
; ++i
)
1200 tokens_buff_add_token (macro_tokens
, virt_locs
,
1202 src
->src_loc
, map
, i
);
1205 push_extended_tokens_context (pfile
, node
,
1208 (const cpp_token
**)
1213 _cpp_push_token_context (pfile
, node
, macro
->exp
.tokens
,
1215 num_macro_tokens_counter
+= tokens_count
;
1220 if (!pfile
->state
.in_directive
)
1221 _cpp_push_token_context (pfile
, NULL
,
1222 padding_token (pfile
, result
), 1);
1225 unsigned tokens_count
;
1226 _cpp_buff
*tail
= pragma_buff
->next
;
1227 pragma_buff
->next
= NULL
;
1228 tokens_count
= ((const cpp_token
**) BUFF_FRONT (pragma_buff
)
1229 - (const cpp_token
**) pragma_buff
->base
);
1230 push_ptoken_context (pfile
, NULL
, pragma_buff
,
1231 (const cpp_token
**) pragma_buff
->base
,
1234 if (!CPP_OPTION (pfile
, track_macro_expansion
))
1235 num_macro_tokens_counter
+= tokens_count
;
1238 while (pragma_buff
!= NULL
);
1239 pfile
->about_to_expand_macro_p
= false;
1243 pfile
->about_to_expand_macro_p
= false;
1247 pfile
->about_to_expand_macro_p
= false;
1248 /* Handle built-in macros and the _Pragma operator. */
1250 source_location loc
, expand_loc
;
1252 if (/* The top-level macro invocation that triggered the expansion
1253 we are looking at is with a standard macro ...*/
1254 !(pfile
->top_most_macro_node
->flags
& NODE_BUILTIN
)
1255 /* ... and it's a function-like macro invocation. */
1256 && pfile
->top_most_macro_node
->value
.macro
->fun_like
)
1258 /* Then the location of the end of the macro invocation is the
1259 location of the closing parenthesis. */
1260 loc
= pfile
->cur_token
[-1].src_loc
;
1265 /* Otherwise, the location of the end of the macro invocation is
1266 the location of the expansion point of that top-level macro
1269 expand_loc
= pfile
->invocation_location
;
1272 return builtin_macro (pfile
, node
, loc
, expand_loc
);
1276 /* De-allocate the memory used by BUFF which is an array of instances
1277 of macro_arg. NUM_ARGS is the number of instances of macro_arg
1280 delete_macro_args (_cpp_buff
*buff
, unsigned num_args
)
1282 macro_arg
*macro_args
;
1288 macro_args
= (macro_arg
*) buff
->base
;
1290 /* Walk instances of macro_arg to free their expanded tokens as well
1291 as their macro_arg::virt_locs members. */
1292 for (i
= 0; i
< num_args
; ++i
)
1294 if (macro_args
[i
].expanded
)
1296 free (macro_args
[i
].expanded
);
1297 macro_args
[i
].expanded
= NULL
;
1299 if (macro_args
[i
].virt_locs
)
1301 free (macro_args
[i
].virt_locs
);
1302 macro_args
[i
].virt_locs
= NULL
;
1304 if (macro_args
[i
].expanded_virt_locs
)
1306 free (macro_args
[i
].expanded_virt_locs
);
1307 macro_args
[i
].expanded_virt_locs
= NULL
;
1310 _cpp_free_buff (buff
);
1313 /* Set the INDEXth token of the macro argument ARG. TOKEN is the token
1314 to set, LOCATION is its virtual location. "Virtual" location means
1315 the location that encodes loci across macro expansion. Otherwise
1316 it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the
1317 argument ARG is supposed to contain. Note that ARG must be
1318 tailored so that it has enough room to contain INDEX + 1 numbers of
1319 tokens, at least. */
1321 set_arg_token (macro_arg
*arg
, const cpp_token
*token
,
1322 source_location location
, size_t index
,
1323 enum macro_arg_token_kind kind
,
1324 bool track_macro_exp_p
)
1326 const cpp_token
**token_ptr
;
1327 source_location
*loc
= NULL
;
1330 arg_token_ptr_at (arg
, index
, kind
,
1331 track_macro_exp_p
? &loc
: NULL
);
1336 /* We can't set the location of a stringified argument
1337 token and we can't set any location if we aren't tracking
1338 macro expansion locations. */
1339 gcc_checking_assert (kind
!= MACRO_ARG_TOKEN_STRINGIFIED
1340 && track_macro_exp_p
);
1345 /* Get the pointer to the location of the argument token of the
1346 function-like macro argument ARG. This function must be called
1347 only when we -ftrack-macro-expansion is on. */
1348 static const source_location
*
1349 get_arg_token_location (const macro_arg
*arg
,
1350 enum macro_arg_token_kind kind
)
1352 const source_location
*loc
= NULL
;
1353 const cpp_token
**token_ptr
=
1354 arg_token_ptr_at (arg
, 0, kind
, (source_location
**) &loc
);
1356 if (token_ptr
== NULL
)
1362 /* Return the pointer to the INDEXth token of the macro argument ARG.
1363 KIND specifies the kind of token the macro argument ARG contains.
1364 If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
1365 of the virtual location of the returned token if the
1366 -ftrack-macro-expansion flag is on; otherwise, it's set to the
1367 spelling location of the returned token. */
1368 static const cpp_token
**
1369 arg_token_ptr_at (const macro_arg
*arg
, size_t index
,
1370 enum macro_arg_token_kind kind
,
1371 source_location
**virt_location
)
1373 const cpp_token
**tokens_ptr
= NULL
;
1377 case MACRO_ARG_TOKEN_NORMAL
:
1378 tokens_ptr
= arg
->first
;
1380 case MACRO_ARG_TOKEN_STRINGIFIED
:
1381 tokens_ptr
= (const cpp_token
**) &arg
->stringified
;
1383 case MACRO_ARG_TOKEN_EXPANDED
:
1384 tokens_ptr
= arg
->expanded
;
1388 if (tokens_ptr
== NULL
)
1389 /* This can happen for e.g, an empty token argument to a
1390 funtion-like macro. */
1395 if (kind
== MACRO_ARG_TOKEN_NORMAL
)
1396 *virt_location
= &arg
->virt_locs
[index
];
1397 else if (kind
== MACRO_ARG_TOKEN_EXPANDED
)
1398 *virt_location
= &arg
->expanded_virt_locs
[index
];
1399 else if (kind
== MACRO_ARG_TOKEN_STRINGIFIED
)
1401 (source_location
*) &tokens_ptr
[index
]->src_loc
;
1403 return &tokens_ptr
[index
];
1406 /* Initialize an iterator so that it iterates over the tokens of a
1407 function-like macro argument. KIND is the kind of tokens we want
1408 ITER to iterate over. TOKEN_PTR points the first token ITER will
1411 macro_arg_token_iter_init (macro_arg_token_iter
*iter
,
1412 bool track_macro_exp_p
,
1413 enum macro_arg_token_kind kind
,
1414 const macro_arg
*arg
,
1415 const cpp_token
**token_ptr
)
1417 iter
->track_macro_exp_p
= track_macro_exp_p
;
1419 iter
->token_ptr
= token_ptr
;
1420 /* Unconditionally initialize this so that the compiler doesn't warn
1421 about iter->location_ptr being possibly uninitialized later after
1422 this code has been inlined somewhere. */
1423 iter
->location_ptr
= NULL
;
1424 if (track_macro_exp_p
)
1425 iter
->location_ptr
= get_arg_token_location (arg
, kind
);
1427 iter
->num_forwards
= 0;
1428 if (track_macro_exp_p
1429 && token_ptr
!= NULL
1430 && iter
->location_ptr
== NULL
)
1435 /* Move the iterator one token forward. Note that if IT was
1436 initialized on an argument that has a stringified token, moving it
1437 forward doesn't make sense as a stringified token is essentially one
1440 macro_arg_token_iter_forward (macro_arg_token_iter
*it
)
1444 case MACRO_ARG_TOKEN_NORMAL
:
1445 case MACRO_ARG_TOKEN_EXPANDED
:
1447 if (it
->track_macro_exp_p
)
1450 case MACRO_ARG_TOKEN_STRINGIFIED
:
1452 if (it
->num_forwards
> 0)
1463 /* Return the token pointed to by the iterator. */
1464 static const cpp_token
*
1465 macro_arg_token_iter_get_token (const macro_arg_token_iter
*it
)
1468 if (it
->kind
== MACRO_ARG_TOKEN_STRINGIFIED
1469 && it
->num_forwards
> 0)
1472 if (it
->token_ptr
== NULL
)
1474 return *it
->token_ptr
;
1477 /* Return the location of the token pointed to by the iterator.*/
1478 static source_location
1479 macro_arg_token_iter_get_location (const macro_arg_token_iter
*it
)
1482 if (it
->kind
== MACRO_ARG_TOKEN_STRINGIFIED
1483 && it
->num_forwards
> 0)
1486 if (it
->track_macro_exp_p
)
1487 return *it
->location_ptr
;
1489 return (*it
->token_ptr
)->src_loc
;
1492 /* Return the index of a token [resulting from macro expansion] inside
1493 the total list of tokens resulting from a given macro
1494 expansion. The index can be different depending on whether if we
1495 want each tokens resulting from function-like macro arguments
1496 expansion to have a different location or not.
1498 E.g, consider this function-like macro:
1502 Then consider us "calling" it (and thus expanding it) like:
1506 It will be expanded into:
1510 Let's consider the case of the token '4'.
1512 Its index can be 2 (it's the third token of the set of tokens
1513 resulting from the expansion) or it can be 0 if we consider that
1514 all tokens resulting from the expansion of the argument "1+2" have
1515 the same index, which is 0. In this later case, the index of token
1516 '-' would then be 1 and the index of token '3' would be 2.
1518 The later case is useful to use less memory e.g, for the case of
1519 the user using the option -ftrack-macro-expansion=1.
1521 ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
1522 are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro
1523 parameter (inside the macro replacement list) that corresponds to
1524 the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
1527 If we refer to the example above, for the '4' argument token,
1528 ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
1529 would be set to the token 'x', in the replacement list "x - 3" of
1532 This is a subroutine of replace_args. */
1533 inline static unsigned
1534 expanded_token_index (cpp_reader
*pfile
, cpp_macro
*macro
,
1535 const cpp_token
*cur_replacement_token
,
1536 unsigned absolute_token_index
)
1538 if (CPP_OPTION (pfile
, track_macro_expansion
) > 1)
1539 return absolute_token_index
;
1540 return cur_replacement_token
- macro
->exp
.tokens
;
1543 /* Replace the parameters in a function-like macro of NODE with the
1544 actual ARGS, and place the result in a newly pushed token context.
1545 Expand each argument before replacing, unless it is operated upon
1546 by the # or ## operators. EXPANSION_POINT_LOC is the location of
1547 the expansion point of the macro. E.g, the location of the
1548 function-like macro invocation. */
1550 replace_args (cpp_reader
*pfile
, cpp_hashnode
*node
, cpp_macro
*macro
,
1551 macro_arg
*args
, source_location expansion_point_loc
)
1553 unsigned int i
, total
;
1554 const cpp_token
*src
, *limit
;
1555 const cpp_token
**first
= NULL
;
1557 _cpp_buff
*buff
= NULL
;
1558 source_location
*virt_locs
= NULL
;
1559 unsigned int exp_count
;
1560 const line_map_macro
*map
= NULL
;
1561 int track_macro_exp
;
1563 /* First, fully macro-expand arguments, calculating the number of
1564 tokens in the final expansion as we go. The ordering of the if
1565 statements below is subtle; we must handle stringification before
1568 /* EXP_COUNT is the number of tokens in the macro replacement
1569 list. TOTAL is the number of tokens /after/ macro parameters
1570 have been replaced by their arguments. */
1571 exp_count
= macro_real_token_count (macro
);
1573 limit
= macro
->exp
.tokens
+ exp_count
;
1575 for (src
= macro
->exp
.tokens
; src
< limit
; src
++)
1576 if (src
->type
== CPP_MACRO_ARG
)
1578 /* Leading and trailing padding tokens. */
1580 /* Account for leading and padding tokens in exp_count too.
1581 This is going to be important later down this function,
1582 when we want to handle the case of (track_macro_exp <
1586 /* We have an argument. If it is not being stringified or
1587 pasted it is macro-replaced before insertion. */
1588 arg
= &args
[src
->val
.macro_arg
.arg_no
- 1];
1590 if (src
->flags
& STRINGIFY_ARG
)
1592 if (!arg
->stringified
)
1593 arg
->stringified
= stringify_arg (pfile
, arg
);
1595 else if ((src
->flags
& PASTE_LEFT
)
1596 || (src
> macro
->exp
.tokens
&& (src
[-1].flags
& PASTE_LEFT
)))
1597 total
+= arg
->count
- 1;
1601 expand_arg (pfile
, arg
);
1602 total
+= arg
->expanded_count
- 1;
1606 /* When the compiler is called with the -ftrack-macro-expansion
1607 flag, we need to keep track of the location of each token that
1608 results from macro expansion.
1610 A token resulting from macro expansion is not a new token. It is
1611 simply the same token as the token coming from the macro
1612 definition. The new things that are allocated are the buffer
1613 that holds the tokens resulting from macro expansion and a new
1614 location that records many things like the locus of the expansion
1615 point as well as the original locus inside the definition of the
1616 macro. This location is called a virtual location.
1618 So the buffer BUFF holds a set of cpp_token*, and the buffer
1619 VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
1621 Both of these two buffers are going to be hung off of the macro
1622 context, when the latter is pushed. The memory allocated to
1623 store the tokens and their locations is going to be freed once
1624 the context of macro expansion is popped.
1626 As far as tokens are concerned, the memory overhead of
1627 -ftrack-macro-expansion is proportional to the number of
1628 macros that get expanded multiplied by sizeof (source_location).
1629 The good news is that extra memory gets freed when the macro
1630 context is freed, i.e shortly after the macro got expanded. */
1632 /* Is the -ftrack-macro-expansion flag in effect? */
1633 track_macro_exp
= CPP_OPTION (pfile
, track_macro_expansion
);
1635 /* Now allocate memory space for tokens and locations resulting from
1636 the macro expansion, copy the tokens and replace the arguments.
1637 This memory must be freed when the context of the macro MACRO is
1639 buff
= tokens_buff_new (pfile
, total
, track_macro_exp
? &virt_locs
: NULL
);
1641 first
= (const cpp_token
**) buff
->base
;
1643 /* Create a macro map to record the locations of the tokens that are
1644 involved in the expansion. Note that the expansion point is set
1645 to the location of the closing parenthesis. Otherwise, the
1646 subsequent map created for the first token that comes after the
1647 macro map might have a wrong line number. That would lead to
1648 tokens with wrong line numbers after the macro expansion. This
1649 adds up to the memory overhead of the -ftrack-macro-expansion
1650 flag; for every macro that is expanded, a "macro map" is
1652 if (track_macro_exp
)
1654 int num_macro_tokens
= total
;
1655 if (track_macro_exp
< 2)
1656 /* Then the number of macro tokens won't take in account the
1657 fact that function-like macro arguments can expand to
1658 multiple tokens. This is to save memory at the expense of
1661 Suppose we have #define SQARE(A) A * A
1663 And then we do SQARE(2+3)
1665 Then the tokens 2, +, 3, will have the same location,
1666 saying they come from the expansion of the argument A. */
1667 num_macro_tokens
= exp_count
;
1668 map
= linemap_enter_macro (pfile
->line_table
, node
,
1669 expansion_point_loc
,
1673 for (src
= macro
->exp
.tokens
; src
< limit
; src
++)
1675 unsigned int arg_tokens_count
;
1676 macro_arg_token_iter from
;
1677 const cpp_token
**paste_flag
= NULL
;
1678 const cpp_token
**tmp_token_ptr
;
1680 if (src
->type
!= CPP_MACRO_ARG
)
1682 /* Allocate a virtual location for token SRC, and add that
1683 token and its virtual location into the buffers BUFF and
1685 unsigned index
= expanded_token_index (pfile
, macro
, src
, i
);
1686 tokens_buff_add_token (buff
, virt_locs
, src
,
1687 src
->src_loc
, src
->src_loc
,
1694 arg
= &args
[src
->val
.macro_arg
.arg_no
- 1];
1695 /* SRC is a macro parameter that we need to replace with its
1696 corresponding argument. So at some point we'll need to
1697 iterate over the tokens of the macro argument and copy them
1698 into the "place" now holding the correspondig macro
1699 parameter. We are going to use the iterator type
1700 macro_argo_token_iter to handle that iterating. The 'if'
1701 below is to initialize the iterator depending on the type of
1702 tokens the macro argument has. It also does some adjustment
1703 related to padding tokens and some pasting corner cases. */
1704 if (src
->flags
& STRINGIFY_ARG
)
1706 arg_tokens_count
= 1;
1707 macro_arg_token_iter_init (&from
,
1709 track_macro_expansion
),
1710 MACRO_ARG_TOKEN_STRINGIFIED
,
1711 arg
, &arg
->stringified
);
1713 else if (src
->flags
& PASTE_LEFT
)
1715 arg_tokens_count
= arg
->count
;
1716 macro_arg_token_iter_init (&from
,
1718 track_macro_expansion
),
1719 MACRO_ARG_TOKEN_NORMAL
,
1722 else if (src
!= macro
->exp
.tokens
&& (src
[-1].flags
& PASTE_LEFT
))
1725 arg_tokens_count
= arg
->count
;
1726 macro_arg_token_iter_init (&from
,
1728 track_macro_expansion
),
1729 MACRO_ARG_TOKEN_NORMAL
,
1732 num_toks
= tokens_buff_count (buff
);
1736 /* So the current parameter token is pasted to the previous
1737 token in the replacement list. Let's look at what
1738 we have as previous and current arguments. */
1740 /* This is the previous argument's token ... */
1741 tmp_token_ptr
= tokens_buff_last_token_ptr (buff
);
1743 if ((*tmp_token_ptr
)->type
== CPP_COMMA
1745 && src
->val
.macro_arg
.arg_no
== macro
->paramc
)
1747 /* ... which is a comma; and the current parameter
1748 is the last parameter of a variadic function-like
1749 macro. If the argument to the current last
1750 parameter is NULL, then swallow the comma,
1751 otherwise drop the paste flag. */
1752 if (macro_arg_token_iter_get_token (&from
) == NULL
)
1753 tokens_buff_remove_last_token (buff
);
1755 paste_flag
= tmp_token_ptr
;
1757 /* Remove the paste flag if the RHS is a placemarker. */
1758 else if (arg_tokens_count
== 0)
1759 paste_flag
= tmp_token_ptr
;
1764 arg_tokens_count
= arg
->expanded_count
;
1765 macro_arg_token_iter_init (&from
,
1767 track_macro_expansion
),
1768 MACRO_ARG_TOKEN_EXPANDED
,
1769 arg
, arg
->expanded
);
1772 /* Padding on the left of an argument (unless RHS of ##). */
1773 if ((!pfile
->state
.in_directive
|| pfile
->state
.directive_wants_padding
)
1774 && src
!= macro
->exp
.tokens
&& !(src
[-1].flags
& PASTE_LEFT
))
1776 const cpp_token
*t
= padding_token (pfile
, src
);
1777 unsigned index
= expanded_token_index (pfile
, macro
, src
, i
);
1778 /* Allocate a virtual location for the padding token and
1779 append the token and its location to BUFF and
1781 tokens_buff_add_token (buff
, virt_locs
, t
,
1782 t
->src_loc
, t
->src_loc
,
1786 if (arg_tokens_count
)
1788 /* So now we've got the number of tokens that make up the
1789 argument that is going to replace the current parameter
1790 in the macro's replacement list. */
1792 for (j
= 0; j
< arg_tokens_count
; ++j
)
1794 /* So if track_macro_exp is < 2, the user wants to
1795 save extra memory while tracking macro expansion
1796 locations. So in that case here is what we do:
1798 Suppose we have #define SQARE(A) A * A
1800 And then we do SQARE(2+3)
1802 Then the tokens 2, +, 3, will have the same location,
1803 saying they come from the expansion of the argument
1806 So that means we are going to ignore the COUNT tokens
1807 resulting from the expansion of the current macro
1808 arugment. In other words all the ARG_TOKENS_COUNT tokens
1809 resulting from the expansion of the macro argument will
1810 have the index I. Normally, each of those token should
1812 unsigned token_index
= i
;
1814 if (track_macro_exp
> 1)
1817 index
= expanded_token_index (pfile
, macro
, src
, token_index
);
1818 tokens_buff_add_token (buff
, virt_locs
,
1819 macro_arg_token_iter_get_token (&from
),
1820 macro_arg_token_iter_get_location (&from
),
1821 src
->src_loc
, map
, index
);
1822 macro_arg_token_iter_forward (&from
);
1825 /* With a non-empty argument on the LHS of ##, the last
1826 token should be flagged PASTE_LEFT. */
1827 if (src
->flags
& PASTE_LEFT
)
1829 (const cpp_token
**) tokens_buff_last_token_ptr (buff
);
1831 else if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, c99
)
1832 && ! macro
->syshdr
&& ! cpp_in_system_header (pfile
))
1834 if (CPP_OPTION (pfile
, cplusplus
))
1835 cpp_pedwarning (pfile
, CPP_W_PEDANTIC
,
1836 "invoking macro %s argument %d: "
1837 "empty macro arguments are undefined"
1839 NODE_NAME (node
), src
->val
.macro_arg
.arg_no
);
1840 else if (CPP_OPTION (pfile
, cpp_warn_c90_c99_compat
))
1841 cpp_pedwarning (pfile
,
1842 CPP_OPTION (pfile
, cpp_warn_c90_c99_compat
) > 0
1843 ? CPP_W_C90_C99_COMPAT
: CPP_W_PEDANTIC
,
1844 "invoking macro %s argument %d: "
1845 "empty macro arguments are undefined"
1847 NODE_NAME (node
), src
->val
.macro_arg
.arg_no
);
1849 else if (CPP_OPTION (pfile
, cpp_warn_c90_c99_compat
) > 0
1850 && ! CPP_OPTION (pfile
, cplusplus
)
1851 && ! macro
->syshdr
&& ! cpp_in_system_header (pfile
))
1852 cpp_warning (pfile
, CPP_W_C90_C99_COMPAT
,
1853 "invoking macro %s argument %d: "
1854 "empty macro arguments are undefined"
1856 NODE_NAME (node
), src
->val
.macro_arg
.arg_no
);
1858 /* Avoid paste on RHS (even case count == 0). */
1859 if (!pfile
->state
.in_directive
&& !(src
->flags
& PASTE_LEFT
))
1861 const cpp_token
*t
= &pfile
->avoid_paste
;
1862 tokens_buff_add_token (buff
, virt_locs
,
1863 t
, t
->src_loc
, t
->src_loc
,
1867 /* Add a new paste flag, or remove an unwanted one. */
1870 cpp_token
*token
= _cpp_temp_token (pfile
);
1871 token
->type
= (*paste_flag
)->type
;
1872 token
->val
= (*paste_flag
)->val
;
1873 if (src
->flags
& PASTE_LEFT
)
1874 token
->flags
= (*paste_flag
)->flags
| PASTE_LEFT
;
1876 token
->flags
= (*paste_flag
)->flags
& ~PASTE_LEFT
;
1877 *paste_flag
= token
;
1880 i
+= arg_tokens_count
;
1883 if (track_macro_exp
)
1884 push_extended_tokens_context (pfile
, node
, buff
, virt_locs
, first
,
1885 tokens_buff_count (buff
));
1887 push_ptoken_context (pfile
, node
, buff
, first
,
1888 tokens_buff_count (buff
));
1890 num_macro_tokens_counter
+= tokens_buff_count (buff
);
1893 /* Return a special padding token, with padding inherited from SOURCE. */
1894 static const cpp_token
*
1895 padding_token (cpp_reader
*pfile
, const cpp_token
*source
)
1897 cpp_token
*result
= _cpp_temp_token (pfile
);
1899 result
->type
= CPP_PADDING
;
1901 /* Data in GCed data structures cannot be made const so far, so we
1902 need a cast here. */
1903 result
->val
.source
= (cpp_token
*) source
;
1908 /* Get a new uninitialized context. Create a new one if we cannot
1909 re-use an old one. */
1910 static cpp_context
*
1911 next_context (cpp_reader
*pfile
)
1913 cpp_context
*result
= pfile
->context
->next
;
1917 result
= XNEW (cpp_context
);
1918 memset (result
, 0, sizeof (cpp_context
));
1919 result
->prev
= pfile
->context
;
1921 pfile
->context
->next
= result
;
1924 pfile
->context
= result
;
1928 /* Push a list of pointers to tokens. */
1930 push_ptoken_context (cpp_reader
*pfile
, cpp_hashnode
*macro
, _cpp_buff
*buff
,
1931 const cpp_token
**first
, unsigned int count
)
1933 cpp_context
*context
= next_context (pfile
);
1935 context
->tokens_kind
= TOKENS_KIND_INDIRECT
;
1936 context
->c
.macro
= macro
;
1937 context
->buff
= buff
;
1938 FIRST (context
).ptoken
= first
;
1939 LAST (context
).ptoken
= first
+ count
;
1942 /* Push a list of tokens.
1944 A NULL macro means that we should continue the current macro
1945 expansion, in essence. That means that if we are currently in a
1946 macro expansion context, we'll make the new pfile->context refer to
1947 the current macro. */
1949 _cpp_push_token_context (cpp_reader
*pfile
, cpp_hashnode
*macro
,
1950 const cpp_token
*first
, unsigned int count
)
1952 cpp_context
*context
;
1955 macro
= macro_of_context (pfile
->context
);
1957 context
= next_context (pfile
);
1958 context
->tokens_kind
= TOKENS_KIND_DIRECT
;
1959 context
->c
.macro
= macro
;
1960 context
->buff
= NULL
;
1961 FIRST (context
).token
= first
;
1962 LAST (context
).token
= first
+ count
;
1965 /* Build a context containing a list of tokens as well as their
1966 virtual locations and push it. TOKENS_BUFF is the buffer that
1967 contains the tokens pointed to by FIRST. If TOKENS_BUFF is
1968 non-NULL, it means that the context owns it, meaning that
1969 _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
1970 contains the virtual locations.
1972 A NULL macro means that we should continue the current macro
1973 expansion, in essence. That means that if we are currently in a
1974 macro expansion context, we'll make the new pfile->context refer to
1975 the current macro. */
1977 push_extended_tokens_context (cpp_reader
*pfile
,
1978 cpp_hashnode
*macro
,
1979 _cpp_buff
*token_buff
,
1980 source_location
*virt_locs
,
1981 const cpp_token
**first
,
1984 cpp_context
*context
;
1988 macro
= macro_of_context (pfile
->context
);
1990 context
= next_context (pfile
);
1991 context
->tokens_kind
= TOKENS_KIND_EXTENDED
;
1992 context
->buff
= token_buff
;
1994 m
= XNEW (macro_context
);
1995 m
->macro_node
= macro
;
1996 m
->virt_locs
= virt_locs
;
1997 m
->cur_virt_loc
= virt_locs
;
1999 FIRST (context
).ptoken
= first
;
2000 LAST (context
).ptoken
= first
+ count
;
2003 /* Push a traditional macro's replacement text. */
2005 _cpp_push_text_context (cpp_reader
*pfile
, cpp_hashnode
*macro
,
2006 const uchar
*start
, size_t len
)
2008 cpp_context
*context
= next_context (pfile
);
2010 context
->tokens_kind
= TOKENS_KIND_DIRECT
;
2011 context
->c
.macro
= macro
;
2012 context
->buff
= NULL
;
2013 CUR (context
) = start
;
2014 RLIMIT (context
) = start
+ len
;
2015 macro
->flags
|= NODE_DISABLED
;
2018 /* Creates a buffer that holds tokens a.k.a "token buffer", usually
2019 for the purpose of storing them on a cpp_context. If VIRT_LOCS is
2020 non-null (which means that -ftrack-macro-expansion is on),
2021 *VIRT_LOCS is set to a newly allocated buffer that is supposed to
2022 hold the virtual locations of the tokens resulting from macro
2025 tokens_buff_new (cpp_reader
*pfile
, size_t len
,
2026 source_location
**virt_locs
)
2028 size_t tokens_size
= len
* sizeof (cpp_token
*);
2029 size_t locs_size
= len
* sizeof (source_location
);
2031 if (virt_locs
!= NULL
)
2032 *virt_locs
= XNEWVEC (source_location
, locs_size
);
2033 return _cpp_get_buff (pfile
, tokens_size
);
2036 /* Returns the number of tokens contained in a token buffer. The
2037 buffer holds a set of cpp_token*. */
2039 tokens_buff_count (_cpp_buff
*buff
)
2041 return (BUFF_FRONT (buff
) - buff
->base
) / sizeof (cpp_token
*);
2044 /* Return a pointer to the last token contained in the token buffer
2046 static const cpp_token
**
2047 tokens_buff_last_token_ptr (_cpp_buff
*buff
)
2049 return &((const cpp_token
**) BUFF_FRONT (buff
))[-1];
2052 /* Remove the last token contained in the token buffer TOKENS_BUFF.
2053 If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer
2054 containing the virtual locations of the tokens in TOKENS_BUFF; in
2055 which case the function updates that buffer as well. */
2057 tokens_buff_remove_last_token (_cpp_buff
*tokens_buff
)
2060 if (BUFF_FRONT (tokens_buff
) > tokens_buff
->base
)
2061 BUFF_FRONT (tokens_buff
) =
2062 (unsigned char *) &((cpp_token
**) BUFF_FRONT (tokens_buff
))[-1];
2065 /* Insert a token into the token buffer at the position pointed to by
2066 DEST. Note that the buffer is not enlarged so the previous token
2067 that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null,
2068 means -ftrack-macro-expansion is effect; it then points to where to
2069 insert the virtual location of TOKEN. TOKEN is the token to
2070 insert. VIRT_LOC is the virtual location of the token, i.e, the
2071 location possibly encoding its locus across macro expansion. If
2072 TOKEN is an argument of a function-like macro (inside a macro
2073 replacement list), PARM_DEF_LOC is the spelling location of the
2074 macro parameter that TOKEN is replacing, in the replacement list of
2075 the macro. If TOKEN is not an argument of a function-like macro or
2076 if it doesn't come from a macro expansion, then VIRT_LOC can just
2077 be set to the same value as PARM_DEF_LOC. If MAP is non null, it
2078 means TOKEN comes from a macro expansion and MAP is the macro map
2079 associated to the macro. MACRO_TOKEN_INDEX points to the index of
2080 the token in the macro map; it is not considered if MAP is NULL.
2082 Upon successful completion this function returns the a pointer to
2083 the position of the token coming right after the insertion
2085 static inline const cpp_token
**
2086 tokens_buff_put_token_to (const cpp_token
**dest
,
2087 source_location
*virt_loc_dest
,
2088 const cpp_token
*token
,
2089 source_location virt_loc
,
2090 source_location parm_def_loc
,
2091 const line_map_macro
*map
,
2092 unsigned int macro_token_index
)
2094 source_location macro_loc
= virt_loc
;
2095 const cpp_token
**result
;
2099 /* -ftrack-macro-expansion is on. */
2101 macro_loc
= linemap_add_macro_token (map
, macro_token_index
,
2102 virt_loc
, parm_def_loc
);
2103 *virt_loc_dest
= macro_loc
;
2111 /* Adds a token at the end of the tokens contained in BUFFER. Note
2112 that this function doesn't enlarge BUFFER when the number of tokens
2113 reaches BUFFER's size; it aborts in that situation.
2115 TOKEN is the token to append. VIRT_LOC is the virtual location of
2116 the token, i.e, the location possibly encoding its locus across
2117 macro expansion. If TOKEN is an argument of a function-like macro
2118 (inside a macro replacement list), PARM_DEF_LOC is the location of
2119 the macro parameter that TOKEN is replacing. If TOKEN doesn't come
2120 from a macro expansion, then VIRT_LOC can just be set to the same
2121 value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes
2122 from a macro expansion and MAP is the macro map associated to the
2123 macro. MACRO_TOKEN_INDEX points to the index of the token in the
2124 macro map; It is not considered if MAP is NULL. If VIRT_LOCS is
2125 non-null, it means -ftrack-macro-expansion is on; in which case
2126 this function adds the virtual location DEF_LOC to the VIRT_LOCS
2127 array, at the same index as the one of TOKEN in BUFFER. Upon
2128 successful completion this function returns the a pointer to the
2129 position of the token coming right after the insertion point. */
2130 static const cpp_token
**
2131 tokens_buff_add_token (_cpp_buff
*buffer
,
2132 source_location
*virt_locs
,
2133 const cpp_token
*token
,
2134 source_location virt_loc
,
2135 source_location parm_def_loc
,
2136 const line_map_macro
*map
,
2137 unsigned int macro_token_index
)
2139 const cpp_token
**result
;
2140 source_location
*virt_loc_dest
= NULL
;
2141 unsigned token_index
=
2142 (BUFF_FRONT (buffer
) - buffer
->base
) / sizeof (cpp_token
*);
2144 /* Abort if we pass the end the buffer. */
2145 if (BUFF_FRONT (buffer
) > BUFF_LIMIT (buffer
))
2148 if (virt_locs
!= NULL
)
2149 virt_loc_dest
= &virt_locs
[token_index
];
2152 tokens_buff_put_token_to ((const cpp_token
**) BUFF_FRONT (buffer
),
2153 virt_loc_dest
, token
, virt_loc
, parm_def_loc
,
2154 map
, macro_token_index
);
2156 BUFF_FRONT (buffer
) = (unsigned char *) result
;
2160 /* Allocate space for the function-like macro argument ARG to store
2161 the tokens resulting from the macro-expansion of the tokens that
2162 make up ARG itself. That space is allocated in ARG->expanded and
2163 needs to be freed using free. */
2165 alloc_expanded_arg_mem (cpp_reader
*pfile
, macro_arg
*arg
, size_t capacity
)
2167 gcc_checking_assert (arg
->expanded
== NULL
2168 && arg
->expanded_virt_locs
== NULL
);
2170 arg
->expanded
= XNEWVEC (const cpp_token
*, capacity
);
2171 if (CPP_OPTION (pfile
, track_macro_expansion
))
2172 arg
->expanded_virt_locs
= XNEWVEC (source_location
, capacity
);
2176 /* If necessary, enlarge ARG->expanded to so that it can contain SIZE
2179 ensure_expanded_arg_room (cpp_reader
*pfile
, macro_arg
*arg
,
2180 size_t size
, size_t *expanded_capacity
)
2182 if (size
<= *expanded_capacity
)
2188 XRESIZEVEC (const cpp_token
*, arg
->expanded
, size
);
2189 *expanded_capacity
= size
;
2191 if (CPP_OPTION (pfile
, track_macro_expansion
))
2193 if (arg
->expanded_virt_locs
== NULL
)
2194 arg
->expanded_virt_locs
= XNEWVEC (source_location
, size
);
2196 arg
->expanded_virt_locs
= XRESIZEVEC (source_location
,
2197 arg
->expanded_virt_locs
,
2202 /* Expand an argument ARG before replacing parameters in a
2203 function-like macro. This works by pushing a context with the
2204 argument's tokens, and then expanding that into a temporary buffer
2205 as if it were a normal part of the token stream. collect_args()
2206 has terminated the argument's tokens with a CPP_EOF so that we know
2207 when we have fully expanded the argument. */
2209 expand_arg (cpp_reader
*pfile
, macro_arg
*arg
)
2212 bool saved_warn_trad
;
2213 bool track_macro_exp_p
= CPP_OPTION (pfile
, track_macro_expansion
);
2216 || arg
->expanded
!= NULL
)
2219 /* Don't warn about funlike macros when pre-expanding. */
2220 saved_warn_trad
= CPP_WTRADITIONAL (pfile
);
2221 CPP_WTRADITIONAL (pfile
) = 0;
2223 /* Loop, reading in the tokens of the argument. */
2225 alloc_expanded_arg_mem (pfile
, arg
, capacity
);
2227 if (track_macro_exp_p
)
2228 push_extended_tokens_context (pfile
, NULL
, NULL
,
2233 push_ptoken_context (pfile
, NULL
, NULL
,
2234 arg
->first
, arg
->count
+ 1);
2238 const cpp_token
*token
;
2239 source_location location
;
2241 ensure_expanded_arg_room (pfile
, arg
, arg
->expanded_count
+ 1,
2244 token
= cpp_get_token_1 (pfile
, &location
);
2246 if (token
->type
== CPP_EOF
)
2249 set_arg_token (arg
, token
, location
,
2250 arg
->expanded_count
, MACRO_ARG_TOKEN_EXPANDED
,
2251 CPP_OPTION (pfile
, track_macro_expansion
));
2252 arg
->expanded_count
++;
2255 _cpp_pop_context (pfile
);
2257 CPP_WTRADITIONAL (pfile
) = saved_warn_trad
;
2260 /* Returns the macro associated to the current context if we are in
2261 the context a macro expansion, NULL otherwise. */
2262 static cpp_hashnode
*
2263 macro_of_context (cpp_context
*context
)
2265 if (context
== NULL
)
2268 return (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2269 ? context
->c
.mc
->macro_node
2273 /* Return TRUE iff we are expanding a macro or are about to start
2274 expanding one. If we are effectively expanding a macro, the
2275 function macro_of_context returns a pointer to the macro being
2278 in_macro_expansion_p (cpp_reader
*pfile
)
2283 return (pfile
->about_to_expand_macro_p
2284 || macro_of_context (pfile
->context
));
2287 /* Pop the current context off the stack, re-enabling the macro if the
2288 context represented a macro's replacement list. Initially the
2289 context structure was not freed so that we can re-use it later, but
2290 now we do free it to reduce peak memory consumption. */
2292 _cpp_pop_context (cpp_reader
*pfile
)
2294 cpp_context
*context
= pfile
->context
;
2296 /* We should not be popping the base context. */
2297 if (context
== &pfile
->base_context
)
2300 if (context
->c
.macro
)
2302 cpp_hashnode
*macro
;
2303 if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2305 macro_context
*mc
= context
->c
.mc
;
2306 macro
= mc
->macro_node
;
2307 /* If context->buff is set, it means the life time of tokens
2308 is bound to the life time of this context; so we must
2309 free the tokens; that means we must free the virtual
2310 locations of these tokens too. */
2311 if (context
->buff
&& mc
->virt_locs
)
2313 free (mc
->virt_locs
);
2314 mc
->virt_locs
= NULL
;
2317 context
->c
.mc
= NULL
;
2320 macro
= context
->c
.macro
;
2322 /* Beware that MACRO can be NULL in cases like when we are
2323 called from expand_arg. In those cases, a dummy context with
2324 tokens is pushed just for the purpose of walking them using
2325 cpp_get_token_1. In that case, no 'macro' field is set into
2326 the dummy context. */
2328 /* Several contiguous macro expansion contexts can be
2329 associated to the same macro; that means it's the same
2330 macro expansion that spans across all these (sub)
2331 contexts. So we should re-enable an expansion-disabled
2332 macro only when we are sure we are really out of that
2334 && macro_of_context (context
->prev
) != macro
)
2335 macro
->flags
&= ~NODE_DISABLED
;
2337 if (macro
== pfile
->top_most_macro_node
&& context
->prev
== NULL
)
2338 /* We are popping the context of the top-most macro node. */
2339 pfile
->top_most_macro_node
= NULL
;
2344 /* Decrease memory peak consumption by freeing the memory used
2346 _cpp_free_buff (context
->buff
);
2349 pfile
->context
= context
->prev
;
2350 /* decrease peak memory consumption by feeing the context. */
2351 pfile
->context
->next
= NULL
;
2355 /* Return TRUE if we reached the end of the set of tokens stored in
2356 CONTEXT, FALSE otherwise. */
2358 reached_end_of_context (cpp_context
*context
)
2360 if (context
->tokens_kind
== TOKENS_KIND_DIRECT
)
2361 return FIRST (context
).token
== LAST (context
).token
;
2362 else if (context
->tokens_kind
== TOKENS_KIND_INDIRECT
2363 || context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2364 return FIRST (context
).ptoken
== LAST (context
).ptoken
;
2369 /* Consume the next token contained in the current context of PFILE,
2370 and return it in *TOKEN. It's "full location" is returned in
2371 *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
2372 means the location encoding the locus of the token across macro
2373 expansion; otherwise it's just is the "normal" location of the
2374 token which (*TOKEN)->src_loc. */
2376 consume_next_token_from_context (cpp_reader
*pfile
,
2377 const cpp_token
** token
,
2378 source_location
*location
)
2380 cpp_context
*c
= pfile
->context
;
2382 if ((c
)->tokens_kind
== TOKENS_KIND_DIRECT
)
2384 *token
= FIRST (c
).token
;
2385 *location
= (*token
)->src_loc
;
2388 else if ((c
)->tokens_kind
== TOKENS_KIND_INDIRECT
)
2390 *token
= *FIRST (c
).ptoken
;
2391 *location
= (*token
)->src_loc
;
2394 else if ((c
)->tokens_kind
== TOKENS_KIND_EXTENDED
)
2396 macro_context
*m
= c
->c
.mc
;
2397 *token
= *FIRST (c
).ptoken
;
2400 *location
= *m
->cur_virt_loc
;
2404 *location
= (*token
)->src_loc
;
2411 /* In the traditional mode of the preprocessor, if we are currently in
2412 a directive, the location of a token must be the location of the
2413 start of the directive line. This function returns the proper
2414 location if we are in the traditional mode, and just returns
2415 LOCATION otherwise. */
2417 static inline source_location
2418 maybe_adjust_loc_for_trad_cpp (cpp_reader
*pfile
, source_location location
)
2420 if (CPP_OPTION (pfile
, traditional
))
2422 if (pfile
->state
.in_directive
)
2423 return pfile
->directive_line
;
2428 /* Routine to get a token as well as its location.
2430 Macro expansions and directives are transparently handled,
2431 including entering included files. Thus tokens are post-macro
2432 expansion, and after any intervening directives. External callers
2433 see CPP_EOF only at EOF. Internal callers also see it when meeting
2434 a directive inside a macro call, when at the end of a directive and
2435 state.in_directive is still 1, and at the end of argument
2438 LOC is an out parameter; *LOC is set to the location "as expected
2439 by the user". Please read the comment of
2440 cpp_get_token_with_location to learn more about the meaning of this
2442 static const cpp_token
*
2443 cpp_get_token_1 (cpp_reader
*pfile
, source_location
*location
)
2445 const cpp_token
*result
;
2446 /* This token is a virtual token that either encodes a location
2447 related to macro expansion or a spelling location. */
2448 source_location virt_loc
= 0;
2449 /* pfile->about_to_expand_macro_p can be overriden by indirect calls
2450 to functions that push macro contexts. So let's save it so that
2451 we can restore it when we are about to leave this routine. */
2452 bool saved_about_to_expand_macro
= pfile
->about_to_expand_macro_p
;
2457 cpp_context
*context
= pfile
->context
;
2459 /* Context->prev == 0 <=> base context. */
2462 result
= _cpp_lex_token (pfile
);
2463 virt_loc
= result
->src_loc
;
2465 else if (!reached_end_of_context (context
))
2467 consume_next_token_from_context (pfile
, &result
,
2469 if (result
->flags
& PASTE_LEFT
)
2471 paste_all_tokens (pfile
, result
);
2472 if (pfile
->state
.in_directive
)
2474 result
= padding_token (pfile
, result
);
2480 if (pfile
->context
->c
.macro
)
2481 ++num_expanded_macros_counter
;
2482 _cpp_pop_context (pfile
);
2483 if (pfile
->state
.in_directive
)
2485 result
= &pfile
->avoid_paste
;
2489 if (pfile
->state
.in_directive
&& result
->type
== CPP_COMMENT
)
2492 if (result
->type
!= CPP_NAME
)
2495 node
= result
->val
.node
.node
;
2497 if (node
->type
!= NT_MACRO
|| (result
->flags
& NO_EXPAND
))
2500 if (!(node
->flags
& NODE_DISABLED
))
2503 /* If not in a macro context, and we're going to start an
2504 expansion, record the location and the top level macro
2505 about to be expanded. */
2506 if (!in_macro_expansion_p (pfile
))
2508 pfile
->invocation_location
= result
->src_loc
;
2509 pfile
->top_most_macro_node
= node
;
2511 if (pfile
->state
.prevent_expansion
)
2514 /* Conditional macros require that a predicate be evaluated
2516 if ((node
->flags
& NODE_CONDITIONAL
) != 0)
2518 if (pfile
->cb
.macro_to_expand
)
2520 bool whitespace_after
;
2521 const cpp_token
*peek_tok
= cpp_peek_token (pfile
, 0);
2523 whitespace_after
= (peek_tok
->type
== CPP_PADDING
2524 || (peek_tok
->flags
& PREV_WHITE
));
2525 node
= pfile
->cb
.macro_to_expand (pfile
, result
);
2527 ret
= enter_macro_context (pfile
, node
, result
,
2529 else if (whitespace_after
)
2531 /* If macro_to_expand hook returned NULL and it
2532 ate some tokens, see if we don't need to add
2533 a padding token in between this and the
2535 peek_tok
= cpp_peek_token (pfile
, 0);
2536 if (peek_tok
->type
!= CPP_PADDING
2537 && (peek_tok
->flags
& PREV_WHITE
) == 0)
2538 _cpp_push_token_context (pfile
, NULL
,
2539 padding_token (pfile
,
2545 ret
= enter_macro_context (pfile
, node
, result
,
2549 if (pfile
->state
.in_directive
|| ret
== 2)
2551 result
= padding_token (pfile
, result
);
2557 /* Flag this token as always unexpandable. FIXME: move this
2558 to collect_args()?. */
2559 cpp_token
*t
= _cpp_temp_token (pfile
);
2560 t
->type
= result
->type
;
2561 t
->flags
= result
->flags
| NO_EXPAND
;
2562 t
->val
= result
->val
;
2570 if (location
!= NULL
)
2573 virt_loc
= result
->src_loc
;
2574 *location
= virt_loc
;
2576 if (!CPP_OPTION (pfile
, track_macro_expansion
)
2577 && macro_of_context (pfile
->context
) != NULL
)
2578 /* We are in a macro expansion context, are not tracking
2579 virtual location, but were asked to report the location
2580 of the expansion point of the macro being expanded. */
2581 *location
= pfile
->invocation_location
;
2583 *location
= maybe_adjust_loc_for_trad_cpp (pfile
, *location
);
2586 pfile
->about_to_expand_macro_p
= saved_about_to_expand_macro
;
2590 /* External routine to get a token. Also used nearly everywhere
2591 internally, except for places where we know we can safely call
2592 _cpp_lex_token directly, such as lexing a directive name.
2594 Macro expansions and directives are transparently handled,
2595 including entering included files. Thus tokens are post-macro
2596 expansion, and after any intervening directives. External callers
2597 see CPP_EOF only at EOF. Internal callers also see it when meeting
2598 a directive inside a macro call, when at the end of a directive and
2599 state.in_directive is still 1, and at the end of argument
2602 cpp_get_token (cpp_reader
*pfile
)
2604 return cpp_get_token_1 (pfile
, NULL
);
2607 /* Like cpp_get_token, but also returns a virtual token location
2608 separate from the spelling location carried by the returned token.
2610 LOC is an out parameter; *LOC is set to the location "as expected
2611 by the user". This matters when a token results from macro
2612 expansion; in that case the token's spelling location indicates the
2613 locus of the token in the definition of the macro but *LOC
2614 virtually encodes all the other meaningful locuses associated to
2617 What? virtual location? Yes, virtual location.
2619 If the token results from macro expansion and if macro expansion
2620 location tracking is enabled its virtual location encodes (at the
2623 - the spelling location of the token
2625 - the locus of the macro expansion point
2627 - the locus of the point where the token got instantiated as part
2628 of the macro expansion process.
2630 You have to use the linemap API to get the locus you are interested
2631 in from a given virtual location.
2633 Note however that virtual locations are not necessarily ordered for
2634 relations '<' and '>'. One must use the function
2635 linemap_location_before_p instead of using the relational operator
2638 If macro expansion tracking is off and if the token results from
2639 macro expansion the virtual location is the expansion point of the
2640 macro that got expanded.
2642 When the token doesn't result from macro expansion, the virtual
2643 location is just the same thing as its spelling location. */
2646 cpp_get_token_with_location (cpp_reader
*pfile
, source_location
*loc
)
2648 return cpp_get_token_1 (pfile
, loc
);
2651 /* Returns true if we're expanding an object-like macro that was
2652 defined in a system header. Just checks the macro at the top of
2653 the stack. Used for diagnostic suppression. */
2655 cpp_sys_macro_p (cpp_reader
*pfile
)
2657 cpp_hashnode
*node
= NULL
;
2659 if (pfile
->context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2660 node
= pfile
->context
->c
.mc
->macro_node
;
2662 node
= pfile
->context
->c
.macro
;
2664 return node
&& node
->value
.macro
&& node
->value
.macro
->syshdr
;
2667 /* Read each token in, until end of the current file. Directives are
2668 transparently processed. */
2670 cpp_scan_nooutput (cpp_reader
*pfile
)
2672 /* Request a CPP_EOF token at the end of this file, rather than
2673 transparently continuing with the including file. */
2674 pfile
->buffer
->return_at_eof
= true;
2676 pfile
->state
.discarding_output
++;
2677 pfile
->state
.prevent_expansion
++;
2679 if (CPP_OPTION (pfile
, traditional
))
2680 while (_cpp_read_logical_line_trad (pfile
))
2683 while (cpp_get_token (pfile
)->type
!= CPP_EOF
)
2686 pfile
->state
.discarding_output
--;
2687 pfile
->state
.prevent_expansion
--;
2690 /* Step back one or more tokens obtained from the lexer. */
2692 _cpp_backup_tokens_direct (cpp_reader
*pfile
, unsigned int count
)
2694 pfile
->lookaheads
+= count
;
2698 if (pfile
->cur_token
== pfile
->cur_run
->base
2699 /* Possible with -fpreprocessed and no leading #line. */
2700 && pfile
->cur_run
->prev
!= NULL
)
2702 pfile
->cur_run
= pfile
->cur_run
->prev
;
2703 pfile
->cur_token
= pfile
->cur_run
->limit
;
2708 /* Step back one (or more) tokens. Can only step back more than 1 if
2709 they are from the lexer, and not from macro expansion. */
2711 _cpp_backup_tokens (cpp_reader
*pfile
, unsigned int count
)
2713 if (pfile
->context
->prev
== NULL
)
2714 _cpp_backup_tokens_direct (pfile
, count
);
2719 if (pfile
->context
->tokens_kind
== TOKENS_KIND_DIRECT
)
2720 FIRST (pfile
->context
).token
--;
2721 else if (pfile
->context
->tokens_kind
== TOKENS_KIND_INDIRECT
)
2722 FIRST (pfile
->context
).ptoken
--;
2723 else if (pfile
->context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2725 FIRST (pfile
->context
).ptoken
--;
2726 if (pfile
->context
->c
.macro
)
2728 macro_context
*m
= pfile
->context
->c
.mc
;
2730 gcc_checking_assert (m
->cur_virt_loc
>= m
->virt_locs
);
2740 /* #define directive parsing and handling. */
2742 /* Returns nonzero if a macro redefinition warning is required. */
2744 warn_of_redefinition (cpp_reader
*pfile
, cpp_hashnode
*node
,
2745 const cpp_macro
*macro2
)
2747 const cpp_macro
*macro1
;
2750 /* Some redefinitions need to be warned about regardless. */
2751 if (node
->flags
& NODE_WARN
)
2754 /* Suppress warnings for builtins that lack the NODE_WARN flag,
2755 unless Wbuiltin-macro-redefined. */
2756 if (node
->flags
& NODE_BUILTIN
2757 && (!pfile
->cb
.user_builtin_macro
2758 || !pfile
->cb
.user_builtin_macro (pfile
, node
)))
2759 return CPP_OPTION (pfile
, warn_builtin_macro_redefined
);
2761 /* Redefinitions of conditional (context-sensitive) macros, on
2762 the other hand, must be allowed silently. */
2763 if (node
->flags
& NODE_CONDITIONAL
)
2766 /* Redefinition of a macro is allowed if and only if the old and new
2767 definitions are the same. (6.10.3 paragraph 2). */
2768 macro1
= node
->value
.macro
;
2770 /* Don't check count here as it can be different in valid
2771 traditional redefinitions with just whitespace differences. */
2772 if (macro1
->paramc
!= macro2
->paramc
2773 || macro1
->fun_like
!= macro2
->fun_like
2774 || macro1
->variadic
!= macro2
->variadic
)
2777 /* Check parameter spellings. */
2778 for (i
= 0; i
< macro1
->paramc
; i
++)
2779 if (macro1
->params
[i
] != macro2
->params
[i
])
2782 /* Check the replacement text or tokens. */
2783 if (CPP_OPTION (pfile
, traditional
))
2784 return _cpp_expansions_different_trad (macro1
, macro2
);
2786 if (macro1
->count
!= macro2
->count
)
2789 for (i
= 0; i
< macro1
->count
; i
++)
2790 if (!_cpp_equiv_tokens (¯o1
->exp
.tokens
[i
], ¯o2
->exp
.tokens
[i
]))
2796 /* Free the definition of hashnode H. */
2798 _cpp_free_definition (cpp_hashnode
*h
)
2800 /* Macros and assertions no longer have anything to free. */
2802 /* Clear builtin flag in case of redefinition. */
2803 h
->flags
&= ~(NODE_BUILTIN
| NODE_DISABLED
| NODE_USED
);
2806 /* Save parameter NODE (spelling SPELLING) to the parameter list of
2807 macro MACRO. Returns zero on success, nonzero if the parameter is
2810 _cpp_save_parameter (cpp_reader
*pfile
, cpp_macro
*macro
, cpp_hashnode
*node
,
2811 cpp_hashnode
*spelling
)
2814 /* Constraint 6.10.3.6 - duplicate parameter names. */
2815 if (node
->flags
& NODE_MACRO_ARG
)
2817 cpp_error (pfile
, CPP_DL_ERROR
, "duplicate macro parameter \"%s\"",
2822 if (BUFF_ROOM (pfile
->a_buff
)
2823 < (macro
->paramc
+ 1) * sizeof (cpp_hashnode
*))
2824 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (cpp_hashnode
*));
2826 ((cpp_hashnode
**) BUFF_FRONT (pfile
->a_buff
))[macro
->paramc
++] = spelling
;
2827 node
->flags
|= NODE_MACRO_ARG
;
2828 len
= macro
->paramc
* sizeof (struct macro_arg_saved_data
);
2829 if (len
> pfile
->macro_buffer_len
)
2831 pfile
->macro_buffer
= XRESIZEVEC (unsigned char, pfile
->macro_buffer
,
2833 pfile
->macro_buffer_len
= len
;
2835 struct macro_arg_saved_data save
;
2836 save
.value
= node
->value
;
2837 save
.canonical_node
= node
;
2838 ((struct macro_arg_saved_data
*) pfile
->macro_buffer
)[macro
->paramc
- 1]
2841 node
->value
.arg_index
= macro
->paramc
;
2845 /* Check the syntax of the parameters in a MACRO definition. Returns
2846 false if an error occurs. */
2848 parse_params (cpp_reader
*pfile
, cpp_macro
*macro
)
2850 unsigned int prev_ident
= 0;
2854 const cpp_token
*token
= _cpp_lex_token (pfile
);
2856 switch (token
->type
)
2859 /* Allow/ignore comments in parameter lists if we are
2860 preserving comments in macro expansions. */
2861 if (token
->type
== CPP_COMMENT
2862 && ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
))
2865 cpp_error (pfile
, CPP_DL_ERROR
,
2866 "\"%s\" may not appear in macro parameter list",
2867 cpp_token_as_text (pfile
, token
));
2873 cpp_error (pfile
, CPP_DL_ERROR
,
2874 "macro parameters must be comma-separated");
2879 if (_cpp_save_parameter (pfile
, macro
, token
->val
.node
.node
,
2880 token
->val
.node
.spelling
))
2884 case CPP_CLOSE_PAREN
:
2885 if (prev_ident
|| macro
->paramc
== 0)
2888 /* Fall through to pick up the error. */
2892 cpp_error (pfile
, CPP_DL_ERROR
, "parameter name missing");
2899 macro
->variadic
= 1;
2902 _cpp_save_parameter (pfile
, macro
,
2903 pfile
->spec_nodes
.n__VA_ARGS__
,
2904 pfile
->spec_nodes
.n__VA_ARGS__
);
2905 pfile
->state
.va_args_ok
= 1;
2906 if (! CPP_OPTION (pfile
, c99
)
2907 && CPP_OPTION (pfile
, cpp_pedantic
)
2908 && CPP_OPTION (pfile
, warn_variadic_macros
))
2910 if (CPP_OPTION (pfile
, cplusplus
))
2912 (pfile
, CPP_W_VARIADIC_MACROS
,
2913 "anonymous variadic macros were introduced in C++11");
2916 (pfile
, CPP_W_VARIADIC_MACROS
,
2917 "anonymous variadic macros were introduced in C99");
2919 else if (CPP_OPTION (pfile
, cpp_warn_c90_c99_compat
) > 0
2920 && ! CPP_OPTION (pfile
, cplusplus
))
2921 cpp_error (pfile
, CPP_DL_WARNING
,
2922 "anonymous variadic macros were introduced in C99");
2924 else if (CPP_OPTION (pfile
, cpp_pedantic
)
2925 && CPP_OPTION (pfile
, warn_variadic_macros
))
2927 if (CPP_OPTION (pfile
, cplusplus
))
2928 cpp_pedwarning (pfile
, CPP_W_VARIADIC_MACROS
,
2929 "ISO C++ does not permit named variadic macros");
2931 cpp_pedwarning (pfile
, CPP_W_VARIADIC_MACROS
,
2932 "ISO C does not permit named variadic macros");
2935 /* We're at the end, and just expect a closing parenthesis. */
2936 token
= _cpp_lex_token (pfile
);
2937 if (token
->type
== CPP_CLOSE_PAREN
)
2942 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' in macro parameter list");
2948 /* Allocate room for a token from a macro's replacement list. */
2950 alloc_expansion_token (cpp_reader
*pfile
, cpp_macro
*macro
)
2952 if (BUFF_ROOM (pfile
->a_buff
) < (macro
->count
+ 1) * sizeof (cpp_token
))
2953 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (cpp_token
));
2955 return &((cpp_token
*) BUFF_FRONT (pfile
->a_buff
))[macro
->count
++];
2958 /* Lex a token from the expansion of MACRO, but mark parameters as we
2959 find them and warn of traditional stringification. */
2961 lex_expansion_token (cpp_reader
*pfile
, cpp_macro
*macro
)
2963 cpp_token
*token
, *saved_cur_token
;
2965 saved_cur_token
= pfile
->cur_token
;
2966 pfile
->cur_token
= alloc_expansion_token (pfile
, macro
);
2967 token
= _cpp_lex_direct (pfile
);
2968 pfile
->cur_token
= saved_cur_token
;
2970 /* Is this a parameter? */
2971 if (token
->type
== CPP_NAME
2972 && (token
->val
.node
.node
->flags
& NODE_MACRO_ARG
) != 0)
2974 cpp_hashnode
*spelling
= token
->val
.node
.spelling
;
2975 token
->type
= CPP_MACRO_ARG
;
2976 token
->val
.macro_arg
.arg_no
= token
->val
.node
.node
->value
.arg_index
;
2977 token
->val
.macro_arg
.spelling
= spelling
;
2979 else if (CPP_WTRADITIONAL (pfile
) && macro
->paramc
> 0
2980 && (token
->type
== CPP_STRING
|| token
->type
== CPP_CHAR
))
2981 check_trad_stringification (pfile
, macro
, &token
->val
.str
);
2987 create_iso_definition (cpp_reader
*pfile
, cpp_macro
*macro
)
2990 const cpp_token
*ctoken
;
2991 bool following_paste_op
= false;
2992 const char *paste_op_error_msg
=
2993 N_("'##' cannot appear at either end of a macro expansion");
2994 unsigned int num_extra_tokens
= 0;
2996 /* Get the first token of the expansion (or the '(' of a
2997 function-like macro). */
2998 ctoken
= _cpp_lex_token (pfile
);
3000 if (ctoken
->type
== CPP_OPEN_PAREN
&& !(ctoken
->flags
& PREV_WHITE
))
3002 bool ok
= parse_params (pfile
, macro
);
3003 macro
->params
= (cpp_hashnode
**) BUFF_FRONT (pfile
->a_buff
);
3007 /* Success. Commit or allocate the parameter array. */
3008 if (pfile
->hash_table
->alloc_subobject
)
3010 cpp_hashnode
**params
=
3011 (cpp_hashnode
**) pfile
->hash_table
->alloc_subobject
3012 (sizeof (cpp_hashnode
*) * macro
->paramc
);
3013 memcpy (params
, macro
->params
,
3014 sizeof (cpp_hashnode
*) * macro
->paramc
);
3015 macro
->params
= params
;
3018 BUFF_FRONT (pfile
->a_buff
) = (uchar
*) ¯o
->params
[macro
->paramc
];
3019 macro
->fun_like
= 1;
3021 else if (ctoken
->type
!= CPP_EOF
&& !(ctoken
->flags
& PREV_WHITE
))
3023 /* While ISO C99 requires whitespace before replacement text
3024 in a macro definition, ISO C90 with TC1 allows characters
3025 from the basic source character set there. */
3026 if (CPP_OPTION (pfile
, c99
))
3028 if (CPP_OPTION (pfile
, cplusplus
))
3029 cpp_error (pfile
, CPP_DL_PEDWARN
,
3030 "ISO C++11 requires whitespace after the macro name");
3032 cpp_error (pfile
, CPP_DL_PEDWARN
,
3033 "ISO C99 requires whitespace after the macro name");
3037 int warntype
= CPP_DL_WARNING
;
3038 switch (ctoken
->type
)
3042 case CPP_OBJC_STRING
:
3043 /* '@' is not in basic character set. */
3044 warntype
= CPP_DL_PEDWARN
;
3047 /* Basic character set sans letters, digits and _. */
3048 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
3049 ctoken
->val
.str
.text
[0]) == NULL
)
3050 warntype
= CPP_DL_PEDWARN
;
3053 /* All other tokens start with a character from basic
3057 cpp_error (pfile
, warntype
,
3058 "missing whitespace after the macro name");
3062 if (macro
->fun_like
)
3063 token
= lex_expansion_token (pfile
, macro
);
3066 token
= alloc_expansion_token (pfile
, macro
);
3072 /* Check the stringifying # constraint 6.10.3.2.1 of
3073 function-like macros when lexing the subsequent token. */
3074 if (macro
->count
> 1 && token
[-1].type
== CPP_HASH
&& macro
->fun_like
)
3076 if (token
->type
== CPP_MACRO_ARG
)
3078 if (token
->flags
& PREV_WHITE
)
3079 token
->flags
|= SP_PREV_WHITE
;
3080 if (token
[-1].flags
& DIGRAPH
)
3081 token
->flags
|= SP_DIGRAPH
;
3082 token
->flags
&= ~PREV_WHITE
;
3083 token
->flags
|= STRINGIFY_ARG
;
3084 token
->flags
|= token
[-1].flags
& PREV_WHITE
;
3085 token
[-1] = token
[0];
3088 /* Let assembler get away with murder. */
3089 else if (CPP_OPTION (pfile
, lang
) != CLK_ASM
)
3091 cpp_error (pfile
, CPP_DL_ERROR
,
3092 "'#' is not followed by a macro parameter");
3097 if (token
->type
== CPP_EOF
)
3099 /* Paste operator constraint 6.10.3.3.1:
3100 Token-paste ##, can appear in both object-like and
3101 function-like macros, but not at the end. */
3102 if (following_paste_op
)
3104 cpp_error (pfile
, CPP_DL_ERROR
, paste_op_error_msg
);
3110 /* Paste operator constraint 6.10.3.3.1. */
3111 if (token
->type
== CPP_PASTE
)
3113 /* Token-paste ##, can appear in both object-like and
3114 function-like macros, but not at the beginning. */
3115 if (macro
->count
== 1)
3117 cpp_error (pfile
, CPP_DL_ERROR
, paste_op_error_msg
);
3121 if (token
[-1].flags
& PASTE_LEFT
)
3123 macro
->extra_tokens
= 1;
3125 token
->val
.token_no
= macro
->count
- 1;
3130 token
[-1].flags
|= PASTE_LEFT
;
3131 if (token
->flags
& DIGRAPH
)
3132 token
[-1].flags
|= SP_DIGRAPH
;
3133 if (token
->flags
& PREV_WHITE
)
3134 token
[-1].flags
|= SP_PREV_WHITE
;
3138 following_paste_op
= (token
->type
== CPP_PASTE
);
3139 token
= lex_expansion_token (pfile
, macro
);
3142 macro
->exp
.tokens
= (cpp_token
*) BUFF_FRONT (pfile
->a_buff
);
3143 macro
->traditional
= 0;
3145 /* Don't count the CPP_EOF. */
3148 /* Clear whitespace on first token for warn_of_redefinition(). */
3150 macro
->exp
.tokens
[0].flags
&= ~PREV_WHITE
;
3152 /* Commit or allocate the memory. */
3153 if (pfile
->hash_table
->alloc_subobject
)
3156 (cpp_token
*) pfile
->hash_table
->alloc_subobject (sizeof (cpp_token
)
3158 if (num_extra_tokens
)
3160 /* Place second and subsequent ## or %:%: tokens in
3161 sequences of consecutive such tokens at the end of the
3162 list to preserve information about where they appear, how
3163 they are spelt and whether they are preceded by
3164 whitespace without otherwise interfering with macro
3166 cpp_token
*normal_dest
= tokns
;
3167 cpp_token
*extra_dest
= tokns
+ macro
->count
- num_extra_tokens
;
3169 for (i
= 0; i
< macro
->count
; i
++)
3171 if (macro
->exp
.tokens
[i
].type
== CPP_PASTE
)
3172 *extra_dest
++ = macro
->exp
.tokens
[i
];
3174 *normal_dest
++ = macro
->exp
.tokens
[i
];
3178 memcpy (tokns
, macro
->exp
.tokens
, sizeof (cpp_token
) * macro
->count
);
3179 macro
->exp
.tokens
= tokns
;
3182 BUFF_FRONT (pfile
->a_buff
) = (uchar
*) ¯o
->exp
.tokens
[macro
->count
];
3187 /* Parse a macro and save its expansion. Returns nonzero on success. */
3189 _cpp_create_definition (cpp_reader
*pfile
, cpp_hashnode
*node
)
3195 if (pfile
->hash_table
->alloc_subobject
)
3196 macro
= (cpp_macro
*) pfile
->hash_table
->alloc_subobject
3197 (sizeof (cpp_macro
));
3199 macro
= (cpp_macro
*) _cpp_aligned_alloc (pfile
, sizeof (cpp_macro
));
3200 macro
->line
= pfile
->directive_line
;
3203 macro
->variadic
= 0;
3204 macro
->used
= !CPP_OPTION (pfile
, warn_unused_macros
);
3206 macro
->fun_like
= 0;
3207 macro
->extra_tokens
= 0;
3208 /* To suppress some diagnostics. */
3209 macro
->syshdr
= pfile
->buffer
&& pfile
->buffer
->sysp
!= 0;
3211 if (CPP_OPTION (pfile
, traditional
))
3212 ok
= _cpp_create_trad_definition (pfile
, macro
);
3215 ok
= create_iso_definition (pfile
, macro
);
3217 /* We set the type for SEEN_EOL() in directives.c.
3219 Longer term we should lex the whole line before coming here,
3220 and just copy the expansion. */
3222 /* Stop the lexer accepting __VA_ARGS__. */
3223 pfile
->state
.va_args_ok
= 0;
3226 /* Clear the fast argument lookup indices. */
3227 for (i
= macro
->paramc
; i
-- > 0; )
3229 struct macro_arg_saved_data
*save
=
3230 &((struct macro_arg_saved_data
*) pfile
->macro_buffer
)[i
];
3231 struct cpp_hashnode
*node
= save
->canonical_node
;
3232 node
->flags
&= ~ NODE_MACRO_ARG
;
3233 node
->value
= save
->value
;
3239 if (node
->type
== NT_MACRO
)
3241 if (CPP_OPTION (pfile
, warn_unused_macros
))
3242 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
3244 if (warn_of_redefinition (pfile
, node
, macro
))
3246 const int reason
= ((node
->flags
& NODE_BUILTIN
)
3247 && !(node
->flags
& NODE_WARN
))
3248 ? CPP_W_BUILTIN_MACRO_REDEFINED
: CPP_W_NONE
;
3251 cpp_pedwarning_with_line (pfile
, reason
,
3252 pfile
->directive_line
, 0,
3253 "\"%s\" redefined", NODE_NAME (node
));
3255 if (warned
&& node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
))
3256 cpp_error_with_line (pfile
, CPP_DL_NOTE
,
3257 node
->value
.macro
->line
, 0,
3258 "this is the location of the previous definition");
3262 if (node
->type
!= NT_VOID
)
3263 _cpp_free_definition (node
);
3265 /* Enter definition in hash table. */
3266 node
->type
= NT_MACRO
;
3267 node
->value
.macro
= macro
;
3268 if (! ustrncmp (NODE_NAME (node
), DSC ("__STDC_"))
3269 && ustrcmp (NODE_NAME (node
), (const uchar
*) "__STDC_FORMAT_MACROS")
3270 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
3271 in the C standard, as something that one must use in C++.
3272 However DR#593 and C++11 indicate that they play no role in C++.
3273 We special-case them anyway. */
3274 && ustrcmp (NODE_NAME (node
), (const uchar
*) "__STDC_LIMIT_MACROS")
3275 && ustrcmp (NODE_NAME (node
), (const uchar
*) "__STDC_CONSTANT_MACROS"))
3276 node
->flags
|= NODE_WARN
;
3278 /* If user defines one of the conditional macros, remove the
3280 node
->flags
&= ~NODE_CONDITIONAL
;
3285 /* Warn if a token in STRING matches one of a function-like MACRO's
3288 check_trad_stringification (cpp_reader
*pfile
, const cpp_macro
*macro
,
3289 const cpp_string
*string
)
3291 unsigned int i
, len
;
3292 const uchar
*p
, *q
, *limit
;
3294 /* Loop over the string. */
3295 limit
= string
->text
+ string
->len
- 1;
3296 for (p
= string
->text
+ 1; p
< limit
; p
= q
)
3298 /* Find the start of an identifier. */
3299 while (p
< limit
&& !is_idstart (*p
))
3302 /* Find the end of the identifier. */
3304 while (q
< limit
&& is_idchar (*q
))
3309 /* Loop over the function macro arguments to see if the
3310 identifier inside the string matches one of them. */
3311 for (i
= 0; i
< macro
->paramc
; i
++)
3313 const cpp_hashnode
*node
= macro
->params
[i
];
3315 if (NODE_LEN (node
) == len
3316 && !memcmp (p
, NODE_NAME (node
), len
))
3318 cpp_error (pfile
, CPP_DL_WARNING
,
3319 "macro argument \"%s\" would be stringified in traditional C",
3327 /* Returns true of NODE is a function-like macro. */
3329 cpp_fun_like_macro_p (cpp_hashnode
*node
)
3331 return (node
->type
== NT_MACRO
3332 && (node
->flags
& (NODE_BUILTIN
| NODE_MACRO_ARG
)) == 0
3333 && node
->value
.macro
->fun_like
);
3336 /* Returns the name, arguments and expansion of a macro, in a format
3337 suitable to be read back in again, and therefore also for DWARF 2
3338 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
3339 Caller is expected to generate the "#define" bit if needed. The
3340 returned text is temporary, and automatically freed later. */
3341 const unsigned char *
3342 cpp_macro_definition (cpp_reader
*pfile
, cpp_hashnode
*node
)
3344 unsigned int i
, len
;
3345 const cpp_macro
*macro
;
3346 unsigned char *buffer
;
3348 if (node
->type
!= NT_MACRO
|| (node
->flags
& NODE_BUILTIN
))
3350 if (node
->type
!= NT_MACRO
3351 || !pfile
->cb
.user_builtin_macro
3352 || !pfile
->cb
.user_builtin_macro (pfile
, node
))
3354 cpp_error (pfile
, CPP_DL_ICE
,
3355 "invalid hash type %d in cpp_macro_definition",
3361 macro
= node
->value
.macro
;
3362 /* Calculate length. */
3363 len
= NODE_LEN (node
) * 10 + 2; /* ' ' and NUL. */
3364 if (macro
->fun_like
)
3366 len
+= 4; /* "()" plus possible final ".." of named
3367 varargs (we have + 1 below). */
3368 for (i
= 0; i
< macro
->paramc
; i
++)
3369 len
+= NODE_LEN (macro
->params
[i
]) + 1; /* "," */
3372 /* This should match below where we fill in the buffer. */
3373 if (CPP_OPTION (pfile
, traditional
))
3374 len
+= _cpp_replacement_text_len (macro
);
3377 unsigned int count
= macro_real_token_count (macro
);
3378 for (i
= 0; i
< count
; i
++)
3380 cpp_token
*token
= ¯o
->exp
.tokens
[i
];
3382 if (token
->type
== CPP_MACRO_ARG
)
3383 len
+= NODE_LEN (token
->val
.macro_arg
.spelling
);
3385 len
+= cpp_token_len (token
);
3387 if (token
->flags
& STRINGIFY_ARG
)
3389 if (token
->flags
& PASTE_LEFT
)
3390 len
+= 3; /* " ##" */
3391 if (token
->flags
& PREV_WHITE
)
3396 if (len
> pfile
->macro_buffer_len
)
3398 pfile
->macro_buffer
= XRESIZEVEC (unsigned char,
3399 pfile
->macro_buffer
, len
);
3400 pfile
->macro_buffer_len
= len
;
3403 /* Fill in the buffer. Start with the macro name. */
3404 buffer
= pfile
->macro_buffer
;
3405 buffer
= _cpp_spell_ident_ucns (buffer
, node
);
3407 /* Parameter names. */
3408 if (macro
->fun_like
)
3411 for (i
= 0; i
< macro
->paramc
; i
++)
3413 cpp_hashnode
*param
= macro
->params
[i
];
3415 if (param
!= pfile
->spec_nodes
.n__VA_ARGS__
)
3417 memcpy (buffer
, NODE_NAME (param
), NODE_LEN (param
));
3418 buffer
+= NODE_LEN (param
);
3421 if (i
+ 1 < macro
->paramc
)
3422 /* Don't emit a space after the comma here; we're trying
3423 to emit a Dwarf-friendly definition, and the Dwarf spec
3424 forbids spaces in the argument list. */
3426 else if (macro
->variadic
)
3427 *buffer
++ = '.', *buffer
++ = '.', *buffer
++ = '.';
3432 /* The Dwarf spec requires a space after the macro name, even if the
3433 definition is the empty string. */
3436 if (CPP_OPTION (pfile
, traditional
))
3437 buffer
= _cpp_copy_replacement_text (macro
, buffer
);
3438 else if (macro
->count
)
3439 /* Expansion tokens. */
3441 unsigned int count
= macro_real_token_count (macro
);
3442 for (i
= 0; i
< count
; i
++)
3444 cpp_token
*token
= ¯o
->exp
.tokens
[i
];
3446 if (token
->flags
& PREV_WHITE
)
3448 if (token
->flags
& STRINGIFY_ARG
)
3451 if (token
->type
== CPP_MACRO_ARG
)
3454 NODE_NAME (token
->val
.macro_arg
.spelling
),
3455 NODE_LEN (token
->val
.macro_arg
.spelling
));
3456 buffer
+= NODE_LEN (token
->val
.macro_arg
.spelling
);
3459 buffer
= cpp_spell_token (pfile
, token
, buffer
, true);
3461 if (token
->flags
& PASTE_LEFT
)
3466 /* Next has PREV_WHITE; see _cpp_create_definition. */
3472 return pfile
->macro_buffer
;