1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 Contributed by Per Bothner, 1994-95.
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. */
24 #include "coretypes.h"
31 /* Chained list of answers to an assertion. */
39 /* Stack of conditionals currently in progress
40 (including both successful and failing conditionals). */
43 struct if_stack
*next
;
44 unsigned int line
; /* Line where condition started. */
45 const cpp_hashnode
*mi_cmacro
;/* macro name for #ifndef around entire file */
46 bool skip_elses
; /* Can future #else / #elif be skipped? */
47 bool was_skipping
; /* If were skipping on entry. */
48 int type
; /* Most recent conditional, for diagnostics. */
51 /* Contains a registered pragma or pragma namespace. */
52 typedef void (*pragma_cb
) PARAMS ((cpp_reader
*));
55 struct pragma_entry
*next
;
56 const cpp_hashnode
*pragma
; /* Name and length. */
60 struct pragma_entry
*space
;
64 /* Values for the origin field of struct directive. KANDR directives
65 come from traditional (K&R) C. STDC89 directives come from the
66 1989 C standard. EXTENSION directives are extensions. */
71 /* Values for the flags field of struct directive. COND indicates a
72 conditional; IF_COND an opening conditional. INCL means to treat
73 "..." and <...> as q-char and h-char sequences respectively. IN_I
74 means this directive should be handled even if -fpreprocessed is in
75 effect (these are the directives with callback hooks).
77 EXPAND is set on directives that are always macro-expanded. */
79 #define IF_COND (1 << 1)
82 #define EXPAND (1 << 4)
84 /* Defines one #-directive, including how to handle it. */
85 typedef void (*directive_handler
) PARAMS ((cpp_reader
*));
86 typedef struct directive directive
;
89 directive_handler handler
; /* Function to handle directive. */
90 const uchar
*name
; /* Name of directive. */
91 unsigned short length
; /* Length of name. */
92 unsigned char origin
; /* Origin of directive. */
93 unsigned char flags
; /* Flags describing this directive. */
96 /* Forward declarations. */
98 static void skip_rest_of_line
PARAMS ((cpp_reader
*));
99 static void check_eol
PARAMS ((cpp_reader
*));
100 static void start_directive
PARAMS ((cpp_reader
*));
101 static void prepare_directive_trad
PARAMS ((cpp_reader
*));
102 static void end_directive
PARAMS ((cpp_reader
*, int));
103 static void directive_diagnostics
104 PARAMS ((cpp_reader
*, const directive
*, int));
105 static void run_directive
PARAMS ((cpp_reader
*, int,
106 const char *, size_t));
107 static const cpp_token
*glue_header_name
PARAMS ((cpp_reader
*));
108 static const cpp_token
*parse_include
PARAMS ((cpp_reader
*));
109 static void push_conditional
PARAMS ((cpp_reader
*, int, int,
110 const cpp_hashnode
*));
111 static unsigned int read_flag
PARAMS ((cpp_reader
*, unsigned int));
112 static uchar
*dequote_string
PARAMS ((cpp_reader
*, const uchar
*,
114 static int strtoul_for_line
PARAMS ((const uchar
*, unsigned int,
116 static void do_diagnostic
PARAMS ((cpp_reader
*, int, int));
117 static cpp_hashnode
*lex_macro_node
PARAMS ((cpp_reader
*));
118 static void do_include_common
PARAMS ((cpp_reader
*, enum include_type
));
119 static struct pragma_entry
*lookup_pragma_entry
120 PARAMS ((struct pragma_entry
*, const cpp_hashnode
*pragma
));
121 static struct pragma_entry
*insert_pragma_entry
122 PARAMS ((cpp_reader
*, struct pragma_entry
**, const cpp_hashnode
*,
124 static int count_registered_pragmas
PARAMS ((struct pragma_entry
*));
125 static char ** save_registered_pragmas
126 PARAMS ((struct pragma_entry
*, char **));
127 static char ** restore_registered_pragmas
128 PARAMS ((cpp_reader
*, struct pragma_entry
*, char **));
129 static void do_pragma_once
PARAMS ((cpp_reader
*));
130 static void do_pragma_poison
PARAMS ((cpp_reader
*));
131 static void do_pragma_system_header
PARAMS ((cpp_reader
*));
132 static void do_pragma_dependency
PARAMS ((cpp_reader
*));
133 static void do_linemarker
PARAMS ((cpp_reader
*));
134 static const cpp_token
*get_token_no_padding
PARAMS ((cpp_reader
*));
135 static const cpp_token
*get__Pragma_string
PARAMS ((cpp_reader
*));
136 static void destringize_and_run
PARAMS ((cpp_reader
*, const cpp_string
*));
137 static int parse_answer
PARAMS ((cpp_reader
*, struct answer
**, int));
138 static cpp_hashnode
*parse_assertion
PARAMS ((cpp_reader
*, struct answer
**,
140 static struct answer
** find_answer
PARAMS ((cpp_hashnode
*,
141 const struct answer
*));
142 static void handle_assertion
PARAMS ((cpp_reader
*, const char *, int));
144 /* This is the table of directive handlers. It is ordered by
145 frequency of occurrence; the numbers at the end are directive
146 counts from all the source code I have lying around (egcs and libc
147 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
148 pcmcia-cs-3.0.9). This is no longer important as directive lookup
149 is now O(1). All extensions other than #warning and #include_next
150 are deprecated. The name is where the extension appears to have
153 #define DIRECTIVE_TABLE \
154 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
155 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
156 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
157 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
158 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
159 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
160 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
161 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
162 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
163 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
164 D(error, T_ERROR, STDC89, 0) /* 475 */ \
165 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
166 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
167 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
168 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
169 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
170 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
171 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
172 D(sccs, T_SCCS, EXTENSION, 0) /* 0 SVR4? */
174 /* Use the table to generate a series of prototypes, an enum for the
175 directive names, and an array of directive handlers. */
177 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
178 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
182 #define D(n, tag, o, f) tag,
190 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
191 #define D(name, t, origin, flags) \
192 { CONCAT2(do_,name), (const uchar *) STRINGX(name), \
193 sizeof STRINGX(name) - 1, origin, flags },
194 static const directive dtable
[] =
199 #undef DIRECTIVE_TABLE
201 /* Wrapper struct directive for linemarkers.
202 The origin is more or less true - the original K+R cpp
203 did use this notation in its preprocessed output. */
204 static const directive linemarker_dir
=
206 do_linemarker
, U
"#", 1, KANDR
, IN_I
209 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
211 /* Skip any remaining tokens in a directive. */
213 skip_rest_of_line (pfile
)
216 /* Discard all stacked contexts. */
217 while (pfile
->context
->prev
)
218 _cpp_pop_context (pfile
);
220 /* Sweep up all tokens remaining on the line. */
222 while (_cpp_lex_token (pfile
)->type
!= CPP_EOF
)
226 /* Ensure there are no stray tokens at the end of a directive. */
231 if (! SEEN_EOL () && _cpp_lex_token (pfile
)->type
!= CPP_EOF
)
232 cpp_error (pfile
, DL_PEDWARN
, "extra tokens at end of #%s directive",
233 pfile
->directive
->name
);
236 /* Called when entering a directive, _Pragma or command-line directive. */
238 start_directive (pfile
)
241 /* Setup in-directive state. */
242 pfile
->state
.in_directive
= 1;
243 pfile
->state
.save_comments
= 0;
245 /* Some handlers need the position of the # for diagnostics. */
246 pfile
->directive_line
= pfile
->line
;
249 /* Called when leaving a directive, _Pragma or command-line directive. */
251 end_directive (pfile
, skip_line
)
255 if (CPP_OPTION (pfile
, traditional
))
257 /* Revert change of prepare_directive_trad. */
258 pfile
->state
.prevent_expansion
--;
260 if (pfile
->directive
!= &dtable
[T_DEFINE
])
261 _cpp_remove_overlay (pfile
);
263 /* We don't skip for an assembler #. */
266 skip_rest_of_line (pfile
);
267 if (!pfile
->keep_tokens
)
269 pfile
->cur_run
= &pfile
->base_run
;
270 pfile
->cur_token
= pfile
->base_run
.base
;
275 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
276 pfile
->state
.in_directive
= 0;
277 pfile
->state
.in_expression
= 0;
278 pfile
->state
.angled_headers
= 0;
279 pfile
->directive
= 0;
282 /* Prepare to handle the directive in pfile->directive. */
284 prepare_directive_trad (pfile
)
287 if (pfile
->directive
!= &dtable
[T_DEFINE
])
289 bool no_expand
= (pfile
->directive
290 && ! (pfile
->directive
->flags
& EXPAND
));
291 bool was_skipping
= pfile
->state
.skipping
;
293 pfile
->state
.skipping
= false;
294 pfile
->state
.in_expression
= (pfile
->directive
== &dtable
[T_IF
]
295 || pfile
->directive
== &dtable
[T_ELIF
]);
297 pfile
->state
.prevent_expansion
++;
298 _cpp_read_logical_line_trad (pfile
);
300 pfile
->state
.prevent_expansion
--;
301 pfile
->state
.skipping
= was_skipping
;
302 _cpp_overlay_buffer (pfile
, pfile
->out
.base
,
303 pfile
->out
.cur
- pfile
->out
.base
);
306 /* Stop ISO C from expanding anything. */
307 pfile
->state
.prevent_expansion
++;
310 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
311 the '#' was indented. */
313 directive_diagnostics (pfile
, dir
, indented
)
315 const directive
*dir
;
318 /* Issue -pedantic warnings for extensions. */
319 if (CPP_PEDANTIC (pfile
)
320 && ! pfile
->state
.skipping
321 && dir
->origin
== EXTENSION
)
322 cpp_error (pfile
, DL_PEDWARN
, "#%s is a GCC extension", dir
->name
);
324 /* Traditionally, a directive is ignored unless its # is in
325 column 1. Therefore in code intended to work with K+R
326 compilers, directives added by C89 must have their #
327 indented, and directives present in traditional C must not.
328 This is true even of directives in skipped conditional
329 blocks. #elif cannot be used at all. */
330 if (CPP_WTRADITIONAL (pfile
))
332 if (dir
== &dtable
[T_ELIF
])
333 cpp_error (pfile
, DL_WARNING
,
334 "suggest not using #elif in traditional C");
335 else if (indented
&& dir
->origin
== KANDR
)
336 cpp_error (pfile
, DL_WARNING
,
337 "traditional C ignores #%s with the # indented",
339 else if (!indented
&& dir
->origin
!= KANDR
)
340 cpp_error (pfile
, DL_WARNING
,
341 "suggest hiding #%s from traditional C with an indented #",
346 /* Check if we have a known directive. INDENTED is nonzero if the
347 '#' of the directive was indented. This function is in this file
348 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
349 nonzero if the line of tokens has been handled, zero if we should
350 continue processing the line. */
352 _cpp_handle_directive (pfile
, indented
)
356 const directive
*dir
= 0;
357 const cpp_token
*dname
;
358 bool was_parsing_args
= pfile
->state
.parsing_args
;
361 if (was_parsing_args
)
363 if (CPP_OPTION (pfile
, pedantic
))
364 cpp_error (pfile
, DL_PEDWARN
,
365 "embedding a directive within macro arguments is not portable");
366 pfile
->state
.parsing_args
= 0;
367 pfile
->state
.prevent_expansion
= 0;
369 start_directive (pfile
);
370 dname
= _cpp_lex_token (pfile
);
372 if (dname
->type
== CPP_NAME
)
374 if (dname
->val
.node
->is_directive
)
375 dir
= &dtable
[dname
->val
.node
->directive_index
];
377 /* We do not recognize the # followed by a number extension in
379 else if (dname
->type
== CPP_NUMBER
&& CPP_OPTION (pfile
, lang
) != CLK_ASM
)
381 dir
= &linemarker_dir
;
382 if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, preprocessed
)
383 && ! pfile
->state
.skipping
)
384 cpp_error (pfile
, DL_PEDWARN
,
385 "style of line directive is a GCC extension");
390 /* If we have a directive that is not an opening conditional,
391 invalidate any control macro. */
392 if (! (dir
->flags
& IF_COND
))
393 pfile
->mi_valid
= false;
395 /* Kluge alert. In order to be sure that code like this
400 does not cause '#define foo bar' to get executed when
401 compiled with -save-temps, we recognize directives in
402 -fpreprocessed mode only if the # is in column 1. cppmacro.c
403 puts a space in front of any '#' at the start of a macro. */
404 if (CPP_OPTION (pfile
, preprocessed
)
405 && (indented
|| !(dir
->flags
& IN_I
)))
412 /* In failed conditional groups, all non-conditional
413 directives are ignored. Before doing that, whether
414 skipping or not, we should lex angle-bracketed headers
415 correctly, and maybe output some diagnostics. */
416 pfile
->state
.angled_headers
= dir
->flags
& INCL
;
417 if (! CPP_OPTION (pfile
, preprocessed
))
418 directive_diagnostics (pfile
, dir
, indented
);
419 if (pfile
->state
.skipping
&& !(dir
->flags
& COND
))
423 else if (dname
->type
== CPP_EOF
)
424 ; /* CPP_EOF is the "null directive". */
427 /* An unknown directive. Don't complain about it in assembly
428 source: we don't know where the comments are, and # may
429 introduce assembler pseudo-ops. Don't complain about invalid
430 directives in skipped conditional groups (6.10 p4). */
431 if (CPP_OPTION (pfile
, lang
) == CLK_ASM
)
433 else if (!pfile
->state
.skipping
)
434 cpp_error (pfile
, DL_ERROR
, "invalid preprocessing directive #%s",
435 cpp_token_as_text (pfile
, dname
));
438 pfile
->directive
= dir
;
439 if (CPP_OPTION (pfile
, traditional
))
440 prepare_directive_trad (pfile
);
443 (*pfile
->directive
->handler
) (pfile
);
445 _cpp_backup_tokens (pfile
, 1);
447 end_directive (pfile
, skip
);
448 if (was_parsing_args
)
450 /* Restore state when within macro args. */
451 pfile
->state
.parsing_args
= 2;
452 pfile
->state
.prevent_expansion
= 1;
453 pfile
->buffer
->saved_flags
|= PREV_WHITE
;
458 /* Directive handler wrapper used by the command line option
461 run_directive (pfile
, dir_no
, buf
, count
)
467 cpp_push_buffer (pfile
, (const uchar
*) buf
, count
,
468 /* from_stage3 */ true, 1);
469 /* Disgusting hack. */
470 if (dir_no
== T_PRAGMA
)
471 pfile
->buffer
->inc
= pfile
->buffer
->prev
->inc
;
472 start_directive (pfile
);
473 /* We don't want a leading # to be interpreted as a directive. */
474 pfile
->buffer
->saved_flags
= 0;
475 pfile
->directive
= &dtable
[dir_no
];
476 if (CPP_OPTION (pfile
, traditional
))
477 prepare_directive_trad (pfile
);
478 (void) (*pfile
->directive
->handler
) (pfile
);
479 end_directive (pfile
, 1);
480 if (dir_no
== T_PRAGMA
)
481 pfile
->buffer
->inc
= NULL
;
482 _cpp_pop_buffer (pfile
);
485 /* Checks for validity the macro name in #define, #undef, #ifdef and
486 #ifndef directives. */
487 static cpp_hashnode
*
488 lex_macro_node (pfile
)
491 const cpp_token
*token
= _cpp_lex_token (pfile
);
493 /* The token immediately after #define must be an identifier. That
494 identifier may not be "defined", per C99 6.10.8p4.
495 In C++, it may not be any of the "named operators" either,
496 per C++98 [lex.digraph], [lex.key].
497 Finally, the identifier may not have been poisoned. (In that case
498 the lexer has issued the error message for us.) */
500 if (token
->type
== CPP_NAME
)
502 cpp_hashnode
*node
= token
->val
.node
;
504 if (node
== pfile
->spec_nodes
.n_defined
)
505 cpp_error (pfile
, DL_ERROR
,
506 "\"defined\" cannot be used as a macro name");
507 else if (! (node
->flags
& NODE_POISONED
))
510 else if (token
->flags
& NAMED_OP
)
511 cpp_error (pfile
, DL_ERROR
,
512 "\"%s\" cannot be used as a macro name as it is an operator in C++",
513 NODE_NAME (token
->val
.node
));
514 else if (token
->type
== CPP_EOF
)
515 cpp_error (pfile
, DL_ERROR
, "no macro name given in #%s directive",
516 pfile
->directive
->name
);
518 cpp_error (pfile
, DL_ERROR
, "macro names must be identifiers");
523 /* Process a #define directive. Most work is done in cppmacro.c. */
528 cpp_hashnode
*node
= lex_macro_node (pfile
);
532 /* If we have been requested to expand comments into macros,
533 then re-enable saving of comments. */
534 pfile
->state
.save_comments
=
535 ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
);
537 if (_cpp_create_definition (pfile
, node
))
538 if (pfile
->cb
.define
)
539 (*pfile
->cb
.define
) (pfile
, pfile
->directive_line
, node
);
543 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
548 cpp_hashnode
*node
= lex_macro_node (pfile
);
550 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
551 is not currently defined as a macro name. */
552 if (node
&& node
->type
== NT_MACRO
)
555 (*pfile
->cb
.undef
) (pfile
, pfile
->directive_line
, node
);
557 if (node
->flags
& NODE_WARN
)
558 cpp_error (pfile
, DL_WARNING
, "undefining \"%s\"", NODE_NAME (node
));
560 if (CPP_OPTION (pfile
, warn_unused_macros
))
561 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
563 _cpp_free_definition (node
);
568 /* Helper routine used by parse_include. Reinterpret the current line
569 as an h-char-sequence (< ... >); we are looking at the first token
570 after the <. Returns the header as a token, or NULL on failure. */
571 static const cpp_token
*
572 glue_header_name (pfile
)
575 cpp_token
*header
= NULL
;
576 const cpp_token
*token
;
577 unsigned char *buffer
;
578 size_t len
, total_len
= 0, capacity
= 1024;
580 /* To avoid lexed tokens overwriting our glued name, we can only
581 allocate from the string pool once we've lexed everything. */
582 buffer
= (unsigned char *) xmalloc (capacity
);
585 token
= cpp_get_token (pfile
);
587 if (token
->type
== CPP_GREATER
|| token
->type
== CPP_EOF
)
590 len
= cpp_token_len (token
);
591 if (total_len
+ len
> capacity
)
593 capacity
= (capacity
+ len
) * 2;
594 buffer
= (unsigned char *) xrealloc (buffer
, capacity
);
597 if (token
->flags
& PREV_WHITE
)
598 buffer
[total_len
++] = ' ';
600 total_len
= cpp_spell_token (pfile
, token
, &buffer
[total_len
]) - buffer
;
603 if (token
->type
== CPP_EOF
)
604 cpp_error (pfile
, DL_ERROR
, "missing terminating > character");
607 unsigned char *token_mem
= _cpp_unaligned_alloc (pfile
, total_len
+ 1);
608 memcpy (token_mem
, buffer
, total_len
);
609 token_mem
[total_len
] = '\0';
611 header
= _cpp_temp_token (pfile
);
612 header
->type
= CPP_HEADER_NAME
;
614 header
->val
.str
.len
= total_len
;
615 header
->val
.str
.text
= token_mem
;
622 /* Returns the header string of #include, #include_next, #import and
623 #pragma dependency. Returns NULL on error. */
624 static const cpp_token
*
625 parse_include (pfile
)
628 const unsigned char *dir
;
629 const cpp_token
*header
;
631 if (pfile
->directive
== &dtable
[T_PRAGMA
])
632 dir
= U
"pragma dependency";
634 dir
= pfile
->directive
->name
;
636 /* Allow macro expansion. */
637 header
= cpp_get_token (pfile
);
638 if (header
->type
!= CPP_STRING
&& header
->type
!= CPP_HEADER_NAME
)
640 if (header
->type
!= CPP_LESS
)
642 cpp_error (pfile
, DL_ERROR
,
643 "#%s expects \"FILENAME\" or <FILENAME>", dir
);
647 header
= glue_header_name (pfile
);
652 if (header
->val
.str
.len
== 0)
654 cpp_error (pfile
, DL_ERROR
, "empty file name in #%s", dir
);
661 /* Handle #include, #include_next and #import. */
663 do_include_common (pfile
, type
)
665 enum include_type type
;
667 const cpp_token
*header
;
669 /* For #include_next, if this is the primary source file, warn and
670 use the normal search logic. */
671 if (type
== IT_INCLUDE_NEXT
&& ! pfile
->buffer
->prev
)
673 cpp_error (pfile
, DL_WARNING
, "#include_next in primary source file");
676 else if (type
== IT_IMPORT
&& CPP_OPTION (pfile
, warn_import
))
678 CPP_OPTION (pfile
, warn_import
) = 0;
679 cpp_error (pfile
, DL_WARNING
,
680 "#import is obsolete, use an #ifndef wrapper in the header file");
683 header
= parse_include (pfile
);
686 /* Prevent #include recursion. */
687 if (pfile
->line_maps
.depth
>= CPP_STACK_MAX
)
688 cpp_error (pfile
, DL_ERROR
, "#include nested too deeply");
692 /* Get out of macro context, if we are. */
693 skip_rest_of_line (pfile
);
694 if (pfile
->cb
.include
)
695 (*pfile
->cb
.include
) (pfile
, pfile
->directive_line
,
696 pfile
->directive
->name
, header
);
697 _cpp_execute_include (pfile
, header
, type
);
706 do_include_common (pfile
, IT_INCLUDE
);
713 do_include_common (pfile
, IT_IMPORT
);
717 do_include_next (pfile
)
720 do_include_common (pfile
, IT_INCLUDE_NEXT
);
723 /* Subroutine of do_linemarker. Read possible flags after file name.
724 LAST is the last flag seen; 0 if this is the first flag. Return the
725 flag if it is valid, 0 at the end of the directive. Otherwise
728 read_flag (pfile
, last
)
732 const cpp_token
*token
= _cpp_lex_token (pfile
);
734 if (token
->type
== CPP_NUMBER
&& token
->val
.str
.len
== 1)
736 unsigned int flag
= token
->val
.str
.text
[0] - '0';
738 if (flag
> last
&& flag
<= 4
739 && (flag
!= 4 || last
== 3)
740 && (flag
!= 2 || last
== 0))
744 if (token
->type
!= CPP_EOF
)
745 cpp_error (pfile
, DL_ERROR
, "invalid flag \"%s\" in line directive",
746 cpp_token_as_text (pfile
, token
));
750 /* Subroutine of do_line and do_linemarker. Returns a version of STR
751 which has a NUL terminator and all escape sequences converted to
752 their equivalents. Temporary, hopefully. */
754 dequote_string (pfile
, str
, len
)
759 uchar
*result
= _cpp_unaligned_alloc (pfile
, len
+ 1);
761 const uchar
*limit
= str
+ len
;
770 *dst
++ = cpp_parse_escape (pfile
, &str
, limit
, 0);
776 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
777 of length LEN, to binary; store it in NUMP, and return 0 if the
778 number was well-formed, 1 if not. Temporary, hopefully. */
780 strtoul_for_line (str
, len
, nump
)
785 unsigned long reg
= 0;
799 /* Interpret #line command.
800 Note that the filename string (if any) is a true string constant
801 (escapes are interpreted), unlike in #line. */
806 const cpp_token
*token
;
807 const char *new_file
= pfile
->map
->to_file
;
808 unsigned long new_lineno
;
810 /* C99 raised the minimum limit on #line numbers. */
811 unsigned int cap
= CPP_OPTION (pfile
, c99
) ? 2147483647 : 32767;
813 /* #line commands expand macros. */
814 token
= cpp_get_token (pfile
);
815 if (token
->type
!= CPP_NUMBER
816 || strtoul_for_line (token
->val
.str
.text
, token
->val
.str
.len
,
819 cpp_error (pfile
, DL_ERROR
,
820 "\"%s\" after #line is not a positive integer",
821 cpp_token_as_text (pfile
, token
));
825 if (CPP_PEDANTIC (pfile
) && (new_lineno
== 0 || new_lineno
> cap
))
826 cpp_error (pfile
, DL_PEDWARN
, "line number out of range");
828 token
= cpp_get_token (pfile
);
829 if (token
->type
== CPP_STRING
)
831 new_file
= (const char *) dequote_string (pfile
, token
->val
.str
.text
,
835 else if (token
->type
!= CPP_EOF
)
837 cpp_error (pfile
, DL_ERROR
, "\"%s\" is not a valid filename",
838 cpp_token_as_text (pfile
, token
));
842 skip_rest_of_line (pfile
);
843 _cpp_do_file_change (pfile
, LC_RENAME
, new_file
, new_lineno
,
847 /* Interpret the # 44 "file" [flags] notation, which has slightly
848 different syntax and semantics from #line: Flags are allowed,
849 and we never complain about the line number being too big. */
851 do_linemarker (pfile
)
854 const cpp_token
*token
;
855 const char *new_file
= pfile
->map
->to_file
;
856 unsigned long new_lineno
;
857 unsigned int new_sysp
= pfile
->map
->sysp
;
858 enum lc_reason reason
= LC_RENAME
;
861 /* Back up so we can get the number again. Putting this in
862 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
863 some circumstances, which can segfault. */
864 _cpp_backup_tokens (pfile
, 1);
866 /* #line commands expand macros. */
867 token
= cpp_get_token (pfile
);
868 if (token
->type
!= CPP_NUMBER
869 || strtoul_for_line (token
->val
.str
.text
, token
->val
.str
.len
,
872 cpp_error (pfile
, DL_ERROR
, "\"%s\" after # is not a positive integer",
873 cpp_token_as_text (pfile
, token
));
877 token
= cpp_get_token (pfile
);
878 if (token
->type
== CPP_STRING
)
880 new_file
= (const char *) dequote_string (pfile
, token
->val
.str
.text
,
883 flag
= read_flag (pfile
, 0);
887 /* Fake an include for cpp_included (). */
888 _cpp_fake_include (pfile
, new_file
);
889 flag
= read_flag (pfile
, flag
);
894 flag
= read_flag (pfile
, flag
);
899 flag
= read_flag (pfile
, flag
);
906 else if (token
->type
!= CPP_EOF
)
908 cpp_error (pfile
, DL_ERROR
, "\"%s\" is not a valid filename",
909 cpp_token_as_text (pfile
, token
));
913 skip_rest_of_line (pfile
);
914 _cpp_do_file_change (pfile
, reason
, new_file
, new_lineno
, new_sysp
);
917 /* Arrange the file_change callback. pfile->line has changed to
918 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
919 header, 2 for a system header that needs to be extern "C" protected,
920 and zero otherwise. */
922 _cpp_do_file_change (pfile
, reason
, to_file
, file_line
, sysp
)
924 enum lc_reason reason
;
926 unsigned int file_line
;
929 pfile
->map
= add_line_map (&pfile
->line_maps
, reason
, sysp
,
930 pfile
->line
, to_file
, file_line
);
932 if (pfile
->cb
.file_change
)
933 (*pfile
->cb
.file_change
) (pfile
, pfile
->map
);
936 /* Report a warning or error detected by the program we are
937 processing. Use the directive's tokens in the error message. */
939 do_diagnostic (pfile
, code
, print_dir
)
944 if (_cpp_begin_message (pfile
, code
,
945 pfile
->cur_token
[-1].line
,
946 pfile
->cur_token
[-1].col
))
949 fprintf (stderr
, "#%s ", pfile
->directive
->name
);
950 pfile
->state
.prevent_expansion
++;
951 cpp_output_line (pfile
, stderr
);
952 pfile
->state
.prevent_expansion
--;
960 do_diagnostic (pfile
, DL_ERROR
, 1);
967 /* We want #warning diagnostics to be emitted in system headers too. */
968 do_diagnostic (pfile
, DL_WARNING_SYSHDR
, 1);
971 /* Report program identification. */
976 const cpp_token
*str
= cpp_get_token (pfile
);
978 if (str
->type
!= CPP_STRING
)
979 cpp_error (pfile
, DL_ERROR
, "invalid #ident directive");
980 else if (pfile
->cb
.ident
)
981 (*pfile
->cb
.ident
) (pfile
, pfile
->directive_line
, &str
->val
.str
);
986 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
987 matching entry, or NULL if none is found. The returned entry could
988 be the start of a namespace chain, or a pragma. */
989 static struct pragma_entry
*
990 lookup_pragma_entry (chain
, pragma
)
991 struct pragma_entry
*chain
;
992 const cpp_hashnode
*pragma
;
994 while (chain
&& chain
->pragma
!= pragma
)
1000 /* Create and insert a pragma entry for NAME at the beginning of a
1001 singly-linked CHAIN. If handler is NULL, it is a namespace,
1002 otherwise it is a pragma and its handler. */
1003 static struct pragma_entry
*
1004 insert_pragma_entry (pfile
, chain
, pragma
, handler
)
1006 struct pragma_entry
**chain
;
1007 const cpp_hashnode
*pragma
;
1010 struct pragma_entry
*new;
1012 new = (struct pragma_entry
*)
1013 _cpp_aligned_alloc (pfile
, sizeof (struct pragma_entry
));
1014 new->pragma
= pragma
;
1018 new->u
.handler
= handler
;
1023 new->u
.space
= NULL
;
1031 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1032 goes in the global namespace. HANDLER is the handler it will call,
1033 which must be non-NULL. */
1035 cpp_register_pragma (pfile
, space
, name
, handler
)
1041 struct pragma_entry
**chain
= &pfile
->pragmas
;
1042 struct pragma_entry
*entry
;
1043 const cpp_hashnode
*node
;
1050 node
= cpp_lookup (pfile
, U space
, strlen (space
));
1051 entry
= lookup_pragma_entry (*chain
, node
);
1053 entry
= insert_pragma_entry (pfile
, chain
, node
, NULL
);
1054 else if (!entry
->is_nspace
)
1056 chain
= &entry
->u
.space
;
1059 /* Check for duplicates. */
1060 node
= cpp_lookup (pfile
, U name
, strlen (name
));
1061 entry
= lookup_pragma_entry (*chain
, node
);
1064 if (entry
->is_nspace
)
1066 cpp_error (pfile
, DL_ICE
,
1067 "registering \"%s\" as both a pragma and a pragma namespace",
1070 cpp_error (pfile
, DL_ICE
, "#pragma %s %s is already registered",
1073 cpp_error (pfile
, DL_ICE
, "#pragma %s is already registered", name
);
1076 insert_pragma_entry (pfile
, chain
, node
, handler
);
1079 /* Register the pragmas the preprocessor itself handles. */
1081 _cpp_init_internal_pragmas (pfile
)
1084 /* Pragmas in the global namespace. */
1085 cpp_register_pragma (pfile
, 0, "once", do_pragma_once
);
1087 /* New GCC-specific pragmas should be put in the GCC namespace. */
1088 cpp_register_pragma (pfile
, "GCC", "poison", do_pragma_poison
);
1089 cpp_register_pragma (pfile
, "GCC", "system_header", do_pragma_system_header
);
1090 cpp_register_pragma (pfile
, "GCC", "dependency", do_pragma_dependency
);
1093 /* Return the number of registered pragmas in PE. */
1096 count_registered_pragmas (pe
)
1097 struct pragma_entry
*pe
;
1100 for (; pe
!= NULL
; pe
= pe
->next
)
1103 ct
+= count_registered_pragmas (pe
->u
.space
);
1109 /* Save into SD the names of the registered pragmas referenced by PE,
1110 and return a pointer to the next free space in SD. */
1113 save_registered_pragmas (pe
, sd
)
1114 struct pragma_entry
*pe
;
1117 for (; pe
!= NULL
; pe
= pe
->next
)
1120 sd
= save_registered_pragmas (pe
->u
.space
, sd
);
1121 *sd
++ = xmemdup (HT_STR (&pe
->pragma
->ident
),
1122 HT_LEN (&pe
->pragma
->ident
),
1123 HT_LEN (&pe
->pragma
->ident
) + 1);
1128 /* Return a newly-allocated array which saves the names of the
1129 registered pragmas. */
1132 _cpp_save_pragma_names (pfile
)
1135 int ct
= count_registered_pragmas (pfile
->pragmas
);
1136 char **result
= xnewvec (char *, ct
);
1137 (void) save_registered_pragmas (pfile
->pragmas
, result
);
1141 /* Restore from SD the names of the registered pragmas referenced by PE,
1142 and return a pointer to the next unused name in SD. */
1145 restore_registered_pragmas (pfile
, pe
, sd
)
1147 struct pragma_entry
*pe
;
1150 for (; pe
!= NULL
; pe
= pe
->next
)
1153 sd
= restore_registered_pragmas (pfile
, pe
->u
.space
, sd
);
1154 pe
->pragma
= cpp_lookup (pfile
, U
*sd
, strlen (*sd
));
1161 /* Restore the names of the registered pragmas from SAVED. */
1164 _cpp_restore_pragma_names (pfile
, saved
)
1168 (void) restore_registered_pragmas (pfile
, pfile
->pragmas
, saved
);
1172 /* Pragmata handling. We handle some, and pass the rest on to the
1173 front end. C99 defines three pragmas and says that no macro
1174 expansion is to be performed on them; whether or not macro
1175 expansion happens for other pragmas is implementation defined.
1176 This implementation never macro-expands the text after #pragma. */
1181 const struct pragma_entry
*p
= NULL
;
1182 const cpp_token
*token
;
1183 unsigned int count
= 1;
1185 pfile
->state
.prevent_expansion
++;
1187 token
= cpp_get_token (pfile
);
1188 if (token
->type
== CPP_NAME
)
1190 p
= lookup_pragma_entry (pfile
->pragmas
, token
->val
.node
);
1191 if (p
&& p
->is_nspace
)
1194 token
= cpp_get_token (pfile
);
1195 if (token
->type
== CPP_NAME
)
1196 p
= lookup_pragma_entry (p
->u
.space
, token
->val
.node
);
1202 /* FIXME. This is an awful kludge to get the front ends to update
1203 their notion of line number for diagnostic purposes. The line
1204 number should be passed to the handler and they should do it
1205 themselves. Stand-alone CPP must ignore us, otherwise it will
1206 prefix the directive with spaces, hence the 1. Ugh. */
1207 if (pfile
->cb
.line_change
)
1208 (*pfile
->cb
.line_change
)(pfile
, token
, 1);
1211 (*p
->u
.handler
) (pfile
);
1212 else if (pfile
->cb
.def_pragma
)
1214 _cpp_backup_tokens (pfile
, count
);
1215 (*pfile
->cb
.def_pragma
) (pfile
, pfile
->directive_line
);
1218 pfile
->state
.prevent_expansion
--;
1221 /* Handle #pragma once. */
1223 do_pragma_once (pfile
)
1226 cpp_error (pfile
, DL_WARNING
, "#pragma once is obsolete");
1228 if (pfile
->buffer
->prev
== NULL
)
1229 cpp_error (pfile
, DL_WARNING
, "#pragma once in main file");
1231 _cpp_never_reread (pfile
->buffer
->inc
);
1236 /* Handle #pragma GCC poison, to poison one or more identifiers so
1237 that the lexer produces a hard error for each subsequent usage. */
1239 do_pragma_poison (pfile
)
1242 const cpp_token
*tok
;
1245 pfile
->state
.poisoned_ok
= 1;
1248 tok
= _cpp_lex_token (pfile
);
1249 if (tok
->type
== CPP_EOF
)
1251 if (tok
->type
!= CPP_NAME
)
1253 cpp_error (pfile
, DL_ERROR
, "invalid #pragma GCC poison directive");
1258 if (hp
->flags
& NODE_POISONED
)
1261 if (hp
->type
== NT_MACRO
)
1262 cpp_error (pfile
, DL_WARNING
, "poisoning existing macro \"%s\"",
1264 _cpp_free_definition (hp
);
1265 hp
->flags
|= NODE_POISONED
| NODE_DIAGNOSTIC
;
1267 pfile
->state
.poisoned_ok
= 0;
1270 /* Mark the current header as a system header. This will suppress
1271 some categories of warnings (notably those from -pedantic). It is
1272 intended for use in system libraries that cannot be implemented in
1273 conforming C, but cannot be certain that their headers appear in a
1274 system include directory. To prevent abuse, it is rejected in the
1275 primary source file. */
1277 do_pragma_system_header (pfile
)
1280 cpp_buffer
*buffer
= pfile
->buffer
;
1282 if (buffer
->prev
== 0)
1283 cpp_error (pfile
, DL_WARNING
,
1284 "#pragma system_header ignored outside include file");
1288 skip_rest_of_line (pfile
);
1289 cpp_make_system_header (pfile
, 1, 0);
1293 /* Check the modified date of the current include file against a specified
1294 file. Issue a diagnostic, if the specified file is newer. We use this to
1295 determine if a fixed header should be refixed. */
1297 do_pragma_dependency (pfile
)
1300 const cpp_token
*header
;
1303 header
= parse_include (pfile
);
1307 ordering
= _cpp_compare_file_date (pfile
, header
);
1309 cpp_error (pfile
, DL_WARNING
, "cannot find source %s",
1310 cpp_token_as_text (pfile
, header
));
1311 else if (ordering
> 0)
1313 cpp_error (pfile
, DL_WARNING
, "current file is older than %s",
1314 cpp_token_as_text (pfile
, header
));
1315 if (cpp_get_token (pfile
)->type
!= CPP_EOF
)
1317 _cpp_backup_tokens (pfile
, 1);
1318 do_diagnostic (pfile
, DL_WARNING
, 0);
1323 /* Get a token but skip padding. */
1324 static const cpp_token
*
1325 get_token_no_padding (pfile
)
1330 const cpp_token
*result
= cpp_get_token (pfile
);
1331 if (result
->type
!= CPP_PADDING
)
1336 /* Check syntax is "(string-literal)". Returns the string on success,
1337 or NULL on failure. */
1338 static const cpp_token
*
1339 get__Pragma_string (pfile
)
1342 const cpp_token
*string
;
1344 if (get_token_no_padding (pfile
)->type
!= CPP_OPEN_PAREN
)
1347 string
= get_token_no_padding (pfile
);
1348 if (string
->type
!= CPP_STRING
&& string
->type
!= CPP_WSTRING
)
1351 if (get_token_no_padding (pfile
)->type
!= CPP_CLOSE_PAREN
)
1357 /* Destringize IN into a temporary buffer, by removing the first \ of
1358 \" and \\ sequences, and process the result as a #pragma directive. */
1360 destringize_and_run (pfile
, in
)
1362 const cpp_string
*in
;
1364 const unsigned char *src
, *limit
;
1365 char *dest
, *result
;
1367 dest
= result
= alloca (in
->len
+ 1);
1368 for (src
= in
->text
, limit
= src
+ in
->len
; src
< limit
;)
1370 /* We know there is a character following the backslash. */
1371 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1377 /* Ugh; an awful kludge. We are really not set up to be lexing
1378 tokens when in the middle of a macro expansion. Use a new
1379 context to force cpp_get_token to lex, and so skip_rest_of_line
1380 doesn't go beyond the end of the text. Also, remember the
1381 current lexing position so we can return to it later.
1383 Something like line-at-a-time lexing should remove the need for
1386 cpp_context
*saved_context
= pfile
->context
;
1387 cpp_token
*saved_cur_token
= pfile
->cur_token
;
1388 tokenrun
*saved_cur_run
= pfile
->cur_run
;
1390 pfile
->context
= xnew (cpp_context
);
1391 pfile
->context
->macro
= 0;
1392 pfile
->context
->prev
= 0;
1393 run_directive (pfile
, T_PRAGMA
, result
, dest
- result
);
1394 free (pfile
->context
);
1395 pfile
->context
= saved_context
;
1396 pfile
->cur_token
= saved_cur_token
;
1397 pfile
->cur_run
= saved_cur_run
;
1401 /* See above comment. For the moment, we'd like
1403 token1 _Pragma ("foo") token2
1413 Getting the line markers is a little tricky. */
1414 if (pfile
->cb
.line_change
)
1415 (*pfile
->cb
.line_change
) (pfile
, pfile
->cur_token
, false);
1418 /* Handle the _Pragma operator. */
1420 _cpp_do__Pragma (pfile
)
1423 const cpp_token
*string
= get__Pragma_string (pfile
);
1426 destringize_and_run (pfile
, &string
->val
.str
);
1428 cpp_error (pfile
, DL_ERROR
,
1429 "_Pragma takes a parenthesized string literal");
1432 /* Just ignore #sccs on all systems. */
1435 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
1439 /* Handle #ifdef. */
1446 if (! pfile
->state
.skipping
)
1448 const cpp_hashnode
*node
= lex_macro_node (pfile
);
1452 skip
= node
->type
!= NT_MACRO
;
1453 _cpp_mark_macro_used (node
);
1458 push_conditional (pfile
, skip
, T_IFDEF
, 0);
1461 /* Handle #ifndef. */
1467 const cpp_hashnode
*node
= 0;
1469 if (! pfile
->state
.skipping
)
1471 node
= lex_macro_node (pfile
);
1475 skip
= node
->type
== NT_MACRO
;
1476 _cpp_mark_macro_used (node
);
1481 push_conditional (pfile
, skip
, T_IFNDEF
, node
);
1484 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1485 pfile->mi_ind_cmacro so we can handle multiple-include
1486 optimisations. If macro expansion occurs in the expression, we
1487 cannot treat it as a controlling conditional, since the expansion
1488 could change in the future. That is handled by cpp_get_token. */
1495 if (! pfile
->state
.skipping
)
1496 skip
= _cpp_parse_expr (pfile
) == false;
1498 push_conditional (pfile
, skip
, T_IF
, pfile
->mi_ind_cmacro
);
1501 /* Flip skipping state if appropriate and continue without changing
1502 if_stack; this is so that the error message for missing #endif's
1503 etc. will point to the original #if. */
1508 cpp_buffer
*buffer
= pfile
->buffer
;
1509 struct if_stack
*ifs
= buffer
->if_stack
;
1512 cpp_error (pfile
, DL_ERROR
, "#else without #if");
1515 if (ifs
->type
== T_ELSE
)
1517 cpp_error (pfile
, DL_ERROR
, "#else after #else");
1518 cpp_error_with_line (pfile
, DL_ERROR
, ifs
->line
, 0,
1519 "the conditional began here");
1523 /* Skip any future (erroneous) #elses or #elifs. */
1524 pfile
->state
.skipping
= ifs
->skip_elses
;
1525 ifs
->skip_elses
= true;
1527 /* Invalidate any controlling macro. */
1530 /* Only check EOL if was not originally skipping. */
1531 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1536 /* Handle a #elif directive by not changing if_stack either. See the
1537 comment above do_else. */
1542 cpp_buffer
*buffer
= pfile
->buffer
;
1543 struct if_stack
*ifs
= buffer
->if_stack
;
1546 cpp_error (pfile
, DL_ERROR
, "#elif without #if");
1549 if (ifs
->type
== T_ELSE
)
1551 cpp_error (pfile
, DL_ERROR
, "#elif after #else");
1552 cpp_error_with_line (pfile
, DL_ERROR
, ifs
->line
, 0,
1553 "the conditional began here");
1557 /* Only evaluate this if we aren't skipping elses. During
1558 evaluation, set skipping to false to get lexer warnings. */
1559 if (ifs
->skip_elses
)
1560 pfile
->state
.skipping
= 1;
1563 pfile
->state
.skipping
= 0;
1564 pfile
->state
.skipping
= ! _cpp_parse_expr (pfile
);
1565 ifs
->skip_elses
= ! pfile
->state
.skipping
;
1568 /* Invalidate any controlling macro. */
1573 /* #endif pops the if stack and resets pfile->state.skipping. */
1578 cpp_buffer
*buffer
= pfile
->buffer
;
1579 struct if_stack
*ifs
= buffer
->if_stack
;
1582 cpp_error (pfile
, DL_ERROR
, "#endif without #if");
1585 /* Only check EOL if was not originally skipping. */
1586 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1589 /* If potential control macro, we go back outside again. */
1590 if (ifs
->next
== 0 && ifs
->mi_cmacro
)
1592 pfile
->mi_valid
= true;
1593 pfile
->mi_cmacro
= ifs
->mi_cmacro
;
1596 buffer
->if_stack
= ifs
->next
;
1597 pfile
->state
.skipping
= ifs
->was_skipping
;
1598 obstack_free (&pfile
->buffer_ob
, ifs
);
1602 /* Push an if_stack entry for a preprocessor conditional, and set
1603 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1604 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1605 we need to check here that we are at the top of the file. */
1607 push_conditional (pfile
, skip
, type
, cmacro
)
1611 const cpp_hashnode
*cmacro
;
1613 struct if_stack
*ifs
;
1614 cpp_buffer
*buffer
= pfile
->buffer
;
1616 ifs
= xobnew (&pfile
->buffer_ob
, struct if_stack
);
1617 ifs
->line
= pfile
->directive_line
;
1618 ifs
->next
= buffer
->if_stack
;
1619 ifs
->skip_elses
= pfile
->state
.skipping
|| !skip
;
1620 ifs
->was_skipping
= pfile
->state
.skipping
;
1622 /* This condition is effectively a test for top-of-file. */
1623 if (pfile
->mi_valid
&& pfile
->mi_cmacro
== 0)
1624 ifs
->mi_cmacro
= cmacro
;
1628 pfile
->state
.skipping
= skip
;
1629 buffer
->if_stack
= ifs
;
1632 /* Read the tokens of the answer into the macro pool, in a directive
1633 of type TYPE. Only commit the memory if we intend it as permanent
1634 storage, i.e. the #assert case. Returns 0 on success, and sets
1635 ANSWERP to point to the answer. */
1637 parse_answer (pfile
, answerp
, type
)
1639 struct answer
**answerp
;
1642 const cpp_token
*paren
;
1643 struct answer
*answer
;
1644 unsigned int acount
;
1646 /* In a conditional, it is legal to not have an open paren. We
1647 should save the following token in this case. */
1648 paren
= cpp_get_token (pfile
);
1650 /* If not a paren, see if we're OK. */
1651 if (paren
->type
!= CPP_OPEN_PAREN
)
1653 /* In a conditional no answer is a test for any answer. It
1654 could be followed by any token. */
1657 _cpp_backup_tokens (pfile
, 1);
1661 /* #unassert with no answer is valid - it removes all answers. */
1662 if (type
== T_UNASSERT
&& paren
->type
== CPP_EOF
)
1665 cpp_error (pfile
, DL_ERROR
, "missing '(' after predicate");
1669 for (acount
= 0;; acount
++)
1672 const cpp_token
*token
= cpp_get_token (pfile
);
1675 if (token
->type
== CPP_CLOSE_PAREN
)
1678 if (token
->type
== CPP_EOF
)
1680 cpp_error (pfile
, DL_ERROR
, "missing ')' to complete answer");
1684 /* struct answer includes the space for one token. */
1685 room_needed
= (sizeof (struct answer
) + acount
* sizeof (cpp_token
));
1687 if (BUFF_ROOM (pfile
->a_buff
) < room_needed
)
1688 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (struct answer
));
1690 dest
= &((struct answer
*) BUFF_FRONT (pfile
->a_buff
))->first
[acount
];
1693 /* Drop whitespace at start, for answer equivalence purposes. */
1695 dest
->flags
&= ~PREV_WHITE
;
1700 cpp_error (pfile
, DL_ERROR
, "predicate's answer is empty");
1704 answer
= (struct answer
*) BUFF_FRONT (pfile
->a_buff
);
1705 answer
->count
= acount
;
1706 answer
->next
= NULL
;
1712 /* Parses an assertion directive of type TYPE, returning a pointer to
1713 the hash node of the predicate, or 0 on error. If an answer was
1714 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
1715 static cpp_hashnode
*
1716 parse_assertion (pfile
, answerp
, type
)
1718 struct answer
**answerp
;
1721 cpp_hashnode
*result
= 0;
1722 const cpp_token
*predicate
;
1724 /* We don't expand predicates or answers. */
1725 pfile
->state
.prevent_expansion
++;
1728 predicate
= cpp_get_token (pfile
);
1729 if (predicate
->type
== CPP_EOF
)
1730 cpp_error (pfile
, DL_ERROR
, "assertion without predicate");
1731 else if (predicate
->type
!= CPP_NAME
)
1732 cpp_error (pfile
, DL_ERROR
, "predicate must be an identifier");
1733 else if (parse_answer (pfile
, answerp
, type
) == 0)
1735 unsigned int len
= NODE_LEN (predicate
->val
.node
);
1736 unsigned char *sym
= alloca (len
+ 1);
1738 /* Prefix '#' to get it out of macro namespace. */
1740 memcpy (sym
+ 1, NODE_NAME (predicate
->val
.node
), len
);
1741 result
= cpp_lookup (pfile
, sym
, len
+ 1);
1744 pfile
->state
.prevent_expansion
--;
1748 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1749 or a pointer to NULL if the answer is not in the chain. */
1750 static struct answer
**
1751 find_answer (node
, candidate
)
1753 const struct answer
*candidate
;
1756 struct answer
**result
;
1758 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->next
)
1760 struct answer
*answer
= *result
;
1762 if (answer
->count
== candidate
->count
)
1764 for (i
= 0; i
< answer
->count
; i
++)
1765 if (! _cpp_equiv_tokens (&answer
->first
[i
], &candidate
->first
[i
]))
1768 if (i
== answer
->count
)
1776 /* Test an assertion within a preprocessor conditional. Returns
1777 nonzero on failure, zero on success. On success, the result of
1778 the test is written into VALUE, otherwise the value 0. */
1780 _cpp_test_assertion (pfile
, value
)
1782 unsigned int *value
;
1784 struct answer
*answer
;
1787 node
= parse_assertion (pfile
, &answer
, T_IF
);
1789 /* For recovery, an erroneous assertion expression is handled as a
1790 failing assertion. */
1794 *value
= (node
->type
== NT_ASSERTION
&&
1795 (answer
== 0 || *find_answer (node
, answer
) != 0));
1796 else if (pfile
->cur_token
[-1].type
== CPP_EOF
)
1797 _cpp_backup_tokens (pfile
, 1);
1799 /* We don't commit the memory for the answer - it's temporary only. */
1803 /* Handle #assert. */
1808 struct answer
*new_answer
;
1811 node
= parse_assertion (pfile
, &new_answer
, T_ASSERT
);
1814 /* Place the new answer in the answer list. First check there
1815 is not a duplicate. */
1816 new_answer
->next
= 0;
1817 if (node
->type
== NT_ASSERTION
)
1819 if (*find_answer (node
, new_answer
))
1821 cpp_error (pfile
, DL_WARNING
, "\"%s\" re-asserted",
1822 NODE_NAME (node
) + 1);
1825 new_answer
->next
= node
->value
.answers
;
1828 node
->type
= NT_ASSERTION
;
1829 node
->value
.answers
= new_answer
;
1830 BUFF_FRONT (pfile
->a_buff
) += (sizeof (struct answer
)
1831 + (new_answer
->count
- 1)
1832 * sizeof (cpp_token
));
1837 /* Handle #unassert. */
1843 struct answer
*answer
;
1845 node
= parse_assertion (pfile
, &answer
, T_UNASSERT
);
1846 /* It isn't an error to #unassert something that isn't asserted. */
1847 if (node
&& node
->type
== NT_ASSERTION
)
1851 struct answer
**p
= find_answer (node
, answer
), *temp
;
1853 /* Remove the answer from the list. */
1858 /* Did we free the last answer? */
1859 if (node
->value
.answers
== 0)
1860 node
->type
= NT_VOID
;
1865 _cpp_free_definition (node
);
1868 /* We don't commit the memory for the answer - it's temporary only. */
1871 /* These are for -D, -U, -A. */
1873 /* Process the string STR as if it appeared as the body of a #define.
1874 If STR is just an identifier, define it with value 1.
1875 If STR has anything after the identifier, then it should
1876 be identifier=definition. */
1878 cpp_define (pfile
, str
)
1885 /* Copy the entire option so we can modify it.
1886 Change the first "=" in the string to a space. If there is none,
1887 tack " 1" on the end. */
1889 count
= strlen (str
);
1890 buf
= (char *) alloca (count
+ 3);
1891 memcpy (buf
, str
, count
);
1893 p
= strchr (str
, '=');
1903 run_directive (pfile
, T_DEFINE
, buf
, count
);
1906 /* Slight variant of the above for use by initialize_builtins. */
1908 _cpp_define_builtin (pfile
, str
)
1912 run_directive (pfile
, T_DEFINE
, str
, strlen (str
));
1915 /* Process MACRO as if it appeared as the body of an #undef. */
1917 cpp_undef (pfile
, macro
)
1921 run_directive (pfile
, T_UNDEF
, macro
, strlen (macro
));
1924 /* Process the string STR as if it appeared as the body of a #assert. */
1926 cpp_assert (pfile
, str
)
1930 handle_assertion (pfile
, str
, T_ASSERT
);
1933 /* Process STR as if it appeared as the body of an #unassert. */
1935 cpp_unassert (pfile
, str
)
1939 handle_assertion (pfile
, str
, T_UNASSERT
);
1942 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1944 handle_assertion (pfile
, str
, type
)
1949 size_t count
= strlen (str
);
1950 const char *p
= strchr (str
, '=');
1954 /* Copy the entire option so we can modify it. Change the first
1955 "=" in the string to a '(', and tack a ')' on the end. */
1956 char *buf
= (char *) alloca (count
+ 2);
1958 memcpy (buf
, str
, count
);
1965 run_directive (pfile
, type
, str
, count
);
1968 /* The number of errors for a given reader. */
1973 return pfile
->errors
;
1976 /* The options structure. */
1978 cpp_get_options (pfile
)
1981 return &pfile
->opts
;
1984 /* The callbacks structure. */
1986 cpp_get_callbacks (pfile
)
1992 /* The line map set. */
1993 const struct line_maps
*
1994 cpp_get_line_maps (pfile
)
1997 return &pfile
->line_maps
;
2000 /* Copy the given callbacks structure to our own. */
2002 cpp_set_callbacks (pfile
, cb
)
2009 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2010 doesn't fail. It does not generate a file change call back; that
2011 is the responsibility of the caller. */
2013 cpp_push_buffer (pfile
, buffer
, len
, from_stage3
, return_at_eof
)
2015 const uchar
*buffer
;
2020 cpp_buffer
*new = xobnew (&pfile
->buffer_ob
, cpp_buffer
);
2022 /* Clears, amongst other things, if_stack and mi_cmacro. */
2023 memset (new, 0, sizeof (cpp_buffer
));
2025 new->line_base
= new->buf
= new->cur
= buffer
;
2026 new->rlimit
= buffer
+ len
;
2027 new->from_stage3
= from_stage3
|| CPP_OPTION (pfile
, traditional
);
2028 new->prev
= pfile
->buffer
;
2029 new->return_at_eof
= return_at_eof
;
2030 new->saved_flags
= BOL
;
2032 pfile
->buffer
= new;
2037 /* Pops a single buffer, with a file change call-back if appropriate.
2038 Then pushes the next -include file, if any remain. */
2040 _cpp_pop_buffer (pfile
)
2043 cpp_buffer
*buffer
= pfile
->buffer
;
2044 struct include_file
*inc
= buffer
->inc
;
2045 struct if_stack
*ifs
;
2047 /* Walk back up the conditional stack till we reach its level at
2048 entry to this file, issuing error messages. */
2049 for (ifs
= buffer
->if_stack
; ifs
; ifs
= ifs
->next
)
2050 cpp_error_with_line (pfile
, DL_ERROR
, ifs
->line
, 0,
2051 "unterminated #%s", dtable
[ifs
->type
].name
);
2053 /* In case of a missing #endif. */
2054 pfile
->state
.skipping
= 0;
2056 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2057 pfile
->buffer
= buffer
->prev
;
2059 /* Free the buffer object now; we may want to push a new buffer
2060 in _cpp_push_next_include_file. */
2061 obstack_free (&pfile
->buffer_ob
, buffer
);
2065 _cpp_pop_file_buffer (pfile
, inc
);
2067 /* Don't generate a callback for popping the main file. */
2070 _cpp_do_file_change (pfile
, LC_LEAVE
, 0, 0, 0);
2072 /* If this is the main file, there may be some -include
2073 files left to push. */
2074 if (!pfile
->buffer
->prev
)
2075 _cpp_maybe_push_include_file (pfile
);
2080 /* Enter all recognized directives in the hash table. */
2082 _cpp_init_directives (pfile
)
2088 for (i
= 0; i
< (unsigned int) N_DIRECTIVES
; i
++)
2090 node
= cpp_lookup (pfile
, dtable
[i
].name
, dtable
[i
].length
);
2091 node
->is_directive
= 1;
2092 node
->directive_index
= i
;