1 /* Part of CPP library. (Macro and #define handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
3 1999, 2000 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 const char *file; /* Defined in file name. */
37 unsigned int line; /* Starting line number. */
38 unsigned int count; /* Number of tokens in expansion. */
39 unsigned short paramc; /* Number of parameters. */
40 unsigned int fun_like : 1; /* If a function-like macro. */
41 unsigned int var_args : 1; /* If a variable-args macro. */
42 unsigned int disabled : 1; /* If macro is disabled. */
45 typedef struct macro_arg macro_arg;
48 cpp_token *first; /* First token in unexpanded argument. */
49 cpp_token *expanded; /* Macro-expanded argument. */
50 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_token *));
60 static void builtin_macro PARAMS ((cpp_reader *, cpp_token *));
61 static cpp_context *push_arg_context PARAMS ((cpp_reader *, macro_arg *));
62 static enum cpp_ttype parse_arg PARAMS ((cpp_reader *, macro_arg *, int));
63 static macro_arg *parse_args PARAMS ((cpp_reader *, const cpp_hashnode *));
64 static cpp_context *next_context PARAMS ((cpp_reader *));
65 static void expand_arg PARAMS ((cpp_reader *, macro_arg *));
66 static unsigned char *quote_string PARAMS ((unsigned char *,
67 const unsigned char *,
69 static void make_string_token PARAMS ((cpp_pool *, cpp_token *,
70 const U_CHAR *, unsigned int));
71 static void make_number_token PARAMS ((cpp_reader *, cpp_token *, int));
72 static void stringify_arg PARAMS ((cpp_reader *, macro_arg *));
73 static void paste_all_tokens PARAMS ((cpp_reader *, cpp_token *));
74 static void paste_payloads PARAMS ((cpp_reader *, cpp_token *,
76 static int funlike_invocation_p PARAMS ((cpp_reader *, const cpp_hashnode *,
78 static void replace_args PARAMS ((cpp_reader *, cpp_macro *, macro_arg *,
83 static void save_lookahead_token PARAMS ((cpp_reader *, const cpp_token *));
84 static void take_lookahead_token PARAMS ((cpp_reader *, cpp_token *));
85 static void release_lookahead PARAMS ((cpp_reader *));
86 static cpp_lookahead *alloc_lookahead PARAMS ((cpp_reader *));
87 static void free_lookahead PARAMS ((cpp_lookahead *));
89 /* #define directive parsing and handling. */
91 static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
92 static int check_macro_redefinition PARAMS ((cpp_reader *,
95 static int save_parameter PARAMS ((cpp_reader *, cpp_macro *, cpp_hashnode *));
96 static int parse_params PARAMS ((cpp_reader *, cpp_macro *));
97 static void check_trad_stringification PARAMS ((cpp_reader *,
101 /* Allocates a buffer to hold a token's TEXT, and converts TOKEN to a
102 CPP_STRING token containing TEXT in quoted form. */
104 make_string_token (pool, token, text, len)
110 U_CHAR *buf = _cpp_pool_alloc (pool, len * 4);
112 token->type = CPP_STRING;
113 token->val.str.text = buf;
114 token->val.str.len = quote_string (buf, text, len) - buf;
118 /* Allocates and converts a temporary token to a CPP_NUMBER token,
119 evaluating to NUMBER. */
121 make_number_token (pfile, token, number)
126 unsigned char *buf = _cpp_pool_alloc (pfile->string_pool, 20);
128 sprintf ((char *) buf, "%d", number);
129 token->type = CPP_NUMBER;
130 token->val.str.text = buf;
131 token->val.str.len = ustrlen (buf);
135 static const char * const monthnames[] =
137 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
138 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
141 /* Handle builtin macros like __FILE__. */
143 builtin_macro (pfile, token)
147 unsigned char flags = token->flags & PREV_WHITE;
148 cpp_hashnode *node = token->val.node;
151 switch (node->value.builtin)
158 ip = CPP_BUFFER (pfile);
163 if (node->value.builtin == BT_BASE_FILE)
164 while (CPP_PREV_BUFFER (ip) != NULL)
165 ip = CPP_PREV_BUFFER (ip);
167 file = ip->nominal_fname;
169 make_string_token (pfile->string_pool, token,
170 (U_CHAR *) file, strlen (file));
174 case BT_INCLUDE_LEVEL:
175 /* pfile->include_depth counts the primary source as level 1,
176 but historically __INCLUDE_DEPTH__ has called the primary
178 make_number_token (pfile, token, pfile->include_depth - 1);
182 /* If __LINE__ is embedded in a macro, it must expand to the
183 line of the macro's invocation, not its definition.
184 Otherwise things like assert() will not work properly. */
185 make_number_token (pfile, token, cpp_get_line (pfile)->line);
192 #ifdef STDC_0_IN_SYSTEM_HEADERS
193 if (CPP_IN_SYSTEM_HEADER (pfile)
194 && pfile->spec_nodes.n__STRICT_ANSI__->type == NT_VOID)
197 make_number_token (pfile, token, stdc);
203 if (pfile->date.type == CPP_EOF)
205 /* Allocate __DATE__ and __TIME__ from permanent storage,
206 and save them in pfile so we don't have to do this again.
207 We don't generate these strings at init time because
208 time() and localtime() are very slow on some systems. */
209 time_t tt = time (NULL);
210 struct tm *tb = localtime (&tt);
212 make_string_token (&pfile->ident_pool, &pfile->date,
214 make_string_token (&pfile->ident_pool, &pfile->time,
217 sprintf ((char *) pfile->date.val.str.text, "%s %2d %4d",
218 monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
219 sprintf ((char *) pfile->time.val.str.text, "%02d:%02d:%02d",
220 tb->tm_hour, tb->tm_min, tb->tm_sec);
222 *token = node->value.builtin == BT_DATE ? pfile->date: pfile->time;
226 cpp_ice (pfile, "invalid builtin macro \"%s\"", node->name);
230 token->flags = flags;
233 /* Used by cpperror.c to obtain the correct line and column to report
235 const cpp_lexer_pos *
239 /* Within a macro expansion, return the position of the outermost
241 if (pfile->context->prev)
242 return &pfile->macro_pos;
243 return &pfile->lexer_pos;
250 _cpp_lock_pool (&pfile->temp_string_pool);
251 _cpp_lock_pool (&pfile->argument_pool);
258 _cpp_unlock_pool (&pfile->temp_string_pool);
259 _cpp_unlock_pool (&pfile->argument_pool);
263 paste_payloads (pfile, lhs, rhs)
266 const cpp_token *rhs;
268 unsigned int total_len = cpp_token_len (lhs) + cpp_token_len (rhs);
269 unsigned char *result, *end;
272 pool = lhs->type == CPP_NAME ? &pfile->ident_pool: pfile->string_pool;
273 result = _cpp_pool_alloc (pool, total_len + 1);
275 /* Paste the spellings and null terminate. */
276 end = cpp_spell_token (pfile, rhs, cpp_spell_token (pfile, lhs, result));
278 total_len = end - result;
280 if (lhs->type == CPP_NAME)
282 lhs->val.node = cpp_lookup (pfile, result, total_len);
283 if (lhs->val.node->flags & NODE_OPERATOR)
285 lhs->flags |= NAMED_OP;
286 lhs->type = lhs->val.node->value.operator;
291 lhs->val.str.text = result;
292 lhs->val.str.len = total_len;
296 /* Adds backslashes before all backslashes and double quotes appearing
297 in strings. Non-printable characters are converted to octal. */
299 quote_string (dest, src, len)
308 if (c == '\\' || c == '"')
319 sprintf ((char *) dest, "\\%03o", c);
328 /* Convert a token sequence to a single string token according to the
329 rules of the ISO C #-operator. */
331 stringify_arg (pfile, arg)
335 cpp_pool *pool = pfile->string_pool;
336 unsigned char *start = POOL_FRONT (pool);
337 unsigned int i, escape_it, total_len = 0, backslash_count = 0;
338 unsigned int prev_white = 0;
340 /* Loop, reading in the argument's tokens. */
341 for (i = 0; i < arg->count; i++)
344 const cpp_token *token = &arg->first[i];
345 unsigned int len = cpp_token_len (token);
347 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
348 || token->type == CPP_CHAR || token->type == CPP_WCHAR
349 || token->type == CPP_OSTRING);
352 /* Worst case is each char is octal. */
354 len++; /* Room for initial space. */
356 dest = &start[total_len];
357 if (dest + len > POOL_LIMIT (pool))
359 _cpp_next_chunk (pool, len, (unsigned char **) &start);
360 dest = &start[total_len];
363 prev_white |= token->flags & PREV_WHITE;
364 if (token->type == CPP_PLACEMARKER)
367 /* No leading white space. */
377 unsigned char *buf = (unsigned char *) xmalloc (len);
379 len = cpp_spell_token (pfile, token, buf) - buf;
380 dest = quote_string (dest, buf, len);
384 dest = cpp_spell_token (pfile, token, dest);
385 total_len = dest - start;
387 if (token->type == CPP_BACKSLASH)
393 /* Ignore the final \ of invalid string literals. */
394 if (backslash_count & 1)
396 cpp_warning (pfile, "invalid string literal, ignoring final '\\'");
400 POOL_COMMIT (pool, total_len);
402 arg->stringified = xnew (cpp_token);
403 arg->stringified->flags = 0;
404 arg->stringified->type = CPP_STRING;
405 arg->stringified->val.str.text = start;
406 arg->stringified->val.str.len = total_len;
409 /* Handles an arbitrarily long sequence of ## operators. This
410 implementation is left-associative, non-recursive, and finishes a
411 paste before handling succeeding ones. If the paste fails, the
412 right hand side of the ## operator is placed in the then-current
413 context's lookahead buffer, with the effect that it appears in the
414 output stream normally. */
416 paste_all_tokens (pfile, lhs)
420 unsigned char orig_flags = lhs->flags;
425 /* Take the token directly from the current context. We can do
426 this, because we are in the replacement list of either an
427 object-like macro, or a function-like macro with arguments
428 inserted. In either case, the constraints to #define
429 guarantee we have at least one more token (empty arguments
430 become placemarkers). */
431 rhs = pfile->context->list.first++;
433 if (rhs->type == CPP_PLACEMARKER)
435 /* GCC has special extended semantics for , ## b where b is
436 a varargs parameter: the comma disappears if b was given
437 no actual arguments (not merely if b is an empty
439 if (lhs->type == CPP_COMMA && (rhs->flags & VARARGS_FIRST))
440 lhs->type = CPP_PLACEMARKER;
442 else if (lhs->type == CPP_PLACEMARKER)
447 enum cpp_ttype type = cpp_can_paste (pfile, lhs, rhs, &digraph);
451 /* Do nothing special about , ## <whatever> if
452 <whatever> came from a variable argument, because the
453 author probably intended the ## to trigger the
454 special extended semantics (see above). */
455 if (lhs->type == CPP_COMMA && (rhs->flags & VARARGS_FIRST))
459 if (CPP_OPTION (pfile, warn_paste))
461 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
462 cpp_token_as_text (pfile, lhs),
463 cpp_token_as_text (pfile, rhs));
465 /* The standard states that behaviour is undefined.
466 By the principle of least surpise, we step back
467 before the RHS, and mark it to prevent macro
468 expansion. Tests in the testsuite rely on
469 clearing PREV_WHITE here, though you could argue
470 we should actually set it. */
471 rhs->flags &= ~PREV_WHITE;
472 rhs->flags |= NO_EXPAND;
475 /* Step back so we read the RHS in next. */
476 pfile->context->list.first--;
481 lhs->flags &= ~DIGRAPH;
483 lhs->flags |= DIGRAPH;
485 if (type == CPP_NAME || type == CPP_NUMBER)
486 paste_payloads (pfile, lhs, rhs);
487 else if (type == CPP_WCHAR || type == CPP_WSTRING)
488 lhs->val.str = rhs->val.str;
491 while (rhs->flags & PASTE_LEFT);
493 /* The pasted token has the PREV_WHITE flag of the LHS, is no longer
494 PASTE_LEFT, and is subject to macro expansion. */
495 lhs->flags &= ~(PREV_WHITE | PASTE_LEFT | NO_EXPAND);
496 lhs->flags |= orig_flags & PREV_WHITE;
499 /* Reads the unexpanded tokens of a macro argument into ARG. Empty
500 arguments are saved as a single CPP_PLACEMARKER token. VAR_ARGS is
501 non-zero if this is a variable argument. Returns the type of the
502 token that caused reading to finish. */
503 static enum cpp_ttype
504 parse_arg (pfile, arg, var_args)
506 struct macro_arg *arg;
509 enum cpp_ttype result;
510 unsigned int paren = 0;
512 arg->first = (cpp_token *) POOL_FRONT (&pfile->argument_pool);
513 for (;; arg->count++)
515 cpp_token *token = &arg->first[arg->count];
516 if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->argument_pool))
518 _cpp_next_chunk (&pfile->argument_pool, sizeof (cpp_token),
519 (unsigned char **) &arg->first);
520 token = &arg->first[arg->count];
523 _cpp_get_token (pfile, token);
524 result = token->type;
526 if (result == CPP_OPEN_PAREN)
528 else if (result == CPP_CLOSE_PAREN && paren-- == 0)
530 /* Commas are not terminators within parantheses or var_args. */
531 else if (result == CPP_COMMA && paren == 0 && !var_args)
533 else if (result == CPP_EOF)
534 break; /* Error reported by caller. */
535 else if (result == CPP_DHASH)
537 /* 6.10.3 paragraph 11: If there are sequences of
538 preprocessing tokens within the list of arguments that
539 would otherwise act as preprocessing directives, the
540 behavior is undefined.
542 This implementation will report a hard error, terminate
543 the macro invocation, and proceed to process the
545 cpp_error (pfile, "directives may not be used inside a macro argument");
546 _cpp_push_token (pfile, token, &pfile->lexer_pos);
552 /* Empty arguments become a single placemarker token. */
555 arg->first->type = CPP_PLACEMARKER;
559 /* Commit the memory used to store the arguments. */
560 POOL_COMMIT (&pfile->argument_pool, arg->count * sizeof (cpp_token));
565 /* Parse the arguments making up a macro invocation. */
567 parse_args (pfile, node)
569 const cpp_hashnode *node;
571 cpp_macro *macro = node->value.macro;
572 macro_arg *args, *cur;
576 /* Allocate room for at least one argument, and zero it out. */
577 argc = macro->paramc ? macro->paramc: 1;
578 args = xcnewvec (macro_arg, argc);
580 for (cur = args, argc = 0; ;)
584 type = parse_arg (pfile, cur, argc == macro->paramc && macro->var_args);
585 if (type == CPP_CLOSE_PAREN || type == CPP_EOF)
588 /* Re-use the last argument for excess arguments. */
589 if (argc < macro->paramc)
595 cpp_error (pfile, "unterminated argument list invoking macro \"%s\"",
599 else if (argc < macro->paramc)
601 /* As an extension, a rest argument is allowed to not appear in
602 the invocation at all.
603 e.g. #define debug(format, args...) something
606 This is exactly the same as if there had been an empty rest
607 argument - debug("string", ). */
609 if (argc + 1 == macro->paramc && macro->var_args)
611 /* parse_arg ensured there was space for the closing
612 parenthesis. Use this space to store a placemarker. */
613 args[argc].first = (cpp_token *) POOL_FRONT (&pfile->argument_pool);
614 args[argc].first->type = CPP_PLACEMARKER;
615 args[argc].count = 1;
616 POOL_COMMIT (&pfile->argument_pool, sizeof (cpp_token));
618 if (CPP_OPTION (pfile, c99) && CPP_PEDANTIC (pfile))
619 cpp_pedwarn (pfile, "ISO C99 requires rest arguments to be used");
624 "macro \"%s\" requires %u arguments, but only %u given",
625 node->name, macro->paramc, argc);
629 else if (argc > macro->paramc)
631 /* An empty argument to an empty function-like macro is fine. */
632 if (argc != 1 || args[0].first->type != CPP_PLACEMARKER)
635 "macro \"%s\" passed %u arguments, but takes just %u",
636 node->name, argc, macro->paramc);
651 funlike_invocation_p (pfile, node, list)
653 const cpp_hashnode *node;
654 struct toklist *list;
656 cpp_context *orig_context;
657 cpp_token maybe_paren;
660 pfile->state.parsing_args = 1;
661 pfile->state.prevent_expansion++;
662 orig_context = pfile->context;
664 cpp_start_lookahead (pfile);
665 cpp_get_token (pfile, &maybe_paren);
666 cpp_stop_lookahead (pfile, maybe_paren.type == CPP_OPEN_PAREN);
668 if (maybe_paren.type == CPP_OPEN_PAREN)
669 args = parse_args (pfile, node);
670 else if (CPP_WTRADITIONAL (pfile))
672 "function-like macro \"%s\" must be used with arguments in traditional C",
675 /* Restore original context. */
676 pfile->context = orig_context;
677 pfile->state.prevent_expansion--;
678 pfile->state.parsing_args = 0;
682 if (node->value.macro->paramc > 0)
683 replace_args (pfile, node->value.macro, args, list);
690 /* Push the context of a macro onto the context stack. TOKEN is the
691 macro name. If we can successfully start expanding the macro,
692 TOKEN is replaced with the first token of the expansion. */
694 enter_macro_context (pfile, token)
698 cpp_context *context;
703 macro = token->val.node->value.macro;
706 token->flags |= NO_EXPAND;
710 /* Save the position of the outermost macro invocation. */
711 if (!pfile->context->prev)
713 pfile->macro_pos = pfile->lexer_pos;
717 if (macro->fun_like && !funlike_invocation_p (pfile, token->val.node, &list))
719 if (!pfile->context->prev)
720 unlock_pools (pfile);
724 /* Now push its context. */
725 context = next_context (pfile);
726 if (macro->paramc == 0)
728 context->list.first = macro->expansion;
729 context->list.limit = macro->expansion + macro->count;
732 context->list = list;
733 context->macro = macro;
735 /* The first expansion token inherits the PREV_WHITE of TOKEN. */
736 flags = token->flags & PREV_WHITE;
737 *token = *context->list.first++;
738 token->flags |= flags;
740 /* Disable the macro within its expansion. */
746 /* Move to the next context. Create one if there is none. */
751 cpp_context *prev = pfile->context;
752 cpp_context *result = prev->next;
756 result = xnew (cpp_context);
762 pfile->context = result;
767 replace_args (pfile, macro, args, list)
771 struct toklist *list;
773 unsigned int i, total;
774 const cpp_token *src, *limit;
778 src = macro->expansion;
779 limit = src + macro->count;
781 /* First, fully macro-expand arguments, calculating the number of
782 tokens in the final expansion as we go. This ensures that the
783 possible recursive use of argument_pool is fine. */
785 for (; src < limit; src++)
786 if (src->type == CPP_MACRO_ARG)
788 /* We have an argument. If it is not being stringified or
789 pasted it is macro-replaced before insertion. */
790 arg = &args[src->val.aux - 1];
791 if (src->flags & STRINGIFY_ARG)
793 if (!arg->stringified)
794 stringify_arg (pfile, arg);
796 else if ((src->flags & PASTE_LEFT)
797 || (src > macro->expansion && (src[-1].flags & PASTE_LEFT)))
798 total += arg->count - 1;
802 expand_arg (pfile, arg);
803 total += arg->expanded_count - 1;
807 dest = (cpp_token *) _cpp_pool_alloc (&pfile->argument_pool,
808 total * sizeof (cpp_token));
810 list->limit = list->first + total;
812 for (src = macro->expansion; src < limit; src++)
813 if (src->type == CPP_MACRO_ARG)
816 const cpp_token *from;
818 arg = &args[src->val.aux - 1];
819 if (src->flags & STRINGIFY_ARG)
820 from = arg->stringified, count = 1;
821 else if ((src->flags & PASTE_LEFT)
822 || (src > macro->expansion && (src[-1].flags & PASTE_LEFT)))
823 count = arg->count, from = arg->first;
825 count = arg->expanded_count, from = arg->expanded;
826 memcpy (dest, from, count * sizeof (cpp_token));
828 /* The first token gets PREV_WHITE of the CPP_MACRO_ARG. If
829 it is a variable argument, it is also flagged. */
830 dest->flags &= ~PREV_WHITE;
831 dest->flags |= src->flags & PREV_WHITE;
832 if (macro->var_args && src->val.aux == macro->paramc)
833 dest->flags |= VARARGS_FIRST;
835 /* The last token gets the PASTE_LEFT of the CPP_MACRO_ARG. */
836 dest[count - 1].flags |= src->flags & PASTE_LEFT;
843 /* Free the expanded arguments. */
844 for (i = 0; i < macro->paramc; i++)
846 if (args[i].expanded)
847 free (args[i].expanded);
848 if (args[i].stringified)
849 free (args[i].stringified);
853 /* Subroutine of expand_arg to put the unexpanded tokens on the
856 push_arg_context (pfile, arg)
860 cpp_context *context = next_context (pfile);
862 context->list.first = arg->first;
863 context->list.limit = arg->first + arg->count;
869 expand_arg (pfile, arg)
874 unsigned int capacity = 256;
876 /* Loop, reading in the arguments. */
877 arg->expanded = (cpp_token *) xmalloc (capacity * sizeof (cpp_token));
878 arg->expanded_count = 0;
880 push_arg_context (pfile, arg);
883 if (arg->expanded_count >= capacity)
886 arg->expanded = (cpp_token *)
887 xrealloc (arg->expanded, capacity * sizeof (cpp_token));
889 token = &arg->expanded[arg->expanded_count++];
890 _cpp_get_token (pfile, token);
892 while (token->type != CPP_EOF);
894 arg->expanded_count--;
896 /* Pop the context we pushed. */
897 pfile->context = pfile->context->prev;
901 _cpp_pop_context (pfile)
904 cpp_context *context = pfile->context;
906 pfile->context = context->prev;
907 /* Re-enable a macro and free resources when leaving its expansion. */
908 if (!pfile->state.parsing_args)
910 if (!pfile->context->prev)
911 unlock_pools (pfile);
912 context->macro->disabled = 0;
916 /* Internal routine to return a token, either from an in-progress
917 macro expansion, or from the source file as appropriate. Handles
918 macros, so tokens returned are post-expansion. Does not filter
919 CPP_PLACEMARKER tokens. Returns CPP_EOF at EOL and EOF. */
921 _cpp_get_token (pfile, token)
927 cpp_context *context = pfile->context;
930 take_lookahead_token (pfile, token);
931 /* Context->prev == 0 <=> base context. */
932 else if (!context->prev)
933 _cpp_lex_token (pfile, token);
934 else if (context->list.first != context->list.limit)
935 *token = *context->list.first++;
940 _cpp_pop_context (pfile);
943 token->type = CPP_EOF;
949 /* Only perform macro expansion (and therefore pasting) when not
950 skipping, or when skipping but in a directive. The only
951 directive where this could be true is #elif. A possible later
952 optimisation: get do_elif to clear skipping so we don't need the
953 directive test here. */
954 if (pfile->skipping && !pfile->state.in_directive)
959 if (token->flags & PASTE_LEFT)
960 paste_all_tokens (pfile, token);
962 if (token->type != CPP_NAME
963 || token->val.node->type != NT_MACRO
964 || pfile->state.prevent_expansion
965 || token->flags & NO_EXPAND)
968 /* Macros, built-in or not, invalidate controlling macros. */
969 pfile->mi_state = MI_FAILED;
971 if (token->val.node->flags & NODE_BUILTIN)
973 builtin_macro (pfile, token);
976 else if (enter_macro_context (pfile, token))
981 /* External interface to get a token. Tokens are returned after macro
982 expansion and directives have been handled, as a continuous stream.
983 Transparently enters included files. CPP_EOF indicates end of
984 original source file. Filters out CPP_PLACEMARKER tokens.
986 For the benefit of #pragma callbacks which may want to get the
987 pragma's tokens, returns CPP_EOF to indicate end-of-directive in
990 cpp_get_token (pfile, token)
996 _cpp_get_token (pfile, token);
998 if (token->type == CPP_EOF)
1000 /* In directives we should pass through EOLs for the callbacks. */
1001 if (pfile->buffer->cur == pfile->buffer->rlimit
1002 || pfile->state.in_directive || pfile->state.parsing_args)
1006 else if (token->type == CPP_DHASH)
1008 /* Handle directives. */
1009 if (_cpp_handle_directive (pfile, token->flags & PREV_WHITE))
1011 /* This is in fact an assembler #. */
1012 if (pfile->skipping)
1014 token->type = CPP_HASH;
1016 /* We are not merging the PREV_WHITE of CPP_PLACEMARKERS. I
1017 don't think it really matters. */
1018 else if (pfile->skipping || token->type == CPP_PLACEMARKER)
1021 /* Non-comment tokens invalidate any controlling macros. */
1022 if (token->type != CPP_COMMENT)
1023 pfile->mi_state = MI_FAILED;
1028 if (pfile->la_write)
1029 save_lookahead_token (pfile, token);
1032 /* Read each token in, until EOF. Directives are transparently
1035 cpp_scan_buffer_nooutput (pfile)
1042 cpp_get_token (pfile, &token);
1043 while (token.type != CPP_EOF);
1044 while (cpp_pop_buffer (pfile) != 0);
1047 /* Lookahead handling. */
1050 save_lookahead_token (pfile, token)
1052 const cpp_token *token;
1054 if (token->type != CPP_EOF)
1056 cpp_lookahead *la = pfile->la_write;
1057 cpp_token_with_pos *twp;
1059 if (la->count == la->cap)
1061 la->cap += la->cap + 8;
1062 la->tokens = (cpp_token_with_pos *)
1063 xrealloc (la->tokens, la->cap * sizeof (cpp_token_with_pos));
1066 twp = &la->tokens[la->count++];
1067 twp->token = *token;
1068 twp->pos = *cpp_get_line (pfile);
1073 take_lookahead_token (pfile, token)
1077 cpp_lookahead *la = pfile->la_read;
1078 cpp_token_with_pos *twp = &la->tokens[la->cur];
1080 *token = twp->token;
1081 pfile->lexer_pos = twp->pos;
1083 if (++la->cur == la->count)
1084 release_lookahead (pfile);
1087 /* Moves the lookahead at the front of the read list to the free store. */
1089 release_lookahead (pfile)
1092 cpp_lookahead *la = pfile->la_read;
1094 pfile->la_read = la->next;
1095 la->next = pfile->la_unused;
1096 pfile->la_unused = la;
1097 unlock_pools (pfile);
1100 /* Take a new lookahead from the free store, or allocate one if none. */
1101 static cpp_lookahead *
1102 alloc_lookahead (pfile)
1105 cpp_lookahead *la = pfile->la_unused;
1108 pfile->la_unused = la->next;
1111 la = xnew (cpp_lookahead);
1116 la->cur = la->count = 0;
1120 /* Free memory associated with a lookahead list. */
1126 free ((PTR) la->tokens);
1130 /* Free all the lookaheads of a cpp_reader. */
1132 _cpp_free_lookaheads (pfile)
1135 cpp_lookahead *la, *lan;
1138 free_lookahead (pfile->la_read);
1139 if (pfile->la_write)
1140 free_lookahead (pfile->la_write);
1142 for (la = pfile->la_unused; la; la = lan)
1145 free_lookahead (la);
1149 /* Allocate a lookahead and move it to the front of the write list. */
1151 cpp_start_lookahead (pfile)
1154 cpp_lookahead *la = alloc_lookahead (pfile);
1156 la->next = pfile->la_write;
1157 pfile->la_write = la;
1159 la->pos = *cpp_get_line (pfile);
1161 /* Don't allow memory pools to be re-used whilst we're reading ahead. */
1165 /* Stop reading ahead - either step back, or drop the read ahead. */
1167 cpp_stop_lookahead (pfile, drop)
1171 cpp_lookahead *la = pfile->la_write;
1173 pfile->la_write = la->next;
1174 la->next = pfile->la_read;
1175 pfile->la_read = la;
1177 if (drop || la->count == 0)
1178 release_lookahead (pfile);
1180 pfile->lexer_pos = la->pos;
1183 /* Push a single token back to the front of the queue. Only to be
1184 used by cpplib, and only then when necessary. POS is the position
1185 to report for the preceding token. */
1187 _cpp_push_token (pfile, token, pos)
1189 const cpp_token *token;
1190 const cpp_lexer_pos *pos;
1192 cpp_start_lookahead (pfile);
1193 save_lookahead_token (pfile, token);
1194 cpp_stop_lookahead (pfile, 0);
1195 pfile->lexer_pos = *pos;
1198 /* #define directive parsing and handling. */
1200 /* Returns non-zero if a macro redefinition is trivial. */
1202 check_macro_redefinition (pfile, node, macro2)
1204 const cpp_hashnode *node;
1205 const cpp_macro *macro2;
1207 const cpp_macro *macro1;
1210 if (node->type != NT_MACRO || node->flags & NODE_BUILTIN)
1211 return ! pfile->done_initializing;
1213 macro1 = node->value.macro;
1215 /* The quick failures. */
1216 if (macro1->count != macro2->count
1217 || macro1->paramc != macro2->paramc
1218 || macro1->fun_like != macro2->fun_like
1219 || macro1->var_args != macro2->var_args)
1222 /* Check each token. */
1223 for (i = 0; i < macro1->count; i++)
1224 if (! _cpp_equiv_tokens (¯o1->expansion[i], ¯o2->expansion[i]))
1227 /* Check parameter spellings. */
1228 for (i = 0; i < macro1->paramc; i++)
1229 if (macro1->params[i] != macro2->params[i])
1235 /* Free the definition of hashnode H. */
1238 _cpp_free_definition (h)
1241 /* Macros and assertions no longer have anything to free. */
1243 /* Clear builtin flag in case of redefinition. */
1244 h->flags &= ~NODE_BUILTIN;
1248 save_parameter (pfile, macro, node)
1253 cpp_hashnode **dest;
1255 /* Constraint 6.10.3.6 - duplicate parameter names. */
1256 if (node->arg_index)
1258 cpp_error (pfile, "duplicate macro parameter \"%s\"", node->name);
1262 dest = ¯o->params[macro->paramc];
1264 /* Check we have room for the parameters. */
1265 if ((unsigned char *) (dest + 1) >= POOL_LIMIT (&pfile->macro_pool))
1267 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_hashnode *),
1268 (unsigned char **) ¯o->params);
1269 dest = ¯o->params[macro->paramc];
1273 node->arg_index = ++macro->paramc;
1278 parse_params (pfile, macro)
1283 unsigned int prev_ident = 0;
1285 macro->params = (cpp_hashnode **) POOL_FRONT (&pfile->macro_pool);
1288 _cpp_lex_token (pfile, &token);
1293 cpp_error (pfile, "\"%s\" may not appear in macro parameter list",
1294 cpp_token_as_text (pfile, &token));
1300 cpp_error (pfile, "macro parameters must be comma-separated");
1305 if (save_parameter (pfile, macro, token.val.node))
1309 case CPP_CLOSE_PAREN:
1310 if (prev_ident || macro->paramc == 0)
1313 /* Fall through to pick up the error. */
1317 cpp_error (pfile, "parameter name missing");
1324 macro->var_args = 1;
1327 save_parameter (pfile, macro, pfile->spec_nodes.n__VA_ARGS__);
1328 pfile->state.va_args_ok = 1;
1329 if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
1331 "C89 does not permit anonymous variable arguments");
1333 else if (CPP_OPTION (pfile, pedantic))
1335 "ISO C does not permit named variable arguments");
1337 /* We're at the end, and just expect a closing parenthesis. */
1338 _cpp_lex_token (pfile, &token);
1339 if (token.type == CPP_CLOSE_PAREN)
1344 cpp_error (pfile, "missing ')' in macro parameter list");
1348 /* Success. Commit the parameter array. */
1349 POOL_COMMIT (&pfile->macro_pool,
1350 macro->paramc * sizeof (cpp_hashnode *));
1355 /* Lex a token from a macro's replacement list. Translate it to a
1356 CPP_MACRO_ARG if appropriate. */
1358 lex_expansion_token (pfile, macro)
1362 cpp_token *token = ¯o->expansion[macro->count];
1364 /* Check we have room for the token. */
1365 if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->macro_pool))
1367 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_token),
1368 (unsigned char **) ¯o->expansion);
1369 token = ¯o->expansion[macro->count];
1373 _cpp_lex_token (pfile, token);
1375 /* Is this an argument? */
1376 if (token->type == CPP_NAME && token->val.node->arg_index)
1378 token->type = CPP_MACRO_ARG;
1379 token->val.aux = token->val.node->arg_index;
1381 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1382 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1383 check_trad_stringification (pfile, macro, &token->val.str);
1388 /* Parse a macro and save its expansion. Returns non-zero on success. */
1390 _cpp_create_definition (pfile, node)
1396 unsigned int i, ok = 1;
1398 macro = (cpp_macro *) _cpp_pool_alloc (&pfile->macro_pool,
1399 sizeof (cpp_macro));
1400 macro->file = pfile->buffer->nominal_fname;
1401 macro->line = pfile->directive_pos.line;
1404 macro->fun_like = 0;
1405 macro->var_args = 0;
1407 macro->expansion = (cpp_token *) POOL_FRONT (&pfile->macro_pool);
1409 /* Get the first token of the expansion (or the '(' of a
1410 function-like macro). */
1411 token = lex_expansion_token (pfile, macro);
1412 if (token->type == CPP_OPEN_PAREN && !(token->flags & PREV_WHITE))
1414 if (!(ok = parse_params (pfile, macro)))
1417 macro->fun_like = 1;
1418 /* Some of the pool may have been used for the parameter store. */
1419 macro->expansion = (cpp_token *) POOL_FRONT (&pfile->macro_pool);
1420 token = lex_expansion_token (pfile, macro);
1422 else if (token->type != CPP_EOF && !(token->flags & PREV_WHITE))
1423 cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name");
1425 /* Setting it here means we don't catch leading comments. */
1426 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
1430 /* Check the stringifying # constraint 6.10.3.2.1 of
1431 function-like macros when lexing the subsequent token. */
1432 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1434 if (token->type == CPP_MACRO_ARG)
1436 token->flags &= ~PREV_WHITE;
1437 token->flags |= STRINGIFY_ARG;
1438 token->flags |= token[-1].flags & PREV_WHITE;
1439 token[-1] = token[0];
1442 /* Let assembler get away with murder. */
1443 else if (!CPP_OPTION (pfile, lang_asm))
1446 cpp_error (pfile, "'#' is not followed by a macro parameter");
1451 if (token->type == CPP_EOF)
1454 /* Paste operator constraint 6.10.3.3.1. */
1455 if (token->type == CPP_PASTE)
1457 /* Token-paste ##, can appear in both object-like and
1458 function-like macros, but not at the ends. */
1459 if (--macro->count > 0)
1460 token = lex_expansion_token (pfile, macro);
1462 if (macro->count == 0 || token->type == CPP_EOF)
1466 "'##' cannot appear at either end of a macro expansion");
1470 token[-1].flags |= PASTE_LEFT;
1471 /* Give it a PREV_WHITE for -dM etc. */
1472 token->flags |= PREV_WHITE;
1475 token = lex_expansion_token (pfile, macro);
1478 /* Don't count the CPP_EOF. Empty macros become a place marker. */
1479 if (macro->count > 1)
1482 macro->expansion[0].type = CPP_PLACEMARKER;
1484 /* Clear the whitespace flag from the leading token. */
1485 macro->expansion[0].flags &= ~PREV_WHITE;
1487 /* Implement the macro-defined-to-itself optimisation. */
1488 macro->disabled = (macro->count == 1 && !macro->fun_like
1489 && macro->expansion[0].type == CPP_NAME
1490 && macro->expansion[0].val.node == node);
1492 /* Commit the memory. */
1493 POOL_COMMIT (&pfile->macro_pool, macro->count * sizeof (cpp_token));
1495 /* Redefinition of a macro is allowed if and only if the old and new
1496 definitions are the same. (6.10.3 paragraph 2). */
1497 if (node->type != NT_VOID)
1499 if (CPP_PEDANTIC (pfile)
1500 && !check_macro_redefinition (pfile, node, macro))
1502 cpp_pedwarn_with_line (pfile, pfile->directive_pos.line,
1503 pfile->directive_pos.col,
1504 "\"%s\" redefined", node->name);
1506 if (pfile->done_initializing && node->type == NT_MACRO
1507 && !(node->flags & NODE_BUILTIN))
1508 cpp_pedwarn_with_file_and_line (pfile,
1509 node->value.macro->file,
1510 node->value.macro->line, 1,
1511 "this is the location of the previous definition");
1513 _cpp_free_definition (node);
1516 /* Enter definition in hash table. */
1517 node->type = NT_MACRO;
1518 node->value.macro = macro;
1522 /* Stop the lexer accepting __VA_ARGS__. */
1523 pfile->state.va_args_ok = 0;
1525 /* Clear the fast argument lookup indices. */
1526 for (i = macro->paramc; i-- > 0; )
1527 macro->params[i]->arg_index = 0;
1532 /* Warn if a token in `string' matches one of the function macro
1533 arguments in `info'. This function assumes that the macro is a
1534 function macro and not an object macro. */
1536 check_trad_stringification (pfile, macro, string)
1538 const cpp_macro *macro;
1539 const cpp_string *string;
1541 unsigned int i, len;
1542 const U_CHAR *p, *q, *limit = string->text + string->len;
1544 /* Loop over the string. */
1545 for (p = string->text; p < limit; p = q)
1547 /* Find the start of an identifier. */
1548 while (p < limit && !is_idstart (*p))
1551 /* Find the end of the identifier. */
1553 while (q < limit && is_idchar (*q))
1558 /* Loop over the function macro arguments to see if the
1559 identifier inside the string matches one of them. */
1560 for (i = 0; i < macro->paramc; i++)
1562 const cpp_hashnode *node = macro->params[i];
1564 if (node->length == len && !memcmp (p, node->name, len))
1567 "macro argument \"%s\" would be stringified with -traditional.",
1575 /* Returns the expansion of a macro, in a format suitable to be read
1576 back in again, and therefore also for DWARF 2 debugging info.
1577 Caller is expected to generate the "#define NAME" bit. The
1578 returned text is temporary, and automatically freed later. */
1580 const unsigned char *
1581 cpp_macro_definition (pfile, node)
1583 const cpp_hashnode *node;
1585 unsigned int i, len;
1586 const cpp_macro *macro = node->value.macro;
1587 unsigned char *buffer;
1589 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1591 cpp_ice (pfile, "invalid hash type %d in dump_definition", node->type);
1595 /* Calculate length. */
1597 if (macro->fun_like)
1599 len += 3; /* "()" plus possible final "." of ellipsis. */
1600 for (i = 0; i < macro->paramc; i++)
1601 len += macro->params[i]->length + 2; /* ", " */
1604 if (macro->count > 1 || macro->expansion[0].type != CPP_PLACEMARKER)
1606 for (i = 0; i < macro->count; i++)
1608 cpp_token *token = ¯o->expansion[i];
1610 if (token->type == CPP_MACRO_ARG)
1611 len += macro->params[token->val.aux - 1]->length;
1613 len += cpp_token_len (token); /* Includes room for ' '. */
1614 if (token->flags & STRINGIFY_ARG)
1616 if (token->flags & PASTE_LEFT)
1617 len += 3; /* " ##" */
1621 if (len > pfile->macro_buffer_len)
1622 pfile->macro_buffer = (U_CHAR *) xrealloc (pfile->macro_buffer, len);
1623 buffer = pfile->macro_buffer;
1625 /* Parameter names. */
1626 if (macro->fun_like)
1629 for (i = 0; i < macro->paramc; i++)
1631 cpp_hashnode *param = macro->params[i];
1633 if (param != pfile->spec_nodes.n__VA_ARGS__)
1635 memcpy (buffer, param->name, param->length);
1636 buffer += param->length;
1639 if (i + 1 < macro->paramc)
1640 *buffer++ = ',', *buffer++ = ' ';
1641 else if (macro->var_args)
1642 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1647 /* Expansion tokens. */
1648 if (macro->count > 1 || macro->expansion[0].type != CPP_PLACEMARKER)
1651 for (i = 0; i < macro->count; i++)
1653 cpp_token *token = ¯o->expansion[i];
1655 if (token->flags & PREV_WHITE)
1657 if (token->flags & STRINGIFY_ARG)
1660 if (token->type == CPP_MACRO_ARG)
1662 len = macro->params[token->val.aux - 1]->length;
1663 memcpy (buffer, macro->params[token->val.aux - 1]->name, len);
1667 buffer = cpp_spell_token (pfile, token, buffer);
1669 if (token->flags & PASTE_LEFT)
1674 /* Next has PREV_WHITE; see _cpp_create_definition. */
1680 return pfile->macro_buffer;