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 void lock_pools PARAMS ((cpp_reader *));
58 static void unlock_pools PARAMS ((cpp_reader *));
59 static int enter_macro_context PARAMS ((cpp_reader *, cpp_hashnode *));
60 static const cpp_token *builtin_macro PARAMS ((cpp_reader *, cpp_hashnode *));
61 static void push_token_context
62 PARAMS ((cpp_reader *, cpp_macro *, const cpp_token *, unsigned int));
63 static void push_ptoken_context
64 PARAMS ((cpp_reader *, cpp_macro *, const cpp_token **, unsigned int));
65 static enum cpp_ttype parse_arg PARAMS ((cpp_reader *, macro_arg *, int));
66 static macro_arg *parse_args PARAMS ((cpp_reader *, const cpp_hashnode *));
67 static cpp_context *next_context PARAMS ((cpp_reader *));
68 static const cpp_token *padding_token
69 PARAMS ((cpp_reader *, const cpp_token *));
70 static void expand_arg PARAMS ((cpp_reader *, macro_arg *));
71 static unsigned char *quote_string PARAMS ((unsigned char *,
72 const unsigned char *,
74 static const cpp_token *new_string_token PARAMS ((cpp_reader *, U_CHAR *,
76 static const cpp_token *new_number_token PARAMS ((cpp_reader *, int));
77 static const cpp_token *stringify_arg PARAMS ((cpp_reader *, macro_arg *));
78 static void paste_all_tokens PARAMS ((cpp_reader *, const cpp_token *));
79 static int paste_tokens PARAMS ((cpp_reader *, cpp_token *,
81 static int funlike_invocation_p PARAMS ((cpp_reader *, const cpp_hashnode *));
82 static void replace_args PARAMS ((cpp_reader *, cpp_macro *, macro_arg *));
84 /* #define directive parsing and handling. */
86 static cpp_token *alloc_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
87 static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
88 static int warn_of_redefinition PARAMS ((cpp_reader *, const cpp_hashnode *,
90 static int save_parameter PARAMS ((cpp_reader *, cpp_macro *, cpp_hashnode *));
91 static int parse_params PARAMS ((cpp_reader *, cpp_macro *));
92 static void check_trad_stringification PARAMS ((cpp_reader *,
96 /* Allocates and returns a CPP_STRING token, containing TEXT of length
97 LEN, after null-terminating it. TEXT must be in permanent storage. */
98 static const cpp_token *
99 new_string_token (pfile, text, len)
104 cpp_token *token = _cpp_temp_token (pfile);
107 token->type = CPP_STRING;
108 token->val.str.len = len;
109 token->val.str.text = text;
114 /* Allocates and returns a CPP_NUMBER token evaluating to NUMBER. */
115 static const cpp_token *
116 new_number_token (pfile, number)
120 cpp_token *token = _cpp_temp_token (pfile);
121 unsigned char *buf = _cpp_pool_alloc (&pfile->ident_pool, 20);
123 sprintf ((char *) buf, "%d", number);
124 token->type = CPP_NUMBER;
125 token->val.str.text = buf;
126 token->val.str.len = ustrlen (buf);
131 static const char * const monthnames[] =
133 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
134 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
137 /* Handle builtin macros like __FILE__. */
138 static const cpp_token *
139 builtin_macro (pfile, node)
143 switch (node->value.builtin)
146 cpp_ice (pfile, "invalid builtin macro \"%s\"", NODE_NAME (node));
147 return new_number_token (pfile, 1);
155 const struct line_map *map = pfile->map;
157 if (node->value.builtin == BT_BASE_FILE)
158 while (! MAIN_FILE_P (map))
159 map = INCLUDED_FROM (&pfile->line_maps, map);
163 buf = _cpp_pool_alloc (&pfile->ident_pool, len * 4 + 1);
164 len = quote_string (buf, (const unsigned char *) name, len) - buf;
166 return 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 return new_number_token (pfile, pfile->line_maps.depth - 1);
176 /* If __LINE__ is embedded in a macro, it must expand to the
177 line of the macro's invocation, not its definition.
178 Otherwise things like assert() will not work properly. */
179 return new_number_token (pfile, SOURCE_LINE (pfile->map,
180 pfile->cur_token[-1].line));
184 int stdc = (!CPP_IN_SYSTEM_HEADER (pfile)
185 || pfile->spec_nodes.n__STRICT_ANSI__->type != NT_VOID);
186 return new_number_token (pfile, stdc);
191 if (pfile->date.type == CPP_EOF)
193 /* Allocate __DATE__ and __TIME__ strings from permanent
194 storage. We only do this once, and don't generate them
195 at init time, because time() and localtime() are very
196 slow on some systems. */
197 time_t tt = time (NULL);
198 struct tm *tb = localtime (&tt);
200 pfile->date.val.str.text =
201 _cpp_pool_alloc (&pfile->ident_pool, sizeof ("Oct 11 1347"));
202 pfile->date.val.str.len = sizeof ("Oct 11 1347") - 1;
203 pfile->date.type = CPP_STRING;
204 pfile->date.flags = 0;
205 sprintf ((char *) pfile->date.val.str.text, "%s %2d %4d",
206 monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
208 pfile->time.val.str.text =
209 _cpp_pool_alloc (&pfile->ident_pool, sizeof ("12:34:56"));
210 pfile->time.val.str.len = sizeof ("12:34:56") - 1;
211 pfile->time.type = CPP_STRING;
212 pfile->time.flags = 0;
213 sprintf ((char *) pfile->time.val.str.text, "%02d:%02d:%02d",
214 tb->tm_hour, tb->tm_min, tb->tm_sec);
217 return node->value.builtin == BT_DATE ? &pfile->date: &pfile->time;
225 _cpp_lock_pool (&pfile->argument_pool);
232 _cpp_unlock_pool (&pfile->argument_pool);
235 /* Adds backslashes before all backslashes and double quotes appearing
236 in strings. Non-printable characters are converted to octal. */
238 quote_string (dest, src, len)
247 if (c == '\\' || c == '"')
258 sprintf ((char *) dest, "\\%03o", c);
267 /* Convert a token sequence to a single string token according to the
268 rules of the ISO C #-operator. */
269 static const cpp_token *
270 stringify_arg (pfile, arg)
274 cpp_pool *pool = &pfile->ident_pool;
275 unsigned char *start = POOL_FRONT (pool);
276 unsigned int i, escape_it, total_len = 0, backslash_count = 0;
277 const cpp_token *source = NULL;
279 /* Loop, reading in the argument's tokens. */
280 for (i = 0; i < arg->count; i++)
283 const cpp_token *token = arg->first[i];
286 if (token->type == CPP_PADDING)
289 source = token->val.source;
293 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
294 || token->type == CPP_CHAR || token->type == CPP_WCHAR);
296 len = cpp_token_len (token);
298 /* Worst case is each char is octal. */
300 len += 2; /* Room for initial space and final NUL. */
302 dest = &start[total_len];
303 if (dest + len > POOL_LIMIT (pool))
305 _cpp_next_chunk (pool, len, (unsigned char **) &start);
306 dest = &start[total_len];
309 /* Leading white space? */
314 if (source->flags & PREV_WHITE)
321 unsigned char *buf = (unsigned char *) xmalloc (len);
323 len = cpp_spell_token (pfile, token, buf) - buf;
324 dest = quote_string (dest, buf, len);
328 dest = cpp_spell_token (pfile, token, dest);
329 total_len = dest - start;
331 if (token->type == CPP_OTHER && token->val.c == '\\')
337 /* Ignore the final \ of invalid string literals. */
338 if (backslash_count & 1)
340 cpp_warning (pfile, "invalid string literal, ignoring final '\\'");
344 /* Commit the memory, including NUL, and return the token. */
345 POOL_COMMIT (pool, total_len + 1);
346 return new_string_token (pfile, start, total_len);
349 /* Try to paste two tokens. On success, the LHS becomes the pasted
350 token, and 0 is returned. For failure, we update the flags of the
351 RHS appropriately and return non-zero. */
353 paste_tokens (pfile, lhs, rhs)
356 const cpp_token *rhs;
358 unsigned char flags = 0;
362 type = cpp_can_paste (pfile, lhs, rhs, &digraph);
366 /* Mandatory warning for all apart from assembler. */
367 if (CPP_OPTION (pfile, lang) != CLK_ASM)
369 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
370 cpp_token_as_text (pfile, lhs),
371 cpp_token_as_text (pfile, rhs));
378 /* Identifiers and numbers need spellings to be pasted. */
379 if (type == CPP_NAME || type == CPP_NUMBER)
381 unsigned int total_len = cpp_token_len (lhs) + cpp_token_len (rhs);
382 unsigned char *result, *end;
384 result = _cpp_pool_alloc (&pfile->ident_pool, total_len + 1);
386 /* Paste the spellings and null terminate. */
387 end = cpp_spell_token (pfile, rhs, cpp_spell_token (pfile, lhs, result));
389 total_len = end - result;
391 if (type == CPP_NAME)
393 lhs->val.node = cpp_lookup (pfile, result, total_len);
394 if (lhs->val.node->flags & NODE_OPERATOR)
397 lhs->type = lhs->val.node->value.operator;
402 lhs->val.str.text = result;
403 lhs->val.str.len = total_len;
406 else if (type == CPP_WCHAR || type == CPP_WSTRING)
407 lhs->val.str = rhs->val.str;
409 /* Set type and flags after pasting spellings. */
416 /* Handles an arbitrarily long sequence of ## operators. This
417 implementation is left-associative, non-recursive, and finishes a
418 paste before handling succeeding ones. If the paste fails, we back
419 up a token to just after the ## operator, with the effect that it
420 appears in the output stream normally. */
422 paste_all_tokens (pfile, lhs)
424 const cpp_token *lhs;
427 const cpp_token *rhs;
428 cpp_context *context = pfile->context;
430 /* Copy lhs to pasted, but preserve original line and column. */
431 pasted = _cpp_temp_token (pfile);
432 pasted->type = lhs->type;
433 pasted->flags = lhs->flags;
434 pasted->val.str = lhs->val.str;
438 /* Take the token directly from the current context. We can do
439 this, because we are in the replacement list of either an
440 object-like macro, or a function-like macro with arguments
441 inserted. In either case, the constraints to #define
442 guarantee we have at least one more token. */
443 if (context->direct_p)
444 rhs = context->first.token++;
446 rhs = *context->first.ptoken++;
448 if (rhs->type == CPP_PADDING)
451 if (paste_tokens (pfile, pasted, rhs))
453 _cpp_backup_tokens (pfile, 1);
457 while (rhs->flags & PASTE_LEFT);
459 /* Clear PASTE_LEFT flag, put the token in its own context. */
460 pasted->flags &= ~PASTE_LEFT;
461 push_token_context (pfile, NULL, pasted, 1);
464 /* Reads the unexpanded tokens of a macro argument into ARG. VAR_ARGS
465 is non-zero if this is a variadic macro. Returns the type of the
466 token that caused reading to finish. */
467 static enum cpp_ttype
468 parse_arg (pfile, arg, variadic)
470 struct macro_arg *arg;
473 enum cpp_ttype result;
474 unsigned int paren = 0;
476 arg->first = (const cpp_token **) POOL_FRONT (&pfile->argument_pool);
477 for (;; arg->count++)
479 const cpp_token *token;
480 const cpp_token **ptoken = &arg->first[arg->count];
481 if ((unsigned char *) (ptoken + 2) >= POOL_LIMIT (&pfile->argument_pool))
483 _cpp_next_chunk (&pfile->argument_pool, 2 * sizeof (cpp_token *),
484 (unsigned char **) &arg->first);
485 ptoken = &arg->first[arg->count];
488 /* Drop leading padding. */
490 token = cpp_get_token (pfile);
491 while (arg->count == 0 && token->type == CPP_PADDING);
493 result = token->type;
495 if (result == CPP_OPEN_PAREN)
497 else if (result == CPP_CLOSE_PAREN && paren-- == 0)
499 /* Commas are not terminators within parantheses or variadic. */
500 else if (result == CPP_COMMA && paren == 0 && !variadic)
502 else if (result == CPP_EOF)
504 /* We still need the EOF (added below) to end pre-expansion
506 if (pfile->context->prev || pfile->state.in_directive)
507 _cpp_backup_tokens (pfile, 1);
508 /* Error reported by caller. */
511 else if (result == CPP_HASH && token->flags & BOL)
513 /* 6.10.3 paragraph 11: If there are sequences of
514 preprocessing tokens within the list of arguments that
515 would otherwise act as preprocessing directives, the
516 behavior is undefined.
518 This implementation will report a hard error, terminate
519 the macro invocation, and proceed to process the
522 "directives may not be used inside a macro argument");
523 _cpp_backup_tokens (pfile, 1);
529 /* Drop trailing padding. */
530 while (arg->count > 0 && arg->first[arg->count - 1]->type == CPP_PADDING)
533 /* Commit the memory used to store the arguments. We make the last
534 argument a CPP_EOF, so that it terminates macro pre-expansion,
535 but it is not included in arg->count. */
536 arg->first[arg->count] = &pfile->eof;
537 POOL_COMMIT (&pfile->argument_pool, (arg->count + 1) * sizeof (cpp_token *));
541 /* Parse the arguments making up a macro invocation. */
543 parse_args (pfile, node)
545 const cpp_hashnode *node;
547 cpp_macro *macro = node->value.macro;
548 macro_arg *args, *cur;
552 /* Allocate room for at least one argument, and zero it out. */
553 argc = macro->paramc ? macro->paramc: 1;
554 args = xcnewvec (macro_arg, argc);
556 for (cur = args, argc = 0; ;)
560 type = parse_arg (pfile, cur, argc == macro->paramc && macro->variadic);
561 if (type == CPP_CLOSE_PAREN || type == CPP_EOF)
564 /* Re-use the last argument for excess arguments. */
565 if (argc < macro->paramc)
571 cpp_error (pfile, "unterminated argument list invoking macro \"%s\"",
575 else if (argc < macro->paramc)
577 /* As an extension, a rest argument is allowed to not appear in
578 the invocation at all.
579 e.g. #define debug(format, args...) something
582 This is exactly the same as if there had been an empty rest
583 argument - debug("string", ). */
585 if (argc + 1 == macro->paramc && macro->variadic)
587 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
588 cpp_pedwarn (pfile, "ISO C99 requires rest arguments to be used");
593 "macro \"%s\" requires %u arguments, but only %u given",
594 NODE_NAME (node), macro->paramc, argc);
598 else if (argc > macro->paramc)
600 /* Empty argument to a macro taking no arguments is OK. */
601 if (argc != 1 || cur->count)
604 "macro \"%s\" passed %u arguments, but takes just %u",
605 NODE_NAME (node), argc, macro->paramc);
620 funlike_invocation_p (pfile, node)
622 const cpp_hashnode *node;
624 const cpp_token *maybe_paren;
627 pfile->state.prevent_expansion++;
628 pfile->keep_tokens++;
630 pfile->state.parsing_args = 1;
632 maybe_paren = cpp_get_token (pfile);
633 while (maybe_paren->type == CPP_PADDING);
634 pfile->state.parsing_args = 2;
636 if (maybe_paren->type == CPP_OPEN_PAREN)
637 args = parse_args (pfile, node);
640 _cpp_backup_tokens (pfile, 1);
641 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
643 "function-like macro \"%s\" must be used with arguments in traditional C",
647 pfile->state.parsing_args = 0;
648 pfile->keep_tokens--;
649 pfile->state.prevent_expansion--;
653 if (node->value.macro->paramc > 0)
654 replace_args (pfile, node->value.macro, args);
661 /* Push the context of a macro onto the context stack. TOKEN is the
662 macro name. If we can successfully start expanding the macro,
663 TOKEN is replaced with the first token of the expansion, and we
666 enter_macro_context (pfile, node)
670 if (node->flags & NODE_BUILTIN)
671 push_token_context (pfile, NULL, builtin_macro (pfile, node), 1);
674 cpp_macro *macro = node->value.macro;
676 if (!pfile->context->prev)
679 if (macro->fun_like && !funlike_invocation_p (pfile, node))
681 if (!pfile->context->prev)
682 unlock_pools (pfile);
686 /* Disable the macro within its expansion. */
689 if (macro->paramc == 0)
690 push_token_context (pfile, macro, macro->expansion, macro->count);
696 /* Take the expansion of a function-like MACRO, replacing parameters
697 with the actual arguments. Each instance is first macro-expanded,
698 unless that paramter is operated upon by the # or ## operators. */
700 replace_args (pfile, macro, args)
705 unsigned int i, total;
706 const cpp_token *src, *limit;
707 const cpp_token **dest, **first;
710 /* First, fully macro-expand arguments, calculating the number of
711 tokens in the final expansion as we go. This ensures that the
712 possible recursive use of argument_pool is fine. The ordering of
713 the if statements below is subtle; we must handle stringification
715 total = macro->count;
716 limit = macro->expansion + macro->count;
718 for (src = macro->expansion; src < limit; src++)
719 if (src->type == CPP_MACRO_ARG)
721 /* Leading and trailing padding tokens. */
724 /* We have an argument. If it is not being stringified or
725 pasted it is macro-replaced before insertion. */
726 arg = &args[src->val.arg_no - 1];
728 if (src->flags & STRINGIFY_ARG)
730 if (!arg->stringified)
731 arg->stringified = stringify_arg (pfile, arg);
733 else if ((src->flags & PASTE_LEFT)
734 || (src > macro->expansion && (src[-1].flags & PASTE_LEFT)))
735 total += arg->count - 1;
739 expand_arg (pfile, arg);
740 total += arg->expanded_count - 1;
744 /* Now allocate space for the expansion, copy the tokens and replace
746 first = (const cpp_token **) _cpp_pool_alloc (&pfile->argument_pool,
747 total * sizeof (cpp_token *));
750 for (src = macro->expansion; src < limit; src++)
753 const cpp_token **from, **paste_flag;
755 if (src->type != CPP_MACRO_ARG)
762 arg = &args[src->val.arg_no - 1];
763 if (src->flags & STRINGIFY_ARG)
764 count = 1, from = &arg->stringified;
765 else if (src->flags & PASTE_LEFT)
766 count = arg->count, from = arg->first;
767 else if (src != macro->expansion && (src[-1].flags & PASTE_LEFT))
769 count = arg->count, from = arg->first;
772 /* GCC has special semantics for , ## b where b is a
773 varargs parameter: the comma disappears if b was
774 given no actual arguments (not merely if b is an
775 empty argument); otherwise the paste flag is removed. */
776 if (dest[-1]->type == CPP_COMMA
778 && src->val.arg_no == macro->paramc)
783 paste_flag = dest - 1;
785 /* Remove the paste flag if the RHS is a placemarker. */
787 paste_flag = dest - 1;
791 count = arg->expanded_count, from = arg->expanded;
793 /* Padding on the left of an argument (unless RHS of ##). */
794 if (!pfile->state.in_directive
795 && src != macro->expansion && !(src[-1].flags & PASTE_LEFT))
796 *dest++ = padding_token (pfile, src);
800 memcpy (dest, from, count * sizeof (cpp_token *));
803 /* With a non-empty argument on the LHS of ##, the last
804 token should be flagged PASTE_LEFT. */
805 if (src->flags & PASTE_LEFT)
806 paste_flag = dest - 1;
809 /* Avoid paste on RHS (even case count == 0). */
810 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
811 *dest++ = &pfile->avoid_paste;
813 /* Add a new paste flag, or remove an unwanted one. */
816 cpp_token *token = _cpp_temp_token (pfile);
817 token->type = (*paste_flag)->type;
818 token->val.str = (*paste_flag)->val.str;
819 if (src->flags & PASTE_LEFT)
820 token->flags = (*paste_flag)->flags | PASTE_LEFT;
822 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
827 /* Free the expanded arguments. */
828 for (i = 0; i < macro->paramc; i++)
829 if (args[i].expanded)
830 free (args[i].expanded);
832 push_ptoken_context (pfile, macro, first, dest - first);
835 /* Return a special padding token, with padding inherited from SOURCE. */
836 static const cpp_token *
837 padding_token (pfile, source)
839 const cpp_token *source;
841 cpp_token *result = _cpp_temp_token (pfile);
843 result->type = CPP_PADDING;
844 result->val.source = source;
849 /* Move to the next context. Create one if there is none. */
854 cpp_context *result = pfile->context->next;
858 result = xnew (cpp_context);
859 result->prev = pfile->context;
861 pfile->context->next = result;
864 pfile->context = result;
868 /* Push a list of pointers to tokens. */
870 push_ptoken_context (pfile, macro, first, count)
873 const cpp_token **first;
876 cpp_context *context = next_context (pfile);
878 context->direct_p = false;
879 context->macro = macro;
880 context->first.ptoken = first;
881 context->last.ptoken = first + count;
884 /* Push a list of tokens. */
886 push_token_context (pfile, macro, first, count)
889 const cpp_token *first;
892 cpp_context *context = next_context (pfile);
894 context->direct_p = true;
895 context->macro = macro;
896 context->first.token = first;
897 context->last.token = first + count;
901 expand_arg (pfile, arg)
905 unsigned int capacity;
907 arg->expanded_count = 0;
911 /* Loop, reading in the arguments. */
913 arg->expanded = (const cpp_token **)
914 xmalloc (capacity * sizeof (cpp_token *));
916 push_ptoken_context (pfile, NULL, arg->first, arg->count + 1);
919 const cpp_token *token;
921 if (arg->expanded_count + 1 >= capacity)
924 arg->expanded = (const cpp_token **)
925 xrealloc (arg->expanded, capacity * sizeof (cpp_token *));
928 token = cpp_get_token (pfile);
930 if (token->type == CPP_EOF)
933 arg->expanded[arg->expanded_count++] = token;
936 /* Avoid the unlock_pools test of _cpp_pop_context. Change this to
937 call _cpp_pop_context once we remove pool locking. */
938 pfile->context = pfile->context->prev;
942 _cpp_pop_context (pfile)
945 /* Re-enable a macro when leaving its expansion. */
946 if (pfile->context->macro)
947 pfile->context->macro->disabled = 0;
949 pfile->context = pfile->context->prev;
950 if (!pfile->context->prev && !pfile->state.parsing_args)
951 unlock_pools (pfile);
954 /* Eternal routine to get a token. Also used nearly everywhere
955 internally, except for places where we know we can safely call
956 the lexer directly, such as lexing a directive name.
958 Macro expansions and directives are transparently handled,
959 including entering included files. Thus tokens are post-macro
960 expansion, and after any intervening directives. External callers
961 see CPP_EOF only at EOF. Internal callers also see it when meeting
962 a directive inside a macro call, when at the end of a directive and
963 state.in_directive is still 1, and at the end of argument
966 cpp_get_token (pfile)
969 const cpp_token *result;
974 cpp_context *context = pfile->context;
976 /* Context->prev == 0 <=> base context. */
978 result = _cpp_lex_token (pfile);
979 else if (context->first.token != context->last.token)
981 if (context->direct_p)
982 result = context->first.token++;
984 result = *context->first.ptoken++;
986 if (result->flags & PASTE_LEFT)
988 paste_all_tokens (pfile, result);
989 if (pfile->state.in_directive)
991 return padding_token (pfile, result);
996 _cpp_pop_context (pfile);
997 if (pfile->state.in_directive)
999 return &pfile->avoid_paste;
1002 if (result->type != CPP_NAME)
1005 node = result->val.node;
1007 /* Handle macros and the _Pragma operator. */
1008 if (node->type == NT_MACRO && !(result->flags & NO_EXPAND))
1010 /* Macros invalidate controlling macros. */
1011 pfile->mi_valid = false;
1013 if (!(node->flags & NODE_BUILTIN) && node->value.macro->disabled)
1015 /* Flag this token as always unexpandable. */
1016 cpp_token *t = _cpp_temp_token (pfile);
1017 t->type = result->type;
1018 t->flags = result->flags | NO_EXPAND;
1019 t->val.str = result->val.str;
1022 else if (!pfile->state.prevent_expansion
1023 && enter_macro_context (pfile, node))
1025 if (pfile->state.in_directive)
1027 return padding_token (pfile, result);
1031 /* Don't interpret _Pragma within directives. The standard is
1032 not clear on this, but to me this makes most sense. */
1033 if (node != pfile->spec_nodes.n__Pragma
1034 || pfile->state.in_directive)
1037 /* Handle it, and loop back for another token. MI is cleared
1038 since this token came from either the lexer or a macro. */
1039 _cpp_do__Pragma (pfile);
1045 /* Returns true if we're expanding an object-like macro that was
1046 defined in a system header. Just checks the macro at the top of
1047 the stack. Used for diagnostic suppression. */
1049 cpp_sys_macro_p (pfile)
1052 cpp_macro *macro = pfile->context->macro;
1054 return macro && macro->syshdr;
1057 /* Read each token in, until EOF. Directives are transparently
1060 cpp_scan_nooutput (pfile)
1063 while (cpp_get_token (pfile)->type != CPP_EOF)
1067 /* Step back one (or more) tokens. Can only step mack more than 1 if
1068 they are from the lexer, and not from macro expansion. */
1070 _cpp_backup_tokens (pfile, count)
1074 if (pfile->context->prev == NULL)
1076 pfile->lookaheads += count;
1080 if (pfile->cur_token == pfile->cur_run->base)
1082 pfile->cur_run = pfile->cur_run->prev;
1083 pfile->cur_token = pfile->cur_run->limit;
1091 if (pfile->context->direct_p)
1092 pfile->context->first.token--;
1094 pfile->context->first.ptoken--;
1098 /* #define directive parsing and handling. */
1100 /* Returns non-zero if a macro redefinition warning is required. */
1102 warn_of_redefinition (pfile, node, macro2)
1104 const cpp_hashnode *node;
1105 const cpp_macro *macro2;
1107 const cpp_macro *macro1;
1110 /* Some redefinitions need to be warned about regardless. */
1111 if (node->flags & NODE_WARN)
1114 if (! CPP_PEDANTIC (pfile))
1117 /* Redefinition of a macro is allowed if and only if the old and new
1118 definitions are the same. (6.10.3 paragraph 2). */
1119 macro1 = node->value.macro;
1121 /* The quick failures. */
1122 if (macro1->count != macro2->count
1123 || macro1->paramc != macro2->paramc
1124 || macro1->fun_like != macro2->fun_like
1125 || macro1->variadic != macro2->variadic)
1128 /* Check each token. */
1129 for (i = 0; i < macro1->count; i++)
1130 if (! _cpp_equiv_tokens (¯o1->expansion[i], ¯o2->expansion[i]))
1133 /* Check parameter spellings. */
1134 for (i = 0; i < macro1->paramc; i++)
1135 if (macro1->params[i] != macro2->params[i])
1141 /* Free the definition of hashnode H. */
1144 _cpp_free_definition (h)
1147 /* Macros and assertions no longer have anything to free. */
1149 /* Clear builtin flag in case of redefinition. */
1150 h->flags &= ~NODE_BUILTIN;
1153 /* Save parameter NODE to the parameter list of macro MACRO. Returns
1154 zero on success, non-zero if the paramter is a duplicate. */
1156 save_parameter (pfile, macro, node)
1161 cpp_hashnode **dest;
1163 /* Constraint 6.10.3.6 - duplicate parameter names. */
1164 if (node->arg_index)
1166 cpp_error (pfile, "duplicate macro parameter \"%s\"", NODE_NAME (node));
1170 dest = ¯o->params[macro->paramc];
1172 /* Check we have room for the parameters. */
1173 if ((unsigned char *) (dest + 1) >= POOL_LIMIT (&pfile->macro_pool))
1175 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_hashnode *),
1176 (unsigned char **) ¯o->params);
1177 dest = ¯o->params[macro->paramc];
1181 node->arg_index = ++macro->paramc;
1185 /* Check the syntax of the paramters in a MACRO definition. */
1187 parse_params (pfile, macro)
1191 unsigned int prev_ident = 0;
1193 macro->params = (cpp_hashnode **) POOL_FRONT (&pfile->macro_pool);
1196 const cpp_token *token = _cpp_lex_token (pfile);
1198 switch (token->type)
1201 cpp_error (pfile, "\"%s\" may not appear in macro parameter list",
1202 cpp_token_as_text (pfile, token));
1208 cpp_error (pfile, "macro parameters must be comma-separated");
1213 if (save_parameter (pfile, macro, token->val.node))
1217 case CPP_CLOSE_PAREN:
1218 if (prev_ident || macro->paramc == 0)
1221 /* Fall through to pick up the error. */
1225 cpp_error (pfile, "parameter name missing");
1232 macro->variadic = 1;
1235 save_parameter (pfile, macro, pfile->spec_nodes.n__VA_ARGS__);
1236 pfile->state.va_args_ok = 1;
1237 if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
1239 "anonymous variadic macros were introduced in C99");
1241 else if (CPP_OPTION (pfile, pedantic))
1242 cpp_pedwarn (pfile, "ISO C does not permit named variadic macros");
1244 /* We're at the end, and just expect a closing parenthesis. */
1245 token = _cpp_lex_token (pfile);
1246 if (token->type == CPP_CLOSE_PAREN)
1251 cpp_error (pfile, "missing ')' in macro parameter list");
1255 /* Success. Commit the parameter array. */
1256 POOL_COMMIT (&pfile->macro_pool,
1257 macro->paramc * sizeof (cpp_hashnode *));
1262 /* Allocate room for a token from a macro's replacement list. */
1264 alloc_expansion_token (pfile, macro)
1268 cpp_token *token = ¯o->expansion[macro->count];
1270 /* Check we have room for the token. */
1271 if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->macro_pool))
1273 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_token),
1274 (unsigned char **) ¯o->expansion);
1275 token = ¯o->expansion[macro->count];
1283 lex_expansion_token (pfile, macro)
1289 pfile->cur_token = alloc_expansion_token (pfile, macro);
1290 token = _cpp_lex_direct (pfile);
1292 /* Is this an argument? */
1293 if (token->type == CPP_NAME && token->val.node->arg_index)
1295 token->type = CPP_MACRO_ARG;
1296 token->val.arg_no = token->val.node->arg_index;
1298 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1299 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1300 check_trad_stringification (pfile, macro, &token->val.str);
1305 /* Parse a macro and save its expansion. Returns non-zero on success. */
1307 _cpp_create_definition (pfile, node)
1312 cpp_token *token, *saved_cur_token;
1313 const cpp_token *ctoken;
1314 unsigned int i, ok = 1;
1316 macro = (cpp_macro *) _cpp_pool_alloc (&pfile->macro_pool,
1317 sizeof (cpp_macro));
1318 macro->line = pfile->directive_line;
1321 macro->variadic = 0;
1323 macro->fun_like = 0;
1325 /* Get the first token of the expansion (or the '(' of a
1326 function-like macro). */
1327 ctoken = _cpp_lex_token (pfile);
1329 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
1331 if (!(ok = parse_params (pfile, macro)))
1333 macro->fun_like = 1;
1335 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
1336 cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name");
1338 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
1339 saved_cur_token = pfile->cur_token;
1340 macro->expansion = (cpp_token *) POOL_FRONT (&pfile->macro_pool);
1342 if (macro->fun_like)
1343 token = lex_expansion_token (pfile, macro);
1346 token = alloc_expansion_token (pfile, macro);
1352 /* Check the stringifying # constraint 6.10.3.2.1 of
1353 function-like macros when lexing the subsequent token. */
1354 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1356 if (token->type == CPP_MACRO_ARG)
1358 token->flags &= ~PREV_WHITE;
1359 token->flags |= STRINGIFY_ARG;
1360 token->flags |= token[-1].flags & PREV_WHITE;
1361 token[-1] = token[0];
1364 /* Let assembler get away with murder. */
1365 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
1368 cpp_error (pfile, "'#' is not followed by a macro parameter");
1373 if (token->type == CPP_EOF)
1376 /* Paste operator constraint 6.10.3.3.1. */
1377 if (token->type == CPP_PASTE)
1379 /* Token-paste ##, can appear in both object-like and
1380 function-like macros, but not at the ends. */
1381 if (--macro->count > 0)
1382 token = lex_expansion_token (pfile, macro);
1384 if (macro->count == 0 || token->type == CPP_EOF)
1388 "'##' cannot appear at either end of a macro expansion");
1392 token[-1].flags |= PASTE_LEFT;
1395 token = lex_expansion_token (pfile, macro);
1398 /* Don't count the CPP_EOF. */
1401 /* Implement the macro-defined-to-itself optimisation. */
1402 macro->disabled = (macro->count == 1 && !macro->fun_like
1403 && macro->expansion[0].type == CPP_NAME
1404 && macro->expansion[0].val.node == node);
1406 /* To suppress some diagnostics. */
1407 macro->syshdr = pfile->map->sysp != 0;
1409 /* Commit the memory. */
1410 POOL_COMMIT (&pfile->macro_pool, macro->count * sizeof (cpp_token));
1412 if (node->type != NT_VOID)
1414 if (warn_of_redefinition (pfile, node, macro))
1416 cpp_pedwarn_with_line (pfile, pfile->directive_line, 0,
1417 "\"%s\" redefined", NODE_NAME (node));
1419 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1420 cpp_pedwarn_with_line (pfile, node->value.macro->line, 0,
1421 "this is the location of the previous definition");
1423 _cpp_free_definition (node);
1426 /* Enter definition in hash table. */
1427 node->type = NT_MACRO;
1428 node->value.macro = macro;
1429 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
1430 node->flags |= NODE_WARN;
1434 /* Set type for SEEN_EOL() in cpplib.c, restore the lexer position. */
1435 saved_cur_token[-1].type = pfile->cur_token[-1].type;
1436 pfile->cur_token = saved_cur_token;
1440 /* Stop the lexer accepting __VA_ARGS__. */
1441 pfile->state.va_args_ok = 0;
1443 /* Clear the fast argument lookup indices. */
1444 for (i = macro->paramc; i-- > 0; )
1445 macro->params[i]->arg_index = 0;
1450 /* Warn if a token in `string' matches one of the function macro
1451 arguments in `info'. This function assumes that the macro is a
1452 function macro and not an object macro. */
1454 check_trad_stringification (pfile, macro, string)
1456 const cpp_macro *macro;
1457 const cpp_string *string;
1459 unsigned int i, len;
1460 const U_CHAR *p, *q, *limit = string->text + string->len;
1462 /* Loop over the string. */
1463 for (p = string->text; p < limit; p = q)
1465 /* Find the start of an identifier. */
1466 while (p < limit && !is_idstart (*p))
1469 /* Find the end of the identifier. */
1471 while (q < limit && is_idchar (*q))
1476 /* Loop over the function macro arguments to see if the
1477 identifier inside the string matches one of them. */
1478 for (i = 0; i < macro->paramc; i++)
1480 const cpp_hashnode *node = macro->params[i];
1482 if (NODE_LEN (node) == len
1483 && !memcmp (p, NODE_NAME (node), len))
1486 "macro argument \"%s\" would be stringified with -traditional.",
1494 /* Returns the name, arguments and expansion of a macro, in a format
1495 suitable to be read back in again, and therefore also for DWARF 2
1496 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1497 Caller is expected to generate the "#define" bit if needed. The
1498 returned text is temporary, and automatically freed later. */
1500 const unsigned char *
1501 cpp_macro_definition (pfile, node)
1503 const cpp_hashnode *node;
1505 unsigned int i, len;
1506 const cpp_macro *macro = node->value.macro;
1507 unsigned char *buffer;
1509 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1511 cpp_ice (pfile, "invalid hash type %d in cpp_macro_definition", node->type);
1515 /* Calculate length. */
1516 len = NODE_LEN (node) + 1; /* ' ' */
1517 if (macro->fun_like)
1519 len += 3; /* "()" plus possible final "." of named
1520 varargs (we have + 2 below). */
1521 for (i = 0; i < macro->paramc; i++)
1522 len += NODE_LEN (macro->params[i]) + 2; /* ", " */
1525 for (i = 0; i < macro->count; i++)
1527 cpp_token *token = ¯o->expansion[i];
1529 if (token->type == CPP_MACRO_ARG)
1530 len += NODE_LEN (macro->params[token->val.arg_no - 1]);
1532 len += cpp_token_len (token); /* Includes room for ' '. */
1533 if (token->flags & STRINGIFY_ARG)
1535 if (token->flags & PASTE_LEFT)
1536 len += 3; /* " ##" */
1539 if (len > pfile->macro_buffer_len)
1541 pfile->macro_buffer = (U_CHAR *) xrealloc (pfile->macro_buffer, len);
1542 pfile->macro_buffer_len = len;
1545 /* Fill in the buffer. Start with the macro name. */
1546 buffer = pfile->macro_buffer;
1547 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1548 buffer += NODE_LEN (node);
1550 /* Parameter names. */
1551 if (macro->fun_like)
1554 for (i = 0; i < macro->paramc; i++)
1556 cpp_hashnode *param = macro->params[i];
1558 if (param != pfile->spec_nodes.n__VA_ARGS__)
1560 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1561 buffer += NODE_LEN (param);
1564 if (i + 1 < macro->paramc)
1565 *buffer++ = ',', *buffer++ = ' ';
1566 else if (macro->variadic)
1567 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1572 /* Expansion tokens. */
1576 for (i = 0; i < macro->count; i++)
1578 cpp_token *token = ¯o->expansion[i];
1580 if (token->flags & PREV_WHITE)
1582 if (token->flags & STRINGIFY_ARG)
1585 if (token->type == CPP_MACRO_ARG)
1587 len = NODE_LEN (macro->params[token->val.arg_no - 1]);
1589 NODE_NAME (macro->params[token->val.arg_no - 1]), len);
1593 buffer = cpp_spell_token (pfile, token, buffer);
1595 if (token->flags & PASTE_LEFT)
1600 /* Next has PREV_WHITE; see _cpp_create_definition. */
1606 return pfile->macro_buffer;