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 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! */
28 #include "intl.h" /* for _("<command line>") below. */
34 cpp_hashnode **params; /* Parameters, if any. */
35 cpp_token *expansion; /* First token of replacement list. */
36 unsigned int line; /* Starting line number. */
37 unsigned int count; /* Number of tokens in expansion. */
38 unsigned short paramc; /* Number of parameters. */
39 unsigned int fun_like : 1; /* If a function-like macro. */
40 unsigned int variadic : 1; /* If a variadic macro. */
41 unsigned int disabled : 1; /* If macro is disabled. */
42 unsigned int syshdr : 1; /* If macro defined in system header. */
45 typedef struct macro_arg macro_arg;
48 const cpp_token **first; /* First token in unexpanded argument. */
49 const cpp_token **expanded; /* Macro-expanded argument. */
50 const cpp_token *stringified; /* Stringified argument. */
51 unsigned int count; /* # of tokens in argument. */
52 unsigned int expanded_count; /* # of tokens in expanded argument. */
55 /* Macro expansion. */
57 static int enter_macro_context PARAMS ((cpp_reader *, cpp_hashnode *));
58 static const cpp_token *builtin_macro PARAMS ((cpp_reader *, cpp_hashnode *));
59 static void push_token_context
60 PARAMS ((cpp_reader *, cpp_macro *, const cpp_token *, unsigned int));
61 static void push_ptoken_context
62 PARAMS ((cpp_reader *, cpp_macro *, _cpp_buff *,
63 const cpp_token **, unsigned int));
64 static _cpp_buff *collect_args PARAMS ((cpp_reader *, const cpp_hashnode *));
65 static cpp_context *next_context PARAMS ((cpp_reader *));
66 static const cpp_token *padding_token
67 PARAMS ((cpp_reader *, const cpp_token *));
68 static void expand_arg PARAMS ((cpp_reader *, macro_arg *));
69 static unsigned char *quote_string PARAMS ((unsigned char *,
70 const unsigned char *,
72 static const cpp_token *new_string_token PARAMS ((cpp_reader *, U_CHAR *,
74 static const cpp_token *new_number_token PARAMS ((cpp_reader *, int));
75 static const cpp_token *stringify_arg PARAMS ((cpp_reader *, macro_arg *));
76 static void paste_all_tokens PARAMS ((cpp_reader *, const cpp_token *));
77 static bool paste_tokens PARAMS ((cpp_reader *, const cpp_token **,
79 static int funlike_invocation_p PARAMS ((cpp_reader *, const cpp_hashnode *));
80 static void replace_args PARAMS ((cpp_reader *, cpp_macro *, macro_arg *));
82 /* #define directive parsing and handling. */
84 static cpp_token *alloc_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
85 static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
86 static int warn_of_redefinition PARAMS ((cpp_reader *, const cpp_hashnode *,
88 static int save_parameter PARAMS ((cpp_reader *, cpp_macro *, cpp_hashnode *));
89 static int parse_params PARAMS ((cpp_reader *, cpp_macro *));
90 static void check_trad_stringification PARAMS ((cpp_reader *,
94 /* Allocates and returns a CPP_STRING token, containing TEXT of length
95 LEN, after null-terminating it. TEXT must be in permanent storage. */
96 static const cpp_token *
97 new_string_token (pfile, text, len)
102 cpp_token *token = _cpp_temp_token (pfile);
105 token->type = CPP_STRING;
106 token->val.str.len = len;
107 token->val.str.text = text;
112 /* Allocates and returns a CPP_NUMBER token evaluating to NUMBER. */
113 static const cpp_token *
114 new_number_token (pfile, number)
118 cpp_token *token = _cpp_temp_token (pfile);
119 unsigned char *buf = _cpp_unaligned_alloc (pfile, 20);
121 sprintf ((char *) buf, "%d", number);
122 token->type = CPP_NUMBER;
123 token->val.str.text = buf;
124 token->val.str.len = ustrlen (buf);
129 static const char * const monthnames[] =
131 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
132 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
135 /* Handle builtin macros like __FILE__. */
136 static const cpp_token *
137 builtin_macro (pfile, node)
141 switch (node->value.builtin)
144 cpp_ice (pfile, "invalid builtin macro \"%s\"", NODE_NAME (node));
145 return new_number_token (pfile, 1);
153 const struct line_map *map = pfile->map;
155 if (node->value.builtin == BT_BASE_FILE)
156 while (! MAIN_FILE_P (map))
157 map = INCLUDED_FROM (&pfile->line_maps, map);
161 buf = _cpp_unaligned_alloc (pfile, len * 4 + 1);
162 len = quote_string (buf, (const unsigned char *) name, len) - buf;
164 return new_string_token (pfile, buf, len);
167 case BT_INCLUDE_LEVEL:
168 /* The line map depth counts the primary source as level 1, but
169 historically __INCLUDE_DEPTH__ has called the primary source
171 return new_number_token (pfile, 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 return new_number_token (pfile, SOURCE_LINE (pfile->map,
178 pfile->cur_token[-1].line));
182 int stdc = (!CPP_IN_SYSTEM_HEADER (pfile)
183 || pfile->spec_nodes.n__STRICT_ANSI__->type != NT_VOID);
184 return new_number_token (pfile, stdc);
189 if (pfile->date.type == CPP_EOF)
191 /* Allocate __DATE__ and __TIME__ strings from permanent
192 storage. We only do this once, and don't generate them
193 at init time, because time() and localtime() are very
194 slow on some systems. */
195 time_t tt = time (NULL);
196 struct tm *tb = localtime (&tt);
198 pfile->date.val.str.text =
199 _cpp_unaligned_alloc (pfile, sizeof ("Oct 11 1347"));
200 pfile->date.val.str.len = sizeof ("Oct 11 1347") - 1;
201 pfile->date.type = CPP_STRING;
202 pfile->date.flags = 0;
203 sprintf ((char *) pfile->date.val.str.text, "%s %2d %4d",
204 monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
206 pfile->time.val.str.text =
207 _cpp_unaligned_alloc (pfile, sizeof ("12:34:56"));
208 pfile->time.val.str.len = sizeof ("12:34:56") - 1;
209 pfile->time.type = CPP_STRING;
210 pfile->time.flags = 0;
211 sprintf ((char *) pfile->time.val.str.text, "%02d:%02d:%02d",
212 tb->tm_hour, tb->tm_min, tb->tm_sec);
215 return node->value.builtin == BT_DATE ? &pfile->date: &pfile->time;
219 /* Adds backslashes before all backslashes and double quotes appearing
220 in strings. Non-printable characters are converted to octal. */
222 quote_string (dest, src, len)
231 if (c == '\\' || c == '"')
242 sprintf ((char *) dest, "\\%03o", c);
251 /* Convert a token sequence to a single string token according to the
252 rules of the ISO C #-operator. */
253 static const cpp_token *
254 stringify_arg (pfile, arg)
258 unsigned char *dest = BUFF_FRONT (pfile->u_buff);
259 unsigned int i, escape_it, backslash_count = 0;
260 const cpp_token *source = NULL;
263 /* Loop, reading in the argument's tokens. */
264 for (i = 0; i < arg->count; i++)
266 const cpp_token *token = arg->first[i];
268 if (token->type == CPP_PADDING)
271 source = token->val.source;
275 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
276 || token->type == CPP_CHAR || token->type == CPP_WCHAR);
278 /* Room for each char being written in octal, initial space and
280 len = cpp_token_len (token);
285 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
287 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
288 _cpp_extend_buff (pfile, &pfile->u_buff, len);
289 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
292 /* Leading white space? */
293 if (dest != BUFF_FRONT (pfile->u_buff))
297 if (source->flags & PREV_WHITE)
304 _cpp_buff *buff = _cpp_get_buff (pfile, len);
305 unsigned char *buf = BUFF_FRONT (buff);
306 len = cpp_spell_token (pfile, token, buf) - buf;
307 dest = quote_string (dest, buf, len);
308 _cpp_release_buff (pfile, buff);
311 dest = cpp_spell_token (pfile, token, dest);
313 if (token->type == CPP_OTHER && token->val.c == '\\')
319 /* Ignore the final \ of invalid string literals. */
320 if (backslash_count & 1)
322 cpp_warning (pfile, "invalid string literal, ignoring final '\\'");
326 /* Commit the memory, including NUL, and return the token. */
327 len = dest - BUFF_FRONT (pfile->u_buff);
328 BUFF_FRONT (pfile->u_buff) = dest + 1;
329 return new_string_token (pfile, dest - len, len);
332 /* Try to paste two tokens. On success, return non-zero. In any
333 case, PLHS is updated to point to the pasted token, which is
334 guaranteed to not have the PASTE_LEFT flag set. */
336 paste_tokens (pfile, plhs, rhs)
338 const cpp_token **plhs, *rhs;
340 unsigned char *buf, *end;
341 const cpp_token *lhs;
346 len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
347 buf = (unsigned char *) alloca (len);
348 end = cpp_spell_token (pfile, lhs, buf);
350 /* Avoid comment headers, since they are still processed in stage 3.
351 It is simpler to insert a space here, rather than modifying the
352 lexer to ignore comments in some circumstances. Simply returning
353 false doesn't work, since we want to clear the PASTE_LEFT flag. */
354 if (lhs->type == CPP_DIV
355 && (rhs->type == CPP_MULT || rhs->type == CPP_DIV))
357 end = cpp_spell_token (pfile, rhs, end);
359 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true, 1);
361 /* Tweak the column number the lexer will report. */
362 pfile->buffer->col_adjust = pfile->cur_token[-1].col - 1;
364 /* We don't want a leading # to be interpreted as a directive. */
365 pfile->buffer->saved_flags = 0;
367 /* Set pfile->cur_token as required by _cpp_lex_direct. */
368 pfile->cur_token = _cpp_temp_token (pfile);
369 *plhs = _cpp_lex_direct (pfile);
370 valid = (pfile->buffer->cur == pfile->buffer->rlimit
371 && pfile->buffer->read_ahead == EOF);
372 _cpp_pop_buffer (pfile);
377 /* Handles an arbitrarily long sequence of ## operators. This
378 implementation is left-associative, non-recursive, and finishes a
379 paste before handling succeeding ones. If the paste fails, we back
380 up a token to just after the ## operator, with the effect that it
381 appears in the output stream normally. */
383 paste_all_tokens (pfile, lhs)
385 const cpp_token *lhs;
387 const cpp_token *rhs;
388 cpp_context *context = pfile->context;
392 /* Take the token directly from the current context. We can do
393 this, because we are in the replacement list of either an
394 object-like macro, or a function-like macro with arguments
395 inserted. In either case, the constraints to #define
396 guarantee we have at least one more token. */
397 if (context->direct_p)
398 rhs = context->first.token++;
400 rhs = *context->first.ptoken++;
402 if (rhs->type == CPP_PADDING)
405 if (!paste_tokens (pfile, &lhs, rhs))
407 _cpp_backup_tokens (pfile, 1);
409 /* Mandatory warning for all apart from assembler. */
410 if (CPP_OPTION (pfile, lang) != CLK_ASM)
412 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
413 cpp_token_as_text (pfile, lhs),
414 cpp_token_as_text (pfile, rhs));
418 while (rhs->flags & PASTE_LEFT);
420 /* Put the resulting token in its own context. */
421 push_token_context (pfile, NULL, lhs, 1);
424 /* Reads and returns the arguments to a function-like macro invocation.
425 Assumes the opening parenthesis has been processed. If there is an
426 error, emits an appropriate diagnostic and returns NULL. */
428 collect_args (pfile, node)
430 const cpp_hashnode *node;
432 _cpp_buff *buff, *base_buff;
434 macro_arg *args, *arg;
435 const cpp_token *token;
439 macro = node->value.macro;
441 argc = macro->paramc;
444 buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
445 + sizeof (macro_arg)));
447 args = (macro_arg *) buff->base;
448 memset (args, 0, argc * sizeof (macro_arg));
449 buff->cur = (unsigned char *) &args[argc];
450 arg = args, argc = 0;
452 /* Collect the tokens making up each argument. We don't yet know
453 how many arguments have been supplied, whether too many or too
454 few. Hence the slightly bizarre usage of "argc" and "arg". */
457 unsigned int paren_depth = 0;
458 unsigned int ntokens = 0;
461 arg->first = (const cpp_token **) buff->cur;
465 /* Require space for 2 new tokens (including a CPP_EOF). */
466 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
468 buff = _cpp_append_extend_buff (pfile, buff,
469 1000 * sizeof (cpp_token *));
470 arg->first = (const cpp_token **) buff->cur;
473 token = cpp_get_token (pfile);
475 if (token->type == CPP_PADDING)
477 /* Drop leading padding. */
481 else if (token->type == CPP_OPEN_PAREN)
483 else if (token->type == CPP_CLOSE_PAREN)
485 if (paren_depth-- == 0)
488 else if (token->type == CPP_COMMA)
490 /* A comma does not terminate an argument within
491 parentheses or as part of a variable argument. */
493 && ! (macro->variadic && argc == macro->paramc))
496 else if (token->type == CPP_EOF
497 || (token->type == CPP_HASH && token->flags & BOL))
500 arg->first[ntokens++] = token;
503 /* Drop trailing padding. */
504 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
507 arg->count = ntokens;
508 arg->first[ntokens] = &pfile->eof;
510 /* Terminate the argument. Excess arguments loop back and
511 overwrite the final legitimate argument, before failing. */
512 if (argc <= macro->paramc)
514 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
515 if (argc != macro->paramc)
519 while (token->type != CPP_CLOSE_PAREN
520 && token->type != CPP_EOF
521 && token->type != CPP_HASH);
523 if (token->type == CPP_EOF || token->type == CPP_HASH)
525 bool step_back = false;
527 /* 6.10.3 paragraph 11: If there are sequences of preprocessing
528 tokens within the list of arguments that would otherwise act
529 as preprocessing directives, the behavior is undefined.
531 This implementation will report a hard error, terminate the
532 macro invocation, and proceed to process the directive. */
533 if (token->type == CPP_HASH)
536 "directives may not be used inside a macro argument");
540 step_back = (pfile->context->prev || pfile->state.in_directive);
542 /* We still need the CPP_EOF to end directives, and to end
543 pre-expansion of a macro argument. Step back is not
544 unconditional, since we don't want to return a CPP_EOF to our
545 callers at the end of an -include-d file. */
547 _cpp_backup_tokens (pfile, 1);
548 cpp_error (pfile, "unterminated argument list invoking macro \"%s\"",
552 else if (argc < macro->paramc)
554 /* As an extension, a rest argument is allowed to not appear in
555 the invocation at all.
556 e.g. #define debug(format, args...) something
559 This is exactly the same as if there had been an empty rest
560 argument - debug("string", ). */
562 if (argc + 1 == macro->paramc && macro->variadic)
564 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
565 cpp_pedwarn (pfile, "ISO C99 requires rest arguments to be used");
570 "macro \"%s\" requires %u arguments, but only %u given",
571 NODE_NAME (node), macro->paramc, argc);
575 else if (argc > macro->paramc)
577 /* Empty argument to a macro taking no arguments is OK. */
578 if (argc != 1 || arg->count)
581 "macro \"%s\" passed %u arguments, but takes just %u",
582 NODE_NAME (node), argc, macro->paramc);
590 _cpp_release_buff (pfile, base_buff);
595 funlike_invocation_p (pfile, node)
597 const cpp_hashnode *node;
599 const cpp_token *maybe_paren;
600 _cpp_buff *buff = NULL;
602 pfile->state.prevent_expansion++;
603 pfile->keep_tokens++;
605 pfile->state.parsing_args = 1;
607 maybe_paren = cpp_get_token (pfile);
608 while (maybe_paren->type == CPP_PADDING);
609 pfile->state.parsing_args = 2;
611 if (maybe_paren->type == CPP_OPEN_PAREN)
612 buff = collect_args (pfile, node);
615 _cpp_backup_tokens (pfile, 1);
616 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
618 "function-like macro \"%s\" must be used with arguments in traditional C",
622 pfile->state.parsing_args = 0;
623 pfile->keep_tokens--;
624 pfile->state.prevent_expansion--;
628 if (node->value.macro->paramc > 0)
629 replace_args (pfile, node->value.macro, (macro_arg *) buff->base);
630 _cpp_release_buff (pfile, buff);
636 /* Push the context of a macro onto the context stack. TOKEN is the
637 macro name. If we can successfully start expanding the macro,
638 TOKEN is replaced with the first token of the expansion, and we
641 enter_macro_context (pfile, node)
645 if (node->flags & NODE_BUILTIN)
646 push_token_context (pfile, NULL, builtin_macro (pfile, node), 1);
649 cpp_macro *macro = node->value.macro;
651 if (macro->fun_like && !funlike_invocation_p (pfile, node))
654 /* Disable the macro within its expansion. */
657 if (macro->paramc == 0)
658 push_token_context (pfile, macro, macro->expansion, macro->count);
664 /* Take the expansion of a function-like MACRO, replacing parameters
665 with the actual arguments. Each argument is macro-expanded before
666 replacement, unless operated upon by the # or ## operators. */
668 replace_args (pfile, macro, args)
673 unsigned int i, total;
674 const cpp_token *src, *limit;
675 const cpp_token **dest, **first;
679 /* First, fully macro-expand arguments, calculating the number of
680 tokens in the final expansion as we go. The ordering of the if
681 statements below is subtle; we must handle stringification before
683 total = macro->count;
684 limit = macro->expansion + macro->count;
686 for (src = macro->expansion; src < limit; src++)
687 if (src->type == CPP_MACRO_ARG)
689 /* Leading and trailing padding tokens. */
692 /* We have an argument. If it is not being stringified or
693 pasted it is macro-replaced before insertion. */
694 arg = &args[src->val.arg_no - 1];
696 if (src->flags & STRINGIFY_ARG)
698 if (!arg->stringified)
699 arg->stringified = stringify_arg (pfile, arg);
701 else if ((src->flags & PASTE_LEFT)
702 || (src > macro->expansion && (src[-1].flags & PASTE_LEFT)))
703 total += arg->count - 1;
707 expand_arg (pfile, arg);
708 total += arg->expanded_count - 1;
712 /* Now allocate space for the expansion, copy the tokens and replace
714 buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
715 first = (const cpp_token **) buff->base;
718 for (src = macro->expansion; src < limit; src++)
721 const cpp_token **from, **paste_flag;
723 if (src->type != CPP_MACRO_ARG)
730 arg = &args[src->val.arg_no - 1];
731 if (src->flags & STRINGIFY_ARG)
732 count = 1, from = &arg->stringified;
733 else if (src->flags & PASTE_LEFT)
734 count = arg->count, from = arg->first;
735 else if (src != macro->expansion && (src[-1].flags & PASTE_LEFT))
737 count = arg->count, from = arg->first;
740 /* GCC has special semantics for , ## b where b is a
741 varargs parameter: the comma disappears if b was
742 given no actual arguments (not merely if b is an
743 empty argument); otherwise the paste flag is removed. */
744 if (dest[-1]->type == CPP_COMMA
746 && src->val.arg_no == macro->paramc)
751 paste_flag = dest - 1;
753 /* Remove the paste flag if the RHS is a placemarker. */
755 paste_flag = dest - 1;
759 count = arg->expanded_count, from = arg->expanded;
761 /* Padding on the left of an argument (unless RHS of ##). */
762 if (!pfile->state.in_directive
763 && src != macro->expansion && !(src[-1].flags & PASTE_LEFT))
764 *dest++ = padding_token (pfile, src);
768 memcpy (dest, from, count * sizeof (cpp_token *));
771 /* With a non-empty argument on the LHS of ##, the last
772 token should be flagged PASTE_LEFT. */
773 if (src->flags & PASTE_LEFT)
774 paste_flag = dest - 1;
777 /* Avoid paste on RHS (even case count == 0). */
778 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
779 *dest++ = &pfile->avoid_paste;
781 /* Add a new paste flag, or remove an unwanted one. */
784 cpp_token *token = _cpp_temp_token (pfile);
785 token->type = (*paste_flag)->type;
786 token->val.str = (*paste_flag)->val.str;
787 if (src->flags & PASTE_LEFT)
788 token->flags = (*paste_flag)->flags | PASTE_LEFT;
790 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
795 /* Free the expanded arguments. */
796 for (i = 0; i < macro->paramc; i++)
797 if (args[i].expanded)
798 free (args[i].expanded);
800 push_ptoken_context (pfile, macro, buff, first, dest - first);
803 /* Return a special padding token, with padding inherited from SOURCE. */
804 static const cpp_token *
805 padding_token (pfile, source)
807 const cpp_token *source;
809 cpp_token *result = _cpp_temp_token (pfile);
811 result->type = CPP_PADDING;
812 result->val.source = source;
817 /* Move to the next context. Create one if there is none. */
822 cpp_context *result = pfile->context->next;
826 result = xnew (cpp_context);
827 result->prev = pfile->context;
829 pfile->context->next = result;
832 pfile->context = result;
836 /* Push a list of pointers to tokens. */
838 push_ptoken_context (pfile, macro, buff, first, count)
842 const cpp_token **first;
845 cpp_context *context = next_context (pfile);
847 context->direct_p = false;
848 context->macro = macro;
849 context->buff = buff;
850 context->first.ptoken = first;
851 context->last.ptoken = first + count;
854 /* Push a list of tokens. */
856 push_token_context (pfile, macro, first, count)
859 const cpp_token *first;
862 cpp_context *context = next_context (pfile);
864 context->direct_p = true;
865 context->macro = macro;
866 context->buff = NULL;
867 context->first.token = first;
868 context->last.token = first + count;
872 expand_arg (pfile, arg)
876 unsigned int capacity;
881 /* Loop, reading in the arguments. */
883 arg->expanded = (const cpp_token **)
884 xmalloc (capacity * sizeof (cpp_token *));
886 push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
889 const cpp_token *token;
891 if (arg->expanded_count + 1 >= capacity)
894 arg->expanded = (const cpp_token **)
895 xrealloc (arg->expanded, capacity * sizeof (cpp_token *));
898 token = cpp_get_token (pfile);
900 if (token->type == CPP_EOF)
903 arg->expanded[arg->expanded_count++] = token;
906 _cpp_pop_context (pfile);
910 _cpp_pop_context (pfile)
913 cpp_context *context = pfile->context;
915 /* Re-enable a macro when leaving its expansion. */
917 context->macro->disabled = 0;
920 _cpp_release_buff (pfile, context->buff);
922 pfile->context = context->prev;
925 /* Eternal routine to get a token. Also used nearly everywhere
926 internally, except for places where we know we can safely call
927 the lexer directly, such as lexing a directive name.
929 Macro expansions and directives are transparently handled,
930 including entering included files. Thus tokens are post-macro
931 expansion, and after any intervening directives. External callers
932 see CPP_EOF only at EOF. Internal callers also see it when meeting
933 a directive inside a macro call, when at the end of a directive and
934 state.in_directive is still 1, and at the end of argument
937 cpp_get_token (pfile)
940 const cpp_token *result;
945 cpp_context *context = pfile->context;
947 /* Context->prev == 0 <=> base context. */
949 result = _cpp_lex_token (pfile);
950 else if (context->first.token != context->last.token)
952 if (context->direct_p)
953 result = context->first.token++;
955 result = *context->first.ptoken++;
957 if (result->flags & PASTE_LEFT)
959 paste_all_tokens (pfile, result);
960 if (pfile->state.in_directive)
962 return padding_token (pfile, result);
967 _cpp_pop_context (pfile);
968 if (pfile->state.in_directive)
970 return &pfile->avoid_paste;
973 if (result->type != CPP_NAME)
976 node = result->val.node;
978 /* Handle macros and the _Pragma operator. */
979 if (node->type == NT_MACRO && !(result->flags & NO_EXPAND))
981 /* Macros invalidate controlling macros. */
982 pfile->mi_valid = false;
984 if (!(node->flags & NODE_BUILTIN) && node->value.macro->disabled)
986 /* Flag this token as always unexpandable. */
987 cpp_token *t = _cpp_temp_token (pfile);
988 t->type = result->type;
989 t->flags = result->flags | NO_EXPAND;
990 t->val.str = result->val.str;
993 else if (!pfile->state.prevent_expansion
994 && enter_macro_context (pfile, node))
996 if (pfile->state.in_directive)
998 return padding_token (pfile, result);
1002 /* Don't interpret _Pragma within directives. The standard is
1003 not clear on this, but to me this makes most sense. */
1004 if (node != pfile->spec_nodes.n__Pragma
1005 || pfile->state.in_directive)
1008 /* Handle it, and loop back for another token. MI is cleared
1009 since this token came from either the lexer or a macro. */
1010 _cpp_do__Pragma (pfile);
1016 /* Returns true if we're expanding an object-like macro that was
1017 defined in a system header. Just checks the macro at the top of
1018 the stack. Used for diagnostic suppression. */
1020 cpp_sys_macro_p (pfile)
1023 cpp_macro *macro = pfile->context->macro;
1025 return macro && macro->syshdr;
1028 /* Read each token in, until EOF. Directives are transparently
1031 cpp_scan_nooutput (pfile)
1034 while (cpp_get_token (pfile)->type != CPP_EOF)
1038 /* Step back one (or more) tokens. Can only step mack more than 1 if
1039 they are from the lexer, and not from macro expansion. */
1041 _cpp_backup_tokens (pfile, count)
1045 if (pfile->context->prev == NULL)
1047 pfile->lookaheads += count;
1051 if (pfile->cur_token == pfile->cur_run->base)
1053 pfile->cur_run = pfile->cur_run->prev;
1054 pfile->cur_token = pfile->cur_run->limit;
1062 if (pfile->context->direct_p)
1063 pfile->context->first.token--;
1065 pfile->context->first.ptoken--;
1069 /* #define directive parsing and handling. */
1071 /* Returns non-zero if a macro redefinition warning is required. */
1073 warn_of_redefinition (pfile, node, macro2)
1075 const cpp_hashnode *node;
1076 const cpp_macro *macro2;
1078 const cpp_macro *macro1;
1081 /* Some redefinitions need to be warned about regardless. */
1082 if (node->flags & NODE_WARN)
1085 if (! CPP_PEDANTIC (pfile))
1088 /* Redefinition of a macro is allowed if and only if the old and new
1089 definitions are the same. (6.10.3 paragraph 2). */
1090 macro1 = node->value.macro;
1092 /* The quick failures. */
1093 if (macro1->count != macro2->count
1094 || macro1->paramc != macro2->paramc
1095 || macro1->fun_like != macro2->fun_like
1096 || macro1->variadic != macro2->variadic)
1099 /* Check each token. */
1100 for (i = 0; i < macro1->count; i++)
1101 if (! _cpp_equiv_tokens (¯o1->expansion[i], ¯o2->expansion[i]))
1104 /* Check parameter spellings. */
1105 for (i = 0; i < macro1->paramc; i++)
1106 if (macro1->params[i] != macro2->params[i])
1112 /* Free the definition of hashnode H. */
1115 _cpp_free_definition (h)
1118 /* Macros and assertions no longer have anything to free. */
1120 /* Clear builtin flag in case of redefinition. */
1121 h->flags &= ~NODE_BUILTIN;
1124 /* Save parameter NODE to the parameter list of macro MACRO. Returns
1125 zero on success, non-zero if the paramter is a duplicate. */
1127 save_parameter (pfile, macro, node)
1132 /* Constraint 6.10.3.6 - duplicate parameter names. */
1133 if (node->arg_index)
1135 cpp_error (pfile, "duplicate macro parameter \"%s\"", NODE_NAME (node));
1139 if (BUFF_ROOM (pfile->a_buff)
1140 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1141 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
1143 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
1144 node->arg_index = macro->paramc;
1148 /* Check the syntax of the parameters in a MACRO definition. */
1150 parse_params (pfile, macro)
1154 unsigned int prev_ident = 0;
1158 const cpp_token *token = _cpp_lex_token (pfile);
1160 switch (token->type)
1163 cpp_error (pfile, "\"%s\" may not appear in macro parameter list",
1164 cpp_token_as_text (pfile, token));
1170 cpp_error (pfile, "macro parameters must be comma-separated");
1175 if (save_parameter (pfile, macro, token->val.node))
1179 case CPP_CLOSE_PAREN:
1180 if (prev_ident || macro->paramc == 0)
1183 /* Fall through to pick up the error. */
1187 cpp_error (pfile, "parameter name missing");
1194 macro->variadic = 1;
1197 save_parameter (pfile, macro, pfile->spec_nodes.n__VA_ARGS__);
1198 pfile->state.va_args_ok = 1;
1199 if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
1201 "anonymous variadic macros were introduced in C99");
1203 else if (CPP_OPTION (pfile, pedantic))
1204 cpp_pedwarn (pfile, "ISO C does not permit named variadic macros");
1206 /* We're at the end, and just expect a closing parenthesis. */
1207 token = _cpp_lex_token (pfile);
1208 if (token->type == CPP_CLOSE_PAREN)
1213 cpp_error (pfile, "missing ')' in macro parameter list");
1219 /* Allocate room for a token from a macro's replacement list. */
1221 alloc_expansion_token (pfile, macro)
1225 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1226 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
1228 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
1232 lex_expansion_token (pfile, macro)
1238 pfile->cur_token = alloc_expansion_token (pfile, macro);
1239 token = _cpp_lex_direct (pfile);
1241 /* Is this an argument? */
1242 if (token->type == CPP_NAME && token->val.node->arg_index)
1244 token->type = CPP_MACRO_ARG;
1245 token->val.arg_no = token->val.node->arg_index;
1247 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1248 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1249 check_trad_stringification (pfile, macro, &token->val.str);
1254 /* Parse a macro and save its expansion. Returns non-zero on success. */
1256 _cpp_create_definition (pfile, node)
1261 cpp_token *token, *saved_cur_token;
1262 const cpp_token *ctoken;
1263 unsigned int i, ok = 1;
1265 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
1266 macro->line = pfile->directive_line;
1269 macro->variadic = 0;
1271 macro->fun_like = 0;
1273 /* Get the first token of the expansion (or the '(' of a
1274 function-like macro). */
1275 ctoken = _cpp_lex_token (pfile);
1277 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
1279 ok = parse_params (pfile, macro);
1280 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1284 /* Success. Commit the parameter array. */
1285 BUFF_FRONT (pfile->a_buff) = (U_CHAR *) ¯o->params[macro->paramc];
1286 macro->fun_like = 1;
1288 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
1289 cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name");
1291 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
1292 saved_cur_token = pfile->cur_token;
1294 if (macro->fun_like)
1295 token = lex_expansion_token (pfile, macro);
1298 token = alloc_expansion_token (pfile, macro);
1304 /* Check the stringifying # constraint 6.10.3.2.1 of
1305 function-like macros when lexing the subsequent token. */
1306 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1308 if (token->type == CPP_MACRO_ARG)
1310 token->flags &= ~PREV_WHITE;
1311 token->flags |= STRINGIFY_ARG;
1312 token->flags |= token[-1].flags & PREV_WHITE;
1313 token[-1] = token[0];
1316 /* Let assembler get away with murder. */
1317 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
1320 cpp_error (pfile, "'#' is not followed by a macro parameter");
1325 if (token->type == CPP_EOF)
1328 /* Paste operator constraint 6.10.3.3.1. */
1329 if (token->type == CPP_PASTE)
1331 /* Token-paste ##, can appear in both object-like and
1332 function-like macros, but not at the ends. */
1333 if (--macro->count > 0)
1334 token = lex_expansion_token (pfile, macro);
1336 if (macro->count == 0 || token->type == CPP_EOF)
1340 "'##' cannot appear at either end of a macro expansion");
1344 token[-1].flags |= PASTE_LEFT;
1347 token = lex_expansion_token (pfile, macro);
1350 macro->expansion = (cpp_token *) BUFF_FRONT (pfile->a_buff);
1352 /* Don't count the CPP_EOF. */
1355 /* Clear whitespace on first token for macro equivalence purposes. */
1357 macro->expansion[0].flags &= ~PREV_WHITE;
1359 /* Commit the memory. */
1360 BUFF_FRONT (pfile->a_buff) = (U_CHAR *) ¯o->expansion[macro->count];
1362 /* Implement the macro-defined-to-itself optimisation. */
1363 macro->disabled = (macro->count == 1 && !macro->fun_like
1364 && macro->expansion[0].type == CPP_NAME
1365 && macro->expansion[0].val.node == node);
1367 /* To suppress some diagnostics. */
1368 macro->syshdr = pfile->map->sysp != 0;
1370 if (node->type != NT_VOID)
1372 if (warn_of_redefinition (pfile, node, macro))
1374 cpp_pedwarn_with_line (pfile, pfile->directive_line, 0,
1375 "\"%s\" redefined", NODE_NAME (node));
1377 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1378 cpp_pedwarn_with_line (pfile, node->value.macro->line, 0,
1379 "this is the location of the previous definition");
1381 _cpp_free_definition (node);
1384 /* Enter definition in hash table. */
1385 node->type = NT_MACRO;
1386 node->value.macro = macro;
1387 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
1388 node->flags |= NODE_WARN;
1392 /* Set type for SEEN_EOL() in cpplib.c, restore the lexer position. */
1393 saved_cur_token[-1].type = pfile->cur_token[-1].type;
1394 pfile->cur_token = saved_cur_token;
1398 /* Stop the lexer accepting __VA_ARGS__. */
1399 pfile->state.va_args_ok = 0;
1401 /* Clear the fast argument lookup indices. */
1402 for (i = macro->paramc; i-- > 0; )
1403 macro->params[i]->arg_index = 0;
1408 /* Warn if a token in `string' matches one of the function macro
1409 arguments in `info'. This function assumes that the macro is a
1410 function macro and not an object macro. */
1412 check_trad_stringification (pfile, macro, string)
1414 const cpp_macro *macro;
1415 const cpp_string *string;
1417 unsigned int i, len;
1418 const U_CHAR *p, *q, *limit = string->text + string->len;
1420 /* Loop over the string. */
1421 for (p = string->text; p < limit; p = q)
1423 /* Find the start of an identifier. */
1424 while (p < limit && !is_idstart (*p))
1427 /* Find the end of the identifier. */
1429 while (q < limit && is_idchar (*q))
1434 /* Loop over the function macro arguments to see if the
1435 identifier inside the string matches one of them. */
1436 for (i = 0; i < macro->paramc; i++)
1438 const cpp_hashnode *node = macro->params[i];
1440 if (NODE_LEN (node) == len
1441 && !memcmp (p, NODE_NAME (node), len))
1444 "macro argument \"%s\" would be stringified with -traditional.",
1452 /* Returns the name, arguments and expansion of a macro, in a format
1453 suitable to be read back in again, and therefore also for DWARF 2
1454 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1455 Caller is expected to generate the "#define" bit if needed. The
1456 returned text is temporary, and automatically freed later. */
1458 const unsigned char *
1459 cpp_macro_definition (pfile, node)
1461 const cpp_hashnode *node;
1463 unsigned int i, len;
1464 const cpp_macro *macro = node->value.macro;
1465 unsigned char *buffer;
1467 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1469 cpp_ice (pfile, "invalid hash type %d in cpp_macro_definition", node->type);
1473 /* Calculate length. */
1474 len = NODE_LEN (node) + 1; /* ' ' */
1475 if (macro->fun_like)
1477 len += 3; /* "()" plus possible final "." of named
1478 varargs (we have + 2 below). */
1479 for (i = 0; i < macro->paramc; i++)
1480 len += NODE_LEN (macro->params[i]) + 2; /* ", " */
1483 for (i = 0; i < macro->count; i++)
1485 cpp_token *token = ¯o->expansion[i];
1487 if (token->type == CPP_MACRO_ARG)
1488 len += NODE_LEN (macro->params[token->val.arg_no - 1]);
1490 len += cpp_token_len (token); /* Includes room for ' '. */
1491 if (token->flags & STRINGIFY_ARG)
1493 if (token->flags & PASTE_LEFT)
1494 len += 3; /* " ##" */
1497 if (len > pfile->macro_buffer_len)
1499 pfile->macro_buffer = (U_CHAR *) xrealloc (pfile->macro_buffer, len);
1500 pfile->macro_buffer_len = len;
1503 /* Fill in the buffer. Start with the macro name. */
1504 buffer = pfile->macro_buffer;
1505 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1506 buffer += NODE_LEN (node);
1508 /* Parameter names. */
1509 if (macro->fun_like)
1512 for (i = 0; i < macro->paramc; i++)
1514 cpp_hashnode *param = macro->params[i];
1516 if (param != pfile->spec_nodes.n__VA_ARGS__)
1518 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1519 buffer += NODE_LEN (param);
1522 if (i + 1 < macro->paramc)
1523 *buffer++ = ',', *buffer++ = ' ';
1524 else if (macro->variadic)
1525 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1530 /* Expansion tokens. */
1534 for (i = 0; i < macro->count; i++)
1536 cpp_token *token = ¯o->expansion[i];
1538 if (token->flags & PREV_WHITE)
1540 if (token->flags & STRINGIFY_ARG)
1543 if (token->type == CPP_MACRO_ARG)
1545 len = NODE_LEN (macro->params[token->val.arg_no - 1]);
1547 NODE_NAME (macro->params[token->val.arg_no - 1]), len);
1551 buffer = cpp_spell_token (pfile, token, buffer);
1553 if (token->flags & PASTE_LEFT)
1558 /* Next has PREV_WHITE; see _cpp_create_definition. */
1564 return pfile->macro_buffer;