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 Free Software Foundation, Inc.
4 Written by Per Bothner, 1994.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 In other words, you are welcome to use, share and improve this program.
23 You are forbidden to forbid anyone else to use, share and improve
24 what you give them. Help stamp out software-hoarding! */
31 typedef struct macro_arg macro_arg
;
34 const cpp_token
**first
; /* First token in unexpanded argument. */
35 const cpp_token
**expanded
; /* Macro-expanded argument. */
36 const cpp_token
*stringified
; /* Stringified argument. */
37 unsigned int count
; /* # of tokens in argument. */
38 unsigned int expanded_count
; /* # of tokens in expanded argument. */
41 /* Macro expansion. */
43 static int enter_macro_context (cpp_reader
*, cpp_hashnode
*);
44 static int builtin_macro (cpp_reader
*, cpp_hashnode
*);
45 static void push_ptoken_context (cpp_reader
*, cpp_hashnode
*, _cpp_buff
*,
46 const cpp_token
**, unsigned int);
47 static _cpp_buff
*collect_args (cpp_reader
*, const cpp_hashnode
*);
48 static cpp_context
*next_context (cpp_reader
*);
49 static const cpp_token
*padding_token (cpp_reader
*, const cpp_token
*);
50 static void expand_arg (cpp_reader
*, macro_arg
*);
51 static const cpp_token
*new_string_token (cpp_reader
*, uchar
*, unsigned int);
52 static const cpp_token
*stringify_arg (cpp_reader
*, macro_arg
*);
53 static void paste_all_tokens (cpp_reader
*, const cpp_token
*);
54 static bool paste_tokens (cpp_reader
*, const cpp_token
**, const cpp_token
*);
55 static void replace_args (cpp_reader
*, cpp_hashnode
*, cpp_macro
*,
57 static _cpp_buff
*funlike_invocation_p (cpp_reader
*, cpp_hashnode
*);
58 static bool create_iso_definition (cpp_reader
*, cpp_macro
*);
60 /* #define directive parsing and handling. */
62 static cpp_token
*alloc_expansion_token (cpp_reader
*, cpp_macro
*);
63 static cpp_token
*lex_expansion_token (cpp_reader
*, cpp_macro
*);
64 static bool warn_of_redefinition (cpp_reader
*, const cpp_hashnode
*,
66 static bool parse_params (cpp_reader
*, cpp_macro
*);
67 static void check_trad_stringification (cpp_reader
*, const cpp_macro
*,
70 /* Emits a warning if NODE is a macro defined in the main file that
73 _cpp_warn_if_unused_macro (cpp_reader
*pfile
, cpp_hashnode
*node
,
74 void *v ATTRIBUTE_UNUSED
)
76 if (node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
))
78 cpp_macro
*macro
= node
->value
.macro
;
81 && MAIN_FILE_P (linemap_lookup (pfile
->line_table
, macro
->line
)))
82 cpp_error_with_line (pfile
, CPP_DL_WARNING
, macro
->line
, 0,
83 "macro \"%s\" is not used", NODE_NAME (node
));
89 /* Allocates and returns a CPP_STRING token, containing TEXT of length
90 LEN, after null-terminating it. TEXT must be in permanent storage. */
91 static const cpp_token
*
92 new_string_token (cpp_reader
*pfile
, unsigned char *text
, unsigned int len
)
94 cpp_token
*token
= _cpp_temp_token (pfile
);
97 token
->type
= CPP_STRING
;
98 token
->val
.str
.len
= len
;
99 token
->val
.str
.text
= text
;
104 static const char * const monthnames
[] =
106 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
107 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
110 /* Helper function for builtin_macro. Returns the text generated by
113 _cpp_builtin_macro_text (cpp_reader
*pfile
, cpp_hashnode
*node
)
115 const struct line_map
*map
;
116 const uchar
*result
= NULL
;
117 unsigned int number
= 1;
119 switch (node
->value
.builtin
)
122 cpp_error (pfile
, CPP_DL_ICE
, "invalid built-in macro \"%s\"",
132 map
= linemap_lookup (pfile
->line_table
, pfile
->line_table
->highest_line
);
134 if (node
->value
.builtin
== BT_BASE_FILE
)
135 while (! MAIN_FILE_P (map
))
136 map
= INCLUDED_FROM (pfile
->line_table
, map
);
140 buf
= _cpp_unaligned_alloc (pfile
, len
* 2 + 3);
143 buf
= cpp_quote_string (buf
+ 1, (const unsigned char *) name
, len
);
149 case BT_INCLUDE_LEVEL
:
150 /* The line map depth counts the primary source as level 1, but
151 historically __INCLUDE_DEPTH__ has called the primary source
153 number
= pfile
->line_table
->depth
- 1;
157 map
= &pfile
->line_table
->maps
[pfile
->line_table
->used
-1];
158 /* If __LINE__ is embedded in a macro, it must expand to the
159 line of the macro's invocation, not its definition.
160 Otherwise things like assert() will not work properly. */
161 if (CPP_OPTION (pfile
, traditional
))
162 number
= pfile
->line_table
->highest_line
;
164 number
= pfile
->cur_token
[-1].src_loc
;
165 number
= SOURCE_LINE (map
, number
);
168 /* __STDC__ has the value 1 under normal circumstances.
169 However, if (a) we are in a system header, (b) the option
170 stdc_0_in_system_headers is true (set by target config), and
171 (c) we are not in strictly conforming mode, then it has the
172 value 0. (b) and (c) are already checked in cpp_init_builtins. */
174 if (cpp_in_system_header (pfile
))
182 if (pfile
->date
== NULL
)
184 /* Allocate __DATE__ and __TIME__ strings from permanent
185 storage. We only do this once, and don't generate them
186 at init time, because time() and localtime() are very
187 slow on some systems. */
189 struct tm
*tb
= NULL
;
191 /* (time_t) -1 is a legitimate value for "number of seconds
192 since the Epoch", so we have to do a little dance to
193 distinguish that from a genuine error. */
196 if (tt
!= (time_t)-1 || errno
== 0)
197 tb
= localtime (&tt
);
201 pfile
->date
= _cpp_unaligned_alloc (pfile
,
202 sizeof ("\"Oct 11 1347\""));
203 sprintf ((char *) pfile
->date
, "\"%s %2d %4d\"",
204 monthnames
[tb
->tm_mon
], tb
->tm_mday
,
207 pfile
->time
= _cpp_unaligned_alloc (pfile
,
208 sizeof ("\"12:34:56\""));
209 sprintf ((char *) pfile
->time
, "\"%02d:%02d:%02d\"",
210 tb
->tm_hour
, tb
->tm_min
, tb
->tm_sec
);
214 cpp_errno (pfile
, CPP_DL_WARNING
,
215 "could not determine date and time");
217 pfile
->date
= U
"\"??? ?? ????\"";
218 pfile
->time
= U
"\"??:??:??\"";
222 if (node
->value
.builtin
== BT_DATE
)
223 result
= pfile
->date
;
225 result
= pfile
->time
;
231 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
232 result
= _cpp_unaligned_alloc (pfile
, 21);
233 sprintf ((char *) result
, "%u", number
);
239 /* Convert builtin macros like __FILE__ to a token and push it on the
240 context stack. Also handles _Pragma, for which a new token may not
241 be created. Returns 1 if it generates a new token context, 0 to
242 return the token to the caller. */
244 builtin_macro (cpp_reader
*pfile
, cpp_hashnode
*node
)
250 if (node
->value
.builtin
== BT_PRAGMA
)
252 /* Don't interpret _Pragma within directives. The standard is
253 not clear on this, but to me this makes most sense. */
254 if (pfile
->state
.in_directive
)
257 _cpp_do__Pragma (pfile
);
261 buf
= _cpp_builtin_macro_text (pfile
, node
);
263 nbuf
= (char *) alloca (len
+ 1);
264 memcpy (nbuf
, buf
, len
);
267 cpp_push_buffer (pfile
, (uchar
*) nbuf
, len
, /* from_stage3 */ true);
268 _cpp_clean_line (pfile
);
270 /* Set pfile->cur_token as required by _cpp_lex_direct. */
271 pfile
->cur_token
= _cpp_temp_token (pfile
);
272 _cpp_push_token_context (pfile
, NULL
, _cpp_lex_direct (pfile
), 1);
273 if (pfile
->buffer
->cur
!= pfile
->buffer
->rlimit
)
274 cpp_error (pfile
, CPP_DL_ICE
, "invalid built-in macro \"%s\"",
276 _cpp_pop_buffer (pfile
);
281 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
282 backslashes and double quotes. DEST must be of sufficient size.
283 Returns a pointer to the end of the string. */
285 cpp_quote_string (uchar
*dest
, const uchar
*src
, unsigned int len
)
291 if (c
== '\\' || c
== '"')
303 /* Convert a token sequence ARG to a single string token according to
304 the rules of the ISO C #-operator. */
305 static const cpp_token
*
306 stringify_arg (cpp_reader
*pfile
, macro_arg
*arg
)
309 unsigned int i
, escape_it
, backslash_count
= 0;
310 const cpp_token
*source
= NULL
;
313 if (BUFF_ROOM (pfile
->u_buff
) < 3)
314 _cpp_extend_buff (pfile
, &pfile
->u_buff
, 3);
315 dest
= BUFF_FRONT (pfile
->u_buff
);
318 /* Loop, reading in the argument's tokens. */
319 for (i
= 0; i
< arg
->count
; i
++)
321 const cpp_token
*token
= arg
->first
[i
];
323 if (token
->type
== CPP_PADDING
)
326 source
= token
->val
.source
;
330 escape_it
= (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
331 || token
->type
== CPP_CHAR
|| token
->type
== CPP_WCHAR
);
333 /* Room for each char being written in octal, initial space and
334 final quote and NUL. */
335 len
= cpp_token_len (token
);
340 if ((size_t) (BUFF_LIMIT (pfile
->u_buff
) - dest
) < len
)
342 size_t len_so_far
= dest
- BUFF_FRONT (pfile
->u_buff
);
343 _cpp_extend_buff (pfile
, &pfile
->u_buff
, len
);
344 dest
= BUFF_FRONT (pfile
->u_buff
) + len_so_far
;
347 /* Leading white space? */
348 if (dest
- 1 != BUFF_FRONT (pfile
->u_buff
))
352 if (source
->flags
& PREV_WHITE
)
359 _cpp_buff
*buff
= _cpp_get_buff (pfile
, len
);
360 unsigned char *buf
= BUFF_FRONT (buff
);
361 len
= cpp_spell_token (pfile
, token
, buf
, true) - buf
;
362 dest
= cpp_quote_string (dest
, buf
, len
);
363 _cpp_release_buff (pfile
, buff
);
366 dest
= cpp_spell_token (pfile
, token
, dest
, true);
368 if (token
->type
== CPP_OTHER
&& token
->val
.str
.text
[0] == '\\')
374 /* Ignore the final \ of invalid string literals. */
375 if (backslash_count
& 1)
377 cpp_error (pfile
, CPP_DL_WARNING
,
378 "invalid string literal, ignoring final '\\'");
382 /* Commit the memory, including NUL, and return the token. */
384 len
= dest
- BUFF_FRONT (pfile
->u_buff
);
385 BUFF_FRONT (pfile
->u_buff
) = dest
+ 1;
386 return new_string_token (pfile
, dest
- len
, len
);
389 /* Try to paste two tokens. On success, return nonzero. In any
390 case, PLHS is updated to point to the pasted token, which is
391 guaranteed to not have the PASTE_LEFT flag set. */
393 paste_tokens (cpp_reader
*pfile
, const cpp_token
**plhs
, const cpp_token
*rhs
)
395 unsigned char *buf
, *end
;
396 const cpp_token
*lhs
;
401 len
= cpp_token_len (lhs
) + cpp_token_len (rhs
) + 1;
402 buf
= (unsigned char *) alloca (len
);
403 end
= cpp_spell_token (pfile
, lhs
, buf
, false);
405 /* Avoid comment headers, since they are still processed in stage 3.
406 It is simpler to insert a space here, rather than modifying the
407 lexer to ignore comments in some circumstances. Simply returning
408 false doesn't work, since we want to clear the PASTE_LEFT flag. */
409 if (lhs
->type
== CPP_DIV
&& rhs
->type
!= CPP_EQ
)
411 end
= cpp_spell_token (pfile
, rhs
, end
, false);
414 cpp_push_buffer (pfile
, buf
, end
- buf
, /* from_stage3 */ true);
415 _cpp_clean_line (pfile
);
417 /* Set pfile->cur_token as required by _cpp_lex_direct. */
418 pfile
->cur_token
= _cpp_temp_token (pfile
);
419 *plhs
= _cpp_lex_direct (pfile
);
420 valid
= pfile
->buffer
->cur
== pfile
->buffer
->rlimit
;
421 _cpp_pop_buffer (pfile
);
426 /* Handles an arbitrarily long sequence of ## operators, with initial
427 operand LHS. This implementation is left-associative,
428 non-recursive, and finishes a paste before handling succeeding
429 ones. If a paste fails, we back up to the RHS of the failing ##
430 operator before pushing the context containing the result of prior
431 successful pastes, with the effect that the RHS appears in the
432 output stream after the pasted LHS normally. */
434 paste_all_tokens (cpp_reader
*pfile
, const cpp_token
*lhs
)
436 const cpp_token
*rhs
;
437 cpp_context
*context
= pfile
->context
;
441 /* Take the token directly from the current context. We can do
442 this, because we are in the replacement list of either an
443 object-like macro, or a function-like macro with arguments
444 inserted. In either case, the constraints to #define
445 guarantee we have at least one more token. */
446 if (context
->direct_p
)
447 rhs
= FIRST (context
).token
++;
449 rhs
= *FIRST (context
).ptoken
++;
451 if (rhs
->type
== CPP_PADDING
)
454 if (!paste_tokens (pfile
, &lhs
, rhs
))
456 _cpp_backup_tokens (pfile
, 1);
458 /* Mandatory error for all apart from assembler. */
459 if (CPP_OPTION (pfile
, lang
) != CLK_ASM
)
460 cpp_error (pfile
, CPP_DL_ERROR
,
461 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
462 cpp_token_as_text (pfile
, lhs
),
463 cpp_token_as_text (pfile
, rhs
));
467 while (rhs
->flags
& PASTE_LEFT
);
469 /* Put the resulting token in its own context. */
470 _cpp_push_token_context (pfile
, NULL
, lhs
, 1);
473 /* Returns TRUE if the number of arguments ARGC supplied in an
474 invocation of the MACRO referenced by NODE is valid. An empty
475 invocation to a macro with no parameters should pass ARGC as zero.
477 Note that MACRO cannot necessarily be deduced from NODE, in case
478 NODE was redefined whilst collecting arguments. */
480 _cpp_arguments_ok (cpp_reader
*pfile
, cpp_macro
*macro
, const cpp_hashnode
*node
, unsigned int argc
)
482 if (argc
== macro
->paramc
)
485 if (argc
< macro
->paramc
)
487 /* As an extension, a rest argument is allowed to not appear in
488 the invocation at all.
489 e.g. #define debug(format, args...) something
492 This is exactly the same as if there had been an empty rest
493 argument - debug("string", ). */
495 if (argc
+ 1 == macro
->paramc
&& macro
->variadic
)
497 if (CPP_PEDANTIC (pfile
) && ! macro
->syshdr
)
498 cpp_error (pfile
, CPP_DL_PEDWARN
,
499 "ISO C99 requires rest arguments to be used");
503 cpp_error (pfile
, CPP_DL_ERROR
,
504 "macro \"%s\" requires %u arguments, but only %u given",
505 NODE_NAME (node
), macro
->paramc
, argc
);
508 cpp_error (pfile
, CPP_DL_ERROR
,
509 "macro \"%s\" passed %u arguments, but takes just %u",
510 NODE_NAME (node
), argc
, macro
->paramc
);
515 /* Reads and returns the arguments to a function-like macro
516 invocation. Assumes the opening parenthesis has been processed.
517 If there is an error, emits an appropriate diagnostic and returns
518 NULL. Each argument is terminated by a CPP_EOF token, for the
519 future benefit of expand_arg(). */
521 collect_args (cpp_reader
*pfile
, const cpp_hashnode
*node
)
523 _cpp_buff
*buff
, *base_buff
;
525 macro_arg
*args
, *arg
;
526 const cpp_token
*token
;
529 macro
= node
->value
.macro
;
531 argc
= macro
->paramc
;
534 buff
= _cpp_get_buff (pfile
, argc
* (50 * sizeof (cpp_token
*)
535 + sizeof (macro_arg
)));
537 args
= (macro_arg
*) buff
->base
;
538 memset (args
, 0, argc
* sizeof (macro_arg
));
539 buff
->cur
= (unsigned char *) &args
[argc
];
540 arg
= args
, argc
= 0;
542 /* Collect the tokens making up each argument. We don't yet know
543 how many arguments have been supplied, whether too many or too
544 few. Hence the slightly bizarre usage of "argc" and "arg". */
547 unsigned int paren_depth
= 0;
548 unsigned int ntokens
= 0;
551 arg
->first
= (const cpp_token
**) buff
->cur
;
555 /* Require space for 2 new tokens (including a CPP_EOF). */
556 if ((unsigned char *) &arg
->first
[ntokens
+ 2] > buff
->limit
)
558 buff
= _cpp_append_extend_buff (pfile
, buff
,
559 1000 * sizeof (cpp_token
*));
560 arg
->first
= (const cpp_token
**) buff
->cur
;
563 token
= cpp_get_token (pfile
);
565 if (token
->type
== CPP_PADDING
)
567 /* Drop leading padding. */
571 else if (token
->type
== CPP_OPEN_PAREN
)
573 else if (token
->type
== CPP_CLOSE_PAREN
)
575 if (paren_depth
-- == 0)
578 else if (token
->type
== CPP_COMMA
)
580 /* A comma does not terminate an argument within
581 parentheses or as part of a variable argument. */
583 && ! (macro
->variadic
&& argc
== macro
->paramc
))
586 else if (token
->type
== CPP_EOF
587 || (token
->type
== CPP_HASH
&& token
->flags
& BOL
))
590 arg
->first
[ntokens
++] = token
;
593 /* Drop trailing padding. */
594 while (ntokens
> 0 && arg
->first
[ntokens
- 1]->type
== CPP_PADDING
)
597 arg
->count
= ntokens
;
598 arg
->first
[ntokens
] = &pfile
->eof
;
600 /* Terminate the argument. Excess arguments loop back and
601 overwrite the final legitimate argument, before failing. */
602 if (argc
<= macro
->paramc
)
604 buff
->cur
= (unsigned char *) &arg
->first
[ntokens
+ 1];
605 if (argc
!= macro
->paramc
)
609 while (token
->type
!= CPP_CLOSE_PAREN
&& token
->type
!= CPP_EOF
);
611 if (token
->type
== CPP_EOF
)
613 /* We still need the CPP_EOF to end directives, and to end
614 pre-expansion of a macro argument. Step back is not
615 unconditional, since we don't want to return a CPP_EOF to our
616 callers at the end of an -include-d file. */
617 if (pfile
->context
->prev
|| pfile
->state
.in_directive
)
618 _cpp_backup_tokens (pfile
, 1);
619 cpp_error (pfile
, CPP_DL_ERROR
,
620 "unterminated argument list invoking macro \"%s\"",
625 /* A single empty argument is counted as no argument. */
626 if (argc
== 1 && macro
->paramc
== 0 && args
[0].count
== 0)
628 if (_cpp_arguments_ok (pfile
, macro
, node
, argc
))
630 /* GCC has special semantics for , ## b where b is a varargs
631 parameter: we remove the comma if b was omitted entirely.
632 If b was merely an empty argument, the comma is retained.
633 If the macro takes just one (varargs) parameter, then we
634 retain the comma only if we are standards conforming.
636 If FIRST is NULL replace_args () swallows the comma. */
637 if (macro
->variadic
&& (argc
< macro
->paramc
638 || (argc
== 1 && args
[0].count
== 0
639 && !CPP_OPTION (pfile
, std
))))
640 args
[macro
->paramc
- 1].first
= NULL
;
645 /* An error occurred. */
646 _cpp_release_buff (pfile
, base_buff
);
650 /* Search for an opening parenthesis to the macro of NODE, in such a
651 way that, if none is found, we don't lose the information in any
652 intervening padding tokens. If we find the parenthesis, collect
653 the arguments and return the buffer containing them. */
655 funlike_invocation_p (cpp_reader
*pfile
, cpp_hashnode
*node
)
657 const cpp_token
*token
, *padding
= NULL
;
661 token
= cpp_get_token (pfile
);
662 if (token
->type
!= CPP_PADDING
)
665 || (!(padding
->flags
& PREV_WHITE
) && token
->val
.source
== NULL
))
669 if (token
->type
== CPP_OPEN_PAREN
)
671 pfile
->state
.parsing_args
= 2;
672 return collect_args (pfile
, node
);
675 /* CPP_EOF can be the end of macro arguments, or the end of the
676 file. We mustn't back up over the latter. Ugh. */
677 if (token
->type
!= CPP_EOF
|| token
== &pfile
->eof
)
679 /* Back up. We may have skipped padding, in which case backing
680 up more than one token when expanding macros is in general
681 too difficult. We re-insert it in its own context. */
682 _cpp_backup_tokens (pfile
, 1);
684 _cpp_push_token_context (pfile
, NULL
, padding
, 1);
690 /* Push the context of a macro with hash entry NODE onto the context
691 stack. If we can successfully expand the macro, we push a context
692 containing its yet-to-be-rescanned replacement list and return one.
693 Otherwise, we don't push a context and return zero. */
695 enter_macro_context (cpp_reader
*pfile
, cpp_hashnode
*node
)
697 /* The presence of a macro invalidates a file's controlling macro. */
698 pfile
->mi_valid
= false;
700 pfile
->state
.angled_headers
= false;
702 /* Handle standard macros. */
703 if (! (node
->flags
& NODE_BUILTIN
))
705 cpp_macro
*macro
= node
->value
.macro
;
711 pfile
->state
.prevent_expansion
++;
712 pfile
->keep_tokens
++;
713 pfile
->state
.parsing_args
= 1;
714 buff
= funlike_invocation_p (pfile
, node
);
715 pfile
->state
.parsing_args
= 0;
716 pfile
->keep_tokens
--;
717 pfile
->state
.prevent_expansion
--;
721 if (CPP_WTRADITIONAL (pfile
) && ! node
->value
.macro
->syshdr
)
722 cpp_error (pfile
, CPP_DL_WARNING
,
723 "function-like macro \"%s\" must be used with arguments in traditional C",
729 if (macro
->paramc
> 0)
730 replace_args (pfile
, node
, macro
, (macro_arg
*) buff
->base
);
731 _cpp_release_buff (pfile
, buff
);
734 /* Disable the macro within its expansion. */
735 node
->flags
|= NODE_DISABLED
;
739 if (macro
->paramc
== 0)
740 _cpp_push_token_context (pfile
, node
, macro
->exp
.tokens
, macro
->count
);
745 /* Handle built-in macros and the _Pragma operator. */
746 return builtin_macro (pfile
, node
);
749 /* Replace the parameters in a function-like macro of NODE with the
750 actual ARGS, and place the result in a newly pushed token context.
751 Expand each argument before replacing, unless it is operated upon
752 by the # or ## operators. */
754 replace_args (cpp_reader
*pfile
, cpp_hashnode
*node
, cpp_macro
*macro
, macro_arg
*args
)
756 unsigned int i
, total
;
757 const cpp_token
*src
, *limit
;
758 const cpp_token
**dest
, **first
;
762 /* First, fully macro-expand arguments, calculating the number of
763 tokens in the final expansion as we go. The ordering of the if
764 statements below is subtle; we must handle stringification before
766 total
= macro
->count
;
767 limit
= macro
->exp
.tokens
+ macro
->count
;
769 for (src
= macro
->exp
.tokens
; src
< limit
; src
++)
770 if (src
->type
== CPP_MACRO_ARG
)
772 /* Leading and trailing padding tokens. */
775 /* We have an argument. If it is not being stringified or
776 pasted it is macro-replaced before insertion. */
777 arg
= &args
[src
->val
.arg_no
- 1];
779 if (src
->flags
& STRINGIFY_ARG
)
781 if (!arg
->stringified
)
782 arg
->stringified
= stringify_arg (pfile
, arg
);
784 else if ((src
->flags
& PASTE_LEFT
)
785 || (src
> macro
->exp
.tokens
&& (src
[-1].flags
& PASTE_LEFT
)))
786 total
+= arg
->count
- 1;
790 expand_arg (pfile
, arg
);
791 total
+= arg
->expanded_count
- 1;
795 /* Now allocate space for the expansion, copy the tokens and replace
797 buff
= _cpp_get_buff (pfile
, total
* sizeof (cpp_token
*));
798 first
= (const cpp_token
**) buff
->base
;
801 for (src
= macro
->exp
.tokens
; src
< limit
; src
++)
804 const cpp_token
**from
, **paste_flag
;
806 if (src
->type
!= CPP_MACRO_ARG
)
813 arg
= &args
[src
->val
.arg_no
- 1];
814 if (src
->flags
& STRINGIFY_ARG
)
815 count
= 1, from
= &arg
->stringified
;
816 else if (src
->flags
& PASTE_LEFT
)
817 count
= arg
->count
, from
= arg
->first
;
818 else if (src
!= macro
->exp
.tokens
&& (src
[-1].flags
& PASTE_LEFT
))
820 count
= arg
->count
, from
= arg
->first
;
823 if (dest
[-1]->type
== CPP_COMMA
825 && src
->val
.arg_no
== macro
->paramc
)
827 /* Swallow a pasted comma if from == NULL, otherwise
828 drop the paste flag. */
832 paste_flag
= dest
- 1;
834 /* Remove the paste flag if the RHS is a placemarker. */
836 paste_flag
= dest
- 1;
840 count
= arg
->expanded_count
, from
= arg
->expanded
;
842 /* Padding on the left of an argument (unless RHS of ##). */
843 if ((!pfile
->state
.in_directive
|| pfile
->state
.directive_wants_padding
)
844 && src
!= macro
->exp
.tokens
&& !(src
[-1].flags
& PASTE_LEFT
))
845 *dest
++ = padding_token (pfile
, src
);
849 memcpy (dest
, from
, count
* sizeof (cpp_token
*));
852 /* With a non-empty argument on the LHS of ##, the last
853 token should be flagged PASTE_LEFT. */
854 if (src
->flags
& PASTE_LEFT
)
855 paste_flag
= dest
- 1;
858 /* Avoid paste on RHS (even case count == 0). */
859 if (!pfile
->state
.in_directive
&& !(src
->flags
& PASTE_LEFT
))
860 *dest
++ = &pfile
->avoid_paste
;
862 /* Add a new paste flag, or remove an unwanted one. */
865 cpp_token
*token
= _cpp_temp_token (pfile
);
866 token
->type
= (*paste_flag
)->type
;
867 token
->val
= (*paste_flag
)->val
;
868 if (src
->flags
& PASTE_LEFT
)
869 token
->flags
= (*paste_flag
)->flags
| PASTE_LEFT
;
871 token
->flags
= (*paste_flag
)->flags
& ~PASTE_LEFT
;
876 /* Free the expanded arguments. */
877 for (i
= 0; i
< macro
->paramc
; i
++)
878 if (args
[i
].expanded
)
879 free (args
[i
].expanded
);
881 push_ptoken_context (pfile
, node
, buff
, first
, dest
- first
);
884 /* Return a special padding token, with padding inherited from SOURCE. */
885 static const cpp_token
*
886 padding_token (cpp_reader
*pfile
, const cpp_token
*source
)
888 cpp_token
*result
= _cpp_temp_token (pfile
);
890 result
->type
= CPP_PADDING
;
892 /* Data in GCed data structures cannot be made const so far, so we
894 result
->val
.source
= (cpp_token
*) source
;
899 /* Get a new uninitialized context. Create a new one if we cannot
900 re-use an old one. */
902 next_context (cpp_reader
*pfile
)
904 cpp_context
*result
= pfile
->context
->next
;
908 result
= XNEW (cpp_context
);
909 result
->prev
= pfile
->context
;
911 pfile
->context
->next
= result
;
914 pfile
->context
= result
;
918 /* Push a list of pointers to tokens. */
920 push_ptoken_context (cpp_reader
*pfile
, cpp_hashnode
*macro
, _cpp_buff
*buff
,
921 const cpp_token
**first
, unsigned int count
)
923 cpp_context
*context
= next_context (pfile
);
925 context
->direct_p
= false;
926 context
->macro
= macro
;
927 context
->buff
= buff
;
928 FIRST (context
).ptoken
= first
;
929 LAST (context
).ptoken
= first
+ count
;
932 /* Push a list of tokens. */
934 _cpp_push_token_context (cpp_reader
*pfile
, cpp_hashnode
*macro
,
935 const cpp_token
*first
, unsigned int count
)
937 cpp_context
*context
= next_context (pfile
);
939 context
->direct_p
= true;
940 context
->macro
= macro
;
941 context
->buff
= NULL
;
942 FIRST (context
).token
= first
;
943 LAST (context
).token
= first
+ count
;
946 /* Push a traditional macro's replacement text. */
948 _cpp_push_text_context (cpp_reader
*pfile
, cpp_hashnode
*macro
,
949 const uchar
*start
, size_t len
)
951 cpp_context
*context
= next_context (pfile
);
953 context
->direct_p
= true;
954 context
->macro
= macro
;
955 context
->buff
= NULL
;
956 CUR (context
) = start
;
957 RLIMIT (context
) = start
+ len
;
958 macro
->flags
|= NODE_DISABLED
;
961 /* Expand an argument ARG before replacing parameters in a
962 function-like macro. This works by pushing a context with the
963 argument's tokens, and then expanding that into a temporary buffer
964 as if it were a normal part of the token stream. collect_args()
965 has terminated the argument's tokens with a CPP_EOF so that we know
966 when we have fully expanded the argument. */
968 expand_arg (cpp_reader
*pfile
, macro_arg
*arg
)
970 unsigned int capacity
;
971 bool saved_warn_trad
;
976 /* Don't warn about funlike macros when pre-expanding. */
977 saved_warn_trad
= CPP_WTRADITIONAL (pfile
);
978 CPP_WTRADITIONAL (pfile
) = 0;
980 /* Loop, reading in the arguments. */
982 arg
->expanded
= XNEWVEC (const cpp_token
*, capacity
);
984 push_ptoken_context (pfile
, NULL
, NULL
, arg
->first
, arg
->count
+ 1);
987 const cpp_token
*token
;
989 if (arg
->expanded_count
+ 1 >= capacity
)
992 arg
->expanded
= XRESIZEVEC (const cpp_token
*, arg
->expanded
,
996 token
= cpp_get_token (pfile
);
998 if (token
->type
== CPP_EOF
)
1001 arg
->expanded
[arg
->expanded_count
++] = token
;
1004 _cpp_pop_context (pfile
);
1006 CPP_WTRADITIONAL (pfile
) = saved_warn_trad
;
1009 /* Pop the current context off the stack, re-enabling the macro if the
1010 context represented a macro's replacement list. The context
1011 structure is not freed so that we can re-use it later. */
1013 _cpp_pop_context (cpp_reader
*pfile
)
1015 cpp_context
*context
= pfile
->context
;
1018 context
->macro
->flags
&= ~NODE_DISABLED
;
1021 _cpp_release_buff (pfile
, context
->buff
);
1023 pfile
->context
= context
->prev
;
1026 /* External routine to get a token. Also used nearly everywhere
1027 internally, except for places where we know we can safely call
1028 _cpp_lex_token directly, such as lexing a directive name.
1030 Macro expansions and directives are transparently handled,
1031 including entering included files. Thus tokens are post-macro
1032 expansion, and after any intervening directives. External callers
1033 see CPP_EOF only at EOF. Internal callers also see it when meeting
1034 a directive inside a macro call, when at the end of a directive and
1035 state.in_directive is still 1, and at the end of argument
1038 cpp_get_token (cpp_reader
*pfile
)
1040 const cpp_token
*result
;
1045 cpp_context
*context
= pfile
->context
;
1047 /* Context->prev == 0 <=> base context. */
1049 result
= _cpp_lex_token (pfile
);
1050 else if (FIRST (context
).token
!= LAST (context
).token
)
1052 if (context
->direct_p
)
1053 result
= FIRST (context
).token
++;
1055 result
= *FIRST (context
).ptoken
++;
1057 if (result
->flags
& PASTE_LEFT
)
1059 paste_all_tokens (pfile
, result
);
1060 if (pfile
->state
.in_directive
)
1062 return padding_token (pfile
, result
);
1067 _cpp_pop_context (pfile
);
1068 if (pfile
->state
.in_directive
)
1070 return &pfile
->avoid_paste
;
1073 if (pfile
->state
.in_directive
&& result
->type
== CPP_COMMENT
)
1076 if (result
->type
!= CPP_NAME
)
1079 node
= result
->val
.node
;
1081 if (node
->type
!= NT_MACRO
|| (result
->flags
& NO_EXPAND
))
1084 if (!(node
->flags
& NODE_DISABLED
))
1086 if (!pfile
->state
.prevent_expansion
1087 && enter_macro_context (pfile
, node
))
1089 if (pfile
->state
.in_directive
)
1091 return padding_token (pfile
, result
);
1096 /* Flag this token as always unexpandable. FIXME: move this
1097 to collect_args()?. */
1098 cpp_token
*t
= _cpp_temp_token (pfile
);
1099 t
->type
= result
->type
;
1100 t
->flags
= result
->flags
| NO_EXPAND
;
1101 t
->val
= result
->val
;
1111 /* Returns true if we're expanding an object-like macro that was
1112 defined in a system header. Just checks the macro at the top of
1113 the stack. Used for diagnostic suppression. */
1115 cpp_sys_macro_p (cpp_reader
*pfile
)
1117 cpp_hashnode
*node
= pfile
->context
->macro
;
1119 return node
&& node
->value
.macro
&& node
->value
.macro
->syshdr
;
1122 /* Read each token in, until end of the current file. Directives are
1123 transparently processed. */
1125 cpp_scan_nooutput (cpp_reader
*pfile
)
1127 /* Request a CPP_EOF token at the end of this file, rather than
1128 transparently continuing with the including file. */
1129 pfile
->buffer
->return_at_eof
= true;
1131 pfile
->state
.discarding_output
++;
1132 pfile
->state
.prevent_expansion
++;
1134 if (CPP_OPTION (pfile
, traditional
))
1135 while (_cpp_read_logical_line_trad (pfile
))
1138 while (cpp_get_token (pfile
)->type
!= CPP_EOF
)
1141 pfile
->state
.discarding_output
--;
1142 pfile
->state
.prevent_expansion
--;
1145 /* Step back one (or more) tokens. Can only step mack more than 1 if
1146 they are from the lexer, and not from macro expansion. */
1148 _cpp_backup_tokens (cpp_reader
*pfile
, unsigned int count
)
1150 if (pfile
->context
->prev
== NULL
)
1152 pfile
->lookaheads
+= count
;
1156 if (pfile
->cur_token
== pfile
->cur_run
->base
1157 /* Possible with -fpreprocessed and no leading #line. */
1158 && pfile
->cur_run
->prev
!= NULL
)
1160 pfile
->cur_run
= pfile
->cur_run
->prev
;
1161 pfile
->cur_token
= pfile
->cur_run
->limit
;
1169 if (pfile
->context
->direct_p
)
1170 FIRST (pfile
->context
).token
--;
1172 FIRST (pfile
->context
).ptoken
--;
1176 /* #define directive parsing and handling. */
1178 /* Returns nonzero if a macro redefinition warning is required. */
1180 warn_of_redefinition (cpp_reader
*pfile
, const cpp_hashnode
*node
,
1181 const cpp_macro
*macro2
)
1183 const cpp_macro
*macro1
;
1186 /* Some redefinitions need to be warned about regardless. */
1187 if (node
->flags
& NODE_WARN
)
1190 /* Redefinition of a macro is allowed if and only if the old and new
1191 definitions are the same. (6.10.3 paragraph 2). */
1192 macro1
= node
->value
.macro
;
1194 /* Don't check count here as it can be different in valid
1195 traditional redefinitions with just whitespace differences. */
1196 if (macro1
->paramc
!= macro2
->paramc
1197 || macro1
->fun_like
!= macro2
->fun_like
1198 || macro1
->variadic
!= macro2
->variadic
)
1201 /* Check parameter spellings. */
1202 for (i
= 0; i
< macro1
->paramc
; i
++)
1203 if (macro1
->params
[i
] != macro2
->params
[i
])
1206 /* Check the replacement text or tokens. */
1207 if (CPP_OPTION (pfile
, traditional
))
1208 return _cpp_expansions_different_trad (macro1
, macro2
);
1210 if (macro1
->count
!= macro2
->count
)
1213 for (i
= 0; i
< macro1
->count
; i
++)
1214 if (!_cpp_equiv_tokens (¯o1
->exp
.tokens
[i
], ¯o2
->exp
.tokens
[i
]))
1220 /* Free the definition of hashnode H. */
1222 _cpp_free_definition (cpp_hashnode
*h
)
1224 /* Macros and assertions no longer have anything to free. */
1226 /* Clear builtin flag in case of redefinition. */
1227 h
->flags
&= ~(NODE_BUILTIN
| NODE_DISABLED
);
1230 /* Save parameter NODE to the parameter list of macro MACRO. Returns
1231 zero on success, nonzero if the parameter is a duplicate. */
1233 _cpp_save_parameter (cpp_reader
*pfile
, cpp_macro
*macro
, cpp_hashnode
*node
)
1236 /* Constraint 6.10.3.6 - duplicate parameter names. */
1237 if (node
->flags
& NODE_MACRO_ARG
)
1239 cpp_error (pfile
, CPP_DL_ERROR
, "duplicate macro parameter \"%s\"",
1244 if (BUFF_ROOM (pfile
->a_buff
)
1245 < (macro
->paramc
+ 1) * sizeof (cpp_hashnode
*))
1246 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (cpp_hashnode
*));
1248 ((cpp_hashnode
**) BUFF_FRONT (pfile
->a_buff
))[macro
->paramc
++] = node
;
1249 node
->flags
|= NODE_MACRO_ARG
;
1250 len
= macro
->paramc
* sizeof (union _cpp_hashnode_value
);
1251 if (len
> pfile
->macro_buffer_len
)
1253 pfile
->macro_buffer
= XRESIZEVEC (unsigned char, pfile
->macro_buffer
,
1255 pfile
->macro_buffer_len
= len
;
1257 ((union _cpp_hashnode_value
*) pfile
->macro_buffer
)[macro
->paramc
- 1]
1260 node
->value
.arg_index
= macro
->paramc
;
1264 /* Check the syntax of the parameters in a MACRO definition. Returns
1265 false if an error occurs. */
1267 parse_params (cpp_reader
*pfile
, cpp_macro
*macro
)
1269 unsigned int prev_ident
= 0;
1273 const cpp_token
*token
= _cpp_lex_token (pfile
);
1275 switch (token
->type
)
1278 /* Allow/ignore comments in parameter lists if we are
1279 preserving comments in macro expansions. */
1280 if (token
->type
== CPP_COMMENT
1281 && ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
))
1284 cpp_error (pfile
, CPP_DL_ERROR
,
1285 "\"%s\" may not appear in macro parameter list",
1286 cpp_token_as_text (pfile
, token
));
1292 cpp_error (pfile
, CPP_DL_ERROR
,
1293 "macro parameters must be comma-separated");
1298 if (_cpp_save_parameter (pfile
, macro
, token
->val
.node
))
1302 case CPP_CLOSE_PAREN
:
1303 if (prev_ident
|| macro
->paramc
== 0)
1306 /* Fall through to pick up the error. */
1310 cpp_error (pfile
, CPP_DL_ERROR
, "parameter name missing");
1317 macro
->variadic
= 1;
1320 _cpp_save_parameter (pfile
, macro
,
1321 pfile
->spec_nodes
.n__VA_ARGS__
);
1322 pfile
->state
.va_args_ok
= 1;
1323 if (! CPP_OPTION (pfile
, c99
)
1324 && CPP_OPTION (pfile
, pedantic
)
1325 && CPP_OPTION (pfile
, warn_variadic_macros
))
1326 cpp_error (pfile
, CPP_DL_PEDWARN
,
1327 "anonymous variadic macros were introduced in C99");
1329 else if (CPP_OPTION (pfile
, pedantic
)
1330 && CPP_OPTION (pfile
, warn_variadic_macros
))
1331 cpp_error (pfile
, CPP_DL_PEDWARN
,
1332 "ISO C does not permit named variadic macros");
1334 /* We're at the end, and just expect a closing parenthesis. */
1335 token
= _cpp_lex_token (pfile
);
1336 if (token
->type
== CPP_CLOSE_PAREN
)
1341 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' in macro parameter list");
1347 /* Allocate room for a token from a macro's replacement list. */
1349 alloc_expansion_token (cpp_reader
*pfile
, cpp_macro
*macro
)
1351 if (BUFF_ROOM (pfile
->a_buff
) < (macro
->count
+ 1) * sizeof (cpp_token
))
1352 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (cpp_token
));
1354 return &((cpp_token
*) BUFF_FRONT (pfile
->a_buff
))[macro
->count
++];
1357 /* Lex a token from the expansion of MACRO, but mark parameters as we
1358 find them and warn of traditional stringification. */
1360 lex_expansion_token (cpp_reader
*pfile
, cpp_macro
*macro
)
1364 pfile
->cur_token
= alloc_expansion_token (pfile
, macro
);
1365 token
= _cpp_lex_direct (pfile
);
1367 /* Is this a parameter? */
1368 if (token
->type
== CPP_NAME
1369 && (token
->val
.node
->flags
& NODE_MACRO_ARG
) != 0)
1371 token
->type
= CPP_MACRO_ARG
;
1372 token
->val
.arg_no
= token
->val
.node
->value
.arg_index
;
1374 else if (CPP_WTRADITIONAL (pfile
) && macro
->paramc
> 0
1375 && (token
->type
== CPP_STRING
|| token
->type
== CPP_CHAR
))
1376 check_trad_stringification (pfile
, macro
, &token
->val
.str
);
1382 create_iso_definition (cpp_reader
*pfile
, cpp_macro
*macro
)
1385 const cpp_token
*ctoken
;
1387 /* Get the first token of the expansion (or the '(' of a
1388 function-like macro). */
1389 ctoken
= _cpp_lex_token (pfile
);
1391 if (ctoken
->type
== CPP_OPEN_PAREN
&& !(ctoken
->flags
& PREV_WHITE
))
1393 bool ok
= parse_params (pfile
, macro
);
1394 macro
->params
= (cpp_hashnode
**) BUFF_FRONT (pfile
->a_buff
);
1398 /* Success. Commit or allocate the parameter array. */
1399 if (pfile
->hash_table
->alloc_subobject
)
1401 cpp_hashnode
**params
=
1402 (cpp_hashnode
**) pfile
->hash_table
->alloc_subobject
1403 (sizeof (cpp_hashnode
*) * macro
->paramc
);
1404 memcpy (params
, macro
->params
,
1405 sizeof (cpp_hashnode
*) * macro
->paramc
);
1406 macro
->params
= params
;
1409 BUFF_FRONT (pfile
->a_buff
) = (uchar
*) ¯o
->params
[macro
->paramc
];
1410 macro
->fun_like
= 1;
1412 else if (ctoken
->type
!= CPP_EOF
&& !(ctoken
->flags
& PREV_WHITE
))
1414 /* While ISO C99 requires whitespace before replacement text
1415 in a macro definition, ISO C90 with TC1 allows there characters
1416 from the basic source character set. */
1417 if (CPP_OPTION (pfile
, c99
))
1418 cpp_error (pfile
, CPP_DL_PEDWARN
,
1419 "ISO C99 requires whitespace after the macro name");
1422 int warntype
= CPP_DL_WARNING
;
1423 switch (ctoken
->type
)
1427 case CPP_OBJC_STRING
:
1428 /* '@' is not in basic character set. */
1429 warntype
= CPP_DL_PEDWARN
;
1432 /* Basic character set sans letters, digits and _. */
1433 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
1434 ctoken
->val
.str
.text
[0]) == NULL
)
1435 warntype
= CPP_DL_PEDWARN
;
1438 /* All other tokens start with a character from basic
1442 cpp_error (pfile
, warntype
,
1443 "missing whitespace after the macro name");
1447 if (macro
->fun_like
)
1448 token
= lex_expansion_token (pfile
, macro
);
1451 token
= alloc_expansion_token (pfile
, macro
);
1457 /* Check the stringifying # constraint 6.10.3.2.1 of
1458 function-like macros when lexing the subsequent token. */
1459 if (macro
->count
> 1 && token
[-1].type
== CPP_HASH
&& macro
->fun_like
)
1461 if (token
->type
== CPP_MACRO_ARG
)
1463 token
->flags
&= ~PREV_WHITE
;
1464 token
->flags
|= STRINGIFY_ARG
;
1465 token
->flags
|= token
[-1].flags
& PREV_WHITE
;
1466 token
[-1] = token
[0];
1469 /* Let assembler get away with murder. */
1470 else if (CPP_OPTION (pfile
, lang
) != CLK_ASM
)
1472 cpp_error (pfile
, CPP_DL_ERROR
,
1473 "'#' is not followed by a macro parameter");
1478 if (token
->type
== CPP_EOF
)
1481 /* Paste operator constraint 6.10.3.3.1. */
1482 if (token
->type
== CPP_PASTE
)
1484 /* Token-paste ##, can appear in both object-like and
1485 function-like macros, but not at the ends. */
1486 if (--macro
->count
> 0)
1487 token
= lex_expansion_token (pfile
, macro
);
1489 if (macro
->count
== 0 || token
->type
== CPP_EOF
)
1491 cpp_error (pfile
, CPP_DL_ERROR
,
1492 "'##' cannot appear at either end of a macro expansion");
1496 token
[-1].flags
|= PASTE_LEFT
;
1499 token
= lex_expansion_token (pfile
, macro
);
1502 macro
->exp
.tokens
= (cpp_token
*) BUFF_FRONT (pfile
->a_buff
);
1503 macro
->traditional
= 0;
1505 /* Don't count the CPP_EOF. */
1508 /* Clear whitespace on first token for warn_of_redefinition(). */
1510 macro
->exp
.tokens
[0].flags
&= ~PREV_WHITE
;
1512 /* Commit or allocate the memory. */
1513 if (pfile
->hash_table
->alloc_subobject
)
1516 (cpp_token
*) pfile
->hash_table
->alloc_subobject (sizeof (cpp_token
)
1518 memcpy (tokns
, macro
->exp
.tokens
, sizeof (cpp_token
) * macro
->count
);
1519 macro
->exp
.tokens
= tokns
;
1522 BUFF_FRONT (pfile
->a_buff
) = (uchar
*) ¯o
->exp
.tokens
[macro
->count
];
1527 /* Parse a macro and save its expansion. Returns nonzero on success. */
1529 _cpp_create_definition (cpp_reader
*pfile
, cpp_hashnode
*node
)
1535 if (pfile
->hash_table
->alloc_subobject
)
1536 macro
= (cpp_macro
*) pfile
->hash_table
->alloc_subobject
1537 (sizeof (cpp_macro
));
1539 macro
= (cpp_macro
*) _cpp_aligned_alloc (pfile
, sizeof (cpp_macro
));
1540 macro
->line
= pfile
->directive_line
;
1543 macro
->variadic
= 0;
1544 macro
->used
= !CPP_OPTION (pfile
, warn_unused_macros
);
1546 macro
->fun_like
= 0;
1547 /* To suppress some diagnostics. */
1548 macro
->syshdr
= pfile
->buffer
&& pfile
->buffer
->sysp
!= 0;
1550 if (CPP_OPTION (pfile
, traditional
))
1551 ok
= _cpp_create_trad_definition (pfile
, macro
);
1554 cpp_token
*saved_cur_token
= pfile
->cur_token
;
1556 ok
= create_iso_definition (pfile
, macro
);
1558 /* Restore lexer position because of games lex_expansion_token()
1559 plays lexing the macro. We set the type for SEEN_EOL() in
1562 Longer term we should lex the whole line before coming here,
1563 and just copy the expansion. */
1564 saved_cur_token
[-1].type
= pfile
->cur_token
[-1].type
;
1565 pfile
->cur_token
= saved_cur_token
;
1567 /* Stop the lexer accepting __VA_ARGS__. */
1568 pfile
->state
.va_args_ok
= 0;
1571 /* Clear the fast argument lookup indices. */
1572 for (i
= macro
->paramc
; i
-- > 0; )
1574 struct cpp_hashnode
*node
= macro
->params
[i
];
1575 node
->flags
&= ~ NODE_MACRO_ARG
;
1576 node
->value
= ((union _cpp_hashnode_value
*) pfile
->macro_buffer
)[i
];
1582 if (node
->type
== NT_MACRO
)
1584 if (CPP_OPTION (pfile
, warn_unused_macros
))
1585 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
1587 if (warn_of_redefinition (pfile
, node
, macro
))
1589 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, pfile
->directive_line
, 0,
1590 "\"%s\" redefined", NODE_NAME (node
));
1592 if (node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
))
1593 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
,
1594 node
->value
.macro
->line
, 0,
1595 "this is the location of the previous definition");
1599 if (node
->type
!= NT_VOID
)
1600 _cpp_free_definition (node
);
1602 /* Enter definition in hash table. */
1603 node
->type
= NT_MACRO
;
1604 node
->value
.macro
= macro
;
1605 if (! ustrncmp (NODE_NAME (node
), DSC ("__STDC_")))
1606 node
->flags
|= NODE_WARN
;
1611 /* Warn if a token in STRING matches one of a function-like MACRO's
1614 check_trad_stringification (cpp_reader
*pfile
, const cpp_macro
*macro
,
1615 const cpp_string
*string
)
1617 unsigned int i
, len
;
1618 const uchar
*p
, *q
, *limit
;
1620 /* Loop over the string. */
1621 limit
= string
->text
+ string
->len
- 1;
1622 for (p
= string
->text
+ 1; p
< limit
; p
= q
)
1624 /* Find the start of an identifier. */
1625 while (p
< limit
&& !is_idstart (*p
))
1628 /* Find the end of the identifier. */
1630 while (q
< limit
&& is_idchar (*q
))
1635 /* Loop over the function macro arguments to see if the
1636 identifier inside the string matches one of them. */
1637 for (i
= 0; i
< macro
->paramc
; i
++)
1639 const cpp_hashnode
*node
= macro
->params
[i
];
1641 if (NODE_LEN (node
) == len
1642 && !memcmp (p
, NODE_NAME (node
), len
))
1644 cpp_error (pfile
, CPP_DL_WARNING
,
1645 "macro argument \"%s\" would be stringified in traditional C",
1653 /* Returns the name, arguments and expansion of a macro, in a format
1654 suitable to be read back in again, and therefore also for DWARF 2
1655 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1656 Caller is expected to generate the "#define" bit if needed. The
1657 returned text is temporary, and automatically freed later. */
1658 const unsigned char *
1659 cpp_macro_definition (cpp_reader
*pfile
, const cpp_hashnode
*node
)
1661 unsigned int i
, len
;
1662 const cpp_macro
*macro
= node
->value
.macro
;
1663 unsigned char *buffer
;
1665 if (node
->type
!= NT_MACRO
|| (node
->flags
& NODE_BUILTIN
))
1667 cpp_error (pfile
, CPP_DL_ICE
,
1668 "invalid hash type %d in cpp_macro_definition", node
->type
);
1672 /* Calculate length. */
1673 len
= NODE_LEN (node
) + 2; /* ' ' and NUL. */
1674 if (macro
->fun_like
)
1676 len
+= 4; /* "()" plus possible final ".." of named
1677 varargs (we have + 1 below). */
1678 for (i
= 0; i
< macro
->paramc
; i
++)
1679 len
+= NODE_LEN (macro
->params
[i
]) + 1; /* "," */
1682 /* This should match below where we fill in the buffer. */
1683 if (CPP_OPTION (pfile
, traditional
))
1684 len
+= _cpp_replacement_text_len (macro
);
1687 for (i
= 0; i
< macro
->count
; i
++)
1689 cpp_token
*token
= ¯o
->exp
.tokens
[i
];
1691 if (token
->type
== CPP_MACRO_ARG
)
1692 len
+= NODE_LEN (macro
->params
[token
->val
.arg_no
- 1]);
1694 len
+= cpp_token_len (token
);
1696 if (token
->flags
& STRINGIFY_ARG
)
1698 if (token
->flags
& PASTE_LEFT
)
1699 len
+= 3; /* " ##" */
1700 if (token
->flags
& PREV_WHITE
)
1705 if (len
> pfile
->macro_buffer_len
)
1707 pfile
->macro_buffer
= XRESIZEVEC (unsigned char,
1708 pfile
->macro_buffer
, len
);
1709 pfile
->macro_buffer_len
= len
;
1712 /* Fill in the buffer. Start with the macro name. */
1713 buffer
= pfile
->macro_buffer
;
1714 memcpy (buffer
, NODE_NAME (node
), NODE_LEN (node
));
1715 buffer
+= NODE_LEN (node
);
1717 /* Parameter names. */
1718 if (macro
->fun_like
)
1721 for (i
= 0; i
< macro
->paramc
; i
++)
1723 cpp_hashnode
*param
= macro
->params
[i
];
1725 if (param
!= pfile
->spec_nodes
.n__VA_ARGS__
)
1727 memcpy (buffer
, NODE_NAME (param
), NODE_LEN (param
));
1728 buffer
+= NODE_LEN (param
);
1731 if (i
+ 1 < macro
->paramc
)
1732 /* Don't emit a space after the comma here; we're trying
1733 to emit a Dwarf-friendly definition, and the Dwarf spec
1734 forbids spaces in the argument list. */
1736 else if (macro
->variadic
)
1737 *buffer
++ = '.', *buffer
++ = '.', *buffer
++ = '.';
1742 /* The Dwarf spec requires a space after the macro name, even if the
1743 definition is the empty string. */
1746 if (CPP_OPTION (pfile
, traditional
))
1747 buffer
= _cpp_copy_replacement_text (macro
, buffer
);
1748 else if (macro
->count
)
1749 /* Expansion tokens. */
1751 for (i
= 0; i
< macro
->count
; i
++)
1753 cpp_token
*token
= ¯o
->exp
.tokens
[i
];
1755 if (token
->flags
& PREV_WHITE
)
1757 if (token
->flags
& STRINGIFY_ARG
)
1760 if (token
->type
== CPP_MACRO_ARG
)
1763 NODE_NAME (macro
->params
[token
->val
.arg_no
- 1]),
1764 NODE_LEN (macro
->params
[token
->val
.arg_no
- 1]));
1765 buffer
+= NODE_LEN (macro
->params
[token
->val
.arg_no
- 1]);
1768 buffer
= cpp_spell_token (pfile
, token
, buffer
, false);
1770 if (token
->flags
& PASTE_LEFT
)
1775 /* Next has PREV_WHITE; see _cpp_create_definition. */
1781 return pfile
->macro_buffer
;