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,
4 2007, 2008, 2009 Free Software Foundation, Inc.
5 Contributed by Per Bothner, 1994-95.
6 Based on CCCP program by Paul Rubin, June 1986
7 Adapted to ANSI C, Richard Stallman, Jan 1987
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3, or (at your option) any
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
30 /* Stack of conditionals currently in progress
31 (including both successful and failing conditionals). */
34 struct if_stack
*next
;
35 linenum_type line
; /* Line where condition started. */
36 const cpp_hashnode
*mi_cmacro
;/* macro name for #ifndef around entire file */
37 bool skip_elses
; /* Can future #else / #elif be skipped? */
38 bool was_skipping
; /* If were skipping on entry. */
39 int type
; /* Most recent conditional for diagnostics. */
42 /* Contains a registered pragma or pragma namespace. */
43 typedef void (*pragma_cb
) (cpp_reader
*);
46 struct pragma_entry
*next
;
47 const cpp_hashnode
*pragma
; /* Name and length. */
54 struct pragma_entry
*space
;
59 /* Values for the origin field of struct directive. KANDR directives
60 come from traditional (K&R) C. STDC89 directives come from the
61 1989 C standard. EXTENSION directives are extensions. */
66 /* Values for the flags field of struct directive. COND indicates a
67 conditional; IF_COND an opening conditional. INCL means to treat
68 "..." and <...> as q-char and h-char sequences respectively. IN_I
69 means this directive should be handled even if -fpreprocessed is in
70 effect (these are the directives with callback hooks).
72 EXPAND is set on directives that are always macro-expanded. */
74 #define IF_COND (1 << 1)
77 #define EXPAND (1 << 4)
78 #define DEPRECATED (1 << 5)
80 /* Defines one #-directive, including how to handle it. */
81 typedef void (*directive_handler
) (cpp_reader
*);
82 typedef struct directive directive
;
85 directive_handler handler
; /* Function to handle directive. */
86 const uchar
*name
; /* Name of directive. */
87 unsigned short length
; /* Length of name. */
88 unsigned char origin
; /* Origin of directive. */
89 unsigned char flags
; /* Flags describing this directive. */
92 /* Forward declarations. */
94 static void skip_rest_of_line (cpp_reader
*);
95 static void check_eol (cpp_reader
*, bool);
96 static void start_directive (cpp_reader
*);
97 static void prepare_directive_trad (cpp_reader
*);
98 static void end_directive (cpp_reader
*, int);
99 static void directive_diagnostics (cpp_reader
*, const directive
*, int);
100 static void run_directive (cpp_reader
*, int, const char *, size_t);
101 static char *glue_header_name (cpp_reader
*);
102 static const char *parse_include (cpp_reader
*, int *, const cpp_token
***);
103 static void push_conditional (cpp_reader
*, int, int, const cpp_hashnode
*);
104 static unsigned int read_flag (cpp_reader
*, unsigned int);
105 static bool strtolinenum (const uchar
*, size_t, linenum_type
*, bool *);
106 static void do_diagnostic (cpp_reader
*, int, int);
107 static cpp_hashnode
*lex_macro_node (cpp_reader
*, bool);
108 static int undefine_macros (cpp_reader
*, cpp_hashnode
*, void *);
109 static void do_include_common (cpp_reader
*, enum include_type
);
110 static struct pragma_entry
*lookup_pragma_entry (struct pragma_entry
*,
111 const cpp_hashnode
*);
112 static int count_registered_pragmas (struct pragma_entry
*);
113 static char ** save_registered_pragmas (struct pragma_entry
*, char **);
114 static char ** restore_registered_pragmas (cpp_reader
*, struct pragma_entry
*,
116 static void do_pragma_once (cpp_reader
*);
117 static void do_pragma_poison (cpp_reader
*);
118 static void do_pragma_system_header (cpp_reader
*);
119 static void do_pragma_dependency (cpp_reader
*);
120 static void do_linemarker (cpp_reader
*);
121 static const cpp_token
*get_token_no_padding (cpp_reader
*);
122 static const cpp_token
*get__Pragma_string (cpp_reader
*);
123 static void destringize_and_run (cpp_reader
*, const cpp_string
*);
124 static int parse_answer (cpp_reader
*, struct answer
**, int);
125 static cpp_hashnode
*parse_assertion (cpp_reader
*, struct answer
**, int);
126 static struct answer
** find_answer (cpp_hashnode
*, const struct answer
*);
127 static void handle_assertion (cpp_reader
*, const char *, int);
129 /* This is the table of directive handlers. It is ordered by
130 frequency of occurrence; the numbers at the end are directive
131 counts from all the source code I have lying around (egcs and libc
132 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
133 pcmcia-cs-3.0.9). This is no longer important as directive lookup
134 is now O(1). All extensions other than #warning, #include_next,
135 and #import are deprecated. The name is where the extension
136 appears to have come from. */
138 #define DIRECTIVE_TABLE \
139 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
140 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
141 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
142 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
143 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
144 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
145 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
146 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
147 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
148 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
149 D(error, T_ERROR, STDC89, 0) /* 475 */ \
150 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
151 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
152 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
153 D(ident, T_IDENT, EXTENSION, IN_I | DEPRECATED) /* 11 */ \
154 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
155 D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
156 D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
157 D(sccs, T_SCCS, EXTENSION, IN_I | DEPRECATED) /* 0 SVR4? */
159 /* #sccs is synonymous with #ident. */
160 #define do_sccs do_ident
162 /* Use the table to generate a series of prototypes, an enum for the
163 directive names, and an array of directive handlers. */
165 #define D(name, t, o, f) static void do_##name (cpp_reader *);
169 #define D(n, tag, o, f) tag,
177 #define D(name, t, origin, flags) \
178 { do_##name, (const uchar *) #name, \
179 sizeof #name - 1, origin, flags },
180 static const directive dtable
[] =
185 #undef DIRECTIVE_TABLE
187 /* Wrapper struct directive for linemarkers.
188 The origin is more or less true - the original K+R cpp
189 did use this notation in its preprocessed output. */
190 static const directive linemarker_dir
=
192 do_linemarker
, UC
"#", 1, KANDR
, IN_I
195 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
197 /* Skip any remaining tokens in a directive. */
199 skip_rest_of_line (cpp_reader
*pfile
)
201 /* Discard all stacked contexts. */
202 while (pfile
->context
->prev
)
203 _cpp_pop_context (pfile
);
205 /* Sweep up all tokens remaining on the line. */
207 while (_cpp_lex_token (pfile
)->type
!= CPP_EOF
)
211 /* Ensure there are no stray tokens at the end of a directive. If
212 EXPAND is true, tokens macro-expanding to nothing are allowed. */
214 check_eol (cpp_reader
*pfile
, bool expand
)
216 if (! SEEN_EOL () && (expand
217 ? cpp_get_token (pfile
)
218 : _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 /* Ensure there are no stray tokens other than comments at the end of
224 a directive, and gather the comments. */
225 static const cpp_token
**
226 check_eol_return_comments (cpp_reader
*pfile
)
230 const cpp_token
**buf
;
232 buf
= XNEWVEC (const cpp_token
*, capacity
);
238 const cpp_token
*tok
;
240 tok
= _cpp_lex_token (pfile
);
241 if (tok
->type
== CPP_EOF
)
243 if (tok
->type
!= CPP_COMMENT
)
244 cpp_error (pfile
, CPP_DL_PEDWARN
,
245 "extra tokens at end of #%s directive",
246 pfile
->directive
->name
);
249 if (c
+ 1 >= capacity
)
252 buf
= XRESIZEVEC (const cpp_token
*, buf
, capacity
);
263 /* Called when entering a directive, _Pragma or command-line directive. */
265 start_directive (cpp_reader
*pfile
)
267 /* Setup in-directive state. */
268 pfile
->state
.in_directive
= 1;
269 pfile
->state
.save_comments
= 0;
270 pfile
->directive_result
.type
= CPP_PADDING
;
272 /* Some handlers need the position of the # for diagnostics. */
273 pfile
->directive_line
= pfile
->line_table
->highest_line
;
276 /* Called when leaving a directive, _Pragma or command-line directive. */
278 end_directive (cpp_reader
*pfile
, int skip_line
)
280 if (pfile
->state
.in_deferred_pragma
)
282 else if (CPP_OPTION (pfile
, traditional
))
284 /* Revert change of prepare_directive_trad. */
285 pfile
->state
.prevent_expansion
--;
287 if (pfile
->directive
!= &dtable
[T_DEFINE
])
288 _cpp_remove_overlay (pfile
);
290 /* We don't skip for an assembler #. */
293 skip_rest_of_line (pfile
);
294 if (!pfile
->keep_tokens
)
296 pfile
->cur_run
= &pfile
->base_run
;
297 pfile
->cur_token
= pfile
->base_run
.base
;
302 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
303 pfile
->state
.in_directive
= 0;
304 pfile
->state
.in_expression
= 0;
305 pfile
->state
.angled_headers
= 0;
306 pfile
->directive
= 0;
309 /* Prepare to handle the directive in pfile->directive. */
311 prepare_directive_trad (cpp_reader
*pfile
)
313 if (pfile
->directive
!= &dtable
[T_DEFINE
])
315 bool no_expand
= (pfile
->directive
316 && ! (pfile
->directive
->flags
& EXPAND
));
317 bool was_skipping
= pfile
->state
.skipping
;
319 pfile
->state
.in_expression
= (pfile
->directive
== &dtable
[T_IF
]
320 || pfile
->directive
== &dtable
[T_ELIF
]);
321 if (pfile
->state
.in_expression
)
322 pfile
->state
.skipping
= false;
325 pfile
->state
.prevent_expansion
++;
326 _cpp_scan_out_logical_line (pfile
, NULL
);
328 pfile
->state
.prevent_expansion
--;
330 pfile
->state
.skipping
= was_skipping
;
331 _cpp_overlay_buffer (pfile
, pfile
->out
.base
,
332 pfile
->out
.cur
- pfile
->out
.base
);
335 /* Stop ISO C from expanding anything. */
336 pfile
->state
.prevent_expansion
++;
339 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
340 the '#' was indented. */
342 directive_diagnostics (cpp_reader
*pfile
, const directive
*dir
, int indented
)
344 /* Issue -pedantic or deprecated warnings for extensions. We let
345 -pedantic take precedence if both are applicable. */
346 if (! pfile
->state
.skipping
)
348 if (dir
->origin
== EXTENSION
349 && !(dir
== &dtable
[T_IMPORT
] && CPP_OPTION (pfile
, objc
))
350 && CPP_PEDANTIC (pfile
))
351 cpp_error (pfile
, CPP_DL_PEDWARN
, "#%s is a GCC extension", dir
->name
);
352 else if (((dir
->flags
& DEPRECATED
) != 0
353 || (dir
== &dtable
[T_IMPORT
] && !CPP_OPTION (pfile
, objc
)))
354 && CPP_OPTION (pfile
, warn_deprecated
))
355 cpp_error (pfile
, CPP_DL_WARNING
, "#%s is a deprecated GCC extension",
359 /* Traditionally, a directive is ignored unless its # is in
360 column 1. Therefore in code intended to work with K+R
361 compilers, directives added by C89 must have their #
362 indented, and directives present in traditional C must not.
363 This is true even of directives in skipped conditional
364 blocks. #elif cannot be used at all. */
365 if (CPP_WTRADITIONAL (pfile
))
367 if (dir
== &dtable
[T_ELIF
])
368 cpp_error (pfile
, CPP_DL_WARNING
,
369 "suggest not using #elif in traditional C");
370 else if (indented
&& dir
->origin
== KANDR
)
371 cpp_error (pfile
, CPP_DL_WARNING
,
372 "traditional C ignores #%s with the # indented",
374 else if (!indented
&& dir
->origin
!= KANDR
)
375 cpp_error (pfile
, CPP_DL_WARNING
,
376 "suggest hiding #%s from traditional C with an indented #",
381 /* Check if we have a known directive. INDENTED is nonzero if the
382 '#' of the directive was indented. This function is in this file
383 to save unnecessarily exporting dtable etc. to lex.c. Returns
384 nonzero if the line of tokens has been handled, zero if we should
385 continue processing the line. */
387 _cpp_handle_directive (cpp_reader
*pfile
, int indented
)
389 const directive
*dir
= 0;
390 const cpp_token
*dname
;
391 bool was_parsing_args
= pfile
->state
.parsing_args
;
392 bool was_discarding_output
= pfile
->state
.discarding_output
;
395 if (was_discarding_output
)
396 pfile
->state
.prevent_expansion
= 0;
398 if (was_parsing_args
)
400 if (CPP_OPTION (pfile
, pedantic
))
401 cpp_error (pfile
, CPP_DL_PEDWARN
,
402 "embedding a directive within macro arguments is not portable");
403 pfile
->state
.parsing_args
= 0;
404 pfile
->state
.prevent_expansion
= 0;
406 start_directive (pfile
);
407 dname
= _cpp_lex_token (pfile
);
409 if (dname
->type
== CPP_NAME
)
411 if (dname
->val
.node
.node
->is_directive
)
412 dir
= &dtable
[dname
->val
.node
.node
->directive_index
];
414 /* We do not recognize the # followed by a number extension in
416 else if (dname
->type
== CPP_NUMBER
&& CPP_OPTION (pfile
, lang
) != CLK_ASM
)
418 dir
= &linemarker_dir
;
419 if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, preprocessed
)
420 && ! pfile
->state
.skipping
)
421 cpp_error (pfile
, CPP_DL_PEDWARN
,
422 "style of line directive is a GCC extension");
427 /* If we have a directive that is not an opening conditional,
428 invalidate any control macro. */
429 if (! (dir
->flags
& IF_COND
))
430 pfile
->mi_valid
= false;
432 /* Kluge alert. In order to be sure that code like this
437 does not cause '#define foo bar' to get executed when
438 compiled with -save-temps, we recognize directives in
439 -fpreprocessed mode only if the # is in column 1. macro.c
440 puts a space in front of any '#' at the start of a macro.
442 We exclude the -fdirectives-only case because macro expansion
443 has not been performed yet, and block comments can cause spaces
444 to preceed the directive. */
445 if (CPP_OPTION (pfile
, preprocessed
)
446 && !CPP_OPTION (pfile
, directives_only
)
447 && (indented
|| !(dir
->flags
& IN_I
)))
454 /* In failed conditional groups, all non-conditional
455 directives are ignored. Before doing that, whether
456 skipping or not, we should lex angle-bracketed headers
457 correctly, and maybe output some diagnostics. */
458 pfile
->state
.angled_headers
= dir
->flags
& INCL
;
459 pfile
->state
.directive_wants_padding
= dir
->flags
& INCL
;
460 if (! CPP_OPTION (pfile
, preprocessed
))
461 directive_diagnostics (pfile
, dir
, indented
);
462 if (pfile
->state
.skipping
&& !(dir
->flags
& COND
))
466 else if (dname
->type
== CPP_EOF
)
467 ; /* CPP_EOF is the "null directive". */
470 /* An unknown directive. Don't complain about it in assembly
471 source: we don't know where the comments are, and # may
472 introduce assembler pseudo-ops. Don't complain about invalid
473 directives in skipped conditional groups (6.10 p4). */
474 if (CPP_OPTION (pfile
, lang
) == CLK_ASM
)
476 else if (!pfile
->state
.skipping
)
477 cpp_error (pfile
, CPP_DL_ERROR
, "invalid preprocessing directive #%s",
478 cpp_token_as_text (pfile
, dname
));
481 pfile
->directive
= dir
;
482 if (CPP_OPTION (pfile
, traditional
))
483 prepare_directive_trad (pfile
);
486 pfile
->directive
->handler (pfile
);
488 _cpp_backup_tokens (pfile
, 1);
490 end_directive (pfile
, skip
);
491 if (was_parsing_args
&& !pfile
->state
.in_deferred_pragma
)
493 /* Restore state when within macro args. */
494 pfile
->state
.parsing_args
= 2;
495 pfile
->state
.prevent_expansion
= 1;
497 if (was_discarding_output
)
498 pfile
->state
.prevent_expansion
= 1;
502 /* Directive handler wrapper used by the command line option
503 processor. BUF is \n terminated. */
505 run_directive (cpp_reader
*pfile
, int dir_no
, const char *buf
, size_t count
)
507 cpp_push_buffer (pfile
, (const uchar
*) buf
, count
,
508 /* from_stage3 */ true);
509 start_directive (pfile
);
511 /* This is a short-term fix to prevent a leading '#' being
512 interpreted as a directive. */
513 _cpp_clean_line (pfile
);
515 pfile
->directive
= &dtable
[dir_no
];
516 if (CPP_OPTION (pfile
, traditional
))
517 prepare_directive_trad (pfile
);
518 pfile
->directive
->handler (pfile
);
519 end_directive (pfile
, 1);
520 _cpp_pop_buffer (pfile
);
523 /* Checks for validity the macro name in #define, #undef, #ifdef and
524 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
525 processing a #define or #undefine directive, and false
527 static cpp_hashnode
*
528 lex_macro_node (cpp_reader
*pfile
, bool is_def_or_undef
)
530 const cpp_token
*token
= _cpp_lex_token (pfile
);
532 /* The token immediately after #define must be an identifier. That
533 identifier may not be "defined", per C99 6.10.8p4.
534 In C++, it may not be any of the "named operators" either,
535 per C++98 [lex.digraph], [lex.key].
536 Finally, the identifier may not have been poisoned. (In that case
537 the lexer has issued the error message for us.) */
539 if (token
->type
== CPP_NAME
)
541 cpp_hashnode
*node
= token
->val
.node
.node
;
543 if (is_def_or_undef
&& node
== pfile
->spec_nodes
.n_defined
)
544 cpp_error (pfile
, CPP_DL_ERROR
,
545 "\"defined\" cannot be used as a macro name");
546 else if (! (node
->flags
& NODE_POISONED
))
549 else if (token
->flags
& NAMED_OP
)
550 cpp_error (pfile
, CPP_DL_ERROR
,
551 "\"%s\" cannot be used as a macro name as it is an operator in C++",
552 NODE_NAME (token
->val
.node
.node
));
553 else if (token
->type
== CPP_EOF
)
554 cpp_error (pfile
, CPP_DL_ERROR
, "no macro name given in #%s directive",
555 pfile
->directive
->name
);
557 cpp_error (pfile
, CPP_DL_ERROR
, "macro names must be identifiers");
562 /* Process a #define directive. Most work is done in macro.c. */
564 do_define (cpp_reader
*pfile
)
566 cpp_hashnode
*node
= lex_macro_node (pfile
, true);
570 /* If we have been requested to expand comments into macros,
571 then re-enable saving of comments. */
572 pfile
->state
.save_comments
=
573 ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
);
575 if (pfile
->cb
.before_define
)
576 pfile
->cb
.before_define (pfile
);
578 if (_cpp_create_definition (pfile
, node
))
579 if (pfile
->cb
.define
)
580 pfile
->cb
.define (pfile
, pfile
->directive_line
, node
);
582 node
->flags
&= ~NODE_USED
;
586 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
588 do_undef (cpp_reader
*pfile
)
590 cpp_hashnode
*node
= lex_macro_node (pfile
, true);
594 if (pfile
->cb
.before_define
)
595 pfile
->cb
.before_define (pfile
);
598 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
600 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
601 identifier is not currently defined as a macro name. */
602 if (node
->type
== NT_MACRO
)
604 if (node
->flags
& NODE_WARN
)
605 cpp_error (pfile
, CPP_DL_WARNING
,
606 "undefining \"%s\"", NODE_NAME (node
));
608 if (CPP_OPTION (pfile
, warn_unused_macros
))
609 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
611 _cpp_free_definition (node
);
615 check_eol (pfile
, false);
618 /* Undefine a single macro/assertion/whatever. */
621 undefine_macros (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*h
,
622 void *data_p ATTRIBUTE_UNUSED
)
624 /* Body of _cpp_free_definition inlined here for speed.
625 Macros and assertions no longer have anything to free. */
627 h
->flags
&= ~(NODE_POISONED
|NODE_BUILTIN
|NODE_DISABLED
|NODE_USED
);
631 /* Undefine all macros and assertions. */
634 cpp_undef_all (cpp_reader
*pfile
)
636 cpp_forall_identifiers (pfile
, undefine_macros
, NULL
);
640 /* Helper routine used by parse_include. Reinterpret the current line
641 as an h-char-sequence (< ... >); we are looking at the first token
642 after the <. Returns a malloced filename. */
644 glue_header_name (cpp_reader
*pfile
)
646 const cpp_token
*token
;
648 size_t len
, total_len
= 0, capacity
= 1024;
650 /* To avoid lexed tokens overwriting our glued name, we can only
651 allocate from the string pool once we've lexed everything. */
652 buffer
= XNEWVEC (char, capacity
);
655 token
= get_token_no_padding (pfile
);
657 if (token
->type
== CPP_GREATER
)
659 if (token
->type
== CPP_EOF
)
661 cpp_error (pfile
, CPP_DL_ERROR
, "missing terminating > character");
665 len
= cpp_token_len (token
) + 2; /* Leading space, terminating \0. */
666 if (total_len
+ len
> capacity
)
668 capacity
= (capacity
+ len
) * 2;
669 buffer
= XRESIZEVEC (char, buffer
, capacity
);
672 if (token
->flags
& PREV_WHITE
)
673 buffer
[total_len
++] = ' ';
675 total_len
= (cpp_spell_token (pfile
, token
, (uchar
*) &buffer
[total_len
],
680 buffer
[total_len
] = '\0';
684 /* Returns the file name of #include, #include_next, #import and
685 #pragma dependency. The string is malloced and the caller should
686 free it. Returns NULL on error. */
688 parse_include (cpp_reader
*pfile
, int *pangle_brackets
,
689 const cpp_token
***buf
)
692 const cpp_token
*header
;
694 /* Allow macro expansion. */
695 header
= get_token_no_padding (pfile
);
696 if (header
->type
== CPP_STRING
|| header
->type
== CPP_HEADER_NAME
)
698 fname
= XNEWVEC (char, header
->val
.str
.len
- 1);
699 memcpy (fname
, header
->val
.str
.text
+ 1, header
->val
.str
.len
- 2);
700 fname
[header
->val
.str
.len
- 2] = '\0';
701 *pangle_brackets
= header
->type
== CPP_HEADER_NAME
;
703 else if (header
->type
== CPP_LESS
)
705 fname
= glue_header_name (pfile
);
706 *pangle_brackets
= 1;
710 const unsigned char *dir
;
712 if (pfile
->directive
== &dtable
[T_PRAGMA
])
713 dir
= UC
"pragma dependency";
715 dir
= pfile
->directive
->name
;
716 cpp_error (pfile
, CPP_DL_ERROR
, "#%s expects \"FILENAME\" or <FILENAME>",
722 if (pfile
->directive
== &dtable
[T_PRAGMA
])
724 /* This pragma allows extra tokens after the file name. */
726 else if (buf
== NULL
|| CPP_OPTION (pfile
, discard_comments
))
727 check_eol (pfile
, true);
730 /* If we are not discarding comments, then gather them while
731 doing the eol check. */
732 *buf
= check_eol_return_comments (pfile
);
738 /* Handle #include, #include_next and #import. */
740 do_include_common (cpp_reader
*pfile
, enum include_type type
)
744 const cpp_token
**buf
= NULL
;
746 /* Re-enable saving of comments if requested, so that the include
747 callback can dump comments which follow #include. */
748 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
750 fname
= parse_include (pfile
, &angle_brackets
, &buf
);
760 cpp_error (pfile
, CPP_DL_ERROR
, "empty filename in #%s",
761 pfile
->directive
->name
);
768 /* Prevent #include recursion. */
769 if (pfile
->line_table
->depth
>= CPP_STACK_MAX
)
770 cpp_error (pfile
, CPP_DL_ERROR
, "#include nested too deeply");
773 /* Get out of macro context, if we are. */
774 skip_rest_of_line (pfile
);
776 if (pfile
->cb
.include
)
777 pfile
->cb
.include (pfile
, pfile
->directive_line
,
778 pfile
->directive
->name
, fname
, angle_brackets
,
781 _cpp_stack_include (pfile
, fname
, angle_brackets
, type
);
790 do_include (cpp_reader
*pfile
)
792 do_include_common (pfile
, IT_INCLUDE
);
796 do_import (cpp_reader
*pfile
)
798 do_include_common (pfile
, IT_IMPORT
);
802 do_include_next (cpp_reader
*pfile
)
804 enum include_type type
= IT_INCLUDE_NEXT
;
806 /* If this is the primary source file, warn and use the normal
808 if (cpp_in_primary_file (pfile
))
810 cpp_error (pfile
, CPP_DL_WARNING
,
811 "#include_next in primary source file");
814 do_include_common (pfile
, type
);
817 /* Subroutine of do_linemarker. Read possible flags after file name.
818 LAST is the last flag seen; 0 if this is the first flag. Return the
819 flag if it is valid, 0 at the end of the directive. Otherwise
822 read_flag (cpp_reader
*pfile
, unsigned int last
)
824 const cpp_token
*token
= _cpp_lex_token (pfile
);
826 if (token
->type
== CPP_NUMBER
&& token
->val
.str
.len
== 1)
828 unsigned int flag
= token
->val
.str
.text
[0] - '0';
830 if (flag
> last
&& flag
<= 4
831 && (flag
!= 4 || last
== 3)
832 && (flag
!= 2 || last
== 0))
836 if (token
->type
!= CPP_EOF
)
837 cpp_error (pfile
, CPP_DL_ERROR
, "invalid flag \"%s\" in line directive",
838 cpp_token_as_text (pfile
, token
));
842 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
843 of length LEN, to binary; store it in NUMP, and return false if the
844 number was well-formed, true if not. WRAPPED is set to true if the
845 number did not fit into 'unsigned long'. */
847 strtolinenum (const uchar
*str
, size_t len
, linenum_type
*nump
, bool *wrapped
)
849 linenum_type reg
= 0;
850 linenum_type reg_prev
= 0;
869 /* Interpret #line command.
870 Note that the filename string (if any) is a true string constant
871 (escapes are interpreted), unlike in #line. */
873 do_line (cpp_reader
*pfile
)
875 const struct line_maps
*line_table
= pfile
->line_table
;
876 const struct line_map
*map
= &line_table
->maps
[line_table
->used
- 1];
878 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
881 unsigned char map_sysp
= map
->sysp
;
882 const cpp_token
*token
;
883 const char *new_file
= map
->to_file
;
884 linenum_type new_lineno
;
886 /* C99 raised the minimum limit on #line numbers. */
887 linenum_type cap
= CPP_OPTION (pfile
, c99
) ? 2147483647 : 32767;
890 /* #line commands expand macros. */
891 token
= cpp_get_token (pfile
);
892 if (token
->type
!= CPP_NUMBER
893 || strtolinenum (token
->val
.str
.text
, token
->val
.str
.len
,
894 &new_lineno
, &wrapped
))
896 if (token
->type
== CPP_EOF
)
897 cpp_error (pfile
, CPP_DL_ERROR
, "unexpected end of file after #line");
899 cpp_error (pfile
, CPP_DL_ERROR
,
900 "\"%s\" after #line is not a positive integer",
901 cpp_token_as_text (pfile
, token
));
905 if (CPP_PEDANTIC (pfile
) && (new_lineno
== 0 || new_lineno
> cap
|| wrapped
))
906 cpp_error (pfile
, CPP_DL_PEDWARN
, "line number out of range");
908 cpp_error (pfile
, CPP_DL_WARNING
, "line number out of range");
910 token
= cpp_get_token (pfile
);
911 if (token
->type
== CPP_STRING
)
913 cpp_string s
= { 0, 0 };
914 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
, 1,
916 new_file
= (const char *)s
.text
;
917 check_eol (pfile
, true);
919 else if (token
->type
!= CPP_EOF
)
921 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
922 cpp_token_as_text (pfile
, token
));
926 skip_rest_of_line (pfile
);
927 _cpp_do_file_change (pfile
, LC_RENAME_VERBATIM
, new_file
, new_lineno
,
931 /* Interpret the # 44 "file" [flags] notation, which has slightly
932 different syntax and semantics from #line: Flags are allowed,
933 and we never complain about the line number being too big. */
935 do_linemarker (cpp_reader
*pfile
)
937 const struct line_maps
*line_table
= pfile
->line_table
;
938 const struct line_map
*map
= &line_table
->maps
[line_table
->used
- 1];
939 const cpp_token
*token
;
940 const char *new_file
= map
->to_file
;
941 linenum_type new_lineno
;
942 unsigned int new_sysp
= map
->sysp
;
943 enum lc_reason reason
= LC_RENAME_VERBATIM
;
947 /* Back up so we can get the number again. Putting this in
948 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
949 some circumstances, which can segfault. */
950 _cpp_backup_tokens (pfile
, 1);
952 /* #line commands expand macros. */
953 token
= cpp_get_token (pfile
);
954 if (token
->type
!= CPP_NUMBER
955 || strtolinenum (token
->val
.str
.text
, token
->val
.str
.len
,
956 &new_lineno
, &wrapped
))
958 /* Unlike #line, there does not seem to be a way to get an EOF
959 here. So, it should be safe to always spell the token. */
960 cpp_error (pfile
, CPP_DL_ERROR
,
961 "\"%s\" after # is not a positive integer",
962 cpp_token_as_text (pfile
, token
));
966 token
= cpp_get_token (pfile
);
967 if (token
->type
== CPP_STRING
)
969 cpp_string s
= { 0, 0 };
970 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
,
972 new_file
= (const char *)s
.text
;
975 flag
= read_flag (pfile
, 0);
979 /* Fake an include for cpp_included (). */
980 _cpp_fake_include (pfile
, new_file
);
981 flag
= read_flag (pfile
, flag
);
986 flag
= read_flag (pfile
, flag
);
991 flag
= read_flag (pfile
, flag
);
995 pfile
->buffer
->sysp
= new_sysp
;
997 check_eol (pfile
, false);
999 else if (token
->type
!= CPP_EOF
)
1001 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
1002 cpp_token_as_text (pfile
, token
));
1006 skip_rest_of_line (pfile
);
1008 /* Compensate for the increment in linemap_add that occurs in
1009 _cpp_do_file_change. We're currently at the start of the line
1010 *following* the #line directive. A separate source_location for this
1011 location makes no sense (until we do the LC_LEAVE), and
1012 complicates LAST_SOURCE_LINE_LOCATION. */
1013 pfile
->line_table
->highest_location
--;
1015 _cpp_do_file_change (pfile
, reason
, new_file
, new_lineno
, new_sysp
);
1018 /* Arrange the file_change callback. pfile->line has changed to
1019 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
1020 header, 2 for a system header that needs to be extern "C" protected,
1021 and zero otherwise. */
1023 _cpp_do_file_change (cpp_reader
*pfile
, enum lc_reason reason
,
1024 const char *to_file
, linenum_type file_line
,
1027 const struct line_map
*map
= linemap_add (pfile
->line_table
, reason
, sysp
,
1028 to_file
, file_line
);
1030 linemap_line_start (pfile
->line_table
, map
->to_line
, 127);
1032 if (pfile
->cb
.file_change
)
1033 pfile
->cb
.file_change (pfile
, map
);
1036 /* Report a warning or error detected by the program we are
1037 processing. Use the directive's tokens in the error message. */
1039 do_diagnostic (cpp_reader
*pfile
, int code
, int print_dir
)
1041 const unsigned char *dir_name
;
1042 unsigned char *line
;
1043 source_location src_loc
= pfile
->cur_token
[-1].src_loc
;
1046 dir_name
= pfile
->directive
->name
;
1049 pfile
->state
.prevent_expansion
++;
1050 line
= cpp_output_line_to_string (pfile
, dir_name
);
1051 pfile
->state
.prevent_expansion
--;
1053 cpp_error_with_line (pfile
, code
, src_loc
, 0, "%s", line
);
1058 do_error (cpp_reader
*pfile
)
1060 do_diagnostic (pfile
, CPP_DL_ERROR
, 1);
1064 do_warning (cpp_reader
*pfile
)
1066 /* We want #warning diagnostics to be emitted in system headers too. */
1067 do_diagnostic (pfile
, CPP_DL_WARNING_SYSHDR
, 1);
1070 /* Report program identification. */
1072 do_ident (cpp_reader
*pfile
)
1074 const cpp_token
*str
= cpp_get_token (pfile
);
1076 if (str
->type
!= CPP_STRING
)
1077 cpp_error (pfile
, CPP_DL_ERROR
, "invalid #%s directive",
1078 pfile
->directive
->name
);
1079 else if (pfile
->cb
.ident
)
1080 pfile
->cb
.ident (pfile
, pfile
->directive_line
, &str
->val
.str
);
1082 check_eol (pfile
, false);
1085 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1086 matching entry, or NULL if none is found. The returned entry could
1087 be the start of a namespace chain, or a pragma. */
1088 static struct pragma_entry
*
1089 lookup_pragma_entry (struct pragma_entry
*chain
, const cpp_hashnode
*pragma
)
1091 while (chain
&& chain
->pragma
!= pragma
)
1092 chain
= chain
->next
;
1097 /* Create and insert a blank pragma entry at the beginning of a
1098 singly-linked CHAIN. */
1099 static struct pragma_entry
*
1100 new_pragma_entry (cpp_reader
*pfile
, struct pragma_entry
**chain
)
1102 struct pragma_entry
*new_entry
;
1104 new_entry
= (struct pragma_entry
*)
1105 _cpp_aligned_alloc (pfile
, sizeof (struct pragma_entry
));
1107 memset (new_entry
, 0, sizeof (struct pragma_entry
));
1108 new_entry
->next
= *chain
;
1114 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1115 goes in the global namespace. */
1116 static struct pragma_entry
*
1117 register_pragma_1 (cpp_reader
*pfile
, const char *space
, const char *name
,
1118 bool allow_name_expansion
)
1120 struct pragma_entry
**chain
= &pfile
->pragmas
;
1121 struct pragma_entry
*entry
;
1122 const cpp_hashnode
*node
;
1126 node
= cpp_lookup (pfile
, UC space
, strlen (space
));
1127 entry
= lookup_pragma_entry (*chain
, node
);
1130 entry
= new_pragma_entry (pfile
, chain
);
1131 entry
->pragma
= node
;
1132 entry
->is_nspace
= true;
1133 entry
->allow_expansion
= allow_name_expansion
;
1135 else if (!entry
->is_nspace
)
1137 else if (entry
->allow_expansion
!= allow_name_expansion
)
1139 cpp_error (pfile
, CPP_DL_ICE
,
1140 "registering pragmas in namespace \"%s\" with mismatched "
1141 "name expansion", space
);
1144 chain
= &entry
->u
.space
;
1146 else if (allow_name_expansion
)
1148 cpp_error (pfile
, CPP_DL_ICE
,
1149 "registering pragma \"%s\" with name expansion "
1150 "and no namespace", name
);
1154 /* Check for duplicates. */
1155 node
= cpp_lookup (pfile
, UC name
, strlen (name
));
1156 entry
= lookup_pragma_entry (*chain
, node
);
1159 entry
= new_pragma_entry (pfile
, chain
);
1160 entry
->pragma
= node
;
1164 if (entry
->is_nspace
)
1166 cpp_error (pfile
, CPP_DL_ICE
,
1167 "registering \"%s\" as both a pragma and a pragma namespace",
1170 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s %s is already registered",
1173 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s is already registered", name
);
1178 /* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1180 register_pragma_internal (cpp_reader
*pfile
, const char *space
,
1181 const char *name
, pragma_cb handler
)
1183 struct pragma_entry
*entry
;
1185 entry
= register_pragma_1 (pfile
, space
, name
, false);
1186 entry
->is_internal
= true;
1187 entry
->u
.handler
= handler
;
1190 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1191 goes in the global namespace. HANDLER is the handler it will call,
1192 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1193 expansion while parsing pragma NAME. This function is exported
1196 cpp_register_pragma (cpp_reader
*pfile
, const char *space
, const char *name
,
1197 pragma_cb handler
, bool allow_expansion
)
1199 struct pragma_entry
*entry
;
1203 cpp_error (pfile
, CPP_DL_ICE
, "registering pragma with NULL handler");
1207 entry
= register_pragma_1 (pfile
, space
, name
, false);
1210 entry
->allow_expansion
= allow_expansion
;
1211 entry
->u
.handler
= handler
;
1215 /* Similarly, but create mark the pragma for deferred processing.
1216 When found, a CPP_PRAGMA token will be insertted into the stream
1217 with IDENT in the token->u.pragma slot. */
1219 cpp_register_deferred_pragma (cpp_reader
*pfile
, const char *space
,
1220 const char *name
, unsigned int ident
,
1221 bool allow_expansion
, bool allow_name_expansion
)
1223 struct pragma_entry
*entry
;
1225 entry
= register_pragma_1 (pfile
, space
, name
, allow_name_expansion
);
1228 entry
->is_deferred
= true;
1229 entry
->allow_expansion
= allow_expansion
;
1230 entry
->u
.ident
= ident
;
1234 /* Register the pragmas the preprocessor itself handles. */
1236 _cpp_init_internal_pragmas (cpp_reader
*pfile
)
1238 /* Pragmas in the global namespace. */
1239 register_pragma_internal (pfile
, 0, "once", do_pragma_once
);
1241 /* New GCC-specific pragmas should be put in the GCC namespace. */
1242 register_pragma_internal (pfile
, "GCC", "poison", do_pragma_poison
);
1243 register_pragma_internal (pfile
, "GCC", "system_header",
1244 do_pragma_system_header
);
1245 register_pragma_internal (pfile
, "GCC", "dependency", do_pragma_dependency
);
1248 /* Return the number of registered pragmas in PE. */
1251 count_registered_pragmas (struct pragma_entry
*pe
)
1254 for (; pe
!= NULL
; pe
= pe
->next
)
1257 ct
+= count_registered_pragmas (pe
->u
.space
);
1263 /* Save into SD the names of the registered pragmas referenced by PE,
1264 and return a pointer to the next free space in SD. */
1267 save_registered_pragmas (struct pragma_entry
*pe
, char **sd
)
1269 for (; pe
!= NULL
; pe
= pe
->next
)
1272 sd
= save_registered_pragmas (pe
->u
.space
, sd
);
1273 *sd
++ = (char *) xmemdup (HT_STR (&pe
->pragma
->ident
),
1274 HT_LEN (&pe
->pragma
->ident
),
1275 HT_LEN (&pe
->pragma
->ident
) + 1);
1280 /* Return a newly-allocated array which saves the names of the
1281 registered pragmas. */
1284 _cpp_save_pragma_names (cpp_reader
*pfile
)
1286 int ct
= count_registered_pragmas (pfile
->pragmas
);
1287 char **result
= XNEWVEC (char *, ct
);
1288 (void) save_registered_pragmas (pfile
->pragmas
, result
);
1292 /* Restore from SD the names of the registered pragmas referenced by PE,
1293 and return a pointer to the next unused name in SD. */
1296 restore_registered_pragmas (cpp_reader
*pfile
, struct pragma_entry
*pe
,
1299 for (; pe
!= NULL
; pe
= pe
->next
)
1302 sd
= restore_registered_pragmas (pfile
, pe
->u
.space
, sd
);
1303 pe
->pragma
= cpp_lookup (pfile
, UC
*sd
, strlen (*sd
));
1310 /* Restore the names of the registered pragmas from SAVED. */
1313 _cpp_restore_pragma_names (cpp_reader
*pfile
, char **saved
)
1315 (void) restore_registered_pragmas (pfile
, pfile
->pragmas
, saved
);
1319 /* Pragmata handling. We handle some, and pass the rest on to the
1320 front end. C99 defines three pragmas and says that no macro
1321 expansion is to be performed on them; whether or not macro
1322 expansion happens for other pragmas is implementation defined.
1323 This implementation allows for a mix of both, since GCC did not
1324 traditionally macro expand its (few) pragmas, whereas OpenMP
1325 specifies that macro expansion should happen. */
1327 do_pragma (cpp_reader
*pfile
)
1329 const struct pragma_entry
*p
= NULL
;
1330 const cpp_token
*token
, *pragma_token
= pfile
->cur_token
;
1332 unsigned int count
= 1;
1334 pfile
->state
.prevent_expansion
++;
1336 token
= cpp_get_token (pfile
);
1338 if (token
->type
== CPP_NAME
)
1340 p
= lookup_pragma_entry (pfile
->pragmas
, token
->val
.node
.node
);
1341 if (p
&& p
->is_nspace
)
1343 bool allow_name_expansion
= p
->allow_expansion
;
1344 if (allow_name_expansion
)
1345 pfile
->state
.prevent_expansion
--;
1346 token
= cpp_get_token (pfile
);
1347 if (token
->type
== CPP_NAME
)
1348 p
= lookup_pragma_entry (p
->u
.space
, token
->val
.node
.node
);
1351 if (allow_name_expansion
)
1352 pfile
->state
.prevent_expansion
++;
1361 pfile
->directive_result
.src_loc
= pragma_token
->src_loc
;
1362 pfile
->directive_result
.type
= CPP_PRAGMA
;
1363 pfile
->directive_result
.flags
= pragma_token
->flags
;
1364 pfile
->directive_result
.val
.pragma
= p
->u
.ident
;
1365 pfile
->state
.in_deferred_pragma
= true;
1366 pfile
->state
.pragma_allow_expansion
= p
->allow_expansion
;
1367 if (!p
->allow_expansion
)
1368 pfile
->state
.prevent_expansion
++;
1372 /* Since the handler below doesn't get the line number, that
1373 it might need for diagnostics, make sure it has the right
1374 numbers in place. */
1375 if (pfile
->cb
.line_change
)
1376 (*pfile
->cb
.line_change
) (pfile
, pragma_token
, false);
1377 if (p
->allow_expansion
)
1378 pfile
->state
.prevent_expansion
--;
1379 (*p
->u
.handler
) (pfile
);
1380 if (p
->allow_expansion
)
1381 pfile
->state
.prevent_expansion
++;
1384 else if (pfile
->cb
.def_pragma
)
1386 if (count
== 1 || pfile
->context
->prev
== NULL
)
1387 _cpp_backup_tokens (pfile
, count
);
1390 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1391 won't allow backing 2 tokens. */
1392 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1393 reads both tokens, we could perhaps free it, but if it doesn't,
1394 we don't know the exact lifespan. */
1395 cpp_token
*toks
= XNEWVEC (cpp_token
, 2);
1397 toks
[0].flags
|= NO_EXPAND
;
1399 toks
[1].flags
|= NO_EXPAND
;
1400 _cpp_push_token_context (pfile
, NULL
, toks
, 2);
1402 pfile
->cb
.def_pragma (pfile
, pfile
->directive_line
);
1405 pfile
->state
.prevent_expansion
--;
1408 /* Handle #pragma once. */
1410 do_pragma_once (cpp_reader
*pfile
)
1412 if (cpp_in_primary_file (pfile
))
1413 cpp_error (pfile
, CPP_DL_WARNING
, "#pragma once in main file");
1415 check_eol (pfile
, false);
1416 _cpp_mark_file_once_only (pfile
, pfile
->buffer
->file
);
1419 /* Handle #pragma GCC poison, to poison one or more identifiers so
1420 that the lexer produces a hard error for each subsequent usage. */
1422 do_pragma_poison (cpp_reader
*pfile
)
1424 const cpp_token
*tok
;
1427 pfile
->state
.poisoned_ok
= 1;
1430 tok
= _cpp_lex_token (pfile
);
1431 if (tok
->type
== CPP_EOF
)
1433 if (tok
->type
!= CPP_NAME
)
1435 cpp_error (pfile
, CPP_DL_ERROR
,
1436 "invalid #pragma GCC poison directive");
1440 hp
= tok
->val
.node
.node
;
1441 if (hp
->flags
& NODE_POISONED
)
1444 if (hp
->type
== NT_MACRO
)
1445 cpp_error (pfile
, CPP_DL_WARNING
, "poisoning existing macro \"%s\"",
1447 _cpp_free_definition (hp
);
1448 hp
->flags
|= NODE_POISONED
| NODE_DIAGNOSTIC
;
1450 pfile
->state
.poisoned_ok
= 0;
1453 /* Mark the current header as a system header. This will suppress
1454 some categories of warnings (notably those from -pedantic). It is
1455 intended for use in system libraries that cannot be implemented in
1456 conforming C, but cannot be certain that their headers appear in a
1457 system include directory. To prevent abuse, it is rejected in the
1458 primary source file. */
1460 do_pragma_system_header (cpp_reader
*pfile
)
1462 if (cpp_in_primary_file (pfile
))
1463 cpp_error (pfile
, CPP_DL_WARNING
,
1464 "#pragma system_header ignored outside include file");
1467 check_eol (pfile
, false);
1468 skip_rest_of_line (pfile
);
1469 cpp_make_system_header (pfile
, 1, 0);
1473 /* Check the modified date of the current include file against a specified
1474 file. Issue a diagnostic, if the specified file is newer. We use this to
1475 determine if a fixed header should be refixed. */
1477 do_pragma_dependency (cpp_reader
*pfile
)
1480 int angle_brackets
, ordering
;
1482 fname
= parse_include (pfile
, &angle_brackets
, NULL
);
1486 ordering
= _cpp_compare_file_date (pfile
, fname
, angle_brackets
);
1488 cpp_error (pfile
, CPP_DL_WARNING
, "cannot find source file %s", fname
);
1489 else if (ordering
> 0)
1491 cpp_error (pfile
, CPP_DL_WARNING
,
1492 "current file is older than %s", fname
);
1493 if (cpp_get_token (pfile
)->type
!= CPP_EOF
)
1495 _cpp_backup_tokens (pfile
, 1);
1496 do_diagnostic (pfile
, CPP_DL_WARNING
, 0);
1500 free ((void *) fname
);
1503 /* Get a token but skip padding. */
1504 static const cpp_token
*
1505 get_token_no_padding (cpp_reader
*pfile
)
1509 const cpp_token
*result
= cpp_get_token (pfile
);
1510 if (result
->type
!= CPP_PADDING
)
1515 /* Check syntax is "(string-literal)". Returns the string on success,
1516 or NULL on failure. */
1517 static const cpp_token
*
1518 get__Pragma_string (cpp_reader
*pfile
)
1520 const cpp_token
*string
;
1521 const cpp_token
*paren
;
1523 paren
= get_token_no_padding (pfile
);
1524 if (paren
->type
== CPP_EOF
)
1525 _cpp_backup_tokens (pfile
, 1);
1526 if (paren
->type
!= CPP_OPEN_PAREN
)
1529 string
= get_token_no_padding (pfile
);
1530 if (string
->type
== CPP_EOF
)
1531 _cpp_backup_tokens (pfile
, 1);
1532 if (string
->type
!= CPP_STRING
&& string
->type
!= CPP_WSTRING
1533 && string
->type
!= CPP_STRING32
&& string
->type
!= CPP_STRING16
)
1536 paren
= get_token_no_padding (pfile
);
1537 if (paren
->type
== CPP_EOF
)
1538 _cpp_backup_tokens (pfile
, 1);
1539 if (paren
->type
!= CPP_CLOSE_PAREN
)
1545 /* Destringize IN into a temporary buffer, by removing the first \ of
1546 \" and \\ sequences, and process the result as a #pragma directive. */
1548 destringize_and_run (cpp_reader
*pfile
, const cpp_string
*in
)
1550 const unsigned char *src
, *limit
;
1551 char *dest
, *result
;
1552 cpp_context
*saved_context
;
1553 cpp_token
*saved_cur_token
;
1554 tokenrun
*saved_cur_run
;
1557 const struct directive
*save_directive
;
1559 dest
= result
= (char *) alloca (in
->len
- 1);
1560 src
= in
->text
+ 1 + (in
->text
[0] == 'L');
1561 limit
= in
->text
+ in
->len
- 1;
1564 /* We know there is a character following the backslash. */
1565 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1571 /* Ugh; an awful kludge. We are really not set up to be lexing
1572 tokens when in the middle of a macro expansion. Use a new
1573 context to force cpp_get_token to lex, and so skip_rest_of_line
1574 doesn't go beyond the end of the text. Also, remember the
1575 current lexing position so we can return to it later.
1577 Something like line-at-a-time lexing should remove the need for
1579 saved_context
= pfile
->context
;
1580 saved_cur_token
= pfile
->cur_token
;
1581 saved_cur_run
= pfile
->cur_run
;
1583 pfile
->context
= XNEW (cpp_context
);
1584 pfile
->context
->macro
= 0;
1585 pfile
->context
->prev
= 0;
1586 pfile
->context
->next
= 0;
1588 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1589 until we've read all of the tokens that we want. */
1590 cpp_push_buffer (pfile
, (const uchar
*) result
, dest
- result
,
1591 /* from_stage3 */ true);
1592 /* ??? Antique Disgusting Hack. What does this do? */
1593 if (pfile
->buffer
->prev
)
1594 pfile
->buffer
->file
= pfile
->buffer
->prev
->file
;
1596 start_directive (pfile
);
1597 _cpp_clean_line (pfile
);
1598 save_directive
= pfile
->directive
;
1599 pfile
->directive
= &dtable
[T_PRAGMA
];
1601 end_directive (pfile
, 1);
1602 pfile
->directive
= save_directive
;
1604 /* We always insert at least one token, the directive result. It'll
1605 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1606 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
1608 /* If we're not handling the pragma internally, read all of the tokens from
1609 the string buffer now, while the string buffer is still installed. */
1610 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1611 to me what the true lifespan of the tokens are. It would appear that
1612 the lifespan is the entire parse of the main input stream, in which case
1613 this may not be wrong. */
1614 if (pfile
->directive_result
.type
== CPP_PRAGMA
)
1620 toks
= XNEWVEC (cpp_token
, maxcount
);
1621 toks
[0] = pfile
->directive_result
;
1625 if (count
== maxcount
)
1627 maxcount
= maxcount
* 3 / 2;
1628 toks
= XRESIZEVEC (cpp_token
, toks
, maxcount
);
1630 toks
[count
] = *cpp_get_token (pfile
);
1631 /* Macros have been already expanded by cpp_get_token
1632 if the pragma allowed expansion. */
1633 toks
[count
++].flags
|= NO_EXPAND
;
1635 while (toks
[count
-1].type
!= CPP_PRAGMA_EOL
);
1640 toks
= XNEW (cpp_token
);
1641 toks
[0] = pfile
->directive_result
;
1643 /* If we handled the entire pragma internally, make sure we get the
1644 line number correct for the next token. */
1645 if (pfile
->cb
.line_change
)
1646 pfile
->cb
.line_change (pfile
, pfile
->cur_token
, false);
1649 /* Finish inlining run_directive. */
1650 pfile
->buffer
->file
= NULL
;
1651 _cpp_pop_buffer (pfile
);
1653 /* Reset the old macro state before ... */
1654 XDELETE (pfile
->context
);
1655 pfile
->context
= saved_context
;
1656 pfile
->cur_token
= saved_cur_token
;
1657 pfile
->cur_run
= saved_cur_run
;
1659 /* ... inserting the new tokens we collected. */
1660 _cpp_push_token_context (pfile
, NULL
, toks
, count
);
1663 /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
1665 _cpp_do__Pragma (cpp_reader
*pfile
)
1667 const cpp_token
*string
= get__Pragma_string (pfile
);
1668 pfile
->directive_result
.type
= CPP_PADDING
;
1672 destringize_and_run (pfile
, &string
->val
.str
);
1675 cpp_error (pfile
, CPP_DL_ERROR
,
1676 "_Pragma takes a parenthesized string literal");
1680 /* Handle #ifdef. */
1682 do_ifdef (cpp_reader
*pfile
)
1686 if (! pfile
->state
.skipping
)
1688 cpp_hashnode
*node
= lex_macro_node (pfile
, false);
1692 skip
= node
->type
!= NT_MACRO
;
1693 _cpp_mark_macro_used (node
);
1694 if (!(node
->flags
& NODE_USED
))
1696 node
->flags
|= NODE_USED
;
1697 if (node
->type
== NT_MACRO
)
1699 if (pfile
->cb
.used_define
)
1700 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
1704 if (pfile
->cb
.used_undef
)
1705 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
1708 check_eol (pfile
, false);
1712 push_conditional (pfile
, skip
, T_IFDEF
, 0);
1715 /* Handle #ifndef. */
1717 do_ifndef (cpp_reader
*pfile
)
1720 cpp_hashnode
*node
= 0;
1722 if (! pfile
->state
.skipping
)
1724 node
= lex_macro_node (pfile
, false);
1728 skip
= node
->type
== NT_MACRO
;
1729 _cpp_mark_macro_used (node
);
1730 if (!(node
->flags
& NODE_USED
))
1732 node
->flags
|= NODE_USED
;
1733 if (node
->type
== NT_MACRO
)
1735 if (pfile
->cb
.used_define
)
1736 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
1740 if (pfile
->cb
.used_undef
)
1741 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
1744 check_eol (pfile
, false);
1748 push_conditional (pfile
, skip
, T_IFNDEF
, node
);
1751 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1752 pfile->mi_ind_cmacro so we can handle multiple-include
1753 optimizations. If macro expansion occurs in the expression, we
1754 cannot treat it as a controlling conditional, since the expansion
1755 could change in the future. That is handled by cpp_get_token. */
1757 do_if (cpp_reader
*pfile
)
1761 if (! pfile
->state
.skipping
)
1762 skip
= _cpp_parse_expr (pfile
, true) == false;
1764 push_conditional (pfile
, skip
, T_IF
, pfile
->mi_ind_cmacro
);
1767 /* Flip skipping state if appropriate and continue without changing
1768 if_stack; this is so that the error message for missing #endif's
1769 etc. will point to the original #if. */
1771 do_else (cpp_reader
*pfile
)
1773 cpp_buffer
*buffer
= pfile
->buffer
;
1774 struct if_stack
*ifs
= buffer
->if_stack
;
1777 cpp_error (pfile
, CPP_DL_ERROR
, "#else without #if");
1780 if (ifs
->type
== T_ELSE
)
1782 cpp_error (pfile
, CPP_DL_ERROR
, "#else after #else");
1783 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
1784 "the conditional began here");
1788 /* Skip any future (erroneous) #elses or #elifs. */
1789 pfile
->state
.skipping
= ifs
->skip_elses
;
1790 ifs
->skip_elses
= true;
1792 /* Invalidate any controlling macro. */
1795 /* Only check EOL if was not originally skipping. */
1796 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1797 check_eol (pfile
, false);
1801 /* Handle a #elif directive by not changing if_stack either. See the
1802 comment above do_else. */
1804 do_elif (cpp_reader
*pfile
)
1806 cpp_buffer
*buffer
= pfile
->buffer
;
1807 struct if_stack
*ifs
= buffer
->if_stack
;
1810 cpp_error (pfile
, CPP_DL_ERROR
, "#elif without #if");
1813 if (ifs
->type
== T_ELSE
)
1815 cpp_error (pfile
, CPP_DL_ERROR
, "#elif after #else");
1816 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
1817 "the conditional began here");
1821 if (! ifs
->was_skipping
)
1824 /* The standard mandates that the expression be parsed even
1825 if we are skipping elses at this point -- the lexical
1826 restrictions on #elif only apply to skipped groups, but
1827 this group is not being skipped. Temporarily set
1828 skipping to false to get lexer warnings. */
1829 pfile
->state
.skipping
= 0;
1830 value
= _cpp_parse_expr (pfile
, false);
1831 if (ifs
->skip_elses
)
1832 pfile
->state
.skipping
= 1;
1835 pfile
->state
.skipping
= ! value
;
1836 ifs
->skip_elses
= value
;
1840 /* Invalidate any controlling macro. */
1845 /* #endif pops the if stack and resets pfile->state.skipping. */
1847 do_endif (cpp_reader
*pfile
)
1849 cpp_buffer
*buffer
= pfile
->buffer
;
1850 struct if_stack
*ifs
= buffer
->if_stack
;
1853 cpp_error (pfile
, CPP_DL_ERROR
, "#endif without #if");
1856 /* Only check EOL if was not originally skipping. */
1857 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1858 check_eol (pfile
, false);
1860 /* If potential control macro, we go back outside again. */
1861 if (ifs
->next
== 0 && ifs
->mi_cmacro
)
1863 pfile
->mi_valid
= true;
1864 pfile
->mi_cmacro
= ifs
->mi_cmacro
;
1867 buffer
->if_stack
= ifs
->next
;
1868 pfile
->state
.skipping
= ifs
->was_skipping
;
1869 obstack_free (&pfile
->buffer_ob
, ifs
);
1873 /* Push an if_stack entry for a preprocessor conditional, and set
1874 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1875 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1876 we need to check here that we are at the top of the file. */
1878 push_conditional (cpp_reader
*pfile
, int skip
, int type
,
1879 const cpp_hashnode
*cmacro
)
1881 struct if_stack
*ifs
;
1882 cpp_buffer
*buffer
= pfile
->buffer
;
1884 ifs
= XOBNEW (&pfile
->buffer_ob
, struct if_stack
);
1885 ifs
->line
= pfile
->directive_line
;
1886 ifs
->next
= buffer
->if_stack
;
1887 ifs
->skip_elses
= pfile
->state
.skipping
|| !skip
;
1888 ifs
->was_skipping
= pfile
->state
.skipping
;
1890 /* This condition is effectively a test for top-of-file. */
1891 if (pfile
->mi_valid
&& pfile
->mi_cmacro
== 0)
1892 ifs
->mi_cmacro
= cmacro
;
1896 pfile
->state
.skipping
= skip
;
1897 buffer
->if_stack
= ifs
;
1900 /* Read the tokens of the answer into the macro pool, in a directive
1901 of type TYPE. Only commit the memory if we intend it as permanent
1902 storage, i.e. the #assert case. Returns 0 on success, and sets
1903 ANSWERP to point to the answer. */
1905 parse_answer (cpp_reader
*pfile
, struct answer
**answerp
, int type
)
1907 const cpp_token
*paren
;
1908 struct answer
*answer
;
1909 unsigned int acount
;
1911 /* In a conditional, it is legal to not have an open paren. We
1912 should save the following token in this case. */
1913 paren
= cpp_get_token (pfile
);
1915 /* If not a paren, see if we're OK. */
1916 if (paren
->type
!= CPP_OPEN_PAREN
)
1918 /* In a conditional no answer is a test for any answer. It
1919 could be followed by any token. */
1922 _cpp_backup_tokens (pfile
, 1);
1926 /* #unassert with no answer is valid - it removes all answers. */
1927 if (type
== T_UNASSERT
&& paren
->type
== CPP_EOF
)
1930 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' after predicate");
1934 for (acount
= 0;; acount
++)
1937 const cpp_token
*token
= cpp_get_token (pfile
);
1940 if (token
->type
== CPP_CLOSE_PAREN
)
1943 if (token
->type
== CPP_EOF
)
1945 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' to complete answer");
1949 /* struct answer includes the space for one token. */
1950 room_needed
= (sizeof (struct answer
) + acount
* sizeof (cpp_token
));
1952 if (BUFF_ROOM (pfile
->a_buff
) < room_needed
)
1953 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (struct answer
));
1955 dest
= &((struct answer
*) BUFF_FRONT (pfile
->a_buff
))->first
[acount
];
1958 /* Drop whitespace at start, for answer equivalence purposes. */
1960 dest
->flags
&= ~PREV_WHITE
;
1965 cpp_error (pfile
, CPP_DL_ERROR
, "predicate's answer is empty");
1969 answer
= (struct answer
*) BUFF_FRONT (pfile
->a_buff
);
1970 answer
->count
= acount
;
1971 answer
->next
= NULL
;
1977 /* Parses an assertion directive of type TYPE, returning a pointer to
1978 the hash node of the predicate, or 0 on error. If an answer was
1979 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
1980 static cpp_hashnode
*
1981 parse_assertion (cpp_reader
*pfile
, struct answer
**answerp
, int type
)
1983 cpp_hashnode
*result
= 0;
1984 const cpp_token
*predicate
;
1986 /* We don't expand predicates or answers. */
1987 pfile
->state
.prevent_expansion
++;
1990 predicate
= cpp_get_token (pfile
);
1991 if (predicate
->type
== CPP_EOF
)
1992 cpp_error (pfile
, CPP_DL_ERROR
, "assertion without predicate");
1993 else if (predicate
->type
!= CPP_NAME
)
1994 cpp_error (pfile
, CPP_DL_ERROR
, "predicate must be an identifier");
1995 else if (parse_answer (pfile
, answerp
, type
) == 0)
1997 unsigned int len
= NODE_LEN (predicate
->val
.node
.node
);
1998 unsigned char *sym
= (unsigned char *) alloca (len
+ 1);
2000 /* Prefix '#' to get it out of macro namespace. */
2002 memcpy (sym
+ 1, NODE_NAME (predicate
->val
.node
.node
), len
);
2003 result
= cpp_lookup (pfile
, sym
, len
+ 1);
2006 pfile
->state
.prevent_expansion
--;
2010 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
2011 or a pointer to NULL if the answer is not in the chain. */
2012 static struct answer
**
2013 find_answer (cpp_hashnode
*node
, const struct answer
*candidate
)
2016 struct answer
**result
;
2018 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->next
)
2020 struct answer
*answer
= *result
;
2022 if (answer
->count
== candidate
->count
)
2024 for (i
= 0; i
< answer
->count
; i
++)
2025 if (! _cpp_equiv_tokens (&answer
->first
[i
], &candidate
->first
[i
]))
2028 if (i
== answer
->count
)
2036 /* Test an assertion within a preprocessor conditional. Returns
2037 nonzero on failure, zero on success. On success, the result of
2038 the test is written into VALUE, otherwise the value 0. */
2040 _cpp_test_assertion (cpp_reader
*pfile
, unsigned int *value
)
2042 struct answer
*answer
;
2045 node
= parse_assertion (pfile
, &answer
, T_IF
);
2047 /* For recovery, an erroneous assertion expression is handled as a
2048 failing assertion. */
2052 *value
= (node
->type
== NT_ASSERTION
&&
2053 (answer
== 0 || *find_answer (node
, answer
) != 0));
2054 else if (pfile
->cur_token
[-1].type
== CPP_EOF
)
2055 _cpp_backup_tokens (pfile
, 1);
2057 /* We don't commit the memory for the answer - it's temporary only. */
2061 /* Handle #assert. */
2063 do_assert (cpp_reader
*pfile
)
2065 struct answer
*new_answer
;
2068 node
= parse_assertion (pfile
, &new_answer
, T_ASSERT
);
2073 /* Place the new answer in the answer list. First check there
2074 is not a duplicate. */
2075 new_answer
->next
= 0;
2076 if (node
->type
== NT_ASSERTION
)
2078 if (*find_answer (node
, new_answer
))
2080 cpp_error (pfile
, CPP_DL_WARNING
, "\"%s\" re-asserted",
2081 NODE_NAME (node
) + 1);
2084 new_answer
->next
= node
->value
.answers
;
2087 answer_size
= sizeof (struct answer
) + ((new_answer
->count
- 1)
2088 * sizeof (cpp_token
));
2089 /* Commit or allocate storage for the object. */
2090 if (pfile
->hash_table
->alloc_subobject
)
2092 struct answer
*temp_answer
= new_answer
;
2093 new_answer
= (struct answer
*) pfile
->hash_table
->alloc_subobject
2095 memcpy (new_answer
, temp_answer
, answer_size
);
2098 BUFF_FRONT (pfile
->a_buff
) += answer_size
;
2100 node
->type
= NT_ASSERTION
;
2101 node
->value
.answers
= new_answer
;
2102 check_eol (pfile
, false);
2106 /* Handle #unassert. */
2108 do_unassert (cpp_reader
*pfile
)
2111 struct answer
*answer
;
2113 node
= parse_assertion (pfile
, &answer
, T_UNASSERT
);
2114 /* It isn't an error to #unassert something that isn't asserted. */
2115 if (node
&& node
->type
== NT_ASSERTION
)
2119 struct answer
**p
= find_answer (node
, answer
), *temp
;
2121 /* Remove the answer from the list. */
2126 /* Did we free the last answer? */
2127 if (node
->value
.answers
== 0)
2128 node
->type
= NT_VOID
;
2130 check_eol (pfile
, false);
2133 _cpp_free_definition (node
);
2136 /* We don't commit the memory for the answer - it's temporary only. */
2139 /* These are for -D, -U, -A. */
2141 /* Process the string STR as if it appeared as the body of a #define.
2142 If STR is just an identifier, define it with value 1.
2143 If STR has anything after the identifier, then it should
2144 be identifier=definition. */
2146 cpp_define (cpp_reader
*pfile
, const char *str
)
2151 /* Copy the entire option so we can modify it.
2152 Change the first "=" in the string to a space. If there is none,
2153 tack " 1" on the end. */
2155 count
= strlen (str
);
2156 buf
= (char *) alloca (count
+ 3);
2157 memcpy (buf
, str
, count
);
2159 p
= strchr (str
, '=');
2169 run_directive (pfile
, T_DEFINE
, buf
, count
);
2173 /* Use to build macros to be run through cpp_define() as
2175 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
2178 cpp_define_formatted (cpp_reader
*pfile
, const char *fmt
, ...)
2184 vasprintf (&ptr
, fmt
, ap
);
2187 cpp_define (pfile
, ptr
);
2192 /* Slight variant of the above for use by initialize_builtins. */
2194 _cpp_define_builtin (cpp_reader
*pfile
, const char *str
)
2196 size_t len
= strlen (str
);
2197 char *buf
= (char *) alloca (len
+ 1);
2198 memcpy (buf
, str
, len
);
2200 run_directive (pfile
, T_DEFINE
, buf
, len
);
2203 /* Process MACRO as if it appeared as the body of an #undef. */
2205 cpp_undef (cpp_reader
*pfile
, const char *macro
)
2207 size_t len
= strlen (macro
);
2208 char *buf
= (char *) alloca (len
+ 1);
2209 memcpy (buf
, macro
, len
);
2211 run_directive (pfile
, T_UNDEF
, buf
, len
);
2214 /* Like lex_macro_node, but read the input from STR. */
2215 static cpp_hashnode
*
2216 lex_macro_node_from_str (cpp_reader
*pfile
, const char *str
)
2218 size_t len
= strlen (str
);
2219 uchar
*buf
= (uchar
*) alloca (len
+ 1);
2222 memcpy (buf
, str
, len
);
2224 cpp_push_buffer (pfile
, buf
, len
, true);
2225 node
= lex_macro_node (pfile
, true);
2226 _cpp_pop_buffer (pfile
);
2231 /* If STR is a defined macro, return its definition node, else return NULL. */
2233 cpp_push_definition (cpp_reader
*pfile
, const char *str
)
2235 cpp_hashnode
*node
= lex_macro_node_from_str (pfile
, str
);
2236 if (node
&& node
->type
== NT_MACRO
)
2237 return node
->value
.macro
;
2242 /* Replace a previous definition DFN of the macro STR. If DFN is NULL,
2243 then the macro should be undefined. */
2245 cpp_pop_definition (cpp_reader
*pfile
, const char *str
, cpp_macro
*dfn
)
2247 cpp_hashnode
*node
= lex_macro_node_from_str (pfile
, str
);
2251 if (pfile
->cb
.before_define
)
2252 pfile
->cb
.before_define (pfile
);
2254 if (node
->type
== NT_MACRO
)
2256 if (pfile
->cb
.undef
)
2257 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
2258 if (CPP_OPTION (pfile
, warn_unused_macros
))
2259 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
2261 if (node
->type
!= NT_VOID
)
2262 _cpp_free_definition (node
);
2266 node
->type
= NT_MACRO
;
2267 node
->value
.macro
= dfn
;
2268 if (! ustrncmp (NODE_NAME (node
), DSC ("__STDC_")))
2269 node
->flags
|= NODE_WARN
;
2271 if (pfile
->cb
.define
)
2272 pfile
->cb
.define (pfile
, pfile
->directive_line
, node
);
2276 /* Process the string STR as if it appeared as the body of a #assert. */
2278 cpp_assert (cpp_reader
*pfile
, const char *str
)
2280 handle_assertion (pfile
, str
, T_ASSERT
);
2283 /* Process STR as if it appeared as the body of an #unassert. */
2285 cpp_unassert (cpp_reader
*pfile
, const char *str
)
2287 handle_assertion (pfile
, str
, T_UNASSERT
);
2290 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2292 handle_assertion (cpp_reader
*pfile
, const char *str
, int type
)
2294 size_t count
= strlen (str
);
2295 const char *p
= strchr (str
, '=');
2297 /* Copy the entire option so we can modify it. Change the first
2298 "=" in the string to a '(', and tack a ')' on the end. */
2299 char *buf
= (char *) alloca (count
+ 2);
2301 memcpy (buf
, str
, count
);
2310 run_directive (pfile
, type
, str
, count
);
2313 /* The options structure. */
2315 cpp_get_options (cpp_reader
*pfile
)
2317 return &pfile
->opts
;
2320 /* The callbacks structure. */
2322 cpp_get_callbacks (cpp_reader
*pfile
)
2327 /* Copy the given callbacks structure to our own. */
2329 cpp_set_callbacks (cpp_reader
*pfile
, cpp_callbacks
*cb
)
2334 /* The dependencies structure. (Creates one if it hasn't already been.) */
2336 cpp_get_deps (cpp_reader
*pfile
)
2339 pfile
->deps
= deps_init ();
2343 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2344 doesn't fail. It does not generate a file change call back; that
2345 is the responsibility of the caller. */
2347 cpp_push_buffer (cpp_reader
*pfile
, const uchar
*buffer
, size_t len
,
2350 cpp_buffer
*new_buffer
= XOBNEW (&pfile
->buffer_ob
, cpp_buffer
);
2352 /* Clears, amongst other things, if_stack and mi_cmacro. */
2353 memset (new_buffer
, 0, sizeof (cpp_buffer
));
2355 new_buffer
->next_line
= new_buffer
->buf
= buffer
;
2356 new_buffer
->rlimit
= buffer
+ len
;
2357 new_buffer
->from_stage3
= from_stage3
;
2358 new_buffer
->prev
= pfile
->buffer
;
2359 new_buffer
->need_line
= true;
2361 pfile
->buffer
= new_buffer
;
2366 /* Pops a single buffer, with a file change call-back if appropriate.
2367 Then pushes the next -include file, if any remain. */
2369 _cpp_pop_buffer (cpp_reader
*pfile
)
2371 cpp_buffer
*buffer
= pfile
->buffer
;
2372 struct _cpp_file
*inc
= buffer
->file
;
2373 struct if_stack
*ifs
;
2375 /* Walk back up the conditional stack till we reach its level at
2376 entry to this file, issuing error messages. */
2377 for (ifs
= buffer
->if_stack
; ifs
; ifs
= ifs
->next
)
2378 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2379 "unterminated #%s", dtable
[ifs
->type
].name
);
2381 /* In case of a missing #endif. */
2382 pfile
->state
.skipping
= 0;
2384 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2385 pfile
->buffer
= buffer
->prev
;
2387 free (buffer
->notes
);
2389 /* Free the buffer object now; we may want to push a new buffer
2390 in _cpp_push_next_include_file. */
2391 obstack_free (&pfile
->buffer_ob
, buffer
);
2395 _cpp_pop_file_buffer (pfile
, inc
);
2397 _cpp_do_file_change (pfile
, LC_LEAVE
, 0, 0, 0);
2401 /* Enter all recognized directives in the hash table. */
2403 _cpp_init_directives (cpp_reader
*pfile
)
2408 for (i
= 0; i
< (unsigned int) N_DIRECTIVES
; i
++)
2410 node
= cpp_lookup (pfile
, dtable
[i
].name
, dtable
[i
].length
);
2411 node
->is_directive
= 1;
2412 node
->directive_index
= i
;