1 /* Part of CPP library. (Macro and #define handling.)
2 Copyright (C) 1986-2018 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 literal, 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 static const char *vaopt_paste_error
=
93 N_("'##' cannot appear at either end of __VA_OPT__");
95 /* A class for tracking __VA_OPT__ state while iterating over a
96 sequence of tokens. This is used during both macro definition and
102 /* Initialize the state tracker. ANY_ARGS is true if variable
103 arguments were provided to the macro invocation. */
104 vaopt_state (cpp_reader
*pfile
, bool is_variadic
, bool any_args
)
106 m_allowed (any_args
),
107 m_variadic (is_variadic
),
108 m_last_was_paste (false),
110 m_paste_location (0),
124 /* Given a token, update the state of this tracker and return a
125 boolean indicating whether the token should be be included in the
127 update_type
update (const cpp_token
*token
)
129 /* If the macro isn't variadic, just don't bother. */
133 if (token
->type
== CPP_NAME
134 && token
->val
.node
.node
== m_pfile
->spec_nodes
.n__VA_OPT__
)
138 cpp_error_at (m_pfile
, CPP_DL_ERROR
, token
->src_loc
,
139 "__VA_OPT__ may not appear in a __VA_OPT__");
143 m_location
= token
->src_loc
;
146 else if (m_state
== 1)
148 if (token
->type
!= CPP_OPEN_PAREN
)
150 cpp_error_at (m_pfile
, CPP_DL_ERROR
, m_location
,
151 "__VA_OPT__ must be followed by an "
158 else if (m_state
>= 2)
160 if (m_state
== 2 && token
->type
== CPP_PASTE
)
162 cpp_error_at (m_pfile
, CPP_DL_ERROR
, token
->src_loc
,
166 /* Advance states before further considering this token, in
167 case we see a close paren immediately after the open
172 bool was_paste
= m_last_was_paste
;
173 m_last_was_paste
= false;
174 if (token
->type
== CPP_PASTE
)
176 m_last_was_paste
= true;
177 m_paste_location
= token
->src_loc
;
179 else if (token
->type
== CPP_OPEN_PAREN
)
181 else if (token
->type
== CPP_CLOSE_PAREN
)
186 /* Saw the final paren. */
191 cpp_error_at (m_pfile
, CPP_DL_ERROR
, token
->src_loc
,
199 return m_allowed
? INCLUDE
: DROP
;
202 /* Nothing to do with __VA_OPT__. */
206 /* Ensure that any __VA_OPT__ was completed. If ok, return true.
207 Otherwise, issue an error and return false. */
210 if (m_variadic
&& m_state
!= 0)
211 cpp_error_at (m_pfile
, CPP_DL_ERROR
, m_location
,
212 "unterminated __VA_OPT__");
218 /* The cpp_reader. */
221 /* True if there were varargs. */
223 /* True if the macro is variadic. */
225 /* If true, the previous token was ##. This is used to detect when
226 a paste occurs at the end of the sequence. */
227 bool m_last_was_paste
;
229 /* The state variable:
231 1 means __VA_OPT__ seen, looking for "("
232 2 means "(" seen (so the next token can't be "##")
233 >= 3 means looking for ")", the number encodes the paren depth. */
236 /* The location of the paste token. */
237 source_location m_paste_location
;
239 /* Location of the __VA_OPT__ token. */
240 source_location m_location
;
243 /* Macro expansion. */
245 static int enter_macro_context (cpp_reader
*, cpp_hashnode
*,
246 const cpp_token
*, source_location
);
247 static int builtin_macro (cpp_reader
*, cpp_hashnode
*,
248 source_location
, source_location
);
249 static void push_ptoken_context (cpp_reader
*, cpp_hashnode
*, _cpp_buff
*,
250 const cpp_token
**, unsigned int);
251 static void push_extended_tokens_context (cpp_reader
*, cpp_hashnode
*,
252 _cpp_buff
*, source_location
*,
253 const cpp_token
**, unsigned int);
254 static _cpp_buff
*collect_args (cpp_reader
*, const cpp_hashnode
*,
255 _cpp_buff
**, unsigned *);
256 static cpp_context
*next_context (cpp_reader
*);
257 static const cpp_token
*padding_token (cpp_reader
*, const cpp_token
*);
258 static void expand_arg (cpp_reader
*, macro_arg
*);
259 static const cpp_token
*new_string_token (cpp_reader
*, uchar
*, unsigned int);
260 static const cpp_token
*stringify_arg (cpp_reader
*, macro_arg
*);
261 static void paste_all_tokens (cpp_reader
*, const cpp_token
*);
262 static bool paste_tokens (cpp_reader
*, source_location
,
263 const cpp_token
**, const cpp_token
*);
264 static void alloc_expanded_arg_mem (cpp_reader
*, macro_arg
*, size_t);
265 static void ensure_expanded_arg_room (cpp_reader
*, macro_arg
*, size_t, size_t *);
266 static void delete_macro_args (_cpp_buff
*, unsigned num_args
);
267 static void set_arg_token (macro_arg
*, const cpp_token
*,
268 source_location
, size_t,
269 enum macro_arg_token_kind
,
271 static const source_location
*get_arg_token_location (const macro_arg
*,
272 enum macro_arg_token_kind
);
273 static const cpp_token
**arg_token_ptr_at (const macro_arg
*,
275 enum macro_arg_token_kind
,
276 source_location
**virt_location
);
278 static void macro_arg_token_iter_init (macro_arg_token_iter
*, bool,
279 enum macro_arg_token_kind
,
282 static const cpp_token
*macro_arg_token_iter_get_token
283 (const macro_arg_token_iter
*it
);
284 static source_location macro_arg_token_iter_get_location
285 (const macro_arg_token_iter
*);
286 static void macro_arg_token_iter_forward (macro_arg_token_iter
*);
287 static _cpp_buff
*tokens_buff_new (cpp_reader
*, size_t,
289 static size_t tokens_buff_count (_cpp_buff
*);
290 static const cpp_token
**tokens_buff_last_token_ptr (_cpp_buff
*);
291 static inline const cpp_token
**tokens_buff_put_token_to (const cpp_token
**,
296 const line_map_macro
*,
299 static const cpp_token
**tokens_buff_add_token (_cpp_buff
*,
304 const line_map_macro
*,
306 static inline void tokens_buff_remove_last_token (_cpp_buff
*);
307 static void replace_args (cpp_reader
*, cpp_hashnode
*, cpp_macro
*,
308 macro_arg
*, source_location
);
309 static _cpp_buff
*funlike_invocation_p (cpp_reader
*, cpp_hashnode
*,
310 _cpp_buff
**, unsigned *);
311 static cpp_macro
*create_iso_definition (cpp_reader
*);
313 /* #define directive parsing and handling. */
315 static cpp_macro
*lex_expansion_token (cpp_reader
*, cpp_macro
*);
316 static bool warn_of_redefinition (cpp_reader
*, cpp_hashnode
*,
318 static bool parse_params (cpp_reader
*, unsigned *, bool *);
319 static void check_trad_stringification (cpp_reader
*, const cpp_macro
*,
321 static bool reached_end_of_context (cpp_context
*);
322 static void consume_next_token_from_context (cpp_reader
*pfile
,
325 static const cpp_token
* cpp_get_token_1 (cpp_reader
*, source_location
*);
327 static cpp_hashnode
* macro_of_context (cpp_context
*context
);
329 static bool in_macro_expansion_p (cpp_reader
*pfile
);
331 /* Statistical counter tracking the number of macros that got
333 unsigned num_expanded_macros_counter
= 0;
334 /* Statistical counter tracking the total number tokens resulting
335 from macro expansion. */
336 unsigned num_macro_tokens_counter
= 0;
338 /* Emits a warning if NODE is a macro defined in the main file that
339 has not been used. */
341 _cpp_warn_if_unused_macro (cpp_reader
*pfile
, cpp_hashnode
*node
,
342 void *v ATTRIBUTE_UNUSED
)
344 if (cpp_user_macro_p (node
))
346 cpp_macro
*macro
= node
->value
.macro
;
349 && MAIN_FILE_P (linemap_check_ordinary
350 (linemap_lookup (pfile
->line_table
,
352 cpp_warning_with_line (pfile
, CPP_W_UNUSED_MACROS
, macro
->line
, 0,
353 "macro \"%s\" is not used", NODE_NAME (node
));
359 /* Allocates and returns a CPP_STRING token, containing TEXT of length
360 LEN, after null-terminating it. TEXT must be in permanent storage. */
361 static const cpp_token
*
362 new_string_token (cpp_reader
*pfile
, unsigned char *text
, unsigned int len
)
364 cpp_token
*token
= _cpp_temp_token (pfile
);
367 token
->type
= CPP_STRING
;
368 token
->val
.str
.len
= len
;
369 token
->val
.str
.text
= text
;
374 static const char * const monthnames
[] =
376 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
377 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
380 /* Helper function for builtin_macro. Returns the text generated by
383 _cpp_builtin_macro_text (cpp_reader
*pfile
, cpp_hashnode
*node
,
386 const uchar
*result
= NULL
;
387 linenum_type number
= 1;
389 switch (node
->value
.builtin
)
392 cpp_error (pfile
, CPP_DL_ICE
, "invalid built-in macro \"%s\"",
398 if (CPP_OPTION (pfile
, warn_date_time
))
399 cpp_warning (pfile
, CPP_W_DATE_TIME
, "macro \"%s\" might prevent "
400 "reproducible builds", NODE_NAME (node
));
402 cpp_buffer
*pbuffer
= cpp_get_buffer (pfile
);
403 if (pbuffer
->timestamp
== NULL
)
405 /* Initialize timestamp value of the assotiated file. */
406 struct _cpp_file
*file
= cpp_get_file (pbuffer
);
409 /* Generate __TIMESTAMP__ string, that represents
410 the date and time of the last modification
411 of the current source file. The string constant
412 looks like "Sun Sep 16 01:03:52 1973". */
413 struct tm
*tb
= NULL
;
414 struct stat
*st
= _cpp_get_file_stat (file
);
416 tb
= localtime (&st
->st_mtime
);
419 char *str
= asctime (tb
);
420 size_t len
= strlen (str
);
421 unsigned char *buf
= _cpp_unaligned_alloc (pfile
, len
+ 2);
423 strcpy ((char *) buf
+ 1, str
);
425 pbuffer
->timestamp
= buf
;
429 cpp_errno (pfile
, CPP_DL_WARNING
,
430 "could not determine file timestamp");
431 pbuffer
->timestamp
= UC
"\"??? ??? ?? ??:??:?? ????\"";
435 result
= pbuffer
->timestamp
;
445 if (node
->value
.builtin
== BT_FILE
)
446 name
= linemap_get_expansion_filename (pfile
->line_table
,
447 pfile
->line_table
->highest_line
);
450 name
= _cpp_get_file_name (pfile
->main_file
);
454 if (pfile
->cb
.remap_filename
)
455 name
= pfile
->cb
.remap_filename (name
);
457 buf
= _cpp_unaligned_alloc (pfile
, len
* 2 + 3);
460 buf
= cpp_quote_string (buf
+ 1, (const unsigned char *) name
, len
);
466 case BT_INCLUDE_LEVEL
:
467 /* The line map depth counts the primary source as level 1, but
468 historically __INCLUDE_DEPTH__ has called the primary source
470 number
= pfile
->line_table
->depth
- 1;
474 /* If __LINE__ is embedded in a macro, it must expand to the
475 line of the macro's invocation, not its definition.
476 Otherwise things like assert() will not work properly.
477 See WG14 N1911, WG21 N4220 sec 6.5, and PR 61861. */
478 if (CPP_OPTION (pfile
, traditional
))
479 loc
= pfile
->line_table
->highest_line
;
481 loc
= linemap_resolve_location (pfile
->line_table
, loc
,
482 LRK_MACRO_EXPANSION_POINT
, NULL
);
483 number
= linemap_get_expansion_line (pfile
->line_table
, loc
);
486 /* __STDC__ has the value 1 under normal circumstances.
487 However, if (a) we are in a system header, (b) the option
488 stdc_0_in_system_headers is true (set by target config), and
489 (c) we are not in strictly conforming mode, then it has the
490 value 0. (b) and (c) are already checked in cpp_init_builtins. */
492 if (cpp_in_system_header (pfile
))
500 if (CPP_OPTION (pfile
, warn_date_time
))
501 cpp_warning (pfile
, CPP_W_DATE_TIME
, "macro \"%s\" might prevent "
502 "reproducible builds", NODE_NAME (node
));
503 if (pfile
->date
== NULL
)
505 /* Allocate __DATE__ and __TIME__ strings from permanent
506 storage. We only do this once, and don't generate them
507 at init time, because time() and localtime() are very
508 slow on some systems. */
510 struct tm
*tb
= NULL
;
512 /* Set a reproducible timestamp for __DATE__ and __TIME__ macro
513 if SOURCE_DATE_EPOCH is defined. */
514 if (pfile
->source_date_epoch
== (time_t) -2
515 && pfile
->cb
.get_source_date_epoch
!= NULL
)
516 pfile
->source_date_epoch
= pfile
->cb
.get_source_date_epoch (pfile
);
518 if (pfile
->source_date_epoch
>= (time_t) 0)
519 tb
= gmtime (&pfile
->source_date_epoch
);
522 /* (time_t) -1 is a legitimate value for "number of seconds
523 since the Epoch", so we have to do a little dance to
524 distinguish that from a genuine error. */
527 if (tt
!= (time_t)-1 || errno
== 0)
528 tb
= localtime (&tt
);
533 pfile
->date
= _cpp_unaligned_alloc (pfile
,
534 sizeof ("\"Oct 11 1347\""));
535 sprintf ((char *) pfile
->date
, "\"%s %2d %4d\"",
536 monthnames
[tb
->tm_mon
], tb
->tm_mday
,
539 pfile
->time
= _cpp_unaligned_alloc (pfile
,
540 sizeof ("\"12:34:56\""));
541 sprintf ((char *) pfile
->time
, "\"%02d:%02d:%02d\"",
542 tb
->tm_hour
, tb
->tm_min
, tb
->tm_sec
);
546 cpp_errno (pfile
, CPP_DL_WARNING
,
547 "could not determine date and time");
549 pfile
->date
= UC
"\"??? ?? ????\"";
550 pfile
->time
= UC
"\"??:??:??\"";
554 if (node
->value
.builtin
== BT_DATE
)
555 result
= pfile
->date
;
557 result
= pfile
->time
;
561 if (CPP_OPTION (pfile
, directives_only
) && pfile
->state
.in_directive
)
562 cpp_error (pfile
, CPP_DL_ERROR
,
563 "__COUNTER__ expanded inside directive with -fdirectives-only");
564 number
= pfile
->counter
++;
567 case BT_HAS_ATTRIBUTE
:
568 number
= pfile
->cb
.has_attribute (pfile
);
574 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
575 result
= _cpp_unaligned_alloc (pfile
, 21);
576 sprintf ((char *) result
, "%u", number
);
582 /* Convert builtin macros like __FILE__ to a token and push it on the
583 context stack. Also handles _Pragma, for which a new token may not
584 be created. Returns 1 if it generates a new token context, 0 to
585 return the token to the caller. LOC is the location of the expansion
586 point of the macro. */
588 builtin_macro (cpp_reader
*pfile
, cpp_hashnode
*node
,
589 source_location loc
, source_location expand_loc
)
595 if (node
->value
.builtin
== BT_PRAGMA
)
597 /* Don't interpret _Pragma within directives. The standard is
598 not clear on this, but to me this makes most sense. */
599 if (pfile
->state
.in_directive
)
602 return _cpp_do__Pragma (pfile
, loc
);
605 buf
= _cpp_builtin_macro_text (pfile
, node
, expand_loc
);
607 nbuf
= (char *) alloca (len
+ 1);
608 memcpy (nbuf
, buf
, len
);
611 cpp_push_buffer (pfile
, (uchar
*) nbuf
, len
, /* from_stage3 */ true);
612 _cpp_clean_line (pfile
);
614 /* Set pfile->cur_token as required by _cpp_lex_direct. */
615 pfile
->cur_token
= _cpp_temp_token (pfile
);
616 cpp_token
*token
= _cpp_lex_direct (pfile
);
617 /* We should point to the expansion point of the builtin macro. */
618 token
->src_loc
= loc
;
619 if (pfile
->context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
621 /* We are tracking tokens resulting from macro expansion.
622 Create a macro line map and generate a virtual location for
623 the token resulting from the expansion of the built-in
625 source_location
*virt_locs
= NULL
;
626 _cpp_buff
*token_buf
= tokens_buff_new (pfile
, 1, &virt_locs
);
627 const line_map_macro
* map
=
628 linemap_enter_macro (pfile
->line_table
, node
, loc
, 1);
629 tokens_buff_add_token (token_buf
, virt_locs
, token
,
630 pfile
->line_table
->builtin_location
,
631 pfile
->line_table
->builtin_location
,
632 map
, /*macro_token_index=*/0);
633 push_extended_tokens_context (pfile
, node
, token_buf
, virt_locs
,
634 (const cpp_token
**)token_buf
->base
,
638 _cpp_push_token_context (pfile
, NULL
, token
, 1);
639 if (pfile
->buffer
->cur
!= pfile
->buffer
->rlimit
)
640 cpp_error (pfile
, CPP_DL_ICE
, "invalid built-in macro \"%s\"",
642 _cpp_pop_buffer (pfile
);
647 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
648 backslashes and double quotes. DEST must be of sufficient size.
649 Returns a pointer to the end of the string. */
651 cpp_quote_string (uchar
*dest
, const uchar
*src
, unsigned int len
)
660 /* Naked LF can appear in raw string literals */
677 /* Convert a token sequence ARG to a single string token according to
678 the rules of the ISO C #-operator. */
679 static const cpp_token
*
680 stringify_arg (cpp_reader
*pfile
, macro_arg
*arg
)
683 unsigned int i
, escape_it
, backslash_count
= 0;
684 const cpp_token
*source
= NULL
;
687 if (BUFF_ROOM (pfile
->u_buff
) < 3)
688 _cpp_extend_buff (pfile
, &pfile
->u_buff
, 3);
689 dest
= BUFF_FRONT (pfile
->u_buff
);
692 /* Loop, reading in the argument's tokens. */
693 for (i
= 0; i
< arg
->count
; i
++)
695 const cpp_token
*token
= arg
->first
[i
];
697 if (token
->type
== CPP_PADDING
)
700 || (!(source
->flags
& PREV_WHITE
)
701 && token
->val
.source
== NULL
))
702 source
= token
->val
.source
;
706 escape_it
= (token
->type
== CPP_STRING
|| token
->type
== CPP_CHAR
707 || token
->type
== CPP_WSTRING
|| token
->type
== CPP_WCHAR
708 || token
->type
== CPP_STRING32
|| token
->type
== CPP_CHAR32
709 || token
->type
== CPP_STRING16
|| token
->type
== CPP_CHAR16
710 || token
->type
== CPP_UTF8STRING
|| token
->type
== CPP_UTF8CHAR
711 || cpp_userdef_string_p (token
->type
)
712 || cpp_userdef_char_p (token
->type
));
714 /* Room for each char being written in octal, initial space and
715 final quote and NUL. */
716 len
= cpp_token_len (token
);
721 if ((size_t) (BUFF_LIMIT (pfile
->u_buff
) - dest
) < len
)
723 size_t len_so_far
= dest
- BUFF_FRONT (pfile
->u_buff
);
724 _cpp_extend_buff (pfile
, &pfile
->u_buff
, len
);
725 dest
= BUFF_FRONT (pfile
->u_buff
) + len_so_far
;
728 /* Leading white space? */
729 if (dest
- 1 != BUFF_FRONT (pfile
->u_buff
))
733 if (source
->flags
& PREV_WHITE
)
740 _cpp_buff
*buff
= _cpp_get_buff (pfile
, len
);
741 unsigned char *buf
= BUFF_FRONT (buff
);
742 len
= cpp_spell_token (pfile
, token
, buf
, true) - buf
;
743 dest
= cpp_quote_string (dest
, buf
, len
);
744 _cpp_release_buff (pfile
, buff
);
747 dest
= cpp_spell_token (pfile
, token
, dest
, true);
749 if (token
->type
== CPP_OTHER
&& token
->val
.str
.text
[0] == '\\')
755 /* Ignore the final \ of invalid string literals. */
756 if (backslash_count
& 1)
758 cpp_error (pfile
, CPP_DL_WARNING
,
759 "invalid string literal, ignoring final '\\'");
763 /* Commit the memory, including NUL, and return the token. */
765 len
= dest
- BUFF_FRONT (pfile
->u_buff
);
766 BUFF_FRONT (pfile
->u_buff
) = dest
+ 1;
767 return new_string_token (pfile
, dest
- len
, len
);
770 /* Try to paste two tokens. On success, return nonzero. In any
771 case, PLHS is updated to point to the pasted token, which is
772 guaranteed to not have the PASTE_LEFT flag set. LOCATION is
773 the virtual location used for error reporting. */
775 paste_tokens (cpp_reader
*pfile
, source_location location
,
776 const cpp_token
**plhs
, const cpp_token
*rhs
)
778 unsigned char *buf
, *end
, *lhsend
;
782 len
= cpp_token_len (*plhs
) + cpp_token_len (rhs
) + 1;
783 buf
= (unsigned char *) alloca (len
);
784 end
= lhsend
= cpp_spell_token (pfile
, *plhs
, buf
, true);
786 /* Avoid comment headers, since they are still processed in stage 3.
787 It is simpler to insert a space here, rather than modifying the
788 lexer to ignore comments in some circumstances. Simply returning
789 false doesn't work, since we want to clear the PASTE_LEFT flag. */
790 if ((*plhs
)->type
== CPP_DIV
&& rhs
->type
!= CPP_EQ
)
792 /* In one obscure case we might see padding here. */
793 if (rhs
->type
!= CPP_PADDING
)
794 end
= cpp_spell_token (pfile
, rhs
, end
, true);
797 cpp_push_buffer (pfile
, buf
, end
- buf
, /* from_stage3 */ true);
798 _cpp_clean_line (pfile
);
800 /* Set pfile->cur_token as required by _cpp_lex_direct. */
801 pfile
->cur_token
= _cpp_temp_token (pfile
);
802 lhs
= _cpp_lex_direct (pfile
);
803 if (pfile
->buffer
->cur
!= pfile
->buffer
->rlimit
)
805 source_location saved_loc
= lhs
->src_loc
;
807 _cpp_pop_buffer (pfile
);
808 _cpp_backup_tokens (pfile
, 1);
811 /* We have to remove the PASTE_LEFT flag from the old lhs, but
812 we want to keep the new location. */
815 lhs
->src_loc
= saved_loc
;
816 lhs
->flags
&= ~PASTE_LEFT
;
818 /* Mandatory error for all apart from assembler. */
819 if (CPP_OPTION (pfile
, lang
) != CLK_ASM
)
820 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
821 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
822 buf
, cpp_token_as_text (pfile
, rhs
));
827 _cpp_pop_buffer (pfile
);
831 /* Handles an arbitrarily long sequence of ## operators, with initial
832 operand LHS. This implementation is left-associative,
833 non-recursive, and finishes a paste before handling succeeding
834 ones. If a paste fails, we back up to the RHS of the failing ##
835 operator before pushing the context containing the result of prior
836 successful pastes, with the effect that the RHS appears in the
837 output stream after the pasted LHS normally. */
839 paste_all_tokens (cpp_reader
*pfile
, const cpp_token
*lhs
)
841 const cpp_token
*rhs
= NULL
;
842 cpp_context
*context
= pfile
->context
;
843 source_location virt_loc
= 0;
845 /* We are expanding a macro and we must have been called on a token
846 that appears at the left hand side of a ## operator. */
847 if (macro_of_context (pfile
->context
) == NULL
848 || (!(lhs
->flags
& PASTE_LEFT
)))
851 if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
852 /* The caller must have called consume_next_token_from_context
853 right before calling us. That has incremented the pointer to
854 the current virtual location. So it now points to the location
855 of the token that comes right after *LHS. We want the
856 resulting pasted token to have the location of the current
858 virt_loc
= context
->c
.mc
->cur_virt_loc
[-1];
860 /* We are not tracking macro expansion. So the best virtual
861 location we can get here is the expansion point of the macro we
862 are currently expanding. */
863 virt_loc
= pfile
->invocation_location
;
867 /* Take the token directly from the current context. We can do
868 this, because we are in the replacement list of either an
869 object-like macro, or a function-like macro with arguments
870 inserted. In either case, the constraints to #define
871 guarantee we have at least one more token. */
872 if (context
->tokens_kind
== TOKENS_KIND_DIRECT
)
873 rhs
= FIRST (context
).token
++;
874 else if (context
->tokens_kind
== TOKENS_KIND_INDIRECT
)
875 rhs
= *FIRST (context
).ptoken
++;
876 else if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
878 /* So we are in presence of an extended token context, which
879 means that each token in this context has a virtual
880 location attached to it. So let's not forget to update
881 the pointer to the current virtual location of the
882 current token when we update the pointer to the current
885 rhs
= *FIRST (context
).ptoken
++;
886 /* context->c.mc must be non-null, as if we were not in a
887 macro context, context->tokens_kind could not be equal to
888 TOKENS_KIND_EXTENDED. */
889 context
->c
.mc
->cur_virt_loc
++;
892 if (rhs
->type
== CPP_PADDING
)
894 if (rhs
->flags
& PASTE_LEFT
)
897 if (!paste_tokens (pfile
, virt_loc
, &lhs
, rhs
))
900 while (rhs
->flags
& PASTE_LEFT
);
902 /* Put the resulting token in its own context. */
903 if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
905 source_location
*virt_locs
= NULL
;
906 _cpp_buff
*token_buf
= tokens_buff_new (pfile
, 1, &virt_locs
);
907 tokens_buff_add_token (token_buf
, virt_locs
, lhs
,
908 virt_loc
, 0, NULL
, 0);
909 push_extended_tokens_context (pfile
, context
->c
.mc
->macro_node
,
910 token_buf
, virt_locs
,
911 (const cpp_token
**)token_buf
->base
, 1);
914 _cpp_push_token_context (pfile
, NULL
, lhs
, 1);
917 /* Returns TRUE if the number of arguments ARGC supplied in an
918 invocation of the MACRO referenced by NODE is valid. An empty
919 invocation to a macro with no parameters should pass ARGC as zero.
921 Note that MACRO cannot necessarily be deduced from NODE, in case
922 NODE was redefined whilst collecting arguments. */
924 _cpp_arguments_ok (cpp_reader
*pfile
, cpp_macro
*macro
, const cpp_hashnode
*node
, unsigned int argc
)
926 if (argc
== macro
->paramc
)
929 if (argc
< macro
->paramc
)
931 /* In C++2a (here the va_opt flag is used), and also as a GNU
932 extension, variadic arguments are allowed to not appear in
933 the invocation at all.
934 e.g. #define debug(format, args...) something
937 This is exactly the same as if an empty variadic list had been
938 supplied - debug("string", ). */
940 if (argc
+ 1 == macro
->paramc
&& macro
->variadic
)
942 if (CPP_PEDANTIC (pfile
) && ! macro
->syshdr
943 && ! CPP_OPTION (pfile
, va_opt
))
945 if (CPP_OPTION (pfile
, cplusplus
))
946 cpp_error (pfile
, CPP_DL_PEDWARN
,
947 "ISO C++11 requires at least one argument "
948 "for the \"...\" in a variadic macro");
950 cpp_error (pfile
, CPP_DL_PEDWARN
,
951 "ISO C99 requires at least one argument "
952 "for the \"...\" in a variadic macro");
957 cpp_error (pfile
, CPP_DL_ERROR
,
958 "macro \"%s\" requires %u arguments, but only %u given",
959 NODE_NAME (node
), macro
->paramc
, argc
);
962 cpp_error (pfile
, CPP_DL_ERROR
,
963 "macro \"%s\" passed %u arguments, but takes just %u",
964 NODE_NAME (node
), argc
, macro
->paramc
);
969 /* Reads and returns the arguments to a function-like macro
970 invocation. Assumes the opening parenthesis has been processed.
971 If there is an error, emits an appropriate diagnostic and returns
972 NULL. Each argument is terminated by a CPP_EOF token, for the
973 future benefit of expand_arg(). If there are any deferred
974 #pragma directives among macro arguments, store pointers to the
975 CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.
977 What is returned is the buffer that contains the memory allocated
978 to hold the macro arguments. NODE is the name of the macro this
979 function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is
980 set to the actual number of macro arguments allocated in the
983 collect_args (cpp_reader
*pfile
, const cpp_hashnode
*node
,
984 _cpp_buff
**pragma_buff
, unsigned *num_args
)
986 _cpp_buff
*buff
, *base_buff
;
988 macro_arg
*args
, *arg
;
989 const cpp_token
*token
;
991 source_location virt_loc
;
992 bool track_macro_expansion_p
= CPP_OPTION (pfile
, track_macro_expansion
);
993 unsigned num_args_alloced
= 0;
995 macro
= node
->value
.macro
;
997 argc
= macro
->paramc
;
1001 #define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50
1002 #define ARG_TOKENS_EXTENT 1000
1004 buff
= _cpp_get_buff (pfile
, argc
* (DEFAULT_NUM_TOKENS_PER_MACRO_ARG
1005 * sizeof (cpp_token
*)
1006 + sizeof (macro_arg
)));
1008 args
= (macro_arg
*) buff
->base
;
1009 memset (args
, 0, argc
* sizeof (macro_arg
));
1010 buff
->cur
= (unsigned char *) &args
[argc
];
1011 arg
= args
, argc
= 0;
1013 /* Collect the tokens making up each argument. We don't yet know
1014 how many arguments have been supplied, whether too many or too
1015 few. Hence the slightly bizarre usage of "argc" and "arg". */
1018 unsigned int paren_depth
= 0;
1019 unsigned int ntokens
= 0;
1020 unsigned virt_locs_capacity
= DEFAULT_NUM_TOKENS_PER_MACRO_ARG
;
1024 arg
->first
= (const cpp_token
**) buff
->cur
;
1025 if (track_macro_expansion_p
)
1027 virt_locs_capacity
= DEFAULT_NUM_TOKENS_PER_MACRO_ARG
;
1028 arg
->virt_locs
= XNEWVEC (source_location
,
1029 virt_locs_capacity
);
1034 /* Require space for 2 new tokens (including a CPP_EOF). */
1035 if ((unsigned char *) &arg
->first
[ntokens
+ 2] > buff
->limit
)
1037 buff
= _cpp_append_extend_buff (pfile
, buff
,
1039 * sizeof (cpp_token
*));
1040 arg
->first
= (const cpp_token
**) buff
->cur
;
1042 if (track_macro_expansion_p
1043 && (ntokens
+ 2 > virt_locs_capacity
))
1045 virt_locs_capacity
+= ARG_TOKENS_EXTENT
;
1046 arg
->virt_locs
= XRESIZEVEC (source_location
,
1048 virt_locs_capacity
);
1051 token
= cpp_get_token_1 (pfile
, &virt_loc
);
1053 if (token
->type
== CPP_PADDING
)
1055 /* Drop leading padding. */
1059 else if (token
->type
== CPP_OPEN_PAREN
)
1061 else if (token
->type
== CPP_CLOSE_PAREN
)
1063 if (paren_depth
-- == 0)
1066 else if (token
->type
== CPP_COMMA
)
1068 /* A comma does not terminate an argument within
1069 parentheses or as part of a variable argument. */
1070 if (paren_depth
== 0
1071 && ! (macro
->variadic
&& argc
== macro
->paramc
))
1074 else if (token
->type
== CPP_EOF
1075 || (token
->type
== CPP_HASH
&& token
->flags
& BOL
))
1077 else if (token
->type
== CPP_PRAGMA
)
1079 cpp_token
*newtok
= _cpp_temp_token (pfile
);
1081 /* CPP_PRAGMA token lives in directive_result, which will
1082 be overwritten on the next directive. */
1087 if (*pragma_buff
== NULL
1088 || BUFF_ROOM (*pragma_buff
) < sizeof (cpp_token
*))
1091 if (*pragma_buff
== NULL
)
1093 = _cpp_get_buff (pfile
, 32 * sizeof (cpp_token
*));
1096 next
= *pragma_buff
;
1098 = _cpp_get_buff (pfile
,
1099 (BUFF_FRONT (*pragma_buff
)
1100 - (*pragma_buff
)->base
) * 2);
1101 (*pragma_buff
)->next
= next
;
1104 *(const cpp_token
**) BUFF_FRONT (*pragma_buff
) = token
;
1105 BUFF_FRONT (*pragma_buff
) += sizeof (cpp_token
*);
1106 if (token
->type
== CPP_PRAGMA_EOL
)
1108 token
= cpp_get_token_1 (pfile
, &virt_loc
);
1110 while (token
->type
!= CPP_EOF
);
1112 /* In deferred pragmas parsing_args and prevent_expansion
1113 had been changed, reset it. */
1114 pfile
->state
.parsing_args
= 2;
1115 pfile
->state
.prevent_expansion
= 1;
1117 if (token
->type
== CPP_EOF
)
1122 set_arg_token (arg
, token
, virt_loc
,
1123 ntokens
, MACRO_ARG_TOKEN_NORMAL
,
1124 CPP_OPTION (pfile
, track_macro_expansion
));
1128 /* Drop trailing padding. */
1129 while (ntokens
> 0 && arg
->first
[ntokens
- 1]->type
== CPP_PADDING
)
1132 arg
->count
= ntokens
;
1133 set_arg_token (arg
, &pfile
->eof
, pfile
->eof
.src_loc
,
1134 ntokens
, MACRO_ARG_TOKEN_NORMAL
,
1135 CPP_OPTION (pfile
, track_macro_expansion
));
1137 /* Terminate the argument. Excess arguments loop back and
1138 overwrite the final legitimate argument, before failing. */
1139 if (argc
<= macro
->paramc
)
1141 buff
->cur
= (unsigned char *) &arg
->first
[ntokens
+ 1];
1142 if (argc
!= macro
->paramc
)
1146 while (token
->type
!= CPP_CLOSE_PAREN
&& token
->type
!= CPP_EOF
);
1148 if (token
->type
== CPP_EOF
)
1150 /* We still need the CPP_EOF to end directives, and to end
1151 pre-expansion of a macro argument. Step back is not
1152 unconditional, since we don't want to return a CPP_EOF to our
1153 callers at the end of an -include-d file. */
1154 if (pfile
->context
->prev
|| pfile
->state
.in_directive
)
1155 _cpp_backup_tokens (pfile
, 1);
1156 cpp_error (pfile
, CPP_DL_ERROR
,
1157 "unterminated argument list invoking macro \"%s\"",
1162 /* A single empty argument is counted as no argument. */
1163 if (argc
== 1 && macro
->paramc
== 0 && args
[0].count
== 0)
1165 if (_cpp_arguments_ok (pfile
, macro
, node
, argc
))
1167 /* GCC has special semantics for , ## b where b is a varargs
1168 parameter: we remove the comma if b was omitted entirely.
1169 If b was merely an empty argument, the comma is retained.
1170 If the macro takes just one (varargs) parameter, then we
1171 retain the comma only if we are standards conforming.
1173 If FIRST is NULL replace_args () swallows the comma. */
1174 if (macro
->variadic
&& (argc
< macro
->paramc
1175 || (argc
== 1 && args
[0].count
== 0
1176 && !CPP_OPTION (pfile
, std
))))
1177 args
[macro
->paramc
- 1].first
= NULL
;
1179 *num_args
= num_args_alloced
;
1184 /* An error occurred. */
1185 _cpp_release_buff (pfile
, base_buff
);
1189 /* Search for an opening parenthesis to the macro of NODE, in such a
1190 way that, if none is found, we don't lose the information in any
1191 intervening padding tokens. If we find the parenthesis, collect
1192 the arguments and return the buffer containing them. PRAGMA_BUFF
1193 argument is the same as in collect_args. If NUM_ARGS is non-NULL,
1194 *NUM_ARGS is set to the number of arguments contained in the
1197 funlike_invocation_p (cpp_reader
*pfile
, cpp_hashnode
*node
,
1198 _cpp_buff
**pragma_buff
, unsigned *num_args
)
1200 const cpp_token
*token
, *padding
= NULL
;
1204 token
= cpp_get_token (pfile
);
1205 if (token
->type
!= CPP_PADDING
)
1208 || (!(padding
->flags
& PREV_WHITE
) && token
->val
.source
== NULL
))
1212 if (token
->type
== CPP_OPEN_PAREN
)
1214 pfile
->state
.parsing_args
= 2;
1215 return collect_args (pfile
, node
, pragma_buff
, num_args
);
1218 /* CPP_EOF can be the end of macro arguments, or the end of the
1219 file. We mustn't back up over the latter. Ugh. */
1220 if (token
->type
!= CPP_EOF
|| token
== &pfile
->eof
)
1222 /* Back up. We may have skipped padding, in which case backing
1223 up more than one token when expanding macros is in general
1224 too difficult. We re-insert it in its own context. */
1225 _cpp_backup_tokens (pfile
, 1);
1227 _cpp_push_token_context (pfile
, NULL
, padding
, 1);
1233 /* Return the real number of tokens in the expansion of MACRO. */
1234 static inline unsigned int
1235 macro_real_token_count (const cpp_macro
*macro
)
1237 if (__builtin_expect (!macro
->extra_tokens
, true))
1238 return macro
->count
;
1240 for (unsigned i
= macro
->count
; i
--;)
1241 if (macro
->exp
.tokens
[i
].type
!= CPP_PASTE
)
1247 /* Push the context of a macro with hash entry NODE onto the context
1248 stack. If we can successfully expand the macro, we push a context
1249 containing its yet-to-be-rescanned replacement list and return one.
1250 If there were additionally any unexpanded deferred #pragma
1251 directives among macro arguments, push another context containing
1252 the pragma tokens before the yet-to-be-rescanned replacement list
1253 and return two. Otherwise, we don't push a context and return
1254 zero. LOCATION is the location of the expansion point of the
1257 enter_macro_context (cpp_reader
*pfile
, cpp_hashnode
*node
,
1258 const cpp_token
*result
, source_location location
)
1260 /* The presence of a macro invalidates a file's controlling macro. */
1261 pfile
->mi_valid
= false;
1263 pfile
->state
.angled_headers
= false;
1265 /* From here to when we push the context for the macro later down
1266 this function, we need to flag the fact that we are about to
1267 expand a macro. This is useful when -ftrack-macro-expansion is
1268 turned off. In that case, we need to record the location of the
1269 expansion point of the top-most macro we are about to to expand,
1270 into pfile->invocation_location. But we must not record any such
1271 location once the process of expanding the macro starts; that is,
1272 we must not do that recording between now and later down this
1273 function where set this flag to FALSE. */
1274 pfile
->about_to_expand_macro_p
= true;
1276 if (cpp_user_macro_p (node
))
1278 cpp_macro
*macro
= node
->value
.macro
;
1279 _cpp_buff
*pragma_buff
= NULL
;
1281 if (macro
->fun_like
)
1284 unsigned num_args
= 0;
1286 pfile
->state
.prevent_expansion
++;
1287 pfile
->keep_tokens
++;
1288 pfile
->state
.parsing_args
= 1;
1289 buff
= funlike_invocation_p (pfile
, node
, &pragma_buff
,
1291 pfile
->state
.parsing_args
= 0;
1292 pfile
->keep_tokens
--;
1293 pfile
->state
.prevent_expansion
--;
1297 if (CPP_WTRADITIONAL (pfile
) && ! node
->value
.macro
->syshdr
)
1298 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
1299 "function-like macro \"%s\" must be used with arguments in traditional C",
1303 _cpp_release_buff (pfile
, pragma_buff
);
1305 pfile
->about_to_expand_macro_p
= false;
1309 if (macro
->paramc
> 0)
1310 replace_args (pfile
, node
, macro
,
1311 (macro_arg
*) buff
->base
,
1313 /* Free the memory used by the arguments of this
1314 function-like macro. This memory has been allocated by
1315 funlike_invocation_p and by replace_args. */
1316 delete_macro_args (buff
, num_args
);
1319 /* Disable the macro within its expansion. */
1320 node
->flags
|= NODE_DISABLED
;
1322 /* Laziness can only affect the expansion tokens of the macro,
1323 not its fun-likeness or parameters. */
1324 _cpp_maybe_notify_macro_use (pfile
, node
);
1326 pfile
->cb
.used (pfile
, location
, node
);
1330 if (macro
->paramc
== 0)
1332 unsigned tokens_count
= macro_real_token_count (macro
);
1333 if (CPP_OPTION (pfile
, track_macro_expansion
))
1336 const cpp_token
*src
= macro
->exp
.tokens
;
1337 const line_map_macro
*map
;
1338 source_location
*virt_locs
= NULL
;
1339 _cpp_buff
*macro_tokens
1340 = tokens_buff_new (pfile
, tokens_count
, &virt_locs
);
1342 /* Create a macro map to record the locations of the
1343 tokens that are involved in the expansion. LOCATION
1344 is the location of the macro expansion point. */
1345 map
= linemap_enter_macro (pfile
->line_table
,
1346 node
, location
, tokens_count
);
1347 for (i
= 0; i
< tokens_count
; ++i
)
1349 tokens_buff_add_token (macro_tokens
, virt_locs
,
1351 src
->src_loc
, map
, i
);
1354 push_extended_tokens_context (pfile
, node
,
1357 (const cpp_token
**)
1362 _cpp_push_token_context (pfile
, node
, macro
->exp
.tokens
,
1364 num_macro_tokens_counter
+= tokens_count
;
1369 if (!pfile
->state
.in_directive
)
1370 _cpp_push_token_context (pfile
, NULL
,
1371 padding_token (pfile
, result
), 1);
1374 unsigned tokens_count
;
1375 _cpp_buff
*tail
= pragma_buff
->next
;
1376 pragma_buff
->next
= NULL
;
1377 tokens_count
= ((const cpp_token
**) BUFF_FRONT (pragma_buff
)
1378 - (const cpp_token
**) pragma_buff
->base
);
1379 push_ptoken_context (pfile
, NULL
, pragma_buff
,
1380 (const cpp_token
**) pragma_buff
->base
,
1383 if (!CPP_OPTION (pfile
, track_macro_expansion
))
1384 num_macro_tokens_counter
+= tokens_count
;
1387 while (pragma_buff
!= NULL
);
1388 pfile
->about_to_expand_macro_p
= false;
1392 pfile
->about_to_expand_macro_p
= false;
1396 pfile
->about_to_expand_macro_p
= false;
1397 /* Handle built-in macros and the _Pragma operator. */
1399 source_location expand_loc
;
1401 if (/* The top-level macro invocation that triggered the expansion
1402 we are looking at is with a function-like user macro ... */
1403 cpp_fun_like_macro_p (pfile
->top_most_macro_node
)
1404 /* ... and we are tracking the macro expansion. */
1405 && CPP_OPTION (pfile
, track_macro_expansion
))
1406 /* Then the location of the end of the macro invocation is the
1407 location of the expansion point of this macro. */
1408 expand_loc
= location
;
1410 /* Otherwise, the location of the end of the macro invocation is
1411 the location of the expansion point of that top-level macro
1413 expand_loc
= pfile
->invocation_location
;
1415 return builtin_macro (pfile
, node
, location
, expand_loc
);
1419 /* De-allocate the memory used by BUFF which is an array of instances
1420 of macro_arg. NUM_ARGS is the number of instances of macro_arg
1423 delete_macro_args (_cpp_buff
*buff
, unsigned num_args
)
1425 macro_arg
*macro_args
;
1431 macro_args
= (macro_arg
*) buff
->base
;
1433 /* Walk instances of macro_arg to free their expanded tokens as well
1434 as their macro_arg::virt_locs members. */
1435 for (i
= 0; i
< num_args
; ++i
)
1437 if (macro_args
[i
].expanded
)
1439 free (macro_args
[i
].expanded
);
1440 macro_args
[i
].expanded
= NULL
;
1442 if (macro_args
[i
].virt_locs
)
1444 free (macro_args
[i
].virt_locs
);
1445 macro_args
[i
].virt_locs
= NULL
;
1447 if (macro_args
[i
].expanded_virt_locs
)
1449 free (macro_args
[i
].expanded_virt_locs
);
1450 macro_args
[i
].expanded_virt_locs
= NULL
;
1453 _cpp_free_buff (buff
);
1456 /* Set the INDEXth token of the macro argument ARG. TOKEN is the token
1457 to set, LOCATION is its virtual location. "Virtual" location means
1458 the location that encodes loci across macro expansion. Otherwise
1459 it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the
1460 argument ARG is supposed to contain. Note that ARG must be
1461 tailored so that it has enough room to contain INDEX + 1 numbers of
1462 tokens, at least. */
1464 set_arg_token (macro_arg
*arg
, const cpp_token
*token
,
1465 source_location location
, size_t index
,
1466 enum macro_arg_token_kind kind
,
1467 bool track_macro_exp_p
)
1469 const cpp_token
**token_ptr
;
1470 source_location
*loc
= NULL
;
1473 arg_token_ptr_at (arg
, index
, kind
,
1474 track_macro_exp_p
? &loc
: NULL
);
1479 /* We can't set the location of a stringified argument
1480 token and we can't set any location if we aren't tracking
1481 macro expansion locations. */
1482 gcc_checking_assert (kind
!= MACRO_ARG_TOKEN_STRINGIFIED
1483 && track_macro_exp_p
);
1488 /* Get the pointer to the location of the argument token of the
1489 function-like macro argument ARG. This function must be called
1490 only when we -ftrack-macro-expansion is on. */
1491 static const source_location
*
1492 get_arg_token_location (const macro_arg
*arg
,
1493 enum macro_arg_token_kind kind
)
1495 const source_location
*loc
= NULL
;
1496 const cpp_token
**token_ptr
=
1497 arg_token_ptr_at (arg
, 0, kind
, (source_location
**) &loc
);
1499 if (token_ptr
== NULL
)
1505 /* Return the pointer to the INDEXth token of the macro argument ARG.
1506 KIND specifies the kind of token the macro argument ARG contains.
1507 If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
1508 of the virtual location of the returned token if the
1509 -ftrack-macro-expansion flag is on; otherwise, it's set to the
1510 spelling location of the returned token. */
1511 static const cpp_token
**
1512 arg_token_ptr_at (const macro_arg
*arg
, size_t index
,
1513 enum macro_arg_token_kind kind
,
1514 source_location
**virt_location
)
1516 const cpp_token
**tokens_ptr
= NULL
;
1520 case MACRO_ARG_TOKEN_NORMAL
:
1521 tokens_ptr
= arg
->first
;
1523 case MACRO_ARG_TOKEN_STRINGIFIED
:
1524 tokens_ptr
= (const cpp_token
**) &arg
->stringified
;
1526 case MACRO_ARG_TOKEN_EXPANDED
:
1527 tokens_ptr
= arg
->expanded
;
1531 if (tokens_ptr
== NULL
)
1532 /* This can happen for e.g, an empty token argument to a
1533 funtion-like macro. */
1538 if (kind
== MACRO_ARG_TOKEN_NORMAL
)
1539 *virt_location
= &arg
->virt_locs
[index
];
1540 else if (kind
== MACRO_ARG_TOKEN_EXPANDED
)
1541 *virt_location
= &arg
->expanded_virt_locs
[index
];
1542 else if (kind
== MACRO_ARG_TOKEN_STRINGIFIED
)
1544 (source_location
*) &tokens_ptr
[index
]->src_loc
;
1546 return &tokens_ptr
[index
];
1549 /* Initialize an iterator so that it iterates over the tokens of a
1550 function-like macro argument. KIND is the kind of tokens we want
1551 ITER to iterate over. TOKEN_PTR points the first token ITER will
1554 macro_arg_token_iter_init (macro_arg_token_iter
*iter
,
1555 bool track_macro_exp_p
,
1556 enum macro_arg_token_kind kind
,
1557 const macro_arg
*arg
,
1558 const cpp_token
**token_ptr
)
1560 iter
->track_macro_exp_p
= track_macro_exp_p
;
1562 iter
->token_ptr
= token_ptr
;
1563 /* Unconditionally initialize this so that the compiler doesn't warn
1564 about iter->location_ptr being possibly uninitialized later after
1565 this code has been inlined somewhere. */
1566 iter
->location_ptr
= NULL
;
1567 if (track_macro_exp_p
)
1568 iter
->location_ptr
= get_arg_token_location (arg
, kind
);
1570 iter
->num_forwards
= 0;
1571 if (track_macro_exp_p
1572 && token_ptr
!= NULL
1573 && iter
->location_ptr
== NULL
)
1578 /* Move the iterator one token forward. Note that if IT was
1579 initialized on an argument that has a stringified token, moving it
1580 forward doesn't make sense as a stringified token is essentially one
1583 macro_arg_token_iter_forward (macro_arg_token_iter
*it
)
1587 case MACRO_ARG_TOKEN_NORMAL
:
1588 case MACRO_ARG_TOKEN_EXPANDED
:
1590 if (it
->track_macro_exp_p
)
1593 case MACRO_ARG_TOKEN_STRINGIFIED
:
1595 if (it
->num_forwards
> 0)
1606 /* Return the token pointed to by the iterator. */
1607 static const cpp_token
*
1608 macro_arg_token_iter_get_token (const macro_arg_token_iter
*it
)
1611 if (it
->kind
== MACRO_ARG_TOKEN_STRINGIFIED
1612 && it
->num_forwards
> 0)
1615 if (it
->token_ptr
== NULL
)
1617 return *it
->token_ptr
;
1620 /* Return the location of the token pointed to by the iterator.*/
1621 static source_location
1622 macro_arg_token_iter_get_location (const macro_arg_token_iter
*it
)
1625 if (it
->kind
== MACRO_ARG_TOKEN_STRINGIFIED
1626 && it
->num_forwards
> 0)
1629 if (it
->track_macro_exp_p
)
1630 return *it
->location_ptr
;
1632 return (*it
->token_ptr
)->src_loc
;
1635 /* Return the index of a token [resulting from macro expansion] inside
1636 the total list of tokens resulting from a given macro
1637 expansion. The index can be different depending on whether if we
1638 want each tokens resulting from function-like macro arguments
1639 expansion to have a different location or not.
1641 E.g, consider this function-like macro:
1645 Then consider us "calling" it (and thus expanding it) like:
1649 It will be expanded into:
1653 Let's consider the case of the token '4'.
1655 Its index can be 2 (it's the third token of the set of tokens
1656 resulting from the expansion) or it can be 0 if we consider that
1657 all tokens resulting from the expansion of the argument "1+2" have
1658 the same index, which is 0. In this later case, the index of token
1659 '-' would then be 1 and the index of token '3' would be 2.
1661 The later case is useful to use less memory e.g, for the case of
1662 the user using the option -ftrack-macro-expansion=1.
1664 ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
1665 are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro
1666 parameter (inside the macro replacement list) that corresponds to
1667 the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
1670 If we refer to the example above, for the '4' argument token,
1671 ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
1672 would be set to the token 'x', in the replacement list "x - 3" of
1675 This is a subroutine of replace_args. */
1676 inline static unsigned
1677 expanded_token_index (cpp_reader
*pfile
, cpp_macro
*macro
,
1678 const cpp_token
*cur_replacement_token
,
1679 unsigned absolute_token_index
)
1681 if (CPP_OPTION (pfile
, track_macro_expansion
) > 1)
1682 return absolute_token_index
;
1683 return cur_replacement_token
- macro
->exp
.tokens
;
1686 /* Copy whether PASTE_LEFT is set from SRC to *PASTE_FLAG. */
1689 copy_paste_flag (cpp_reader
*pfile
, const cpp_token
**paste_flag
,
1690 const cpp_token
*src
)
1692 cpp_token
*token
= _cpp_temp_token (pfile
);
1693 token
->type
= (*paste_flag
)->type
;
1694 token
->val
= (*paste_flag
)->val
;
1695 if (src
->flags
& PASTE_LEFT
)
1696 token
->flags
= (*paste_flag
)->flags
| PASTE_LEFT
;
1698 token
->flags
= (*paste_flag
)->flags
& ~PASTE_LEFT
;
1699 *paste_flag
= token
;
1702 /* True IFF the last token emitted into BUFF (if any) is PTR. */
1705 last_token_is (_cpp_buff
*buff
, const cpp_token
**ptr
)
1707 return (ptr
&& tokens_buff_last_token_ptr (buff
) == ptr
);
1710 /* Replace the parameters in a function-like macro of NODE with the
1711 actual ARGS, and place the result in a newly pushed token context.
1712 Expand each argument before replacing, unless it is operated upon
1713 by the # or ## operators. EXPANSION_POINT_LOC is the location of
1714 the expansion point of the macro. E.g, the location of the
1715 function-like macro invocation. */
1717 replace_args (cpp_reader
*pfile
, cpp_hashnode
*node
, cpp_macro
*macro
,
1718 macro_arg
*args
, source_location expansion_point_loc
)
1720 unsigned int i
, total
;
1721 const cpp_token
*src
, *limit
;
1722 const cpp_token
**first
= NULL
;
1724 _cpp_buff
*buff
= NULL
;
1725 source_location
*virt_locs
= NULL
;
1726 unsigned int exp_count
;
1727 const line_map_macro
*map
= NULL
;
1728 int track_macro_exp
;
1730 /* First, fully macro-expand arguments, calculating the number of
1731 tokens in the final expansion as we go. The ordering of the if
1732 statements below is subtle; we must handle stringification before
1735 /* EXP_COUNT is the number of tokens in the macro replacement
1736 list. TOTAL is the number of tokens /after/ macro parameters
1737 have been replaced by their arguments. */
1738 exp_count
= macro_real_token_count (macro
);
1740 limit
= macro
->exp
.tokens
+ exp_count
;
1742 for (src
= macro
->exp
.tokens
; src
< limit
; src
++)
1743 if (src
->type
== CPP_MACRO_ARG
)
1745 /* Leading and trailing padding tokens. */
1747 /* Account for leading and padding tokens in exp_count too.
1748 This is going to be important later down this function,
1749 when we want to handle the case of (track_macro_exp <
1753 /* We have an argument. If it is not being stringified or
1754 pasted it is macro-replaced before insertion. */
1755 arg
= &args
[src
->val
.macro_arg
.arg_no
- 1];
1757 if (src
->flags
& STRINGIFY_ARG
)
1759 if (!arg
->stringified
)
1760 arg
->stringified
= stringify_arg (pfile
, arg
);
1762 else if ((src
->flags
& PASTE_LEFT
)
1763 || (src
!= macro
->exp
.tokens
&& (src
[-1].flags
& PASTE_LEFT
)))
1764 total
+= arg
->count
- 1;
1768 expand_arg (pfile
, arg
);
1769 total
+= arg
->expanded_count
- 1;
1773 /* When the compiler is called with the -ftrack-macro-expansion
1774 flag, we need to keep track of the location of each token that
1775 results from macro expansion.
1777 A token resulting from macro expansion is not a new token. It is
1778 simply the same token as the token coming from the macro
1779 definition. The new things that are allocated are the buffer
1780 that holds the tokens resulting from macro expansion and a new
1781 location that records many things like the locus of the expansion
1782 point as well as the original locus inside the definition of the
1783 macro. This location is called a virtual location.
1785 So the buffer BUFF holds a set of cpp_token*, and the buffer
1786 VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
1788 Both of these two buffers are going to be hung off of the macro
1789 context, when the latter is pushed. The memory allocated to
1790 store the tokens and their locations is going to be freed once
1791 the context of macro expansion is popped.
1793 As far as tokens are concerned, the memory overhead of
1794 -ftrack-macro-expansion is proportional to the number of
1795 macros that get expanded multiplied by sizeof (source_location).
1796 The good news is that extra memory gets freed when the macro
1797 context is freed, i.e shortly after the macro got expanded. */
1799 /* Is the -ftrack-macro-expansion flag in effect? */
1800 track_macro_exp
= CPP_OPTION (pfile
, track_macro_expansion
);
1802 /* Now allocate memory space for tokens and locations resulting from
1803 the macro expansion, copy the tokens and replace the arguments.
1804 This memory must be freed when the context of the macro MACRO is
1806 buff
= tokens_buff_new (pfile
, total
, track_macro_exp
? &virt_locs
: NULL
);
1808 first
= (const cpp_token
**) buff
->base
;
1810 /* Create a macro map to record the locations of the tokens that are
1811 involved in the expansion. Note that the expansion point is set
1812 to the location of the closing parenthesis. Otherwise, the
1813 subsequent map created for the first token that comes after the
1814 macro map might have a wrong line number. That would lead to
1815 tokens with wrong line numbers after the macro expansion. This
1816 adds up to the memory overhead of the -ftrack-macro-expansion
1817 flag; for every macro that is expanded, a "macro map" is
1819 if (track_macro_exp
)
1821 int num_macro_tokens
= total
;
1822 if (track_macro_exp
< 2)
1823 /* Then the number of macro tokens won't take in account the
1824 fact that function-like macro arguments can expand to
1825 multiple tokens. This is to save memory at the expense of
1828 Suppose we have #define SQUARE(A) A * A
1830 And then we do SQUARE(2+3)
1832 Then the tokens 2, +, 3, will have the same location,
1833 saying they come from the expansion of the argument A. */
1834 num_macro_tokens
= exp_count
;
1835 map
= linemap_enter_macro (pfile
->line_table
, node
,
1836 expansion_point_loc
,
1840 vaopt_state
vaopt_tracker (pfile
, macro
->variadic
,
1841 args
[macro
->paramc
- 1].count
> 0);
1842 const cpp_token
**vaopt_start
= NULL
;
1843 for (src
= macro
->exp
.tokens
; src
< limit
; src
++)
1845 unsigned int arg_tokens_count
;
1846 macro_arg_token_iter from
;
1847 const cpp_token
**paste_flag
= NULL
;
1848 const cpp_token
**tmp_token_ptr
;
1850 /* __VA_OPT__ handling. */
1851 vaopt_state::update_type vostate
= vaopt_tracker
.update (src
);
1852 if (vostate
!= vaopt_state::INCLUDE
)
1854 if (vostate
== vaopt_state::BEGIN
)
1856 /* Padding on the left of __VA_OPT__ (unless RHS of ##). */
1857 if (src
!= macro
->exp
.tokens
&& !(src
[-1].flags
& PASTE_LEFT
))
1859 const cpp_token
*t
= padding_token (pfile
, src
);
1860 unsigned index
= expanded_token_index (pfile
, macro
, src
, i
);
1861 /* Allocate a virtual location for the padding token and
1862 append the token and its location to BUFF and
1864 tokens_buff_add_token (buff
, virt_locs
, t
,
1865 t
->src_loc
, t
->src_loc
,
1868 vaopt_start
= tokens_buff_last_token_ptr (buff
);
1870 else if (vostate
== vaopt_state::END
)
1872 const cpp_token
**start
= vaopt_start
;
1875 /* Remove any tail padding from inside the __VA_OPT__. */
1876 paste_flag
= tokens_buff_last_token_ptr (buff
);
1877 while (paste_flag
&& paste_flag
!= start
1878 && (*paste_flag
)->type
== CPP_PADDING
)
1880 tokens_buff_remove_last_token (buff
);
1881 paste_flag
= tokens_buff_last_token_ptr (buff
);
1884 if (src
->flags
& PASTE_LEFT
)
1886 /* With a non-empty __VA_OPT__ on the LHS of ##, the last
1887 token should be flagged PASTE_LEFT. */
1888 if (paste_flag
&& (*paste_flag
)->type
!= CPP_PADDING
)
1889 copy_paste_flag (pfile
, paste_flag
, src
);
1893 /* Otherwise, avoid paste on RHS, __VA_OPT__(c)d or
1894 __VA_OPT__(c)__VA_OPT__(d). */
1895 const cpp_token
*t
= &pfile
->avoid_paste
;
1896 tokens_buff_add_token (buff
, virt_locs
,
1897 t
, t
->src_loc
, t
->src_loc
,
1904 if (src
->type
!= CPP_MACRO_ARG
)
1906 /* Allocate a virtual location for token SRC, and add that
1907 token and its virtual location into the buffers BUFF and
1909 unsigned index
= expanded_token_index (pfile
, macro
, src
, i
);
1910 tokens_buff_add_token (buff
, virt_locs
, src
,
1911 src
->src_loc
, src
->src_loc
,
1918 arg
= &args
[src
->val
.macro_arg
.arg_no
- 1];
1919 /* SRC is a macro parameter that we need to replace with its
1920 corresponding argument. So at some point we'll need to
1921 iterate over the tokens of the macro argument and copy them
1922 into the "place" now holding the correspondig macro
1923 parameter. We are going to use the iterator type
1924 macro_argo_token_iter to handle that iterating. The 'if'
1925 below is to initialize the iterator depending on the type of
1926 tokens the macro argument has. It also does some adjustment
1927 related to padding tokens and some pasting corner cases. */
1928 if (src
->flags
& STRINGIFY_ARG
)
1930 arg_tokens_count
= 1;
1931 macro_arg_token_iter_init (&from
,
1933 track_macro_expansion
),
1934 MACRO_ARG_TOKEN_STRINGIFIED
,
1935 arg
, &arg
->stringified
);
1937 else if (src
->flags
& PASTE_LEFT
)
1939 arg_tokens_count
= arg
->count
;
1940 macro_arg_token_iter_init (&from
,
1942 track_macro_expansion
),
1943 MACRO_ARG_TOKEN_NORMAL
,
1946 else if (src
!= macro
->exp
.tokens
&& (src
[-1].flags
& PASTE_LEFT
))
1949 arg_tokens_count
= arg
->count
;
1950 macro_arg_token_iter_init (&from
,
1952 track_macro_expansion
),
1953 MACRO_ARG_TOKEN_NORMAL
,
1956 num_toks
= tokens_buff_count (buff
);
1960 /* So the current parameter token is pasted to the previous
1961 token in the replacement list. Let's look at what
1962 we have as previous and current arguments. */
1964 /* This is the previous argument's token ... */
1965 tmp_token_ptr
= tokens_buff_last_token_ptr (buff
);
1967 if ((*tmp_token_ptr
)->type
== CPP_COMMA
1969 && src
->val
.macro_arg
.arg_no
== macro
->paramc
)
1971 /* ... which is a comma; and the current parameter
1972 is the last parameter of a variadic function-like
1973 macro. If the argument to the current last
1974 parameter is NULL, then swallow the comma,
1975 otherwise drop the paste flag. */
1976 if (macro_arg_token_iter_get_token (&from
) == NULL
)
1977 tokens_buff_remove_last_token (buff
);
1979 paste_flag
= tmp_token_ptr
;
1981 /* Remove the paste flag if the RHS is a placemarker, unless the
1982 previous emitted token is at the beginning of __VA_OPT__;
1983 placemarkers within __VA_OPT__ are ignored in that case. */
1984 else if (arg_tokens_count
== 0
1985 && tmp_token_ptr
!= vaopt_start
)
1986 paste_flag
= tmp_token_ptr
;
1991 arg_tokens_count
= arg
->expanded_count
;
1992 macro_arg_token_iter_init (&from
,
1994 track_macro_expansion
),
1995 MACRO_ARG_TOKEN_EXPANDED
,
1996 arg
, arg
->expanded
);
1998 if (last_token_is (buff
, vaopt_start
))
2000 /* We're expanding an arg at the beginning of __VA_OPT__.
2002 while (arg_tokens_count
)
2004 const cpp_token
*t
= macro_arg_token_iter_get_token (&from
);
2005 if (t
->type
!= CPP_PADDING
)
2007 macro_arg_token_iter_forward (&from
);
2013 /* Padding on the left of an argument (unless RHS of ##). */
2014 if ((!pfile
->state
.in_directive
|| pfile
->state
.directive_wants_padding
)
2015 && src
!= macro
->exp
.tokens
&& !(src
[-1].flags
& PASTE_LEFT
)
2016 && !last_token_is (buff
, vaopt_start
))
2018 const cpp_token
*t
= padding_token (pfile
, src
);
2019 unsigned index
= expanded_token_index (pfile
, macro
, src
, i
);
2020 /* Allocate a virtual location for the padding token and
2021 append the token and its location to BUFF and
2023 tokens_buff_add_token (buff
, virt_locs
, t
,
2024 t
->src_loc
, t
->src_loc
,
2028 if (arg_tokens_count
)
2030 /* So now we've got the number of tokens that make up the
2031 argument that is going to replace the current parameter
2032 in the macro's replacement list. */
2034 for (j
= 0; j
< arg_tokens_count
; ++j
)
2036 /* So if track_macro_exp is < 2, the user wants to
2037 save extra memory while tracking macro expansion
2038 locations. So in that case here is what we do:
2040 Suppose we have #define SQUARE(A) A * A
2042 And then we do SQUARE(2+3)
2044 Then the tokens 2, +, 3, will have the same location,
2045 saying they come from the expansion of the argument
2048 So that means we are going to ignore the COUNT tokens
2049 resulting from the expansion of the current macro
2050 argument. In other words all the ARG_TOKENS_COUNT tokens
2051 resulting from the expansion of the macro argument will
2052 have the index I. Normally, each of those tokens should
2054 unsigned token_index
= i
;
2056 if (track_macro_exp
> 1)
2059 index
= expanded_token_index (pfile
, macro
, src
, token_index
);
2060 tokens_buff_add_token (buff
, virt_locs
,
2061 macro_arg_token_iter_get_token (&from
),
2062 macro_arg_token_iter_get_location (&from
),
2063 src
->src_loc
, map
, index
);
2064 macro_arg_token_iter_forward (&from
);
2067 /* With a non-empty argument on the LHS of ##, the last
2068 token should be flagged PASTE_LEFT. */
2069 if (src
->flags
& PASTE_LEFT
)
2071 = (const cpp_token
**) tokens_buff_last_token_ptr (buff
);
2073 else if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, c99
)
2074 && ! macro
->syshdr
&& ! cpp_in_system_header (pfile
))
2076 if (CPP_OPTION (pfile
, cplusplus
))
2077 cpp_pedwarning (pfile
, CPP_W_PEDANTIC
,
2078 "invoking macro %s argument %d: "
2079 "empty macro arguments are undefined"
2081 NODE_NAME (node
), src
->val
.macro_arg
.arg_no
);
2082 else if (CPP_OPTION (pfile
, cpp_warn_c90_c99_compat
))
2083 cpp_pedwarning (pfile
,
2084 CPP_OPTION (pfile
, cpp_warn_c90_c99_compat
) > 0
2085 ? CPP_W_C90_C99_COMPAT
: CPP_W_PEDANTIC
,
2086 "invoking macro %s argument %d: "
2087 "empty macro arguments are undefined"
2089 NODE_NAME (node
), src
->val
.macro_arg
.arg_no
);
2091 else if (CPP_OPTION (pfile
, cpp_warn_c90_c99_compat
) > 0
2092 && ! CPP_OPTION (pfile
, cplusplus
)
2093 && ! macro
->syshdr
&& ! cpp_in_system_header (pfile
))
2094 cpp_warning (pfile
, CPP_W_C90_C99_COMPAT
,
2095 "invoking macro %s argument %d: "
2096 "empty macro arguments are undefined"
2098 NODE_NAME (node
), src
->val
.macro_arg
.arg_no
);
2100 /* Avoid paste on RHS (even case count == 0). */
2101 if (!pfile
->state
.in_directive
&& !(src
->flags
& PASTE_LEFT
)
2102 && !last_token_is (buff
, vaopt_start
))
2104 const cpp_token
*t
= &pfile
->avoid_paste
;
2105 tokens_buff_add_token (buff
, virt_locs
,
2106 t
, t
->src_loc
, t
->src_loc
,
2110 /* Add a new paste flag, or remove an unwanted one. */
2112 copy_paste_flag (pfile
, paste_flag
, src
);
2114 i
+= arg_tokens_count
;
2117 if (track_macro_exp
)
2118 push_extended_tokens_context (pfile
, node
, buff
, virt_locs
, first
,
2119 tokens_buff_count (buff
));
2121 push_ptoken_context (pfile
, node
, buff
, first
,
2122 tokens_buff_count (buff
));
2124 num_macro_tokens_counter
+= tokens_buff_count (buff
);
2127 /* Return a special padding token, with padding inherited from SOURCE. */
2128 static const cpp_token
*
2129 padding_token (cpp_reader
*pfile
, const cpp_token
*source
)
2131 cpp_token
*result
= _cpp_temp_token (pfile
);
2133 result
->type
= CPP_PADDING
;
2135 /* Data in GCed data structures cannot be made const so far, so we
2136 need a cast here. */
2137 result
->val
.source
= (cpp_token
*) source
;
2142 /* Get a new uninitialized context. Create a new one if we cannot
2143 re-use an old one. */
2144 static cpp_context
*
2145 next_context (cpp_reader
*pfile
)
2147 cpp_context
*result
= pfile
->context
->next
;
2151 result
= XNEW (cpp_context
);
2152 memset (result
, 0, sizeof (cpp_context
));
2153 result
->prev
= pfile
->context
;
2155 pfile
->context
->next
= result
;
2158 pfile
->context
= result
;
2162 /* Push a list of pointers to tokens. */
2164 push_ptoken_context (cpp_reader
*pfile
, cpp_hashnode
*macro
, _cpp_buff
*buff
,
2165 const cpp_token
**first
, unsigned int count
)
2167 cpp_context
*context
= next_context (pfile
);
2169 context
->tokens_kind
= TOKENS_KIND_INDIRECT
;
2170 context
->c
.macro
= macro
;
2171 context
->buff
= buff
;
2172 FIRST (context
).ptoken
= first
;
2173 LAST (context
).ptoken
= first
+ count
;
2176 /* Push a list of tokens.
2178 A NULL macro means that we should continue the current macro
2179 expansion, in essence. That means that if we are currently in a
2180 macro expansion context, we'll make the new pfile->context refer to
2181 the current macro. */
2183 _cpp_push_token_context (cpp_reader
*pfile
, cpp_hashnode
*macro
,
2184 const cpp_token
*first
, unsigned int count
)
2186 cpp_context
*context
;
2189 macro
= macro_of_context (pfile
->context
);
2191 context
= next_context (pfile
);
2192 context
->tokens_kind
= TOKENS_KIND_DIRECT
;
2193 context
->c
.macro
= macro
;
2194 context
->buff
= NULL
;
2195 FIRST (context
).token
= first
;
2196 LAST (context
).token
= first
+ count
;
2199 /* Build a context containing a list of tokens as well as their
2200 virtual locations and push it. TOKENS_BUFF is the buffer that
2201 contains the tokens pointed to by FIRST. If TOKENS_BUFF is
2202 non-NULL, it means that the context owns it, meaning that
2203 _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
2204 contains the virtual locations.
2206 A NULL macro means that we should continue the current macro
2207 expansion, in essence. That means that if we are currently in a
2208 macro expansion context, we'll make the new pfile->context refer to
2209 the current macro. */
2211 push_extended_tokens_context (cpp_reader
*pfile
,
2212 cpp_hashnode
*macro
,
2213 _cpp_buff
*token_buff
,
2214 source_location
*virt_locs
,
2215 const cpp_token
**first
,
2218 cpp_context
*context
;
2222 macro
= macro_of_context (pfile
->context
);
2224 context
= next_context (pfile
);
2225 context
->tokens_kind
= TOKENS_KIND_EXTENDED
;
2226 context
->buff
= token_buff
;
2228 m
= XNEW (macro_context
);
2229 m
->macro_node
= macro
;
2230 m
->virt_locs
= virt_locs
;
2231 m
->cur_virt_loc
= virt_locs
;
2233 FIRST (context
).ptoken
= first
;
2234 LAST (context
).ptoken
= first
+ count
;
2237 /* Push a traditional macro's replacement text. */
2239 _cpp_push_text_context (cpp_reader
*pfile
, cpp_hashnode
*macro
,
2240 const uchar
*start
, size_t len
)
2242 cpp_context
*context
= next_context (pfile
);
2244 context
->tokens_kind
= TOKENS_KIND_DIRECT
;
2245 context
->c
.macro
= macro
;
2246 context
->buff
= NULL
;
2247 CUR (context
) = start
;
2248 RLIMIT (context
) = start
+ len
;
2249 macro
->flags
|= NODE_DISABLED
;
2252 /* Creates a buffer that holds tokens a.k.a "token buffer", usually
2253 for the purpose of storing them on a cpp_context. If VIRT_LOCS is
2254 non-null (which means that -ftrack-macro-expansion is on),
2255 *VIRT_LOCS is set to a newly allocated buffer that is supposed to
2256 hold the virtual locations of the tokens resulting from macro
2259 tokens_buff_new (cpp_reader
*pfile
, size_t len
,
2260 source_location
**virt_locs
)
2262 size_t tokens_size
= len
* sizeof (cpp_token
*);
2263 size_t locs_size
= len
* sizeof (source_location
);
2265 if (virt_locs
!= NULL
)
2266 *virt_locs
= XNEWVEC (source_location
, locs_size
);
2267 return _cpp_get_buff (pfile
, tokens_size
);
2270 /* Returns the number of tokens contained in a token buffer. The
2271 buffer holds a set of cpp_token*. */
2273 tokens_buff_count (_cpp_buff
*buff
)
2275 return (BUFF_FRONT (buff
) - buff
->base
) / sizeof (cpp_token
*);
2278 /* Return a pointer to the last token contained in the token buffer
2280 static const cpp_token
**
2281 tokens_buff_last_token_ptr (_cpp_buff
*buff
)
2283 if (BUFF_FRONT (buff
) == buff
->base
)
2285 return &((const cpp_token
**) BUFF_FRONT (buff
))[-1];
2288 /* Remove the last token contained in the token buffer TOKENS_BUFF.
2289 If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer
2290 containing the virtual locations of the tokens in TOKENS_BUFF; in
2291 which case the function updates that buffer as well. */
2293 tokens_buff_remove_last_token (_cpp_buff
*tokens_buff
)
2296 if (BUFF_FRONT (tokens_buff
) > tokens_buff
->base
)
2297 BUFF_FRONT (tokens_buff
) =
2298 (unsigned char *) &((cpp_token
**) BUFF_FRONT (tokens_buff
))[-1];
2301 /* Insert a token into the token buffer at the position pointed to by
2302 DEST. Note that the buffer is not enlarged so the previous token
2303 that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null,
2304 means -ftrack-macro-expansion is effect; it then points to where to
2305 insert the virtual location of TOKEN. TOKEN is the token to
2306 insert. VIRT_LOC is the virtual location of the token, i.e, the
2307 location possibly encoding its locus across macro expansion. If
2308 TOKEN is an argument of a function-like macro (inside a macro
2309 replacement list), PARM_DEF_LOC is the spelling location of the
2310 macro parameter that TOKEN is replacing, in the replacement list of
2311 the macro. If TOKEN is not an argument of a function-like macro or
2312 if it doesn't come from a macro expansion, then VIRT_LOC can just
2313 be set to the same value as PARM_DEF_LOC. If MAP is non null, it
2314 means TOKEN comes from a macro expansion and MAP is the macro map
2315 associated to the macro. MACRO_TOKEN_INDEX points to the index of
2316 the token in the macro map; it is not considered if MAP is NULL.
2318 Upon successful completion this function returns the a pointer to
2319 the position of the token coming right after the insertion
2321 static inline const cpp_token
**
2322 tokens_buff_put_token_to (const cpp_token
**dest
,
2323 source_location
*virt_loc_dest
,
2324 const cpp_token
*token
,
2325 source_location virt_loc
,
2326 source_location parm_def_loc
,
2327 const line_map_macro
*map
,
2328 unsigned int macro_token_index
)
2330 source_location macro_loc
= virt_loc
;
2331 const cpp_token
**result
;
2335 /* -ftrack-macro-expansion is on. */
2337 macro_loc
= linemap_add_macro_token (map
, macro_token_index
,
2338 virt_loc
, parm_def_loc
);
2339 *virt_loc_dest
= macro_loc
;
2347 /* Adds a token at the end of the tokens contained in BUFFER. Note
2348 that this function doesn't enlarge BUFFER when the number of tokens
2349 reaches BUFFER's size; it aborts in that situation.
2351 TOKEN is the token to append. VIRT_LOC is the virtual location of
2352 the token, i.e, the location possibly encoding its locus across
2353 macro expansion. If TOKEN is an argument of a function-like macro
2354 (inside a macro replacement list), PARM_DEF_LOC is the location of
2355 the macro parameter that TOKEN is replacing. If TOKEN doesn't come
2356 from a macro expansion, then VIRT_LOC can just be set to the same
2357 value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes
2358 from a macro expansion and MAP is the macro map associated to the
2359 macro. MACRO_TOKEN_INDEX points to the index of the token in the
2360 macro map; It is not considered if MAP is NULL. If VIRT_LOCS is
2361 non-null, it means -ftrack-macro-expansion is on; in which case
2362 this function adds the virtual location DEF_LOC to the VIRT_LOCS
2363 array, at the same index as the one of TOKEN in BUFFER. Upon
2364 successful completion this function returns the a pointer to the
2365 position of the token coming right after the insertion point. */
2366 static const cpp_token
**
2367 tokens_buff_add_token (_cpp_buff
*buffer
,
2368 source_location
*virt_locs
,
2369 const cpp_token
*token
,
2370 source_location virt_loc
,
2371 source_location parm_def_loc
,
2372 const line_map_macro
*map
,
2373 unsigned int macro_token_index
)
2375 const cpp_token
**result
;
2376 source_location
*virt_loc_dest
= NULL
;
2377 unsigned token_index
=
2378 (BUFF_FRONT (buffer
) - buffer
->base
) / sizeof (cpp_token
*);
2380 /* Abort if we pass the end the buffer. */
2381 if (BUFF_FRONT (buffer
) > BUFF_LIMIT (buffer
))
2384 if (virt_locs
!= NULL
)
2385 virt_loc_dest
= &virt_locs
[token_index
];
2388 tokens_buff_put_token_to ((const cpp_token
**) BUFF_FRONT (buffer
),
2389 virt_loc_dest
, token
, virt_loc
, parm_def_loc
,
2390 map
, macro_token_index
);
2392 BUFF_FRONT (buffer
) = (unsigned char *) result
;
2396 /* Allocate space for the function-like macro argument ARG to store
2397 the tokens resulting from the macro-expansion of the tokens that
2398 make up ARG itself. That space is allocated in ARG->expanded and
2399 needs to be freed using free. */
2401 alloc_expanded_arg_mem (cpp_reader
*pfile
, macro_arg
*arg
, size_t capacity
)
2403 gcc_checking_assert (arg
->expanded
== NULL
2404 && arg
->expanded_virt_locs
== NULL
);
2406 arg
->expanded
= XNEWVEC (const cpp_token
*, capacity
);
2407 if (CPP_OPTION (pfile
, track_macro_expansion
))
2408 arg
->expanded_virt_locs
= XNEWVEC (source_location
, capacity
);
2412 /* If necessary, enlarge ARG->expanded to so that it can contain SIZE
2415 ensure_expanded_arg_room (cpp_reader
*pfile
, macro_arg
*arg
,
2416 size_t size
, size_t *expanded_capacity
)
2418 if (size
<= *expanded_capacity
)
2424 XRESIZEVEC (const cpp_token
*, arg
->expanded
, size
);
2425 *expanded_capacity
= size
;
2427 if (CPP_OPTION (pfile
, track_macro_expansion
))
2429 if (arg
->expanded_virt_locs
== NULL
)
2430 arg
->expanded_virt_locs
= XNEWVEC (source_location
, size
);
2432 arg
->expanded_virt_locs
= XRESIZEVEC (source_location
,
2433 arg
->expanded_virt_locs
,
2438 /* Expand an argument ARG before replacing parameters in a
2439 function-like macro. This works by pushing a context with the
2440 argument's tokens, and then expanding that into a temporary buffer
2441 as if it were a normal part of the token stream. collect_args()
2442 has terminated the argument's tokens with a CPP_EOF so that we know
2443 when we have fully expanded the argument. */
2445 expand_arg (cpp_reader
*pfile
, macro_arg
*arg
)
2448 bool saved_warn_trad
;
2449 bool track_macro_exp_p
= CPP_OPTION (pfile
, track_macro_expansion
);
2452 || arg
->expanded
!= NULL
)
2455 /* Don't warn about funlike macros when pre-expanding. */
2456 saved_warn_trad
= CPP_WTRADITIONAL (pfile
);
2457 CPP_WTRADITIONAL (pfile
) = 0;
2459 /* Loop, reading in the tokens of the argument. */
2461 alloc_expanded_arg_mem (pfile
, arg
, capacity
);
2463 if (track_macro_exp_p
)
2464 push_extended_tokens_context (pfile
, NULL
, NULL
,
2469 push_ptoken_context (pfile
, NULL
, NULL
,
2470 arg
->first
, arg
->count
+ 1);
2474 const cpp_token
*token
;
2475 source_location location
;
2477 ensure_expanded_arg_room (pfile
, arg
, arg
->expanded_count
+ 1,
2480 token
= cpp_get_token_1 (pfile
, &location
);
2482 if (token
->type
== CPP_EOF
)
2485 set_arg_token (arg
, token
, location
,
2486 arg
->expanded_count
, MACRO_ARG_TOKEN_EXPANDED
,
2487 CPP_OPTION (pfile
, track_macro_expansion
));
2488 arg
->expanded_count
++;
2491 _cpp_pop_context (pfile
);
2493 CPP_WTRADITIONAL (pfile
) = saved_warn_trad
;
2496 /* Returns the macro associated to the current context if we are in
2497 the context a macro expansion, NULL otherwise. */
2498 static cpp_hashnode
*
2499 macro_of_context (cpp_context
*context
)
2501 if (context
== NULL
)
2504 return (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2505 ? context
->c
.mc
->macro_node
2509 /* Return TRUE iff we are expanding a macro or are about to start
2510 expanding one. If we are effectively expanding a macro, the
2511 function macro_of_context returns a pointer to the macro being
2514 in_macro_expansion_p (cpp_reader
*pfile
)
2519 return (pfile
->about_to_expand_macro_p
2520 || macro_of_context (pfile
->context
));
2523 /* Pop the current context off the stack, re-enabling the macro if the
2524 context represented a macro's replacement list. Initially the
2525 context structure was not freed so that we can re-use it later, but
2526 now we do free it to reduce peak memory consumption. */
2528 _cpp_pop_context (cpp_reader
*pfile
)
2530 cpp_context
*context
= pfile
->context
;
2532 /* We should not be popping the base context. */
2533 if (context
== &pfile
->base_context
)
2536 if (context
->c
.macro
)
2538 cpp_hashnode
*macro
;
2539 if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2541 macro_context
*mc
= context
->c
.mc
;
2542 macro
= mc
->macro_node
;
2543 /* If context->buff is set, it means the life time of tokens
2544 is bound to the life time of this context; so we must
2545 free the tokens; that means we must free the virtual
2546 locations of these tokens too. */
2547 if (context
->buff
&& mc
->virt_locs
)
2549 free (mc
->virt_locs
);
2550 mc
->virt_locs
= NULL
;
2553 context
->c
.mc
= NULL
;
2556 macro
= context
->c
.macro
;
2558 /* Beware that MACRO can be NULL in cases like when we are
2559 called from expand_arg. In those cases, a dummy context with
2560 tokens is pushed just for the purpose of walking them using
2561 cpp_get_token_1. In that case, no 'macro' field is set into
2562 the dummy context. */
2564 /* Several contiguous macro expansion contexts can be
2565 associated to the same macro; that means it's the same
2566 macro expansion that spans across all these (sub)
2567 contexts. So we should re-enable an expansion-disabled
2568 macro only when we are sure we are really out of that
2570 && macro_of_context (context
->prev
) != macro
)
2571 macro
->flags
&= ~NODE_DISABLED
;
2573 if (macro
== pfile
->top_most_macro_node
&& context
->prev
== NULL
)
2574 /* We are popping the context of the top-most macro node. */
2575 pfile
->top_most_macro_node
= NULL
;
2580 /* Decrease memory peak consumption by freeing the memory used
2582 _cpp_free_buff (context
->buff
);
2585 pfile
->context
= context
->prev
;
2586 /* decrease peak memory consumption by feeing the context. */
2587 pfile
->context
->next
= NULL
;
2591 /* Return TRUE if we reached the end of the set of tokens stored in
2592 CONTEXT, FALSE otherwise. */
2594 reached_end_of_context (cpp_context
*context
)
2596 if (context
->tokens_kind
== TOKENS_KIND_DIRECT
)
2597 return FIRST (context
).token
== LAST (context
).token
;
2598 else if (context
->tokens_kind
== TOKENS_KIND_INDIRECT
2599 || context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2600 return FIRST (context
).ptoken
== LAST (context
).ptoken
;
2605 /* Consume the next token contained in the current context of PFILE,
2606 and return it in *TOKEN. It's "full location" is returned in
2607 *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
2608 means the location encoding the locus of the token across macro
2609 expansion; otherwise it's just is the "normal" location of the
2610 token which (*TOKEN)->src_loc. */
2612 consume_next_token_from_context (cpp_reader
*pfile
,
2613 const cpp_token
** token
,
2614 source_location
*location
)
2616 cpp_context
*c
= pfile
->context
;
2618 if ((c
)->tokens_kind
== TOKENS_KIND_DIRECT
)
2620 *token
= FIRST (c
).token
;
2621 *location
= (*token
)->src_loc
;
2624 else if ((c
)->tokens_kind
== TOKENS_KIND_INDIRECT
)
2626 *token
= *FIRST (c
).ptoken
;
2627 *location
= (*token
)->src_loc
;
2630 else if ((c
)->tokens_kind
== TOKENS_KIND_EXTENDED
)
2632 macro_context
*m
= c
->c
.mc
;
2633 *token
= *FIRST (c
).ptoken
;
2636 *location
= *m
->cur_virt_loc
;
2640 *location
= (*token
)->src_loc
;
2647 /* In the traditional mode of the preprocessor, if we are currently in
2648 a directive, the location of a token must be the location of the
2649 start of the directive line. This function returns the proper
2650 location if we are in the traditional mode, and just returns
2651 LOCATION otherwise. */
2653 static inline source_location
2654 maybe_adjust_loc_for_trad_cpp (cpp_reader
*pfile
, source_location location
)
2656 if (CPP_OPTION (pfile
, traditional
))
2658 if (pfile
->state
.in_directive
)
2659 return pfile
->directive_line
;
2664 /* Routine to get a token as well as its location.
2666 Macro expansions and directives are transparently handled,
2667 including entering included files. Thus tokens are post-macro
2668 expansion, and after any intervening directives. External callers
2669 see CPP_EOF only at EOF. Internal callers also see it when meeting
2670 a directive inside a macro call, when at the end of a directive and
2671 state.in_directive is still 1, and at the end of argument
2674 LOC is an out parameter; *LOC is set to the location "as expected
2675 by the user". Please read the comment of
2676 cpp_get_token_with_location to learn more about the meaning of this
2678 static const cpp_token
*
2679 cpp_get_token_1 (cpp_reader
*pfile
, source_location
*location
)
2681 const cpp_token
*result
;
2682 /* This token is a virtual token that either encodes a location
2683 related to macro expansion or a spelling location. */
2684 source_location virt_loc
= 0;
2685 /* pfile->about_to_expand_macro_p can be overriden by indirect calls
2686 to functions that push macro contexts. So let's save it so that
2687 we can restore it when we are about to leave this routine. */
2688 bool saved_about_to_expand_macro
= pfile
->about_to_expand_macro_p
;
2693 cpp_context
*context
= pfile
->context
;
2695 /* Context->prev == 0 <=> base context. */
2698 result
= _cpp_lex_token (pfile
);
2699 virt_loc
= result
->src_loc
;
2701 else if (!reached_end_of_context (context
))
2703 consume_next_token_from_context (pfile
, &result
,
2705 if (result
->flags
& PASTE_LEFT
)
2707 paste_all_tokens (pfile
, result
);
2708 if (pfile
->state
.in_directive
)
2710 result
= padding_token (pfile
, result
);
2716 if (pfile
->context
->c
.macro
)
2717 ++num_expanded_macros_counter
;
2718 _cpp_pop_context (pfile
);
2719 if (pfile
->state
.in_directive
)
2721 result
= &pfile
->avoid_paste
;
2725 if (pfile
->state
.in_directive
&& result
->type
== CPP_COMMENT
)
2728 if (result
->type
!= CPP_NAME
)
2731 node
= result
->val
.node
.node
;
2733 if (node
->type
!= NT_MACRO
|| (result
->flags
& NO_EXPAND
))
2736 if (!(node
->flags
& NODE_DISABLED
))
2739 /* If not in a macro context, and we're going to start an
2740 expansion, record the location and the top level macro
2741 about to be expanded. */
2742 if (!in_macro_expansion_p (pfile
))
2744 pfile
->invocation_location
= result
->src_loc
;
2745 pfile
->top_most_macro_node
= node
;
2747 if (pfile
->state
.prevent_expansion
)
2750 /* Conditional macros require that a predicate be evaluated
2752 if ((node
->flags
& NODE_CONDITIONAL
) != 0)
2754 if (pfile
->cb
.macro_to_expand
)
2756 bool whitespace_after
;
2757 const cpp_token
*peek_tok
= cpp_peek_token (pfile
, 0);
2759 whitespace_after
= (peek_tok
->type
== CPP_PADDING
2760 || (peek_tok
->flags
& PREV_WHITE
));
2761 node
= pfile
->cb
.macro_to_expand (pfile
, result
);
2763 ret
= enter_macro_context (pfile
, node
, result
,
2765 else if (whitespace_after
)
2767 /* If macro_to_expand hook returned NULL and it
2768 ate some tokens, see if we don't need to add
2769 a padding token in between this and the
2771 peek_tok
= cpp_peek_token (pfile
, 0);
2772 if (peek_tok
->type
!= CPP_PADDING
2773 && (peek_tok
->flags
& PREV_WHITE
) == 0)
2774 _cpp_push_token_context (pfile
, NULL
,
2775 padding_token (pfile
,
2781 ret
= enter_macro_context (pfile
, node
, result
,
2785 if (pfile
->state
.in_directive
|| ret
== 2)
2787 result
= padding_token (pfile
, result
);
2793 /* Flag this token as always unexpandable. FIXME: move this
2794 to collect_args()?. */
2795 cpp_token
*t
= _cpp_temp_token (pfile
);
2796 t
->type
= result
->type
;
2797 t
->flags
= result
->flags
| NO_EXPAND
;
2798 t
->val
= result
->val
;
2806 if (location
!= NULL
)
2809 virt_loc
= result
->src_loc
;
2810 *location
= virt_loc
;
2812 if (!CPP_OPTION (pfile
, track_macro_expansion
)
2813 && macro_of_context (pfile
->context
) != NULL
)
2814 /* We are in a macro expansion context, are not tracking
2815 virtual location, but were asked to report the location
2816 of the expansion point of the macro being expanded. */
2817 *location
= pfile
->invocation_location
;
2819 *location
= maybe_adjust_loc_for_trad_cpp (pfile
, *location
);
2822 pfile
->about_to_expand_macro_p
= saved_about_to_expand_macro
;
2826 /* External routine to get a token. Also used nearly everywhere
2827 internally, except for places where we know we can safely call
2828 _cpp_lex_token directly, such as lexing a directive name.
2830 Macro expansions and directives are transparently handled,
2831 including entering included files. Thus tokens are post-macro
2832 expansion, and after any intervening directives. External callers
2833 see CPP_EOF only at EOF. Internal callers also see it when meeting
2834 a directive inside a macro call, when at the end of a directive and
2835 state.in_directive is still 1, and at the end of argument
2838 cpp_get_token (cpp_reader
*pfile
)
2840 return cpp_get_token_1 (pfile
, NULL
);
2843 /* Like cpp_get_token, but also returns a virtual token location
2844 separate from the spelling location carried by the returned token.
2846 LOC is an out parameter; *LOC is set to the location "as expected
2847 by the user". This matters when a token results from macro
2848 expansion; in that case the token's spelling location indicates the
2849 locus of the token in the definition of the macro but *LOC
2850 virtually encodes all the other meaningful locuses associated to
2853 What? virtual location? Yes, virtual location.
2855 If the token results from macro expansion and if macro expansion
2856 location tracking is enabled its virtual location encodes (at the
2859 - the spelling location of the token
2861 - the locus of the macro expansion point
2863 - the locus of the point where the token got instantiated as part
2864 of the macro expansion process.
2866 You have to use the linemap API to get the locus you are interested
2867 in from a given virtual location.
2869 Note however that virtual locations are not necessarily ordered for
2870 relations '<' and '>'. One must use the function
2871 linemap_location_before_p instead of using the relational operator
2874 If macro expansion tracking is off and if the token results from
2875 macro expansion the virtual location is the expansion point of the
2876 macro that got expanded.
2878 When the token doesn't result from macro expansion, the virtual
2879 location is just the same thing as its spelling location. */
2882 cpp_get_token_with_location (cpp_reader
*pfile
, source_location
*loc
)
2884 return cpp_get_token_1 (pfile
, loc
);
2887 /* Returns true if we're expanding an object-like macro that was
2888 defined in a system header. Just checks the macro at the top of
2889 the stack. Used for diagnostic suppression. */
2891 cpp_sys_macro_p (cpp_reader
*pfile
)
2893 cpp_hashnode
*node
= NULL
;
2895 if (pfile
->context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2896 node
= pfile
->context
->c
.mc
->macro_node
;
2898 node
= pfile
->context
->c
.macro
;
2900 return node
&& node
->value
.macro
&& node
->value
.macro
->syshdr
;
2903 /* Read each token in, until end of the current file. Directives are
2904 transparently processed. */
2906 cpp_scan_nooutput (cpp_reader
*pfile
)
2908 /* Request a CPP_EOF token at the end of this file, rather than
2909 transparently continuing with the including file. */
2910 pfile
->buffer
->return_at_eof
= true;
2912 pfile
->state
.discarding_output
++;
2913 pfile
->state
.prevent_expansion
++;
2915 if (CPP_OPTION (pfile
, traditional
))
2916 while (_cpp_read_logical_line_trad (pfile
))
2919 while (cpp_get_token (pfile
)->type
!= CPP_EOF
)
2922 pfile
->state
.discarding_output
--;
2923 pfile
->state
.prevent_expansion
--;
2926 /* Step back one or more tokens obtained from the lexer. */
2928 _cpp_backup_tokens_direct (cpp_reader
*pfile
, unsigned int count
)
2930 pfile
->lookaheads
+= count
;
2934 if (pfile
->cur_token
== pfile
->cur_run
->base
2935 /* Possible with -fpreprocessed and no leading #line. */
2936 && pfile
->cur_run
->prev
!= NULL
)
2938 pfile
->cur_run
= pfile
->cur_run
->prev
;
2939 pfile
->cur_token
= pfile
->cur_run
->limit
;
2944 /* Step back one (or more) tokens. Can only step back more than 1 if
2945 they are from the lexer, and not from macro expansion. */
2947 _cpp_backup_tokens (cpp_reader
*pfile
, unsigned int count
)
2949 if (pfile
->context
->prev
== NULL
)
2950 _cpp_backup_tokens_direct (pfile
, count
);
2955 if (pfile
->context
->tokens_kind
== TOKENS_KIND_DIRECT
)
2956 FIRST (pfile
->context
).token
--;
2957 else if (pfile
->context
->tokens_kind
== TOKENS_KIND_INDIRECT
)
2958 FIRST (pfile
->context
).ptoken
--;
2959 else if (pfile
->context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2961 FIRST (pfile
->context
).ptoken
--;
2962 if (pfile
->context
->c
.macro
)
2964 macro_context
*m
= pfile
->context
->c
.mc
;
2966 gcc_checking_assert (m
->cur_virt_loc
>= m
->virt_locs
);
2976 /* #define directive parsing and handling. */
2978 /* Returns nonzero if a macro redefinition warning is required. */
2980 warn_of_redefinition (cpp_reader
*pfile
, cpp_hashnode
*node
,
2981 const cpp_macro
*macro2
)
2985 /* Some redefinitions need to be warned about regardless. */
2986 if (node
->flags
& NODE_WARN
)
2989 /* Suppress warnings for builtins that lack the NODE_WARN flag,
2990 unless Wbuiltin-macro-redefined. */
2991 if (cpp_builtin_macro_p (node
))
2992 return CPP_OPTION (pfile
, warn_builtin_macro_redefined
);
2994 /* Redefinitions of conditional (context-sensitive) macros, on
2995 the other hand, must be allowed silently. */
2996 if (node
->flags
& NODE_CONDITIONAL
)
2999 cpp_macro
*macro1
= node
->value
.macro
;
3002 /* We don't want to mark MACRO as used, but do need to finalize
3004 pfile
->cb
.user_lazy_macro (pfile
, macro1
, macro1
->lazy
- 1);
3008 /* Redefinition of a macro is allowed if and only if the old and new
3009 definitions are the same. (6.10.3 paragraph 2). */
3011 /* Don't check count here as it can be different in valid
3012 traditional redefinitions with just whitespace differences. */
3013 if (macro1
->paramc
!= macro2
->paramc
3014 || macro1
->fun_like
!= macro2
->fun_like
3015 || macro1
->variadic
!= macro2
->variadic
)
3018 /* Check parameter spellings. */
3019 for (i
= 0; i
< macro1
->paramc
; i
++)
3020 if (macro1
->params
[i
] != macro2
->params
[i
])
3023 /* Check the replacement text or tokens. */
3024 if (CPP_OPTION (pfile
, traditional
))
3025 return _cpp_expansions_different_trad (macro1
, macro2
);
3027 if (macro1
->count
!= macro2
->count
)
3030 for (i
= 0; i
< macro1
->count
; i
++)
3031 if (!_cpp_equiv_tokens (¯o1
->exp
.tokens
[i
], ¯o2
->exp
.tokens
[i
]))
3037 /* Free the definition of hashnode H. */
3039 _cpp_free_definition (cpp_hashnode
*h
)
3041 /* Macros and assertions no longer have anything to free. */
3043 /* Clear builtin flag in case of redefinition. */
3044 h
->flags
&= ~(NODE_BUILTIN
| NODE_DISABLED
| NODE_USED
);
3047 /* Save parameter NODE (spelling SPELLING) to the parameter list of
3048 macro MACRO. Returns true on success, false on failure. */
3050 _cpp_save_parameter (cpp_reader
*pfile
, unsigned n
, cpp_hashnode
*node
,
3051 cpp_hashnode
*spelling
)
3053 /* Constraint 6.10.3.6 - duplicate parameter names. */
3054 if (node
->flags
& NODE_MACRO_ARG
)
3056 cpp_error (pfile
, CPP_DL_ERROR
, "duplicate macro parameter \"%s\"",
3061 unsigned len
= (n
+ 1) * sizeof (struct macro_arg_saved_data
);
3062 if (len
> pfile
->macro_buffer_len
)
3065 = XRESIZEVEC (unsigned char, pfile
->macro_buffer
, len
);
3066 pfile
->macro_buffer_len
= len
;
3069 macro_arg_saved_data
*saved
= (macro_arg_saved_data
*)pfile
->macro_buffer
;
3070 saved
[n
].canonical_node
= node
;
3071 saved
[n
].value
= node
->value
;
3073 void *base
= _cpp_reserve_room (pfile
, n
* sizeof (cpp_hashnode
*),
3074 sizeof (cpp_hashnode
*));
3075 ((cpp_hashnode
**)base
)[n
] = spelling
;
3077 /* Morph into a macro arg. */
3078 node
->flags
|= NODE_MACRO_ARG
;
3079 /* Index is 1 based. */
3080 node
->value
.arg_index
= n
+ 1;
3085 /* Restore the parameters to their previous state. */
3087 _cpp_unsave_parameters (cpp_reader
*pfile
, unsigned n
)
3089 /* Clear the fast argument lookup indices. */
3092 struct macro_arg_saved_data
*save
=
3093 &((struct macro_arg_saved_data
*) pfile
->macro_buffer
)[n
];
3095 struct cpp_hashnode
*node
= save
->canonical_node
;
3096 node
->value
= save
->value
;
3097 node
->flags
&= ~NODE_MACRO_ARG
;
3101 /* Check the syntax of the parameters in a MACRO definition. Return
3102 false on failure. Set *N_PTR and *VARADIC_PTR as appropriate.
3104 '(' parm-list ',' last-parm ')'
3113 parse_params (cpp_reader
*pfile
, unsigned *n_ptr
, bool *varadic_ptr
)
3115 unsigned nparms
= 0;
3118 for (bool prev_ident
= false;;)
3120 const cpp_token
*token
= _cpp_lex_token (pfile
);
3122 switch (token
->type
)
3125 /* Allow/ignore comments in parameter lists if we are
3126 preserving comments in macro expansions. */
3127 if (!CPP_OPTION (pfile
, discard_comments_in_macro_exp
))
3134 const char *const msgs
[5] =
3136 N_("expected parameter name, found \"%s\""),
3137 N_("expected ',' or ')', found \"%s\""),
3138 N_("expected parameter name before end of line"),
3139 N_("expected ')' before end of line"),
3140 N_("expected ')' after \"...\"")
3142 unsigned ix
= prev_ident
;
3143 const unsigned char *as_text
= NULL
;
3146 else if (token
->type
== CPP_EOF
)
3149 as_text
= cpp_token_as_text (pfile
, token
);
3150 cpp_error (pfile
, CPP_DL_ERROR
, msgs
[ix
], as_text
);
3155 if (prev_ident
|| *varadic_ptr
)
3159 if (!_cpp_save_parameter (pfile
, nparms
, token
->val
.node
.node
,
3160 token
->val
.node
.spelling
))
3165 case CPP_CLOSE_PAREN
:
3166 if (prev_ident
|| !nparms
|| *varadic_ptr
)
3174 if (!prev_ident
|| *varadic_ptr
)
3182 *varadic_ptr
= true;
3185 /* An ISO bare ellipsis. */
3186 _cpp_save_parameter (pfile
, nparms
,
3187 pfile
->spec_nodes
.n__VA_ARGS__
,
3188 pfile
->spec_nodes
.n__VA_ARGS__
);
3190 pfile
->state
.va_args_ok
= 1;
3191 if (! CPP_OPTION (pfile
, c99
)
3192 && CPP_OPTION (pfile
, cpp_pedantic
)
3193 && CPP_OPTION (pfile
, warn_variadic_macros
))
3195 (pfile
, CPP_W_VARIADIC_MACROS
,
3196 CPP_OPTION (pfile
, cplusplus
)
3197 ? N_("anonymous variadic macros were introduced in C++11")
3198 : N_("anonymous variadic macros were introduced in C99"));
3199 else if (CPP_OPTION (pfile
, cpp_warn_c90_c99_compat
) > 0
3200 && ! CPP_OPTION (pfile
, cplusplus
))
3201 cpp_error (pfile
, CPP_DL_WARNING
,
3202 "anonymous variadic macros were introduced in C99");
3204 else if (CPP_OPTION (pfile
, cpp_pedantic
)
3205 && CPP_OPTION (pfile
, warn_variadic_macros
))
3206 cpp_pedwarning (pfile
, CPP_W_VARIADIC_MACROS
,
3207 CPP_OPTION (pfile
, cplusplus
)
3208 ? N_("ISO C++ does not permit named variadic macros")
3209 : N_("ISO C does not permit named variadic macros"));
3220 /* Lex a token from the expansion of MACRO, but mark parameters as we
3221 find them and warn of traditional stringification. */
3223 lex_expansion_token (cpp_reader
*pfile
, cpp_macro
*macro
)
3225 macro
= (cpp_macro
*)_cpp_reserve_room (pfile
,
3226 sizeof (cpp_macro
) - sizeof (cpp_token
)
3227 + macro
->count
* sizeof (cpp_token
),
3228 sizeof (cpp_token
));
3229 cpp_token
*saved_cur_token
= pfile
->cur_token
;
3230 pfile
->cur_token
= ¯o
->exp
.tokens
[macro
->count
];
3231 cpp_token
*token
= _cpp_lex_direct (pfile
);
3232 pfile
->cur_token
= saved_cur_token
;
3234 /* Is this a parameter? */
3235 if (token
->type
== CPP_NAME
3236 && (token
->val
.node
.node
->flags
& NODE_MACRO_ARG
) != 0)
3238 cpp_hashnode
*spelling
= token
->val
.node
.spelling
;
3239 token
->type
= CPP_MACRO_ARG
;
3240 token
->val
.macro_arg
.arg_no
= token
->val
.node
.node
->value
.arg_index
;
3241 token
->val
.macro_arg
.spelling
= spelling
;
3243 else if (CPP_WTRADITIONAL (pfile
) && macro
->paramc
> 0
3244 && (token
->type
== CPP_STRING
|| token
->type
== CPP_CHAR
))
3245 check_trad_stringification (pfile
, macro
, &token
->val
.str
);
3251 create_iso_definition (cpp_reader
*pfile
)
3253 bool following_paste_op
= false;
3254 const char *paste_op_error_msg
=
3255 N_("'##' cannot appear at either end of a macro expansion");
3256 unsigned int num_extra_tokens
= 0;
3257 unsigned nparms
= 0;
3258 cpp_hashnode
**params
= NULL
;
3259 bool varadic
= false;
3261 cpp_macro
*macro
= NULL
;
3263 /* Look at the first token, to see if this is a function-like
3266 cpp_token
*saved_cur_token
= pfile
->cur_token
;
3267 pfile
->cur_token
= &first
;
3268 cpp_token
*token
= _cpp_lex_direct (pfile
);
3269 pfile
->cur_token
= saved_cur_token
;
3271 if (token
->flags
& PREV_WHITE
)
3272 /* Preceeded by space, must be part of expansion. */;
3273 else if (token
->type
== CPP_OPEN_PAREN
)
3275 /* An open-paren, get a parameter list. */
3276 if (!parse_params (pfile
, &nparms
, &varadic
))
3279 params
= (cpp_hashnode
**)_cpp_commit_buff
3280 (pfile
, sizeof (cpp_hashnode
*) * nparms
);
3283 else if (token
->type
!= CPP_EOF
3284 && !(token
->type
== CPP_COMMENT
3285 && ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
)))
3287 /* While ISO C99 requires whitespace before replacement text
3288 in a macro definition, ISO C90 with TC1 allows characters
3289 from the basic source character set there. */
3290 if (CPP_OPTION (pfile
, c99
))
3291 cpp_error (pfile
, CPP_DL_PEDWARN
,
3292 CPP_OPTION (pfile
, cplusplus
)
3293 ? N_("ISO C++11 requires whitespace after the macro name")
3294 : N_("ISO C99 requires whitespace after the macro name"));
3297 int warntype
= CPP_DL_WARNING
;
3298 switch (token
->type
)
3302 case CPP_OBJC_STRING
:
3303 /* '@' is not in basic character set. */
3304 warntype
= CPP_DL_PEDWARN
;
3307 /* Basic character set sans letters, digits and _. */
3308 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
3309 token
->val
.str
.text
[0]) == NULL
)
3310 warntype
= CPP_DL_PEDWARN
;
3313 /* All other tokens start with a character from basic
3317 cpp_error (pfile
, warntype
,
3318 "missing whitespace after the macro name");
3322 macro
= _cpp_new_macro (pfile
, cmk_macro
,
3323 _cpp_reserve_room (pfile
, 0, sizeof (cpp_macro
)));
3327 macro
->variadic
= varadic
;
3328 macro
->paramc
= nparms
;
3329 macro
->params
= params
;
3330 macro
->fun_like
= true;
3334 /* Preserve the token we peeked, there is already a single slot for it. */
3335 macro
->exp
.tokens
[0] = *token
;
3336 token
= ¯o
->exp
.tokens
[0];
3340 for (vaopt_state
vaopt_tracker (pfile
, macro
->variadic
, true);; token
= NULL
)
3344 macro
= lex_expansion_token (pfile
, macro
);
3345 token
= ¯o
->exp
.tokens
[macro
->count
++];
3348 /* Check the stringifying # constraint 6.10.3.2.1 of
3349 function-like macros when lexing the subsequent token. */
3350 if (macro
->count
> 1 && token
[-1].type
== CPP_HASH
&& macro
->fun_like
)
3352 if (token
->type
== CPP_MACRO_ARG
)
3354 if (token
->flags
& PREV_WHITE
)
3355 token
->flags
|= SP_PREV_WHITE
;
3356 if (token
[-1].flags
& DIGRAPH
)
3357 token
->flags
|= SP_DIGRAPH
;
3358 token
->flags
&= ~PREV_WHITE
;
3359 token
->flags
|= STRINGIFY_ARG
;
3360 token
->flags
|= token
[-1].flags
& PREV_WHITE
;
3361 token
[-1] = token
[0];
3364 /* Let assembler get away with murder. */
3365 else if (CPP_OPTION (pfile
, lang
) != CLK_ASM
)
3367 cpp_error (pfile
, CPP_DL_ERROR
,
3368 "'#' is not followed by a macro parameter");
3373 if (token
->type
== CPP_EOF
)
3375 /* Paste operator constraint 6.10.3.3.1:
3376 Token-paste ##, can appear in both object-like and
3377 function-like macros, but not at the end. */
3378 if (following_paste_op
)
3380 cpp_error (pfile
, CPP_DL_ERROR
, paste_op_error_msg
);
3383 if (!vaopt_tracker
.completed ())
3388 /* Paste operator constraint 6.10.3.3.1. */
3389 if (token
->type
== CPP_PASTE
)
3391 /* Token-paste ##, can appear in both object-like and
3392 function-like macros, but not at the beginning. */
3393 if (macro
->count
== 1)
3395 cpp_error (pfile
, CPP_DL_ERROR
, paste_op_error_msg
);
3399 if (following_paste_op
)
3401 /* Consecutive paste operators. This one will be moved
3404 token
->val
.token_no
= macro
->count
- 1;
3408 /* Drop the paste operator. */
3410 token
[-1].flags
|= PASTE_LEFT
;
3411 if (token
->flags
& DIGRAPH
)
3412 token
[-1].flags
|= SP_DIGRAPH
;
3413 if (token
->flags
& PREV_WHITE
)
3414 token
[-1].flags
|= SP_PREV_WHITE
;
3416 following_paste_op
= true;
3419 following_paste_op
= false;
3421 if (vaopt_tracker
.update (token
) == vaopt_state::ERROR
)
3425 /* We're committed to winning now. */
3428 /* Don't count the CPP_EOF. */
3431 macro
= (cpp_macro
*)_cpp_commit_buff
3432 (pfile
, sizeof (cpp_macro
) - sizeof (cpp_token
)
3433 + sizeof (cpp_token
) * macro
->count
);
3435 /* Clear whitespace on first token for warn_of_redefinition(). */
3437 macro
->exp
.tokens
[0].flags
&= ~PREV_WHITE
;
3439 if (num_extra_tokens
)
3441 /* Place second and subsequent ## or %:%: tokens in sequences of
3442 consecutive such tokens at the end of the list to preserve
3443 information about where they appear, how they are spelt and
3444 whether they are preceded by whitespace without otherwise
3445 interfering with macro expansion. Remember, this is
3446 extremely rare, so efficiency is not a priority. */
3447 cpp_token
*temp
= (cpp_token
*)_cpp_reserve_room
3448 (pfile
, 0, num_extra_tokens
* sizeof (cpp_token
));
3449 unsigned extra_ix
= 0, norm_ix
= 0;
3450 cpp_token
*exp
= macro
->exp
.tokens
;
3451 for (unsigned ix
= 0; ix
!= macro
->count
; ix
++)
3452 if (exp
[ix
].type
== CPP_PASTE
)
3453 temp
[extra_ix
++] = exp
[ix
];
3455 exp
[norm_ix
++] = exp
[ix
];
3456 memcpy (&exp
[norm_ix
], temp
, num_extra_tokens
* sizeof (cpp_token
));
3458 /* Record there are extra tokens. */
3459 macro
->extra_tokens
= 1;
3463 pfile
->state
.va_args_ok
= 0;
3464 _cpp_unsave_parameters (pfile
, nparms
);
3466 return ok
? macro
: NULL
;
3470 _cpp_new_macro (cpp_reader
*pfile
, cpp_macro_kind kind
, void *placement
)
3472 cpp_macro
*macro
= (cpp_macro
*) placement
;
3474 macro
->line
= pfile
->directive_line
;
3478 macro
->variadic
= 0;
3479 macro
->used
= !CPP_OPTION (pfile
, warn_unused_macros
);
3481 macro
->fun_like
= 0;
3482 macro
->extra_tokens
= 0;
3483 /* To suppress some diagnostics. */
3484 macro
->syshdr
= pfile
->buffer
&& pfile
->buffer
->sysp
!= 0;
3491 /* Parse a macro and save its expansion. Returns nonzero on success. */
3493 _cpp_create_definition (cpp_reader
*pfile
, cpp_hashnode
*node
)
3497 if (CPP_OPTION (pfile
, traditional
))
3498 macro
= _cpp_create_trad_definition (pfile
);
3500 macro
= create_iso_definition (pfile
);
3505 if (cpp_macro_p (node
))
3507 if (CPP_OPTION (pfile
, warn_unused_macros
))
3508 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
3510 if (warn_of_redefinition (pfile
, node
, macro
))
3513 = (cpp_builtin_macro_p (node
) && !(node
->flags
& NODE_WARN
))
3514 ? CPP_W_BUILTIN_MACRO_REDEFINED
: CPP_W_NONE
;
3517 cpp_pedwarning_with_line (pfile
, reason
,
3518 pfile
->directive_line
, 0,
3519 "\"%s\" redefined", NODE_NAME (node
));
3521 if (warned
&& cpp_user_macro_p (node
))
3522 cpp_error_with_line (pfile
, CPP_DL_NOTE
,
3523 node
->value
.macro
->line
, 0,
3524 "this is the location of the previous definition");
3526 _cpp_free_definition (node
);
3529 /* Enter definition in hash table. */
3530 node
->type
= NT_MACRO
;
3531 node
->value
.macro
= macro
;
3532 if (! ustrncmp (NODE_NAME (node
), DSC ("__STDC_"))
3533 && ustrcmp (NODE_NAME (node
), (const uchar
*) "__STDC_FORMAT_MACROS")
3534 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
3535 in the C standard, as something that one must use in C++.
3536 However DR#593 and C++11 indicate that they play no role in C++.
3537 We special-case them anyway. */
3538 && ustrcmp (NODE_NAME (node
), (const uchar
*) "__STDC_LIMIT_MACROS")
3539 && ustrcmp (NODE_NAME (node
), (const uchar
*) "__STDC_CONSTANT_MACROS"))
3540 node
->flags
|= NODE_WARN
;
3542 /* If user defines one of the conditional macros, remove the
3544 node
->flags
&= ~NODE_CONDITIONAL
;
3550 cpp_define_lazily (cpp_reader
*pfile
, cpp_hashnode
*node
, unsigned num
)
3552 cpp_macro
*macro
= node
->value
.macro
;
3554 gcc_checking_assert (pfile
->cb
.user_lazy_macro
&& macro
&& num
< 255);
3556 macro
->lazy
= num
+ 1;
3559 /* Notify the use of NODE in a macro-aware context (i.e. expanding it,
3560 or testing its existance). Also applies any lazy definition. */
3563 _cpp_notify_macro_use (cpp_reader
*pfile
, cpp_hashnode
*node
)
3565 node
->flags
|= NODE_USED
;
3569 if (!(node
->flags
& NODE_BUILTIN
))
3571 cpp_macro
*macro
= node
->value
.macro
;
3574 pfile
->cb
.user_lazy_macro (pfile
, macro
, macro
->lazy
- 1);
3579 if (pfile
->cb
.used_define
)
3580 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
3584 if (pfile
->cb
.used_undef
)
3585 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
3593 /* Warn if a token in STRING matches one of a function-like MACRO's
3596 check_trad_stringification (cpp_reader
*pfile
, const cpp_macro
*macro
,
3597 const cpp_string
*string
)
3599 unsigned int i
, len
;
3600 const uchar
*p
, *q
, *limit
;
3602 /* Loop over the string. */
3603 limit
= string
->text
+ string
->len
- 1;
3604 for (p
= string
->text
+ 1; p
< limit
; p
= q
)
3606 /* Find the start of an identifier. */
3607 while (p
< limit
&& !is_idstart (*p
))
3610 /* Find the end of the identifier. */
3612 while (q
< limit
&& is_idchar (*q
))
3617 /* Loop over the function macro arguments to see if the
3618 identifier inside the string matches one of them. */
3619 for (i
= 0; i
< macro
->paramc
; i
++)
3621 const cpp_hashnode
*node
= macro
->params
[i
];
3623 if (NODE_LEN (node
) == len
3624 && !memcmp (p
, NODE_NAME (node
), len
))
3626 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
3627 "macro argument \"%s\" would be stringified in traditional C",
3635 /* Returns true of NODE is a function-like macro. */
3637 cpp_fun_like_macro_p (cpp_hashnode
*node
)
3639 return (node
->type
== NT_MACRO
3640 && (node
->flags
& (NODE_BUILTIN
| NODE_MACRO_ARG
)) == 0
3641 && node
->value
.macro
->fun_like
);
3644 /* Returns the name, arguments and expansion of a macro, in a format
3645 suitable to be read back in again, and therefore also for DWARF 2
3646 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
3647 Caller is expected to generate the "#define" bit if needed. The
3648 returned text is temporary, and automatically freed later. */
3649 const unsigned char *
3650 cpp_macro_definition (cpp_reader
*pfile
, cpp_hashnode
*node
)
3652 unsigned int i
, len
;
3653 unsigned char *buffer
;
3655 gcc_checking_assert (cpp_user_macro_p (node
));
3657 const cpp_macro
*macro
= node
->value
.macro
;
3659 /* Calculate length. */
3660 len
= NODE_LEN (node
) * 10 + 2; /* ' ' and NUL. */
3661 if (macro
->fun_like
)
3663 len
+= 4; /* "()" plus possible final ".." of named
3664 varargs (we have + 1 below). */
3665 for (i
= 0; i
< macro
->paramc
; i
++)
3666 len
+= NODE_LEN (macro
->params
[i
]) + 1; /* "," */
3669 /* This should match below where we fill in the buffer. */
3670 if (CPP_OPTION (pfile
, traditional
))
3671 len
+= _cpp_replacement_text_len (macro
);
3674 unsigned int count
= macro_real_token_count (macro
);
3675 for (i
= 0; i
< count
; i
++)
3677 const cpp_token
*token
= ¯o
->exp
.tokens
[i
];
3679 if (token
->type
== CPP_MACRO_ARG
)
3680 len
+= NODE_LEN (token
->val
.macro_arg
.spelling
);
3682 len
+= cpp_token_len (token
);
3684 if (token
->flags
& STRINGIFY_ARG
)
3686 if (token
->flags
& PASTE_LEFT
)
3687 len
+= 3; /* " ##" */
3688 if (token
->flags
& PREV_WHITE
)
3693 if (len
> pfile
->macro_buffer_len
)
3695 pfile
->macro_buffer
= XRESIZEVEC (unsigned char,
3696 pfile
->macro_buffer
, len
);
3697 pfile
->macro_buffer_len
= len
;
3700 /* Fill in the buffer. Start with the macro name. */
3701 buffer
= pfile
->macro_buffer
;
3702 buffer
= _cpp_spell_ident_ucns (buffer
, node
);
3704 /* Parameter names. */
3705 if (macro
->fun_like
)
3708 for (i
= 0; i
< macro
->paramc
; i
++)
3710 cpp_hashnode
*param
= macro
->params
[i
];
3712 if (param
!= pfile
->spec_nodes
.n__VA_ARGS__
)
3714 memcpy (buffer
, NODE_NAME (param
), NODE_LEN (param
));
3715 buffer
+= NODE_LEN (param
);
3718 if (i
+ 1 < macro
->paramc
)
3719 /* Don't emit a space after the comma here; we're trying
3720 to emit a Dwarf-friendly definition, and the Dwarf spec
3721 forbids spaces in the argument list. */
3723 else if (macro
->variadic
)
3724 *buffer
++ = '.', *buffer
++ = '.', *buffer
++ = '.';
3729 /* The Dwarf spec requires a space after the macro name, even if the
3730 definition is the empty string. */
3733 if (CPP_OPTION (pfile
, traditional
))
3734 buffer
= _cpp_copy_replacement_text (macro
, buffer
);
3735 else if (macro
->count
)
3736 /* Expansion tokens. */
3738 unsigned int count
= macro_real_token_count (macro
);
3739 for (i
= 0; i
< count
; i
++)
3741 const cpp_token
*token
= ¯o
->exp
.tokens
[i
];
3743 if (token
->flags
& PREV_WHITE
)
3745 if (token
->flags
& STRINGIFY_ARG
)
3748 if (token
->type
== CPP_MACRO_ARG
)
3751 NODE_NAME (token
->val
.macro_arg
.spelling
),
3752 NODE_LEN (token
->val
.macro_arg
.spelling
));
3753 buffer
+= NODE_LEN (token
->val
.macro_arg
.spelling
);
3756 buffer
= cpp_spell_token (pfile
, token
, buffer
, true);
3758 if (token
->flags
& PASTE_LEFT
)
3763 /* Next has PREV_WHITE; see _cpp_create_definition. */
3769 return pfile
->macro_buffer
;
3772 /* Get the line at which the macro was defined. */
3775 cpp_macro_definition_location (cpp_hashnode
*node
)
3777 return node
->value
.macro
->line
;