1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
29 /* Stack of conditionals currently in progress
30 (including both successful and failing conditionals). */
33 struct if_stack
*next
;
34 unsigned int line
; /* Line where condition started. */
35 const cpp_hashnode
*mi_cmacro
;/* macro name for #ifndef around entire file */
36 bool skip_elses
; /* Can future #else / #elif be skipped? */
37 bool was_skipping
; /* If were skipping on entry. */
38 int type
; /* Most recent conditional for diagnostics. */
41 /* Contains a registered pragma or pragma namespace. */
42 typedef void (*pragma_cb
) (cpp_reader
*);
45 struct pragma_entry
*next
;
46 const cpp_hashnode
*pragma
; /* Name and length. */
52 struct pragma_entry
*space
;
56 /* Values for the origin field of struct directive. KANDR directives
57 come from traditional (K&R) C. STDC89 directives come from the
58 1989 C standard. EXTENSION directives are extensions. */
63 /* Values for the flags field of struct directive. COND indicates a
64 conditional; IF_COND an opening conditional. INCL means to treat
65 "..." and <...> as q-char and h-char sequences respectively. IN_I
66 means this directive should be handled even if -fpreprocessed is in
67 effect (these are the directives with callback hooks).
69 EXPAND is set on directives that are always macro-expanded. */
71 #define IF_COND (1 << 1)
74 #define EXPAND (1 << 4)
76 /* Defines one #-directive, including how to handle it. */
77 typedef void (*directive_handler
) (cpp_reader
*);
78 typedef struct directive directive
;
81 directive_handler handler
; /* Function to handle directive. */
82 const uchar
*name
; /* Name of directive. */
83 unsigned short length
; /* Length of name. */
84 unsigned char origin
; /* Origin of directive. */
85 unsigned char flags
; /* Flags describing this directive. */
88 /* Forward declarations. */
90 static void skip_rest_of_line (cpp_reader
*);
91 static void check_eol (cpp_reader
*);
92 static void start_directive (cpp_reader
*);
93 static void prepare_directive_trad (cpp_reader
*);
94 static void end_directive (cpp_reader
*, int);
95 static void directive_diagnostics (cpp_reader
*, const directive
*, int);
96 static void run_directive (cpp_reader
*, int, const char *, size_t);
97 static char *glue_header_name (cpp_reader
*);
98 static const char *parse_include (cpp_reader
*, int *);
99 static void push_conditional (cpp_reader
*, int, int, const cpp_hashnode
*);
100 static unsigned int read_flag (cpp_reader
*, unsigned int);
101 static int strtoul_for_line (const uchar
*, unsigned int, unsigned long *);
102 static void do_diagnostic (cpp_reader
*, int, int);
103 static cpp_hashnode
*lex_macro_node (cpp_reader
*);
104 static int undefine_macros (cpp_reader
*, cpp_hashnode
*, void *);
105 static void do_include_common (cpp_reader
*, enum include_type
);
106 static struct pragma_entry
*lookup_pragma_entry (struct pragma_entry
*,
107 const cpp_hashnode
*);
108 static struct pragma_entry
*insert_pragma_entry (cpp_reader
*,
109 struct pragma_entry
**,
110 const cpp_hashnode
*,
113 static void register_pragma (cpp_reader
*, const char *, const char *,
114 pragma_cb
, bool, bool);
115 static int count_registered_pragmas (struct pragma_entry
*);
116 static char ** save_registered_pragmas (struct pragma_entry
*, char **);
117 static char ** restore_registered_pragmas (cpp_reader
*, struct pragma_entry
*,
119 static void do_pragma_once (cpp_reader
*);
120 static void do_pragma_poison (cpp_reader
*);
121 static void do_pragma_system_header (cpp_reader
*);
122 static void do_pragma_dependency (cpp_reader
*);
123 static void do_linemarker (cpp_reader
*);
124 static const cpp_token
*get_token_no_padding (cpp_reader
*);
125 static const cpp_token
*get__Pragma_string (cpp_reader
*);
126 static void destringize_and_run (cpp_reader
*, const cpp_string
*);
127 static int parse_answer (cpp_reader
*, struct answer
**, int);
128 static cpp_hashnode
*parse_assertion (cpp_reader
*, struct answer
**, int);
129 static struct answer
** find_answer (cpp_hashnode
*, const struct answer
*);
130 static void handle_assertion (cpp_reader
*, const char *, int);
132 /* This is the table of directive handlers. It is ordered by
133 frequency of occurrence; the numbers at the end are directive
134 counts from all the source code I have lying around (egcs and libc
135 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
136 pcmcia-cs-3.0.9). This is no longer important as directive lookup
137 is now O(1). All extensions other than #warning and #include_next
138 are deprecated. The name is where the extension appears to have
141 #define DIRECTIVE_TABLE \
142 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
143 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
144 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
145 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
146 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
147 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
148 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
149 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
150 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
151 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
152 D(error, T_ERROR, STDC89, 0) /* 475 */ \
153 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
154 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
155 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
156 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
157 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
158 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
159 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
160 D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */
162 /* #sccs is synonymous with #ident. */
163 #define do_sccs do_ident
165 /* Use the table to generate a series of prototypes, an enum for the
166 directive names, and an array of directive handlers. */
168 #define D(name, t, o, f) static void do_##name (cpp_reader *);
172 #define D(n, tag, o, f) tag,
180 #define D(name, t, origin, flags) \
181 { do_##name, (const uchar *) #name, \
182 sizeof #name - 1, origin, flags },
183 static const directive dtable
[] =
188 #undef DIRECTIVE_TABLE
190 /* Wrapper struct directive for linemarkers.
191 The origin is more or less true - the original K+R cpp
192 did use this notation in its preprocessed output. */
193 static const directive linemarker_dir
=
195 do_linemarker
, U
"#", 1, KANDR
, IN_I
198 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
200 /* Skip any remaining tokens in a directive. */
202 skip_rest_of_line (cpp_reader
*pfile
)
204 /* Discard all stacked contexts. */
205 while (pfile
->context
->prev
)
206 _cpp_pop_context (pfile
);
208 /* Sweep up all tokens remaining on the line. */
210 while (_cpp_lex_token (pfile
)->type
!= CPP_EOF
)
214 /* Ensure there are no stray tokens at the end of a directive. */
216 check_eol (cpp_reader
*pfile
)
218 if (! SEEN_EOL () && _cpp_lex_token (pfile
)->type
!= CPP_EOF
)
219 cpp_error (pfile
, CPP_DL_PEDWARN
, "extra tokens at end of #%s directive",
220 pfile
->directive
->name
);
223 /* Called when entering a directive, _Pragma or command-line directive. */
225 start_directive (cpp_reader
*pfile
)
227 /* Setup in-directive state. */
228 pfile
->state
.in_directive
= 1;
229 pfile
->state
.save_comments
= 0;
230 pfile
->directive_result
.type
= CPP_PADDING
;
232 /* Some handlers need the position of the # for diagnostics. */
233 pfile
->directive_line
= pfile
->line_table
->highest_line
;
236 /* Called when leaving a directive, _Pragma or command-line directive. */
238 end_directive (cpp_reader
*pfile
, int skip_line
)
240 if (CPP_OPTION (pfile
, traditional
))
242 /* Revert change of prepare_directive_trad. */
243 pfile
->state
.prevent_expansion
--;
245 if (pfile
->directive
!= &dtable
[T_DEFINE
])
246 _cpp_remove_overlay (pfile
);
248 /* We don't skip for an assembler #. */
251 skip_rest_of_line (pfile
);
252 if (!pfile
->keep_tokens
)
254 pfile
->cur_run
= &pfile
->base_run
;
255 pfile
->cur_token
= pfile
->base_run
.base
;
260 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
261 pfile
->state
.in_directive
= 0;
262 pfile
->state
.in_expression
= 0;
263 pfile
->state
.angled_headers
= 0;
264 pfile
->directive
= 0;
267 /* Prepare to handle the directive in pfile->directive. */
269 prepare_directive_trad (cpp_reader
*pfile
)
271 if (pfile
->directive
!= &dtable
[T_DEFINE
])
273 bool no_expand
= (pfile
->directive
274 && ! (pfile
->directive
->flags
& EXPAND
));
275 bool was_skipping
= pfile
->state
.skipping
;
277 pfile
->state
.in_expression
= (pfile
->directive
== &dtable
[T_IF
]
278 || pfile
->directive
== &dtable
[T_ELIF
]);
279 if (pfile
->state
.in_expression
)
280 pfile
->state
.skipping
= false;
283 pfile
->state
.prevent_expansion
++;
284 _cpp_scan_out_logical_line (pfile
, NULL
);
286 pfile
->state
.prevent_expansion
--;
288 pfile
->state
.skipping
= was_skipping
;
289 _cpp_overlay_buffer (pfile
, pfile
->out
.base
,
290 pfile
->out
.cur
- pfile
->out
.base
);
293 /* Stop ISO C from expanding anything. */
294 pfile
->state
.prevent_expansion
++;
297 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
298 the '#' was indented. */
300 directive_diagnostics (cpp_reader
*pfile
, const directive
*dir
, int indented
)
302 /* Issue -pedantic warnings for extensions. */
303 if (CPP_PEDANTIC (pfile
)
304 && ! pfile
->state
.skipping
305 && dir
->origin
== EXTENSION
)
306 cpp_error (pfile
, CPP_DL_PEDWARN
, "#%s is a GCC extension", dir
->name
);
308 /* Traditionally, a directive is ignored unless its # is in
309 column 1. Therefore in code intended to work with K+R
310 compilers, directives added by C89 must have their #
311 indented, and directives present in traditional C must not.
312 This is true even of directives in skipped conditional
313 blocks. #elif cannot be used at all. */
314 if (CPP_WTRADITIONAL (pfile
))
316 if (dir
== &dtable
[T_ELIF
])
317 cpp_error (pfile
, CPP_DL_WARNING
,
318 "suggest not using #elif in traditional C");
319 else if (indented
&& dir
->origin
== KANDR
)
320 cpp_error (pfile
, CPP_DL_WARNING
,
321 "traditional C ignores #%s with the # indented",
323 else if (!indented
&& dir
->origin
!= KANDR
)
324 cpp_error (pfile
, CPP_DL_WARNING
,
325 "suggest hiding #%s from traditional C with an indented #",
330 /* Check if we have a known directive. INDENTED is nonzero if the
331 '#' of the directive was indented. This function is in this file
332 to save unnecessarily exporting dtable etc. to lex.c. Returns
333 nonzero if the line of tokens has been handled, zero if we should
334 continue processing the line. */
336 _cpp_handle_directive (cpp_reader
*pfile
, int indented
)
338 const directive
*dir
= 0;
339 const cpp_token
*dname
;
340 bool was_parsing_args
= pfile
->state
.parsing_args
;
341 bool was_discarding_output
= pfile
->state
.discarding_output
;
344 if (was_discarding_output
)
345 pfile
->state
.prevent_expansion
= 0;
347 if (was_parsing_args
)
349 if (CPP_OPTION (pfile
, pedantic
))
350 cpp_error (pfile
, CPP_DL_PEDWARN
,
351 "embedding a directive within macro arguments is not portable");
352 pfile
->state
.parsing_args
= 0;
353 pfile
->state
.prevent_expansion
= 0;
355 start_directive (pfile
);
356 dname
= _cpp_lex_token (pfile
);
358 if (dname
->type
== CPP_NAME
)
360 if (dname
->val
.node
->is_directive
)
361 dir
= &dtable
[dname
->val
.node
->directive_index
];
363 /* We do not recognize the # followed by a number extension in
365 else if (dname
->type
== CPP_NUMBER
&& CPP_OPTION (pfile
, lang
) != CLK_ASM
)
367 dir
= &linemarker_dir
;
368 if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, preprocessed
)
369 && ! pfile
->state
.skipping
)
370 cpp_error (pfile
, CPP_DL_PEDWARN
,
371 "style of line directive is a GCC extension");
376 /* If we have a directive that is not an opening conditional,
377 invalidate any control macro. */
378 if (! (dir
->flags
& IF_COND
))
379 pfile
->mi_valid
= false;
381 /* Kluge alert. In order to be sure that code like this
386 does not cause '#define foo bar' to get executed when
387 compiled with -save-temps, we recognize directives in
388 -fpreprocessed mode only if the # is in column 1. macro.c
389 puts a space in front of any '#' at the start of a macro. */
390 if (CPP_OPTION (pfile
, preprocessed
)
391 && (indented
|| !(dir
->flags
& IN_I
)))
398 /* In failed conditional groups, all non-conditional
399 directives are ignored. Before doing that, whether
400 skipping or not, we should lex angle-bracketed headers
401 correctly, and maybe output some diagnostics. */
402 pfile
->state
.angled_headers
= dir
->flags
& INCL
;
403 pfile
->state
.directive_wants_padding
= dir
->flags
& INCL
;
404 if (! CPP_OPTION (pfile
, preprocessed
))
405 directive_diagnostics (pfile
, dir
, indented
);
406 if (pfile
->state
.skipping
&& !(dir
->flags
& COND
))
410 else if (dname
->type
== CPP_EOF
)
411 ; /* CPP_EOF is the "null directive". */
414 /* An unknown directive. Don't complain about it in assembly
415 source: we don't know where the comments are, and # may
416 introduce assembler pseudo-ops. Don't complain about invalid
417 directives in skipped conditional groups (6.10 p4). */
418 if (CPP_OPTION (pfile
, lang
) == CLK_ASM
)
420 else if (!pfile
->state
.skipping
)
421 cpp_error (pfile
, CPP_DL_ERROR
, "invalid preprocessing directive #%s",
422 cpp_token_as_text (pfile
, dname
));
425 pfile
->directive
= dir
;
426 if (CPP_OPTION (pfile
, traditional
))
427 prepare_directive_trad (pfile
);
430 pfile
->directive
->handler (pfile
);
432 _cpp_backup_tokens (pfile
, 1);
434 end_directive (pfile
, skip
);
435 if (was_parsing_args
)
437 /* Restore state when within macro args. */
438 pfile
->state
.parsing_args
= 2;
439 pfile
->state
.prevent_expansion
= 1;
441 if (was_discarding_output
)
442 pfile
->state
.prevent_expansion
= 1;
446 /* Directive handler wrapper used by the command line option
447 processor. BUF is \n terminated. */
449 run_directive (cpp_reader
*pfile
, int dir_no
, const char *buf
, size_t count
)
451 cpp_push_buffer (pfile
, (const uchar
*) buf
, count
,
452 /* from_stage3 */ true);
453 /* Disgusting hack. */
454 if (dir_no
== T_PRAGMA
&& pfile
->buffer
->prev
)
455 pfile
->buffer
->file
= pfile
->buffer
->prev
->file
;
456 start_directive (pfile
);
458 /* This is a short-term fix to prevent a leading '#' being
459 interpreted as a directive. */
460 _cpp_clean_line (pfile
);
462 pfile
->directive
= &dtable
[dir_no
];
463 if (CPP_OPTION (pfile
, traditional
))
464 prepare_directive_trad (pfile
);
465 pfile
->directive
->handler (pfile
);
466 end_directive (pfile
, 1);
467 if (dir_no
== T_PRAGMA
)
468 pfile
->buffer
->file
= NULL
;
469 _cpp_pop_buffer (pfile
);
472 /* Checks for validity the macro name in #define, #undef, #ifdef and
473 #ifndef directives. */
474 static cpp_hashnode
*
475 lex_macro_node (cpp_reader
*pfile
)
477 const cpp_token
*token
= _cpp_lex_token (pfile
);
479 /* The token immediately after #define must be an identifier. That
480 identifier may not be "defined", per C99 6.10.8p4.
481 In C++, it may not be any of the "named operators" either,
482 per C++98 [lex.digraph], [lex.key].
483 Finally, the identifier may not have been poisoned. (In that case
484 the lexer has issued the error message for us.) */
486 if (token
->type
== CPP_NAME
)
488 cpp_hashnode
*node
= token
->val
.node
;
490 if (node
== pfile
->spec_nodes
.n_defined
)
491 cpp_error (pfile
, CPP_DL_ERROR
,
492 "\"defined\" cannot be used as a macro name");
493 else if (! (node
->flags
& NODE_POISONED
))
496 else if (token
->flags
& NAMED_OP
)
497 cpp_error (pfile
, CPP_DL_ERROR
,
498 "\"%s\" cannot be used as a macro name as it is an operator in C++",
499 NODE_NAME (token
->val
.node
));
500 else if (token
->type
== CPP_EOF
)
501 cpp_error (pfile
, CPP_DL_ERROR
, "no macro name given in #%s directive",
502 pfile
->directive
->name
);
504 cpp_error (pfile
, CPP_DL_ERROR
, "macro names must be identifiers");
509 /* Process a #define directive. Most work is done in macro.c. */
511 do_define (cpp_reader
*pfile
)
513 cpp_hashnode
*node
= lex_macro_node (pfile
);
517 /* If we have been requested to expand comments into macros,
518 then re-enable saving of comments. */
519 pfile
->state
.save_comments
=
520 ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
);
522 if (_cpp_create_definition (pfile
, node
))
523 if (pfile
->cb
.define
)
524 pfile
->cb
.define (pfile
, pfile
->directive_line
, node
);
528 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
530 do_undef (cpp_reader
*pfile
)
532 cpp_hashnode
*node
= lex_macro_node (pfile
);
537 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
539 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
540 identifier is not currently defined as a macro name. */
541 if (node
->type
== NT_MACRO
)
543 if (node
->flags
& NODE_WARN
)
544 cpp_error (pfile
, CPP_DL_WARNING
,
545 "undefining \"%s\"", NODE_NAME (node
));
547 if (CPP_OPTION (pfile
, warn_unused_macros
))
548 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
550 _cpp_free_definition (node
);
557 /* Undefine a single macro/assertion/whatever. */
560 undefine_macros (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*h
,
561 void *data_p ATTRIBUTE_UNUSED
)
563 /* Body of _cpp_free_definition inlined here for speed.
564 Macros and assertions no longer have anything to free. */
566 h
->flags
&= ~(NODE_POISONED
|NODE_BUILTIN
|NODE_DISABLED
);
570 /* Undefine all macros and assertions. */
573 cpp_undef_all (cpp_reader
*pfile
)
575 cpp_forall_identifiers (pfile
, undefine_macros
, NULL
);
579 /* Helper routine used by parse_include. Reinterpret the current line
580 as an h-char-sequence (< ... >); we are looking at the first token
581 after the <. Returns a malloced filename. */
583 glue_header_name (cpp_reader
*pfile
)
585 const cpp_token
*token
;
587 size_t len
, total_len
= 0, capacity
= 1024;
589 /* To avoid lexed tokens overwriting our glued name, we can only
590 allocate from the string pool once we've lexed everything. */
591 buffer
= XNEWVEC (char, capacity
);
594 token
= get_token_no_padding (pfile
);
596 if (token
->type
== CPP_GREATER
)
598 if (token
->type
== CPP_EOF
)
600 cpp_error (pfile
, CPP_DL_ERROR
, "missing terminating > character");
604 len
= cpp_token_len (token
) + 2; /* Leading space, terminating \0. */
605 if (total_len
+ len
> capacity
)
607 capacity
= (capacity
+ len
) * 2;
608 buffer
= XRESIZEVEC (char, buffer
, capacity
);
611 if (token
->flags
& PREV_WHITE
)
612 buffer
[total_len
++] = ' ';
614 total_len
= (cpp_spell_token (pfile
, token
, (uchar
*) &buffer
[total_len
],
619 buffer
[total_len
] = '\0';
623 /* Returns the file name of #include, #include_next, #import and
624 #pragma dependency. The string is malloced and the caller should
625 free it. Returns NULL on error. */
627 parse_include (cpp_reader
*pfile
, int *pangle_brackets
)
630 const cpp_token
*header
;
632 /* Allow macro expansion. */
633 header
= get_token_no_padding (pfile
);
634 if (header
->type
== CPP_STRING
|| header
->type
== CPP_HEADER_NAME
)
636 fname
= XNEWVEC (char, header
->val
.str
.len
- 1);
637 memcpy (fname
, header
->val
.str
.text
+ 1, header
->val
.str
.len
- 2);
638 fname
[header
->val
.str
.len
- 2] = '\0';
639 *pangle_brackets
= header
->type
== CPP_HEADER_NAME
;
641 else if (header
->type
== CPP_LESS
)
643 fname
= glue_header_name (pfile
);
644 *pangle_brackets
= 1;
648 const unsigned char *dir
;
650 if (pfile
->directive
== &dtable
[T_PRAGMA
])
651 dir
= U
"pragma dependency";
653 dir
= pfile
->directive
->name
;
654 cpp_error (pfile
, CPP_DL_ERROR
, "#%s expects \"FILENAME\" or <FILENAME>",
664 /* Handle #include, #include_next and #import. */
666 do_include_common (cpp_reader
*pfile
, enum include_type type
)
671 fname
= parse_include (pfile
, &angle_brackets
);
677 cpp_error (pfile
, CPP_DL_ERROR
, "empty filename in #%s",
678 pfile
->directive
->name
);
679 free ((void *) fname
);
683 /* Prevent #include recursion. */
684 if (pfile
->line_table
->depth
>= CPP_STACK_MAX
)
685 cpp_error (pfile
, CPP_DL_ERROR
, "#include nested too deeply");
688 /* Get out of macro context, if we are. */
689 skip_rest_of_line (pfile
);
691 if (pfile
->cb
.include
)
692 pfile
->cb
.include (pfile
, pfile
->directive_line
,
693 pfile
->directive
->name
, fname
, angle_brackets
);
695 _cpp_stack_include (pfile
, fname
, angle_brackets
, type
);
698 free ((void *) fname
);
702 do_include (cpp_reader
*pfile
)
704 do_include_common (pfile
, IT_INCLUDE
);
708 do_import (cpp_reader
*pfile
)
710 do_include_common (pfile
, IT_IMPORT
);
714 do_include_next (cpp_reader
*pfile
)
716 enum include_type type
= IT_INCLUDE_NEXT
;
718 /* If this is the primary source file, warn and use the normal
720 if (! pfile
->buffer
->prev
)
722 cpp_error (pfile
, CPP_DL_WARNING
,
723 "#include_next in primary source file");
726 do_include_common (pfile
, type
);
729 /* Subroutine of do_linemarker. Read possible flags after file name.
730 LAST is the last flag seen; 0 if this is the first flag. Return the
731 flag if it is valid, 0 at the end of the directive. Otherwise
734 read_flag (cpp_reader
*pfile
, unsigned int 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
, CPP_DL_ERROR
, "invalid flag \"%s\" in line directive",
750 cpp_token_as_text (pfile
, token
));
754 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
755 of length LEN, to binary; store it in NUMP, and return 0 if the
756 number was well-formed, 1 if not. Temporary, hopefully. */
758 strtoul_for_line (const uchar
*str
, unsigned int len
, long unsigned int *nump
)
760 unsigned long reg
= 0;
774 /* Interpret #line command.
775 Note that the filename string (if any) is a true string constant
776 (escapes are interpreted), unlike in #line. */
778 do_line (cpp_reader
*pfile
)
780 const struct line_maps
*line_table
= pfile
->line_table
;
781 const struct line_map
*map
= &line_table
->maps
[line_table
->used
- 1];
783 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
786 unsigned char map_sysp
= map
->sysp
;
787 const cpp_token
*token
;
788 const char *new_file
= map
->to_file
;
789 unsigned long new_lineno
;
791 /* C99 raised the minimum limit on #line numbers. */
792 unsigned int cap
= CPP_OPTION (pfile
, c99
) ? 2147483647 : 32767;
794 /* #line commands expand macros. */
795 token
= cpp_get_token (pfile
);
796 if (token
->type
!= CPP_NUMBER
797 || strtoul_for_line (token
->val
.str
.text
, token
->val
.str
.len
,
800 cpp_error (pfile
, CPP_DL_ERROR
,
801 "\"%s\" after #line is not a positive integer",
802 cpp_token_as_text (pfile
, token
));
806 if (CPP_PEDANTIC (pfile
) && (new_lineno
== 0 || new_lineno
> cap
))
807 cpp_error (pfile
, CPP_DL_PEDWARN
, "line number out of range");
809 token
= cpp_get_token (pfile
);
810 if (token
->type
== CPP_STRING
)
812 cpp_string s
= { 0, 0 };
813 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
, 1,
815 new_file
= (const char *)s
.text
;
818 else if (token
->type
!= CPP_EOF
)
820 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
821 cpp_token_as_text (pfile
, token
));
825 skip_rest_of_line (pfile
);
826 _cpp_do_file_change (pfile
, LC_RENAME
, new_file
, new_lineno
,
830 /* Interpret the # 44 "file" [flags] notation, which has slightly
831 different syntax and semantics from #line: Flags are allowed,
832 and we never complain about the line number being too big. */
834 do_linemarker (cpp_reader
*pfile
)
836 const struct line_maps
*line_table
= pfile
->line_table
;
837 const struct line_map
*map
= &line_table
->maps
[line_table
->used
- 1];
838 const cpp_token
*token
;
839 const char *new_file
= map
->to_file
;
840 unsigned long new_lineno
;
841 unsigned int new_sysp
= map
->sysp
;
842 enum lc_reason reason
= LC_RENAME
;
845 /* Back up so we can get the number again. Putting this in
846 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
847 some circumstances, which can segfault. */
848 _cpp_backup_tokens (pfile
, 1);
850 /* #line commands expand macros. */
851 token
= cpp_get_token (pfile
);
852 if (token
->type
!= CPP_NUMBER
853 || strtoul_for_line (token
->val
.str
.text
, token
->val
.str
.len
,
856 cpp_error (pfile
, CPP_DL_ERROR
,
857 "\"%s\" after # is not a positive integer",
858 cpp_token_as_text (pfile
, token
));
862 token
= cpp_get_token (pfile
);
863 if (token
->type
== CPP_STRING
)
865 cpp_string s
= { 0, 0 };
866 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
,
868 new_file
= (const char *)s
.text
;
871 flag
= read_flag (pfile
, 0);
875 /* Fake an include for cpp_included (). */
876 _cpp_fake_include (pfile
, new_file
);
877 flag
= read_flag (pfile
, flag
);
882 flag
= read_flag (pfile
, flag
);
887 flag
= read_flag (pfile
, flag
);
890 pfile
->buffer
->sysp
= new_sysp
;
895 else if (token
->type
!= CPP_EOF
)
897 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
898 cpp_token_as_text (pfile
, token
));
902 skip_rest_of_line (pfile
);
903 _cpp_do_file_change (pfile
, reason
, new_file
, new_lineno
, new_sysp
);
906 /* Arrange the file_change callback. pfile->line has changed to
907 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
908 header, 2 for a system header that needs to be extern "C" protected,
909 and zero otherwise. */
911 _cpp_do_file_change (cpp_reader
*pfile
, enum lc_reason reason
,
912 const char *to_file
, unsigned int file_line
,
915 const struct line_map
*map
= linemap_add (pfile
->line_table
, reason
, sysp
,
918 linemap_line_start (pfile
->line_table
, map
->to_line
, 127);
920 if (pfile
->cb
.file_change
)
921 pfile
->cb
.file_change (pfile
, map
);
924 /* Report a warning or error detected by the program we are
925 processing. Use the directive's tokens in the error message. */
927 do_diagnostic (cpp_reader
*pfile
, int code
, int print_dir
)
929 if (_cpp_begin_message (pfile
, code
, pfile
->cur_token
[-1].src_loc
, 0))
932 fprintf (stderr
, "#%s ", pfile
->directive
->name
);
933 pfile
->state
.prevent_expansion
++;
934 cpp_output_line (pfile
, stderr
);
935 pfile
->state
.prevent_expansion
--;
940 do_error (cpp_reader
*pfile
)
942 do_diagnostic (pfile
, CPP_DL_ERROR
, 1);
946 do_warning (cpp_reader
*pfile
)
948 /* We want #warning diagnostics to be emitted in system headers too. */
949 do_diagnostic (pfile
, CPP_DL_WARNING_SYSHDR
, 1);
952 /* Report program identification. */
954 do_ident (cpp_reader
*pfile
)
956 const cpp_token
*str
= cpp_get_token (pfile
);
958 if (str
->type
!= CPP_STRING
)
959 cpp_error (pfile
, CPP_DL_ERROR
, "invalid #%s directive",
960 pfile
->directive
->name
);
961 else if (pfile
->cb
.ident
)
962 pfile
->cb
.ident (pfile
, pfile
->directive_line
, &str
->val
.str
);
967 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
968 matching entry, or NULL if none is found. The returned entry could
969 be the start of a namespace chain, or a pragma. */
970 static struct pragma_entry
*
971 lookup_pragma_entry (struct pragma_entry
*chain
, const cpp_hashnode
*pragma
)
973 while (chain
&& chain
->pragma
!= pragma
)
979 /* Create and insert a pragma entry for NAME at the beginning of a
980 singly-linked CHAIN. If handler is NULL, it is a namespace,
981 otherwise it is a pragma and its handler. If INTERNAL is true
982 this pragma is being inserted by libcpp itself. */
983 static struct pragma_entry
*
984 insert_pragma_entry (cpp_reader
*pfile
, struct pragma_entry
**chain
,
985 const cpp_hashnode
*pragma
, pragma_cb handler
,
986 bool allow_expansion
, bool internal
)
988 struct pragma_entry
*new_entry
;
990 new_entry
= (struct pragma_entry
*)
991 _cpp_aligned_alloc (pfile
, sizeof (struct pragma_entry
));
992 new_entry
->pragma
= pragma
;
995 new_entry
->is_nspace
= 0;
996 new_entry
->u
.handler
= handler
;
1000 new_entry
->is_nspace
= 1;
1001 new_entry
->u
.space
= NULL
;
1004 new_entry
->allow_expansion
= allow_expansion
;
1005 new_entry
->is_internal
= internal
;
1006 new_entry
->next
= *chain
;
1011 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1012 goes in the global namespace. HANDLER is the handler it will call,
1013 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1014 expansion while parsing pragma NAME. INTERNAL is true if this is a
1015 pragma registered by cpplib itself, false if it is registered via
1016 cpp_register_pragma */
1018 register_pragma (cpp_reader
*pfile
, const char *space
, const char *name
,
1019 pragma_cb handler
, bool allow_expansion
, bool internal
)
1021 struct pragma_entry
**chain
= &pfile
->pragmas
;
1022 struct pragma_entry
*entry
;
1023 const cpp_hashnode
*node
;
1030 node
= cpp_lookup (pfile
, U space
, strlen (space
));
1031 entry
= lookup_pragma_entry (*chain
, node
);
1033 entry
= insert_pragma_entry (pfile
, chain
, node
, NULL
,
1034 allow_expansion
, internal
);
1035 else if (!entry
->is_nspace
)
1037 chain
= &entry
->u
.space
;
1040 /* Check for duplicates. */
1041 node
= cpp_lookup (pfile
, U name
, strlen (name
));
1042 entry
= lookup_pragma_entry (*chain
, node
);
1045 if (entry
->is_nspace
)
1047 cpp_error (pfile
, CPP_DL_ICE
,
1048 "registering \"%s\" as both a pragma and a pragma namespace",
1051 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s %s is already registered",
1054 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s is already registered", name
);
1057 insert_pragma_entry (pfile
, chain
, node
, handler
, allow_expansion
,
1061 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1062 goes in the global namespace. HANDLER is the handler it will call,
1063 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1064 expansion while parsing pragma NAME. This function is exported
1067 cpp_register_pragma (cpp_reader
*pfile
, const char *space
, const char *name
,
1068 pragma_cb handler
, bool allow_expansion
)
1070 register_pragma (pfile
, space
, name
, handler
, allow_expansion
, false);
1073 /* Register the pragmas the preprocessor itself handles. */
1075 _cpp_init_internal_pragmas (cpp_reader
*pfile
)
1077 /* Pragmas in the global namespace. */
1078 register_pragma (pfile
, 0, "once", do_pragma_once
, false, true);
1080 /* New GCC-specific pragmas should be put in the GCC namespace. */
1081 register_pragma (pfile
, "GCC", "poison", do_pragma_poison
, false, true);
1082 register_pragma (pfile
, "GCC", "system_header", do_pragma_system_header
,
1084 register_pragma (pfile
, "GCC", "dependency", do_pragma_dependency
,
1088 /* Return the number of registered pragmas in PE. */
1091 count_registered_pragmas (struct pragma_entry
*pe
)
1094 for (; pe
!= NULL
; pe
= pe
->next
)
1097 ct
+= count_registered_pragmas (pe
->u
.space
);
1103 /* Save into SD the names of the registered pragmas referenced by PE,
1104 and return a pointer to the next free space in SD. */
1107 save_registered_pragmas (struct pragma_entry
*pe
, char **sd
)
1109 for (; pe
!= NULL
; pe
= pe
->next
)
1112 sd
= save_registered_pragmas (pe
->u
.space
, sd
);
1113 *sd
++ = (char *) xmemdup (HT_STR (&pe
->pragma
->ident
),
1114 HT_LEN (&pe
->pragma
->ident
),
1115 HT_LEN (&pe
->pragma
->ident
) + 1);
1120 /* Return a newly-allocated array which saves the names of the
1121 registered pragmas. */
1124 _cpp_save_pragma_names (cpp_reader
*pfile
)
1126 int ct
= count_registered_pragmas (pfile
->pragmas
);
1127 char **result
= XNEWVEC (char *, ct
);
1128 (void) save_registered_pragmas (pfile
->pragmas
, result
);
1132 /* Restore from SD the names of the registered pragmas referenced by PE,
1133 and return a pointer to the next unused name in SD. */
1136 restore_registered_pragmas (cpp_reader
*pfile
, struct pragma_entry
*pe
,
1139 for (; pe
!= NULL
; pe
= pe
->next
)
1142 sd
= restore_registered_pragmas (pfile
, pe
->u
.space
, sd
);
1143 pe
->pragma
= cpp_lookup (pfile
, U
*sd
, strlen (*sd
));
1150 /* Restore the names of the registered pragmas from SAVED. */
1153 _cpp_restore_pragma_names (cpp_reader
*pfile
, char **saved
)
1155 (void) restore_registered_pragmas (pfile
, pfile
->pragmas
, saved
);
1159 /* Pragmata handling. We handle some, and pass the rest on to the
1160 front end. C99 defines three pragmas and says that no macro
1161 expansion is to be performed on them; whether or not macro
1162 expansion happens for other pragmas is implementation defined.
1163 This implementation never macro-expands the text after #pragma.
1165 The library user has the option of deferring execution of
1166 #pragmas not handled by cpplib, in which case they are converted
1167 to CPP_PRAGMA tokens and inserted into the output stream. */
1169 do_pragma (cpp_reader
*pfile
)
1171 const struct pragma_entry
*p
= NULL
;
1172 const cpp_token
*token
, *pragma_token
= pfile
->cur_token
;
1173 unsigned int count
= 1;
1175 /* Save the current position so that defer_pragmas mode can
1176 copy the entire current line to a string. It will not work
1177 to use _cpp_backup_tokens as that does not reverse buffer->cur. */
1178 const uchar
*line_start
= CPP_BUFFER (pfile
)->cur
;
1180 pfile
->state
.prevent_expansion
++;
1182 token
= cpp_get_token (pfile
);
1183 if (token
->type
== CPP_NAME
)
1185 p
= lookup_pragma_entry (pfile
->pragmas
, token
->val
.node
);
1186 if (p
&& p
->is_nspace
)
1189 token
= cpp_get_token (pfile
);
1190 if (token
->type
== CPP_NAME
)
1191 p
= lookup_pragma_entry (p
->u
.space
, token
->val
.node
);
1199 if (p
->is_internal
|| !CPP_OPTION (pfile
, defer_pragmas
))
1201 /* Since the handler below doesn't get the line number, that it
1202 might need for diagnostics, make sure it has the right
1203 numbers in place. */
1204 if (pfile
->cb
.line_change
)
1205 (*pfile
->cb
.line_change
) (pfile
, pragma_token
, false);
1206 /* Never expand macros if handling a deferred pragma, since
1207 the macro definitions now applicable may be different
1208 from those at the point the pragma appeared. */
1209 if (p
->allow_expansion
&& !pfile
->state
.in_deferred_pragma
)
1210 pfile
->state
.prevent_expansion
--;
1211 (*p
->u
.handler
) (pfile
);
1212 if (p
->allow_expansion
&& !pfile
->state
.in_deferred_pragma
)
1213 pfile
->state
.prevent_expansion
++;
1217 /* Squirrel away the pragma text. Pragmas are
1218 newline-terminated. */
1219 const uchar
*line_end
;
1224 line_end
= ustrchr (line_start
, '\n');
1226 body
.len
= (line_end
- line_start
) + 1;
1227 s
= _cpp_unaligned_alloc (pfile
, body
.len
+ 1);
1228 memcpy (s
, line_start
, body
.len
);
1232 /* Create a CPP_PRAGMA token. */
1233 ptok
= &pfile
->directive_result
;
1234 ptok
->src_loc
= pragma_token
->src_loc
;
1235 ptok
->type
= CPP_PRAGMA
;
1236 ptok
->flags
= pragma_token
->flags
| NO_EXPAND
;
1237 ptok
->val
.str
= body
;
1240 else if (pfile
->cb
.def_pragma
)
1242 _cpp_backup_tokens (pfile
, count
);
1243 pfile
->cb
.def_pragma (pfile
, pfile
->directive_line
);
1246 pfile
->state
.prevent_expansion
--;
1249 /* Handle #pragma once. */
1251 do_pragma_once (cpp_reader
*pfile
)
1253 if (pfile
->buffer
->prev
== NULL
)
1254 cpp_error (pfile
, CPP_DL_WARNING
, "#pragma once in main file");
1257 _cpp_mark_file_once_only (pfile
, pfile
->buffer
->file
);
1260 /* Handle #pragma GCC poison, to poison one or more identifiers so
1261 that the lexer produces a hard error for each subsequent usage. */
1263 do_pragma_poison (cpp_reader
*pfile
)
1265 const cpp_token
*tok
;
1268 pfile
->state
.poisoned_ok
= 1;
1271 tok
= _cpp_lex_token (pfile
);
1272 if (tok
->type
== CPP_EOF
)
1274 if (tok
->type
!= CPP_NAME
)
1276 cpp_error (pfile
, CPP_DL_ERROR
,
1277 "invalid #pragma GCC poison directive");
1282 if (hp
->flags
& NODE_POISONED
)
1285 if (hp
->type
== NT_MACRO
)
1286 cpp_error (pfile
, CPP_DL_WARNING
, "poisoning existing macro \"%s\"",
1288 _cpp_free_definition (hp
);
1289 hp
->flags
|= NODE_POISONED
| NODE_DIAGNOSTIC
;
1291 pfile
->state
.poisoned_ok
= 0;
1294 /* Mark the current header as a system header. This will suppress
1295 some categories of warnings (notably those from -pedantic). It is
1296 intended for use in system libraries that cannot be implemented in
1297 conforming C, but cannot be certain that their headers appear in a
1298 system include directory. To prevent abuse, it is rejected in the
1299 primary source file. */
1301 do_pragma_system_header (cpp_reader
*pfile
)
1303 cpp_buffer
*buffer
= pfile
->buffer
;
1305 if (buffer
->prev
== 0)
1306 cpp_error (pfile
, CPP_DL_WARNING
,
1307 "#pragma system_header ignored outside include file");
1311 skip_rest_of_line (pfile
);
1312 cpp_make_system_header (pfile
, 1, 0);
1316 /* Check the modified date of the current include file against a specified
1317 file. Issue a diagnostic, if the specified file is newer. We use this to
1318 determine if a fixed header should be refixed. */
1320 do_pragma_dependency (cpp_reader
*pfile
)
1323 int angle_brackets
, ordering
;
1325 fname
= parse_include (pfile
, &angle_brackets
);
1329 ordering
= _cpp_compare_file_date (pfile
, fname
, angle_brackets
);
1331 cpp_error (pfile
, CPP_DL_WARNING
, "cannot find source file %s", fname
);
1332 else if (ordering
> 0)
1334 cpp_error (pfile
, CPP_DL_WARNING
,
1335 "current file is older than %s", fname
);
1336 if (cpp_get_token (pfile
)->type
!= CPP_EOF
)
1338 _cpp_backup_tokens (pfile
, 1);
1339 do_diagnostic (pfile
, CPP_DL_WARNING
, 0);
1343 free ((void *) fname
);
1346 /* Get a token but skip padding. */
1347 static const cpp_token
*
1348 get_token_no_padding (cpp_reader
*pfile
)
1352 const cpp_token
*result
= cpp_get_token (pfile
);
1353 if (result
->type
!= CPP_PADDING
)
1358 /* Check syntax is "(string-literal)". Returns the string on success,
1359 or NULL on failure. */
1360 static const cpp_token
*
1361 get__Pragma_string (cpp_reader
*pfile
)
1363 const cpp_token
*string
;
1365 if (get_token_no_padding (pfile
)->type
!= CPP_OPEN_PAREN
)
1368 string
= get_token_no_padding (pfile
);
1369 if (string
->type
!= CPP_STRING
&& string
->type
!= CPP_WSTRING
)
1372 if (get_token_no_padding (pfile
)->type
!= CPP_CLOSE_PAREN
)
1378 /* Destringize IN into a temporary buffer, by removing the first \ of
1379 \" and \\ sequences, and process the result as a #pragma directive. */
1381 destringize_and_run (cpp_reader
*pfile
, const cpp_string
*in
)
1383 const unsigned char *src
, *limit
;
1384 char *dest
, *result
;
1386 dest
= result
= (char *) alloca (in
->len
- 1);
1387 src
= in
->text
+ 1 + (in
->text
[0] == 'L');
1388 limit
= in
->text
+ in
->len
- 1;
1391 /* We know there is a character following the backslash. */
1392 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1398 /* Ugh; an awful kludge. We are really not set up to be lexing
1399 tokens when in the middle of a macro expansion. Use a new
1400 context to force cpp_get_token to lex, and so skip_rest_of_line
1401 doesn't go beyond the end of the text. Also, remember the
1402 current lexing position so we can return to it later.
1404 Something like line-at-a-time lexing should remove the need for
1407 cpp_context
*saved_context
= pfile
->context
;
1408 cpp_token
*saved_cur_token
= pfile
->cur_token
;
1409 tokenrun
*saved_cur_run
= pfile
->cur_run
;
1411 pfile
->context
= XNEW (cpp_context
);
1412 pfile
->context
->macro
= 0;
1413 pfile
->context
->prev
= 0;
1414 run_directive (pfile
, T_PRAGMA
, result
, dest
- result
);
1415 XDELETE (pfile
->context
);
1416 pfile
->context
= saved_context
;
1417 pfile
->cur_token
= saved_cur_token
;
1418 pfile
->cur_run
= saved_cur_run
;
1421 /* See above comment. For the moment, we'd like
1423 token1 _Pragma ("foo") token2
1433 Getting the line markers is a little tricky. */
1434 if (pfile
->cb
.line_change
)
1435 pfile
->cb
.line_change (pfile
, pfile
->cur_token
, false);
1438 /* Handle the _Pragma operator. */
1440 _cpp_do__Pragma (cpp_reader
*pfile
)
1442 const cpp_token
*string
= get__Pragma_string (pfile
);
1443 pfile
->directive_result
.type
= CPP_PADDING
;
1446 destringize_and_run (pfile
, &string
->val
.str
);
1448 cpp_error (pfile
, CPP_DL_ERROR
,
1449 "_Pragma takes a parenthesized string literal");
1452 /* Handle a pragma that the front end deferred until now. */
1454 cpp_handle_deferred_pragma (cpp_reader
*pfile
, const cpp_string
*s
)
1456 cpp_context
*saved_context
= pfile
->context
;
1457 cpp_token
*saved_cur_token
= pfile
->cur_token
;
1458 tokenrun
*saved_cur_run
= pfile
->cur_run
;
1459 bool saved_defer_pragmas
= CPP_OPTION (pfile
, defer_pragmas
);
1460 void (*saved_line_change
) (cpp_reader
*, const cpp_token
*, int)
1461 = pfile
->cb
.line_change
;
1463 pfile
->context
= XNEW (cpp_context
);
1464 pfile
->context
->macro
= 0;
1465 pfile
->context
->prev
= 0;
1466 pfile
->cb
.line_change
= NULL
;
1467 pfile
->state
.in_deferred_pragma
= true;
1468 CPP_OPTION (pfile
, defer_pragmas
) = false;
1470 run_directive (pfile
, T_PRAGMA
, (const char *)s
->text
, s
->len
);
1472 XDELETE (pfile
->context
);
1473 pfile
->context
= saved_context
;
1474 pfile
->cur_token
= saved_cur_token
;
1475 pfile
->cur_run
= saved_cur_run
;
1476 pfile
->cb
.line_change
= saved_line_change
;
1477 pfile
->state
.in_deferred_pragma
= false;
1478 CPP_OPTION (pfile
, defer_pragmas
) = saved_defer_pragmas
;
1481 /* Handle #ifdef. */
1483 do_ifdef (cpp_reader
*pfile
)
1487 if (! pfile
->state
.skipping
)
1489 const cpp_hashnode
*node
= lex_macro_node (pfile
);
1493 skip
= node
->type
!= NT_MACRO
;
1494 _cpp_mark_macro_used (node
);
1499 push_conditional (pfile
, skip
, T_IFDEF
, 0);
1502 /* Handle #ifndef. */
1504 do_ifndef (cpp_reader
*pfile
)
1507 const cpp_hashnode
*node
= 0;
1509 if (! pfile
->state
.skipping
)
1511 node
= lex_macro_node (pfile
);
1515 skip
= node
->type
== NT_MACRO
;
1516 _cpp_mark_macro_used (node
);
1521 push_conditional (pfile
, skip
, T_IFNDEF
, node
);
1524 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1525 pfile->mi_ind_cmacro so we can handle multiple-include
1526 optimizations. If macro expansion occurs in the expression, we
1527 cannot treat it as a controlling conditional, since the expansion
1528 could change in the future. That is handled by cpp_get_token. */
1530 do_if (cpp_reader
*pfile
)
1534 if (! pfile
->state
.skipping
)
1535 skip
= _cpp_parse_expr (pfile
) == false;
1537 push_conditional (pfile
, skip
, T_IF
, pfile
->mi_ind_cmacro
);
1540 /* Flip skipping state if appropriate and continue without changing
1541 if_stack; this is so that the error message for missing #endif's
1542 etc. will point to the original #if. */
1544 do_else (cpp_reader
*pfile
)
1546 cpp_buffer
*buffer
= pfile
->buffer
;
1547 struct if_stack
*ifs
= buffer
->if_stack
;
1550 cpp_error (pfile
, CPP_DL_ERROR
, "#else without #if");
1553 if (ifs
->type
== T_ELSE
)
1555 cpp_error (pfile
, CPP_DL_ERROR
, "#else after #else");
1556 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
1557 "the conditional began here");
1561 /* Skip any future (erroneous) #elses or #elifs. */
1562 pfile
->state
.skipping
= ifs
->skip_elses
;
1563 ifs
->skip_elses
= true;
1565 /* Invalidate any controlling macro. */
1568 /* Only check EOL if was not originally skipping. */
1569 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1574 /* Handle a #elif directive by not changing if_stack either. See the
1575 comment above do_else. */
1577 do_elif (cpp_reader
*pfile
)
1579 cpp_buffer
*buffer
= pfile
->buffer
;
1580 struct if_stack
*ifs
= buffer
->if_stack
;
1583 cpp_error (pfile
, CPP_DL_ERROR
, "#elif without #if");
1586 if (ifs
->type
== T_ELSE
)
1588 cpp_error (pfile
, CPP_DL_ERROR
, "#elif after #else");
1589 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
1590 "the conditional began here");
1594 /* Only evaluate this if we aren't skipping elses. During
1595 evaluation, set skipping to false to get lexer warnings. */
1596 if (ifs
->skip_elses
)
1597 pfile
->state
.skipping
= 1;
1600 pfile
->state
.skipping
= 0;
1601 pfile
->state
.skipping
= ! _cpp_parse_expr (pfile
);
1602 ifs
->skip_elses
= ! pfile
->state
.skipping
;
1605 /* Invalidate any controlling macro. */
1610 /* #endif pops the if stack and resets pfile->state.skipping. */
1612 do_endif (cpp_reader
*pfile
)
1614 cpp_buffer
*buffer
= pfile
->buffer
;
1615 struct if_stack
*ifs
= buffer
->if_stack
;
1618 cpp_error (pfile
, CPP_DL_ERROR
, "#endif without #if");
1621 /* Only check EOL if was not originally skipping. */
1622 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1625 /* If potential control macro, we go back outside again. */
1626 if (ifs
->next
== 0 && ifs
->mi_cmacro
)
1628 pfile
->mi_valid
= true;
1629 pfile
->mi_cmacro
= ifs
->mi_cmacro
;
1632 buffer
->if_stack
= ifs
->next
;
1633 pfile
->state
.skipping
= ifs
->was_skipping
;
1634 obstack_free (&pfile
->buffer_ob
, ifs
);
1638 /* Push an if_stack entry for a preprocessor conditional, and set
1639 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1640 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1641 we need to check here that we are at the top of the file. */
1643 push_conditional (cpp_reader
*pfile
, int skip
, int type
,
1644 const cpp_hashnode
*cmacro
)
1646 struct if_stack
*ifs
;
1647 cpp_buffer
*buffer
= pfile
->buffer
;
1649 ifs
= XOBNEW (&pfile
->buffer_ob
, struct if_stack
);
1650 ifs
->line
= pfile
->directive_line
;
1651 ifs
->next
= buffer
->if_stack
;
1652 ifs
->skip_elses
= pfile
->state
.skipping
|| !skip
;
1653 ifs
->was_skipping
= pfile
->state
.skipping
;
1655 /* This condition is effectively a test for top-of-file. */
1656 if (pfile
->mi_valid
&& pfile
->mi_cmacro
== 0)
1657 ifs
->mi_cmacro
= cmacro
;
1661 pfile
->state
.skipping
= skip
;
1662 buffer
->if_stack
= ifs
;
1665 /* Read the tokens of the answer into the macro pool, in a directive
1666 of type TYPE. Only commit the memory if we intend it as permanent
1667 storage, i.e. the #assert case. Returns 0 on success, and sets
1668 ANSWERP to point to the answer. */
1670 parse_answer (cpp_reader
*pfile
, struct answer
**answerp
, int type
)
1672 const cpp_token
*paren
;
1673 struct answer
*answer
;
1674 unsigned int acount
;
1676 /* In a conditional, it is legal to not have an open paren. We
1677 should save the following token in this case. */
1678 paren
= cpp_get_token (pfile
);
1680 /* If not a paren, see if we're OK. */
1681 if (paren
->type
!= CPP_OPEN_PAREN
)
1683 /* In a conditional no answer is a test for any answer. It
1684 could be followed by any token. */
1687 _cpp_backup_tokens (pfile
, 1);
1691 /* #unassert with no answer is valid - it removes all answers. */
1692 if (type
== T_UNASSERT
&& paren
->type
== CPP_EOF
)
1695 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' after predicate");
1699 for (acount
= 0;; acount
++)
1702 const cpp_token
*token
= cpp_get_token (pfile
);
1705 if (token
->type
== CPP_CLOSE_PAREN
)
1708 if (token
->type
== CPP_EOF
)
1710 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' to complete answer");
1714 /* struct answer includes the space for one token. */
1715 room_needed
= (sizeof (struct answer
) + acount
* sizeof (cpp_token
));
1717 if (BUFF_ROOM (pfile
->a_buff
) < room_needed
)
1718 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (struct answer
));
1720 dest
= &((struct answer
*) BUFF_FRONT (pfile
->a_buff
))->first
[acount
];
1723 /* Drop whitespace at start, for answer equivalence purposes. */
1725 dest
->flags
&= ~PREV_WHITE
;
1730 cpp_error (pfile
, CPP_DL_ERROR
, "predicate's answer is empty");
1734 answer
= (struct answer
*) BUFF_FRONT (pfile
->a_buff
);
1735 answer
->count
= acount
;
1736 answer
->next
= NULL
;
1742 /* Parses an assertion directive of type TYPE, returning a pointer to
1743 the hash node of the predicate, or 0 on error. If an answer was
1744 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
1745 static cpp_hashnode
*
1746 parse_assertion (cpp_reader
*pfile
, struct answer
**answerp
, int type
)
1748 cpp_hashnode
*result
= 0;
1749 const cpp_token
*predicate
;
1751 /* We don't expand predicates or answers. */
1752 pfile
->state
.prevent_expansion
++;
1755 predicate
= cpp_get_token (pfile
);
1756 if (predicate
->type
== CPP_EOF
)
1757 cpp_error (pfile
, CPP_DL_ERROR
, "assertion without predicate");
1758 else if (predicate
->type
!= CPP_NAME
)
1759 cpp_error (pfile
, CPP_DL_ERROR
, "predicate must be an identifier");
1760 else if (parse_answer (pfile
, answerp
, type
) == 0)
1762 unsigned int len
= NODE_LEN (predicate
->val
.node
);
1763 unsigned char *sym
= (unsigned char *) alloca (len
+ 1);
1765 /* Prefix '#' to get it out of macro namespace. */
1767 memcpy (sym
+ 1, NODE_NAME (predicate
->val
.node
), len
);
1768 result
= cpp_lookup (pfile
, sym
, len
+ 1);
1771 pfile
->state
.prevent_expansion
--;
1775 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1776 or a pointer to NULL if the answer is not in the chain. */
1777 static struct answer
**
1778 find_answer (cpp_hashnode
*node
, const struct answer
*candidate
)
1781 struct answer
**result
;
1783 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->next
)
1785 struct answer
*answer
= *result
;
1787 if (answer
->count
== candidate
->count
)
1789 for (i
= 0; i
< answer
->count
; i
++)
1790 if (! _cpp_equiv_tokens (&answer
->first
[i
], &candidate
->first
[i
]))
1793 if (i
== answer
->count
)
1801 /* Test an assertion within a preprocessor conditional. Returns
1802 nonzero on failure, zero on success. On success, the result of
1803 the test is written into VALUE, otherwise the value 0. */
1805 _cpp_test_assertion (cpp_reader
*pfile
, unsigned int *value
)
1807 struct answer
*answer
;
1810 node
= parse_assertion (pfile
, &answer
, T_IF
);
1812 /* For recovery, an erroneous assertion expression is handled as a
1813 failing assertion. */
1817 *value
= (node
->type
== NT_ASSERTION
&&
1818 (answer
== 0 || *find_answer (node
, answer
) != 0));
1819 else if (pfile
->cur_token
[-1].type
== CPP_EOF
)
1820 _cpp_backup_tokens (pfile
, 1);
1822 /* We don't commit the memory for the answer - it's temporary only. */
1826 /* Handle #assert. */
1828 do_assert (cpp_reader
*pfile
)
1830 struct answer
*new_answer
;
1833 node
= parse_assertion (pfile
, &new_answer
, T_ASSERT
);
1838 /* Place the new answer in the answer list. First check there
1839 is not a duplicate. */
1840 new_answer
->next
= 0;
1841 if (node
->type
== NT_ASSERTION
)
1843 if (*find_answer (node
, new_answer
))
1845 cpp_error (pfile
, CPP_DL_WARNING
, "\"%s\" re-asserted",
1846 NODE_NAME (node
) + 1);
1849 new_answer
->next
= node
->value
.answers
;
1852 answer_size
= sizeof (struct answer
) + ((new_answer
->count
- 1)
1853 * sizeof (cpp_token
));
1854 /* Commit or allocate storage for the object. */
1855 if (pfile
->hash_table
->alloc_subobject
)
1857 struct answer
*temp_answer
= new_answer
;
1858 new_answer
= (struct answer
*) pfile
->hash_table
->alloc_subobject
1860 memcpy (new_answer
, temp_answer
, answer_size
);
1863 BUFF_FRONT (pfile
->a_buff
) += answer_size
;
1865 node
->type
= NT_ASSERTION
;
1866 node
->value
.answers
= new_answer
;
1871 /* Handle #unassert. */
1873 do_unassert (cpp_reader
*pfile
)
1876 struct answer
*answer
;
1878 node
= parse_assertion (pfile
, &answer
, T_UNASSERT
);
1879 /* It isn't an error to #unassert something that isn't asserted. */
1880 if (node
&& node
->type
== NT_ASSERTION
)
1884 struct answer
**p
= find_answer (node
, answer
), *temp
;
1886 /* Remove the answer from the list. */
1891 /* Did we free the last answer? */
1892 if (node
->value
.answers
== 0)
1893 node
->type
= NT_VOID
;
1898 _cpp_free_definition (node
);
1901 /* We don't commit the memory for the answer - it's temporary only. */
1904 /* These are for -D, -U, -A. */
1906 /* Process the string STR as if it appeared as the body of a #define.
1907 If STR is just an identifier, define it with value 1.
1908 If STR has anything after the identifier, then it should
1909 be identifier=definition. */
1911 cpp_define (cpp_reader
*pfile
, const char *str
)
1916 /* Copy the entire option so we can modify it.
1917 Change the first "=" in the string to a space. If there is none,
1918 tack " 1" on the end. */
1920 count
= strlen (str
);
1921 buf
= (char *) alloca (count
+ 3);
1922 memcpy (buf
, str
, count
);
1924 p
= strchr (str
, '=');
1934 run_directive (pfile
, T_DEFINE
, buf
, count
);
1937 /* Slight variant of the above for use by initialize_builtins. */
1939 _cpp_define_builtin (cpp_reader
*pfile
, const char *str
)
1941 size_t len
= strlen (str
);
1942 char *buf
= (char *) alloca (len
+ 1);
1943 memcpy (buf
, str
, len
);
1945 run_directive (pfile
, T_DEFINE
, buf
, len
);
1948 /* Process MACRO as if it appeared as the body of an #undef. */
1950 cpp_undef (cpp_reader
*pfile
, const char *macro
)
1952 size_t len
= strlen (macro
);
1953 char *buf
= (char *) alloca (len
+ 1);
1954 memcpy (buf
, macro
, len
);
1956 run_directive (pfile
, T_UNDEF
, buf
, len
);
1959 /* Process the string STR as if it appeared as the body of a #assert. */
1961 cpp_assert (cpp_reader
*pfile
, const char *str
)
1963 handle_assertion (pfile
, str
, T_ASSERT
);
1966 /* Process STR as if it appeared as the body of an #unassert. */
1968 cpp_unassert (cpp_reader
*pfile
, const char *str
)
1970 handle_assertion (pfile
, str
, T_UNASSERT
);
1973 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1975 handle_assertion (cpp_reader
*pfile
, const char *str
, int type
)
1977 size_t count
= strlen (str
);
1978 const char *p
= strchr (str
, '=');
1980 /* Copy the entire option so we can modify it. Change the first
1981 "=" in the string to a '(', and tack a ')' on the end. */
1982 char *buf
= (char *) alloca (count
+ 2);
1984 memcpy (buf
, str
, count
);
1993 run_directive (pfile
, type
, str
, count
);
1996 /* The number of errors for a given reader. */
1998 cpp_errors (cpp_reader
*pfile
)
2000 return pfile
->errors
;
2003 /* The options structure. */
2005 cpp_get_options (cpp_reader
*pfile
)
2007 return &pfile
->opts
;
2010 /* The callbacks structure. */
2012 cpp_get_callbacks (cpp_reader
*pfile
)
2017 /* Copy the given callbacks structure to our own. */
2019 cpp_set_callbacks (cpp_reader
*pfile
, cpp_callbacks
*cb
)
2024 /* The dependencies structure. (Creates one if it hasn't already been.) */
2026 cpp_get_deps (cpp_reader
*pfile
)
2029 pfile
->deps
= deps_init ();
2033 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2034 doesn't fail. It does not generate a file change call back; that
2035 is the responsibility of the caller. */
2037 cpp_push_buffer (cpp_reader
*pfile
, const uchar
*buffer
, size_t len
,
2040 cpp_buffer
*new_buffer
= XOBNEW (&pfile
->buffer_ob
, cpp_buffer
);
2042 /* Clears, amongst other things, if_stack and mi_cmacro. */
2043 memset (new_buffer
, 0, sizeof (cpp_buffer
));
2045 new_buffer
->next_line
= new_buffer
->buf
= buffer
;
2046 new_buffer
->rlimit
= buffer
+ len
;
2047 new_buffer
->from_stage3
= from_stage3
;
2048 new_buffer
->prev
= pfile
->buffer
;
2049 new_buffer
->need_line
= true;
2051 pfile
->buffer
= new_buffer
;
2056 /* Pops a single buffer, with a file change call-back if appropriate.
2057 Then pushes the next -include file, if any remain. */
2059 _cpp_pop_buffer (cpp_reader
*pfile
)
2061 cpp_buffer
*buffer
= pfile
->buffer
;
2062 struct _cpp_file
*inc
= buffer
->file
;
2063 struct if_stack
*ifs
;
2065 /* Walk back up the conditional stack till we reach its level at
2066 entry to this file, issuing error messages. */
2067 for (ifs
= buffer
->if_stack
; ifs
; ifs
= ifs
->next
)
2068 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2069 "unterminated #%s", dtable
[ifs
->type
].name
);
2071 /* In case of a missing #endif. */
2072 pfile
->state
.skipping
= 0;
2074 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2075 pfile
->buffer
= buffer
->prev
;
2077 free (buffer
->notes
);
2079 /* Free the buffer object now; we may want to push a new buffer
2080 in _cpp_push_next_include_file. */
2081 obstack_free (&pfile
->buffer_ob
, buffer
);
2085 _cpp_pop_file_buffer (pfile
, inc
);
2087 _cpp_do_file_change (pfile
, LC_LEAVE
, 0, 0, 0);
2091 /* Enter all recognized directives in the hash table. */
2093 _cpp_init_directives (cpp_reader
*pfile
)
2098 for (i
= 0; i
< (unsigned int) N_DIRECTIVES
; i
++)
2100 node
= cpp_lookup (pfile
, dtable
[i
].name
, dtable
[i
].length
);
2101 node
->is_directive
= 1;
2102 node
->directive_index
= i
;