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 Free Software Foundation, Inc.
4 Written by Per Bothner, 1994.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 In other words, you are welcome to use, share and improve this program.
23 You are forbidden to forbid anyone else to use, share and improve
24 what you give them. Help stamp out software-hoarding! */
31 typedef struct macro_arg macro_arg;
34 const cpp_token **first; /* First token in unexpanded argument. */
35 const cpp_token **expanded; /* Macro-expanded argument. */
36 const cpp_token *stringified; /* Stringified argument. */
37 unsigned int count; /* # of tokens in argument. */
38 unsigned int expanded_count; /* # of tokens in expanded argument. */
41 /* Macro expansion. */
43 static int enter_macro_context PARAMS ((cpp_reader *, cpp_hashnode *));
44 static int builtin_macro PARAMS ((cpp_reader *, cpp_hashnode *));
45 static void push_token_context
46 PARAMS ((cpp_reader *, cpp_hashnode *, const cpp_token *, unsigned int));
47 static void push_ptoken_context
48 PARAMS ((cpp_reader *, cpp_hashnode *, _cpp_buff *,
49 const cpp_token **, unsigned int));
50 static _cpp_buff *collect_args PARAMS ((cpp_reader *, const cpp_hashnode *));
51 static cpp_context *next_context PARAMS ((cpp_reader *));
52 static const cpp_token *padding_token
53 PARAMS ((cpp_reader *, const cpp_token *));
54 static void expand_arg PARAMS ((cpp_reader *, macro_arg *));
55 static const cpp_token *new_string_token PARAMS ((cpp_reader *, uchar *,
57 static const cpp_token *stringify_arg PARAMS ((cpp_reader *, macro_arg *));
58 static void paste_all_tokens PARAMS ((cpp_reader *, const cpp_token *));
59 static bool paste_tokens PARAMS ((cpp_reader *, const cpp_token **,
61 static void replace_args PARAMS ((cpp_reader *, cpp_hashnode *, cpp_macro *,
63 static _cpp_buff *funlike_invocation_p PARAMS ((cpp_reader *, cpp_hashnode *));
64 static bool create_iso_definition PARAMS ((cpp_reader *, cpp_macro *));
66 /* #define directive parsing and handling. */
68 static cpp_token *alloc_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
69 static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
70 static bool warn_of_redefinition PARAMS ((cpp_reader *, const cpp_hashnode *,
72 static bool parse_params PARAMS ((cpp_reader *, cpp_macro *));
73 static void check_trad_stringification PARAMS ((cpp_reader *,
77 /* Emits a warning if NODE is a macro defined in the main file that
80 _cpp_warn_if_unused_macro (pfile, node, v)
83 void *v ATTRIBUTE_UNUSED;
85 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
87 cpp_macro *macro = node->value.macro;
90 /* Skip front-end built-ins and command line macros. */
91 && macro->line >= pfile->first_unused_line
92 && MAIN_FILE_P (lookup_line (&pfile->line_maps, macro->line)))
93 cpp_error_with_line (pfile, DL_WARNING, macro->line, 0,
94 "macro \"%s\" is not used", NODE_NAME (node));
100 /* Allocates and returns a CPP_STRING token, containing TEXT of length
101 LEN, after null-terminating it. TEXT must be in permanent storage. */
102 static const cpp_token *
103 new_string_token (pfile, text, len)
108 cpp_token *token = _cpp_temp_token (pfile);
111 token->type = CPP_STRING;
112 token->val.str.len = len;
113 token->val.str.text = text;
118 static const char * const monthnames[] =
120 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
121 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
124 /* Handle builtin macros like __FILE__, and push the resulting token
125 on the context stack. Also handles _Pragma, for which no new token
126 is created. Returns 1 if it generates a new token context, 0 to
127 return the token to the caller. */
129 _cpp_builtin_macro_text (pfile, node)
133 const uchar *result = NULL;
134 unsigned int number = 1;
136 switch (node->value.builtin)
139 cpp_error (pfile, DL_ICE, "invalid built-in macro \"%s\"",
149 const struct line_map *map = pfile->map;
151 if (node->value.builtin == BT_BASE_FILE)
152 while (! MAIN_FILE_P (map))
153 map = INCLUDED_FROM (&pfile->line_maps, map);
157 buf = _cpp_unaligned_alloc (pfile, len * 4 + 3);
160 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
166 case BT_INCLUDE_LEVEL:
167 /* The line map depth counts the primary source as level 1, but
168 historically __INCLUDE_DEPTH__ has called the primary source
170 number = pfile->line_maps.depth - 1;
174 /* If __LINE__ is embedded in a macro, it must expand to the
175 line of the macro's invocation, not its definition.
176 Otherwise things like assert() will not work properly. */
177 if (CPP_OPTION (pfile, traditional))
178 number = pfile->line;
180 number = pfile->cur_token[-1].line;
181 number = SOURCE_LINE (pfile->map, number);
184 /* __STDC__ has the value 1 under normal circumstances.
185 However, if (a) we are in a system header, (b) the option
186 stdc_0_in_system_headers is true (set by target config), and
187 (c) we are not in strictly conforming mode, then it has the
191 if (CPP_IN_SYSTEM_HEADER (pfile)
192 && CPP_OPTION (pfile, stdc_0_in_system_headers)
193 && !CPP_OPTION (pfile,std))
202 if (pfile->date == NULL)
204 /* Allocate __DATE__ and __TIME__ strings from permanent
205 storage. We only do this once, and don't generate them
206 at init time, because time() and localtime() are very
207 slow on some systems. */
209 struct tm *tb = NULL;
211 /* (time_t) -1 is a legitimate value for "number of seconds
212 since the Epoch", so we have to do a little dance to
213 distinguish that from a genuine error. */
216 if (tt != (time_t)-1 || errno == 0)
217 tb = localtime (&tt);
221 pfile->date = _cpp_unaligned_alloc (pfile,
222 sizeof ("\"Oct 11 1347\""));
223 sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
224 monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
226 pfile->time = _cpp_unaligned_alloc (pfile,
227 sizeof ("\"12:34:56\""));
228 sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
229 tb->tm_hour, tb->tm_min, tb->tm_sec);
233 cpp_errno (pfile, DL_WARNING,
234 "could not determine date and time");
236 pfile->date = U"\"??? ?? ????\"";
237 pfile->time = U"\"??:??:??\"";
241 if (node->value.builtin == BT_DATE)
242 result = pfile->date;
244 result = pfile->time;
250 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
251 result = _cpp_unaligned_alloc (pfile, 21);
252 sprintf ((char *) result, "%u", number);
258 /* Convert builtin macros like __FILE__ to a token and push it on the
259 context stack. Also handles _Pragma, for which no new token is
260 created. Returns 1 if it generates a new token context, 0 to
261 return the token to the caller. */
263 builtin_macro (pfile, node)
269 if (node->value.builtin == BT_PRAGMA)
271 /* Don't interpret _Pragma within directives. The standard is
272 not clear on this, but to me this makes most sense. */
273 if (pfile->state.in_directive)
276 _cpp_do__Pragma (pfile);
280 buf = _cpp_builtin_macro_text (pfile, node);
282 cpp_push_buffer (pfile, buf, ustrlen (buf), /* from_stage3 */ true, 1);
284 /* Tweak the column number the lexer will report. */
285 pfile->buffer->col_adjust = pfile->cur_token[-1].col - 1;
287 /* We don't want a leading # to be interpreted as a directive. */
288 pfile->buffer->saved_flags = 0;
290 /* Set pfile->cur_token as required by _cpp_lex_direct. */
291 pfile->cur_token = _cpp_temp_token (pfile);
292 push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1);
293 if (pfile->buffer->cur != pfile->buffer->rlimit)
294 cpp_error (pfile, DL_ICE, "invalid built-in macro \"%s\"",
296 _cpp_pop_buffer (pfile);
301 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
302 backslashes and double quotes. Non-printable characters are
303 converted to octal. DEST must be of sufficient size. Returns
304 a pointer to the end of the string. */
306 cpp_quote_string (dest, src, len)
315 if (c == '\\' || c == '"')
326 sprintf ((char *) dest, "\\%03o", c);
335 /* Convert a token sequence ARG to a single string token according to
336 the rules of the ISO C #-operator. */
337 static const cpp_token *
338 stringify_arg (pfile, arg)
342 unsigned char *dest = BUFF_FRONT (pfile->u_buff);
343 unsigned int i, escape_it, backslash_count = 0;
344 const cpp_token *source = NULL;
347 /* Loop, reading in the argument's tokens. */
348 for (i = 0; i < arg->count; i++)
350 const cpp_token *token = arg->first[i];
352 if (token->type == CPP_PADDING)
355 source = token->val.source;
359 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
360 || token->type == CPP_CHAR || token->type == CPP_WCHAR);
362 /* Room for each char being written in octal, initial space and
364 len = cpp_token_len (token);
369 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
371 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
372 _cpp_extend_buff (pfile, &pfile->u_buff, len);
373 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
376 /* Leading white space? */
377 if (dest != BUFF_FRONT (pfile->u_buff))
381 if (source->flags & PREV_WHITE)
388 _cpp_buff *buff = _cpp_get_buff (pfile, len);
389 unsigned char *buf = BUFF_FRONT (buff);
390 len = cpp_spell_token (pfile, token, buf) - buf;
391 dest = cpp_quote_string (dest, buf, len);
392 _cpp_release_buff (pfile, buff);
395 dest = cpp_spell_token (pfile, token, dest);
397 if (token->type == CPP_OTHER && token->val.c == '\\')
403 /* Ignore the final \ of invalid string literals. */
404 if (backslash_count & 1)
406 cpp_error (pfile, DL_WARNING,
407 "invalid string literal, ignoring final '\\'");
411 /* Commit the memory, including NUL, and return the token. */
412 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < 1)
414 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
415 _cpp_extend_buff (pfile, &pfile->u_buff, 1);
416 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
418 len = dest - BUFF_FRONT (pfile->u_buff);
419 BUFF_FRONT (pfile->u_buff) = dest + 1;
420 return new_string_token (pfile, dest - len, len);
423 /* Try to paste two tokens. On success, return nonzero. In any
424 case, PLHS is updated to point to the pasted token, which is
425 guaranteed to not have the PASTE_LEFT flag set. */
427 paste_tokens (pfile, plhs, rhs)
429 const cpp_token **plhs, *rhs;
431 unsigned char *buf, *end;
432 const cpp_token *lhs;
437 len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
438 buf = (unsigned char *) alloca (len);
439 end = cpp_spell_token (pfile, lhs, buf);
441 /* Avoid comment headers, since they are still processed in stage 3.
442 It is simpler to insert a space here, rather than modifying the
443 lexer to ignore comments in some circumstances. Simply returning
444 false doesn't work, since we want to clear the PASTE_LEFT flag. */
445 if (lhs->type == CPP_DIV
446 && (rhs->type == CPP_MULT || rhs->type == CPP_DIV))
448 end = cpp_spell_token (pfile, rhs, end);
451 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true, 1);
453 /* Tweak the column number the lexer will report. */
454 pfile->buffer->col_adjust = pfile->cur_token[-1].col - 1;
456 /* We don't want a leading # to be interpreted as a directive. */
457 pfile->buffer->saved_flags = 0;
459 /* Set pfile->cur_token as required by _cpp_lex_direct. */
460 pfile->cur_token = _cpp_temp_token (pfile);
461 *plhs = _cpp_lex_direct (pfile);
462 valid = pfile->buffer->cur == pfile->buffer->rlimit;
463 _cpp_pop_buffer (pfile);
468 /* Handles an arbitrarily long sequence of ## operators, with initial
469 operand LHS. This implementation is left-associative,
470 non-recursive, and finishes a paste before handling succeeding
471 ones. If a paste fails, we back up to the RHS of the failing ##
472 operator before pushing the context containing the result of prior
473 successful pastes, with the effect that the RHS appears in the
474 output stream after the pasted LHS normally. */
476 paste_all_tokens (pfile, lhs)
478 const cpp_token *lhs;
480 const cpp_token *rhs;
481 cpp_context *context = pfile->context;
485 /* Take the token directly from the current context. We can do
486 this, because we are in the replacement list of either an
487 object-like macro, or a function-like macro with arguments
488 inserted. In either case, the constraints to #define
489 guarantee we have at least one more token. */
490 if (context->direct_p)
491 rhs = FIRST (context).token++;
493 rhs = *FIRST (context).ptoken++;
495 if (rhs->type == CPP_PADDING)
498 if (!paste_tokens (pfile, &lhs, rhs))
500 _cpp_backup_tokens (pfile, 1);
502 /* Mandatory error for all apart from assembler. */
503 if (CPP_OPTION (pfile, lang) != CLK_ASM)
504 cpp_error (pfile, DL_ERROR,
505 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
506 cpp_token_as_text (pfile, lhs),
507 cpp_token_as_text (pfile, rhs));
511 while (rhs->flags & PASTE_LEFT);
513 /* Put the resulting token in its own context. */
514 push_token_context (pfile, NULL, lhs, 1);
517 /* Returns TRUE if the number of arguments ARGC supplied in an
518 invocation of the MACRO referenced by NODE is valid. An empty
519 invocation to a macro with no parameters should pass ARGC as zero.
521 Note that MACRO cannot necessarily be deduced from NODE, in case
522 NODE was redefined whilst collecting arguments. */
524 _cpp_arguments_ok (pfile, macro, node, argc)
527 const cpp_hashnode *node;
530 if (argc == macro->paramc)
533 if (argc < macro->paramc)
535 /* As an extension, a rest argument is allowed to not appear in
536 the invocation at all.
537 e.g. #define debug(format, args...) something
540 This is exactly the same as if there had been an empty rest
541 argument - debug("string", ). */
543 if (argc + 1 == macro->paramc && macro->variadic)
545 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
546 cpp_error (pfile, DL_PEDWARN,
547 "ISO C99 requires rest arguments to be used");
551 cpp_error (pfile, DL_ERROR,
552 "macro \"%s\" requires %u arguments, but only %u given",
553 NODE_NAME (node), macro->paramc, argc);
556 cpp_error (pfile, DL_ERROR,
557 "macro \"%s\" passed %u arguments, but takes just %u",
558 NODE_NAME (node), argc, macro->paramc);
563 /* Reads and returns the arguments to a function-like macro
564 invocation. Assumes the opening parenthesis has been processed.
565 If there is an error, emits an appropriate diagnostic and returns
566 NULL. Each argument is terminated by a CPP_EOF token, for the
567 future benefit of expand_arg(). */
569 collect_args (pfile, node)
571 const cpp_hashnode *node;
573 _cpp_buff *buff, *base_buff;
575 macro_arg *args, *arg;
576 const cpp_token *token;
579 macro = node->value.macro;
581 argc = macro->paramc;
584 buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
585 + sizeof (macro_arg)));
587 args = (macro_arg *) buff->base;
588 memset (args, 0, argc * sizeof (macro_arg));
589 buff->cur = (unsigned char *) &args[argc];
590 arg = args, argc = 0;
592 /* Collect the tokens making up each argument. We don't yet know
593 how many arguments have been supplied, whether too many or too
594 few. Hence the slightly bizarre usage of "argc" and "arg". */
597 unsigned int paren_depth = 0;
598 unsigned int ntokens = 0;
601 arg->first = (const cpp_token **) buff->cur;
605 /* Require space for 2 new tokens (including a CPP_EOF). */
606 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
608 buff = _cpp_append_extend_buff (pfile, buff,
609 1000 * sizeof (cpp_token *));
610 arg->first = (const cpp_token **) buff->cur;
613 token = cpp_get_token (pfile);
615 if (token->type == CPP_PADDING)
617 /* Drop leading padding. */
621 else if (token->type == CPP_OPEN_PAREN)
623 else if (token->type == CPP_CLOSE_PAREN)
625 if (paren_depth-- == 0)
628 else if (token->type == CPP_COMMA)
630 /* A comma does not terminate an argument within
631 parentheses or as part of a variable argument. */
633 && ! (macro->variadic && argc == macro->paramc))
636 else if (token->type == CPP_EOF
637 || (token->type == CPP_HASH && token->flags & BOL))
640 arg->first[ntokens++] = token;
643 /* Drop trailing padding. */
644 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
647 arg->count = ntokens;
648 arg->first[ntokens] = &pfile->eof;
650 /* Terminate the argument. Excess arguments loop back and
651 overwrite the final legitimate argument, before failing. */
652 if (argc <= macro->paramc)
654 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
655 if (argc != macro->paramc)
659 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
661 if (token->type == CPP_EOF)
663 /* We still need the CPP_EOF to end directives, and to end
664 pre-expansion of a macro argument. Step back is not
665 unconditional, since we don't want to return a CPP_EOF to our
666 callers at the end of an -include-d file. */
667 if (pfile->context->prev || pfile->state.in_directive)
668 _cpp_backup_tokens (pfile, 1);
669 cpp_error (pfile, DL_ERROR,
670 "unterminated argument list invoking macro \"%s\"",
675 /* A single empty argument is counted as no argument. */
676 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
678 if (_cpp_arguments_ok (pfile, macro, node, argc))
680 /* GCC has special semantics for , ## b where b is a varargs
681 parameter: we remove the comma if b was omitted entirely.
682 If b was merely an empty argument, the comma is retained.
683 If the macro takes just one (varargs) parameter, then we
684 retain the comma only if we are standards conforming.
686 If FIRST is NULL replace_args () swallows the comma. */
687 if (macro->variadic && (argc < macro->paramc
688 || (argc == 1 && args[0].count == 0
689 && !CPP_OPTION (pfile, std))))
690 args[macro->paramc - 1].first = NULL;
695 /* An error occurred. */
696 _cpp_release_buff (pfile, base_buff);
700 /* Search for an opening parenthesis to the macro of NODE, in such a
701 way that, if none is found, we don't lose the information in any
702 intervening padding tokens. If we find the parenthesis, collect
703 the arguments and return the buffer containing them. */
705 funlike_invocation_p (pfile, node)
709 const cpp_token *token, *padding = NULL;
713 token = cpp_get_token (pfile);
714 if (token->type != CPP_PADDING)
717 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
721 if (token->type == CPP_OPEN_PAREN)
723 pfile->state.parsing_args = 2;
724 return collect_args (pfile, node);
727 /* CPP_EOF can be the end of macro arguments, or the end of the
728 file. We mustn't back up over the latter. Ugh. */
729 if (token->type != CPP_EOF || token == &pfile->eof)
731 /* Back up. We may have skipped padding, in which case backing
732 up more than one token when expanding macros is in general
733 too difficult. We re-insert it in its own context. */
734 _cpp_backup_tokens (pfile, 1);
736 push_token_context (pfile, NULL, padding, 1);
742 /* Push the context of a macro with hash entry NODE onto the context
743 stack. If we can successfully expand the macro, we push a context
744 containing its yet-to-be-rescanned replacement list and return one.
745 Otherwise, we don't push a context and return zero. */
747 enter_macro_context (pfile, node)
751 /* The presence of a macro invalidates a file's controlling macro. */
752 pfile->mi_valid = false;
754 pfile->state.angled_headers = false;
756 /* Handle standard macros. */
757 if (! (node->flags & NODE_BUILTIN))
759 cpp_macro *macro = node->value.macro;
765 pfile->state.prevent_expansion++;
766 pfile->keep_tokens++;
767 pfile->state.parsing_args = 1;
768 buff = funlike_invocation_p (pfile, node);
769 pfile->state.parsing_args = 0;
770 pfile->keep_tokens--;
771 pfile->state.prevent_expansion--;
775 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
776 cpp_error (pfile, DL_WARNING,
777 "function-like macro \"%s\" must be used with arguments in traditional C",
783 if (macro->paramc > 0)
784 replace_args (pfile, node, macro, (macro_arg *) buff->base);
785 _cpp_release_buff (pfile, buff);
788 /* Disable the macro within its expansion. */
789 node->flags |= NODE_DISABLED;
793 if (macro->paramc == 0)
794 push_token_context (pfile, node, macro->exp.tokens, macro->count);
799 /* Handle built-in macros and the _Pragma operator. */
800 return builtin_macro (pfile, node);
803 /* Replace the parameters in a function-like macro of NODE with the
804 actual ARGS, and place the result in a newly pushed token context.
805 Expand each argument before replacing, unless it is operated upon
806 by the # or ## operators. */
808 replace_args (pfile, node, macro, args)
814 unsigned int i, total;
815 const cpp_token *src, *limit;
816 const cpp_token **dest, **first;
820 /* First, fully macro-expand arguments, calculating the number of
821 tokens in the final expansion as we go. The ordering of the if
822 statements below is subtle; we must handle stringification before
824 total = macro->count;
825 limit = macro->exp.tokens + macro->count;
827 for (src = macro->exp.tokens; src < limit; src++)
828 if (src->type == CPP_MACRO_ARG)
830 /* Leading and trailing padding tokens. */
833 /* We have an argument. If it is not being stringified or
834 pasted it is macro-replaced before insertion. */
835 arg = &args[src->val.arg_no - 1];
837 if (src->flags & STRINGIFY_ARG)
839 if (!arg->stringified)
840 arg->stringified = stringify_arg (pfile, arg);
842 else if ((src->flags & PASTE_LEFT)
843 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
844 total += arg->count - 1;
848 expand_arg (pfile, arg);
849 total += arg->expanded_count - 1;
853 /* Now allocate space for the expansion, copy the tokens and replace
855 buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
856 first = (const cpp_token **) buff->base;
859 for (src = macro->exp.tokens; src < limit; src++)
862 const cpp_token **from, **paste_flag;
864 if (src->type != CPP_MACRO_ARG)
871 arg = &args[src->val.arg_no - 1];
872 if (src->flags & STRINGIFY_ARG)
873 count = 1, from = &arg->stringified;
874 else if (src->flags & PASTE_LEFT)
875 count = arg->count, from = arg->first;
876 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
878 count = arg->count, from = arg->first;
881 if (dest[-1]->type == CPP_COMMA
883 && src->val.arg_no == macro->paramc)
885 /* Swallow a pasted comma if from == NULL, otherwise
886 drop the paste flag. */
890 paste_flag = dest - 1;
892 /* Remove the paste flag if the RHS is a placemarker. */
894 paste_flag = dest - 1;
898 count = arg->expanded_count, from = arg->expanded;
900 /* Padding on the left of an argument (unless RHS of ##). */
901 if (!pfile->state.in_directive
902 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
903 *dest++ = padding_token (pfile, src);
907 memcpy (dest, from, count * sizeof (cpp_token *));
910 /* With a non-empty argument on the LHS of ##, the last
911 token should be flagged PASTE_LEFT. */
912 if (src->flags & PASTE_LEFT)
913 paste_flag = dest - 1;
916 /* Avoid paste on RHS (even case count == 0). */
917 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
918 *dest++ = &pfile->avoid_paste;
920 /* Add a new paste flag, or remove an unwanted one. */
923 cpp_token *token = _cpp_temp_token (pfile);
924 token->type = (*paste_flag)->type;
925 token->val.str = (*paste_flag)->val.str;
926 if (src->flags & PASTE_LEFT)
927 token->flags = (*paste_flag)->flags | PASTE_LEFT;
929 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
934 /* Free the expanded arguments. */
935 for (i = 0; i < macro->paramc; i++)
936 if (args[i].expanded)
937 free (args[i].expanded);
939 push_ptoken_context (pfile, node, buff, first, dest - first);
942 /* Return a special padding token, with padding inherited from SOURCE. */
943 static const cpp_token *
944 padding_token (pfile, source)
946 const cpp_token *source;
948 cpp_token *result = _cpp_temp_token (pfile);
950 result->type = CPP_PADDING;
951 result->val.source = source;
956 /* Get a new uninitialized context. Create a new one if we cannot
957 re-use an old one. */
962 cpp_context *result = pfile->context->next;
966 result = xnew (cpp_context);
967 result->prev = pfile->context;
969 pfile->context->next = result;
972 pfile->context = result;
976 /* Push a list of pointers to tokens. */
978 push_ptoken_context (pfile, macro, buff, first, count)
982 const cpp_token **first;
985 cpp_context *context = next_context (pfile);
987 context->direct_p = false;
988 context->macro = macro;
989 context->buff = buff;
990 FIRST (context).ptoken = first;
991 LAST (context).ptoken = first + count;
994 /* Push a list of tokens. */
996 push_token_context (pfile, macro, first, count)
999 const cpp_token *first;
1002 cpp_context *context = next_context (pfile);
1004 context->direct_p = true;
1005 context->macro = macro;
1006 context->buff = NULL;
1007 FIRST (context).token = first;
1008 LAST (context).token = first + count;
1011 /* Push a traditional macro's replacement text. */
1013 _cpp_push_text_context (pfile, macro, start, len)
1015 cpp_hashnode *macro;
1019 cpp_context *context = next_context (pfile);
1021 context->direct_p = true;
1022 context->macro = macro;
1023 context->buff = NULL;
1024 CUR (context) = start;
1025 RLIMIT (context) = start + len;
1026 macro->flags |= NODE_DISABLED;
1029 /* Expand an argument ARG before replacing parameters in a
1030 function-like macro. This works by pushing a context with the
1031 argument's tokens, and then expanding that into a temporary buffer
1032 as if it were a normal part of the token stream. collect_args()
1033 has terminated the argument's tokens with a CPP_EOF so that we know
1034 when we have fully expanded the argument. */
1036 expand_arg (pfile, arg)
1040 unsigned int capacity;
1041 bool saved_warn_trad;
1043 if (arg->count == 0)
1046 /* Don't warn about funlike macros when pre-expanding. */
1047 saved_warn_trad = CPP_WTRADITIONAL (pfile);
1048 CPP_WTRADITIONAL (pfile) = 0;
1050 /* Loop, reading in the arguments. */
1052 arg->expanded = (const cpp_token **)
1053 xmalloc (capacity * sizeof (cpp_token *));
1055 push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
1058 const cpp_token *token;
1060 if (arg->expanded_count + 1 >= capacity)
1063 arg->expanded = (const cpp_token **)
1064 xrealloc (arg->expanded, capacity * sizeof (cpp_token *));
1067 token = cpp_get_token (pfile);
1069 if (token->type == CPP_EOF)
1072 arg->expanded[arg->expanded_count++] = token;
1075 _cpp_pop_context (pfile);
1077 CPP_WTRADITIONAL (pfile) = saved_warn_trad;
1080 /* Pop the current context off the stack, re-enabling the macro if the
1081 context represented a macro's replacement list. The context
1082 structure is not freed so that we can re-use it later. */
1084 _cpp_pop_context (pfile)
1087 cpp_context *context = pfile->context;
1090 context->macro->flags &= ~NODE_DISABLED;
1093 _cpp_release_buff (pfile, context->buff);
1095 pfile->context = context->prev;
1098 /* Eternal routine to get a token. Also used nearly everywhere
1099 internally, except for places where we know we can safely call
1100 the lexer directly, such as lexing a directive name.
1102 Macro expansions and directives are transparently handled,
1103 including entering included files. Thus tokens are post-macro
1104 expansion, and after any intervening directives. External callers
1105 see CPP_EOF only at EOF. Internal callers also see it when meeting
1106 a directive inside a macro call, when at the end of a directive and
1107 state.in_directive is still 1, and at the end of argument
1110 cpp_get_token (pfile)
1113 const cpp_token *result;
1118 cpp_context *context = pfile->context;
1120 /* Context->prev == 0 <=> base context. */
1122 result = _cpp_lex_token (pfile);
1123 else if (FIRST (context).token != LAST (context).token)
1125 if (context->direct_p)
1126 result = FIRST (context).token++;
1128 result = *FIRST (context).ptoken++;
1130 if (result->flags & PASTE_LEFT)
1132 paste_all_tokens (pfile, result);
1133 if (pfile->state.in_directive)
1135 return padding_token (pfile, result);
1140 _cpp_pop_context (pfile);
1141 if (pfile->state.in_directive)
1143 return &pfile->avoid_paste;
1146 if (pfile->state.in_directive && result->type == CPP_COMMENT)
1149 if (result->type != CPP_NAME)
1152 node = result->val.node;
1154 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
1157 if (!(node->flags & NODE_DISABLED))
1159 if (!pfile->state.prevent_expansion
1160 && enter_macro_context (pfile, node))
1162 if (pfile->state.in_directive)
1164 return padding_token (pfile, result);
1169 /* Flag this token as always unexpandable. FIXME: move this
1170 to collect_args()?. */
1171 cpp_token *t = _cpp_temp_token (pfile);
1172 t->type = result->type;
1173 t->flags = result->flags | NO_EXPAND;
1174 t->val.str = result->val.str;
1184 /* Returns true if we're expanding an object-like macro that was
1185 defined in a system header. Just checks the macro at the top of
1186 the stack. Used for diagnostic suppression. */
1188 cpp_sys_macro_p (pfile)
1191 cpp_hashnode *node = pfile->context->macro;
1193 return node && node->value.macro && node->value.macro->syshdr;
1196 /* Read each token in, until end of the current file. Directives are
1197 transparently processed. */
1199 cpp_scan_nooutput (pfile)
1202 /* Request a CPP_EOF token at the end of this file, rather than
1203 transparently continuing with the including file. */
1204 pfile->buffer->return_at_eof = true;
1206 if (CPP_OPTION (pfile, traditional))
1207 while (_cpp_read_logical_line_trad (pfile))
1210 while (cpp_get_token (pfile)->type != CPP_EOF)
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 (pfile, count)
1221 if (pfile->context->prev == NULL)
1223 pfile->lookaheads += count;
1227 if (pfile->cur_token == pfile->cur_run->base
1228 /* Possible with -fpreprocessed and no leading #line. */
1229 && pfile->cur_run->prev != NULL)
1231 pfile->cur_run = pfile->cur_run->prev;
1232 pfile->cur_token = pfile->cur_run->limit;
1240 if (pfile->context->direct_p)
1241 FIRST (pfile->context).token--;
1243 FIRST (pfile->context).ptoken--;
1247 /* #define directive parsing and handling. */
1249 /* Returns nonzero if a macro redefinition warning is required. */
1251 warn_of_redefinition (pfile, node, macro2)
1253 const cpp_hashnode *node;
1254 const cpp_macro *macro2;
1256 const cpp_macro *macro1;
1259 /* Some redefinitions need to be warned about regardless. */
1260 if (node->flags & NODE_WARN)
1263 /* Redefinition of a macro is allowed if and only if the old and new
1264 definitions are the same. (6.10.3 paragraph 2). */
1265 macro1 = node->value.macro;
1267 /* Don't check count here as it can be different in valid
1268 traditional redefinitions with just whitespace differences. */
1269 if (macro1->paramc != macro2->paramc
1270 || macro1->fun_like != macro2->fun_like
1271 || macro1->variadic != macro2->variadic)
1274 /* Check parameter spellings. */
1275 for (i = 0; i < macro1->paramc; i++)
1276 if (macro1->params[i] != macro2->params[i])
1279 /* Check the replacement text or tokens. */
1280 if (CPP_OPTION (pfile, traditional))
1281 return _cpp_expansions_different_trad (macro1, macro2);
1283 if (macro1->count == macro2->count)
1284 for (i = 0; i < macro1->count; i++)
1285 if (!_cpp_equiv_tokens (¯o1->exp.tokens[i], ¯o2->exp.tokens[i]))
1291 /* Free the definition of hashnode H. */
1293 _cpp_free_definition (h)
1296 /* Macros and assertions no longer have anything to free. */
1298 /* Clear builtin flag in case of redefinition. */
1299 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED);
1302 /* Save parameter NODE to the parameter list of macro MACRO. Returns
1303 zero on success, nonzero if the parameter is a duplicate. */
1305 _cpp_save_parameter (pfile, macro, node)
1310 /* Constraint 6.10.3.6 - duplicate parameter names. */
1311 if (node->arg_index)
1313 cpp_error (pfile, DL_ERROR, "duplicate macro parameter \"%s\"",
1318 if (BUFF_ROOM (pfile->a_buff)
1319 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1320 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
1322 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
1323 node->arg_index = macro->paramc;
1327 /* Check the syntax of the parameters in a MACRO definition. Returns
1328 false if an error occurs. */
1330 parse_params (pfile, macro)
1334 unsigned int prev_ident = 0;
1338 const cpp_token *token = _cpp_lex_token (pfile);
1340 switch (token->type)
1343 /* Allow/ignore comments in parameter lists if we are
1344 preserving comments in macro expansions. */
1345 if (token->type == CPP_COMMENT
1346 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
1349 cpp_error (pfile, DL_ERROR,
1350 "\"%s\" may not appear in macro parameter list",
1351 cpp_token_as_text (pfile, token));
1357 cpp_error (pfile, DL_ERROR,
1358 "macro parameters must be comma-separated");
1363 if (_cpp_save_parameter (pfile, macro, token->val.node))
1367 case CPP_CLOSE_PAREN:
1368 if (prev_ident || macro->paramc == 0)
1371 /* Fall through to pick up the error. */
1375 cpp_error (pfile, DL_ERROR, "parameter name missing");
1382 macro->variadic = 1;
1385 _cpp_save_parameter (pfile, macro,
1386 pfile->spec_nodes.n__VA_ARGS__);
1387 pfile->state.va_args_ok = 1;
1388 if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
1389 cpp_error (pfile, DL_PEDWARN,
1390 "anonymous variadic macros were introduced in C99");
1392 else if (CPP_OPTION (pfile, pedantic))
1393 cpp_error (pfile, DL_PEDWARN,
1394 "ISO C does not permit named variadic macros");
1396 /* We're at the end, and just expect a closing parenthesis. */
1397 token = _cpp_lex_token (pfile);
1398 if (token->type == CPP_CLOSE_PAREN)
1403 cpp_error (pfile, DL_ERROR, "missing ')' in macro parameter list");
1409 /* Allocate room for a token from a macro's replacement list. */
1411 alloc_expansion_token (pfile, macro)
1415 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1416 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
1418 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
1421 /* Lex a token from the expansion of MACRO, but mark parameters as we
1422 find them and warn of traditional stringification. */
1424 lex_expansion_token (pfile, macro)
1430 pfile->cur_token = alloc_expansion_token (pfile, macro);
1431 token = _cpp_lex_direct (pfile);
1433 /* Is this a parameter? */
1434 if (token->type == CPP_NAME && token->val.node->arg_index)
1436 token->type = CPP_MACRO_ARG;
1437 token->val.arg_no = token->val.node->arg_index;
1439 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1440 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1441 check_trad_stringification (pfile, macro, &token->val.str);
1447 create_iso_definition (pfile, macro)
1452 const cpp_token *ctoken;
1454 /* Get the first token of the expansion (or the '(' of a
1455 function-like macro). */
1456 ctoken = _cpp_lex_token (pfile);
1458 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
1460 bool ok = parse_params (pfile, macro);
1461 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1465 /* Success. Commit the parameter array. */
1466 BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->params[macro->paramc];
1467 macro->fun_like = 1;
1469 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
1470 cpp_error (pfile, DL_PEDWARN,
1471 "ISO C requires whitespace after the macro name");
1473 if (macro->fun_like)
1474 token = lex_expansion_token (pfile, macro);
1477 token = alloc_expansion_token (pfile, macro);
1483 /* Check the stringifying # constraint 6.10.3.2.1 of
1484 function-like macros when lexing the subsequent token. */
1485 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1487 if (token->type == CPP_MACRO_ARG)
1489 token->flags &= ~PREV_WHITE;
1490 token->flags |= STRINGIFY_ARG;
1491 token->flags |= token[-1].flags & PREV_WHITE;
1492 token[-1] = token[0];
1495 /* Let assembler get away with murder. */
1496 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
1498 cpp_error (pfile, DL_ERROR,
1499 "'#' is not followed by a macro parameter");
1504 if (token->type == CPP_EOF)
1507 /* Paste operator constraint 6.10.3.3.1. */
1508 if (token->type == CPP_PASTE)
1510 /* Token-paste ##, can appear in both object-like and
1511 function-like macros, but not at the ends. */
1512 if (--macro->count > 0)
1513 token = lex_expansion_token (pfile, macro);
1515 if (macro->count == 0 || token->type == CPP_EOF)
1517 cpp_error (pfile, DL_ERROR,
1518 "'##' cannot appear at either end of a macro expansion");
1522 token[-1].flags |= PASTE_LEFT;
1525 token = lex_expansion_token (pfile, macro);
1528 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
1530 /* Don't count the CPP_EOF. */
1533 /* Clear whitespace on first token for warn_of_redefinition(). */
1535 macro->exp.tokens[0].flags &= ~PREV_WHITE;
1537 /* Commit the memory. */
1538 BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->exp.tokens[macro->count];
1543 /* Parse a macro and save its expansion. Returns nonzero on success. */
1545 _cpp_create_definition (pfile, node)
1553 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
1554 macro->line = pfile->directive_line;
1557 macro->variadic = 0;
1560 macro->fun_like = 0;
1561 /* To suppress some diagnostics. */
1562 macro->syshdr = pfile->map->sysp != 0;
1564 if (CPP_OPTION (pfile, traditional))
1565 ok = _cpp_create_trad_definition (pfile, macro);
1568 cpp_token *saved_cur_token = pfile->cur_token;
1570 ok = create_iso_definition (pfile, macro);
1572 /* Restore lexer position because of games lex_expansion_token()
1573 plays lexing the macro. We set the type for SEEN_EOL() in
1576 Longer term we should lex the whole line before coming here,
1577 and just copy the expansion. */
1578 saved_cur_token[-1].type = pfile->cur_token[-1].type;
1579 pfile->cur_token = saved_cur_token;
1581 /* Stop the lexer accepting __VA_ARGS__. */
1582 pfile->state.va_args_ok = 0;
1585 /* Clear the fast argument lookup indices. */
1586 for (i = macro->paramc; i-- > 0; )
1587 macro->params[i]->arg_index = 0;
1592 if (node->type == NT_MACRO)
1594 if (CPP_OPTION (pfile, warn_unused_macros))
1595 _cpp_warn_if_unused_macro (pfile, node, NULL);
1597 if (warn_of_redefinition (pfile, node, macro))
1599 cpp_error_with_line (pfile, DL_PEDWARN, pfile->directive_line, 0,
1600 "\"%s\" redefined", NODE_NAME (node));
1602 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1603 cpp_error_with_line (pfile, DL_PEDWARN,
1604 node->value.macro->line, 0,
1605 "this is the location of the previous definition");
1609 if (node->type != NT_VOID)
1610 _cpp_free_definition (node);
1612 /* Enter definition in hash table. */
1613 node->type = NT_MACRO;
1614 node->value.macro = macro;
1615 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
1616 node->flags |= NODE_WARN;
1621 /* Warn if a token in STRING matches one of a function-like MACRO's
1624 check_trad_stringification (pfile, macro, string)
1626 const cpp_macro *macro;
1627 const cpp_string *string;
1629 unsigned int i, len;
1630 const uchar *p, *q, *limit = string->text + string->len;
1632 /* Loop over the string. */
1633 for (p = string->text; p < limit; p = q)
1635 /* Find the start of an identifier. */
1636 while (p < limit && !is_idstart (*p))
1639 /* Find the end of the identifier. */
1641 while (q < limit && is_idchar (*q))
1646 /* Loop over the function macro arguments to see if the
1647 identifier inside the string matches one of them. */
1648 for (i = 0; i < macro->paramc; i++)
1650 const cpp_hashnode *node = macro->params[i];
1652 if (NODE_LEN (node) == len
1653 && !memcmp (p, NODE_NAME (node), len))
1655 cpp_error (pfile, DL_WARNING,
1656 "macro argument \"%s\" would be stringified in traditional C",
1664 /* Returns the name, arguments and expansion of a macro, in a format
1665 suitable to be read back in again, and therefore also for DWARF 2
1666 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1667 Caller is expected to generate the "#define" bit if needed. The
1668 returned text is temporary, and automatically freed later. */
1669 const unsigned char *
1670 cpp_macro_definition (pfile, node)
1672 const cpp_hashnode *node;
1674 unsigned int i, len;
1675 const cpp_macro *macro = node->value.macro;
1676 unsigned char *buffer;
1678 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1680 cpp_error (pfile, DL_ICE,
1681 "invalid hash type %d in cpp_macro_definition", node->type);
1685 /* Calculate length. */
1686 len = NODE_LEN (node) + 2; /* ' ' and NUL. */
1687 if (macro->fun_like)
1689 len += 4; /* "()" plus possible final ".." of named
1690 varargs (we have + 1 below). */
1691 for (i = 0; i < macro->paramc; i++)
1692 len += NODE_LEN (macro->params[i]) + 1; /* "," */
1695 if (CPP_OPTION (pfile, traditional))
1696 len += _cpp_replacement_text_len (macro);
1699 for (i = 0; i < macro->count; i++)
1701 cpp_token *token = ¯o->exp.tokens[i];
1703 if (token->type == CPP_MACRO_ARG)
1704 len += NODE_LEN (macro->params[token->val.arg_no - 1]);
1706 len += cpp_token_len (token); /* Includes room for ' '. */
1707 if (token->flags & STRINGIFY_ARG)
1709 if (token->flags & PASTE_LEFT)
1710 len += 3; /* " ##" */
1714 if (len > pfile->macro_buffer_len)
1716 pfile->macro_buffer = (uchar *) xrealloc (pfile->macro_buffer, len);
1717 pfile->macro_buffer_len = len;
1720 /* Fill in the buffer. Start with the macro name. */
1721 buffer = pfile->macro_buffer;
1722 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1723 buffer += NODE_LEN (node);
1725 /* Parameter names. */
1726 if (macro->fun_like)
1729 for (i = 0; i < macro->paramc; i++)
1731 cpp_hashnode *param = macro->params[i];
1733 if (param != pfile->spec_nodes.n__VA_ARGS__)
1735 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1736 buffer += NODE_LEN (param);
1739 if (i + 1 < macro->paramc)
1740 /* Don't emit a space after the comma here; we're trying
1741 to emit a Dwarf-friendly definition, and the Dwarf spec
1742 forbids spaces in the argument list. */
1744 else if (macro->variadic)
1745 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1750 /* The Dwarf spec requires a space after the macro name, even if the
1751 definition is the empty string. */
1754 if (CPP_OPTION (pfile, traditional))
1755 buffer = _cpp_copy_replacement_text (macro, buffer);
1756 else if (macro->count)
1757 /* Expansion tokens. */
1759 for (i = 0; i < macro->count; i++)
1761 cpp_token *token = ¯o->exp.tokens[i];
1763 if (token->flags & PREV_WHITE)
1765 if (token->flags & STRINGIFY_ARG)
1768 if (token->type == CPP_MACRO_ARG)
1770 len = NODE_LEN (macro->params[token->val.arg_no - 1]);
1772 NODE_NAME (macro->params[token->val.arg_no - 1]), len);
1776 buffer = cpp_spell_token (pfile, token, buffer);
1778 if (token->flags & PASTE_LEFT)
1783 /* Next has PREV_WHITE; see _cpp_create_definition. */
1789 return pfile->macro_buffer;