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. */
29 /* Chained list of answers to an assertion. */
37 /* Stack of conditionals currently in progress
38 (including both successful and failing conditionals). */
41 struct if_stack
*next
;
42 unsigned int line
; /* Line where condition started. */
43 const cpp_hashnode
*mi_cmacro
;/* macro name for #ifndef around entire file */
44 bool skip_elses
; /* Can future #else / #elif be skipped? */
45 bool was_skipping
; /* If were skipping on entry. */
46 int type
; /* Most recent conditional, for diagnostics. */
49 /* Contains a registered pragma or pragma namespace. */
50 typedef void (*pragma_cb
) PARAMS ((cpp_reader
*));
53 struct pragma_entry
*next
;
54 const cpp_hashnode
*pragma
; /* Name and length. */
58 struct pragma_entry
*space
;
62 /* Values for the origin field of struct directive. KANDR directives
63 come from traditional (K&R) C. STDC89 directives come from the
64 1989 C standard. EXTENSION directives are extensions. */
69 /* Values for the flags field of struct directive. COND indicates a
70 conditional; IF_COND an opening conditional. INCL means to treat
71 "..." and <...> as q-char and h-char sequences respectively. IN_I
72 means this directive should be handled even if -fpreprocessed is in
73 effect (these are the directives with callback hooks).
75 EXPAND is set on directives that are always macro-expanded. If
76 INCL is set, macro expansion is special-cased and EXPAND should not
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 void do_pragma_once
PARAMS ((cpp_reader
*));
125 static void do_pragma_poison
PARAMS ((cpp_reader
*));
126 static void do_pragma_system_header
PARAMS ((cpp_reader
*));
127 static void do_pragma_dependency
PARAMS ((cpp_reader
*));
128 static void do_linemarker
PARAMS ((cpp_reader
*));
129 static const cpp_token
*get_token_no_padding
PARAMS ((cpp_reader
*));
130 static const cpp_token
*get__Pragma_string
PARAMS ((cpp_reader
*));
131 static void destringize_and_run
PARAMS ((cpp_reader
*, const cpp_string
*));
132 static int parse_answer
PARAMS ((cpp_reader
*, struct answer
**, int));
133 static cpp_hashnode
*parse_assertion
PARAMS ((cpp_reader
*, struct answer
**,
135 static struct answer
** find_answer
PARAMS ((cpp_hashnode
*,
136 const struct answer
*));
137 static void handle_assertion
PARAMS ((cpp_reader
*, const char *, int));
139 /* This is the table of directive handlers. It is ordered by
140 frequency of occurrence; the numbers at the end are directive
141 counts from all the source code I have lying around (egcs and libc
142 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
143 pcmcia-cs-3.0.9). This is no longer important as directive lookup
144 is now O(1). All extensions other than #warning and #include_next
145 are deprecated. The name is where the extension appears to have
148 #define DIRECTIVE_TABLE \
149 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
150 D(include, T_INCLUDE, KANDR, INCL) /* 52262 */ \
151 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
152 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
153 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
154 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
155 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
156 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
157 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
158 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
159 D(error, T_ERROR, STDC89, 0) /* 475 */ \
160 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
161 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
162 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL) /* 19 */ \
163 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
164 D(import, T_IMPORT, EXTENSION, INCL) /* 0 ObjC */ \
165 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
166 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
167 SCCS_ENTRY /* 0 SVR4? */
169 /* #sccs is not always recognized. */
170 #ifdef SCCS_DIRECTIVE
171 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
173 # define SCCS_ENTRY /* nothing */
176 /* Use the table to generate a series of prototypes, an enum for the
177 directive names, and an array of directive handlers. */
179 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
180 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
184 #define D(n, tag, o, f) tag,
192 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
193 #define D(name, t, origin, flags) \
194 { CONCAT2(do_,name), (const uchar *) STRINGX(name), \
195 sizeof STRINGX(name) - 1, origin, flags },
196 static const directive dtable
[] =
201 #undef DIRECTIVE_TABLE
203 /* Wrapper struct directive for linemarkers.
204 The origin is more or less true - the original K+R cpp
205 did use this notation in its preprocessed output. */
206 static const directive linemarker_dir
=
208 do_linemarker
, U
"#", 1, KANDR
, IN_I
211 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
213 /* Skip any remaining tokens in a directive. */
215 skip_rest_of_line (pfile
)
218 /* Discard all stacked contexts. */
219 while (pfile
->context
!= &pfile
->base_context
)
220 _cpp_pop_context (pfile
);
222 /* Sweep up all tokens remaining on the line. */
224 while (_cpp_lex_token (pfile
)->type
!= CPP_EOF
)
228 /* Ensure there are no stray tokens at the end of a directive. */
233 if (! SEEN_EOL () && _cpp_lex_token (pfile
)->type
!= CPP_EOF
)
234 cpp_error (pfile
, DL_PEDWARN
, "extra tokens at end of #%s directive",
235 pfile
->directive
->name
);
238 /* Called when entering a directive, _Pragma or command-line directive. */
240 start_directive (pfile
)
243 /* Setup in-directive state. */
244 pfile
->state
.in_directive
= 1;
245 pfile
->state
.save_comments
= 0;
247 /* Some handlers need the position of the # for diagnostics. */
248 pfile
->directive_line
= pfile
->line
;
251 /* Called when leaving a directive, _Pragma or command-line directive. */
253 end_directive (pfile
, skip_line
)
257 if (CPP_OPTION (pfile
, traditional
))
259 if (!pfile
->directive
|| pfile
->directive
== &dtable
[T_DEFINE
])
262 _cpp_remove_overlay (pfile
);
265 /* We don't skip for an assembler #. */
268 skip_rest_of_line (pfile
);
269 if (!pfile
->keep_tokens
)
271 pfile
->cur_run
= &pfile
->base_run
;
272 pfile
->cur_token
= pfile
->base_run
.base
;
277 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
278 pfile
->state
.in_directive
= 0;
279 pfile
->state
.angled_headers
= 0;
280 pfile
->directive
= 0;
283 /* Prepare to handle the directive in pfile->directive. */
285 prepare_directive_trad (pfile
)
288 if (pfile
->directive
== &dtable
[T_DEFINE
])
289 CUR (pfile
->context
) = pfile
->buffer
->cur
;
292 bool no_expand
= ! (pfile
->directive
->flags
& EXPAND
);
293 bool was_skipping
= pfile
->state
.skipping
;
295 pfile
->state
.skipping
= false;
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
);
307 /* Output diagnostics for a directive DIR. INDENTED is non-zero if
308 the '#' was indented. */
310 directive_diagnostics (pfile
, dir
, indented
)
312 const directive
*dir
;
315 /* Issue -pedantic warnings for extensions. */
316 if (CPP_PEDANTIC (pfile
)
317 && ! pfile
->state
.skipping
318 && dir
->origin
== EXTENSION
)
319 cpp_error (pfile
, DL_PEDWARN
, "#%s is a GCC extension", dir
->name
);
321 /* Traditionally, a directive is ignored unless its # is in
322 column 1. Therefore in code intended to work with K+R
323 compilers, directives added by C89 must have their #
324 indented, and directives present in traditional C must not.
325 This is true even of directives in skipped conditional
326 blocks. #elif cannot be used at all. */
327 if (CPP_WTRADITIONAL (pfile
))
329 if (dir
== &dtable
[T_ELIF
])
330 cpp_error (pfile
, DL_WARNING
,
331 "suggest not using #elif in traditional C");
332 else if (indented
&& dir
->origin
== KANDR
)
333 cpp_error (pfile
, DL_WARNING
,
334 "traditional C ignores #%s with the # indented",
336 else if (!indented
&& dir
->origin
!= KANDR
)
337 cpp_error (pfile
, DL_WARNING
,
338 "suggest hiding #%s from traditional C with an indented #",
343 /* Check if we have a known directive. INDENTED is non-zero if the
344 '#' of the directive was indented. This function is in this file
345 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
346 non-zero if the line of tokens has been handled, zero if we should
347 continue processing the line. */
349 _cpp_handle_directive (pfile
, indented
)
353 const directive
*dir
= 0;
354 const cpp_token
*dname
;
355 bool was_parsing_args
= pfile
->state
.parsing_args
;
358 if (was_parsing_args
)
360 if (CPP_OPTION (pfile
, pedantic
))
361 cpp_error (pfile
, DL_PEDWARN
,
362 "embedding a directive within macro arguments is not portable");
363 pfile
->state
.parsing_args
= 0;
364 pfile
->state
.prevent_expansion
= 0;
366 start_directive (pfile
);
367 dname
= _cpp_lex_token (pfile
);
369 if (dname
->type
== CPP_NAME
)
371 if (dname
->val
.node
->directive_index
)
372 dir
= &dtable
[dname
->val
.node
->directive_index
- 1];
374 /* We do not recognise the # followed by a number extension in
376 else if (dname
->type
== CPP_NUMBER
&& CPP_OPTION (pfile
, lang
) != CLK_ASM
)
378 dir
= &linemarker_dir
;
379 if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, preprocessed
)
380 && ! pfile
->state
.skipping
)
381 cpp_error (pfile
, DL_PEDWARN
,
382 "style of line directive is a GCC extension");
387 /* If we have a directive that is not an opening conditional,
388 invalidate any control macro. */
389 if (! (dir
->flags
& IF_COND
))
390 pfile
->mi_valid
= false;
392 /* Kluge alert. In order to be sure that code like this
397 does not cause '#define foo bar' to get executed when
398 compiled with -save-temps, we recognize directives in
399 -fpreprocessed mode only if the # is in column 1. cppmacro.c
400 puts a space in front of any '#' at the start of a macro. */
401 if (CPP_OPTION (pfile
, preprocessed
)
402 && (indented
|| !(dir
->flags
& IN_I
)))
409 /* In failed conditional groups, all non-conditional
410 directives are ignored. Before doing that, whether
411 skipping or not, we should lex angle-bracketed headers
412 correctly, and maybe output some diagnostics. */
413 pfile
->state
.angled_headers
= dir
->flags
& INCL
;
414 if (! CPP_OPTION (pfile
, preprocessed
))
415 directive_diagnostics (pfile
, dir
, indented
);
416 if (pfile
->state
.skipping
&& !(dir
->flags
& COND
))
420 else if (dname
->type
== CPP_EOF
)
421 ; /* CPP_EOF is the "null directive". */
424 /* An unknown directive. Don't complain about it in assembly
425 source: we don't know where the comments are, and # may
426 introduce assembler pseudo-ops. Don't complain about invalid
427 directives in skipped conditional groups (6.10 p4). */
428 if (CPP_OPTION (pfile
, lang
) == CLK_ASM
)
430 else if (!pfile
->state
.skipping
)
431 cpp_error (pfile
, DL_ERROR
, "invalid preprocessing directive #%s",
432 cpp_token_as_text (pfile
, dname
));
437 /* If we are processing a `#define' directive and we have been
438 requested to expand comments into macros, then re-enable
439 saving of comments. */
440 if (dir
== &dtable
[T_DEFINE
])
441 pfile
->state
.save_comments
=
442 ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
);
444 pfile
->directive
= dir
;
445 if (CPP_OPTION (pfile
, traditional
))
446 prepare_directive_trad (pfile
);
447 (*pfile
->directive
->handler
) (pfile
);
450 _cpp_backup_tokens (pfile
, 1);
452 end_directive (pfile
, skip
);
453 if (was_parsing_args
)
455 /* Restore state when within macro args. */
456 pfile
->state
.parsing_args
= 2;
457 pfile
->state
.prevent_expansion
= 1;
458 pfile
->buffer
->saved_flags
|= PREV_WHITE
;
463 /* Directive handler wrapper used by the command line option
466 run_directive (pfile
, dir_no
, buf
, count
)
472 cpp_push_buffer (pfile
, (const uchar
*) buf
, count
,
473 /* from_stage3 */ true, 1);
474 start_directive (pfile
);
475 /* We don't want a leading # to be interpreted as a directive. */
476 pfile
->buffer
->saved_flags
= 0;
477 pfile
->directive
= &dtable
[dir_no
];
478 if (CPP_OPTION (pfile
, traditional
))
479 prepare_directive_trad (pfile
);
480 (void) (*pfile
->directive
->handler
) (pfile
);
481 end_directive (pfile
, 1);
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 Note that if we're copying comments into macro expansions, we
501 could encounter comment tokens here, so eat them all up first. */
503 if (! CPP_OPTION (pfile
, discard_comments_in_macro_exp
))
505 while (token
->type
== CPP_COMMENT
)
506 token
= _cpp_lex_token (pfile
);
509 if (token
->type
== CPP_NAME
)
511 cpp_hashnode
*node
= token
->val
.node
;
513 if (node
== pfile
->spec_nodes
.n_defined
)
514 cpp_error (pfile
, DL_ERROR
,
515 "\"defined\" cannot be used as a macro name");
516 else if (! (node
->flags
& NODE_POISONED
))
519 else if (token
->flags
& NAMED_OP
)
520 cpp_error (pfile
, DL_ERROR
,
521 "\"%s\" cannot be used as a macro name as it is an operator in C++",
522 NODE_NAME (token
->val
.node
));
523 else if (token
->type
== CPP_EOF
)
524 cpp_error (pfile
, DL_ERROR
, "no macro name given in #%s directive",
525 pfile
->directive
->name
);
527 cpp_error (pfile
, DL_ERROR
, "macro names must be identifiers");
532 /* Process a #define directive. Most work is done in cppmacro.c. */
537 cpp_hashnode
*node
= lex_macro_node (pfile
);
541 if (_cpp_create_definition (pfile
, node
))
542 if (pfile
->cb
.define
)
543 (*pfile
->cb
.define
) (pfile
, pfile
->directive_line
, node
);
547 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
552 cpp_hashnode
*node
= lex_macro_node (pfile
);
554 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
555 is not currently defined as a macro name. */
556 if (node
&& node
->type
== NT_MACRO
)
559 (*pfile
->cb
.undef
) (pfile
, pfile
->directive_line
, node
);
561 if (node
->flags
& NODE_WARN
)
562 cpp_error (pfile
, DL_WARNING
, "undefining \"%s\"", NODE_NAME (node
));
564 _cpp_free_definition (node
);
569 /* Helper routine used by parse_include. Reinterpret the current line
570 as an h-char-sequence (< ... >); we are looking at the first token
571 after the <. Returns the header as a token, or NULL on failure. */
572 static const cpp_token
*
573 glue_header_name (pfile
)
576 cpp_token
*header
= NULL
;
577 const cpp_token
*token
;
578 unsigned char *buffer
;
579 size_t len
, total_len
= 0, capacity
= 1024;
581 /* To avoid lexed tokens overwriting our glued name, we can only
582 allocate from the string pool once we've lexed everything. */
583 buffer
= (unsigned char *) xmalloc (capacity
);
586 token
= cpp_get_token (pfile
);
588 if (token
->type
== CPP_GREATER
|| token
->type
== CPP_EOF
)
591 len
= cpp_token_len (token
);
592 if (total_len
+ len
> capacity
)
594 capacity
= (capacity
+ len
) * 2;
595 buffer
= (unsigned char *) xrealloc (buffer
, capacity
);
598 if (token
->flags
& PREV_WHITE
)
599 buffer
[total_len
++] = ' ';
601 total_len
= cpp_spell_token (pfile
, token
, &buffer
[total_len
]) - buffer
;
604 if (token
->type
== CPP_EOF
)
605 cpp_error (pfile
, DL_ERROR
, "missing terminating > character");
608 unsigned char *token_mem
= _cpp_unaligned_alloc (pfile
, total_len
+ 1);
609 memcpy (token_mem
, buffer
, total_len
);
610 token_mem
[total_len
] = '\0';
612 header
= _cpp_temp_token (pfile
);
613 header
->type
= CPP_HEADER_NAME
;
615 header
->val
.str
.len
= total_len
;
616 header
->val
.str
.text
= token_mem
;
623 /* Returns the header string of #include, #include_next, #import and
624 #pragma dependency. Returns NULL on error. */
625 static const cpp_token
*
626 parse_include (pfile
)
629 const unsigned char *dir
;
630 const cpp_token
*header
;
632 if (pfile
->directive
== &dtable
[T_PRAGMA
])
633 dir
= U
"pragma dependency";
635 dir
= pfile
->directive
->name
;
637 /* Allow macro expansion. */
638 header
= cpp_get_token (pfile
);
639 if (header
->type
!= CPP_STRING
&& header
->type
!= CPP_HEADER_NAME
)
641 if (header
->type
!= CPP_LESS
)
643 cpp_error (pfile
, DL_ERROR
,
644 "#%s expects \"FILENAME\" or <FILENAME>", dir
);
648 header
= glue_header_name (pfile
);
653 if (header
->val
.str
.len
== 0)
655 cpp_error (pfile
, DL_ERROR
, "empty file name in #%s", dir
);
662 /* Handle #include, #include_next and #import. */
664 do_include_common (pfile
, type
)
666 enum include_type type
;
668 const cpp_token
*header
;
670 /* For #include_next, if this is the primary source file, warn and
671 use the normal search logic. */
672 if (type
== IT_INCLUDE_NEXT
&& ! pfile
->buffer
->prev
)
674 cpp_error (pfile
, DL_WARNING
, "#include_next in primary source file");
677 else if (type
== IT_IMPORT
&& CPP_OPTION (pfile
, warn_import
))
679 CPP_OPTION (pfile
, warn_import
) = 0;
680 cpp_error (pfile
, DL_WARNING
,
681 "#import is obsolete, use an #ifndef wrapper in the header file");
684 header
= parse_include (pfile
);
687 /* Prevent #include recursion. */
688 if (pfile
->line_maps
.depth
>= CPP_STACK_MAX
)
689 cpp_error (pfile
, DL_ERROR
, "#include nested too deeply");
693 /* Get out of macro context, if we are. */
694 skip_rest_of_line (pfile
);
695 if (pfile
->cb
.include
)
696 (*pfile
->cb
.include
) (pfile
, pfile
->directive_line
,
697 pfile
->directive
->name
, header
);
699 _cpp_execute_include (pfile
, header
, type
);
708 do_include_common (pfile
, IT_INCLUDE
);
715 do_include_common (pfile
, IT_IMPORT
);
719 do_include_next (pfile
)
722 do_include_common (pfile
, IT_INCLUDE_NEXT
);
725 /* Subroutine of do_linemarker. Read possible flags after file name.
726 LAST is the last flag seen; 0 if this is the first flag. Return the
727 flag if it is valid, 0 at the end of the directive. Otherwise
730 read_flag (pfile
, last
)
734 const cpp_token
*token
= _cpp_lex_token (pfile
);
736 if (token
->type
== CPP_NUMBER
&& token
->val
.str
.len
== 1)
738 unsigned int flag
= token
->val
.str
.text
[0] - '0';
740 if (flag
> last
&& flag
<= 4
741 && (flag
!= 4 || last
== 3)
742 && (flag
!= 2 || last
== 0))
746 if (token
->type
!= CPP_EOF
)
747 cpp_error (pfile
, DL_ERROR
, "invalid flag \"%s\" in line directive",
748 cpp_token_as_text (pfile
, token
));
752 /* Subroutine of do_line and do_linemarker. Returns a version of STR
753 which has a NUL terminator and all escape sequences converted to
754 their equivalents. Temporary, hopefully. */
756 dequote_string (pfile
, str
, len
)
761 uchar
*result
= _cpp_unaligned_alloc (pfile
, len
+ 1);
763 const uchar
*limit
= str
+ len
;
772 *dst
++ = cpp_parse_escape (pfile
, &str
, limit
, 0);
778 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
779 of length LEN, to binary; store it in NUMP, and return 0 if the
780 number was well-formed, 1 if not. Temporary, hopefully. */
782 strtoul_for_line (str
, len
, nump
)
787 unsigned long reg
= 0;
801 /* Interpret #line command.
802 Note that the filename string (if any) is a true string constant
803 (escapes are interpreted), unlike in #line. */
808 const cpp_token
*token
;
809 const char *new_file
= pfile
->map
->to_file
;
810 unsigned long new_lineno
;
812 /* C99 raised the minimum limit on #line numbers. */
813 unsigned int cap
= CPP_OPTION (pfile
, c99
) ? 2147483647 : 32767;
815 /* #line commands expand macros. */
816 token
= cpp_get_token (pfile
);
817 if (token
->type
!= CPP_NUMBER
818 || strtoul_for_line (token
->val
.str
.text
, token
->val
.str
.len
,
821 cpp_error (pfile
, DL_ERROR
,
822 "\"%s\" after #line is not a positive integer",
823 cpp_token_as_text (pfile
, token
));
827 if (CPP_PEDANTIC (pfile
) && (new_lineno
== 0 || new_lineno
> cap
))
828 cpp_error (pfile
, DL_PEDWARN
, "line number out of range");
830 token
= cpp_get_token (pfile
);
831 if (token
->type
== CPP_STRING
)
833 new_file
= (const char *) dequote_string (pfile
, token
->val
.str
.text
,
837 else if (token
->type
!= CPP_EOF
)
839 cpp_error (pfile
, DL_ERROR
, "\"%s\" is not a valid filename",
840 cpp_token_as_text (pfile
, token
));
844 skip_rest_of_line (pfile
);
845 _cpp_do_file_change (pfile
, LC_RENAME
, new_file
, new_lineno
,
849 /* Interpret the # 44 "file" [flags] notation, which has slightly
850 different syntax and semantics from #line: Flags are allowed,
851 and we never complain about the line number being too big. */
853 do_linemarker (pfile
)
856 const cpp_token
*token
;
857 const char *new_file
= pfile
->map
->to_file
;
858 unsigned long new_lineno
;
859 unsigned int new_sysp
= pfile
->map
->sysp
;
860 enum lc_reason reason
= LC_RENAME
;
863 /* Back up so we can get the number again. Putting this in
864 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
865 some circumstances, which can segfault. */
866 _cpp_backup_tokens (pfile
, 1);
868 /* #line commands expand macros. */
869 token
= cpp_get_token (pfile
);
870 if (token
->type
!= CPP_NUMBER
871 || strtoul_for_line (token
->val
.str
.text
, token
->val
.str
.len
,
874 cpp_error (pfile
, DL_ERROR
, "\"%s\" after # is not a positive integer",
875 cpp_token_as_text (pfile
, token
));
879 token
= cpp_get_token (pfile
);
880 if (token
->type
== CPP_STRING
)
882 new_file
= (const char *) dequote_string (pfile
, token
->val
.str
.text
,
885 flag
= read_flag (pfile
, 0);
889 /* Fake an include for cpp_included (). */
890 _cpp_fake_include (pfile
, new_file
);
891 flag
= read_flag (pfile
, flag
);
896 flag
= read_flag (pfile
, flag
);
901 flag
= read_flag (pfile
, flag
);
908 else if (token
->type
!= CPP_EOF
)
910 cpp_error (pfile
, DL_ERROR
, "\"%s\" is not a valid filename",
911 cpp_token_as_text (pfile
, token
));
915 skip_rest_of_line (pfile
);
916 _cpp_do_file_change (pfile
, reason
, new_file
, new_lineno
, new_sysp
);
919 /* Arrange the file_change callback. pfile->line has changed to
920 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
921 header, 2 for a system header that needs to be extern "C" protected,
922 and zero otherwise. */
924 _cpp_do_file_change (pfile
, reason
, to_file
, file_line
, sysp
)
926 enum lc_reason reason
;
928 unsigned int file_line
;
931 pfile
->map
= add_line_map (&pfile
->line_maps
, reason
, sysp
,
932 pfile
->line
, to_file
, file_line
);
934 if (pfile
->cb
.file_change
)
935 (*pfile
->cb
.file_change
) (pfile
, pfile
->map
);
938 /* Report a warning or error detected by the program we are
939 processing. Use the directive's tokens in the error message. */
941 do_diagnostic (pfile
, code
, print_dir
)
946 if (_cpp_begin_message (pfile
, code
,
947 pfile
->cur_token
[-1].line
,
948 pfile
->cur_token
[-1].col
))
951 fprintf (stderr
, "#%s ", pfile
->directive
->name
);
952 pfile
->state
.prevent_expansion
++;
953 cpp_output_line (pfile
, stderr
);
954 pfile
->state
.prevent_expansion
--;
962 do_diagnostic (pfile
, DL_ERROR
, 1);
969 /* We want #warning diagnostics to be emitted in system headers too. */
970 do_diagnostic (pfile
, DL_WARNING_SYSHDR
, 1);
973 /* Report program identification. */
978 const cpp_token
*str
= cpp_get_token (pfile
);
980 if (str
->type
!= CPP_STRING
)
981 cpp_error (pfile
, DL_ERROR
, "invalid #ident directive");
982 else if (pfile
->cb
.ident
)
983 (*pfile
->cb
.ident
) (pfile
, pfile
->directive_line
, &str
->val
.str
);
988 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
989 matching entry, or NULL if none is found. The returned entry could
990 be the start of a namespace chain, or a pragma. */
991 static struct pragma_entry
*
992 lookup_pragma_entry (chain
, pragma
)
993 struct pragma_entry
*chain
;
994 const cpp_hashnode
*pragma
;
996 while (chain
&& chain
->pragma
!= pragma
)
1002 /* Create and insert a pragma entry for NAME at the beginning of a
1003 singly-linked CHAIN. If handler is NULL, it is a namespace,
1004 otherwise it is a pragma and its handler. */
1005 static struct pragma_entry
*
1006 insert_pragma_entry (pfile
, chain
, pragma
, handler
)
1008 struct pragma_entry
**chain
;
1009 const cpp_hashnode
*pragma
;
1012 struct pragma_entry
*new;
1014 new = (struct pragma_entry
*)
1015 _cpp_aligned_alloc (pfile
, sizeof (struct pragma_entry
));
1016 new->pragma
= pragma
;
1020 new->u
.handler
= handler
;
1025 new->u
.space
= NULL
;
1033 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1034 goes in the global namespace. HANDLER is the handler it will call,
1035 which must be non-NULL. */
1037 cpp_register_pragma (pfile
, space
, name
, handler
)
1043 struct pragma_entry
**chain
= &pfile
->pragmas
;
1044 struct pragma_entry
*entry
;
1045 const cpp_hashnode
*node
;
1052 node
= cpp_lookup (pfile
, U space
, strlen (space
));
1053 entry
= lookup_pragma_entry (*chain
, node
);
1055 entry
= insert_pragma_entry (pfile
, chain
, node
, NULL
);
1056 else if (!entry
->is_nspace
)
1058 chain
= &entry
->u
.space
;
1061 /* Check for duplicates. */
1062 node
= cpp_lookup (pfile
, U name
, strlen (name
));
1063 entry
= lookup_pragma_entry (*chain
, node
);
1066 if (entry
->is_nspace
)
1068 cpp_error (pfile
, DL_ICE
,
1069 "registering \"%s\" as both a pragma and a pragma namespace",
1072 cpp_error (pfile
, DL_ICE
, "#pragma %s %s is already registered",
1075 cpp_error (pfile
, DL_ICE
, "#pragma %s is already registered", name
);
1078 insert_pragma_entry (pfile
, chain
, node
, handler
);
1081 /* Register the pragmas the preprocessor itself handles. */
1083 _cpp_init_internal_pragmas (pfile
)
1086 /* Pragmas in the global namespace. */
1087 cpp_register_pragma (pfile
, 0, "once", do_pragma_once
);
1089 /* New GCC-specific pragmas should be put in the GCC namespace. */
1090 cpp_register_pragma (pfile
, "GCC", "poison", do_pragma_poison
);
1091 cpp_register_pragma (pfile
, "GCC", "system_header", do_pragma_system_header
);
1092 cpp_register_pragma (pfile
, "GCC", "dependency", do_pragma_dependency
);
1095 /* Pragmata handling. We handle some, and pass the rest on to the
1096 front end. C99 defines three pragmas and says that no macro
1097 expansion is to be performed on them; whether or not macro
1098 expansion happens for other pragmas is implementation defined.
1099 This implementation never macro-expands the text after #pragma. */
1104 const struct pragma_entry
*p
= NULL
;
1105 const cpp_token
*token
;
1106 unsigned int count
= 1;
1108 pfile
->state
.prevent_expansion
++;
1110 token
= cpp_get_token (pfile
);
1111 if (token
->type
== CPP_NAME
)
1113 p
= lookup_pragma_entry (pfile
->pragmas
, token
->val
.node
);
1114 if (p
&& p
->is_nspace
)
1117 token
= cpp_get_token (pfile
);
1118 if (token
->type
== CPP_NAME
)
1119 p
= lookup_pragma_entry (p
->u
.space
, token
->val
.node
);
1125 /* FIXME. This is an awful kludge to get the front ends to update
1126 their notion of line number for diagnostic purposes. The line
1127 number should be passed to the handler and they should do it
1128 themselves. Stand-alone CPP must ignore us, otherwise it will
1129 prefix the directive with spaces, hence the 1. Ugh. */
1130 if (pfile
->cb
.line_change
)
1131 (*pfile
->cb
.line_change
)(pfile
, token
, 1);
1134 (*p
->u
.handler
) (pfile
);
1135 else if (pfile
->cb
.def_pragma
)
1137 _cpp_backup_tokens (pfile
, count
);
1138 (*pfile
->cb
.def_pragma
) (pfile
, pfile
->directive_line
);
1141 pfile
->state
.prevent_expansion
--;
1144 /* Handle #pragma once. */
1146 do_pragma_once (pfile
)
1149 cpp_error (pfile
, DL_WARNING
, "#pragma once is obsolete");
1151 if (pfile
->buffer
->prev
== NULL
)
1152 cpp_error (pfile
, DL_WARNING
, "#pragma once in main file");
1154 _cpp_never_reread (pfile
->buffer
->inc
);
1159 /* Handle #pragma GCC poison, to poison one or more identifiers so
1160 that the lexer produces a hard error for each subsequent usage. */
1162 do_pragma_poison (pfile
)
1165 const cpp_token
*tok
;
1168 pfile
->state
.poisoned_ok
= 1;
1171 tok
= _cpp_lex_token (pfile
);
1172 if (tok
->type
== CPP_EOF
)
1174 if (tok
->type
!= CPP_NAME
)
1176 cpp_error (pfile
, DL_ERROR
, "invalid #pragma GCC poison directive");
1181 if (hp
->flags
& NODE_POISONED
)
1184 if (hp
->type
== NT_MACRO
)
1185 cpp_error (pfile
, DL_WARNING
, "poisoning existing macro \"%s\"",
1187 _cpp_free_definition (hp
);
1188 hp
->flags
|= NODE_POISONED
| NODE_DIAGNOSTIC
;
1190 pfile
->state
.poisoned_ok
= 0;
1193 /* Mark the current header as a system header. This will suppress
1194 some categories of warnings (notably those from -pedantic). It is
1195 intended for use in system libraries that cannot be implemented in
1196 conforming C, but cannot be certain that their headers appear in a
1197 system include directory. To prevent abuse, it is rejected in the
1198 primary source file. */
1200 do_pragma_system_header (pfile
)
1203 cpp_buffer
*buffer
= pfile
->buffer
;
1205 if (buffer
->prev
== 0)
1206 cpp_error (pfile
, DL_WARNING
,
1207 "#pragma system_header ignored outside include file");
1211 skip_rest_of_line (pfile
);
1212 cpp_make_system_header (pfile
, 1, 0);
1216 /* Check the modified date of the current include file against a specified
1217 file. Issue a diagnostic, if the specified file is newer. We use this to
1218 determine if a fixed header should be refixed. */
1220 do_pragma_dependency (pfile
)
1223 const cpp_token
*header
;
1226 header
= parse_include (pfile
);
1230 ordering
= _cpp_compare_file_date (pfile
, header
);
1232 cpp_error (pfile
, DL_WARNING
, "cannot find source %s",
1233 cpp_token_as_text (pfile
, header
));
1234 else if (ordering
> 0)
1236 cpp_error (pfile
, DL_WARNING
, "current file is older than %s",
1237 cpp_token_as_text (pfile
, header
));
1238 if (cpp_get_token (pfile
)->type
!= CPP_EOF
)
1240 _cpp_backup_tokens (pfile
, 1);
1241 do_diagnostic (pfile
, DL_WARNING
, 0);
1246 /* Get a token but skip padding. */
1247 static const cpp_token
*
1248 get_token_no_padding (pfile
)
1253 const cpp_token
*result
= cpp_get_token (pfile
);
1254 if (result
->type
!= CPP_PADDING
)
1259 /* Check syntax is "(string-literal)". Returns the string on success,
1260 or NULL on failure. */
1261 static const cpp_token
*
1262 get__Pragma_string (pfile
)
1265 const cpp_token
*string
;
1267 if (get_token_no_padding (pfile
)->type
!= CPP_OPEN_PAREN
)
1270 string
= get_token_no_padding (pfile
);
1271 if (string
->type
!= CPP_STRING
&& string
->type
!= CPP_WSTRING
)
1274 if (get_token_no_padding (pfile
)->type
!= CPP_CLOSE_PAREN
)
1280 /* Destringize IN into a temporary buffer, by removing the first \ of
1281 \" and \\ sequences, and process the result as a #pragma directive. */
1283 destringize_and_run (pfile
, in
)
1285 const cpp_string
*in
;
1287 const unsigned char *src
, *limit
;
1288 char *dest
, *result
;
1290 dest
= result
= alloca (in
->len
+ 1);
1291 for (src
= in
->text
, limit
= src
+ in
->len
; src
< limit
;)
1293 /* We know there is a character following the backslash. */
1294 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1300 run_directive (pfile
, T_PRAGMA
, result
, dest
- result
);
1303 /* Handle the _Pragma operator. */
1305 _cpp_do__Pragma (pfile
)
1308 const cpp_token
*string
= get__Pragma_string (pfile
);
1311 cpp_error (pfile
, DL_ERROR
,
1312 "_Pragma takes a parenthesized string literal");
1315 /* Ideally, we'd like
1316 token1 _Pragma ("foo") token2
1323 Getting these correct line markers is a little tricky. */
1325 unsigned int orig_line
= pfile
->line
;
1326 destringize_and_run (pfile
, &string
->val
.str
);
1327 pfile
->line
= orig_line
;
1328 pfile
->buffer
->saved_flags
= BOL
;
1332 /* Just ignore #sccs, on systems where we define it at all. */
1333 #ifdef SCCS_DIRECTIVE
1336 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
1341 /* Handle #ifdef. */
1348 if (! pfile
->state
.skipping
)
1350 const cpp_hashnode
*node
= lex_macro_node (pfile
);
1353 skip
= node
->type
!= NT_MACRO
;
1359 push_conditional (pfile
, skip
, T_IFDEF
, 0);
1362 /* Handle #ifndef. */
1368 const cpp_hashnode
*node
= 0;
1370 if (! pfile
->state
.skipping
)
1372 node
= lex_macro_node (pfile
);
1374 skip
= node
->type
== NT_MACRO
;
1380 push_conditional (pfile
, skip
, T_IFNDEF
, node
);
1383 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1384 pfile->mi_ind_cmacro so we can handle multiple-include
1385 optimisations. If macro expansion occurs in the expression, we
1386 cannot treat it as a controlling conditional, since the expansion
1387 could change in the future. That is handled by cpp_get_token. */
1394 if (! pfile
->state
.skipping
)
1395 skip
= _cpp_parse_expr (pfile
) == false;
1397 push_conditional (pfile
, skip
, T_IF
, pfile
->mi_ind_cmacro
);
1400 /* Flip skipping state if appropriate and continue without changing
1401 if_stack; this is so that the error message for missing #endif's
1402 etc. will point to the original #if. */
1407 cpp_buffer
*buffer
= pfile
->buffer
;
1408 struct if_stack
*ifs
= buffer
->if_stack
;
1411 cpp_error (pfile
, DL_ERROR
, "#else without #if");
1414 if (ifs
->type
== T_ELSE
)
1416 cpp_error (pfile
, DL_ERROR
, "#else after #else");
1417 cpp_error_with_line (pfile
, DL_ERROR
, ifs
->line
, 0,
1418 "the conditional began here");
1422 /* Skip any future (erroneous) #elses or #elifs. */
1423 pfile
->state
.skipping
= ifs
->skip_elses
;
1424 ifs
->skip_elses
= true;
1426 /* Invalidate any controlling macro. */
1429 /* Only check EOL if was not originally skipping. */
1430 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1435 /* Handle a #elif directive by not changing if_stack either. See the
1436 comment above do_else. */
1441 cpp_buffer
*buffer
= pfile
->buffer
;
1442 struct if_stack
*ifs
= buffer
->if_stack
;
1445 cpp_error (pfile
, DL_ERROR
, "#elif without #if");
1448 if (ifs
->type
== T_ELSE
)
1450 cpp_error (pfile
, DL_ERROR
, "#elif after #else");
1451 cpp_error_with_line (pfile
, DL_ERROR
, ifs
->line
, 0,
1452 "the conditional began here");
1456 /* Only evaluate this if we aren't skipping elses. During
1457 evaluation, set skipping to false to get lexer warnings. */
1458 if (ifs
->skip_elses
)
1459 pfile
->state
.skipping
= 1;
1462 pfile
->state
.skipping
= 0;
1463 pfile
->state
.skipping
= ! _cpp_parse_expr (pfile
);
1464 ifs
->skip_elses
= ! pfile
->state
.skipping
;
1467 /* Invalidate any controlling macro. */
1472 /* #endif pops the if stack and resets pfile->state.skipping. */
1477 cpp_buffer
*buffer
= pfile
->buffer
;
1478 struct if_stack
*ifs
= buffer
->if_stack
;
1481 cpp_error (pfile
, DL_ERROR
, "#endif without #if");
1484 /* Only check EOL if was not originally skipping. */
1485 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1488 /* If potential control macro, we go back outside again. */
1489 if (ifs
->next
== 0 && ifs
->mi_cmacro
)
1491 pfile
->mi_valid
= true;
1492 pfile
->mi_cmacro
= ifs
->mi_cmacro
;
1495 buffer
->if_stack
= ifs
->next
;
1496 pfile
->state
.skipping
= ifs
->was_skipping
;
1497 obstack_free (&pfile
->buffer_ob
, ifs
);
1501 /* Push an if_stack entry for a preprocessor conditional, and set
1502 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1503 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1504 we need to check here that we are at the top of the file. */
1506 push_conditional (pfile
, skip
, type
, cmacro
)
1510 const cpp_hashnode
*cmacro
;
1512 struct if_stack
*ifs
;
1513 cpp_buffer
*buffer
= pfile
->buffer
;
1515 ifs
= xobnew (&pfile
->buffer_ob
, struct if_stack
);
1516 ifs
->line
= pfile
->directive_line
;
1517 ifs
->next
= buffer
->if_stack
;
1518 ifs
->skip_elses
= pfile
->state
.skipping
|| !skip
;
1519 ifs
->was_skipping
= pfile
->state
.skipping
;
1521 /* This condition is effectively a test for top-of-file. */
1522 if (pfile
->mi_valid
&& pfile
->mi_cmacro
== 0)
1523 ifs
->mi_cmacro
= cmacro
;
1527 pfile
->state
.skipping
= skip
;
1528 buffer
->if_stack
= ifs
;
1531 /* Read the tokens of the answer into the macro pool, in a directive
1532 of type TYPE. Only commit the memory if we intend it as permanent
1533 storage, i.e. the #assert case. Returns 0 on success, and sets
1534 ANSWERP to point to the answer. */
1536 parse_answer (pfile
, answerp
, type
)
1538 struct answer
**answerp
;
1541 const cpp_token
*paren
;
1542 struct answer
*answer
;
1543 unsigned int acount
;
1545 /* In a conditional, it is legal to not have an open paren. We
1546 should save the following token in this case. */
1547 paren
= cpp_get_token (pfile
);
1549 /* If not a paren, see if we're OK. */
1550 if (paren
->type
!= CPP_OPEN_PAREN
)
1552 /* In a conditional no answer is a test for any answer. It
1553 could be followed by any token. */
1556 _cpp_backup_tokens (pfile
, 1);
1560 /* #unassert with no answer is valid - it removes all answers. */
1561 if (type
== T_UNASSERT
&& paren
->type
== CPP_EOF
)
1564 cpp_error (pfile
, DL_ERROR
, "missing '(' after predicate");
1568 for (acount
= 0;; acount
++)
1571 const cpp_token
*token
= cpp_get_token (pfile
);
1574 if (token
->type
== CPP_CLOSE_PAREN
)
1577 if (token
->type
== CPP_EOF
)
1579 cpp_error (pfile
, DL_ERROR
, "missing ')' to complete answer");
1583 /* struct answer includes the space for one token. */
1584 room_needed
= (sizeof (struct answer
) + acount
* sizeof (cpp_token
));
1586 if (BUFF_ROOM (pfile
->a_buff
) < room_needed
)
1587 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (struct answer
));
1589 dest
= &((struct answer
*) BUFF_FRONT (pfile
->a_buff
))->first
[acount
];
1592 /* Drop whitespace at start, for answer equivalence purposes. */
1594 dest
->flags
&= ~PREV_WHITE
;
1599 cpp_error (pfile
, DL_ERROR
, "predicate's answer is empty");
1603 answer
= (struct answer
*) BUFF_FRONT (pfile
->a_buff
);
1604 answer
->count
= acount
;
1605 answer
->next
= NULL
;
1611 /* Parses an assertion directive of type TYPE, returning a pointer to
1612 the hash node of the predicate, or 0 on error. If an answer was
1613 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
1614 static cpp_hashnode
*
1615 parse_assertion (pfile
, answerp
, type
)
1617 struct answer
**answerp
;
1620 cpp_hashnode
*result
= 0;
1621 const cpp_token
*predicate
;
1623 /* We don't expand predicates or answers. */
1624 pfile
->state
.prevent_expansion
++;
1627 predicate
= cpp_get_token (pfile
);
1628 if (predicate
->type
== CPP_EOF
)
1629 cpp_error (pfile
, DL_ERROR
, "assertion without predicate");
1630 else if (predicate
->type
!= CPP_NAME
)
1631 cpp_error (pfile
, DL_ERROR
, "predicate must be an identifier");
1632 else if (parse_answer (pfile
, answerp
, type
) == 0)
1634 unsigned int len
= NODE_LEN (predicate
->val
.node
);
1635 unsigned char *sym
= alloca (len
+ 1);
1637 /* Prefix '#' to get it out of macro namespace. */
1639 memcpy (sym
+ 1, NODE_NAME (predicate
->val
.node
), len
);
1640 result
= cpp_lookup (pfile
, sym
, len
+ 1);
1643 pfile
->state
.prevent_expansion
--;
1647 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1648 or a pointer to NULL if the answer is not in the chain. */
1649 static struct answer
**
1650 find_answer (node
, candidate
)
1652 const struct answer
*candidate
;
1655 struct answer
**result
;
1657 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->next
)
1659 struct answer
*answer
= *result
;
1661 if (answer
->count
== candidate
->count
)
1663 for (i
= 0; i
< answer
->count
; i
++)
1664 if (! _cpp_equiv_tokens (&answer
->first
[i
], &candidate
->first
[i
]))
1667 if (i
== answer
->count
)
1675 /* Test an assertion within a preprocessor conditional. Returns
1676 non-zero on failure, zero on success. On success, the result of
1677 the test is written into VALUE. */
1679 _cpp_test_assertion (pfile
, value
)
1681 unsigned int *value
;
1683 struct answer
*answer
;
1686 node
= parse_assertion (pfile
, &answer
, T_IF
);
1688 *value
= (node
->type
== NT_ASSERTION
&&
1689 (answer
== 0 || *find_answer (node
, answer
) != 0));
1690 else if (pfile
->cur_token
[-1].type
== CPP_EOF
)
1691 _cpp_backup_tokens (pfile
, 1);
1693 /* We don't commit the memory for the answer - it's temporary only. */
1697 /* Handle #assert. */
1702 struct answer
*new_answer
;
1705 node
= parse_assertion (pfile
, &new_answer
, T_ASSERT
);
1708 /* Place the new answer in the answer list. First check there
1709 is not a duplicate. */
1710 new_answer
->next
= 0;
1711 if (node
->type
== NT_ASSERTION
)
1713 if (*find_answer (node
, new_answer
))
1715 cpp_error (pfile
, DL_WARNING
, "\"%s\" re-asserted",
1716 NODE_NAME (node
) + 1);
1719 new_answer
->next
= node
->value
.answers
;
1722 node
->type
= NT_ASSERTION
;
1723 node
->value
.answers
= new_answer
;
1724 BUFF_FRONT (pfile
->a_buff
) += (sizeof (struct answer
)
1725 + (new_answer
->count
- 1)
1726 * sizeof (cpp_token
));
1731 /* Handle #unassert. */
1737 struct answer
*answer
;
1739 node
= parse_assertion (pfile
, &answer
, T_UNASSERT
);
1740 /* It isn't an error to #unassert something that isn't asserted. */
1741 if (node
&& node
->type
== NT_ASSERTION
)
1745 struct answer
**p
= find_answer (node
, answer
), *temp
;
1747 /* Remove the answer from the list. */
1752 /* Did we free the last answer? */
1753 if (node
->value
.answers
== 0)
1754 node
->type
= NT_VOID
;
1759 _cpp_free_definition (node
);
1762 /* We don't commit the memory for the answer - it's temporary only. */
1765 /* These are for -D, -U, -A. */
1767 /* Process the string STR as if it appeared as the body of a #define.
1768 If STR is just an identifier, define it with value 1.
1769 If STR has anything after the identifier, then it should
1770 be identifier=definition. */
1772 cpp_define (pfile
, str
)
1779 /* Copy the entire option so we can modify it.
1780 Change the first "=" in the string to a space. If there is none,
1781 tack " 1" on the end. */
1783 count
= strlen (str
);
1784 buf
= (char *) alloca (count
+ 3);
1785 memcpy (buf
, str
, count
);
1787 p
= strchr (str
, '=');
1797 run_directive (pfile
, T_DEFINE
, buf
, count
);
1800 /* Slight variant of the above for use by initialize_builtins. */
1802 _cpp_define_builtin (pfile
, str
)
1806 run_directive (pfile
, T_DEFINE
, str
, strlen (str
));
1809 /* Process MACRO as if it appeared as the body of an #undef. */
1811 cpp_undef (pfile
, macro
)
1815 run_directive (pfile
, T_UNDEF
, macro
, strlen (macro
));
1818 /* Process the string STR as if it appeared as the body of a #assert. */
1820 cpp_assert (pfile
, str
)
1824 handle_assertion (pfile
, str
, T_ASSERT
);
1827 /* Process STR as if it appeared as the body of an #unassert. */
1829 cpp_unassert (pfile
, str
)
1833 handle_assertion (pfile
, str
, T_UNASSERT
);
1836 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1838 handle_assertion (pfile
, str
, type
)
1843 size_t count
= strlen (str
);
1844 const char *p
= strchr (str
, '=');
1848 /* Copy the entire option so we can modify it. Change the first
1849 "=" in the string to a '(', and tack a ')' on the end. */
1850 char *buf
= (char *) alloca (count
+ 2);
1852 memcpy (buf
, str
, count
);
1859 run_directive (pfile
, type
, str
, count
);
1862 /* The number of errors for a given reader. */
1867 return pfile
->errors
;
1870 /* The options structure. */
1872 cpp_get_options (pfile
)
1875 return &pfile
->opts
;
1878 /* The callbacks structure. */
1880 cpp_get_callbacks (pfile
)
1886 /* The line map set. */
1887 const struct line_maps
*
1888 cpp_get_line_maps (pfile
)
1891 return &pfile
->line_maps
;
1894 /* Copy the given callbacks structure to our own. */
1896 cpp_set_callbacks (pfile
, cb
)
1903 /* Push a new buffer on the buffer stack. Returns the new buffer; it
1904 doesn't fail. It does not generate a file change call back; that
1905 is the responsibility of the caller. */
1907 cpp_push_buffer (pfile
, buffer
, len
, from_stage3
, return_at_eof
)
1909 const uchar
*buffer
;
1914 cpp_buffer
*new = xobnew (&pfile
->buffer_ob
, cpp_buffer
);
1916 /* Clears, amongst other things, if_stack and mi_cmacro. */
1917 memset (new, 0, sizeof (cpp_buffer
));
1919 new->line_base
= new->buf
= new->cur
= buffer
;
1920 new->rlimit
= buffer
+ len
;
1921 new->from_stage3
= from_stage3
|| CPP_OPTION (pfile
, traditional
);
1922 new->prev
= pfile
->buffer
;
1923 new->return_at_eof
= return_at_eof
;
1924 new->saved_flags
= BOL
;
1926 pfile
->buffer
= new;
1928 if (CPP_OPTION (pfile
, traditional
))
1929 _cpp_set_trad_context (pfile
);
1934 /* Pops a single buffer, with a file change call-back if appropriate.
1935 Then pushes the next -include file, if any remain. */
1937 _cpp_pop_buffer (pfile
)
1940 cpp_buffer
*buffer
= pfile
->buffer
;
1941 struct include_file
*inc
= buffer
->inc
;
1942 struct if_stack
*ifs
;
1944 /* Walk back up the conditional stack till we reach its level at
1945 entry to this file, issuing error messages. */
1946 for (ifs
= buffer
->if_stack
; ifs
; ifs
= ifs
->next
)
1947 cpp_error_with_line (pfile
, DL_ERROR
, ifs
->line
, 0,
1948 "unterminated #%s", dtable
[ifs
->type
].name
);
1950 /* In case of a missing #endif. */
1951 pfile
->state
.skipping
= 0;
1953 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
1954 pfile
->buffer
= buffer
->prev
;
1956 /* Free the buffer object now; we may want to push a new buffer
1957 in _cpp_push_next_include_file. */
1958 obstack_free (&pfile
->buffer_ob
, buffer
);
1962 _cpp_pop_file_buffer (pfile
, inc
);
1964 /* Don't generate a callback for popping the main file. */
1967 _cpp_do_file_change (pfile
, LC_LEAVE
, 0, 0, 0);
1969 /* If this is the main file, there may be some -include
1970 files left to push. */
1971 if (!pfile
->buffer
->prev
)
1972 _cpp_maybe_push_include_file (pfile
);
1976 if (pfile
->buffer
&& CPP_OPTION (pfile
, traditional
))
1977 _cpp_set_trad_context (pfile
);
1980 /* Enter all recognised directives in the hash table. */
1982 _cpp_init_directives (pfile
)
1988 for (i
= 0; i
< (unsigned int) N_DIRECTIVES
; i
++)
1990 node
= cpp_lookup (pfile
, dtable
[i
].name
, dtable
[i
].length
);
1991 node
->directive_index
= i
+ 1;