1 /* Part of CPP library. (Macro and #define handling.)
2 Copyright (C) 1986-2015 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding! */
30 typedef struct macro_arg macro_arg
;
31 /* This structure represents the tokens of a macro argument. These
32 tokens can be macro themselves, in which case they can be either
33 expanded or unexpanded. When they are expanded, this data
34 structure keeps both the expanded and unexpanded forms. */
37 const cpp_token
**first
; /* First token in unexpanded argument. */
38 const cpp_token
**expanded
; /* Macro-expanded argument. */
39 const cpp_token
*stringified
; /* Stringified argument. */
40 unsigned int count
; /* # of tokens in argument. */
41 unsigned int expanded_count
; /* # of tokens in expanded argument. */
42 source_location
*virt_locs
; /* Where virtual locations for
43 unexpanded tokens are stored. */
44 source_location
*expanded_virt_locs
; /* Where virtual locations for
49 /* The kind of macro tokens which the instance of
50 macro_arg_token_iter is supposed to iterate over. */
51 enum macro_arg_token_kind
{
52 MACRO_ARG_TOKEN_NORMAL
,
53 /* This is a macro argument token that got transformed into a string
54 litteral, e.g. #foo. */
55 MACRO_ARG_TOKEN_STRINGIFIED
,
56 /* This is a token resulting from the expansion of a macro
57 argument that was itself a macro. */
58 MACRO_ARG_TOKEN_EXPANDED
61 /* An iterator over tokens coming from a function-like macro
63 typedef struct macro_arg_token_iter macro_arg_token_iter
;
64 struct macro_arg_token_iter
66 /* Whether or not -ftrack-macro-expansion is used. */
67 bool track_macro_exp_p
;
68 /* The kind of token over which we are supposed to iterate. */
69 enum macro_arg_token_kind kind
;
70 /* A pointer to the current token pointed to by the iterator. */
71 const cpp_token
**token_ptr
;
72 /* A pointer to the "full" location of the current token. If
73 -ftrack-macro-expansion is used this location tracks loci across
75 const source_location
*location_ptr
;
76 #ifdef ENABLE_CHECKING
77 /* The number of times the iterator went forward. This useful only
78 when checking is enabled. */
83 /* Saved data about an identifier being used as a macro argument
85 struct macro_arg_saved_data
{
86 /* The canonical (UTF-8) spelling of this identifier. */
87 cpp_hashnode
*canonical_node
;
88 /* The previous value of this identifier. */
89 union _cpp_hashnode_value value
;
92 /* Macro expansion. */
94 static int enter_macro_context (cpp_reader
*, cpp_hashnode
*,
95 const cpp_token
*, source_location
);
96 static int builtin_macro (cpp_reader
*, cpp_hashnode
*, source_location
);
97 static void push_ptoken_context (cpp_reader
*, cpp_hashnode
*, _cpp_buff
*,
98 const cpp_token
**, unsigned int);
99 static void push_extended_tokens_context (cpp_reader
*, cpp_hashnode
*,
100 _cpp_buff
*, source_location
*,
101 const cpp_token
**, unsigned int);
102 static _cpp_buff
*collect_args (cpp_reader
*, const cpp_hashnode
*,
103 _cpp_buff
**, unsigned *);
104 static cpp_context
*next_context (cpp_reader
*);
105 static const cpp_token
*padding_token (cpp_reader
*, const cpp_token
*);
106 static void expand_arg (cpp_reader
*, macro_arg
*);
107 static const cpp_token
*new_string_token (cpp_reader
*, uchar
*, unsigned int);
108 static const cpp_token
*stringify_arg (cpp_reader
*, macro_arg
*);
109 static void paste_all_tokens (cpp_reader
*, const cpp_token
*);
110 static bool paste_tokens (cpp_reader
*, source_location
,
111 const cpp_token
**, const cpp_token
*);
112 static void alloc_expanded_arg_mem (cpp_reader
*, macro_arg
*, size_t);
113 static void ensure_expanded_arg_room (cpp_reader
*, macro_arg
*, size_t, size_t *);
114 static void delete_macro_args (_cpp_buff
*, unsigned num_args
);
115 static void set_arg_token (macro_arg
*, const cpp_token
*,
116 source_location
, size_t,
117 enum macro_arg_token_kind
,
119 static const source_location
*get_arg_token_location (const macro_arg
*,
120 enum macro_arg_token_kind
);
121 static const cpp_token
**arg_token_ptr_at (const macro_arg
*,
123 enum macro_arg_token_kind
,
124 source_location
**virt_location
);
126 static void macro_arg_token_iter_init (macro_arg_token_iter
*, bool,
127 enum macro_arg_token_kind
,
130 static const cpp_token
*macro_arg_token_iter_get_token
131 (const macro_arg_token_iter
*it
);
132 static source_location macro_arg_token_iter_get_location
133 (const macro_arg_token_iter
*);
134 static void macro_arg_token_iter_forward (macro_arg_token_iter
*);
135 static _cpp_buff
*tokens_buff_new (cpp_reader
*, size_t,
137 static size_t tokens_buff_count (_cpp_buff
*);
138 static const cpp_token
**tokens_buff_last_token_ptr (_cpp_buff
*);
139 static inline const cpp_token
**tokens_buff_put_token_to (const cpp_token
**,
144 const line_map_macro
*,
147 static const cpp_token
**tokens_buff_add_token (_cpp_buff
*,
152 const line_map_macro
*,
154 static inline void tokens_buff_remove_last_token (_cpp_buff
*);
155 static void replace_args (cpp_reader
*, cpp_hashnode
*, cpp_macro
*,
156 macro_arg
*, source_location
);
157 static _cpp_buff
*funlike_invocation_p (cpp_reader
*, cpp_hashnode
*,
158 _cpp_buff
**, unsigned *);
159 static bool create_iso_definition (cpp_reader
*, cpp_macro
*);
161 /* #define directive parsing and handling. */
163 static cpp_token
*alloc_expansion_token (cpp_reader
*, cpp_macro
*);
164 static cpp_token
*lex_expansion_token (cpp_reader
*, cpp_macro
*);
165 static bool warn_of_redefinition (cpp_reader
*, cpp_hashnode
*,
167 static bool parse_params (cpp_reader
*, cpp_macro
*);
168 static void check_trad_stringification (cpp_reader
*, const cpp_macro
*,
170 static bool reached_end_of_context (cpp_context
*);
171 static void consume_next_token_from_context (cpp_reader
*pfile
,
174 static const cpp_token
* cpp_get_token_1 (cpp_reader
*, source_location
*);
176 static cpp_hashnode
* macro_of_context (cpp_context
*context
);
178 static bool in_macro_expansion_p (cpp_reader
*pfile
);
180 /* Statistical counter tracking the number of macros that got
182 unsigned num_expanded_macros_counter
= 0;
183 /* Statistical counter tracking the total number tokens resulting
184 from macro expansion. */
185 unsigned num_macro_tokens_counter
= 0;
187 /* Emits a warning if NODE is a macro defined in the main file that
188 has not been used. */
190 _cpp_warn_if_unused_macro (cpp_reader
*pfile
, cpp_hashnode
*node
,
191 void *v ATTRIBUTE_UNUSED
)
193 if (node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
))
195 cpp_macro
*macro
= node
->value
.macro
;
198 && MAIN_FILE_P (linemap_check_ordinary
199 (linemap_lookup (pfile
->line_table
,
201 cpp_warning_with_line (pfile
, CPP_W_UNUSED_MACROS
, macro
->line
, 0,
202 "macro \"%s\" is not used", NODE_NAME (node
));
208 /* Allocates and returns a CPP_STRING token, containing TEXT of length
209 LEN, after null-terminating it. TEXT must be in permanent storage. */
210 static const cpp_token
*
211 new_string_token (cpp_reader
*pfile
, unsigned char *text
, unsigned int len
)
213 cpp_token
*token
= _cpp_temp_token (pfile
);
216 token
->type
= CPP_STRING
;
217 token
->val
.str
.len
= len
;
218 token
->val
.str
.text
= text
;
223 static const char * const monthnames
[] =
225 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
226 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
229 /* Helper function for builtin_macro. Returns the text generated by
232 _cpp_builtin_macro_text (cpp_reader
*pfile
, cpp_hashnode
*node
)
234 const uchar
*result
= NULL
;
235 linenum_type number
= 1;
237 switch (node
->value
.builtin
)
240 cpp_error (pfile
, CPP_DL_ICE
, "invalid built-in macro \"%s\"",
246 if (CPP_OPTION (pfile
, warn_date_time
))
247 cpp_warning (pfile
, CPP_W_DATE_TIME
, "macro \"%s\" might prevent "
248 "reproducible builds", NODE_NAME (node
));
250 cpp_buffer
*pbuffer
= cpp_get_buffer (pfile
);
251 if (pbuffer
->timestamp
== NULL
)
253 /* Initialize timestamp value of the assotiated file. */
254 struct _cpp_file
*file
= cpp_get_file (pbuffer
);
257 /* Generate __TIMESTAMP__ string, that represents
258 the date and time of the last modification
259 of the current source file. The string constant
260 looks like "Sun Sep 16 01:03:52 1973". */
261 struct tm
*tb
= NULL
;
262 struct stat
*st
= _cpp_get_file_stat (file
);
264 tb
= localtime (&st
->st_mtime
);
267 char *str
= asctime (tb
);
268 size_t len
= strlen (str
);
269 unsigned char *buf
= _cpp_unaligned_alloc (pfile
, len
+ 2);
271 strcpy ((char *) buf
+ 1, str
);
273 pbuffer
->timestamp
= buf
;
277 cpp_errno (pfile
, CPP_DL_WARNING
,
278 "could not determine file timestamp");
279 pbuffer
->timestamp
= UC
"\"??? ??? ?? ??:??:?? ????\"";
283 result
= pbuffer
->timestamp
;
293 if (node
->value
.builtin
== BT_FILE
)
294 name
= linemap_get_expansion_filename (pfile
->line_table
,
295 pfile
->line_table
->highest_line
);
298 name
= _cpp_get_file_name (pfile
->main_file
);
303 buf
= _cpp_unaligned_alloc (pfile
, len
* 2 + 3);
306 buf
= cpp_quote_string (buf
+ 1, (const unsigned char *) name
, len
);
312 case BT_INCLUDE_LEVEL
:
313 /* The line map depth counts the primary source as level 1, but
314 historically __INCLUDE_DEPTH__ has called the primary source
316 number
= pfile
->line_table
->depth
- 1;
320 /* If __LINE__ is embedded in a macro, it must expand to the
321 line of the macro's invocation, not its definition.
322 Otherwise things like assert() will not work properly. */
323 number
= linemap_get_expansion_line (pfile
->line_table
,
324 CPP_OPTION (pfile
, traditional
)
325 ? pfile
->line_table
->highest_line
326 : pfile
->cur_token
[-1].src_loc
);
329 /* __STDC__ has the value 1 under normal circumstances.
330 However, if (a) we are in a system header, (b) the option
331 stdc_0_in_system_headers is true (set by target config), and
332 (c) we are not in strictly conforming mode, then it has the
333 value 0. (b) and (c) are already checked in cpp_init_builtins. */
335 if (cpp_in_system_header (pfile
))
343 if (CPP_OPTION (pfile
, warn_date_time
))
344 cpp_warning (pfile
, CPP_W_DATE_TIME
, "macro \"%s\" might prevent "
345 "reproducible builds", NODE_NAME (node
));
346 if (pfile
->date
== NULL
)
348 /* Allocate __DATE__ and __TIME__ strings from permanent
349 storage. We only do this once, and don't generate them
350 at init time, because time() and localtime() are very
351 slow on some systems. */
353 struct tm
*tb
= NULL
;
355 /* (time_t) -1 is a legitimate value for "number of seconds
356 since the Epoch", so we have to do a little dance to
357 distinguish that from a genuine error. */
360 if (tt
!= (time_t)-1 || errno
== 0)
361 tb
= localtime (&tt
);
365 pfile
->date
= _cpp_unaligned_alloc (pfile
,
366 sizeof ("\"Oct 11 1347\""));
367 sprintf ((char *) pfile
->date
, "\"%s %2d %4d\"",
368 monthnames
[tb
->tm_mon
], tb
->tm_mday
,
371 pfile
->time
= _cpp_unaligned_alloc (pfile
,
372 sizeof ("\"12:34:56\""));
373 sprintf ((char *) pfile
->time
, "\"%02d:%02d:%02d\"",
374 tb
->tm_hour
, tb
->tm_min
, tb
->tm_sec
);
378 cpp_errno (pfile
, CPP_DL_WARNING
,
379 "could not determine date and time");
381 pfile
->date
= UC
"\"??? ?? ????\"";
382 pfile
->time
= UC
"\"??:??:??\"";
386 if (node
->value
.builtin
== BT_DATE
)
387 result
= pfile
->date
;
389 result
= pfile
->time
;
393 if (CPP_OPTION (pfile
, directives_only
) && pfile
->state
.in_directive
)
394 cpp_error (pfile
, CPP_DL_ERROR
,
395 "__COUNTER__ expanded inside directive with -fdirectives-only");
396 number
= pfile
->counter
++;
399 case BT_HAS_ATTRIBUTE
:
400 number
= pfile
->cb
.has_attribute (pfile
);
406 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
407 result
= _cpp_unaligned_alloc (pfile
, 21);
408 sprintf ((char *) result
, "%u", number
);
414 /* Convert builtin macros like __FILE__ to a token and push it on the
415 context stack. Also handles _Pragma, for which a new token may not
416 be created. Returns 1 if it generates a new token context, 0 to
417 return the token to the caller. LOC is the location of the expansion
418 point of the macro. */
420 builtin_macro (cpp_reader
*pfile
, cpp_hashnode
*node
, source_location loc
)
426 if (node
->value
.builtin
== BT_PRAGMA
)
428 /* Don't interpret _Pragma within directives. The standard is
429 not clear on this, but to me this makes most sense. */
430 if (pfile
->state
.in_directive
)
433 return _cpp_do__Pragma (pfile
);
436 buf
= _cpp_builtin_macro_text (pfile
, node
);
438 nbuf
= (char *) alloca (len
+ 1);
439 memcpy (nbuf
, buf
, len
);
442 cpp_push_buffer (pfile
, (uchar
*) nbuf
, len
, /* from_stage3 */ true);
443 _cpp_clean_line (pfile
);
445 /* Set pfile->cur_token as required by _cpp_lex_direct. */
446 pfile
->cur_token
= _cpp_temp_token (pfile
);
447 cpp_token
*token
= _cpp_lex_direct (pfile
);
448 /* We should point to the expansion point of the builtin macro. */
449 token
->src_loc
= loc
;
450 if (pfile
->context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
452 /* We are tracking tokens resulting from macro expansion.
453 Create a macro line map and generate a virtual location for
454 the token resulting from the expansion of the built-in
456 source_location
*virt_locs
= NULL
;
457 _cpp_buff
*token_buf
= tokens_buff_new (pfile
, 1, &virt_locs
);
458 const line_map_macro
* map
=
459 linemap_enter_macro (pfile
->line_table
, node
,
461 tokens_buff_add_token (token_buf
, virt_locs
, token
,
462 pfile
->line_table
->builtin_location
,
463 pfile
->line_table
->builtin_location
,
464 map
, /*macro_token_index=*/0);
465 push_extended_tokens_context (pfile
, node
, token_buf
, virt_locs
,
466 (const cpp_token
**)token_buf
->base
,
470 _cpp_push_token_context (pfile
, NULL
, token
, 1);
471 if (pfile
->buffer
->cur
!= pfile
->buffer
->rlimit
)
472 cpp_error (pfile
, CPP_DL_ICE
, "invalid built-in macro \"%s\"",
474 _cpp_pop_buffer (pfile
);
479 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
480 backslashes and double quotes. DEST must be of sufficient size.
481 Returns a pointer to the end of the string. */
483 cpp_quote_string (uchar
*dest
, const uchar
*src
, unsigned int len
)
489 if (c
== '\\' || c
== '"')
501 /* Convert a token sequence ARG to a single string token according to
502 the rules of the ISO C #-operator. */
503 static const cpp_token
*
504 stringify_arg (cpp_reader
*pfile
, macro_arg
*arg
)
507 unsigned int i
, escape_it
, backslash_count
= 0;
508 const cpp_token
*source
= NULL
;
511 if (BUFF_ROOM (pfile
->u_buff
) < 3)
512 _cpp_extend_buff (pfile
, &pfile
->u_buff
, 3);
513 dest
= BUFF_FRONT (pfile
->u_buff
);
516 /* Loop, reading in the argument's tokens. */
517 for (i
= 0; i
< arg
->count
; i
++)
519 const cpp_token
*token
= arg
->first
[i
];
521 if (token
->type
== CPP_PADDING
)
524 || (!(source
->flags
& PREV_WHITE
)
525 && token
->val
.source
== NULL
))
526 source
= token
->val
.source
;
530 escape_it
= (token
->type
== CPP_STRING
|| token
->type
== CPP_CHAR
531 || token
->type
== CPP_WSTRING
|| token
->type
== CPP_WCHAR
532 || token
->type
== CPP_STRING32
|| token
->type
== CPP_CHAR32
533 || token
->type
== CPP_STRING16
|| token
->type
== CPP_CHAR16
534 || token
->type
== CPP_UTF8STRING
535 || cpp_userdef_string_p (token
->type
)
536 || cpp_userdef_char_p (token
->type
));
538 /* Room for each char being written in octal, initial space and
539 final quote and NUL. */
540 len
= cpp_token_len (token
);
545 if ((size_t) (BUFF_LIMIT (pfile
->u_buff
) - dest
) < len
)
547 size_t len_so_far
= dest
- BUFF_FRONT (pfile
->u_buff
);
548 _cpp_extend_buff (pfile
, &pfile
->u_buff
, len
);
549 dest
= BUFF_FRONT (pfile
->u_buff
) + len_so_far
;
552 /* Leading white space? */
553 if (dest
- 1 != BUFF_FRONT (pfile
->u_buff
))
557 if (source
->flags
& PREV_WHITE
)
564 _cpp_buff
*buff
= _cpp_get_buff (pfile
, len
);
565 unsigned char *buf
= BUFF_FRONT (buff
);
566 len
= cpp_spell_token (pfile
, token
, buf
, true) - buf
;
567 dest
= cpp_quote_string (dest
, buf
, len
);
568 _cpp_release_buff (pfile
, buff
);
571 dest
= cpp_spell_token (pfile
, token
, dest
, true);
573 if (token
->type
== CPP_OTHER
&& token
->val
.str
.text
[0] == '\\')
579 /* Ignore the final \ of invalid string literals. */
580 if (backslash_count
& 1)
582 cpp_error (pfile
, CPP_DL_WARNING
,
583 "invalid string literal, ignoring final '\\'");
587 /* Commit the memory, including NUL, and return the token. */
589 len
= dest
- BUFF_FRONT (pfile
->u_buff
);
590 BUFF_FRONT (pfile
->u_buff
) = dest
+ 1;
591 return new_string_token (pfile
, dest
- len
, len
);
594 /* Try to paste two tokens. On success, return nonzero. In any
595 case, PLHS is updated to point to the pasted token, which is
596 guaranteed to not have the PASTE_LEFT flag set. LOCATION is
597 the virtual location used for error reporting. */
599 paste_tokens (cpp_reader
*pfile
, source_location location
,
600 const cpp_token
**plhs
, const cpp_token
*rhs
)
602 unsigned char *buf
, *end
, *lhsend
;
606 len
= cpp_token_len (*plhs
) + cpp_token_len (rhs
) + 1;
607 buf
= (unsigned char *) alloca (len
);
608 end
= lhsend
= cpp_spell_token (pfile
, *plhs
, buf
, true);
610 /* Avoid comment headers, since they are still processed in stage 3.
611 It is simpler to insert a space here, rather than modifying the
612 lexer to ignore comments in some circumstances. Simply returning
613 false doesn't work, since we want to clear the PASTE_LEFT flag. */
614 if ((*plhs
)->type
== CPP_DIV
&& rhs
->type
!= CPP_EQ
)
616 /* In one obscure case we might see padding here. */
617 if (rhs
->type
!= CPP_PADDING
)
618 end
= cpp_spell_token (pfile
, rhs
, end
, true);
621 cpp_push_buffer (pfile
, buf
, end
- buf
, /* from_stage3 */ true);
622 _cpp_clean_line (pfile
);
624 /* Set pfile->cur_token as required by _cpp_lex_direct. */
625 pfile
->cur_token
= _cpp_temp_token (pfile
);
626 lhs
= _cpp_lex_direct (pfile
);
627 if (pfile
->buffer
->cur
!= pfile
->buffer
->rlimit
)
629 source_location saved_loc
= lhs
->src_loc
;
631 _cpp_pop_buffer (pfile
);
632 _cpp_backup_tokens (pfile
, 1);
635 /* We have to remove the PASTE_LEFT flag from the old lhs, but
636 we want to keep the new location. */
639 lhs
->src_loc
= saved_loc
;
640 lhs
->flags
&= ~PASTE_LEFT
;
642 /* Mandatory error for all apart from assembler. */
643 if (CPP_OPTION (pfile
, lang
) != CLK_ASM
)
644 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
645 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
646 buf
, cpp_token_as_text (pfile
, rhs
));
651 _cpp_pop_buffer (pfile
);
655 /* Handles an arbitrarily long sequence of ## operators, with initial
656 operand LHS. This implementation is left-associative,
657 non-recursive, and finishes a paste before handling succeeding
658 ones. If a paste fails, we back up to the RHS of the failing ##
659 operator before pushing the context containing the result of prior
660 successful pastes, with the effect that the RHS appears in the
661 output stream after the pasted LHS normally. */
663 paste_all_tokens (cpp_reader
*pfile
, const cpp_token
*lhs
)
665 const cpp_token
*rhs
= NULL
;
666 cpp_context
*context
= pfile
->context
;
667 source_location virt_loc
= 0;
669 /* We are expanding a macro and we must have been called on a token
670 that appears at the left hand side of a ## operator. */
671 if (macro_of_context (pfile
->context
) == NULL
672 || (!(lhs
->flags
& PASTE_LEFT
)))
675 if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
676 /* The caller must have called consume_next_token_from_context
677 right before calling us. That has incremented the pointer to
678 the current virtual location. So it now points to the location
679 of the token that comes right after *LHS. We want the
680 resulting pasted token to have the location of the current
682 virt_loc
= context
->c
.mc
->cur_virt_loc
[-1];
684 /* We are not tracking macro expansion. So the best virtual
685 location we can get here is the expansion point of the macro we
686 are currently expanding. */
687 virt_loc
= pfile
->invocation_location
;
691 /* Take the token directly from the current context. We can do
692 this, because we are in the replacement list of either an
693 object-like macro, or a function-like macro with arguments
694 inserted. In either case, the constraints to #define
695 guarantee we have at least one more token. */
696 if (context
->tokens_kind
== TOKENS_KIND_DIRECT
)
697 rhs
= FIRST (context
).token
++;
698 else if (context
->tokens_kind
== TOKENS_KIND_INDIRECT
)
699 rhs
= *FIRST (context
).ptoken
++;
700 else if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
702 /* So we are in presence of an extended token context, which
703 means that each token in this context has a virtual
704 location attached to it. So let's not forget to update
705 the pointer to the current virtual location of the
706 current token when we update the pointer to the current
709 rhs
= *FIRST (context
).ptoken
++;
710 /* context->c.mc must be non-null, as if we were not in a
711 macro context, context->tokens_kind could not be equal to
712 TOKENS_KIND_EXTENDED. */
713 context
->c
.mc
->cur_virt_loc
++;
716 if (rhs
->type
== CPP_PADDING
)
718 if (rhs
->flags
& PASTE_LEFT
)
721 if (!paste_tokens (pfile
, virt_loc
, &lhs
, rhs
))
724 while (rhs
->flags
& PASTE_LEFT
);
726 /* Put the resulting token in its own context. */
727 if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
729 source_location
*virt_locs
= NULL
;
730 _cpp_buff
*token_buf
= tokens_buff_new (pfile
, 1, &virt_locs
);
731 tokens_buff_add_token (token_buf
, virt_locs
, lhs
,
732 virt_loc
, 0, NULL
, 0);
733 push_extended_tokens_context (pfile
, context
->c
.mc
->macro_node
,
734 token_buf
, virt_locs
,
735 (const cpp_token
**)token_buf
->base
, 1);
738 _cpp_push_token_context (pfile
, NULL
, lhs
, 1);
741 /* Returns TRUE if the number of arguments ARGC supplied in an
742 invocation of the MACRO referenced by NODE is valid. An empty
743 invocation to a macro with no parameters should pass ARGC as zero.
745 Note that MACRO cannot necessarily be deduced from NODE, in case
746 NODE was redefined whilst collecting arguments. */
748 _cpp_arguments_ok (cpp_reader
*pfile
, cpp_macro
*macro
, const cpp_hashnode
*node
, unsigned int argc
)
750 if (argc
== macro
->paramc
)
753 if (argc
< macro
->paramc
)
755 /* As an extension, variadic arguments are allowed to not appear in
756 the invocation at all.
757 e.g. #define debug(format, args...) something
760 This is exactly the same as if an empty variadic list had been
761 supplied - debug("string", ). */
763 if (argc
+ 1 == macro
->paramc
&& macro
->variadic
)
765 if (CPP_PEDANTIC (pfile
) && ! macro
->syshdr
)
767 if (CPP_OPTION (pfile
, cplusplus
))
768 cpp_error (pfile
, CPP_DL_PEDWARN
,
769 "ISO C++11 requires at least one argument "
770 "for the \"...\" in a variadic macro");
772 cpp_error (pfile
, CPP_DL_PEDWARN
,
773 "ISO C99 requires at least one argument "
774 "for the \"...\" in a variadic macro");
779 cpp_error (pfile
, CPP_DL_ERROR
,
780 "macro \"%s\" requires %u arguments, but only %u given",
781 NODE_NAME (node
), macro
->paramc
, argc
);
784 cpp_error (pfile
, CPP_DL_ERROR
,
785 "macro \"%s\" passed %u arguments, but takes just %u",
786 NODE_NAME (node
), argc
, macro
->paramc
);
791 /* Reads and returns the arguments to a function-like macro
792 invocation. Assumes the opening parenthesis has been processed.
793 If there is an error, emits an appropriate diagnostic and returns
794 NULL. Each argument is terminated by a CPP_EOF token, for the
795 future benefit of expand_arg(). If there are any deferred
796 #pragma directives among macro arguments, store pointers to the
797 CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.
799 What is returned is the buffer that contains the memory allocated
800 to hold the macro arguments. NODE is the name of the macro this
801 function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is
802 set to the actual number of macro arguments allocated in the
805 collect_args (cpp_reader
*pfile
, const cpp_hashnode
*node
,
806 _cpp_buff
**pragma_buff
, unsigned *num_args
)
808 _cpp_buff
*buff
, *base_buff
;
810 macro_arg
*args
, *arg
;
811 const cpp_token
*token
;
813 source_location virt_loc
;
814 bool track_macro_expansion_p
= CPP_OPTION (pfile
, track_macro_expansion
);
815 unsigned num_args_alloced
= 0;
817 macro
= node
->value
.macro
;
819 argc
= macro
->paramc
;
823 #define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50
824 #define ARG_TOKENS_EXTENT 1000
826 buff
= _cpp_get_buff (pfile
, argc
* (DEFAULT_NUM_TOKENS_PER_MACRO_ARG
827 * sizeof (cpp_token
*)
828 + sizeof (macro_arg
)));
830 args
= (macro_arg
*) buff
->base
;
831 memset (args
, 0, argc
* sizeof (macro_arg
));
832 buff
->cur
= (unsigned char *) &args
[argc
];
833 arg
= args
, argc
= 0;
835 /* Collect the tokens making up each argument. We don't yet know
836 how many arguments have been supplied, whether too many or too
837 few. Hence the slightly bizarre usage of "argc" and "arg". */
840 unsigned int paren_depth
= 0;
841 unsigned int ntokens
= 0;
842 unsigned virt_locs_capacity
= DEFAULT_NUM_TOKENS_PER_MACRO_ARG
;
846 arg
->first
= (const cpp_token
**) buff
->cur
;
847 if (track_macro_expansion_p
)
849 virt_locs_capacity
= DEFAULT_NUM_TOKENS_PER_MACRO_ARG
;
850 arg
->virt_locs
= XNEWVEC (source_location
,
856 /* Require space for 2 new tokens (including a CPP_EOF). */
857 if ((unsigned char *) &arg
->first
[ntokens
+ 2] > buff
->limit
)
859 buff
= _cpp_append_extend_buff (pfile
, buff
,
861 * sizeof (cpp_token
*));
862 arg
->first
= (const cpp_token
**) buff
->cur
;
864 if (track_macro_expansion_p
865 && (ntokens
+ 2 > virt_locs_capacity
))
867 virt_locs_capacity
+= ARG_TOKENS_EXTENT
;
868 arg
->virt_locs
= XRESIZEVEC (source_location
,
873 token
= cpp_get_token_1 (pfile
, &virt_loc
);
875 if (token
->type
== CPP_PADDING
)
877 /* Drop leading padding. */
881 else if (token
->type
== CPP_OPEN_PAREN
)
883 else if (token
->type
== CPP_CLOSE_PAREN
)
885 if (paren_depth
-- == 0)
888 else if (token
->type
== CPP_COMMA
)
890 /* A comma does not terminate an argument within
891 parentheses or as part of a variable argument. */
893 && ! (macro
->variadic
&& argc
== macro
->paramc
))
896 else if (token
->type
== CPP_EOF
897 || (token
->type
== CPP_HASH
&& token
->flags
& BOL
))
899 else if (token
->type
== CPP_PRAGMA
)
901 cpp_token
*newtok
= _cpp_temp_token (pfile
);
903 /* CPP_PRAGMA token lives in directive_result, which will
904 be overwritten on the next directive. */
909 if (*pragma_buff
== NULL
910 || BUFF_ROOM (*pragma_buff
) < sizeof (cpp_token
*))
913 if (*pragma_buff
== NULL
)
915 = _cpp_get_buff (pfile
, 32 * sizeof (cpp_token
*));
920 = _cpp_get_buff (pfile
,
921 (BUFF_FRONT (*pragma_buff
)
922 - (*pragma_buff
)->base
) * 2);
923 (*pragma_buff
)->next
= next
;
926 *(const cpp_token
**) BUFF_FRONT (*pragma_buff
) = token
;
927 BUFF_FRONT (*pragma_buff
) += sizeof (cpp_token
*);
928 if (token
->type
== CPP_PRAGMA_EOL
)
930 token
= cpp_get_token_1 (pfile
, &virt_loc
);
932 while (token
->type
!= CPP_EOF
);
934 /* In deferred pragmas parsing_args and prevent_expansion
935 had been changed, reset it. */
936 pfile
->state
.parsing_args
= 2;
937 pfile
->state
.prevent_expansion
= 1;
939 if (token
->type
== CPP_EOF
)
944 set_arg_token (arg
, token
, virt_loc
,
945 ntokens
, MACRO_ARG_TOKEN_NORMAL
,
946 CPP_OPTION (pfile
, track_macro_expansion
));
950 /* Drop trailing padding. */
951 while (ntokens
> 0 && arg
->first
[ntokens
- 1]->type
== CPP_PADDING
)
954 arg
->count
= ntokens
;
955 set_arg_token (arg
, &pfile
->eof
, pfile
->eof
.src_loc
,
956 ntokens
, MACRO_ARG_TOKEN_NORMAL
,
957 CPP_OPTION (pfile
, track_macro_expansion
));
959 /* Terminate the argument. Excess arguments loop back and
960 overwrite the final legitimate argument, before failing. */
961 if (argc
<= macro
->paramc
)
963 buff
->cur
= (unsigned char *) &arg
->first
[ntokens
+ 1];
964 if (argc
!= macro
->paramc
)
968 while (token
->type
!= CPP_CLOSE_PAREN
&& token
->type
!= CPP_EOF
);
970 if (token
->type
== CPP_EOF
)
972 /* We still need the CPP_EOF to end directives, and to end
973 pre-expansion of a macro argument. Step back is not
974 unconditional, since we don't want to return a CPP_EOF to our
975 callers at the end of an -include-d file. */
976 if (pfile
->context
->prev
|| pfile
->state
.in_directive
)
977 _cpp_backup_tokens (pfile
, 1);
978 cpp_error (pfile
, CPP_DL_ERROR
,
979 "unterminated argument list invoking macro \"%s\"",
984 /* A single empty argument is counted as no argument. */
985 if (argc
== 1 && macro
->paramc
== 0 && args
[0].count
== 0)
987 if (_cpp_arguments_ok (pfile
, macro
, node
, argc
))
989 /* GCC has special semantics for , ## b where b is a varargs
990 parameter: we remove the comma if b was omitted entirely.
991 If b was merely an empty argument, the comma is retained.
992 If the macro takes just one (varargs) parameter, then we
993 retain the comma only if we are standards conforming.
995 If FIRST is NULL replace_args () swallows the comma. */
996 if (macro
->variadic
&& (argc
< macro
->paramc
997 || (argc
== 1 && args
[0].count
== 0
998 && !CPP_OPTION (pfile
, std
))))
999 args
[macro
->paramc
- 1].first
= NULL
;
1001 *num_args
= num_args_alloced
;
1006 /* An error occurred. */
1007 _cpp_release_buff (pfile
, base_buff
);
1011 /* Search for an opening parenthesis to the macro of NODE, in such a
1012 way that, if none is found, we don't lose the information in any
1013 intervening padding tokens. If we find the parenthesis, collect
1014 the arguments and return the buffer containing them. PRAGMA_BUFF
1015 argument is the same as in collect_args. If NUM_ARGS is non-NULL,
1016 *NUM_ARGS is set to the number of arguments contained in the
1019 funlike_invocation_p (cpp_reader
*pfile
, cpp_hashnode
*node
,
1020 _cpp_buff
**pragma_buff
, unsigned *num_args
)
1022 const cpp_token
*token
, *padding
= NULL
;
1026 token
= cpp_get_token (pfile
);
1027 if (token
->type
!= CPP_PADDING
)
1030 || (!(padding
->flags
& PREV_WHITE
) && token
->val
.source
== NULL
))
1034 if (token
->type
== CPP_OPEN_PAREN
)
1036 pfile
->state
.parsing_args
= 2;
1037 return collect_args (pfile
, node
, pragma_buff
, num_args
);
1040 /* CPP_EOF can be the end of macro arguments, or the end of the
1041 file. We mustn't back up over the latter. Ugh. */
1042 if (token
->type
!= CPP_EOF
|| token
== &pfile
->eof
)
1044 /* Back up. We may have skipped padding, in which case backing
1045 up more than one token when expanding macros is in general
1046 too difficult. We re-insert it in its own context. */
1047 _cpp_backup_tokens (pfile
, 1);
1049 _cpp_push_token_context (pfile
, NULL
, padding
, 1);
1055 /* Return the real number of tokens in the expansion of MACRO. */
1056 static inline unsigned int
1057 macro_real_token_count (const cpp_macro
*macro
)
1060 if (__builtin_expect (!macro
->extra_tokens
, true))
1061 return macro
->count
;
1062 for (i
= 0; i
< macro
->count
; i
++)
1063 if (macro
->exp
.tokens
[i
].type
== CPP_PASTE
)
1068 /* Push the context of a macro with hash entry NODE onto the context
1069 stack. If we can successfully expand the macro, we push a context
1070 containing its yet-to-be-rescanned replacement list and return one.
1071 If there were additionally any unexpanded deferred #pragma
1072 directives among macro arguments, push another context containing
1073 the pragma tokens before the yet-to-be-rescanned replacement list
1074 and return two. Otherwise, we don't push a context and return
1075 zero. LOCATION is the location of the expansion point of the
1078 enter_macro_context (cpp_reader
*pfile
, cpp_hashnode
*node
,
1079 const cpp_token
*result
, source_location location
)
1081 /* The presence of a macro invalidates a file's controlling macro. */
1082 pfile
->mi_valid
= false;
1084 pfile
->state
.angled_headers
= false;
1086 /* From here to when we push the context for the macro later down
1087 this function, we need to flag the fact that we are about to
1088 expand a macro. This is useful when -ftrack-macro-expansion is
1089 turned off. In that case, we need to record the location of the
1090 expansion point of the top-most macro we are about to to expand,
1091 into pfile->invocation_location. But we must not record any such
1092 location once the process of expanding the macro starts; that is,
1093 we must not do that recording between now and later down this
1094 function where set this flag to FALSE. */
1095 pfile
->about_to_expand_macro_p
= true;
1097 if ((node
->flags
& NODE_BUILTIN
) && !(node
->flags
& NODE_USED
))
1099 node
->flags
|= NODE_USED
;
1100 if ((!pfile
->cb
.user_builtin_macro
1101 || !pfile
->cb
.user_builtin_macro (pfile
, node
))
1102 && pfile
->cb
.used_define
)
1103 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
1106 /* Handle standard macros. */
1107 if (! (node
->flags
& NODE_BUILTIN
))
1109 cpp_macro
*macro
= node
->value
.macro
;
1110 _cpp_buff
*pragma_buff
= NULL
;
1112 if (macro
->fun_like
)
1115 unsigned num_args
= 0;
1117 pfile
->state
.prevent_expansion
++;
1118 pfile
->keep_tokens
++;
1119 pfile
->state
.parsing_args
= 1;
1120 buff
= funlike_invocation_p (pfile
, node
, &pragma_buff
,
1122 pfile
->state
.parsing_args
= 0;
1123 pfile
->keep_tokens
--;
1124 pfile
->state
.prevent_expansion
--;
1128 if (CPP_WTRADITIONAL (pfile
) && ! node
->value
.macro
->syshdr
)
1129 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
1130 "function-like macro \"%s\" must be used with arguments in traditional C",
1134 _cpp_release_buff (pfile
, pragma_buff
);
1136 pfile
->about_to_expand_macro_p
= false;
1140 if (macro
->paramc
> 0)
1141 replace_args (pfile
, node
, macro
,
1142 (macro_arg
*) buff
->base
,
1144 /* Free the memory used by the arguments of this
1145 function-like macro. This memory has been allocated by
1146 funlike_invocation_p and by replace_args. */
1147 delete_macro_args (buff
, num_args
);
1150 /* Disable the macro within its expansion. */
1151 node
->flags
|= NODE_DISABLED
;
1153 if (!(node
->flags
& NODE_USED
))
1155 node
->flags
|= NODE_USED
;
1156 if (pfile
->cb
.used_define
)
1157 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
1161 pfile
->cb
.used (pfile
, location
, node
);
1165 if (macro
->paramc
== 0)
1167 unsigned tokens_count
= macro_real_token_count (macro
);
1168 if (CPP_OPTION (pfile
, track_macro_expansion
))
1171 const cpp_token
*src
= macro
->exp
.tokens
;
1172 const line_map_macro
*map
;
1173 source_location
*virt_locs
= NULL
;
1174 _cpp_buff
*macro_tokens
1175 = tokens_buff_new (pfile
, tokens_count
, &virt_locs
);
1177 /* Create a macro map to record the locations of the
1178 tokens that are involved in the expansion. LOCATION
1179 is the location of the macro expansion point. */
1180 map
= linemap_enter_macro (pfile
->line_table
,
1181 node
, location
, tokens_count
);
1182 for (i
= 0; i
< tokens_count
; ++i
)
1184 tokens_buff_add_token (macro_tokens
, virt_locs
,
1186 src
->src_loc
, map
, i
);
1189 push_extended_tokens_context (pfile
, node
,
1192 (const cpp_token
**)
1197 _cpp_push_token_context (pfile
, node
, macro
->exp
.tokens
,
1199 num_macro_tokens_counter
+= tokens_count
;
1204 if (!pfile
->state
.in_directive
)
1205 _cpp_push_token_context (pfile
, NULL
,
1206 padding_token (pfile
, result
), 1);
1209 unsigned tokens_count
;
1210 _cpp_buff
*tail
= pragma_buff
->next
;
1211 pragma_buff
->next
= NULL
;
1212 tokens_count
= ((const cpp_token
**) BUFF_FRONT (pragma_buff
)
1213 - (const cpp_token
**) pragma_buff
->base
);
1214 push_ptoken_context (pfile
, NULL
, pragma_buff
,
1215 (const cpp_token
**) pragma_buff
->base
,
1218 if (!CPP_OPTION (pfile
, track_macro_expansion
))
1219 num_macro_tokens_counter
+= tokens_count
;
1222 while (pragma_buff
!= NULL
);
1223 pfile
->about_to_expand_macro_p
= false;
1227 pfile
->about_to_expand_macro_p
= false;
1231 pfile
->about_to_expand_macro_p
= false;
1232 /* Handle built-in macros and the _Pragma operator. */
1234 source_location loc
;
1235 if (/* The top-level macro invocation that triggered the expansion
1236 we are looking at is with a standard macro ...*/
1237 !(pfile
->top_most_macro_node
->flags
& NODE_BUILTIN
)
1238 /* ... and it's a function-like macro invocation. */
1239 && pfile
->top_most_macro_node
->value
.macro
->fun_like
)
1240 /* Then the location of the end of the macro invocation is the
1241 location of the closing parenthesis. */
1242 loc
= pfile
->cur_token
[-1].src_loc
;
1244 /* Otherwise, the location of the end of the macro invocation is
1245 the location of the expansion point of that top-level macro
1249 return builtin_macro (pfile
, node
, loc
);
1253 /* De-allocate the memory used by BUFF which is an array of instances
1254 of macro_arg. NUM_ARGS is the number of instances of macro_arg
1257 delete_macro_args (_cpp_buff
*buff
, unsigned num_args
)
1259 macro_arg
*macro_args
;
1265 macro_args
= (macro_arg
*) buff
->base
;
1267 /* Walk instances of macro_arg to free their expanded tokens as well
1268 as their macro_arg::virt_locs members. */
1269 for (i
= 0; i
< num_args
; ++i
)
1271 if (macro_args
[i
].expanded
)
1273 free (macro_args
[i
].expanded
);
1274 macro_args
[i
].expanded
= NULL
;
1276 if (macro_args
[i
].virt_locs
)
1278 free (macro_args
[i
].virt_locs
);
1279 macro_args
[i
].virt_locs
= NULL
;
1281 if (macro_args
[i
].expanded_virt_locs
)
1283 free (macro_args
[i
].expanded_virt_locs
);
1284 macro_args
[i
].expanded_virt_locs
= NULL
;
1287 _cpp_free_buff (buff
);
1290 /* Set the INDEXth token of the macro argument ARG. TOKEN is the token
1291 to set, LOCATION is its virtual location. "Virtual" location means
1292 the location that encodes loci across macro expansion. Otherwise
1293 it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the
1294 argument ARG is supposed to contain. Note that ARG must be
1295 tailored so that it has enough room to contain INDEX + 1 numbers of
1296 tokens, at least. */
1298 set_arg_token (macro_arg
*arg
, const cpp_token
*token
,
1299 source_location location
, size_t index
,
1300 enum macro_arg_token_kind kind
,
1301 bool track_macro_exp_p
)
1303 const cpp_token
**token_ptr
;
1304 source_location
*loc
= NULL
;
1307 arg_token_ptr_at (arg
, index
, kind
,
1308 track_macro_exp_p
? &loc
: NULL
);
1313 #ifdef ENABLE_CHECKING
1314 if (kind
== MACRO_ARG_TOKEN_STRINGIFIED
1315 || !track_macro_exp_p
)
1316 /* We can't set the location of a stringified argument
1317 token and we can't set any location if we aren't tracking
1318 macro expansion locations. */
1325 /* Get the pointer to the location of the argument token of the
1326 function-like macro argument ARG. This function must be called
1327 only when we -ftrack-macro-expansion is on. */
1328 static const source_location
*
1329 get_arg_token_location (const macro_arg
*arg
,
1330 enum macro_arg_token_kind kind
)
1332 const source_location
*loc
= NULL
;
1333 const cpp_token
**token_ptr
=
1334 arg_token_ptr_at (arg
, 0, kind
, (source_location
**) &loc
);
1336 if (token_ptr
== NULL
)
1342 /* Return the pointer to the INDEXth token of the macro argument ARG.
1343 KIND specifies the kind of token the macro argument ARG contains.
1344 If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
1345 of the virtual location of the returned token if the
1346 -ftrack-macro-expansion flag is on; otherwise, it's set to the
1347 spelling location of the returned token. */
1348 static const cpp_token
**
1349 arg_token_ptr_at (const macro_arg
*arg
, size_t index
,
1350 enum macro_arg_token_kind kind
,
1351 source_location
**virt_location
)
1353 const cpp_token
**tokens_ptr
= NULL
;
1357 case MACRO_ARG_TOKEN_NORMAL
:
1358 tokens_ptr
= arg
->first
;
1360 case MACRO_ARG_TOKEN_STRINGIFIED
:
1361 tokens_ptr
= (const cpp_token
**) &arg
->stringified
;
1363 case MACRO_ARG_TOKEN_EXPANDED
:
1364 tokens_ptr
= arg
->expanded
;
1368 if (tokens_ptr
== NULL
)
1369 /* This can happen for e.g, an empty token argument to a
1370 funtion-like macro. */
1375 if (kind
== MACRO_ARG_TOKEN_NORMAL
)
1376 *virt_location
= &arg
->virt_locs
[index
];
1377 else if (kind
== MACRO_ARG_TOKEN_EXPANDED
)
1378 *virt_location
= &arg
->expanded_virt_locs
[index
];
1379 else if (kind
== MACRO_ARG_TOKEN_STRINGIFIED
)
1381 (source_location
*) &tokens_ptr
[index
]->src_loc
;
1383 return &tokens_ptr
[index
];
1386 /* Initialize an iterator so that it iterates over the tokens of a
1387 function-like macro argument. KIND is the kind of tokens we want
1388 ITER to iterate over. TOKEN_PTR points the first token ITER will
1391 macro_arg_token_iter_init (macro_arg_token_iter
*iter
,
1392 bool track_macro_exp_p
,
1393 enum macro_arg_token_kind kind
,
1394 const macro_arg
*arg
,
1395 const cpp_token
**token_ptr
)
1397 iter
->track_macro_exp_p
= track_macro_exp_p
;
1399 iter
->token_ptr
= token_ptr
;
1400 /* Unconditionally initialize this so that the compiler doesn't warn
1401 about iter->location_ptr being possibly uninitialized later after
1402 this code has been inlined somewhere. */
1403 iter
->location_ptr
= NULL
;
1404 if (track_macro_exp_p
)
1405 iter
->location_ptr
= get_arg_token_location (arg
, kind
);
1406 #ifdef ENABLE_CHECKING
1407 iter
->num_forwards
= 0;
1408 if (track_macro_exp_p
1409 && token_ptr
!= NULL
1410 && iter
->location_ptr
== NULL
)
1415 /* Move the iterator one token forward. Note that if IT was
1416 initialized on an argument that has a stringified token, moving it
1417 forward doesn't make sense as a stringified token is essentially one
1420 macro_arg_token_iter_forward (macro_arg_token_iter
*it
)
1424 case MACRO_ARG_TOKEN_NORMAL
:
1425 case MACRO_ARG_TOKEN_EXPANDED
:
1427 if (it
->track_macro_exp_p
)
1430 case MACRO_ARG_TOKEN_STRINGIFIED
:
1431 #ifdef ENABLE_CHECKING
1432 if (it
->num_forwards
> 0)
1438 #ifdef ENABLE_CHECKING
1443 /* Return the token pointed to by the iterator. */
1444 static const cpp_token
*
1445 macro_arg_token_iter_get_token (const macro_arg_token_iter
*it
)
1447 #ifdef ENABLE_CHECKING
1448 if (it
->kind
== MACRO_ARG_TOKEN_STRINGIFIED
1449 && it
->num_forwards
> 0)
1452 if (it
->token_ptr
== NULL
)
1454 return *it
->token_ptr
;
1457 /* Return the location of the token pointed to by the iterator.*/
1458 static source_location
1459 macro_arg_token_iter_get_location (const macro_arg_token_iter
*it
)
1461 #ifdef ENABLE_CHECKING
1462 if (it
->kind
== MACRO_ARG_TOKEN_STRINGIFIED
1463 && it
->num_forwards
> 0)
1466 if (it
->track_macro_exp_p
)
1467 return *it
->location_ptr
;
1469 return (*it
->token_ptr
)->src_loc
;
1472 /* Return the index of a token [resulting from macro expansion] inside
1473 the total list of tokens resulting from a given macro
1474 expansion. The index can be different depending on whether if we
1475 want each tokens resulting from function-like macro arguments
1476 expansion to have a different location or not.
1478 E.g, consider this function-like macro:
1482 Then consider us "calling" it (and thus expanding it) like:
1486 It will be expanded into:
1490 Let's consider the case of the token '4'.
1492 Its index can be 2 (it's the third token of the set of tokens
1493 resulting from the expansion) or it can be 0 if we consider that
1494 all tokens resulting from the expansion of the argument "1+2" have
1495 the same index, which is 0. In this later case, the index of token
1496 '-' would then be 1 and the index of token '3' would be 2.
1498 The later case is useful to use less memory e.g, for the case of
1499 the user using the option -ftrack-macro-expansion=1.
1501 ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
1502 are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro
1503 parameter (inside the macro replacement list) that corresponds to
1504 the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
1507 If we refer to the example above, for the '4' argument token,
1508 ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
1509 would be set to the token 'x', in the replacement list "x - 3" of
1512 This is a subroutine of replace_args. */
1513 inline static unsigned
1514 expanded_token_index (cpp_reader
*pfile
, cpp_macro
*macro
,
1515 const cpp_token
*cur_replacement_token
,
1516 unsigned absolute_token_index
)
1518 if (CPP_OPTION (pfile
, track_macro_expansion
) > 1)
1519 return absolute_token_index
;
1520 return cur_replacement_token
- macro
->exp
.tokens
;
1523 /* Replace the parameters in a function-like macro of NODE with the
1524 actual ARGS, and place the result in a newly pushed token context.
1525 Expand each argument before replacing, unless it is operated upon
1526 by the # or ## operators. EXPANSION_POINT_LOC is the location of
1527 the expansion point of the macro. E.g, the location of the
1528 function-like macro invocation. */
1530 replace_args (cpp_reader
*pfile
, cpp_hashnode
*node
, cpp_macro
*macro
,
1531 macro_arg
*args
, source_location expansion_point_loc
)
1533 unsigned int i
, total
;
1534 const cpp_token
*src
, *limit
;
1535 const cpp_token
**first
= NULL
;
1537 _cpp_buff
*buff
= NULL
;
1538 source_location
*virt_locs
= NULL
;
1539 unsigned int exp_count
;
1540 const line_map_macro
*map
= NULL
;
1541 int track_macro_exp
;
1543 /* First, fully macro-expand arguments, calculating the number of
1544 tokens in the final expansion as we go. The ordering of the if
1545 statements below is subtle; we must handle stringification before
1548 /* EXP_COUNT is the number of tokens in the macro replacement
1549 list. TOTAL is the number of tokens /after/ macro parameters
1550 have been replaced by their arguments. */
1551 exp_count
= macro_real_token_count (macro
);
1553 limit
= macro
->exp
.tokens
+ exp_count
;
1555 for (src
= macro
->exp
.tokens
; src
< limit
; src
++)
1556 if (src
->type
== CPP_MACRO_ARG
)
1558 /* Leading and trailing padding tokens. */
1560 /* Account for leading and padding tokens in exp_count too.
1561 This is going to be important later down this function,
1562 when we want to handle the case of (track_macro_exp <
1566 /* We have an argument. If it is not being stringified or
1567 pasted it is macro-replaced before insertion. */
1568 arg
= &args
[src
->val
.macro_arg
.arg_no
- 1];
1570 if (src
->flags
& STRINGIFY_ARG
)
1572 if (!arg
->stringified
)
1573 arg
->stringified
= stringify_arg (pfile
, arg
);
1575 else if ((src
->flags
& PASTE_LEFT
)
1576 || (src
> macro
->exp
.tokens
&& (src
[-1].flags
& PASTE_LEFT
)))
1577 total
+= arg
->count
- 1;
1581 expand_arg (pfile
, arg
);
1582 total
+= arg
->expanded_count
- 1;
1586 /* When the compiler is called with the -ftrack-macro-expansion
1587 flag, we need to keep track of the location of each token that
1588 results from macro expansion.
1590 A token resulting from macro expansion is not a new token. It is
1591 simply the same token as the token coming from the macro
1592 definition. The new things that are allocated are the buffer
1593 that holds the tokens resulting from macro expansion and a new
1594 location that records many things like the locus of the expansion
1595 point as well as the original locus inside the definition of the
1596 macro. This location is called a virtual location.
1598 So the buffer BUFF holds a set of cpp_token*, and the buffer
1599 VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
1601 Both of these two buffers are going to be hung off of the macro
1602 context, when the latter is pushed. The memory allocated to
1603 store the tokens and their locations is going to be freed once
1604 the context of macro expansion is popped.
1606 As far as tokens are concerned, the memory overhead of
1607 -ftrack-macro-expansion is proportional to the number of
1608 macros that get expanded multiplied by sizeof (source_location).
1609 The good news is that extra memory gets freed when the macro
1610 context is freed, i.e shortly after the macro got expanded. */
1612 /* Is the -ftrack-macro-expansion flag in effect? */
1613 track_macro_exp
= CPP_OPTION (pfile
, track_macro_expansion
);
1615 /* Now allocate memory space for tokens and locations resulting from
1616 the macro expansion, copy the tokens and replace the arguments.
1617 This memory must be freed when the context of the macro MACRO is
1619 buff
= tokens_buff_new (pfile
, total
, track_macro_exp
? &virt_locs
: NULL
);
1621 first
= (const cpp_token
**) buff
->base
;
1623 /* Create a macro map to record the locations of the tokens that are
1624 involved in the expansion. Note that the expansion point is set
1625 to the location of the closing parenthesis. Otherwise, the
1626 subsequent map created for the first token that comes after the
1627 macro map might have a wrong line number. That would lead to
1628 tokens with wrong line numbers after the macro expansion. This
1629 adds up to the memory overhead of the -ftrack-macro-expansion
1630 flag; for every macro that is expanded, a "macro map" is
1632 if (track_macro_exp
)
1634 int num_macro_tokens
= total
;
1635 if (track_macro_exp
< 2)
1636 /* Then the number of macro tokens won't take in account the
1637 fact that function-like macro arguments can expand to
1638 multiple tokens. This is to save memory at the expense of
1641 Suppose we have #define SQARE(A) A * A
1643 And then we do SQARE(2+3)
1645 Then the tokens 2, +, 3, will have the same location,
1646 saying they come from the expansion of the argument A. */
1647 num_macro_tokens
= exp_count
;
1648 map
= linemap_enter_macro (pfile
->line_table
, node
,
1649 expansion_point_loc
,
1653 for (src
= macro
->exp
.tokens
; src
< limit
; src
++)
1655 unsigned int arg_tokens_count
;
1656 macro_arg_token_iter from
;
1657 const cpp_token
**paste_flag
= NULL
;
1658 const cpp_token
**tmp_token_ptr
;
1660 if (src
->type
!= CPP_MACRO_ARG
)
1662 /* Allocate a virtual location for token SRC, and add that
1663 token and its virtual location into the buffers BUFF and
1665 unsigned index
= expanded_token_index (pfile
, macro
, src
, i
);
1666 tokens_buff_add_token (buff
, virt_locs
, src
,
1667 src
->src_loc
, src
->src_loc
,
1674 arg
= &args
[src
->val
.macro_arg
.arg_no
- 1];
1675 /* SRC is a macro parameter that we need to replace with its
1676 corresponding argument. So at some point we'll need to
1677 iterate over the tokens of the macro argument and copy them
1678 into the "place" now holding the correspondig macro
1679 parameter. We are going to use the iterator type
1680 macro_argo_token_iter to handle that iterating. The 'if'
1681 below is to initialize the iterator depending on the type of
1682 tokens the macro argument has. It also does some adjustment
1683 related to padding tokens and some pasting corner cases. */
1684 if (src
->flags
& STRINGIFY_ARG
)
1686 arg_tokens_count
= 1;
1687 macro_arg_token_iter_init (&from
,
1689 track_macro_expansion
),
1690 MACRO_ARG_TOKEN_STRINGIFIED
,
1691 arg
, &arg
->stringified
);
1693 else if (src
->flags
& PASTE_LEFT
)
1695 arg_tokens_count
= arg
->count
;
1696 macro_arg_token_iter_init (&from
,
1698 track_macro_expansion
),
1699 MACRO_ARG_TOKEN_NORMAL
,
1702 else if (src
!= macro
->exp
.tokens
&& (src
[-1].flags
& PASTE_LEFT
))
1705 arg_tokens_count
= arg
->count
;
1706 macro_arg_token_iter_init (&from
,
1708 track_macro_expansion
),
1709 MACRO_ARG_TOKEN_NORMAL
,
1712 num_toks
= tokens_buff_count (buff
);
1716 /* So the current parameter token is pasted to the previous
1717 token in the replacement list. Let's look at what
1718 we have as previous and current arguments. */
1720 /* This is the previous argument's token ... */
1721 tmp_token_ptr
= tokens_buff_last_token_ptr (buff
);
1723 if ((*tmp_token_ptr
)->type
== CPP_COMMA
1725 && src
->val
.macro_arg
.arg_no
== macro
->paramc
)
1727 /* ... which is a comma; and the current parameter
1728 is the last parameter of a variadic function-like
1729 macro. If the argument to the current last
1730 parameter is NULL, then swallow the comma,
1731 otherwise drop the paste flag. */
1732 if (macro_arg_token_iter_get_token (&from
) == NULL
)
1733 tokens_buff_remove_last_token (buff
);
1735 paste_flag
= tmp_token_ptr
;
1737 /* Remove the paste flag if the RHS is a placemarker. */
1738 else if (arg_tokens_count
== 0)
1739 paste_flag
= tmp_token_ptr
;
1744 arg_tokens_count
= arg
->expanded_count
;
1745 macro_arg_token_iter_init (&from
,
1747 track_macro_expansion
),
1748 MACRO_ARG_TOKEN_EXPANDED
,
1749 arg
, arg
->expanded
);
1752 /* Padding on the left of an argument (unless RHS of ##). */
1753 if ((!pfile
->state
.in_directive
|| pfile
->state
.directive_wants_padding
)
1754 && src
!= macro
->exp
.tokens
&& !(src
[-1].flags
& PASTE_LEFT
))
1756 const cpp_token
*t
= padding_token (pfile
, src
);
1757 unsigned index
= expanded_token_index (pfile
, macro
, src
, i
);
1758 /* Allocate a virtual location for the padding token and
1759 append the token and its location to BUFF and
1761 tokens_buff_add_token (buff
, virt_locs
, t
,
1762 t
->src_loc
, t
->src_loc
,
1766 if (arg_tokens_count
)
1768 /* So now we've got the number of tokens that make up the
1769 argument that is going to replace the current parameter
1770 in the macro's replacement list. */
1772 for (j
= 0; j
< arg_tokens_count
; ++j
)
1774 /* So if track_macro_exp is < 2, the user wants to
1775 save extra memory while tracking macro expansion
1776 locations. So in that case here is what we do:
1778 Suppose we have #define SQARE(A) A * A
1780 And then we do SQARE(2+3)
1782 Then the tokens 2, +, 3, will have the same location,
1783 saying they come from the expansion of the argument
1786 So that means we are going to ignore the COUNT tokens
1787 resulting from the expansion of the current macro
1788 arugment. In other words all the ARG_TOKENS_COUNT tokens
1789 resulting from the expansion of the macro argument will
1790 have the index I. Normally, each of those token should
1792 unsigned token_index
= i
;
1794 if (track_macro_exp
> 1)
1797 index
= expanded_token_index (pfile
, macro
, src
, token_index
);
1798 tokens_buff_add_token (buff
, virt_locs
,
1799 macro_arg_token_iter_get_token (&from
),
1800 macro_arg_token_iter_get_location (&from
),
1801 src
->src_loc
, map
, index
);
1802 macro_arg_token_iter_forward (&from
);
1805 /* With a non-empty argument on the LHS of ##, the last
1806 token should be flagged PASTE_LEFT. */
1807 if (src
->flags
& PASTE_LEFT
)
1809 (const cpp_token
**) tokens_buff_last_token_ptr (buff
);
1811 else if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, c99
)
1812 && ! macro
->syshdr
&& ! cpp_in_system_header (pfile
))
1814 if (CPP_OPTION (pfile
, cplusplus
))
1815 cpp_pedwarning (pfile
, CPP_W_PEDANTIC
,
1816 "invoking macro %s argument %d: "
1817 "empty macro arguments are undefined"
1819 NODE_NAME (node
), src
->val
.macro_arg
.arg_no
);
1820 else if (CPP_OPTION (pfile
, cpp_warn_c90_c99_compat
))
1821 cpp_pedwarning (pfile
,
1822 CPP_OPTION (pfile
, cpp_warn_c90_c99_compat
) > 0
1823 ? CPP_W_C90_C99_COMPAT
: CPP_W_PEDANTIC
,
1824 "invoking macro %s argument %d: "
1825 "empty macro arguments are undefined"
1827 NODE_NAME (node
), src
->val
.macro_arg
.arg_no
);
1829 else if (CPP_OPTION (pfile
, cpp_warn_c90_c99_compat
) > 0
1830 && ! CPP_OPTION (pfile
, cplusplus
)
1831 && ! macro
->syshdr
&& ! cpp_in_system_header (pfile
))
1832 cpp_warning (pfile
, CPP_W_C90_C99_COMPAT
,
1833 "invoking macro %s argument %d: "
1834 "empty macro arguments are undefined"
1836 NODE_NAME (node
), src
->val
.macro_arg
.arg_no
);
1838 /* Avoid paste on RHS (even case count == 0). */
1839 if (!pfile
->state
.in_directive
&& !(src
->flags
& PASTE_LEFT
))
1841 const cpp_token
*t
= &pfile
->avoid_paste
;
1842 tokens_buff_add_token (buff
, virt_locs
,
1843 t
, t
->src_loc
, t
->src_loc
,
1847 /* Add a new paste flag, or remove an unwanted one. */
1850 cpp_token
*token
= _cpp_temp_token (pfile
);
1851 token
->type
= (*paste_flag
)->type
;
1852 token
->val
= (*paste_flag
)->val
;
1853 if (src
->flags
& PASTE_LEFT
)
1854 token
->flags
= (*paste_flag
)->flags
| PASTE_LEFT
;
1856 token
->flags
= (*paste_flag
)->flags
& ~PASTE_LEFT
;
1857 *paste_flag
= token
;
1860 i
+= arg_tokens_count
;
1863 if (track_macro_exp
)
1864 push_extended_tokens_context (pfile
, node
, buff
, virt_locs
, first
,
1865 tokens_buff_count (buff
));
1867 push_ptoken_context (pfile
, node
, buff
, first
,
1868 tokens_buff_count (buff
));
1870 num_macro_tokens_counter
+= tokens_buff_count (buff
);
1873 /* Return a special padding token, with padding inherited from SOURCE. */
1874 static const cpp_token
*
1875 padding_token (cpp_reader
*pfile
, const cpp_token
*source
)
1877 cpp_token
*result
= _cpp_temp_token (pfile
);
1879 result
->type
= CPP_PADDING
;
1881 /* Data in GCed data structures cannot be made const so far, so we
1882 need a cast here. */
1883 result
->val
.source
= (cpp_token
*) source
;
1888 /* Get a new uninitialized context. Create a new one if we cannot
1889 re-use an old one. */
1890 static cpp_context
*
1891 next_context (cpp_reader
*pfile
)
1893 cpp_context
*result
= pfile
->context
->next
;
1897 result
= XNEW (cpp_context
);
1898 memset (result
, 0, sizeof (cpp_context
));
1899 result
->prev
= pfile
->context
;
1901 pfile
->context
->next
= result
;
1904 pfile
->context
= result
;
1908 /* Push a list of pointers to tokens. */
1910 push_ptoken_context (cpp_reader
*pfile
, cpp_hashnode
*macro
, _cpp_buff
*buff
,
1911 const cpp_token
**first
, unsigned int count
)
1913 cpp_context
*context
= next_context (pfile
);
1915 context
->tokens_kind
= TOKENS_KIND_INDIRECT
;
1916 context
->c
.macro
= macro
;
1917 context
->buff
= buff
;
1918 FIRST (context
).ptoken
= first
;
1919 LAST (context
).ptoken
= first
+ count
;
1922 /* Push a list of tokens.
1924 A NULL macro means that we should continue the current macro
1925 expansion, in essence. That means that if we are currently in a
1926 macro expansion context, we'll make the new pfile->context refer to
1927 the current macro. */
1929 _cpp_push_token_context (cpp_reader
*pfile
, cpp_hashnode
*macro
,
1930 const cpp_token
*first
, unsigned int count
)
1932 cpp_context
*context
;
1935 macro
= macro_of_context (pfile
->context
);
1937 context
= next_context (pfile
);
1938 context
->tokens_kind
= TOKENS_KIND_DIRECT
;
1939 context
->c
.macro
= macro
;
1940 context
->buff
= NULL
;
1941 FIRST (context
).token
= first
;
1942 LAST (context
).token
= first
+ count
;
1945 /* Build a context containing a list of tokens as well as their
1946 virtual locations and push it. TOKENS_BUFF is the buffer that
1947 contains the tokens pointed to by FIRST. If TOKENS_BUFF is
1948 non-NULL, it means that the context owns it, meaning that
1949 _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
1950 contains the virtual locations.
1952 A NULL macro means that we should continue the current macro
1953 expansion, in essence. That means that if we are currently in a
1954 macro expansion context, we'll make the new pfile->context refer to
1955 the current macro. */
1957 push_extended_tokens_context (cpp_reader
*pfile
,
1958 cpp_hashnode
*macro
,
1959 _cpp_buff
*token_buff
,
1960 source_location
*virt_locs
,
1961 const cpp_token
**first
,
1964 cpp_context
*context
;
1968 macro
= macro_of_context (pfile
->context
);
1970 context
= next_context (pfile
);
1971 context
->tokens_kind
= TOKENS_KIND_EXTENDED
;
1972 context
->buff
= token_buff
;
1974 m
= XNEW (macro_context
);
1975 m
->macro_node
= macro
;
1976 m
->virt_locs
= virt_locs
;
1977 m
->cur_virt_loc
= virt_locs
;
1979 FIRST (context
).ptoken
= first
;
1980 LAST (context
).ptoken
= first
+ count
;
1983 /* Push a traditional macro's replacement text. */
1985 _cpp_push_text_context (cpp_reader
*pfile
, cpp_hashnode
*macro
,
1986 const uchar
*start
, size_t len
)
1988 cpp_context
*context
= next_context (pfile
);
1990 context
->tokens_kind
= TOKENS_KIND_DIRECT
;
1991 context
->c
.macro
= macro
;
1992 context
->buff
= NULL
;
1993 CUR (context
) = start
;
1994 RLIMIT (context
) = start
+ len
;
1995 macro
->flags
|= NODE_DISABLED
;
1998 /* Creates a buffer that holds tokens a.k.a "token buffer", usually
1999 for the purpose of storing them on a cpp_context. If VIRT_LOCS is
2000 non-null (which means that -ftrack-macro-expansion is on),
2001 *VIRT_LOCS is set to a newly allocated buffer that is supposed to
2002 hold the virtual locations of the tokens resulting from macro
2005 tokens_buff_new (cpp_reader
*pfile
, size_t len
,
2006 source_location
**virt_locs
)
2008 size_t tokens_size
= len
* sizeof (cpp_token
*);
2009 size_t locs_size
= len
* sizeof (source_location
);
2011 if (virt_locs
!= NULL
)
2012 *virt_locs
= XNEWVEC (source_location
, locs_size
);
2013 return _cpp_get_buff (pfile
, tokens_size
);
2016 /* Returns the number of tokens contained in a token buffer. The
2017 buffer holds a set of cpp_token*. */
2019 tokens_buff_count (_cpp_buff
*buff
)
2021 return (BUFF_FRONT (buff
) - buff
->base
) / sizeof (cpp_token
*);
2024 /* Return a pointer to the last token contained in the token buffer
2026 static const cpp_token
**
2027 tokens_buff_last_token_ptr (_cpp_buff
*buff
)
2029 return &((const cpp_token
**) BUFF_FRONT (buff
))[-1];
2032 /* Remove the last token contained in the token buffer TOKENS_BUFF.
2033 If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer
2034 containing the virtual locations of the tokens in TOKENS_BUFF; in
2035 which case the function updates that buffer as well. */
2037 tokens_buff_remove_last_token (_cpp_buff
*tokens_buff
)
2040 if (BUFF_FRONT (tokens_buff
) > tokens_buff
->base
)
2041 BUFF_FRONT (tokens_buff
) =
2042 (unsigned char *) &((cpp_token
**) BUFF_FRONT (tokens_buff
))[-1];
2045 /* Insert a token into the token buffer at the position pointed to by
2046 DEST. Note that the buffer is not enlarged so the previous token
2047 that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null,
2048 means -ftrack-macro-expansion is effect; it then points to where to
2049 insert the virtual location of TOKEN. TOKEN is the token to
2050 insert. VIRT_LOC is the virtual location of the token, i.e, the
2051 location possibly encoding its locus across macro expansion. If
2052 TOKEN is an argument of a function-like macro (inside a macro
2053 replacement list), PARM_DEF_LOC is the spelling location of the
2054 macro parameter that TOKEN is replacing, in the replacement list of
2055 the macro. If TOKEN is not an argument of a function-like macro or
2056 if it doesn't come from a macro expansion, then VIRT_LOC can just
2057 be set to the same value as PARM_DEF_LOC. If MAP is non null, it
2058 means TOKEN comes from a macro expansion and MAP is the macro map
2059 associated to the macro. MACRO_TOKEN_INDEX points to the index of
2060 the token in the macro map; it is not considered if MAP is NULL.
2062 Upon successful completion this function returns the a pointer to
2063 the position of the token coming right after the insertion
2065 static inline const cpp_token
**
2066 tokens_buff_put_token_to (const cpp_token
**dest
,
2067 source_location
*virt_loc_dest
,
2068 const cpp_token
*token
,
2069 source_location virt_loc
,
2070 source_location parm_def_loc
,
2071 const line_map_macro
*map
,
2072 unsigned int macro_token_index
)
2074 source_location macro_loc
= virt_loc
;
2075 const cpp_token
**result
;
2079 /* -ftrack-macro-expansion is on. */
2081 macro_loc
= linemap_add_macro_token (map
, macro_token_index
,
2082 virt_loc
, parm_def_loc
);
2083 *virt_loc_dest
= macro_loc
;
2091 /* Adds a token at the end of the tokens contained in BUFFER. Note
2092 that this function doesn't enlarge BUFFER when the number of tokens
2093 reaches BUFFER's size; it aborts in that situation.
2095 TOKEN is the token to append. VIRT_LOC is the virtual location of
2096 the token, i.e, the location possibly encoding its locus across
2097 macro expansion. If TOKEN is an argument of a function-like macro
2098 (inside a macro replacement list), PARM_DEF_LOC is the location of
2099 the macro parameter that TOKEN is replacing. If TOKEN doesn't come
2100 from a macro expansion, then VIRT_LOC can just be set to the same
2101 value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes
2102 from a macro expansion and MAP is the macro map associated to the
2103 macro. MACRO_TOKEN_INDEX points to the index of the token in the
2104 macro map; It is not considered if MAP is NULL. If VIRT_LOCS is
2105 non-null, it means -ftrack-macro-expansion is on; in which case
2106 this function adds the virtual location DEF_LOC to the VIRT_LOCS
2107 array, at the same index as the one of TOKEN in BUFFER. Upon
2108 successful completion this function returns the a pointer to the
2109 position of the token coming right after the insertion point. */
2110 static const cpp_token
**
2111 tokens_buff_add_token (_cpp_buff
*buffer
,
2112 source_location
*virt_locs
,
2113 const cpp_token
*token
,
2114 source_location virt_loc
,
2115 source_location parm_def_loc
,
2116 const line_map_macro
*map
,
2117 unsigned int macro_token_index
)
2119 const cpp_token
**result
;
2120 source_location
*virt_loc_dest
= NULL
;
2121 unsigned token_index
=
2122 (BUFF_FRONT (buffer
) - buffer
->base
) / sizeof (cpp_token
*);
2124 /* Abort if we pass the end the buffer. */
2125 if (BUFF_FRONT (buffer
) > BUFF_LIMIT (buffer
))
2128 if (virt_locs
!= NULL
)
2129 virt_loc_dest
= &virt_locs
[token_index
];
2132 tokens_buff_put_token_to ((const cpp_token
**) BUFF_FRONT (buffer
),
2133 virt_loc_dest
, token
, virt_loc
, parm_def_loc
,
2134 map
, macro_token_index
);
2136 BUFF_FRONT (buffer
) = (unsigned char *) result
;
2140 /* Allocate space for the function-like macro argument ARG to store
2141 the tokens resulting from the macro-expansion of the tokens that
2142 make up ARG itself. That space is allocated in ARG->expanded and
2143 needs to be freed using free. */
2145 alloc_expanded_arg_mem (cpp_reader
*pfile
, macro_arg
*arg
, size_t capacity
)
2147 #ifdef ENABLE_CHECKING
2148 if (arg
->expanded
!= NULL
2149 || arg
->expanded_virt_locs
!= NULL
)
2152 arg
->expanded
= XNEWVEC (const cpp_token
*, capacity
);
2153 if (CPP_OPTION (pfile
, track_macro_expansion
))
2154 arg
->expanded_virt_locs
= XNEWVEC (source_location
, capacity
);
2158 /* If necessary, enlarge ARG->expanded to so that it can contain SIZE
2161 ensure_expanded_arg_room (cpp_reader
*pfile
, macro_arg
*arg
,
2162 size_t size
, size_t *expanded_capacity
)
2164 if (size
<= *expanded_capacity
)
2170 XRESIZEVEC (const cpp_token
*, arg
->expanded
, size
);
2171 *expanded_capacity
= size
;
2173 if (CPP_OPTION (pfile
, track_macro_expansion
))
2175 if (arg
->expanded_virt_locs
== NULL
)
2176 arg
->expanded_virt_locs
= XNEWVEC (source_location
, size
);
2178 arg
->expanded_virt_locs
= XRESIZEVEC (source_location
,
2179 arg
->expanded_virt_locs
,
2184 /* Expand an argument ARG before replacing parameters in a
2185 function-like macro. This works by pushing a context with the
2186 argument's tokens, and then expanding that into a temporary buffer
2187 as if it were a normal part of the token stream. collect_args()
2188 has terminated the argument's tokens with a CPP_EOF so that we know
2189 when we have fully expanded the argument. */
2191 expand_arg (cpp_reader
*pfile
, macro_arg
*arg
)
2194 bool saved_warn_trad
;
2195 bool track_macro_exp_p
= CPP_OPTION (pfile
, track_macro_expansion
);
2198 || arg
->expanded
!= NULL
)
2201 /* Don't warn about funlike macros when pre-expanding. */
2202 saved_warn_trad
= CPP_WTRADITIONAL (pfile
);
2203 CPP_WTRADITIONAL (pfile
) = 0;
2205 /* Loop, reading in the tokens of the argument. */
2207 alloc_expanded_arg_mem (pfile
, arg
, capacity
);
2209 if (track_macro_exp_p
)
2210 push_extended_tokens_context (pfile
, NULL
, NULL
,
2215 push_ptoken_context (pfile
, NULL
, NULL
,
2216 arg
->first
, arg
->count
+ 1);
2220 const cpp_token
*token
;
2221 source_location location
;
2223 ensure_expanded_arg_room (pfile
, arg
, arg
->expanded_count
+ 1,
2226 token
= cpp_get_token_1 (pfile
, &location
);
2228 if (token
->type
== CPP_EOF
)
2231 set_arg_token (arg
, token
, location
,
2232 arg
->expanded_count
, MACRO_ARG_TOKEN_EXPANDED
,
2233 CPP_OPTION (pfile
, track_macro_expansion
));
2234 arg
->expanded_count
++;
2237 _cpp_pop_context (pfile
);
2239 CPP_WTRADITIONAL (pfile
) = saved_warn_trad
;
2242 /* Returns the macro associated to the current context if we are in
2243 the context a macro expansion, NULL otherwise. */
2244 static cpp_hashnode
*
2245 macro_of_context (cpp_context
*context
)
2247 if (context
== NULL
)
2250 return (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2251 ? context
->c
.mc
->macro_node
2255 /* Return TRUE iff we are expanding a macro or are about to start
2256 expanding one. If we are effectively expanding a macro, the
2257 function macro_of_context returns a pointer to the macro being
2260 in_macro_expansion_p (cpp_reader
*pfile
)
2265 return (pfile
->about_to_expand_macro_p
2266 || macro_of_context (pfile
->context
));
2269 /* Pop the current context off the stack, re-enabling the macro if the
2270 context represented a macro's replacement list. Initially the
2271 context structure was not freed so that we can re-use it later, but
2272 now we do free it to reduce peak memory consumption. */
2274 _cpp_pop_context (cpp_reader
*pfile
)
2276 cpp_context
*context
= pfile
->context
;
2278 /* We should not be popping the base context. */
2279 if (context
== &pfile
->base_context
)
2282 if (context
->c
.macro
)
2284 cpp_hashnode
*macro
;
2285 if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2287 macro_context
*mc
= context
->c
.mc
;
2288 macro
= mc
->macro_node
;
2289 /* If context->buff is set, it means the life time of tokens
2290 is bound to the life time of this context; so we must
2291 free the tokens; that means we must free the virtual
2292 locations of these tokens too. */
2293 if (context
->buff
&& mc
->virt_locs
)
2295 free (mc
->virt_locs
);
2296 mc
->virt_locs
= NULL
;
2299 context
->c
.mc
= NULL
;
2302 macro
= context
->c
.macro
;
2304 /* Beware that MACRO can be NULL in cases like when we are
2305 called from expand_arg. In those cases, a dummy context with
2306 tokens is pushed just for the purpose of walking them using
2307 cpp_get_token_1. In that case, no 'macro' field is set into
2308 the dummy context. */
2310 /* Several contiguous macro expansion contexts can be
2311 associated to the same macro; that means it's the same
2312 macro expansion that spans across all these (sub)
2313 contexts. So we should re-enable an expansion-disabled
2314 macro only when we are sure we are really out of that
2316 && macro_of_context (context
->prev
) != macro
)
2317 macro
->flags
&= ~NODE_DISABLED
;
2319 if (macro
== pfile
->top_most_macro_node
&& context
->prev
== NULL
)
2320 /* We are popping the context of the top-most macro node. */
2321 pfile
->top_most_macro_node
= NULL
;
2326 /* Decrease memory peak consumption by freeing the memory used
2328 _cpp_free_buff (context
->buff
);
2331 pfile
->context
= context
->prev
;
2332 /* decrease peak memory consumption by feeing the context. */
2333 pfile
->context
->next
= NULL
;
2337 /* Return TRUE if we reached the end of the set of tokens stored in
2338 CONTEXT, FALSE otherwise. */
2340 reached_end_of_context (cpp_context
*context
)
2342 if (context
->tokens_kind
== TOKENS_KIND_DIRECT
)
2343 return FIRST (context
).token
== LAST (context
).token
;
2344 else if (context
->tokens_kind
== TOKENS_KIND_INDIRECT
2345 || context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2346 return FIRST (context
).ptoken
== LAST (context
).ptoken
;
2351 /* Consume the next token contained in the current context of PFILE,
2352 and return it in *TOKEN. It's "full location" is returned in
2353 *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
2354 means the location encoding the locus of the token across macro
2355 expansion; otherwise it's just is the "normal" location of the
2356 token which (*TOKEN)->src_loc. */
2358 consume_next_token_from_context (cpp_reader
*pfile
,
2359 const cpp_token
** token
,
2360 source_location
*location
)
2362 cpp_context
*c
= pfile
->context
;
2364 if ((c
)->tokens_kind
== TOKENS_KIND_DIRECT
)
2366 *token
= FIRST (c
).token
;
2367 *location
= (*token
)->src_loc
;
2370 else if ((c
)->tokens_kind
== TOKENS_KIND_INDIRECT
)
2372 *token
= *FIRST (c
).ptoken
;
2373 *location
= (*token
)->src_loc
;
2376 else if ((c
)->tokens_kind
== TOKENS_KIND_EXTENDED
)
2378 macro_context
*m
= c
->c
.mc
;
2379 *token
= *FIRST (c
).ptoken
;
2382 *location
= *m
->cur_virt_loc
;
2386 *location
= (*token
)->src_loc
;
2393 /* In the traditional mode of the preprocessor, if we are currently in
2394 a directive, the location of a token must be the location of the
2395 start of the directive line. This function returns the proper
2396 location if we are in the traditional mode, and just returns
2397 LOCATION otherwise. */
2399 static inline source_location
2400 maybe_adjust_loc_for_trad_cpp (cpp_reader
*pfile
, source_location location
)
2402 if (CPP_OPTION (pfile
, traditional
))
2404 if (pfile
->state
.in_directive
)
2405 return pfile
->directive_line
;
2410 /* Routine to get a token as well as its location.
2412 Macro expansions and directives are transparently handled,
2413 including entering included files. Thus tokens are post-macro
2414 expansion, and after any intervening directives. External callers
2415 see CPP_EOF only at EOF. Internal callers also see it when meeting
2416 a directive inside a macro call, when at the end of a directive and
2417 state.in_directive is still 1, and at the end of argument
2420 LOC is an out parameter; *LOC is set to the location "as expected
2421 by the user". Please read the comment of
2422 cpp_get_token_with_location to learn more about the meaning of this
2424 static const cpp_token
*
2425 cpp_get_token_1 (cpp_reader
*pfile
, source_location
*location
)
2427 const cpp_token
*result
;
2428 /* This token is a virtual token that either encodes a location
2429 related to macro expansion or a spelling location. */
2430 source_location virt_loc
= 0;
2431 /* pfile->about_to_expand_macro_p can be overriden by indirect calls
2432 to functions that push macro contexts. So let's save it so that
2433 we can restore it when we are about to leave this routine. */
2434 bool saved_about_to_expand_macro
= pfile
->about_to_expand_macro_p
;
2439 cpp_context
*context
= pfile
->context
;
2441 /* Context->prev == 0 <=> base context. */
2444 result
= _cpp_lex_token (pfile
);
2445 virt_loc
= result
->src_loc
;
2447 else if (!reached_end_of_context (context
))
2449 consume_next_token_from_context (pfile
, &result
,
2451 if (result
->flags
& PASTE_LEFT
)
2453 paste_all_tokens (pfile
, result
);
2454 if (pfile
->state
.in_directive
)
2456 result
= padding_token (pfile
, result
);
2462 if (pfile
->context
->c
.macro
)
2463 ++num_expanded_macros_counter
;
2464 _cpp_pop_context (pfile
);
2465 if (pfile
->state
.in_directive
)
2467 result
= &pfile
->avoid_paste
;
2471 if (pfile
->state
.in_directive
&& result
->type
== CPP_COMMENT
)
2474 if (result
->type
!= CPP_NAME
)
2477 node
= result
->val
.node
.node
;
2479 if (node
->type
!= NT_MACRO
|| (result
->flags
& NO_EXPAND
))
2482 if (!(node
->flags
& NODE_DISABLED
))
2485 /* If not in a macro context, and we're going to start an
2486 expansion, record the location and the top level macro
2487 about to be expanded. */
2488 if (!in_macro_expansion_p (pfile
))
2490 pfile
->invocation_location
= result
->src_loc
;
2491 pfile
->top_most_macro_node
= node
;
2493 if (pfile
->state
.prevent_expansion
)
2496 /* Conditional macros require that a predicate be evaluated
2498 if ((node
->flags
& NODE_CONDITIONAL
) != 0)
2500 if (pfile
->cb
.macro_to_expand
)
2502 bool whitespace_after
;
2503 const cpp_token
*peek_tok
= cpp_peek_token (pfile
, 0);
2505 whitespace_after
= (peek_tok
->type
== CPP_PADDING
2506 || (peek_tok
->flags
& PREV_WHITE
));
2507 node
= pfile
->cb
.macro_to_expand (pfile
, result
);
2509 ret
= enter_macro_context (pfile
, node
, result
,
2511 else if (whitespace_after
)
2513 /* If macro_to_expand hook returned NULL and it
2514 ate some tokens, see if we don't need to add
2515 a padding token in between this and the
2517 peek_tok
= cpp_peek_token (pfile
, 0);
2518 if (peek_tok
->type
!= CPP_PADDING
2519 && (peek_tok
->flags
& PREV_WHITE
) == 0)
2520 _cpp_push_token_context (pfile
, NULL
,
2521 padding_token (pfile
,
2527 ret
= enter_macro_context (pfile
, node
, result
,
2531 if (pfile
->state
.in_directive
|| ret
== 2)
2533 result
= padding_token (pfile
, result
);
2539 /* Flag this token as always unexpandable. FIXME: move this
2540 to collect_args()?. */
2541 cpp_token
*t
= _cpp_temp_token (pfile
);
2542 t
->type
= result
->type
;
2543 t
->flags
= result
->flags
| NO_EXPAND
;
2544 t
->val
= result
->val
;
2552 if (location
!= NULL
)
2555 virt_loc
= result
->src_loc
;
2556 *location
= virt_loc
;
2558 if (!CPP_OPTION (pfile
, track_macro_expansion
)
2559 && macro_of_context (pfile
->context
) != NULL
)
2560 /* We are in a macro expansion context, are not tracking
2561 virtual location, but were asked to report the location
2562 of the expansion point of the macro being expanded. */
2563 *location
= pfile
->invocation_location
;
2565 *location
= maybe_adjust_loc_for_trad_cpp (pfile
, *location
);
2568 pfile
->about_to_expand_macro_p
= saved_about_to_expand_macro
;
2572 /* External routine to get a token. Also used nearly everywhere
2573 internally, except for places where we know we can safely call
2574 _cpp_lex_token directly, such as lexing a directive name.
2576 Macro expansions and directives are transparently handled,
2577 including entering included files. Thus tokens are post-macro
2578 expansion, and after any intervening directives. External callers
2579 see CPP_EOF only at EOF. Internal callers also see it when meeting
2580 a directive inside a macro call, when at the end of a directive and
2581 state.in_directive is still 1, and at the end of argument
2584 cpp_get_token (cpp_reader
*pfile
)
2586 return cpp_get_token_1 (pfile
, NULL
);
2589 /* Like cpp_get_token, but also returns a virtual token location
2590 separate from the spelling location carried by the returned token.
2592 LOC is an out parameter; *LOC is set to the location "as expected
2593 by the user". This matters when a token results from macro
2594 expansion; in that case the token's spelling location indicates the
2595 locus of the token in the definition of the macro but *LOC
2596 virtually encodes all the other meaningful locuses associated to
2599 What? virtual location? Yes, virtual location.
2601 If the token results from macro expansion and if macro expansion
2602 location tracking is enabled its virtual location encodes (at the
2605 - the spelling location of the token
2607 - the locus of the macro expansion point
2609 - the locus of the point where the token got instantiated as part
2610 of the macro expansion process.
2612 You have to use the linemap API to get the locus you are interested
2613 in from a given virtual location.
2615 Note however that virtual locations are not necessarily ordered for
2616 relations '<' and '>'. One must use the function
2617 linemap_location_before_p instead of using the relational operator
2620 If macro expansion tracking is off and if the token results from
2621 macro expansion the virtual location is the expansion point of the
2622 macro that got expanded.
2624 When the token doesn't result from macro expansion, the virtual
2625 location is just the same thing as its spelling location. */
2628 cpp_get_token_with_location (cpp_reader
*pfile
, source_location
*loc
)
2630 return cpp_get_token_1 (pfile
, loc
);
2633 /* Returns true if we're expanding an object-like macro that was
2634 defined in a system header. Just checks the macro at the top of
2635 the stack. Used for diagnostic suppression. */
2637 cpp_sys_macro_p (cpp_reader
*pfile
)
2639 cpp_hashnode
*node
= NULL
;
2641 if (pfile
->context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2642 node
= pfile
->context
->c
.mc
->macro_node
;
2644 node
= pfile
->context
->c
.macro
;
2646 return node
&& node
->value
.macro
&& node
->value
.macro
->syshdr
;
2649 /* Read each token in, until end of the current file. Directives are
2650 transparently processed. */
2652 cpp_scan_nooutput (cpp_reader
*pfile
)
2654 /* Request a CPP_EOF token at the end of this file, rather than
2655 transparently continuing with the including file. */
2656 pfile
->buffer
->return_at_eof
= true;
2658 pfile
->state
.discarding_output
++;
2659 pfile
->state
.prevent_expansion
++;
2661 if (CPP_OPTION (pfile
, traditional
))
2662 while (_cpp_read_logical_line_trad (pfile
))
2665 while (cpp_get_token (pfile
)->type
!= CPP_EOF
)
2668 pfile
->state
.discarding_output
--;
2669 pfile
->state
.prevent_expansion
--;
2672 /* Step back one or more tokens obtained from the lexer. */
2674 _cpp_backup_tokens_direct (cpp_reader
*pfile
, unsigned int count
)
2676 pfile
->lookaheads
+= count
;
2680 if (pfile
->cur_token
== pfile
->cur_run
->base
2681 /* Possible with -fpreprocessed and no leading #line. */
2682 && pfile
->cur_run
->prev
!= NULL
)
2684 pfile
->cur_run
= pfile
->cur_run
->prev
;
2685 pfile
->cur_token
= pfile
->cur_run
->limit
;
2690 /* Step back one (or more) tokens. Can only step back more than 1 if
2691 they are from the lexer, and not from macro expansion. */
2693 _cpp_backup_tokens (cpp_reader
*pfile
, unsigned int count
)
2695 if (pfile
->context
->prev
== NULL
)
2696 _cpp_backup_tokens_direct (pfile
, count
);
2701 if (pfile
->context
->tokens_kind
== TOKENS_KIND_DIRECT
)
2702 FIRST (pfile
->context
).token
--;
2703 else if (pfile
->context
->tokens_kind
== TOKENS_KIND_INDIRECT
)
2704 FIRST (pfile
->context
).ptoken
--;
2705 else if (pfile
->context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2707 FIRST (pfile
->context
).ptoken
--;
2708 if (pfile
->context
->c
.macro
)
2710 macro_context
*m
= pfile
->context
->c
.mc
;
2712 #ifdef ENABLE_CHECKING
2713 if (m
->cur_virt_loc
< m
->virt_locs
)
2725 /* #define directive parsing and handling. */
2727 /* Returns nonzero if a macro redefinition warning is required. */
2729 warn_of_redefinition (cpp_reader
*pfile
, cpp_hashnode
*node
,
2730 const cpp_macro
*macro2
)
2732 const cpp_macro
*macro1
;
2735 /* Some redefinitions need to be warned about regardless. */
2736 if (node
->flags
& NODE_WARN
)
2739 /* Suppress warnings for builtins that lack the NODE_WARN flag,
2740 unless Wbuiltin-macro-redefined. */
2741 if (node
->flags
& NODE_BUILTIN
2742 && (!pfile
->cb
.user_builtin_macro
2743 || !pfile
->cb
.user_builtin_macro (pfile
, node
)))
2744 return CPP_OPTION (pfile
, warn_builtin_macro_redefined
);
2746 /* Redefinitions of conditional (context-sensitive) macros, on
2747 the other hand, must be allowed silently. */
2748 if (node
->flags
& NODE_CONDITIONAL
)
2751 /* Redefinition of a macro is allowed if and only if the old and new
2752 definitions are the same. (6.10.3 paragraph 2). */
2753 macro1
= node
->value
.macro
;
2755 /* Don't check count here as it can be different in valid
2756 traditional redefinitions with just whitespace differences. */
2757 if (macro1
->paramc
!= macro2
->paramc
2758 || macro1
->fun_like
!= macro2
->fun_like
2759 || macro1
->variadic
!= macro2
->variadic
)
2762 /* Check parameter spellings. */
2763 for (i
= 0; i
< macro1
->paramc
; i
++)
2764 if (macro1
->params
[i
] != macro2
->params
[i
])
2767 /* Check the replacement text or tokens. */
2768 if (CPP_OPTION (pfile
, traditional
))
2769 return _cpp_expansions_different_trad (macro1
, macro2
);
2771 if (macro1
->count
!= macro2
->count
)
2774 for (i
= 0; i
< macro1
->count
; i
++)
2775 if (!_cpp_equiv_tokens (¯o1
->exp
.tokens
[i
], ¯o2
->exp
.tokens
[i
]))
2781 /* Free the definition of hashnode H. */
2783 _cpp_free_definition (cpp_hashnode
*h
)
2785 /* Macros and assertions no longer have anything to free. */
2787 /* Clear builtin flag in case of redefinition. */
2788 h
->flags
&= ~(NODE_BUILTIN
| NODE_DISABLED
| NODE_USED
);
2791 /* Save parameter NODE (spelling SPELLING) to the parameter list of
2792 macro MACRO. Returns zero on success, nonzero if the parameter is
2795 _cpp_save_parameter (cpp_reader
*pfile
, cpp_macro
*macro
, cpp_hashnode
*node
,
2796 cpp_hashnode
*spelling
)
2799 /* Constraint 6.10.3.6 - duplicate parameter names. */
2800 if (node
->flags
& NODE_MACRO_ARG
)
2802 cpp_error (pfile
, CPP_DL_ERROR
, "duplicate macro parameter \"%s\"",
2807 if (BUFF_ROOM (pfile
->a_buff
)
2808 < (macro
->paramc
+ 1) * sizeof (cpp_hashnode
*))
2809 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (cpp_hashnode
*));
2811 ((cpp_hashnode
**) BUFF_FRONT (pfile
->a_buff
))[macro
->paramc
++] = spelling
;
2812 node
->flags
|= NODE_MACRO_ARG
;
2813 len
= macro
->paramc
* sizeof (struct macro_arg_saved_data
);
2814 if (len
> pfile
->macro_buffer_len
)
2816 pfile
->macro_buffer
= XRESIZEVEC (unsigned char, pfile
->macro_buffer
,
2818 pfile
->macro_buffer_len
= len
;
2820 struct macro_arg_saved_data save
;
2821 save
.value
= node
->value
;
2822 save
.canonical_node
= node
;
2823 ((struct macro_arg_saved_data
*) pfile
->macro_buffer
)[macro
->paramc
- 1]
2826 node
->value
.arg_index
= macro
->paramc
;
2830 /* Check the syntax of the parameters in a MACRO definition. Returns
2831 false if an error occurs. */
2833 parse_params (cpp_reader
*pfile
, cpp_macro
*macro
)
2835 unsigned int prev_ident
= 0;
2839 const cpp_token
*token
= _cpp_lex_token (pfile
);
2841 switch (token
->type
)
2844 /* Allow/ignore comments in parameter lists if we are
2845 preserving comments in macro expansions. */
2846 if (token
->type
== CPP_COMMENT
2847 && ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
))
2850 cpp_error (pfile
, CPP_DL_ERROR
,
2851 "\"%s\" may not appear in macro parameter list",
2852 cpp_token_as_text (pfile
, token
));
2858 cpp_error (pfile
, CPP_DL_ERROR
,
2859 "macro parameters must be comma-separated");
2864 if (_cpp_save_parameter (pfile
, macro
, token
->val
.node
.node
,
2865 token
->val
.node
.spelling
))
2869 case CPP_CLOSE_PAREN
:
2870 if (prev_ident
|| macro
->paramc
== 0)
2873 /* Fall through to pick up the error. */
2877 cpp_error (pfile
, CPP_DL_ERROR
, "parameter name missing");
2884 macro
->variadic
= 1;
2887 _cpp_save_parameter (pfile
, macro
,
2888 pfile
->spec_nodes
.n__VA_ARGS__
,
2889 pfile
->spec_nodes
.n__VA_ARGS__
);
2890 pfile
->state
.va_args_ok
= 1;
2891 if (! CPP_OPTION (pfile
, c99
)
2892 && CPP_OPTION (pfile
, cpp_pedantic
)
2893 && CPP_OPTION (pfile
, warn_variadic_macros
))
2895 if (CPP_OPTION (pfile
, cplusplus
))
2897 (pfile
, CPP_W_VARIADIC_MACROS
,
2898 "anonymous variadic macros were introduced in C++11");
2901 (pfile
, CPP_W_VARIADIC_MACROS
,
2902 "anonymous variadic macros were introduced in C99");
2904 else if (CPP_OPTION (pfile
, cpp_warn_c90_c99_compat
) > 0
2905 && ! CPP_OPTION (pfile
, cplusplus
))
2906 cpp_error (pfile
, CPP_DL_WARNING
,
2907 "anonymous variadic macros were introduced in C99");
2909 else if (CPP_OPTION (pfile
, cpp_pedantic
)
2910 && CPP_OPTION (pfile
, warn_variadic_macros
))
2912 if (CPP_OPTION (pfile
, cplusplus
))
2913 cpp_pedwarning (pfile
, CPP_W_VARIADIC_MACROS
,
2914 "ISO C++ does not permit named variadic macros");
2916 cpp_pedwarning (pfile
, CPP_W_VARIADIC_MACROS
,
2917 "ISO C does not permit named variadic macros");
2920 /* We're at the end, and just expect a closing parenthesis. */
2921 token
= _cpp_lex_token (pfile
);
2922 if (token
->type
== CPP_CLOSE_PAREN
)
2927 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' in macro parameter list");
2933 /* Allocate room for a token from a macro's replacement list. */
2935 alloc_expansion_token (cpp_reader
*pfile
, cpp_macro
*macro
)
2937 if (BUFF_ROOM (pfile
->a_buff
) < (macro
->count
+ 1) * sizeof (cpp_token
))
2938 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (cpp_token
));
2940 return &((cpp_token
*) BUFF_FRONT (pfile
->a_buff
))[macro
->count
++];
2943 /* Lex a token from the expansion of MACRO, but mark parameters as we
2944 find them and warn of traditional stringification. */
2946 lex_expansion_token (cpp_reader
*pfile
, cpp_macro
*macro
)
2948 cpp_token
*token
, *saved_cur_token
;
2950 saved_cur_token
= pfile
->cur_token
;
2951 pfile
->cur_token
= alloc_expansion_token (pfile
, macro
);
2952 token
= _cpp_lex_direct (pfile
);
2953 pfile
->cur_token
= saved_cur_token
;
2955 /* Is this a parameter? */
2956 if (token
->type
== CPP_NAME
2957 && (token
->val
.node
.node
->flags
& NODE_MACRO_ARG
) != 0)
2959 cpp_hashnode
*spelling
= token
->val
.node
.spelling
;
2960 token
->type
= CPP_MACRO_ARG
;
2961 token
->val
.macro_arg
.arg_no
= token
->val
.node
.node
->value
.arg_index
;
2962 token
->val
.macro_arg
.spelling
= spelling
;
2964 else if (CPP_WTRADITIONAL (pfile
) && macro
->paramc
> 0
2965 && (token
->type
== CPP_STRING
|| token
->type
== CPP_CHAR
))
2966 check_trad_stringification (pfile
, macro
, &token
->val
.str
);
2972 create_iso_definition (cpp_reader
*pfile
, cpp_macro
*macro
)
2975 const cpp_token
*ctoken
;
2976 bool following_paste_op
= false;
2977 const char *paste_op_error_msg
=
2978 N_("'##' cannot appear at either end of a macro expansion");
2979 unsigned int num_extra_tokens
= 0;
2981 /* Get the first token of the expansion (or the '(' of a
2982 function-like macro). */
2983 ctoken
= _cpp_lex_token (pfile
);
2985 if (ctoken
->type
== CPP_OPEN_PAREN
&& !(ctoken
->flags
& PREV_WHITE
))
2987 bool ok
= parse_params (pfile
, macro
);
2988 macro
->params
= (cpp_hashnode
**) BUFF_FRONT (pfile
->a_buff
);
2992 /* Success. Commit or allocate the parameter array. */
2993 if (pfile
->hash_table
->alloc_subobject
)
2995 cpp_hashnode
**params
=
2996 (cpp_hashnode
**) pfile
->hash_table
->alloc_subobject
2997 (sizeof (cpp_hashnode
*) * macro
->paramc
);
2998 memcpy (params
, macro
->params
,
2999 sizeof (cpp_hashnode
*) * macro
->paramc
);
3000 macro
->params
= params
;
3003 BUFF_FRONT (pfile
->a_buff
) = (uchar
*) ¯o
->params
[macro
->paramc
];
3004 macro
->fun_like
= 1;
3006 else if (ctoken
->type
!= CPP_EOF
&& !(ctoken
->flags
& PREV_WHITE
))
3008 /* While ISO C99 requires whitespace before replacement text
3009 in a macro definition, ISO C90 with TC1 allows characters
3010 from the basic source character set there. */
3011 if (CPP_OPTION (pfile
, c99
))
3013 if (CPP_OPTION (pfile
, cplusplus
))
3014 cpp_error (pfile
, CPP_DL_PEDWARN
,
3015 "ISO C++11 requires whitespace after the macro name");
3017 cpp_error (pfile
, CPP_DL_PEDWARN
,
3018 "ISO C99 requires whitespace after the macro name");
3022 int warntype
= CPP_DL_WARNING
;
3023 switch (ctoken
->type
)
3027 case CPP_OBJC_STRING
:
3028 /* '@' is not in basic character set. */
3029 warntype
= CPP_DL_PEDWARN
;
3032 /* Basic character set sans letters, digits and _. */
3033 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
3034 ctoken
->val
.str
.text
[0]) == NULL
)
3035 warntype
= CPP_DL_PEDWARN
;
3038 /* All other tokens start with a character from basic
3042 cpp_error (pfile
, warntype
,
3043 "missing whitespace after the macro name");
3047 if (macro
->fun_like
)
3048 token
= lex_expansion_token (pfile
, macro
);
3051 token
= alloc_expansion_token (pfile
, macro
);
3057 /* Check the stringifying # constraint 6.10.3.2.1 of
3058 function-like macros when lexing the subsequent token. */
3059 if (macro
->count
> 1 && token
[-1].type
== CPP_HASH
&& macro
->fun_like
)
3061 if (token
->type
== CPP_MACRO_ARG
)
3063 if (token
->flags
& PREV_WHITE
)
3064 token
->flags
|= SP_PREV_WHITE
;
3065 if (token
[-1].flags
& DIGRAPH
)
3066 token
->flags
|= SP_DIGRAPH
;
3067 token
->flags
&= ~PREV_WHITE
;
3068 token
->flags
|= STRINGIFY_ARG
;
3069 token
->flags
|= token
[-1].flags
& PREV_WHITE
;
3070 token
[-1] = token
[0];
3073 /* Let assembler get away with murder. */
3074 else if (CPP_OPTION (pfile
, lang
) != CLK_ASM
)
3076 cpp_error (pfile
, CPP_DL_ERROR
,
3077 "'#' is not followed by a macro parameter");
3082 if (token
->type
== CPP_EOF
)
3084 /* Paste operator constraint 6.10.3.3.1:
3085 Token-paste ##, can appear in both object-like and
3086 function-like macros, but not at the end. */
3087 if (following_paste_op
)
3089 cpp_error (pfile
, CPP_DL_ERROR
, paste_op_error_msg
);
3095 /* Paste operator constraint 6.10.3.3.1. */
3096 if (token
->type
== CPP_PASTE
)
3098 /* Token-paste ##, can appear in both object-like and
3099 function-like macros, but not at the beginning. */
3100 if (macro
->count
== 1)
3102 cpp_error (pfile
, CPP_DL_ERROR
, paste_op_error_msg
);
3106 if (token
[-1].flags
& PASTE_LEFT
)
3108 macro
->extra_tokens
= 1;
3110 token
->val
.token_no
= macro
->count
- 1;
3115 token
[-1].flags
|= PASTE_LEFT
;
3116 if (token
->flags
& DIGRAPH
)
3117 token
[-1].flags
|= SP_DIGRAPH
;
3118 if (token
->flags
& PREV_WHITE
)
3119 token
[-1].flags
|= SP_PREV_WHITE
;
3123 following_paste_op
= (token
->type
== CPP_PASTE
);
3124 token
= lex_expansion_token (pfile
, macro
);
3127 macro
->exp
.tokens
= (cpp_token
*) BUFF_FRONT (pfile
->a_buff
);
3128 macro
->traditional
= 0;
3130 /* Don't count the CPP_EOF. */
3133 /* Clear whitespace on first token for warn_of_redefinition(). */
3135 macro
->exp
.tokens
[0].flags
&= ~PREV_WHITE
;
3137 /* Commit or allocate the memory. */
3138 if (pfile
->hash_table
->alloc_subobject
)
3141 (cpp_token
*) pfile
->hash_table
->alloc_subobject (sizeof (cpp_token
)
3143 if (num_extra_tokens
)
3145 /* Place second and subsequent ## or %:%: tokens in
3146 sequences of consecutive such tokens at the end of the
3147 list to preserve information about where they appear, how
3148 they are spelt and whether they are preceded by
3149 whitespace without otherwise interfering with macro
3151 cpp_token
*normal_dest
= tokns
;
3152 cpp_token
*extra_dest
= tokns
+ macro
->count
- num_extra_tokens
;
3154 for (i
= 0; i
< macro
->count
; i
++)
3156 if (macro
->exp
.tokens
[i
].type
== CPP_PASTE
)
3157 *extra_dest
++ = macro
->exp
.tokens
[i
];
3159 *normal_dest
++ = macro
->exp
.tokens
[i
];
3163 memcpy (tokns
, macro
->exp
.tokens
, sizeof (cpp_token
) * macro
->count
);
3164 macro
->exp
.tokens
= tokns
;
3167 BUFF_FRONT (pfile
->a_buff
) = (uchar
*) ¯o
->exp
.tokens
[macro
->count
];
3172 /* Parse a macro and save its expansion. Returns nonzero on success. */
3174 _cpp_create_definition (cpp_reader
*pfile
, cpp_hashnode
*node
)
3180 if (pfile
->hash_table
->alloc_subobject
)
3181 macro
= (cpp_macro
*) pfile
->hash_table
->alloc_subobject
3182 (sizeof (cpp_macro
));
3184 macro
= (cpp_macro
*) _cpp_aligned_alloc (pfile
, sizeof (cpp_macro
));
3185 macro
->line
= pfile
->directive_line
;
3188 macro
->variadic
= 0;
3189 macro
->used
= !CPP_OPTION (pfile
, warn_unused_macros
);
3191 macro
->fun_like
= 0;
3192 macro
->extra_tokens
= 0;
3193 /* To suppress some diagnostics. */
3194 macro
->syshdr
= pfile
->buffer
&& pfile
->buffer
->sysp
!= 0;
3196 if (CPP_OPTION (pfile
, traditional
))
3197 ok
= _cpp_create_trad_definition (pfile
, macro
);
3200 ok
= create_iso_definition (pfile
, macro
);
3202 /* We set the type for SEEN_EOL() in directives.c.
3204 Longer term we should lex the whole line before coming here,
3205 and just copy the expansion. */
3207 /* Stop the lexer accepting __VA_ARGS__. */
3208 pfile
->state
.va_args_ok
= 0;
3211 /* Clear the fast argument lookup indices. */
3212 for (i
= macro
->paramc
; i
-- > 0; )
3214 struct macro_arg_saved_data
*save
=
3215 &((struct macro_arg_saved_data
*) pfile
->macro_buffer
)[i
];
3216 struct cpp_hashnode
*node
= save
->canonical_node
;
3217 node
->flags
&= ~ NODE_MACRO_ARG
;
3218 node
->value
= save
->value
;
3224 if (node
->type
== NT_MACRO
)
3226 if (CPP_OPTION (pfile
, warn_unused_macros
))
3227 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
3229 if (warn_of_redefinition (pfile
, node
, macro
))
3231 const int reason
= ((node
->flags
& NODE_BUILTIN
)
3232 && !(node
->flags
& NODE_WARN
))
3233 ? CPP_W_BUILTIN_MACRO_REDEFINED
: CPP_W_NONE
;
3236 cpp_pedwarning_with_line (pfile
, reason
,
3237 pfile
->directive_line
, 0,
3238 "\"%s\" redefined", NODE_NAME (node
));
3240 if (warned
&& node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
))
3241 cpp_error_with_line (pfile
, CPP_DL_NOTE
,
3242 node
->value
.macro
->line
, 0,
3243 "this is the location of the previous definition");
3247 if (node
->type
!= NT_VOID
)
3248 _cpp_free_definition (node
);
3250 /* Enter definition in hash table. */
3251 node
->type
= NT_MACRO
;
3252 node
->value
.macro
= macro
;
3253 if (! ustrncmp (NODE_NAME (node
), DSC ("__STDC_"))
3254 && ustrcmp (NODE_NAME (node
), (const uchar
*) "__STDC_FORMAT_MACROS")
3255 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
3256 in the C standard, as something that one must use in C++.
3257 However DR#593 and C++11 indicate that they play no role in C++.
3258 We special-case them anyway. */
3259 && ustrcmp (NODE_NAME (node
), (const uchar
*) "__STDC_LIMIT_MACROS")
3260 && ustrcmp (NODE_NAME (node
), (const uchar
*) "__STDC_CONSTANT_MACROS"))
3261 node
->flags
|= NODE_WARN
;
3263 /* If user defines one of the conditional macros, remove the
3265 node
->flags
&= ~NODE_CONDITIONAL
;
3270 /* Warn if a token in STRING matches one of a function-like MACRO's
3273 check_trad_stringification (cpp_reader
*pfile
, const cpp_macro
*macro
,
3274 const cpp_string
*string
)
3276 unsigned int i
, len
;
3277 const uchar
*p
, *q
, *limit
;
3279 /* Loop over the string. */
3280 limit
= string
->text
+ string
->len
- 1;
3281 for (p
= string
->text
+ 1; p
< limit
; p
= q
)
3283 /* Find the start of an identifier. */
3284 while (p
< limit
&& !is_idstart (*p
))
3287 /* Find the end of the identifier. */
3289 while (q
< limit
&& is_idchar (*q
))
3294 /* Loop over the function macro arguments to see if the
3295 identifier inside the string matches one of them. */
3296 for (i
= 0; i
< macro
->paramc
; i
++)
3298 const cpp_hashnode
*node
= macro
->params
[i
];
3300 if (NODE_LEN (node
) == len
3301 && !memcmp (p
, NODE_NAME (node
), len
))
3303 cpp_error (pfile
, CPP_DL_WARNING
,
3304 "macro argument \"%s\" would be stringified in traditional C",
3312 /* Returns the name, arguments and expansion of a macro, in a format
3313 suitable to be read back in again, and therefore also for DWARF 2
3314 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
3315 Caller is expected to generate the "#define" bit if needed. The
3316 returned text is temporary, and automatically freed later. */
3317 const unsigned char *
3318 cpp_macro_definition (cpp_reader
*pfile
, cpp_hashnode
*node
)
3320 unsigned int i
, len
;
3321 const cpp_macro
*macro
;
3322 unsigned char *buffer
;
3324 if (node
->type
!= NT_MACRO
|| (node
->flags
& NODE_BUILTIN
))
3326 if (node
->type
!= NT_MACRO
3327 || !pfile
->cb
.user_builtin_macro
3328 || !pfile
->cb
.user_builtin_macro (pfile
, node
))
3330 cpp_error (pfile
, CPP_DL_ICE
,
3331 "invalid hash type %d in cpp_macro_definition",
3337 macro
= node
->value
.macro
;
3338 /* Calculate length. */
3339 len
= NODE_LEN (node
) * 10 + 2; /* ' ' and NUL. */
3340 if (macro
->fun_like
)
3342 len
+= 4; /* "()" plus possible final ".." of named
3343 varargs (we have + 1 below). */
3344 for (i
= 0; i
< macro
->paramc
; i
++)
3345 len
+= NODE_LEN (macro
->params
[i
]) + 1; /* "," */
3348 /* This should match below where we fill in the buffer. */
3349 if (CPP_OPTION (pfile
, traditional
))
3350 len
+= _cpp_replacement_text_len (macro
);
3353 unsigned int count
= macro_real_token_count (macro
);
3354 for (i
= 0; i
< count
; i
++)
3356 cpp_token
*token
= ¯o
->exp
.tokens
[i
];
3358 if (token
->type
== CPP_MACRO_ARG
)
3359 len
+= NODE_LEN (token
->val
.macro_arg
.spelling
);
3361 len
+= cpp_token_len (token
);
3363 if (token
->flags
& STRINGIFY_ARG
)
3365 if (token
->flags
& PASTE_LEFT
)
3366 len
+= 3; /* " ##" */
3367 if (token
->flags
& PREV_WHITE
)
3372 if (len
> pfile
->macro_buffer_len
)
3374 pfile
->macro_buffer
= XRESIZEVEC (unsigned char,
3375 pfile
->macro_buffer
, len
);
3376 pfile
->macro_buffer_len
= len
;
3379 /* Fill in the buffer. Start with the macro name. */
3380 buffer
= pfile
->macro_buffer
;
3381 buffer
= _cpp_spell_ident_ucns (buffer
, node
);
3383 /* Parameter names. */
3384 if (macro
->fun_like
)
3387 for (i
= 0; i
< macro
->paramc
; i
++)
3389 cpp_hashnode
*param
= macro
->params
[i
];
3391 if (param
!= pfile
->spec_nodes
.n__VA_ARGS__
)
3393 memcpy (buffer
, NODE_NAME (param
), NODE_LEN (param
));
3394 buffer
+= NODE_LEN (param
);
3397 if (i
+ 1 < macro
->paramc
)
3398 /* Don't emit a space after the comma here; we're trying
3399 to emit a Dwarf-friendly definition, and the Dwarf spec
3400 forbids spaces in the argument list. */
3402 else if (macro
->variadic
)
3403 *buffer
++ = '.', *buffer
++ = '.', *buffer
++ = '.';
3408 /* The Dwarf spec requires a space after the macro name, even if the
3409 definition is the empty string. */
3412 if (CPP_OPTION (pfile
, traditional
))
3413 buffer
= _cpp_copy_replacement_text (macro
, buffer
);
3414 else if (macro
->count
)
3415 /* Expansion tokens. */
3417 unsigned int count
= macro_real_token_count (macro
);
3418 for (i
= 0; i
< count
; i
++)
3420 cpp_token
*token
= ¯o
->exp
.tokens
[i
];
3422 if (token
->flags
& PREV_WHITE
)
3424 if (token
->flags
& STRINGIFY_ARG
)
3427 if (token
->type
== CPP_MACRO_ARG
)
3430 NODE_NAME (token
->val
.macro_arg
.spelling
),
3431 NODE_LEN (token
->val
.macro_arg
.spelling
));
3432 buffer
+= NODE_LEN (token
->val
.macro_arg
.spelling
);
3435 buffer
= cpp_spell_token (pfile
, token
, buffer
, true);
3437 if (token
->flags
& PASTE_LEFT
)
3442 /* Next has PREV_WHITE; see _cpp_create_definition. */
3448 return pfile
->macro_buffer
;