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 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 variadic
: 1; /* If a variadic macro. */
42 unsigned int disabled
: 1; /* If macro is disabled. */
43 unsigned int syshdr
: 1; /* If macro defined in system header. */
46 typedef struct macro_arg macro_arg
;
49 cpp_token
*first
; /* First token in unexpanded argument. */
50 cpp_token
*expanded
; /* Macro-expanded argument. */
51 cpp_token
*stringified
; /* Stringified argument. */
52 unsigned int count
; /* # of tokens in argument. */
53 unsigned int expanded_count
; /* # of tokens in expanded argument. */
56 /* Macro expansion. */
58 static void lock_pools
PARAMS ((cpp_reader
*));
59 static void unlock_pools
PARAMS ((cpp_reader
*));
60 static int enter_macro_context
PARAMS ((cpp_reader
*, cpp_hashnode
*));
61 static void builtin_macro
PARAMS ((cpp_reader
*, cpp_token
*));
62 static cpp_context
*push_arg_context
PARAMS ((cpp_reader
*, macro_arg
*));
63 static enum cpp_ttype parse_arg
PARAMS ((cpp_reader
*, macro_arg
*, int));
64 static macro_arg
*parse_args
PARAMS ((cpp_reader
*, const cpp_hashnode
*));
65 static cpp_context
*next_context
PARAMS ((cpp_reader
*));
66 static void expand_arg
PARAMS ((cpp_reader
*, macro_arg
*));
67 static unsigned char *quote_string
PARAMS ((unsigned char *,
68 const unsigned char *,
70 static void make_string_token
PARAMS ((cpp_pool
*, cpp_token
*,
71 const U_CHAR
*, unsigned int));
72 static void make_number_token
PARAMS ((cpp_reader
*, cpp_token
*, int));
73 static void stringify_arg
PARAMS ((cpp_reader
*, macro_arg
*));
74 static void paste_all_tokens
PARAMS ((cpp_reader
*, cpp_token
*));
75 static int paste_tokens
PARAMS ((cpp_reader
*, cpp_token
*, 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 cpp_lookahead
*alloc_lookahead
PARAMS ((cpp_reader
*));
86 static void free_lookahead
PARAMS ((cpp_lookahead
*));
88 /* #define directive parsing and handling. */
90 static cpp_token
*lex_expansion_token
PARAMS ((cpp_reader
*, cpp_macro
*));
91 static int warn_of_redefinition
PARAMS ((cpp_reader
*, const cpp_hashnode
*,
93 static int save_parameter
PARAMS ((cpp_reader
*, cpp_macro
*, cpp_hashnode
*));
94 static int parse_params
PARAMS ((cpp_reader
*, cpp_macro
*));
95 static void check_trad_stringification
PARAMS ((cpp_reader
*,
99 /* Allocates a buffer to hold a token's TEXT, and converts TOKEN to a
100 CPP_STRING token containing TEXT in quoted form. */
102 make_string_token (pool
, token
, text
, len
)
108 U_CHAR
*buf
= _cpp_pool_alloc (pool
, len
* 4 + 1);
110 token
->type
= CPP_STRING
;
111 token
->val
.str
.text
= buf
;
112 token
->val
.str
.len
= quote_string (buf
, text
, len
) - buf
;
113 buf
[token
->val
.str
.len
] = '\0';
117 /* Allocates and converts a temporary token to a CPP_NUMBER token,
118 evaluating to NUMBER. */
120 make_number_token (pfile
, token
, number
)
125 unsigned char *buf
= _cpp_pool_alloc (&pfile
->ident_pool
, 20);
127 sprintf ((char *) buf
, "%d", number
);
128 token
->type
= CPP_NUMBER
;
129 token
->val
.str
.text
= buf
;
130 token
->val
.str
.len
= ustrlen (buf
);
134 static const char * const monthnames
[] =
136 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
137 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
140 /* Handle builtin macros like __FILE__. */
142 builtin_macro (pfile
, token
)
146 unsigned char flags
= ((token
->flags
& PREV_WHITE
) | AVOID_LPASTE
);
147 cpp_hashnode
*node
= token
->val
.node
;
149 switch (node
->value
.builtin
)
155 cpp_buffer
*buffer
= pfile
->buffer
;
157 if (node
->value
.builtin
== BT_BASE_FILE
)
159 buffer
= buffer
->prev
;
161 name
= buffer
->nominal_fname
;
162 make_string_token (&pfile
->ident_pool
, token
,
163 (const unsigned char *) name
, strlen (name
));
167 case BT_INCLUDE_LEVEL
:
168 /* pfile->include_depth counts the primary source as level 1,
169 but historically __INCLUDE_DEPTH__ has called the primary
171 make_number_token (pfile
, token
, pfile
->include_depth
- 1);
175 /* If __LINE__ is embedded in a macro, it must expand to the
176 line of the macro's invocation, not its definition.
177 Otherwise things like assert() will not work properly. */
178 make_number_token (pfile
, token
, cpp_get_line (pfile
)->line
);
183 int stdc
= (!CPP_IN_SYSTEM_HEADER (pfile
)
184 || pfile
->spec_nodes
.n__STRICT_ANSI__
->type
!= NT_VOID
);
185 make_number_token (pfile
, token
, stdc
);
191 if (pfile
->date
.type
== CPP_EOF
)
193 /* Allocate __DATE__ and __TIME__ from permanent storage,
194 and save them in pfile so we don't have to do this again.
195 We don't generate these strings at init time because
196 time() and localtime() are very slow on some systems. */
197 time_t tt
= time (NULL
);
198 struct tm
*tb
= localtime (&tt
);
200 make_string_token (&pfile
->ident_pool
, &pfile
->date
,
202 make_string_token (&pfile
->ident_pool
, &pfile
->time
,
205 sprintf ((char *) pfile
->date
.val
.str
.text
, "%s %2d %4d",
206 monthnames
[tb
->tm_mon
], tb
->tm_mday
, tb
->tm_year
+ 1900);
207 sprintf ((char *) pfile
->time
.val
.str
.text
, "%02d:%02d:%02d",
208 tb
->tm_hour
, tb
->tm_min
, tb
->tm_sec
);
210 *token
= node
->value
.builtin
== BT_DATE
? pfile
->date
: pfile
->time
;
214 cpp_ice (pfile
, "invalid builtin macro \"%s\"", NODE_NAME (node
));
218 token
->flags
= flags
;
221 /* Used by cpperror.c to obtain the correct line and column to report
223 const cpp_lexer_pos
*
227 return &pfile
->lexer_pos
;
234 _cpp_lock_pool (&pfile
->argument_pool
);
241 _cpp_unlock_pool (&pfile
->argument_pool
);
244 /* Adds backslashes before all backslashes and double quotes appearing
245 in strings. Non-printable characters are converted to octal. */
247 quote_string (dest
, src
, len
)
256 if (c
== '\\' || c
== '"')
267 sprintf ((char *) dest
, "\\%03o", c
);
276 /* Convert a token sequence to a single string token according to the
277 rules of the ISO C #-operator. */
279 stringify_arg (pfile
, arg
)
283 cpp_pool
*pool
= &pfile
->ident_pool
;
284 unsigned char *start
= POOL_FRONT (pool
);
285 unsigned int i
, escape_it
, total_len
= 0, backslash_count
= 0;
287 /* Loop, reading in the argument's tokens. */
288 for (i
= 0; i
< arg
->count
; i
++)
291 const cpp_token
*token
= &arg
->first
[i
];
292 unsigned int len
= cpp_token_len (token
);
294 escape_it
= (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
295 || token
->type
== CPP_CHAR
|| token
->type
== CPP_WCHAR
);
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 /* No leading white space. */
310 if (token
->flags
& PREV_WHITE
&& total_len
> 0)
315 unsigned char *buf
= (unsigned char *) xmalloc (len
);
317 len
= cpp_spell_token (pfile
, token
, buf
) - buf
;
318 dest
= quote_string (dest
, buf
, len
);
322 dest
= cpp_spell_token (pfile
, token
, dest
);
323 total_len
= dest
- start
;
325 if (token
->type
== CPP_OTHER
&& token
->val
.c
== '\\')
331 /* Ignore the final \ of invalid string literals. */
332 if (backslash_count
& 1)
334 cpp_warning (pfile
, "invalid string literal, ignoring final '\\'");
338 /* Null terminate, and commit the memory. */
339 start
[total_len
] = '\0';
340 POOL_COMMIT (pool
, total_len
+ 1);
342 arg
->stringified
= xnew (cpp_token
);
343 arg
->stringified
->flags
= 0;
344 arg
->stringified
->type
= CPP_STRING
;
345 arg
->stringified
->val
.str
.text
= start
;
346 arg
->stringified
->val
.str
.len
= 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
)
355 cpp_token
*lhs
, *rhs
;
361 type
= cpp_can_paste (pfile
, lhs
, rhs
, &digraph
);
365 /* Mandatory warning for all apart from assembler. */
366 if (CPP_OPTION (pfile
, lang
) != CLK_ASM
)
368 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
369 cpp_token_as_text (pfile
, lhs
),
370 cpp_token_as_text (pfile
, rhs
));
372 /* The standard states that behaviour is undefined. By the
373 principle of least surpise, we step back before the RHS, and
374 mark it to prevent macro expansion. Tests in the testsuite
375 rely on clearing PREV_WHITE here, though you could argue we
376 should actually set it. Assembler can have '.' in labels and
377 so requires that we don't insert spaces there. Maybe we should
378 change this to put out a space unless it's assembler. */
379 rhs
->flags
&= ~PREV_WHITE
;
380 rhs
->flags
|= NO_EXPAND
;
384 flags
= lhs
->flags
& ~DIGRAPH
;
388 /* Identifiers and numbers need spellings to be pasted. */
389 if (type
== CPP_NAME
|| type
== CPP_NUMBER
)
391 unsigned int total_len
= cpp_token_len (lhs
) + cpp_token_len (rhs
);
392 unsigned char *result
, *end
;
394 result
= _cpp_pool_alloc (&pfile
->ident_pool
, total_len
+ 1);
396 /* Paste the spellings and null terminate. */
397 end
= cpp_spell_token (pfile
, rhs
, cpp_spell_token (pfile
, lhs
, result
));
399 total_len
= end
- result
;
401 if (type
== CPP_NAME
)
403 lhs
->val
.node
= cpp_lookup (pfile
, result
, total_len
);
404 if (lhs
->val
.node
->flags
& NODE_OPERATOR
)
407 lhs
->type
= lhs
->val
.node
->value
.operator;
412 lhs
->val
.str
.text
= result
;
413 lhs
->val
.str
.len
= total_len
;
416 else if (type
== CPP_WCHAR
|| type
== CPP_WSTRING
)
417 lhs
->val
.str
= rhs
->val
.str
;
419 /* Set type and flags after pasting spellings. */
426 /* Handles an arbitrarily long sequence of ## operators. This
427 implementation is left-associative, non-recursive, and finishes a
428 paste before handling succeeding ones. If the paste fails, we back
429 up a token to just after the ## operator, with the effect that it
430 appears in the output stream normally. */
432 paste_all_tokens (pfile
, lhs
)
437 unsigned char orig_flags
= lhs
->flags
;
441 /* Take the token directly from the current context. We can do
442 this, because we are in the replacement list of either an
443 object-like macro, or a function-like macro with arguments
444 inserted. In either case, the constraints to #define
445 guarantee we have at least one more token. */
446 rhs
= pfile
->context
->list
.first
++;
447 if (paste_tokens (pfile
, lhs
, rhs
))
449 /* We failed. Step back so we read the RHS in next. */
450 pfile
->context
->list
.first
--;
454 while (rhs
->flags
& PASTE_LEFT
);
456 /* The pasted token has the PREV_WHITE flag of the LHS, is no longer
457 PASTE_LEFT, and is subject to macro expansion. */
458 lhs
->flags
&= ~(PREV_WHITE
| PASTE_LEFT
| NO_EXPAND
);
459 lhs
->flags
|= orig_flags
& (PREV_WHITE
| AVOID_LPASTE
);
462 /* Reads the unexpanded tokens of a macro argument into ARG. VAR_ARGS
463 is non-zero if this is a variadic macro. Returns the type of the
464 token that caused reading to finish. */
465 static enum cpp_ttype
466 parse_arg (pfile
, arg
, variadic
)
468 struct macro_arg
*arg
;
471 enum cpp_ttype result
;
472 unsigned int paren
= 0;
475 arg
->first
= (cpp_token
*) POOL_FRONT (&pfile
->argument_pool
);
476 for (;; arg
->count
++)
478 cpp_token
*token
= &arg
->first
[arg
->count
];
479 if ((unsigned char *) (token
+ 1) >= POOL_LIMIT (&pfile
->argument_pool
))
481 _cpp_next_chunk (&pfile
->argument_pool
, sizeof (cpp_token
),
482 (unsigned char **) &arg
->first
);
483 token
= &arg
->first
[arg
->count
];
486 /* Newlines in arguments are white space (6.10.3.10). */
487 line
= pfile
->lexer_pos
.output_line
;
488 cpp_get_token (pfile
, token
);
489 if (line
!= pfile
->lexer_pos
.output_line
)
490 token
->flags
|= PREV_WHITE
;
492 result
= token
->type
;
493 if (result
== CPP_OPEN_PAREN
)
495 else if (result
== CPP_CLOSE_PAREN
&& paren
-- == 0)
497 /* Commas are not terminators within parantheses or variadic. */
498 else if (result
== CPP_COMMA
&& paren
== 0 && !variadic
)
500 else if (result
== CPP_EOF
)
501 break; /* Error reported by caller. */
504 /* Commit the memory used to store the arguments. */
505 POOL_COMMIT (&pfile
->argument_pool
, arg
->count
* sizeof (cpp_token
));
510 /* Parse the arguments making up a macro invocation. */
512 parse_args (pfile
, node
)
514 const cpp_hashnode
*node
;
516 cpp_macro
*macro
= node
->value
.macro
;
517 macro_arg
*args
, *cur
;
521 /* Allocate room for at least one argument, and zero it out. */
522 argc
= macro
->paramc
? macro
->paramc
: 1;
523 args
= xcnewvec (macro_arg
, argc
);
525 for (cur
= args
, argc
= 0; ;)
529 type
= parse_arg (pfile
, cur
, argc
== macro
->paramc
&& macro
->variadic
);
530 if (type
== CPP_CLOSE_PAREN
|| type
== CPP_EOF
)
533 /* Re-use the last argument for excess arguments. */
534 if (argc
< macro
->paramc
)
540 cpp_error (pfile
, "unterminated argument list invoking macro \"%s\"",
544 else if (argc
< macro
->paramc
)
546 /* As an extension, a rest argument is allowed to not appear in
547 the invocation at all.
548 e.g. #define debug(format, args...) something
551 This is exactly the same as if there had been an empty rest
552 argument - debug("string", ). */
554 if (argc
+ 1 == macro
->paramc
&& macro
->variadic
)
556 if (CPP_PEDANTIC (pfile
) && ! macro
->syshdr
)
557 cpp_pedwarn (pfile
, "ISO C99 requires rest arguments to be used");
562 "macro \"%s\" requires %u arguments, but only %u given",
563 NODE_NAME (node
), macro
->paramc
, argc
);
567 else if (argc
> macro
->paramc
)
569 /* Empty argument to a macro taking no arguments is OK. */
570 if (argc
!= 1 || cur
->count
)
573 "macro \"%s\" passed %u arguments, but takes just %u",
574 NODE_NAME (node
), argc
, macro
->paramc
);
589 funlike_invocation_p (pfile
, node
, list
)
591 const cpp_hashnode
*node
;
592 struct toklist
*list
;
595 cpp_token maybe_paren
;
597 cpp_lexer_pos macro_pos
;
599 macro_pos
= pfile
->lexer_pos
;
600 pfile
->state
.parsing_args
= 1;
601 pfile
->state
.prevent_expansion
++;
602 orig
= pfile
->context
;
604 cpp_start_lookahead (pfile
);
605 cpp_get_token (pfile
, &maybe_paren
);
606 cpp_stop_lookahead (pfile
, maybe_paren
.type
== CPP_OPEN_PAREN
);
607 pfile
->state
.parsing_args
= 2;
609 if (maybe_paren
.type
== CPP_OPEN_PAREN
)
610 args
= parse_args (pfile
, node
);
611 else if (CPP_WTRADITIONAL (pfile
) && ! node
->value
.macro
->syshdr
)
613 "function-like macro \"%s\" must be used with arguments in traditional C",
616 /* Restore original context. */
617 pfile
->context
= orig
;
618 pfile
->state
.prevent_expansion
--;
619 pfile
->state
.parsing_args
= 0;
621 /* Reset the position in case of failure. If success, the macro's
622 expansion appears where the name would have. */
623 pfile
->lexer_pos
= macro_pos
;
627 if (node
->value
.macro
->paramc
> 0)
629 /* Don't save tokens during pre-expansion. */
630 struct cpp_lookahead
*la_saved
= pfile
->la_write
;
632 replace_args (pfile
, node
->value
.macro
, args
, list
);
633 pfile
->la_write
= la_saved
;
641 /* Push the context of a macro onto the context stack. TOKEN is the
642 macro name. If we can successfully start expanding the macro,
643 TOKEN is replaced with the first token of the expansion, and we
646 enter_macro_context (pfile
, node
)
650 cpp_context
*context
;
651 cpp_macro
*macro
= node
->value
.macro
;
654 /* Save the position of the outermost macro invocation. */
655 if (!pfile
->context
->prev
)
658 if (macro
->fun_like
&& !funlike_invocation_p (pfile
, node
, &list
))
660 if (!pfile
->context
->prev
)
661 unlock_pools (pfile
);
665 if (macro
->paramc
== 0)
667 list
.first
= macro
->expansion
;
668 list
.limit
= macro
->expansion
+ macro
->count
;
671 /* Only push a macro context for non-empty replacement lists. */
672 if (list
.first
!= list
.limit
)
674 context
= next_context (pfile
);
675 context
->list
= list
;
676 context
->macro
= macro
;
678 /* Disable the macro within its expansion. */
685 /* Move to the next context. Create one if there is none. */
690 cpp_context
*prev
= pfile
->context
;
691 cpp_context
*result
= prev
->next
;
695 result
= xnew (cpp_context
);
701 pfile
->context
= result
;
706 replace_args (pfile
, macro
, args
, list
)
710 struct toklist
*list
;
712 unsigned char flags
= 0;
713 unsigned int i
, total
;
714 const cpp_token
*src
, *limit
;
718 src
= macro
->expansion
;
719 limit
= src
+ macro
->count
;
721 /* First, fully macro-expand arguments, calculating the number of
722 tokens in the final expansion as we go. This ensures that the
723 possible recursive use of argument_pool is fine. */
725 for (; src
< limit
; src
++)
726 if (src
->type
== CPP_MACRO_ARG
)
728 /* We have an argument. If it is not being stringified or
729 pasted it is macro-replaced before insertion. */
730 arg
= &args
[src
->val
.arg_no
- 1];
732 if (src
->flags
& STRINGIFY_ARG
)
734 if (!arg
->stringified
)
735 stringify_arg (pfile
, arg
);
737 else if ((src
->flags
& PASTE_LEFT
)
738 || (src
> macro
->expansion
&& (src
[-1].flags
& PASTE_LEFT
)))
739 total
+= arg
->count
- 1;
744 arg
->expanded_count
= 0;
746 expand_arg (pfile
, arg
);
748 total
+= arg
->expanded_count
- 1;
752 dest
= (cpp_token
*) _cpp_pool_alloc (&pfile
->argument_pool
,
753 total
* sizeof (cpp_token
));
756 for (src
= macro
->expansion
; src
< limit
; src
++)
757 if (src
->type
== CPP_MACRO_ARG
)
760 const cpp_token
*from
;
762 arg
= &args
[src
->val
.arg_no
- 1];
763 if (src
->flags
& STRINGIFY_ARG
)
765 from
= arg
->stringified
, count
= 1;
766 /* Ugh. Maintain position of original argument. */
767 arg
->stringified
->line
= src
->line
;
768 arg
->stringified
->col
= src
->col
;
770 else if (src
->flags
& PASTE_LEFT
)
771 count
= arg
->count
, from
= arg
->first
;
772 else if (src
> macro
->expansion
&& (src
[-1].flags
& PASTE_LEFT
))
774 count
= arg
->count
, from
= arg
->first
;
775 if (dest
!= list
->first
)
777 /* GCC has special semantics for , ## b where b is a
778 varargs parameter: the comma disappears if b was
779 given no actual arguments (not merely if b is an
780 empty argument); otherwise pasting is turned off. */
781 if (dest
[-1].type
== CPP_COMMA
783 && src
->val
.arg_no
== macro
->paramc
)
788 dest
[-1].flags
&= ~PASTE_LEFT
;
790 /* Count == 0 is the RHS a placemarker case. */
792 dest
[-1].flags
&= ~PASTE_LEFT
;
796 count
= arg
->expanded_count
, from
= arg
->expanded
;
798 /* Count == 0 is the LHS a placemarker case. */
801 memcpy (dest
, from
, count
* sizeof (cpp_token
));
803 /* The first token gets PREV_WHITE of the CPP_MACRO_ARG. */
804 dest
->flags
&= ~PREV_WHITE
;
805 dest
->flags
|= src
->flags
& PREV_WHITE
;
806 dest
->flags
|= AVOID_LPASTE
;
808 /* The last token gets the PASTE_LEFT of the CPP_MACRO_ARG. */
809 dest
[count
- 1].flags
|= src
->flags
& PASTE_LEFT
;
814 /* The token after the argument must avoid an accidental paste. */
815 flags
= AVOID_LPASTE
;
820 dest
->flags
|= flags
;
827 /* Free the expanded arguments. */
828 for (i
= 0; i
< macro
->paramc
; i
++)
830 if (args
[i
].expanded
)
831 free (args
[i
].expanded
);
832 if (args
[i
].stringified
)
833 free (args
[i
].stringified
);
837 /* Subroutine of expand_arg to put the unexpanded tokens on the
840 push_arg_context (pfile
, arg
)
844 cpp_context
*context
= next_context (pfile
);
846 context
->list
.first
= arg
->first
;
847 context
->list
.limit
= arg
->first
+ arg
->count
;
853 expand_arg (pfile
, arg
)
858 unsigned int capacity
= 256;
860 /* Loop, reading in the arguments. */
861 arg
->expanded
= (cpp_token
*) xmalloc (capacity
* sizeof (cpp_token
));
863 push_arg_context (pfile
, arg
);
866 if (arg
->expanded_count
>= capacity
)
869 arg
->expanded
= (cpp_token
*)
870 xrealloc (arg
->expanded
, capacity
* sizeof (cpp_token
));
872 token
= &arg
->expanded
[arg
->expanded_count
++];
873 cpp_get_token (pfile
, token
);
875 while (token
->type
!= CPP_EOF
);
877 arg
->expanded_count
--;
879 /* Pop the context we pushed. */
880 pfile
->context
= pfile
->context
->prev
;
884 _cpp_pop_context (pfile
)
887 cpp_context
*context
= pfile
->context
;
889 pfile
->context
= context
->prev
;
890 if (!pfile
->context
->prev
&& !pfile
->state
.parsing_args
)
891 unlock_pools (pfile
);
893 /* Re-enable a macro, temporarily if parsing_args, when leaving its
895 context
->macro
->disabled
= 0;
898 /* Eternal routine to get a token. Also used nearly everywhere
899 internally, except for places where we know we can safely call
900 the lexer directly, such as lexing a directive name.
902 Macro expansions and directives are transparently handled,
903 including entering included files. Thus tokens are post-macro
904 expansion, and after any intervening directives. External callers
905 see CPP_EOF only at EOF. Internal callers also see it when meeting
906 a directive inside a macro call, when at the end of a directive and
907 state.in_directive is still 1, and at the end of argument
910 cpp_get_token (pfile
, token
)
916 cpp_context
*context
= pfile
->context
;
919 take_lookahead_token (pfile
, token
);
920 /* Context->prev == 0 <=> base context. */
921 else if (!context
->prev
)
922 _cpp_lex_token (pfile
, token
);
923 else if (context
->list
.first
!= context
->list
.limit
)
925 *token
= *context
->list
.first
++;
926 token
->flags
|= pfile
->buffer
->saved_flags
;
927 pfile
->buffer
->saved_flags
= 0;
928 /* PASTE_LEFT tokens can only appear in macro expansions. */
929 if (token
->flags
& PASTE_LEFT
)
931 /* Maintains position of original token. */
932 paste_all_tokens (pfile
, token
);
933 pfile
->buffer
->saved_flags
= AVOID_LPASTE
;
940 /* Avoid accidental paste at the end of a macro. */
941 pfile
->buffer
->saved_flags
|= AVOID_LPASTE
;
942 _cpp_pop_context (pfile
);
945 /* End of argument pre-expansion. */
946 token
->type
= CPP_EOF
;
951 if (token
->type
!= CPP_NAME
)
954 /* Handle macros and the _Pragma operator. */
955 if (token
->val
.node
->type
== NT_MACRO
956 && !pfile
->state
.prevent_expansion
957 && !(token
->flags
& NO_EXPAND
))
959 cpp_hashnode
*node
= token
->val
.node
;
961 /* Macros invalidate controlling macros. */
962 pfile
->mi_valid
= false;
964 if (node
->flags
& NODE_BUILTIN
)
966 /* Maintains position of original token. */
967 builtin_macro (pfile
, token
);
968 pfile
->buffer
->saved_flags
= AVOID_LPASTE
;
972 if (node
->value
.macro
->disabled
)
973 token
->flags
|= NO_EXPAND
;
974 else if (enter_macro_context (pfile
, node
))
976 /* Pass AVOID_LPASTE and our PREV_WHITE to next token. */
977 pfile
->buffer
->saved_flags
= ((token
->flags
& PREV_WHITE
)
983 /* Don't interpret _Pragma within directives. The standard is
984 not clear on this, but to me this makes most sense. */
985 if (token
->val
.node
!= pfile
->spec_nodes
.n__Pragma
986 || pfile
->state
.in_directive
)
989 /* Handle it, and loop back for another token. MI is cleared
990 since this token came from either the lexer or a macro. */
991 _cpp_do__Pragma (pfile
);
995 save_lookahead_token (pfile
, token
);
998 /* Returns true if we're expanding an object-like macro that was
999 defined in a system header. Just checks the macro at the top of
1000 the stack. Used for diagnostic suppression. */
1002 cpp_sys_macro_p (pfile
)
1005 cpp_macro
*macro
= pfile
->context
->macro
;
1007 return macro
&& macro
->syshdr
;
1010 /* Read each token in, until EOF. Directives are transparently
1013 cpp_scan_nooutput (pfile
)
1019 cpp_get_token (pfile
, &token
);
1020 while (token
.type
!= CPP_EOF
);
1023 /* Lookahead handling. */
1026 save_lookahead_token (pfile
, token
)
1028 const cpp_token
*token
;
1030 if (token
->type
!= CPP_EOF
)
1032 cpp_lookahead
*la
= pfile
->la_write
;
1033 cpp_token_with_pos
*twp
;
1035 if (la
->count
== la
->cap
)
1037 la
->cap
+= la
->cap
+ 8;
1038 la
->tokens
= (cpp_token_with_pos
*)
1039 xrealloc (la
->tokens
, la
->cap
* sizeof (cpp_token_with_pos
));
1042 twp
= &la
->tokens
[la
->count
++];
1043 twp
->token
= *token
;
1044 twp
->pos
= *cpp_get_line (pfile
);
1049 take_lookahead_token (pfile
, token
)
1053 cpp_lookahead
*la
= pfile
->la_read
;
1054 cpp_token_with_pos
*twp
= &la
->tokens
[la
->cur
];
1056 *token
= twp
->token
;
1057 pfile
->lexer_pos
= twp
->pos
;
1059 if (++la
->cur
== la
->count
)
1060 _cpp_release_lookahead (pfile
);
1063 /* Moves the lookahead at the front of the read list to the free store. */
1065 _cpp_release_lookahead (pfile
)
1068 cpp_lookahead
*la
= pfile
->la_read
;
1070 pfile
->la_read
= la
->next
;
1071 la
->next
= pfile
->la_unused
;
1072 pfile
->la_unused
= la
;
1073 unlock_pools (pfile
);
1076 /* Take a new lookahead from the free store, or allocate one if none. */
1077 static cpp_lookahead
*
1078 alloc_lookahead (pfile
)
1081 cpp_lookahead
*la
= pfile
->la_unused
;
1084 pfile
->la_unused
= la
->next
;
1087 la
= xnew (cpp_lookahead
);
1092 la
->cur
= la
->count
= 0;
1096 /* Free memory associated with a lookahead list. */
1102 free ((PTR
) la
->tokens
);
1106 /* Free all the lookaheads of a cpp_reader. */
1108 _cpp_free_lookaheads (pfile
)
1111 cpp_lookahead
*la
, *lan
;
1114 free_lookahead (pfile
->la_read
);
1115 if (pfile
->la_write
)
1116 free_lookahead (pfile
->la_write
);
1118 for (la
= pfile
->la_unused
; la
; la
= lan
)
1121 free_lookahead (la
);
1125 /* Allocate a lookahead and move it to the front of the write list. */
1127 cpp_start_lookahead (pfile
)
1130 cpp_lookahead
*la
= alloc_lookahead (pfile
);
1132 la
->next
= pfile
->la_write
;
1133 pfile
->la_write
= la
;
1135 la
->pos
= *cpp_get_line (pfile
);
1137 /* Don't allow memory pools to be re-used whilst we're reading ahead. */
1141 /* Stop reading ahead - either step back, or drop the read ahead. */
1143 cpp_stop_lookahead (pfile
, drop
)
1147 cpp_lookahead
*la
= pfile
->la_write
;
1149 pfile
->la_write
= la
->next
;
1150 la
->next
= pfile
->la_read
;
1151 pfile
->la_read
= la
;
1153 if (drop
|| la
->count
== 0)
1154 _cpp_release_lookahead (pfile
);
1156 pfile
->lexer_pos
= la
->pos
;
1159 /* Push a single token back to the front of the queue. Only to be
1160 used by cpplib, and only then when necessary. POS is the position
1161 to report for the preceding token. */
1163 _cpp_push_token (pfile
, token
, pos
)
1165 const cpp_token
*token
;
1166 const cpp_lexer_pos
*pos
;
1168 cpp_start_lookahead (pfile
);
1169 save_lookahead_token (pfile
, token
);
1170 cpp_stop_lookahead (pfile
, 0);
1171 pfile
->lexer_pos
= *pos
;
1174 /* #define directive parsing and handling. */
1176 /* Returns non-zero if a macro redefinition warning is required. */
1178 warn_of_redefinition (pfile
, node
, macro2
)
1180 const cpp_hashnode
*node
;
1181 const cpp_macro
*macro2
;
1183 const cpp_macro
*macro1
;
1186 /* Some redefinitions need to be warned about regardless. */
1187 if (node
->flags
& NODE_WARN
)
1190 if (! CPP_PEDANTIC (pfile
))
1193 /* Redefinition of a macro is allowed if and only if the old and new
1194 definitions are the same. (6.10.3 paragraph 2). */
1195 macro1
= node
->value
.macro
;
1197 /* The quick failures. */
1198 if (macro1
->count
!= macro2
->count
1199 || macro1
->paramc
!= macro2
->paramc
1200 || macro1
->fun_like
!= macro2
->fun_like
1201 || macro1
->variadic
!= macro2
->variadic
)
1204 /* Check each token. */
1205 for (i
= 0; i
< macro1
->count
; i
++)
1206 if (! _cpp_equiv_tokens (¯o1
->expansion
[i
], ¯o2
->expansion
[i
]))
1209 /* Check parameter spellings. */
1210 for (i
= 0; i
< macro1
->paramc
; i
++)
1211 if (macro1
->params
[i
] != macro2
->params
[i
])
1217 /* Free the definition of hashnode H. */
1220 _cpp_free_definition (h
)
1223 /* Macros and assertions no longer have anything to free. */
1225 /* Clear builtin flag in case of redefinition. */
1226 h
->flags
&= ~NODE_BUILTIN
;
1230 save_parameter (pfile
, macro
, node
)
1235 cpp_hashnode
**dest
;
1237 /* Constraint 6.10.3.6 - duplicate parameter names. */
1238 if (node
->arg_index
)
1240 cpp_error (pfile
, "duplicate macro parameter \"%s\"", NODE_NAME (node
));
1244 dest
= ¯o
->params
[macro
->paramc
];
1246 /* Check we have room for the parameters. */
1247 if ((unsigned char *) (dest
+ 1) >= POOL_LIMIT (&pfile
->macro_pool
))
1249 _cpp_next_chunk (&pfile
->macro_pool
, sizeof (cpp_hashnode
*),
1250 (unsigned char **) ¯o
->params
);
1251 dest
= ¯o
->params
[macro
->paramc
];
1255 node
->arg_index
= ++macro
->paramc
;
1260 parse_params (pfile
, macro
)
1265 unsigned int prev_ident
= 0;
1267 macro
->params
= (cpp_hashnode
**) POOL_FRONT (&pfile
->macro_pool
);
1270 _cpp_lex_token (pfile
, &token
);
1275 cpp_error (pfile
, "\"%s\" may not appear in macro parameter list",
1276 cpp_token_as_text (pfile
, &token
));
1282 cpp_error (pfile
, "macro parameters must be comma-separated");
1287 if (save_parameter (pfile
, macro
, token
.val
.node
))
1291 case CPP_CLOSE_PAREN
:
1292 if (prev_ident
|| macro
->paramc
== 0)
1295 /* Fall through to pick up the error. */
1299 cpp_error (pfile
, "parameter name missing");
1306 macro
->variadic
= 1;
1309 save_parameter (pfile
, macro
, pfile
->spec_nodes
.n__VA_ARGS__
);
1310 pfile
->state
.va_args_ok
= 1;
1311 if (! CPP_OPTION (pfile
, c99
) && CPP_OPTION (pfile
, pedantic
))
1313 "anonymous variadic macros were introduced in C99");
1315 else if (CPP_OPTION (pfile
, pedantic
))
1316 cpp_pedwarn (pfile
, "ISO C does not permit named variadic macros");
1318 /* We're at the end, and just expect a closing parenthesis. */
1319 _cpp_lex_token (pfile
, &token
);
1320 if (token
.type
== CPP_CLOSE_PAREN
)
1325 cpp_error (pfile
, "missing ')' in macro parameter list");
1329 /* Success. Commit the parameter array. */
1330 POOL_COMMIT (&pfile
->macro_pool
,
1331 macro
->paramc
* sizeof (cpp_hashnode
*));
1336 /* Lex a token from a macro's replacement list. Translate it to a
1337 CPP_MACRO_ARG if appropriate. */
1339 lex_expansion_token (pfile
, macro
)
1343 cpp_token
*token
= ¯o
->expansion
[macro
->count
];
1345 /* Check we have room for the token. */
1346 if ((unsigned char *) (token
+ 1) >= POOL_LIMIT (&pfile
->macro_pool
))
1348 _cpp_next_chunk (&pfile
->macro_pool
, sizeof (cpp_token
),
1349 (unsigned char **) ¯o
->expansion
);
1350 token
= ¯o
->expansion
[macro
->count
];
1354 _cpp_lex_token (pfile
, token
);
1356 /* Is this an argument? */
1357 if (token
->type
== CPP_NAME
&& token
->val
.node
->arg_index
)
1359 token
->type
= CPP_MACRO_ARG
;
1360 token
->val
.arg_no
= token
->val
.node
->arg_index
;
1362 else if (CPP_WTRADITIONAL (pfile
) && macro
->paramc
> 0
1363 && (token
->type
== CPP_STRING
|| token
->type
== CPP_CHAR
))
1364 check_trad_stringification (pfile
, macro
, &token
->val
.str
);
1369 /* Parse a macro and save its expansion. Returns non-zero on success. */
1371 _cpp_create_definition (pfile
, node
)
1377 unsigned int i
, ok
= 1;
1379 macro
= (cpp_macro
*) _cpp_pool_alloc (&pfile
->macro_pool
,
1380 sizeof (cpp_macro
));
1381 macro
->file
= pfile
->buffer
->nominal_fname
;
1382 macro
->line
= pfile
->directive_pos
.line
;
1385 macro
->fun_like
= 0;
1386 macro
->variadic
= 0;
1388 macro
->expansion
= (cpp_token
*) POOL_FRONT (&pfile
->macro_pool
);
1390 /* Get the first token of the expansion (or the '(' of a
1391 function-like macro). */
1392 token
= lex_expansion_token (pfile
, macro
);
1393 if (token
->type
== CPP_OPEN_PAREN
&& !(token
->flags
& PREV_WHITE
))
1395 if (!(ok
= parse_params (pfile
, macro
)))
1398 macro
->fun_like
= 1;
1399 /* Some of the pool may have been used for the parameter store. */
1400 macro
->expansion
= (cpp_token
*) POOL_FRONT (&pfile
->macro_pool
);
1401 token
= lex_expansion_token (pfile
, macro
);
1403 else if (token
->type
!= CPP_EOF
&& !(token
->flags
& PREV_WHITE
))
1404 cpp_pedwarn (pfile
, "ISO C requires whitespace after the macro name");
1406 /* Setting it here means we don't catch leading comments. */
1407 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
1411 /* Check the stringifying # constraint 6.10.3.2.1 of
1412 function-like macros when lexing the subsequent token. */
1413 if (macro
->count
> 1 && token
[-1].type
== CPP_HASH
&& macro
->fun_like
)
1415 if (token
->type
== CPP_MACRO_ARG
)
1417 token
->flags
&= ~PREV_WHITE
;
1418 token
->flags
|= STRINGIFY_ARG
;
1419 token
->flags
|= token
[-1].flags
& PREV_WHITE
;
1420 token
[-1] = token
[0];
1423 /* Let assembler get away with murder. */
1424 else if (CPP_OPTION (pfile
, lang
) != CLK_ASM
)
1427 cpp_error (pfile
, "'#' is not followed by a macro parameter");
1432 if (token
->type
== CPP_EOF
)
1435 /* Paste operator constraint 6.10.3.3.1. */
1436 if (token
->type
== CPP_PASTE
)
1438 /* Token-paste ##, can appear in both object-like and
1439 function-like macros, but not at the ends. */
1440 if (--macro
->count
> 0)
1441 token
= lex_expansion_token (pfile
, macro
);
1443 if (macro
->count
== 0 || token
->type
== CPP_EOF
)
1447 "'##' cannot appear at either end of a macro expansion");
1451 token
[-1].flags
|= PASTE_LEFT
;
1452 /* Give it a PREV_WHITE for -dM etc. */
1453 token
->flags
|= PREV_WHITE
;
1456 token
= lex_expansion_token (pfile
, macro
);
1459 /* Don't count the CPP_EOF. */
1462 /* Clear the whitespace flag from the leading token. */
1463 macro
->expansion
[0].flags
&= ~PREV_WHITE
;
1465 /* Implement the macro-defined-to-itself optimisation. */
1466 macro
->disabled
= (macro
->count
== 1 && !macro
->fun_like
1467 && macro
->expansion
[0].type
== CPP_NAME
1468 && macro
->expansion
[0].val
.node
== node
);
1470 /* To suppress some diagnostics. */
1471 macro
->syshdr
= pfile
->buffer
->sysp
!= 0;
1473 /* Commit the memory. */
1474 POOL_COMMIT (&pfile
->macro_pool
, macro
->count
* sizeof (cpp_token
));
1476 if (node
->type
!= NT_VOID
)
1478 if (warn_of_redefinition (pfile
, node
, macro
))
1480 cpp_pedwarn_with_line (pfile
, pfile
->directive_pos
.line
,
1481 pfile
->directive_pos
.col
,
1482 "\"%s\" redefined", NODE_NAME (node
));
1484 if (node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
))
1485 cpp_pedwarn_with_file_and_line (pfile
,
1486 node
->value
.macro
->file
,
1487 node
->value
.macro
->line
, 1,
1488 "this is the location of the previous definition");
1490 _cpp_free_definition (node
);
1493 /* Enter definition in hash table. */
1494 node
->type
= NT_MACRO
;
1495 node
->value
.macro
= macro
;
1496 if (! ustrncmp (NODE_NAME (node
), DSC ("__STDC_")))
1497 node
->flags
|= NODE_WARN
;
1501 /* Stop the lexer accepting __VA_ARGS__. */
1502 pfile
->state
.va_args_ok
= 0;
1504 /* Clear the fast argument lookup indices. */
1505 for (i
= macro
->paramc
; i
-- > 0; )
1506 macro
->params
[i
]->arg_index
= 0;
1511 /* Warn if a token in `string' matches one of the function macro
1512 arguments in `info'. This function assumes that the macro is a
1513 function macro and not an object macro. */
1515 check_trad_stringification (pfile
, macro
, string
)
1517 const cpp_macro
*macro
;
1518 const cpp_string
*string
;
1520 unsigned int i
, len
;
1521 const U_CHAR
*p
, *q
, *limit
= string
->text
+ string
->len
;
1523 /* Loop over the string. */
1524 for (p
= string
->text
; p
< limit
; p
= q
)
1526 /* Find the start of an identifier. */
1527 while (p
< limit
&& !is_idstart (*p
))
1530 /* Find the end of the identifier. */
1532 while (q
< limit
&& is_idchar (*q
))
1537 /* Loop over the function macro arguments to see if the
1538 identifier inside the string matches one of them. */
1539 for (i
= 0; i
< macro
->paramc
; i
++)
1541 const cpp_hashnode
*node
= macro
->params
[i
];
1543 if (NODE_LEN (node
) == len
1544 && !memcmp (p
, NODE_NAME (node
), len
))
1547 "macro argument \"%s\" would be stringified with -traditional.",
1555 /* Returns the name, arguments and expansion of a macro, in a format
1556 suitable to be read back in again, and therefore also for DWARF 2
1557 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1558 Caller is expected to generate the "#define" bit if needed. The
1559 returned text is temporary, and automatically freed later. */
1561 const unsigned char *
1562 cpp_macro_definition (pfile
, node
)
1564 const cpp_hashnode
*node
;
1566 unsigned int i
, len
;
1567 const cpp_macro
*macro
= node
->value
.macro
;
1568 unsigned char *buffer
;
1570 if (node
->type
!= NT_MACRO
|| (node
->flags
& NODE_BUILTIN
))
1572 cpp_ice (pfile
, "invalid hash type %d in cpp_macro_definition", node
->type
);
1576 /* Calculate length. */
1577 len
= NODE_LEN (node
) + 1; /* ' ' */
1578 if (macro
->fun_like
)
1580 len
+= 3; /* "()" plus possible final "." of named
1581 varargs (we have + 2 below). */
1582 for (i
= 0; i
< macro
->paramc
; i
++)
1583 len
+= NODE_LEN (macro
->params
[i
]) + 2; /* ", " */
1586 for (i
= 0; i
< macro
->count
; i
++)
1588 cpp_token
*token
= ¯o
->expansion
[i
];
1590 if (token
->type
== CPP_MACRO_ARG
)
1591 len
+= NODE_LEN (macro
->params
[token
->val
.arg_no
- 1]);
1593 len
+= cpp_token_len (token
); /* Includes room for ' '. */
1594 if (token
->flags
& STRINGIFY_ARG
)
1596 if (token
->flags
& PASTE_LEFT
)
1597 len
+= 3; /* " ##" */
1600 if (len
> pfile
->macro_buffer_len
)
1602 pfile
->macro_buffer
= (U_CHAR
*) xrealloc (pfile
->macro_buffer
, len
);
1603 pfile
->macro_buffer_len
= len
;
1606 /* Fill in the buffer. Start with the macro name. */
1607 buffer
= pfile
->macro_buffer
;
1608 memcpy (buffer
, NODE_NAME (node
), NODE_LEN (node
));
1609 buffer
+= NODE_LEN (node
);
1611 /* Parameter names. */
1612 if (macro
->fun_like
)
1615 for (i
= 0; i
< macro
->paramc
; i
++)
1617 cpp_hashnode
*param
= macro
->params
[i
];
1619 if (param
!= pfile
->spec_nodes
.n__VA_ARGS__
)
1621 memcpy (buffer
, NODE_NAME (param
), NODE_LEN (param
));
1622 buffer
+= NODE_LEN (param
);
1625 if (i
+ 1 < macro
->paramc
)
1626 *buffer
++ = ',', *buffer
++ = ' ';
1627 else if (macro
->variadic
)
1628 *buffer
++ = '.', *buffer
++ = '.', *buffer
++ = '.';
1633 /* Expansion tokens. */
1637 for (i
= 0; i
< macro
->count
; i
++)
1639 cpp_token
*token
= ¯o
->expansion
[i
];
1641 if (token
->flags
& PREV_WHITE
)
1643 if (token
->flags
& STRINGIFY_ARG
)
1646 if (token
->type
== CPP_MACRO_ARG
)
1648 len
= NODE_LEN (macro
->params
[token
->val
.arg_no
- 1]);
1650 NODE_NAME (macro
->params
[token
->val
.arg_no
- 1]), len
);
1654 buffer
= cpp_spell_token (pfile
, token
, buffer
);
1656 if (token
->flags
& PASTE_LEFT
)
1661 /* Next has PREV_WHITE; see _cpp_create_definition. */
1667 return pfile
->macro_buffer
;