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 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 | EXPAND) /* 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 | EXPAND) /* 19 */ \
163 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
164 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
165 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
166 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
167 D(sccs, T_SCCS, EXTENSION, 0) /* 0 SVR4? */
169 /* Use the table to generate a series of prototypes, an enum for the
170 directive names, and an array of directive handlers. */
172 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
173 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
177 #define D(n, tag, o, f) tag,
185 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
186 #define D(name, t, origin, flags) \
187 { CONCAT2(do_,name), (const uchar *) STRINGX(name), \
188 sizeof STRINGX(name) - 1, origin, flags },
189 static const directive dtable
[] =
194 #undef DIRECTIVE_TABLE
196 /* Wrapper struct directive for linemarkers.
197 The origin is more or less true - the original K+R cpp
198 did use this notation in its preprocessed output. */
199 static const directive linemarker_dir
=
201 do_linemarker
, U
"#", 1, KANDR
, IN_I
204 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
206 /* Skip any remaining tokens in a directive. */
208 skip_rest_of_line (pfile
)
211 /* Discard all stacked contexts. */
212 while (pfile
->context
->prev
)
213 _cpp_pop_context (pfile
);
215 /* Sweep up all tokens remaining on the line. */
217 while (_cpp_lex_token (pfile
)->type
!= CPP_EOF
)
221 /* Ensure there are no stray tokens at the end of a directive. */
226 if (! SEEN_EOL () && _cpp_lex_token (pfile
)->type
!= CPP_EOF
)
227 cpp_error (pfile
, DL_PEDWARN
, "extra tokens at end of #%s directive",
228 pfile
->directive
->name
);
231 /* Called when entering a directive, _Pragma or command-line directive. */
233 start_directive (pfile
)
236 /* Setup in-directive state. */
237 pfile
->state
.in_directive
= 1;
238 pfile
->state
.save_comments
= 0;
240 /* Some handlers need the position of the # for diagnostics. */
241 pfile
->directive_line
= pfile
->line
;
244 /* Called when leaving a directive, _Pragma or command-line directive. */
246 end_directive (pfile
, skip_line
)
250 if (CPP_OPTION (pfile
, traditional
))
252 /* Revert change of prepare_directive_trad. */
253 pfile
->state
.prevent_expansion
--;
255 if (pfile
->directive
!= &dtable
[T_DEFINE
])
256 _cpp_remove_overlay (pfile
);
258 /* We don't skip for an assembler #. */
261 skip_rest_of_line (pfile
);
262 if (!pfile
->keep_tokens
)
264 pfile
->cur_run
= &pfile
->base_run
;
265 pfile
->cur_token
= pfile
->base_run
.base
;
270 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
271 pfile
->state
.in_directive
= 0;
272 pfile
->state
.in_expression
= 0;
273 pfile
->state
.angled_headers
= 0;
274 pfile
->directive
= 0;
277 /* Prepare to handle the directive in pfile->directive. */
279 prepare_directive_trad (pfile
)
282 if (pfile
->directive
!= &dtable
[T_DEFINE
])
284 bool no_expand
= (pfile
->directive
285 && ! (pfile
->directive
->flags
& EXPAND
));
286 bool was_skipping
= pfile
->state
.skipping
;
288 pfile
->state
.skipping
= false;
289 pfile
->state
.in_expression
= (pfile
->directive
== &dtable
[T_IF
]
290 || pfile
->directive
== &dtable
[T_ELIF
]);
292 pfile
->state
.prevent_expansion
++;
293 _cpp_read_logical_line_trad (pfile
);
295 pfile
->state
.prevent_expansion
--;
296 pfile
->state
.skipping
= was_skipping
;
297 _cpp_overlay_buffer (pfile
, pfile
->out
.base
,
298 pfile
->out
.cur
- pfile
->out
.base
);
301 /* Stop ISO C from expanding anything. */
302 pfile
->state
.prevent_expansion
++;
305 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
306 the '#' was indented. */
308 directive_diagnostics (pfile
, dir
, indented
)
310 const directive
*dir
;
313 /* Issue -pedantic warnings for extensions. */
314 if (CPP_PEDANTIC (pfile
)
315 && ! pfile
->state
.skipping
316 && dir
->origin
== EXTENSION
)
317 cpp_error (pfile
, DL_PEDWARN
, "#%s is a GCC extension", dir
->name
);
319 /* Traditionally, a directive is ignored unless its # is in
320 column 1. Therefore in code intended to work with K+R
321 compilers, directives added by C89 must have their #
322 indented, and directives present in traditional C must not.
323 This is true even of directives in skipped conditional
324 blocks. #elif cannot be used at all. */
325 if (CPP_WTRADITIONAL (pfile
))
327 if (dir
== &dtable
[T_ELIF
])
328 cpp_error (pfile
, DL_WARNING
,
329 "suggest not using #elif in traditional C");
330 else if (indented
&& dir
->origin
== KANDR
)
331 cpp_error (pfile
, DL_WARNING
,
332 "traditional C ignores #%s with the # indented",
334 else if (!indented
&& dir
->origin
!= KANDR
)
335 cpp_error (pfile
, DL_WARNING
,
336 "suggest hiding #%s from traditional C with an indented #",
341 /* Check if we have a known directive. INDENTED is nonzero if the
342 '#' of the directive was indented. This function is in this file
343 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
344 nonzero if the line of tokens has been handled, zero if we should
345 continue processing the line. */
347 _cpp_handle_directive (pfile
, indented
)
351 const directive
*dir
= 0;
352 const cpp_token
*dname
;
353 bool was_parsing_args
= pfile
->state
.parsing_args
;
356 if (was_parsing_args
)
358 if (CPP_OPTION (pfile
, pedantic
))
359 cpp_error (pfile
, DL_PEDWARN
,
360 "embedding a directive within macro arguments is not portable");
361 pfile
->state
.parsing_args
= 0;
362 pfile
->state
.prevent_expansion
= 0;
364 start_directive (pfile
);
365 dname
= _cpp_lex_token (pfile
);
367 if (dname
->type
== CPP_NAME
)
369 if (dname
->val
.node
->is_directive
)
370 dir
= &dtable
[dname
->val
.node
->directive_index
];
372 /* We do not recognize the # followed by a number extension in
374 else if (dname
->type
== CPP_NUMBER
&& CPP_OPTION (pfile
, lang
) != CLK_ASM
)
376 dir
= &linemarker_dir
;
377 if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, preprocessed
)
378 && ! pfile
->state
.skipping
)
379 cpp_error (pfile
, DL_PEDWARN
,
380 "style of line directive is a GCC extension");
385 /* If we have a directive that is not an opening conditional,
386 invalidate any control macro. */
387 if (! (dir
->flags
& IF_COND
))
388 pfile
->mi_valid
= false;
390 /* Kluge alert. In order to be sure that code like this
395 does not cause '#define foo bar' to get executed when
396 compiled with -save-temps, we recognize directives in
397 -fpreprocessed mode only if the # is in column 1. cppmacro.c
398 puts a space in front of any '#' at the start of a macro. */
399 if (CPP_OPTION (pfile
, preprocessed
)
400 && (indented
|| !(dir
->flags
& IN_I
)))
407 /* In failed conditional groups, all non-conditional
408 directives are ignored. Before doing that, whether
409 skipping or not, we should lex angle-bracketed headers
410 correctly, and maybe output some diagnostics. */
411 pfile
->state
.angled_headers
= dir
->flags
& INCL
;
412 if (! CPP_OPTION (pfile
, preprocessed
))
413 directive_diagnostics (pfile
, dir
, indented
);
414 if (pfile
->state
.skipping
&& !(dir
->flags
& COND
))
418 else if (dname
->type
== CPP_EOF
)
419 ; /* CPP_EOF is the "null directive". */
422 /* An unknown directive. Don't complain about it in assembly
423 source: we don't know where the comments are, and # may
424 introduce assembler pseudo-ops. Don't complain about invalid
425 directives in skipped conditional groups (6.10 p4). */
426 if (CPP_OPTION (pfile
, lang
) == CLK_ASM
)
428 else if (!pfile
->state
.skipping
)
429 cpp_error (pfile
, DL_ERROR
, "invalid preprocessing directive #%s",
430 cpp_token_as_text (pfile
, dname
));
433 pfile
->directive
= dir
;
434 if (CPP_OPTION (pfile
, traditional
))
435 prepare_directive_trad (pfile
);
438 (*pfile
->directive
->handler
) (pfile
);
440 _cpp_backup_tokens (pfile
, 1);
442 end_directive (pfile
, skip
);
443 if (was_parsing_args
)
445 /* Restore state when within macro args. */
446 pfile
->state
.parsing_args
= 2;
447 pfile
->state
.prevent_expansion
= 1;
448 pfile
->buffer
->saved_flags
|= PREV_WHITE
;
453 /* Directive handler wrapper used by the command line option
456 run_directive (pfile
, dir_no
, buf
, count
)
462 cpp_push_buffer (pfile
, (const uchar
*) buf
, count
,
463 /* from_stage3 */ true, 1);
464 /* Disgusting hack. */
465 if (dir_no
== T_PRAGMA
)
466 pfile
->buffer
->inc
= pfile
->buffer
->prev
->inc
;
467 start_directive (pfile
);
468 /* We don't want a leading # to be interpreted as a directive. */
469 pfile
->buffer
->saved_flags
= 0;
470 pfile
->directive
= &dtable
[dir_no
];
471 if (CPP_OPTION (pfile
, traditional
))
472 prepare_directive_trad (pfile
);
473 (void) (*pfile
->directive
->handler
) (pfile
);
474 end_directive (pfile
, 1);
475 if (dir_no
== T_PRAGMA
)
476 pfile
->buffer
->inc
= NULL
;
477 _cpp_pop_buffer (pfile
);
480 /* Checks for validity the macro name in #define, #undef, #ifdef and
481 #ifndef directives. */
482 static cpp_hashnode
*
483 lex_macro_node (pfile
)
486 const cpp_token
*token
= _cpp_lex_token (pfile
);
488 /* The token immediately after #define must be an identifier. That
489 identifier may not be "defined", per C99 6.10.8p4.
490 In C++, it may not be any of the "named operators" either,
491 per C++98 [lex.digraph], [lex.key].
492 Finally, the identifier may not have been poisoned. (In that case
493 the lexer has issued the error message for us.) */
495 if (token
->type
== CPP_NAME
)
497 cpp_hashnode
*node
= token
->val
.node
;
499 if (node
== pfile
->spec_nodes
.n_defined
)
500 cpp_error (pfile
, DL_ERROR
,
501 "\"defined\" cannot be used as a macro name");
502 else if (! (node
->flags
& NODE_POISONED
))
505 else if (token
->flags
& NAMED_OP
)
506 cpp_error (pfile
, DL_ERROR
,
507 "\"%s\" cannot be used as a macro name as it is an operator in C++",
508 NODE_NAME (token
->val
.node
));
509 else if (token
->type
== CPP_EOF
)
510 cpp_error (pfile
, DL_ERROR
, "no macro name given in #%s directive",
511 pfile
->directive
->name
);
513 cpp_error (pfile
, DL_ERROR
, "macro names must be identifiers");
518 /* Process a #define directive. Most work is done in cppmacro.c. */
523 cpp_hashnode
*node
= lex_macro_node (pfile
);
527 /* If we have been requested to expand comments into macros,
528 then re-enable saving of comments. */
529 pfile
->state
.save_comments
=
530 ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
);
532 if (_cpp_create_definition (pfile
, node
))
533 if (pfile
->cb
.define
)
534 (*pfile
->cb
.define
) (pfile
, pfile
->directive_line
, node
);
538 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
543 cpp_hashnode
*node
= lex_macro_node (pfile
);
545 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
546 is not currently defined as a macro name. */
547 if (node
&& node
->type
== NT_MACRO
)
550 (*pfile
->cb
.undef
) (pfile
, pfile
->directive_line
, node
);
552 if (node
->flags
& NODE_WARN
)
553 cpp_error (pfile
, DL_WARNING
, "undefining \"%s\"", NODE_NAME (node
));
555 if (CPP_OPTION (pfile
, warn_unused_macros
))
556 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
558 _cpp_free_definition (node
);
563 /* Helper routine used by parse_include. Reinterpret the current line
564 as an h-char-sequence (< ... >); we are looking at the first token
565 after the <. Returns the header as a token, or NULL on failure. */
566 static const cpp_token
*
567 glue_header_name (pfile
)
570 cpp_token
*header
= NULL
;
571 const cpp_token
*token
;
572 unsigned char *buffer
;
573 size_t len
, total_len
= 0, capacity
= 1024;
575 /* To avoid lexed tokens overwriting our glued name, we can only
576 allocate from the string pool once we've lexed everything. */
577 buffer
= (unsigned char *) xmalloc (capacity
);
580 token
= cpp_get_token (pfile
);
582 if (token
->type
== CPP_GREATER
|| token
->type
== CPP_EOF
)
585 len
= cpp_token_len (token
);
586 if (total_len
+ len
> capacity
)
588 capacity
= (capacity
+ len
) * 2;
589 buffer
= (unsigned char *) xrealloc (buffer
, capacity
);
592 if (token
->flags
& PREV_WHITE
)
593 buffer
[total_len
++] = ' ';
595 total_len
= cpp_spell_token (pfile
, token
, &buffer
[total_len
]) - buffer
;
598 if (token
->type
== CPP_EOF
)
599 cpp_error (pfile
, DL_ERROR
, "missing terminating > character");
602 unsigned char *token_mem
= _cpp_unaligned_alloc (pfile
, total_len
+ 1);
603 memcpy (token_mem
, buffer
, total_len
);
604 token_mem
[total_len
] = '\0';
606 header
= _cpp_temp_token (pfile
);
607 header
->type
= CPP_HEADER_NAME
;
609 header
->val
.str
.len
= total_len
;
610 header
->val
.str
.text
= token_mem
;
617 /* Returns the header string of #include, #include_next, #import and
618 #pragma dependency. Returns NULL on error. */
619 static const cpp_token
*
620 parse_include (pfile
)
623 const unsigned char *dir
;
624 const cpp_token
*header
;
626 if (pfile
->directive
== &dtable
[T_PRAGMA
])
627 dir
= U
"pragma dependency";
629 dir
= pfile
->directive
->name
;
631 /* Allow macro expansion. */
632 header
= cpp_get_token (pfile
);
633 if (header
->type
!= CPP_STRING
&& header
->type
!= CPP_HEADER_NAME
)
635 if (header
->type
!= CPP_LESS
)
637 cpp_error (pfile
, DL_ERROR
,
638 "#%s expects \"FILENAME\" or <FILENAME>", dir
);
642 header
= glue_header_name (pfile
);
647 if (header
->val
.str
.len
== 0)
649 cpp_error (pfile
, DL_ERROR
, "empty file name in #%s", dir
);
656 /* Handle #include, #include_next and #import. */
658 do_include_common (pfile
, type
)
660 enum include_type type
;
662 const cpp_token
*header
;
664 /* For #include_next, if this is the primary source file, warn and
665 use the normal search logic. */
666 if (type
== IT_INCLUDE_NEXT
&& ! pfile
->buffer
->prev
)
668 cpp_error (pfile
, DL_WARNING
, "#include_next in primary source file");
671 else if (type
== IT_IMPORT
&& CPP_OPTION (pfile
, warn_import
))
673 CPP_OPTION (pfile
, warn_import
) = 0;
674 cpp_error (pfile
, DL_WARNING
,
675 "#import is obsolete, use an #ifndef wrapper in the header file");
678 header
= parse_include (pfile
);
681 /* Prevent #include recursion. */
682 if (pfile
->line_maps
.depth
>= CPP_STACK_MAX
)
683 cpp_error (pfile
, DL_ERROR
, "#include nested too deeply");
687 /* Get out of macro context, if we are. */
688 skip_rest_of_line (pfile
);
689 if (pfile
->cb
.include
)
690 (*pfile
->cb
.include
) (pfile
, pfile
->directive_line
,
691 pfile
->directive
->name
, header
);
692 _cpp_execute_include (pfile
, header
, type
);
701 do_include_common (pfile
, IT_INCLUDE
);
708 do_include_common (pfile
, IT_IMPORT
);
712 do_include_next (pfile
)
715 do_include_common (pfile
, IT_INCLUDE_NEXT
);
718 /* Subroutine of do_linemarker. Read possible flags after file name.
719 LAST is the last flag seen; 0 if this is the first flag. Return the
720 flag if it is valid, 0 at the end of the directive. Otherwise
723 read_flag (pfile
, last
)
727 const cpp_token
*token
= _cpp_lex_token (pfile
);
729 if (token
->type
== CPP_NUMBER
&& token
->val
.str
.len
== 1)
731 unsigned int flag
= token
->val
.str
.text
[0] - '0';
733 if (flag
> last
&& flag
<= 4
734 && (flag
!= 4 || last
== 3)
735 && (flag
!= 2 || last
== 0))
739 if (token
->type
!= CPP_EOF
)
740 cpp_error (pfile
, DL_ERROR
, "invalid flag \"%s\" in line directive",
741 cpp_token_as_text (pfile
, token
));
745 /* Subroutine of do_line and do_linemarker. Returns a version of STR
746 which has a NUL terminator and all escape sequences converted to
747 their equivalents. Temporary, hopefully. */
749 dequote_string (pfile
, str
, len
)
754 uchar
*result
= _cpp_unaligned_alloc (pfile
, len
+ 1);
756 const uchar
*limit
= str
+ len
;
765 *dst
++ = cpp_parse_escape (pfile
, &str
, limit
, 0);
771 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
772 of length LEN, to binary; store it in NUMP, and return 0 if the
773 number was well-formed, 1 if not. Temporary, hopefully. */
775 strtoul_for_line (str
, len
, nump
)
780 unsigned long reg
= 0;
794 /* Interpret #line command.
795 Note that the filename string (if any) is a true string constant
796 (escapes are interpreted), unlike in #line. */
801 const cpp_token
*token
;
802 const char *new_file
= pfile
->map
->to_file
;
803 unsigned long new_lineno
;
805 /* C99 raised the minimum limit on #line numbers. */
806 unsigned int cap
= CPP_OPTION (pfile
, c99
) ? 2147483647 : 32767;
808 /* #line commands expand macros. */
809 token
= cpp_get_token (pfile
);
810 if (token
->type
!= CPP_NUMBER
811 || strtoul_for_line (token
->val
.str
.text
, token
->val
.str
.len
,
814 cpp_error (pfile
, DL_ERROR
,
815 "\"%s\" after #line is not a positive integer",
816 cpp_token_as_text (pfile
, token
));
820 if (CPP_PEDANTIC (pfile
) && (new_lineno
== 0 || new_lineno
> cap
))
821 cpp_error (pfile
, DL_PEDWARN
, "line number out of range");
823 token
= cpp_get_token (pfile
);
824 if (token
->type
== CPP_STRING
)
826 new_file
= (const char *) dequote_string (pfile
, token
->val
.str
.text
,
830 else if (token
->type
!= CPP_EOF
)
832 cpp_error (pfile
, DL_ERROR
, "\"%s\" is not a valid filename",
833 cpp_token_as_text (pfile
, token
));
837 skip_rest_of_line (pfile
);
838 _cpp_do_file_change (pfile
, LC_RENAME
, new_file
, new_lineno
,
842 /* Interpret the # 44 "file" [flags] notation, which has slightly
843 different syntax and semantics from #line: Flags are allowed,
844 and we never complain about the line number being too big. */
846 do_linemarker (pfile
)
849 const cpp_token
*token
;
850 const char *new_file
= pfile
->map
->to_file
;
851 unsigned long new_lineno
;
852 unsigned int new_sysp
= pfile
->map
->sysp
;
853 enum lc_reason reason
= LC_RENAME
;
856 /* Back up so we can get the number again. Putting this in
857 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
858 some circumstances, which can segfault. */
859 _cpp_backup_tokens (pfile
, 1);
861 /* #line commands expand macros. */
862 token
= cpp_get_token (pfile
);
863 if (token
->type
!= CPP_NUMBER
864 || strtoul_for_line (token
->val
.str
.text
, token
->val
.str
.len
,
867 cpp_error (pfile
, DL_ERROR
, "\"%s\" after # is not a positive integer",
868 cpp_token_as_text (pfile
, token
));
872 token
= cpp_get_token (pfile
);
873 if (token
->type
== CPP_STRING
)
875 new_file
= (const char *) dequote_string (pfile
, token
->val
.str
.text
,
878 flag
= read_flag (pfile
, 0);
882 /* Fake an include for cpp_included (). */
883 _cpp_fake_include (pfile
, new_file
);
884 flag
= read_flag (pfile
, flag
);
889 flag
= read_flag (pfile
, flag
);
894 flag
= read_flag (pfile
, flag
);
901 else if (token
->type
!= CPP_EOF
)
903 cpp_error (pfile
, DL_ERROR
, "\"%s\" is not a valid filename",
904 cpp_token_as_text (pfile
, token
));
908 skip_rest_of_line (pfile
);
909 _cpp_do_file_change (pfile
, reason
, new_file
, new_lineno
, new_sysp
);
912 /* Arrange the file_change callback. pfile->line has changed to
913 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
914 header, 2 for a system header that needs to be extern "C" protected,
915 and zero otherwise. */
917 _cpp_do_file_change (pfile
, reason
, to_file
, file_line
, sysp
)
919 enum lc_reason reason
;
921 unsigned int file_line
;
924 pfile
->map
= add_line_map (&pfile
->line_maps
, reason
, sysp
,
925 pfile
->line
, to_file
, file_line
);
927 if (pfile
->cb
.file_change
)
928 (*pfile
->cb
.file_change
) (pfile
, pfile
->map
);
931 /* Report a warning or error detected by the program we are
932 processing. Use the directive's tokens in the error message. */
934 do_diagnostic (pfile
, code
, print_dir
)
939 if (_cpp_begin_message (pfile
, code
,
940 pfile
->cur_token
[-1].line
,
941 pfile
->cur_token
[-1].col
))
944 fprintf (stderr
, "#%s ", pfile
->directive
->name
);
945 pfile
->state
.prevent_expansion
++;
946 cpp_output_line (pfile
, stderr
);
947 pfile
->state
.prevent_expansion
--;
955 do_diagnostic (pfile
, DL_ERROR
, 1);
962 /* We want #warning diagnostics to be emitted in system headers too. */
963 do_diagnostic (pfile
, DL_WARNING_SYSHDR
, 1);
966 /* Report program identification. */
971 const cpp_token
*str
= cpp_get_token (pfile
);
973 if (str
->type
!= CPP_STRING
)
974 cpp_error (pfile
, DL_ERROR
, "invalid #ident directive");
975 else if (pfile
->cb
.ident
)
976 (*pfile
->cb
.ident
) (pfile
, pfile
->directive_line
, &str
->val
.str
);
981 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
982 matching entry, or NULL if none is found. The returned entry could
983 be the start of a namespace chain, or a pragma. */
984 static struct pragma_entry
*
985 lookup_pragma_entry (chain
, pragma
)
986 struct pragma_entry
*chain
;
987 const cpp_hashnode
*pragma
;
989 while (chain
&& chain
->pragma
!= pragma
)
995 /* Create and insert a pragma entry for NAME at the beginning of a
996 singly-linked CHAIN. If handler is NULL, it is a namespace,
997 otherwise it is a pragma and its handler. */
998 static struct pragma_entry
*
999 insert_pragma_entry (pfile
, chain
, pragma
, handler
)
1001 struct pragma_entry
**chain
;
1002 const cpp_hashnode
*pragma
;
1005 struct pragma_entry
*new;
1007 new = (struct pragma_entry
*)
1008 _cpp_aligned_alloc (pfile
, sizeof (struct pragma_entry
));
1009 new->pragma
= pragma
;
1013 new->u
.handler
= handler
;
1018 new->u
.space
= NULL
;
1026 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1027 goes in the global namespace. HANDLER is the handler it will call,
1028 which must be non-NULL. */
1030 cpp_register_pragma (pfile
, space
, name
, handler
)
1036 struct pragma_entry
**chain
= &pfile
->pragmas
;
1037 struct pragma_entry
*entry
;
1038 const cpp_hashnode
*node
;
1045 node
= cpp_lookup (pfile
, U space
, strlen (space
));
1046 entry
= lookup_pragma_entry (*chain
, node
);
1048 entry
= insert_pragma_entry (pfile
, chain
, node
, NULL
);
1049 else if (!entry
->is_nspace
)
1051 chain
= &entry
->u
.space
;
1054 /* Check for duplicates. */
1055 node
= cpp_lookup (pfile
, U name
, strlen (name
));
1056 entry
= lookup_pragma_entry (*chain
, node
);
1059 if (entry
->is_nspace
)
1061 cpp_error (pfile
, DL_ICE
,
1062 "registering \"%s\" as both a pragma and a pragma namespace",
1065 cpp_error (pfile
, DL_ICE
, "#pragma %s %s is already registered",
1068 cpp_error (pfile
, DL_ICE
, "#pragma %s is already registered", name
);
1071 insert_pragma_entry (pfile
, chain
, node
, handler
);
1074 /* Register the pragmas the preprocessor itself handles. */
1076 _cpp_init_internal_pragmas (pfile
)
1079 /* Pragmas in the global namespace. */
1080 cpp_register_pragma (pfile
, 0, "once", do_pragma_once
);
1082 /* New GCC-specific pragmas should be put in the GCC namespace. */
1083 cpp_register_pragma (pfile
, "GCC", "poison", do_pragma_poison
);
1084 cpp_register_pragma (pfile
, "GCC", "system_header", do_pragma_system_header
);
1085 cpp_register_pragma (pfile
, "GCC", "dependency", do_pragma_dependency
);
1088 /* Pragmata handling. We handle some, and pass the rest on to the
1089 front end. C99 defines three pragmas and says that no macro
1090 expansion is to be performed on them; whether or not macro
1091 expansion happens for other pragmas is implementation defined.
1092 This implementation never macro-expands the text after #pragma. */
1097 const struct pragma_entry
*p
= NULL
;
1098 const cpp_token
*token
;
1099 unsigned int count
= 1;
1101 pfile
->state
.prevent_expansion
++;
1103 token
= cpp_get_token (pfile
);
1104 if (token
->type
== CPP_NAME
)
1106 p
= lookup_pragma_entry (pfile
->pragmas
, token
->val
.node
);
1107 if (p
&& p
->is_nspace
)
1110 token
= cpp_get_token (pfile
);
1111 if (token
->type
== CPP_NAME
)
1112 p
= lookup_pragma_entry (p
->u
.space
, token
->val
.node
);
1118 /* FIXME. This is an awful kludge to get the front ends to update
1119 their notion of line number for diagnostic purposes. The line
1120 number should be passed to the handler and they should do it
1121 themselves. Stand-alone CPP must ignore us, otherwise it will
1122 prefix the directive with spaces, hence the 1. Ugh. */
1123 if (pfile
->cb
.line_change
)
1124 (*pfile
->cb
.line_change
)(pfile
, token
, 1);
1127 (*p
->u
.handler
) (pfile
);
1128 else if (pfile
->cb
.def_pragma
)
1130 _cpp_backup_tokens (pfile
, count
);
1131 (*pfile
->cb
.def_pragma
) (pfile
, pfile
->directive_line
);
1134 pfile
->state
.prevent_expansion
--;
1137 /* Handle #pragma once. */
1139 do_pragma_once (pfile
)
1142 cpp_error (pfile
, DL_WARNING
, "#pragma once is obsolete");
1144 if (pfile
->buffer
->prev
== NULL
)
1145 cpp_error (pfile
, DL_WARNING
, "#pragma once in main file");
1147 _cpp_never_reread (pfile
->buffer
->inc
);
1152 /* Handle #pragma GCC poison, to poison one or more identifiers so
1153 that the lexer produces a hard error for each subsequent usage. */
1155 do_pragma_poison (pfile
)
1158 const cpp_token
*tok
;
1161 pfile
->state
.poisoned_ok
= 1;
1164 tok
= _cpp_lex_token (pfile
);
1165 if (tok
->type
== CPP_EOF
)
1167 if (tok
->type
!= CPP_NAME
)
1169 cpp_error (pfile
, DL_ERROR
, "invalid #pragma GCC poison directive");
1174 if (hp
->flags
& NODE_POISONED
)
1177 if (hp
->type
== NT_MACRO
)
1178 cpp_error (pfile
, DL_WARNING
, "poisoning existing macro \"%s\"",
1180 _cpp_free_definition (hp
);
1181 hp
->flags
|= NODE_POISONED
| NODE_DIAGNOSTIC
;
1183 pfile
->state
.poisoned_ok
= 0;
1186 /* Mark the current header as a system header. This will suppress
1187 some categories of warnings (notably those from -pedantic). It is
1188 intended for use in system libraries that cannot be implemented in
1189 conforming C, but cannot be certain that their headers appear in a
1190 system include directory. To prevent abuse, it is rejected in the
1191 primary source file. */
1193 do_pragma_system_header (pfile
)
1196 cpp_buffer
*buffer
= pfile
->buffer
;
1198 if (buffer
->prev
== 0)
1199 cpp_error (pfile
, DL_WARNING
,
1200 "#pragma system_header ignored outside include file");
1204 skip_rest_of_line (pfile
);
1205 cpp_make_system_header (pfile
, 1, 0);
1209 /* Check the modified date of the current include file against a specified
1210 file. Issue a diagnostic, if the specified file is newer. We use this to
1211 determine if a fixed header should be refixed. */
1213 do_pragma_dependency (pfile
)
1216 const cpp_token
*header
;
1219 header
= parse_include (pfile
);
1223 ordering
= _cpp_compare_file_date (pfile
, header
);
1225 cpp_error (pfile
, DL_WARNING
, "cannot find source %s",
1226 cpp_token_as_text (pfile
, header
));
1227 else if (ordering
> 0)
1229 cpp_error (pfile
, DL_WARNING
, "current file is older than %s",
1230 cpp_token_as_text (pfile
, header
));
1231 if (cpp_get_token (pfile
)->type
!= CPP_EOF
)
1233 _cpp_backup_tokens (pfile
, 1);
1234 do_diagnostic (pfile
, DL_WARNING
, 0);
1239 /* Get a token but skip padding. */
1240 static const cpp_token
*
1241 get_token_no_padding (pfile
)
1246 const cpp_token
*result
= cpp_get_token (pfile
);
1247 if (result
->type
!= CPP_PADDING
)
1252 /* Check syntax is "(string-literal)". Returns the string on success,
1253 or NULL on failure. */
1254 static const cpp_token
*
1255 get__Pragma_string (pfile
)
1258 const cpp_token
*string
;
1260 if (get_token_no_padding (pfile
)->type
!= CPP_OPEN_PAREN
)
1263 string
= get_token_no_padding (pfile
);
1264 if (string
->type
!= CPP_STRING
&& string
->type
!= CPP_WSTRING
)
1267 if (get_token_no_padding (pfile
)->type
!= CPP_CLOSE_PAREN
)
1273 /* Destringize IN into a temporary buffer, by removing the first \ of
1274 \" and \\ sequences, and process the result as a #pragma directive. */
1276 destringize_and_run (pfile
, in
)
1278 const cpp_string
*in
;
1280 const unsigned char *src
, *limit
;
1281 char *dest
, *result
;
1283 dest
= result
= alloca (in
->len
+ 1);
1284 for (src
= in
->text
, limit
= src
+ in
->len
; src
< limit
;)
1286 /* We know there is a character following the backslash. */
1287 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1293 /* Ugh; an awful kludge. We are really not set up to be lexing
1294 tokens when in the middle of a macro expansion. Use a new
1295 context to force cpp_get_token to lex, and so skip_rest_of_line
1296 doesn't go beyond the end of the text. Also, remember the
1297 current lexing position so we can return to it later.
1299 Something like line-at-a-time lexing should remove the need for
1302 cpp_context
*saved_context
= pfile
->context
;
1303 cpp_token
*saved_cur_token
= pfile
->cur_token
;
1304 tokenrun
*saved_cur_run
= pfile
->cur_run
;
1306 pfile
->context
= xnew (cpp_context
);
1307 pfile
->context
->macro
= 0;
1308 pfile
->context
->prev
= 0;
1309 run_directive (pfile
, T_PRAGMA
, result
, dest
- result
);
1310 free (pfile
->context
);
1311 pfile
->context
= saved_context
;
1312 pfile
->cur_token
= saved_cur_token
;
1313 pfile
->cur_run
= saved_cur_run
;
1317 /* See above comment. For the moment, we'd like
1319 token1 _Pragma ("foo") token2
1329 Getting the line markers is a little tricky. */
1330 if (pfile
->cb
.line_change
)
1331 (*pfile
->cb
.line_change
) (pfile
, pfile
->cur_token
, false);
1334 /* Handle the _Pragma operator. */
1336 _cpp_do__Pragma (pfile
)
1339 const cpp_token
*string
= get__Pragma_string (pfile
);
1342 destringize_and_run (pfile
, &string
->val
.str
);
1344 cpp_error (pfile
, DL_ERROR
,
1345 "_Pragma takes a parenthesized string literal");
1348 /* Just ignore #sccs on all systems. */
1351 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
1355 /* Handle #ifdef. */
1362 if (! pfile
->state
.skipping
)
1364 const cpp_hashnode
*node
= lex_macro_node (pfile
);
1368 skip
= node
->type
!= NT_MACRO
;
1369 _cpp_mark_macro_used (node
);
1374 push_conditional (pfile
, skip
, T_IFDEF
, 0);
1377 /* Handle #ifndef. */
1383 const cpp_hashnode
*node
= 0;
1385 if (! pfile
->state
.skipping
)
1387 node
= lex_macro_node (pfile
);
1391 skip
= node
->type
== NT_MACRO
;
1392 _cpp_mark_macro_used (node
);
1397 push_conditional (pfile
, skip
, T_IFNDEF
, node
);
1400 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1401 pfile->mi_ind_cmacro so we can handle multiple-include
1402 optimisations. If macro expansion occurs in the expression, we
1403 cannot treat it as a controlling conditional, since the expansion
1404 could change in the future. That is handled by cpp_get_token. */
1411 if (! pfile
->state
.skipping
)
1412 skip
= _cpp_parse_expr (pfile
) == false;
1414 push_conditional (pfile
, skip
, T_IF
, pfile
->mi_ind_cmacro
);
1417 /* Flip skipping state if appropriate and continue without changing
1418 if_stack; this is so that the error message for missing #endif's
1419 etc. will point to the original #if. */
1424 cpp_buffer
*buffer
= pfile
->buffer
;
1425 struct if_stack
*ifs
= buffer
->if_stack
;
1428 cpp_error (pfile
, DL_ERROR
, "#else without #if");
1431 if (ifs
->type
== T_ELSE
)
1433 cpp_error (pfile
, DL_ERROR
, "#else after #else");
1434 cpp_error_with_line (pfile
, DL_ERROR
, ifs
->line
, 0,
1435 "the conditional began here");
1439 /* Skip any future (erroneous) #elses or #elifs. */
1440 pfile
->state
.skipping
= ifs
->skip_elses
;
1441 ifs
->skip_elses
= true;
1443 /* Invalidate any controlling macro. */
1446 /* Only check EOL if was not originally skipping. */
1447 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1452 /* Handle a #elif directive by not changing if_stack either. See the
1453 comment above do_else. */
1458 cpp_buffer
*buffer
= pfile
->buffer
;
1459 struct if_stack
*ifs
= buffer
->if_stack
;
1462 cpp_error (pfile
, DL_ERROR
, "#elif without #if");
1465 if (ifs
->type
== T_ELSE
)
1467 cpp_error (pfile
, DL_ERROR
, "#elif after #else");
1468 cpp_error_with_line (pfile
, DL_ERROR
, ifs
->line
, 0,
1469 "the conditional began here");
1473 /* Only evaluate this if we aren't skipping elses. During
1474 evaluation, set skipping to false to get lexer warnings. */
1475 if (ifs
->skip_elses
)
1476 pfile
->state
.skipping
= 1;
1479 pfile
->state
.skipping
= 0;
1480 pfile
->state
.skipping
= ! _cpp_parse_expr (pfile
);
1481 ifs
->skip_elses
= ! pfile
->state
.skipping
;
1484 /* Invalidate any controlling macro. */
1489 /* #endif pops the if stack and resets pfile->state.skipping. */
1494 cpp_buffer
*buffer
= pfile
->buffer
;
1495 struct if_stack
*ifs
= buffer
->if_stack
;
1498 cpp_error (pfile
, DL_ERROR
, "#endif without #if");
1501 /* Only check EOL if was not originally skipping. */
1502 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1505 /* If potential control macro, we go back outside again. */
1506 if (ifs
->next
== 0 && ifs
->mi_cmacro
)
1508 pfile
->mi_valid
= true;
1509 pfile
->mi_cmacro
= ifs
->mi_cmacro
;
1512 buffer
->if_stack
= ifs
->next
;
1513 pfile
->state
.skipping
= ifs
->was_skipping
;
1514 obstack_free (&pfile
->buffer_ob
, ifs
);
1518 /* Push an if_stack entry for a preprocessor conditional, and set
1519 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1520 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1521 we need to check here that we are at the top of the file. */
1523 push_conditional (pfile
, skip
, type
, cmacro
)
1527 const cpp_hashnode
*cmacro
;
1529 struct if_stack
*ifs
;
1530 cpp_buffer
*buffer
= pfile
->buffer
;
1532 ifs
= xobnew (&pfile
->buffer_ob
, struct if_stack
);
1533 ifs
->line
= pfile
->directive_line
;
1534 ifs
->next
= buffer
->if_stack
;
1535 ifs
->skip_elses
= pfile
->state
.skipping
|| !skip
;
1536 ifs
->was_skipping
= pfile
->state
.skipping
;
1538 /* This condition is effectively a test for top-of-file. */
1539 if (pfile
->mi_valid
&& pfile
->mi_cmacro
== 0)
1540 ifs
->mi_cmacro
= cmacro
;
1544 pfile
->state
.skipping
= skip
;
1545 buffer
->if_stack
= ifs
;
1548 /* Read the tokens of the answer into the macro pool, in a directive
1549 of type TYPE. Only commit the memory if we intend it as permanent
1550 storage, i.e. the #assert case. Returns 0 on success, and sets
1551 ANSWERP to point to the answer. */
1553 parse_answer (pfile
, answerp
, type
)
1555 struct answer
**answerp
;
1558 const cpp_token
*paren
;
1559 struct answer
*answer
;
1560 unsigned int acount
;
1562 /* In a conditional, it is legal to not have an open paren. We
1563 should save the following token in this case. */
1564 paren
= cpp_get_token (pfile
);
1566 /* If not a paren, see if we're OK. */
1567 if (paren
->type
!= CPP_OPEN_PAREN
)
1569 /* In a conditional no answer is a test for any answer. It
1570 could be followed by any token. */
1573 _cpp_backup_tokens (pfile
, 1);
1577 /* #unassert with no answer is valid - it removes all answers. */
1578 if (type
== T_UNASSERT
&& paren
->type
== CPP_EOF
)
1581 cpp_error (pfile
, DL_ERROR
, "missing '(' after predicate");
1585 for (acount
= 0;; acount
++)
1588 const cpp_token
*token
= cpp_get_token (pfile
);
1591 if (token
->type
== CPP_CLOSE_PAREN
)
1594 if (token
->type
== CPP_EOF
)
1596 cpp_error (pfile
, DL_ERROR
, "missing ')' to complete answer");
1600 /* struct answer includes the space for one token. */
1601 room_needed
= (sizeof (struct answer
) + acount
* sizeof (cpp_token
));
1603 if (BUFF_ROOM (pfile
->a_buff
) < room_needed
)
1604 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (struct answer
));
1606 dest
= &((struct answer
*) BUFF_FRONT (pfile
->a_buff
))->first
[acount
];
1609 /* Drop whitespace at start, for answer equivalence purposes. */
1611 dest
->flags
&= ~PREV_WHITE
;
1616 cpp_error (pfile
, DL_ERROR
, "predicate's answer is empty");
1620 answer
= (struct answer
*) BUFF_FRONT (pfile
->a_buff
);
1621 answer
->count
= acount
;
1622 answer
->next
= NULL
;
1628 /* Parses an assertion directive of type TYPE, returning a pointer to
1629 the hash node of the predicate, or 0 on error. If an answer was
1630 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
1631 static cpp_hashnode
*
1632 parse_assertion (pfile
, answerp
, type
)
1634 struct answer
**answerp
;
1637 cpp_hashnode
*result
= 0;
1638 const cpp_token
*predicate
;
1640 /* We don't expand predicates or answers. */
1641 pfile
->state
.prevent_expansion
++;
1644 predicate
= cpp_get_token (pfile
);
1645 if (predicate
->type
== CPP_EOF
)
1646 cpp_error (pfile
, DL_ERROR
, "assertion without predicate");
1647 else if (predicate
->type
!= CPP_NAME
)
1648 cpp_error (pfile
, DL_ERROR
, "predicate must be an identifier");
1649 else if (parse_answer (pfile
, answerp
, type
) == 0)
1651 unsigned int len
= NODE_LEN (predicate
->val
.node
);
1652 unsigned char *sym
= alloca (len
+ 1);
1654 /* Prefix '#' to get it out of macro namespace. */
1656 memcpy (sym
+ 1, NODE_NAME (predicate
->val
.node
), len
);
1657 result
= cpp_lookup (pfile
, sym
, len
+ 1);
1660 pfile
->state
.prevent_expansion
--;
1664 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1665 or a pointer to NULL if the answer is not in the chain. */
1666 static struct answer
**
1667 find_answer (node
, candidate
)
1669 const struct answer
*candidate
;
1672 struct answer
**result
;
1674 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->next
)
1676 struct answer
*answer
= *result
;
1678 if (answer
->count
== candidate
->count
)
1680 for (i
= 0; i
< answer
->count
; i
++)
1681 if (! _cpp_equiv_tokens (&answer
->first
[i
], &candidate
->first
[i
]))
1684 if (i
== answer
->count
)
1692 /* Test an assertion within a preprocessor conditional. Returns
1693 nonzero on failure, zero on success. On success, the result of
1694 the test is written into VALUE, otherwise the value 0. */
1696 _cpp_test_assertion (pfile
, value
)
1698 unsigned int *value
;
1700 struct answer
*answer
;
1703 node
= parse_assertion (pfile
, &answer
, T_IF
);
1705 /* For recovery, an erroneous assertion expression is handled as a
1706 failing assertion. */
1710 *value
= (node
->type
== NT_ASSERTION
&&
1711 (answer
== 0 || *find_answer (node
, answer
) != 0));
1712 else if (pfile
->cur_token
[-1].type
== CPP_EOF
)
1713 _cpp_backup_tokens (pfile
, 1);
1715 /* We don't commit the memory for the answer - it's temporary only. */
1719 /* Handle #assert. */
1724 struct answer
*new_answer
;
1727 node
= parse_assertion (pfile
, &new_answer
, T_ASSERT
);
1730 /* Place the new answer in the answer list. First check there
1731 is not a duplicate. */
1732 new_answer
->next
= 0;
1733 if (node
->type
== NT_ASSERTION
)
1735 if (*find_answer (node
, new_answer
))
1737 cpp_error (pfile
, DL_WARNING
, "\"%s\" re-asserted",
1738 NODE_NAME (node
) + 1);
1741 new_answer
->next
= node
->value
.answers
;
1744 node
->type
= NT_ASSERTION
;
1745 node
->value
.answers
= new_answer
;
1746 BUFF_FRONT (pfile
->a_buff
) += (sizeof (struct answer
)
1747 + (new_answer
->count
- 1)
1748 * sizeof (cpp_token
));
1753 /* Handle #unassert. */
1759 struct answer
*answer
;
1761 node
= parse_assertion (pfile
, &answer
, T_UNASSERT
);
1762 /* It isn't an error to #unassert something that isn't asserted. */
1763 if (node
&& node
->type
== NT_ASSERTION
)
1767 struct answer
**p
= find_answer (node
, answer
), *temp
;
1769 /* Remove the answer from the list. */
1774 /* Did we free the last answer? */
1775 if (node
->value
.answers
== 0)
1776 node
->type
= NT_VOID
;
1781 _cpp_free_definition (node
);
1784 /* We don't commit the memory for the answer - it's temporary only. */
1787 /* These are for -D, -U, -A. */
1789 /* Process the string STR as if it appeared as the body of a #define.
1790 If STR is just an identifier, define it with value 1.
1791 If STR has anything after the identifier, then it should
1792 be identifier=definition. */
1794 cpp_define (pfile
, str
)
1801 /* Copy the entire option so we can modify it.
1802 Change the first "=" in the string to a space. If there is none,
1803 tack " 1" on the end. */
1805 count
= strlen (str
);
1806 buf
= (char *) alloca (count
+ 3);
1807 memcpy (buf
, str
, count
);
1809 p
= strchr (str
, '=');
1819 run_directive (pfile
, T_DEFINE
, buf
, count
);
1822 /* Slight variant of the above for use by initialize_builtins. */
1824 _cpp_define_builtin (pfile
, str
)
1828 run_directive (pfile
, T_DEFINE
, str
, strlen (str
));
1831 /* Process MACRO as if it appeared as the body of an #undef. */
1833 cpp_undef (pfile
, macro
)
1837 run_directive (pfile
, T_UNDEF
, macro
, strlen (macro
));
1840 /* Process the string STR as if it appeared as the body of a #assert. */
1842 cpp_assert (pfile
, str
)
1846 handle_assertion (pfile
, str
, T_ASSERT
);
1849 /* Process STR as if it appeared as the body of an #unassert. */
1851 cpp_unassert (pfile
, str
)
1855 handle_assertion (pfile
, str
, T_UNASSERT
);
1858 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1860 handle_assertion (pfile
, str
, type
)
1865 size_t count
= strlen (str
);
1866 const char *p
= strchr (str
, '=');
1870 /* Copy the entire option so we can modify it. Change the first
1871 "=" in the string to a '(', and tack a ')' on the end. */
1872 char *buf
= (char *) alloca (count
+ 2);
1874 memcpy (buf
, str
, count
);
1881 run_directive (pfile
, type
, str
, count
);
1884 /* The number of errors for a given reader. */
1889 return pfile
->errors
;
1892 /* The options structure. */
1894 cpp_get_options (pfile
)
1897 return &pfile
->opts
;
1900 /* The callbacks structure. */
1902 cpp_get_callbacks (pfile
)
1908 /* The line map set. */
1909 const struct line_maps
*
1910 cpp_get_line_maps (pfile
)
1913 return &pfile
->line_maps
;
1916 /* Copy the given callbacks structure to our own. */
1918 cpp_set_callbacks (pfile
, cb
)
1925 /* Push a new buffer on the buffer stack. Returns the new buffer; it
1926 doesn't fail. It does not generate a file change call back; that
1927 is the responsibility of the caller. */
1929 cpp_push_buffer (pfile
, buffer
, len
, from_stage3
, return_at_eof
)
1931 const uchar
*buffer
;
1936 cpp_buffer
*new = xobnew (&pfile
->buffer_ob
, cpp_buffer
);
1938 /* Clears, amongst other things, if_stack and mi_cmacro. */
1939 memset (new, 0, sizeof (cpp_buffer
));
1941 new->line_base
= new->buf
= new->cur
= buffer
;
1942 new->rlimit
= buffer
+ len
;
1943 new->from_stage3
= from_stage3
|| CPP_OPTION (pfile
, traditional
);
1944 new->prev
= pfile
->buffer
;
1945 new->return_at_eof
= return_at_eof
;
1946 new->saved_flags
= BOL
;
1948 pfile
->buffer
= new;
1953 /* Pops a single buffer, with a file change call-back if appropriate.
1954 Then pushes the next -include file, if any remain. */
1956 _cpp_pop_buffer (pfile
)
1959 cpp_buffer
*buffer
= pfile
->buffer
;
1960 struct include_file
*inc
= buffer
->inc
;
1961 struct if_stack
*ifs
;
1963 /* Walk back up the conditional stack till we reach its level at
1964 entry to this file, issuing error messages. */
1965 for (ifs
= buffer
->if_stack
; ifs
; ifs
= ifs
->next
)
1966 cpp_error_with_line (pfile
, DL_ERROR
, ifs
->line
, 0,
1967 "unterminated #%s", dtable
[ifs
->type
].name
);
1969 /* In case of a missing #endif. */
1970 pfile
->state
.skipping
= 0;
1972 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
1973 pfile
->buffer
= buffer
->prev
;
1975 /* Free the buffer object now; we may want to push a new buffer
1976 in _cpp_push_next_include_file. */
1977 obstack_free (&pfile
->buffer_ob
, buffer
);
1981 _cpp_pop_file_buffer (pfile
, inc
);
1983 /* Don't generate a callback for popping the main file. */
1986 _cpp_do_file_change (pfile
, LC_LEAVE
, 0, 0, 0);
1988 /* If this is the main file, there may be some -include
1989 files left to push. */
1990 if (!pfile
->buffer
->prev
)
1991 _cpp_maybe_push_include_file (pfile
);
1996 /* Enter all recognized directives in the hash table. */
1998 _cpp_init_directives (pfile
)
2004 for (i
= 0; i
< (unsigned int) N_DIRECTIVES
; i
++)
2006 node
= cpp_lookup (pfile
, dtable
[i
].name
, dtable
[i
].length
);
2007 node
->is_directive
= 1;
2008 node
->directive_index
= i
;