1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 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 char *glue_header_name
PARAMS ((cpp_reader
*));
108 static const char *parse_include
PARAMS ((cpp_reader
*, int *));
109 static void push_conditional
PARAMS ((cpp_reader
*, int, int,
110 const cpp_hashnode
*));
111 static unsigned int read_flag
PARAMS ((cpp_reader
*, unsigned int));
112 static uchar
*dequote_string
PARAMS ((cpp_reader
*, const uchar
*,
114 static int strtoul_for_line
PARAMS ((const uchar
*, unsigned int,
116 static void do_diagnostic
PARAMS ((cpp_reader
*, int, int));
117 static cpp_hashnode
*lex_macro_node
PARAMS ((cpp_reader
*));
118 static void do_include_common
PARAMS ((cpp_reader
*, enum include_type
));
119 static struct pragma_entry
*lookup_pragma_entry
120 PARAMS ((struct pragma_entry
*, const cpp_hashnode
*pragma
));
121 static struct pragma_entry
*insert_pragma_entry
122 PARAMS ((cpp_reader
*, struct pragma_entry
**, const cpp_hashnode
*,
124 static int count_registered_pragmas
PARAMS ((struct pragma_entry
*));
125 static char ** save_registered_pragmas
126 PARAMS ((struct pragma_entry
*, char **));
127 static char ** restore_registered_pragmas
128 PARAMS ((cpp_reader
*, struct pragma_entry
*, char **));
129 static void do_pragma_once
PARAMS ((cpp_reader
*));
130 static void do_pragma_poison
PARAMS ((cpp_reader
*));
131 static void do_pragma_system_header
PARAMS ((cpp_reader
*));
132 static void do_pragma_dependency
PARAMS ((cpp_reader
*));
133 static void do_linemarker
PARAMS ((cpp_reader
*));
134 static const cpp_token
*get_token_no_padding
PARAMS ((cpp_reader
*));
135 static const cpp_token
*get__Pragma_string
PARAMS ((cpp_reader
*));
136 static void destringize_and_run
PARAMS ((cpp_reader
*, const cpp_string
*));
137 static int parse_answer
PARAMS ((cpp_reader
*, struct answer
**, int));
138 static cpp_hashnode
*parse_assertion
PARAMS ((cpp_reader
*, struct answer
**,
140 static struct answer
** find_answer
PARAMS ((cpp_hashnode
*,
141 const struct answer
*));
142 static void handle_assertion
PARAMS ((cpp_reader
*, const char *, int));
144 /* This is the table of directive handlers. It is ordered by
145 frequency of occurrence; the numbers at the end are directive
146 counts from all the source code I have lying around (egcs and libc
147 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
148 pcmcia-cs-3.0.9). This is no longer important as directive lookup
149 is now O(1). All extensions other than #warning and #include_next
150 are deprecated. The name is where the extension appears to have
153 #define DIRECTIVE_TABLE \
154 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
155 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
156 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
157 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
158 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
159 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
160 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
161 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
162 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
163 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
164 D(error, T_ERROR, STDC89, 0) /* 475 */ \
165 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
166 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
167 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
168 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
169 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
170 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
171 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
172 D(sccs, T_SCCS, EXTENSION, 0) /* 0 SVR4? */
174 /* Use the table to generate a series of prototypes, an enum for the
175 directive names, and an array of directive handlers. */
177 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
178 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
182 #define D(n, tag, o, f) tag,
190 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
191 #define D(name, t, origin, flags) \
192 { CONCAT2(do_,name), (const uchar *) STRINGX(name), \
193 sizeof STRINGX(name) - 1, origin, flags },
194 static const directive dtable
[] =
199 #undef DIRECTIVE_TABLE
201 /* Wrapper struct directive for linemarkers.
202 The origin is more or less true - the original K+R cpp
203 did use this notation in its preprocessed output. */
204 static const directive linemarker_dir
=
206 do_linemarker
, U
"#", 1, KANDR
, IN_I
209 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
211 /* Skip any remaining tokens in a directive. */
213 skip_rest_of_line (pfile
)
216 /* Discard all stacked contexts. */
217 while (pfile
->context
->prev
)
218 _cpp_pop_context (pfile
);
220 /* Sweep up all tokens remaining on the line. */
222 while (_cpp_lex_token (pfile
)->type
!= CPP_EOF
)
226 /* Ensure there are no stray tokens at the end of a directive. */
231 if (! SEEN_EOL () && _cpp_lex_token (pfile
)->type
!= CPP_EOF
)
232 cpp_error (pfile
, DL_PEDWARN
, "extra tokens at end of #%s directive",
233 pfile
->directive
->name
);
236 /* Called when entering a directive, _Pragma or command-line directive. */
238 start_directive (pfile
)
241 /* Setup in-directive state. */
242 pfile
->state
.in_directive
= 1;
243 pfile
->state
.save_comments
= 0;
245 /* Some handlers need the position of the # for diagnostics. */
246 pfile
->directive_line
= pfile
->line
;
249 /* Called when leaving a directive, _Pragma or command-line directive. */
251 end_directive (pfile
, skip_line
)
255 if (CPP_OPTION (pfile
, traditional
))
257 /* Revert change of prepare_directive_trad. */
258 pfile
->state
.prevent_expansion
--;
260 if (pfile
->directive
!= &dtable
[T_DEFINE
])
261 _cpp_remove_overlay (pfile
);
263 /* We don't skip for an assembler #. */
266 skip_rest_of_line (pfile
);
267 if (!pfile
->keep_tokens
)
269 pfile
->cur_run
= &pfile
->base_run
;
270 pfile
->cur_token
= pfile
->base_run
.base
;
275 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
276 pfile
->state
.in_directive
= 0;
277 pfile
->state
.in_expression
= 0;
278 pfile
->state
.angled_headers
= 0;
279 pfile
->directive
= 0;
282 /* Prepare to handle the directive in pfile->directive. */
284 prepare_directive_trad (pfile
)
287 if (pfile
->directive
!= &dtable
[T_DEFINE
])
289 bool no_expand
= (pfile
->directive
290 && ! (pfile
->directive
->flags
& EXPAND
));
291 bool was_skipping
= pfile
->state
.skipping
;
293 pfile
->state
.skipping
= false;
294 pfile
->state
.in_expression
= (pfile
->directive
== &dtable
[T_IF
]
295 || pfile
->directive
== &dtable
[T_ELIF
]);
297 pfile
->state
.prevent_expansion
++;
298 scan_out_logical_line (pfile
, NULL
);
300 pfile
->state
.prevent_expansion
--;
301 pfile
->state
.skipping
= was_skipping
;
302 _cpp_overlay_buffer (pfile
, pfile
->out
.base
,
303 pfile
->out
.cur
- pfile
->out
.base
);
306 /* Stop ISO C from expanding anything. */
307 pfile
->state
.prevent_expansion
++;
310 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
311 the '#' was indented. */
313 directive_diagnostics (pfile
, dir
, indented
)
315 const directive
*dir
;
318 /* Issue -pedantic warnings for extensions. */
319 if (CPP_PEDANTIC (pfile
)
320 && ! pfile
->state
.skipping
321 && dir
->origin
== EXTENSION
)
322 cpp_error (pfile
, DL_PEDWARN
, "#%s is a GCC extension", dir
->name
);
324 /* Traditionally, a directive is ignored unless its # is in
325 column 1. Therefore in code intended to work with K+R
326 compilers, directives added by C89 must have their #
327 indented, and directives present in traditional C must not.
328 This is true even of directives in skipped conditional
329 blocks. #elif cannot be used at all. */
330 if (CPP_WTRADITIONAL (pfile
))
332 if (dir
== &dtable
[T_ELIF
])
333 cpp_error (pfile
, DL_WARNING
,
334 "suggest not using #elif in traditional C");
335 else if (indented
&& dir
->origin
== KANDR
)
336 cpp_error (pfile
, DL_WARNING
,
337 "traditional C ignores #%s with the # indented",
339 else if (!indented
&& dir
->origin
!= KANDR
)
340 cpp_error (pfile
, DL_WARNING
,
341 "suggest hiding #%s from traditional C with an indented #",
346 /* Check if we have a known directive. INDENTED is nonzero if the
347 '#' of the directive was indented. This function is in this file
348 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
349 nonzero if the line of tokens has been handled, zero if we should
350 continue processing the line. */
352 _cpp_handle_directive (pfile
, indented
)
356 const directive
*dir
= 0;
357 const cpp_token
*dname
;
358 bool was_parsing_args
= pfile
->state
.parsing_args
;
361 if (was_parsing_args
)
363 if (CPP_OPTION (pfile
, pedantic
))
364 cpp_error (pfile
, DL_PEDWARN
,
365 "embedding a directive within macro arguments is not portable");
366 pfile
->state
.parsing_args
= 0;
367 pfile
->state
.prevent_expansion
= 0;
369 start_directive (pfile
);
370 dname
= _cpp_lex_token (pfile
);
372 if (dname
->type
== CPP_NAME
)
374 if (dname
->val
.node
->is_directive
)
375 dir
= &dtable
[dname
->val
.node
->directive_index
];
377 /* We do not recognize the # followed by a number extension in
379 else if (dname
->type
== CPP_NUMBER
&& CPP_OPTION (pfile
, lang
) != CLK_ASM
)
381 dir
= &linemarker_dir
;
382 if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, preprocessed
)
383 && ! pfile
->state
.skipping
)
384 cpp_error (pfile
, DL_PEDWARN
,
385 "style of line directive is a GCC extension");
390 /* If we have a directive that is not an opening conditional,
391 invalidate any control macro. */
392 if (! (dir
->flags
& IF_COND
))
393 pfile
->mi_valid
= false;
395 /* Kluge alert. In order to be sure that code like this
400 does not cause '#define foo bar' to get executed when
401 compiled with -save-temps, we recognize directives in
402 -fpreprocessed mode only if the # is in column 1. cppmacro.c
403 puts a space in front of any '#' at the start of a macro. */
404 if (CPP_OPTION (pfile
, preprocessed
)
405 && (indented
|| !(dir
->flags
& IN_I
)))
412 /* In failed conditional groups, all non-conditional
413 directives are ignored. Before doing that, whether
414 skipping or not, we should lex angle-bracketed headers
415 correctly, and maybe output some diagnostics. */
416 pfile
->state
.angled_headers
= dir
->flags
& INCL
;
417 pfile
->state
.directive_wants_padding
= dir
->flags
& INCL
;
418 if (! CPP_OPTION (pfile
, preprocessed
))
419 directive_diagnostics (pfile
, dir
, indented
);
420 if (pfile
->state
.skipping
&& !(dir
->flags
& COND
))
424 else if (dname
->type
== CPP_EOF
)
425 ; /* CPP_EOF is the "null directive". */
428 /* An unknown directive. Don't complain about it in assembly
429 source: we don't know where the comments are, and # may
430 introduce assembler pseudo-ops. Don't complain about invalid
431 directives in skipped conditional groups (6.10 p4). */
432 if (CPP_OPTION (pfile
, lang
) == CLK_ASM
)
434 else if (!pfile
->state
.skipping
)
435 cpp_error (pfile
, DL_ERROR
, "invalid preprocessing directive #%s",
436 cpp_token_as_text (pfile
, dname
));
439 pfile
->directive
= dir
;
440 if (CPP_OPTION (pfile
, traditional
))
441 prepare_directive_trad (pfile
);
444 (*pfile
->directive
->handler
) (pfile
);
446 _cpp_backup_tokens (pfile
, 1);
448 end_directive (pfile
, skip
);
449 if (was_parsing_args
)
451 /* Restore state when within macro args. */
452 pfile
->state
.parsing_args
= 2;
453 pfile
->state
.prevent_expansion
= 1;
458 /* Directive handler wrapper used by the command line option
459 processor. BUF is \n terminated. */
461 run_directive (pfile
, dir_no
, buf
, count
)
467 cpp_push_buffer (pfile
, (const uchar
*) buf
, count
,
468 /* from_stage3 */ true, 1);
469 /* Disgusting hack. */
470 if (dir_no
== T_PRAGMA
)
471 pfile
->buffer
->inc
= pfile
->buffer
->prev
->inc
;
472 start_directive (pfile
);
474 /* This is a short-term fix to prevent a leading '#' being
475 interpreted as a directive. */
476 _cpp_clean_line (pfile
);
478 pfile
->directive
= &dtable
[dir_no
];
479 if (CPP_OPTION (pfile
, traditional
))
480 prepare_directive_trad (pfile
);
481 (void) (*pfile
->directive
->handler
) (pfile
);
482 end_directive (pfile
, 1);
483 if (dir_no
== T_PRAGMA
)
484 pfile
->buffer
->inc
= NULL
;
485 _cpp_pop_buffer (pfile
);
488 /* Checks for validity the macro name in #define, #undef, #ifdef and
489 #ifndef directives. */
490 static cpp_hashnode
*
491 lex_macro_node (pfile
)
494 const cpp_token
*token
= _cpp_lex_token (pfile
);
496 /* The token immediately after #define must be an identifier. That
497 identifier may not be "defined", per C99 6.10.8p4.
498 In C++, it may not be any of the "named operators" either,
499 per C++98 [lex.digraph], [lex.key].
500 Finally, the identifier may not have been poisoned. (In that case
501 the lexer has issued the error message for us.) */
503 if (token
->type
== CPP_NAME
)
505 cpp_hashnode
*node
= token
->val
.node
;
507 if (node
== pfile
->spec_nodes
.n_defined
)
508 cpp_error (pfile
, DL_ERROR
,
509 "\"defined\" cannot be used as a macro name");
510 else if (! (node
->flags
& NODE_POISONED
))
513 else if (token
->flags
& NAMED_OP
)
514 cpp_error (pfile
, DL_ERROR
,
515 "\"%s\" cannot be used as a macro name as it is an operator in C++",
516 NODE_NAME (token
->val
.node
));
517 else if (token
->type
== CPP_EOF
)
518 cpp_error (pfile
, DL_ERROR
, "no macro name given in #%s directive",
519 pfile
->directive
->name
);
521 cpp_error (pfile
, DL_ERROR
, "macro names must be identifiers");
526 /* Process a #define directive. Most work is done in cppmacro.c. */
531 cpp_hashnode
*node
= lex_macro_node (pfile
);
535 /* If we have been requested to expand comments into macros,
536 then re-enable saving of comments. */
537 pfile
->state
.save_comments
=
538 ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
);
540 if (_cpp_create_definition (pfile
, node
))
541 if (pfile
->cb
.define
)
542 (*pfile
->cb
.define
) (pfile
, pfile
->directive_line
, node
);
546 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
551 cpp_hashnode
*node
= lex_macro_node (pfile
);
553 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
554 is not currently defined as a macro name. */
555 if (node
&& node
->type
== NT_MACRO
)
558 (*pfile
->cb
.undef
) (pfile
, pfile
->directive_line
, node
);
560 if (node
->flags
& NODE_WARN
)
561 cpp_error (pfile
, DL_WARNING
, "undefining \"%s\"", NODE_NAME (node
));
563 if (CPP_OPTION (pfile
, warn_unused_macros
))
564 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
566 _cpp_free_definition (node
);
571 /* Helper routine used by parse_include. Reinterpret the current line
572 as an h-char-sequence (< ... >); we are looking at the first token
573 after the <. Returns a malloced filename. */
575 glue_header_name (pfile
)
578 const cpp_token
*token
;
580 size_t len
, total_len
= 0, capacity
= 1024;
582 /* To avoid lexed tokens overwriting our glued name, we can only
583 allocate from the string pool once we've lexed everything. */
584 buffer
= xmalloc (capacity
);
587 token
= get_token_no_padding (pfile
);
589 if (token
->type
== CPP_GREATER
)
591 if (token
->type
== CPP_EOF
)
593 cpp_error (pfile
, DL_ERROR
, "missing terminating > character");
597 len
= cpp_token_len (token
) + 2; /* Leading space, terminating \0. */
598 if (total_len
+ len
> capacity
)
600 capacity
= (capacity
+ len
) * 2;
601 buffer
= xrealloc (buffer
, capacity
);
604 if (token
->flags
& PREV_WHITE
)
605 buffer
[total_len
++] = ' ';
607 total_len
= (cpp_spell_token (pfile
, token
, (uchar
*) &buffer
[total_len
])
611 buffer
[total_len
] = '\0';
615 /* Returns the file name of #include, #include_next, #import and
616 #pragma dependency. The string is malloced and the caller should
617 free it. Returns NULL on error. */
619 parse_include (pfile
, pangle_brackets
)
621 int *pangle_brackets
;
624 const cpp_token
*header
;
626 /* Allow macro expansion. */
627 header
= get_token_no_padding (pfile
);
628 if (header
->type
== CPP_STRING
|| header
->type
== CPP_HEADER_NAME
)
630 fname
= xmalloc (header
->val
.str
.len
- 1);
631 memcpy (fname
, header
->val
.str
.text
+ 1, header
->val
.str
.len
- 2);
632 fname
[header
->val
.str
.len
- 2] = '\0';
633 *pangle_brackets
= header
->type
== CPP_HEADER_NAME
;
635 else if (header
->type
== CPP_LESS
)
637 fname
= glue_header_name (pfile
);
638 *pangle_brackets
= 1;
642 const unsigned char *dir
;
644 if (pfile
->directive
== &dtable
[T_PRAGMA
])
645 dir
= U
"pragma dependency";
647 dir
= pfile
->directive
->name
;
648 cpp_error (pfile
, DL_ERROR
, "#%s expects \"FILENAME\" or <FILENAME>",
658 /* Handle #include, #include_next and #import. */
660 do_include_common (pfile
, type
)
662 enum include_type type
;
667 fname
= parse_include (pfile
, &angle_brackets
);
671 /* Prevent #include recursion. */
672 if (pfile
->line_maps
.depth
>= CPP_STACK_MAX
)
673 cpp_error (pfile
, DL_ERROR
, "#include nested too deeply");
676 /* Get out of macro context, if we are. */
677 skip_rest_of_line (pfile
);
679 if (pfile
->cb
.include
)
680 (*pfile
->cb
.include
) (pfile
, pfile
->directive_line
,
681 pfile
->directive
->name
, fname
, angle_brackets
);
683 _cpp_execute_include (pfile
, fname
, angle_brackets
, type
);
693 do_include_common (pfile
, IT_INCLUDE
);
700 if (CPP_OPTION (pfile
, warn_import
))
702 CPP_OPTION (pfile
, warn_import
) = 0;
703 cpp_error (pfile
, DL_WARNING
,
704 "#import is obsolete, use an #ifndef wrapper in the header file");
707 do_include_common (pfile
, IT_IMPORT
);
711 do_include_next (pfile
)
714 enum include_type type
= IT_INCLUDE_NEXT
;
716 /* If this is the primary source file, warn and use the normal
718 if (! pfile
->buffer
->prev
)
720 cpp_error (pfile
, DL_WARNING
,
721 "#include_next in primary source file");
724 do_include_common (pfile
, type
);
727 /* Subroutine of do_linemarker. Read possible flags after file name.
728 LAST is the last flag seen; 0 if this is the first flag. Return the
729 flag if it is valid, 0 at the end of the directive. Otherwise
732 read_flag (pfile
, last
)
736 const cpp_token
*token
= _cpp_lex_token (pfile
);
738 if (token
->type
== CPP_NUMBER
&& token
->val
.str
.len
== 1)
740 unsigned int flag
= token
->val
.str
.text
[0] - '0';
742 if (flag
> last
&& flag
<= 4
743 && (flag
!= 4 || last
== 3)
744 && (flag
!= 2 || last
== 0))
748 if (token
->type
!= CPP_EOF
)
749 cpp_error (pfile
, DL_ERROR
, "invalid flag \"%s\" in line directive",
750 cpp_token_as_text (pfile
, token
));
754 /* Subroutine of do_line and do_linemarker. Returns a version of STR
755 which has a NUL terminator and all escape sequences converted to
756 their equivalents. Temporary, hopefully. */
758 dequote_string (pfile
, str
, len
)
763 uchar
*result
= _cpp_unaligned_alloc (pfile
, len
+ 1);
765 const uchar
*limit
= str
+ len
;
774 *dst
++ = cpp_parse_escape (pfile
, &str
, limit
, 0);
780 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
781 of length LEN, to binary; store it in NUMP, and return 0 if the
782 number was well-formed, 1 if not. Temporary, hopefully. */
784 strtoul_for_line (str
, len
, nump
)
789 unsigned long reg
= 0;
803 /* Interpret #line command.
804 Note that the filename string (if any) is a true string constant
805 (escapes are interpreted), unlike in #line. */
810 const cpp_token
*token
;
811 const char *new_file
= pfile
->map
->to_file
;
812 unsigned long new_lineno
;
814 /* C99 raised the minimum limit on #line numbers. */
815 unsigned int cap
= CPP_OPTION (pfile
, c99
) ? 2147483647 : 32767;
817 /* #line commands expand macros. */
818 token
= cpp_get_token (pfile
);
819 if (token
->type
!= CPP_NUMBER
820 || strtoul_for_line (token
->val
.str
.text
, token
->val
.str
.len
,
823 cpp_error (pfile
, DL_ERROR
,
824 "\"%s\" after #line is not a positive integer",
825 cpp_token_as_text (pfile
, token
));
829 if (CPP_PEDANTIC (pfile
) && (new_lineno
== 0 || new_lineno
> cap
))
830 cpp_error (pfile
, DL_PEDWARN
, "line number out of range");
832 token
= cpp_get_token (pfile
);
833 if (token
->type
== CPP_STRING
)
835 new_file
= (const char *) dequote_string (pfile
, token
->val
.str
.text
+ 1,
836 token
->val
.str
.len
- 2);
839 else if (token
->type
!= CPP_EOF
)
841 cpp_error (pfile
, DL_ERROR
, "\"%s\" is not a valid filename",
842 cpp_token_as_text (pfile
, token
));
846 skip_rest_of_line (pfile
);
847 _cpp_do_file_change (pfile
, LC_RENAME
, new_file
, new_lineno
,
851 /* Interpret the # 44 "file" [flags] notation, which has slightly
852 different syntax and semantics from #line: Flags are allowed,
853 and we never complain about the line number being too big. */
855 do_linemarker (pfile
)
858 const cpp_token
*token
;
859 const char *new_file
= pfile
->map
->to_file
;
860 unsigned long new_lineno
;
861 unsigned int new_sysp
= pfile
->map
->sysp
;
862 enum lc_reason reason
= LC_RENAME
;
865 /* Back up so we can get the number again. Putting this in
866 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
867 some circumstances, which can segfault. */
868 _cpp_backup_tokens (pfile
, 1);
870 /* #line commands expand macros. */
871 token
= cpp_get_token (pfile
);
872 if (token
->type
!= CPP_NUMBER
873 || strtoul_for_line (token
->val
.str
.text
, token
->val
.str
.len
,
876 cpp_error (pfile
, DL_ERROR
, "\"%s\" after # is not a positive integer",
877 cpp_token_as_text (pfile
, token
));
881 token
= cpp_get_token (pfile
);
882 if (token
->type
== CPP_STRING
)
884 new_file
= (const char *) dequote_string (pfile
, token
->val
.str
.text
+ 1,
885 token
->val
.str
.len
- 2);
887 flag
= read_flag (pfile
, 0);
891 /* Fake an include for cpp_included (). */
892 _cpp_fake_include (pfile
, new_file
);
893 flag
= read_flag (pfile
, flag
);
898 flag
= read_flag (pfile
, flag
);
903 flag
= read_flag (pfile
, flag
);
910 else if (token
->type
!= CPP_EOF
)
912 cpp_error (pfile
, DL_ERROR
, "\"%s\" is not a valid filename",
913 cpp_token_as_text (pfile
, token
));
917 skip_rest_of_line (pfile
);
918 _cpp_do_file_change (pfile
, reason
, new_file
, new_lineno
, new_sysp
);
921 /* Arrange the file_change callback. pfile->line has changed to
922 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
923 header, 2 for a system header that needs to be extern "C" protected,
924 and zero otherwise. */
926 _cpp_do_file_change (pfile
, reason
, to_file
, file_line
, sysp
)
928 enum lc_reason reason
;
930 unsigned int file_line
;
933 pfile
->map
= add_line_map (&pfile
->line_maps
, reason
, sysp
,
934 pfile
->line
, to_file
, file_line
);
936 if (pfile
->cb
.file_change
)
937 (*pfile
->cb
.file_change
) (pfile
, pfile
->map
);
940 /* Report a warning or error detected by the program we are
941 processing. Use the directive's tokens in the error message. */
943 do_diagnostic (pfile
, code
, print_dir
)
948 if (_cpp_begin_message (pfile
, code
,
949 pfile
->cur_token
[-1].line
,
950 pfile
->cur_token
[-1].col
))
953 fprintf (stderr
, "#%s ", pfile
->directive
->name
);
954 pfile
->state
.prevent_expansion
++;
955 cpp_output_line (pfile
, stderr
);
956 pfile
->state
.prevent_expansion
--;
964 do_diagnostic (pfile
, DL_ERROR
, 1);
971 /* We want #warning diagnostics to be emitted in system headers too. */
972 do_diagnostic (pfile
, DL_WARNING_SYSHDR
, 1);
975 /* Report program identification. */
980 const cpp_token
*str
= cpp_get_token (pfile
);
982 if (str
->type
!= CPP_STRING
)
983 cpp_error (pfile
, DL_ERROR
, "invalid #ident directive");
984 else if (pfile
->cb
.ident
)
985 (*pfile
->cb
.ident
) (pfile
, pfile
->directive_line
, &str
->val
.str
);
990 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
991 matching entry, or NULL if none is found. The returned entry could
992 be the start of a namespace chain, or a pragma. */
993 static struct pragma_entry
*
994 lookup_pragma_entry (chain
, pragma
)
995 struct pragma_entry
*chain
;
996 const cpp_hashnode
*pragma
;
998 while (chain
&& chain
->pragma
!= pragma
)
1004 /* Create and insert a pragma entry for NAME at the beginning of a
1005 singly-linked CHAIN. If handler is NULL, it is a namespace,
1006 otherwise it is a pragma and its handler. */
1007 static struct pragma_entry
*
1008 insert_pragma_entry (pfile
, chain
, pragma
, handler
)
1010 struct pragma_entry
**chain
;
1011 const cpp_hashnode
*pragma
;
1014 struct pragma_entry
*new;
1016 new = (struct pragma_entry
*)
1017 _cpp_aligned_alloc (pfile
, sizeof (struct pragma_entry
));
1018 new->pragma
= pragma
;
1022 new->u
.handler
= handler
;
1027 new->u
.space
= NULL
;
1035 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1036 goes in the global namespace. HANDLER is the handler it will call,
1037 which must be non-NULL. */
1039 cpp_register_pragma (pfile
, space
, name
, handler
)
1045 struct pragma_entry
**chain
= &pfile
->pragmas
;
1046 struct pragma_entry
*entry
;
1047 const cpp_hashnode
*node
;
1054 node
= cpp_lookup (pfile
, U space
, strlen (space
));
1055 entry
= lookup_pragma_entry (*chain
, node
);
1057 entry
= insert_pragma_entry (pfile
, chain
, node
, NULL
);
1058 else if (!entry
->is_nspace
)
1060 chain
= &entry
->u
.space
;
1063 /* Check for duplicates. */
1064 node
= cpp_lookup (pfile
, U name
, strlen (name
));
1065 entry
= lookup_pragma_entry (*chain
, node
);
1068 if (entry
->is_nspace
)
1070 cpp_error (pfile
, DL_ICE
,
1071 "registering \"%s\" as both a pragma and a pragma namespace",
1074 cpp_error (pfile
, DL_ICE
, "#pragma %s %s is already registered",
1077 cpp_error (pfile
, DL_ICE
, "#pragma %s is already registered", name
);
1080 insert_pragma_entry (pfile
, chain
, node
, handler
);
1083 /* Register the pragmas the preprocessor itself handles. */
1085 _cpp_init_internal_pragmas (pfile
)
1088 /* Pragmas in the global namespace. */
1089 cpp_register_pragma (pfile
, 0, "once", do_pragma_once
);
1091 /* New GCC-specific pragmas should be put in the GCC namespace. */
1092 cpp_register_pragma (pfile
, "GCC", "poison", do_pragma_poison
);
1093 cpp_register_pragma (pfile
, "GCC", "system_header", do_pragma_system_header
);
1094 cpp_register_pragma (pfile
, "GCC", "dependency", do_pragma_dependency
);
1097 /* Return the number of registered pragmas in PE. */
1100 count_registered_pragmas (pe
)
1101 struct pragma_entry
*pe
;
1104 for (; pe
!= NULL
; pe
= pe
->next
)
1107 ct
+= count_registered_pragmas (pe
->u
.space
);
1113 /* Save into SD the names of the registered pragmas referenced by PE,
1114 and return a pointer to the next free space in SD. */
1117 save_registered_pragmas (pe
, sd
)
1118 struct pragma_entry
*pe
;
1121 for (; pe
!= NULL
; pe
= pe
->next
)
1124 sd
= save_registered_pragmas (pe
->u
.space
, sd
);
1125 *sd
++ = xmemdup (HT_STR (&pe
->pragma
->ident
),
1126 HT_LEN (&pe
->pragma
->ident
),
1127 HT_LEN (&pe
->pragma
->ident
) + 1);
1132 /* Return a newly-allocated array which saves the names of the
1133 registered pragmas. */
1136 _cpp_save_pragma_names (pfile
)
1139 int ct
= count_registered_pragmas (pfile
->pragmas
);
1140 char **result
= xnewvec (char *, ct
);
1141 (void) save_registered_pragmas (pfile
->pragmas
, result
);
1145 /* Restore from SD the names of the registered pragmas referenced by PE,
1146 and return a pointer to the next unused name in SD. */
1149 restore_registered_pragmas (pfile
, pe
, sd
)
1151 struct pragma_entry
*pe
;
1154 for (; pe
!= NULL
; pe
= pe
->next
)
1157 sd
= restore_registered_pragmas (pfile
, pe
->u
.space
, sd
);
1158 pe
->pragma
= cpp_lookup (pfile
, U
*sd
, strlen (*sd
));
1165 /* Restore the names of the registered pragmas from SAVED. */
1168 _cpp_restore_pragma_names (pfile
, saved
)
1172 (void) restore_registered_pragmas (pfile
, pfile
->pragmas
, saved
);
1176 /* Pragmata handling. We handle some, and pass the rest on to the
1177 front end. C99 defines three pragmas and says that no macro
1178 expansion is to be performed on them; whether or not macro
1179 expansion happens for other pragmas is implementation defined.
1180 This implementation never macro-expands the text after #pragma. */
1185 const struct pragma_entry
*p
= NULL
;
1186 const cpp_token
*token
;
1187 unsigned int count
= 1;
1189 pfile
->state
.prevent_expansion
++;
1191 token
= cpp_get_token (pfile
);
1192 if (token
->type
== CPP_NAME
)
1194 p
= lookup_pragma_entry (pfile
->pragmas
, token
->val
.node
);
1195 if (p
&& p
->is_nspace
)
1198 token
= cpp_get_token (pfile
);
1199 if (token
->type
== CPP_NAME
)
1200 p
= lookup_pragma_entry (p
->u
.space
, token
->val
.node
);
1206 /* FIXME. This is an awful kludge to get the front ends to update
1207 their notion of line number for diagnostic purposes. The line
1208 number should be passed to the handler and they should do it
1209 themselves. Stand-alone CPP must ignore us, otherwise it will
1210 prefix the directive with spaces, hence the 1. Ugh. */
1211 if (pfile
->cb
.line_change
)
1212 (*pfile
->cb
.line_change
)(pfile
, token
, 1);
1215 (*p
->u
.handler
) (pfile
);
1216 else if (pfile
->cb
.def_pragma
)
1218 _cpp_backup_tokens (pfile
, count
);
1219 (*pfile
->cb
.def_pragma
) (pfile
, pfile
->directive_line
);
1222 pfile
->state
.prevent_expansion
--;
1225 /* Handle #pragma once. */
1227 do_pragma_once (pfile
)
1230 if (CPP_OPTION (pfile
, warn_deprecated
))
1231 cpp_error (pfile
, DL_WARNING
, "#pragma once is obsolete");
1233 if (pfile
->buffer
->prev
== NULL
)
1234 cpp_error (pfile
, DL_WARNING
, "#pragma once in main file");
1236 _cpp_never_reread (pfile
->buffer
->inc
);
1241 /* Handle #pragma GCC poison, to poison one or more identifiers so
1242 that the lexer produces a hard error for each subsequent usage. */
1244 do_pragma_poison (pfile
)
1247 const cpp_token
*tok
;
1250 pfile
->state
.poisoned_ok
= 1;
1253 tok
= _cpp_lex_token (pfile
);
1254 if (tok
->type
== CPP_EOF
)
1256 if (tok
->type
!= CPP_NAME
)
1258 cpp_error (pfile
, DL_ERROR
, "invalid #pragma GCC poison directive");
1263 if (hp
->flags
& NODE_POISONED
)
1266 if (hp
->type
== NT_MACRO
)
1267 cpp_error (pfile
, DL_WARNING
, "poisoning existing macro \"%s\"",
1269 _cpp_free_definition (hp
);
1270 hp
->flags
|= NODE_POISONED
| NODE_DIAGNOSTIC
;
1272 pfile
->state
.poisoned_ok
= 0;
1275 /* Mark the current header as a system header. This will suppress
1276 some categories of warnings (notably those from -pedantic). It is
1277 intended for use in system libraries that cannot be implemented in
1278 conforming C, but cannot be certain that their headers appear in a
1279 system include directory. To prevent abuse, it is rejected in the
1280 primary source file. */
1282 do_pragma_system_header (pfile
)
1285 cpp_buffer
*buffer
= pfile
->buffer
;
1287 if (buffer
->prev
== 0)
1288 cpp_error (pfile
, DL_WARNING
,
1289 "#pragma system_header ignored outside include file");
1293 skip_rest_of_line (pfile
);
1294 cpp_make_system_header (pfile
, 1, 0);
1298 /* Check the modified date of the current include file against a specified
1299 file. Issue a diagnostic, if the specified file is newer. We use this to
1300 determine if a fixed header should be refixed. */
1302 do_pragma_dependency (pfile
)
1306 int angle_brackets
, ordering
;
1308 fname
= parse_include (pfile
, &angle_brackets
);
1312 ordering
= _cpp_compare_file_date (pfile
, fname
, angle_brackets
);
1314 cpp_error (pfile
, DL_WARNING
, "cannot find source file %s", fname
);
1315 else if (ordering
> 0)
1317 cpp_error (pfile
, DL_WARNING
, "current file is older than %s", fname
);
1318 if (cpp_get_token (pfile
)->type
!= CPP_EOF
)
1320 _cpp_backup_tokens (pfile
, 1);
1321 do_diagnostic (pfile
, DL_WARNING
, 0);
1328 /* Get a token but skip padding. */
1329 static const cpp_token
*
1330 get_token_no_padding (pfile
)
1335 const cpp_token
*result
= cpp_get_token (pfile
);
1336 if (result
->type
!= CPP_PADDING
)
1341 /* Check syntax is "(string-literal)". Returns the string on success,
1342 or NULL on failure. */
1343 static const cpp_token
*
1344 get__Pragma_string (pfile
)
1347 const cpp_token
*string
;
1349 if (get_token_no_padding (pfile
)->type
!= CPP_OPEN_PAREN
)
1352 string
= get_token_no_padding (pfile
);
1353 if (string
->type
!= CPP_STRING
&& string
->type
!= CPP_WSTRING
)
1356 if (get_token_no_padding (pfile
)->type
!= CPP_CLOSE_PAREN
)
1362 /* Destringize IN into a temporary buffer, by removing the first \ of
1363 \" and \\ sequences, and process the result as a #pragma directive. */
1365 destringize_and_run (pfile
, in
)
1367 const cpp_string
*in
;
1369 const unsigned char *src
, *limit
;
1370 char *dest
, *result
;
1372 dest
= result
= alloca (in
->len
- 1);
1373 src
= in
->text
+ 1 + (in
->text
[0] == 'L');
1374 limit
= in
->text
+ in
->len
- 1;
1377 /* We know there is a character following the backslash. */
1378 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1384 /* Ugh; an awful kludge. We are really not set up to be lexing
1385 tokens when in the middle of a macro expansion. Use a new
1386 context to force cpp_get_token to lex, and so skip_rest_of_line
1387 doesn't go beyond the end of the text. Also, remember the
1388 current lexing position so we can return to it later.
1390 Something like line-at-a-time lexing should remove the need for
1393 cpp_context
*saved_context
= pfile
->context
;
1394 cpp_token
*saved_cur_token
= pfile
->cur_token
;
1395 tokenrun
*saved_cur_run
= pfile
->cur_run
;
1397 pfile
->context
= xnew (cpp_context
);
1398 pfile
->context
->macro
= 0;
1399 pfile
->context
->prev
= 0;
1400 run_directive (pfile
, T_PRAGMA
, result
, dest
- result
);
1401 free (pfile
->context
);
1402 pfile
->context
= saved_context
;
1403 pfile
->cur_token
= saved_cur_token
;
1404 pfile
->cur_run
= saved_cur_run
;
1408 /* See above comment. For the moment, we'd like
1410 token1 _Pragma ("foo") token2
1420 Getting the line markers is a little tricky. */
1421 if (pfile
->cb
.line_change
)
1422 (*pfile
->cb
.line_change
) (pfile
, pfile
->cur_token
, false);
1425 /* Handle the _Pragma operator. */
1427 _cpp_do__Pragma (pfile
)
1430 const cpp_token
*string
= get__Pragma_string (pfile
);
1433 destringize_and_run (pfile
, &string
->val
.str
);
1435 cpp_error (pfile
, DL_ERROR
,
1436 "_Pragma takes a parenthesized string literal");
1439 /* Just ignore #sccs on all systems. */
1442 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
1446 /* Handle #ifdef. */
1453 if (! pfile
->state
.skipping
)
1455 const cpp_hashnode
*node
= lex_macro_node (pfile
);
1459 skip
= node
->type
!= NT_MACRO
;
1460 _cpp_mark_macro_used (node
);
1465 push_conditional (pfile
, skip
, T_IFDEF
, 0);
1468 /* Handle #ifndef. */
1474 const cpp_hashnode
*node
= 0;
1476 if (! pfile
->state
.skipping
)
1478 node
= lex_macro_node (pfile
);
1482 skip
= node
->type
== NT_MACRO
;
1483 _cpp_mark_macro_used (node
);
1488 push_conditional (pfile
, skip
, T_IFNDEF
, node
);
1491 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1492 pfile->mi_ind_cmacro so we can handle multiple-include
1493 optimisations. If macro expansion occurs in the expression, we
1494 cannot treat it as a controlling conditional, since the expansion
1495 could change in the future. That is handled by cpp_get_token. */
1502 if (! pfile
->state
.skipping
)
1503 skip
= _cpp_parse_expr (pfile
) == false;
1505 push_conditional (pfile
, skip
, T_IF
, pfile
->mi_ind_cmacro
);
1508 /* Flip skipping state if appropriate and continue without changing
1509 if_stack; this is so that the error message for missing #endif's
1510 etc. will point to the original #if. */
1515 cpp_buffer
*buffer
= pfile
->buffer
;
1516 struct if_stack
*ifs
= buffer
->if_stack
;
1519 cpp_error (pfile
, DL_ERROR
, "#else without #if");
1522 if (ifs
->type
== T_ELSE
)
1524 cpp_error (pfile
, DL_ERROR
, "#else after #else");
1525 cpp_error_with_line (pfile
, DL_ERROR
, ifs
->line
, 0,
1526 "the conditional began here");
1530 /* Skip any future (erroneous) #elses or #elifs. */
1531 pfile
->state
.skipping
= ifs
->skip_elses
;
1532 ifs
->skip_elses
= true;
1534 /* Invalidate any controlling macro. */
1537 /* Only check EOL if was not originally skipping. */
1538 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1543 /* Handle a #elif directive by not changing if_stack either. See the
1544 comment above do_else. */
1549 cpp_buffer
*buffer
= pfile
->buffer
;
1550 struct if_stack
*ifs
= buffer
->if_stack
;
1553 cpp_error (pfile
, DL_ERROR
, "#elif without #if");
1556 if (ifs
->type
== T_ELSE
)
1558 cpp_error (pfile
, DL_ERROR
, "#elif after #else");
1559 cpp_error_with_line (pfile
, DL_ERROR
, ifs
->line
, 0,
1560 "the conditional began here");
1564 /* Only evaluate this if we aren't skipping elses. During
1565 evaluation, set skipping to false to get lexer warnings. */
1566 if (ifs
->skip_elses
)
1567 pfile
->state
.skipping
= 1;
1570 pfile
->state
.skipping
= 0;
1571 pfile
->state
.skipping
= ! _cpp_parse_expr (pfile
);
1572 ifs
->skip_elses
= ! pfile
->state
.skipping
;
1575 /* Invalidate any controlling macro. */
1580 /* #endif pops the if stack and resets pfile->state.skipping. */
1585 cpp_buffer
*buffer
= pfile
->buffer
;
1586 struct if_stack
*ifs
= buffer
->if_stack
;
1589 cpp_error (pfile
, DL_ERROR
, "#endif without #if");
1592 /* Only check EOL if was not originally skipping. */
1593 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1596 /* If potential control macro, we go back outside again. */
1597 if (ifs
->next
== 0 && ifs
->mi_cmacro
)
1599 pfile
->mi_valid
= true;
1600 pfile
->mi_cmacro
= ifs
->mi_cmacro
;
1603 buffer
->if_stack
= ifs
->next
;
1604 pfile
->state
.skipping
= ifs
->was_skipping
;
1605 obstack_free (&pfile
->buffer_ob
, ifs
);
1609 /* Push an if_stack entry for a preprocessor conditional, and set
1610 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1611 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1612 we need to check here that we are at the top of the file. */
1614 push_conditional (pfile
, skip
, type
, cmacro
)
1618 const cpp_hashnode
*cmacro
;
1620 struct if_stack
*ifs
;
1621 cpp_buffer
*buffer
= pfile
->buffer
;
1623 ifs
= xobnew (&pfile
->buffer_ob
, struct if_stack
);
1624 ifs
->line
= pfile
->directive_line
;
1625 ifs
->next
= buffer
->if_stack
;
1626 ifs
->skip_elses
= pfile
->state
.skipping
|| !skip
;
1627 ifs
->was_skipping
= pfile
->state
.skipping
;
1629 /* This condition is effectively a test for top-of-file. */
1630 if (pfile
->mi_valid
&& pfile
->mi_cmacro
== 0)
1631 ifs
->mi_cmacro
= cmacro
;
1635 pfile
->state
.skipping
= skip
;
1636 buffer
->if_stack
= ifs
;
1639 /* Read the tokens of the answer into the macro pool, in a directive
1640 of type TYPE. Only commit the memory if we intend it as permanent
1641 storage, i.e. the #assert case. Returns 0 on success, and sets
1642 ANSWERP to point to the answer. */
1644 parse_answer (pfile
, answerp
, type
)
1646 struct answer
**answerp
;
1649 const cpp_token
*paren
;
1650 struct answer
*answer
;
1651 unsigned int acount
;
1653 /* In a conditional, it is legal to not have an open paren. We
1654 should save the following token in this case. */
1655 paren
= cpp_get_token (pfile
);
1657 /* If not a paren, see if we're OK. */
1658 if (paren
->type
!= CPP_OPEN_PAREN
)
1660 /* In a conditional no answer is a test for any answer. It
1661 could be followed by any token. */
1664 _cpp_backup_tokens (pfile
, 1);
1668 /* #unassert with no answer is valid - it removes all answers. */
1669 if (type
== T_UNASSERT
&& paren
->type
== CPP_EOF
)
1672 cpp_error (pfile
, DL_ERROR
, "missing '(' after predicate");
1676 for (acount
= 0;; acount
++)
1679 const cpp_token
*token
= cpp_get_token (pfile
);
1682 if (token
->type
== CPP_CLOSE_PAREN
)
1685 if (token
->type
== CPP_EOF
)
1687 cpp_error (pfile
, DL_ERROR
, "missing ')' to complete answer");
1691 /* struct answer includes the space for one token. */
1692 room_needed
= (sizeof (struct answer
) + acount
* sizeof (cpp_token
));
1694 if (BUFF_ROOM (pfile
->a_buff
) < room_needed
)
1695 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (struct answer
));
1697 dest
= &((struct answer
*) BUFF_FRONT (pfile
->a_buff
))->first
[acount
];
1700 /* Drop whitespace at start, for answer equivalence purposes. */
1702 dest
->flags
&= ~PREV_WHITE
;
1707 cpp_error (pfile
, DL_ERROR
, "predicate's answer is empty");
1711 answer
= (struct answer
*) BUFF_FRONT (pfile
->a_buff
);
1712 answer
->count
= acount
;
1713 answer
->next
= NULL
;
1719 /* Parses an assertion directive of type TYPE, returning a pointer to
1720 the hash node of the predicate, or 0 on error. If an answer was
1721 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
1722 static cpp_hashnode
*
1723 parse_assertion (pfile
, answerp
, type
)
1725 struct answer
**answerp
;
1728 cpp_hashnode
*result
= 0;
1729 const cpp_token
*predicate
;
1731 /* We don't expand predicates or answers. */
1732 pfile
->state
.prevent_expansion
++;
1735 predicate
= cpp_get_token (pfile
);
1736 if (predicate
->type
== CPP_EOF
)
1737 cpp_error (pfile
, DL_ERROR
, "assertion without predicate");
1738 else if (predicate
->type
!= CPP_NAME
)
1739 cpp_error (pfile
, DL_ERROR
, "predicate must be an identifier");
1740 else if (parse_answer (pfile
, answerp
, type
) == 0)
1742 unsigned int len
= NODE_LEN (predicate
->val
.node
);
1743 unsigned char *sym
= alloca (len
+ 1);
1745 /* Prefix '#' to get it out of macro namespace. */
1747 memcpy (sym
+ 1, NODE_NAME (predicate
->val
.node
), len
);
1748 result
= cpp_lookup (pfile
, sym
, len
+ 1);
1751 pfile
->state
.prevent_expansion
--;
1755 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1756 or a pointer to NULL if the answer is not in the chain. */
1757 static struct answer
**
1758 find_answer (node
, candidate
)
1760 const struct answer
*candidate
;
1763 struct answer
**result
;
1765 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->next
)
1767 struct answer
*answer
= *result
;
1769 if (answer
->count
== candidate
->count
)
1771 for (i
= 0; i
< answer
->count
; i
++)
1772 if (! _cpp_equiv_tokens (&answer
->first
[i
], &candidate
->first
[i
]))
1775 if (i
== answer
->count
)
1783 /* Test an assertion within a preprocessor conditional. Returns
1784 nonzero on failure, zero on success. On success, the result of
1785 the test is written into VALUE, otherwise the value 0. */
1787 _cpp_test_assertion (pfile
, value
)
1789 unsigned int *value
;
1791 struct answer
*answer
;
1794 node
= parse_assertion (pfile
, &answer
, T_IF
);
1796 /* For recovery, an erroneous assertion expression is handled as a
1797 failing assertion. */
1801 *value
= (node
->type
== NT_ASSERTION
&&
1802 (answer
== 0 || *find_answer (node
, answer
) != 0));
1803 else if (pfile
->cur_token
[-1].type
== CPP_EOF
)
1804 _cpp_backup_tokens (pfile
, 1);
1806 /* We don't commit the memory for the answer - it's temporary only. */
1810 /* Handle #assert. */
1815 struct answer
*new_answer
;
1818 node
= parse_assertion (pfile
, &new_answer
, T_ASSERT
);
1821 /* Place the new answer in the answer list. First check there
1822 is not a duplicate. */
1823 new_answer
->next
= 0;
1824 if (node
->type
== NT_ASSERTION
)
1826 if (*find_answer (node
, new_answer
))
1828 cpp_error (pfile
, DL_WARNING
, "\"%s\" re-asserted",
1829 NODE_NAME (node
) + 1);
1832 new_answer
->next
= node
->value
.answers
;
1835 node
->type
= NT_ASSERTION
;
1836 node
->value
.answers
= new_answer
;
1837 BUFF_FRONT (pfile
->a_buff
) += (sizeof (struct answer
)
1838 + (new_answer
->count
- 1)
1839 * sizeof (cpp_token
));
1844 /* Handle #unassert. */
1850 struct answer
*answer
;
1852 node
= parse_assertion (pfile
, &answer
, T_UNASSERT
);
1853 /* It isn't an error to #unassert something that isn't asserted. */
1854 if (node
&& node
->type
== NT_ASSERTION
)
1858 struct answer
**p
= find_answer (node
, answer
), *temp
;
1860 /* Remove the answer from the list. */
1865 /* Did we free the last answer? */
1866 if (node
->value
.answers
== 0)
1867 node
->type
= NT_VOID
;
1872 _cpp_free_definition (node
);
1875 /* We don't commit the memory for the answer - it's temporary only. */
1878 /* These are for -D, -U, -A. */
1880 /* Process the string STR as if it appeared as the body of a #define.
1881 If STR is just an identifier, define it with value 1.
1882 If STR has anything after the identifier, then it should
1883 be identifier=definition. */
1885 cpp_define (pfile
, str
)
1892 /* Copy the entire option so we can modify it.
1893 Change the first "=" in the string to a space. If there is none,
1894 tack " 1" on the end. */
1896 count
= strlen (str
);
1897 buf
= (char *) alloca (count
+ 3);
1898 memcpy (buf
, str
, count
);
1900 p
= strchr (str
, '=');
1910 run_directive (pfile
, T_DEFINE
, buf
, count
);
1913 /* Slight variant of the above for use by initialize_builtins. */
1915 _cpp_define_builtin (pfile
, str
)
1919 size_t len
= strlen (str
);
1920 char *buf
= alloca (len
+ 1);
1921 memcpy (buf
, str
, len
);
1923 run_directive (pfile
, T_DEFINE
, buf
, len
);
1926 /* Process MACRO as if it appeared as the body of an #undef. */
1928 cpp_undef (pfile
, macro
)
1932 size_t len
= strlen (macro
);
1933 char *buf
= alloca (len
+ 1);
1934 memcpy (buf
, macro
, len
);
1936 run_directive (pfile
, T_UNDEF
, buf
, len
);
1939 /* Process the string STR as if it appeared as the body of a #assert. */
1941 cpp_assert (pfile
, str
)
1945 handle_assertion (pfile
, str
, T_ASSERT
);
1948 /* Process STR as if it appeared as the body of an #unassert. */
1950 cpp_unassert (pfile
, str
)
1954 handle_assertion (pfile
, str
, T_UNASSERT
);
1957 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1959 handle_assertion (pfile
, str
, type
)
1964 size_t count
= strlen (str
);
1965 const char *p
= strchr (str
, '=');
1967 /* Copy the entire option so we can modify it. Change the first
1968 "=" in the string to a '(', and tack a ')' on the end. */
1969 char *buf
= (char *) alloca (count
+ 2);
1971 memcpy (buf
, str
, count
);
1980 run_directive (pfile
, type
, str
, count
);
1983 /* The number of errors for a given reader. */
1988 return pfile
->errors
;
1991 /* The options structure. */
1993 cpp_get_options (pfile
)
1996 return &pfile
->opts
;
1999 /* The callbacks structure. */
2001 cpp_get_callbacks (pfile
)
2007 /* The line map set. */
2008 const struct line_maps
*
2009 cpp_get_line_maps (pfile
)
2012 return &pfile
->line_maps
;
2015 /* Copy the given callbacks structure to our own. */
2017 cpp_set_callbacks (pfile
, cb
)
2024 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2025 doesn't fail. It does not generate a file change call back; that
2026 is the responsibility of the caller. */
2028 cpp_push_buffer (pfile
, buffer
, len
, from_stage3
, return_at_eof
)
2030 const uchar
*buffer
;
2035 cpp_buffer
*new = xobnew (&pfile
->buffer_ob
, cpp_buffer
);
2037 /* Clears, amongst other things, if_stack and mi_cmacro. */
2038 memset (new, 0, sizeof (cpp_buffer
));
2040 new->next_line
= new->buf
= buffer
;
2041 new->rlimit
= buffer
+ len
;
2042 new->from_stage3
= from_stage3
;
2043 new->prev
= pfile
->buffer
;
2044 new->return_at_eof
= return_at_eof
;
2045 new->need_line
= true;
2047 pfile
->buffer
= new;
2051 /* Pops a single buffer, with a file change call-back if appropriate.
2052 Then pushes the next -include file, if any remain. */
2054 _cpp_pop_buffer (pfile
)
2057 cpp_buffer
*buffer
= pfile
->buffer
;
2058 struct include_file
*inc
= buffer
->inc
;
2059 struct if_stack
*ifs
;
2061 /* Walk back up the conditional stack till we reach its level at
2062 entry to this file, issuing error messages. */
2063 for (ifs
= buffer
->if_stack
; ifs
; ifs
= ifs
->next
)
2064 cpp_error_with_line (pfile
, DL_ERROR
, ifs
->line
, 0,
2065 "unterminated #%s", dtable
[ifs
->type
].name
);
2067 /* In case of a missing #endif. */
2068 pfile
->state
.skipping
= 0;
2070 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2071 pfile
->buffer
= buffer
->prev
;
2073 free (buffer
->notes
);
2075 /* Free the buffer object now; we may want to push a new buffer
2076 in _cpp_push_next_include_file. */
2077 obstack_free (&pfile
->buffer_ob
, buffer
);
2081 _cpp_pop_file_buffer (pfile
, inc
);
2083 /* Don't generate a callback for popping the main file. */
2085 _cpp_do_file_change (pfile
, LC_LEAVE
, 0, 0, 0);
2089 /* Enter all recognized directives in the hash table. */
2091 _cpp_init_directives (pfile
)
2097 for (i
= 0; i
< (unsigned int) N_DIRECTIVES
; i
++)
2099 node
= cpp_lookup (pfile
, dtable
[i
].name
, dtable
[i
].length
);
2100 node
->is_directive
= 1;
2101 node
->directive_index
= i
;