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_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 static size_t remap_pairs
;
113 static char **remap_src
;
114 static char **remap_dst
;
117 add_cpp_remap_path (const char *arg
)
122 arg_dst
= strchr(arg
, ':');
123 if (arg_dst
== NULL
) {
124 fprintf(stderr
, "Invalid argument for -iremap");
130 remap_src
= xrealloc(remap_src
, sizeof(char *) * (remap_pairs
+ 1));
131 remap_dst
= xrealloc(remap_dst
, sizeof(char *) * (remap_pairs
+ 1));
133 remap_src
[remap_pairs
] = xmalloc(len
+ 1);
134 memcpy(remap_src
[remap_pairs
], arg
, len
);
135 remap_src
[remap_pairs
][len
] = '\0';
136 remap_dst
[remap_pairs
] = xstrdup(arg_dst
);
141 cpp_remap_file (const char *arg
, char **tmp_name
)
146 for (i
= 0; i
< remap_pairs
; ++i
) {
147 len
= strlen (remap_src
[i
]);
148 if (strncmp (remap_src
[i
], arg
, len
))
150 if (arg
[len
] == '\0')
151 return xstrdup (remap_dst
[i
]);
155 len
= strlen (remap_dst
[i
]);
156 result
= xmalloc (len
+ strlen (arg
) + 1);
157 memcpy(result
, remap_dst
[i
], len
);
158 strcpy(result
+ len
, arg
);
167 /* Helper function for builtin_macro. Returns the text generated by
170 _cpp_builtin_macro_text (cpp_reader
*pfile
, cpp_hashnode
*node
)
172 const struct line_map
*map
;
173 const uchar
*result
= NULL
;
174 unsigned int number
= 1;
176 switch (node
->value
.builtin
)
179 cpp_error (pfile
, CPP_DL_ICE
, "invalid built-in macro \"%s\"",
190 map
= linemap_lookup (pfile
->line_table
, pfile
->line_table
->highest_line
);
192 if (node
->value
.builtin
== BT_BASE_FILE
)
193 while (! MAIN_FILE_P (map
))
194 map
= INCLUDED_FROM (pfile
->line_table
, map
);
197 name
= cpp_remap_file (map
->to_file
, &tmp_name
);
199 buf
= _cpp_unaligned_alloc (pfile
, len
* 2 + 3);
202 buf
= cpp_quote_string (buf
+ 1, (const unsigned char *) name
, len
);
209 case BT_INCLUDE_LEVEL
:
210 /* The line map depth counts the primary source as level 1, but
211 historically __INCLUDE_DEPTH__ has called the primary source
213 number
= pfile
->line_table
->depth
- 1;
217 map
= &pfile
->line_table
->maps
[pfile
->line_table
->used
-1];
218 /* If __LINE__ is embedded in a macro, it must expand to the
219 line of the macro's invocation, not its definition.
220 Otherwise things like assert() will not work properly. */
221 if (CPP_OPTION (pfile
, traditional
))
222 number
= pfile
->line_table
->highest_line
;
224 number
= pfile
->cur_token
[-1].src_loc
;
225 number
= SOURCE_LINE (map
, number
);
228 /* __STDC__ has the value 1 under normal circumstances.
229 However, if (a) we are in a system header, (b) the option
230 stdc_0_in_system_headers is true (set by target config), and
231 (c) we are not in strictly conforming mode, then it has the
232 value 0. (b) and (c) are already checked in cpp_init_builtins. */
234 if (cpp_in_system_header (pfile
))
242 if (pfile
->date
== NULL
)
244 /* Allocate __DATE__ and __TIME__ strings from permanent
245 storage. We only do this once, and don't generate them
246 at init time, because time() and localtime() are very
247 slow on some systems. */
249 struct tm
*tb
= NULL
;
251 /* (time_t) -1 is a legitimate value for "number of seconds
252 since the Epoch", so we have to do a little dance to
253 distinguish that from a genuine error. */
256 if (tt
!= (time_t)-1 || errno
== 0)
257 tb
= localtime (&tt
);
261 pfile
->date
= _cpp_unaligned_alloc (pfile
,
262 sizeof ("\"Oct 11 1347\""));
263 sprintf ((char *) pfile
->date
, "\"%s %2d %4d\"",
264 monthnames
[tb
->tm_mon
], tb
->tm_mday
,
267 pfile
->time
= _cpp_unaligned_alloc (pfile
,
268 sizeof ("\"12:34:56\""));
269 sprintf ((char *) pfile
->time
, "\"%02d:%02d:%02d\"",
270 tb
->tm_hour
, tb
->tm_min
, tb
->tm_sec
);
274 cpp_errno (pfile
, CPP_DL_WARNING
,
275 "could not determine date and time");
277 pfile
->date
= U
"\"??? ?? ????\"";
278 pfile
->time
= U
"\"??:??:??\"";
282 if (node
->value
.builtin
== BT_DATE
)
283 result
= pfile
->date
;
285 result
= pfile
->time
;
291 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
292 result
= _cpp_unaligned_alloc (pfile
, 21);
293 sprintf ((char *) result
, "%u", number
);
299 /* Convert builtin macros like __FILE__ to a token and push it on the
300 context stack. Also handles _Pragma, for which a new token may not
301 be created. Returns 1 if it generates a new token context, 0 to
302 return the token to the caller. */
304 builtin_macro (cpp_reader
*pfile
, cpp_hashnode
*node
)
310 if (node
->value
.builtin
== BT_PRAGMA
)
312 /* Don't interpret _Pragma within directives. The standard is
313 not clear on this, but to me this makes most sense. */
314 if (pfile
->state
.in_directive
)
317 _cpp_do__Pragma (pfile
);
318 if (pfile
->directive_result
.type
== CPP_PRAGMA
)
320 cpp_token
*tok
= _cpp_temp_token (pfile
);
321 *tok
= pfile
->directive_result
;
322 push_token_context (pfile
, NULL
, tok
, 1);
328 buf
= _cpp_builtin_macro_text (pfile
, node
);
330 nbuf
= (char *) alloca (len
+ 1);
331 memcpy (nbuf
, buf
, len
);
334 cpp_push_buffer (pfile
, (uchar
*) nbuf
, len
, /* from_stage3 */ true);
335 _cpp_clean_line (pfile
);
337 /* Set pfile->cur_token as required by _cpp_lex_direct. */
338 pfile
->cur_token
= _cpp_temp_token (pfile
);
339 push_token_context (pfile
, NULL
, _cpp_lex_direct (pfile
), 1);
340 if (pfile
->buffer
->cur
!= pfile
->buffer
->rlimit
)
341 cpp_error (pfile
, CPP_DL_ICE
, "invalid built-in macro \"%s\"",
343 _cpp_pop_buffer (pfile
);
348 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
349 backslashes and double quotes. DEST must be of sufficient size.
350 Returns a pointer to the end of the string. */
352 cpp_quote_string (uchar
*dest
, const uchar
*src
, unsigned int len
)
358 if (c
== '\\' || c
== '"')
370 /* Convert a token sequence ARG to a single string token according to
371 the rules of the ISO C #-operator. */
372 static const cpp_token
*
373 stringify_arg (cpp_reader
*pfile
, macro_arg
*arg
)
376 unsigned int i
, escape_it
, backslash_count
= 0;
377 const cpp_token
*source
= NULL
;
380 if (BUFF_ROOM (pfile
->u_buff
) < 3)
381 _cpp_extend_buff (pfile
, &pfile
->u_buff
, 3);
382 dest
= BUFF_FRONT (pfile
->u_buff
);
385 /* Loop, reading in the argument's tokens. */
386 for (i
= 0; i
< arg
->count
; i
++)
388 const cpp_token
*token
= arg
->first
[i
];
390 if (token
->type
== CPP_PADDING
)
393 source
= token
->val
.source
;
397 escape_it
= (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
398 || token
->type
== CPP_CHAR
|| token
->type
== CPP_WCHAR
);
400 /* Room for each char being written in octal, initial space and
401 final quote and NUL. */
402 len
= cpp_token_len (token
);
407 if ((size_t) (BUFF_LIMIT (pfile
->u_buff
) - dest
) < len
)
409 size_t len_so_far
= dest
- BUFF_FRONT (pfile
->u_buff
);
410 _cpp_extend_buff (pfile
, &pfile
->u_buff
, len
);
411 dest
= BUFF_FRONT (pfile
->u_buff
) + len_so_far
;
414 /* Leading white space? */
415 if (dest
- 1 != BUFF_FRONT (pfile
->u_buff
))
419 if (source
->flags
& PREV_WHITE
)
426 _cpp_buff
*buff
= _cpp_get_buff (pfile
, len
);
427 unsigned char *buf
= BUFF_FRONT (buff
);
428 len
= cpp_spell_token (pfile
, token
, buf
, true) - buf
;
429 dest
= cpp_quote_string (dest
, buf
, len
);
430 _cpp_release_buff (pfile
, buff
);
433 dest
= cpp_spell_token (pfile
, token
, dest
, true);
435 if (token
->type
== CPP_OTHER
&& token
->val
.str
.text
[0] == '\\')
441 /* Ignore the final \ of invalid string literals. */
442 if (backslash_count
& 1)
444 cpp_error (pfile
, CPP_DL_WARNING
,
445 "invalid string literal, ignoring final '\\'");
449 /* Commit the memory, including NUL, and return the token. */
451 len
= dest
- BUFF_FRONT (pfile
->u_buff
);
452 BUFF_FRONT (pfile
->u_buff
) = dest
+ 1;
453 return new_string_token (pfile
, dest
- len
, len
);
456 /* Try to paste two tokens. On success, return nonzero. In any
457 case, PLHS is updated to point to the pasted token, which is
458 guaranteed to not have the PASTE_LEFT flag set. */
460 paste_tokens (cpp_reader
*pfile
, const cpp_token
**plhs
, const cpp_token
*rhs
)
462 unsigned char *buf
, *end
;
463 const cpp_token
*lhs
;
468 len
= cpp_token_len (lhs
) + cpp_token_len (rhs
) + 1;
469 buf
= (unsigned char *) alloca (len
);
470 end
= cpp_spell_token (pfile
, lhs
, buf
, false);
472 /* Avoid comment headers, since they are still processed in stage 3.
473 It is simpler to insert a space here, rather than modifying the
474 lexer to ignore comments in some circumstances. Simply returning
475 false doesn't work, since we want to clear the PASTE_LEFT flag. */
476 if (lhs
->type
== CPP_DIV
&& rhs
->type
!= CPP_EQ
)
478 end
= cpp_spell_token (pfile
, rhs
, end
, false);
481 cpp_push_buffer (pfile
, buf
, end
- buf
, /* from_stage3 */ true);
482 _cpp_clean_line (pfile
);
484 /* Set pfile->cur_token as required by _cpp_lex_direct. */
485 pfile
->cur_token
= _cpp_temp_token (pfile
);
486 *plhs
= _cpp_lex_direct (pfile
);
487 valid
= pfile
->buffer
->cur
== pfile
->buffer
->rlimit
;
488 _cpp_pop_buffer (pfile
);
493 /* Handles an arbitrarily long sequence of ## operators, with initial
494 operand LHS. This implementation is left-associative,
495 non-recursive, and finishes a paste before handling succeeding
496 ones. If a paste fails, we back up to the RHS of the failing ##
497 operator before pushing the context containing the result of prior
498 successful pastes, with the effect that the RHS appears in the
499 output stream after the pasted LHS normally. */
501 paste_all_tokens (cpp_reader
*pfile
, const cpp_token
*lhs
)
503 const cpp_token
*rhs
;
504 cpp_context
*context
= pfile
->context
;
508 /* Take the token directly from the current context. We can do
509 this, because we are in the replacement list of either an
510 object-like macro, or a function-like macro with arguments
511 inserted. In either case, the constraints to #define
512 guarantee we have at least one more token. */
513 if (context
->direct_p
)
514 rhs
= FIRST (context
).token
++;
516 rhs
= *FIRST (context
).ptoken
++;
518 if (rhs
->type
== CPP_PADDING
)
521 if (!paste_tokens (pfile
, &lhs
, rhs
))
523 _cpp_backup_tokens (pfile
, 1);
525 /* Mandatory error for all apart from assembler. */
526 if (CPP_OPTION (pfile
, lang
) != CLK_ASM
)
527 cpp_error (pfile
, CPP_DL_ERROR
,
528 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
529 cpp_token_as_text (pfile
, lhs
),
530 cpp_token_as_text (pfile
, rhs
));
534 while (rhs
->flags
& PASTE_LEFT
);
536 /* Put the resulting token in its own context. */
537 push_token_context (pfile
, NULL
, lhs
, 1);
540 /* Returns TRUE if the number of arguments ARGC supplied in an
541 invocation of the MACRO referenced by NODE is valid. An empty
542 invocation to a macro with no parameters should pass ARGC as zero.
544 Note that MACRO cannot necessarily be deduced from NODE, in case
545 NODE was redefined whilst collecting arguments. */
547 _cpp_arguments_ok (cpp_reader
*pfile
, cpp_macro
*macro
, const cpp_hashnode
*node
, unsigned int argc
)
549 if (argc
== macro
->paramc
)
552 if (argc
< macro
->paramc
)
554 /* As an extension, a rest argument is allowed to not appear in
555 the invocation at all.
556 e.g. #define debug(format, args...) something
559 This is exactly the same as if there had been an empty rest
560 argument - debug("string", ). */
562 if (argc
+ 1 == macro
->paramc
&& macro
->variadic
)
564 if (CPP_PEDANTIC (pfile
) && ! macro
->syshdr
)
565 cpp_error (pfile
, CPP_DL_PEDWARN
,
566 "ISO C99 requires rest arguments to be used");
570 cpp_error (pfile
, CPP_DL_ERROR
,
571 "macro \"%s\" requires %u arguments, but only %u given",
572 NODE_NAME (node
), macro
->paramc
, argc
);
575 cpp_error (pfile
, CPP_DL_ERROR
,
576 "macro \"%s\" passed %u arguments, but takes just %u",
577 NODE_NAME (node
), argc
, macro
->paramc
);
582 /* Reads and returns the arguments to a function-like macro
583 invocation. Assumes the opening parenthesis has been processed.
584 If there is an error, emits an appropriate diagnostic and returns
585 NULL. Each argument is terminated by a CPP_EOF token, for the
586 future benefit of expand_arg(). */
588 collect_args (cpp_reader
*pfile
, const cpp_hashnode
*node
)
590 _cpp_buff
*buff
, *base_buff
;
592 macro_arg
*args
, *arg
;
593 const cpp_token
*token
;
596 macro
= node
->value
.macro
;
598 argc
= macro
->paramc
;
601 buff
= _cpp_get_buff (pfile
, argc
* (50 * sizeof (cpp_token
*)
602 + sizeof (macro_arg
)));
604 args
= (macro_arg
*) buff
->base
;
605 memset (args
, 0, argc
* sizeof (macro_arg
));
606 buff
->cur
= (unsigned char *) &args
[argc
];
607 arg
= args
, argc
= 0;
608 pfile
->state
.collecting_args
= 1;
610 /* Collect the tokens making up each argument. We don't yet know
611 how many arguments have been supplied, whether too many or too
612 few. Hence the slightly bizarre usage of "argc" and "arg". */
615 unsigned int paren_depth
= 0;
616 unsigned int ntokens
= 0;
619 arg
->first
= (const cpp_token
**) buff
->cur
;
623 /* Require space for 2 new tokens (including a CPP_EOF). */
624 if ((unsigned char *) &arg
->first
[ntokens
+ 2] > buff
->limit
)
626 buff
= _cpp_append_extend_buff (pfile
, buff
,
627 1000 * sizeof (cpp_token
*));
628 arg
->first
= (const cpp_token
**) buff
->cur
;
631 token
= cpp_get_token (pfile
);
633 if (token
->type
== CPP_PADDING
)
635 /* Drop leading padding. */
639 else if (token
->type
== CPP_OPEN_PAREN
)
641 else if (token
->type
== CPP_CLOSE_PAREN
)
643 if (paren_depth
-- == 0)
646 else if (token
->type
== CPP_COMMA
)
648 /* A comma does not terminate an argument within
649 parentheses or as part of a variable argument. */
651 && ! (macro
->variadic
&& argc
== macro
->paramc
))
654 else if (token
->type
== CPP_EOF
655 || (token
->type
== CPP_HASH
&& token
->flags
& BOL
))
658 arg
->first
[ntokens
++] = token
;
661 /* Drop trailing padding. */
662 while (ntokens
> 0 && arg
->first
[ntokens
- 1]->type
== CPP_PADDING
)
665 arg
->count
= ntokens
;
666 arg
->first
[ntokens
] = &pfile
->eof
;
668 /* Terminate the argument. Excess arguments loop back and
669 overwrite the final legitimate argument, before failing. */
670 if (argc
<= macro
->paramc
)
672 buff
->cur
= (unsigned char *) &arg
->first
[ntokens
+ 1];
673 if (argc
!= macro
->paramc
)
677 while (token
->type
!= CPP_CLOSE_PAREN
&& token
->type
!= CPP_EOF
);
678 pfile
->state
.collecting_args
= 0;
680 if (token
->type
== CPP_EOF
)
682 /* We still need the CPP_EOF to end directives, and to end
683 pre-expansion of a macro argument. Step back is not
684 unconditional, since we don't want to return a CPP_EOF to our
685 callers at the end of an -include-d file. */
686 if (pfile
->context
->prev
|| pfile
->state
.in_directive
)
687 _cpp_backup_tokens (pfile
, 1);
688 cpp_error (pfile
, CPP_DL_ERROR
,
689 "unterminated argument list invoking macro \"%s\"",
694 /* A single empty argument is counted as no argument. */
695 if (argc
== 1 && macro
->paramc
== 0 && args
[0].count
== 0)
697 if (_cpp_arguments_ok (pfile
, macro
, node
, argc
))
699 /* GCC has special semantics for , ## b where b is a varargs
700 parameter: we remove the comma if b was omitted entirely.
701 If b was merely an empty argument, the comma is retained.
702 If the macro takes just one (varargs) parameter, then we
703 retain the comma only if we are standards conforming.
705 If FIRST is NULL replace_args () swallows the comma. */
706 if (macro
->variadic
&& (argc
< macro
->paramc
707 || (argc
== 1 && args
[0].count
== 0
708 && !CPP_OPTION (pfile
, std
))))
709 args
[macro
->paramc
- 1].first
= NULL
;
714 /* An error occurred. */
715 _cpp_release_buff (pfile
, base_buff
);
719 /* Search for an opening parenthesis to the macro of NODE, in such a
720 way that, if none is found, we don't lose the information in any
721 intervening padding tokens. If we find the parenthesis, collect
722 the arguments and return the buffer containing them. */
724 funlike_invocation_p (cpp_reader
*pfile
, cpp_hashnode
*node
)
726 const cpp_token
*token
, *padding
= NULL
;
730 token
= cpp_get_token (pfile
);
731 if (token
->type
!= CPP_PADDING
)
734 || (!(padding
->flags
& PREV_WHITE
) && token
->val
.source
== NULL
))
738 if (token
->type
== CPP_OPEN_PAREN
)
740 pfile
->state
.parsing_args
= 2;
741 return collect_args (pfile
, node
);
744 /* CPP_EOF can be the end of macro arguments, or the end of the
745 file. We mustn't back up over the latter. Ugh. */
746 if (token
->type
!= CPP_EOF
|| token
== &pfile
->eof
)
748 /* Back up. We may have skipped padding, in which case backing
749 up more than one token when expanding macros is in general
750 too difficult. We re-insert it in its own context. */
751 _cpp_backup_tokens (pfile
, 1);
753 push_token_context (pfile
, NULL
, padding
, 1);
759 /* Push the context of a macro with hash entry NODE onto the context
760 stack. If we can successfully expand the macro, we push a context
761 containing its yet-to-be-rescanned replacement list and return one.
762 Otherwise, we don't push a context and return zero. */
764 enter_macro_context (cpp_reader
*pfile
, cpp_hashnode
*node
)
766 /* The presence of a macro invalidates a file's controlling macro. */
767 pfile
->mi_valid
= false;
769 pfile
->state
.angled_headers
= false;
771 /* Handle standard macros. */
772 if (! (node
->flags
& NODE_BUILTIN
))
774 cpp_macro
*macro
= node
->value
.macro
;
780 pfile
->state
.prevent_expansion
++;
781 pfile
->keep_tokens
++;
782 pfile
->state
.parsing_args
= 1;
783 buff
= funlike_invocation_p (pfile
, node
);
784 pfile
->state
.parsing_args
= 0;
785 pfile
->keep_tokens
--;
786 pfile
->state
.prevent_expansion
--;
790 if (CPP_WTRADITIONAL (pfile
) && ! node
->value
.macro
->syshdr
)
791 cpp_error (pfile
, CPP_DL_WARNING
,
792 "function-like macro \"%s\" must be used with arguments in traditional C",
798 if (macro
->paramc
> 0)
799 replace_args (pfile
, node
, macro
, (macro_arg
*) buff
->base
);
800 _cpp_release_buff (pfile
, buff
);
803 /* Disable the macro within its expansion. */
804 node
->flags
|= NODE_DISABLED
;
808 if (macro
->paramc
== 0)
809 push_token_context (pfile
, node
, macro
->exp
.tokens
, macro
->count
);
814 /* Handle built-in macros and the _Pragma operator. */
815 return builtin_macro (pfile
, node
);
818 /* Replace the parameters in a function-like macro of NODE with the
819 actual ARGS, and place the result in a newly pushed token context.
820 Expand each argument before replacing, unless it is operated upon
821 by the # or ## operators. */
823 replace_args (cpp_reader
*pfile
, cpp_hashnode
*node
, cpp_macro
*macro
, macro_arg
*args
)
825 unsigned int i
, total
;
826 const cpp_token
*src
, *limit
;
827 const cpp_token
**dest
, **first
;
831 /* First, fully macro-expand arguments, calculating the number of
832 tokens in the final expansion as we go. The ordering of the if
833 statements below is subtle; we must handle stringification before
835 total
= macro
->count
;
836 limit
= macro
->exp
.tokens
+ macro
->count
;
838 for (src
= macro
->exp
.tokens
; src
< limit
; src
++)
839 if (src
->type
== CPP_MACRO_ARG
)
841 /* Leading and trailing padding tokens. */
844 /* We have an argument. If it is not being stringified or
845 pasted it is macro-replaced before insertion. */
846 arg
= &args
[src
->val
.arg_no
- 1];
848 if (src
->flags
& STRINGIFY_ARG
)
850 if (!arg
->stringified
)
851 arg
->stringified
= stringify_arg (pfile
, arg
);
853 else if ((src
->flags
& PASTE_LEFT
)
854 || (src
> macro
->exp
.tokens
&& (src
[-1].flags
& PASTE_LEFT
)))
855 total
+= arg
->count
- 1;
859 expand_arg (pfile
, arg
);
860 total
+= arg
->expanded_count
- 1;
864 /* Now allocate space for the expansion, copy the tokens and replace
866 buff
= _cpp_get_buff (pfile
, total
* sizeof (cpp_token
*));
867 first
= (const cpp_token
**) buff
->base
;
870 for (src
= macro
->exp
.tokens
; src
< limit
; src
++)
873 const cpp_token
**from
, **paste_flag
;
875 if (src
->type
!= CPP_MACRO_ARG
)
882 arg
= &args
[src
->val
.arg_no
- 1];
883 if (src
->flags
& STRINGIFY_ARG
)
884 count
= 1, from
= &arg
->stringified
;
885 else if (src
->flags
& PASTE_LEFT
)
886 count
= arg
->count
, from
= arg
->first
;
887 else if (src
!= macro
->exp
.tokens
&& (src
[-1].flags
& PASTE_LEFT
))
889 count
= arg
->count
, from
= arg
->first
;
892 if (dest
[-1]->type
== CPP_COMMA
894 && src
->val
.arg_no
== macro
->paramc
)
896 /* Swallow a pasted comma if from == NULL, otherwise
897 drop the paste flag. */
901 paste_flag
= dest
- 1;
903 /* Remove the paste flag if the RHS is a placemarker. */
905 paste_flag
= dest
- 1;
909 count
= arg
->expanded_count
, from
= arg
->expanded
;
911 /* Padding on the left of an argument (unless RHS of ##). */
912 if ((!pfile
->state
.in_directive
|| pfile
->state
.directive_wants_padding
)
913 && src
!= macro
->exp
.tokens
&& !(src
[-1].flags
& PASTE_LEFT
))
914 *dest
++ = padding_token (pfile
, src
);
918 memcpy (dest
, from
, count
* sizeof (cpp_token
*));
921 /* With a non-empty argument on the LHS of ##, the last
922 token should be flagged PASTE_LEFT. */
923 if (src
->flags
& PASTE_LEFT
)
924 paste_flag
= dest
- 1;
927 /* Avoid paste on RHS (even case count == 0). */
928 if (!pfile
->state
.in_directive
&& !(src
->flags
& PASTE_LEFT
))
929 *dest
++ = &pfile
->avoid_paste
;
931 /* Add a new paste flag, or remove an unwanted one. */
934 cpp_token
*token
= _cpp_temp_token (pfile
);
935 token
->type
= (*paste_flag
)->type
;
936 token
->val
= (*paste_flag
)->val
;
937 if (src
->flags
& PASTE_LEFT
)
938 token
->flags
= (*paste_flag
)->flags
| PASTE_LEFT
;
940 token
->flags
= (*paste_flag
)->flags
& ~PASTE_LEFT
;
945 /* Free the expanded arguments. */
946 for (i
= 0; i
< macro
->paramc
; i
++)
947 if (args
[i
].expanded
)
948 free (args
[i
].expanded
);
950 push_ptoken_context (pfile
, node
, buff
, first
, dest
- first
);
953 /* Return a special padding token, with padding inherited from SOURCE. */
954 static const cpp_token
*
955 padding_token (cpp_reader
*pfile
, const cpp_token
*source
)
957 cpp_token
*result
= _cpp_temp_token (pfile
);
959 result
->type
= CPP_PADDING
;
961 /* Data in GCed data structures cannot be made const so far, so we
963 result
->val
.source
= (cpp_token
*) source
;
968 /* Get a new uninitialized context. Create a new one if we cannot
969 re-use an old one. */
971 next_context (cpp_reader
*pfile
)
973 cpp_context
*result
= pfile
->context
->next
;
977 result
= XNEW (cpp_context
);
978 result
->prev
= pfile
->context
;
980 pfile
->context
->next
= result
;
983 pfile
->context
= result
;
987 /* Push a list of pointers to tokens. */
989 push_ptoken_context (cpp_reader
*pfile
, cpp_hashnode
*macro
, _cpp_buff
*buff
,
990 const cpp_token
**first
, unsigned int count
)
992 cpp_context
*context
= next_context (pfile
);
994 context
->direct_p
= false;
995 context
->macro
= macro
;
996 context
->buff
= buff
;
997 FIRST (context
).ptoken
= first
;
998 LAST (context
).ptoken
= first
+ count
;
1001 /* Push a list of tokens. */
1003 push_token_context (cpp_reader
*pfile
, cpp_hashnode
*macro
,
1004 const cpp_token
*first
, unsigned int count
)
1006 cpp_context
*context
= next_context (pfile
);
1008 context
->direct_p
= true;
1009 context
->macro
= macro
;
1010 context
->buff
= NULL
;
1011 FIRST (context
).token
= first
;
1012 LAST (context
).token
= first
+ count
;
1015 /* Push a traditional macro's replacement text. */
1017 _cpp_push_text_context (cpp_reader
*pfile
, cpp_hashnode
*macro
,
1018 const uchar
*start
, size_t len
)
1020 cpp_context
*context
= next_context (pfile
);
1022 context
->direct_p
= true;
1023 context
->macro
= macro
;
1024 context
->buff
= NULL
;
1025 CUR (context
) = start
;
1026 RLIMIT (context
) = start
+ len
;
1027 macro
->flags
|= NODE_DISABLED
;
1030 /* Expand an argument ARG before replacing parameters in a
1031 function-like macro. This works by pushing a context with the
1032 argument's tokens, and then expanding that into a temporary buffer
1033 as if it were a normal part of the token stream. collect_args()
1034 has terminated the argument's tokens with a CPP_EOF so that we know
1035 when we have fully expanded the argument. */
1037 expand_arg (cpp_reader
*pfile
, macro_arg
*arg
)
1039 unsigned int capacity
;
1040 bool saved_warn_trad
;
1042 if (arg
->count
== 0)
1045 /* Don't warn about funlike macros when pre-expanding. */
1046 saved_warn_trad
= CPP_WTRADITIONAL (pfile
);
1047 CPP_WTRADITIONAL (pfile
) = 0;
1049 /* Loop, reading in the arguments. */
1051 arg
->expanded
= XNEWVEC (const cpp_token
*, capacity
);
1053 push_ptoken_context (pfile
, NULL
, NULL
, arg
->first
, arg
->count
+ 1);
1056 const cpp_token
*token
;
1058 if (arg
->expanded_count
+ 1 >= capacity
)
1061 arg
->expanded
= XRESIZEVEC (const cpp_token
*, arg
->expanded
,
1065 token
= cpp_get_token (pfile
);
1067 if (token
->type
== CPP_EOF
)
1070 arg
->expanded
[arg
->expanded_count
++] = token
;
1073 _cpp_pop_context (pfile
);
1075 CPP_WTRADITIONAL (pfile
) = saved_warn_trad
;
1078 /* Pop the current context off the stack, re-enabling the macro if the
1079 context represented a macro's replacement list. The context
1080 structure is not freed so that we can re-use it later. */
1082 _cpp_pop_context (cpp_reader
*pfile
)
1084 cpp_context
*context
= pfile
->context
;
1087 context
->macro
->flags
&= ~NODE_DISABLED
;
1090 _cpp_release_buff (pfile
, context
->buff
);
1092 pfile
->context
= context
->prev
;
1095 /* External routine to get a token. Also used nearly everywhere
1096 internally, except for places where we know we can safely call
1097 _cpp_lex_token directly, such as lexing a directive name.
1099 Macro expansions and directives are transparently handled,
1100 including entering included files. Thus tokens are post-macro
1101 expansion, and after any intervening directives. External callers
1102 see CPP_EOF only at EOF. Internal callers also see it when meeting
1103 a directive inside a macro call, when at the end of a directive and
1104 state.in_directive is still 1, and at the end of argument
1107 cpp_get_token (cpp_reader
*pfile
)
1109 const cpp_token
*result
;
1114 cpp_context
*context
= pfile
->context
;
1116 /* Context->prev == 0 <=> base context. */
1118 result
= _cpp_lex_token (pfile
);
1119 else if (FIRST (context
).token
!= LAST (context
).token
)
1121 if (context
->direct_p
)
1122 result
= FIRST (context
).token
++;
1124 result
= *FIRST (context
).ptoken
++;
1126 if (result
->flags
& PASTE_LEFT
)
1128 paste_all_tokens (pfile
, result
);
1129 if (pfile
->state
.in_directive
)
1131 return padding_token (pfile
, result
);
1136 _cpp_pop_context (pfile
);
1137 if (pfile
->state
.in_directive
)
1139 return &pfile
->avoid_paste
;
1142 if (pfile
->state
.in_directive
&& result
->type
== CPP_COMMENT
)
1145 if (result
->type
!= CPP_NAME
)
1148 node
= result
->val
.node
;
1150 if (node
->type
!= NT_MACRO
|| (result
->flags
& NO_EXPAND
))
1153 if (!(node
->flags
& NODE_DISABLED
))
1155 if (!pfile
->state
.prevent_expansion
1156 && enter_macro_context (pfile
, node
))
1158 if (pfile
->state
.in_directive
)
1160 return padding_token (pfile
, result
);
1165 /* Flag this token as always unexpandable. FIXME: move this
1166 to collect_args()?. */
1167 cpp_token
*t
= _cpp_temp_token (pfile
);
1168 t
->type
= result
->type
;
1169 t
->flags
= result
->flags
| NO_EXPAND
;
1170 t
->val
= result
->val
;
1180 /* Returns true if we're expanding an object-like macro that was
1181 defined in a system header. Just checks the macro at the top of
1182 the stack. Used for diagnostic suppression. */
1184 cpp_sys_macro_p (cpp_reader
*pfile
)
1186 cpp_hashnode
*node
= pfile
->context
->macro
;
1188 return node
&& node
->value
.macro
&& node
->value
.macro
->syshdr
;
1191 /* Read each token in, until end of the current file. Directives are
1192 transparently processed. */
1194 cpp_scan_nooutput (cpp_reader
*pfile
)
1196 /* Request a CPP_EOF token at the end of this file, rather than
1197 transparently continuing with the including file. */
1198 pfile
->buffer
->return_at_eof
= true;
1200 pfile
->state
.discarding_output
++;
1201 pfile
->state
.prevent_expansion
++;
1203 if (CPP_OPTION (pfile
, traditional
))
1204 while (_cpp_read_logical_line_trad (pfile
))
1207 while (cpp_get_token (pfile
)->type
!= CPP_EOF
)
1210 pfile
->state
.discarding_output
--;
1211 pfile
->state
.prevent_expansion
--;
1214 /* Step back one (or more) tokens. Can only step mack more than 1 if
1215 they are from the lexer, and not from macro expansion. */
1217 _cpp_backup_tokens (cpp_reader
*pfile
, unsigned int count
)
1219 if (pfile
->context
->prev
== NULL
)
1221 pfile
->lookaheads
+= count
;
1225 if (pfile
->cur_token
== pfile
->cur_run
->base
1226 /* Possible with -fpreprocessed and no leading #line. */
1227 && pfile
->cur_run
->prev
!= NULL
)
1229 pfile
->cur_run
= pfile
->cur_run
->prev
;
1230 pfile
->cur_token
= pfile
->cur_run
->limit
;
1238 if (pfile
->context
->direct_p
)
1239 FIRST (pfile
->context
).token
--;
1241 FIRST (pfile
->context
).ptoken
--;
1245 /* #define directive parsing and handling. */
1247 /* Returns nonzero if a macro redefinition warning is required. */
1249 warn_of_redefinition (cpp_reader
*pfile
, const cpp_hashnode
*node
,
1250 const cpp_macro
*macro2
)
1252 const cpp_macro
*macro1
;
1255 /* Some redefinitions need to be warned about regardless. */
1256 if (node
->flags
& NODE_WARN
)
1259 /* Redefinition of a macro is allowed if and only if the old and new
1260 definitions are the same. (6.10.3 paragraph 2). */
1261 macro1
= node
->value
.macro
;
1263 /* Don't check count here as it can be different in valid
1264 traditional redefinitions with just whitespace differences. */
1265 if (macro1
->paramc
!= macro2
->paramc
1266 || macro1
->fun_like
!= macro2
->fun_like
1267 || macro1
->variadic
!= macro2
->variadic
)
1270 /* Check parameter spellings. */
1271 for (i
= 0; i
< macro1
->paramc
; i
++)
1272 if (macro1
->params
[i
] != macro2
->params
[i
])
1275 /* Check the replacement text or tokens. */
1276 if (CPP_OPTION (pfile
, traditional
))
1277 return _cpp_expansions_different_trad (macro1
, macro2
);
1279 if (macro1
->count
!= macro2
->count
)
1282 for (i
= 0; i
< macro1
->count
; i
++)
1283 if (!_cpp_equiv_tokens (¯o1
->exp
.tokens
[i
], ¯o2
->exp
.tokens
[i
]))
1289 /* Free the definition of hashnode H. */
1291 _cpp_free_definition (cpp_hashnode
*h
)
1293 /* Macros and assertions no longer have anything to free. */
1295 /* Clear builtin flag in case of redefinition. */
1296 h
->flags
&= ~(NODE_BUILTIN
| NODE_DISABLED
);
1299 /* Save parameter NODE to the parameter list of macro MACRO. Returns
1300 zero on success, nonzero if the parameter is a duplicate. */
1302 _cpp_save_parameter (cpp_reader
*pfile
, cpp_macro
*macro
, cpp_hashnode
*node
)
1305 /* Constraint 6.10.3.6 - duplicate parameter names. */
1306 if (node
->flags
& NODE_MACRO_ARG
)
1308 cpp_error (pfile
, CPP_DL_ERROR
, "duplicate macro parameter \"%s\"",
1313 if (BUFF_ROOM (pfile
->a_buff
)
1314 < (macro
->paramc
+ 1) * sizeof (cpp_hashnode
*))
1315 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (cpp_hashnode
*));
1317 ((cpp_hashnode
**) BUFF_FRONT (pfile
->a_buff
))[macro
->paramc
++] = node
;
1318 node
->flags
|= NODE_MACRO_ARG
;
1319 len
= macro
->paramc
* sizeof (union _cpp_hashnode_value
);
1320 if (len
> pfile
->macro_buffer_len
)
1322 pfile
->macro_buffer
= XRESIZEVEC (unsigned char, pfile
->macro_buffer
,
1324 pfile
->macro_buffer_len
= len
;
1326 ((union _cpp_hashnode_value
*) pfile
->macro_buffer
)[macro
->paramc
- 1]
1329 node
->value
.arg_index
= macro
->paramc
;
1333 /* Check the syntax of the parameters in a MACRO definition. Returns
1334 false if an error occurs. */
1336 parse_params (cpp_reader
*pfile
, cpp_macro
*macro
)
1338 unsigned int prev_ident
= 0;
1342 const cpp_token
*token
= _cpp_lex_token (pfile
);
1344 switch (token
->type
)
1347 /* Allow/ignore comments in parameter lists if we are
1348 preserving comments in macro expansions. */
1349 if (token
->type
== CPP_COMMENT
1350 && ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
))
1353 cpp_error (pfile
, CPP_DL_ERROR
,
1354 "\"%s\" may not appear in macro parameter list",
1355 cpp_token_as_text (pfile
, token
));
1361 cpp_error (pfile
, CPP_DL_ERROR
,
1362 "macro parameters must be comma-separated");
1367 if (_cpp_save_parameter (pfile
, macro
, token
->val
.node
))
1371 case CPP_CLOSE_PAREN
:
1372 if (prev_ident
|| macro
->paramc
== 0)
1375 /* Fall through to pick up the error. */
1379 cpp_error (pfile
, CPP_DL_ERROR
, "parameter name missing");
1386 macro
->variadic
= 1;
1389 _cpp_save_parameter (pfile
, macro
,
1390 pfile
->spec_nodes
.n__VA_ARGS__
);
1391 pfile
->state
.va_args_ok
= 1;
1392 if (! CPP_OPTION (pfile
, c99
)
1393 && CPP_OPTION (pfile
, pedantic
)
1394 && CPP_OPTION (pfile
, warn_variadic_macros
))
1395 cpp_error (pfile
, CPP_DL_PEDWARN
,
1396 "anonymous variadic macros were introduced in C99");
1398 else if (CPP_OPTION (pfile
, pedantic
)
1399 && CPP_OPTION (pfile
, warn_variadic_macros
))
1400 cpp_error (pfile
, CPP_DL_PEDWARN
,
1401 "ISO C does not permit named variadic macros");
1403 /* We're at the end, and just expect a closing parenthesis. */
1404 token
= _cpp_lex_token (pfile
);
1405 if (token
->type
== CPP_CLOSE_PAREN
)
1410 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' in macro parameter list");
1416 /* Allocate room for a token from a macro's replacement list. */
1418 alloc_expansion_token (cpp_reader
*pfile
, cpp_macro
*macro
)
1420 if (BUFF_ROOM (pfile
->a_buff
) < (macro
->count
+ 1) * sizeof (cpp_token
))
1421 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (cpp_token
));
1423 return &((cpp_token
*) BUFF_FRONT (pfile
->a_buff
))[macro
->count
++];
1426 /* Lex a token from the expansion of MACRO, but mark parameters as we
1427 find them and warn of traditional stringification. */
1429 lex_expansion_token (cpp_reader
*pfile
, cpp_macro
*macro
)
1433 pfile
->cur_token
= alloc_expansion_token (pfile
, macro
);
1434 token
= _cpp_lex_direct (pfile
);
1436 /* Is this a parameter? */
1437 if (token
->type
== CPP_NAME
1438 && (token
->val
.node
->flags
& NODE_MACRO_ARG
) != 0)
1440 token
->type
= CPP_MACRO_ARG
;
1441 token
->val
.arg_no
= token
->val
.node
->value
.arg_index
;
1443 else if (CPP_WTRADITIONAL (pfile
) && macro
->paramc
> 0
1444 && (token
->type
== CPP_STRING
|| token
->type
== CPP_CHAR
))
1445 check_trad_stringification (pfile
, macro
, &token
->val
.str
);
1451 create_iso_definition (cpp_reader
*pfile
, cpp_macro
*macro
)
1454 const cpp_token
*ctoken
;
1456 /* Get the first token of the expansion (or the '(' of a
1457 function-like macro). */
1458 ctoken
= _cpp_lex_token (pfile
);
1460 if (ctoken
->type
== CPP_OPEN_PAREN
&& !(ctoken
->flags
& PREV_WHITE
))
1462 bool ok
= parse_params (pfile
, macro
);
1463 macro
->params
= (cpp_hashnode
**) BUFF_FRONT (pfile
->a_buff
);
1467 /* Success. Commit or allocate the parameter array. */
1468 if (pfile
->hash_table
->alloc_subobject
)
1470 cpp_hashnode
**params
=
1471 (cpp_hashnode
**) pfile
->hash_table
->alloc_subobject
1472 (sizeof (cpp_hashnode
*) * macro
->paramc
);
1473 memcpy (params
, macro
->params
,
1474 sizeof (cpp_hashnode
*) * macro
->paramc
);
1475 macro
->params
= params
;
1478 BUFF_FRONT (pfile
->a_buff
) = (uchar
*) ¯o
->params
[macro
->paramc
];
1479 macro
->fun_like
= 1;
1481 else if (ctoken
->type
!= CPP_EOF
&& !(ctoken
->flags
& PREV_WHITE
))
1483 /* While ISO C99 requires whitespace before replacement text
1484 in a macro definition, ISO C90 with TC1 allows there characters
1485 from the basic source character set. */
1486 if (CPP_OPTION (pfile
, c99
))
1487 cpp_error (pfile
, CPP_DL_PEDWARN
,
1488 "ISO C99 requires whitespace after the macro name");
1491 int warntype
= CPP_DL_WARNING
;
1492 switch (ctoken
->type
)
1496 case CPP_OBJC_STRING
:
1497 /* '@' is not in basic character set. */
1498 warntype
= CPP_DL_PEDWARN
;
1501 /* Basic character set sans letters, digits and _. */
1502 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
1503 ctoken
->val
.str
.text
[0]) == NULL
)
1504 warntype
= CPP_DL_PEDWARN
;
1507 /* All other tokens start with a character from basic
1511 cpp_error (pfile
, warntype
,
1512 "missing whitespace after the macro name");
1516 if (macro
->fun_like
)
1517 token
= lex_expansion_token (pfile
, macro
);
1520 token
= alloc_expansion_token (pfile
, macro
);
1526 /* Check the stringifying # constraint 6.10.3.2.1 of
1527 function-like macros when lexing the subsequent token. */
1528 if (macro
->count
> 1 && token
[-1].type
== CPP_HASH
&& macro
->fun_like
)
1530 if (token
->type
== CPP_MACRO_ARG
)
1532 token
->flags
&= ~PREV_WHITE
;
1533 token
->flags
|= STRINGIFY_ARG
;
1534 token
->flags
|= token
[-1].flags
& PREV_WHITE
;
1535 token
[-1] = token
[0];
1538 /* Let assembler get away with murder. */
1539 else if (CPP_OPTION (pfile
, lang
) != CLK_ASM
)
1541 cpp_error (pfile
, CPP_DL_ERROR
,
1542 "'#' is not followed by a macro parameter");
1547 if (token
->type
== CPP_EOF
)
1550 /* Paste operator constraint 6.10.3.3.1. */
1551 if (token
->type
== CPP_PASTE
)
1553 /* Token-paste ##, can appear in both object-like and
1554 function-like macros, but not at the ends. */
1555 if (--macro
->count
> 0)
1556 token
= lex_expansion_token (pfile
, macro
);
1558 if (macro
->count
== 0 || token
->type
== CPP_EOF
)
1560 cpp_error (pfile
, CPP_DL_ERROR
,
1561 "'##' cannot appear at either end of a macro expansion");
1565 token
[-1].flags
|= PASTE_LEFT
;
1568 token
= lex_expansion_token (pfile
, macro
);
1571 macro
->exp
.tokens
= (cpp_token
*) BUFF_FRONT (pfile
->a_buff
);
1572 macro
->traditional
= 0;
1574 /* Don't count the CPP_EOF. */
1577 /* Clear whitespace on first token for warn_of_redefinition(). */
1579 macro
->exp
.tokens
[0].flags
&= ~PREV_WHITE
;
1581 /* Commit or allocate the memory. */
1582 if (pfile
->hash_table
->alloc_subobject
)
1585 (cpp_token
*) pfile
->hash_table
->alloc_subobject (sizeof (cpp_token
)
1587 memcpy (tokns
, macro
->exp
.tokens
, sizeof (cpp_token
) * macro
->count
);
1588 macro
->exp
.tokens
= tokns
;
1591 BUFF_FRONT (pfile
->a_buff
) = (uchar
*) ¯o
->exp
.tokens
[macro
->count
];
1596 /* Parse a macro and save its expansion. Returns nonzero on success. */
1598 _cpp_create_definition (cpp_reader
*pfile
, cpp_hashnode
*node
)
1604 if (pfile
->hash_table
->alloc_subobject
)
1605 macro
= (cpp_macro
*) pfile
->hash_table
->alloc_subobject
1606 (sizeof (cpp_macro
));
1608 macro
= (cpp_macro
*) _cpp_aligned_alloc (pfile
, sizeof (cpp_macro
));
1609 macro
->line
= pfile
->directive_line
;
1612 macro
->variadic
= 0;
1613 macro
->used
= !CPP_OPTION (pfile
, warn_unused_macros
);
1615 macro
->fun_like
= 0;
1616 /* To suppress some diagnostics. */
1617 macro
->syshdr
= pfile
->buffer
&& pfile
->buffer
->sysp
!= 0;
1619 if (CPP_OPTION (pfile
, traditional
))
1620 ok
= _cpp_create_trad_definition (pfile
, macro
);
1623 cpp_token
*saved_cur_token
= pfile
->cur_token
;
1625 ok
= create_iso_definition (pfile
, macro
);
1627 /* Restore lexer position because of games lex_expansion_token()
1628 plays lexing the macro. We set the type for SEEN_EOL() in
1631 Longer term we should lex the whole line before coming here,
1632 and just copy the expansion. */
1633 saved_cur_token
[-1].type
= pfile
->cur_token
[-1].type
;
1634 pfile
->cur_token
= saved_cur_token
;
1636 /* Stop the lexer accepting __VA_ARGS__. */
1637 pfile
->state
.va_args_ok
= 0;
1640 /* Clear the fast argument lookup indices. */
1641 for (i
= macro
->paramc
; i
-- > 0; )
1643 struct cpp_hashnode
*node
= macro
->params
[i
];
1644 node
->flags
&= ~ NODE_MACRO_ARG
;
1645 node
->value
= ((union _cpp_hashnode_value
*) pfile
->macro_buffer
)[i
];
1651 if (node
->type
== NT_MACRO
)
1653 if (CPP_OPTION (pfile
, warn_unused_macros
))
1654 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
1656 if (warn_of_redefinition (pfile
, node
, macro
))
1658 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, pfile
->directive_line
, 0,
1659 "\"%s\" redefined", NODE_NAME (node
));
1661 if (node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
))
1662 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
,
1663 node
->value
.macro
->line
, 0,
1664 "this is the location of the previous definition");
1668 if (node
->type
!= NT_VOID
)
1669 _cpp_free_definition (node
);
1671 /* Enter definition in hash table. */
1672 node
->type
= NT_MACRO
;
1673 node
->value
.macro
= macro
;
1674 if (! ustrncmp (NODE_NAME (node
), DSC ("__STDC_")))
1675 node
->flags
|= NODE_WARN
;
1680 /* Warn if a token in STRING matches one of a function-like MACRO's
1683 check_trad_stringification (cpp_reader
*pfile
, const cpp_macro
*macro
,
1684 const cpp_string
*string
)
1686 unsigned int i
, len
;
1687 const uchar
*p
, *q
, *limit
;
1689 /* Loop over the string. */
1690 limit
= string
->text
+ string
->len
- 1;
1691 for (p
= string
->text
+ 1; p
< limit
; p
= q
)
1693 /* Find the start of an identifier. */
1694 while (p
< limit
&& !is_idstart (*p
))
1697 /* Find the end of the identifier. */
1699 while (q
< limit
&& is_idchar (*q
))
1704 /* Loop over the function macro arguments to see if the
1705 identifier inside the string matches one of them. */
1706 for (i
= 0; i
< macro
->paramc
; i
++)
1708 const cpp_hashnode
*node
= macro
->params
[i
];
1710 if (NODE_LEN (node
) == len
1711 && !memcmp (p
, NODE_NAME (node
), len
))
1713 cpp_error (pfile
, CPP_DL_WARNING
,
1714 "macro argument \"%s\" would be stringified in traditional C",
1722 /* Returns the name, arguments and expansion of a macro, in a format
1723 suitable to be read back in again, and therefore also for DWARF 2
1724 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1725 Caller is expected to generate the "#define" bit if needed. The
1726 returned text is temporary, and automatically freed later. */
1727 const unsigned char *
1728 cpp_macro_definition (cpp_reader
*pfile
, const cpp_hashnode
*node
)
1730 unsigned int i
, len
;
1731 const cpp_macro
*macro
= node
->value
.macro
;
1732 unsigned char *buffer
;
1734 if (node
->type
!= NT_MACRO
|| (node
->flags
& NODE_BUILTIN
))
1736 cpp_error (pfile
, CPP_DL_ICE
,
1737 "invalid hash type %d in cpp_macro_definition", node
->type
);
1741 /* Calculate length. */
1742 len
= NODE_LEN (node
) + 2; /* ' ' and NUL. */
1743 if (macro
->fun_like
)
1745 len
+= 4; /* "()" plus possible final ".." of named
1746 varargs (we have + 1 below). */
1747 for (i
= 0; i
< macro
->paramc
; i
++)
1748 len
+= NODE_LEN (macro
->params
[i
]) + 1; /* "," */
1751 /* This should match below where we fill in the buffer. */
1752 if (CPP_OPTION (pfile
, traditional
))
1753 len
+= _cpp_replacement_text_len (macro
);
1756 for (i
= 0; i
< macro
->count
; i
++)
1758 cpp_token
*token
= ¯o
->exp
.tokens
[i
];
1760 if (token
->type
== CPP_MACRO_ARG
)
1761 len
+= NODE_LEN (macro
->params
[token
->val
.arg_no
- 1]);
1763 len
+= cpp_token_len (token
);
1765 if (token
->flags
& STRINGIFY_ARG
)
1767 if (token
->flags
& PASTE_LEFT
)
1768 len
+= 3; /* " ##" */
1769 if (token
->flags
& PREV_WHITE
)
1774 if (len
> pfile
->macro_buffer_len
)
1776 pfile
->macro_buffer
= XRESIZEVEC (unsigned char,
1777 pfile
->macro_buffer
, len
);
1778 pfile
->macro_buffer_len
= len
;
1781 /* Fill in the buffer. Start with the macro name. */
1782 buffer
= pfile
->macro_buffer
;
1783 memcpy (buffer
, NODE_NAME (node
), NODE_LEN (node
));
1784 buffer
+= NODE_LEN (node
);
1786 /* Parameter names. */
1787 if (macro
->fun_like
)
1790 for (i
= 0; i
< macro
->paramc
; i
++)
1792 cpp_hashnode
*param
= macro
->params
[i
];
1794 if (param
!= pfile
->spec_nodes
.n__VA_ARGS__
)
1796 memcpy (buffer
, NODE_NAME (param
), NODE_LEN (param
));
1797 buffer
+= NODE_LEN (param
);
1800 if (i
+ 1 < macro
->paramc
)
1801 /* Don't emit a space after the comma here; we're trying
1802 to emit a Dwarf-friendly definition, and the Dwarf spec
1803 forbids spaces in the argument list. */
1805 else if (macro
->variadic
)
1806 *buffer
++ = '.', *buffer
++ = '.', *buffer
++ = '.';
1811 /* The Dwarf spec requires a space after the macro name, even if the
1812 definition is the empty string. */
1815 if (CPP_OPTION (pfile
, traditional
))
1816 buffer
= _cpp_copy_replacement_text (macro
, buffer
);
1817 else if (macro
->count
)
1818 /* Expansion tokens. */
1820 for (i
= 0; i
< macro
->count
; i
++)
1822 cpp_token
*token
= ¯o
->exp
.tokens
[i
];
1824 if (token
->flags
& PREV_WHITE
)
1826 if (token
->flags
& STRINGIFY_ARG
)
1829 if (token
->type
== CPP_MACRO_ARG
)
1832 NODE_NAME (macro
->params
[token
->val
.arg_no
- 1]),
1833 NODE_LEN (macro
->params
[token
->val
.arg_no
- 1]));
1834 buffer
+= NODE_LEN (macro
->params
[token
->val
.arg_no
- 1]);
1837 buffer
= cpp_spell_token (pfile
, token
, buffer
, false);
1839 if (token
->flags
& PASTE_LEFT
)
1844 /* Next has PREV_WHITE; see _cpp_create_definition. */
1850 return pfile
->macro_buffer
;