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. */
32 #ifndef STDC_0_IN_SYSTEM_HEADERS
33 #define STDC_0_IN_SYSTEM_HEADERS 0 /* Boolean macro. */
38 cpp_hashnode
**params
; /* Parameters, if any. */
39 cpp_token
*expansion
; /* First token of replacement list. */
40 const char *file
; /* Defined in file name. */
41 unsigned int line
; /* Starting line number. */
42 unsigned int count
; /* Number of tokens in expansion. */
43 unsigned short paramc
; /* Number of parameters. */
44 unsigned int fun_like
: 1; /* If a function-like macro. */
45 unsigned int var_args
: 1; /* If a variable-args macro. */
46 unsigned int disabled
: 1; /* If macro is disabled. */
49 typedef struct macro_arg macro_arg
;
52 cpp_token
*first
; /* First token in unexpanded argument. */
53 cpp_token
*expanded
; /* Macro-expanded argument. */
54 cpp_token
*stringified
; /* Stringified argument. */
55 unsigned int count
; /* # of tokens in argument. */
56 unsigned int expanded_count
; /* # of tokens in expanded argument. */
59 /* Macro expansion. */
61 static void lock_pools
PARAMS ((cpp_reader
*));
62 static void unlock_pools
PARAMS ((cpp_reader
*));
63 static int enter_macro_context
PARAMS ((cpp_reader
*, cpp_hashnode
*));
64 static void builtin_macro
PARAMS ((cpp_reader
*, cpp_token
*));
65 static cpp_context
*push_arg_context
PARAMS ((cpp_reader
*, macro_arg
*));
66 static enum cpp_ttype parse_arg
PARAMS ((cpp_reader
*, macro_arg
*, int));
67 static macro_arg
*parse_args
PARAMS ((cpp_reader
*, const cpp_hashnode
*));
68 static cpp_context
*next_context
PARAMS ((cpp_reader
*));
69 static void expand_arg
PARAMS ((cpp_reader
*, macro_arg
*));
70 static unsigned char *quote_string
PARAMS ((unsigned char *,
71 const unsigned char *,
73 static void make_string_token
PARAMS ((cpp_pool
*, cpp_token
*,
74 const U_CHAR
*, unsigned int));
75 static void make_number_token
PARAMS ((cpp_reader
*, cpp_token
*, int));
76 static void stringify_arg
PARAMS ((cpp_reader
*, macro_arg
*));
77 static void paste_all_tokens
PARAMS ((cpp_reader
*, cpp_token
*));
78 static void paste_payloads
PARAMS ((cpp_reader
*, cpp_token
*,
80 static int funlike_invocation_p
PARAMS ((cpp_reader
*, const cpp_hashnode
*,
82 static void replace_args
PARAMS ((cpp_reader
*, cpp_macro
*, macro_arg
*,
87 static void save_lookahead_token
PARAMS ((cpp_reader
*, const cpp_token
*));
88 static void take_lookahead_token
PARAMS ((cpp_reader
*, cpp_token
*));
89 static cpp_lookahead
*alloc_lookahead
PARAMS ((cpp_reader
*));
90 static void free_lookahead
PARAMS ((cpp_lookahead
*));
92 /* #define directive parsing and handling. */
94 static cpp_token
*lex_expansion_token
PARAMS ((cpp_reader
*, cpp_macro
*));
95 static int check_macro_redefinition
PARAMS ((cpp_reader
*,
98 static int save_parameter
PARAMS ((cpp_reader
*, cpp_macro
*, cpp_hashnode
*));
99 static int parse_params
PARAMS ((cpp_reader
*, cpp_macro
*));
100 static void check_trad_stringification
PARAMS ((cpp_reader
*,
102 const cpp_string
*));
104 /* Allocates a buffer to hold a token's TEXT, and converts TOKEN to a
105 CPP_STRING token containing TEXT in quoted form. */
107 make_string_token (pool
, token
, text
, len
)
113 U_CHAR
*buf
= _cpp_pool_alloc (pool
, len
* 4);
115 token
->type
= CPP_STRING
;
116 token
->val
.str
.text
= buf
;
117 token
->val
.str
.len
= quote_string (buf
, text
, len
) - buf
;
121 /* Allocates and converts a temporary token to a CPP_NUMBER token,
122 evaluating to NUMBER. */
124 make_number_token (pfile
, token
, number
)
129 unsigned char *buf
= _cpp_pool_alloc (pfile
->string_pool
, 20);
131 sprintf ((char *) buf
, "%d", number
);
132 token
->type
= CPP_NUMBER
;
133 token
->val
.str
.text
= buf
;
134 token
->val
.str
.len
= ustrlen (buf
);
138 static const char * const monthnames
[] =
140 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
141 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
144 /* Handle builtin macros like __FILE__. */
146 builtin_macro (pfile
, token
)
150 unsigned char flags
= token
->flags
& PREV_WHITE
;
151 cpp_hashnode
*node
= token
->val
.node
;
154 switch (node
->value
.builtin
)
161 ip
= CPP_BUFFER (pfile
);
166 if (node
->value
.builtin
== BT_BASE_FILE
)
167 while (CPP_PREV_BUFFER (ip
) != NULL
)
168 ip
= CPP_PREV_BUFFER (ip
);
170 file
= ip
->nominal_fname
;
172 make_string_token (pfile
->string_pool
, token
,
173 (const U_CHAR
*) file
, strlen (file
));
177 case BT_INCLUDE_LEVEL
:
178 /* pfile->include_depth counts the primary source as level 1,
179 but historically __INCLUDE_DEPTH__ has called the primary
181 make_number_token (pfile
, token
, pfile
->include_depth
- 1);
185 /* If __LINE__ is embedded in a macro, it must expand to the
186 line of the macro's invocation, not its definition.
187 Otherwise things like assert() will not work properly. */
188 make_number_token (pfile
, token
, cpp_get_line (pfile
)->line
);
195 if (STDC_0_IN_SYSTEM_HEADERS
&& CPP_IN_SYSTEM_HEADER (pfile
)
196 && pfile
->spec_nodes
.n__STRICT_ANSI__
->type
== NT_VOID
)
198 make_number_token (pfile
, token
, stdc
);
204 if (pfile
->date
.type
== CPP_EOF
)
206 /* Allocate __DATE__ and __TIME__ from permanent storage,
207 and save them in pfile so we don't have to do this again.
208 We don't generate these strings at init time because
209 time() and localtime() are very slow on some systems. */
210 time_t tt
= time (NULL
);
211 struct tm
*tb
= localtime (&tt
);
213 make_string_token (&pfile
->ident_pool
, &pfile
->date
,
215 make_string_token (&pfile
->ident_pool
, &pfile
->time
,
218 sprintf ((char *) pfile
->date
.val
.str
.text
, "%s %2d %4d",
219 monthnames
[tb
->tm_mon
], tb
->tm_mday
, tb
->tm_year
+ 1900);
220 sprintf ((char *) pfile
->time
.val
.str
.text
, "%02d:%02d:%02d",
221 tb
->tm_hour
, tb
->tm_min
, tb
->tm_sec
);
223 *token
= node
->value
.builtin
== BT_DATE
? pfile
->date
: pfile
->time
;
227 cpp_ice (pfile
, "invalid builtin macro \"%s\"", node
->name
);
231 token
->flags
= flags
;
234 /* Used by cpperror.c to obtain the correct line and column to report
236 const cpp_lexer_pos
*
240 /* Within a macro expansion, return the position of the outermost
242 if (pfile
->context
->prev
)
243 return &pfile
->macro_pos
;
244 return &pfile
->lexer_pos
;
251 _cpp_lock_pool (&pfile
->temp_string_pool
);
252 _cpp_lock_pool (&pfile
->argument_pool
);
259 _cpp_unlock_pool (&pfile
->temp_string_pool
);
260 _cpp_unlock_pool (&pfile
->argument_pool
);
264 paste_payloads (pfile
, lhs
, rhs
)
267 const cpp_token
*rhs
;
269 unsigned int total_len
= cpp_token_len (lhs
) + cpp_token_len (rhs
);
270 unsigned char *result
, *end
;
273 pool
= lhs
->type
== CPP_NAME
? &pfile
->ident_pool
: pfile
->string_pool
;
274 result
= _cpp_pool_alloc (pool
, total_len
+ 1);
276 /* Paste the spellings and null terminate. */
277 end
= cpp_spell_token (pfile
, rhs
, cpp_spell_token (pfile
, lhs
, result
));
279 total_len
= end
- result
;
281 if (lhs
->type
== CPP_NAME
)
283 lhs
->val
.node
= cpp_lookup (pfile
, result
, total_len
);
284 if (lhs
->val
.node
->flags
& NODE_OPERATOR
)
286 lhs
->flags
|= NAMED_OP
;
287 lhs
->type
= lhs
->val
.node
->value
.operator;
292 lhs
->val
.str
.text
= result
;
293 lhs
->val
.str
.len
= total_len
;
297 /* Adds backslashes before all backslashes and double quotes appearing
298 in strings. Non-printable characters are converted to octal. */
300 quote_string (dest
, src
, len
)
309 if (c
== '\\' || c
== '"')
320 sprintf ((char *) dest
, "\\%03o", c
);
329 /* Convert a token sequence to a single string token according to the
330 rules of the ISO C #-operator. */
332 stringify_arg (pfile
, arg
)
336 cpp_pool
*pool
= pfile
->string_pool
;
337 unsigned char *start
= POOL_FRONT (pool
);
338 unsigned int i
, escape_it
, total_len
= 0, backslash_count
= 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 /* No leading white space. */
364 if (token
->flags
& PREV_WHITE
&& total_len
> 0)
369 unsigned char *buf
= (unsigned char *) xmalloc (len
);
371 len
= cpp_spell_token (pfile
, token
, buf
) - buf
;
372 dest
= quote_string (dest
, buf
, len
);
376 dest
= cpp_spell_token (pfile
, token
, dest
);
377 total_len
= dest
- start
;
379 if (token
->type
== CPP_OTHER
&& token
->val
.c
== '\\')
385 /* Ignore the final \ of invalid string literals. */
386 if (backslash_count
& 1)
388 cpp_warning (pfile
, "invalid string literal, ignoring final '\\'");
392 POOL_COMMIT (pool
, total_len
);
394 arg
->stringified
= xnew (cpp_token
);
395 arg
->stringified
->flags
= 0;
396 arg
->stringified
->type
= CPP_STRING
;
397 arg
->stringified
->val
.str
.text
= start
;
398 arg
->stringified
->val
.str
.len
= total_len
;
401 /* Handles an arbitrarily long sequence of ## operators. This
402 implementation is left-associative, non-recursive, and finishes a
403 paste before handling succeeding ones. If the paste fails, the
404 right hand side of the ## operator is placed in the then-current
405 context's lookahead buffer, with the effect that it appears in the
406 output stream normally. */
408 paste_all_tokens (pfile
, lhs
)
412 unsigned char orig_flags
= lhs
->flags
;
420 /* Take the token directly from the current context. We can do
421 this, because we are in the replacement list of either an
422 object-like macro, or a function-like macro with arguments
423 inserted. In either case, the constraints to #define
424 guarantee we have at least one more token (empty arguments
425 become placemarkers). */
426 rhs
= pfile
->context
->list
.first
++;
428 type
= cpp_can_paste (pfile
, lhs
, rhs
, &digraph
);
431 if (CPP_OPTION (pfile
, warn_paste
))
433 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
434 cpp_token_as_text (pfile
, lhs
),
435 cpp_token_as_text (pfile
, rhs
));
437 /* The standard states that behaviour is undefined. By the
438 principle of least surpise, we step back before the RHS,
439 and mark it to prevent macro expansion. Tests in the
440 testsuite rely on clearing PREV_WHITE here, though you
441 could argue we should actually set it. */
442 rhs
->flags
&= ~PREV_WHITE
;
443 rhs
->flags
|= NO_EXPAND
;
445 /* Step back so we read the RHS in next. */
446 pfile
->context
->list
.first
--;
451 lhs
->flags
&= ~DIGRAPH
;
453 lhs
->flags
|= DIGRAPH
;
455 if (type
== CPP_NAME
|| type
== CPP_NUMBER
)
456 paste_payloads (pfile
, lhs
, rhs
);
457 else if (type
== CPP_WCHAR
|| type
== CPP_WSTRING
)
458 lhs
->val
.str
= rhs
->val
.str
;
460 while (rhs
->flags
& PASTE_LEFT
);
462 /* The pasted token has the PREV_WHITE flag of the LHS, is no longer
463 PASTE_LEFT, and is subject to macro expansion. */
464 lhs
->flags
&= ~(PREV_WHITE
| PASTE_LEFT
| NO_EXPAND
);
465 lhs
->flags
|= orig_flags
& PREV_WHITE
;
468 /* Reads the unexpanded tokens of a macro argument into ARG. VAR_ARGS
469 is non-zero if this is a variable argument. Returns the type of
470 the token that caused reading to finish. */
471 static enum cpp_ttype
472 parse_arg (pfile
, arg
, var_args
)
474 struct macro_arg
*arg
;
477 enum cpp_ttype result
;
478 unsigned int paren
= 0;
480 arg
->first
= (cpp_token
*) POOL_FRONT (&pfile
->argument_pool
);
481 for (;; arg
->count
++)
483 cpp_token
*token
= &arg
->first
[arg
->count
];
484 if ((unsigned char *) (token
+ 1) >= POOL_LIMIT (&pfile
->argument_pool
))
486 _cpp_next_chunk (&pfile
->argument_pool
, sizeof (cpp_token
),
487 (unsigned char **) &arg
->first
);
488 token
= &arg
->first
[arg
->count
];
491 cpp_get_token (pfile
, token
);
492 result
= token
->type
;
494 if (result
== CPP_OPEN_PAREN
)
496 else if (result
== CPP_CLOSE_PAREN
&& paren
-- == 0)
498 /* Commas are not terminators within parantheses or var_args. */
499 else if (result
== CPP_COMMA
&& paren
== 0 && !var_args
)
501 else if (result
== CPP_EOF
)
502 break; /* Error reported by caller. */
505 /* Commit the memory used to store the arguments. */
506 POOL_COMMIT (&pfile
->argument_pool
, arg
->count
* sizeof (cpp_token
));
511 /* Parse the arguments making up a macro invocation. */
513 parse_args (pfile
, node
)
515 const cpp_hashnode
*node
;
517 cpp_macro
*macro
= node
->value
.macro
;
518 macro_arg
*args
, *cur
;
522 /* Allocate room for at least one argument, and zero it out. */
523 argc
= macro
->paramc
? macro
->paramc
: 1;
524 args
= xcnewvec (macro_arg
, argc
);
526 for (cur
= args
, argc
= 0; ;)
530 type
= parse_arg (pfile
, cur
, argc
== macro
->paramc
&& macro
->var_args
);
531 if (type
== CPP_CLOSE_PAREN
|| type
== CPP_EOF
)
534 /* Re-use the last argument for excess arguments. */
535 if (argc
< macro
->paramc
)
541 cpp_error (pfile
, "unterminated argument list invoking macro \"%s\"",
545 else if (argc
< macro
->paramc
)
547 /* As an extension, a rest argument is allowed to not appear in
548 the invocation at all.
549 e.g. #define debug(format, args...) something
552 This is exactly the same as if there had been an empty rest
553 argument - debug("string", ). */
555 if (argc
+ 1 == macro
->paramc
&& macro
->var_args
)
557 if (CPP_OPTION (pfile
, c99
) && CPP_PEDANTIC (pfile
))
558 cpp_pedwarn (pfile
, "ISO C99 requires rest arguments to be used");
563 "macro \"%s\" requires %u arguments, but only %u given",
564 node
->name
, macro
->paramc
, argc
);
568 else if (argc
> macro
->paramc
)
570 /* Empty argument to a macro taking no arguments is OK. */
571 if (argc
!= 1 || cur
->count
)
574 "macro \"%s\" passed %u arguments, but takes just %u",
575 node
->name
, argc
, macro
->paramc
);
590 funlike_invocation_p (pfile
, node
, list
)
592 const cpp_hashnode
*node
;
593 struct toklist
*list
;
595 cpp_context
*orig_context
;
596 cpp_token maybe_paren
;
599 pfile
->state
.parsing_args
= 1;
600 pfile
->state
.prevent_expansion
++;
601 orig_context
= pfile
->context
;
603 cpp_start_lookahead (pfile
);
604 cpp_get_token (pfile
, &maybe_paren
);
605 cpp_stop_lookahead (pfile
, maybe_paren
.type
== CPP_OPEN_PAREN
);
607 if (maybe_paren
.type
== CPP_OPEN_PAREN
)
608 args
= parse_args (pfile
, node
);
609 else if (CPP_WTRADITIONAL (pfile
))
611 "function-like macro \"%s\" must be used with arguments in traditional C",
614 /* Restore original context. */
615 pfile
->context
= orig_context
;
616 pfile
->state
.prevent_expansion
--;
617 pfile
->state
.parsing_args
= 0;
621 if (node
->value
.macro
->paramc
> 0)
623 /* Don't save tokens during pre-expansion. */
624 struct cpp_lookahead
*la_saved
= pfile
->la_write
;
626 replace_args (pfile
, node
->value
.macro
, args
, list
);
627 pfile
->la_write
= la_saved
;
635 /* Push the context of a macro onto the context stack. TOKEN is the
636 macro name. If we can successfully start expanding the macro,
637 TOKEN is replaced with the first token of the expansion, and we
640 enter_macro_context (pfile
, node
)
644 cpp_context
*context
;
645 cpp_macro
*macro
= node
->value
.macro
;
648 /* Save the position of the outermost macro invocation. */
649 if (!pfile
->context
->prev
)
651 pfile
->macro_pos
= pfile
->lexer_pos
;
655 if (macro
->fun_like
&& !funlike_invocation_p (pfile
, node
, &list
))
657 if (!pfile
->context
->prev
)
658 unlock_pools (pfile
);
662 if (macro
->paramc
== 0)
664 list
.first
= macro
->expansion
;
665 list
.limit
= macro
->expansion
+ macro
->count
;
668 if (list
.first
!= list
.limit
)
670 /* Push its context. */
671 context
= next_context (pfile
);
672 context
->list
= list
;
673 context
->macro
= macro
;
675 /* Disable the macro within its expansion. */
682 /* Move to the next context. Create one if there is none. */
687 cpp_context
*prev
= pfile
->context
;
688 cpp_context
*result
= prev
->next
;
692 result
= xnew (cpp_context
);
698 pfile
->context
= result
;
703 replace_args (pfile
, macro
, args
, list
)
707 struct toklist
*list
;
709 unsigned int i
, total
;
710 const cpp_token
*src
, *limit
;
714 src
= macro
->expansion
;
715 limit
= src
+ macro
->count
;
717 /* First, fully macro-expand arguments, calculating the number of
718 tokens in the final expansion as we go. This ensures that the
719 possible recursive use of argument_pool is fine. */
721 for (; src
< limit
; src
++)
722 if (src
->type
== CPP_MACRO_ARG
)
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 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;
740 arg
->expanded_count
= 0;
742 expand_arg (pfile
, arg
);
744 total
+= arg
->expanded_count
- 1;
748 dest
= (cpp_token
*) _cpp_pool_alloc (&pfile
->argument_pool
,
749 total
* sizeof (cpp_token
));
752 for (src
= macro
->expansion
; src
< limit
; src
++)
753 if (src
->type
== CPP_MACRO_ARG
)
756 const cpp_token
*from
;
758 arg
= &args
[src
->val
.arg_no
- 1];
759 if (src
->flags
& STRINGIFY_ARG
)
760 from
= arg
->stringified
, count
= 1;
761 else if (src
->flags
& PASTE_LEFT
)
762 count
= arg
->count
, from
= arg
->first
;
763 else if (src
> macro
->expansion
&& (src
[-1].flags
& PASTE_LEFT
))
765 count
= arg
->count
, from
= arg
->first
;
766 if (dest
!= list
->first
)
768 /* GCC has special semantics for , ## b where b is a
769 varargs parameter: the comma disappears if b was
770 given no actual arguments (not merely if b is an
771 empty argument); otherwise pasting is turned off. */
772 if (dest
[-1].type
== CPP_COMMA
774 && src
->val
.arg_no
== macro
->paramc
)
779 dest
[-1].flags
&= ~PASTE_LEFT
;
781 /* Count == 0 is the RHS a placemarker case. */
783 dest
[-1].flags
&= ~PASTE_LEFT
;
787 count
= arg
->expanded_count
, from
= arg
->expanded
;
789 /* Count == 0 is the LHS a placemarker case. */
792 memcpy (dest
, from
, count
* sizeof (cpp_token
));
794 /* The first token gets PREV_WHITE of the CPP_MACRO_ARG. */
795 dest
->flags
&= ~PREV_WHITE
;
796 dest
->flags
|= src
->flags
& PREV_WHITE
;
798 /* The last token gets the PASTE_LEFT of the CPP_MACRO_ARG. */
799 dest
[count
- 1].flags
|= src
->flags
& PASTE_LEFT
;
809 /* Free the expanded arguments. */
810 for (i
= 0; i
< macro
->paramc
; i
++)
812 if (args
[i
].expanded
)
813 free (args
[i
].expanded
);
814 if (args
[i
].stringified
)
815 free (args
[i
].stringified
);
819 /* Subroutine of expand_arg to put the unexpanded tokens on the
822 push_arg_context (pfile
, arg
)
826 cpp_context
*context
= next_context (pfile
);
828 context
->list
.first
= arg
->first
;
829 context
->list
.limit
= arg
->first
+ arg
->count
;
835 expand_arg (pfile
, arg
)
840 unsigned int capacity
= 256;
842 /* Loop, reading in the arguments. */
843 arg
->expanded
= (cpp_token
*) xmalloc (capacity
* sizeof (cpp_token
));
845 push_arg_context (pfile
, arg
);
848 if (arg
->expanded_count
>= capacity
)
851 arg
->expanded
= (cpp_token
*)
852 xrealloc (arg
->expanded
, capacity
* sizeof (cpp_token
));
854 token
= &arg
->expanded
[arg
->expanded_count
++];
855 cpp_get_token (pfile
, token
);
857 while (token
->type
!= CPP_EOF
);
859 arg
->expanded_count
--;
861 /* Pop the context we pushed. */
862 pfile
->context
= pfile
->context
->prev
;
866 _cpp_pop_context (pfile
)
869 cpp_context
*context
= pfile
->context
;
871 pfile
->context
= context
->prev
;
872 /* Re-enable a macro and free resources when leaving its expansion. */
873 if (!pfile
->state
.parsing_args
)
875 if (!pfile
->context
->prev
)
876 unlock_pools (pfile
);
877 context
->macro
->disabled
= 0;
881 /* Eternal routine to get a token. Also used nearly everywhere
882 internally, except for places where we know we can safely call
883 the lexer directly, such as lexing a directive name.
885 Macro expansions and directives are transparently handled,
886 including entering included files. Thus tokens are post-macro
887 expansion, and after any intervening directives. External callers
888 see CPP_EOF only at EOF. Internal callers also see it when meeting
889 a directive inside a macro call, when at the end of a directive and
890 state.in_directive is still 1, and at the end of argument
893 cpp_get_token (pfile
, token
)
897 unsigned char flags
= 0;
901 cpp_context
*context
= pfile
->context
;
904 take_lookahead_token (pfile
, token
);
905 /* Context->prev == 0 <=> base context. */
906 else if (!context
->prev
)
907 _cpp_lex_token (pfile
, token
);
908 else if (context
->list
.first
!= context
->list
.limit
)
910 *token
= *context
->list
.first
++;
911 token
->flags
|= flags
;
913 /* PASTE_LEFT tokens can only appear in macro expansions. */
914 if (token
->flags
& PASTE_LEFT
&& !pfile
->skipping
)
915 paste_all_tokens (pfile
, token
);
921 _cpp_pop_context (pfile
);
924 /* End of argument pre-expansion. */
925 token
->type
= CPP_EOF
;
930 /* Loop until we're not skipping. */
934 if (token
->type
!= CPP_NAME
)
937 /* Handle macros and the _Pragma operator. */
938 if (token
->val
.node
->type
== NT_MACRO
939 && !pfile
->state
.prevent_expansion
940 && !(token
->flags
& NO_EXPAND
))
942 cpp_hashnode
*node
= token
->val
.node
;
944 /* Macros invalidate controlling macros. */
945 pfile
->mi_state
= MI_FAILED
;
947 if (node
->flags
& NODE_BUILTIN
)
949 builtin_macro (pfile
, token
);
953 /* Merge PREV_WHITE of tokens. */
954 flags
= token
->flags
& PREV_WHITE
;
956 if (node
->value
.macro
->disabled
)
957 token
->flags
|= NO_EXPAND
;
958 else if (enter_macro_context (pfile
, node
))
962 /* Don't interpret _Pragma within directives. The standard is
963 not clear on this, but to me this makes most sense. */
964 if (token
->val
.node
!= pfile
->spec_nodes
.n__Pragma
965 || pfile
->state
.in_directive
)
968 /* Handle it, and loop back for another token. MI is cleared
969 since this token came from either the lexer or a macro. */
970 _cpp_do__Pragma (pfile
);
974 save_lookahead_token (pfile
, token
);
977 /* Read each token in, until EOF. Directives are transparently
980 cpp_scan_buffer_nooutput (pfile
)
987 cpp_get_token (pfile
, &token
);
988 while (token
.type
!= CPP_EOF
);
989 while (cpp_pop_buffer (pfile
) != 0);
992 /* Lookahead handling. */
995 save_lookahead_token (pfile
, token
)
997 const cpp_token
*token
;
999 if (token
->type
!= CPP_EOF
)
1001 cpp_lookahead
*la
= pfile
->la_write
;
1002 cpp_token_with_pos
*twp
;
1004 if (la
->count
== la
->cap
)
1006 la
->cap
+= la
->cap
+ 8;
1007 la
->tokens
= (cpp_token_with_pos
*)
1008 xrealloc (la
->tokens
, la
->cap
* sizeof (cpp_token_with_pos
));
1011 twp
= &la
->tokens
[la
->count
++];
1012 twp
->token
= *token
;
1013 twp
->pos
= *cpp_get_line (pfile
);
1018 take_lookahead_token (pfile
, token
)
1022 cpp_lookahead
*la
= pfile
->la_read
;
1023 cpp_token_with_pos
*twp
= &la
->tokens
[la
->cur
];
1025 *token
= twp
->token
;
1026 pfile
->lexer_pos
= twp
->pos
;
1028 if (++la
->cur
== la
->count
)
1029 _cpp_release_lookahead (pfile
);
1032 /* Moves the lookahead at the front of the read list to the free store. */
1034 _cpp_release_lookahead (pfile
)
1037 cpp_lookahead
*la
= pfile
->la_read
;
1039 pfile
->la_read
= la
->next
;
1040 la
->next
= pfile
->la_unused
;
1041 pfile
->la_unused
= la
;
1042 unlock_pools (pfile
);
1045 /* Take a new lookahead from the free store, or allocate one if none. */
1046 static cpp_lookahead
*
1047 alloc_lookahead (pfile
)
1050 cpp_lookahead
*la
= pfile
->la_unused
;
1053 pfile
->la_unused
= la
->next
;
1056 la
= xnew (cpp_lookahead
);
1061 la
->cur
= la
->count
= 0;
1065 /* Free memory associated with a lookahead list. */
1071 free ((PTR
) la
->tokens
);
1075 /* Free all the lookaheads of a cpp_reader. */
1077 _cpp_free_lookaheads (pfile
)
1080 cpp_lookahead
*la
, *lan
;
1083 free_lookahead (pfile
->la_read
);
1084 if (pfile
->la_write
)
1085 free_lookahead (pfile
->la_write
);
1087 for (la
= pfile
->la_unused
; la
; la
= lan
)
1090 free_lookahead (la
);
1094 /* Allocate a lookahead and move it to the front of the write list. */
1096 cpp_start_lookahead (pfile
)
1099 cpp_lookahead
*la
= alloc_lookahead (pfile
);
1101 la
->next
= pfile
->la_write
;
1102 pfile
->la_write
= la
;
1104 la
->pos
= *cpp_get_line (pfile
);
1106 /* Don't allow memory pools to be re-used whilst we're reading ahead. */
1110 /* Stop reading ahead - either step back, or drop the read ahead. */
1112 cpp_stop_lookahead (pfile
, drop
)
1116 cpp_lookahead
*la
= pfile
->la_write
;
1118 pfile
->la_write
= la
->next
;
1119 la
->next
= pfile
->la_read
;
1120 pfile
->la_read
= la
;
1122 if (drop
|| la
->count
== 0)
1123 _cpp_release_lookahead (pfile
);
1125 pfile
->lexer_pos
= la
->pos
;
1128 /* Push a single token back to the front of the queue. Only to be
1129 used by cpplib, and only then when necessary. POS is the position
1130 to report for the preceding token. */
1132 _cpp_push_token (pfile
, token
, pos
)
1134 const cpp_token
*token
;
1135 const cpp_lexer_pos
*pos
;
1137 cpp_start_lookahead (pfile
);
1138 save_lookahead_token (pfile
, token
);
1139 cpp_stop_lookahead (pfile
, 0);
1140 pfile
->lexer_pos
= *pos
;
1143 /* #define directive parsing and handling. */
1145 /* Returns non-zero if a macro redefinition is trivial. */
1147 check_macro_redefinition (pfile
, node
, macro2
)
1149 const cpp_hashnode
*node
;
1150 const cpp_macro
*macro2
;
1152 const cpp_macro
*macro1
;
1155 if (node
->type
!= NT_MACRO
|| node
->flags
& NODE_BUILTIN
)
1156 return ! pfile
->done_initializing
;
1158 macro1
= node
->value
.macro
;
1160 /* The quick failures. */
1161 if (macro1
->count
!= macro2
->count
1162 || macro1
->paramc
!= macro2
->paramc
1163 || macro1
->fun_like
!= macro2
->fun_like
1164 || macro1
->var_args
!= macro2
->var_args
)
1167 /* Check each token. */
1168 for (i
= 0; i
< macro1
->count
; i
++)
1169 if (! _cpp_equiv_tokens (¯o1
->expansion
[i
], ¯o2
->expansion
[i
]))
1172 /* Check parameter spellings. */
1173 for (i
= 0; i
< macro1
->paramc
; i
++)
1174 if (macro1
->params
[i
] != macro2
->params
[i
])
1180 /* Free the definition of hashnode H. */
1183 _cpp_free_definition (h
)
1186 /* Macros and assertions no longer have anything to free. */
1188 /* Clear builtin flag in case of redefinition. */
1189 h
->flags
&= ~NODE_BUILTIN
;
1193 save_parameter (pfile
, macro
, node
)
1198 cpp_hashnode
**dest
;
1200 /* Constraint 6.10.3.6 - duplicate parameter names. */
1201 if (node
->arg_index
)
1203 cpp_error (pfile
, "duplicate macro parameter \"%s\"", node
->name
);
1207 dest
= ¯o
->params
[macro
->paramc
];
1209 /* Check we have room for the parameters. */
1210 if ((unsigned char *) (dest
+ 1) >= POOL_LIMIT (&pfile
->macro_pool
))
1212 _cpp_next_chunk (&pfile
->macro_pool
, sizeof (cpp_hashnode
*),
1213 (unsigned char **) ¯o
->params
);
1214 dest
= ¯o
->params
[macro
->paramc
];
1218 node
->arg_index
= ++macro
->paramc
;
1223 parse_params (pfile
, macro
)
1228 unsigned int prev_ident
= 0;
1230 macro
->params
= (cpp_hashnode
**) POOL_FRONT (&pfile
->macro_pool
);
1233 _cpp_lex_token (pfile
, &token
);
1238 cpp_error (pfile
, "\"%s\" may not appear in macro parameter list",
1239 cpp_token_as_text (pfile
, &token
));
1245 cpp_error (pfile
, "macro parameters must be comma-separated");
1250 if (save_parameter (pfile
, macro
, token
.val
.node
))
1254 case CPP_CLOSE_PAREN
:
1255 if (prev_ident
|| macro
->paramc
== 0)
1258 /* Fall through to pick up the error. */
1262 cpp_error (pfile
, "parameter name missing");
1269 macro
->var_args
= 1;
1272 save_parameter (pfile
, macro
, pfile
->spec_nodes
.n__VA_ARGS__
);
1273 pfile
->state
.va_args_ok
= 1;
1274 if (! CPP_OPTION (pfile
, c99
) && CPP_OPTION (pfile
, pedantic
))
1276 "C89 does not permit anonymous variable arguments");
1278 else if (CPP_OPTION (pfile
, pedantic
))
1280 "ISO C does not permit named variable arguments");
1282 /* We're at the end, and just expect a closing parenthesis. */
1283 _cpp_lex_token (pfile
, &token
);
1284 if (token
.type
== CPP_CLOSE_PAREN
)
1289 cpp_error (pfile
, "missing ')' in macro parameter list");
1293 /* Success. Commit the parameter array. */
1294 POOL_COMMIT (&pfile
->macro_pool
,
1295 macro
->paramc
* sizeof (cpp_hashnode
*));
1300 /* Lex a token from a macro's replacement list. Translate it to a
1301 CPP_MACRO_ARG if appropriate. */
1303 lex_expansion_token (pfile
, macro
)
1307 cpp_token
*token
= ¯o
->expansion
[macro
->count
];
1309 /* Check we have room for the token. */
1310 if ((unsigned char *) (token
+ 1) >= POOL_LIMIT (&pfile
->macro_pool
))
1312 _cpp_next_chunk (&pfile
->macro_pool
, sizeof (cpp_token
),
1313 (unsigned char **) ¯o
->expansion
);
1314 token
= ¯o
->expansion
[macro
->count
];
1318 _cpp_lex_token (pfile
, token
);
1320 /* Is this an argument? */
1321 if (token
->type
== CPP_NAME
&& token
->val
.node
->arg_index
)
1323 token
->type
= CPP_MACRO_ARG
;
1324 token
->val
.arg_no
= token
->val
.node
->arg_index
;
1326 else if (CPP_WTRADITIONAL (pfile
) && macro
->paramc
> 0
1327 && (token
->type
== CPP_STRING
|| token
->type
== CPP_CHAR
))
1328 check_trad_stringification (pfile
, macro
, &token
->val
.str
);
1333 /* Parse a macro and save its expansion. Returns non-zero on success. */
1335 _cpp_create_definition (pfile
, node
)
1341 unsigned int i
, ok
= 1;
1343 macro
= (cpp_macro
*) _cpp_pool_alloc (&pfile
->macro_pool
,
1344 sizeof (cpp_macro
));
1345 macro
->file
= pfile
->buffer
->nominal_fname
;
1346 macro
->line
= pfile
->directive_pos
.line
;
1349 macro
->fun_like
= 0;
1350 macro
->var_args
= 0;
1352 macro
->expansion
= (cpp_token
*) POOL_FRONT (&pfile
->macro_pool
);
1354 /* Get the first token of the expansion (or the '(' of a
1355 function-like macro). */
1356 token
= lex_expansion_token (pfile
, macro
);
1357 if (token
->type
== CPP_OPEN_PAREN
&& !(token
->flags
& PREV_WHITE
))
1359 if (!(ok
= parse_params (pfile
, macro
)))
1362 macro
->fun_like
= 1;
1363 /* Some of the pool may have been used for the parameter store. */
1364 macro
->expansion
= (cpp_token
*) POOL_FRONT (&pfile
->macro_pool
);
1365 token
= lex_expansion_token (pfile
, macro
);
1367 else if (token
->type
!= CPP_EOF
&& !(token
->flags
& PREV_WHITE
))
1368 cpp_pedwarn (pfile
, "ISO C requires whitespace after the macro name");
1370 /* Setting it here means we don't catch leading comments. */
1371 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
1375 /* Check the stringifying # constraint 6.10.3.2.1 of
1376 function-like macros when lexing the subsequent token. */
1377 if (macro
->count
> 1 && token
[-1].type
== CPP_HASH
&& macro
->fun_like
)
1379 if (token
->type
== CPP_MACRO_ARG
)
1381 token
->flags
&= ~PREV_WHITE
;
1382 token
->flags
|= STRINGIFY_ARG
;
1383 token
->flags
|= token
[-1].flags
& PREV_WHITE
;
1384 token
[-1] = token
[0];
1387 /* Let assembler get away with murder. */
1388 else if (!CPP_OPTION (pfile
, lang_asm
))
1391 cpp_error (pfile
, "'#' is not followed by a macro parameter");
1396 if (token
->type
== CPP_EOF
)
1399 /* Paste operator constraint 6.10.3.3.1. */
1400 if (token
->type
== CPP_PASTE
)
1402 /* Token-paste ##, can appear in both object-like and
1403 function-like macros, but not at the ends. */
1404 if (--macro
->count
> 0)
1405 token
= lex_expansion_token (pfile
, macro
);
1407 if (macro
->count
== 0 || token
->type
== CPP_EOF
)
1411 "'##' cannot appear at either end of a macro expansion");
1415 token
[-1].flags
|= PASTE_LEFT
;
1416 /* Give it a PREV_WHITE for -dM etc. */
1417 token
->flags
|= PREV_WHITE
;
1420 token
= lex_expansion_token (pfile
, macro
);
1423 /* Don't count the CPP_EOF. */
1426 /* Clear the whitespace flag from the leading token. */
1427 macro
->expansion
[0].flags
&= ~PREV_WHITE
;
1429 /* Implement the macro-defined-to-itself optimisation. */
1430 macro
->disabled
= (macro
->count
== 1 && !macro
->fun_like
1431 && macro
->expansion
[0].type
== CPP_NAME
1432 && macro
->expansion
[0].val
.node
== node
);
1434 /* Commit the memory. */
1435 POOL_COMMIT (&pfile
->macro_pool
, macro
->count
* sizeof (cpp_token
));
1437 /* Redefinition of a macro is allowed if and only if the old and new
1438 definitions are the same. (6.10.3 paragraph 2). */
1439 if (node
->type
!= NT_VOID
)
1441 if (CPP_PEDANTIC (pfile
)
1442 && !check_macro_redefinition (pfile
, node
, macro
))
1444 cpp_pedwarn_with_line (pfile
, pfile
->directive_pos
.line
,
1445 pfile
->directive_pos
.col
,
1446 "\"%s\" redefined", node
->name
);
1448 if (pfile
->done_initializing
&& node
->type
== NT_MACRO
1449 && !(node
->flags
& NODE_BUILTIN
))
1450 cpp_pedwarn_with_file_and_line (pfile
,
1451 node
->value
.macro
->file
,
1452 node
->value
.macro
->line
, 1,
1453 "this is the location of the previous definition");
1455 _cpp_free_definition (node
);
1458 /* Enter definition in hash table. */
1459 node
->type
= NT_MACRO
;
1460 node
->value
.macro
= macro
;
1464 /* Stop the lexer accepting __VA_ARGS__. */
1465 pfile
->state
.va_args_ok
= 0;
1467 /* Clear the fast argument lookup indices. */
1468 for (i
= macro
->paramc
; i
-- > 0; )
1469 macro
->params
[i
]->arg_index
= 0;
1474 /* Warn if a token in `string' matches one of the function macro
1475 arguments in `info'. This function assumes that the macro is a
1476 function macro and not an object macro. */
1478 check_trad_stringification (pfile
, macro
, string
)
1480 const cpp_macro
*macro
;
1481 const cpp_string
*string
;
1483 unsigned int i
, len
;
1484 const U_CHAR
*p
, *q
, *limit
= string
->text
+ string
->len
;
1486 /* Loop over the string. */
1487 for (p
= string
->text
; p
< limit
; p
= q
)
1489 /* Find the start of an identifier. */
1490 while (p
< limit
&& !is_idstart (*p
))
1493 /* Find the end of the identifier. */
1495 while (q
< limit
&& is_idchar (*q
))
1500 /* Loop over the function macro arguments to see if the
1501 identifier inside the string matches one of them. */
1502 for (i
= 0; i
< macro
->paramc
; i
++)
1504 const cpp_hashnode
*node
= macro
->params
[i
];
1506 if (node
->length
== len
&& !memcmp (p
, node
->name
, len
))
1509 "macro argument \"%s\" would be stringified with -traditional.",
1517 /* Returns the expansion of a macro, in a format suitable to be read
1518 back in again, and therefore also for DWARF 2 debugging info.
1519 Caller is expected to generate the "#define NAME" bit. The
1520 returned text is temporary, and automatically freed later. */
1522 const unsigned char *
1523 cpp_macro_definition (pfile
, node
)
1525 const cpp_hashnode
*node
;
1527 unsigned int i
, len
;
1528 const cpp_macro
*macro
= node
->value
.macro
;
1529 unsigned char *buffer
;
1531 if (node
->type
!= NT_MACRO
|| (node
->flags
& NODE_BUILTIN
))
1533 cpp_ice (pfile
, "invalid hash type %d in dump_definition", node
->type
);
1537 /* Calculate length. */
1539 if (macro
->fun_like
)
1541 len
+= 3; /* "()" plus possible final "." of ellipsis. */
1542 for (i
= 0; i
< macro
->paramc
; i
++)
1543 len
+= macro
->params
[i
]->length
+ 2; /* ", " */
1546 for (i
= 0; i
< macro
->count
; i
++)
1548 cpp_token
*token
= ¯o
->expansion
[i
];
1550 if (token
->type
== CPP_MACRO_ARG
)
1551 len
+= macro
->params
[token
->val
.arg_no
- 1]->length
;
1553 len
+= cpp_token_len (token
); /* Includes room for ' '. */
1554 if (token
->flags
& STRINGIFY_ARG
)
1556 if (token
->flags
& PASTE_LEFT
)
1557 len
+= 3; /* " ##" */
1560 if (len
> pfile
->macro_buffer_len
)
1561 pfile
->macro_buffer
= (U_CHAR
*) xrealloc (pfile
->macro_buffer
, len
);
1562 buffer
= pfile
->macro_buffer
;
1564 /* Parameter names. */
1565 if (macro
->fun_like
)
1568 for (i
= 0; i
< macro
->paramc
; i
++)
1570 cpp_hashnode
*param
= macro
->params
[i
];
1572 if (param
!= pfile
->spec_nodes
.n__VA_ARGS__
)
1574 memcpy (buffer
, param
->name
, param
->length
);
1575 buffer
+= param
->length
;
1578 if (i
+ 1 < macro
->paramc
)
1579 *buffer
++ = ',', *buffer
++ = ' ';
1580 else if (macro
->var_args
)
1581 *buffer
++ = '.', *buffer
++ = '.', *buffer
++ = '.';
1586 /* Expansion tokens. */
1590 for (i
= 0; i
< macro
->count
; i
++)
1592 cpp_token
*token
= ¯o
->expansion
[i
];
1594 if (token
->flags
& PREV_WHITE
)
1596 if (token
->flags
& STRINGIFY_ARG
)
1599 if (token
->type
== CPP_MACRO_ARG
)
1601 len
= macro
->params
[token
->val
.arg_no
- 1]->length
;
1602 memcpy (buffer
, macro
->params
[token
->val
.arg_no
- 1]->name
, len
);
1606 buffer
= cpp_spell_token (pfile
, token
, buffer
);
1608 if (token
->flags
& PASTE_LEFT
)
1613 /* Next has PREV_WHITE; see _cpp_create_definition. */
1619 return pfile
->macro_buffer
;