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! */
33 cpp_hashnode **params; /* Parameters, if any. */
34 cpp_token *expansion; /* First token of replacement list. */
35 unsigned int line; /* Starting line number. */
36 unsigned int count; /* Number of tokens in expansion. */
37 unsigned short paramc; /* Number of parameters. */
38 unsigned int fun_like : 1; /* If a function-like macro. */
39 unsigned int variadic : 1; /* If a variadic macro. */
40 unsigned int syshdr : 1; /* If macro defined in system header. */
43 typedef struct macro_arg macro_arg;
46 const cpp_token **first; /* First token in unexpanded argument. */
47 const cpp_token **expanded; /* Macro-expanded argument. */
48 const cpp_token *stringified; /* Stringified argument. */
49 unsigned int count; /* # of tokens in argument. */
50 unsigned int expanded_count; /* # of tokens in expanded argument. */
53 /* Macro expansion. */
55 static int enter_macro_context PARAMS ((cpp_reader *, cpp_hashnode *));
56 static int builtin_macro PARAMS ((cpp_reader *, cpp_hashnode *));
57 static void push_token_context
58 PARAMS ((cpp_reader *, cpp_hashnode *, const cpp_token *, unsigned int));
59 static void push_ptoken_context
60 PARAMS ((cpp_reader *, cpp_hashnode *, _cpp_buff *,
61 const cpp_token **, unsigned int));
62 static _cpp_buff *collect_args PARAMS ((cpp_reader *, const cpp_hashnode *));
63 static cpp_context *next_context PARAMS ((cpp_reader *));
64 static const cpp_token *padding_token
65 PARAMS ((cpp_reader *, const cpp_token *));
66 static void expand_arg PARAMS ((cpp_reader *, macro_arg *));
67 static const cpp_token *new_string_token PARAMS ((cpp_reader *, U_CHAR *,
69 static const cpp_token *new_number_token PARAMS ((cpp_reader *, unsigned int));
70 static const cpp_token *stringify_arg PARAMS ((cpp_reader *, macro_arg *));
71 static void paste_all_tokens PARAMS ((cpp_reader *, const cpp_token *));
72 static bool paste_tokens PARAMS ((cpp_reader *, const cpp_token **,
74 static void replace_args PARAMS ((cpp_reader *, cpp_hashnode *, macro_arg *));
75 static _cpp_buff *funlike_invocation_p PARAMS ((cpp_reader *, cpp_hashnode *));
77 /* #define directive parsing and handling. */
79 static cpp_token *alloc_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
80 static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
81 static int warn_of_redefinition PARAMS ((const cpp_hashnode *,
83 static int save_parameter PARAMS ((cpp_reader *, cpp_macro *, cpp_hashnode *));
84 static int parse_params PARAMS ((cpp_reader *, cpp_macro *));
85 static void check_trad_stringification PARAMS ((cpp_reader *,
89 /* Allocates and returns a CPP_STRING token, containing TEXT of length
90 LEN, after null-terminating it. TEXT must be in permanent storage. */
91 static const cpp_token *
92 new_string_token (pfile, text, len)
97 cpp_token *token = _cpp_temp_token (pfile);
100 token->type = CPP_STRING;
101 token->val.str.len = len;
102 token->val.str.text = text;
107 /* Allocates and returns a CPP_NUMBER token evaluating to NUMBER. */
108 static const cpp_token *
109 new_number_token (pfile, number)
113 cpp_token *token = _cpp_temp_token (pfile);
114 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
115 unsigned char *buf = _cpp_unaligned_alloc (pfile, 21);
117 sprintf ((char *) buf, "%u", number);
118 token->type = CPP_NUMBER;
119 token->val.str.text = buf;
120 token->val.str.len = ustrlen (buf);
125 static const char * const monthnames[] =
127 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
128 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
131 /* Handle builtin macros like __FILE__, and push the resulting token
132 on the context stack. Also handles _Pragma, for which no new token
133 is created. Returns 1 if it generates a new token context, 0 to
134 return the token to the caller. */
136 builtin_macro (pfile, node)
140 const cpp_token *result;
142 switch (node->value.builtin)
145 cpp_ice (pfile, "invalid built-in macro \"%s\"", NODE_NAME (node));
154 const struct line_map *map = pfile->map;
156 if (node->value.builtin == BT_BASE_FILE)
157 while (! MAIN_FILE_P (map))
158 map = INCLUDED_FROM (&pfile->line_maps, map);
162 buf = _cpp_unaligned_alloc (pfile, len * 4 + 1);
163 len = cpp_quote_string (buf, (const unsigned char *) name, len) - buf;
165 result = new_string_token (pfile, buf, len);
169 case BT_INCLUDE_LEVEL:
170 /* The line map depth counts the primary source as level 1, but
171 historically __INCLUDE_DEPTH__ has called the primary source
173 result = new_number_token (pfile, pfile->line_maps.depth - 1);
177 /* If __LINE__ is embedded in a macro, it must expand to the
178 line of the macro's invocation, not its definition.
179 Otherwise things like assert() will not work properly. */
180 result = new_number_token (pfile,
181 SOURCE_LINE (pfile->map,
182 pfile->cur_token[-1].line));
187 int stdc = (!CPP_IN_SYSTEM_HEADER (pfile)
188 || pfile->spec_nodes.n__STRICT_ANSI__->type != NT_VOID);
189 result = new_number_token (pfile, stdc);
195 if (pfile->date.type == CPP_EOF)
197 /* Allocate __DATE__ and __TIME__ strings from permanent
198 storage. We only do this once, and don't generate them
199 at init time, because time() and localtime() are very
200 slow on some systems. */
201 time_t tt = time (NULL);
202 struct tm *tb = localtime (&tt);
204 pfile->date.val.str.text =
205 _cpp_unaligned_alloc (pfile, sizeof ("Oct 11 1347"));
206 pfile->date.val.str.len = sizeof ("Oct 11 1347") - 1;
207 pfile->date.type = CPP_STRING;
208 pfile->date.flags = 0;
209 sprintf ((char *) pfile->date.val.str.text, "%s %2d %4d",
210 monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
212 pfile->time.val.str.text =
213 _cpp_unaligned_alloc (pfile, sizeof ("12:34:56"));
214 pfile->time.val.str.len = sizeof ("12:34:56") - 1;
215 pfile->time.type = CPP_STRING;
216 pfile->time.flags = 0;
217 sprintf ((char *) pfile->time.val.str.text, "%02d:%02d:%02d",
218 tb->tm_hour, tb->tm_min, tb->tm_sec);
221 if (node->value.builtin == BT_DATE)
222 result = &pfile->date;
224 result = &pfile->time;
228 /* Don't interpret _Pragma within directives. The standard is
229 not clear on this, but to me this makes most sense. */
230 if (pfile->state.in_directive)
233 _cpp_do__Pragma (pfile);
237 push_token_context (pfile, NULL, result, 1);
241 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
242 backslashes and double quotes. Non-printable characters are
243 converted to octal. DEST must be of sufficient size. Returns
244 a pointer to the end of the string. */
246 cpp_quote_string (dest, src, len)
255 if (c == '\\' || c == '"')
266 sprintf ((char *) dest, "\\%03o", c);
275 /* Convert a token sequence ARG to a single string token according to
276 the rules of the ISO C #-operator. */
277 static const cpp_token *
278 stringify_arg (pfile, arg)
282 unsigned char *dest = BUFF_FRONT (pfile->u_buff);
283 unsigned int i, escape_it, backslash_count = 0;
284 const cpp_token *source = NULL;
287 /* Loop, reading in the argument's tokens. */
288 for (i = 0; i < arg->count; i++)
290 const cpp_token *token = arg->first[i];
292 if (token->type == CPP_PADDING)
295 source = token->val.source;
299 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
300 || token->type == CPP_CHAR || token->type == CPP_WCHAR);
302 /* Room for each char being written in octal, initial space and
304 len = cpp_token_len (token);
309 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
311 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
312 _cpp_extend_buff (pfile, &pfile->u_buff, len);
313 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
316 /* Leading white space? */
317 if (dest != BUFF_FRONT (pfile->u_buff))
321 if (source->flags & PREV_WHITE)
328 _cpp_buff *buff = _cpp_get_buff (pfile, len);
329 unsigned char *buf = BUFF_FRONT (buff);
330 len = cpp_spell_token (pfile, token, buf) - buf;
331 dest = cpp_quote_string (dest, buf, len);
332 _cpp_release_buff (pfile, buff);
335 dest = cpp_spell_token (pfile, token, dest);
337 if (token->type == CPP_OTHER && token->val.c == '\\')
343 /* Ignore the final \ of invalid string literals. */
344 if (backslash_count & 1)
346 cpp_warning (pfile, "invalid string literal, ignoring final '\\'");
350 /* Commit the memory, including NUL, and return the token. */
351 len = dest - BUFF_FRONT (pfile->u_buff);
352 BUFF_FRONT (pfile->u_buff) = dest + 1;
353 return new_string_token (pfile, dest - len, len);
356 /* Try to paste two tokens. On success, return non-zero. In any
357 case, PLHS is updated to point to the pasted token, which is
358 guaranteed to not have the PASTE_LEFT flag set. */
360 paste_tokens (pfile, plhs, rhs)
362 const cpp_token **plhs, *rhs;
364 unsigned char *buf, *end;
365 const cpp_token *lhs;
370 len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
371 buf = (unsigned char *) alloca (len);
372 end = cpp_spell_token (pfile, lhs, buf);
374 /* Avoid comment headers, since they are still processed in stage 3.
375 It is simpler to insert a space here, rather than modifying the
376 lexer to ignore comments in some circumstances. Simply returning
377 false doesn't work, since we want to clear the PASTE_LEFT flag. */
378 if (lhs->type == CPP_DIV
379 && (rhs->type == CPP_MULT || rhs->type == CPP_DIV))
381 end = cpp_spell_token (pfile, rhs, end);
384 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true, 1);
386 /* Tweak the column number the lexer will report. */
387 pfile->buffer->col_adjust = pfile->cur_token[-1].col - 1;
389 /* We don't want a leading # to be interpreted as a directive. */
390 pfile->buffer->saved_flags = 0;
392 /* Set pfile->cur_token as required by _cpp_lex_direct. */
393 pfile->cur_token = _cpp_temp_token (pfile);
394 *plhs = _cpp_lex_direct (pfile);
395 valid = pfile->buffer->cur == pfile->buffer->rlimit;
396 _cpp_pop_buffer (pfile);
401 /* Handles an arbitrarily long sequence of ## operators, with initial
402 operand LHS. This implementation is left-associative,
403 non-recursive, and finishes a paste before handling succeeding
404 ones. If a paste fails, we back up to the RHS of the failing ##
405 operator before pushing the context containing the result of prior
406 successful pastes, with the effect that the RHS appears in the
407 output stream after the pasted LHS normally. */
409 paste_all_tokens (pfile, lhs)
411 const cpp_token *lhs;
413 const cpp_token *rhs;
414 cpp_context *context = pfile->context;
418 /* Take the token directly from the current context. We can do
419 this, because we are in the replacement list of either an
420 object-like macro, or a function-like macro with arguments
421 inserted. In either case, the constraints to #define
422 guarantee we have at least one more token. */
423 if (context->direct_p)
424 rhs = context->first.token++;
426 rhs = *context->first.ptoken++;
428 if (rhs->type == CPP_PADDING)
431 if (!paste_tokens (pfile, &lhs, rhs))
433 _cpp_backup_tokens (pfile, 1);
435 /* Mandatory warning for all apart from assembler. */
436 if (CPP_OPTION (pfile, lang) != CLK_ASM)
438 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
439 cpp_token_as_text (pfile, lhs),
440 cpp_token_as_text (pfile, rhs));
444 while (rhs->flags & PASTE_LEFT);
446 /* Put the resulting token in its own context. */
447 push_token_context (pfile, NULL, lhs, 1);
450 /* Reads and returns the arguments to a function-like macro
451 invocation. Assumes the opening parenthesis has been processed.
452 If there is an error, emits an appropriate diagnostic and returns
453 NULL. Each argument is terminated by a CPP_EOF token, for the
454 future benefit of expand_arg(). */
456 collect_args (pfile, node)
458 const cpp_hashnode *node;
460 _cpp_buff *buff, *base_buff;
462 macro_arg *args, *arg;
463 const cpp_token *token;
467 macro = node->value.macro;
469 argc = macro->paramc;
472 buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
473 + sizeof (macro_arg)));
475 args = (macro_arg *) buff->base;
476 memset (args, 0, argc * sizeof (macro_arg));
477 buff->cur = (unsigned char *) &args[argc];
478 arg = args, argc = 0;
480 /* Collect the tokens making up each argument. We don't yet know
481 how many arguments have been supplied, whether too many or too
482 few. Hence the slightly bizarre usage of "argc" and "arg". */
485 unsigned int paren_depth = 0;
486 unsigned int ntokens = 0;
489 arg->first = (const cpp_token **) buff->cur;
493 /* Require space for 2 new tokens (including a CPP_EOF). */
494 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
496 buff = _cpp_append_extend_buff (pfile, buff,
497 1000 * sizeof (cpp_token *));
498 arg->first = (const cpp_token **) buff->cur;
501 token = cpp_get_token (pfile);
503 if (token->type == CPP_PADDING)
505 /* Drop leading padding. */
509 else if (token->type == CPP_OPEN_PAREN)
511 else if (token->type == CPP_CLOSE_PAREN)
513 if (paren_depth-- == 0)
516 else if (token->type == CPP_COMMA)
518 /* A comma does not terminate an argument within
519 parentheses or as part of a variable argument. */
521 && ! (macro->variadic && argc == macro->paramc))
524 else if (token->type == CPP_EOF
525 || (token->type == CPP_HASH && token->flags & BOL))
528 arg->first[ntokens++] = token;
531 /* Drop trailing padding. */
532 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
535 arg->count = ntokens;
536 arg->first[ntokens] = &pfile->eof;
538 /* Terminate the argument. Excess arguments loop back and
539 overwrite the final legitimate argument, before failing. */
540 if (argc <= macro->paramc)
542 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
543 if (argc != macro->paramc)
547 while (token->type != CPP_CLOSE_PAREN
548 && token->type != CPP_EOF
549 && token->type != CPP_HASH);
551 if (token->type == CPP_EOF || token->type == CPP_HASH)
553 bool step_back = false;
555 /* 6.10.3 paragraph 11: If there are sequences of preprocessing
556 tokens within the list of arguments that would otherwise act
557 as preprocessing directives, the behavior is undefined.
559 This implementation will report a hard error, terminate the
560 macro invocation, and proceed to process the directive. */
561 if (token->type == CPP_HASH)
564 "directives may not be used inside a macro argument");
568 step_back = (pfile->context->prev || pfile->state.in_directive);
570 /* We still need the CPP_EOF to end directives, and to end
571 pre-expansion of a macro argument. Step back is not
572 unconditional, since we don't want to return a CPP_EOF to our
573 callers at the end of an -include-d file. */
575 _cpp_backup_tokens (pfile, 1);
576 cpp_error (pfile, "unterminated argument list invoking macro \"%s\"",
580 else if (argc < macro->paramc)
582 /* As an extension, a rest argument is allowed to not appear in
583 the invocation at all.
584 e.g. #define debug(format, args...) something
587 This is exactly the same as if there had been an empty rest
588 argument - debug("string", ). */
590 if (argc + 1 == macro->paramc && macro->variadic)
592 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
593 cpp_pedwarn (pfile, "ISO C99 requires rest arguments to be used");
598 "macro \"%s\" requires %u arguments, but only %u given",
599 NODE_NAME (node), macro->paramc, argc);
603 else if (argc > macro->paramc)
605 /* Empty argument to a macro taking no arguments is OK. */
606 if (argc != 1 || arg->count)
609 "macro \"%s\" passed %u arguments, but takes just %u",
610 NODE_NAME (node), argc, macro->paramc);
617 /* GCC has special semantics for , ## b where b is a varargs
618 parameter: we remove the comma if b was omitted entirely.
619 If b was merely an empty argument, the comma is retained.
620 If the macro takes just one (varargs) parameter, then we
621 retain the comma only if we are standards conforming.
623 If FIRST is NULL replace_args () swallows the comma. */
624 if (macro->variadic && (argc < macro->paramc
625 || (argc == 1 && args[0].count == 0
626 && !CPP_OPTION (pfile, std))))
627 args[macro->paramc - 1].first = NULL;
631 _cpp_release_buff (pfile, base_buff);
635 /* Search for an opening parenthesis to the macro of NODE, in such a
636 way that, if none is found, we don't lose the information in any
637 intervening padding tokens. If we find the parenthesis, collect
638 the arguments and return the buffer containing them. */
640 funlike_invocation_p (pfile, node)
644 const cpp_token *token, *padding = NULL;
648 token = cpp_get_token (pfile);
649 if (token->type != CPP_PADDING)
652 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
656 if (token->type == CPP_OPEN_PAREN)
658 pfile->state.parsing_args = 2;
659 return collect_args (pfile, node);
662 /* CPP_EOF can be the end of macro arguments, or the end of the
663 file. We mustn't back up over the latter. Ugh. */
664 if (token->type != CPP_EOF || token == &pfile->eof)
666 /* Back up. We may have skipped padding, in which case backing
667 up more than one token when expanding macros is in general
668 too difficult. We re-insert it in its own context. */
669 _cpp_backup_tokens (pfile, 1);
671 push_token_context (pfile, NULL, padding, 1);
677 /* Push the context of a macro with hash entry NODE onto the context
678 stack. If we can successfully expand the macro, we push a context
679 containing its yet-to-be-rescanned replacement list and return one.
680 Otherwise, we don't push a context and return zero. */
682 enter_macro_context (pfile, node)
686 /* The presence of a macro invalidates a file's controlling macro. */
687 pfile->mi_valid = false;
689 pfile->state.angled_headers = false;
691 /* Handle standard macros. */
692 if (! (node->flags & NODE_BUILTIN))
694 cpp_macro *macro = node->value.macro;
700 pfile->state.prevent_expansion++;
701 pfile->keep_tokens++;
702 pfile->state.parsing_args = 1;
703 buff = funlike_invocation_p (pfile, node);
704 pfile->state.parsing_args = 0;
705 pfile->keep_tokens--;
706 pfile->state.prevent_expansion--;
710 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
712 "function-like macro \"%s\" must be used with arguments in traditional C",
718 if (node->value.macro->paramc > 0)
719 replace_args (pfile, node, (macro_arg *) buff->base);
720 _cpp_release_buff (pfile, buff);
723 /* Disable the macro within its expansion. */
724 node->flags |= NODE_DISABLED;
726 if (macro->paramc == 0)
727 push_token_context (pfile, node, macro->expansion, macro->count);
732 /* Handle built-in macros and the _Pragma operator. */
733 return builtin_macro (pfile, node);
736 /* Replace the parameters in a function-like macro of NODE with the
737 actual ARGS, and place the result in a newly pushed token context.
738 Expand each argument before replacing, unless it is operated upon
739 by the # or ## operators. */
741 replace_args (pfile, node, args)
746 unsigned int i, total;
747 const cpp_token *src, *limit;
748 const cpp_token **dest, **first;
753 /* First, fully macro-expand arguments, calculating the number of
754 tokens in the final expansion as we go. The ordering of the if
755 statements below is subtle; we must handle stringification before
757 macro = node->value.macro;
758 total = macro->count;
759 limit = macro->expansion + macro->count;
761 for (src = macro->expansion; src < limit; src++)
762 if (src->type == CPP_MACRO_ARG)
764 /* Leading and trailing padding tokens. */
767 /* We have an argument. If it is not being stringified or
768 pasted it is macro-replaced before insertion. */
769 arg = &args[src->val.arg_no - 1];
771 if (src->flags & STRINGIFY_ARG)
773 if (!arg->stringified)
774 arg->stringified = stringify_arg (pfile, arg);
776 else if ((src->flags & PASTE_LEFT)
777 || (src > macro->expansion && (src[-1].flags & PASTE_LEFT)))
778 total += arg->count - 1;
782 expand_arg (pfile, arg);
783 total += arg->expanded_count - 1;
787 /* Now allocate space for the expansion, copy the tokens and replace
789 buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
790 first = (const cpp_token **) buff->base;
793 for (src = macro->expansion; src < limit; src++)
796 const cpp_token **from, **paste_flag;
798 if (src->type != CPP_MACRO_ARG)
805 arg = &args[src->val.arg_no - 1];
806 if (src->flags & STRINGIFY_ARG)
807 count = 1, from = &arg->stringified;
808 else if (src->flags & PASTE_LEFT)
809 count = arg->count, from = arg->first;
810 else if (src != macro->expansion && (src[-1].flags & PASTE_LEFT))
812 count = arg->count, from = arg->first;
815 if (dest[-1]->type == CPP_COMMA
817 && src->val.arg_no == macro->paramc)
819 /* Swallow a pasted comma if from == NULL, otherwise
820 drop the paste flag. */
824 paste_flag = dest - 1;
826 /* Remove the paste flag if the RHS is a placemarker. */
828 paste_flag = dest - 1;
832 count = arg->expanded_count, from = arg->expanded;
834 /* Padding on the left of an argument (unless RHS of ##). */
835 if (!pfile->state.in_directive
836 && src != macro->expansion && !(src[-1].flags & PASTE_LEFT))
837 *dest++ = padding_token (pfile, src);
841 memcpy (dest, from, count * sizeof (cpp_token *));
844 /* With a non-empty argument on the LHS of ##, the last
845 token should be flagged PASTE_LEFT. */
846 if (src->flags & PASTE_LEFT)
847 paste_flag = dest - 1;
850 /* Avoid paste on RHS (even case count == 0). */
851 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
852 *dest++ = &pfile->avoid_paste;
854 /* Add a new paste flag, or remove an unwanted one. */
857 cpp_token *token = _cpp_temp_token (pfile);
858 token->type = (*paste_flag)->type;
859 token->val.str = (*paste_flag)->val.str;
860 if (src->flags & PASTE_LEFT)
861 token->flags = (*paste_flag)->flags | PASTE_LEFT;
863 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
868 /* Free the expanded arguments. */
869 for (i = 0; i < macro->paramc; i++)
870 if (args[i].expanded)
871 free (args[i].expanded);
873 push_ptoken_context (pfile, node, buff, first, dest - first);
876 /* Return a special padding token, with padding inherited from SOURCE. */
877 static const cpp_token *
878 padding_token (pfile, source)
880 const cpp_token *source;
882 cpp_token *result = _cpp_temp_token (pfile);
884 result->type = CPP_PADDING;
885 result->val.source = source;
890 /* Get a new uninitialized context. Create a new one if we cannot
891 re-use an old one. */
896 cpp_context *result = pfile->context->next;
900 result = xnew (cpp_context);
901 result->prev = pfile->context;
903 pfile->context->next = result;
906 pfile->context = result;
910 /* Push a list of pointers to tokens. */
912 push_ptoken_context (pfile, macro, buff, first, count)
916 const cpp_token **first;
919 cpp_context *context = next_context (pfile);
921 context->direct_p = false;
922 context->macro = macro;
923 context->buff = buff;
924 context->first.ptoken = first;
925 context->last.ptoken = first + count;
928 /* Push a list of tokens. */
930 push_token_context (pfile, macro, first, count)
933 const cpp_token *first;
936 cpp_context *context = next_context (pfile);
938 context->direct_p = true;
939 context->macro = macro;
940 context->buff = NULL;
941 context->first.token = first;
942 context->last.token = first + count;
945 /* Expand an argument ARG before replacing parameters in a
946 function-like macro. This works by pushing a context with the
947 argument's tokens, and then expanding that into a temporary buffer
948 as if it were a normal part of the token stream. collect_args()
949 has terminated the argument's tokens with a CPP_EOF so that we know
950 when we have fully expanded the argument. */
952 expand_arg (pfile, arg)
956 unsigned int capacity;
961 /* Loop, reading in the arguments. */
963 arg->expanded = (const cpp_token **)
964 xmalloc (capacity * sizeof (cpp_token *));
966 push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
969 const cpp_token *token;
971 if (arg->expanded_count + 1 >= capacity)
974 arg->expanded = (const cpp_token **)
975 xrealloc (arg->expanded, capacity * sizeof (cpp_token *));
978 token = cpp_get_token (pfile);
980 if (token->type == CPP_EOF)
983 arg->expanded[arg->expanded_count++] = token;
986 _cpp_pop_context (pfile);
989 /* Pop the current context off the stack, re-enabling the macro if the
990 context represented a macro's replacement list. The context
991 structure is not freed so that we can re-use it later. */
993 _cpp_pop_context (pfile)
996 cpp_context *context = pfile->context;
999 context->macro->flags &= ~NODE_DISABLED;
1002 _cpp_release_buff (pfile, context->buff);
1004 pfile->context = context->prev;
1007 /* Eternal routine to get a token. Also used nearly everywhere
1008 internally, except for places where we know we can safely call
1009 the lexer directly, such as lexing a directive name.
1011 Macro expansions and directives are transparently handled,
1012 including entering included files. Thus tokens are post-macro
1013 expansion, and after any intervening directives. External callers
1014 see CPP_EOF only at EOF. Internal callers also see it when meeting
1015 a directive inside a macro call, when at the end of a directive and
1016 state.in_directive is still 1, and at the end of argument
1019 cpp_get_token (pfile)
1022 const cpp_token *result;
1027 cpp_context *context = pfile->context;
1029 /* Context->prev == 0 <=> base context. */
1031 result = _cpp_lex_token (pfile);
1032 else if (context->first.token != context->last.token)
1034 if (context->direct_p)
1035 result = context->first.token++;
1037 result = *context->first.ptoken++;
1039 if (result->flags & PASTE_LEFT)
1041 paste_all_tokens (pfile, result);
1042 if (pfile->state.in_directive)
1044 return padding_token (pfile, result);
1049 _cpp_pop_context (pfile);
1050 if (pfile->state.in_directive)
1052 return &pfile->avoid_paste;
1055 if (result->type != CPP_NAME)
1058 node = result->val.node;
1060 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
1063 if (!(node->flags & NODE_DISABLED))
1065 if (!pfile->state.prevent_expansion
1066 && enter_macro_context (pfile, node))
1068 if (pfile->state.in_directive)
1070 return padding_token (pfile, result);
1075 /* Flag this token as always unexpandable. FIXME: move this
1076 to collect_args()?. */
1077 cpp_token *t = _cpp_temp_token (pfile);
1078 t->type = result->type;
1079 t->flags = result->flags | NO_EXPAND;
1080 t->val.str = result->val.str;
1090 /* Returns true if we're expanding an object-like macro that was
1091 defined in a system header. Just checks the macro at the top of
1092 the stack. Used for diagnostic suppression. */
1094 cpp_sys_macro_p (pfile)
1097 cpp_hashnode *node = pfile->context->macro;
1099 return node && node->value.macro && node->value.macro->syshdr;
1102 /* Read each token in, until EOF. Directives are transparently
1105 cpp_scan_nooutput (pfile)
1108 while (cpp_get_token (pfile)->type != CPP_EOF)
1112 /* Step back one (or more) tokens. Can only step mack more than 1 if
1113 they are from the lexer, and not from macro expansion. */
1115 _cpp_backup_tokens (pfile, count)
1119 if (pfile->context->prev == NULL)
1121 pfile->lookaheads += count;
1125 if (pfile->cur_token == pfile->cur_run->base
1126 /* Possible with -fpreprocessed and no leading #line. */
1127 && pfile->cur_run->prev != NULL)
1129 pfile->cur_run = pfile->cur_run->prev;
1130 pfile->cur_token = pfile->cur_run->limit;
1138 if (pfile->context->direct_p)
1139 pfile->context->first.token--;
1141 pfile->context->first.ptoken--;
1145 /* #define directive parsing and handling. */
1147 /* Returns non-zero if a macro redefinition warning is required. */
1149 warn_of_redefinition (node, macro2)
1150 const cpp_hashnode *node;
1151 const cpp_macro *macro2;
1153 const cpp_macro *macro1;
1156 /* Some redefinitions need to be warned about regardless. */
1157 if (node->flags & NODE_WARN)
1160 /* Redefinition of a macro is allowed if and only if the old and new
1161 definitions are the same. (6.10.3 paragraph 2). */
1162 macro1 = node->value.macro;
1164 /* The quick failures. */
1165 if (macro1->count != macro2->count
1166 || macro1->paramc != macro2->paramc
1167 || macro1->fun_like != macro2->fun_like
1168 || macro1->variadic != macro2->variadic)
1171 /* Check each token. */
1172 for (i = 0; i < macro1->count; i++)
1173 if (! _cpp_equiv_tokens (¯o1->expansion[i], ¯o2->expansion[i]))
1176 /* Check parameter spellings. */
1177 for (i = 0; i < macro1->paramc; i++)
1178 if (macro1->params[i] != macro2->params[i])
1184 /* Free the definition of hashnode H. */
1186 _cpp_free_definition (h)
1189 /* Macros and assertions no longer have anything to free. */
1191 /* Clear builtin flag in case of redefinition. */
1192 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED);
1195 /* Save parameter NODE to the parameter list of macro MACRO. Returns
1196 zero on success, non-zero if the parameter is a duplicate. */
1198 save_parameter (pfile, macro, node)
1203 /* Constraint 6.10.3.6 - duplicate parameter names. */
1204 if (node->arg_index)
1206 cpp_error (pfile, "duplicate macro parameter \"%s\"", NODE_NAME (node));
1210 if (BUFF_ROOM (pfile->a_buff)
1211 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1212 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
1214 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
1215 node->arg_index = macro->paramc;
1219 /* Check the syntax of the parameters in a MACRO definition. */
1221 parse_params (pfile, macro)
1225 unsigned int prev_ident = 0;
1229 const cpp_token *token = _cpp_lex_token (pfile);
1231 switch (token->type)
1234 cpp_error (pfile, "\"%s\" may not appear in macro parameter list",
1235 cpp_token_as_text (pfile, token));
1241 cpp_error (pfile, "macro parameters must be comma-separated");
1246 if (save_parameter (pfile, macro, token->val.node))
1250 case CPP_CLOSE_PAREN:
1251 if (prev_ident || macro->paramc == 0)
1254 /* Fall through to pick up the error. */
1258 cpp_error (pfile, "parameter name missing");
1265 macro->variadic = 1;
1268 save_parameter (pfile, macro, pfile->spec_nodes.n__VA_ARGS__);
1269 pfile->state.va_args_ok = 1;
1270 if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
1272 "anonymous variadic macros were introduced in C99");
1274 else if (CPP_OPTION (pfile, pedantic))
1275 cpp_pedwarn (pfile, "ISO C does not permit named variadic macros");
1277 /* We're at the end, and just expect a closing parenthesis. */
1278 token = _cpp_lex_token (pfile);
1279 if (token->type == CPP_CLOSE_PAREN)
1284 cpp_error (pfile, "missing ')' in macro parameter list");
1290 /* Allocate room for a token from a macro's replacement list. */
1292 alloc_expansion_token (pfile, macro)
1296 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1297 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
1299 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
1302 /* Lex a token from the expansion of MACRO, but mark parameters as we
1303 find them and warn of traditional stringification. */
1305 lex_expansion_token (pfile, macro)
1311 pfile->cur_token = alloc_expansion_token (pfile, macro);
1312 token = _cpp_lex_direct (pfile);
1314 /* Is this a parameter? */
1315 if (token->type == CPP_NAME && token->val.node->arg_index)
1317 token->type = CPP_MACRO_ARG;
1318 token->val.arg_no = token->val.node->arg_index;
1320 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1321 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1322 check_trad_stringification (pfile, macro, &token->val.str);
1327 /* Parse a macro and save its expansion. Returns non-zero on success. */
1329 _cpp_create_definition (pfile, node)
1334 cpp_token *token, *saved_cur_token;
1335 const cpp_token *ctoken;
1336 unsigned int i, ok = 1;
1338 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
1339 macro->line = pfile->directive_line;
1342 macro->variadic = 0;
1344 macro->fun_like = 0;
1346 /* Get the first token of the expansion (or the '(' of a
1347 function-like macro). */
1348 ctoken = _cpp_lex_token (pfile);
1350 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
1352 ok = parse_params (pfile, macro);
1353 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1357 /* Success. Commit the parameter array. */
1358 BUFF_FRONT (pfile->a_buff) = (U_CHAR *) ¯o->params[macro->paramc];
1359 macro->fun_like = 1;
1361 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
1362 cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name");
1364 saved_cur_token = pfile->cur_token;
1366 if (macro->fun_like)
1367 token = lex_expansion_token (pfile, macro);
1370 token = alloc_expansion_token (pfile, macro);
1376 /* Check the stringifying # constraint 6.10.3.2.1 of
1377 function-like macros when lexing the subsequent token. */
1378 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1380 if (token->type == CPP_MACRO_ARG)
1382 token->flags &= ~PREV_WHITE;
1383 token->flags |= STRINGIFY_ARG;
1384 token->flags |= token[-1].flags & PREV_WHITE;
1385 token[-1] = token[0];
1388 /* Let assembler get away with murder. */
1389 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
1392 cpp_error (pfile, "'#' is not followed by a macro parameter");
1397 if (token->type == CPP_EOF)
1400 /* Paste operator constraint 6.10.3.3.1. */
1401 if (token->type == CPP_PASTE)
1403 /* Token-paste ##, can appear in both object-like and
1404 function-like macros, but not at the ends. */
1405 if (--macro->count > 0)
1406 token = lex_expansion_token (pfile, macro);
1408 if (macro->count == 0 || token->type == CPP_EOF)
1412 "'##' cannot appear at either end of a macro expansion");
1416 token[-1].flags |= PASTE_LEFT;
1419 token = lex_expansion_token (pfile, macro);
1422 macro->expansion = (cpp_token *) BUFF_FRONT (pfile->a_buff);
1424 /* Don't count the CPP_EOF. */
1427 /* Clear whitespace on first token for warn_of_redefinition(). */
1429 macro->expansion[0].flags &= ~PREV_WHITE;
1431 /* Commit the memory. */
1432 BUFF_FRONT (pfile->a_buff) = (U_CHAR *) ¯o->expansion[macro->count];
1434 /* Implement the macro-defined-to-itself optimisation. */
1435 if (macro->count == 1 && !macro->fun_like
1436 && macro->expansion[0].type == CPP_NAME
1437 && macro->expansion[0].val.node == node)
1438 node->flags |= NODE_DISABLED;
1440 /* To suppress some diagnostics. */
1441 macro->syshdr = pfile->map->sysp != 0;
1443 if (node->type != NT_VOID)
1445 if (warn_of_redefinition (node, macro))
1447 cpp_pedwarn_with_line (pfile, pfile->directive_line, 0,
1448 "\"%s\" redefined", NODE_NAME (node));
1450 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1451 cpp_pedwarn_with_line (pfile, node->value.macro->line, 0,
1452 "this is the location of the previous definition");
1454 _cpp_free_definition (node);
1457 /* Enter definition in hash table. */
1458 node->type = NT_MACRO;
1459 node->value.macro = macro;
1460 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
1461 node->flags |= NODE_WARN;
1465 /* Set type for SEEN_EOL() in cpplib.c, restore the lexer position. */
1466 saved_cur_token[-1].type = pfile->cur_token[-1].type;
1467 pfile->cur_token = saved_cur_token;
1471 /* Stop the lexer accepting __VA_ARGS__. */
1472 pfile->state.va_args_ok = 0;
1474 /* Clear the fast argument lookup indices. */
1475 for (i = macro->paramc; i-- > 0; )
1476 macro->params[i]->arg_index = 0;
1481 /* Warn if a token in STRING matches one of a function-like MACRO's
1484 check_trad_stringification (pfile, macro, string)
1486 const cpp_macro *macro;
1487 const cpp_string *string;
1489 unsigned int i, len;
1490 const U_CHAR *p, *q, *limit = string->text + string->len;
1492 /* Loop over the string. */
1493 for (p = string->text; p < limit; p = q)
1495 /* Find the start of an identifier. */
1496 while (p < limit && !is_idstart (*p))
1499 /* Find the end of the identifier. */
1501 while (q < limit && is_idchar (*q))
1506 /* Loop over the function macro arguments to see if the
1507 identifier inside the string matches one of them. */
1508 for (i = 0; i < macro->paramc; i++)
1510 const cpp_hashnode *node = macro->params[i];
1512 if (NODE_LEN (node) == len
1513 && !memcmp (p, NODE_NAME (node), len))
1516 "macro argument \"%s\" would be stringified with -traditional",
1524 /* Returns the name, arguments and expansion of a macro, in a format
1525 suitable to be read back in again, and therefore also for DWARF 2
1526 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1527 Caller is expected to generate the "#define" bit if needed. The
1528 returned text is temporary, and automatically freed later. */
1529 const unsigned char *
1530 cpp_macro_definition (pfile, node)
1532 const cpp_hashnode *node;
1534 unsigned int i, len;
1535 const cpp_macro *macro = node->value.macro;
1536 unsigned char *buffer;
1538 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1540 cpp_ice (pfile, "invalid hash type %d in cpp_macro_definition", node->type);
1544 /* Calculate length. */
1545 len = NODE_LEN (node) + 2; /* ' ' and NUL. */
1546 if (macro->fun_like)
1548 len += 4; /* "()" plus possible final ".." of named
1549 varargs (we have + 1 below). */
1550 for (i = 0; i < macro->paramc; i++)
1551 len += NODE_LEN (macro->params[i]) + 1; /* "," */
1554 for (i = 0; i < macro->count; i++)
1556 cpp_token *token = ¯o->expansion[i];
1558 if (token->type == CPP_MACRO_ARG)
1559 len += NODE_LEN (macro->params[token->val.arg_no - 1]);
1561 len += cpp_token_len (token); /* Includes room for ' '. */
1562 if (token->flags & STRINGIFY_ARG)
1564 if (token->flags & PASTE_LEFT)
1565 len += 3; /* " ##" */
1568 if (len > pfile->macro_buffer_len)
1570 pfile->macro_buffer = (U_CHAR *) xrealloc (pfile->macro_buffer, len);
1571 pfile->macro_buffer_len = len;
1574 /* Fill in the buffer. Start with the macro name. */
1575 buffer = pfile->macro_buffer;
1576 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1577 buffer += NODE_LEN (node);
1579 /* Parameter names. */
1580 if (macro->fun_like)
1583 for (i = 0; i < macro->paramc; i++)
1585 cpp_hashnode *param = macro->params[i];
1587 if (param != pfile->spec_nodes.n__VA_ARGS__)
1589 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1590 buffer += NODE_LEN (param);
1593 if (i + 1 < macro->paramc)
1594 /* Don't emit a space after the comma here; we're trying
1595 to emit a Dwarf-friendly definition, and the Dwarf spec
1596 forbids spaces in the argument list. */
1598 else if (macro->variadic)
1599 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1604 /* The Dwarf spec requires a space after the macro name, even if the
1605 definition is the empty string. */
1608 /* Expansion tokens. */
1611 for (i = 0; i < macro->count; i++)
1613 cpp_token *token = ¯o->expansion[i];
1615 if (token->flags & PREV_WHITE)
1617 if (token->flags & STRINGIFY_ARG)
1620 if (token->type == CPP_MACRO_ARG)
1622 len = NODE_LEN (macro->params[token->val.arg_no - 1]);
1624 NODE_NAME (macro->params[token->val.arg_no - 1]), len);
1628 buffer = cpp_spell_token (pfile, token, buffer);
1630 if (token->flags & PASTE_LEFT)
1635 /* Next has PREV_WHITE; see _cpp_create_definition. */
1641 return pfile->macro_buffer;