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 enum c_lang lang = CPP_OPTION (pfile, lang);
192 if (CPP_IN_SYSTEM_HEADER (pfile)
193 && CPP_OPTION (pfile, stdc_0_in_system_headers)
194 && !(lang == CLK_STDC89 || lang == CLK_STDC94
195 || lang == CLK_STDC99)) /* || lang == CLK_CXX98 ? */
204 if (pfile->date == NULL)
206 /* Allocate __DATE__ and __TIME__ strings from permanent
207 storage. We only do this once, and don't generate them
208 at init time, because time() and localtime() are very
209 slow on some systems. */
210 time_t tt = time (NULL);
211 struct tm *tb = localtime (&tt);
213 pfile->date = _cpp_unaligned_alloc (pfile,
214 sizeof ("\"Oct 11 1347\""));
215 sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
216 monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
218 pfile->time = _cpp_unaligned_alloc (pfile, sizeof ("\"12:34:56\""));
219 sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
220 tb->tm_hour, tb->tm_min, tb->tm_sec);
223 if (node->value.builtin == BT_DATE)
224 result = pfile->date;
226 result = pfile->time;
232 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
233 result = _cpp_unaligned_alloc (pfile, 21);
234 sprintf ((char *) result, "%u", number);
240 /* Convert builtin macros like __FILE__ to a token and push it on the
241 context stack. Also handles _Pragma, for which no new token is
242 created. Returns 1 if it generates a new token context, 0 to
243 return the token to the caller. */
245 builtin_macro (pfile, node)
251 if (node->value.builtin == BT_PRAGMA)
253 /* Don't interpret _Pragma within directives. The standard is
254 not clear on this, but to me this makes most sense. */
255 if (pfile->state.in_directive)
258 _cpp_do__Pragma (pfile);
262 buf = _cpp_builtin_macro_text (pfile, node);
264 cpp_push_buffer (pfile, buf, ustrlen (buf), /* from_stage3 */ true, 1);
266 /* Tweak the column number the lexer will report. */
267 pfile->buffer->col_adjust = pfile->cur_token[-1].col - 1;
269 /* We don't want a leading # to be interpreted as a directive. */
270 pfile->buffer->saved_flags = 0;
272 /* Set pfile->cur_token as required by _cpp_lex_direct. */
273 pfile->cur_token = _cpp_temp_token (pfile);
274 push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1);
275 if (pfile->buffer->cur != pfile->buffer->rlimit)
276 cpp_error (pfile, DL_ICE, "invalid built-in macro \"%s\"",
278 _cpp_pop_buffer (pfile);
283 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
284 backslashes and double quotes. Non-printable characters are
285 converted to octal. DEST must be of sufficient size. Returns
286 a pointer to the end of the string. */
288 cpp_quote_string (dest, src, len)
297 if (c == '\\' || c == '"')
308 sprintf ((char *) dest, "\\%03o", c);
317 /* Convert a token sequence ARG to a single string token according to
318 the rules of the ISO C #-operator. */
319 static const cpp_token *
320 stringify_arg (pfile, arg)
324 unsigned char *dest = BUFF_FRONT (pfile->u_buff);
325 unsigned int i, escape_it, backslash_count = 0;
326 const cpp_token *source = NULL;
329 /* Loop, reading in the argument's tokens. */
330 for (i = 0; i < arg->count; i++)
332 const cpp_token *token = arg->first[i];
334 if (token->type == CPP_PADDING)
337 source = token->val.source;
341 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
342 || token->type == CPP_CHAR || token->type == CPP_WCHAR);
344 /* Room for each char being written in octal, initial space and
346 len = cpp_token_len (token);
351 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
353 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
354 _cpp_extend_buff (pfile, &pfile->u_buff, len);
355 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
358 /* Leading white space? */
359 if (dest != BUFF_FRONT (pfile->u_buff))
363 if (source->flags & PREV_WHITE)
370 _cpp_buff *buff = _cpp_get_buff (pfile, len);
371 unsigned char *buf = BUFF_FRONT (buff);
372 len = cpp_spell_token (pfile, token, buf) - buf;
373 dest = cpp_quote_string (dest, buf, len);
374 _cpp_release_buff (pfile, buff);
377 dest = cpp_spell_token (pfile, token, dest);
379 if (token->type == CPP_OTHER && token->val.c == '\\')
385 /* Ignore the final \ of invalid string literals. */
386 if (backslash_count & 1)
388 cpp_error (pfile, DL_WARNING,
389 "invalid string literal, ignoring final '\\'");
393 /* Commit the memory, including NUL, and return the token. */
394 len = dest - BUFF_FRONT (pfile->u_buff);
395 BUFF_FRONT (pfile->u_buff) = dest + 1;
396 return new_string_token (pfile, dest - len, len);
399 /* Try to paste two tokens. On success, return non-zero. In any
400 case, PLHS is updated to point to the pasted token, which is
401 guaranteed to not have the PASTE_LEFT flag set. */
403 paste_tokens (pfile, plhs, rhs)
405 const cpp_token **plhs, *rhs;
407 unsigned char *buf, *end;
408 const cpp_token *lhs;
413 len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
414 buf = (unsigned char *) alloca (len);
415 end = cpp_spell_token (pfile, lhs, buf);
417 /* Avoid comment headers, since they are still processed in stage 3.
418 It is simpler to insert a space here, rather than modifying the
419 lexer to ignore comments in some circumstances. Simply returning
420 false doesn't work, since we want to clear the PASTE_LEFT flag. */
421 if (lhs->type == CPP_DIV
422 && (rhs->type == CPP_MULT || rhs->type == CPP_DIV))
424 end = cpp_spell_token (pfile, rhs, end);
427 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true, 1);
429 /* Tweak the column number the lexer will report. */
430 pfile->buffer->col_adjust = pfile->cur_token[-1].col - 1;
432 /* We don't want a leading # to be interpreted as a directive. */
433 pfile->buffer->saved_flags = 0;
435 /* Set pfile->cur_token as required by _cpp_lex_direct. */
436 pfile->cur_token = _cpp_temp_token (pfile);
437 *plhs = _cpp_lex_direct (pfile);
438 valid = pfile->buffer->cur == pfile->buffer->rlimit;
439 _cpp_pop_buffer (pfile);
444 /* Handles an arbitrarily long sequence of ## operators, with initial
445 operand LHS. This implementation is left-associative,
446 non-recursive, and finishes a paste before handling succeeding
447 ones. If a paste fails, we back up to the RHS of the failing ##
448 operator before pushing the context containing the result of prior
449 successful pastes, with the effect that the RHS appears in the
450 output stream after the pasted LHS normally. */
452 paste_all_tokens (pfile, lhs)
454 const cpp_token *lhs;
456 const cpp_token *rhs;
457 cpp_context *context = pfile->context;
461 /* Take the token directly from the current context. We can do
462 this, because we are in the replacement list of either an
463 object-like macro, or a function-like macro with arguments
464 inserted. In either case, the constraints to #define
465 guarantee we have at least one more token. */
466 if (context->direct_p)
467 rhs = FIRST (context).token++;
469 rhs = *FIRST (context).ptoken++;
471 if (rhs->type == CPP_PADDING)
474 if (!paste_tokens (pfile, &lhs, rhs))
476 _cpp_backup_tokens (pfile, 1);
478 /* Mandatory error for all apart from assembler. */
479 if (CPP_OPTION (pfile, lang) != CLK_ASM)
480 cpp_error (pfile, DL_ERROR,
481 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
482 cpp_token_as_text (pfile, lhs),
483 cpp_token_as_text (pfile, rhs));
487 while (rhs->flags & PASTE_LEFT);
489 /* Put the resulting token in its own context. */
490 push_token_context (pfile, NULL, lhs, 1);
493 /* Returns TRUE if the number of arguments ARGC supplied in an
494 invocation of the MACRO referenced by NODE is valid. An empty
495 invocation to a macro with no parameters should pass ARGC as zero.
497 Note that MACRO cannot necessarily be deduced from NODE, in case
498 NODE was redefined whilst collecting arguments. */
500 _cpp_arguments_ok (pfile, macro, node, argc)
503 const cpp_hashnode *node;
506 if (argc == macro->paramc)
509 if (argc < macro->paramc)
511 /* As an extension, a rest argument is allowed to not appear in
512 the invocation at all.
513 e.g. #define debug(format, args...) something
516 This is exactly the same as if there had been an empty rest
517 argument - debug("string", ). */
519 if (argc + 1 == macro->paramc && macro->variadic)
521 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
522 cpp_error (pfile, DL_PEDWARN,
523 "ISO C99 requires rest arguments to be used");
527 cpp_error (pfile, DL_ERROR,
528 "macro \"%s\" requires %u arguments, but only %u given",
529 NODE_NAME (node), macro->paramc, argc);
532 cpp_error (pfile, DL_ERROR,
533 "macro \"%s\" passed %u arguments, but takes just %u",
534 NODE_NAME (node), argc, macro->paramc);
539 /* Reads and returns the arguments to a function-like macro
540 invocation. Assumes the opening parenthesis has been processed.
541 If there is an error, emits an appropriate diagnostic and returns
542 NULL. Each argument is terminated by a CPP_EOF token, for the
543 future benefit of expand_arg(). */
545 collect_args (pfile, node)
547 const cpp_hashnode *node;
549 _cpp_buff *buff, *base_buff;
551 macro_arg *args, *arg;
552 const cpp_token *token;
555 macro = node->value.macro;
557 argc = macro->paramc;
560 buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
561 + sizeof (macro_arg)));
563 args = (macro_arg *) buff->base;
564 memset (args, 0, argc * sizeof (macro_arg));
565 buff->cur = (unsigned char *) &args[argc];
566 arg = args, argc = 0;
568 /* Collect the tokens making up each argument. We don't yet know
569 how many arguments have been supplied, whether too many or too
570 few. Hence the slightly bizarre usage of "argc" and "arg". */
573 unsigned int paren_depth = 0;
574 unsigned int ntokens = 0;
577 arg->first = (const cpp_token **) buff->cur;
581 /* Require space for 2 new tokens (including a CPP_EOF). */
582 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
584 buff = _cpp_append_extend_buff (pfile, buff,
585 1000 * sizeof (cpp_token *));
586 arg->first = (const cpp_token **) buff->cur;
589 token = cpp_get_token (pfile);
591 if (token->type == CPP_PADDING)
593 /* Drop leading padding. */
597 else if (token->type == CPP_OPEN_PAREN)
599 else if (token->type == CPP_CLOSE_PAREN)
601 if (paren_depth-- == 0)
604 else if (token->type == CPP_COMMA)
606 /* A comma does not terminate an argument within
607 parentheses or as part of a variable argument. */
609 && ! (macro->variadic && argc == macro->paramc))
612 else if (token->type == CPP_EOF
613 || (token->type == CPP_HASH && token->flags & BOL))
616 arg->first[ntokens++] = token;
619 /* Drop trailing padding. */
620 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
623 arg->count = ntokens;
624 arg->first[ntokens] = &pfile->eof;
626 /* Terminate the argument. Excess arguments loop back and
627 overwrite the final legitimate argument, before failing. */
628 if (argc <= macro->paramc)
630 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
631 if (argc != macro->paramc)
635 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
637 if (token->type == CPP_EOF)
639 /* We still need the CPP_EOF to end directives, and to end
640 pre-expansion of a macro argument. Step back is not
641 unconditional, since we don't want to return a CPP_EOF to our
642 callers at the end of an -include-d file. */
643 if (pfile->context->prev || pfile->state.in_directive)
644 _cpp_backup_tokens (pfile, 1);
645 cpp_error (pfile, DL_ERROR,
646 "unterminated argument list invoking macro \"%s\"",
651 /* A single empty argument is counted as no argument. */
652 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
654 if (_cpp_arguments_ok (pfile, macro, node, argc))
658 /* An error occurred. */
659 _cpp_release_buff (pfile, base_buff);
663 /* Search for an opening parenthesis to the macro of NODE, in such a
664 way that, if none is found, we don't lose the information in any
665 intervening padding tokens. If we find the parenthesis, collect
666 the arguments and return the buffer containing them. */
668 funlike_invocation_p (pfile, node)
672 const cpp_token *token, *padding = NULL;
676 token = cpp_get_token (pfile);
677 if (token->type != CPP_PADDING)
680 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
684 if (token->type == CPP_OPEN_PAREN)
686 pfile->state.parsing_args = 2;
687 return collect_args (pfile, node);
690 /* CPP_EOF can be the end of macro arguments, or the end of the
691 file. We mustn't back up over the latter. Ugh. */
692 if (token->type != CPP_EOF || token == &pfile->eof)
694 /* Back up. We may have skipped padding, in which case backing
695 up more than one token when expanding macros is in general
696 too difficult. We re-insert it in its own context. */
697 _cpp_backup_tokens (pfile, 1);
699 push_token_context (pfile, NULL, padding, 1);
705 /* Push the context of a macro with hash entry NODE onto the context
706 stack. If we can successfully expand the macro, we push a context
707 containing its yet-to-be-rescanned replacement list and return one.
708 Otherwise, we don't push a context and return zero. */
710 enter_macro_context (pfile, node)
714 /* The presence of a macro invalidates a file's controlling macro. */
715 pfile->mi_valid = false;
717 pfile->state.angled_headers = false;
719 /* Handle standard macros. */
720 if (! (node->flags & NODE_BUILTIN))
722 cpp_macro *macro = node->value.macro;
728 pfile->state.prevent_expansion++;
729 pfile->keep_tokens++;
730 pfile->state.parsing_args = 1;
731 buff = funlike_invocation_p (pfile, node);
732 pfile->state.parsing_args = 0;
733 pfile->keep_tokens--;
734 pfile->state.prevent_expansion--;
738 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
739 cpp_error (pfile, DL_WARNING,
740 "function-like macro \"%s\" must be used with arguments in traditional C",
746 if (macro->paramc > 0)
747 replace_args (pfile, node, macro, (macro_arg *) buff->base);
748 _cpp_release_buff (pfile, buff);
751 /* Disable the macro within its expansion. */
752 node->flags |= NODE_DISABLED;
756 if (macro->paramc == 0)
757 push_token_context (pfile, node, macro->exp.tokens, macro->count);
762 /* Handle built-in macros and the _Pragma operator. */
763 return builtin_macro (pfile, node);
766 /* Replace the parameters in a function-like macro of NODE with the
767 actual ARGS, and place the result in a newly pushed token context.
768 Expand each argument before replacing, unless it is operated upon
769 by the # or ## operators. */
771 replace_args (pfile, node, macro, args)
777 unsigned int i, total;
778 const cpp_token *src, *limit;
779 const cpp_token **dest, **first;
783 /* First, fully macro-expand arguments, calculating the number of
784 tokens in the final expansion as we go. The ordering of the if
785 statements below is subtle; we must handle stringification before
787 total = macro->count;
788 limit = macro->exp.tokens + macro->count;
790 for (src = macro->exp.tokens; src < limit; src++)
791 if (src->type == CPP_MACRO_ARG)
793 /* Leading and trailing padding tokens. */
796 /* We have an argument. If it is not being stringified or
797 pasted it is macro-replaced before insertion. */
798 arg = &args[src->val.arg_no - 1];
800 if (src->flags & STRINGIFY_ARG)
802 if (!arg->stringified)
803 arg->stringified = stringify_arg (pfile, arg);
805 else if ((src->flags & PASTE_LEFT)
806 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
807 total += arg->count - 1;
811 expand_arg (pfile, arg);
812 total += arg->expanded_count - 1;
816 /* Now allocate space for the expansion, copy the tokens and replace
818 buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
819 first = (const cpp_token **) buff->base;
822 for (src = macro->exp.tokens; src < limit; src++)
825 const cpp_token **from, **paste_flag;
827 if (src->type != CPP_MACRO_ARG)
834 arg = &args[src->val.arg_no - 1];
835 if (src->flags & STRINGIFY_ARG)
836 count = 1, from = &arg->stringified;
837 else if (src->flags & PASTE_LEFT)
838 count = arg->count, from = arg->first;
839 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
841 count = arg->count, from = arg->first;
844 /* GCC has special semantics for , ## b where b is a
845 varargs parameter: the comma disappears if b was
846 given no actual arguments (not merely if b is an
847 empty argument); otherwise the paste flag is removed. */
848 if (dest[-1]->type == CPP_COMMA
850 && src->val.arg_no == macro->paramc)
855 paste_flag = dest - 1;
857 /* Remove the paste flag if the RHS is a placemarker. */
859 paste_flag = dest - 1;
863 count = arg->expanded_count, from = arg->expanded;
865 /* Padding on the left of an argument (unless RHS of ##). */
866 if (!pfile->state.in_directive
867 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
868 *dest++ = padding_token (pfile, src);
872 memcpy (dest, from, count * sizeof (cpp_token *));
875 /* With a non-empty argument on the LHS of ##, the last
876 token should be flagged PASTE_LEFT. */
877 if (src->flags & PASTE_LEFT)
878 paste_flag = dest - 1;
881 /* Avoid paste on RHS (even case count == 0). */
882 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
883 *dest++ = &pfile->avoid_paste;
885 /* Add a new paste flag, or remove an unwanted one. */
888 cpp_token *token = _cpp_temp_token (pfile);
889 token->type = (*paste_flag)->type;
890 token->val.str = (*paste_flag)->val.str;
891 if (src->flags & PASTE_LEFT)
892 token->flags = (*paste_flag)->flags | PASTE_LEFT;
894 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
899 /* Free the expanded arguments. */
900 for (i = 0; i < macro->paramc; i++)
901 if (args[i].expanded)
902 free (args[i].expanded);
904 push_ptoken_context (pfile, node, buff, first, dest - first);
907 /* Return a special padding token, with padding inherited from SOURCE. */
908 static const cpp_token *
909 padding_token (pfile, source)
911 const cpp_token *source;
913 cpp_token *result = _cpp_temp_token (pfile);
915 result->type = CPP_PADDING;
916 result->val.source = source;
921 /* Get a new uninitialized context. Create a new one if we cannot
922 re-use an old one. */
927 cpp_context *result = pfile->context->next;
931 result = xnew (cpp_context);
932 result->prev = pfile->context;
934 pfile->context->next = result;
937 pfile->context = result;
941 /* Push a list of pointers to tokens. */
943 push_ptoken_context (pfile, macro, buff, first, count)
947 const cpp_token **first;
950 cpp_context *context = next_context (pfile);
952 context->direct_p = false;
953 context->macro = macro;
954 context->buff = buff;
955 FIRST (context).ptoken = first;
956 LAST (context).ptoken = first + count;
959 /* Push a list of tokens. */
961 push_token_context (pfile, macro, first, count)
964 const cpp_token *first;
967 cpp_context *context = next_context (pfile);
969 context->direct_p = true;
970 context->macro = macro;
971 context->buff = NULL;
972 FIRST (context).token = first;
973 LAST (context).token = first + count;
976 /* Push a traditional macro's replacement text. */
978 _cpp_push_text_context (pfile, macro, start, len)
984 cpp_context *context = next_context (pfile);
986 context->direct_p = true;
987 context->macro = macro;
988 context->buff = NULL;
989 CUR (context) = start;
990 RLIMIT (context) = start + len;
991 macro->flags |= NODE_DISABLED;
994 /* Expand an argument ARG before replacing parameters in a
995 function-like macro. This works by pushing a context with the
996 argument's tokens, and then expanding that into a temporary buffer
997 as if it were a normal part of the token stream. collect_args()
998 has terminated the argument's tokens with a CPP_EOF so that we know
999 when we have fully expanded the argument. */
1001 expand_arg (pfile, arg)
1005 unsigned int capacity;
1007 if (arg->count == 0)
1010 /* Loop, reading in the arguments. */
1012 arg->expanded = (const cpp_token **)
1013 xmalloc (capacity * sizeof (cpp_token *));
1015 push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
1018 const cpp_token *token;
1020 if (arg->expanded_count + 1 >= capacity)
1023 arg->expanded = (const cpp_token **)
1024 xrealloc (arg->expanded, capacity * sizeof (cpp_token *));
1027 token = cpp_get_token (pfile);
1029 if (token->type == CPP_EOF)
1032 arg->expanded[arg->expanded_count++] = token;
1035 _cpp_pop_context (pfile);
1038 /* Pop the current context off the stack, re-enabling the macro if the
1039 context represented a macro's replacement list. The context
1040 structure is not freed so that we can re-use it later. */
1042 _cpp_pop_context (pfile)
1045 cpp_context *context = pfile->context;
1048 context->macro->flags &= ~NODE_DISABLED;
1051 _cpp_release_buff (pfile, context->buff);
1053 pfile->context = context->prev;
1056 /* Eternal routine to get a token. Also used nearly everywhere
1057 internally, except for places where we know we can safely call
1058 the lexer directly, such as lexing a directive name.
1060 Macro expansions and directives are transparently handled,
1061 including entering included files. Thus tokens are post-macro
1062 expansion, and after any intervening directives. External callers
1063 see CPP_EOF only at EOF. Internal callers also see it when meeting
1064 a directive inside a macro call, when at the end of a directive and
1065 state.in_directive is still 1, and at the end of argument
1068 cpp_get_token (pfile)
1071 const cpp_token *result;
1076 cpp_context *context = pfile->context;
1078 /* Context->prev == 0 <=> base context. */
1080 result = _cpp_lex_token (pfile);
1081 else if (FIRST (context).token != LAST (context).token)
1083 if (context->direct_p)
1084 result = FIRST (context).token++;
1086 result = *FIRST (context).ptoken++;
1088 if (result->flags & PASTE_LEFT)
1090 paste_all_tokens (pfile, result);
1091 if (pfile->state.in_directive)
1093 return padding_token (pfile, result);
1098 _cpp_pop_context (pfile);
1099 if (pfile->state.in_directive)
1101 return &pfile->avoid_paste;
1104 if (pfile->state.in_directive && result->type == CPP_COMMENT)
1107 if (result->type != CPP_NAME)
1110 node = result->val.node;
1112 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
1115 if (!(node->flags & NODE_DISABLED))
1117 if (!pfile->state.prevent_expansion
1118 && enter_macro_context (pfile, node))
1120 if (pfile->state.in_directive)
1122 return padding_token (pfile, result);
1127 /* Flag this token as always unexpandable. FIXME: move this
1128 to collect_args()?. */
1129 cpp_token *t = _cpp_temp_token (pfile);
1130 t->type = result->type;
1131 t->flags = result->flags | NO_EXPAND;
1132 t->val.str = result->val.str;
1142 /* Returns true if we're expanding an object-like macro that was
1143 defined in a system header. Just checks the macro at the top of
1144 the stack. Used for diagnostic suppression. */
1146 cpp_sys_macro_p (pfile)
1149 cpp_hashnode *node = pfile->context->macro;
1151 return node && node->value.macro && node->value.macro->syshdr;
1154 /* Read each token in, until end of the current file. Directives are
1155 transparently processed. */
1157 cpp_scan_nooutput (pfile)
1160 /* Request a CPP_EOF token at the end of this file, rather than
1161 transparently continuing with the including file. */
1162 pfile->buffer->return_at_eof = true;
1164 if (CPP_OPTION (pfile, traditional))
1165 while (_cpp_read_logical_line_trad (pfile))
1168 while (cpp_get_token (pfile)->type != CPP_EOF)
1172 /* Step back one (or more) tokens. Can only step mack more than 1 if
1173 they are from the lexer, and not from macro expansion. */
1175 _cpp_backup_tokens (pfile, count)
1179 if (pfile->context->prev == NULL)
1181 pfile->lookaheads += count;
1185 if (pfile->cur_token == pfile->cur_run->base
1186 /* Possible with -fpreprocessed and no leading #line. */
1187 && pfile->cur_run->prev != NULL)
1189 pfile->cur_run = pfile->cur_run->prev;
1190 pfile->cur_token = pfile->cur_run->limit;
1198 if (pfile->context->direct_p)
1199 FIRST (pfile->context).token--;
1201 FIRST (pfile->context).ptoken--;
1205 /* #define directive parsing and handling. */
1207 /* Returns non-zero if a macro redefinition warning is required. */
1209 warn_of_redefinition (pfile, node, macro2)
1211 const cpp_hashnode *node;
1212 const cpp_macro *macro2;
1214 const cpp_macro *macro1;
1217 /* Some redefinitions need to be warned about regardless. */
1218 if (node->flags & NODE_WARN)
1221 /* Redefinition of a macro is allowed if and only if the old and new
1222 definitions are the same. (6.10.3 paragraph 2). */
1223 macro1 = node->value.macro;
1225 /* Don't check count here as it can be different in valid
1226 traditional redefinitions with just whitespace differences. */
1227 if (macro1->paramc != macro2->paramc
1228 || macro1->fun_like != macro2->fun_like
1229 || macro1->variadic != macro2->variadic)
1232 /* Check parameter spellings. */
1233 for (i = 0; i < macro1->paramc; i++)
1234 if (macro1->params[i] != macro2->params[i])
1237 /* Check the replacement text or tokens. */
1238 if (CPP_OPTION (pfile, traditional))
1239 return _cpp_expansions_different_trad (macro1, macro2);
1241 if (macro1->count == macro2->count)
1242 for (i = 0; i < macro1->count; i++)
1243 if (!_cpp_equiv_tokens (¯o1->exp.tokens[i], ¯o2->exp.tokens[i]))
1249 /* Free the definition of hashnode H. */
1251 _cpp_free_definition (h)
1254 /* Macros and assertions no longer have anything to free. */
1256 /* Clear builtin flag in case of redefinition. */
1257 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED);
1260 /* Save parameter NODE to the parameter list of macro MACRO. Returns
1261 zero on success, non-zero if the parameter is a duplicate. */
1263 _cpp_save_parameter (pfile, macro, node)
1268 /* Constraint 6.10.3.6 - duplicate parameter names. */
1269 if (node->arg_index)
1271 cpp_error (pfile, DL_ERROR, "duplicate macro parameter \"%s\"",
1276 if (BUFF_ROOM (pfile->a_buff)
1277 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1278 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
1280 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
1281 node->arg_index = macro->paramc;
1285 /* Check the syntax of the parameters in a MACRO definition. Returns
1286 false if an error occurs. */
1288 parse_params (pfile, macro)
1292 unsigned int prev_ident = 0;
1296 const cpp_token *token = _cpp_lex_token (pfile);
1298 switch (token->type)
1301 /* Allow/ignore comments in parameter lists if we are
1302 preserving comments in macro expansions. */
1303 if (token->type == CPP_COMMENT
1304 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
1307 cpp_error (pfile, DL_ERROR,
1308 "\"%s\" may not appear in macro parameter list",
1309 cpp_token_as_text (pfile, token));
1315 cpp_error (pfile, DL_ERROR,
1316 "macro parameters must be comma-separated");
1321 if (_cpp_save_parameter (pfile, macro, token->val.node))
1325 case CPP_CLOSE_PAREN:
1326 if (prev_ident || macro->paramc == 0)
1329 /* Fall through to pick up the error. */
1333 cpp_error (pfile, DL_ERROR, "parameter name missing");
1340 macro->variadic = 1;
1343 _cpp_save_parameter (pfile, macro,
1344 pfile->spec_nodes.n__VA_ARGS__);
1345 pfile->state.va_args_ok = 1;
1346 if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
1347 cpp_error (pfile, DL_PEDWARN,
1348 "anonymous variadic macros were introduced in C99");
1350 else if (CPP_OPTION (pfile, pedantic))
1351 cpp_error (pfile, DL_PEDWARN,
1352 "ISO C does not permit named variadic macros");
1354 /* We're at the end, and just expect a closing parenthesis. */
1355 token = _cpp_lex_token (pfile);
1356 if (token->type == CPP_CLOSE_PAREN)
1361 cpp_error (pfile, DL_ERROR, "missing ')' in macro parameter list");
1367 /* Allocate room for a token from a macro's replacement list. */
1369 alloc_expansion_token (pfile, macro)
1373 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1374 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
1376 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
1379 /* Lex a token from the expansion of MACRO, but mark parameters as we
1380 find them and warn of traditional stringification. */
1382 lex_expansion_token (pfile, macro)
1388 pfile->cur_token = alloc_expansion_token (pfile, macro);
1389 token = _cpp_lex_direct (pfile);
1391 /* Is this a parameter? */
1392 if (token->type == CPP_NAME && token->val.node->arg_index)
1394 token->type = CPP_MACRO_ARG;
1395 token->val.arg_no = token->val.node->arg_index;
1397 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1398 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1399 check_trad_stringification (pfile, macro, &token->val.str);
1405 create_iso_definition (pfile, macro)
1410 const cpp_token *ctoken;
1412 /* Get the first token of the expansion (or the '(' of a
1413 function-like macro). */
1414 ctoken = _cpp_lex_token (pfile);
1416 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
1418 bool ok = parse_params (pfile, macro);
1419 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1423 /* Success. Commit the parameter array. */
1424 BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->params[macro->paramc];
1425 macro->fun_like = 1;
1427 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
1428 cpp_error (pfile, DL_PEDWARN,
1429 "ISO C requires whitespace after the macro name");
1431 if (macro->fun_like)
1432 token = lex_expansion_token (pfile, macro);
1435 token = alloc_expansion_token (pfile, macro);
1441 /* Check the stringifying # constraint 6.10.3.2.1 of
1442 function-like macros when lexing the subsequent token. */
1443 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1445 if (token->type == CPP_MACRO_ARG)
1447 token->flags &= ~PREV_WHITE;
1448 token->flags |= STRINGIFY_ARG;
1449 token->flags |= token[-1].flags & PREV_WHITE;
1450 token[-1] = token[0];
1453 /* Let assembler get away with murder. */
1454 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
1456 cpp_error (pfile, DL_ERROR,
1457 "'#' is not followed by a macro parameter");
1462 if (token->type == CPP_EOF)
1465 /* Paste operator constraint 6.10.3.3.1. */
1466 if (token->type == CPP_PASTE)
1468 /* Token-paste ##, can appear in both object-like and
1469 function-like macros, but not at the ends. */
1470 if (--macro->count > 0)
1471 token = lex_expansion_token (pfile, macro);
1473 if (macro->count == 0 || token->type == CPP_EOF)
1475 cpp_error (pfile, DL_ERROR,
1476 "'##' cannot appear at either end of a macro expansion");
1480 token[-1].flags |= PASTE_LEFT;
1483 token = lex_expansion_token (pfile, macro);
1486 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
1488 /* Don't count the CPP_EOF. */
1491 /* Clear whitespace on first token for warn_of_redefinition(). */
1493 macro->exp.tokens[0].flags &= ~PREV_WHITE;
1495 /* Commit the memory. */
1496 BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->exp.tokens[macro->count];
1501 /* Parse a macro and save its expansion. Returns non-zero on success. */
1503 _cpp_create_definition (pfile, node)
1511 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
1512 macro->line = pfile->directive_line;
1515 macro->variadic = 0;
1518 macro->fun_like = 0;
1519 /* To suppress some diagnostics. */
1520 macro->syshdr = pfile->map->sysp != 0;
1522 if (CPP_OPTION (pfile, traditional))
1523 ok = _cpp_create_trad_definition (pfile, macro);
1526 cpp_token *saved_cur_token = pfile->cur_token;
1528 ok = create_iso_definition (pfile, macro);
1530 /* Restore lexer position because of games lex_expansion_token()
1531 plays lexing the macro. We set the type for SEEN_EOL() in
1534 Longer term we should lex the whole line before coming here,
1535 and just copy the expansion. */
1536 saved_cur_token[-1].type = pfile->cur_token[-1].type;
1537 pfile->cur_token = saved_cur_token;
1539 /* Stop the lexer accepting __VA_ARGS__. */
1540 pfile->state.va_args_ok = 0;
1543 /* Clear the fast argument lookup indices. */
1544 for (i = macro->paramc; i-- > 0; )
1545 macro->params[i]->arg_index = 0;
1550 if (node->type != NT_VOID)
1552 if (CPP_OPTION (pfile, warn_unused_macros))
1553 _cpp_warn_if_unused_macro (pfile, node, NULL);
1555 if (warn_of_redefinition (pfile, node, macro))
1557 cpp_error_with_line (pfile, DL_PEDWARN, pfile->directive_line, 0,
1558 "\"%s\" redefined", NODE_NAME (node));
1560 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1561 cpp_error_with_line (pfile, DL_PEDWARN,
1562 node->value.macro->line, 0,
1563 "this is the location of the previous definition");
1565 _cpp_free_definition (node);
1568 /* Enter definition in hash table. */
1569 node->type = NT_MACRO;
1570 node->value.macro = macro;
1571 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
1572 node->flags |= NODE_WARN;
1577 /* Warn if a token in STRING matches one of a function-like MACRO's
1580 check_trad_stringification (pfile, macro, string)
1582 const cpp_macro *macro;
1583 const cpp_string *string;
1585 unsigned int i, len;
1586 const uchar *p, *q, *limit = string->text + string->len;
1588 /* Loop over the string. */
1589 for (p = string->text; p < limit; p = q)
1591 /* Find the start of an identifier. */
1592 while (p < limit && !is_idstart (*p))
1595 /* Find the end of the identifier. */
1597 while (q < limit && is_idchar (*q))
1602 /* Loop over the function macro arguments to see if the
1603 identifier inside the string matches one of them. */
1604 for (i = 0; i < macro->paramc; i++)
1606 const cpp_hashnode *node = macro->params[i];
1608 if (NODE_LEN (node) == len
1609 && !memcmp (p, NODE_NAME (node), len))
1611 cpp_error (pfile, DL_WARNING,
1612 "macro argument \"%s\" would be stringified in traditional C",
1620 /* Returns the name, arguments and expansion of a macro, in a format
1621 suitable to be read back in again, and therefore also for DWARF 2
1622 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1623 Caller is expected to generate the "#define" bit if needed. The
1624 returned text is temporary, and automatically freed later. */
1625 const unsigned char *
1626 cpp_macro_definition (pfile, node)
1628 const cpp_hashnode *node;
1630 unsigned int i, len;
1631 const cpp_macro *macro = node->value.macro;
1632 unsigned char *buffer;
1634 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1636 cpp_error (pfile, DL_ICE,
1637 "invalid hash type %d in cpp_macro_definition", node->type);
1641 /* Calculate length. */
1642 len = NODE_LEN (node) + 2; /* ' ' and NUL. */
1643 if (macro->fun_like)
1645 len += 4; /* "()" plus possible final ".." of named
1646 varargs (we have + 1 below). */
1647 for (i = 0; i < macro->paramc; i++)
1648 len += NODE_LEN (macro->params[i]) + 1; /* "," */
1651 if (CPP_OPTION (pfile, traditional))
1652 len += _cpp_replacement_text_len (macro);
1655 for (i = 0; i < macro->count; i++)
1657 cpp_token *token = ¯o->exp.tokens[i];
1659 if (token->type == CPP_MACRO_ARG)
1660 len += NODE_LEN (macro->params[token->val.arg_no - 1]);
1662 len += cpp_token_len (token); /* Includes room for ' '. */
1663 if (token->flags & STRINGIFY_ARG)
1665 if (token->flags & PASTE_LEFT)
1666 len += 3; /* " ##" */
1670 if (len > pfile->macro_buffer_len)
1672 pfile->macro_buffer = (uchar *) xrealloc (pfile->macro_buffer, len);
1673 pfile->macro_buffer_len = len;
1676 /* Fill in the buffer. Start with the macro name. */
1677 buffer = pfile->macro_buffer;
1678 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1679 buffer += NODE_LEN (node);
1681 /* Parameter names. */
1682 if (macro->fun_like)
1685 for (i = 0; i < macro->paramc; i++)
1687 cpp_hashnode *param = macro->params[i];
1689 if (param != pfile->spec_nodes.n__VA_ARGS__)
1691 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1692 buffer += NODE_LEN (param);
1695 if (i + 1 < macro->paramc)
1696 /* Don't emit a space after the comma here; we're trying
1697 to emit a Dwarf-friendly definition, and the Dwarf spec
1698 forbids spaces in the argument list. */
1700 else if (macro->variadic)
1701 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1706 /* The Dwarf spec requires a space after the macro name, even if the
1707 definition is the empty string. */
1710 if (CPP_OPTION (pfile, traditional))
1711 buffer = _cpp_copy_replacement_text (macro, buffer);
1712 else if (macro->count)
1713 /* Expansion tokens. */
1715 for (i = 0; i < macro->count; i++)
1717 cpp_token *token = ¯o->exp.tokens[i];
1719 if (token->flags & PREV_WHITE)
1721 if (token->flags & STRINGIFY_ARG)
1724 if (token->type == CPP_MACRO_ARG)
1726 len = NODE_LEN (macro->params[token->val.arg_no - 1]);
1728 NODE_NAME (macro->params[token->val.arg_no - 1]), len);
1732 buffer = cpp_spell_token (pfile, token, buffer);
1734 if (token->flags & PASTE_LEFT)
1739 /* Next has PREV_WHITE; see _cpp_create_definition. */
1745 return pfile->macro_buffer;