1 /* Part of CPP library. (Macro and #define handling.)
2 Copyright (C) 1986-2021 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 location_t
*virt_locs
; /* Where virtual locations for
43 unexpanded tokens are stored. */
44 location_t
*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 location_t
*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 & type of this identifier. */
89 union _cpp_hashnode_value value
;
93 static const char *vaopt_paste_error
=
94 N_("'##' cannot appear at either end of __VA_OPT__");
96 static void expand_arg (cpp_reader
*, macro_arg
*);
98 /* A class for tracking __VA_OPT__ state while iterating over a
99 sequence of tokens. This is used during both macro definition and
114 /* Initialize the state tracker. ANY_ARGS is true if variable
115 arguments were provided to the macro invocation. */
116 vaopt_state (cpp_reader
*pfile
, bool is_variadic
, macro_arg
*arg
)
119 m_variadic (is_variadic
),
120 m_last_was_paste (false),
123 m_paste_location (0),
129 /* Given a token, update the state of this tracker and return a
130 boolean indicating whether the token should be be included in the
132 update_type
update (const cpp_token
*token
)
134 /* If the macro isn't variadic, just don't bother. */
138 if (token
->type
== CPP_NAME
139 && token
->val
.node
.node
== m_pfile
->spec_nodes
.n__VA_OPT__
)
143 cpp_error_at (m_pfile
, CPP_DL_ERROR
, token
->src_loc
,
144 "__VA_OPT__ may not appear in a __VA_OPT__");
148 m_location
= token
->src_loc
;
149 m_stringify
= (token
->flags
& STRINGIFY_ARG
) != 0;
152 else if (m_state
== 1)
154 if (token
->type
!= CPP_OPEN_PAREN
)
156 cpp_error_at (m_pfile
, CPP_DL_ERROR
, m_location
,
157 "__VA_OPT__ must be followed by an "
162 if (m_update
== ERROR
)
169 if (!m_arg
->expanded
)
170 expand_arg (m_pfile
, m_arg
);
171 for (unsigned idx
= 0; idx
< m_arg
->expanded_count
; ++idx
)
172 if (m_arg
->expanded
[idx
]->type
!= CPP_PADDING
)
181 else if (m_state
>= 2)
183 if (m_state
== 2 && token
->type
== CPP_PASTE
)
185 cpp_error_at (m_pfile
, CPP_DL_ERROR
, token
->src_loc
,
189 /* Advance states before further considering this token, in
190 case we see a close paren immediately after the open
195 bool was_paste
= m_last_was_paste
;
196 m_last_was_paste
= false;
197 if (token
->type
== CPP_PASTE
)
199 m_last_was_paste
= true;
200 m_paste_location
= token
->src_loc
;
202 else if (token
->type
== CPP_OPEN_PAREN
)
204 else if (token
->type
== CPP_CLOSE_PAREN
)
209 /* Saw the final paren. */
214 cpp_error_at (m_pfile
, CPP_DL_ERROR
, token
->src_loc
,
225 /* Nothing to do with __VA_OPT__. */
229 /* Ensure that any __VA_OPT__ was completed. If ok, return true.
230 Otherwise, issue an error and return false. */
233 if (m_variadic
&& m_state
!= 0)
234 cpp_error_at (m_pfile
, CPP_DL_ERROR
, m_location
,
235 "unterminated __VA_OPT__");
239 /* Return true for # __VA_OPT__. */
240 bool stringify () const
247 /* The cpp_reader. */
250 /* The __VA_ARGS__ argument. */
253 /* True if the macro is variadic. */
255 /* If true, the previous token was ##. This is used to detect when
256 a paste occurs at the end of the sequence. */
257 bool m_last_was_paste
;
258 /* True for #__VA_OPT__. */
261 /* The state variable:
263 1 means __VA_OPT__ seen, looking for "("
264 2 means "(" seen (so the next token can't be "##")
265 >= 3 means looking for ")", the number encodes the paren depth. */
268 /* The location of the paste token. */
269 location_t m_paste_location
;
271 /* Location of the __VA_OPT__ token. */
272 location_t m_location
;
274 /* If __VA_ARGS__ substitutes to no preprocessing tokens,
275 INCLUDE, otherwise DROP. ERROR when unknown yet. */
276 update_type m_update
;
279 /* Macro expansion. */
281 static cpp_macro
*get_deferred_or_lazy_macro (cpp_reader
*, cpp_hashnode
*,
283 static int enter_macro_context (cpp_reader
*, cpp_hashnode
*,
284 const cpp_token
*, location_t
);
285 static int builtin_macro (cpp_reader
*, cpp_hashnode
*,
286 location_t
, location_t
);
287 static void push_ptoken_context (cpp_reader
*, cpp_hashnode
*, _cpp_buff
*,
288 const cpp_token
**, unsigned int);
289 static void push_extended_tokens_context (cpp_reader
*, cpp_hashnode
*,
290 _cpp_buff
*, location_t
*,
291 const cpp_token
**, unsigned int);
292 static _cpp_buff
*collect_args (cpp_reader
*, const cpp_hashnode
*,
293 _cpp_buff
**, unsigned *);
294 static cpp_context
*next_context (cpp_reader
*);
295 static const cpp_token
*padding_token (cpp_reader
*, const cpp_token
*);
296 static const cpp_token
*new_string_token (cpp_reader
*, uchar
*, unsigned int);
297 static const cpp_token
*stringify_arg (cpp_reader
*, const cpp_token
**,
299 static void paste_all_tokens (cpp_reader
*, const cpp_token
*);
300 static bool paste_tokens (cpp_reader
*, location_t
,
301 const cpp_token
**, const cpp_token
*);
302 static void alloc_expanded_arg_mem (cpp_reader
*, macro_arg
*, size_t);
303 static void ensure_expanded_arg_room (cpp_reader
*, macro_arg
*, size_t, size_t *);
304 static void delete_macro_args (_cpp_buff
*, unsigned num_args
);
305 static void set_arg_token (macro_arg
*, const cpp_token
*,
307 enum macro_arg_token_kind
,
309 static const location_t
*get_arg_token_location (const macro_arg
*,
310 enum macro_arg_token_kind
);
311 static const cpp_token
**arg_token_ptr_at (const macro_arg
*,
313 enum macro_arg_token_kind
,
314 location_t
**virt_location
);
316 static void macro_arg_token_iter_init (macro_arg_token_iter
*, bool,
317 enum macro_arg_token_kind
,
320 static const cpp_token
*macro_arg_token_iter_get_token
321 (const macro_arg_token_iter
*it
);
322 static location_t macro_arg_token_iter_get_location
323 (const macro_arg_token_iter
*);
324 static void macro_arg_token_iter_forward (macro_arg_token_iter
*);
325 static _cpp_buff
*tokens_buff_new (cpp_reader
*, size_t,
327 static size_t tokens_buff_count (_cpp_buff
*);
328 static const cpp_token
**tokens_buff_last_token_ptr (_cpp_buff
*);
329 static inline const cpp_token
**tokens_buff_put_token_to (const cpp_token
**,
334 const line_map_macro
*,
337 static const cpp_token
**tokens_buff_add_token (_cpp_buff
*,
342 const line_map_macro
*,
344 static inline void tokens_buff_remove_last_token (_cpp_buff
*);
345 static void replace_args (cpp_reader
*, cpp_hashnode
*, cpp_macro
*,
346 macro_arg
*, location_t
);
347 static _cpp_buff
*funlike_invocation_p (cpp_reader
*, cpp_hashnode
*,
348 _cpp_buff
**, unsigned *);
349 static cpp_macro
*create_iso_definition (cpp_reader
*);
351 /* #define directive parsing and handling. */
353 static cpp_macro
*lex_expansion_token (cpp_reader
*, cpp_macro
*);
354 static bool parse_params (cpp_reader
*, unsigned *, bool *);
355 static void check_trad_stringification (cpp_reader
*, const cpp_macro
*,
357 static bool reached_end_of_context (cpp_context
*);
358 static void consume_next_token_from_context (cpp_reader
*pfile
,
361 static const cpp_token
* cpp_get_token_1 (cpp_reader
*, location_t
*);
363 static cpp_hashnode
* macro_of_context (cpp_context
*context
);
365 /* Statistical counter tracking the number of macros that got
367 unsigned num_expanded_macros_counter
= 0;
368 /* Statistical counter tracking the total number tokens resulting
369 from macro expansion. */
370 unsigned num_macro_tokens_counter
= 0;
372 /* Wrapper around cpp_get_token to skip CPP_PADDING tokens
373 and not consume CPP_EOF. */
374 static const cpp_token
*
375 cpp_get_token_no_padding (cpp_reader
*pfile
)
379 const cpp_token
*ret
= cpp_peek_token (pfile
, 0);
380 if (ret
->type
== CPP_EOF
)
382 ret
= cpp_get_token (pfile
);
383 if (ret
->type
!= CPP_PADDING
)
388 /* Handle meeting "__has_include" builtin macro. */
391 builtin_has_include (cpp_reader
*pfile
, cpp_hashnode
*op
, bool has_next
)
395 if (!pfile
->state
.in_directive
)
396 cpp_error (pfile
, CPP_DL_ERROR
,
397 "\"%s\" used outside of preprocessing directive",
400 pfile
->state
.angled_headers
= true;
401 const cpp_token
*token
= cpp_get_token_no_padding (pfile
);
402 bool paren
= token
->type
== CPP_OPEN_PAREN
;
404 token
= cpp_get_token_no_padding (pfile
);
406 cpp_error (pfile
, CPP_DL_ERROR
,
407 "missing '(' before \"%s\" operand", NODE_NAME (op
));
408 pfile
->state
.angled_headers
= false;
410 bool bracket
= token
->type
!= CPP_STRING
;
412 if (token
->type
== CPP_STRING
|| token
->type
== CPP_HEADER_NAME
)
414 fname
= XNEWVEC (char, token
->val
.str
.len
- 1);
415 memcpy (fname
, token
->val
.str
.text
+ 1, token
->val
.str
.len
- 2);
416 fname
[token
->val
.str
.len
- 2] = '\0';
418 else if (token
->type
== CPP_LESS
)
419 fname
= _cpp_bracket_include (pfile
);
421 cpp_error (pfile
, CPP_DL_ERROR
,
422 "operator \"%s\" requires a header-name", NODE_NAME (op
));
426 /* Do not do the lookup if we're skipping, that's unnecessary
428 if (!pfile
->state
.skip_eval
429 && _cpp_has_header (pfile
, fname
, bracket
,
430 has_next
? IT_INCLUDE_NEXT
: IT_INCLUDE
))
437 && cpp_get_token_no_padding (pfile
)->type
!= CPP_CLOSE_PAREN
)
438 cpp_error (pfile
, CPP_DL_ERROR
,
439 "missing ')' after \"%s\" operand", NODE_NAME (op
));
444 /* Emits a warning if NODE is a macro defined in the main file that
445 has not been used. */
447 _cpp_warn_if_unused_macro (cpp_reader
*pfile
, cpp_hashnode
*node
,
448 void *v ATTRIBUTE_UNUSED
)
450 if (cpp_user_macro_p (node
))
452 cpp_macro
*macro
= node
->value
.macro
;
455 && MAIN_FILE_P (linemap_check_ordinary
456 (linemap_lookup (pfile
->line_table
,
458 cpp_warning_with_line (pfile
, CPP_W_UNUSED_MACROS
, macro
->line
, 0,
459 "macro \"%s\" is not used", NODE_NAME (node
));
465 /* Allocates and returns a CPP_STRING token, containing TEXT of length
466 LEN, after null-terminating it. TEXT must be in permanent storage. */
467 static const cpp_token
*
468 new_string_token (cpp_reader
*pfile
, unsigned char *text
, unsigned int len
)
470 cpp_token
*token
= _cpp_temp_token (pfile
);
473 token
->type
= CPP_STRING
;
474 token
->val
.str
.len
= len
;
475 token
->val
.str
.text
= text
;
480 static const char * const monthnames
[] =
482 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
483 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
486 /* Helper function for builtin_macro. Returns the text generated by
489 _cpp_builtin_macro_text (cpp_reader
*pfile
, cpp_hashnode
*node
,
492 const uchar
*result
= NULL
;
493 linenum_type number
= 1;
495 switch (node
->value
.builtin
)
498 cpp_error (pfile
, CPP_DL_ICE
, "invalid built-in macro \"%s\"",
504 if (CPP_OPTION (pfile
, warn_date_time
))
505 cpp_warning (pfile
, CPP_W_DATE_TIME
, "macro \"%s\" might prevent "
506 "reproducible builds", NODE_NAME (node
));
508 cpp_buffer
*pbuffer
= cpp_get_buffer (pfile
);
509 if (pbuffer
->timestamp
== NULL
)
511 /* Initialize timestamp value of the assotiated file. */
512 struct _cpp_file
*file
= cpp_get_file (pbuffer
);
515 /* Generate __TIMESTAMP__ string, that represents
516 the date and time of the last modification
517 of the current source file. The string constant
518 looks like "Sun Sep 16 01:03:52 1973". */
519 struct tm
*tb
= NULL
;
520 struct stat
*st
= _cpp_get_file_stat (file
);
522 tb
= localtime (&st
->st_mtime
);
525 char *str
= asctime (tb
);
526 size_t len
= strlen (str
);
527 unsigned char *buf
= _cpp_unaligned_alloc (pfile
, len
+ 2);
529 strcpy ((char *) buf
+ 1, str
);
531 pbuffer
->timestamp
= buf
;
535 cpp_errno (pfile
, CPP_DL_WARNING
,
536 "could not determine file timestamp");
537 pbuffer
->timestamp
= UC
"\"??? ??? ?? ??:??:?? ????\"";
541 result
= pbuffer
->timestamp
;
552 if (node
->value
.builtin
== BT_FILE
553 || node
->value
.builtin
== BT_FILE_NAME
)
555 name
= linemap_get_expansion_filename (pfile
->line_table
,
556 pfile
->line_table
->highest_line
);
557 if ((node
->value
.builtin
== BT_FILE_NAME
) && name
)
558 name
= lbasename (name
);
562 name
= _cpp_get_file_name (pfile
->main_file
);
566 if (pfile
->cb
.remap_filename
)
567 name
= pfile
->cb
.remap_filename (name
);
569 buf
= _cpp_unaligned_alloc (pfile
, len
* 2 + 3);
572 buf
= cpp_quote_string (buf
+ 1, (const unsigned char *) name
, len
);
578 case BT_INCLUDE_LEVEL
:
579 /* The line map depth counts the primary source as level 1, but
580 historically __INCLUDE_DEPTH__ has called the primary source
582 number
= pfile
->line_table
->depth
- 1;
586 /* If __LINE__ is embedded in a macro, it must expand to the
587 line of the macro's invocation, not its definition.
588 Otherwise things like assert() will not work properly.
589 See WG14 N1911, WG21 N4220 sec 6.5, and PR 61861. */
590 if (CPP_OPTION (pfile
, traditional
))
591 loc
= pfile
->line_table
->highest_line
;
593 loc
= linemap_resolve_location (pfile
->line_table
, loc
,
594 LRK_MACRO_EXPANSION_POINT
, NULL
);
595 number
= linemap_get_expansion_line (pfile
->line_table
, loc
);
598 /* __STDC__ has the value 1 under normal circumstances.
599 However, if (a) we are in a system header, (b) the option
600 stdc_0_in_system_headers is true (set by target config), and
601 (c) we are not in strictly conforming mode, then it has the
602 value 0. (b) and (c) are already checked in cpp_init_builtins. */
604 if (_cpp_in_system_header (pfile
))
612 if (CPP_OPTION (pfile
, warn_date_time
))
613 cpp_warning (pfile
, CPP_W_DATE_TIME
, "macro \"%s\" might prevent "
614 "reproducible builds", NODE_NAME (node
));
615 if (pfile
->date
== NULL
)
617 /* Allocate __DATE__ and __TIME__ strings from permanent
618 storage. We only do this once, and don't generate them
619 at init time, because time() and localtime() are very
620 slow on some systems. */
622 auto kind
= cpp_get_date (pfile
, &tt
);
624 if (kind
== CPP_time_kind::UNKNOWN
)
626 cpp_errno (pfile
, CPP_DL_WARNING
,
627 "could not determine date and time");
629 pfile
->date
= UC
"\"??? ?? ????\"";
630 pfile
->time
= UC
"\"??:??:??\"";
634 struct tm
*tb
= (kind
== CPP_time_kind::FIXED
635 ? gmtime
: localtime
) (&tt
);
637 pfile
->date
= _cpp_unaligned_alloc (pfile
,
638 sizeof ("\"Oct 11 1347\""));
639 sprintf ((char *) pfile
->date
, "\"%s %2d %4d\"",
640 monthnames
[tb
->tm_mon
], tb
->tm_mday
,
643 pfile
->time
= _cpp_unaligned_alloc (pfile
,
644 sizeof ("\"12:34:56\""));
645 sprintf ((char *) pfile
->time
, "\"%02d:%02d:%02d\"",
646 tb
->tm_hour
, tb
->tm_min
, tb
->tm_sec
);
650 if (node
->value
.builtin
== BT_DATE
)
651 result
= pfile
->date
;
653 result
= pfile
->time
;
657 if (CPP_OPTION (pfile
, directives_only
) && pfile
->state
.in_directive
)
658 cpp_error (pfile
, CPP_DL_ERROR
,
659 "__COUNTER__ expanded inside directive with -fdirectives-only");
660 number
= pfile
->counter
++;
663 case BT_HAS_ATTRIBUTE
:
664 number
= pfile
->cb
.has_attribute (pfile
, false);
667 case BT_HAS_STD_ATTRIBUTE
:
668 number
= pfile
->cb
.has_attribute (pfile
, true);
672 number
= pfile
->cb
.has_builtin (pfile
);
676 case BT_HAS_INCLUDE_NEXT
:
677 number
= builtin_has_include (pfile
, node
,
678 node
->value
.builtin
== BT_HAS_INCLUDE_NEXT
);
684 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
685 result
= _cpp_unaligned_alloc (pfile
, 21);
686 sprintf ((char *) result
, "%u", number
);
692 /* Get an idempotent date. Either the cached value, the value from
693 source epoch, or failing that, the value from time(2). Use this
694 during compilation so that every time stamp is the same. */
696 cpp_get_date (cpp_reader
*pfile
, time_t *result
)
698 if (!pfile
->time_stamp_kind
)
701 if (pfile
->cb
.get_source_date_epoch
)
703 /* Try reading the fixed epoch. */
704 pfile
->time_stamp
= pfile
->cb
.get_source_date_epoch (pfile
);
705 if (pfile
->time_stamp
!= time_t (-1))
706 kind
= int (CPP_time_kind::FIXED
);
711 /* Pedantically time_t (-1) is a legitimate value for
712 "number of seconds since the Epoch". It is a silly
715 pfile
->time_stamp
= time (nullptr);
716 /* Annoyingly a library could legally set errno and return a
717 valid time! Bad library! */
718 if (pfile
->time_stamp
== time_t (-1) && errno
)
721 kind
= int (CPP_time_kind::DYNAMIC
);
724 pfile
->time_stamp_kind
= kind
;
727 *result
= pfile
->time_stamp
;
728 if (pfile
->time_stamp_kind
>= 0)
730 errno
= pfile
->time_stamp_kind
;
731 return CPP_time_kind::UNKNOWN
;
734 return CPP_time_kind (pfile
->time_stamp_kind
);
737 /* Convert builtin macros like __FILE__ to a token and push it on the
738 context stack. Also handles _Pragma, for which a new token may not
739 be created. Returns 1 if it generates a new token context, 0 to
740 return the token to the caller. LOC is the location of the expansion
741 point of the macro. */
743 builtin_macro (cpp_reader
*pfile
, cpp_hashnode
*node
,
744 location_t loc
, location_t expand_loc
)
750 if (node
->value
.builtin
== BT_PRAGMA
)
752 /* Don't interpret _Pragma within directives. The standard is
753 not clear on this, but to me this makes most sense.
754 Similarly, don't interpret _Pragma inside expand_args, we might
755 need to stringize it later on. */
756 if (pfile
->state
.in_directive
|| pfile
->state
.ignore__Pragma
)
759 return _cpp_do__Pragma (pfile
, loc
);
762 buf
= _cpp_builtin_macro_text (pfile
, node
, expand_loc
);
764 nbuf
= (char *) alloca (len
+ 1);
765 memcpy (nbuf
, buf
, len
);
768 cpp_push_buffer (pfile
, (uchar
*) nbuf
, len
, /* from_stage3 */ true);
769 _cpp_clean_line (pfile
);
771 /* Set pfile->cur_token as required by _cpp_lex_direct. */
772 pfile
->cur_token
= _cpp_temp_token (pfile
);
773 cpp_token
*token
= _cpp_lex_direct (pfile
);
774 /* We should point to the expansion point of the builtin macro. */
775 token
->src_loc
= loc
;
776 if (pfile
->context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
778 /* We are tracking tokens resulting from macro expansion.
779 Create a macro line map and generate a virtual location for
780 the token resulting from the expansion of the built-in
782 location_t
*virt_locs
= NULL
;
783 _cpp_buff
*token_buf
= tokens_buff_new (pfile
, 1, &virt_locs
);
784 const line_map_macro
* map
=
785 linemap_enter_macro (pfile
->line_table
, node
, loc
, 1);
786 tokens_buff_add_token (token_buf
, virt_locs
, token
,
787 pfile
->line_table
->builtin_location
,
788 pfile
->line_table
->builtin_location
,
789 map
, /*macro_token_index=*/0);
790 push_extended_tokens_context (pfile
, node
, token_buf
, virt_locs
,
791 (const cpp_token
**)token_buf
->base
,
795 _cpp_push_token_context (pfile
, NULL
, token
, 1);
796 if (pfile
->buffer
->cur
!= pfile
->buffer
->rlimit
)
797 cpp_error (pfile
, CPP_DL_ICE
, "invalid built-in macro \"%s\"",
799 _cpp_pop_buffer (pfile
);
804 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
805 backslashes and double quotes. DEST must be of sufficient size.
806 Returns a pointer to the end of the string. */
808 cpp_quote_string (uchar
*dest
, const uchar
*src
, unsigned int len
)
817 /* Naked LF can appear in raw string literals */
834 /* Convert a token sequence FIRST to FIRST+COUNT-1 to a single string token
835 according to the rules of the ISO C #-operator. */
836 static const cpp_token
*
837 stringify_arg (cpp_reader
*pfile
, const cpp_token
**first
, unsigned int count
)
840 unsigned int i
, escape_it
, backslash_count
= 0;
841 const cpp_token
*source
= NULL
;
844 if (BUFF_ROOM (pfile
->u_buff
) < 3)
845 _cpp_extend_buff (pfile
, &pfile
->u_buff
, 3);
846 dest
= BUFF_FRONT (pfile
->u_buff
);
849 /* Loop, reading in the argument's tokens. */
850 for (i
= 0; i
< count
; i
++)
852 const cpp_token
*token
= first
[i
];
854 if (token
->type
== CPP_PADDING
)
857 || (!(source
->flags
& PREV_WHITE
)
858 && token
->val
.source
== NULL
))
859 source
= token
->val
.source
;
863 escape_it
= (token
->type
== CPP_STRING
|| token
->type
== CPP_CHAR
864 || token
->type
== CPP_WSTRING
|| token
->type
== CPP_WCHAR
865 || token
->type
== CPP_STRING32
|| token
->type
== CPP_CHAR32
866 || token
->type
== CPP_STRING16
|| token
->type
== CPP_CHAR16
867 || token
->type
== CPP_UTF8STRING
|| token
->type
== CPP_UTF8CHAR
868 || cpp_userdef_string_p (token
->type
)
869 || cpp_userdef_char_p (token
->type
));
871 /* Room for each char being written in octal, initial space and
872 final quote and NUL. */
873 len
= cpp_token_len (token
);
878 if ((size_t) (BUFF_LIMIT (pfile
->u_buff
) - dest
) < len
)
880 size_t len_so_far
= dest
- BUFF_FRONT (pfile
->u_buff
);
881 _cpp_extend_buff (pfile
, &pfile
->u_buff
, len
);
882 dest
= BUFF_FRONT (pfile
->u_buff
) + len_so_far
;
885 /* Leading white space? */
886 if (dest
- 1 != BUFF_FRONT (pfile
->u_buff
))
890 if (source
->flags
& PREV_WHITE
)
897 _cpp_buff
*buff
= _cpp_get_buff (pfile
, len
);
898 unsigned char *buf
= BUFF_FRONT (buff
);
899 len
= cpp_spell_token (pfile
, token
, buf
, true) - buf
;
900 dest
= cpp_quote_string (dest
, buf
, len
);
901 _cpp_release_buff (pfile
, buff
);
904 dest
= cpp_spell_token (pfile
, token
, dest
, true);
906 if (token
->type
== CPP_OTHER
&& token
->val
.str
.text
[0] == '\\')
912 /* Ignore the final \ of invalid string literals. */
913 if (backslash_count
& 1)
915 cpp_error (pfile
, CPP_DL_WARNING
,
916 "invalid string literal, ignoring final '\\'");
920 /* Commit the memory, including NUL, and return the token. */
922 len
= dest
- BUFF_FRONT (pfile
->u_buff
);
923 BUFF_FRONT (pfile
->u_buff
) = dest
+ 1;
924 return new_string_token (pfile
, dest
- len
, len
);
927 /* Try to paste two tokens. On success, return nonzero. In any
928 case, PLHS is updated to point to the pasted token, which is
929 guaranteed to not have the PASTE_LEFT flag set. LOCATION is
930 the virtual location used for error reporting. */
932 paste_tokens (cpp_reader
*pfile
, location_t location
,
933 const cpp_token
**plhs
, const cpp_token
*rhs
)
935 unsigned char *buf
, *end
, *lhsend
;
939 len
= cpp_token_len (*plhs
) + cpp_token_len (rhs
) + 2;
940 buf
= (unsigned char *) alloca (len
);
941 end
= lhsend
= cpp_spell_token (pfile
, *plhs
, buf
, true);
943 /* Avoid comment headers, since they are still processed in stage 3.
944 It is simpler to insert a space here, rather than modifying the
945 lexer to ignore comments in some circumstances. Simply returning
946 false doesn't work, since we want to clear the PASTE_LEFT flag. */
947 if ((*plhs
)->type
== CPP_DIV
&& rhs
->type
!= CPP_EQ
)
949 /* In one obscure case we might see padding here. */
950 if (rhs
->type
!= CPP_PADDING
)
951 end
= cpp_spell_token (pfile
, rhs
, end
, true);
954 cpp_push_buffer (pfile
, buf
, end
- buf
, /* from_stage3 */ true);
955 _cpp_clean_line (pfile
);
957 /* Set pfile->cur_token as required by _cpp_lex_direct. */
958 pfile
->cur_token
= _cpp_temp_token (pfile
);
959 lhs
= _cpp_lex_direct (pfile
);
960 if (pfile
->buffer
->cur
!= pfile
->buffer
->rlimit
)
962 location_t saved_loc
= lhs
->src_loc
;
964 _cpp_pop_buffer (pfile
);
966 unsigned char *rhsstart
= lhsend
;
967 if ((*plhs
)->type
== CPP_DIV
&& rhs
->type
!= CPP_EQ
)
970 /* We have to remove the PASTE_LEFT flag from the old lhs, but
971 we want to keep the new location. */
974 lhs
->src_loc
= saved_loc
;
975 lhs
->flags
&= ~PASTE_LEFT
;
977 /* Mandatory error for all apart from assembler. */
978 if (CPP_OPTION (pfile
, lang
) != CLK_ASM
)
979 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
980 "pasting \"%.*s\" and \"%.*s\" does not give "
981 "a valid preprocessing token",
982 (int) (lhsend
- buf
), buf
,
983 (int) (end
- rhsstart
), rhsstart
);
987 lhs
->flags
|= (*plhs
)->flags
& (PREV_WHITE
| PREV_FALLTHROUGH
);
989 _cpp_pop_buffer (pfile
);
993 /* Handles an arbitrarily long sequence of ## operators, with initial
994 operand LHS. This implementation is left-associative,
995 non-recursive, and finishes a paste before handling succeeding
996 ones. If a paste fails, we back up to the RHS of the failing ##
997 operator before pushing the context containing the result of prior
998 successful pastes, with the effect that the RHS appears in the
999 output stream after the pasted LHS normally. */
1001 paste_all_tokens (cpp_reader
*pfile
, const cpp_token
*lhs
)
1003 const cpp_token
*rhs
= NULL
;
1004 cpp_context
*context
= pfile
->context
;
1005 location_t virt_loc
= 0;
1007 /* We are expanding a macro and we must have been called on a token
1008 that appears at the left hand side of a ## operator. */
1009 if (macro_of_context (pfile
->context
) == NULL
1010 || (!(lhs
->flags
& PASTE_LEFT
)))
1013 if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
1014 /* The caller must have called consume_next_token_from_context
1015 right before calling us. That has incremented the pointer to
1016 the current virtual location. So it now points to the location
1017 of the token that comes right after *LHS. We want the
1018 resulting pasted token to have the location of the current
1020 virt_loc
= context
->c
.mc
->cur_virt_loc
[-1];
1022 /* We are not tracking macro expansion. So the best virtual
1023 location we can get here is the expansion point of the macro we
1024 are currently expanding. */
1025 virt_loc
= pfile
->invocation_location
;
1029 /* Take the token directly from the current context. We can do
1030 this, because we are in the replacement list of either an
1031 object-like macro, or a function-like macro with arguments
1032 inserted. In either case, the constraints to #define
1033 guarantee we have at least one more token. */
1034 if (context
->tokens_kind
== TOKENS_KIND_DIRECT
)
1035 rhs
= FIRST (context
).token
++;
1036 else if (context
->tokens_kind
== TOKENS_KIND_INDIRECT
)
1037 rhs
= *FIRST (context
).ptoken
++;
1038 else if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
1040 /* So we are in presence of an extended token context, which
1041 means that each token in this context has a virtual
1042 location attached to it. So let's not forget to update
1043 the pointer to the current virtual location of the
1044 current token when we update the pointer to the current
1047 rhs
= *FIRST (context
).ptoken
++;
1048 /* context->c.mc must be non-null, as if we were not in a
1049 macro context, context->tokens_kind could not be equal to
1050 TOKENS_KIND_EXTENDED. */
1051 context
->c
.mc
->cur_virt_loc
++;
1054 if (rhs
->type
== CPP_PADDING
)
1056 if (rhs
->flags
& PASTE_LEFT
)
1059 if (!paste_tokens (pfile
, virt_loc
, &lhs
, rhs
))
1061 _cpp_backup_tokens (pfile
, 1);
1065 while (rhs
->flags
& PASTE_LEFT
);
1067 /* Put the resulting token in its own context. */
1068 if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
1070 location_t
*virt_locs
= NULL
;
1071 _cpp_buff
*token_buf
= tokens_buff_new (pfile
, 1, &virt_locs
);
1072 tokens_buff_add_token (token_buf
, virt_locs
, lhs
,
1073 virt_loc
, 0, NULL
, 0);
1074 push_extended_tokens_context (pfile
, context
->c
.mc
->macro_node
,
1075 token_buf
, virt_locs
,
1076 (const cpp_token
**)token_buf
->base
, 1);
1079 _cpp_push_token_context (pfile
, NULL
, lhs
, 1);
1082 /* Returns TRUE if the number of arguments ARGC supplied in an
1083 invocation of the MACRO referenced by NODE is valid. An empty
1084 invocation to a macro with no parameters should pass ARGC as zero.
1086 Note that MACRO cannot necessarily be deduced from NODE, in case
1087 NODE was redefined whilst collecting arguments. */
1089 _cpp_arguments_ok (cpp_reader
*pfile
, cpp_macro
*macro
, const cpp_hashnode
*node
, unsigned int argc
)
1091 if (argc
== macro
->paramc
)
1094 if (argc
< macro
->paramc
)
1096 /* In C++20 (here the va_opt flag is used), and also as a GNU
1097 extension, variadic arguments are allowed to not appear in
1098 the invocation at all.
1099 e.g. #define debug(format, args...) something
1102 This is exactly the same as if an empty variadic list had been
1103 supplied - debug("string", ). */
1105 if (argc
+ 1 == macro
->paramc
&& macro
->variadic
)
1107 if (CPP_PEDANTIC (pfile
) && ! macro
->syshdr
1108 && ! CPP_OPTION (pfile
, va_opt
))
1110 if (CPP_OPTION (pfile
, cplusplus
))
1111 cpp_error (pfile
, CPP_DL_PEDWARN
,
1112 "ISO C++11 requires at least one argument "
1113 "for the \"...\" in a variadic macro");
1115 cpp_error (pfile
, CPP_DL_PEDWARN
,
1116 "ISO C99 requires at least one argument "
1117 "for the \"...\" in a variadic macro");
1122 cpp_error (pfile
, CPP_DL_ERROR
,
1123 "macro \"%s\" requires %u arguments, but only %u given",
1124 NODE_NAME (node
), macro
->paramc
, argc
);
1127 cpp_error (pfile
, CPP_DL_ERROR
,
1128 "macro \"%s\" passed %u arguments, but takes just %u",
1129 NODE_NAME (node
), argc
, macro
->paramc
);
1131 if (macro
->line
> RESERVED_LOCATION_COUNT
)
1132 cpp_error_at (pfile
, CPP_DL_NOTE
, macro
->line
, "macro \"%s\" defined here",
1138 /* Reads and returns the arguments to a function-like macro
1139 invocation. Assumes the opening parenthesis has been processed.
1140 If there is an error, emits an appropriate diagnostic and returns
1141 NULL. Each argument is terminated by a CPP_EOF token, for the
1142 future benefit of expand_arg(). If there are any deferred
1143 #pragma directives among macro arguments, store pointers to the
1144 CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.
1146 What is returned is the buffer that contains the memory allocated
1147 to hold the macro arguments. NODE is the name of the macro this
1148 function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is
1149 set to the actual number of macro arguments allocated in the
1152 collect_args (cpp_reader
*pfile
, const cpp_hashnode
*node
,
1153 _cpp_buff
**pragma_buff
, unsigned *num_args
)
1155 _cpp_buff
*buff
, *base_buff
;
1157 macro_arg
*args
, *arg
;
1158 const cpp_token
*token
;
1160 location_t virt_loc
;
1161 bool track_macro_expansion_p
= CPP_OPTION (pfile
, track_macro_expansion
);
1162 unsigned num_args_alloced
= 0;
1164 macro
= node
->value
.macro
;
1166 argc
= macro
->paramc
;
1170 #define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50
1171 #define ARG_TOKENS_EXTENT 1000
1173 buff
= _cpp_get_buff (pfile
, argc
* (DEFAULT_NUM_TOKENS_PER_MACRO_ARG
1174 * sizeof (cpp_token
*)
1175 + sizeof (macro_arg
)));
1177 args
= (macro_arg
*) buff
->base
;
1178 memset (args
, 0, argc
* sizeof (macro_arg
));
1179 buff
->cur
= (unsigned char *) &args
[argc
];
1180 arg
= args
, argc
= 0;
1182 /* Collect the tokens making up each argument. We don't yet know
1183 how many arguments have been supplied, whether too many or too
1184 few. Hence the slightly bizarre usage of "argc" and "arg". */
1187 unsigned int paren_depth
= 0;
1188 unsigned int ntokens
= 0;
1189 unsigned virt_locs_capacity
= DEFAULT_NUM_TOKENS_PER_MACRO_ARG
;
1193 arg
->first
= (const cpp_token
**) buff
->cur
;
1194 if (track_macro_expansion_p
)
1196 virt_locs_capacity
= DEFAULT_NUM_TOKENS_PER_MACRO_ARG
;
1197 arg
->virt_locs
= XNEWVEC (location_t
,
1198 virt_locs_capacity
);
1203 /* Require space for 2 new tokens (including a CPP_EOF). */
1204 if ((unsigned char *) &arg
->first
[ntokens
+ 2] > buff
->limit
)
1206 buff
= _cpp_append_extend_buff (pfile
, buff
,
1208 * sizeof (cpp_token
*));
1209 arg
->first
= (const cpp_token
**) buff
->cur
;
1211 if (track_macro_expansion_p
1212 && (ntokens
+ 2 > virt_locs_capacity
))
1214 virt_locs_capacity
+= ARG_TOKENS_EXTENT
;
1215 arg
->virt_locs
= XRESIZEVEC (location_t
,
1217 virt_locs_capacity
);
1220 token
= cpp_get_token_1 (pfile
, &virt_loc
);
1222 if (token
->type
== CPP_PADDING
)
1224 /* Drop leading padding. */
1228 else if (token
->type
== CPP_OPEN_PAREN
)
1230 else if (token
->type
== CPP_CLOSE_PAREN
)
1232 if (paren_depth
-- == 0)
1235 else if (token
->type
== CPP_COMMA
)
1237 /* A comma does not terminate an argument within
1238 parentheses or as part of a variable argument. */
1239 if (paren_depth
== 0
1240 && ! (macro
->variadic
&& argc
== macro
->paramc
))
1243 else if (token
->type
== CPP_EOF
1244 || (token
->type
== CPP_HASH
&& token
->flags
& BOL
))
1246 else if (token
->type
== CPP_PRAGMA
&& !(token
->flags
& PRAGMA_OP
))
1248 cpp_token
*newtok
= _cpp_temp_token (pfile
);
1250 /* CPP_PRAGMA token lives in directive_result, which will
1251 be overwritten on the next directive. */
1256 if (*pragma_buff
== NULL
1257 || BUFF_ROOM (*pragma_buff
) < sizeof (cpp_token
*))
1260 if (*pragma_buff
== NULL
)
1262 = _cpp_get_buff (pfile
, 32 * sizeof (cpp_token
*));
1265 next
= *pragma_buff
;
1267 = _cpp_get_buff (pfile
,
1268 (BUFF_FRONT (*pragma_buff
)
1269 - (*pragma_buff
)->base
) * 2);
1270 (*pragma_buff
)->next
= next
;
1273 *(const cpp_token
**) BUFF_FRONT (*pragma_buff
) = token
;
1274 BUFF_FRONT (*pragma_buff
) += sizeof (cpp_token
*);
1275 if (token
->type
== CPP_PRAGMA_EOL
)
1277 token
= cpp_get_token_1 (pfile
, &virt_loc
);
1279 while (token
->type
!= CPP_EOF
);
1281 /* In deferred pragmas parsing_args and prevent_expansion
1282 had been changed, reset it. */
1283 pfile
->state
.parsing_args
= 2;
1284 pfile
->state
.prevent_expansion
= 1;
1286 if (token
->type
== CPP_EOF
)
1291 set_arg_token (arg
, token
, virt_loc
,
1292 ntokens
, MACRO_ARG_TOKEN_NORMAL
,
1293 CPP_OPTION (pfile
, track_macro_expansion
));
1297 /* Drop trailing padding. */
1298 while (ntokens
> 0 && arg
->first
[ntokens
- 1]->type
== CPP_PADDING
)
1301 arg
->count
= ntokens
;
1302 /* Append an EOF to mark end-of-argument. */
1303 set_arg_token (arg
, &pfile
->endarg
, token
->src_loc
,
1304 ntokens
, MACRO_ARG_TOKEN_NORMAL
,
1305 CPP_OPTION (pfile
, track_macro_expansion
));
1307 /* Terminate the argument. Excess arguments loop back and
1308 overwrite the final legitimate argument, before failing. */
1309 if (argc
<= macro
->paramc
)
1311 buff
->cur
= (unsigned char *) &arg
->first
[ntokens
+ 1];
1312 if (argc
!= macro
->paramc
)
1316 while (token
->type
!= CPP_CLOSE_PAREN
&& token
->type
!= CPP_EOF
);
1318 if (token
->type
== CPP_EOF
)
1320 /* Unless the EOF is marking the end of an argument, it's a fake
1321 one from the end of a file that _cpp_clean_line will not have
1323 if (token
== &pfile
->endarg
)
1324 _cpp_backup_tokens (pfile
, 1);
1325 cpp_error (pfile
, CPP_DL_ERROR
,
1326 "unterminated argument list invoking macro \"%s\"",
1331 /* A single empty argument is counted as no argument. */
1332 if (argc
== 1 && macro
->paramc
== 0 && args
[0].count
== 0)
1334 if (_cpp_arguments_ok (pfile
, macro
, node
, argc
))
1336 /* GCC has special semantics for , ## b where b is a varargs
1337 parameter: we remove the comma if b was omitted entirely.
1338 If b was merely an empty argument, the comma is retained.
1339 If the macro takes just one (varargs) parameter, then we
1340 retain the comma only if we are standards conforming.
1342 If FIRST is NULL replace_args () swallows the comma. */
1343 if (macro
->variadic
&& (argc
< macro
->paramc
1344 || (argc
== 1 && args
[0].count
== 0
1345 && !CPP_OPTION (pfile
, std
))))
1346 args
[macro
->paramc
- 1].first
= NULL
;
1348 *num_args
= num_args_alloced
;
1353 /* An error occurred. */
1354 _cpp_release_buff (pfile
, base_buff
);
1358 /* Search for an opening parenthesis to the macro of NODE, in such a
1359 way that, if none is found, we don't lose the information in any
1360 intervening padding tokens. If we find the parenthesis, collect
1361 the arguments and return the buffer containing them. PRAGMA_BUFF
1362 argument is the same as in collect_args. If NUM_ARGS is non-NULL,
1363 *NUM_ARGS is set to the number of arguments contained in the
1366 funlike_invocation_p (cpp_reader
*pfile
, cpp_hashnode
*node
,
1367 _cpp_buff
**pragma_buff
, unsigned *num_args
)
1369 const cpp_token
*token
, *padding
= NULL
;
1373 token
= cpp_get_token (pfile
);
1374 if (token
->type
!= CPP_PADDING
)
1377 || (!(padding
->flags
& PREV_WHITE
) && token
->val
.source
== NULL
))
1381 if (token
->type
== CPP_OPEN_PAREN
)
1383 pfile
->state
.parsing_args
= 2;
1384 return collect_args (pfile
, node
, pragma_buff
, num_args
);
1387 /* Back up. A CPP_EOF is either an EOF from an argument we're
1388 expanding, or a fake one from lex_direct. We want to backup the
1389 former, but not the latter. We may have skipped padding, in
1390 which case backing up more than one token when expanding macros
1391 is in general too difficult. We re-insert it in its own
1393 if (token
->type
!= CPP_EOF
|| token
== &pfile
->endarg
)
1395 _cpp_backup_tokens (pfile
, 1);
1397 _cpp_push_token_context (pfile
, NULL
, padding
, 1);
1403 /* Return the real number of tokens in the expansion of MACRO. */
1404 static inline unsigned int
1405 macro_real_token_count (const cpp_macro
*macro
)
1407 if (__builtin_expect (!macro
->extra_tokens
, true))
1408 return macro
->count
;
1410 for (unsigned i
= macro
->count
; i
--;)
1411 if (macro
->exp
.tokens
[i
].type
!= CPP_PASTE
)
1417 /* Push the context of a macro with hash entry NODE onto the context
1418 stack. If we can successfully expand the macro, we push a context
1419 containing its yet-to-be-rescanned replacement list and return one.
1420 If there were additionally any unexpanded deferred #pragma
1421 directives among macro arguments, push another context containing
1422 the pragma tokens before the yet-to-be-rescanned replacement list
1423 and return two. Otherwise, we don't push a context and return
1424 zero. LOCATION is the location of the expansion point of the
1427 enter_macro_context (cpp_reader
*pfile
, cpp_hashnode
*node
,
1428 const cpp_token
*result
, location_t location
)
1430 /* The presence of a macro invalidates a file's controlling macro. */
1431 pfile
->mi_valid
= false;
1433 pfile
->state
.angled_headers
= false;
1435 /* From here to when we push the context for the macro later down
1436 this function, we need to flag the fact that we are about to
1437 expand a macro. This is useful when -ftrack-macro-expansion is
1438 turned off. In that case, we need to record the location of the
1439 expansion point of the top-most macro we are about to to expand,
1440 into pfile->invocation_location. But we must not record any such
1441 location once the process of expanding the macro starts; that is,
1442 we must not do that recording between now and later down this
1443 function where set this flag to FALSE. */
1444 pfile
->about_to_expand_macro_p
= true;
1446 if (cpp_user_macro_p (node
))
1448 cpp_macro
*macro
= node
->value
.macro
;
1449 _cpp_buff
*pragma_buff
= NULL
;
1451 if (macro
->fun_like
)
1454 unsigned num_args
= 0;
1456 pfile
->state
.prevent_expansion
++;
1457 pfile
->keep_tokens
++;
1458 pfile
->state
.parsing_args
= 1;
1459 buff
= funlike_invocation_p (pfile
, node
, &pragma_buff
,
1461 pfile
->state
.parsing_args
= 0;
1462 pfile
->keep_tokens
--;
1463 pfile
->state
.prevent_expansion
--;
1467 if (CPP_WTRADITIONAL (pfile
) && ! node
->value
.macro
->syshdr
)
1468 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
1469 "function-like macro \"%s\" must be used with arguments in traditional C",
1473 _cpp_release_buff (pfile
, pragma_buff
);
1475 pfile
->about_to_expand_macro_p
= false;
1479 if (macro
->paramc
> 0)
1480 replace_args (pfile
, node
, macro
,
1481 (macro_arg
*) buff
->base
,
1483 /* Free the memory used by the arguments of this
1484 function-like macro. This memory has been allocated by
1485 funlike_invocation_p and by replace_args. */
1486 delete_macro_args (buff
, num_args
);
1489 /* Disable the macro within its expansion. */
1490 node
->flags
|= NODE_DISABLED
;
1492 /* Laziness can only affect the expansion tokens of the macro,
1493 not its fun-likeness or parameters. */
1494 _cpp_maybe_notify_macro_use (pfile
, node
, location
);
1496 pfile
->cb
.used (pfile
, location
, node
);
1500 if (macro
->paramc
== 0)
1502 unsigned tokens_count
= macro_real_token_count (macro
);
1503 if (CPP_OPTION (pfile
, track_macro_expansion
))
1506 const cpp_token
*src
= macro
->exp
.tokens
;
1507 const line_map_macro
*map
;
1508 location_t
*virt_locs
= NULL
;
1509 _cpp_buff
*macro_tokens
1510 = tokens_buff_new (pfile
, tokens_count
, &virt_locs
);
1512 /* Create a macro map to record the locations of the
1513 tokens that are involved in the expansion. LOCATION
1514 is the location of the macro expansion point. */
1515 map
= linemap_enter_macro (pfile
->line_table
,
1516 node
, location
, tokens_count
);
1517 for (i
= 0; i
< tokens_count
; ++i
)
1519 tokens_buff_add_token (macro_tokens
, virt_locs
,
1521 src
->src_loc
, map
, i
);
1524 push_extended_tokens_context (pfile
, node
,
1527 (const cpp_token
**)
1532 _cpp_push_token_context (pfile
, node
, macro
->exp
.tokens
,
1534 num_macro_tokens_counter
+= tokens_count
;
1539 if (!pfile
->state
.in_directive
)
1540 _cpp_push_token_context (pfile
, NULL
,
1541 padding_token (pfile
, result
), 1);
1544 unsigned tokens_count
;
1545 _cpp_buff
*tail
= pragma_buff
->next
;
1546 pragma_buff
->next
= NULL
;
1547 tokens_count
= ((const cpp_token
**) BUFF_FRONT (pragma_buff
)
1548 - (const cpp_token
**) pragma_buff
->base
);
1549 push_ptoken_context (pfile
, NULL
, pragma_buff
,
1550 (const cpp_token
**) pragma_buff
->base
,
1553 if (!CPP_OPTION (pfile
, track_macro_expansion
))
1554 num_macro_tokens_counter
+= tokens_count
;
1557 while (pragma_buff
!= NULL
);
1558 pfile
->about_to_expand_macro_p
= false;
1562 pfile
->about_to_expand_macro_p
= false;
1566 pfile
->about_to_expand_macro_p
= false;
1567 /* Handle built-in macros and the _Pragma operator. */
1569 location_t expand_loc
;
1571 if (/* The top-level macro invocation that triggered the expansion
1572 we are looking at is with a function-like user macro ... */
1573 cpp_fun_like_macro_p (pfile
->top_most_macro_node
)
1574 /* ... and we are tracking the macro expansion. */
1575 && CPP_OPTION (pfile
, track_macro_expansion
))
1576 /* Then the location of the end of the macro invocation is the
1577 location of the expansion point of this macro. */
1578 expand_loc
= location
;
1580 /* Otherwise, the location of the end of the macro invocation is
1581 the location of the expansion point of that top-level macro
1583 expand_loc
= pfile
->invocation_location
;
1585 return builtin_macro (pfile
, node
, location
, expand_loc
);
1589 /* De-allocate the memory used by BUFF which is an array of instances
1590 of macro_arg. NUM_ARGS is the number of instances of macro_arg
1593 delete_macro_args (_cpp_buff
*buff
, unsigned num_args
)
1595 macro_arg
*macro_args
;
1601 macro_args
= (macro_arg
*) buff
->base
;
1603 /* Walk instances of macro_arg to free their expanded tokens as well
1604 as their macro_arg::virt_locs members. */
1605 for (i
= 0; i
< num_args
; ++i
)
1607 if (macro_args
[i
].expanded
)
1609 free (macro_args
[i
].expanded
);
1610 macro_args
[i
].expanded
= NULL
;
1612 if (macro_args
[i
].virt_locs
)
1614 free (macro_args
[i
].virt_locs
);
1615 macro_args
[i
].virt_locs
= NULL
;
1617 if (macro_args
[i
].expanded_virt_locs
)
1619 free (macro_args
[i
].expanded_virt_locs
);
1620 macro_args
[i
].expanded_virt_locs
= NULL
;
1623 _cpp_free_buff (buff
);
1626 /* Set the INDEXth token of the macro argument ARG. TOKEN is the token
1627 to set, LOCATION is its virtual location. "Virtual" location means
1628 the location that encodes loci across macro expansion. Otherwise
1629 it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the
1630 argument ARG is supposed to contain. Note that ARG must be
1631 tailored so that it has enough room to contain INDEX + 1 numbers of
1632 tokens, at least. */
1634 set_arg_token (macro_arg
*arg
, const cpp_token
*token
,
1635 location_t location
, size_t index
,
1636 enum macro_arg_token_kind kind
,
1637 bool track_macro_exp_p
)
1639 const cpp_token
**token_ptr
;
1640 location_t
*loc
= NULL
;
1643 arg_token_ptr_at (arg
, index
, kind
,
1644 track_macro_exp_p
? &loc
: NULL
);
1649 /* We can't set the location of a stringified argument
1650 token and we can't set any location if we aren't tracking
1651 macro expansion locations. */
1652 gcc_checking_assert (kind
!= MACRO_ARG_TOKEN_STRINGIFIED
1653 && track_macro_exp_p
);
1658 /* Get the pointer to the location of the argument token of the
1659 function-like macro argument ARG. This function must be called
1660 only when we -ftrack-macro-expansion is on. */
1661 static const location_t
*
1662 get_arg_token_location (const macro_arg
*arg
,
1663 enum macro_arg_token_kind kind
)
1665 const location_t
*loc
= NULL
;
1666 const cpp_token
**token_ptr
=
1667 arg_token_ptr_at (arg
, 0, kind
, (location_t
**) &loc
);
1669 if (token_ptr
== NULL
)
1675 /* Return the pointer to the INDEXth token of the macro argument ARG.
1676 KIND specifies the kind of token the macro argument ARG contains.
1677 If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
1678 of the virtual location of the returned token if the
1679 -ftrack-macro-expansion flag is on; otherwise, it's set to the
1680 spelling location of the returned token. */
1681 static const cpp_token
**
1682 arg_token_ptr_at (const macro_arg
*arg
, size_t index
,
1683 enum macro_arg_token_kind kind
,
1684 location_t
**virt_location
)
1686 const cpp_token
**tokens_ptr
= NULL
;
1690 case MACRO_ARG_TOKEN_NORMAL
:
1691 tokens_ptr
= arg
->first
;
1693 case MACRO_ARG_TOKEN_STRINGIFIED
:
1694 tokens_ptr
= (const cpp_token
**) &arg
->stringified
;
1696 case MACRO_ARG_TOKEN_EXPANDED
:
1697 tokens_ptr
= arg
->expanded
;
1701 if (tokens_ptr
== NULL
)
1702 /* This can happen for e.g, an empty token argument to a
1703 funtion-like macro. */
1708 if (kind
== MACRO_ARG_TOKEN_NORMAL
)
1709 *virt_location
= &arg
->virt_locs
[index
];
1710 else if (kind
== MACRO_ARG_TOKEN_EXPANDED
)
1711 *virt_location
= &arg
->expanded_virt_locs
[index
];
1712 else if (kind
== MACRO_ARG_TOKEN_STRINGIFIED
)
1714 (location_t
*) &tokens_ptr
[index
]->src_loc
;
1716 return &tokens_ptr
[index
];
1719 /* Initialize an iterator so that it iterates over the tokens of a
1720 function-like macro argument. KIND is the kind of tokens we want
1721 ITER to iterate over. TOKEN_PTR points the first token ITER will
1724 macro_arg_token_iter_init (macro_arg_token_iter
*iter
,
1725 bool track_macro_exp_p
,
1726 enum macro_arg_token_kind kind
,
1727 const macro_arg
*arg
,
1728 const cpp_token
**token_ptr
)
1730 iter
->track_macro_exp_p
= track_macro_exp_p
;
1732 iter
->token_ptr
= token_ptr
;
1733 /* Unconditionally initialize this so that the compiler doesn't warn
1734 about iter->location_ptr being possibly uninitialized later after
1735 this code has been inlined somewhere. */
1736 iter
->location_ptr
= NULL
;
1737 if (track_macro_exp_p
)
1738 iter
->location_ptr
= get_arg_token_location (arg
, kind
);
1740 iter
->num_forwards
= 0;
1741 if (track_macro_exp_p
1742 && token_ptr
!= NULL
1743 && iter
->location_ptr
== NULL
)
1748 /* Move the iterator one token forward. Note that if IT was
1749 initialized on an argument that has a stringified token, moving it
1750 forward doesn't make sense as a stringified token is essentially one
1753 macro_arg_token_iter_forward (macro_arg_token_iter
*it
)
1757 case MACRO_ARG_TOKEN_NORMAL
:
1758 case MACRO_ARG_TOKEN_EXPANDED
:
1760 if (it
->track_macro_exp_p
)
1763 case MACRO_ARG_TOKEN_STRINGIFIED
:
1765 if (it
->num_forwards
> 0)
1776 /* Return the token pointed to by the iterator. */
1777 static const cpp_token
*
1778 macro_arg_token_iter_get_token (const macro_arg_token_iter
*it
)
1781 if (it
->kind
== MACRO_ARG_TOKEN_STRINGIFIED
1782 && it
->num_forwards
> 0)
1785 if (it
->token_ptr
== NULL
)
1787 return *it
->token_ptr
;
1790 /* Return the location of the token pointed to by the iterator.*/
1792 macro_arg_token_iter_get_location (const macro_arg_token_iter
*it
)
1795 if (it
->kind
== MACRO_ARG_TOKEN_STRINGIFIED
1796 && it
->num_forwards
> 0)
1799 if (it
->track_macro_exp_p
)
1800 return *it
->location_ptr
;
1802 return (*it
->token_ptr
)->src_loc
;
1805 /* Return the index of a token [resulting from macro expansion] inside
1806 the total list of tokens resulting from a given macro
1807 expansion. The index can be different depending on whether if we
1808 want each tokens resulting from function-like macro arguments
1809 expansion to have a different location or not.
1811 E.g, consider this function-like macro:
1815 Then consider us "calling" it (and thus expanding it) like:
1819 It will be expanded into:
1823 Let's consider the case of the token '4'.
1825 Its index can be 2 (it's the third token of the set of tokens
1826 resulting from the expansion) or it can be 0 if we consider that
1827 all tokens resulting from the expansion of the argument "1+2" have
1828 the same index, which is 0. In this later case, the index of token
1829 '-' would then be 1 and the index of token '3' would be 2.
1831 The later case is useful to use less memory e.g, for the case of
1832 the user using the option -ftrack-macro-expansion=1.
1834 ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
1835 are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro
1836 parameter (inside the macro replacement list) that corresponds to
1837 the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
1840 If we refer to the example above, for the '4' argument token,
1841 ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
1842 would be set to the token 'x', in the replacement list "x - 3" of
1845 This is a subroutine of replace_args. */
1846 inline static unsigned
1847 expanded_token_index (cpp_reader
*pfile
, cpp_macro
*macro
,
1848 const cpp_token
*cur_replacement_token
,
1849 unsigned absolute_token_index
)
1851 if (CPP_OPTION (pfile
, track_macro_expansion
) > 1)
1852 return absolute_token_index
;
1853 return cur_replacement_token
- macro
->exp
.tokens
;
1856 /* Copy whether PASTE_LEFT is set from SRC to *PASTE_FLAG. */
1859 copy_paste_flag (cpp_reader
*pfile
, const cpp_token
**paste_flag
,
1860 const cpp_token
*src
)
1862 cpp_token
*token
= _cpp_temp_token (pfile
);
1863 token
->type
= (*paste_flag
)->type
;
1864 token
->val
= (*paste_flag
)->val
;
1865 if (src
->flags
& PASTE_LEFT
)
1866 token
->flags
= (*paste_flag
)->flags
| PASTE_LEFT
;
1868 token
->flags
= (*paste_flag
)->flags
& ~PASTE_LEFT
;
1869 *paste_flag
= token
;
1872 /* True IFF the last token emitted into BUFF (if any) is PTR. */
1875 last_token_is (_cpp_buff
*buff
, const cpp_token
**ptr
)
1877 return (ptr
&& tokens_buff_last_token_ptr (buff
) == ptr
);
1880 /* Replace the parameters in a function-like macro of NODE with the
1881 actual ARGS, and place the result in a newly pushed token context.
1882 Expand each argument before replacing, unless it is operated upon
1883 by the # or ## operators. EXPANSION_POINT_LOC is the location of
1884 the expansion point of the macro. E.g, the location of the
1885 function-like macro invocation. */
1887 replace_args (cpp_reader
*pfile
, cpp_hashnode
*node
, cpp_macro
*macro
,
1888 macro_arg
*args
, location_t expansion_point_loc
)
1890 unsigned int i
, total
;
1891 const cpp_token
*src
, *limit
;
1892 const cpp_token
**first
= NULL
;
1894 _cpp_buff
*buff
= NULL
;
1895 location_t
*virt_locs
= NULL
;
1896 unsigned int exp_count
;
1897 const line_map_macro
*map
= NULL
;
1898 int track_macro_exp
;
1900 /* First, fully macro-expand arguments, calculating the number of
1901 tokens in the final expansion as we go. The ordering of the if
1902 statements below is subtle; we must handle stringification before
1905 /* EXP_COUNT is the number of tokens in the macro replacement
1906 list. TOTAL is the number of tokens /after/ macro parameters
1907 have been replaced by their arguments. */
1908 exp_count
= macro_real_token_count (macro
);
1910 limit
= macro
->exp
.tokens
+ exp_count
;
1912 for (src
= macro
->exp
.tokens
; src
< limit
; src
++)
1913 if (src
->type
== CPP_MACRO_ARG
)
1915 /* Leading and trailing padding tokens. */
1917 /* Account for leading and padding tokens in exp_count too.
1918 This is going to be important later down this function,
1919 when we want to handle the case of (track_macro_exp <
1923 /* We have an argument. If it is not being stringified or
1924 pasted it is macro-replaced before insertion. */
1925 arg
= &args
[src
->val
.macro_arg
.arg_no
- 1];
1927 if (src
->flags
& STRINGIFY_ARG
)
1929 if (!arg
->stringified
)
1930 arg
->stringified
= stringify_arg (pfile
, arg
->first
, arg
->count
);
1932 else if ((src
->flags
& PASTE_LEFT
)
1933 || (src
!= macro
->exp
.tokens
&& (src
[-1].flags
& PASTE_LEFT
)))
1934 total
+= arg
->count
- 1;
1938 expand_arg (pfile
, arg
);
1939 total
+= arg
->expanded_count
- 1;
1943 /* When the compiler is called with the -ftrack-macro-expansion
1944 flag, we need to keep track of the location of each token that
1945 results from macro expansion.
1947 A token resulting from macro expansion is not a new token. It is
1948 simply the same token as the token coming from the macro
1949 definition. The new things that are allocated are the buffer
1950 that holds the tokens resulting from macro expansion and a new
1951 location that records many things like the locus of the expansion
1952 point as well as the original locus inside the definition of the
1953 macro. This location is called a virtual location.
1955 So the buffer BUFF holds a set of cpp_token*, and the buffer
1956 VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
1958 Both of these two buffers are going to be hung off of the macro
1959 context, when the latter is pushed. The memory allocated to
1960 store the tokens and their locations is going to be freed once
1961 the context of macro expansion is popped.
1963 As far as tokens are concerned, the memory overhead of
1964 -ftrack-macro-expansion is proportional to the number of
1965 macros that get expanded multiplied by sizeof (location_t).
1966 The good news is that extra memory gets freed when the macro
1967 context is freed, i.e shortly after the macro got expanded. */
1969 /* Is the -ftrack-macro-expansion flag in effect? */
1970 track_macro_exp
= CPP_OPTION (pfile
, track_macro_expansion
);
1972 /* Now allocate memory space for tokens and locations resulting from
1973 the macro expansion, copy the tokens and replace the arguments.
1974 This memory must be freed when the context of the macro MACRO is
1976 buff
= tokens_buff_new (pfile
, total
, track_macro_exp
? &virt_locs
: NULL
);
1978 first
= (const cpp_token
**) buff
->base
;
1980 /* Create a macro map to record the locations of the tokens that are
1981 involved in the expansion. Note that the expansion point is set
1982 to the location of the closing parenthesis. Otherwise, the
1983 subsequent map created for the first token that comes after the
1984 macro map might have a wrong line number. That would lead to
1985 tokens with wrong line numbers after the macro expansion. This
1986 adds up to the memory overhead of the -ftrack-macro-expansion
1987 flag; for every macro that is expanded, a "macro map" is
1989 if (track_macro_exp
)
1991 int num_macro_tokens
= total
;
1992 if (track_macro_exp
< 2)
1993 /* Then the number of macro tokens won't take in account the
1994 fact that function-like macro arguments can expand to
1995 multiple tokens. This is to save memory at the expense of
1998 Suppose we have #define SQUARE(A) A * A
2000 And then we do SQUARE(2+3)
2002 Then the tokens 2, +, 3, will have the same location,
2003 saying they come from the expansion of the argument A. */
2004 num_macro_tokens
= exp_count
;
2005 map
= linemap_enter_macro (pfile
->line_table
, node
,
2006 expansion_point_loc
,
2010 vaopt_state
vaopt_tracker (pfile
, macro
->variadic
, &args
[macro
->paramc
- 1]);
2011 const cpp_token
**vaopt_start
= NULL
;
2012 for (src
= macro
->exp
.tokens
; src
< limit
; src
++)
2014 unsigned int arg_tokens_count
;
2015 macro_arg_token_iter from
;
2016 const cpp_token
**paste_flag
= NULL
;
2017 const cpp_token
**tmp_token_ptr
;
2019 /* __VA_OPT__ handling. */
2020 vaopt_state::update_type vostate
= vaopt_tracker
.update (src
);
2021 if (__builtin_expect (vostate
!= vaopt_state::INCLUDE
, false))
2023 if (vostate
== vaopt_state::BEGIN
)
2025 /* Padding on the left of __VA_OPT__ (unless RHS of ##). */
2026 if (src
!= macro
->exp
.tokens
&& !(src
[-1].flags
& PASTE_LEFT
))
2028 const cpp_token
*t
= padding_token (pfile
, src
);
2029 unsigned index
= expanded_token_index (pfile
, macro
, src
, i
);
2030 /* Allocate a virtual location for the padding token and
2031 append the token and its location to BUFF and
2033 tokens_buff_add_token (buff
, virt_locs
, t
,
2034 t
->src_loc
, t
->src_loc
,
2037 vaopt_start
= tokens_buff_last_token_ptr (buff
);
2039 else if (vostate
== vaopt_state::END
)
2041 const cpp_token
**start
= vaopt_start
;
2044 paste_flag
= tokens_buff_last_token_ptr (buff
);
2046 if (vaopt_tracker
.stringify ())
2049 = start
? paste_flag
- start
: tokens_buff_count (buff
);
2050 const cpp_token
**first
2052 : (const cpp_token
**) (buff
->base
);
2055 /* Paste any tokens that need to be pasted before calling
2056 stringify_arg, because stringify_arg uses pfile->u_buff
2057 which paste_tokens can use as well. */
2058 for (i
= 0, j
= 0; i
< count
; i
++, j
++)
2060 const cpp_token
*token
= first
[i
];
2062 if (token
->flags
& PASTE_LEFT
)
2064 location_t virt_loc
= pfile
->invocation_location
;
2065 const cpp_token
*rhs
;
2071 if (!paste_tokens (pfile
, virt_loc
, &token
, rhs
))
2077 while (rhs
->flags
& PASTE_LEFT
);
2085 tokens_buff_remove_last_token (buff
);
2089 const cpp_token
*t
= stringify_arg (pfile
, first
, count
);
2091 tokens_buff_remove_last_token (buff
);
2092 if (src
->flags
& PASTE_LEFT
)
2093 copy_paste_flag (pfile
, &t
, src
);
2094 tokens_buff_add_token (buff
, virt_locs
,
2095 t
, t
->src_loc
, t
->src_loc
,
2098 else if (src
->flags
& PASTE_LEFT
)
2100 /* Don't avoid paste after all. */
2101 while (paste_flag
&& paste_flag
!= start
2102 && *paste_flag
== &pfile
->avoid_paste
)
2104 tokens_buff_remove_last_token (buff
);
2105 paste_flag
= tokens_buff_last_token_ptr (buff
);
2108 /* With a non-empty __VA_OPT__ on the LHS of ##, the last
2109 token should be flagged PASTE_LEFT. */
2110 if (paste_flag
&& (*paste_flag
)->type
!= CPP_PADDING
)
2111 copy_paste_flag (pfile
, paste_flag
, src
);
2115 /* Otherwise, avoid paste on RHS, __VA_OPT__(c)d or
2116 __VA_OPT__(c)__VA_OPT__(d). */
2117 const cpp_token
*t
= &pfile
->avoid_paste
;
2118 tokens_buff_add_token (buff
, virt_locs
,
2119 t
, t
->src_loc
, t
->src_loc
,
2126 if (src
->type
!= CPP_MACRO_ARG
)
2128 /* Allocate a virtual location for token SRC, and add that
2129 token and its virtual location into the buffers BUFF and
2131 unsigned index
= expanded_token_index (pfile
, macro
, src
, i
);
2132 tokens_buff_add_token (buff
, virt_locs
, src
,
2133 src
->src_loc
, src
->src_loc
,
2140 arg
= &args
[src
->val
.macro_arg
.arg_no
- 1];
2141 /* SRC is a macro parameter that we need to replace with its
2142 corresponding argument. So at some point we'll need to
2143 iterate over the tokens of the macro argument and copy them
2144 into the "place" now holding the correspondig macro
2145 parameter. We are going to use the iterator type
2146 macro_argo_token_iter to handle that iterating. The 'if'
2147 below is to initialize the iterator depending on the type of
2148 tokens the macro argument has. It also does some adjustment
2149 related to padding tokens and some pasting corner cases. */
2150 if (src
->flags
& STRINGIFY_ARG
)
2152 arg_tokens_count
= 1;
2153 macro_arg_token_iter_init (&from
,
2155 track_macro_expansion
),
2156 MACRO_ARG_TOKEN_STRINGIFIED
,
2157 arg
, &arg
->stringified
);
2159 else if (src
->flags
& PASTE_LEFT
)
2161 arg_tokens_count
= arg
->count
;
2162 macro_arg_token_iter_init (&from
,
2164 track_macro_expansion
),
2165 MACRO_ARG_TOKEN_NORMAL
,
2168 else if (src
!= macro
->exp
.tokens
&& (src
[-1].flags
& PASTE_LEFT
))
2171 arg_tokens_count
= arg
->count
;
2172 macro_arg_token_iter_init (&from
,
2174 track_macro_expansion
),
2175 MACRO_ARG_TOKEN_NORMAL
,
2178 num_toks
= tokens_buff_count (buff
);
2182 /* So the current parameter token is pasted to the previous
2183 token in the replacement list. Let's look at what
2184 we have as previous and current arguments. */
2186 /* This is the previous argument's token ... */
2187 tmp_token_ptr
= tokens_buff_last_token_ptr (buff
);
2189 if ((*tmp_token_ptr
)->type
== CPP_COMMA
2191 && src
->val
.macro_arg
.arg_no
== macro
->paramc
)
2193 /* ... which is a comma; and the current parameter
2194 is the last parameter of a variadic function-like
2195 macro. If the argument to the current last
2196 parameter is NULL, then swallow the comma,
2197 otherwise drop the paste flag. */
2198 if (macro_arg_token_iter_get_token (&from
) == NULL
)
2199 tokens_buff_remove_last_token (buff
);
2201 paste_flag
= tmp_token_ptr
;
2203 /* Remove the paste flag if the RHS is a placemarker. */
2204 else if (arg_tokens_count
== 0)
2205 paste_flag
= tmp_token_ptr
;
2210 arg_tokens_count
= arg
->expanded_count
;
2211 macro_arg_token_iter_init (&from
,
2213 track_macro_expansion
),
2214 MACRO_ARG_TOKEN_EXPANDED
,
2215 arg
, arg
->expanded
);
2217 if (last_token_is (buff
, vaopt_start
))
2219 /* We're expanding an arg at the beginning of __VA_OPT__.
2221 while (arg_tokens_count
)
2223 const cpp_token
*t
= macro_arg_token_iter_get_token (&from
);
2224 if (t
->type
!= CPP_PADDING
)
2226 macro_arg_token_iter_forward (&from
);
2232 /* Padding on the left of an argument (unless RHS of ##). */
2233 if ((!pfile
->state
.in_directive
|| pfile
->state
.directive_wants_padding
)
2234 && src
!= macro
->exp
.tokens
2235 && !(src
[-1].flags
& PASTE_LEFT
)
2236 && !last_token_is (buff
, vaopt_start
))
2238 const cpp_token
*t
= padding_token (pfile
, src
);
2239 unsigned index
= expanded_token_index (pfile
, macro
, src
, i
);
2240 /* Allocate a virtual location for the padding token and
2241 append the token and its location to BUFF and
2243 tokens_buff_add_token (buff
, virt_locs
, t
,
2244 t
->src_loc
, t
->src_loc
,
2248 if (arg_tokens_count
)
2250 /* So now we've got the number of tokens that make up the
2251 argument that is going to replace the current parameter
2252 in the macro's replacement list. */
2254 for (j
= 0; j
< arg_tokens_count
; ++j
)
2256 /* So if track_macro_exp is < 2, the user wants to
2257 save extra memory while tracking macro expansion
2258 locations. So in that case here is what we do:
2260 Suppose we have #define SQUARE(A) A * A
2262 And then we do SQUARE(2+3)
2264 Then the tokens 2, +, 3, will have the same location,
2265 saying they come from the expansion of the argument
2268 So that means we are going to ignore the COUNT tokens
2269 resulting from the expansion of the current macro
2270 argument. In other words all the ARG_TOKENS_COUNT tokens
2271 resulting from the expansion of the macro argument will
2272 have the index I. Normally, each of those tokens should
2274 unsigned token_index
= i
;
2276 if (track_macro_exp
> 1)
2279 index
= expanded_token_index (pfile
, macro
, src
, token_index
);
2280 const cpp_token
*tok
= macro_arg_token_iter_get_token (&from
);
2281 tokens_buff_add_token (buff
, virt_locs
, tok
,
2282 macro_arg_token_iter_get_location (&from
),
2283 src
->src_loc
, map
, index
);
2284 macro_arg_token_iter_forward (&from
);
2287 /* With a non-empty argument on the LHS of ##, the last
2288 token should be flagged PASTE_LEFT. */
2289 if (src
->flags
& PASTE_LEFT
)
2291 = (const cpp_token
**) tokens_buff_last_token_ptr (buff
);
2293 else if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, c99
)
2294 && ! macro
->syshdr
&& ! _cpp_in_system_header (pfile
))
2296 if (CPP_OPTION (pfile
, cplusplus
))
2297 cpp_pedwarning (pfile
, CPP_W_PEDANTIC
,
2298 "invoking macro %s argument %d: "
2299 "empty macro arguments are undefined"
2301 NODE_NAME (node
), src
->val
.macro_arg
.arg_no
);
2302 else if (CPP_OPTION (pfile
, cpp_warn_c90_c99_compat
))
2303 cpp_pedwarning (pfile
,
2304 CPP_OPTION (pfile
, cpp_warn_c90_c99_compat
) > 0
2305 ? CPP_W_C90_C99_COMPAT
: CPP_W_PEDANTIC
,
2306 "invoking macro %s argument %d: "
2307 "empty macro arguments are undefined"
2309 NODE_NAME (node
), src
->val
.macro_arg
.arg_no
);
2311 else if (CPP_OPTION (pfile
, cpp_warn_c90_c99_compat
) > 0
2312 && ! CPP_OPTION (pfile
, cplusplus
)
2313 && ! macro
->syshdr
&& ! _cpp_in_system_header (pfile
))
2314 cpp_warning (pfile
, CPP_W_C90_C99_COMPAT
,
2315 "invoking macro %s argument %d: "
2316 "empty macro arguments are undefined"
2318 NODE_NAME (node
), src
->val
.macro_arg
.arg_no
);
2320 /* Avoid paste on RHS (even case count == 0). */
2321 if (!pfile
->state
.in_directive
&& !(src
->flags
& PASTE_LEFT
))
2323 const cpp_token
*t
= &pfile
->avoid_paste
;
2324 tokens_buff_add_token (buff
, virt_locs
,
2325 t
, t
->src_loc
, t
->src_loc
,
2329 /* Add a new paste flag, or remove an unwanted one. */
2331 copy_paste_flag (pfile
, paste_flag
, src
);
2333 i
+= arg_tokens_count
;
2336 if (track_macro_exp
)
2337 push_extended_tokens_context (pfile
, node
, buff
, virt_locs
, first
,
2338 tokens_buff_count (buff
));
2340 push_ptoken_context (pfile
, node
, buff
, first
,
2341 tokens_buff_count (buff
));
2343 num_macro_tokens_counter
+= tokens_buff_count (buff
);
2346 /* Return a special padding token, with padding inherited from SOURCE. */
2347 static const cpp_token
*
2348 padding_token (cpp_reader
*pfile
, const cpp_token
*source
)
2350 cpp_token
*result
= _cpp_temp_token (pfile
);
2352 result
->type
= CPP_PADDING
;
2354 /* Data in GCed data structures cannot be made const so far, so we
2355 need a cast here. */
2356 result
->val
.source
= (cpp_token
*) source
;
2361 /* Get a new uninitialized context. Create a new one if we cannot
2362 re-use an old one. */
2363 static cpp_context
*
2364 next_context (cpp_reader
*pfile
)
2366 cpp_context
*result
= pfile
->context
->next
;
2370 result
= XNEW (cpp_context
);
2371 memset (result
, 0, sizeof (cpp_context
));
2372 result
->prev
= pfile
->context
;
2374 pfile
->context
->next
= result
;
2377 pfile
->context
= result
;
2381 /* Push a list of pointers to tokens. */
2383 push_ptoken_context (cpp_reader
*pfile
, cpp_hashnode
*macro
, _cpp_buff
*buff
,
2384 const cpp_token
**first
, unsigned int count
)
2386 cpp_context
*context
= next_context (pfile
);
2388 context
->tokens_kind
= TOKENS_KIND_INDIRECT
;
2389 context
->c
.macro
= macro
;
2390 context
->buff
= buff
;
2391 FIRST (context
).ptoken
= first
;
2392 LAST (context
).ptoken
= first
+ count
;
2395 /* Push a list of tokens.
2397 A NULL macro means that we should continue the current macro
2398 expansion, in essence. That means that if we are currently in a
2399 macro expansion context, we'll make the new pfile->context refer to
2400 the current macro. */
2402 _cpp_push_token_context (cpp_reader
*pfile
, cpp_hashnode
*macro
,
2403 const cpp_token
*first
, unsigned int count
)
2405 cpp_context
*context
;
2408 macro
= macro_of_context (pfile
->context
);
2410 context
= next_context (pfile
);
2411 context
->tokens_kind
= TOKENS_KIND_DIRECT
;
2412 context
->c
.macro
= macro
;
2413 context
->buff
= NULL
;
2414 FIRST (context
).token
= first
;
2415 LAST (context
).token
= first
+ count
;
2418 /* Build a context containing a list of tokens as well as their
2419 virtual locations and push it. TOKENS_BUFF is the buffer that
2420 contains the tokens pointed to by FIRST. If TOKENS_BUFF is
2421 non-NULL, it means that the context owns it, meaning that
2422 _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
2423 contains the virtual locations.
2425 A NULL macro means that we should continue the current macro
2426 expansion, in essence. That means that if we are currently in a
2427 macro expansion context, we'll make the new pfile->context refer to
2428 the current macro. */
2430 push_extended_tokens_context (cpp_reader
*pfile
,
2431 cpp_hashnode
*macro
,
2432 _cpp_buff
*token_buff
,
2433 location_t
*virt_locs
,
2434 const cpp_token
**first
,
2437 cpp_context
*context
;
2441 macro
= macro_of_context (pfile
->context
);
2443 context
= next_context (pfile
);
2444 context
->tokens_kind
= TOKENS_KIND_EXTENDED
;
2445 context
->buff
= token_buff
;
2447 m
= XNEW (macro_context
);
2448 m
->macro_node
= macro
;
2449 m
->virt_locs
= virt_locs
;
2450 m
->cur_virt_loc
= virt_locs
;
2452 FIRST (context
).ptoken
= first
;
2453 LAST (context
).ptoken
= first
+ count
;
2456 /* Push a traditional macro's replacement text. */
2458 _cpp_push_text_context (cpp_reader
*pfile
, cpp_hashnode
*macro
,
2459 const uchar
*start
, size_t len
)
2461 cpp_context
*context
= next_context (pfile
);
2463 context
->tokens_kind
= TOKENS_KIND_DIRECT
;
2464 context
->c
.macro
= macro
;
2465 context
->buff
= NULL
;
2466 CUR (context
) = start
;
2467 RLIMIT (context
) = start
+ len
;
2468 macro
->flags
|= NODE_DISABLED
;
2471 /* Creates a buffer that holds tokens a.k.a "token buffer", usually
2472 for the purpose of storing them on a cpp_context. If VIRT_LOCS is
2473 non-null (which means that -ftrack-macro-expansion is on),
2474 *VIRT_LOCS is set to a newly allocated buffer that is supposed to
2475 hold the virtual locations of the tokens resulting from macro
2478 tokens_buff_new (cpp_reader
*pfile
, size_t len
,
2479 location_t
**virt_locs
)
2481 size_t tokens_size
= len
* sizeof (cpp_token
*);
2482 size_t locs_size
= len
* sizeof (location_t
);
2484 if (virt_locs
!= NULL
)
2485 *virt_locs
= XNEWVEC (location_t
, locs_size
);
2486 return _cpp_get_buff (pfile
, tokens_size
);
2489 /* Returns the number of tokens contained in a token buffer. The
2490 buffer holds a set of cpp_token*. */
2492 tokens_buff_count (_cpp_buff
*buff
)
2494 return (BUFF_FRONT (buff
) - buff
->base
) / sizeof (cpp_token
*);
2497 /* Return a pointer to the last token contained in the token buffer
2499 static const cpp_token
**
2500 tokens_buff_last_token_ptr (_cpp_buff
*buff
)
2502 if (BUFF_FRONT (buff
) == buff
->base
)
2504 return &((const cpp_token
**) BUFF_FRONT (buff
))[-1];
2507 /* Remove the last token contained in the token buffer TOKENS_BUFF.
2508 If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer
2509 containing the virtual locations of the tokens in TOKENS_BUFF; in
2510 which case the function updates that buffer as well. */
2512 tokens_buff_remove_last_token (_cpp_buff
*tokens_buff
)
2515 if (BUFF_FRONT (tokens_buff
) > tokens_buff
->base
)
2516 BUFF_FRONT (tokens_buff
) =
2517 (unsigned char *) &((cpp_token
**) BUFF_FRONT (tokens_buff
))[-1];
2520 /* Insert a token into the token buffer at the position pointed to by
2521 DEST. Note that the buffer is not enlarged so the previous token
2522 that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null,
2523 means -ftrack-macro-expansion is effect; it then points to where to
2524 insert the virtual location of TOKEN. TOKEN is the token to
2525 insert. VIRT_LOC is the virtual location of the token, i.e, the
2526 location possibly encoding its locus across macro expansion. If
2527 TOKEN is an argument of a function-like macro (inside a macro
2528 replacement list), PARM_DEF_LOC is the spelling location of the
2529 macro parameter that TOKEN is replacing, in the replacement list of
2530 the macro. If TOKEN is not an argument of a function-like macro or
2531 if it doesn't come from a macro expansion, then VIRT_LOC can just
2532 be set to the same value as PARM_DEF_LOC. If MAP is non null, it
2533 means TOKEN comes from a macro expansion and MAP is the macro map
2534 associated to the macro. MACRO_TOKEN_INDEX points to the index of
2535 the token in the macro map; it is not considered if MAP is NULL.
2537 Upon successful completion this function returns the a pointer to
2538 the position of the token coming right after the insertion
2540 static inline const cpp_token
**
2541 tokens_buff_put_token_to (const cpp_token
**dest
,
2542 location_t
*virt_loc_dest
,
2543 const cpp_token
*token
,
2544 location_t virt_loc
,
2545 location_t parm_def_loc
,
2546 const line_map_macro
*map
,
2547 unsigned int macro_token_index
)
2549 location_t macro_loc
= virt_loc
;
2550 const cpp_token
**result
;
2554 /* -ftrack-macro-expansion is on. */
2556 macro_loc
= linemap_add_macro_token (map
, macro_token_index
,
2557 virt_loc
, parm_def_loc
);
2558 *virt_loc_dest
= macro_loc
;
2566 /* Adds a token at the end of the tokens contained in BUFFER. Note
2567 that this function doesn't enlarge BUFFER when the number of tokens
2568 reaches BUFFER's size; it aborts in that situation.
2570 TOKEN is the token to append. VIRT_LOC is the virtual location of
2571 the token, i.e, the location possibly encoding its locus across
2572 macro expansion. If TOKEN is an argument of a function-like macro
2573 (inside a macro replacement list), PARM_DEF_LOC is the location of
2574 the macro parameter that TOKEN is replacing. If TOKEN doesn't come
2575 from a macro expansion, then VIRT_LOC can just be set to the same
2576 value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes
2577 from a macro expansion and MAP is the macro map associated to the
2578 macro. MACRO_TOKEN_INDEX points to the index of the token in the
2579 macro map; It is not considered if MAP is NULL. If VIRT_LOCS is
2580 non-null, it means -ftrack-macro-expansion is on; in which case
2581 this function adds the virtual location DEF_LOC to the VIRT_LOCS
2582 array, at the same index as the one of TOKEN in BUFFER. Upon
2583 successful completion this function returns the a pointer to the
2584 position of the token coming right after the insertion point. */
2585 static const cpp_token
**
2586 tokens_buff_add_token (_cpp_buff
*buffer
,
2587 location_t
*virt_locs
,
2588 const cpp_token
*token
,
2589 location_t virt_loc
,
2590 location_t parm_def_loc
,
2591 const line_map_macro
*map
,
2592 unsigned int macro_token_index
)
2594 const cpp_token
**result
;
2595 location_t
*virt_loc_dest
= NULL
;
2596 unsigned token_index
=
2597 (BUFF_FRONT (buffer
) - buffer
->base
) / sizeof (cpp_token
*);
2599 /* Abort if we pass the end the buffer. */
2600 if (BUFF_FRONT (buffer
) > BUFF_LIMIT (buffer
))
2603 if (virt_locs
!= NULL
)
2604 virt_loc_dest
= &virt_locs
[token_index
];
2607 tokens_buff_put_token_to ((const cpp_token
**) BUFF_FRONT (buffer
),
2608 virt_loc_dest
, token
, virt_loc
, parm_def_loc
,
2609 map
, macro_token_index
);
2611 BUFF_FRONT (buffer
) = (unsigned char *) result
;
2615 /* Allocate space for the function-like macro argument ARG to store
2616 the tokens resulting from the macro-expansion of the tokens that
2617 make up ARG itself. That space is allocated in ARG->expanded and
2618 needs to be freed using free. */
2620 alloc_expanded_arg_mem (cpp_reader
*pfile
, macro_arg
*arg
, size_t capacity
)
2622 gcc_checking_assert (arg
->expanded
== NULL
2623 && arg
->expanded_virt_locs
== NULL
);
2625 arg
->expanded
= XNEWVEC (const cpp_token
*, capacity
);
2626 if (CPP_OPTION (pfile
, track_macro_expansion
))
2627 arg
->expanded_virt_locs
= XNEWVEC (location_t
, capacity
);
2631 /* If necessary, enlarge ARG->expanded to so that it can contain SIZE
2634 ensure_expanded_arg_room (cpp_reader
*pfile
, macro_arg
*arg
,
2635 size_t size
, size_t *expanded_capacity
)
2637 if (size
<= *expanded_capacity
)
2643 XRESIZEVEC (const cpp_token
*, arg
->expanded
, size
);
2644 *expanded_capacity
= size
;
2646 if (CPP_OPTION (pfile
, track_macro_expansion
))
2648 if (arg
->expanded_virt_locs
== NULL
)
2649 arg
->expanded_virt_locs
= XNEWVEC (location_t
, size
);
2651 arg
->expanded_virt_locs
= XRESIZEVEC (location_t
,
2652 arg
->expanded_virt_locs
,
2657 /* Expand an argument ARG before replacing parameters in a
2658 function-like macro. This works by pushing a context with the
2659 argument's tokens, and then expanding that into a temporary buffer
2660 as if it were a normal part of the token stream. collect_args()
2661 has terminated the argument's tokens with a CPP_EOF so that we know
2662 when we have fully expanded the argument. */
2664 expand_arg (cpp_reader
*pfile
, macro_arg
*arg
)
2667 bool saved_warn_trad
;
2668 bool track_macro_exp_p
= CPP_OPTION (pfile
, track_macro_expansion
);
2669 bool saved_ignore__Pragma
;
2672 || arg
->expanded
!= NULL
)
2675 /* Don't warn about funlike macros when pre-expanding. */
2676 saved_warn_trad
= CPP_WTRADITIONAL (pfile
);
2677 CPP_WTRADITIONAL (pfile
) = 0;
2679 /* Loop, reading in the tokens of the argument. */
2681 alloc_expanded_arg_mem (pfile
, arg
, capacity
);
2683 if (track_macro_exp_p
)
2684 push_extended_tokens_context (pfile
, NULL
, NULL
,
2689 push_ptoken_context (pfile
, NULL
, NULL
,
2690 arg
->first
, arg
->count
+ 1);
2692 saved_ignore__Pragma
= pfile
->state
.ignore__Pragma
;
2693 pfile
->state
.ignore__Pragma
= 1;
2697 const cpp_token
*token
;
2698 location_t location
;
2700 ensure_expanded_arg_room (pfile
, arg
, arg
->expanded_count
+ 1,
2703 token
= cpp_get_token_1 (pfile
, &location
);
2705 if (token
->type
== CPP_EOF
)
2708 set_arg_token (arg
, token
, location
,
2709 arg
->expanded_count
, MACRO_ARG_TOKEN_EXPANDED
,
2710 CPP_OPTION (pfile
, track_macro_expansion
));
2711 arg
->expanded_count
++;
2714 _cpp_pop_context (pfile
);
2716 CPP_WTRADITIONAL (pfile
) = saved_warn_trad
;
2717 pfile
->state
.ignore__Pragma
= saved_ignore__Pragma
;
2720 /* Returns the macro associated to the current context if we are in
2721 the context a macro expansion, NULL otherwise. */
2722 static cpp_hashnode
*
2723 macro_of_context (cpp_context
*context
)
2725 if (context
== NULL
)
2728 return (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2729 ? context
->c
.mc
->macro_node
2733 /* Return TRUE iff we are expanding a macro or are about to start
2734 expanding one. If we are effectively expanding a macro, the
2735 function macro_of_context returns a pointer to the macro being
2738 in_macro_expansion_p (cpp_reader
*pfile
)
2743 return (pfile
->about_to_expand_macro_p
2744 || macro_of_context (pfile
->context
));
2747 /* Pop the current context off the stack, re-enabling the macro if the
2748 context represented a macro's replacement list. Initially the
2749 context structure was not freed so that we can re-use it later, but
2750 now we do free it to reduce peak memory consumption. */
2752 _cpp_pop_context (cpp_reader
*pfile
)
2754 cpp_context
*context
= pfile
->context
;
2756 /* We should not be popping the base context. */
2757 gcc_assert (context
!= &pfile
->base_context
);
2759 if (context
->c
.macro
)
2761 cpp_hashnode
*macro
;
2762 if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2764 macro_context
*mc
= context
->c
.mc
;
2765 macro
= mc
->macro_node
;
2766 /* If context->buff is set, it means the life time of tokens
2767 is bound to the life time of this context; so we must
2768 free the tokens; that means we must free the virtual
2769 locations of these tokens too. */
2770 if (context
->buff
&& mc
->virt_locs
)
2772 free (mc
->virt_locs
);
2773 mc
->virt_locs
= NULL
;
2776 context
->c
.mc
= NULL
;
2779 macro
= context
->c
.macro
;
2781 /* Beware that MACRO can be NULL in cases like when we are
2782 called from expand_arg. In those cases, a dummy context with
2783 tokens is pushed just for the purpose of walking them using
2784 cpp_get_token_1. In that case, no 'macro' field is set into
2785 the dummy context. */
2787 /* Several contiguous macro expansion contexts can be
2788 associated to the same macro; that means it's the same
2789 macro expansion that spans across all these (sub)
2790 contexts. So we should re-enable an expansion-disabled
2791 macro only when we are sure we are really out of that
2793 && macro_of_context (context
->prev
) != macro
)
2794 macro
->flags
&= ~NODE_DISABLED
;
2796 if (macro
== pfile
->top_most_macro_node
&& context
->prev
== NULL
)
2797 /* We are popping the context of the top-most macro node. */
2798 pfile
->top_most_macro_node
= NULL
;
2803 /* Decrease memory peak consumption by freeing the memory used
2805 _cpp_free_buff (context
->buff
);
2808 pfile
->context
= context
->prev
;
2809 /* decrease peak memory consumption by feeing the context. */
2810 pfile
->context
->next
= NULL
;
2814 /* Return TRUE if we reached the end of the set of tokens stored in
2815 CONTEXT, FALSE otherwise. */
2817 reached_end_of_context (cpp_context
*context
)
2819 if (context
->tokens_kind
== TOKENS_KIND_DIRECT
)
2820 return FIRST (context
).token
== LAST (context
).token
;
2821 else if (context
->tokens_kind
== TOKENS_KIND_INDIRECT
2822 || context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2823 return FIRST (context
).ptoken
== LAST (context
).ptoken
;
2828 /* Consume the next token contained in the current context of PFILE,
2829 and return it in *TOKEN. It's "full location" is returned in
2830 *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
2831 means the location encoding the locus of the token across macro
2832 expansion; otherwise it's just is the "normal" location of the
2833 token which (*TOKEN)->src_loc. */
2835 consume_next_token_from_context (cpp_reader
*pfile
,
2836 const cpp_token
** token
,
2837 location_t
*location
)
2839 cpp_context
*c
= pfile
->context
;
2841 if ((c
)->tokens_kind
== TOKENS_KIND_DIRECT
)
2843 *token
= FIRST (c
).token
;
2844 *location
= (*token
)->src_loc
;
2847 else if ((c
)->tokens_kind
== TOKENS_KIND_INDIRECT
)
2849 *token
= *FIRST (c
).ptoken
;
2850 *location
= (*token
)->src_loc
;
2853 else if ((c
)->tokens_kind
== TOKENS_KIND_EXTENDED
)
2855 macro_context
*m
= c
->c
.mc
;
2856 *token
= *FIRST (c
).ptoken
;
2859 *location
= *m
->cur_virt_loc
;
2863 *location
= (*token
)->src_loc
;
2870 /* In the traditional mode of the preprocessor, if we are currently in
2871 a directive, the location of a token must be the location of the
2872 start of the directive line. This function returns the proper
2873 location if we are in the traditional mode, and just returns
2874 LOCATION otherwise. */
2876 static inline location_t
2877 maybe_adjust_loc_for_trad_cpp (cpp_reader
*pfile
, location_t location
)
2879 if (CPP_OPTION (pfile
, traditional
))
2881 if (pfile
->state
.in_directive
)
2882 return pfile
->directive_line
;
2887 /* Routine to get a token as well as its location.
2889 Macro expansions and directives are transparently handled,
2890 including entering included files. Thus tokens are post-macro
2891 expansion, and after any intervening directives. External callers
2892 see CPP_EOF only at EOF. Internal callers also see it when meeting
2893 a directive inside a macro call, when at the end of a directive and
2894 state.in_directive is still 1, and at the end of argument
2897 LOC is an out parameter; *LOC is set to the location "as expected
2898 by the user". Please read the comment of
2899 cpp_get_token_with_location to learn more about the meaning of this
2901 static const cpp_token
*
2902 cpp_get_token_1 (cpp_reader
*pfile
, location_t
*location
)
2904 const cpp_token
*result
;
2905 /* This token is a virtual token that either encodes a location
2906 related to macro expansion or a spelling location. */
2907 location_t virt_loc
= 0;
2908 /* pfile->about_to_expand_macro_p can be overriden by indirect calls
2909 to functions that push macro contexts. So let's save it so that
2910 we can restore it when we are about to leave this routine. */
2911 bool saved_about_to_expand_macro
= pfile
->about_to_expand_macro_p
;
2916 cpp_context
*context
= pfile
->context
;
2918 /* Context->prev == 0 <=> base context. */
2921 result
= _cpp_lex_token (pfile
);
2922 virt_loc
= result
->src_loc
;
2924 else if (!reached_end_of_context (context
))
2926 consume_next_token_from_context (pfile
, &result
,
2928 if (result
->flags
& PASTE_LEFT
)
2930 paste_all_tokens (pfile
, result
);
2931 if (pfile
->state
.in_directive
)
2933 result
= padding_token (pfile
, result
);
2939 if (pfile
->context
->c
.macro
)
2940 ++num_expanded_macros_counter
;
2941 _cpp_pop_context (pfile
);
2942 if (pfile
->state
.in_directive
)
2944 result
= &pfile
->avoid_paste
;
2948 if (pfile
->state
.in_directive
&& result
->type
== CPP_COMMENT
)
2951 if (result
->type
!= CPP_NAME
)
2954 node
= result
->val
.node
.node
;
2956 if (node
->type
== NT_VOID
|| (result
->flags
& NO_EXPAND
))
2959 if (!(node
->flags
& NODE_USED
)
2960 && node
->type
== NT_USER_MACRO
2961 && !node
->value
.macro
2962 && !cpp_get_deferred_macro (pfile
, node
, result
->src_loc
))
2965 if (!(node
->flags
& NODE_DISABLED
))
2968 /* If not in a macro context, and we're going to start an
2969 expansion, record the location and the top level macro
2970 about to be expanded. */
2971 if (!in_macro_expansion_p (pfile
))
2973 pfile
->invocation_location
= result
->src_loc
;
2974 pfile
->top_most_macro_node
= node
;
2976 if (pfile
->state
.prevent_expansion
)
2979 /* Conditional macros require that a predicate be evaluated
2981 if ((node
->flags
& NODE_CONDITIONAL
) != 0)
2983 if (pfile
->cb
.macro_to_expand
)
2985 bool whitespace_after
;
2986 const cpp_token
*peek_tok
= cpp_peek_token (pfile
, 0);
2988 whitespace_after
= (peek_tok
->type
== CPP_PADDING
2989 || (peek_tok
->flags
& PREV_WHITE
));
2990 node
= pfile
->cb
.macro_to_expand (pfile
, result
);
2992 ret
= enter_macro_context (pfile
, node
, result
, virt_loc
);
2993 else if (whitespace_after
)
2995 /* If macro_to_expand hook returned NULL and it
2996 ate some tokens, see if we don't need to add
2997 a padding token in between this and the
2999 peek_tok
= cpp_peek_token (pfile
, 0);
3000 if (peek_tok
->type
!= CPP_PADDING
3001 && (peek_tok
->flags
& PREV_WHITE
) == 0)
3002 _cpp_push_token_context (pfile
, NULL
,
3003 padding_token (pfile
,
3009 ret
= enter_macro_context (pfile
, node
, result
, virt_loc
);
3012 if (pfile
->state
.in_directive
|| ret
== 2)
3014 result
= padding_token (pfile
, result
);
3020 /* Flag this token as always unexpandable. FIXME: move this
3021 to collect_args()?. */
3022 cpp_token
*t
= _cpp_temp_token (pfile
);
3023 t
->type
= result
->type
;
3024 t
->flags
= result
->flags
| NO_EXPAND
;
3025 t
->val
= result
->val
;
3033 if (location
!= NULL
)
3036 virt_loc
= result
->src_loc
;
3037 *location
= virt_loc
;
3039 if (!CPP_OPTION (pfile
, track_macro_expansion
)
3040 && macro_of_context (pfile
->context
) != NULL
)
3041 /* We are in a macro expansion context, are not tracking
3042 virtual location, but were asked to report the location
3043 of the expansion point of the macro being expanded. */
3044 *location
= pfile
->invocation_location
;
3046 *location
= maybe_adjust_loc_for_trad_cpp (pfile
, *location
);
3049 pfile
->about_to_expand_macro_p
= saved_about_to_expand_macro
;
3051 if (pfile
->state
.directive_file_token
3052 && !pfile
->state
.parsing_args
3053 && !(result
->type
== CPP_PADDING
|| result
->type
== CPP_COMMENT
)
3054 && !(15 & --pfile
->state
.directive_file_token
))
3056 /* Do header-name frobbery. Concatenate < ... > as approprate.
3057 Do header search if needed, and finally drop the outer <> or
3059 pfile
->state
.angled_headers
= false;
3061 /* Do angle-header reconstitution. Then do include searching.
3062 We'll always end up with a ""-quoted header-name in that
3063 case. If searching finds nothing, we emit a diagnostic and
3068 cpp_token
*tmp
= _cpp_temp_token (pfile
);
3071 tmp
->type
= CPP_HEADER_NAME
;
3072 bool need_search
= !pfile
->state
.directive_file_token
;
3073 pfile
->state
.directive_file_token
= 0;
3075 bool angle
= result
->type
!= CPP_STRING
;
3076 if (result
->type
== CPP_HEADER_NAME
3077 || (result
->type
== CPP_STRING
&& result
->val
.str
.text
[0] != 'R'))
3079 len
= result
->val
.str
.len
- 2;
3080 fname
= XNEWVEC (char, len
+ 1);
3081 memcpy (fname
, result
->val
.str
.text
+ 1, len
);
3084 else if (result
->type
== CPP_LESS
)
3085 fname
= _cpp_bracket_include (pfile
);
3089 /* We have a header-name. Look it up. This will emit an
3090 unfound diagnostic. Canonicalize the found name. */
3091 const char *found
= fname
;
3095 found
= _cpp_find_header_unit (pfile
, fname
, angle
, tmp
->src_loc
);
3098 len
= strlen (found
);
3100 /* Force a leading './' if it's not absolute. */
3101 bool dotme
= (found
[0] == '.' ? !IS_DIR_SEPARATOR (found
[1])
3102 : found
[0] && !IS_ABSOLUTE_PATH (found
));
3104 if (BUFF_ROOM (pfile
->u_buff
) < len
+ 1 + dotme
* 2)
3105 _cpp_extend_buff (pfile
, &pfile
->u_buff
, len
+ 1 + dotme
* 2);
3106 unsigned char *buf
= BUFF_FRONT (pfile
->u_buff
);
3112 /* Apparently '/' is unconditional. */
3115 memcpy (&buf
[pos
], found
, len
);
3119 tmp
->val
.str
.len
= pos
;
3120 tmp
->val
.str
.text
= buf
;
3122 tmp
->type
= CPP_HEADER_NAME
;
3132 /* External routine to get a token. Also used nearly everywhere
3133 internally, except for places where we know we can safely call
3134 _cpp_lex_token directly, such as lexing a directive name.
3136 Macro expansions and directives are transparently handled,
3137 including entering included files. Thus tokens are post-macro
3138 expansion, and after any intervening directives. External callers
3139 see CPP_EOF only at EOF. Internal callers also see it when meeting
3140 a directive inside a macro call, when at the end of a directive and
3141 state.in_directive is still 1, and at the end of argument
3144 cpp_get_token (cpp_reader
*pfile
)
3146 return cpp_get_token_1 (pfile
, NULL
);
3149 /* Like cpp_get_token, but also returns a virtual token location
3150 separate from the spelling location carried by the returned token.
3152 LOC is an out parameter; *LOC is set to the location "as expected
3153 by the user". This matters when a token results from macro
3154 expansion; in that case the token's spelling location indicates the
3155 locus of the token in the definition of the macro but *LOC
3156 virtually encodes all the other meaningful locuses associated to
3159 What? virtual location? Yes, virtual location.
3161 If the token results from macro expansion and if macro expansion
3162 location tracking is enabled its virtual location encodes (at the
3165 - the spelling location of the token
3167 - the locus of the macro expansion point
3169 - the locus of the point where the token got instantiated as part
3170 of the macro expansion process.
3172 You have to use the linemap API to get the locus you are interested
3173 in from a given virtual location.
3175 Note however that virtual locations are not necessarily ordered for
3176 relations '<' and '>'. One must use the function
3177 linemap_location_before_p instead of using the relational operator
3180 If macro expansion tracking is off and if the token results from
3181 macro expansion the virtual location is the expansion point of the
3182 macro that got expanded.
3184 When the token doesn't result from macro expansion, the virtual
3185 location is just the same thing as its spelling location. */
3188 cpp_get_token_with_location (cpp_reader
*pfile
, location_t
*loc
)
3190 return cpp_get_token_1 (pfile
, loc
);
3193 /* Returns true if we're expanding an object-like macro that was
3194 defined in a system header. Just checks the macro at the top of
3195 the stack. Used for diagnostic suppression.
3196 Also return true for builtin macros. */
3198 cpp_sys_macro_p (cpp_reader
*pfile
)
3200 cpp_hashnode
*node
= NULL
;
3202 if (pfile
->context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
3203 node
= pfile
->context
->c
.mc
->macro_node
;
3205 node
= pfile
->context
->c
.macro
;
3209 if (cpp_builtin_macro_p (node
))
3211 return node
->value
.macro
&& node
->value
.macro
->syshdr
;
3214 /* Read each token in, until end of the current file. Directives are
3215 transparently processed. */
3217 cpp_scan_nooutput (cpp_reader
*pfile
)
3219 /* Request a CPP_EOF token at the end of this file, rather than
3220 transparently continuing with the including file. */
3221 pfile
->buffer
->return_at_eof
= true;
3223 pfile
->state
.discarding_output
++;
3224 pfile
->state
.prevent_expansion
++;
3226 if (CPP_OPTION (pfile
, traditional
))
3227 while (_cpp_read_logical_line_trad (pfile
))
3230 while (cpp_get_token (pfile
)->type
!= CPP_EOF
)
3233 pfile
->state
.discarding_output
--;
3234 pfile
->state
.prevent_expansion
--;
3237 /* Step back one or more tokens obtained from the lexer. */
3239 _cpp_backup_tokens_direct (cpp_reader
*pfile
, unsigned int count
)
3241 pfile
->lookaheads
+= count
;
3245 if (pfile
->cur_token
== pfile
->cur_run
->base
3246 /* Possible with -fpreprocessed and no leading #line. */
3247 && pfile
->cur_run
->prev
!= NULL
)
3249 pfile
->cur_run
= pfile
->cur_run
->prev
;
3250 pfile
->cur_token
= pfile
->cur_run
->limit
;
3255 /* Step back one (or more) tokens. Can only step back more than 1 if
3256 they are from the lexer, and not from macro expansion. */
3258 _cpp_backup_tokens (cpp_reader
*pfile
, unsigned int count
)
3260 if (pfile
->context
->prev
== NULL
)
3261 _cpp_backup_tokens_direct (pfile
, count
);
3266 if (pfile
->context
->tokens_kind
== TOKENS_KIND_DIRECT
)
3267 FIRST (pfile
->context
).token
--;
3268 else if (pfile
->context
->tokens_kind
== TOKENS_KIND_INDIRECT
)
3269 FIRST (pfile
->context
).ptoken
--;
3270 else if (pfile
->context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
3272 FIRST (pfile
->context
).ptoken
--;
3273 if (pfile
->context
->c
.macro
)
3275 macro_context
*m
= pfile
->context
->c
.mc
;
3277 gcc_checking_assert (m
->cur_virt_loc
>= m
->virt_locs
);
3287 /* #define directive parsing and handling. */
3289 /* Returns true if a macro redefinition warning is required. */
3291 warn_of_redefinition (cpp_reader
*pfile
, cpp_hashnode
*node
,
3292 const cpp_macro
*macro2
)
3294 /* Some redefinitions need to be warned about regardless. */
3295 if (node
->flags
& NODE_WARN
)
3298 /* Suppress warnings for builtins that lack the NODE_WARN flag,
3299 unless Wbuiltin-macro-redefined. */
3300 if (cpp_builtin_macro_p (node
))
3301 return CPP_OPTION (pfile
, warn_builtin_macro_redefined
);
3303 /* Redefinitions of conditional (context-sensitive) macros, on
3304 the other hand, must be allowed silently. */
3305 if (node
->flags
& NODE_CONDITIONAL
)
3308 if (cpp_macro
*macro1
= get_deferred_or_lazy_macro (pfile
, node
, macro2
->line
))
3309 return cpp_compare_macros (macro1
, macro2
);
3313 /* Return TRUE if MACRO1 and MACRO2 differ. */
3316 cpp_compare_macros (const cpp_macro
*macro1
, const cpp_macro
*macro2
)
3318 /* Redefinition of a macro is allowed if and only if the old and new
3319 definitions are the same. (6.10.3 paragraph 2). */
3321 /* Don't check count here as it can be different in valid
3322 traditional redefinitions with just whitespace differences. */
3323 if (macro1
->paramc
!= macro2
->paramc
3324 || macro1
->fun_like
!= macro2
->fun_like
3325 || macro1
->variadic
!= macro2
->variadic
)
3328 /* Check parameter spellings. */
3329 for (unsigned i
= macro1
->paramc
; i
--; )
3330 if (macro1
->parm
.params
[i
] != macro2
->parm
.params
[i
])
3333 /* Check the replacement text or tokens. */
3334 if (macro1
->kind
== cmk_traditional
)
3335 return _cpp_expansions_different_trad (macro1
, macro2
);
3337 if (macro1
->count
!= macro2
->count
)
3340 for (unsigned i
= macro1
->count
; i
--; )
3341 if (!_cpp_equiv_tokens (¯o1
->exp
.tokens
[i
], ¯o2
->exp
.tokens
[i
]))
3347 /* Free the definition of hashnode H. */
3349 _cpp_free_definition (cpp_hashnode
*h
)
3351 /* Macros and assertions no longer have anything to free. */
3353 h
->value
.answers
= NULL
;
3354 h
->flags
&= ~(NODE_DISABLED
| NODE_USED
);
3357 /* Save parameter NODE (spelling SPELLING) to the parameter list of
3358 macro MACRO. Returns true on success, false on failure. */
3360 _cpp_save_parameter (cpp_reader
*pfile
, unsigned n
, cpp_hashnode
*node
,
3361 cpp_hashnode
*spelling
)
3363 /* Constraint 6.10.3.6 - duplicate parameter names. */
3364 if (node
->type
== NT_MACRO_ARG
)
3366 cpp_error (pfile
, CPP_DL_ERROR
, "duplicate macro parameter \"%s\"",
3371 unsigned len
= (n
+ 1) * sizeof (struct macro_arg_saved_data
);
3372 if (len
> pfile
->macro_buffer_len
)
3375 = XRESIZEVEC (unsigned char, pfile
->macro_buffer
, len
);
3376 pfile
->macro_buffer_len
= len
;
3379 macro_arg_saved_data
*saved
= (macro_arg_saved_data
*)pfile
->macro_buffer
;
3380 saved
[n
].canonical_node
= node
;
3381 saved
[n
].value
= node
->value
;
3382 saved
[n
].type
= node
->type
;
3384 void *base
= _cpp_reserve_room (pfile
, n
* sizeof (cpp_hashnode
*),
3385 sizeof (cpp_hashnode
*));
3386 ((cpp_hashnode
**)base
)[n
] = spelling
;
3388 /* Morph into a macro arg. */
3389 node
->type
= NT_MACRO_ARG
;
3390 /* Index is 1 based. */
3391 node
->value
.arg_index
= n
+ 1;
3396 /* Restore the parameters to their previous state. */
3398 _cpp_unsave_parameters (cpp_reader
*pfile
, unsigned n
)
3400 /* Clear the fast argument lookup indices. */
3403 struct macro_arg_saved_data
*save
=
3404 &((struct macro_arg_saved_data
*) pfile
->macro_buffer
)[n
];
3406 struct cpp_hashnode
*node
= save
->canonical_node
;
3407 node
->type
= save
->type
;
3408 node
->value
= save
->value
;
3412 /* Check the syntax of the parameters in a MACRO definition. Return
3413 false on failure. Set *N_PTR and *VARADIC_PTR as appropriate.
3415 '(' parm-list ',' last-parm ')'
3425 parse_params (cpp_reader
*pfile
, unsigned *n_ptr
, bool *varadic_ptr
)
3427 unsigned nparms
= 0;
3430 for (bool prev_ident
= false;;)
3432 const cpp_token
*token
= _cpp_lex_token (pfile
);
3434 switch (token
->type
)
3437 /* Allow/ignore comments in parameter lists if we are
3438 preserving comments in macro expansions. */
3439 if (!CPP_OPTION (pfile
, discard_comments_in_macro_exp
))
3446 const char *const msgs
[5] =
3448 N_("expected parameter name, found \"%s\""),
3449 N_("expected ',' or ')', found \"%s\""),
3450 N_("expected parameter name before end of line"),
3451 N_("expected ')' before end of line"),
3452 N_("expected ')' after \"...\"")
3454 unsigned ix
= prev_ident
;
3455 const unsigned char *as_text
= NULL
;
3458 else if (token
->type
== CPP_EOF
)
3461 as_text
= cpp_token_as_text (pfile
, token
);
3462 cpp_error (pfile
, CPP_DL_ERROR
, msgs
[ix
], as_text
);
3467 if (prev_ident
|| *varadic_ptr
)
3471 if (!_cpp_save_parameter (pfile
, nparms
, token
->val
.node
.node
,
3472 token
->val
.node
.spelling
))
3477 case CPP_CLOSE_PAREN
:
3478 if (prev_ident
|| !nparms
|| *varadic_ptr
)
3486 if (!prev_ident
|| *varadic_ptr
)
3494 *varadic_ptr
= true;
3497 /* An ISO bare ellipsis. */
3498 _cpp_save_parameter (pfile
, nparms
,
3499 pfile
->spec_nodes
.n__VA_ARGS__
,
3500 pfile
->spec_nodes
.n__VA_ARGS__
);
3502 pfile
->state
.va_args_ok
= 1;
3503 if (! CPP_OPTION (pfile
, c99
)
3504 && CPP_OPTION (pfile
, cpp_pedantic
)
3505 && CPP_OPTION (pfile
, warn_variadic_macros
))
3507 (pfile
, CPP_W_VARIADIC_MACROS
,
3508 CPP_OPTION (pfile
, cplusplus
)
3509 ? N_("anonymous variadic macros were introduced in C++11")
3510 : N_("anonymous variadic macros were introduced in C99"));
3511 else if (CPP_OPTION (pfile
, cpp_warn_c90_c99_compat
) > 0
3512 && ! CPP_OPTION (pfile
, cplusplus
))
3513 cpp_error (pfile
, CPP_DL_WARNING
,
3514 "anonymous variadic macros were introduced in C99");
3516 else if (CPP_OPTION (pfile
, cpp_pedantic
)
3517 && CPP_OPTION (pfile
, warn_variadic_macros
))
3518 cpp_pedwarning (pfile
, CPP_W_VARIADIC_MACROS
,
3519 CPP_OPTION (pfile
, cplusplus
)
3520 ? N_("ISO C++ does not permit named variadic macros")
3521 : N_("ISO C does not permit named variadic macros"));
3532 /* Lex a token from the expansion of MACRO, but mark parameters as we
3533 find them and warn of traditional stringification. */
3535 lex_expansion_token (cpp_reader
*pfile
, cpp_macro
*macro
)
3537 macro
= (cpp_macro
*)_cpp_reserve_room (pfile
,
3538 sizeof (cpp_macro
) - sizeof (cpp_token
)
3539 + macro
->count
* sizeof (cpp_token
),
3540 sizeof (cpp_token
));
3541 cpp_token
*saved_cur_token
= pfile
->cur_token
;
3542 pfile
->cur_token
= ¯o
->exp
.tokens
[macro
->count
];
3543 cpp_token
*token
= _cpp_lex_direct (pfile
);
3544 pfile
->cur_token
= saved_cur_token
;
3546 /* Is this a parameter? */
3547 if (token
->type
== CPP_NAME
&& token
->val
.node
.node
->type
== NT_MACRO_ARG
)
3549 /* Morph into a parameter reference. */
3550 cpp_hashnode
*spelling
= token
->val
.node
.spelling
;
3551 token
->type
= CPP_MACRO_ARG
;
3552 token
->val
.macro_arg
.arg_no
= token
->val
.node
.node
->value
.arg_index
;
3553 token
->val
.macro_arg
.spelling
= spelling
;
3555 else if (CPP_WTRADITIONAL (pfile
) && macro
->paramc
> 0
3556 && (token
->type
== CPP_STRING
|| token
->type
== CPP_CHAR
))
3557 check_trad_stringification (pfile
, macro
, &token
->val
.str
);
3563 create_iso_definition (cpp_reader
*pfile
)
3565 bool following_paste_op
= false;
3566 const char *paste_op_error_msg
=
3567 N_("'##' cannot appear at either end of a macro expansion");
3568 unsigned int num_extra_tokens
= 0;
3569 unsigned nparms
= 0;
3570 cpp_hashnode
**params
= NULL
;
3571 bool varadic
= false;
3573 cpp_macro
*macro
= NULL
;
3575 /* Look at the first token, to see if this is a function-like
3578 cpp_token
*saved_cur_token
= pfile
->cur_token
;
3579 pfile
->cur_token
= &first
;
3580 cpp_token
*token
= _cpp_lex_direct (pfile
);
3581 pfile
->cur_token
= saved_cur_token
;
3583 if (token
->flags
& PREV_WHITE
)
3584 /* Preceeded by space, must be part of expansion. */;
3585 else if (token
->type
== CPP_OPEN_PAREN
)
3587 /* An open-paren, get a parameter list. */
3588 if (!parse_params (pfile
, &nparms
, &varadic
))
3591 params
= (cpp_hashnode
**)_cpp_commit_buff
3592 (pfile
, sizeof (cpp_hashnode
*) * nparms
);
3595 else if (token
->type
!= CPP_EOF
3596 && !(token
->type
== CPP_COMMENT
3597 && ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
)))
3599 /* While ISO C99 requires whitespace before replacement text
3600 in a macro definition, ISO C90 with TC1 allows characters
3601 from the basic source character set there. */
3602 if (CPP_OPTION (pfile
, c99
))
3603 cpp_error (pfile
, CPP_DL_PEDWARN
,
3604 CPP_OPTION (pfile
, cplusplus
)
3605 ? N_("ISO C++11 requires whitespace after the macro name")
3606 : N_("ISO C99 requires whitespace after the macro name"));
3609 enum cpp_diagnostic_level warntype
= CPP_DL_WARNING
;
3610 switch (token
->type
)
3614 case CPP_OBJC_STRING
:
3615 /* '@' is not in basic character set. */
3616 warntype
= CPP_DL_PEDWARN
;
3619 /* Basic character set sans letters, digits and _. */
3620 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
3621 token
->val
.str
.text
[0]) == NULL
)
3622 warntype
= CPP_DL_PEDWARN
;
3625 /* All other tokens start with a character from basic
3629 cpp_error (pfile
, warntype
,
3630 "missing whitespace after the macro name");
3634 macro
= _cpp_new_macro (pfile
, cmk_macro
,
3635 _cpp_reserve_room (pfile
, 0, sizeof (cpp_macro
)));
3639 macro
->variadic
= varadic
;
3640 macro
->paramc
= nparms
;
3641 macro
->parm
.params
= params
;
3642 macro
->fun_like
= true;
3646 /* Preserve the token we peeked, there is already a single slot for it. */
3647 macro
->exp
.tokens
[0] = *token
;
3648 token
= ¯o
->exp
.tokens
[0];
3652 for (vaopt_state
vaopt_tracker (pfile
, macro
->variadic
, NULL
);; token
= NULL
)
3656 macro
= lex_expansion_token (pfile
, macro
);
3657 token
= ¯o
->exp
.tokens
[macro
->count
++];
3660 /* Check the stringifying # constraint 6.10.3.2.1 of
3661 function-like macros when lexing the subsequent token. */
3662 if (macro
->count
> 1 && token
[-1].type
== CPP_HASH
&& macro
->fun_like
)
3664 if (token
->type
== CPP_MACRO_ARG
3666 && token
->type
== CPP_NAME
3667 && token
->val
.node
.node
== pfile
->spec_nodes
.n__VA_OPT__
))
3669 if (token
->flags
& PREV_WHITE
)
3670 token
->flags
|= SP_PREV_WHITE
;
3671 if (token
[-1].flags
& DIGRAPH
)
3672 token
->flags
|= SP_DIGRAPH
;
3673 token
->flags
&= ~PREV_WHITE
;
3674 token
->flags
|= STRINGIFY_ARG
;
3675 token
->flags
|= token
[-1].flags
& PREV_WHITE
;
3676 token
[-1] = token
[0];
3679 /* Let assembler get away with murder. */
3680 else if (CPP_OPTION (pfile
, lang
) != CLK_ASM
)
3682 cpp_error (pfile
, CPP_DL_ERROR
,
3683 "'#' is not followed by a macro parameter");
3688 if (token
->type
== CPP_EOF
)
3690 /* Paste operator constraint 6.10.3.3.1:
3691 Token-paste ##, can appear in both object-like and
3692 function-like macros, but not at the end. */
3693 if (following_paste_op
)
3695 cpp_error (pfile
, CPP_DL_ERROR
, paste_op_error_msg
);
3698 if (!vaopt_tracker
.completed ())
3703 /* Paste operator constraint 6.10.3.3.1. */
3704 if (token
->type
== CPP_PASTE
)
3706 /* Token-paste ##, can appear in both object-like and
3707 function-like macros, but not at the beginning. */
3708 if (macro
->count
== 1)
3710 cpp_error (pfile
, CPP_DL_ERROR
, paste_op_error_msg
);
3714 if (following_paste_op
)
3716 /* Consecutive paste operators. This one will be moved
3719 token
->val
.token_no
= macro
->count
- 1;
3723 /* Drop the paste operator. */
3725 token
[-1].flags
|= PASTE_LEFT
;
3726 if (token
->flags
& DIGRAPH
)
3727 token
[-1].flags
|= SP_DIGRAPH
;
3728 if (token
->flags
& PREV_WHITE
)
3729 token
[-1].flags
|= SP_PREV_WHITE
;
3731 following_paste_op
= true;
3734 following_paste_op
= false;
3736 if (vaopt_tracker
.update (token
) == vaopt_state::ERROR
)
3740 /* We're committed to winning now. */
3743 /* Don't count the CPP_EOF. */
3746 macro
= (cpp_macro
*)_cpp_commit_buff
3747 (pfile
, sizeof (cpp_macro
) - sizeof (cpp_token
)
3748 + sizeof (cpp_token
) * macro
->count
);
3750 /* Clear whitespace on first token. */
3752 macro
->exp
.tokens
[0].flags
&= ~PREV_WHITE
;
3754 if (num_extra_tokens
)
3756 /* Place second and subsequent ## or %:%: tokens in sequences of
3757 consecutive such tokens at the end of the list to preserve
3758 information about where they appear, how they are spelt and
3759 whether they are preceded by whitespace without otherwise
3760 interfering with macro expansion. Remember, this is
3761 extremely rare, so efficiency is not a priority. */
3762 cpp_token
*temp
= (cpp_token
*)_cpp_reserve_room
3763 (pfile
, 0, num_extra_tokens
* sizeof (cpp_token
));
3764 unsigned extra_ix
= 0, norm_ix
= 0;
3765 cpp_token
*exp
= macro
->exp
.tokens
;
3766 for (unsigned ix
= 0; ix
!= macro
->count
; ix
++)
3767 if (exp
[ix
].type
== CPP_PASTE
)
3768 temp
[extra_ix
++] = exp
[ix
];
3770 exp
[norm_ix
++] = exp
[ix
];
3771 memcpy (&exp
[norm_ix
], temp
, num_extra_tokens
* sizeof (cpp_token
));
3773 /* Record there are extra tokens. */
3774 macro
->extra_tokens
= 1;
3778 pfile
->state
.va_args_ok
= 0;
3779 _cpp_unsave_parameters (pfile
, nparms
);
3781 return ok
? macro
: NULL
;
3785 _cpp_new_macro (cpp_reader
*pfile
, cpp_macro_kind kind
, void *placement
)
3787 cpp_macro
*macro
= (cpp_macro
*) placement
;
3789 /* Zero init all the fields. This'll tell the compiler know all the
3790 following inits are writing a virgin object. */
3791 memset (macro
, 0, offsetof (cpp_macro
, exp
));
3793 macro
->line
= pfile
->directive_line
;
3794 macro
->parm
.params
= 0;
3797 macro
->variadic
= 0;
3798 macro
->used
= !CPP_OPTION (pfile
, warn_unused_macros
);
3800 macro
->fun_like
= 0;
3801 macro
->imported_p
= false;
3802 macro
->extra_tokens
= 0;
3803 /* To suppress some diagnostics. */
3804 macro
->syshdr
= pfile
->buffer
&& pfile
->buffer
->sysp
!= 0;
3811 /* Parse a macro and save its expansion. Returns nonzero on success. */
3813 _cpp_create_definition (cpp_reader
*pfile
, cpp_hashnode
*node
)
3817 if (CPP_OPTION (pfile
, traditional
))
3818 macro
= _cpp_create_trad_definition (pfile
);
3820 macro
= create_iso_definition (pfile
);
3825 if (cpp_macro_p (node
))
3827 if (CPP_OPTION (pfile
, warn_unused_macros
))
3828 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
3830 if (warn_of_redefinition (pfile
, node
, macro
))
3832 const enum cpp_warning_reason reason
3833 = (cpp_builtin_macro_p (node
) && !(node
->flags
& NODE_WARN
))
3834 ? CPP_W_BUILTIN_MACRO_REDEFINED
: CPP_W_NONE
;
3837 cpp_pedwarning_with_line (pfile
, reason
,
3838 pfile
->directive_line
, 0,
3839 "\"%s\" redefined", NODE_NAME (node
));
3841 if (warned
&& cpp_user_macro_p (node
))
3842 cpp_error_with_line (pfile
, CPP_DL_NOTE
,
3843 node
->value
.macro
->line
, 0,
3844 "this is the location of the previous definition");
3846 _cpp_free_definition (node
);
3849 /* Enter definition in hash table. */
3850 node
->type
= NT_USER_MACRO
;
3851 node
->value
.macro
= macro
;
3852 if (! ustrncmp (NODE_NAME (node
), DSC ("__STDC_"))
3853 && ustrcmp (NODE_NAME (node
), (const uchar
*) "__STDC_FORMAT_MACROS")
3854 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
3855 in the C standard, as something that one must use in C++.
3856 However DR#593 and C++11 indicate that they play no role in C++.
3857 We special-case them anyway. */
3858 && ustrcmp (NODE_NAME (node
), (const uchar
*) "__STDC_LIMIT_MACROS")
3859 && ustrcmp (NODE_NAME (node
), (const uchar
*) "__STDC_CONSTANT_MACROS"))
3860 node
->flags
|= NODE_WARN
;
3862 /* If user defines one of the conditional macros, remove the
3864 node
->flags
&= ~NODE_CONDITIONAL
;
3870 cpp_define_lazily (cpp_reader
*pfile
, cpp_hashnode
*node
, unsigned num
)
3872 cpp_macro
*macro
= node
->value
.macro
;
3874 gcc_checking_assert (pfile
->cb
.user_lazy_macro
&& macro
&& num
< UCHAR_MAX
);
3876 macro
->lazy
= num
+ 1;
3879 /* NODE is a deferred macro, resolve it, returning the definition
3880 (which may be NULL). */
3882 cpp_get_deferred_macro (cpp_reader
*pfile
, cpp_hashnode
*node
,
3885 gcc_checking_assert (node
->type
== NT_USER_MACRO
);
3887 node
->value
.macro
= pfile
->cb
.user_deferred_macro (pfile
, loc
, node
);
3889 if (!node
->value
.macro
)
3890 node
->type
= NT_VOID
;
3892 return node
->value
.macro
;
3896 get_deferred_or_lazy_macro (cpp_reader
*pfile
, cpp_hashnode
*node
,
3899 cpp_macro
*macro
= node
->value
.macro
;
3902 macro
= cpp_get_deferred_macro (pfile
, node
, loc
);
3903 gcc_checking_assert (!macro
|| !macro
->lazy
);
3905 else if (macro
->lazy
)
3907 pfile
->cb
.user_lazy_macro (pfile
, macro
, macro
->lazy
- 1);
3914 /* Notify the use of NODE in a macro-aware context (i.e. expanding it,
3915 or testing its existance). Also applies any lazy definition.
3916 Return FALSE if the macro isn't really there. */
3919 _cpp_notify_macro_use (cpp_reader
*pfile
, cpp_hashnode
*node
,
3922 node
->flags
|= NODE_USED
;
3926 if (!get_deferred_or_lazy_macro (pfile
, node
, loc
))
3930 case NT_BUILTIN_MACRO
:
3931 if (pfile
->cb
.used_define
)
3932 pfile
->cb
.used_define (pfile
, loc
, node
);
3936 if (pfile
->cb
.used_undef
)
3937 pfile
->cb
.used_undef (pfile
, loc
, node
);
3947 /* Warn if a token in STRING matches one of a function-like MACRO's
3950 check_trad_stringification (cpp_reader
*pfile
, const cpp_macro
*macro
,
3951 const cpp_string
*string
)
3953 unsigned int i
, len
;
3954 const uchar
*p
, *q
, *limit
;
3956 /* Loop over the string. */
3957 limit
= string
->text
+ string
->len
- 1;
3958 for (p
= string
->text
+ 1; p
< limit
; p
= q
)
3960 /* Find the start of an identifier. */
3961 while (p
< limit
&& !is_idstart (*p
))
3964 /* Find the end of the identifier. */
3966 while (q
< limit
&& is_idchar (*q
))
3971 /* Loop over the function macro arguments to see if the
3972 identifier inside the string matches one of them. */
3973 for (i
= 0; i
< macro
->paramc
; i
++)
3975 const cpp_hashnode
*node
= macro
->parm
.params
[i
];
3977 if (NODE_LEN (node
) == len
3978 && !memcmp (p
, NODE_NAME (node
), len
))
3980 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
3981 "macro argument \"%s\" would be stringified in traditional C",
3989 /* Returns the name, arguments and expansion of a macro, in a format
3990 suitable to be read back in again, and therefore also for DWARF 2
3991 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
3992 Caller is expected to generate the "#define" bit if needed. The
3993 returned text is temporary, and automatically freed later. */
3994 const unsigned char *
3995 cpp_macro_definition (cpp_reader
*pfile
, cpp_hashnode
*node
)
3997 gcc_checking_assert (cpp_user_macro_p (node
));
3999 if (const cpp_macro
*macro
= get_deferred_or_lazy_macro (pfile
, node
, 0))
4000 return cpp_macro_definition (pfile
, node
, macro
);
4004 const unsigned char *
4005 cpp_macro_definition (cpp_reader
*pfile
, cpp_hashnode
*node
,
4006 const cpp_macro
*macro
)
4008 unsigned int i
, len
;
4009 unsigned char *buffer
;
4011 /* Calculate length. */
4012 len
= NODE_LEN (node
) * 10 + 2; /* ' ' and NUL. */
4013 if (macro
->fun_like
)
4015 len
+= 4; /* "()" plus possible final ".." of named
4016 varargs (we have + 1 below). */
4017 for (i
= 0; i
< macro
->paramc
; i
++)
4018 len
+= NODE_LEN (macro
->parm
.params
[i
]) + 1; /* "," */
4021 /* This should match below where we fill in the buffer. */
4022 if (CPP_OPTION (pfile
, traditional
))
4023 len
+= _cpp_replacement_text_len (macro
);
4026 unsigned int count
= macro_real_token_count (macro
);
4027 for (i
= 0; i
< count
; i
++)
4029 const cpp_token
*token
= ¯o
->exp
.tokens
[i
];
4031 if (token
->type
== CPP_MACRO_ARG
)
4032 len
+= NODE_LEN (token
->val
.macro_arg
.spelling
);
4034 len
+= cpp_token_len (token
);
4036 if (token
->flags
& STRINGIFY_ARG
)
4038 if (token
->flags
& PASTE_LEFT
)
4039 len
+= 3; /* " ##" */
4040 if (token
->flags
& PREV_WHITE
)
4045 if (len
> pfile
->macro_buffer_len
)
4047 pfile
->macro_buffer
= XRESIZEVEC (unsigned char,
4048 pfile
->macro_buffer
, len
);
4049 pfile
->macro_buffer_len
= len
;
4052 /* Fill in the buffer. Start with the macro name. */
4053 buffer
= pfile
->macro_buffer
;
4054 buffer
= _cpp_spell_ident_ucns (buffer
, node
);
4056 /* Parameter names. */
4057 if (macro
->fun_like
)
4060 for (i
= 0; i
< macro
->paramc
; i
++)
4062 cpp_hashnode
*param
= macro
->parm
.params
[i
];
4064 if (param
!= pfile
->spec_nodes
.n__VA_ARGS__
)
4066 memcpy (buffer
, NODE_NAME (param
), NODE_LEN (param
));
4067 buffer
+= NODE_LEN (param
);
4070 if (i
+ 1 < macro
->paramc
)
4071 /* Don't emit a space after the comma here; we're trying
4072 to emit a Dwarf-friendly definition, and the Dwarf spec
4073 forbids spaces in the argument list. */
4075 else if (macro
->variadic
)
4076 *buffer
++ = '.', *buffer
++ = '.', *buffer
++ = '.';
4081 /* The Dwarf spec requires a space after the macro name, even if the
4082 definition is the empty string. */
4085 if (CPP_OPTION (pfile
, traditional
))
4086 buffer
= _cpp_copy_replacement_text (macro
, buffer
);
4087 else if (macro
->count
)
4088 /* Expansion tokens. */
4090 unsigned int count
= macro_real_token_count (macro
);
4091 for (i
= 0; i
< count
; i
++)
4093 const cpp_token
*token
= ¯o
->exp
.tokens
[i
];
4095 if (token
->flags
& PREV_WHITE
)
4097 if (token
->flags
& STRINGIFY_ARG
)
4100 if (token
->type
== CPP_MACRO_ARG
)
4103 NODE_NAME (token
->val
.macro_arg
.spelling
),
4104 NODE_LEN (token
->val
.macro_arg
.spelling
));
4105 buffer
+= NODE_LEN (token
->val
.macro_arg
.spelling
);
4108 buffer
= cpp_spell_token (pfile
, token
, buffer
, true);
4110 if (token
->flags
& PASTE_LEFT
)
4115 /* Next has PREV_WHITE; see _cpp_create_definition. */
4121 return pfile
->macro_buffer
;