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 const cpp_token
*glue_header_name
PARAMS ((cpp_reader
*));
108 static const cpp_token
*parse_include
PARAMS ((cpp_reader
*));
109 static void push_conditional
PARAMS ((cpp_reader
*, int, int,
110 const cpp_hashnode
*));
111 static unsigned int read_flag
PARAMS ((cpp_reader
*, unsigned int));
112 static uchar
*dequote_string
PARAMS ((cpp_reader
*, const uchar
*,
114 static int strtoul_for_line
PARAMS ((const uchar
*, unsigned int,
116 static void do_diagnostic
PARAMS ((cpp_reader
*, int, int));
117 static cpp_hashnode
*lex_macro_node
PARAMS ((cpp_reader
*));
118 static void do_include_common
PARAMS ((cpp_reader
*, enum include_type
));
119 static struct pragma_entry
*lookup_pragma_entry
120 PARAMS ((struct pragma_entry
*, const cpp_hashnode
*pragma
));
121 static struct pragma_entry
*insert_pragma_entry
122 PARAMS ((cpp_reader
*, struct pragma_entry
**, const cpp_hashnode
*,
124 static int count_registered_pragmas
PARAMS ((struct pragma_entry
*));
125 static char ** save_registered_pragmas
126 PARAMS ((struct pragma_entry
*, char **));
127 static char ** restore_registered_pragmas
128 PARAMS ((cpp_reader
*, struct pragma_entry
*, char **));
129 static void do_pragma_once
PARAMS ((cpp_reader
*));
130 static void do_pragma_poison
PARAMS ((cpp_reader
*));
131 static void do_pragma_system_header
PARAMS ((cpp_reader
*));
132 static void do_pragma_dependency
PARAMS ((cpp_reader
*));
133 static void do_linemarker
PARAMS ((cpp_reader
*));
134 static const cpp_token
*get_token_no_padding
PARAMS ((cpp_reader
*));
135 static const cpp_token
*get__Pragma_string
PARAMS ((cpp_reader
*));
136 static void destringize_and_run
PARAMS ((cpp_reader
*, const cpp_string
*));
137 static int parse_answer
PARAMS ((cpp_reader
*, struct answer
**, int));
138 static cpp_hashnode
*parse_assertion
PARAMS ((cpp_reader
*, struct answer
**,
140 static struct answer
** find_answer
PARAMS ((cpp_hashnode
*,
141 const struct answer
*));
142 static void handle_assertion
PARAMS ((cpp_reader
*, const char *, int));
144 /* This is the table of directive handlers. It is ordered by
145 frequency of occurrence; the numbers at the end are directive
146 counts from all the source code I have lying around (egcs and libc
147 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
148 pcmcia-cs-3.0.9). This is no longer important as directive lookup
149 is now O(1). All extensions other than #warning and #include_next
150 are deprecated. The name is where the extension appears to have
153 #define DIRECTIVE_TABLE \
154 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
155 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
156 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
157 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
158 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
159 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
160 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
161 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
162 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
163 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
164 D(error, T_ERROR, STDC89, 0) /* 475 */ \
165 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
166 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
167 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
168 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
169 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
170 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
171 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
172 D(sccs, T_SCCS, EXTENSION, 0) /* 0 SVR4? */
174 /* Use the table to generate a series of prototypes, an enum for the
175 directive names, and an array of directive handlers. */
177 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
178 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
182 #define D(n, tag, o, f) tag,
190 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
191 #define D(name, t, origin, flags) \
192 { CONCAT2(do_,name), (const uchar *) STRINGX(name), \
193 sizeof STRINGX(name) - 1, origin, flags },
194 static const directive dtable
[] =
199 #undef DIRECTIVE_TABLE
201 /* Wrapper struct directive for linemarkers.
202 The origin is more or less true - the original K+R cpp
203 did use this notation in its preprocessed output. */
204 static const directive linemarker_dir
=
206 do_linemarker
, U
"#", 1, KANDR
, IN_I
209 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
211 /* Skip any remaining tokens in a directive. */
213 skip_rest_of_line (pfile
)
216 /* Discard all stacked contexts. */
217 while (pfile
->context
->prev
)
218 _cpp_pop_context (pfile
);
220 /* Sweep up all tokens remaining on the line. */
222 while (_cpp_lex_token (pfile
)->type
!= CPP_EOF
)
226 /* Ensure there are no stray tokens at the end of a directive. */
231 if (! SEEN_EOL () && _cpp_lex_token (pfile
)->type
!= CPP_EOF
)
232 cpp_error (pfile
, DL_PEDWARN
, "extra tokens at end of #%s directive",
233 pfile
->directive
->name
);
236 /* Called when entering a directive, _Pragma or command-line directive. */
238 start_directive (pfile
)
241 /* Setup in-directive state. */
242 pfile
->state
.in_directive
= 1;
243 pfile
->state
.save_comments
= 0;
245 /* Some handlers need the position of the # for diagnostics. */
246 pfile
->directive_line
= pfile
->line
;
249 /* Called when leaving a directive, _Pragma or command-line directive. */
251 end_directive (pfile
, skip_line
)
255 if (CPP_OPTION (pfile
, traditional
))
257 /* Revert change of prepare_directive_trad. */
258 pfile
->state
.prevent_expansion
--;
260 if (pfile
->directive
!= &dtable
[T_DEFINE
])
261 _cpp_remove_overlay (pfile
);
263 /* We don't skip for an assembler #. */
266 skip_rest_of_line (pfile
);
267 if (!pfile
->keep_tokens
)
269 pfile
->cur_run
= &pfile
->base_run
;
270 pfile
->cur_token
= pfile
->base_run
.base
;
275 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
276 pfile
->state
.in_directive
= 0;
277 pfile
->state
.in_expression
= 0;
278 pfile
->state
.angled_headers
= 0;
279 pfile
->directive
= 0;
282 /* Prepare to handle the directive in pfile->directive. */
284 prepare_directive_trad (pfile
)
287 if (pfile
->directive
!= &dtable
[T_DEFINE
])
289 bool no_expand
= (pfile
->directive
290 && ! (pfile
->directive
->flags
& EXPAND
));
291 bool was_skipping
= pfile
->state
.skipping
;
293 pfile
->state
.skipping
= false;
294 pfile
->state
.in_expression
= (pfile
->directive
== &dtable
[T_IF
]
295 || pfile
->directive
== &dtable
[T_ELIF
]);
297 pfile
->state
.prevent_expansion
++;
298 _cpp_read_logical_line_trad (pfile
);
300 pfile
->state
.prevent_expansion
--;
301 pfile
->state
.skipping
= was_skipping
;
302 _cpp_overlay_buffer (pfile
, pfile
->out
.base
,
303 pfile
->out
.cur
- pfile
->out
.base
);
306 /* Stop ISO C from expanding anything. */
307 pfile
->state
.prevent_expansion
++;
310 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
311 the '#' was indented. */
313 directive_diagnostics (pfile
, dir
, indented
)
315 const directive
*dir
;
318 /* Issue -pedantic warnings for extensions. */
319 if (CPP_PEDANTIC (pfile
)
320 && ! pfile
->state
.skipping
321 && dir
->origin
== EXTENSION
)
322 cpp_error (pfile
, DL_PEDWARN
, "#%s is a GCC extension", dir
->name
);
324 /* Traditionally, a directive is ignored unless its # is in
325 column 1. Therefore in code intended to work with K+R
326 compilers, directives added by C89 must have their #
327 indented, and directives present in traditional C must not.
328 This is true even of directives in skipped conditional
329 blocks. #elif cannot be used at all. */
330 if (CPP_WTRADITIONAL (pfile
))
332 if (dir
== &dtable
[T_ELIF
])
333 cpp_error (pfile
, DL_WARNING
,
334 "suggest not using #elif in traditional C");
335 else if (indented
&& dir
->origin
== KANDR
)
336 cpp_error (pfile
, DL_WARNING
,
337 "traditional C ignores #%s with the # indented",
339 else if (!indented
&& dir
->origin
!= KANDR
)
340 cpp_error (pfile
, DL_WARNING
,
341 "suggest hiding #%s from traditional C with an indented #",
346 /* Check if we have a known directive. INDENTED is nonzero if the
347 '#' of the directive was indented. This function is in this file
348 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
349 nonzero if the line of tokens has been handled, zero if we should
350 continue processing the line. */
352 _cpp_handle_directive (pfile
, indented
)
356 const directive
*dir
= 0;
357 const cpp_token
*dname
;
358 bool was_parsing_args
= pfile
->state
.parsing_args
;
361 if (was_parsing_args
)
363 if (CPP_OPTION (pfile
, pedantic
))
364 cpp_error (pfile
, DL_PEDWARN
,
365 "embedding a directive within macro arguments is not portable");
366 pfile
->state
.parsing_args
= 0;
367 pfile
->state
.prevent_expansion
= 0;
369 start_directive (pfile
);
370 dname
= _cpp_lex_token (pfile
);
372 if (dname
->type
== CPP_NAME
)
374 if (dname
->val
.node
->is_directive
)
375 dir
= &dtable
[dname
->val
.node
->directive_index
];
377 /* We do not recognize the # followed by a number extension in
379 else if (dname
->type
== CPP_NUMBER
&& CPP_OPTION (pfile
, lang
) != CLK_ASM
)
381 dir
= &linemarker_dir
;
382 if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, preprocessed
)
383 && ! pfile
->state
.skipping
)
384 cpp_error (pfile
, DL_PEDWARN
,
385 "style of line directive is a GCC extension");
390 /* If we have a directive that is not an opening conditional,
391 invalidate any control macro. */
392 if (! (dir
->flags
& IF_COND
))
393 pfile
->mi_valid
= false;
395 /* Kluge alert. In order to be sure that code like this
400 does not cause '#define foo bar' to get executed when
401 compiled with -save-temps, we recognize directives in
402 -fpreprocessed mode only if the # is in column 1. cppmacro.c
403 puts a space in front of any '#' at the start of a macro. */
404 if (CPP_OPTION (pfile
, preprocessed
)
405 && (indented
|| !(dir
->flags
& IN_I
)))
412 /* In failed conditional groups, all non-conditional
413 directives are ignored. Before doing that, whether
414 skipping or not, we should lex angle-bracketed headers
415 correctly, and maybe output some diagnostics. */
416 pfile
->state
.angled_headers
= dir
->flags
& INCL
;
417 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;
454 pfile
->buffer
->saved_flags
|= PREV_WHITE
;
459 /* Directive handler wrapper used by the command line option
462 run_directive (pfile
, dir_no
, buf
, count
)
468 cpp_push_buffer (pfile
, (const uchar
*) buf
, count
,
469 /* from_stage3 */ true, 1);
470 /* Disgusting hack. */
471 if (dir_no
== T_PRAGMA
)
472 pfile
->buffer
->inc
= pfile
->buffer
->prev
->inc
;
473 start_directive (pfile
);
474 /* We don't want a leading # to be interpreted as a directive. */
475 pfile
->buffer
->saved_flags
= 0;
476 pfile
->directive
= &dtable
[dir_no
];
477 if (CPP_OPTION (pfile
, traditional
))
478 prepare_directive_trad (pfile
);
479 (void) (*pfile
->directive
->handler
) (pfile
);
480 end_directive (pfile
, 1);
481 if (dir_no
== T_PRAGMA
)
482 pfile
->buffer
->inc
= NULL
;
483 _cpp_pop_buffer (pfile
);
486 /* Checks for validity the macro name in #define, #undef, #ifdef and
487 #ifndef directives. */
488 static cpp_hashnode
*
489 lex_macro_node (pfile
)
492 const cpp_token
*token
= _cpp_lex_token (pfile
);
494 /* The token immediately after #define must be an identifier. That
495 identifier may not be "defined", per C99 6.10.8p4.
496 In C++, it may not be any of the "named operators" either,
497 per C++98 [lex.digraph], [lex.key].
498 Finally, the identifier may not have been poisoned. (In that case
499 the lexer has issued the error message for us.) */
501 if (token
->type
== CPP_NAME
)
503 cpp_hashnode
*node
= token
->val
.node
;
505 if (node
== pfile
->spec_nodes
.n_defined
)
506 cpp_error (pfile
, DL_ERROR
,
507 "\"defined\" cannot be used as a macro name");
508 else if (! (node
->flags
& NODE_POISONED
))
511 else if (token
->flags
& NAMED_OP
)
512 cpp_error (pfile
, DL_ERROR
,
513 "\"%s\" cannot be used as a macro name as it is an operator in C++",
514 NODE_NAME (token
->val
.node
));
515 else if (token
->type
== CPP_EOF
)
516 cpp_error (pfile
, DL_ERROR
, "no macro name given in #%s directive",
517 pfile
->directive
->name
);
519 cpp_error (pfile
, DL_ERROR
, "macro names must be identifiers");
524 /* Process a #define directive. Most work is done in cppmacro.c. */
529 cpp_hashnode
*node
= lex_macro_node (pfile
);
533 /* If we have been requested to expand comments into macros,
534 then re-enable saving of comments. */
535 pfile
->state
.save_comments
=
536 ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
);
538 if (_cpp_create_definition (pfile
, node
))
539 if (pfile
->cb
.define
)
540 (*pfile
->cb
.define
) (pfile
, pfile
->directive_line
, node
);
544 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
549 cpp_hashnode
*node
= lex_macro_node (pfile
);
551 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
552 is not currently defined as a macro name. */
553 if (node
&& node
->type
== NT_MACRO
)
556 (*pfile
->cb
.undef
) (pfile
, pfile
->directive_line
, node
);
558 if (node
->flags
& NODE_WARN
)
559 cpp_error (pfile
, DL_WARNING
, "undefining \"%s\"", NODE_NAME (node
));
561 if (CPP_OPTION (pfile
, warn_unused_macros
))
562 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
564 _cpp_free_definition (node
);
569 /* Helper routine used by parse_include. Reinterpret the current line
570 as an h-char-sequence (< ... >); we are looking at the first token
571 after the <. Returns the header as a token, or NULL on failure. */
572 static const cpp_token
*
573 glue_header_name (pfile
)
576 cpp_token
*header
= NULL
;
577 const cpp_token
*token
;
578 unsigned char *buffer
;
579 size_t len
, total_len
= 0, capacity
= 1024;
581 /* To avoid lexed tokens overwriting our glued name, we can only
582 allocate from the string pool once we've lexed everything. */
583 buffer
= (unsigned char *) xmalloc (capacity
);
586 token
= get_token_no_padding (pfile
);
588 if (token
->type
== CPP_GREATER
|| token
->type
== CPP_EOF
)
591 len
= cpp_token_len (token
);
592 if (total_len
+ len
> capacity
)
594 capacity
= (capacity
+ len
) * 2;
595 buffer
= (unsigned char *) xrealloc (buffer
, capacity
);
598 if (token
->flags
& PREV_WHITE
)
599 buffer
[total_len
++] = ' ';
601 total_len
= cpp_spell_token (pfile
, token
, &buffer
[total_len
]) - buffer
;
604 if (token
->type
== CPP_EOF
)
605 cpp_error (pfile
, DL_ERROR
, "missing terminating > character");
608 unsigned char *token_mem
= _cpp_unaligned_alloc (pfile
, total_len
+ 1);
609 memcpy (token_mem
, buffer
, total_len
);
610 token_mem
[total_len
] = '\0';
612 header
= _cpp_temp_token (pfile
);
613 header
->type
= CPP_HEADER_NAME
;
615 header
->val
.str
.len
= total_len
;
616 header
->val
.str
.text
= token_mem
;
623 /* Returns the header string of #include, #include_next, #import and
624 #pragma dependency. Returns NULL on error. */
625 static const cpp_token
*
626 parse_include (pfile
)
629 const unsigned char *dir
;
630 const cpp_token
*header
;
632 if (pfile
->directive
== &dtable
[T_PRAGMA
])
633 dir
= U
"pragma dependency";
635 dir
= pfile
->directive
->name
;
637 /* Allow macro expansion. */
638 header
= get_token_no_padding (pfile
);
639 if (header
->type
!= CPP_STRING
&& header
->type
!= CPP_HEADER_NAME
)
641 if (header
->type
!= CPP_LESS
)
643 cpp_error (pfile
, DL_ERROR
,
644 "#%s expects \"FILENAME\" or <FILENAME>", dir
);
648 header
= glue_header_name (pfile
);
653 if (header
->val
.str
.len
== 0)
655 cpp_error (pfile
, DL_ERROR
, "empty file name in #%s", dir
);
663 /* Handle #include, #include_next and #import. */
665 do_include_common (pfile
, type
)
667 enum include_type type
;
669 const cpp_token
*header
= parse_include (pfile
);
673 /* Prevent #include recursion. */
674 if (pfile
->line_maps
.depth
>= CPP_STACK_MAX
)
676 cpp_error (pfile
, DL_ERROR
, "#include nested too deeply");
680 /* Get out of macro context, if we are. */
681 skip_rest_of_line (pfile
);
683 if (pfile
->cb
.include
)
684 (*pfile
->cb
.include
) (pfile
, pfile
->directive_line
,
685 pfile
->directive
->name
, header
);
687 _cpp_execute_include (pfile
, header
, type
);
694 do_include_common (pfile
, IT_INCLUDE
);
701 if (CPP_OPTION (pfile
, warn_import
))
703 CPP_OPTION (pfile
, warn_import
) = 0;
704 cpp_error (pfile
, DL_WARNING
,
705 "#import is obsolete, use an #ifndef wrapper in the header file");
708 do_include_common (pfile
, IT_IMPORT
);
712 do_include_next (pfile
)
715 enum include_type type
= IT_INCLUDE_NEXT
;
717 /* If this is the primary source file, warn and use the normal
719 if (! pfile
->buffer
->prev
)
721 cpp_error (pfile
, DL_WARNING
,
722 "#include_next in primary source file");
725 do_include_common (pfile
, type
);
728 /* Subroutine of do_linemarker. Read possible flags after file name.
729 LAST is the last flag seen; 0 if this is the first flag. Return the
730 flag if it is valid, 0 at the end of the directive. Otherwise
733 read_flag (pfile
, last
)
737 const cpp_token
*token
= _cpp_lex_token (pfile
);
739 if (token
->type
== CPP_NUMBER
&& token
->val
.str
.len
== 1)
741 unsigned int flag
= token
->val
.str
.text
[0] - '0';
743 if (flag
> last
&& flag
<= 4
744 && (flag
!= 4 || last
== 3)
745 && (flag
!= 2 || last
== 0))
749 if (token
->type
!= CPP_EOF
)
750 cpp_error (pfile
, DL_ERROR
, "invalid flag \"%s\" in line directive",
751 cpp_token_as_text (pfile
, token
));
755 /* Subroutine of do_line and do_linemarker. Returns a version of STR
756 which has a NUL terminator and all escape sequences converted to
757 their equivalents. Temporary, hopefully. */
759 dequote_string (pfile
, str
, len
)
764 uchar
*result
= _cpp_unaligned_alloc (pfile
, len
+ 1);
766 const uchar
*limit
= str
+ len
;
775 *dst
++ = cpp_parse_escape (pfile
, &str
, limit
, 0);
781 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
782 of length LEN, to binary; store it in NUMP, and return 0 if the
783 number was well-formed, 1 if not. Temporary, hopefully. */
785 strtoul_for_line (str
, len
, nump
)
790 unsigned long reg
= 0;
804 /* Interpret #line command.
805 Note that the filename string (if any) is a true string constant
806 (escapes are interpreted), unlike in #line. */
811 const cpp_token
*token
;
812 const char *new_file
= pfile
->map
->to_file
;
813 unsigned long new_lineno
;
815 /* C99 raised the minimum limit on #line numbers. */
816 unsigned int cap
= CPP_OPTION (pfile
, c99
) ? 2147483647 : 32767;
818 /* #line commands expand macros. */
819 token
= cpp_get_token (pfile
);
820 if (token
->type
!= CPP_NUMBER
821 || strtoul_for_line (token
->val
.str
.text
, token
->val
.str
.len
,
824 cpp_error (pfile
, DL_ERROR
,
825 "\"%s\" after #line is not a positive integer",
826 cpp_token_as_text (pfile
, token
));
830 if (CPP_PEDANTIC (pfile
) && (new_lineno
== 0 || new_lineno
> cap
))
831 cpp_error (pfile
, DL_PEDWARN
, "line number out of range");
833 token
= cpp_get_token (pfile
);
834 if (token
->type
== CPP_STRING
)
836 new_file
= (const char *) dequote_string (pfile
, token
->val
.str
.text
,
840 else if (token
->type
!= CPP_EOF
)
842 cpp_error (pfile
, DL_ERROR
, "\"%s\" is not a valid filename",
843 cpp_token_as_text (pfile
, token
));
847 skip_rest_of_line (pfile
);
848 _cpp_do_file_change (pfile
, LC_RENAME
, new_file
, new_lineno
,
852 /* Interpret the # 44 "file" [flags] notation, which has slightly
853 different syntax and semantics from #line: Flags are allowed,
854 and we never complain about the line number being too big. */
856 do_linemarker (pfile
)
859 const cpp_token
*token
;
860 const char *new_file
= pfile
->map
->to_file
;
861 unsigned long new_lineno
;
862 unsigned int new_sysp
= pfile
->map
->sysp
;
863 enum lc_reason reason
= LC_RENAME
;
866 /* Back up so we can get the number again. Putting this in
867 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
868 some circumstances, which can segfault. */
869 _cpp_backup_tokens (pfile
, 1);
871 /* #line commands expand macros. */
872 token
= cpp_get_token (pfile
);
873 if (token
->type
!= CPP_NUMBER
874 || strtoul_for_line (token
->val
.str
.text
, token
->val
.str
.len
,
877 cpp_error (pfile
, DL_ERROR
, "\"%s\" after # is not a positive integer",
878 cpp_token_as_text (pfile
, token
));
882 token
= cpp_get_token (pfile
);
883 if (token
->type
== CPP_STRING
)
885 new_file
= (const char *) dequote_string (pfile
, token
->val
.str
.text
,
888 flag
= read_flag (pfile
, 0);
892 /* Fake an include for cpp_included (). */
893 _cpp_fake_include (pfile
, new_file
);
894 flag
= read_flag (pfile
, flag
);
899 flag
= read_flag (pfile
, flag
);
904 flag
= read_flag (pfile
, flag
);
911 else if (token
->type
!= CPP_EOF
)
913 cpp_error (pfile
, DL_ERROR
, "\"%s\" is not a valid filename",
914 cpp_token_as_text (pfile
, token
));
918 skip_rest_of_line (pfile
);
919 _cpp_do_file_change (pfile
, reason
, new_file
, new_lineno
, new_sysp
);
922 /* Arrange the file_change callback. pfile->line has changed to
923 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
924 header, 2 for a system header that needs to be extern "C" protected,
925 and zero otherwise. */
927 _cpp_do_file_change (pfile
, reason
, to_file
, file_line
, sysp
)
929 enum lc_reason reason
;
931 unsigned int file_line
;
934 pfile
->map
= add_line_map (&pfile
->line_maps
, reason
, sysp
,
935 pfile
->line
, to_file
, file_line
);
937 if (pfile
->cb
.file_change
)
938 (*pfile
->cb
.file_change
) (pfile
, pfile
->map
);
941 /* Report a warning or error detected by the program we are
942 processing. Use the directive's tokens in the error message. */
944 do_diagnostic (pfile
, code
, print_dir
)
949 if (_cpp_begin_message (pfile
, code
,
950 pfile
->cur_token
[-1].line
,
951 pfile
->cur_token
[-1].col
))
954 fprintf (stderr
, "#%s ", pfile
->directive
->name
);
955 pfile
->state
.prevent_expansion
++;
956 cpp_output_line (pfile
, stderr
);
957 pfile
->state
.prevent_expansion
--;
965 do_diagnostic (pfile
, DL_ERROR
, 1);
972 /* We want #warning diagnostics to be emitted in system headers too. */
973 do_diagnostic (pfile
, DL_WARNING_SYSHDR
, 1);
976 /* Report program identification. */
981 const cpp_token
*str
= cpp_get_token (pfile
);
983 if (str
->type
!= CPP_STRING
)
984 cpp_error (pfile
, DL_ERROR
, "invalid #ident directive");
985 else if (pfile
->cb
.ident
)
986 (*pfile
->cb
.ident
) (pfile
, pfile
->directive_line
, &str
->val
.str
);
991 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
992 matching entry, or NULL if none is found. The returned entry could
993 be the start of a namespace chain, or a pragma. */
994 static struct pragma_entry
*
995 lookup_pragma_entry (chain
, pragma
)
996 struct pragma_entry
*chain
;
997 const cpp_hashnode
*pragma
;
999 while (chain
&& chain
->pragma
!= pragma
)
1000 chain
= chain
->next
;
1005 /* Create and insert a pragma entry for NAME at the beginning of a
1006 singly-linked CHAIN. If handler is NULL, it is a namespace,
1007 otherwise it is a pragma and its handler. */
1008 static struct pragma_entry
*
1009 insert_pragma_entry (pfile
, chain
, pragma
, handler
)
1011 struct pragma_entry
**chain
;
1012 const cpp_hashnode
*pragma
;
1015 struct pragma_entry
*new;
1017 new = (struct pragma_entry
*)
1018 _cpp_aligned_alloc (pfile
, sizeof (struct pragma_entry
));
1019 new->pragma
= pragma
;
1023 new->u
.handler
= handler
;
1028 new->u
.space
= NULL
;
1036 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1037 goes in the global namespace. HANDLER is the handler it will call,
1038 which must be non-NULL. */
1040 cpp_register_pragma (pfile
, space
, name
, handler
)
1046 struct pragma_entry
**chain
= &pfile
->pragmas
;
1047 struct pragma_entry
*entry
;
1048 const cpp_hashnode
*node
;
1055 node
= cpp_lookup (pfile
, U space
, strlen (space
));
1056 entry
= lookup_pragma_entry (*chain
, node
);
1058 entry
= insert_pragma_entry (pfile
, chain
, node
, NULL
);
1059 else if (!entry
->is_nspace
)
1061 chain
= &entry
->u
.space
;
1064 /* Check for duplicates. */
1065 node
= cpp_lookup (pfile
, U name
, strlen (name
));
1066 entry
= lookup_pragma_entry (*chain
, node
);
1069 if (entry
->is_nspace
)
1071 cpp_error (pfile
, DL_ICE
,
1072 "registering \"%s\" as both a pragma and a pragma namespace",
1075 cpp_error (pfile
, DL_ICE
, "#pragma %s %s is already registered",
1078 cpp_error (pfile
, DL_ICE
, "#pragma %s is already registered", name
);
1081 insert_pragma_entry (pfile
, chain
, node
, handler
);
1084 /* Register the pragmas the preprocessor itself handles. */
1086 _cpp_init_internal_pragmas (pfile
)
1089 /* Pragmas in the global namespace. */
1090 cpp_register_pragma (pfile
, 0, "once", do_pragma_once
);
1092 /* New GCC-specific pragmas should be put in the GCC namespace. */
1093 cpp_register_pragma (pfile
, "GCC", "poison", do_pragma_poison
);
1094 cpp_register_pragma (pfile
, "GCC", "system_header", do_pragma_system_header
);
1095 cpp_register_pragma (pfile
, "GCC", "dependency", do_pragma_dependency
);
1098 /* Return the number of registered pragmas in PE. */
1101 count_registered_pragmas (pe
)
1102 struct pragma_entry
*pe
;
1105 for (; pe
!= NULL
; pe
= pe
->next
)
1108 ct
+= count_registered_pragmas (pe
->u
.space
);
1114 /* Save into SD the names of the registered pragmas referenced by PE,
1115 and return a pointer to the next free space in SD. */
1118 save_registered_pragmas (pe
, sd
)
1119 struct pragma_entry
*pe
;
1122 for (; pe
!= NULL
; pe
= pe
->next
)
1125 sd
= save_registered_pragmas (pe
->u
.space
, sd
);
1126 *sd
++ = xmemdup (HT_STR (&pe
->pragma
->ident
),
1127 HT_LEN (&pe
->pragma
->ident
),
1128 HT_LEN (&pe
->pragma
->ident
) + 1);
1133 /* Return a newly-allocated array which saves the names of the
1134 registered pragmas. */
1137 _cpp_save_pragma_names (pfile
)
1140 int ct
= count_registered_pragmas (pfile
->pragmas
);
1141 char **result
= xnewvec (char *, ct
);
1142 (void) save_registered_pragmas (pfile
->pragmas
, result
);
1146 /* Restore from SD the names of the registered pragmas referenced by PE,
1147 and return a pointer to the next unused name in SD. */
1150 restore_registered_pragmas (pfile
, pe
, sd
)
1152 struct pragma_entry
*pe
;
1155 for (; pe
!= NULL
; pe
= pe
->next
)
1158 sd
= restore_registered_pragmas (pfile
, pe
->u
.space
, sd
);
1159 pe
->pragma
= cpp_lookup (pfile
, U
*sd
, strlen (*sd
));
1166 /* Restore the names of the registered pragmas from SAVED. */
1169 _cpp_restore_pragma_names (pfile
, saved
)
1173 (void) restore_registered_pragmas (pfile
, pfile
->pragmas
, saved
);
1177 /* Pragmata handling. We handle some, and pass the rest on to the
1178 front end. C99 defines three pragmas and says that no macro
1179 expansion is to be performed on them; whether or not macro
1180 expansion happens for other pragmas is implementation defined.
1181 This implementation never macro-expands the text after #pragma. */
1186 const struct pragma_entry
*p
= NULL
;
1187 const cpp_token
*token
;
1188 unsigned int count
= 1;
1190 pfile
->state
.prevent_expansion
++;
1192 token
= cpp_get_token (pfile
);
1193 if (token
->type
== CPP_NAME
)
1195 p
= lookup_pragma_entry (pfile
->pragmas
, token
->val
.node
);
1196 if (p
&& p
->is_nspace
)
1199 token
= cpp_get_token (pfile
);
1200 if (token
->type
== CPP_NAME
)
1201 p
= lookup_pragma_entry (p
->u
.space
, token
->val
.node
);
1207 /* FIXME. This is an awful kludge to get the front ends to update
1208 their notion of line number for diagnostic purposes. The line
1209 number should be passed to the handler and they should do it
1210 themselves. Stand-alone CPP must ignore us, otherwise it will
1211 prefix the directive with spaces, hence the 1. Ugh. */
1212 if (pfile
->cb
.line_change
)
1213 (*pfile
->cb
.line_change
)(pfile
, token
, 1);
1216 (*p
->u
.handler
) (pfile
);
1217 else if (pfile
->cb
.def_pragma
)
1219 _cpp_backup_tokens (pfile
, count
);
1220 (*pfile
->cb
.def_pragma
) (pfile
, pfile
->directive_line
);
1223 pfile
->state
.prevent_expansion
--;
1226 /* Handle #pragma once. */
1228 do_pragma_once (pfile
)
1231 if (CPP_OPTION (pfile
, warn_deprecated
))
1232 cpp_error (pfile
, DL_WARNING
, "#pragma once is obsolete");
1234 if (pfile
->buffer
->prev
== NULL
)
1235 cpp_error (pfile
, DL_WARNING
, "#pragma once in main file");
1237 _cpp_never_reread (pfile
->buffer
->inc
);
1242 /* Handle #pragma GCC poison, to poison one or more identifiers so
1243 that the lexer produces a hard error for each subsequent usage. */
1245 do_pragma_poison (pfile
)
1248 const cpp_token
*tok
;
1251 pfile
->state
.poisoned_ok
= 1;
1254 tok
= _cpp_lex_token (pfile
);
1255 if (tok
->type
== CPP_EOF
)
1257 if (tok
->type
!= CPP_NAME
)
1259 cpp_error (pfile
, DL_ERROR
, "invalid #pragma GCC poison directive");
1264 if (hp
->flags
& NODE_POISONED
)
1267 if (hp
->type
== NT_MACRO
)
1268 cpp_error (pfile
, DL_WARNING
, "poisoning existing macro \"%s\"",
1270 _cpp_free_definition (hp
);
1271 hp
->flags
|= NODE_POISONED
| NODE_DIAGNOSTIC
;
1273 pfile
->state
.poisoned_ok
= 0;
1276 /* Mark the current header as a system header. This will suppress
1277 some categories of warnings (notably those from -pedantic). It is
1278 intended for use in system libraries that cannot be implemented in
1279 conforming C, but cannot be certain that their headers appear in a
1280 system include directory. To prevent abuse, it is rejected in the
1281 primary source file. */
1283 do_pragma_system_header (pfile
)
1286 cpp_buffer
*buffer
= pfile
->buffer
;
1288 if (buffer
->prev
== 0)
1289 cpp_error (pfile
, DL_WARNING
,
1290 "#pragma system_header ignored outside include file");
1294 skip_rest_of_line (pfile
);
1295 cpp_make_system_header (pfile
, 1, 0);
1299 /* Check the modified date of the current include file against a specified
1300 file. Issue a diagnostic, if the specified file is newer. We use this to
1301 determine if a fixed header should be refixed. */
1303 do_pragma_dependency (pfile
)
1306 const cpp_token
*header
;
1309 header
= parse_include (pfile
);
1313 ordering
= _cpp_compare_file_date (pfile
, header
);
1315 cpp_error (pfile
, DL_WARNING
, "cannot find source %s",
1316 cpp_token_as_text (pfile
, header
));
1317 else if (ordering
> 0)
1319 cpp_error (pfile
, DL_WARNING
, "current file is older than %s",
1320 cpp_token_as_text (pfile
, header
));
1321 if (cpp_get_token (pfile
)->type
!= CPP_EOF
)
1323 _cpp_backup_tokens (pfile
, 1);
1324 do_diagnostic (pfile
, DL_WARNING
, 0);
1329 /* Get a token but skip padding. */
1330 static const cpp_token
*
1331 get_token_no_padding (pfile
)
1336 const cpp_token
*result
= cpp_get_token (pfile
);
1337 if (result
->type
!= CPP_PADDING
)
1342 /* Check syntax is "(string-literal)". Returns the string on success,
1343 or NULL on failure. */
1344 static const cpp_token
*
1345 get__Pragma_string (pfile
)
1348 const cpp_token
*string
;
1350 if (get_token_no_padding (pfile
)->type
!= CPP_OPEN_PAREN
)
1353 string
= get_token_no_padding (pfile
);
1354 if (string
->type
!= CPP_STRING
&& string
->type
!= CPP_WSTRING
)
1357 if (get_token_no_padding (pfile
)->type
!= CPP_CLOSE_PAREN
)
1363 /* Destringize IN into a temporary buffer, by removing the first \ of
1364 \" and \\ sequences, and process the result as a #pragma directive. */
1366 destringize_and_run (pfile
, in
)
1368 const cpp_string
*in
;
1370 const unsigned char *src
, *limit
;
1371 char *dest
, *result
;
1373 dest
= result
= alloca (in
->len
+ 1);
1374 for (src
= in
->text
, limit
= src
+ in
->len
; src
< limit
;)
1376 /* We know there is a character following the backslash. */
1377 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1383 /* Ugh; an awful kludge. We are really not set up to be lexing
1384 tokens when in the middle of a macro expansion. Use a new
1385 context to force cpp_get_token to lex, and so skip_rest_of_line
1386 doesn't go beyond the end of the text. Also, remember the
1387 current lexing position so we can return to it later.
1389 Something like line-at-a-time lexing should remove the need for
1392 cpp_context
*saved_context
= pfile
->context
;
1393 cpp_token
*saved_cur_token
= pfile
->cur_token
;
1394 tokenrun
*saved_cur_run
= pfile
->cur_run
;
1396 pfile
->context
= xnew (cpp_context
);
1397 pfile
->context
->macro
= 0;
1398 pfile
->context
->prev
= 0;
1399 run_directive (pfile
, T_PRAGMA
, result
, dest
- result
);
1400 free (pfile
->context
);
1401 pfile
->context
= saved_context
;
1402 pfile
->cur_token
= saved_cur_token
;
1403 pfile
->cur_run
= saved_cur_run
;
1407 /* See above comment. For the moment, we'd like
1409 token1 _Pragma ("foo") token2
1419 Getting the line markers is a little tricky. */
1420 if (pfile
->cb
.line_change
)
1421 (*pfile
->cb
.line_change
) (pfile
, pfile
->cur_token
, false);
1424 /* Handle the _Pragma operator. */
1426 _cpp_do__Pragma (pfile
)
1429 const cpp_token
*string
= get__Pragma_string (pfile
);
1432 destringize_and_run (pfile
, &string
->val
.str
);
1434 cpp_error (pfile
, DL_ERROR
,
1435 "_Pragma takes a parenthesized string literal");
1438 /* Just ignore #sccs on all systems. */
1441 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
1445 /* Handle #ifdef. */
1452 if (! pfile
->state
.skipping
)
1454 const cpp_hashnode
*node
= lex_macro_node (pfile
);
1458 skip
= node
->type
!= NT_MACRO
;
1459 _cpp_mark_macro_used (node
);
1464 push_conditional (pfile
, skip
, T_IFDEF
, 0);
1467 /* Handle #ifndef. */
1473 const cpp_hashnode
*node
= 0;
1475 if (! pfile
->state
.skipping
)
1477 node
= lex_macro_node (pfile
);
1481 skip
= node
->type
== NT_MACRO
;
1482 _cpp_mark_macro_used (node
);
1487 push_conditional (pfile
, skip
, T_IFNDEF
, node
);
1490 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1491 pfile->mi_ind_cmacro so we can handle multiple-include
1492 optimisations. If macro expansion occurs in the expression, we
1493 cannot treat it as a controlling conditional, since the expansion
1494 could change in the future. That is handled by cpp_get_token. */
1501 if (! pfile
->state
.skipping
)
1502 skip
= _cpp_parse_expr (pfile
) == false;
1504 push_conditional (pfile
, skip
, T_IF
, pfile
->mi_ind_cmacro
);
1507 /* Flip skipping state if appropriate and continue without changing
1508 if_stack; this is so that the error message for missing #endif's
1509 etc. will point to the original #if. */
1514 cpp_buffer
*buffer
= pfile
->buffer
;
1515 struct if_stack
*ifs
= buffer
->if_stack
;
1518 cpp_error (pfile
, DL_ERROR
, "#else without #if");
1521 if (ifs
->type
== T_ELSE
)
1523 cpp_error (pfile
, DL_ERROR
, "#else after #else");
1524 cpp_error_with_line (pfile
, DL_ERROR
, ifs
->line
, 0,
1525 "the conditional began here");
1529 /* Skip any future (erroneous) #elses or #elifs. */
1530 pfile
->state
.skipping
= ifs
->skip_elses
;
1531 ifs
->skip_elses
= true;
1533 /* Invalidate any controlling macro. */
1536 /* Only check EOL if was not originally skipping. */
1537 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1542 /* Handle a #elif directive by not changing if_stack either. See the
1543 comment above do_else. */
1548 cpp_buffer
*buffer
= pfile
->buffer
;
1549 struct if_stack
*ifs
= buffer
->if_stack
;
1552 cpp_error (pfile
, DL_ERROR
, "#elif without #if");
1555 if (ifs
->type
== T_ELSE
)
1557 cpp_error (pfile
, DL_ERROR
, "#elif after #else");
1558 cpp_error_with_line (pfile
, DL_ERROR
, ifs
->line
, 0,
1559 "the conditional began here");
1563 /* Only evaluate this if we aren't skipping elses. During
1564 evaluation, set skipping to false to get lexer warnings. */
1565 if (ifs
->skip_elses
)
1566 pfile
->state
.skipping
= 1;
1569 pfile
->state
.skipping
= 0;
1570 pfile
->state
.skipping
= ! _cpp_parse_expr (pfile
);
1571 ifs
->skip_elses
= ! pfile
->state
.skipping
;
1574 /* Invalidate any controlling macro. */
1579 /* #endif pops the if stack and resets pfile->state.skipping. */
1584 cpp_buffer
*buffer
= pfile
->buffer
;
1585 struct if_stack
*ifs
= buffer
->if_stack
;
1588 cpp_error (pfile
, DL_ERROR
, "#endif without #if");
1591 /* Only check EOL if was not originally skipping. */
1592 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1595 /* If potential control macro, we go back outside again. */
1596 if (ifs
->next
== 0 && ifs
->mi_cmacro
)
1598 pfile
->mi_valid
= true;
1599 pfile
->mi_cmacro
= ifs
->mi_cmacro
;
1602 buffer
->if_stack
= ifs
->next
;
1603 pfile
->state
.skipping
= ifs
->was_skipping
;
1604 obstack_free (&pfile
->buffer_ob
, ifs
);
1608 /* Push an if_stack entry for a preprocessor conditional, and set
1609 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1610 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1611 we need to check here that we are at the top of the file. */
1613 push_conditional (pfile
, skip
, type
, cmacro
)
1617 const cpp_hashnode
*cmacro
;
1619 struct if_stack
*ifs
;
1620 cpp_buffer
*buffer
= pfile
->buffer
;
1622 ifs
= xobnew (&pfile
->buffer_ob
, struct if_stack
);
1623 ifs
->line
= pfile
->directive_line
;
1624 ifs
->next
= buffer
->if_stack
;
1625 ifs
->skip_elses
= pfile
->state
.skipping
|| !skip
;
1626 ifs
->was_skipping
= pfile
->state
.skipping
;
1628 /* This condition is effectively a test for top-of-file. */
1629 if (pfile
->mi_valid
&& pfile
->mi_cmacro
== 0)
1630 ifs
->mi_cmacro
= cmacro
;
1634 pfile
->state
.skipping
= skip
;
1635 buffer
->if_stack
= ifs
;
1638 /* Read the tokens of the answer into the macro pool, in a directive
1639 of type TYPE. Only commit the memory if we intend it as permanent
1640 storage, i.e. the #assert case. Returns 0 on success, and sets
1641 ANSWERP to point to the answer. */
1643 parse_answer (pfile
, answerp
, type
)
1645 struct answer
**answerp
;
1648 const cpp_token
*paren
;
1649 struct answer
*answer
;
1650 unsigned int acount
;
1652 /* In a conditional, it is legal to not have an open paren. We
1653 should save the following token in this case. */
1654 paren
= cpp_get_token (pfile
);
1656 /* If not a paren, see if we're OK. */
1657 if (paren
->type
!= CPP_OPEN_PAREN
)
1659 /* In a conditional no answer is a test for any answer. It
1660 could be followed by any token. */
1663 _cpp_backup_tokens (pfile
, 1);
1667 /* #unassert with no answer is valid - it removes all answers. */
1668 if (type
== T_UNASSERT
&& paren
->type
== CPP_EOF
)
1671 cpp_error (pfile
, DL_ERROR
, "missing '(' after predicate");
1675 for (acount
= 0;; acount
++)
1678 const cpp_token
*token
= cpp_get_token (pfile
);
1681 if (token
->type
== CPP_CLOSE_PAREN
)
1684 if (token
->type
== CPP_EOF
)
1686 cpp_error (pfile
, DL_ERROR
, "missing ')' to complete answer");
1690 /* struct answer includes the space for one token. */
1691 room_needed
= (sizeof (struct answer
) + acount
* sizeof (cpp_token
));
1693 if (BUFF_ROOM (pfile
->a_buff
) < room_needed
)
1694 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (struct answer
));
1696 dest
= &((struct answer
*) BUFF_FRONT (pfile
->a_buff
))->first
[acount
];
1699 /* Drop whitespace at start, for answer equivalence purposes. */
1701 dest
->flags
&= ~PREV_WHITE
;
1706 cpp_error (pfile
, DL_ERROR
, "predicate's answer is empty");
1710 answer
= (struct answer
*) BUFF_FRONT (pfile
->a_buff
);
1711 answer
->count
= acount
;
1712 answer
->next
= NULL
;
1718 /* Parses an assertion directive of type TYPE, returning a pointer to
1719 the hash node of the predicate, or 0 on error. If an answer was
1720 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
1721 static cpp_hashnode
*
1722 parse_assertion (pfile
, answerp
, type
)
1724 struct answer
**answerp
;
1727 cpp_hashnode
*result
= 0;
1728 const cpp_token
*predicate
;
1730 /* We don't expand predicates or answers. */
1731 pfile
->state
.prevent_expansion
++;
1734 predicate
= cpp_get_token (pfile
);
1735 if (predicate
->type
== CPP_EOF
)
1736 cpp_error (pfile
, DL_ERROR
, "assertion without predicate");
1737 else if (predicate
->type
!= CPP_NAME
)
1738 cpp_error (pfile
, DL_ERROR
, "predicate must be an identifier");
1739 else if (parse_answer (pfile
, answerp
, type
) == 0)
1741 unsigned int len
= NODE_LEN (predicate
->val
.node
);
1742 unsigned char *sym
= alloca (len
+ 1);
1744 /* Prefix '#' to get it out of macro namespace. */
1746 memcpy (sym
+ 1, NODE_NAME (predicate
->val
.node
), len
);
1747 result
= cpp_lookup (pfile
, sym
, len
+ 1);
1750 pfile
->state
.prevent_expansion
--;
1754 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1755 or a pointer to NULL if the answer is not in the chain. */
1756 static struct answer
**
1757 find_answer (node
, candidate
)
1759 const struct answer
*candidate
;
1762 struct answer
**result
;
1764 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->next
)
1766 struct answer
*answer
= *result
;
1768 if (answer
->count
== candidate
->count
)
1770 for (i
= 0; i
< answer
->count
; i
++)
1771 if (! _cpp_equiv_tokens (&answer
->first
[i
], &candidate
->first
[i
]))
1774 if (i
== answer
->count
)
1782 /* Test an assertion within a preprocessor conditional. Returns
1783 nonzero on failure, zero on success. On success, the result of
1784 the test is written into VALUE, otherwise the value 0. */
1786 _cpp_test_assertion (pfile
, value
)
1788 unsigned int *value
;
1790 struct answer
*answer
;
1793 node
= parse_assertion (pfile
, &answer
, T_IF
);
1795 /* For recovery, an erroneous assertion expression is handled as a
1796 failing assertion. */
1800 *value
= (node
->type
== NT_ASSERTION
&&
1801 (answer
== 0 || *find_answer (node
, answer
) != 0));
1802 else if (pfile
->cur_token
[-1].type
== CPP_EOF
)
1803 _cpp_backup_tokens (pfile
, 1);
1805 /* We don't commit the memory for the answer - it's temporary only. */
1809 /* Handle #assert. */
1814 struct answer
*new_answer
;
1817 node
= parse_assertion (pfile
, &new_answer
, T_ASSERT
);
1820 /* Place the new answer in the answer list. First check there
1821 is not a duplicate. */
1822 new_answer
->next
= 0;
1823 if (node
->type
== NT_ASSERTION
)
1825 if (*find_answer (node
, new_answer
))
1827 cpp_error (pfile
, DL_WARNING
, "\"%s\" re-asserted",
1828 NODE_NAME (node
) + 1);
1831 new_answer
->next
= node
->value
.answers
;
1834 node
->type
= NT_ASSERTION
;
1835 node
->value
.answers
= new_answer
;
1836 BUFF_FRONT (pfile
->a_buff
) += (sizeof (struct answer
)
1837 + (new_answer
->count
- 1)
1838 * sizeof (cpp_token
));
1843 /* Handle #unassert. */
1849 struct answer
*answer
;
1851 node
= parse_assertion (pfile
, &answer
, T_UNASSERT
);
1852 /* It isn't an error to #unassert something that isn't asserted. */
1853 if (node
&& node
->type
== NT_ASSERTION
)
1857 struct answer
**p
= find_answer (node
, answer
), *temp
;
1859 /* Remove the answer from the list. */
1864 /* Did we free the last answer? */
1865 if (node
->value
.answers
== 0)
1866 node
->type
= NT_VOID
;
1871 _cpp_free_definition (node
);
1874 /* We don't commit the memory for the answer - it's temporary only. */
1877 /* These are for -D, -U, -A. */
1879 /* Process the string STR as if it appeared as the body of a #define.
1880 If STR is just an identifier, define it with value 1.
1881 If STR has anything after the identifier, then it should
1882 be identifier=definition. */
1884 cpp_define (pfile
, str
)
1891 /* Copy the entire option so we can modify it.
1892 Change the first "=" in the string to a space. If there is none,
1893 tack " 1" on the end. */
1895 count
= strlen (str
);
1896 buf
= (char *) alloca (count
+ 3);
1897 memcpy (buf
, str
, count
);
1899 p
= strchr (str
, '=');
1909 run_directive (pfile
, T_DEFINE
, buf
, count
);
1912 /* Slight variant of the above for use by initialize_builtins. */
1914 _cpp_define_builtin (pfile
, str
)
1918 run_directive (pfile
, T_DEFINE
, str
, strlen (str
));
1921 /* Process MACRO as if it appeared as the body of an #undef. */
1923 cpp_undef (pfile
, macro
)
1927 run_directive (pfile
, T_UNDEF
, macro
, strlen (macro
));
1930 /* Process the string STR as if it appeared as the body of a #assert. */
1932 cpp_assert (pfile
, str
)
1936 handle_assertion (pfile
, str
, T_ASSERT
);
1939 /* Process STR as if it appeared as the body of an #unassert. */
1941 cpp_unassert (pfile
, str
)
1945 handle_assertion (pfile
, str
, T_UNASSERT
);
1948 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1950 handle_assertion (pfile
, str
, type
)
1955 size_t count
= strlen (str
);
1956 const char *p
= strchr (str
, '=');
1960 /* Copy the entire option so we can modify it. Change the first
1961 "=" in the string to a '(', and tack a ')' on the end. */
1962 char *buf
= (char *) alloca (count
+ 2);
1964 memcpy (buf
, str
, count
);
1971 run_directive (pfile
, type
, str
, count
);
1974 /* The number of errors for a given reader. */
1979 return pfile
->errors
;
1982 /* The options structure. */
1984 cpp_get_options (pfile
)
1987 return &pfile
->opts
;
1990 /* The callbacks structure. */
1992 cpp_get_callbacks (pfile
)
1998 /* The line map set. */
1999 const struct line_maps
*
2000 cpp_get_line_maps (pfile
)
2003 return &pfile
->line_maps
;
2006 /* Copy the given callbacks structure to our own. */
2008 cpp_set_callbacks (pfile
, cb
)
2015 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2016 doesn't fail. It does not generate a file change call back; that
2017 is the responsibility of the caller. */
2019 cpp_push_buffer (pfile
, buffer
, len
, from_stage3
, return_at_eof
)
2021 const uchar
*buffer
;
2026 cpp_buffer
*new = xobnew (&pfile
->buffer_ob
, cpp_buffer
);
2028 /* Clears, amongst other things, if_stack and mi_cmacro. */
2029 memset (new, 0, sizeof (cpp_buffer
));
2031 new->line_base
= new->buf
= new->cur
= buffer
;
2032 new->rlimit
= buffer
+ len
;
2033 new->from_stage3
= from_stage3
|| CPP_OPTION (pfile
, traditional
);
2034 new->prev
= pfile
->buffer
;
2035 new->return_at_eof
= return_at_eof
;
2036 new->saved_flags
= BOL
;
2038 pfile
->buffer
= new;
2043 /* Pops a single buffer, with a file change call-back if appropriate.
2044 Then pushes the next -include file, if any remain. */
2046 _cpp_pop_buffer (pfile
)
2049 cpp_buffer
*buffer
= pfile
->buffer
;
2050 struct include_file
*inc
= buffer
->inc
;
2051 struct if_stack
*ifs
;
2053 /* Walk back up the conditional stack till we reach its level at
2054 entry to this file, issuing error messages. */
2055 for (ifs
= buffer
->if_stack
; ifs
; ifs
= ifs
->next
)
2056 cpp_error_with_line (pfile
, DL_ERROR
, ifs
->line
, 0,
2057 "unterminated #%s", dtable
[ifs
->type
].name
);
2059 /* In case of a missing #endif. */
2060 pfile
->state
.skipping
= 0;
2062 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2063 pfile
->buffer
= buffer
->prev
;
2065 /* Free the buffer object now; we may want to push a new buffer
2066 in _cpp_push_next_include_file. */
2067 obstack_free (&pfile
->buffer_ob
, buffer
);
2071 _cpp_pop_file_buffer (pfile
, inc
);
2073 /* Don't generate a callback for popping the main file. */
2075 _cpp_do_file_change (pfile
, LC_LEAVE
, 0, 0, 0);
2079 /* Enter all recognized directives in the hash table. */
2081 _cpp_init_directives (pfile
)
2087 for (i
= 0; i
< (unsigned int) N_DIRECTIVES
; i
++)
2089 node
= cpp_lookup (pfile
, dtable
[i
].name
, dtable
[i
].length
);
2090 node
->is_directive
= 1;
2091 node
->directive_index
= i
;