1 /* Part of CPP library. (Macro and #define handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
5 Written by Per Bothner, 1994.
6 Based on CCCP program by Paul Rubin, June 1986
7 Adapted to ANSI C, Richard Stallman, Jan 1987
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3, or (at your option) any
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>.
23 In other words, you are welcome to use, share and improve this program.
24 You are forbidden to forbid anyone else to use, share and improve
25 what you give them. Help stamp out software-hoarding! */
32 typedef struct macro_arg macro_arg
;
33 /* This structure represents the tokens of a macro argument. These
34 tokens can be macro themselves, in which case they can be either
35 expanded or unexpanded. When they are expanded, this data
36 structure keeps both the expanded and unexpanded forms. */
39 const cpp_token
**first
; /* First token in unexpanded argument. */
40 const cpp_token
**expanded
; /* Macro-expanded argument. */
41 const cpp_token
*stringified
; /* Stringified argument. */
42 unsigned int count
; /* # of tokens in argument. */
43 unsigned int expanded_count
; /* # of tokens in expanded argument. */
44 source_location
*virt_locs
; /* Where virtual locations for
45 unexpanded tokens are stored. */
46 source_location
*expanded_virt_locs
; /* Where virtual locations for
51 /* The kind of macro tokens which the instance of
52 macro_arg_token_iter is supposed to iterate over. */
53 enum macro_arg_token_kind
{
54 MACRO_ARG_TOKEN_NORMAL
,
55 /* This is a macro argument token that got transformed into a string
56 litteral, e.g. #foo. */
57 MACRO_ARG_TOKEN_STRINGIFIED
,
58 /* This is a token resulting from the expansion of a macro
59 argument that was itself a macro. */
60 MACRO_ARG_TOKEN_EXPANDED
63 /* An iterator over tokens coming from a function-like macro
65 typedef struct macro_arg_token_iter macro_arg_token_iter
;
66 struct macro_arg_token_iter
68 /* Whether or not -ftrack-macro-expansion is used. */
69 bool track_macro_exp_p
;
70 /* The kind of token over which we are supposed to iterate. */
71 enum macro_arg_token_kind kind
;
72 /* A pointer to the current token pointed to by the iterator. */
73 const cpp_token
**token_ptr
;
74 /* A pointer to the "full" location of the current token. If
75 -ftrack-macro-expansion is used this location tracks loci across
77 const source_location
*location_ptr
;
78 #ifdef ENABLE_CHECKING
79 /* The number of times the iterator went forward. This useful only
80 when checking is enabled. */
85 /* Macro expansion. */
87 static int enter_macro_context (cpp_reader
*, cpp_hashnode
*,
88 const cpp_token
*, source_location
);
89 static int builtin_macro (cpp_reader
*, cpp_hashnode
*);
90 static void push_ptoken_context (cpp_reader
*, cpp_hashnode
*, _cpp_buff
*,
91 const cpp_token
**, unsigned int);
92 static void push_extended_tokens_context (cpp_reader
*, cpp_hashnode
*,
93 _cpp_buff
*, source_location
*,
94 const cpp_token
**, unsigned int);
95 static _cpp_buff
*collect_args (cpp_reader
*, const cpp_hashnode
*,
96 _cpp_buff
**, unsigned *);
97 static cpp_context
*next_context (cpp_reader
*);
98 static const cpp_token
*padding_token (cpp_reader
*, const cpp_token
*);
99 static void expand_arg (cpp_reader
*, macro_arg
*);
100 static const cpp_token
*new_string_token (cpp_reader
*, uchar
*, unsigned int);
101 static const cpp_token
*stringify_arg (cpp_reader
*, macro_arg
*);
102 static void paste_all_tokens (cpp_reader
*, const cpp_token
*);
103 static bool paste_tokens (cpp_reader
*, source_location
,
104 const cpp_token
**, const cpp_token
*);
105 static void alloc_expanded_arg_mem (cpp_reader
*, macro_arg
*, size_t);
106 static void ensure_expanded_arg_room (cpp_reader
*, macro_arg
*, size_t, size_t *);
107 static void delete_macro_args (_cpp_buff
*, unsigned num_args
);
108 static void set_arg_token (macro_arg
*, const cpp_token
*,
109 source_location
, size_t,
110 enum macro_arg_token_kind
,
112 static const source_location
*get_arg_token_location (const macro_arg
*,
113 enum macro_arg_token_kind
);
114 static const cpp_token
**arg_token_ptr_at (const macro_arg
*,
116 enum macro_arg_token_kind
,
117 source_location
**virt_location
);
119 static void macro_arg_token_iter_init (macro_arg_token_iter
*, bool,
120 enum macro_arg_token_kind
,
123 static const cpp_token
*macro_arg_token_iter_get_token
124 (const macro_arg_token_iter
*it
);
125 static source_location macro_arg_token_iter_get_location
126 (const macro_arg_token_iter
*);
127 static void macro_arg_token_iter_forward (macro_arg_token_iter
*);
128 static _cpp_buff
*tokens_buff_new (cpp_reader
*, size_t,
130 static size_t tokens_buff_count (_cpp_buff
*);
131 static const cpp_token
**tokens_buff_last_token_ptr (_cpp_buff
*);
132 static inline const cpp_token
**tokens_buff_put_token_to (const cpp_token
**,
137 const struct line_map
*,
140 static const cpp_token
**tokens_buff_add_token (_cpp_buff
*,
145 const struct line_map
*,
147 static inline void tokens_buff_remove_last_token (_cpp_buff
*);
148 static void replace_args (cpp_reader
*, cpp_hashnode
*, cpp_macro
*,
149 macro_arg
*, source_location
);
150 static _cpp_buff
*funlike_invocation_p (cpp_reader
*, cpp_hashnode
*,
151 _cpp_buff
**, unsigned *);
152 static bool create_iso_definition (cpp_reader
*, cpp_macro
*);
154 /* #define directive parsing and handling. */
156 static cpp_token
*alloc_expansion_token (cpp_reader
*, cpp_macro
*);
157 static cpp_token
*lex_expansion_token (cpp_reader
*, cpp_macro
*);
158 static bool warn_of_redefinition (cpp_reader
*, cpp_hashnode
*,
160 static bool parse_params (cpp_reader
*, cpp_macro
*);
161 static void check_trad_stringification (cpp_reader
*, const cpp_macro
*,
163 static bool reached_end_of_context (cpp_context
*);
164 static void consume_next_token_from_context (cpp_reader
*pfile
,
167 static const cpp_token
* cpp_get_token_1 (cpp_reader
*, source_location
*);
169 static cpp_hashnode
* macro_of_context (cpp_context
*context
);
171 static bool in_macro_expansion_p (cpp_reader
*pfile
);
173 /* Statistical counter tracking the number of macros that got
175 unsigned num_expanded_macros_counter
= 0;
176 /* Statistical counter tracking the total number tokens resulting
177 from macro expansion. */
178 unsigned num_macro_tokens_counter
= 0;
180 /* Emits a warning if NODE is a macro defined in the main file that
181 has not been used. */
183 _cpp_warn_if_unused_macro (cpp_reader
*pfile
, cpp_hashnode
*node
,
184 void *v ATTRIBUTE_UNUSED
)
186 if (node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
))
188 cpp_macro
*macro
= node
->value
.macro
;
191 && MAIN_FILE_P (linemap_lookup (pfile
->line_table
, macro
->line
)))
192 cpp_warning_with_line (pfile
, CPP_W_UNUSED_MACROS
, macro
->line
, 0,
193 "macro \"%s\" is not used", NODE_NAME (node
));
199 /* Allocates and returns a CPP_STRING token, containing TEXT of length
200 LEN, after null-terminating it. TEXT must be in permanent storage. */
201 static const cpp_token
*
202 new_string_token (cpp_reader
*pfile
, unsigned char *text
, unsigned int len
)
204 cpp_token
*token
= _cpp_temp_token (pfile
);
207 token
->type
= CPP_STRING
;
208 token
->val
.str
.len
= len
;
209 token
->val
.str
.text
= text
;
214 static const char * const monthnames
[] =
216 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
217 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
220 /* Helper function for builtin_macro. Returns the text generated by
223 _cpp_builtin_macro_text (cpp_reader
*pfile
, cpp_hashnode
*node
)
225 const uchar
*result
= NULL
;
226 linenum_type number
= 1;
228 switch (node
->value
.builtin
)
231 cpp_error (pfile
, CPP_DL_ICE
, "invalid built-in macro \"%s\"",
237 cpp_buffer
*pbuffer
= cpp_get_buffer (pfile
);
238 if (pbuffer
->timestamp
== NULL
)
240 /* Initialize timestamp value of the assotiated file. */
241 struct _cpp_file
*file
= cpp_get_file (pbuffer
);
244 /* Generate __TIMESTAMP__ string, that represents
245 the date and time of the last modification
246 of the current source file. The string constant
247 looks like "Sun Sep 16 01:03:52 1973". */
248 struct tm
*tb
= NULL
;
249 struct stat
*st
= _cpp_get_file_stat (file
);
251 tb
= localtime (&st
->st_mtime
);
254 char *str
= asctime (tb
);
255 size_t len
= strlen (str
);
256 unsigned char *buf
= _cpp_unaligned_alloc (pfile
, len
+ 2);
258 strcpy ((char *) buf
+ 1, str
);
260 pbuffer
->timestamp
= buf
;
264 cpp_errno (pfile
, CPP_DL_WARNING
,
265 "could not determine file timestamp");
266 pbuffer
->timestamp
= UC
"\"??? ??? ?? ??:??:?? ????\"";
270 result
= pbuffer
->timestamp
;
280 if (node
->value
.builtin
== BT_FILE
)
281 name
= linemap_get_expansion_filename (pfile
->line_table
,
282 pfile
->line_table
->highest_line
);
285 name
= _cpp_get_file_name (pfile
->main_file
);
290 buf
= _cpp_unaligned_alloc (pfile
, len
* 2 + 3);
293 buf
= cpp_quote_string (buf
+ 1, (const unsigned char *) name
, len
);
299 case BT_INCLUDE_LEVEL
:
300 /* The line map depth counts the primary source as level 1, but
301 historically __INCLUDE_DEPTH__ has called the primary source
303 number
= pfile
->line_table
->depth
- 1;
307 /* If __LINE__ is embedded in a macro, it must expand to the
308 line of the macro's invocation, not its definition.
309 Otherwise things like assert() will not work properly. */
310 number
= linemap_get_expansion_line (pfile
->line_table
,
311 CPP_OPTION (pfile
, traditional
)
312 ? pfile
->line_table
->highest_line
313 : pfile
->cur_token
[-1].src_loc
);
316 /* __STDC__ has the value 1 under normal circumstances.
317 However, if (a) we are in a system header, (b) the option
318 stdc_0_in_system_headers is true (set by target config), and
319 (c) we are not in strictly conforming mode, then it has the
320 value 0. (b) and (c) are already checked in cpp_init_builtins. */
322 if (cpp_in_system_header (pfile
))
330 if (pfile
->date
== NULL
)
332 /* Allocate __DATE__ and __TIME__ strings from permanent
333 storage. We only do this once, and don't generate them
334 at init time, because time() and localtime() are very
335 slow on some systems. */
337 struct tm
*tb
= NULL
;
339 /* (time_t) -1 is a legitimate value for "number of seconds
340 since the Epoch", so we have to do a little dance to
341 distinguish that from a genuine error. */
344 if (tt
!= (time_t)-1 || errno
== 0)
345 tb
= localtime (&tt
);
349 pfile
->date
= _cpp_unaligned_alloc (pfile
,
350 sizeof ("\"Oct 11 1347\""));
351 sprintf ((char *) pfile
->date
, "\"%s %2d %4d\"",
352 monthnames
[tb
->tm_mon
], tb
->tm_mday
,
355 pfile
->time
= _cpp_unaligned_alloc (pfile
,
356 sizeof ("\"12:34:56\""));
357 sprintf ((char *) pfile
->time
, "\"%02d:%02d:%02d\"",
358 tb
->tm_hour
, tb
->tm_min
, tb
->tm_sec
);
362 cpp_errno (pfile
, CPP_DL_WARNING
,
363 "could not determine date and time");
365 pfile
->date
= UC
"\"??? ?? ????\"";
366 pfile
->time
= UC
"\"??:??:??\"";
370 if (node
->value
.builtin
== BT_DATE
)
371 result
= pfile
->date
;
373 result
= pfile
->time
;
377 if (CPP_OPTION (pfile
, directives_only
) && pfile
->state
.in_directive
)
378 cpp_error (pfile
, CPP_DL_ERROR
,
379 "__COUNTER__ expanded inside directive with -fdirectives-only");
380 number
= pfile
->counter
++;
386 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
387 result
= _cpp_unaligned_alloc (pfile
, 21);
388 sprintf ((char *) result
, "%u", number
);
394 /* Convert builtin macros like __FILE__ to a token and push it on the
395 context stack. Also handles _Pragma, for which a new token may not
396 be created. Returns 1 if it generates a new token context, 0 to
397 return the token to the caller. */
399 builtin_macro (cpp_reader
*pfile
, cpp_hashnode
*node
)
405 if (node
->value
.builtin
== BT_PRAGMA
)
407 /* Don't interpret _Pragma within directives. The standard is
408 not clear on this, but to me this makes most sense. */
409 if (pfile
->state
.in_directive
)
412 return _cpp_do__Pragma (pfile
);
415 buf
= _cpp_builtin_macro_text (pfile
, node
);
417 nbuf
= (char *) alloca (len
+ 1);
418 memcpy (nbuf
, buf
, len
);
421 cpp_push_buffer (pfile
, (uchar
*) nbuf
, len
, /* from_stage3 */ true);
422 _cpp_clean_line (pfile
);
424 /* Set pfile->cur_token as required by _cpp_lex_direct. */
425 pfile
->cur_token
= _cpp_temp_token (pfile
);
426 _cpp_push_token_context (pfile
, NULL
, _cpp_lex_direct (pfile
), 1);
427 if (pfile
->buffer
->cur
!= pfile
->buffer
->rlimit
)
428 cpp_error (pfile
, CPP_DL_ICE
, "invalid built-in macro \"%s\"",
430 _cpp_pop_buffer (pfile
);
435 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
436 backslashes and double quotes. DEST must be of sufficient size.
437 Returns a pointer to the end of the string. */
439 cpp_quote_string (uchar
*dest
, const uchar
*src
, unsigned int len
)
445 if (c
== '\\' || c
== '"')
457 /* Convert a token sequence ARG to a single string token according to
458 the rules of the ISO C #-operator. */
459 static const cpp_token
*
460 stringify_arg (cpp_reader
*pfile
, macro_arg
*arg
)
463 unsigned int i
, escape_it
, backslash_count
= 0;
464 const cpp_token
*source
= NULL
;
467 if (BUFF_ROOM (pfile
->u_buff
) < 3)
468 _cpp_extend_buff (pfile
, &pfile
->u_buff
, 3);
469 dest
= BUFF_FRONT (pfile
->u_buff
);
472 /* Loop, reading in the argument's tokens. */
473 for (i
= 0; i
< arg
->count
; i
++)
475 const cpp_token
*token
= arg
->first
[i
];
477 if (token
->type
== CPP_PADDING
)
480 || (!(source
->flags
& PREV_WHITE
)
481 && token
->val
.source
== NULL
))
482 source
= token
->val
.source
;
486 escape_it
= (token
->type
== CPP_STRING
|| token
->type
== CPP_CHAR
487 || token
->type
== CPP_WSTRING
|| token
->type
== CPP_WCHAR
488 || token
->type
== CPP_STRING32
|| token
->type
== CPP_CHAR32
489 || token
->type
== CPP_STRING16
|| token
->type
== CPP_CHAR16
490 || token
->type
== CPP_UTF8STRING
);
492 /* Room for each char being written in octal, initial space and
493 final quote and NUL. */
494 len
= cpp_token_len (token
);
499 if ((size_t) (BUFF_LIMIT (pfile
->u_buff
) - dest
) < len
)
501 size_t len_so_far
= dest
- BUFF_FRONT (pfile
->u_buff
);
502 _cpp_extend_buff (pfile
, &pfile
->u_buff
, len
);
503 dest
= BUFF_FRONT (pfile
->u_buff
) + len_so_far
;
506 /* Leading white space? */
507 if (dest
- 1 != BUFF_FRONT (pfile
->u_buff
))
511 if (source
->flags
& PREV_WHITE
)
518 _cpp_buff
*buff
= _cpp_get_buff (pfile
, len
);
519 unsigned char *buf
= BUFF_FRONT (buff
);
520 len
= cpp_spell_token (pfile
, token
, buf
, true) - buf
;
521 dest
= cpp_quote_string (dest
, buf
, len
);
522 _cpp_release_buff (pfile
, buff
);
525 dest
= cpp_spell_token (pfile
, token
, dest
, true);
527 if (token
->type
== CPP_OTHER
&& token
->val
.str
.text
[0] == '\\')
533 /* Ignore the final \ of invalid string literals. */
534 if (backslash_count
& 1)
536 cpp_error (pfile
, CPP_DL_WARNING
,
537 "invalid string literal, ignoring final '\\'");
541 /* Commit the memory, including NUL, and return the token. */
543 len
= dest
- BUFF_FRONT (pfile
->u_buff
);
544 BUFF_FRONT (pfile
->u_buff
) = dest
+ 1;
545 return new_string_token (pfile
, dest
- len
, len
);
548 /* Try to paste two tokens. On success, return nonzero. In any
549 case, PLHS is updated to point to the pasted token, which is
550 guaranteed to not have the PASTE_LEFT flag set. LOCATION is
551 the virtual location used for error reporting. */
553 paste_tokens (cpp_reader
*pfile
, source_location location
,
554 const cpp_token
**plhs
, const cpp_token
*rhs
)
556 unsigned char *buf
, *end
, *lhsend
;
560 len
= cpp_token_len (*plhs
) + cpp_token_len (rhs
) + 1;
561 buf
= (unsigned char *) alloca (len
);
562 end
= lhsend
= cpp_spell_token (pfile
, *plhs
, buf
, false);
564 /* Avoid comment headers, since they are still processed in stage 3.
565 It is simpler to insert a space here, rather than modifying the
566 lexer to ignore comments in some circumstances. Simply returning
567 false doesn't work, since we want to clear the PASTE_LEFT flag. */
568 if ((*plhs
)->type
== CPP_DIV
&& rhs
->type
!= CPP_EQ
)
570 /* In one obscure case we might see padding here. */
571 if (rhs
->type
!= CPP_PADDING
)
572 end
= cpp_spell_token (pfile
, rhs
, end
, false);
575 cpp_push_buffer (pfile
, buf
, end
- buf
, /* from_stage3 */ true);
576 _cpp_clean_line (pfile
);
578 /* Set pfile->cur_token as required by _cpp_lex_direct. */
579 pfile
->cur_token
= _cpp_temp_token (pfile
);
580 lhs
= _cpp_lex_direct (pfile
);
581 if (pfile
->buffer
->cur
!= pfile
->buffer
->rlimit
)
583 source_location saved_loc
= lhs
->src_loc
;
585 _cpp_pop_buffer (pfile
);
586 _cpp_backup_tokens (pfile
, 1);
589 /* We have to remove the PASTE_LEFT flag from the old lhs, but
590 we want to keep the new location. */
593 lhs
->src_loc
= saved_loc
;
594 lhs
->flags
&= ~PASTE_LEFT
;
596 /* Mandatory error for all apart from assembler. */
597 if (CPP_OPTION (pfile
, lang
) != CLK_ASM
)
598 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
599 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
600 buf
, cpp_token_as_text (pfile
, rhs
));
605 _cpp_pop_buffer (pfile
);
609 /* Handles an arbitrarily long sequence of ## operators, with initial
610 operand LHS. This implementation is left-associative,
611 non-recursive, and finishes a paste before handling succeeding
612 ones. If a paste fails, we back up to the RHS of the failing ##
613 operator before pushing the context containing the result of prior
614 successful pastes, with the effect that the RHS appears in the
615 output stream after the pasted LHS normally. */
617 paste_all_tokens (cpp_reader
*pfile
, const cpp_token
*lhs
)
619 const cpp_token
*rhs
= NULL
;
620 cpp_context
*context
= pfile
->context
;
621 source_location virt_loc
= 0;
623 /* We are expanding a macro and we must have been called on a token
624 that appears at the left hand side of a ## operator. */
625 if (macro_of_context (pfile
->context
) == NULL
626 || (!(lhs
->flags
& PASTE_LEFT
)))
629 if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
630 /* The caller must have called consume_next_token_from_context
631 right before calling us. That has incremented the pointer to
632 the current virtual location. So it now points to the location
633 of the token that comes right after *LHS. We want the
634 resulting pasted token to have the location of the current
636 virt_loc
= context
->c
.mc
->cur_virt_loc
[-1];
638 /* We are not tracking macro expansion. So the best virtual
639 location we can get here is the expansion point of the macro we
640 are currently expanding. */
641 virt_loc
= pfile
->invocation_location
;
645 /* Take the token directly from the current context. We can do
646 this, because we are in the replacement list of either an
647 object-like macro, or a function-like macro with arguments
648 inserted. In either case, the constraints to #define
649 guarantee we have at least one more token. */
650 if (context
->tokens_kind
== TOKENS_KIND_DIRECT
)
651 rhs
= FIRST (context
).token
++;
652 else if (context
->tokens_kind
== TOKENS_KIND_INDIRECT
)
653 rhs
= *FIRST (context
).ptoken
++;
654 else if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
656 /* So we are in presence of an extended token context, which
657 means that each token in this context has a virtual
658 location attached to it. So let's not forget to update
659 the pointer to the current virtual location of the
660 current token when we update the pointer to the current
663 rhs
= *FIRST (context
).ptoken
++;
664 /* context->c.mc must be non-null, as if we were not in a
665 macro context, context->tokens_kind could not be equal to
666 TOKENS_KIND_EXTENDED. */
667 context
->c
.mc
->cur_virt_loc
++;
670 if (rhs
->type
== CPP_PADDING
)
672 if (rhs
->flags
& PASTE_LEFT
)
675 if (!paste_tokens (pfile
, virt_loc
, &lhs
, rhs
))
678 while (rhs
->flags
& PASTE_LEFT
);
680 /* Put the resulting token in its own context. */
681 if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
683 source_location
*virt_locs
= NULL
;
684 _cpp_buff
*token_buf
= tokens_buff_new (pfile
, 1, &virt_locs
);
685 tokens_buff_add_token (token_buf
, virt_locs
, lhs
,
686 virt_loc
, 0, NULL
, 0);
687 push_extended_tokens_context (pfile
, context
->c
.mc
->macro_node
,
688 token_buf
, virt_locs
,
689 (const cpp_token
**)token_buf
->base
, 1);
692 _cpp_push_token_context (pfile
, NULL
, lhs
, 1);
695 /* Returns TRUE if the number of arguments ARGC supplied in an
696 invocation of the MACRO referenced by NODE is valid. An empty
697 invocation to a macro with no parameters should pass ARGC as zero.
699 Note that MACRO cannot necessarily be deduced from NODE, in case
700 NODE was redefined whilst collecting arguments. */
702 _cpp_arguments_ok (cpp_reader
*pfile
, cpp_macro
*macro
, const cpp_hashnode
*node
, unsigned int argc
)
704 if (argc
== macro
->paramc
)
707 if (argc
< macro
->paramc
)
709 /* As an extension, a rest argument is allowed to not appear in
710 the invocation at all.
711 e.g. #define debug(format, args...) something
714 This is exactly the same as if there had been an empty rest
715 argument - debug("string", ). */
717 if (argc
+ 1 == macro
->paramc
&& macro
->variadic
)
719 if (CPP_PEDANTIC (pfile
) && ! macro
->syshdr
)
720 cpp_error (pfile
, CPP_DL_PEDWARN
,
721 "ISO C99 requires rest arguments to be used");
725 cpp_error (pfile
, CPP_DL_ERROR
,
726 "macro \"%s\" requires %u arguments, but only %u given",
727 NODE_NAME (node
), macro
->paramc
, argc
);
730 cpp_error (pfile
, CPP_DL_ERROR
,
731 "macro \"%s\" passed %u arguments, but takes just %u",
732 NODE_NAME (node
), argc
, macro
->paramc
);
737 /* Reads and returns the arguments to a function-like macro
738 invocation. Assumes the opening parenthesis has been processed.
739 If there is an error, emits an appropriate diagnostic and returns
740 NULL. Each argument is terminated by a CPP_EOF token, for the
741 future benefit of expand_arg(). If there are any deferred
742 #pragma directives among macro arguments, store pointers to the
743 CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.
745 What is returned is the buffer that contains the memory allocated
746 to hold the macro arguments. NODE is the name of the macro this
747 function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is
748 set to the actual number of macro arguments allocated in the
751 collect_args (cpp_reader
*pfile
, const cpp_hashnode
*node
,
752 _cpp_buff
**pragma_buff
, unsigned *num_args
)
754 _cpp_buff
*buff
, *base_buff
;
756 macro_arg
*args
, *arg
;
757 const cpp_token
*token
;
759 source_location virt_loc
;
760 bool track_macro_expansion_p
= CPP_OPTION (pfile
, track_macro_expansion
);
761 unsigned num_args_alloced
= 0;
763 macro
= node
->value
.macro
;
765 argc
= macro
->paramc
;
769 #define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50
770 #define ARG_TOKENS_EXTENT 1000
772 buff
= _cpp_get_buff (pfile
, argc
* (DEFAULT_NUM_TOKENS_PER_MACRO_ARG
773 * sizeof (cpp_token
*)
774 + sizeof (macro_arg
)));
776 args
= (macro_arg
*) buff
->base
;
777 memset (args
, 0, argc
* sizeof (macro_arg
));
778 buff
->cur
= (unsigned char *) &args
[argc
];
779 arg
= args
, argc
= 0;
781 /* Collect the tokens making up each argument. We don't yet know
782 how many arguments have been supplied, whether too many or too
783 few. Hence the slightly bizarre usage of "argc" and "arg". */
786 unsigned int paren_depth
= 0;
787 unsigned int ntokens
= 0;
788 unsigned virt_locs_capacity
= DEFAULT_NUM_TOKENS_PER_MACRO_ARG
;
792 arg
->first
= (const cpp_token
**) buff
->cur
;
793 if (track_macro_expansion_p
)
795 virt_locs_capacity
= DEFAULT_NUM_TOKENS_PER_MACRO_ARG
;
796 arg
->virt_locs
= XNEWVEC (source_location
,
802 /* Require space for 2 new tokens (including a CPP_EOF). */
803 if ((unsigned char *) &arg
->first
[ntokens
+ 2] > buff
->limit
)
805 buff
= _cpp_append_extend_buff (pfile
, buff
,
807 * sizeof (cpp_token
*));
808 arg
->first
= (const cpp_token
**) buff
->cur
;
810 if (track_macro_expansion_p
811 && (ntokens
+ 2 > virt_locs_capacity
))
813 virt_locs_capacity
+= ARG_TOKENS_EXTENT
;
814 arg
->virt_locs
= XRESIZEVEC (source_location
,
819 token
= cpp_get_token_1 (pfile
, &virt_loc
);
821 if (token
->type
== CPP_PADDING
)
823 /* Drop leading padding. */
827 else if (token
->type
== CPP_OPEN_PAREN
)
829 else if (token
->type
== CPP_CLOSE_PAREN
)
831 if (paren_depth
-- == 0)
834 else if (token
->type
== CPP_COMMA
)
836 /* A comma does not terminate an argument within
837 parentheses or as part of a variable argument. */
839 && ! (macro
->variadic
&& argc
== macro
->paramc
))
842 else if (token
->type
== CPP_EOF
843 || (token
->type
== CPP_HASH
&& token
->flags
& BOL
))
845 else if (token
->type
== CPP_PRAGMA
)
847 cpp_token
*newtok
= _cpp_temp_token (pfile
);
849 /* CPP_PRAGMA token lives in directive_result, which will
850 be overwritten on the next directive. */
855 if (*pragma_buff
== NULL
856 || BUFF_ROOM (*pragma_buff
) < sizeof (cpp_token
*))
859 if (*pragma_buff
== NULL
)
861 = _cpp_get_buff (pfile
, 32 * sizeof (cpp_token
*));
866 = _cpp_get_buff (pfile
,
867 (BUFF_FRONT (*pragma_buff
)
868 - (*pragma_buff
)->base
) * 2);
869 (*pragma_buff
)->next
= next
;
872 *(const cpp_token
**) BUFF_FRONT (*pragma_buff
) = token
;
873 BUFF_FRONT (*pragma_buff
) += sizeof (cpp_token
*);
874 if (token
->type
== CPP_PRAGMA_EOL
)
876 token
= cpp_get_token_1 (pfile
, &virt_loc
);
878 while (token
->type
!= CPP_EOF
);
880 /* In deferred pragmas parsing_args and prevent_expansion
881 had been changed, reset it. */
882 pfile
->state
.parsing_args
= 2;
883 pfile
->state
.prevent_expansion
= 1;
885 if (token
->type
== CPP_EOF
)
890 set_arg_token (arg
, token
, virt_loc
,
891 ntokens
, MACRO_ARG_TOKEN_NORMAL
,
892 CPP_OPTION (pfile
, track_macro_expansion
));
896 /* Drop trailing padding. */
897 while (ntokens
> 0 && arg
->first
[ntokens
- 1]->type
== CPP_PADDING
)
900 arg
->count
= ntokens
;
901 set_arg_token (arg
, &pfile
->eof
, pfile
->eof
.src_loc
,
902 ntokens
, MACRO_ARG_TOKEN_NORMAL
,
903 CPP_OPTION (pfile
, track_macro_expansion
));
905 /* Terminate the argument. Excess arguments loop back and
906 overwrite the final legitimate argument, before failing. */
907 if (argc
<= macro
->paramc
)
909 buff
->cur
= (unsigned char *) &arg
->first
[ntokens
+ 1];
910 if (argc
!= macro
->paramc
)
914 while (token
->type
!= CPP_CLOSE_PAREN
&& token
->type
!= CPP_EOF
);
916 if (token
->type
== CPP_EOF
)
918 /* We still need the CPP_EOF to end directives, and to end
919 pre-expansion of a macro argument. Step back is not
920 unconditional, since we don't want to return a CPP_EOF to our
921 callers at the end of an -include-d file. */
922 if (pfile
->context
->prev
|| pfile
->state
.in_directive
)
923 _cpp_backup_tokens (pfile
, 1);
924 cpp_error (pfile
, CPP_DL_ERROR
,
925 "unterminated argument list invoking macro \"%s\"",
930 /* A single empty argument is counted as no argument. */
931 if (argc
== 1 && macro
->paramc
== 0 && args
[0].count
== 0)
933 if (_cpp_arguments_ok (pfile
, macro
, node
, argc
))
935 /* GCC has special semantics for , ## b where b is a varargs
936 parameter: we remove the comma if b was omitted entirely.
937 If b was merely an empty argument, the comma is retained.
938 If the macro takes just one (varargs) parameter, then we
939 retain the comma only if we are standards conforming.
941 If FIRST is NULL replace_args () swallows the comma. */
942 if (macro
->variadic
&& (argc
< macro
->paramc
943 || (argc
== 1 && args
[0].count
== 0
944 && !CPP_OPTION (pfile
, std
))))
945 args
[macro
->paramc
- 1].first
= NULL
;
947 *num_args
= num_args_alloced
;
952 /* An error occurred. */
953 _cpp_release_buff (pfile
, base_buff
);
957 /* Search for an opening parenthesis to the macro of NODE, in such a
958 way that, if none is found, we don't lose the information in any
959 intervening padding tokens. If we find the parenthesis, collect
960 the arguments and return the buffer containing them. PRAGMA_BUFF
961 argument is the same as in collect_args. If NUM_ARGS is non-NULL,
962 *NUM_ARGS is set to the number of arguments contained in the
965 funlike_invocation_p (cpp_reader
*pfile
, cpp_hashnode
*node
,
966 _cpp_buff
**pragma_buff
, unsigned *num_args
)
968 const cpp_token
*token
, *padding
= NULL
;
972 token
= cpp_get_token (pfile
);
973 if (token
->type
!= CPP_PADDING
)
976 || (!(padding
->flags
& PREV_WHITE
) && token
->val
.source
== NULL
))
980 if (token
->type
== CPP_OPEN_PAREN
)
982 pfile
->state
.parsing_args
= 2;
983 return collect_args (pfile
, node
, pragma_buff
, num_args
);
986 /* CPP_EOF can be the end of macro arguments, or the end of the
987 file. We mustn't back up over the latter. Ugh. */
988 if (token
->type
!= CPP_EOF
|| token
== &pfile
->eof
)
990 /* Back up. We may have skipped padding, in which case backing
991 up more than one token when expanding macros is in general
992 too difficult. We re-insert it in its own context. */
993 _cpp_backup_tokens (pfile
, 1);
995 _cpp_push_token_context (pfile
, NULL
, padding
, 1);
1001 /* Return the real number of tokens in the expansion of MACRO. */
1002 static inline unsigned int
1003 macro_real_token_count (const cpp_macro
*macro
)
1006 if (__builtin_expect (!macro
->extra_tokens
, true))
1007 return macro
->count
;
1008 for (i
= 0; i
< macro
->count
; i
++)
1009 if (macro
->exp
.tokens
[i
].type
== CPP_PASTE
)
1014 /* Push the context of a macro with hash entry NODE onto the context
1015 stack. If we can successfully expand the macro, we push a context
1016 containing its yet-to-be-rescanned replacement list and return one.
1017 If there were additionally any unexpanded deferred #pragma
1018 directives among macro arguments, push another context containing
1019 the pragma tokens before the yet-to-be-rescanned replacement list
1020 and return two. Otherwise, we don't push a context and return
1021 zero. LOCATION is the location of the expansion point of the
1024 enter_macro_context (cpp_reader
*pfile
, cpp_hashnode
*node
,
1025 const cpp_token
*result
, source_location location
)
1027 /* The presence of a macro invalidates a file's controlling macro. */
1028 pfile
->mi_valid
= false;
1030 pfile
->state
.angled_headers
= false;
1032 /* From here to when we push the context for the macro later down
1033 this function, we need to flag the fact that we are about to
1034 expand a macro. This is useful when -ftrack-macro-expansion is
1035 turned off. In that case, we need to record the location of the
1036 expansion point of the top-most macro we are about to to expand,
1037 into pfile->invocation_location. But we must not record any such
1038 location once the process of expanding the macro starts; that is,
1039 we must not do that recording between now and later down this
1040 function where set this flag to FALSE. */
1041 pfile
->about_to_expand_macro_p
= true;
1043 if ((node
->flags
& NODE_BUILTIN
) && !(node
->flags
& NODE_USED
))
1045 node
->flags
|= NODE_USED
;
1046 if ((!pfile
->cb
.user_builtin_macro
1047 || !pfile
->cb
.user_builtin_macro (pfile
, node
))
1048 && pfile
->cb
.used_define
)
1049 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
1052 /* Handle standard macros. */
1053 if (! (node
->flags
& NODE_BUILTIN
))
1055 cpp_macro
*macro
= node
->value
.macro
;
1056 _cpp_buff
*pragma_buff
= NULL
;
1058 if (macro
->fun_like
)
1061 unsigned num_args
= 0;
1063 pfile
->state
.prevent_expansion
++;
1064 pfile
->keep_tokens
++;
1065 pfile
->state
.parsing_args
= 1;
1066 buff
= funlike_invocation_p (pfile
, node
, &pragma_buff
,
1068 pfile
->state
.parsing_args
= 0;
1069 pfile
->keep_tokens
--;
1070 pfile
->state
.prevent_expansion
--;
1074 if (CPP_WTRADITIONAL (pfile
) && ! node
->value
.macro
->syshdr
)
1075 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
1076 "function-like macro \"%s\" must be used with arguments in traditional C",
1080 _cpp_release_buff (pfile
, pragma_buff
);
1082 pfile
->about_to_expand_macro_p
= false;
1086 if (macro
->paramc
> 0)
1087 replace_args (pfile
, node
, macro
,
1088 (macro_arg
*) buff
->base
,
1090 /* Free the memory used by the arguments of this
1091 function-like macro. This memory has been allocated by
1092 funlike_invocation_p and by replace_args. */
1093 delete_macro_args (buff
, num_args
);
1096 /* Disable the macro within its expansion. */
1097 node
->flags
|= NODE_DISABLED
;
1099 if (!(node
->flags
& NODE_USED
))
1101 node
->flags
|= NODE_USED
;
1102 if (pfile
->cb
.used_define
)
1103 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
1107 pfile
->cb
.used (pfile
, location
, node
);
1111 if (macro
->paramc
== 0)
1113 if (CPP_OPTION (pfile
, track_macro_expansion
))
1115 unsigned int i
, count
= macro
->count
;
1116 const cpp_token
*src
= macro
->exp
.tokens
;
1117 const struct line_map
*map
;
1118 source_location
*virt_locs
= NULL
;
1119 _cpp_buff
*macro_tokens
=
1120 tokens_buff_new (pfile
, count
, &virt_locs
);
1122 /* Create a macro map to record the locations of the
1123 tokens that are involved in the expansion. LOCATION
1124 is the location of the macro expansion point. */
1125 map
= linemap_enter_macro (pfile
->line_table
,
1126 node
, location
, count
);
1127 for (i
= 0; i
< count
; ++i
)
1129 tokens_buff_add_token (macro_tokens
, virt_locs
,
1131 src
->src_loc
, map
, i
);
1134 push_extended_tokens_context (pfile
, node
,
1137 (const cpp_token
**)
1140 num_macro_tokens_counter
+= count
;
1144 unsigned tokens_count
= macro_real_token_count (macro
);
1145 _cpp_push_token_context (pfile
, node
, macro
->exp
.tokens
,
1147 num_macro_tokens_counter
+= tokens_count
;
1153 if (!pfile
->state
.in_directive
)
1154 _cpp_push_token_context (pfile
, NULL
,
1155 padding_token (pfile
, result
), 1);
1158 unsigned tokens_count
;
1159 _cpp_buff
*tail
= pragma_buff
->next
;
1160 pragma_buff
->next
= NULL
;
1161 tokens_count
= ((const cpp_token
**) BUFF_FRONT (pragma_buff
)
1162 - (const cpp_token
**) pragma_buff
->base
);
1163 push_ptoken_context (pfile
, NULL
, pragma_buff
,
1164 (const cpp_token
**) pragma_buff
->base
,
1167 if (!CPP_OPTION (pfile
, track_macro_expansion
))
1168 num_macro_tokens_counter
+= tokens_count
;
1171 while (pragma_buff
!= NULL
);
1172 pfile
->about_to_expand_macro_p
= false;
1176 pfile
->about_to_expand_macro_p
= false;
1180 pfile
->about_to_expand_macro_p
= false;
1181 /* Handle built-in macros and the _Pragma operator. */
1182 return builtin_macro (pfile
, node
);
1185 /* De-allocate the memory used by BUFF which is an array of instances
1186 of macro_arg. NUM_ARGS is the number of instances of macro_arg
1189 delete_macro_args (_cpp_buff
*buff
, unsigned num_args
)
1191 macro_arg
*macro_args
;
1197 macro_args
= (macro_arg
*) buff
->base
;
1199 /* Walk instances of macro_arg to free their expanded tokens as well
1200 as their macro_arg::virt_locs members. */
1201 for (i
= 0; i
< num_args
; ++i
)
1203 if (macro_args
[i
].expanded
)
1205 free (macro_args
[i
].expanded
);
1206 macro_args
[i
].expanded
= NULL
;
1208 if (macro_args
[i
].virt_locs
)
1210 free (macro_args
[i
].virt_locs
);
1211 macro_args
[i
].virt_locs
= NULL
;
1213 if (macro_args
[i
].expanded_virt_locs
)
1215 free (macro_args
[i
].expanded_virt_locs
);
1216 macro_args
[i
].expanded_virt_locs
= NULL
;
1219 _cpp_free_buff (buff
);
1222 /* Set the INDEXth token of the macro argument ARG. TOKEN is the token
1223 to set, LOCATION is its virtual location. "Virtual" location means
1224 the location that encodes loci across macro expansion. Otherwise
1225 it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the
1226 argument ARG is supposed to contain. Note that ARG must be
1227 tailored so that it has enough room to contain INDEX + 1 numbers of
1228 tokens, at least. */
1230 set_arg_token (macro_arg
*arg
, const cpp_token
*token
,
1231 source_location location
, size_t index
,
1232 enum macro_arg_token_kind kind
,
1233 bool track_macro_exp_p
)
1235 const cpp_token
**token_ptr
;
1236 source_location
*loc
= NULL
;
1239 arg_token_ptr_at (arg
, index
, kind
,
1240 track_macro_exp_p
? &loc
: NULL
);
1245 #ifdef ENABLE_CHECKING
1246 if (kind
== MACRO_ARG_TOKEN_STRINGIFIED
1247 || !track_macro_exp_p
)
1248 /* We can't set the location of a stringified argument
1249 token and we can't set any location if we aren't tracking
1250 macro expansion locations. */
1257 /* Get the pointer to the location of the argument token of the
1258 function-like macro argument ARG. This function must be called
1259 only when we -ftrack-macro-expansion is on. */
1260 static const source_location
*
1261 get_arg_token_location (const macro_arg
*arg
,
1262 enum macro_arg_token_kind kind
)
1264 const source_location
*loc
= NULL
;
1265 const cpp_token
**token_ptr
=
1266 arg_token_ptr_at (arg
, 0, kind
, (source_location
**) &loc
);
1268 if (token_ptr
== NULL
)
1274 /* Return the pointer to the INDEXth token of the macro argument ARG.
1275 KIND specifies the kind of token the macro argument ARG contains.
1276 If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
1277 of the virtual location of the returned token if the
1278 -ftrack-macro-expansion flag is on; otherwise, it's set to the
1279 spelling location of the returned token. */
1280 static const cpp_token
**
1281 arg_token_ptr_at (const macro_arg
*arg
, size_t index
,
1282 enum macro_arg_token_kind kind
,
1283 source_location
**virt_location
)
1285 const cpp_token
**tokens_ptr
= NULL
;
1289 case MACRO_ARG_TOKEN_NORMAL
:
1290 tokens_ptr
= arg
->first
;
1292 case MACRO_ARG_TOKEN_STRINGIFIED
:
1293 tokens_ptr
= (const cpp_token
**) &arg
->stringified
;
1295 case MACRO_ARG_TOKEN_EXPANDED
:
1296 tokens_ptr
= arg
->expanded
;
1300 if (tokens_ptr
== NULL
)
1301 /* This can happen for e.g, an empty token argument to a
1302 funtion-like macro. */
1307 if (kind
== MACRO_ARG_TOKEN_NORMAL
)
1308 *virt_location
= &arg
->virt_locs
[index
];
1309 else if (kind
== MACRO_ARG_TOKEN_EXPANDED
)
1310 *virt_location
= &arg
->expanded_virt_locs
[index
];
1311 else if (kind
== MACRO_ARG_TOKEN_STRINGIFIED
)
1313 (source_location
*) &tokens_ptr
[index
]->src_loc
;
1315 return &tokens_ptr
[index
];
1318 /* Initialize an iterator so that it iterates over the tokens of a
1319 function-like macro argument. KIND is the kind of tokens we want
1320 ITER to iterate over. TOKEN_PTR points the first token ITER will
1323 macro_arg_token_iter_init (macro_arg_token_iter
*iter
,
1324 bool track_macro_exp_p
,
1325 enum macro_arg_token_kind kind
,
1326 const macro_arg
*arg
,
1327 const cpp_token
**token_ptr
)
1329 iter
->track_macro_exp_p
= track_macro_exp_p
;
1331 iter
->token_ptr
= token_ptr
;
1332 /* Unconditionally initialize this so that the compiler doesn't warn
1333 about iter->location_ptr being possibly uninitialized later after
1334 this code has been inlined somewhere. */
1335 iter
->location_ptr
= NULL
;
1336 if (track_macro_exp_p
)
1337 iter
->location_ptr
= get_arg_token_location (arg
, kind
);
1338 #ifdef ENABLE_CHECKING
1339 iter
->num_forwards
= 0;
1340 if (track_macro_exp_p
1341 && token_ptr
!= NULL
1342 && iter
->location_ptr
== NULL
)
1347 /* Move the iterator one token forward. Note that if IT was
1348 initialized on an argument that has a stringified token, moving it
1349 forward doesn't make sense as a stringified token is essentially one
1352 macro_arg_token_iter_forward (macro_arg_token_iter
*it
)
1356 case MACRO_ARG_TOKEN_NORMAL
:
1357 case MACRO_ARG_TOKEN_EXPANDED
:
1359 if (it
->track_macro_exp_p
)
1362 case MACRO_ARG_TOKEN_STRINGIFIED
:
1363 #ifdef ENABLE_CHECKING
1364 if (it
->num_forwards
> 0)
1370 #ifdef ENABLE_CHECKING
1375 /* Return the token pointed to by the iterator. */
1376 static const cpp_token
*
1377 macro_arg_token_iter_get_token (const macro_arg_token_iter
*it
)
1379 #ifdef ENABLE_CHECKING
1380 if (it
->kind
== MACRO_ARG_TOKEN_STRINGIFIED
1381 && it
->num_forwards
> 0)
1384 if (it
->token_ptr
== NULL
)
1386 return *it
->token_ptr
;
1389 /* Return the location of the token pointed to by the iterator.*/
1390 static source_location
1391 macro_arg_token_iter_get_location (const macro_arg_token_iter
*it
)
1393 #ifdef ENABLE_CHECKING
1394 if (it
->kind
== MACRO_ARG_TOKEN_STRINGIFIED
1395 && it
->num_forwards
> 0)
1398 if (it
->track_macro_exp_p
)
1399 return *it
->location_ptr
;
1401 return (*it
->token_ptr
)->src_loc
;
1404 /* Return the index of a token [resulting from macro expansion] inside
1405 the total list of tokens resulting from a given macro
1406 expansion. The index can be different depending on whether if we
1407 want each tokens resulting from function-like macro arguments
1408 expansion to have a different location or not.
1410 E.g, consider this function-like macro:
1414 Then consider us "calling" it (and thus expanding it) like:
1418 It will be expanded into:
1422 Let's consider the case of the token '4'.
1424 Its index can be 2 (it's the third token of the set of tokens
1425 resulting from the expansion) or it can be 0 if we consider that
1426 all tokens resulting from the expansion of the argument "1+2" have
1427 the same index, which is 0. In this later case, the index of token
1428 '-' would then be 1 and the index of token '3' would be 2.
1430 The later case is useful to use less memory e.g, for the case of
1431 the user using the option -ftrack-macro-expansion=1.
1433 ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
1434 are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro
1435 parameter (inside the macro replacement list) that corresponds to
1436 the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
1439 If we refer to the example above, for the '4' argument token,
1440 ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
1441 would be set to the token 'x', in the replacement list "x - 3" of
1444 This is a subroutine of replace_args. */
1445 inline static unsigned
1446 expanded_token_index (cpp_reader
*pfile
, cpp_macro
*macro
,
1447 const cpp_token
*cur_replacement_token
,
1448 unsigned absolute_token_index
)
1450 if (CPP_OPTION (pfile
, track_macro_expansion
) > 1)
1451 return absolute_token_index
;
1452 return cur_replacement_token
- macro
->exp
.tokens
;
1455 /* Replace the parameters in a function-like macro of NODE with the
1456 actual ARGS, and place the result in a newly pushed token context.
1457 Expand each argument before replacing, unless it is operated upon
1458 by the # or ## operators. EXPANSION_POINT_LOC is the location of
1459 the expansion point of the macro. E.g, the location of the
1460 function-like macro invocation. */
1462 replace_args (cpp_reader
*pfile
, cpp_hashnode
*node
, cpp_macro
*macro
,
1463 macro_arg
*args
, source_location expansion_point_loc
)
1465 unsigned int i
, total
;
1466 const cpp_token
*src
, *limit
;
1467 const cpp_token
**first
= NULL
;
1469 _cpp_buff
*buff
= NULL
;
1470 source_location
*virt_locs
= NULL
;
1471 unsigned int exp_count
;
1472 const struct line_map
*map
= NULL
;
1473 int track_macro_exp
;
1475 /* First, fully macro-expand arguments, calculating the number of
1476 tokens in the final expansion as we go. The ordering of the if
1477 statements below is subtle; we must handle stringification before
1480 /* EXP_COUNT is the number of tokens in the macro replacement
1481 list. TOTAL is the number of tokens /after/ macro parameters
1482 have been replaced by their arguments. */
1483 exp_count
= macro_real_token_count (macro
);
1485 limit
= macro
->exp
.tokens
+ exp_count
;
1487 for (src
= macro
->exp
.tokens
; src
< limit
; src
++)
1488 if (src
->type
== CPP_MACRO_ARG
)
1490 /* Leading and trailing padding tokens. */
1492 /* Account for leading and padding tokens in exp_count too.
1493 This is going to be important later down this function,
1494 when we want to handle the case of (track_macro_exp <
1498 /* We have an argument. If it is not being stringified or
1499 pasted it is macro-replaced before insertion. */
1500 arg
= &args
[src
->val
.macro_arg
.arg_no
- 1];
1502 if (src
->flags
& STRINGIFY_ARG
)
1504 if (!arg
->stringified
)
1505 arg
->stringified
= stringify_arg (pfile
, arg
);
1507 else if ((src
->flags
& PASTE_LEFT
)
1508 || (src
> macro
->exp
.tokens
&& (src
[-1].flags
& PASTE_LEFT
)))
1509 total
+= arg
->count
- 1;
1513 expand_arg (pfile
, arg
);
1514 total
+= arg
->expanded_count
- 1;
1518 /* When the compiler is called with the -ftrack-macro-expansion
1519 flag, we need to keep track of the location of each token that
1520 results from macro expansion.
1522 A token resulting from macro expansion is not a new token. It is
1523 simply the same token as the token coming from the macro
1524 definition. The new things that are allocated are the buffer
1525 that holds the tokens resulting from macro expansion and a new
1526 location that records many things like the locus of the expansion
1527 point as well as the original locus inside the definition of the
1528 macro. This location is called a virtual location.
1530 So the buffer BUFF holds a set of cpp_token*, and the buffer
1531 VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
1533 Both of these two buffers are going to be hung off of the macro
1534 context, when the latter is pushed. The memory allocated to
1535 store the tokens and their locations is going to be freed once
1536 the context of macro expansion is popped.
1538 As far as tokens are concerned, the memory overhead of
1539 -ftrack-macro-expansion is proportional to the number of
1540 macros that get expanded multiplied by sizeof (source_location).
1541 The good news is that extra memory gets freed when the macro
1542 context is freed, i.e shortly after the macro got expanded. */
1544 /* Is the -ftrack-macro-expansion flag in effect? */
1545 track_macro_exp
= CPP_OPTION (pfile
, track_macro_expansion
);
1547 /* Now allocate memory space for tokens and locations resulting from
1548 the macro expansion, copy the tokens and replace the arguments.
1549 This memory must be freed when the context of the macro MACRO is
1551 buff
= tokens_buff_new (pfile
, total
, track_macro_exp
? &virt_locs
: NULL
);
1553 first
= (const cpp_token
**) buff
->base
;
1555 /* Create a macro map to record the locations of the tokens that are
1556 involved in the expansion. Note that the expansion point is set
1557 to the location of the closing parenthesis. Otherwise, the
1558 subsequent map created for the first token that comes after the
1559 macro map might have a wrong line number. That would lead to
1560 tokens with wrong line numbers after the macro expansion. This
1561 adds up to the memory overhead of the -ftrack-macro-expansion
1562 flag; for every macro that is expanded, a "macro map" is
1564 if (track_macro_exp
)
1566 int num_macro_tokens
= total
;
1567 if (track_macro_exp
< 2)
1568 /* Then the number of macro tokens won't take in account the
1569 fact that function-like macro arguments can expand to
1570 multiple tokens. This is to save memory at the expense of
1573 Suppose we have #define SQARE(A) A * A
1575 And then we do SQARE(2+3)
1577 Then the tokens 2, +, 3, will have the same location,
1578 saying they come from the expansion of the argument A. */
1579 num_macro_tokens
= exp_count
;
1580 map
= linemap_enter_macro (pfile
->line_table
, node
,
1581 expansion_point_loc
,
1585 for (src
= macro
->exp
.tokens
; src
< limit
; src
++)
1587 unsigned int arg_tokens_count
;
1588 macro_arg_token_iter from
;
1589 const cpp_token
**paste_flag
= NULL
;
1590 const cpp_token
**tmp_token_ptr
;
1592 if (src
->type
!= CPP_MACRO_ARG
)
1594 /* Allocate a virtual location for token SRC, and add that
1595 token and its virtual location into the buffers BUFF and
1597 unsigned index
= expanded_token_index (pfile
, macro
, src
, i
);
1598 tokens_buff_add_token (buff
, virt_locs
, src
,
1599 src
->src_loc
, src
->src_loc
,
1606 arg
= &args
[src
->val
.macro_arg
.arg_no
- 1];
1607 /* SRC is a macro parameter that we need to replace with its
1608 corresponding argument. So at some point we'll need to
1609 iterate over the tokens of the macro argument and copy them
1610 into the "place" now holding the correspondig macro
1611 parameter. We are going to use the iterator type
1612 macro_argo_token_iter to handle that iterating. The 'if'
1613 below is to initialize the iterator depending on the type of
1614 tokens the macro argument has. It also does some adjustment
1615 related to padding tokens and some pasting corner cases. */
1616 if (src
->flags
& STRINGIFY_ARG
)
1618 arg_tokens_count
= 1;
1619 macro_arg_token_iter_init (&from
,
1621 track_macro_expansion
),
1622 MACRO_ARG_TOKEN_STRINGIFIED
,
1623 arg
, &arg
->stringified
);
1625 else if (src
->flags
& PASTE_LEFT
)
1627 arg_tokens_count
= arg
->count
;
1628 macro_arg_token_iter_init (&from
,
1630 track_macro_expansion
),
1631 MACRO_ARG_TOKEN_NORMAL
,
1634 else if (src
!= macro
->exp
.tokens
&& (src
[-1].flags
& PASTE_LEFT
))
1637 arg_tokens_count
= arg
->count
;
1638 macro_arg_token_iter_init (&from
,
1640 track_macro_expansion
),
1641 MACRO_ARG_TOKEN_NORMAL
,
1644 num_toks
= tokens_buff_count (buff
);
1648 /* So the current parameter token is pasted to the previous
1649 token in the replacement list. Let's look at what
1650 we have as previous and current arguments. */
1652 /* This is the previous argument's token ... */
1653 tmp_token_ptr
= tokens_buff_last_token_ptr (buff
);
1655 if ((*tmp_token_ptr
)->type
== CPP_COMMA
1657 && src
->val
.macro_arg
.arg_no
== macro
->paramc
)
1659 /* ... which is a comma; and the current parameter
1660 is the last parameter of a variadic function-like
1661 macro. If the argument to the current last
1662 parameter is NULL, then swallow the comma,
1663 otherwise drop the paste flag. */
1664 if (macro_arg_token_iter_get_token (&from
) == NULL
)
1665 tokens_buff_remove_last_token (buff
);
1667 paste_flag
= tmp_token_ptr
;
1669 /* Remove the paste flag if the RHS is a placemarker. */
1670 else if (arg_tokens_count
== 0)
1671 paste_flag
= tmp_token_ptr
;
1676 arg_tokens_count
= arg
->expanded_count
;
1677 macro_arg_token_iter_init (&from
,
1679 track_macro_expansion
),
1680 MACRO_ARG_TOKEN_EXPANDED
,
1681 arg
, arg
->expanded
);
1684 /* Padding on the left of an argument (unless RHS of ##). */
1685 if ((!pfile
->state
.in_directive
|| pfile
->state
.directive_wants_padding
)
1686 && src
!= macro
->exp
.tokens
&& !(src
[-1].flags
& PASTE_LEFT
))
1688 const cpp_token
*t
= padding_token (pfile
, src
);
1689 unsigned index
= expanded_token_index (pfile
, macro
, src
, i
);
1690 /* Allocate a virtual location for the padding token and
1691 append the token and its location to BUFF and
1693 tokens_buff_add_token (buff
, virt_locs
, t
,
1694 t
->src_loc
, t
->src_loc
,
1698 if (arg_tokens_count
)
1700 /* So now we've got the number of tokens that make up the
1701 argument that is going to replace the current parameter
1702 in the macro's replacement list. */
1704 for (j
= 0; j
< arg_tokens_count
; ++j
)
1706 /* So if track_macro_exp is < 2, the user wants to
1707 save extra memory while tracking macro expansion
1708 locations. So in that case here is what we do:
1710 Suppose we have #define SQARE(A) A * A
1712 And then we do SQARE(2+3)
1714 Then the tokens 2, +, 3, will have the same location,
1715 saying they come from the expansion of the argument
1718 So that means we are going to ignore the COUNT tokens
1719 resulting from the expansion of the current macro
1720 arugment. In other words all the ARG_TOKENS_COUNT tokens
1721 resulting from the expansion of the macro argument will
1722 have the index I. Normally, each of those token should
1724 unsigned token_index
= i
;
1726 if (track_macro_exp
> 1)
1729 index
= expanded_token_index (pfile
, macro
, src
, token_index
);
1730 tokens_buff_add_token (buff
, virt_locs
,
1731 macro_arg_token_iter_get_token (&from
),
1732 macro_arg_token_iter_get_location (&from
),
1733 src
->src_loc
, map
, index
);
1734 macro_arg_token_iter_forward (&from
);
1737 /* With a non-empty argument on the LHS of ##, the last
1738 token should be flagged PASTE_LEFT. */
1739 if (src
->flags
& PASTE_LEFT
)
1741 (const cpp_token
**) tokens_buff_last_token_ptr (buff
);
1743 else if (CPP_PEDANTIC (pfile
) && ! macro
->syshdr
1744 && ! CPP_OPTION (pfile
, c99
)
1745 && ! cpp_in_system_header (pfile
))
1747 cpp_error (pfile
, CPP_DL_PEDWARN
,
1748 "invoking macro %s argument %d: "
1749 "empty macro arguments are undefined"
1750 " in ISO C90 and ISO C++98",
1752 src
->val
.macro_arg
.arg_no
);
1755 /* Avoid paste on RHS (even case count == 0). */
1756 if (!pfile
->state
.in_directive
&& !(src
->flags
& PASTE_LEFT
))
1758 const cpp_token
*t
= &pfile
->avoid_paste
;
1759 tokens_buff_add_token (buff
, virt_locs
,
1760 t
, t
->src_loc
, t
->src_loc
,
1764 /* Add a new paste flag, or remove an unwanted one. */
1767 cpp_token
*token
= _cpp_temp_token (pfile
);
1768 token
->type
= (*paste_flag
)->type
;
1769 token
->val
= (*paste_flag
)->val
;
1770 if (src
->flags
& PASTE_LEFT
)
1771 token
->flags
= (*paste_flag
)->flags
| PASTE_LEFT
;
1773 token
->flags
= (*paste_flag
)->flags
& ~PASTE_LEFT
;
1774 *paste_flag
= token
;
1777 i
+= arg_tokens_count
;
1780 if (track_macro_exp
)
1781 push_extended_tokens_context (pfile
, node
, buff
, virt_locs
, first
,
1782 tokens_buff_count (buff
));
1784 push_ptoken_context (pfile
, node
, buff
, first
,
1785 tokens_buff_count (buff
));
1787 num_macro_tokens_counter
+= tokens_buff_count (buff
);
1790 /* Return a special padding token, with padding inherited from SOURCE. */
1791 static const cpp_token
*
1792 padding_token (cpp_reader
*pfile
, const cpp_token
*source
)
1794 cpp_token
*result
= _cpp_temp_token (pfile
);
1796 result
->type
= CPP_PADDING
;
1798 /* Data in GCed data structures cannot be made const so far, so we
1799 need a cast here. */
1800 result
->val
.source
= (cpp_token
*) source
;
1805 /* Get a new uninitialized context. Create a new one if we cannot
1806 re-use an old one. */
1807 static cpp_context
*
1808 next_context (cpp_reader
*pfile
)
1810 cpp_context
*result
= pfile
->context
->next
;
1814 result
= XNEW (cpp_context
);
1815 memset (result
, 0, sizeof (cpp_context
));
1816 result
->prev
= pfile
->context
;
1818 pfile
->context
->next
= result
;
1821 pfile
->context
= result
;
1825 /* Push a list of pointers to tokens. */
1827 push_ptoken_context (cpp_reader
*pfile
, cpp_hashnode
*macro
, _cpp_buff
*buff
,
1828 const cpp_token
**first
, unsigned int count
)
1830 cpp_context
*context
= next_context (pfile
);
1832 context
->tokens_kind
= TOKENS_KIND_INDIRECT
;
1833 context
->c
.macro
= macro
;
1834 context
->buff
= buff
;
1835 FIRST (context
).ptoken
= first
;
1836 LAST (context
).ptoken
= first
+ count
;
1839 /* Push a list of tokens.
1841 A NULL macro means that we should continue the current macro
1842 expansion, in essence. That means that if we are currently in a
1843 macro expansion context, we'll make the new pfile->context refer to
1844 the current macro. */
1846 _cpp_push_token_context (cpp_reader
*pfile
, cpp_hashnode
*macro
,
1847 const cpp_token
*first
, unsigned int count
)
1849 cpp_context
*context
;
1852 macro
= macro_of_context (pfile
->context
);
1854 context
= next_context (pfile
);
1855 context
->tokens_kind
= TOKENS_KIND_DIRECT
;
1856 context
->c
.macro
= macro
;
1857 context
->buff
= NULL
;
1858 FIRST (context
).token
= first
;
1859 LAST (context
).token
= first
+ count
;
1862 /* Build a context containing a list of tokens as well as their
1863 virtual locations and push it. TOKENS_BUFF is the buffer that
1864 contains the tokens pointed to by FIRST. If TOKENS_BUFF is
1865 non-NULL, it means that the context owns it, meaning that
1866 _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
1867 contains the virtual locations.
1869 A NULL macro means that we should continue the current macro
1870 expansion, in essence. That means that if we are currently in a
1871 macro expansion context, we'll make the new pfile->context refer to
1872 the current macro. */
1874 push_extended_tokens_context (cpp_reader
*pfile
,
1875 cpp_hashnode
*macro
,
1876 _cpp_buff
*token_buff
,
1877 source_location
*virt_locs
,
1878 const cpp_token
**first
,
1881 cpp_context
*context
;
1885 macro
= macro_of_context (pfile
->context
);
1887 context
= next_context (pfile
);
1888 context
->tokens_kind
= TOKENS_KIND_EXTENDED
;
1889 context
->buff
= token_buff
;
1891 m
= XNEW (macro_context
);
1892 m
->macro_node
= macro
;
1893 m
->virt_locs
= virt_locs
;
1894 m
->cur_virt_loc
= virt_locs
;
1896 FIRST (context
).ptoken
= first
;
1897 LAST (context
).ptoken
= first
+ count
;
1900 /* Push a traditional macro's replacement text. */
1902 _cpp_push_text_context (cpp_reader
*pfile
, cpp_hashnode
*macro
,
1903 const uchar
*start
, size_t len
)
1905 cpp_context
*context
= next_context (pfile
);
1907 context
->tokens_kind
= TOKENS_KIND_DIRECT
;
1908 context
->c
.macro
= macro
;
1909 context
->buff
= NULL
;
1910 CUR (context
) = start
;
1911 RLIMIT (context
) = start
+ len
;
1912 macro
->flags
|= NODE_DISABLED
;
1915 /* Creates a buffer that holds tokens a.k.a "token buffer", usually
1916 for the purpose of storing them on a cpp_context. If VIRT_LOCS is
1917 non-null (which means that -ftrack-macro-expansion is on),
1918 *VIRT_LOCS is set to a newly allocated buffer that is supposed to
1919 hold the virtual locations of the tokens resulting from macro
1922 tokens_buff_new (cpp_reader
*pfile
, size_t len
,
1923 source_location
**virt_locs
)
1925 size_t tokens_size
= len
* sizeof (cpp_token
*);
1926 size_t locs_size
= len
* sizeof (source_location
);
1928 if (virt_locs
!= NULL
)
1929 *virt_locs
= XNEWVEC (source_location
, locs_size
);
1930 return _cpp_get_buff (pfile
, tokens_size
);
1933 /* Returns the number of tokens contained in a token buffer. The
1934 buffer holds a set of cpp_token*. */
1936 tokens_buff_count (_cpp_buff
*buff
)
1938 return (BUFF_FRONT (buff
) - buff
->base
) / sizeof (cpp_token
*);
1941 /* Return a pointer to the last token contained in the token buffer
1943 static const cpp_token
**
1944 tokens_buff_last_token_ptr (_cpp_buff
*buff
)
1946 return &((const cpp_token
**) BUFF_FRONT (buff
))[-1];
1949 /* Remove the last token contained in the token buffer TOKENS_BUFF.
1950 If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer
1951 containing the virtual locations of the tokens in TOKENS_BUFF; in
1952 which case the function updates that buffer as well. */
1954 tokens_buff_remove_last_token (_cpp_buff
*tokens_buff
)
1957 if (BUFF_FRONT (tokens_buff
) > tokens_buff
->base
)
1958 BUFF_FRONT (tokens_buff
) =
1959 (unsigned char *) &((cpp_token
**) BUFF_FRONT (tokens_buff
))[-1];
1962 /* Insert a token into the token buffer at the position pointed to by
1963 DEST. Note that the buffer is not enlarged so the previous token
1964 that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null,
1965 means -ftrack-macro-expansion is effect; it then points to where to
1966 insert the virtual location of TOKEN. TOKEN is the token to
1967 insert. VIRT_LOC is the virtual location of the token, i.e, the
1968 location possibly encoding its locus across macro expansion. If
1969 TOKEN is an argument of a function-like macro (inside a macro
1970 replacement list), PARM_DEF_LOC is the spelling location of the
1971 macro parameter that TOKEN is replacing, in the replacement list of
1972 the macro. If TOKEN is not an argument of a function-like macro or
1973 if it doesn't come from a macro expansion, then VIRT_LOC can just
1974 be set to the same value as PARM_DEF_LOC. If MAP is non null, it
1975 means TOKEN comes from a macro expansion and MAP is the macro map
1976 associated to the macro. MACRO_TOKEN_INDEX points to the index of
1977 the token in the macro map; it is not considered if MAP is NULL.
1979 Upon successful completion this function returns the a pointer to
1980 the position of the token coming right after the insertion
1982 static inline const cpp_token
**
1983 tokens_buff_put_token_to (const cpp_token
**dest
,
1984 source_location
*virt_loc_dest
,
1985 const cpp_token
*token
,
1986 source_location virt_loc
,
1987 source_location parm_def_loc
,
1988 const struct line_map
*map
,
1989 unsigned int macro_token_index
)
1991 source_location macro_loc
= virt_loc
;
1992 const cpp_token
**result
;
1996 /* -ftrack-macro-expansion is on. */
1998 macro_loc
= linemap_add_macro_token (map
, macro_token_index
,
1999 virt_loc
, parm_def_loc
);
2000 *virt_loc_dest
= macro_loc
;
2008 /* Adds a token at the end of the tokens contained in BUFFER. Note
2009 that this function doesn't enlarge BUFFER when the number of tokens
2010 reaches BUFFER's size; it aborts in that situation.
2012 TOKEN is the token to append. VIRT_LOC is the virtual location of
2013 the token, i.e, the location possibly encoding its locus across
2014 macro expansion. If TOKEN is an argument of a function-like macro
2015 (inside a macro replacement list), PARM_DEF_LOC is the location of
2016 the macro parameter that TOKEN is replacing. If TOKEN doesn't come
2017 from a macro expansion, then VIRT_LOC can just be set to the same
2018 value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes
2019 from a macro expansion and MAP is the macro map associated to the
2020 macro. MACRO_TOKEN_INDEX points to the index of the token in the
2021 macro map; It is not considered if MAP is NULL. If VIRT_LOCS is
2022 non-null, it means -ftrack-macro-expansion is on; in which case
2023 this function adds the virtual location DEF_LOC to the VIRT_LOCS
2024 array, at the same index as the one of TOKEN in BUFFER. Upon
2025 successful completion this function returns the a pointer to the
2026 position of the token coming right after the insertion point. */
2027 static const cpp_token
**
2028 tokens_buff_add_token (_cpp_buff
*buffer
,
2029 source_location
*virt_locs
,
2030 const cpp_token
*token
,
2031 source_location virt_loc
,
2032 source_location parm_def_loc
,
2033 const struct line_map
*map
,
2034 unsigned int macro_token_index
)
2036 const cpp_token
**result
;
2037 source_location
*virt_loc_dest
= NULL
;
2038 unsigned token_index
=
2039 (BUFF_FRONT (buffer
) - buffer
->base
) / sizeof (cpp_token
*);
2041 /* Abort if we pass the end the buffer. */
2042 if (BUFF_FRONT (buffer
) > BUFF_LIMIT (buffer
))
2045 if (virt_locs
!= NULL
)
2046 virt_loc_dest
= &virt_locs
[token_index
];
2049 tokens_buff_put_token_to ((const cpp_token
**) BUFF_FRONT (buffer
),
2050 virt_loc_dest
, token
, virt_loc
, parm_def_loc
,
2051 map
, macro_token_index
);
2053 BUFF_FRONT (buffer
) = (unsigned char *) result
;
2057 /* Allocate space for the function-like macro argument ARG to store
2058 the tokens resulting from the macro-expansion of the tokens that
2059 make up ARG itself. That space is allocated in ARG->expanded and
2060 needs to be freed using free. */
2062 alloc_expanded_arg_mem (cpp_reader
*pfile
, macro_arg
*arg
, size_t capacity
)
2064 #ifdef ENABLE_CHECKING
2065 if (arg
->expanded
!= NULL
2066 || arg
->expanded_virt_locs
!= NULL
)
2069 arg
->expanded
= XNEWVEC (const cpp_token
*, capacity
);
2070 if (CPP_OPTION (pfile
, track_macro_expansion
))
2071 arg
->expanded_virt_locs
= XNEWVEC (source_location
, capacity
);
2075 /* If necessary, enlarge ARG->expanded to so that it can contain SIZE
2078 ensure_expanded_arg_room (cpp_reader
*pfile
, macro_arg
*arg
,
2079 size_t size
, size_t *expanded_capacity
)
2081 if (size
<= *expanded_capacity
)
2087 XRESIZEVEC (const cpp_token
*, arg
->expanded
, size
);
2088 *expanded_capacity
= size
;
2090 if (CPP_OPTION (pfile
, track_macro_expansion
))
2092 if (arg
->expanded_virt_locs
== NULL
)
2093 arg
->expanded_virt_locs
= XNEWVEC (source_location
, size
);
2095 arg
->expanded_virt_locs
= XRESIZEVEC (source_location
,
2096 arg
->expanded_virt_locs
,
2101 /* Expand an argument ARG before replacing parameters in a
2102 function-like macro. This works by pushing a context with the
2103 argument's tokens, and then expanding that into a temporary buffer
2104 as if it were a normal part of the token stream. collect_args()
2105 has terminated the argument's tokens with a CPP_EOF so that we know
2106 when we have fully expanded the argument. */
2108 expand_arg (cpp_reader
*pfile
, macro_arg
*arg
)
2111 bool saved_warn_trad
;
2112 bool track_macro_exp_p
= CPP_OPTION (pfile
, track_macro_expansion
);
2115 || arg
->expanded
!= NULL
)
2118 /* Don't warn about funlike macros when pre-expanding. */
2119 saved_warn_trad
= CPP_WTRADITIONAL (pfile
);
2120 CPP_WTRADITIONAL (pfile
) = 0;
2122 /* Loop, reading in the tokens of the argument. */
2124 alloc_expanded_arg_mem (pfile
, arg
, capacity
);
2126 if (track_macro_exp_p
)
2127 push_extended_tokens_context (pfile
, NULL
, NULL
,
2132 push_ptoken_context (pfile
, NULL
, NULL
,
2133 arg
->first
, arg
->count
+ 1);
2137 const cpp_token
*token
;
2138 source_location location
;
2140 ensure_expanded_arg_room (pfile
, arg
, arg
->expanded_count
+ 1,
2143 token
= cpp_get_token_1 (pfile
, &location
);
2145 if (token
->type
== CPP_EOF
)
2148 set_arg_token (arg
, token
, location
,
2149 arg
->expanded_count
, MACRO_ARG_TOKEN_EXPANDED
,
2150 CPP_OPTION (pfile
, track_macro_expansion
));
2151 arg
->expanded_count
++;
2154 _cpp_pop_context (pfile
);
2156 CPP_WTRADITIONAL (pfile
) = saved_warn_trad
;
2159 /* Returns the macro associated to the current context if we are in
2160 the context a macro expansion, NULL otherwise. */
2161 static cpp_hashnode
*
2162 macro_of_context (cpp_context
*context
)
2164 if (context
== NULL
)
2167 return (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2168 ? context
->c
.mc
->macro_node
2172 /* Return TRUE iff we are expanding a macro or are about to start
2173 expanding one. If we are effectively expanding a macro, the
2174 function macro_of_context returns a pointer to the macro being
2177 in_macro_expansion_p (cpp_reader
*pfile
)
2182 return (pfile
->about_to_expand_macro_p
2183 || macro_of_context (pfile
->context
));
2186 /* Pop the current context off the stack, re-enabling the macro if the
2187 context represented a macro's replacement list. Initially the
2188 context structure was not freed so that we can re-use it later, but
2189 now we do free it to reduce peak memory consumption. */
2191 _cpp_pop_context (cpp_reader
*pfile
)
2193 cpp_context
*context
= pfile
->context
;
2195 /* We should not be popping the base context. */
2196 if (context
== &pfile
->base_context
)
2199 if (context
->c
.macro
)
2201 cpp_hashnode
*macro
;
2202 if (context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2204 macro_context
*mc
= context
->c
.mc
;
2205 macro
= mc
->macro_node
;
2206 /* If context->buff is set, it means the life time of tokens
2207 is bound to the life time of this context; so we must
2208 free the tokens; that means we must free the virtual
2209 locations of these tokens too. */
2210 if (context
->buff
&& mc
->virt_locs
)
2212 free (mc
->virt_locs
);
2213 mc
->virt_locs
= NULL
;
2216 context
->c
.mc
= NULL
;
2219 macro
= context
->c
.macro
;
2221 /* Beware that MACRO can be NULL in cases like when we are
2222 called from expand_arg. In those cases, a dummy context with
2223 tokens is pushed just for the purpose of walking them using
2224 cpp_get_token_1. In that case, no 'macro' field is set into
2225 the dummy context. */
2227 /* Several contiguous macro expansion contexts can be
2228 associated to the same macro; that means it's the same
2229 macro expansion that spans across all these (sub)
2230 contexts. So we should re-enable an expansion-disabled
2231 macro only when we are sure we are really out of that
2233 && macro_of_context (context
->prev
) != macro
)
2234 macro
->flags
&= ~NODE_DISABLED
;
2239 /* Decrease memory peak consumption by freeing the memory used
2241 _cpp_free_buff (context
->buff
);
2244 pfile
->context
= context
->prev
;
2245 /* decrease peak memory consumption by feeing the context. */
2246 pfile
->context
->next
= NULL
;
2250 /* Return TRUE if we reached the end of the set of tokens stored in
2251 CONTEXT, FALSE otherwise. */
2253 reached_end_of_context (cpp_context
*context
)
2255 if (context
->tokens_kind
== TOKENS_KIND_DIRECT
)
2256 return FIRST (context
).token
== LAST (context
).token
;
2257 else if (context
->tokens_kind
== TOKENS_KIND_INDIRECT
2258 || context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2259 return FIRST (context
).ptoken
== LAST (context
).ptoken
;
2264 /* Consume the next token contained in the current context of PFILE,
2265 and return it in *TOKEN. It's "full location" is returned in
2266 *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
2267 means the location encoding the locus of the token across macro
2268 expansion; otherwise it's just is the "normal" location of the
2269 token which (*TOKEN)->src_loc. */
2271 consume_next_token_from_context (cpp_reader
*pfile
,
2272 const cpp_token
** token
,
2273 source_location
*location
)
2275 cpp_context
*c
= pfile
->context
;
2277 if ((c
)->tokens_kind
== TOKENS_KIND_DIRECT
)
2279 *token
= FIRST (c
).token
;
2280 *location
= (*token
)->src_loc
;
2283 else if ((c
)->tokens_kind
== TOKENS_KIND_INDIRECT
)
2285 *token
= *FIRST (c
).ptoken
;
2286 *location
= (*token
)->src_loc
;
2289 else if ((c
)->tokens_kind
== TOKENS_KIND_EXTENDED
)
2291 macro_context
*m
= c
->c
.mc
;
2292 *token
= *FIRST (c
).ptoken
;
2295 *location
= *m
->cur_virt_loc
;
2299 *location
= (*token
)->src_loc
;
2306 /* In the traditional mode of the preprocessor, if we are currently in
2307 a directive, the location of a token must be the location of the
2308 start of the directive line. This function returns the proper
2309 location if we are in the traditional mode, and just returns
2310 LOCATION otherwise. */
2312 static inline source_location
2313 maybe_adjust_loc_for_trad_cpp (cpp_reader
*pfile
, source_location location
)
2315 if (CPP_OPTION (pfile
, traditional
))
2317 if (pfile
->state
.in_directive
)
2318 return pfile
->directive_line
;
2323 /* Routine to get a token as well as its location.
2325 Macro expansions and directives are transparently handled,
2326 including entering included files. Thus tokens are post-macro
2327 expansion, and after any intervening directives. External callers
2328 see CPP_EOF only at EOF. Internal callers also see it when meeting
2329 a directive inside a macro call, when at the end of a directive and
2330 state.in_directive is still 1, and at the end of argument
2333 LOC is an out parameter; *LOC is set to the location "as expected
2334 by the user". Please read the comment of
2335 cpp_get_token_with_location to learn more about the meaning of this
2337 static const cpp_token
*
2338 cpp_get_token_1 (cpp_reader
*pfile
, source_location
*location
)
2340 const cpp_token
*result
;
2341 /* This token is a virtual token that either encodes a location
2342 related to macro expansion or a spelling location. */
2343 source_location virt_loc
= 0;
2344 /* pfile->about_to_expand_macro_p can be overriden by indirect calls
2345 to functions that push macro contexts. So let's save it so that
2346 we can restore it when we are about to leave this routine. */
2347 bool saved_about_to_expand_macro
= pfile
->about_to_expand_macro_p
;
2352 cpp_context
*context
= pfile
->context
;
2354 /* Context->prev == 0 <=> base context. */
2357 result
= _cpp_lex_token (pfile
);
2358 virt_loc
= result
->src_loc
;
2360 else if (!reached_end_of_context (context
))
2362 consume_next_token_from_context (pfile
, &result
,
2364 if (result
->flags
& PASTE_LEFT
)
2366 paste_all_tokens (pfile
, result
);
2367 if (pfile
->state
.in_directive
)
2369 result
= padding_token (pfile
, result
);
2375 if (pfile
->context
->c
.macro
)
2376 ++num_expanded_macros_counter
;
2377 _cpp_pop_context (pfile
);
2378 if (pfile
->state
.in_directive
)
2380 result
= &pfile
->avoid_paste
;
2384 if (pfile
->state
.in_directive
&& result
->type
== CPP_COMMENT
)
2387 if (result
->type
!= CPP_NAME
)
2390 node
= result
->val
.node
.node
;
2392 if (node
->type
!= NT_MACRO
|| (result
->flags
& NO_EXPAND
))
2395 if (!(node
->flags
& NODE_DISABLED
))
2398 /* If not in a macro context, and we're going to start an
2399 expansion, record the location. */
2400 if (!in_macro_expansion_p (pfile
))
2401 pfile
->invocation_location
= result
->src_loc
;
2402 if (pfile
->state
.prevent_expansion
)
2405 /* Conditional macros require that a predicate be evaluated
2407 if ((node
->flags
& NODE_CONDITIONAL
) != 0)
2409 if (pfile
->cb
.macro_to_expand
)
2411 bool whitespace_after
;
2412 const cpp_token
*peek_tok
= cpp_peek_token (pfile
, 0);
2414 whitespace_after
= (peek_tok
->type
== CPP_PADDING
2415 || (peek_tok
->flags
& PREV_WHITE
));
2416 node
= pfile
->cb
.macro_to_expand (pfile
, result
);
2418 ret
= enter_macro_context (pfile
, node
, result
,
2420 else if (whitespace_after
)
2422 /* If macro_to_expand hook returned NULL and it
2423 ate some tokens, see if we don't need to add
2424 a padding token in between this and the
2426 peek_tok
= cpp_peek_token (pfile
, 0);
2427 if (peek_tok
->type
!= CPP_PADDING
2428 && (peek_tok
->flags
& PREV_WHITE
) == 0)
2429 _cpp_push_token_context (pfile
, NULL
,
2430 padding_token (pfile
,
2436 ret
= enter_macro_context (pfile
, node
, result
,
2440 if (pfile
->state
.in_directive
|| ret
== 2)
2442 result
= padding_token (pfile
, result
);
2448 /* Flag this token as always unexpandable. FIXME: move this
2449 to collect_args()?. */
2450 cpp_token
*t
= _cpp_temp_token (pfile
);
2451 t
->type
= result
->type
;
2452 t
->flags
= result
->flags
| NO_EXPAND
;
2453 t
->val
= result
->val
;
2461 if (location
!= NULL
)
2464 virt_loc
= result
->src_loc
;
2465 *location
= virt_loc
;
2467 if (!CPP_OPTION (pfile
, track_macro_expansion
)
2468 && macro_of_context (pfile
->context
) != NULL
)
2469 /* We are in a macro expansion context, are not tracking
2470 virtual location, but were asked to report the location
2471 of the expansion point of the macro being expanded. */
2472 *location
= pfile
->invocation_location
;
2474 *location
= maybe_adjust_loc_for_trad_cpp (pfile
, *location
);
2477 pfile
->about_to_expand_macro_p
= saved_about_to_expand_macro
;
2481 /* External routine to get a token. Also used nearly everywhere
2482 internally, except for places where we know we can safely call
2483 _cpp_lex_token directly, such as lexing a directive name.
2485 Macro expansions and directives are transparently handled,
2486 including entering included files. Thus tokens are post-macro
2487 expansion, and after any intervening directives. External callers
2488 see CPP_EOF only at EOF. Internal callers also see it when meeting
2489 a directive inside a macro call, when at the end of a directive and
2490 state.in_directive is still 1, and at the end of argument
2493 cpp_get_token (cpp_reader
*pfile
)
2495 return cpp_get_token_1 (pfile
, NULL
);
2498 /* Like cpp_get_token, but also returns a virtual token location
2499 separate from the spelling location carried by the returned token.
2501 LOC is an out parameter; *LOC is set to the location "as expected
2502 by the user". This matters when a token results from macro
2503 expansion; in that case the token's spelling location indicates the
2504 locus of the token in the definition of the macro but *LOC
2505 virtually encodes all the other meaningful locuses associated to
2508 What? virtual location? Yes, virtual location.
2510 If the token results from macro expansion and if macro expansion
2511 location tracking is enabled its virtual location encodes (at the
2514 - the spelling location of the token
2516 - the locus of the macro expansion point
2518 - the locus of the point where the token got instantiated as part
2519 of the macro expansion process.
2521 You have to use the linemap API to get the locus you are interested
2522 in from a given virtual location.
2524 Note however that virtual locations are not necessarily ordered for
2525 relations '<' and '>'. One must use the function
2526 linemap_location_before_p instead of using the relational operator
2529 If macro expansion tracking is off and if the token results from
2530 macro expansion the virtual location is the expansion point of the
2531 macro that got expanded.
2533 When the token doesn't result from macro expansion, the virtual
2534 location is just the same thing as its spelling location. */
2537 cpp_get_token_with_location (cpp_reader
*pfile
, source_location
*loc
)
2539 return cpp_get_token_1 (pfile
, loc
);
2542 /* Returns true if we're expanding an object-like macro that was
2543 defined in a system header. Just checks the macro at the top of
2544 the stack. Used for diagnostic suppression. */
2546 cpp_sys_macro_p (cpp_reader
*pfile
)
2548 cpp_hashnode
*node
= NULL
;
2550 if (pfile
->context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2551 node
= pfile
->context
->c
.mc
->macro_node
;
2553 node
= pfile
->context
->c
.macro
;
2555 return node
&& node
->value
.macro
&& node
->value
.macro
->syshdr
;
2558 /* Read each token in, until end of the current file. Directives are
2559 transparently processed. */
2561 cpp_scan_nooutput (cpp_reader
*pfile
)
2563 /* Request a CPP_EOF token at the end of this file, rather than
2564 transparently continuing with the including file. */
2565 pfile
->buffer
->return_at_eof
= true;
2567 pfile
->state
.discarding_output
++;
2568 pfile
->state
.prevent_expansion
++;
2570 if (CPP_OPTION (pfile
, traditional
))
2571 while (_cpp_read_logical_line_trad (pfile
))
2574 while (cpp_get_token (pfile
)->type
!= CPP_EOF
)
2577 pfile
->state
.discarding_output
--;
2578 pfile
->state
.prevent_expansion
--;
2581 /* Step back one or more tokens obtained from the lexer. */
2583 _cpp_backup_tokens_direct (cpp_reader
*pfile
, unsigned int count
)
2585 pfile
->lookaheads
+= count
;
2589 if (pfile
->cur_token
== pfile
->cur_run
->base
2590 /* Possible with -fpreprocessed and no leading #line. */
2591 && pfile
->cur_run
->prev
!= NULL
)
2593 pfile
->cur_run
= pfile
->cur_run
->prev
;
2594 pfile
->cur_token
= pfile
->cur_run
->limit
;
2599 /* Step back one (or more) tokens. Can only step back more than 1 if
2600 they are from the lexer, and not from macro expansion. */
2602 _cpp_backup_tokens (cpp_reader
*pfile
, unsigned int count
)
2604 if (pfile
->context
->prev
== NULL
)
2605 _cpp_backup_tokens_direct (pfile
, count
);
2610 if (pfile
->context
->tokens_kind
== TOKENS_KIND_DIRECT
)
2611 FIRST (pfile
->context
).token
--;
2612 else if (pfile
->context
->tokens_kind
== TOKENS_KIND_INDIRECT
)
2613 FIRST (pfile
->context
).ptoken
--;
2614 else if (pfile
->context
->tokens_kind
== TOKENS_KIND_EXTENDED
)
2616 FIRST (pfile
->context
).ptoken
--;
2617 if (pfile
->context
->c
.macro
)
2619 macro_context
*m
= pfile
->context
->c
.mc
;
2621 #ifdef ENABLE_CHECKING
2622 if (m
->cur_virt_loc
< m
->virt_locs
)
2634 /* #define directive parsing and handling. */
2636 /* Returns nonzero if a macro redefinition warning is required. */
2638 warn_of_redefinition (cpp_reader
*pfile
, cpp_hashnode
*node
,
2639 const cpp_macro
*macro2
)
2641 const cpp_macro
*macro1
;
2644 /* Some redefinitions need to be warned about regardless. */
2645 if (node
->flags
& NODE_WARN
)
2648 /* Suppress warnings for builtins that lack the NODE_WARN flag. */
2649 if (node
->flags
& NODE_BUILTIN
)
2651 if (!pfile
->cb
.user_builtin_macro
2652 || !pfile
->cb
.user_builtin_macro (pfile
, node
))
2656 /* Redefinitions of conditional (context-sensitive) macros, on
2657 the other hand, must be allowed silently. */
2658 if (node
->flags
& NODE_CONDITIONAL
)
2661 /* Redefinition of a macro is allowed if and only if the old and new
2662 definitions are the same. (6.10.3 paragraph 2). */
2663 macro1
= node
->value
.macro
;
2665 /* Don't check count here as it can be different in valid
2666 traditional redefinitions with just whitespace differences. */
2667 if (macro1
->paramc
!= macro2
->paramc
2668 || macro1
->fun_like
!= macro2
->fun_like
2669 || macro1
->variadic
!= macro2
->variadic
)
2672 /* Check parameter spellings. */
2673 for (i
= 0; i
< macro1
->paramc
; i
++)
2674 if (macro1
->params
[i
] != macro2
->params
[i
])
2677 /* Check the replacement text or tokens. */
2678 if (CPP_OPTION (pfile
, traditional
))
2679 return _cpp_expansions_different_trad (macro1
, macro2
);
2681 if (macro1
->count
!= macro2
->count
)
2684 for (i
= 0; i
< macro1
->count
; i
++)
2685 if (!_cpp_equiv_tokens (¯o1
->exp
.tokens
[i
], ¯o2
->exp
.tokens
[i
]))
2691 /* Free the definition of hashnode H. */
2693 _cpp_free_definition (cpp_hashnode
*h
)
2695 /* Macros and assertions no longer have anything to free. */
2697 /* Clear builtin flag in case of redefinition. */
2698 h
->flags
&= ~(NODE_BUILTIN
| NODE_DISABLED
| NODE_USED
);
2701 /* Save parameter NODE to the parameter list of macro MACRO. Returns
2702 zero on success, nonzero if the parameter is a duplicate. */
2704 _cpp_save_parameter (cpp_reader
*pfile
, cpp_macro
*macro
, cpp_hashnode
*node
)
2707 /* Constraint 6.10.3.6 - duplicate parameter names. */
2708 if (node
->flags
& NODE_MACRO_ARG
)
2710 cpp_error (pfile
, CPP_DL_ERROR
, "duplicate macro parameter \"%s\"",
2715 if (BUFF_ROOM (pfile
->a_buff
)
2716 < (macro
->paramc
+ 1) * sizeof (cpp_hashnode
*))
2717 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (cpp_hashnode
*));
2719 ((cpp_hashnode
**) BUFF_FRONT (pfile
->a_buff
))[macro
->paramc
++] = node
;
2720 node
->flags
|= NODE_MACRO_ARG
;
2721 len
= macro
->paramc
* sizeof (union _cpp_hashnode_value
);
2722 if (len
> pfile
->macro_buffer_len
)
2724 pfile
->macro_buffer
= XRESIZEVEC (unsigned char, pfile
->macro_buffer
,
2726 pfile
->macro_buffer_len
= len
;
2728 ((union _cpp_hashnode_value
*) pfile
->macro_buffer
)[macro
->paramc
- 1]
2731 node
->value
.arg_index
= macro
->paramc
;
2735 /* Check the syntax of the parameters in a MACRO definition. Returns
2736 false if an error occurs. */
2738 parse_params (cpp_reader
*pfile
, cpp_macro
*macro
)
2740 unsigned int prev_ident
= 0;
2744 const cpp_token
*token
= _cpp_lex_token (pfile
);
2746 switch (token
->type
)
2749 /* Allow/ignore comments in parameter lists if we are
2750 preserving comments in macro expansions. */
2751 if (token
->type
== CPP_COMMENT
2752 && ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
))
2755 cpp_error (pfile
, CPP_DL_ERROR
,
2756 "\"%s\" may not appear in macro parameter list",
2757 cpp_token_as_text (pfile
, token
));
2763 cpp_error (pfile
, CPP_DL_ERROR
,
2764 "macro parameters must be comma-separated");
2769 if (_cpp_save_parameter (pfile
, macro
, token
->val
.node
.node
))
2773 case CPP_CLOSE_PAREN
:
2774 if (prev_ident
|| macro
->paramc
== 0)
2777 /* Fall through to pick up the error. */
2781 cpp_error (pfile
, CPP_DL_ERROR
, "parameter name missing");
2788 macro
->variadic
= 1;
2791 _cpp_save_parameter (pfile
, macro
,
2792 pfile
->spec_nodes
.n__VA_ARGS__
);
2793 pfile
->state
.va_args_ok
= 1;
2794 if (! CPP_OPTION (pfile
, c99
)
2795 && CPP_OPTION (pfile
, cpp_pedantic
)
2796 && CPP_OPTION (pfile
, warn_variadic_macros
))
2798 (pfile
, CPP_W_VARIADIC_MACROS
,
2799 "anonymous variadic macros were introduced in C99");
2801 else if (CPP_OPTION (pfile
, cpp_pedantic
)
2802 && CPP_OPTION (pfile
, warn_variadic_macros
))
2803 cpp_pedwarning (pfile
, CPP_W_VARIADIC_MACROS
,
2804 "ISO C does not permit named variadic macros");
2806 /* We're at the end, and just expect a closing parenthesis. */
2807 token
= _cpp_lex_token (pfile
);
2808 if (token
->type
== CPP_CLOSE_PAREN
)
2813 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' in macro parameter list");
2819 /* Allocate room for a token from a macro's replacement list. */
2821 alloc_expansion_token (cpp_reader
*pfile
, cpp_macro
*macro
)
2823 if (BUFF_ROOM (pfile
->a_buff
) < (macro
->count
+ 1) * sizeof (cpp_token
))
2824 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (cpp_token
));
2826 return &((cpp_token
*) BUFF_FRONT (pfile
->a_buff
))[macro
->count
++];
2829 /* Lex a token from the expansion of MACRO, but mark parameters as we
2830 find them and warn of traditional stringification. */
2832 lex_expansion_token (cpp_reader
*pfile
, cpp_macro
*macro
)
2834 cpp_token
*token
, *saved_cur_token
;
2836 saved_cur_token
= pfile
->cur_token
;
2837 pfile
->cur_token
= alloc_expansion_token (pfile
, macro
);
2838 token
= _cpp_lex_direct (pfile
);
2839 pfile
->cur_token
= saved_cur_token
;
2841 /* Is this a parameter? */
2842 if (token
->type
== CPP_NAME
2843 && (token
->val
.node
.node
->flags
& NODE_MACRO_ARG
) != 0)
2845 token
->type
= CPP_MACRO_ARG
;
2846 token
->val
.macro_arg
.arg_no
= token
->val
.node
.node
->value
.arg_index
;
2848 else if (CPP_WTRADITIONAL (pfile
) && macro
->paramc
> 0
2849 && (token
->type
== CPP_STRING
|| token
->type
== CPP_CHAR
))
2850 check_trad_stringification (pfile
, macro
, &token
->val
.str
);
2856 create_iso_definition (cpp_reader
*pfile
, cpp_macro
*macro
)
2859 const cpp_token
*ctoken
;
2860 bool following_paste_op
= false;
2861 const char *paste_op_error_msg
=
2862 N_("'##' cannot appear at either end of a macro expansion");
2863 unsigned int num_extra_tokens
= 0;
2865 /* Get the first token of the expansion (or the '(' of a
2866 function-like macro). */
2867 ctoken
= _cpp_lex_token (pfile
);
2869 if (ctoken
->type
== CPP_OPEN_PAREN
&& !(ctoken
->flags
& PREV_WHITE
))
2871 bool ok
= parse_params (pfile
, macro
);
2872 macro
->params
= (cpp_hashnode
**) BUFF_FRONT (pfile
->a_buff
);
2876 /* Success. Commit or allocate the parameter array. */
2877 if (pfile
->hash_table
->alloc_subobject
)
2879 cpp_hashnode
**params
=
2880 (cpp_hashnode
**) pfile
->hash_table
->alloc_subobject
2881 (sizeof (cpp_hashnode
*) * macro
->paramc
);
2882 memcpy (params
, macro
->params
,
2883 sizeof (cpp_hashnode
*) * macro
->paramc
);
2884 macro
->params
= params
;
2887 BUFF_FRONT (pfile
->a_buff
) = (uchar
*) ¯o
->params
[macro
->paramc
];
2888 macro
->fun_like
= 1;
2890 else if (ctoken
->type
!= CPP_EOF
&& !(ctoken
->flags
& PREV_WHITE
))
2892 /* While ISO C99 requires whitespace before replacement text
2893 in a macro definition, ISO C90 with TC1 allows there characters
2894 from the basic source character set. */
2895 if (CPP_OPTION (pfile
, c99
))
2896 cpp_error (pfile
, CPP_DL_PEDWARN
,
2897 "ISO C99 requires whitespace after the macro name");
2900 int warntype
= CPP_DL_WARNING
;
2901 switch (ctoken
->type
)
2905 case CPP_OBJC_STRING
:
2906 /* '@' is not in basic character set. */
2907 warntype
= CPP_DL_PEDWARN
;
2910 /* Basic character set sans letters, digits and _. */
2911 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
2912 ctoken
->val
.str
.text
[0]) == NULL
)
2913 warntype
= CPP_DL_PEDWARN
;
2916 /* All other tokens start with a character from basic
2920 cpp_error (pfile
, warntype
,
2921 "missing whitespace after the macro name");
2925 if (macro
->fun_like
)
2926 token
= lex_expansion_token (pfile
, macro
);
2929 token
= alloc_expansion_token (pfile
, macro
);
2935 /* Check the stringifying # constraint 6.10.3.2.1 of
2936 function-like macros when lexing the subsequent token. */
2937 if (macro
->count
> 1 && token
[-1].type
== CPP_HASH
&& macro
->fun_like
)
2939 if (token
->type
== CPP_MACRO_ARG
)
2941 if (token
->flags
& PREV_WHITE
)
2942 token
->flags
|= SP_PREV_WHITE
;
2943 if (token
[-1].flags
& DIGRAPH
)
2944 token
->flags
|= SP_DIGRAPH
;
2945 token
->flags
&= ~PREV_WHITE
;
2946 token
->flags
|= STRINGIFY_ARG
;
2947 token
->flags
|= token
[-1].flags
& PREV_WHITE
;
2948 token
[-1] = token
[0];
2951 /* Let assembler get away with murder. */
2952 else if (CPP_OPTION (pfile
, lang
) != CLK_ASM
)
2954 cpp_error (pfile
, CPP_DL_ERROR
,
2955 "'#' is not followed by a macro parameter");
2960 if (token
->type
== CPP_EOF
)
2962 /* Paste operator constraint 6.10.3.3.1:
2963 Token-paste ##, can appear in both object-like and
2964 function-like macros, but not at the end. */
2965 if (following_paste_op
)
2967 cpp_error (pfile
, CPP_DL_ERROR
, paste_op_error_msg
);
2973 /* Paste operator constraint 6.10.3.3.1. */
2974 if (token
->type
== CPP_PASTE
)
2976 /* Token-paste ##, can appear in both object-like and
2977 function-like macros, but not at the beginning. */
2978 if (macro
->count
== 1)
2980 cpp_error (pfile
, CPP_DL_ERROR
, paste_op_error_msg
);
2984 if (token
[-1].flags
& PASTE_LEFT
)
2986 macro
->extra_tokens
= 1;
2988 token
->val
.token_no
= macro
->count
- 1;
2993 token
[-1].flags
|= PASTE_LEFT
;
2994 if (token
->flags
& DIGRAPH
)
2995 token
[-1].flags
|= SP_DIGRAPH
;
2996 if (token
->flags
& PREV_WHITE
)
2997 token
[-1].flags
|= SP_PREV_WHITE
;
3001 following_paste_op
= (token
->type
== CPP_PASTE
);
3002 token
= lex_expansion_token (pfile
, macro
);
3005 macro
->exp
.tokens
= (cpp_token
*) BUFF_FRONT (pfile
->a_buff
);
3006 macro
->traditional
= 0;
3008 /* Don't count the CPP_EOF. */
3011 /* Clear whitespace on first token for warn_of_redefinition(). */
3013 macro
->exp
.tokens
[0].flags
&= ~PREV_WHITE
;
3015 /* Commit or allocate the memory. */
3016 if (pfile
->hash_table
->alloc_subobject
)
3019 (cpp_token
*) pfile
->hash_table
->alloc_subobject (sizeof (cpp_token
)
3021 if (num_extra_tokens
)
3023 /* Place second and subsequent ## or %:%: tokens in
3024 sequences of consecutive such tokens at the end of the
3025 list to preserve information about where they appear, how
3026 they are spelt and whether they are preceded by
3027 whitespace without otherwise interfering with macro
3029 cpp_token
*normal_dest
= tokns
;
3030 cpp_token
*extra_dest
= tokns
+ macro
->count
- num_extra_tokens
;
3032 for (i
= 0; i
< macro
->count
; i
++)
3034 if (macro
->exp
.tokens
[i
].type
== CPP_PASTE
)
3035 *extra_dest
++ = macro
->exp
.tokens
[i
];
3037 *normal_dest
++ = macro
->exp
.tokens
[i
];
3041 memcpy (tokns
, macro
->exp
.tokens
, sizeof (cpp_token
) * macro
->count
);
3042 macro
->exp
.tokens
= tokns
;
3045 BUFF_FRONT (pfile
->a_buff
) = (uchar
*) ¯o
->exp
.tokens
[macro
->count
];
3050 /* Parse a macro and save its expansion. Returns nonzero on success. */
3052 _cpp_create_definition (cpp_reader
*pfile
, cpp_hashnode
*node
)
3058 if (pfile
->hash_table
->alloc_subobject
)
3059 macro
= (cpp_macro
*) pfile
->hash_table
->alloc_subobject
3060 (sizeof (cpp_macro
));
3062 macro
= (cpp_macro
*) _cpp_aligned_alloc (pfile
, sizeof (cpp_macro
));
3063 macro
->line
= pfile
->directive_line
;
3066 macro
->variadic
= 0;
3067 macro
->used
= !CPP_OPTION (pfile
, warn_unused_macros
);
3069 macro
->fun_like
= 0;
3070 macro
->extra_tokens
= 0;
3071 /* To suppress some diagnostics. */
3072 macro
->syshdr
= pfile
->buffer
&& pfile
->buffer
->sysp
!= 0;
3074 if (CPP_OPTION (pfile
, traditional
))
3075 ok
= _cpp_create_trad_definition (pfile
, macro
);
3078 ok
= create_iso_definition (pfile
, macro
);
3080 /* We set the type for SEEN_EOL() in directives.c.
3082 Longer term we should lex the whole line before coming here,
3083 and just copy the expansion. */
3085 /* Stop the lexer accepting __VA_ARGS__. */
3086 pfile
->state
.va_args_ok
= 0;
3089 /* Clear the fast argument lookup indices. */
3090 for (i
= macro
->paramc
; i
-- > 0; )
3092 struct cpp_hashnode
*node
= macro
->params
[i
];
3093 node
->flags
&= ~ NODE_MACRO_ARG
;
3094 node
->value
= ((union _cpp_hashnode_value
*) pfile
->macro_buffer
)[i
];
3100 if (node
->type
== NT_MACRO
)
3102 if (CPP_OPTION (pfile
, warn_unused_macros
))
3103 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
3105 if (warn_of_redefinition (pfile
, node
, macro
))
3107 const int reason
= (node
->flags
& NODE_BUILTIN
)
3108 ? CPP_W_BUILTIN_MACRO_REDEFINED
: CPP_W_NONE
;
3111 warned
= cpp_pedwarning_with_line (pfile
, reason
,
3112 pfile
->directive_line
, 0,
3116 if (warned
&& node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
))
3117 cpp_error_with_line (pfile
, CPP_DL_NOTE
,
3118 node
->value
.macro
->line
, 0,
3119 "this is the location of the previous definition");
3123 if (node
->type
!= NT_VOID
)
3124 _cpp_free_definition (node
);
3126 /* Enter definition in hash table. */
3127 node
->type
= NT_MACRO
;
3128 node
->value
.macro
= macro
;
3129 if (! ustrncmp (NODE_NAME (node
), DSC ("__STDC_"))
3130 && ustrcmp (NODE_NAME (node
), (const uchar
*) "__STDC_FORMAT_MACROS")
3131 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
3132 in the C standard, as something that one must use in C++.
3133 However DR#593 indicates that these aren't actually mentioned
3134 in the C++ standard. We special-case them anyway. */
3135 && ustrcmp (NODE_NAME (node
), (const uchar
*) "__STDC_LIMIT_MACROS")
3136 && ustrcmp (NODE_NAME (node
), (const uchar
*) "__STDC_CONSTANT_MACROS"))
3137 node
->flags
|= NODE_WARN
;
3139 /* If user defines one of the conditional macros, remove the
3141 node
->flags
&= ~NODE_CONDITIONAL
;
3146 /* Warn if a token in STRING matches one of a function-like MACRO's
3149 check_trad_stringification (cpp_reader
*pfile
, const cpp_macro
*macro
,
3150 const cpp_string
*string
)
3152 unsigned int i
, len
;
3153 const uchar
*p
, *q
, *limit
;
3155 /* Loop over the string. */
3156 limit
= string
->text
+ string
->len
- 1;
3157 for (p
= string
->text
+ 1; p
< limit
; p
= q
)
3159 /* Find the start of an identifier. */
3160 while (p
< limit
&& !is_idstart (*p
))
3163 /* Find the end of the identifier. */
3165 while (q
< limit
&& is_idchar (*q
))
3170 /* Loop over the function macro arguments to see if the
3171 identifier inside the string matches one of them. */
3172 for (i
= 0; i
< macro
->paramc
; i
++)
3174 const cpp_hashnode
*node
= macro
->params
[i
];
3176 if (NODE_LEN (node
) == len
3177 && !memcmp (p
, NODE_NAME (node
), len
))
3179 cpp_error (pfile
, CPP_DL_WARNING
,
3180 "macro argument \"%s\" would be stringified in traditional C",
3188 /* Returns the name, arguments and expansion of a macro, in a format
3189 suitable to be read back in again, and therefore also for DWARF 2
3190 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
3191 Caller is expected to generate the "#define" bit if needed. The
3192 returned text is temporary, and automatically freed later. */
3193 const unsigned char *
3194 cpp_macro_definition (cpp_reader
*pfile
, cpp_hashnode
*node
)
3196 unsigned int i
, len
;
3197 const cpp_macro
*macro
;
3198 unsigned char *buffer
;
3200 if (node
->type
!= NT_MACRO
|| (node
->flags
& NODE_BUILTIN
))
3202 if (node
->type
!= NT_MACRO
3203 || !pfile
->cb
.user_builtin_macro
3204 || !pfile
->cb
.user_builtin_macro (pfile
, node
))
3206 cpp_error (pfile
, CPP_DL_ICE
,
3207 "invalid hash type %d in cpp_macro_definition",
3213 macro
= node
->value
.macro
;
3214 /* Calculate length. */
3215 len
= NODE_LEN (node
) + 2; /* ' ' and NUL. */
3216 if (macro
->fun_like
)
3218 len
+= 4; /* "()" plus possible final ".." of named
3219 varargs (we have + 1 below). */
3220 for (i
= 0; i
< macro
->paramc
; i
++)
3221 len
+= NODE_LEN (macro
->params
[i
]) + 1; /* "," */
3224 /* This should match below where we fill in the buffer. */
3225 if (CPP_OPTION (pfile
, traditional
))
3226 len
+= _cpp_replacement_text_len (macro
);
3229 unsigned int count
= macro_real_token_count (macro
);
3230 for (i
= 0; i
< count
; i
++)
3232 cpp_token
*token
= ¯o
->exp
.tokens
[i
];
3234 if (token
->type
== CPP_MACRO_ARG
)
3235 len
+= NODE_LEN (macro
->params
[token
->val
.macro_arg
.arg_no
- 1]);
3237 len
+= cpp_token_len (token
);
3239 if (token
->flags
& STRINGIFY_ARG
)
3241 if (token
->flags
& PASTE_LEFT
)
3242 len
+= 3; /* " ##" */
3243 if (token
->flags
& PREV_WHITE
)
3248 if (len
> pfile
->macro_buffer_len
)
3250 pfile
->macro_buffer
= XRESIZEVEC (unsigned char,
3251 pfile
->macro_buffer
, len
);
3252 pfile
->macro_buffer_len
= len
;
3255 /* Fill in the buffer. Start with the macro name. */
3256 buffer
= pfile
->macro_buffer
;
3257 memcpy (buffer
, NODE_NAME (node
), NODE_LEN (node
));
3258 buffer
+= NODE_LEN (node
);
3260 /* Parameter names. */
3261 if (macro
->fun_like
)
3264 for (i
= 0; i
< macro
->paramc
; i
++)
3266 cpp_hashnode
*param
= macro
->params
[i
];
3268 if (param
!= pfile
->spec_nodes
.n__VA_ARGS__
)
3270 memcpy (buffer
, NODE_NAME (param
), NODE_LEN (param
));
3271 buffer
+= NODE_LEN (param
);
3274 if (i
+ 1 < macro
->paramc
)
3275 /* Don't emit a space after the comma here; we're trying
3276 to emit a Dwarf-friendly definition, and the Dwarf spec
3277 forbids spaces in the argument list. */
3279 else if (macro
->variadic
)
3280 *buffer
++ = '.', *buffer
++ = '.', *buffer
++ = '.';
3285 /* The Dwarf spec requires a space after the macro name, even if the
3286 definition is the empty string. */
3289 if (CPP_OPTION (pfile
, traditional
))
3290 buffer
= _cpp_copy_replacement_text (macro
, buffer
);
3291 else if (macro
->count
)
3292 /* Expansion tokens. */
3294 unsigned int count
= macro_real_token_count (macro
);
3295 for (i
= 0; i
< count
; i
++)
3297 cpp_token
*token
= ¯o
->exp
.tokens
[i
];
3299 if (token
->flags
& PREV_WHITE
)
3301 if (token
->flags
& STRINGIFY_ARG
)
3304 if (token
->type
== CPP_MACRO_ARG
)
3307 NODE_NAME (macro
->params
[token
->val
.macro_arg
.arg_no
- 1]),
3308 NODE_LEN (macro
->params
[token
->val
.macro_arg
.arg_no
- 1]));
3309 buffer
+= NODE_LEN (macro
->params
[token
->val
.macro_arg
.arg_no
- 1]);
3312 buffer
= cpp_spell_token (pfile
, token
, buffer
, false);
3314 if (token
->flags
& PASTE_LEFT
)
3319 /* Next has PREV_WHITE; see _cpp_create_definition. */
3325 return pfile
->macro_buffer
;