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 *new_number_token PARAMS ((cpp_reader *, unsigned int));
58 static const cpp_token *stringify_arg PARAMS ((cpp_reader *, macro_arg *));
59 static void paste_all_tokens PARAMS ((cpp_reader *, const cpp_token *));
60 static bool paste_tokens PARAMS ((cpp_reader *, const cpp_token **,
62 static void replace_args PARAMS ((cpp_reader *, cpp_hashnode *, cpp_macro *,
64 static _cpp_buff *funlike_invocation_p PARAMS ((cpp_reader *, cpp_hashnode *));
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 int warn_of_redefinition PARAMS ((const cpp_hashnode *,
72 static int save_parameter PARAMS ((cpp_reader *, cpp_macro *, cpp_hashnode *));
73 static int parse_params PARAMS ((cpp_reader *, cpp_macro *));
74 static void check_trad_stringification PARAMS ((cpp_reader *,
78 /* Allocates and returns a CPP_STRING token, containing TEXT of length
79 LEN, after null-terminating it. TEXT must be in permanent storage. */
80 static const cpp_token *
81 new_string_token (pfile, text, len)
86 cpp_token *token = _cpp_temp_token (pfile);
89 token->type = CPP_STRING;
90 token->val.str.len = len;
91 token->val.str.text = text;
96 /* Allocates and returns a CPP_NUMBER token evaluating to NUMBER. */
97 static const cpp_token *
98 new_number_token (pfile, number)
102 cpp_token *token = _cpp_temp_token (pfile);
103 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
104 unsigned char *buf = _cpp_unaligned_alloc (pfile, 21);
106 sprintf ((char *) buf, "%u", number);
107 token->type = CPP_NUMBER;
108 token->val.str.text = buf;
109 token->val.str.len = ustrlen (buf);
114 static const char * const monthnames[] =
116 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
117 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
120 /* Handle builtin macros like __FILE__, and push the resulting token
121 on the context stack. Also handles _Pragma, for which no new token
122 is created. Returns 1 if it generates a new token context, 0 to
123 return the token to the caller. */
125 builtin_macro (pfile, node)
129 const cpp_token *result;
131 switch (node->value.builtin)
134 cpp_error (pfile, DL_ICE, "invalid built-in macro \"%s\"",
144 const struct line_map *map = pfile->map;
146 if (node->value.builtin == BT_BASE_FILE)
147 while (! MAIN_FILE_P (map))
148 map = INCLUDED_FROM (&pfile->line_maps, map);
152 buf = _cpp_unaligned_alloc (pfile, len * 4 + 1);
153 len = cpp_quote_string (buf, (const unsigned char *) name, len) - buf;
155 result = new_string_token (pfile, buf, len);
159 case BT_INCLUDE_LEVEL:
160 /* The line map depth counts the primary source as level 1, but
161 historically __INCLUDE_DEPTH__ has called the primary source
163 result = new_number_token (pfile, pfile->line_maps.depth - 1);
167 /* If __LINE__ is embedded in a macro, it must expand to the
168 line of the macro's invocation, not its definition.
169 Otherwise things like assert() will not work properly. */
170 result = new_number_token (pfile,
171 SOURCE_LINE (pfile->map,
172 pfile->cur_token[-1].line));
175 /* __STDC__ has the value 1 under normal circumstances.
176 However, if (a) we are in a system header, (b) the option
177 stdc_0_in_system_headers is true (set by target config), and
178 (c) we are not in strictly conforming mode, then it has the
183 enum c_lang lang = CPP_OPTION (pfile, lang);
184 if (CPP_IN_SYSTEM_HEADER (pfile)
185 && CPP_OPTION (pfile, stdc_0_in_system_headers)
186 && !(lang == CLK_STDC89 || lang == CLK_STDC94
187 || lang == CLK_STDC99)) /* || lang == CLK_CXX98 ? */
192 result = new_number_token (pfile, stdc);
198 if (pfile->date.type == CPP_EOF)
200 /* Allocate __DATE__ and __TIME__ strings from permanent
201 storage. We only do this once, and don't generate them
202 at init time, because time() and localtime() are very
203 slow on some systems. */
204 time_t tt = time (NULL);
205 struct tm *tb = localtime (&tt);
207 pfile->date.val.str.text =
208 _cpp_unaligned_alloc (pfile, sizeof ("Oct 11 1347"));
209 pfile->date.val.str.len = sizeof ("Oct 11 1347") - 1;
210 pfile->date.type = CPP_STRING;
211 pfile->date.flags = 0;
212 sprintf ((char *) pfile->date.val.str.text, "%s %2d %4d",
213 monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
215 pfile->time.val.str.text =
216 _cpp_unaligned_alloc (pfile, sizeof ("12:34:56"));
217 pfile->time.val.str.len = sizeof ("12:34:56") - 1;
218 pfile->time.type = CPP_STRING;
219 pfile->time.flags = 0;
220 sprintf ((char *) pfile->time.val.str.text, "%02d:%02d:%02d",
221 tb->tm_hour, tb->tm_min, tb->tm_sec);
224 if (node->value.builtin == BT_DATE)
225 result = &pfile->date;
227 result = &pfile->time;
231 /* Don't interpret _Pragma within directives. The standard is
232 not clear on this, but to me this makes most sense. */
233 if (pfile->state.in_directive)
236 _cpp_do__Pragma (pfile);
240 push_token_context (pfile, NULL, result, 1);
244 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
245 backslashes and double quotes. Non-printable characters are
246 converted to octal. DEST must be of sufficient size. Returns
247 a pointer to the end of the string. */
249 cpp_quote_string (dest, src, len)
258 if (c == '\\' || c == '"')
269 sprintf ((char *) dest, "\\%03o", c);
278 /* Convert a token sequence ARG to a single string token according to
279 the rules of the ISO C #-operator. */
280 static const cpp_token *
281 stringify_arg (pfile, arg)
285 unsigned char *dest = BUFF_FRONT (pfile->u_buff);
286 unsigned int i, escape_it, backslash_count = 0;
287 const cpp_token *source = NULL;
290 /* Loop, reading in the argument's tokens. */
291 for (i = 0; i < arg->count; i++)
293 const cpp_token *token = arg->first[i];
295 if (token->type == CPP_PADDING)
298 source = token->val.source;
302 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
303 || token->type == CPP_CHAR || token->type == CPP_WCHAR);
305 /* Room for each char being written in octal, initial space and
307 len = cpp_token_len (token);
312 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
314 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
315 _cpp_extend_buff (pfile, &pfile->u_buff, len);
316 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
319 /* Leading white space? */
320 if (dest != BUFF_FRONT (pfile->u_buff))
324 if (source->flags & PREV_WHITE)
331 _cpp_buff *buff = _cpp_get_buff (pfile, len);
332 unsigned char *buf = BUFF_FRONT (buff);
333 len = cpp_spell_token (pfile, token, buf) - buf;
334 dest = cpp_quote_string (dest, buf, len);
335 _cpp_release_buff (pfile, buff);
338 dest = cpp_spell_token (pfile, token, dest);
340 if (token->type == CPP_OTHER && token->val.c == '\\')
346 /* Ignore the final \ of invalid string literals. */
347 if (backslash_count & 1)
349 cpp_error (pfile, DL_WARNING,
350 "invalid string literal, ignoring final '\\'");
354 /* Commit the memory, including NUL, and return the token. */
355 len = dest - BUFF_FRONT (pfile->u_buff);
356 BUFF_FRONT (pfile->u_buff) = dest + 1;
357 return new_string_token (pfile, dest - len, len);
360 /* Try to paste two tokens. On success, return non-zero. In any
361 case, PLHS is updated to point to the pasted token, which is
362 guaranteed to not have the PASTE_LEFT flag set. */
364 paste_tokens (pfile, plhs, rhs)
366 const cpp_token **plhs, *rhs;
368 unsigned char *buf, *end;
369 const cpp_token *lhs;
374 len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
375 buf = (unsigned char *) alloca (len);
376 end = cpp_spell_token (pfile, lhs, buf);
378 /* Avoid comment headers, since they are still processed in stage 3.
379 It is simpler to insert a space here, rather than modifying the
380 lexer to ignore comments in some circumstances. Simply returning
381 false doesn't work, since we want to clear the PASTE_LEFT flag. */
382 if (lhs->type == CPP_DIV
383 && (rhs->type == CPP_MULT || rhs->type == CPP_DIV))
385 end = cpp_spell_token (pfile, rhs, end);
388 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true, 1);
390 /* Tweak the column number the lexer will report. */
391 pfile->buffer->col_adjust = pfile->cur_token[-1].col - 1;
393 /* We don't want a leading # to be interpreted as a directive. */
394 pfile->buffer->saved_flags = 0;
396 /* Set pfile->cur_token as required by _cpp_lex_direct. */
397 pfile->cur_token = _cpp_temp_token (pfile);
398 *plhs = _cpp_lex_direct (pfile);
399 valid = pfile->buffer->cur == pfile->buffer->rlimit;
400 _cpp_pop_buffer (pfile);
405 /* Handles an arbitrarily long sequence of ## operators, with initial
406 operand LHS. This implementation is left-associative,
407 non-recursive, and finishes a paste before handling succeeding
408 ones. If a paste fails, we back up to the RHS of the failing ##
409 operator before pushing the context containing the result of prior
410 successful pastes, with the effect that the RHS appears in the
411 output stream after the pasted LHS normally. */
413 paste_all_tokens (pfile, lhs)
415 const cpp_token *lhs;
417 const cpp_token *rhs;
418 cpp_context *context = pfile->context;
422 /* Take the token directly from the current context. We can do
423 this, because we are in the replacement list of either an
424 object-like macro, or a function-like macro with arguments
425 inserted. In either case, the constraints to #define
426 guarantee we have at least one more token. */
427 if (context->direct_p)
428 rhs = context->first.token++;
430 rhs = *context->first.ptoken++;
432 if (rhs->type == CPP_PADDING)
435 if (!paste_tokens (pfile, &lhs, rhs))
437 _cpp_backup_tokens (pfile, 1);
439 /* Mandatory error for all apart from assembler. */
440 if (CPP_OPTION (pfile, lang) != CLK_ASM)
441 cpp_error (pfile, DL_ERROR,
442 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
443 cpp_token_as_text (pfile, lhs),
444 cpp_token_as_text (pfile, rhs));
448 while (rhs->flags & PASTE_LEFT);
450 /* Put the resulting token in its own context. */
451 push_token_context (pfile, NULL, lhs, 1);
454 /* Reads and returns the arguments to a function-like macro
455 invocation. Assumes the opening parenthesis has been processed.
456 If there is an error, emits an appropriate diagnostic and returns
457 NULL. Each argument is terminated by a CPP_EOF token, for the
458 future benefit of expand_arg(). */
460 collect_args (pfile, node)
462 const cpp_hashnode *node;
464 _cpp_buff *buff, *base_buff;
466 macro_arg *args, *arg;
467 const cpp_token *token;
471 macro = node->value.macro;
473 argc = macro->paramc;
476 buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
477 + sizeof (macro_arg)));
479 args = (macro_arg *) buff->base;
480 memset (args, 0, argc * sizeof (macro_arg));
481 buff->cur = (unsigned char *) &args[argc];
482 arg = args, argc = 0;
484 /* Collect the tokens making up each argument. We don't yet know
485 how many arguments have been supplied, whether too many or too
486 few. Hence the slightly bizarre usage of "argc" and "arg". */
489 unsigned int paren_depth = 0;
490 unsigned int ntokens = 0;
493 arg->first = (const cpp_token **) buff->cur;
497 /* Require space for 2 new tokens (including a CPP_EOF). */
498 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
500 buff = _cpp_append_extend_buff (pfile, buff,
501 1000 * sizeof (cpp_token *));
502 arg->first = (const cpp_token **) buff->cur;
505 token = cpp_get_token (pfile);
507 if (token->type == CPP_PADDING)
509 /* Drop leading padding. */
513 else if (token->type == CPP_OPEN_PAREN)
515 else if (token->type == CPP_CLOSE_PAREN)
517 if (paren_depth-- == 0)
520 else if (token->type == CPP_COMMA)
522 /* A comma does not terminate an argument within
523 parentheses or as part of a variable argument. */
525 && ! (macro->variadic && argc == macro->paramc))
528 else if (token->type == CPP_EOF
529 || (token->type == CPP_HASH && token->flags & BOL))
532 arg->first[ntokens++] = token;
535 /* Drop trailing padding. */
536 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
539 arg->count = ntokens;
540 arg->first[ntokens] = &pfile->eof;
542 /* Terminate the argument. Excess arguments loop back and
543 overwrite the final legitimate argument, before failing. */
544 if (argc <= macro->paramc)
546 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
547 if (argc != macro->paramc)
551 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
553 if (token->type == CPP_EOF)
555 /* We still need the CPP_EOF to end directives, and to end
556 pre-expansion of a macro argument. Step back is not
557 unconditional, since we don't want to return a CPP_EOF to our
558 callers at the end of an -include-d file. */
559 if (pfile->context->prev || pfile->state.in_directive)
560 _cpp_backup_tokens (pfile, 1);
561 cpp_error (pfile, DL_ERROR,
562 "unterminated argument list invoking macro \"%s\"",
566 else if (argc < macro->paramc)
568 /* As an extension, a rest argument is allowed to not appear in
569 the invocation at all.
570 e.g. #define debug(format, args...) something
573 This is exactly the same as if there had been an empty rest
574 argument - debug("string", ). */
576 if (argc + 1 == macro->paramc && macro->variadic)
578 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
579 cpp_error (pfile, DL_PEDWARN,
580 "ISO C99 requires rest arguments to be used");
584 cpp_error (pfile, DL_ERROR,
585 "macro \"%s\" requires %u arguments, but only %u given",
586 NODE_NAME (node), macro->paramc, argc);
590 else if (argc > macro->paramc)
592 /* Empty argument to a macro taking no arguments is OK. */
593 if (argc != 1 || arg->count)
595 cpp_error (pfile, DL_ERROR,
596 "macro \"%s\" passed %u arguments, but takes just %u",
597 NODE_NAME (node), argc, macro->paramc);
605 _cpp_release_buff (pfile, base_buff);
609 /* Search for an opening parenthesis to the macro of NODE, in such a
610 way that, if none is found, we don't lose the information in any
611 intervening padding tokens. If we find the parenthesis, collect
612 the arguments and return the buffer containing them. */
614 funlike_invocation_p (pfile, node)
618 const cpp_token *token, *padding = NULL;
622 token = cpp_get_token (pfile);
623 if (token->type != CPP_PADDING)
626 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
630 if (token->type == CPP_OPEN_PAREN)
632 pfile->state.parsing_args = 2;
633 return collect_args (pfile, node);
636 /* CPP_EOF can be the end of macro arguments, or the end of the
637 file. We mustn't back up over the latter. Ugh. */
638 if (token->type != CPP_EOF || token == &pfile->eof)
640 /* Back up. We may have skipped padding, in which case backing
641 up more than one token when expanding macros is in general
642 too difficult. We re-insert it in its own context. */
643 _cpp_backup_tokens (pfile, 1);
645 push_token_context (pfile, NULL, padding, 1);
651 /* Push the context of a macro with hash entry NODE onto the context
652 stack. If we can successfully expand the macro, we push a context
653 containing its yet-to-be-rescanned replacement list and return one.
654 Otherwise, we don't push a context and return zero. */
656 enter_macro_context (pfile, node)
660 /* The presence of a macro invalidates a file's controlling macro. */
661 pfile->mi_valid = false;
663 pfile->state.angled_headers = false;
665 /* Handle standard macros. */
666 if (! (node->flags & NODE_BUILTIN))
668 cpp_macro *macro = node->value.macro;
674 pfile->state.prevent_expansion++;
675 pfile->keep_tokens++;
676 pfile->state.parsing_args = 1;
677 buff = funlike_invocation_p (pfile, node);
678 pfile->state.parsing_args = 0;
679 pfile->keep_tokens--;
680 pfile->state.prevent_expansion--;
684 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
685 cpp_error (pfile, DL_WARNING,
686 "function-like macro \"%s\" must be used with arguments in traditional C",
692 if (macro->paramc > 0)
693 replace_args (pfile, node, macro, (macro_arg *) buff->base);
694 _cpp_release_buff (pfile, buff);
697 /* Disable the macro within its expansion. */
698 node->flags |= NODE_DISABLED;
700 if (macro->paramc == 0)
701 push_token_context (pfile, node, macro->exp.tokens, macro->count);
706 /* Handle built-in macros and the _Pragma operator. */
707 return builtin_macro (pfile, node);
710 /* Replace the parameters in a function-like macro of NODE with the
711 actual ARGS, and place the result in a newly pushed token context.
712 Expand each argument before replacing, unless it is operated upon
713 by the # or ## operators. */
715 replace_args (pfile, node, macro, args)
721 unsigned int i, total;
722 const cpp_token *src, *limit;
723 const cpp_token **dest, **first;
727 /* First, fully macro-expand arguments, calculating the number of
728 tokens in the final expansion as we go. The ordering of the if
729 statements below is subtle; we must handle stringification before
731 total = macro->count;
732 limit = macro->exp.tokens + macro->count;
734 for (src = macro->exp.tokens; src < limit; src++)
735 if (src->type == CPP_MACRO_ARG)
737 /* Leading and trailing padding tokens. */
740 /* We have an argument. If it is not being stringified or
741 pasted it is macro-replaced before insertion. */
742 arg = &args[src->val.arg_no - 1];
744 if (src->flags & STRINGIFY_ARG)
746 if (!arg->stringified)
747 arg->stringified = stringify_arg (pfile, arg);
749 else if ((src->flags & PASTE_LEFT)
750 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
751 total += arg->count - 1;
755 expand_arg (pfile, arg);
756 total += arg->expanded_count - 1;
760 /* Now allocate space for the expansion, copy the tokens and replace
762 buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
763 first = (const cpp_token **) buff->base;
766 for (src = macro->exp.tokens; src < limit; src++)
769 const cpp_token **from, **paste_flag;
771 if (src->type != CPP_MACRO_ARG)
778 arg = &args[src->val.arg_no - 1];
779 if (src->flags & STRINGIFY_ARG)
780 count = 1, from = &arg->stringified;
781 else if (src->flags & PASTE_LEFT)
782 count = arg->count, from = arg->first;
783 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
785 count = arg->count, from = arg->first;
788 /* GCC has special semantics for , ## b where b is a
789 varargs parameter: the comma disappears if b was
790 given no actual arguments (not merely if b is an
791 empty argument); otherwise the paste flag is removed. */
792 if (dest[-1]->type == CPP_COMMA
794 && src->val.arg_no == macro->paramc)
799 paste_flag = dest - 1;
801 /* Remove the paste flag if the RHS is a placemarker. */
803 paste_flag = dest - 1;
807 count = arg->expanded_count, from = arg->expanded;
809 /* Padding on the left of an argument (unless RHS of ##). */
810 if (!pfile->state.in_directive
811 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
812 *dest++ = padding_token (pfile, src);
816 memcpy (dest, from, count * sizeof (cpp_token *));
819 /* With a non-empty argument on the LHS of ##, the last
820 token should be flagged PASTE_LEFT. */
821 if (src->flags & PASTE_LEFT)
822 paste_flag = dest - 1;
825 /* Avoid paste on RHS (even case count == 0). */
826 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
827 *dest++ = &pfile->avoid_paste;
829 /* Add a new paste flag, or remove an unwanted one. */
832 cpp_token *token = _cpp_temp_token (pfile);
833 token->type = (*paste_flag)->type;
834 token->val.str = (*paste_flag)->val.str;
835 if (src->flags & PASTE_LEFT)
836 token->flags = (*paste_flag)->flags | PASTE_LEFT;
838 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
843 /* Free the expanded arguments. */
844 for (i = 0; i < macro->paramc; i++)
845 if (args[i].expanded)
846 free (args[i].expanded);
848 push_ptoken_context (pfile, node, buff, first, dest - first);
851 /* Return a special padding token, with padding inherited from SOURCE. */
852 static const cpp_token *
853 padding_token (pfile, source)
855 const cpp_token *source;
857 cpp_token *result = _cpp_temp_token (pfile);
859 result->type = CPP_PADDING;
860 result->val.source = source;
865 /* Get a new uninitialized context. Create a new one if we cannot
866 re-use an old one. */
871 cpp_context *result = pfile->context->next;
875 result = xnew (cpp_context);
876 result->prev = pfile->context;
878 pfile->context->next = result;
881 pfile->context = result;
885 /* Push a list of pointers to tokens. */
887 push_ptoken_context (pfile, macro, buff, first, count)
891 const cpp_token **first;
894 cpp_context *context = next_context (pfile);
896 context->direct_p = false;
897 context->macro = macro;
898 context->buff = buff;
899 context->first.ptoken = first;
900 context->last.ptoken = first + count;
903 /* Push a list of tokens. */
905 push_token_context (pfile, macro, first, count)
908 const cpp_token *first;
911 cpp_context *context = next_context (pfile);
913 context->direct_p = true;
914 context->macro = macro;
915 context->buff = NULL;
916 context->first.token = first;
917 context->last.token = first + count;
920 /* Expand an argument ARG before replacing parameters in a
921 function-like macro. This works by pushing a context with the
922 argument's tokens, and then expanding that into a temporary buffer
923 as if it were a normal part of the token stream. collect_args()
924 has terminated the argument's tokens with a CPP_EOF so that we know
925 when we have fully expanded the argument. */
927 expand_arg (pfile, arg)
931 unsigned int capacity;
936 /* Loop, reading in the arguments. */
938 arg->expanded = (const cpp_token **)
939 xmalloc (capacity * sizeof (cpp_token *));
941 push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
944 const cpp_token *token;
946 if (arg->expanded_count + 1 >= capacity)
949 arg->expanded = (const cpp_token **)
950 xrealloc (arg->expanded, capacity * sizeof (cpp_token *));
953 token = cpp_get_token (pfile);
955 if (token->type == CPP_EOF)
958 arg->expanded[arg->expanded_count++] = token;
961 _cpp_pop_context (pfile);
964 /* Pop the current context off the stack, re-enabling the macro if the
965 context represented a macro's replacement list. The context
966 structure is not freed so that we can re-use it later. */
968 _cpp_pop_context (pfile)
971 cpp_context *context = pfile->context;
974 context->macro->flags &= ~NODE_DISABLED;
977 _cpp_release_buff (pfile, context->buff);
979 pfile->context = context->prev;
982 /* Eternal routine to get a token. Also used nearly everywhere
983 internally, except for places where we know we can safely call
984 the lexer directly, such as lexing a directive name.
986 Macro expansions and directives are transparently handled,
987 including entering included files. Thus tokens are post-macro
988 expansion, and after any intervening directives. External callers
989 see CPP_EOF only at EOF. Internal callers also see it when meeting
990 a directive inside a macro call, when at the end of a directive and
991 state.in_directive is still 1, and at the end of argument
994 cpp_get_token (pfile)
997 const cpp_token *result;
1002 cpp_context *context = pfile->context;
1004 /* Context->prev == 0 <=> base context. */
1006 result = _cpp_lex_token (pfile);
1007 else if (context->first.token != context->last.token)
1009 if (context->direct_p)
1010 result = context->first.token++;
1012 result = *context->first.ptoken++;
1014 if (result->flags & PASTE_LEFT)
1016 paste_all_tokens (pfile, result);
1017 if (pfile->state.in_directive)
1019 return padding_token (pfile, result);
1024 _cpp_pop_context (pfile);
1025 if (pfile->state.in_directive)
1027 return &pfile->avoid_paste;
1030 if (pfile->state.in_directive && result->type == CPP_COMMENT)
1033 if (result->type != CPP_NAME)
1036 node = result->val.node;
1038 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
1041 if (!(node->flags & NODE_DISABLED))
1043 if (!pfile->state.prevent_expansion
1044 && enter_macro_context (pfile, node))
1046 if (pfile->state.in_directive)
1048 return padding_token (pfile, result);
1053 /* Flag this token as always unexpandable. FIXME: move this
1054 to collect_args()?. */
1055 cpp_token *t = _cpp_temp_token (pfile);
1056 t->type = result->type;
1057 t->flags = result->flags | NO_EXPAND;
1058 t->val.str = result->val.str;
1068 /* Returns true if we're expanding an object-like macro that was
1069 defined in a system header. Just checks the macro at the top of
1070 the stack. Used for diagnostic suppression. */
1072 cpp_sys_macro_p (pfile)
1075 cpp_hashnode *node = pfile->context->macro;
1077 return node && node->value.macro && node->value.macro->syshdr;
1080 /* Read each token in, until end of the current file. Directives are
1081 transparently processed. */
1083 cpp_scan_nooutput (pfile)
1086 /* Request a CPP_EOF token at the end of this file, rather than
1087 transparently continuing with the including file. */
1088 pfile->buffer->return_at_eof = true;
1090 while (cpp_get_token (pfile)->type != CPP_EOF)
1094 /* Step back one (or more) tokens. Can only step mack more than 1 if
1095 they are from the lexer, and not from macro expansion. */
1097 _cpp_backup_tokens (pfile, count)
1101 if (pfile->context->prev == NULL)
1103 pfile->lookaheads += count;
1107 if (pfile->cur_token == pfile->cur_run->base
1108 /* Possible with -fpreprocessed and no leading #line. */
1109 && pfile->cur_run->prev != NULL)
1111 pfile->cur_run = pfile->cur_run->prev;
1112 pfile->cur_token = pfile->cur_run->limit;
1120 if (pfile->context->direct_p)
1121 pfile->context->first.token--;
1123 pfile->context->first.ptoken--;
1127 /* #define directive parsing and handling. */
1129 /* Returns non-zero if a macro redefinition warning is required. */
1131 warn_of_redefinition (node, macro2)
1132 const cpp_hashnode *node;
1133 const cpp_macro *macro2;
1135 const cpp_macro *macro1;
1138 /* Some redefinitions need to be warned about regardless. */
1139 if (node->flags & NODE_WARN)
1142 /* Redefinition of a macro is allowed if and only if the old and new
1143 definitions are the same. (6.10.3 paragraph 2). */
1144 macro1 = node->value.macro;
1146 /* The quick failures. */
1147 if (macro1->count != macro2->count
1148 || macro1->paramc != macro2->paramc
1149 || macro1->fun_like != macro2->fun_like
1150 || macro1->variadic != macro2->variadic)
1153 /* Check each token. */
1154 for (i = 0; i < macro1->count; i++)
1155 if (! _cpp_equiv_tokens (¯o1->exp.tokens[i], ¯o2->exp.tokens[i]))
1158 /* Check parameter spellings. */
1159 for (i = 0; i < macro1->paramc; i++)
1160 if (macro1->params[i] != macro2->params[i])
1166 /* Free the definition of hashnode H. */
1168 _cpp_free_definition (h)
1171 /* Macros and assertions no longer have anything to free. */
1173 /* Clear builtin flag in case of redefinition. */
1174 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED);
1177 /* Save parameter NODE to the parameter list of macro MACRO. Returns
1178 zero on success, non-zero if the parameter is a duplicate. */
1180 save_parameter (pfile, macro, node)
1185 /* Constraint 6.10.3.6 - duplicate parameter names. */
1186 if (node->arg_index)
1188 cpp_error (pfile, DL_ERROR, "duplicate macro parameter \"%s\"",
1193 if (BUFF_ROOM (pfile->a_buff)
1194 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1195 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
1197 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
1198 node->arg_index = macro->paramc;
1202 /* Check the syntax of the parameters in a MACRO definition. */
1204 parse_params (pfile, macro)
1208 unsigned int prev_ident = 0;
1212 const cpp_token *token = _cpp_lex_token (pfile);
1214 switch (token->type)
1217 /* Allow/ignore comments in parameter lists if we are
1218 preserving comments in macro expansions. */
1219 if (token->type == CPP_COMMENT
1220 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
1223 cpp_error (pfile, DL_ERROR,
1224 "\"%s\" may not appear in macro parameter list",
1225 cpp_token_as_text (pfile, token));
1231 cpp_error (pfile, DL_ERROR,
1232 "macro parameters must be comma-separated");
1237 if (save_parameter (pfile, macro, token->val.node))
1241 case CPP_CLOSE_PAREN:
1242 if (prev_ident || macro->paramc == 0)
1245 /* Fall through to pick up the error. */
1249 cpp_error (pfile, DL_ERROR, "parameter name missing");
1256 macro->variadic = 1;
1259 save_parameter (pfile, macro, pfile->spec_nodes.n__VA_ARGS__);
1260 pfile->state.va_args_ok = 1;
1261 if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
1262 cpp_error (pfile, DL_PEDWARN,
1263 "anonymous variadic macros were introduced in C99");
1265 else if (CPP_OPTION (pfile, pedantic))
1266 cpp_error (pfile, DL_PEDWARN,
1267 "ISO C does not permit named variadic macros");
1269 /* We're at the end, and just expect a closing parenthesis. */
1270 token = _cpp_lex_token (pfile);
1271 if (token->type == CPP_CLOSE_PAREN)
1276 cpp_error (pfile, DL_ERROR, "missing ')' in macro parameter list");
1282 /* Allocate room for a token from a macro's replacement list. */
1284 alloc_expansion_token (pfile, macro)
1288 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1289 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
1291 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
1294 /* Lex a token from the expansion of MACRO, but mark parameters as we
1295 find them and warn of traditional stringification. */
1297 lex_expansion_token (pfile, macro)
1303 pfile->cur_token = alloc_expansion_token (pfile, macro);
1304 token = _cpp_lex_direct (pfile);
1306 /* Is this a parameter? */
1307 if (token->type == CPP_NAME && token->val.node->arg_index)
1309 token->type = CPP_MACRO_ARG;
1310 token->val.arg_no = token->val.node->arg_index;
1312 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1313 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1314 check_trad_stringification (pfile, macro, &token->val.str);
1319 /* Parse a macro and save its expansion. Returns non-zero on success. */
1321 _cpp_create_definition (pfile, node)
1326 cpp_token *token, *saved_cur_token;
1327 const cpp_token *ctoken;
1328 unsigned int i, ok = 1;
1330 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
1331 macro->line = pfile->directive_line;
1334 macro->variadic = 0;
1336 macro->fun_like = 0;
1338 /* Get the first token of the expansion (or the '(' of a
1339 function-like macro). */
1340 ctoken = _cpp_lex_token (pfile);
1342 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
1344 ok = parse_params (pfile, macro);
1345 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1349 /* Success. Commit the parameter array. */
1350 BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->params[macro->paramc];
1351 macro->fun_like = 1;
1353 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
1354 cpp_error (pfile, DL_PEDWARN,
1355 "ISO C requires whitespace after the macro name");
1357 saved_cur_token = pfile->cur_token;
1359 if (macro->fun_like)
1360 token = lex_expansion_token (pfile, macro);
1363 token = alloc_expansion_token (pfile, macro);
1369 /* Check the stringifying # constraint 6.10.3.2.1 of
1370 function-like macros when lexing the subsequent token. */
1371 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1373 if (token->type == CPP_MACRO_ARG)
1375 token->flags &= ~PREV_WHITE;
1376 token->flags |= STRINGIFY_ARG;
1377 token->flags |= token[-1].flags & PREV_WHITE;
1378 token[-1] = token[0];
1381 /* Let assembler get away with murder. */
1382 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
1385 cpp_error (pfile, DL_ERROR,
1386 "'#' is not followed by a macro parameter");
1391 if (token->type == CPP_EOF)
1394 /* Paste operator constraint 6.10.3.3.1. */
1395 if (token->type == CPP_PASTE)
1397 /* Token-paste ##, can appear in both object-like and
1398 function-like macros, but not at the ends. */
1399 if (--macro->count > 0)
1400 token = lex_expansion_token (pfile, macro);
1402 if (macro->count == 0 || token->type == CPP_EOF)
1405 cpp_error (pfile, DL_ERROR,
1406 "'##' cannot appear at either end of a macro expansion");
1410 token[-1].flags |= PASTE_LEFT;
1413 token = lex_expansion_token (pfile, macro);
1416 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
1418 /* Don't count the CPP_EOF. */
1421 /* Clear whitespace on first token for warn_of_redefinition(). */
1423 macro->exp.tokens[0].flags &= ~PREV_WHITE;
1425 /* Commit the memory. */
1426 BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->exp.tokens[macro->count];
1428 /* Implement the macro-defined-to-itself optimisation. */
1429 if (macro->count == 1 && !macro->fun_like
1430 && macro->exp.tokens[0].type == CPP_NAME
1431 && macro->exp.tokens[0].val.node == node)
1432 node->flags |= NODE_DISABLED;
1434 /* To suppress some diagnostics. */
1435 macro->syshdr = pfile->map->sysp != 0;
1437 if (node->type != NT_VOID)
1439 if (warn_of_redefinition (node, macro))
1441 cpp_error_with_line (pfile, DL_PEDWARN, pfile->directive_line, 0,
1442 "\"%s\" redefined", NODE_NAME (node));
1444 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1445 cpp_error_with_line (pfile, DL_PEDWARN, node->value.macro->line, 0,
1446 "this is the location of the previous definition");
1448 _cpp_free_definition (node);
1451 /* Enter definition in hash table. */
1452 node->type = NT_MACRO;
1453 node->value.macro = macro;
1454 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
1455 node->flags |= NODE_WARN;
1459 /* Set type for SEEN_EOL() in cpplib.c, restore the lexer position. */
1460 saved_cur_token[-1].type = pfile->cur_token[-1].type;
1461 pfile->cur_token = saved_cur_token;
1465 /* Stop the lexer accepting __VA_ARGS__. */
1466 pfile->state.va_args_ok = 0;
1468 /* Clear the fast argument lookup indices. */
1469 for (i = macro->paramc; i-- > 0; )
1470 macro->params[i]->arg_index = 0;
1475 /* Warn if a token in STRING matches one of a function-like MACRO's
1478 check_trad_stringification (pfile, macro, string)
1480 const cpp_macro *macro;
1481 const cpp_string *string;
1483 unsigned int i, len;
1484 const uchar *p, *q, *limit = string->text + string->len;
1486 /* Loop over the string. */
1487 for (p = string->text; p < limit; p = q)
1489 /* Find the start of an identifier. */
1490 while (p < limit && !is_idstart (*p))
1493 /* Find the end of the identifier. */
1495 while (q < limit && is_idchar (*q))
1500 /* Loop over the function macro arguments to see if the
1501 identifier inside the string matches one of them. */
1502 for (i = 0; i < macro->paramc; i++)
1504 const cpp_hashnode *node = macro->params[i];
1506 if (NODE_LEN (node) == len
1507 && !memcmp (p, NODE_NAME (node), len))
1509 cpp_error (pfile, DL_WARNING,
1510 "macro argument \"%s\" would be stringified in traditional C",
1518 /* Returns the name, arguments and expansion of a macro, in a format
1519 suitable to be read back in again, and therefore also for DWARF 2
1520 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1521 Caller is expected to generate the "#define" bit if needed. The
1522 returned text is temporary, and automatically freed later. */
1523 const unsigned char *
1524 cpp_macro_definition (pfile, node)
1526 const cpp_hashnode *node;
1528 unsigned int i, len;
1529 const cpp_macro *macro = node->value.macro;
1530 unsigned char *buffer;
1532 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1534 cpp_error (pfile, DL_ICE,
1535 "invalid hash type %d in cpp_macro_definition", node->type);
1539 /* Calculate length. */
1540 len = NODE_LEN (node) + 2; /* ' ' and NUL. */
1541 if (macro->fun_like)
1543 len += 4; /* "()" plus possible final ".." of named
1544 varargs (we have + 1 below). */
1545 for (i = 0; i < macro->paramc; i++)
1546 len += NODE_LEN (macro->params[i]) + 1; /* "," */
1549 for (i = 0; i < macro->count; i++)
1551 cpp_token *token = ¯o->exp.tokens[i];
1553 if (token->type == CPP_MACRO_ARG)
1554 len += NODE_LEN (macro->params[token->val.arg_no - 1]);
1556 len += cpp_token_len (token); /* Includes room for ' '. */
1557 if (token->flags & STRINGIFY_ARG)
1559 if (token->flags & PASTE_LEFT)
1560 len += 3; /* " ##" */
1563 if (len > pfile->macro_buffer_len)
1565 pfile->macro_buffer = (uchar *) xrealloc (pfile->macro_buffer, len);
1566 pfile->macro_buffer_len = len;
1569 /* Fill in the buffer. Start with the macro name. */
1570 buffer = pfile->macro_buffer;
1571 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1572 buffer += NODE_LEN (node);
1574 /* Parameter names. */
1575 if (macro->fun_like)
1578 for (i = 0; i < macro->paramc; i++)
1580 cpp_hashnode *param = macro->params[i];
1582 if (param != pfile->spec_nodes.n__VA_ARGS__)
1584 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1585 buffer += NODE_LEN (param);
1588 if (i + 1 < macro->paramc)
1589 /* Don't emit a space after the comma here; we're trying
1590 to emit a Dwarf-friendly definition, and the Dwarf spec
1591 forbids spaces in the argument list. */
1593 else if (macro->variadic)
1594 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1599 /* The Dwarf spec requires a space after the macro name, even if the
1600 definition is the empty string. */
1603 /* Expansion tokens. */
1606 for (i = 0; i < macro->count; i++)
1608 cpp_token *token = ¯o->exp.tokens[i];
1610 if (token->flags & PREV_WHITE)
1612 if (token->flags & STRINGIFY_ARG)
1615 if (token->type == CPP_MACRO_ARG)
1617 len = NODE_LEN (macro->params[token->val.arg_no - 1]);
1619 NODE_NAME (macro->params[token->val.arg_no - 1]), len);
1623 buffer = cpp_spell_token (pfile, token, buffer);
1625 if (token->flags & PASTE_LEFT)
1630 /* Next has PREV_WHITE; see _cpp_create_definition. */
1636 return pfile->macro_buffer;