1 /* Part of CPP library. (Macro and #define handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 Written by Per Bothner, 1994.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 In other words, you are welcome to use, share and improve this program.
23 You are forbidden to forbid anyone else to use, share and improve
24 what you give them. Help stamp out software-hoarding! */
31 typedef struct macro_arg macro_arg;
34 const cpp_token **first; /* First token in unexpanded argument. */
35 const cpp_token **expanded; /* Macro-expanded argument. */
36 const cpp_token *stringified; /* Stringified argument. */
37 unsigned int count; /* # of tokens in argument. */
38 unsigned int expanded_count; /* # of tokens in expanded argument. */
41 /* Macro expansion. */
43 static int enter_macro_context (cpp_reader *, cpp_hashnode *);
44 static int builtin_macro (cpp_reader *, cpp_hashnode *);
45 static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
46 const cpp_token **, unsigned int);
47 static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *);
48 static cpp_context *next_context (cpp_reader *);
49 static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
50 static void expand_arg (cpp_reader *, macro_arg *);
51 static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
52 static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
53 static void paste_all_tokens (cpp_reader *, const cpp_token *);
54 static bool paste_tokens (cpp_reader *, const cpp_token **, const cpp_token *);
55 static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
57 static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *);
58 static bool create_iso_definition (cpp_reader *, cpp_macro *);
60 /* #define directive parsing and handling. */
62 static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *);
63 static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *);
64 static bool warn_of_redefinition (cpp_reader *, const cpp_hashnode *,
66 static bool parse_params (cpp_reader *, cpp_macro *);
67 static void check_trad_stringification (cpp_reader *, const cpp_macro *,
70 /* Emits a warning if NODE is a macro defined in the main file that
73 _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
74 void *v ATTRIBUTE_UNUSED)
76 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
78 cpp_macro *macro = node->value.macro;
81 && MAIN_FILE_P (linemap_lookup (pfile->line_table, macro->line)))
82 cpp_error_with_line (pfile, CPP_DL_WARNING, macro->line, 0,
83 "macro \"%s\" is not used", NODE_NAME (node));
89 /* Allocates and returns a CPP_STRING token, containing TEXT of length
90 LEN, after null-terminating it. TEXT must be in permanent storage. */
91 static const cpp_token *
92 new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len)
94 cpp_token *token = _cpp_temp_token (pfile);
97 token->type = CPP_STRING;
98 token->val.str.len = len;
99 token->val.str.text = text;
104 static const char * const monthnames[] =
106 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
107 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
110 /* Helper function for builtin_macro. Returns the text generated by
113 _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
115 const struct line_map *map;
116 const uchar *result = NULL;
117 unsigned int number = 1;
119 switch (node->value.builtin)
122 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
128 cpp_buffer *pbuffer = cpp_get_buffer (pfile);
129 if (pbuffer->timestamp == NULL)
131 /* Initialize timestamp value of the assotiated file. */
132 struct _cpp_file *file = cpp_get_file (pbuffer);
135 /* Generate __TIMESTAMP__ string, that represents
136 the date and time of the last modification
137 of the current source file. The string constant
138 looks like "Sun Sep 16 01:03:52 1973". */
139 struct tm *tb = NULL;
140 struct stat *st = _cpp_get_file_stat (file);
142 tb = localtime (&st->st_mtime);
145 char *str = asctime (tb);
146 size_t len = strlen (str);
147 unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2);
149 strcpy ((char *) buf + 1, str);
151 pbuffer->timestamp = buf;
155 cpp_errno (pfile, CPP_DL_WARNING,
156 "could not determine file timestamp");
157 pbuffer->timestamp = U"\"??? ??? ?? ??:??:?? ????\"";
161 result = pbuffer->timestamp;
170 map = linemap_lookup (pfile->line_table, pfile->line_table->highest_line);
172 if (node->value.builtin == BT_BASE_FILE)
173 while (! MAIN_FILE_P (map))
174 map = INCLUDED_FROM (pfile->line_table, map);
178 buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
181 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
187 case BT_INCLUDE_LEVEL:
188 /* The line map depth counts the primary source as level 1, but
189 historically __INCLUDE_DEPTH__ has called the primary source
191 number = pfile->line_table->depth - 1;
195 map = &pfile->line_table->maps[pfile->line_table->used-1];
196 /* If __LINE__ is embedded in a macro, it must expand to the
197 line of the macro's invocation, not its definition.
198 Otherwise things like assert() will not work properly. */
199 if (CPP_OPTION (pfile, traditional))
200 number = pfile->line_table->highest_line;
202 number = pfile->cur_token[-1].src_loc;
203 number = SOURCE_LINE (map, number);
206 /* __STDC__ has the value 1 under normal circumstances.
207 However, if (a) we are in a system header, (b) the option
208 stdc_0_in_system_headers is true (set by target config), and
209 (c) we are not in strictly conforming mode, then it has the
210 value 0. (b) and (c) are already checked in cpp_init_builtins. */
212 if (cpp_in_system_header (pfile))
220 if (pfile->date == NULL)
222 /* Allocate __DATE__ and __TIME__ strings from permanent
223 storage. We only do this once, and don't generate them
224 at init time, because time() and localtime() are very
225 slow on some systems. */
227 struct tm *tb = NULL;
229 /* (time_t) -1 is a legitimate value for "number of seconds
230 since the Epoch", so we have to do a little dance to
231 distinguish that from a genuine error. */
234 if (tt != (time_t)-1 || errno == 0)
235 tb = localtime (&tt);
239 pfile->date = _cpp_unaligned_alloc (pfile,
240 sizeof ("\"Oct 11 1347\""));
241 sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
242 monthnames[tb->tm_mon], tb->tm_mday,
245 pfile->time = _cpp_unaligned_alloc (pfile,
246 sizeof ("\"12:34:56\""));
247 sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
248 tb->tm_hour, tb->tm_min, tb->tm_sec);
252 cpp_errno (pfile, CPP_DL_WARNING,
253 "could not determine date and time");
255 pfile->date = U"\"??? ?? ????\"";
256 pfile->time = U"\"??:??:??\"";
260 if (node->value.builtin == BT_DATE)
261 result = pfile->date;
263 result = pfile->time;
267 if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive)
268 cpp_error (pfile, CPP_DL_ERROR,
269 "__COUNTER__ expanded inside directive with -fdirectives-only");
270 number = pfile->nextcounter++;
276 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
277 result = _cpp_unaligned_alloc (pfile, 21);
278 sprintf ((char *) result, "%u", number);
284 /* Convert builtin macros like __FILE__ to a token and push it on the
285 context stack. Also handles _Pragma, for which a new token may not
286 be created. Returns 1 if it generates a new token context, 0 to
287 return the token to the caller. */
289 builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
295 if (node->value.builtin == BT_PRAGMA)
297 /* Don't interpret _Pragma within directives. The standard is
298 not clear on this, but to me this makes most sense. */
299 if (pfile->state.in_directive)
302 _cpp_do__Pragma (pfile);
306 buf = _cpp_builtin_macro_text (pfile, node);
308 nbuf = (char *) alloca (len + 1);
309 memcpy (nbuf, buf, len);
312 cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
313 _cpp_clean_line (pfile);
315 /* Set pfile->cur_token as required by _cpp_lex_direct. */
316 pfile->cur_token = _cpp_temp_token (pfile);
317 _cpp_push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1);
318 if (pfile->buffer->cur != pfile->buffer->rlimit)
319 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
321 _cpp_pop_buffer (pfile);
326 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
327 backslashes and double quotes. DEST must be of sufficient size.
328 Returns a pointer to the end of the string. */
330 cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
336 if (c == '\\' || c == '"')
348 /* Convert a token sequence ARG to a single string token according to
349 the rules of the ISO C #-operator. */
350 static const cpp_token *
351 stringify_arg (cpp_reader *pfile, macro_arg *arg)
354 unsigned int i, escape_it, backslash_count = 0;
355 const cpp_token *source = NULL;
358 if (BUFF_ROOM (pfile->u_buff) < 3)
359 _cpp_extend_buff (pfile, &pfile->u_buff, 3);
360 dest = BUFF_FRONT (pfile->u_buff);
363 /* Loop, reading in the argument's tokens. */
364 for (i = 0; i < arg->count; i++)
366 const cpp_token *token = arg->first[i];
368 if (token->type == CPP_PADDING)
371 source = token->val.source;
375 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
376 || token->type == CPP_CHAR || token->type == CPP_WCHAR);
378 /* Room for each char being written in octal, initial space and
379 final quote and NUL. */
380 len = cpp_token_len (token);
385 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
387 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
388 _cpp_extend_buff (pfile, &pfile->u_buff, len);
389 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
392 /* Leading white space? */
393 if (dest - 1 != BUFF_FRONT (pfile->u_buff))
397 if (source->flags & PREV_WHITE)
404 _cpp_buff *buff = _cpp_get_buff (pfile, len);
405 unsigned char *buf = BUFF_FRONT (buff);
406 len = cpp_spell_token (pfile, token, buf, true) - buf;
407 dest = cpp_quote_string (dest, buf, len);
408 _cpp_release_buff (pfile, buff);
411 dest = cpp_spell_token (pfile, token, dest, true);
413 if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
419 /* Ignore the final \ of invalid string literals. */
420 if (backslash_count & 1)
422 cpp_error (pfile, CPP_DL_WARNING,
423 "invalid string literal, ignoring final '\\'");
427 /* Commit the memory, including NUL, and return the token. */
429 len = dest - BUFF_FRONT (pfile->u_buff);
430 BUFF_FRONT (pfile->u_buff) = dest + 1;
431 return new_string_token (pfile, dest - len, len);
434 /* Try to paste two tokens. On success, return nonzero. In any
435 case, PLHS is updated to point to the pasted token, which is
436 guaranteed to not have the PASTE_LEFT flag set. */
438 paste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs)
440 unsigned char *buf, *end, *lhsend;
441 const cpp_token *lhs;
445 len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
446 buf = (unsigned char *) alloca (len);
447 end = lhsend = cpp_spell_token (pfile, lhs, buf, false);
449 /* Avoid comment headers, since they are still processed in stage 3.
450 It is simpler to insert a space here, rather than modifying the
451 lexer to ignore comments in some circumstances. Simply returning
452 false doesn't work, since we want to clear the PASTE_LEFT flag. */
453 if (lhs->type == CPP_DIV && rhs->type != CPP_EQ)
455 end = cpp_spell_token (pfile, rhs, end, false);
458 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
459 _cpp_clean_line (pfile);
461 /* Set pfile->cur_token as required by _cpp_lex_direct. */
462 pfile->cur_token = _cpp_temp_token (pfile);
463 *plhs = _cpp_lex_direct (pfile);
464 if (pfile->buffer->cur != pfile->buffer->rlimit)
466 _cpp_pop_buffer (pfile);
467 _cpp_backup_tokens (pfile, 1);
470 /* Mandatory error for all apart from assembler. */
471 if (CPP_OPTION (pfile, lang) != CLK_ASM)
472 cpp_error (pfile, CPP_DL_ERROR,
473 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
474 buf, cpp_token_as_text (pfile, rhs));
478 _cpp_pop_buffer (pfile);
482 /* Handles an arbitrarily long sequence of ## operators, with initial
483 operand LHS. This implementation is left-associative,
484 non-recursive, and finishes a paste before handling succeeding
485 ones. If a paste fails, we back up to the RHS of the failing ##
486 operator before pushing the context containing the result of prior
487 successful pastes, with the effect that the RHS appears in the
488 output stream after the pasted LHS normally. */
490 paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
492 const cpp_token *rhs;
493 cpp_context *context = pfile->context;
497 /* Take the token directly from the current context. We can do
498 this, because we are in the replacement list of either an
499 object-like macro, or a function-like macro with arguments
500 inserted. In either case, the constraints to #define
501 guarantee we have at least one more token. */
502 if (context->direct_p)
503 rhs = FIRST (context).token++;
505 rhs = *FIRST (context).ptoken++;
507 if (rhs->type == CPP_PADDING)
510 if (!paste_tokens (pfile, &lhs, rhs))
513 while (rhs->flags & PASTE_LEFT);
515 /* Put the resulting token in its own context. */
516 _cpp_push_token_context (pfile, NULL, lhs, 1);
519 /* Returns TRUE if the number of arguments ARGC supplied in an
520 invocation of the MACRO referenced by NODE is valid. An empty
521 invocation to a macro with no parameters should pass ARGC as zero.
523 Note that MACRO cannot necessarily be deduced from NODE, in case
524 NODE was redefined whilst collecting arguments. */
526 _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
528 if (argc == macro->paramc)
531 if (argc < macro->paramc)
533 /* As an extension, a rest argument is allowed to not appear in
534 the invocation at all.
535 e.g. #define debug(format, args...) something
538 This is exactly the same as if there had been an empty rest
539 argument - debug("string", ). */
541 if (argc + 1 == macro->paramc && macro->variadic)
543 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
544 cpp_error (pfile, CPP_DL_PEDWARN,
545 "ISO C99 requires rest arguments to be used");
549 cpp_error (pfile, CPP_DL_ERROR,
550 "macro \"%s\" requires %u arguments, but only %u given",
551 NODE_NAME (node), macro->paramc, argc);
554 cpp_error (pfile, CPP_DL_ERROR,
555 "macro \"%s\" passed %u arguments, but takes just %u",
556 NODE_NAME (node), argc, macro->paramc);
561 /* Reads and returns the arguments to a function-like macro
562 invocation. Assumes the opening parenthesis has been processed.
563 If there is an error, emits an appropriate diagnostic and returns
564 NULL. Each argument is terminated by a CPP_EOF token, for the
565 future benefit of expand_arg(). */
567 collect_args (cpp_reader *pfile, const cpp_hashnode *node)
569 _cpp_buff *buff, *base_buff;
571 macro_arg *args, *arg;
572 const cpp_token *token;
575 macro = node->value.macro;
577 argc = macro->paramc;
580 buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
581 + sizeof (macro_arg)));
583 args = (macro_arg *) buff->base;
584 memset (args, 0, argc * sizeof (macro_arg));
585 buff->cur = (unsigned char *) &args[argc];
586 arg = args, argc = 0;
588 /* Collect the tokens making up each argument. We don't yet know
589 how many arguments have been supplied, whether too many or too
590 few. Hence the slightly bizarre usage of "argc" and "arg". */
593 unsigned int paren_depth = 0;
594 unsigned int ntokens = 0;
597 arg->first = (const cpp_token **) buff->cur;
601 /* Require space for 2 new tokens (including a CPP_EOF). */
602 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
604 buff = _cpp_append_extend_buff (pfile, buff,
605 1000 * sizeof (cpp_token *));
606 arg->first = (const cpp_token **) buff->cur;
609 token = cpp_get_token (pfile);
611 if (token->type == CPP_PADDING)
613 /* Drop leading padding. */
617 else if (token->type == CPP_OPEN_PAREN)
619 else if (token->type == CPP_CLOSE_PAREN)
621 if (paren_depth-- == 0)
624 else if (token->type == CPP_COMMA)
626 /* A comma does not terminate an argument within
627 parentheses or as part of a variable argument. */
629 && ! (macro->variadic && argc == macro->paramc))
632 else if (token->type == CPP_EOF
633 || (token->type == CPP_HASH && token->flags & BOL))
636 arg->first[ntokens++] = token;
639 /* Drop trailing padding. */
640 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
643 arg->count = ntokens;
644 arg->first[ntokens] = &pfile->eof;
646 /* Terminate the argument. Excess arguments loop back and
647 overwrite the final legitimate argument, before failing. */
648 if (argc <= macro->paramc)
650 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
651 if (argc != macro->paramc)
655 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
657 if (token->type == CPP_EOF)
659 /* We still need the CPP_EOF to end directives, and to end
660 pre-expansion of a macro argument. Step back is not
661 unconditional, since we don't want to return a CPP_EOF to our
662 callers at the end of an -include-d file. */
663 if (pfile->context->prev || pfile->state.in_directive)
664 _cpp_backup_tokens (pfile, 1);
665 cpp_error (pfile, CPP_DL_ERROR,
666 "unterminated argument list invoking macro \"%s\"",
671 /* A single empty argument is counted as no argument. */
672 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
674 if (_cpp_arguments_ok (pfile, macro, node, argc))
676 /* GCC has special semantics for , ## b where b is a varargs
677 parameter: we remove the comma if b was omitted entirely.
678 If b was merely an empty argument, the comma is retained.
679 If the macro takes just one (varargs) parameter, then we
680 retain the comma only if we are standards conforming.
682 If FIRST is NULL replace_args () swallows the comma. */
683 if (macro->variadic && (argc < macro->paramc
684 || (argc == 1 && args[0].count == 0
685 && !CPP_OPTION (pfile, std))))
686 args[macro->paramc - 1].first = NULL;
691 /* An error occurred. */
692 _cpp_release_buff (pfile, base_buff);
696 /* Search for an opening parenthesis to the macro of NODE, in such a
697 way that, if none is found, we don't lose the information in any
698 intervening padding tokens. If we find the parenthesis, collect
699 the arguments and return the buffer containing them. */
701 funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node)
703 const cpp_token *token, *padding = NULL;
707 token = cpp_get_token (pfile);
708 if (token->type != CPP_PADDING)
711 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
715 if (token->type == CPP_OPEN_PAREN)
717 pfile->state.parsing_args = 2;
718 return collect_args (pfile, node);
721 /* CPP_EOF can be the end of macro arguments, or the end of the
722 file. We mustn't back up over the latter. Ugh. */
723 if (token->type != CPP_EOF || token == &pfile->eof)
725 /* Back up. We may have skipped padding, in which case backing
726 up more than one token when expanding macros is in general
727 too difficult. We re-insert it in its own context. */
728 _cpp_backup_tokens (pfile, 1);
730 _cpp_push_token_context (pfile, NULL, padding, 1);
736 /* Push the context of a macro with hash entry NODE onto the context
737 stack. If we can successfully expand the macro, we push a context
738 containing its yet-to-be-rescanned replacement list and return one.
739 Otherwise, we don't push a context and return zero. */
741 enter_macro_context (cpp_reader *pfile, cpp_hashnode *node)
743 /* The presence of a macro invalidates a file's controlling macro. */
744 pfile->mi_valid = false;
746 pfile->state.angled_headers = false;
748 /* Handle standard macros. */
749 if (! (node->flags & NODE_BUILTIN))
751 cpp_macro *macro = node->value.macro;
757 pfile->state.prevent_expansion++;
758 pfile->keep_tokens++;
759 pfile->state.parsing_args = 1;
760 buff = funlike_invocation_p (pfile, node);
761 pfile->state.parsing_args = 0;
762 pfile->keep_tokens--;
763 pfile->state.prevent_expansion--;
767 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
768 cpp_error (pfile, CPP_DL_WARNING,
769 "function-like macro \"%s\" must be used with arguments in traditional C",
775 if (macro->paramc > 0)
776 replace_args (pfile, node, macro, (macro_arg *) buff->base);
777 _cpp_release_buff (pfile, buff);
780 /* Disable the macro within its expansion. */
781 node->flags |= NODE_DISABLED;
785 if (macro->paramc == 0)
786 _cpp_push_token_context (pfile, node, macro->exp.tokens, macro->count);
791 /* Handle built-in macros and the _Pragma operator. */
792 return builtin_macro (pfile, node);
795 /* Replace the parameters in a function-like macro of NODE with the
796 actual ARGS, and place the result in a newly pushed token context.
797 Expand each argument before replacing, unless it is operated upon
798 by the # or ## operators. */
800 replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg *args)
802 unsigned int i, total;
803 const cpp_token *src, *limit;
804 const cpp_token **dest, **first;
808 /* First, fully macro-expand arguments, calculating the number of
809 tokens in the final expansion as we go. The ordering of the if
810 statements below is subtle; we must handle stringification before
812 total = macro->count;
813 limit = macro->exp.tokens + macro->count;
815 for (src = macro->exp.tokens; src < limit; src++)
816 if (src->type == CPP_MACRO_ARG)
818 /* Leading and trailing padding tokens. */
821 /* We have an argument. If it is not being stringified or
822 pasted it is macro-replaced before insertion. */
823 arg = &args[src->val.arg_no - 1];
825 if (src->flags & STRINGIFY_ARG)
827 if (!arg->stringified)
828 arg->stringified = stringify_arg (pfile, arg);
830 else if ((src->flags & PASTE_LEFT)
831 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
832 total += arg->count - 1;
836 expand_arg (pfile, arg);
837 total += arg->expanded_count - 1;
841 /* Now allocate space for the expansion, copy the tokens and replace
843 buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
844 first = (const cpp_token **) buff->base;
847 for (src = macro->exp.tokens; src < limit; src++)
850 const cpp_token **from, **paste_flag;
852 if (src->type != CPP_MACRO_ARG)
859 arg = &args[src->val.arg_no - 1];
860 if (src->flags & STRINGIFY_ARG)
861 count = 1, from = &arg->stringified;
862 else if (src->flags & PASTE_LEFT)
863 count = arg->count, from = arg->first;
864 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
866 count = arg->count, from = arg->first;
869 if (dest[-1]->type == CPP_COMMA
871 && src->val.arg_no == macro->paramc)
873 /* Swallow a pasted comma if from == NULL, otherwise
874 drop the paste flag. */
878 paste_flag = dest - 1;
880 /* Remove the paste flag if the RHS is a placemarker. */
882 paste_flag = dest - 1;
886 count = arg->expanded_count, from = arg->expanded;
888 /* Padding on the left of an argument (unless RHS of ##). */
889 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
890 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
891 *dest++ = padding_token (pfile, src);
895 memcpy (dest, from, count * sizeof (cpp_token *));
898 /* With a non-empty argument on the LHS of ##, the last
899 token should be flagged PASTE_LEFT. */
900 if (src->flags & PASTE_LEFT)
901 paste_flag = dest - 1;
904 /* Avoid paste on RHS (even case count == 0). */
905 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
906 *dest++ = &pfile->avoid_paste;
908 /* Add a new paste flag, or remove an unwanted one. */
911 cpp_token *token = _cpp_temp_token (pfile);
912 token->type = (*paste_flag)->type;
913 token->val = (*paste_flag)->val;
914 if (src->flags & PASTE_LEFT)
915 token->flags = (*paste_flag)->flags | PASTE_LEFT;
917 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
922 /* Free the expanded arguments. */
923 for (i = 0; i < macro->paramc; i++)
924 if (args[i].expanded)
925 free (args[i].expanded);
927 push_ptoken_context (pfile, node, buff, first, dest - first);
930 /* Return a special padding token, with padding inherited from SOURCE. */
931 static const cpp_token *
932 padding_token (cpp_reader *pfile, const cpp_token *source)
934 cpp_token *result = _cpp_temp_token (pfile);
936 result->type = CPP_PADDING;
938 /* Data in GCed data structures cannot be made const so far, so we
940 result->val.source = (cpp_token *) source;
945 /* Get a new uninitialized context. Create a new one if we cannot
946 re-use an old one. */
948 next_context (cpp_reader *pfile)
950 cpp_context *result = pfile->context->next;
954 result = XNEW (cpp_context);
955 result->prev = pfile->context;
957 pfile->context->next = result;
960 pfile->context = result;
964 /* Push a list of pointers to tokens. */
966 push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
967 const cpp_token **first, unsigned int count)
969 cpp_context *context = next_context (pfile);
971 context->direct_p = false;
972 context->macro = macro;
973 context->buff = buff;
974 FIRST (context).ptoken = first;
975 LAST (context).ptoken = first + count;
978 /* Push a list of tokens. */
980 _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
981 const cpp_token *first, unsigned int count)
983 cpp_context *context = next_context (pfile);
985 context->direct_p = true;
986 context->macro = macro;
987 context->buff = NULL;
988 FIRST (context).token = first;
989 LAST (context).token = first + count;
992 /* Push a traditional macro's replacement text. */
994 _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
995 const uchar *start, size_t len)
997 cpp_context *context = next_context (pfile);
999 context->direct_p = true;
1000 context->macro = macro;
1001 context->buff = NULL;
1002 CUR (context) = start;
1003 RLIMIT (context) = start + len;
1004 macro->flags |= NODE_DISABLED;
1007 /* Expand an argument ARG before replacing parameters in a
1008 function-like macro. This works by pushing a context with the
1009 argument's tokens, and then expanding that into a temporary buffer
1010 as if it were a normal part of the token stream. collect_args()
1011 has terminated the argument's tokens with a CPP_EOF so that we know
1012 when we have fully expanded the argument. */
1014 expand_arg (cpp_reader *pfile, macro_arg *arg)
1016 unsigned int capacity;
1017 bool saved_warn_trad;
1019 if (arg->count == 0)
1022 /* Don't warn about funlike macros when pre-expanding. */
1023 saved_warn_trad = CPP_WTRADITIONAL (pfile);
1024 CPP_WTRADITIONAL (pfile) = 0;
1026 /* Loop, reading in the arguments. */
1028 arg->expanded = XNEWVEC (const cpp_token *, capacity);
1030 push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
1033 const cpp_token *token;
1035 if (arg->expanded_count + 1 >= capacity)
1038 arg->expanded = XRESIZEVEC (const cpp_token *, arg->expanded,
1042 token = cpp_get_token (pfile);
1044 if (token->type == CPP_EOF)
1047 arg->expanded[arg->expanded_count++] = token;
1050 _cpp_pop_context (pfile);
1052 CPP_WTRADITIONAL (pfile) = saved_warn_trad;
1055 /* Pop the current context off the stack, re-enabling the macro if the
1056 context represented a macro's replacement list. The context
1057 structure is not freed so that we can re-use it later. */
1059 _cpp_pop_context (cpp_reader *pfile)
1061 cpp_context *context = pfile->context;
1064 context->macro->flags &= ~NODE_DISABLED;
1067 _cpp_release_buff (pfile, context->buff);
1069 pfile->context = context->prev;
1072 /* External routine to get a token. Also used nearly everywhere
1073 internally, except for places where we know we can safely call
1074 _cpp_lex_token directly, such as lexing a directive name.
1076 Macro expansions and directives are transparently handled,
1077 including entering included files. Thus tokens are post-macro
1078 expansion, and after any intervening directives. External callers
1079 see CPP_EOF only at EOF. Internal callers also see it when meeting
1080 a directive inside a macro call, when at the end of a directive and
1081 state.in_directive is still 1, and at the end of argument
1084 cpp_get_token (cpp_reader *pfile)
1086 const cpp_token *result;
1091 cpp_context *context = pfile->context;
1093 /* Context->prev == 0 <=> base context. */
1095 result = _cpp_lex_token (pfile);
1096 else if (FIRST (context).token != LAST (context).token)
1098 if (context->direct_p)
1099 result = FIRST (context).token++;
1101 result = *FIRST (context).ptoken++;
1103 if (result->flags & PASTE_LEFT)
1105 paste_all_tokens (pfile, result);
1106 if (pfile->state.in_directive)
1108 return padding_token (pfile, result);
1113 _cpp_pop_context (pfile);
1114 if (pfile->state.in_directive)
1116 return &pfile->avoid_paste;
1119 if (pfile->state.in_directive && result->type == CPP_COMMENT)
1122 if (result->type != CPP_NAME)
1125 node = result->val.node;
1127 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
1130 if (!(node->flags & NODE_DISABLED))
1132 if (!pfile->state.prevent_expansion
1133 && enter_macro_context (pfile, node))
1135 if (pfile->state.in_directive)
1137 return padding_token (pfile, result);
1142 /* Flag this token as always unexpandable. FIXME: move this
1143 to collect_args()?. */
1144 cpp_token *t = _cpp_temp_token (pfile);
1145 t->type = result->type;
1146 t->flags = result->flags | NO_EXPAND;
1147 t->val = result->val;
1157 /* Returns true if we're expanding an object-like macro that was
1158 defined in a system header. Just checks the macro at the top of
1159 the stack. Used for diagnostic suppression. */
1161 cpp_sys_macro_p (cpp_reader *pfile)
1163 cpp_hashnode *node = pfile->context->macro;
1165 return node && node->value.macro && node->value.macro->syshdr;
1168 /* Read each token in, until end of the current file. Directives are
1169 transparently processed. */
1171 cpp_scan_nooutput (cpp_reader *pfile)
1173 /* Request a CPP_EOF token at the end of this file, rather than
1174 transparently continuing with the including file. */
1175 pfile->buffer->return_at_eof = true;
1177 pfile->state.discarding_output++;
1178 pfile->state.prevent_expansion++;
1180 if (CPP_OPTION (pfile, traditional))
1181 while (_cpp_read_logical_line_trad (pfile))
1184 while (cpp_get_token (pfile)->type != CPP_EOF)
1187 pfile->state.discarding_output--;
1188 pfile->state.prevent_expansion--;
1191 /* Step back one (or more) tokens. Can only step back more than 1 if
1192 they are from the lexer, and not from macro expansion. */
1194 _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
1196 if (pfile->context->prev == NULL)
1198 pfile->lookaheads += count;
1202 if (pfile->cur_token == pfile->cur_run->base
1203 /* Possible with -fpreprocessed and no leading #line. */
1204 && pfile->cur_run->prev != NULL)
1206 pfile->cur_run = pfile->cur_run->prev;
1207 pfile->cur_token = pfile->cur_run->limit;
1215 if (pfile->context->direct_p)
1216 FIRST (pfile->context).token--;
1218 FIRST (pfile->context).ptoken--;
1222 /* #define directive parsing and handling. */
1224 /* Returns nonzero if a macro redefinition warning is required. */
1226 warn_of_redefinition (cpp_reader *pfile, const cpp_hashnode *node,
1227 const cpp_macro *macro2)
1229 const cpp_macro *macro1;
1232 /* Some redefinitions need to be warned about regardless. */
1233 if (node->flags & NODE_WARN)
1236 /* Redefinition of a macro is allowed if and only if the old and new
1237 definitions are the same. (6.10.3 paragraph 2). */
1238 macro1 = node->value.macro;
1240 /* Don't check count here as it can be different in valid
1241 traditional redefinitions with just whitespace differences. */
1242 if (macro1->paramc != macro2->paramc
1243 || macro1->fun_like != macro2->fun_like
1244 || macro1->variadic != macro2->variadic)
1247 /* Check parameter spellings. */
1248 for (i = 0; i < macro1->paramc; i++)
1249 if (macro1->params[i] != macro2->params[i])
1252 /* Check the replacement text or tokens. */
1253 if (CPP_OPTION (pfile, traditional))
1254 return _cpp_expansions_different_trad (macro1, macro2);
1256 if (macro1->count != macro2->count)
1259 for (i = 0; i < macro1->count; i++)
1260 if (!_cpp_equiv_tokens (¯o1->exp.tokens[i], ¯o2->exp.tokens[i]))
1266 /* Free the definition of hashnode H. */
1268 _cpp_free_definition (cpp_hashnode *h)
1270 /* Macros and assertions no longer have anything to free. */
1272 /* Clear builtin flag in case of redefinition. */
1273 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED);
1276 /* Save parameter NODE to the parameter list of macro MACRO. Returns
1277 zero on success, nonzero if the parameter is a duplicate. */
1279 _cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node)
1282 /* Constraint 6.10.3.6 - duplicate parameter names. */
1283 if (node->flags & NODE_MACRO_ARG)
1285 cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
1290 if (BUFF_ROOM (pfile->a_buff)
1291 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1292 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
1294 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
1295 node->flags |= NODE_MACRO_ARG;
1296 len = macro->paramc * sizeof (union _cpp_hashnode_value);
1297 if (len > pfile->macro_buffer_len)
1299 pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer,
1301 pfile->macro_buffer_len = len;
1303 ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1]
1306 node->value.arg_index = macro->paramc;
1310 /* Check the syntax of the parameters in a MACRO definition. Returns
1311 false if an error occurs. */
1313 parse_params (cpp_reader *pfile, cpp_macro *macro)
1315 unsigned int prev_ident = 0;
1319 const cpp_token *token = _cpp_lex_token (pfile);
1321 switch (token->type)
1324 /* Allow/ignore comments in parameter lists if we are
1325 preserving comments in macro expansions. */
1326 if (token->type == CPP_COMMENT
1327 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
1330 cpp_error (pfile, CPP_DL_ERROR,
1331 "\"%s\" may not appear in macro parameter list",
1332 cpp_token_as_text (pfile, token));
1338 cpp_error (pfile, CPP_DL_ERROR,
1339 "macro parameters must be comma-separated");
1344 if (_cpp_save_parameter (pfile, macro, token->val.node))
1348 case CPP_CLOSE_PAREN:
1349 if (prev_ident || macro->paramc == 0)
1352 /* Fall through to pick up the error. */
1356 cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
1363 macro->variadic = 1;
1366 _cpp_save_parameter (pfile, macro,
1367 pfile->spec_nodes.n__VA_ARGS__);
1368 pfile->state.va_args_ok = 1;
1369 if (! CPP_OPTION (pfile, c99)
1370 && CPP_OPTION (pfile, pedantic)
1371 && CPP_OPTION (pfile, warn_variadic_macros))
1372 cpp_error (pfile, CPP_DL_PEDWARN,
1373 "anonymous variadic macros were introduced in C99");
1375 else if (CPP_OPTION (pfile, pedantic)
1376 && CPP_OPTION (pfile, warn_variadic_macros))
1377 cpp_error (pfile, CPP_DL_PEDWARN,
1378 "ISO C does not permit named variadic macros");
1380 /* We're at the end, and just expect a closing parenthesis. */
1381 token = _cpp_lex_token (pfile);
1382 if (token->type == CPP_CLOSE_PAREN)
1387 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
1393 /* Allocate room for a token from a macro's replacement list. */
1395 alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
1397 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1398 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
1400 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
1403 /* Lex a token from the expansion of MACRO, but mark parameters as we
1404 find them and warn of traditional stringification. */
1406 lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
1410 pfile->cur_token = alloc_expansion_token (pfile, macro);
1411 token = _cpp_lex_direct (pfile);
1413 /* Is this a parameter? */
1414 if (token->type == CPP_NAME
1415 && (token->val.node->flags & NODE_MACRO_ARG) != 0)
1417 token->type = CPP_MACRO_ARG;
1418 token->val.arg_no = token->val.node->value.arg_index;
1420 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1421 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1422 check_trad_stringification (pfile, macro, &token->val.str);
1428 create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
1431 const cpp_token *ctoken;
1433 /* Get the first token of the expansion (or the '(' of a
1434 function-like macro). */
1435 ctoken = _cpp_lex_token (pfile);
1437 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
1439 bool ok = parse_params (pfile, macro);
1440 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1444 /* Success. Commit or allocate the parameter array. */
1445 if (pfile->hash_table->alloc_subobject)
1447 cpp_hashnode **params =
1448 (cpp_hashnode **) pfile->hash_table->alloc_subobject
1449 (sizeof (cpp_hashnode *) * macro->paramc);
1450 memcpy (params, macro->params,
1451 sizeof (cpp_hashnode *) * macro->paramc);
1452 macro->params = params;
1455 BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->params[macro->paramc];
1456 macro->fun_like = 1;
1458 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
1460 /* While ISO C99 requires whitespace before replacement text
1461 in a macro definition, ISO C90 with TC1 allows there characters
1462 from the basic source character set. */
1463 if (CPP_OPTION (pfile, c99))
1464 cpp_error (pfile, CPP_DL_PEDWARN,
1465 "ISO C99 requires whitespace after the macro name");
1468 int warntype = CPP_DL_WARNING;
1469 switch (ctoken->type)
1473 case CPP_OBJC_STRING:
1474 /* '@' is not in basic character set. */
1475 warntype = CPP_DL_PEDWARN;
1478 /* Basic character set sans letters, digits and _. */
1479 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
1480 ctoken->val.str.text[0]) == NULL)
1481 warntype = CPP_DL_PEDWARN;
1484 /* All other tokens start with a character from basic
1488 cpp_error (pfile, warntype,
1489 "missing whitespace after the macro name");
1493 if (macro->fun_like)
1494 token = lex_expansion_token (pfile, macro);
1497 token = alloc_expansion_token (pfile, macro);
1503 /* Check the stringifying # constraint 6.10.3.2.1 of
1504 function-like macros when lexing the subsequent token. */
1505 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1507 if (token->type == CPP_MACRO_ARG)
1509 token->flags &= ~PREV_WHITE;
1510 token->flags |= STRINGIFY_ARG;
1511 token->flags |= token[-1].flags & PREV_WHITE;
1512 token[-1] = token[0];
1515 /* Let assembler get away with murder. */
1516 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
1518 cpp_error (pfile, CPP_DL_ERROR,
1519 "'#' is not followed by a macro parameter");
1524 if (token->type == CPP_EOF)
1527 /* Paste operator constraint 6.10.3.3.1. */
1528 if (token->type == CPP_PASTE)
1530 /* Token-paste ##, can appear in both object-like and
1531 function-like macros, but not at the ends. */
1532 if (--macro->count > 0)
1533 token = lex_expansion_token (pfile, macro);
1535 if (macro->count == 0 || token->type == CPP_EOF)
1537 cpp_error (pfile, CPP_DL_ERROR,
1538 "'##' cannot appear at either end of a macro expansion");
1542 token[-1].flags |= PASTE_LEFT;
1545 token = lex_expansion_token (pfile, macro);
1548 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
1549 macro->traditional = 0;
1551 /* Don't count the CPP_EOF. */
1554 /* Clear whitespace on first token for warn_of_redefinition(). */
1556 macro->exp.tokens[0].flags &= ~PREV_WHITE;
1558 /* Commit or allocate the memory. */
1559 if (pfile->hash_table->alloc_subobject)
1562 (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token)
1564 memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
1565 macro->exp.tokens = tokns;
1568 BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->exp.tokens[macro->count];
1573 /* Parse a macro and save its expansion. Returns nonzero on success. */
1575 _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
1581 if (pfile->hash_table->alloc_subobject)
1582 macro = (cpp_macro *) pfile->hash_table->alloc_subobject
1583 (sizeof (cpp_macro));
1585 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
1586 macro->line = pfile->directive_line;
1589 macro->variadic = 0;
1590 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
1592 macro->fun_like = 0;
1593 /* To suppress some diagnostics. */
1594 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
1596 if (CPP_OPTION (pfile, traditional))
1597 ok = _cpp_create_trad_definition (pfile, macro);
1600 cpp_token *saved_cur_token = pfile->cur_token;
1602 ok = create_iso_definition (pfile, macro);
1604 /* Restore lexer position because of games lex_expansion_token()
1605 plays lexing the macro. We set the type for SEEN_EOL() in
1608 Longer term we should lex the whole line before coming here,
1609 and just copy the expansion. */
1610 saved_cur_token[-1].type = pfile->cur_token[-1].type;
1611 pfile->cur_token = saved_cur_token;
1613 /* Stop the lexer accepting __VA_ARGS__. */
1614 pfile->state.va_args_ok = 0;
1617 /* Clear the fast argument lookup indices. */
1618 for (i = macro->paramc; i-- > 0; )
1620 struct cpp_hashnode *node = macro->params[i];
1621 node->flags &= ~ NODE_MACRO_ARG;
1622 node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i];
1628 if (node->type == NT_MACRO)
1630 if (CPP_OPTION (pfile, warn_unused_macros))
1631 _cpp_warn_if_unused_macro (pfile, node, NULL);
1633 if (warn_of_redefinition (pfile, node, macro))
1635 cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->directive_line, 0,
1636 "\"%s\" redefined", NODE_NAME (node));
1638 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1639 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1640 node->value.macro->line, 0,
1641 "this is the location of the previous definition");
1645 if (node->type != NT_VOID)
1646 _cpp_free_definition (node);
1648 /* Enter definition in hash table. */
1649 node->type = NT_MACRO;
1650 node->value.macro = macro;
1651 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
1652 node->flags |= NODE_WARN;
1657 /* Warn if a token in STRING matches one of a function-like MACRO's
1660 check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
1661 const cpp_string *string)
1663 unsigned int i, len;
1664 const uchar *p, *q, *limit;
1666 /* Loop over the string. */
1667 limit = string->text + string->len - 1;
1668 for (p = string->text + 1; p < limit; p = q)
1670 /* Find the start of an identifier. */
1671 while (p < limit && !is_idstart (*p))
1674 /* Find the end of the identifier. */
1676 while (q < limit && is_idchar (*q))
1681 /* Loop over the function macro arguments to see if the
1682 identifier inside the string matches one of them. */
1683 for (i = 0; i < macro->paramc; i++)
1685 const cpp_hashnode *node = macro->params[i];
1687 if (NODE_LEN (node) == len
1688 && !memcmp (p, NODE_NAME (node), len))
1690 cpp_error (pfile, CPP_DL_WARNING,
1691 "macro argument \"%s\" would be stringified in traditional C",
1699 /* Returns the name, arguments and expansion of a macro, in a format
1700 suitable to be read back in again, and therefore also for DWARF 2
1701 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1702 Caller is expected to generate the "#define" bit if needed. The
1703 returned text is temporary, and automatically freed later. */
1704 const unsigned char *
1705 cpp_macro_definition (cpp_reader *pfile, const cpp_hashnode *node)
1707 unsigned int i, len;
1708 const cpp_macro *macro = node->value.macro;
1709 unsigned char *buffer;
1711 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1713 cpp_error (pfile, CPP_DL_ICE,
1714 "invalid hash type %d in cpp_macro_definition", node->type);
1718 /* Calculate length. */
1719 len = NODE_LEN (node) + 2; /* ' ' and NUL. */
1720 if (macro->fun_like)
1722 len += 4; /* "()" plus possible final ".." of named
1723 varargs (we have + 1 below). */
1724 for (i = 0; i < macro->paramc; i++)
1725 len += NODE_LEN (macro->params[i]) + 1; /* "," */
1728 /* This should match below where we fill in the buffer. */
1729 if (CPP_OPTION (pfile, traditional))
1730 len += _cpp_replacement_text_len (macro);
1733 for (i = 0; i < macro->count; i++)
1735 cpp_token *token = ¯o->exp.tokens[i];
1737 if (token->type == CPP_MACRO_ARG)
1738 len += NODE_LEN (macro->params[token->val.arg_no - 1]);
1740 len += cpp_token_len (token);
1742 if (token->flags & STRINGIFY_ARG)
1744 if (token->flags & PASTE_LEFT)
1745 len += 3; /* " ##" */
1746 if (token->flags & PREV_WHITE)
1751 if (len > pfile->macro_buffer_len)
1753 pfile->macro_buffer = XRESIZEVEC (unsigned char,
1754 pfile->macro_buffer, len);
1755 pfile->macro_buffer_len = len;
1758 /* Fill in the buffer. Start with the macro name. */
1759 buffer = pfile->macro_buffer;
1760 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1761 buffer += NODE_LEN (node);
1763 /* Parameter names. */
1764 if (macro->fun_like)
1767 for (i = 0; i < macro->paramc; i++)
1769 cpp_hashnode *param = macro->params[i];
1771 if (param != pfile->spec_nodes.n__VA_ARGS__)
1773 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1774 buffer += NODE_LEN (param);
1777 if (i + 1 < macro->paramc)
1778 /* Don't emit a space after the comma here; we're trying
1779 to emit a Dwarf-friendly definition, and the Dwarf spec
1780 forbids spaces in the argument list. */
1782 else if (macro->variadic)
1783 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1788 /* The Dwarf spec requires a space after the macro name, even if the
1789 definition is the empty string. */
1792 if (CPP_OPTION (pfile, traditional))
1793 buffer = _cpp_copy_replacement_text (macro, buffer);
1794 else if (macro->count)
1795 /* Expansion tokens. */
1797 for (i = 0; i < macro->count; i++)
1799 cpp_token *token = ¯o->exp.tokens[i];
1801 if (token->flags & PREV_WHITE)
1803 if (token->flags & STRINGIFY_ARG)
1806 if (token->type == CPP_MACRO_ARG)
1809 NODE_NAME (macro->params[token->val.arg_no - 1]),
1810 NODE_LEN (macro->params[token->val.arg_no - 1]));
1811 buffer += NODE_LEN (macro->params[token->val.arg_no - 1]);
1814 buffer = cpp_spell_token (pfile, token, buffer, false);
1816 if (token->flags & PASTE_LEFT)
1821 /* Next has PREV_WHITE; see _cpp_create_definition. */
1827 return pfile->macro_buffer;