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 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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_token_context (cpp_reader
*, cpp_hashnode
*,
46 const cpp_token
*, unsigned int);
47 static void push_ptoken_context (cpp_reader
*, cpp_hashnode
*, _cpp_buff
*,
48 const cpp_token
**, unsigned int);
49 static _cpp_buff
*collect_args (cpp_reader
*, const cpp_hashnode
*);
50 static cpp_context
*next_context (cpp_reader
*);
51 static const cpp_token
*padding_token (cpp_reader
*, const cpp_token
*);
52 static void expand_arg (cpp_reader
*, macro_arg
*);
53 static const cpp_token
*new_string_token (cpp_reader
*, uchar
*, unsigned int);
54 static const cpp_token
*stringify_arg (cpp_reader
*, macro_arg
*);
55 static void paste_all_tokens (cpp_reader
*, const cpp_token
*);
56 static bool paste_tokens (cpp_reader
*, const cpp_token
**, const cpp_token
*);
57 static void replace_args (cpp_reader
*, cpp_hashnode
*, cpp_macro
*,
59 static _cpp_buff
*funlike_invocation_p (cpp_reader
*, cpp_hashnode
*);
60 static bool create_iso_definition (cpp_reader
*, cpp_macro
*);
62 /* #define directive parsing and handling. */
64 static cpp_token
*alloc_expansion_token (cpp_reader
*, cpp_macro
*);
65 static cpp_token
*lex_expansion_token (cpp_reader
*, cpp_macro
*);
66 static bool warn_of_redefinition (cpp_reader
*, const cpp_hashnode
*,
68 static bool parse_params (cpp_reader
*, cpp_macro
*);
69 static void check_trad_stringification (cpp_reader
*, const cpp_macro
*,
72 /* Emits a warning if NODE is a macro defined in the main file that
75 _cpp_warn_if_unused_macro (cpp_reader
*pfile
, cpp_hashnode
*node
,
76 void *v ATTRIBUTE_UNUSED
)
78 if (node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
))
80 cpp_macro
*macro
= node
->value
.macro
;
83 && MAIN_FILE_P (linemap_lookup (pfile
->line_table
, macro
->line
)))
84 cpp_error_with_line (pfile
, CPP_DL_WARNING
, macro
->line
, 0,
85 "macro \"%s\" is not used", NODE_NAME (node
));
91 /* Allocates and returns a CPP_STRING token, containing TEXT of length
92 LEN, after null-terminating it. TEXT must be in permanent storage. */
93 static const cpp_token
*
94 new_string_token (cpp_reader
*pfile
, unsigned char *text
, unsigned int len
)
96 cpp_token
*token
= _cpp_temp_token (pfile
);
99 token
->type
= CPP_STRING
;
100 token
->val
.str
.len
= len
;
101 token
->val
.str
.text
= text
;
106 static const char * const monthnames
[] =
108 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
109 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
112 /* Handle builtin macros like __FILE__, and push the resulting token
113 on the context stack. Also handles _Pragma, for which no new token
114 is created. Returns 1 if it generates a new token context, 0 to
115 return the token to the caller. */
117 _cpp_builtin_macro_text (cpp_reader
*pfile
, cpp_hashnode
*node
)
119 const struct line_map
*map
;
120 const uchar
*result
= NULL
;
121 unsigned int number
= 1;
123 switch (node
->value
.builtin
)
126 cpp_error (pfile
, CPP_DL_ICE
, "invalid built-in macro \"%s\"",
136 map
= linemap_lookup (pfile
->line_table
, pfile
->line_table
->highest_line
);
138 if (node
->value
.builtin
== BT_BASE_FILE
)
139 while (! MAIN_FILE_P (map
))
140 map
= INCLUDED_FROM (pfile
->line_table
, map
);
144 buf
= _cpp_unaligned_alloc (pfile
, len
* 4 + 3);
147 buf
= cpp_quote_string (buf
+ 1, (const unsigned char *) name
, len
);
153 case BT_INCLUDE_LEVEL
:
154 /* The line map depth counts the primary source as level 1, but
155 historically __INCLUDE_DEPTH__ has called the primary source
157 number
= pfile
->line_table
->depth
- 1;
161 map
= &pfile
->line_table
->maps
[pfile
->line_table
->used
-1];
162 /* If __LINE__ is embedded in a macro, it must expand to the
163 line of the macro's invocation, not its definition.
164 Otherwise things like assert() will not work properly. */
165 if (CPP_OPTION (pfile
, traditional
))
166 number
= pfile
->line_table
->highest_line
;
168 number
= pfile
->cur_token
[-1].src_loc
;
169 number
= SOURCE_LINE (map
, number
);
172 /* __STDC__ has the value 1 under normal circumstances.
173 However, if (a) we are in a system header, (b) the option
174 stdc_0_in_system_headers is true (set by target config), and
175 (c) we are not in strictly conforming mode, then it has the
179 if (cpp_in_system_header (pfile
)
180 && CPP_OPTION (pfile
, stdc_0_in_system_headers
)
181 && !CPP_OPTION (pfile
,std
))
190 if (pfile
->date
== NULL
)
192 /* Allocate __DATE__ and __TIME__ strings from permanent
193 storage. We only do this once, and don't generate them
194 at init time, because time() and localtime() are very
195 slow on some systems. */
197 struct tm
*tb
= NULL
;
199 /* (time_t) -1 is a legitimate value for "number of seconds
200 since the Epoch", so we have to do a little dance to
201 distinguish that from a genuine error. */
204 if (tt
!= (time_t)-1 || errno
== 0)
205 tb
= localtime (&tt
);
209 pfile
->date
= _cpp_unaligned_alloc (pfile
,
210 sizeof ("\"Oct 11 1347\""));
211 sprintf ((char *) pfile
->date
, "\"%s %2d %4d\"",
212 monthnames
[tb
->tm_mon
], tb
->tm_mday
,
215 pfile
->time
= _cpp_unaligned_alloc (pfile
,
216 sizeof ("\"12:34:56\""));
217 sprintf ((char *) pfile
->time
, "\"%02d:%02d:%02d\"",
218 tb
->tm_hour
, tb
->tm_min
, tb
->tm_sec
);
222 cpp_errno (pfile
, CPP_DL_WARNING
,
223 "could not determine date and time");
225 pfile
->date
= U
"\"??? ?? ????\"";
226 pfile
->time
= U
"\"??:??:??\"";
230 if (node
->value
.builtin
== BT_DATE
)
231 result
= pfile
->date
;
233 result
= pfile
->time
;
239 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
240 result
= _cpp_unaligned_alloc (pfile
, 21);
241 sprintf ((char *) result
, "%u", number
);
247 /* Convert builtin macros like __FILE__ to a token and push it on the
248 context stack. Also handles _Pragma, for which no new token is
249 created. Returns 1 if it generates a new token context, 0 to
250 return the token to the caller. */
252 builtin_macro (cpp_reader
*pfile
, cpp_hashnode
*node
)
258 if (node
->value
.builtin
== BT_PRAGMA
)
260 /* Don't interpret _Pragma within directives. The standard is
261 not clear on this, but to me this makes most sense. */
262 if (pfile
->state
.in_directive
)
265 _cpp_do__Pragma (pfile
);
269 buf
= _cpp_builtin_macro_text (pfile
, node
);
271 nbuf
= alloca (len
+ 1);
272 memcpy (nbuf
, buf
, len
);
275 cpp_push_buffer (pfile
, (uchar
*) nbuf
, len
, /* from_stage3 */ true);
276 _cpp_clean_line (pfile
);
278 /* Set pfile->cur_token as required by _cpp_lex_direct. */
279 pfile
->cur_token
= _cpp_temp_token (pfile
);
280 push_token_context (pfile
, NULL
, _cpp_lex_direct (pfile
), 1);
281 if (pfile
->buffer
->cur
!= pfile
->buffer
->rlimit
)
282 cpp_error (pfile
, CPP_DL_ICE
, "invalid built-in macro \"%s\"",
284 _cpp_pop_buffer (pfile
);
289 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
290 backslashes and double quotes. Non-printable characters are
291 converted to octal. DEST must be of sufficient size. Returns
292 a pointer to the end of the string. */
294 cpp_quote_string (uchar
*dest
, const uchar
*src
, unsigned int len
)
300 if (c
== '\\' || c
== '"')
311 sprintf ((char *) dest
, "\\%03o", c
);
320 /* Convert a token sequence ARG to a single string token according to
321 the rules of the ISO C #-operator. */
322 static const cpp_token
*
323 stringify_arg (cpp_reader
*pfile
, macro_arg
*arg
)
326 unsigned int i
, escape_it
, backslash_count
= 0;
327 const cpp_token
*source
= NULL
;
330 if (BUFF_ROOM (pfile
->u_buff
) < 3)
331 _cpp_extend_buff (pfile
, &pfile
->u_buff
, 3);
332 dest
= BUFF_FRONT (pfile
->u_buff
);
335 /* Loop, reading in the argument's tokens. */
336 for (i
= 0; i
< arg
->count
; i
++)
338 const cpp_token
*token
= arg
->first
[i
];
340 if (token
->type
== CPP_PADDING
)
343 source
= token
->val
.source
;
347 escape_it
= (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
348 || token
->type
== CPP_CHAR
|| token
->type
== CPP_WCHAR
);
350 /* Room for each char being written in octal, initial space and
351 final quote and NUL. */
352 len
= cpp_token_len (token
);
357 if ((size_t) (BUFF_LIMIT (pfile
->u_buff
) - dest
) < len
)
359 size_t len_so_far
= dest
- BUFF_FRONT (pfile
->u_buff
);
360 _cpp_extend_buff (pfile
, &pfile
->u_buff
, len
);
361 dest
= BUFF_FRONT (pfile
->u_buff
) + len_so_far
;
364 /* Leading white space? */
365 if (dest
- 1 != BUFF_FRONT (pfile
->u_buff
))
369 if (source
->flags
& PREV_WHITE
)
376 _cpp_buff
*buff
= _cpp_get_buff (pfile
, len
);
377 unsigned char *buf
= BUFF_FRONT (buff
);
378 len
= cpp_spell_token (pfile
, token
, buf
) - buf
;
379 dest
= cpp_quote_string (dest
, buf
, len
);
380 _cpp_release_buff (pfile
, buff
);
383 dest
= cpp_spell_token (pfile
, token
, dest
);
385 if (token
->type
== CPP_OTHER
&& token
->val
.str
.text
[0] == '\\')
391 /* Ignore the final \ of invalid string literals. */
392 if (backslash_count
& 1)
394 cpp_error (pfile
, CPP_DL_WARNING
,
395 "invalid string literal, ignoring final '\\'");
399 /* Commit the memory, including NUL, and return the token. */
401 len
= dest
- BUFF_FRONT (pfile
->u_buff
);
402 BUFF_FRONT (pfile
->u_buff
) = dest
+ 1;
403 return new_string_token (pfile
, dest
- len
, len
);
406 /* Try to paste two tokens. On success, return nonzero. In any
407 case, PLHS is updated to point to the pasted token, which is
408 guaranteed to not have the PASTE_LEFT flag set. */
410 paste_tokens (cpp_reader
*pfile
, const cpp_token
**plhs
, const cpp_token
*rhs
)
412 unsigned char *buf
, *end
;
413 const cpp_token
*lhs
;
418 len
= cpp_token_len (lhs
) + cpp_token_len (rhs
) + 1;
420 end
= cpp_spell_token (pfile
, lhs
, buf
);
422 /* Avoid comment headers, since they are still processed in stage 3.
423 It is simpler to insert a space here, rather than modifying the
424 lexer to ignore comments in some circumstances. Simply returning
425 false doesn't work, since we want to clear the PASTE_LEFT flag. */
426 if (lhs
->type
== CPP_DIV
&& rhs
->type
!= CPP_EQ
)
428 end
= cpp_spell_token (pfile
, rhs
, end
);
431 cpp_push_buffer (pfile
, buf
, end
- buf
, /* from_stage3 */ true);
432 _cpp_clean_line (pfile
);
434 /* Set pfile->cur_token as required by _cpp_lex_direct. */
435 pfile
->cur_token
= _cpp_temp_token (pfile
);
436 *plhs
= _cpp_lex_direct (pfile
);
437 valid
= pfile
->buffer
->cur
== pfile
->buffer
->rlimit
;
438 _cpp_pop_buffer (pfile
);
443 /* Handles an arbitrarily long sequence of ## operators, with initial
444 operand LHS. This implementation is left-associative,
445 non-recursive, and finishes a paste before handling succeeding
446 ones. If a paste fails, we back up to the RHS of the failing ##
447 operator before pushing the context containing the result of prior
448 successful pastes, with the effect that the RHS appears in the
449 output stream after the pasted LHS normally. */
451 paste_all_tokens (cpp_reader
*pfile
, const cpp_token
*lhs
)
453 const cpp_token
*rhs
;
454 cpp_context
*context
= pfile
->context
;
458 /* Take the token directly from the current context. We can do
459 this, because we are in the replacement list of either an
460 object-like macro, or a function-like macro with arguments
461 inserted. In either case, the constraints to #define
462 guarantee we have at least one more token. */
463 if (context
->direct_p
)
464 rhs
= FIRST (context
).token
++;
466 rhs
= *FIRST (context
).ptoken
++;
468 if (rhs
->type
== CPP_PADDING
)
471 if (!paste_tokens (pfile
, &lhs
, rhs
))
473 _cpp_backup_tokens (pfile
, 1);
475 /* Mandatory error for all apart from assembler. */
476 if (CPP_OPTION (pfile
, lang
) != CLK_ASM
)
477 cpp_error (pfile
, CPP_DL_ERROR
,
478 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
479 cpp_token_as_text (pfile
, lhs
),
480 cpp_token_as_text (pfile
, rhs
));
484 while (rhs
->flags
& PASTE_LEFT
);
486 /* Put the resulting token in its own context. */
487 push_token_context (pfile
, NULL
, lhs
, 1);
490 /* Returns TRUE if the number of arguments ARGC supplied in an
491 invocation of the MACRO referenced by NODE is valid. An empty
492 invocation to a macro with no parameters should pass ARGC as zero.
494 Note that MACRO cannot necessarily be deduced from NODE, in case
495 NODE was redefined whilst collecting arguments. */
497 _cpp_arguments_ok (cpp_reader
*pfile
, cpp_macro
*macro
, const cpp_hashnode
*node
, unsigned int argc
)
499 if (argc
== macro
->paramc
)
502 if (argc
< macro
->paramc
)
504 /* As an extension, a rest argument is allowed to not appear in
505 the invocation at all.
506 e.g. #define debug(format, args...) something
509 This is exactly the same as if there had been an empty rest
510 argument - debug("string", ). */
512 if (argc
+ 1 == macro
->paramc
&& macro
->variadic
)
514 if (CPP_PEDANTIC (pfile
) && ! macro
->syshdr
)
515 cpp_error (pfile
, CPP_DL_PEDWARN
,
516 "ISO C99 requires rest arguments to be used");
520 cpp_error (pfile
, CPP_DL_ERROR
,
521 "macro \"%s\" requires %u arguments, but only %u given",
522 NODE_NAME (node
), macro
->paramc
, argc
);
525 cpp_error (pfile
, CPP_DL_ERROR
,
526 "macro \"%s\" passed %u arguments, but takes just %u",
527 NODE_NAME (node
), argc
, macro
->paramc
);
532 /* Reads and returns the arguments to a function-like macro
533 invocation. Assumes the opening parenthesis has been processed.
534 If there is an error, emits an appropriate diagnostic and returns
535 NULL. Each argument is terminated by a CPP_EOF token, for the
536 future benefit of expand_arg(). */
538 collect_args (cpp_reader
*pfile
, const cpp_hashnode
*node
)
540 _cpp_buff
*buff
, *base_buff
;
542 macro_arg
*args
, *arg
;
543 const cpp_token
*token
;
546 macro
= node
->value
.macro
;
548 argc
= macro
->paramc
;
551 buff
= _cpp_get_buff (pfile
, argc
* (50 * sizeof (cpp_token
*)
552 + sizeof (macro_arg
)));
554 args
= (macro_arg
*) buff
->base
;
555 memset (args
, 0, argc
* sizeof (macro_arg
));
556 buff
->cur
= (unsigned char *) &args
[argc
];
557 arg
= args
, argc
= 0;
559 /* Collect the tokens making up each argument. We don't yet know
560 how many arguments have been supplied, whether too many or too
561 few. Hence the slightly bizarre usage of "argc" and "arg". */
564 unsigned int paren_depth
= 0;
565 unsigned int ntokens
= 0;
568 arg
->first
= (const cpp_token
**) buff
->cur
;
572 /* Require space for 2 new tokens (including a CPP_EOF). */
573 if ((unsigned char *) &arg
->first
[ntokens
+ 2] > buff
->limit
)
575 buff
= _cpp_append_extend_buff (pfile
, buff
,
576 1000 * sizeof (cpp_token
*));
577 arg
->first
= (const cpp_token
**) buff
->cur
;
580 token
= cpp_get_token (pfile
);
582 if (token
->type
== CPP_PADDING
)
584 /* Drop leading padding. */
588 else if (token
->type
== CPP_OPEN_PAREN
)
590 else if (token
->type
== CPP_CLOSE_PAREN
)
592 if (paren_depth
-- == 0)
595 else if (token
->type
== CPP_COMMA
)
597 /* A comma does not terminate an argument within
598 parentheses or as part of a variable argument. */
600 && ! (macro
->variadic
&& argc
== macro
->paramc
))
603 else if (token
->type
== CPP_EOF
604 || (token
->type
== CPP_HASH
&& token
->flags
& BOL
))
607 arg
->first
[ntokens
++] = token
;
610 /* Drop trailing padding. */
611 while (ntokens
> 0 && arg
->first
[ntokens
- 1]->type
== CPP_PADDING
)
614 arg
->count
= ntokens
;
615 arg
->first
[ntokens
] = &pfile
->eof
;
617 /* Terminate the argument. Excess arguments loop back and
618 overwrite the final legitimate argument, before failing. */
619 if (argc
<= macro
->paramc
)
621 buff
->cur
= (unsigned char *) &arg
->first
[ntokens
+ 1];
622 if (argc
!= macro
->paramc
)
626 while (token
->type
!= CPP_CLOSE_PAREN
&& token
->type
!= CPP_EOF
);
628 if (token
->type
== CPP_EOF
)
630 /* We still need the CPP_EOF to end directives, and to end
631 pre-expansion of a macro argument. Step back is not
632 unconditional, since we don't want to return a CPP_EOF to our
633 callers at the end of an -include-d file. */
634 if (pfile
->context
->prev
|| pfile
->state
.in_directive
)
635 _cpp_backup_tokens (pfile
, 1);
636 cpp_error (pfile
, CPP_DL_ERROR
,
637 "unterminated argument list invoking macro \"%s\"",
642 /* A single empty argument is counted as no argument. */
643 if (argc
== 1 && macro
->paramc
== 0 && args
[0].count
== 0)
645 if (_cpp_arguments_ok (pfile
, macro
, node
, argc
))
647 /* GCC has special semantics for , ## b where b is a varargs
648 parameter: we remove the comma if b was omitted entirely.
649 If b was merely an empty argument, the comma is retained.
650 If the macro takes just one (varargs) parameter, then we
651 retain the comma only if we are standards conforming.
653 If FIRST is NULL replace_args () swallows the comma. */
654 if (macro
->variadic
&& (argc
< macro
->paramc
655 || (argc
== 1 && args
[0].count
== 0
656 && !CPP_OPTION (pfile
, std
))))
657 args
[macro
->paramc
- 1].first
= NULL
;
662 /* An error occurred. */
663 _cpp_release_buff (pfile
, base_buff
);
667 /* Search for an opening parenthesis to the macro of NODE, in such a
668 way that, if none is found, we don't lose the information in any
669 intervening padding tokens. If we find the parenthesis, collect
670 the arguments and return the buffer containing them. */
672 funlike_invocation_p (cpp_reader
*pfile
, cpp_hashnode
*node
)
674 const cpp_token
*token
, *padding
= NULL
;
678 token
= cpp_get_token (pfile
);
679 if (token
->type
!= CPP_PADDING
)
682 || (!(padding
->flags
& PREV_WHITE
) && token
->val
.source
== NULL
))
686 if (token
->type
== CPP_OPEN_PAREN
)
688 pfile
->state
.parsing_args
= 2;
689 return collect_args (pfile
, node
);
692 /* CPP_EOF can be the end of macro arguments, or the end of the
693 file. We mustn't back up over the latter. Ugh. */
694 if (token
->type
!= CPP_EOF
|| token
== &pfile
->eof
)
696 /* Back up. We may have skipped padding, in which case backing
697 up more than one token when expanding macros is in general
698 too difficult. We re-insert it in its own context. */
699 _cpp_backup_tokens (pfile
, 1);
701 push_token_context (pfile
, NULL
, padding
, 1);
707 /* Push the context of a macro with hash entry NODE onto the context
708 stack. If we can successfully expand the macro, we push a context
709 containing its yet-to-be-rescanned replacement list and return one.
710 Otherwise, we don't push a context and return zero. */
712 enter_macro_context (cpp_reader
*pfile
, cpp_hashnode
*node
)
714 /* The presence of a macro invalidates a file's controlling macro. */
715 pfile
->mi_valid
= false;
717 pfile
->state
.angled_headers
= false;
719 /* Handle standard macros. */
720 if (! (node
->flags
& NODE_BUILTIN
))
722 cpp_macro
*macro
= node
->value
.macro
;
728 pfile
->state
.prevent_expansion
++;
729 pfile
->keep_tokens
++;
730 pfile
->state
.parsing_args
= 1;
731 buff
= funlike_invocation_p (pfile
, node
);
732 pfile
->state
.parsing_args
= 0;
733 pfile
->keep_tokens
--;
734 pfile
->state
.prevent_expansion
--;
738 if (CPP_WTRADITIONAL (pfile
) && ! node
->value
.macro
->syshdr
)
739 cpp_error (pfile
, CPP_DL_WARNING
,
740 "function-like macro \"%s\" must be used with arguments in traditional C",
746 if (macro
->paramc
> 0)
747 replace_args (pfile
, node
, macro
, (macro_arg
*) buff
->base
);
748 _cpp_release_buff (pfile
, buff
);
751 /* Disable the macro within its expansion. */
752 node
->flags
|= NODE_DISABLED
;
756 if (macro
->paramc
== 0)
757 push_token_context (pfile
, node
, macro
->exp
.tokens
, macro
->count
);
762 /* Handle built-in macros and the _Pragma operator. */
763 return builtin_macro (pfile
, node
);
766 /* Replace the parameters in a function-like macro of NODE with the
767 actual ARGS, and place the result in a newly pushed token context.
768 Expand each argument before replacing, unless it is operated upon
769 by the # or ## operators. */
771 replace_args (cpp_reader
*pfile
, cpp_hashnode
*node
, cpp_macro
*macro
, macro_arg
*args
)
773 unsigned int i
, total
;
774 const cpp_token
*src
, *limit
;
775 const cpp_token
**dest
, **first
;
779 /* First, fully macro-expand arguments, calculating the number of
780 tokens in the final expansion as we go. The ordering of the if
781 statements below is subtle; we must handle stringification before
783 total
= macro
->count
;
784 limit
= macro
->exp
.tokens
+ macro
->count
;
786 for (src
= macro
->exp
.tokens
; src
< limit
; src
++)
787 if (src
->type
== CPP_MACRO_ARG
)
789 /* Leading and trailing padding tokens. */
792 /* We have an argument. If it is not being stringified or
793 pasted it is macro-replaced before insertion. */
794 arg
= &args
[src
->val
.arg_no
- 1];
796 if (src
->flags
& STRINGIFY_ARG
)
798 if (!arg
->stringified
)
799 arg
->stringified
= stringify_arg (pfile
, arg
);
801 else if ((src
->flags
& PASTE_LEFT
)
802 || (src
> macro
->exp
.tokens
&& (src
[-1].flags
& PASTE_LEFT
)))
803 total
+= arg
->count
- 1;
807 expand_arg (pfile
, arg
);
808 total
+= arg
->expanded_count
- 1;
812 /* Now allocate space for the expansion, copy the tokens and replace
814 buff
= _cpp_get_buff (pfile
, total
* sizeof (cpp_token
*));
815 first
= (const cpp_token
**) buff
->base
;
818 for (src
= macro
->exp
.tokens
; src
< limit
; src
++)
821 const cpp_token
**from
, **paste_flag
;
823 if (src
->type
!= CPP_MACRO_ARG
)
830 arg
= &args
[src
->val
.arg_no
- 1];
831 if (src
->flags
& STRINGIFY_ARG
)
832 count
= 1, from
= &arg
->stringified
;
833 else if (src
->flags
& PASTE_LEFT
)
834 count
= arg
->count
, from
= arg
->first
;
835 else if (src
!= macro
->exp
.tokens
&& (src
[-1].flags
& PASTE_LEFT
))
837 count
= arg
->count
, from
= arg
->first
;
840 if (dest
[-1]->type
== CPP_COMMA
842 && src
->val
.arg_no
== macro
->paramc
)
844 /* Swallow a pasted comma if from == NULL, otherwise
845 drop the paste flag. */
849 paste_flag
= dest
- 1;
851 /* Remove the paste flag if the RHS is a placemarker. */
853 paste_flag
= dest
- 1;
857 count
= arg
->expanded_count
, from
= arg
->expanded
;
859 /* Padding on the left of an argument (unless RHS of ##). */
860 if ((!pfile
->state
.in_directive
|| pfile
->state
.directive_wants_padding
)
861 && src
!= macro
->exp
.tokens
&& !(src
[-1].flags
& PASTE_LEFT
))
862 *dest
++ = padding_token (pfile
, src
);
866 memcpy (dest
, from
, count
* sizeof (cpp_token
*));
869 /* With a non-empty argument on the LHS of ##, the last
870 token should be flagged PASTE_LEFT. */
871 if (src
->flags
& PASTE_LEFT
)
872 paste_flag
= dest
- 1;
875 /* Avoid paste on RHS (even case count == 0). */
876 if (!pfile
->state
.in_directive
&& !(src
->flags
& PASTE_LEFT
))
877 *dest
++ = &pfile
->avoid_paste
;
879 /* Add a new paste flag, or remove an unwanted one. */
882 cpp_token
*token
= _cpp_temp_token (pfile
);
883 token
->type
= (*paste_flag
)->type
;
884 token
->val
.str
= (*paste_flag
)->val
.str
;
885 if (src
->flags
& PASTE_LEFT
)
886 token
->flags
= (*paste_flag
)->flags
| PASTE_LEFT
;
888 token
->flags
= (*paste_flag
)->flags
& ~PASTE_LEFT
;
893 /* Free the expanded arguments. */
894 for (i
= 0; i
< macro
->paramc
; i
++)
895 if (args
[i
].expanded
)
896 free (args
[i
].expanded
);
898 push_ptoken_context (pfile
, node
, buff
, first
, dest
- first
);
901 /* Return a special padding token, with padding inherited from SOURCE. */
902 static const cpp_token
*
903 padding_token (cpp_reader
*pfile
, const cpp_token
*source
)
905 cpp_token
*result
= _cpp_temp_token (pfile
);
907 result
->type
= CPP_PADDING
;
908 result
->val
.source
= source
;
913 /* Get a new uninitialized context. Create a new one if we cannot
914 re-use an old one. */
916 next_context (cpp_reader
*pfile
)
918 cpp_context
*result
= pfile
->context
->next
;
922 result
= xnew (cpp_context
);
923 result
->prev
= pfile
->context
;
925 pfile
->context
->next
= result
;
928 pfile
->context
= result
;
932 /* Push a list of pointers to tokens. */
934 push_ptoken_context (cpp_reader
*pfile
, cpp_hashnode
*macro
, _cpp_buff
*buff
,
935 const cpp_token
**first
, unsigned int count
)
937 cpp_context
*context
= next_context (pfile
);
939 context
->direct_p
= false;
940 context
->macro
= macro
;
941 context
->buff
= buff
;
942 FIRST (context
).ptoken
= first
;
943 LAST (context
).ptoken
= first
+ count
;
946 /* Push a list of tokens. */
948 push_token_context (cpp_reader
*pfile
, cpp_hashnode
*macro
,
949 const cpp_token
*first
, unsigned int count
)
951 cpp_context
*context
= next_context (pfile
);
953 context
->direct_p
= true;
954 context
->macro
= macro
;
955 context
->buff
= NULL
;
956 FIRST (context
).token
= first
;
957 LAST (context
).token
= first
+ count
;
960 /* Push a traditional macro's replacement text. */
962 _cpp_push_text_context (cpp_reader
*pfile
, cpp_hashnode
*macro
,
963 const uchar
*start
, size_t len
)
965 cpp_context
*context
= next_context (pfile
);
967 context
->direct_p
= true;
968 context
->macro
= macro
;
969 context
->buff
= NULL
;
970 CUR (context
) = start
;
971 RLIMIT (context
) = start
+ len
;
972 macro
->flags
|= NODE_DISABLED
;
975 /* Expand an argument ARG before replacing parameters in a
976 function-like macro. This works by pushing a context with the
977 argument's tokens, and then expanding that into a temporary buffer
978 as if it were a normal part of the token stream. collect_args()
979 has terminated the argument's tokens with a CPP_EOF so that we know
980 when we have fully expanded the argument. */
982 expand_arg (cpp_reader
*pfile
, macro_arg
*arg
)
984 unsigned int capacity
;
985 bool saved_warn_trad
;
990 /* Don't warn about funlike macros when pre-expanding. */
991 saved_warn_trad
= CPP_WTRADITIONAL (pfile
);
992 CPP_WTRADITIONAL (pfile
) = 0;
994 /* Loop, reading in the arguments. */
996 arg
->expanded
= xmalloc (capacity
* sizeof (cpp_token
*));
998 push_ptoken_context (pfile
, NULL
, NULL
, arg
->first
, arg
->count
+ 1);
1001 const cpp_token
*token
;
1003 if (arg
->expanded_count
+ 1 >= capacity
)
1006 arg
->expanded
= xrealloc (arg
->expanded
,
1007 capacity
* sizeof (cpp_token
*));
1010 token
= cpp_get_token (pfile
);
1012 if (token
->type
== CPP_EOF
)
1015 arg
->expanded
[arg
->expanded_count
++] = token
;
1018 _cpp_pop_context (pfile
);
1020 CPP_WTRADITIONAL (pfile
) = saved_warn_trad
;
1023 /* Pop the current context off the stack, re-enabling the macro if the
1024 context represented a macro's replacement list. The context
1025 structure is not freed so that we can re-use it later. */
1027 _cpp_pop_context (cpp_reader
*pfile
)
1029 cpp_context
*context
= pfile
->context
;
1032 context
->macro
->flags
&= ~NODE_DISABLED
;
1035 _cpp_release_buff (pfile
, context
->buff
);
1037 pfile
->context
= context
->prev
;
1040 /* External routine to get a token. Also used nearly everywhere
1041 internally, except for places where we know we can safely call
1042 _cpp_lex_token directly, such as lexing a directive name.
1044 Macro expansions and directives are transparently handled,
1045 including entering included files. Thus tokens are post-macro
1046 expansion, and after any intervening directives. External callers
1047 see CPP_EOF only at EOF. Internal callers also see it when meeting
1048 a directive inside a macro call, when at the end of a directive and
1049 state.in_directive is still 1, and at the end of argument
1052 cpp_get_token (cpp_reader
*pfile
)
1054 const cpp_token
*result
;
1059 cpp_context
*context
= pfile
->context
;
1061 /* Context->prev == 0 <=> base context. */
1063 result
= _cpp_lex_token (pfile
);
1064 else if (FIRST (context
).token
!= LAST (context
).token
)
1066 if (context
->direct_p
)
1067 result
= FIRST (context
).token
++;
1069 result
= *FIRST (context
).ptoken
++;
1071 if (result
->flags
& PASTE_LEFT
)
1073 paste_all_tokens (pfile
, result
);
1074 if (pfile
->state
.in_directive
)
1076 return padding_token (pfile
, result
);
1081 _cpp_pop_context (pfile
);
1082 if (pfile
->state
.in_directive
)
1084 return &pfile
->avoid_paste
;
1087 if (pfile
->state
.in_directive
&& result
->type
== CPP_COMMENT
)
1090 if (result
->type
!= CPP_NAME
)
1093 node
= result
->val
.node
;
1095 if (node
->type
!= NT_MACRO
|| (result
->flags
& NO_EXPAND
))
1098 if (!(node
->flags
& NODE_DISABLED
))
1100 if (!pfile
->state
.prevent_expansion
1101 && enter_macro_context (pfile
, node
))
1103 if (pfile
->state
.in_directive
)
1105 return padding_token (pfile
, result
);
1110 /* Flag this token as always unexpandable. FIXME: move this
1111 to collect_args()?. */
1112 cpp_token
*t
= _cpp_temp_token (pfile
);
1113 t
->type
= result
->type
;
1114 t
->flags
= result
->flags
| NO_EXPAND
;
1115 t
->val
.str
= result
->val
.str
;
1125 /* Returns true if we're expanding an object-like macro that was
1126 defined in a system header. Just checks the macro at the top of
1127 the stack. Used for diagnostic suppression. */
1129 cpp_sys_macro_p (cpp_reader
*pfile
)
1131 cpp_hashnode
*node
= pfile
->context
->macro
;
1133 return node
&& node
->value
.macro
&& node
->value
.macro
->syshdr
;
1136 /* Read each token in, until end of the current file. Directives are
1137 transparently processed. */
1139 cpp_scan_nooutput (cpp_reader
*pfile
)
1141 /* Request a CPP_EOF token at the end of this file, rather than
1142 transparently continuing with the including file. */
1143 pfile
->buffer
->return_at_eof
= true;
1145 pfile
->state
.discarding_output
++;
1146 pfile
->state
.prevent_expansion
++;
1148 if (CPP_OPTION (pfile
, traditional
))
1149 while (_cpp_read_logical_line_trad (pfile
))
1152 while (cpp_get_token (pfile
)->type
!= CPP_EOF
)
1155 pfile
->state
.discarding_output
--;
1156 pfile
->state
.prevent_expansion
--;
1159 /* Step back one (or more) tokens. Can only step mack more than 1 if
1160 they are from the lexer, and not from macro expansion. */
1162 _cpp_backup_tokens (cpp_reader
*pfile
, unsigned int count
)
1164 if (pfile
->context
->prev
== NULL
)
1166 pfile
->lookaheads
+= count
;
1170 if (pfile
->cur_token
== pfile
->cur_run
->base
1171 /* Possible with -fpreprocessed and no leading #line. */
1172 && pfile
->cur_run
->prev
!= NULL
)
1174 pfile
->cur_run
= pfile
->cur_run
->prev
;
1175 pfile
->cur_token
= pfile
->cur_run
->limit
;
1183 if (pfile
->context
->direct_p
)
1184 FIRST (pfile
->context
).token
--;
1186 FIRST (pfile
->context
).ptoken
--;
1190 /* #define directive parsing and handling. */
1192 /* Returns nonzero if a macro redefinition warning is required. */
1194 warn_of_redefinition (cpp_reader
*pfile
, const cpp_hashnode
*node
,
1195 const cpp_macro
*macro2
)
1197 const cpp_macro
*macro1
;
1200 /* Some redefinitions need to be warned about regardless. */
1201 if (node
->flags
& NODE_WARN
)
1204 /* Redefinition of a macro is allowed if and only if the old and new
1205 definitions are the same. (6.10.3 paragraph 2). */
1206 macro1
= node
->value
.macro
;
1208 /* Don't check count here as it can be different in valid
1209 traditional redefinitions with just whitespace differences. */
1210 if (macro1
->paramc
!= macro2
->paramc
1211 || macro1
->fun_like
!= macro2
->fun_like
1212 || macro1
->variadic
!= macro2
->variadic
)
1215 /* Check parameter spellings. */
1216 for (i
= 0; i
< macro1
->paramc
; i
++)
1217 if (macro1
->params
[i
] != macro2
->params
[i
])
1220 /* Check the replacement text or tokens. */
1221 if (CPP_OPTION (pfile
, traditional
))
1222 return _cpp_expansions_different_trad (macro1
, macro2
);
1224 if (macro1
->count
!= macro2
->count
)
1227 for (i
= 0; i
< macro1
->count
; i
++)
1228 if (!_cpp_equiv_tokens (¯o1
->exp
.tokens
[i
], ¯o2
->exp
.tokens
[i
]))
1234 /* Free the definition of hashnode H. */
1236 _cpp_free_definition (cpp_hashnode
*h
)
1238 /* Macros and assertions no longer have anything to free. */
1240 /* Clear builtin flag in case of redefinition. */
1241 h
->flags
&= ~(NODE_BUILTIN
| NODE_DISABLED
);
1244 /* Save parameter NODE to the parameter list of macro MACRO. Returns
1245 zero on success, nonzero if the parameter is a duplicate. */
1247 _cpp_save_parameter (cpp_reader
*pfile
, cpp_macro
*macro
, cpp_hashnode
*node
)
1250 /* Constraint 6.10.3.6 - duplicate parameter names. */
1251 if (node
->flags
& NODE_MACRO_ARG
)
1253 cpp_error (pfile
, CPP_DL_ERROR
, "duplicate macro parameter \"%s\"",
1258 if (BUFF_ROOM (pfile
->a_buff
)
1259 < (macro
->paramc
+ 1) * sizeof (cpp_hashnode
*))
1260 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (cpp_hashnode
*));
1262 ((cpp_hashnode
**) BUFF_FRONT (pfile
->a_buff
))[macro
->paramc
++] = node
;
1263 node
->flags
|= NODE_MACRO_ARG
;
1264 len
= macro
->paramc
* sizeof (union _cpp_hashnode_value
);
1265 if (len
> pfile
->macro_buffer_len
)
1267 pfile
->macro_buffer
= xrealloc (pfile
->macro_buffer
, len
);
1268 pfile
->macro_buffer_len
= len
;
1270 ((union _cpp_hashnode_value
*) pfile
->macro_buffer
)[macro
->paramc
- 1]
1273 node
->value
.arg_index
= macro
->paramc
;
1277 /* Check the syntax of the parameters in a MACRO definition. Returns
1278 false if an error occurs. */
1280 parse_params (cpp_reader
*pfile
, cpp_macro
*macro
)
1282 unsigned int prev_ident
= 0;
1286 const cpp_token
*token
= _cpp_lex_token (pfile
);
1288 switch (token
->type
)
1291 /* Allow/ignore comments in parameter lists if we are
1292 preserving comments in macro expansions. */
1293 if (token
->type
== CPP_COMMENT
1294 && ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
))
1297 cpp_error (pfile
, CPP_DL_ERROR
,
1298 "\"%s\" may not appear in macro parameter list",
1299 cpp_token_as_text (pfile
, token
));
1305 cpp_error (pfile
, CPP_DL_ERROR
,
1306 "macro parameters must be comma-separated");
1311 if (_cpp_save_parameter (pfile
, macro
, token
->val
.node
))
1315 case CPP_CLOSE_PAREN
:
1316 if (prev_ident
|| macro
->paramc
== 0)
1319 /* Fall through to pick up the error. */
1323 cpp_error (pfile
, CPP_DL_ERROR
, "parameter name missing");
1330 macro
->variadic
= 1;
1333 _cpp_save_parameter (pfile
, macro
,
1334 pfile
->spec_nodes
.n__VA_ARGS__
);
1335 pfile
->state
.va_args_ok
= 1;
1336 if (! CPP_OPTION (pfile
, c99
)
1337 && CPP_OPTION (pfile
, pedantic
)
1338 && CPP_OPTION (pfile
, warn_variadic_macros
))
1339 cpp_error (pfile
, CPP_DL_PEDWARN
,
1340 "anonymous variadic macros were introduced in C99");
1342 else if (CPP_OPTION (pfile
, pedantic
)
1343 && CPP_OPTION (pfile
, warn_variadic_macros
))
1344 cpp_error (pfile
, CPP_DL_PEDWARN
,
1345 "ISO C does not permit named variadic macros");
1347 /* We're at the end, and just expect a closing parenthesis. */
1348 token
= _cpp_lex_token (pfile
);
1349 if (token
->type
== CPP_CLOSE_PAREN
)
1354 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' in macro parameter list");
1360 /* Allocate room for a token from a macro's replacement list. */
1362 alloc_expansion_token (cpp_reader
*pfile
, cpp_macro
*macro
)
1364 if (BUFF_ROOM (pfile
->a_buff
) < (macro
->count
+ 1) * sizeof (cpp_token
))
1365 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (cpp_token
));
1367 return &((cpp_token
*) BUFF_FRONT (pfile
->a_buff
))[macro
->count
++];
1370 /* Lex a token from the expansion of MACRO, but mark parameters as we
1371 find them and warn of traditional stringification. */
1373 lex_expansion_token (cpp_reader
*pfile
, cpp_macro
*macro
)
1377 pfile
->cur_token
= alloc_expansion_token (pfile
, macro
);
1378 token
= _cpp_lex_direct (pfile
);
1380 /* Is this a parameter? */
1381 if (token
->type
== CPP_NAME
1382 && (token
->val
.node
->flags
& NODE_MACRO_ARG
) != 0)
1384 token
->type
= CPP_MACRO_ARG
;
1385 token
->val
.arg_no
= token
->val
.node
->value
.arg_index
;
1387 else if (CPP_WTRADITIONAL (pfile
) && macro
->paramc
> 0
1388 && (token
->type
== CPP_STRING
|| token
->type
== CPP_CHAR
))
1389 check_trad_stringification (pfile
, macro
, &token
->val
.str
);
1395 create_iso_definition (cpp_reader
*pfile
, cpp_macro
*macro
)
1398 const cpp_token
*ctoken
;
1400 /* Get the first token of the expansion (or the '(' of a
1401 function-like macro). */
1402 ctoken
= _cpp_lex_token (pfile
);
1404 if (ctoken
->type
== CPP_OPEN_PAREN
&& !(ctoken
->flags
& PREV_WHITE
))
1406 bool ok
= parse_params (pfile
, macro
);
1407 macro
->params
= (cpp_hashnode
**) BUFF_FRONT (pfile
->a_buff
);
1411 /* Success. Commit or allocate the parameter array. */
1412 if (pfile
->hash_table
->alloc_subobject
)
1414 cpp_token
*tokns
= pfile
->hash_table
->alloc_subobject
1415 (sizeof (cpp_token
) * macro
->paramc
);
1416 memcpy (tokns
, macro
->params
, sizeof (cpp_token
) * macro
->paramc
);
1417 macro
->params
= tokns
;
1420 BUFF_FRONT (pfile
->a_buff
) = (uchar
*) ¯o
->params
[macro
->paramc
];
1421 macro
->fun_like
= 1;
1423 else if (ctoken
->type
!= CPP_EOF
&& !(ctoken
->flags
& PREV_WHITE
))
1424 cpp_error (pfile
, CPP_DL_PEDWARN
,
1425 "ISO C requires whitespace after the macro name");
1427 if (macro
->fun_like
)
1428 token
= lex_expansion_token (pfile
, macro
);
1431 token
= alloc_expansion_token (pfile
, macro
);
1437 /* Check the stringifying # constraint 6.10.3.2.1 of
1438 function-like macros when lexing the subsequent token. */
1439 if (macro
->count
> 1 && token
[-1].type
== CPP_HASH
&& macro
->fun_like
)
1441 if (token
->type
== CPP_MACRO_ARG
)
1443 token
->flags
&= ~PREV_WHITE
;
1444 token
->flags
|= STRINGIFY_ARG
;
1445 token
->flags
|= token
[-1].flags
& PREV_WHITE
;
1446 token
[-1] = token
[0];
1449 /* Let assembler get away with murder. */
1450 else if (CPP_OPTION (pfile
, lang
) != CLK_ASM
)
1452 cpp_error (pfile
, CPP_DL_ERROR
,
1453 "'#' is not followed by a macro parameter");
1458 if (token
->type
== CPP_EOF
)
1461 /* Paste operator constraint 6.10.3.3.1. */
1462 if (token
->type
== CPP_PASTE
)
1464 /* Token-paste ##, can appear in both object-like and
1465 function-like macros, but not at the ends. */
1466 if (--macro
->count
> 0)
1467 token
= lex_expansion_token (pfile
, macro
);
1469 if (macro
->count
== 0 || token
->type
== CPP_EOF
)
1471 cpp_error (pfile
, CPP_DL_ERROR
,
1472 "'##' cannot appear at either end of a macro expansion");
1476 token
[-1].flags
|= PASTE_LEFT
;
1479 token
= lex_expansion_token (pfile
, macro
);
1482 macro
->exp
.tokens
= (cpp_token
*) BUFF_FRONT (pfile
->a_buff
);
1483 macro
->traditional
= 0;
1485 /* Don't count the CPP_EOF. */
1488 /* Clear whitespace on first token for warn_of_redefinition(). */
1490 macro
->exp
.tokens
[0].flags
&= ~PREV_WHITE
;
1492 /* Commit or allocate the memory. */
1493 if (pfile
->hash_table
->alloc_subobject
)
1495 cpp_token
*tokns
= pfile
->hash_table
->alloc_subobject (sizeof (cpp_token
)
1497 memcpy (tokns
, macro
->exp
.tokens
, sizeof (cpp_token
) * macro
->count
);
1498 macro
->exp
.tokens
= tokns
;
1501 BUFF_FRONT (pfile
->a_buff
) = (uchar
*) ¯o
->exp
.tokens
[macro
->count
];
1506 /* Parse a macro and save its expansion. Returns nonzero on success. */
1508 _cpp_create_definition (cpp_reader
*pfile
, cpp_hashnode
*node
)
1514 if (pfile
->hash_table
->alloc_subobject
)
1515 macro
= pfile
->hash_table
->alloc_subobject (sizeof (cpp_macro
));
1517 macro
= (cpp_macro
*) _cpp_aligned_alloc (pfile
, sizeof (cpp_macro
));
1518 macro
->line
= pfile
->directive_line
;
1521 macro
->variadic
= 0;
1522 macro
->used
= !CPP_OPTION (pfile
, warn_unused_macros
);
1524 macro
->fun_like
= 0;
1525 /* To suppress some diagnostics. */
1526 macro
->syshdr
= pfile
->buffer
&& pfile
->buffer
->sysp
!= 0;
1528 if (CPP_OPTION (pfile
, traditional
))
1529 ok
= _cpp_create_trad_definition (pfile
, macro
);
1532 cpp_token
*saved_cur_token
= pfile
->cur_token
;
1534 ok
= create_iso_definition (pfile
, macro
);
1536 /* Restore lexer position because of games lex_expansion_token()
1537 plays lexing the macro. We set the type for SEEN_EOL() in
1540 Longer term we should lex the whole line before coming here,
1541 and just copy the expansion. */
1542 saved_cur_token
[-1].type
= pfile
->cur_token
[-1].type
;
1543 pfile
->cur_token
= saved_cur_token
;
1545 /* Stop the lexer accepting __VA_ARGS__. */
1546 pfile
->state
.va_args_ok
= 0;
1549 /* Clear the fast argument lookup indices. */
1550 for (i
= macro
->paramc
; i
-- > 0; )
1552 struct cpp_hashnode
*node
= macro
->params
[i
];
1553 node
->flags
&= ~ NODE_MACRO_ARG
;
1554 node
->value
= ((union _cpp_hashnode_value
*) pfile
->macro_buffer
)[i
];
1560 if (node
->type
== NT_MACRO
)
1562 if (CPP_OPTION (pfile
, warn_unused_macros
))
1563 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
1565 if (warn_of_redefinition (pfile
, node
, macro
))
1567 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, pfile
->directive_line
, 0,
1568 "\"%s\" redefined", NODE_NAME (node
));
1570 if (node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
))
1571 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
,
1572 node
->value
.macro
->line
, 0,
1573 "this is the location of the previous definition");
1577 if (node
->type
!= NT_VOID
)
1578 _cpp_free_definition (node
);
1580 /* Enter definition in hash table. */
1581 node
->type
= NT_MACRO
;
1582 node
->value
.macro
= macro
;
1583 if (! ustrncmp (NODE_NAME (node
), DSC ("__STDC_")))
1584 node
->flags
|= NODE_WARN
;
1589 /* Warn if a token in STRING matches one of a function-like MACRO's
1592 check_trad_stringification (cpp_reader
*pfile
, const cpp_macro
*macro
,
1593 const cpp_string
*string
)
1595 unsigned int i
, len
;
1596 const uchar
*p
, *q
, *limit
;
1598 /* Loop over the string. */
1599 limit
= string
->text
+ string
->len
- 1;
1600 for (p
= string
->text
+ 1; p
< limit
; p
= q
)
1602 /* Find the start of an identifier. */
1603 while (p
< limit
&& !is_idstart (*p
))
1606 /* Find the end of the identifier. */
1608 while (q
< limit
&& is_idchar (*q
))
1613 /* Loop over the function macro arguments to see if the
1614 identifier inside the string matches one of them. */
1615 for (i
= 0; i
< macro
->paramc
; i
++)
1617 const cpp_hashnode
*node
= macro
->params
[i
];
1619 if (NODE_LEN (node
) == len
1620 && !memcmp (p
, NODE_NAME (node
), len
))
1622 cpp_error (pfile
, CPP_DL_WARNING
,
1623 "macro argument \"%s\" would be stringified in traditional C",
1631 /* Returns the name, arguments and expansion of a macro, in a format
1632 suitable to be read back in again, and therefore also for DWARF 2
1633 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1634 Caller is expected to generate the "#define" bit if needed. The
1635 returned text is temporary, and automatically freed later. */
1636 const unsigned char *
1637 cpp_macro_definition (cpp_reader
*pfile
, const cpp_hashnode
*node
)
1639 unsigned int i
, len
;
1640 const cpp_macro
*macro
= node
->value
.macro
;
1641 unsigned char *buffer
;
1643 if (node
->type
!= NT_MACRO
|| (node
->flags
& NODE_BUILTIN
))
1645 cpp_error (pfile
, CPP_DL_ICE
,
1646 "invalid hash type %d in cpp_macro_definition", node
->type
);
1650 /* Calculate length. */
1651 len
= NODE_LEN (node
) + 2; /* ' ' and NUL. */
1652 if (macro
->fun_like
)
1654 len
+= 4; /* "()" plus possible final ".." of named
1655 varargs (we have + 1 below). */
1656 for (i
= 0; i
< macro
->paramc
; i
++)
1657 len
+= NODE_LEN (macro
->params
[i
]) + 1; /* "," */
1660 if (CPP_OPTION (pfile
, traditional
))
1661 len
+= _cpp_replacement_text_len (macro
);
1664 for (i
= 0; i
< macro
->count
; i
++)
1666 cpp_token
*token
= ¯o
->exp
.tokens
[i
];
1668 if (token
->type
== CPP_MACRO_ARG
)
1669 len
+= NODE_LEN (macro
->params
[token
->val
.arg_no
- 1]);
1671 len
+= cpp_token_len (token
) + 1; /* Includes room for ' '. */
1672 if (token
->flags
& STRINGIFY_ARG
)
1674 if (token
->flags
& PASTE_LEFT
)
1675 len
+= 3; /* " ##" */
1679 if (len
> pfile
->macro_buffer_len
)
1681 pfile
->macro_buffer
= xrealloc (pfile
->macro_buffer
, len
);
1682 pfile
->macro_buffer_len
= len
;
1685 /* Fill in the buffer. Start with the macro name. */
1686 buffer
= pfile
->macro_buffer
;
1687 memcpy (buffer
, NODE_NAME (node
), NODE_LEN (node
));
1688 buffer
+= NODE_LEN (node
);
1690 /* Parameter names. */
1691 if (macro
->fun_like
)
1694 for (i
= 0; i
< macro
->paramc
; i
++)
1696 cpp_hashnode
*param
= macro
->params
[i
];
1698 if (param
!= pfile
->spec_nodes
.n__VA_ARGS__
)
1700 memcpy (buffer
, NODE_NAME (param
), NODE_LEN (param
));
1701 buffer
+= NODE_LEN (param
);
1704 if (i
+ 1 < macro
->paramc
)
1705 /* Don't emit a space after the comma here; we're trying
1706 to emit a Dwarf-friendly definition, and the Dwarf spec
1707 forbids spaces in the argument list. */
1709 else if (macro
->variadic
)
1710 *buffer
++ = '.', *buffer
++ = '.', *buffer
++ = '.';
1715 /* The Dwarf spec requires a space after the macro name, even if the
1716 definition is the empty string. */
1719 if (CPP_OPTION (pfile
, traditional
))
1720 buffer
= _cpp_copy_replacement_text (macro
, buffer
);
1721 else if (macro
->count
)
1722 /* Expansion tokens. */
1724 for (i
= 0; i
< macro
->count
; i
++)
1726 cpp_token
*token
= ¯o
->exp
.tokens
[i
];
1728 if (token
->flags
& PREV_WHITE
)
1730 if (token
->flags
& STRINGIFY_ARG
)
1733 if (token
->type
== CPP_MACRO_ARG
)
1735 len
= NODE_LEN (macro
->params
[token
->val
.arg_no
- 1]);
1737 NODE_NAME (macro
->params
[token
->val
.arg_no
- 1]), len
);
1741 buffer
= cpp_spell_token (pfile
, token
, buffer
);
1743 if (token
->flags
& PASTE_LEFT
)
1748 /* Next has PREV_WHITE; see _cpp_create_definition. */
1754 return pfile
->macro_buffer
;