1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986-2020 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
28 /* Stack of conditionals currently in progress
29 (including both successful and failing conditionals). */
32 struct if_stack
*next
;
33 location_t line
; /* Line where condition started. */
34 const cpp_hashnode
*mi_cmacro
;/* macro name for #ifndef around entire file */
35 bool skip_elses
; /* Can future #else / #elif be skipped? */
36 bool was_skipping
; /* If were skipping on entry. */
37 int type
; /* Most recent conditional for diagnostics. */
40 /* Contains a registered pragma or pragma namespace. */
41 typedef void (*pragma_cb
) (cpp_reader
*);
44 struct pragma_entry
*next
;
45 const cpp_hashnode
*pragma
; /* Name and length. */
52 struct pragma_entry
*space
;
57 /* Values for the origin field of struct directive. KANDR directives
58 come from traditional (K&R) C. STDC89 directives come from the
59 1989 C standard. EXTENSION directives are extensions. */
64 /* Values for the flags field of struct directive. COND indicates a
65 conditional; IF_COND an opening conditional. INCL means to treat
66 "..." and <...> as q-char and h-char sequences respectively. IN_I
67 means this directive should be handled even if -fpreprocessed is in
68 effect (these are the directives with callback hooks).
70 EXPAND is set on directives that are always macro-expanded. */
72 #define IF_COND (1 << 1)
75 #define EXPAND (1 << 4)
76 #define DEPRECATED (1 << 5)
78 /* Defines one #-directive, including how to handle it. */
79 typedef void (*directive_handler
) (cpp_reader
*);
80 typedef struct directive directive
;
83 directive_handler handler
; /* Function to handle directive. */
84 const uchar
*name
; /* Name of directive. */
85 unsigned short length
; /* Length of name. */
86 unsigned char origin
; /* Origin of directive. */
87 unsigned char flags
; /* Flags describing this directive. */
90 /* Forward declarations. */
92 static void skip_rest_of_line (cpp_reader
*);
93 static void check_eol (cpp_reader
*, bool);
94 static void start_directive (cpp_reader
*);
95 static void prepare_directive_trad (cpp_reader
*);
96 static void end_directive (cpp_reader
*, int);
97 static void directive_diagnostics (cpp_reader
*, const directive
*, int);
98 static void run_directive (cpp_reader
*, int, const char *, size_t);
99 static char *glue_header_name (cpp_reader
*);
100 static const char *parse_include (cpp_reader
*, int *, const cpp_token
***,
102 static void push_conditional (cpp_reader
*, int, int, const cpp_hashnode
*);
103 static unsigned int read_flag (cpp_reader
*, unsigned int);
104 static bool strtolinenum (const uchar
*, size_t, linenum_type
*, bool *);
105 static void do_diagnostic (cpp_reader
*, enum cpp_diagnostic_level code
,
106 enum cpp_warning_reason reason
, 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_pragma_warning_or_error (cpp_reader
*, bool error
);
121 static void do_pragma_warning (cpp_reader
*);
122 static void do_pragma_error (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
*,
128 static bool parse_answer (cpp_reader
*, int, location_t
, cpp_macro
**);
129 static cpp_hashnode
*parse_assertion (cpp_reader
*, int, cpp_macro
**);
130 static cpp_macro
**find_answer (cpp_hashnode
*, const cpp_macro
*);
131 static void handle_assertion (cpp_reader
*, const char *, int);
132 static void do_pragma_push_macro (cpp_reader
*);
133 static void do_pragma_pop_macro (cpp_reader
*);
134 static void cpp_pop_definition (cpp_reader
*, struct def_pragma_macro
*);
136 /* This is the table of directive handlers. All extensions other than
137 #warning, #include_next, and #import are deprecated. The name is
138 where the extension appears to have come from. */
140 #define DIRECTIVE_TABLE \
141 D(define, T_DEFINE = 0, KANDR, IN_I) \
142 D(include, T_INCLUDE, KANDR, INCL | EXPAND) \
143 D(endif, T_ENDIF, KANDR, COND) \
144 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) \
145 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) \
146 D(else, T_ELSE, KANDR, COND) \
147 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) \
148 D(undef, T_UNDEF, KANDR, IN_I) \
149 D(line, T_LINE, KANDR, EXPAND) \
150 D(elif, T_ELIF, STDC89, COND | EXPAND) \
151 D(error, T_ERROR, STDC89, 0) \
152 D(pragma, T_PRAGMA, STDC89, IN_I) \
153 D(warning, T_WARNING, EXTENSION, 0) \
154 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) \
155 D(ident, T_IDENT, EXTENSION, IN_I) \
156 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* ObjC */ \
157 D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* SVR4 */ \
158 D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* SVR4 */ \
159 D(sccs, T_SCCS, EXTENSION, IN_I) /* SVR4? */
161 /* #sccs is synonymous with #ident. */
162 #define do_sccs do_ident
164 /* Use the table to generate a series of prototypes, an enum for the
165 directive names, and an array of directive handlers. */
167 #define D(name, t, o, f) static void do_##name (cpp_reader *);
171 #define D(n, tag, o, f) tag,
179 #define D(name, t, origin, flags) \
180 { do_##name, (const uchar *) #name, \
181 sizeof #name - 1, origin, flags },
182 static const directive dtable
[] =
188 /* A NULL-terminated array of directive names for use
189 when suggesting corrections for misspelled directives. */
190 #define D(name, t, origin, flags) #name,
191 static const char * const directive_names
[] = {
197 #undef DIRECTIVE_TABLE
199 /* Wrapper struct directive for linemarkers.
200 The origin is more or less true - the original K+R cpp
201 did use this notation in its preprocessed output. */
202 static const directive linemarker_dir
=
204 do_linemarker
, UC
"#", 1, KANDR
, IN_I
207 /* Skip any remaining tokens in a directive. */
209 skip_rest_of_line (cpp_reader
*pfile
)
211 /* Discard all stacked contexts. */
212 while (pfile
->context
->prev
)
213 _cpp_pop_context (pfile
);
215 /* Sweep up all tokens remaining on the line. */
217 while (_cpp_lex_token (pfile
)->type
!= CPP_EOF
)
221 /* Helper function for check_oel. */
224 check_eol_1 (cpp_reader
*pfile
, bool expand
, enum cpp_warning_reason reason
)
226 if (! SEEN_EOL () && (expand
227 ? cpp_get_token (pfile
)
228 : _cpp_lex_token (pfile
))->type
!= CPP_EOF
)
229 cpp_pedwarning (pfile
, reason
, "extra tokens at end of #%s directive",
230 pfile
->directive
->name
);
233 /* Variant of check_eol used for Wendif-labels warnings. */
236 check_eol_endif_labels (cpp_reader
*pfile
)
238 check_eol_1 (pfile
, false, CPP_W_ENDIF_LABELS
);
241 /* Ensure there are no stray tokens at the end of a directive. If
242 EXPAND is true, tokens macro-expanding to nothing are allowed. */
245 check_eol (cpp_reader
*pfile
, bool expand
)
247 check_eol_1 (pfile
, expand
, CPP_W_NONE
);
250 /* Ensure there are no stray tokens other than comments at the end of
251 a directive, and gather the comments. */
252 static const cpp_token
**
253 check_eol_return_comments (cpp_reader
*pfile
)
257 const cpp_token
**buf
;
259 buf
= XNEWVEC (const cpp_token
*, capacity
);
265 const cpp_token
*tok
;
267 tok
= _cpp_lex_token (pfile
);
268 if (tok
->type
== CPP_EOF
)
270 if (tok
->type
!= CPP_COMMENT
)
271 cpp_error (pfile
, CPP_DL_PEDWARN
,
272 "extra tokens at end of #%s directive",
273 pfile
->directive
->name
);
276 if (c
+ 1 >= capacity
)
279 buf
= XRESIZEVEC (const cpp_token
*, buf
, capacity
);
290 /* Called when entering a directive, _Pragma or command-line directive. */
292 start_directive (cpp_reader
*pfile
)
294 /* Setup in-directive state. */
295 pfile
->state
.in_directive
= 1;
296 pfile
->state
.save_comments
= 0;
297 pfile
->directive_result
.type
= CPP_PADDING
;
299 /* Some handlers need the position of the # for diagnostics. */
300 pfile
->directive_line
= pfile
->line_table
->highest_line
;
303 /* Called when leaving a directive, _Pragma or command-line directive. */
305 end_directive (cpp_reader
*pfile
, int skip_line
)
307 if (CPP_OPTION (pfile
, traditional
))
309 /* Revert change of prepare_directive_trad. */
310 if (!pfile
->state
.in_deferred_pragma
)
311 pfile
->state
.prevent_expansion
--;
313 if (pfile
->directive
!= &dtable
[T_DEFINE
])
314 _cpp_remove_overlay (pfile
);
316 else if (pfile
->state
.in_deferred_pragma
)
318 /* We don't skip for an assembler #. */
321 skip_rest_of_line (pfile
);
322 if (!pfile
->keep_tokens
)
324 pfile
->cur_run
= &pfile
->base_run
;
325 pfile
->cur_token
= pfile
->base_run
.base
;
330 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
331 pfile
->state
.in_directive
= 0;
332 pfile
->state
.in_expression
= 0;
333 pfile
->state
.angled_headers
= 0;
334 pfile
->directive
= 0;
337 /* Prepare to handle the directive in pfile->directive. */
339 prepare_directive_trad (cpp_reader
*pfile
)
341 if (pfile
->directive
!= &dtable
[T_DEFINE
])
343 bool no_expand
= (pfile
->directive
344 && ! (pfile
->directive
->flags
& EXPAND
));
345 bool was_skipping
= pfile
->state
.skipping
;
347 pfile
->state
.in_expression
= (pfile
->directive
== &dtable
[T_IF
]
348 || pfile
->directive
== &dtable
[T_ELIF
]);
349 if (pfile
->state
.in_expression
)
350 pfile
->state
.skipping
= false;
353 pfile
->state
.prevent_expansion
++;
354 _cpp_scan_out_logical_line (pfile
, NULL
, false);
356 pfile
->state
.prevent_expansion
--;
358 pfile
->state
.skipping
= was_skipping
;
359 _cpp_overlay_buffer (pfile
, pfile
->out
.base
,
360 pfile
->out
.cur
- pfile
->out
.base
);
363 /* Stop ISO C from expanding anything. */
364 pfile
->state
.prevent_expansion
++;
367 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
368 the '#' was indented. */
370 directive_diagnostics (cpp_reader
*pfile
, const directive
*dir
, int indented
)
372 /* Issue -pedantic or deprecated warnings for extensions. We let
373 -pedantic take precedence if both are applicable. */
374 if (! pfile
->state
.skipping
)
376 if (dir
->origin
== EXTENSION
377 && !(dir
== &dtable
[T_IMPORT
] && CPP_OPTION (pfile
, objc
))
378 && CPP_PEDANTIC (pfile
))
379 cpp_error (pfile
, CPP_DL_PEDWARN
, "#%s is a GCC extension", dir
->name
);
380 else if (((dir
->flags
& DEPRECATED
) != 0
381 || (dir
== &dtable
[T_IMPORT
] && !CPP_OPTION (pfile
, objc
)))
382 && CPP_OPTION (pfile
, cpp_warn_deprecated
))
383 cpp_warning (pfile
, CPP_W_DEPRECATED
,
384 "#%s is a deprecated GCC extension", dir
->name
);
387 /* Traditionally, a directive is ignored unless its # is in
388 column 1. Therefore in code intended to work with K+R
389 compilers, directives added by C89 must have their #
390 indented, and directives present in traditional C must not.
391 This is true even of directives in skipped conditional
392 blocks. #elif cannot be used at all. */
393 if (CPP_WTRADITIONAL (pfile
))
395 if (dir
== &dtable
[T_ELIF
])
396 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
397 "suggest not using #elif in traditional C");
398 else if (indented
&& dir
->origin
== KANDR
)
399 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
400 "traditional C ignores #%s with the # indented",
402 else if (!indented
&& dir
->origin
!= KANDR
)
403 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
404 "suggest hiding #%s from traditional C with an indented #",
409 /* Check if we have a known directive. INDENTED is true if the
410 '#' of the directive was indented. This function is in this file
411 to save unnecessarily exporting dtable etc. to lex.c. Returns
412 nonzero if the line of tokens has been handled, zero if we should
413 continue processing the line. */
415 _cpp_handle_directive (cpp_reader
*pfile
, bool indented
)
417 const directive
*dir
= 0;
418 const cpp_token
*dname
;
419 bool was_parsing_args
= pfile
->state
.parsing_args
;
420 bool was_discarding_output
= pfile
->state
.discarding_output
;
423 if (was_discarding_output
)
424 pfile
->state
.prevent_expansion
= 0;
426 if (was_parsing_args
)
428 if (CPP_OPTION (pfile
, cpp_pedantic
))
429 cpp_error (pfile
, CPP_DL_PEDWARN
,
430 "embedding a directive within macro arguments is not portable");
431 pfile
->state
.parsing_args
= 0;
432 pfile
->state
.prevent_expansion
= 0;
434 start_directive (pfile
);
435 dname
= _cpp_lex_token (pfile
);
437 if (dname
->type
== CPP_NAME
)
439 if (dname
->val
.node
.node
->is_directive
)
440 dir
= &dtable
[dname
->val
.node
.node
->directive_index
];
442 /* We do not recognize the # followed by a number extension in
444 else if (dname
->type
== CPP_NUMBER
&& CPP_OPTION (pfile
, lang
) != CLK_ASM
)
446 dir
= &linemarker_dir
;
447 if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, preprocessed
)
448 && ! pfile
->state
.skipping
)
449 cpp_error (pfile
, CPP_DL_PEDWARN
,
450 "style of line directive is a GCC extension");
455 /* If we have a directive that is not an opening conditional,
456 invalidate any control macro. */
457 if (! (dir
->flags
& IF_COND
))
458 pfile
->mi_valid
= false;
460 /* Kluge alert. In order to be sure that code like this
465 does not cause '#define foo bar' to get executed when
466 compiled with -save-temps, we recognize directives in
467 -fpreprocessed mode only if the # is in column 1. macro.c
468 puts a space in front of any '#' at the start of a macro.
470 We exclude the -fdirectives-only case because macro expansion
471 has not been performed yet, and block comments can cause spaces
472 to precede the directive. */
473 if (CPP_OPTION (pfile
, preprocessed
)
474 && !CPP_OPTION (pfile
, directives_only
)
475 && (indented
|| !(dir
->flags
& IN_I
)))
482 /* In failed conditional groups, all non-conditional
483 directives are ignored. Before doing that, whether
484 skipping or not, we should lex angle-bracketed headers
485 correctly, and maybe output some diagnostics. */
486 pfile
->state
.angled_headers
= dir
->flags
& INCL
;
487 pfile
->state
.directive_wants_padding
= dir
->flags
& INCL
;
488 if (! CPP_OPTION (pfile
, preprocessed
))
489 directive_diagnostics (pfile
, dir
, indented
);
490 if (pfile
->state
.skipping
&& !(dir
->flags
& COND
))
494 else if (dname
->type
== CPP_EOF
)
495 ; /* CPP_EOF is the "null directive". */
498 /* An unknown directive. Don't complain about it in assembly
499 source: we don't know where the comments are, and # may
500 introduce assembler pseudo-ops. Don't complain about invalid
501 directives in skipped conditional groups (6.10 p4). */
502 if (CPP_OPTION (pfile
, lang
) == CLK_ASM
)
504 else if (!pfile
->state
.skipping
)
506 const char *unrecognized
507 = (const char *)cpp_token_as_text (pfile
, dname
);
508 const char *hint
= NULL
;
510 /* Call back into gcc to get a spelling suggestion. Ideally
511 we'd just use best_match from gcc/spellcheck.h (and filter
512 out the uncommon directives), but that requires moving it
513 to a support library. */
514 if (pfile
->cb
.get_suggestion
)
515 hint
= pfile
->cb
.get_suggestion (pfile
, unrecognized
,
520 rich_location
richloc (pfile
->line_table
, dname
->src_loc
);
521 source_range misspelled_token_range
522 = get_range_from_loc (pfile
->line_table
, dname
->src_loc
);
523 richloc
.add_fixit_replace (misspelled_token_range
, hint
);
524 cpp_error_at (pfile
, CPP_DL_ERROR
, &richloc
,
525 "invalid preprocessing directive #%s;"
526 " did you mean #%s?",
530 cpp_error (pfile
, CPP_DL_ERROR
,
531 "invalid preprocessing directive #%s",
536 pfile
->directive
= dir
;
537 if (CPP_OPTION (pfile
, traditional
))
538 prepare_directive_trad (pfile
);
541 pfile
->directive
->handler (pfile
);
543 _cpp_backup_tokens (pfile
, 1);
545 end_directive (pfile
, skip
);
546 if (was_parsing_args
&& !pfile
->state
.in_deferred_pragma
)
548 /* Restore state when within macro args. */
549 pfile
->state
.parsing_args
= 2;
550 pfile
->state
.prevent_expansion
= 1;
552 if (was_discarding_output
)
553 pfile
->state
.prevent_expansion
= 1;
557 /* Directive handler wrapper used by the command line option
558 processor. BUF is \n terminated. */
560 run_directive (cpp_reader
*pfile
, int dir_no
, const char *buf
, size_t count
)
562 cpp_push_buffer (pfile
, (const uchar
*) buf
, count
,
563 /* from_stage3 */ true);
564 start_directive (pfile
);
566 /* This is a short-term fix to prevent a leading '#' being
567 interpreted as a directive. */
568 _cpp_clean_line (pfile
);
570 pfile
->directive
= &dtable
[dir_no
];
571 if (CPP_OPTION (pfile
, traditional
))
572 prepare_directive_trad (pfile
);
573 pfile
->directive
->handler (pfile
);
574 end_directive (pfile
, 1);
575 _cpp_pop_buffer (pfile
);
578 /* Checks for validity the macro name in #define, #undef, #ifdef and
579 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
580 processing a #define or #undefine directive, and false
582 static cpp_hashnode
*
583 lex_macro_node (cpp_reader
*pfile
, bool is_def_or_undef
)
585 const cpp_token
*token
= _cpp_lex_token (pfile
);
587 /* The token immediately after #define must be an identifier. That
588 identifier may not be "defined", per C99 6.10.8p4.
589 In C++, it may not be any of the "named operators" either,
590 per C++98 [lex.digraph], [lex.key].
591 Finally, the identifier may not have been poisoned. (In that case
592 the lexer has issued the error message for us.) */
594 if (token
->type
== CPP_NAME
)
596 cpp_hashnode
*node
= token
->val
.node
.node
;
599 && (node
== pfile
->spec_nodes
.n_defined
600 || node
== pfile
->spec_nodes
.n__has_include
601 || node
== pfile
->spec_nodes
.n__has_include_next
))
602 cpp_error (pfile
, CPP_DL_ERROR
,
603 "\"%s\" cannot be used as a macro name",
605 else if (! (node
->flags
& NODE_POISONED
))
608 else if (token
->flags
& NAMED_OP
)
609 cpp_error (pfile
, CPP_DL_ERROR
,
610 "\"%s\" cannot be used as a macro name as it is an operator in C++",
611 NODE_NAME (token
->val
.node
.node
));
612 else if (token
->type
== CPP_EOF
)
613 cpp_error (pfile
, CPP_DL_ERROR
, "no macro name given in #%s directive",
614 pfile
->directive
->name
);
616 cpp_error (pfile
, CPP_DL_ERROR
, "macro names must be identifiers");
621 /* Process a #define directive. Most work is done in macro.c. */
623 do_define (cpp_reader
*pfile
)
625 cpp_hashnode
*node
= lex_macro_node (pfile
, true);
629 /* If we have been requested to expand comments into macros,
630 then re-enable saving of comments. */
631 pfile
->state
.save_comments
=
632 ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
);
634 if (pfile
->cb
.before_define
)
635 pfile
->cb
.before_define (pfile
);
637 if (_cpp_create_definition (pfile
, node
))
638 if (pfile
->cb
.define
)
639 pfile
->cb
.define (pfile
, pfile
->directive_line
, node
);
641 node
->flags
&= ~NODE_USED
;
645 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
647 do_undef (cpp_reader
*pfile
)
649 cpp_hashnode
*node
= lex_macro_node (pfile
, true);
653 if (pfile
->cb
.before_define
)
654 pfile
->cb
.before_define (pfile
);
657 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
659 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
660 identifier is not currently defined as a macro name. */
661 if (cpp_macro_p (node
))
663 if (node
->flags
& NODE_WARN
)
664 cpp_error (pfile
, CPP_DL_WARNING
,
665 "undefining \"%s\"", NODE_NAME (node
));
666 else if (cpp_builtin_macro_p (node
)
667 && CPP_OPTION (pfile
, warn_builtin_macro_redefined
))
668 cpp_warning_with_line (pfile
, CPP_W_BUILTIN_MACRO_REDEFINED
,
669 pfile
->directive_line
, 0,
670 "undefining \"%s\"", NODE_NAME (node
));
672 if (CPP_OPTION (pfile
, warn_unused_macros
))
673 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
675 _cpp_free_definition (node
);
679 check_eol (pfile
, false);
682 /* Undefine a single macro/assertion/whatever. */
685 undefine_macros (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*h
,
686 void *data_p ATTRIBUTE_UNUSED
)
688 /* Body of _cpp_free_definition inlined here for speed.
689 Macros and assertions no longer have anything to free. */
691 h
->value
.answers
= NULL
;
692 h
->flags
&= ~(NODE_POISONED
|NODE_DISABLED
|NODE_USED
);
696 /* Undefine all macros and assertions. */
699 cpp_undef_all (cpp_reader
*pfile
)
701 cpp_forall_identifiers (pfile
, undefine_macros
, NULL
);
705 /* Helper routine used by parse_include. Reinterpret the current line
706 as an h-char-sequence (< ... >); we are looking at the first token
707 after the <. Returns a malloced filename. */
709 glue_header_name (cpp_reader
*pfile
)
711 const cpp_token
*token
;
713 size_t len
, total_len
= 0, capacity
= 1024;
715 /* To avoid lexed tokens overwriting our glued name, we can only
716 allocate from the string pool once we've lexed everything. */
717 buffer
= XNEWVEC (char, capacity
);
720 token
= get_token_no_padding (pfile
);
722 if (token
->type
== CPP_GREATER
)
724 if (token
->type
== CPP_EOF
)
726 cpp_error (pfile
, CPP_DL_ERROR
, "missing terminating > character");
730 len
= cpp_token_len (token
) + 2; /* Leading space, terminating \0. */
731 if (total_len
+ len
> capacity
)
733 capacity
= (capacity
+ len
) * 2;
734 buffer
= XRESIZEVEC (char, buffer
, capacity
);
737 if (token
->flags
& PREV_WHITE
)
738 buffer
[total_len
++] = ' ';
740 total_len
= (cpp_spell_token (pfile
, token
, (uchar
*) &buffer
[total_len
],
745 buffer
[total_len
] = '\0';
749 /* Returns the file name of #include, #include_next, #import and
750 #pragma dependency. The string is malloced and the caller should
751 free it. Returns NULL on error. LOCATION is the source location
755 parse_include (cpp_reader
*pfile
, int *pangle_brackets
,
756 const cpp_token
***buf
, location_t
*location
)
759 const cpp_token
*header
;
761 /* Allow macro expansion. */
762 header
= get_token_no_padding (pfile
);
763 *location
= header
->src_loc
;
764 if ((header
->type
== CPP_STRING
&& header
->val
.str
.text
[0] != 'R')
765 || header
->type
== CPP_HEADER_NAME
)
767 fname
= XNEWVEC (char, header
->val
.str
.len
- 1);
768 memcpy (fname
, header
->val
.str
.text
+ 1, header
->val
.str
.len
- 2);
769 fname
[header
->val
.str
.len
- 2] = '\0';
770 *pangle_brackets
= header
->type
== CPP_HEADER_NAME
;
772 else if (header
->type
== CPP_LESS
)
774 fname
= glue_header_name (pfile
);
775 *pangle_brackets
= 1;
779 const unsigned char *dir
;
781 if (pfile
->directive
== &dtable
[T_PRAGMA
])
782 dir
= UC
"pragma dependency";
784 dir
= pfile
->directive
->name
;
785 cpp_error (pfile
, CPP_DL_ERROR
, "#%s expects \"FILENAME\" or <FILENAME>",
791 if (pfile
->directive
== &dtable
[T_PRAGMA
])
793 /* This pragma allows extra tokens after the file name. */
795 else if (buf
== NULL
|| CPP_OPTION (pfile
, discard_comments
))
796 check_eol (pfile
, true);
799 /* If we are not discarding comments, then gather them while
800 doing the eol check. */
801 *buf
= check_eol_return_comments (pfile
);
807 /* Handle #include, #include_next and #import. */
809 do_include_common (cpp_reader
*pfile
, enum include_type type
)
813 const cpp_token
**buf
= NULL
;
816 /* Re-enable saving of comments if requested, so that the include
817 callback can dump comments which follow #include. */
818 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
820 /* Tell the lexer this is an include directive -- we want it to
821 increment the line number even if this is the last line of a file. */
822 pfile
->state
.in_directive
= 2;
824 fname
= parse_include (pfile
, &angle_brackets
, &buf
, &location
);
830 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
831 "empty filename in #%s",
832 pfile
->directive
->name
);
836 /* Prevent #include recursion. */
837 if (pfile
->line_table
->depth
>= CPP_OPTION (pfile
, max_include_depth
))
840 "#include nested depth %u exceeds maximum of %u"
841 " (use -fmax-include-depth=DEPTH to increase the maximum)",
842 pfile
->line_table
->depth
,
843 CPP_OPTION (pfile
, max_include_depth
));
846 /* Get out of macro context, if we are. */
847 skip_rest_of_line (pfile
);
849 if (pfile
->cb
.include
)
850 pfile
->cb
.include (pfile
, pfile
->directive_line
,
851 pfile
->directive
->name
, fname
, angle_brackets
,
854 _cpp_stack_include (pfile
, fname
, angle_brackets
, type
, location
);
864 do_include (cpp_reader
*pfile
)
866 do_include_common (pfile
, IT_INCLUDE
);
870 do_import (cpp_reader
*pfile
)
872 do_include_common (pfile
, IT_IMPORT
);
876 do_include_next (cpp_reader
*pfile
)
878 enum include_type type
= IT_INCLUDE_NEXT
;
880 /* If this is the primary source file, warn and use the normal
882 if (cpp_in_primary_file (pfile
))
884 cpp_error (pfile
, CPP_DL_WARNING
,
885 "#include_next in primary source file");
888 do_include_common (pfile
, type
);
891 /* Subroutine of do_linemarker. Read possible flags after file name.
892 LAST is the last flag seen; 0 if this is the first flag. Return the
893 flag if it is valid, 0 at the end of the directive. Otherwise
896 read_flag (cpp_reader
*pfile
, unsigned int last
)
898 const cpp_token
*token
= _cpp_lex_token (pfile
);
900 if (token
->type
== CPP_NUMBER
&& token
->val
.str
.len
== 1)
902 unsigned int flag
= token
->val
.str
.text
[0] - '0';
904 if (flag
> last
&& flag
<= 4
905 && (flag
!= 4 || last
== 3)
906 && (flag
!= 2 || last
== 0))
910 if (token
->type
!= CPP_EOF
)
911 cpp_error (pfile
, CPP_DL_ERROR
, "invalid flag \"%s\" in line directive",
912 cpp_token_as_text (pfile
, token
));
916 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
917 of length LEN, to binary; store it in NUMP, and return false if the
918 number was well-formed, true if not. WRAPPED is set to true if the
919 number did not fit into 'unsigned long'. */
921 strtolinenum (const uchar
*str
, size_t len
, linenum_type
*nump
, bool *wrapped
)
923 linenum_type reg
= 0;
924 linenum_type reg_prev
= 0;
943 /* Interpret #line command.
944 Note that the filename string (if any) is a true string constant
945 (escapes are interpreted), unlike in #line. */
947 do_line (cpp_reader
*pfile
)
949 class line_maps
*line_table
= pfile
->line_table
;
950 const line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (line_table
);
952 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
955 unsigned char map_sysp
= ORDINARY_MAP_IN_SYSTEM_HEADER_P (map
);
956 const cpp_token
*token
;
957 const char *new_file
= ORDINARY_MAP_FILE_NAME (map
);
958 linenum_type new_lineno
;
960 /* C99 raised the minimum limit on #line numbers. */
961 linenum_type cap
= CPP_OPTION (pfile
, c99
) ? 2147483647 : 32767;
964 /* #line commands expand macros. */
965 token
= cpp_get_token (pfile
);
966 if (token
->type
!= CPP_NUMBER
967 || strtolinenum (token
->val
.str
.text
, token
->val
.str
.len
,
968 &new_lineno
, &wrapped
))
970 if (token
->type
== CPP_EOF
)
971 cpp_error (pfile
, CPP_DL_ERROR
, "unexpected end of file after #line");
973 cpp_error (pfile
, CPP_DL_ERROR
,
974 "\"%s\" after #line is not a positive integer",
975 cpp_token_as_text (pfile
, token
));
979 if (CPP_PEDANTIC (pfile
) && (new_lineno
== 0 || new_lineno
> cap
|| wrapped
))
980 cpp_error (pfile
, CPP_DL_PEDWARN
, "line number out of range");
982 cpp_error (pfile
, CPP_DL_WARNING
, "line number out of range");
984 token
= cpp_get_token (pfile
);
985 if (token
->type
== CPP_STRING
)
987 cpp_string s
= { 0, 0 };
988 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
, 1,
990 new_file
= (const char *)s
.text
;
991 check_eol (pfile
, true);
993 else if (token
->type
!= CPP_EOF
)
995 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
996 cpp_token_as_text (pfile
, token
));
1000 skip_rest_of_line (pfile
);
1001 _cpp_do_file_change (pfile
, LC_RENAME_VERBATIM
, new_file
, new_lineno
,
1003 line_table
->seen_line_directive
= true;
1006 /* Interpret the # 44 "file" [flags] notation, which has slightly
1007 different syntax and semantics from #line: Flags are allowed,
1008 and we never complain about the line number being too big. */
1010 do_linemarker (cpp_reader
*pfile
)
1012 class line_maps
*line_table
= pfile
->line_table
;
1013 const line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (line_table
);
1014 const cpp_token
*token
;
1015 const char *new_file
= ORDINARY_MAP_FILE_NAME (map
);
1016 linenum_type new_lineno
;
1017 unsigned int new_sysp
= ORDINARY_MAP_IN_SYSTEM_HEADER_P (map
);
1018 enum lc_reason reason
= LC_RENAME_VERBATIM
;
1022 /* Back up so we can get the number again. Putting this in
1023 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
1024 some circumstances, which can segfault. */
1025 _cpp_backup_tokens (pfile
, 1);
1027 /* #line commands expand macros. */
1028 token
= cpp_get_token (pfile
);
1029 if (token
->type
!= CPP_NUMBER
1030 || strtolinenum (token
->val
.str
.text
, token
->val
.str
.len
,
1031 &new_lineno
, &wrapped
))
1033 /* Unlike #line, there does not seem to be a way to get an EOF
1034 here. So, it should be safe to always spell the token. */
1035 cpp_error (pfile
, CPP_DL_ERROR
,
1036 "\"%s\" after # is not a positive integer",
1037 cpp_token_as_text (pfile
, token
));
1041 token
= cpp_get_token (pfile
);
1042 if (token
->type
== CPP_STRING
)
1044 cpp_string s
= { 0, 0 };
1045 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
,
1047 new_file
= (const char *)s
.text
;
1050 flag
= read_flag (pfile
, 0);
1054 /* Fake an include for cpp_included (). */
1055 _cpp_fake_include (pfile
, new_file
);
1056 flag
= read_flag (pfile
, flag
);
1061 flag
= read_flag (pfile
, flag
);
1066 flag
= read_flag (pfile
, flag
);
1070 pfile
->buffer
->sysp
= new_sysp
;
1072 check_eol (pfile
, false);
1074 else if (token
->type
!= CPP_EOF
)
1076 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
1077 cpp_token_as_text (pfile
, token
));
1081 skip_rest_of_line (pfile
);
1083 if (reason
== LC_LEAVE
)
1085 /* Reread map since cpp_get_token can invalidate it with a
1087 map
= LINEMAPS_LAST_ORDINARY_MAP (line_table
);
1088 const line_map_ordinary
*from
1089 = linemap_included_from_linemap (line_table
, map
);
1093 else if (!new_file
[0])
1094 /* Leaving to "" means fill in the popped-to name. */
1095 new_file
= ORDINARY_MAP_FILE_NAME (from
);
1096 else if (filename_cmp (ORDINARY_MAP_FILE_NAME (from
), new_file
) != 0)
1097 /* It's the wrong name, Grommit! */
1102 cpp_warning (pfile
, CPP_W_NONE
,
1103 "file \"%s\" linemarker ignored due to "
1104 "incorrect nesting", new_file
);
1109 /* Compensate for the increment in linemap_add that occurs in
1110 _cpp_do_file_change. We're currently at the start of the line
1111 *following* the #line directive. A separate location_t for this
1112 location makes no sense (until we do the LC_LEAVE), and
1113 complicates LAST_SOURCE_LINE_LOCATION. */
1114 pfile
->line_table
->highest_location
--;
1116 _cpp_do_file_change (pfile
, reason
, new_file
, new_lineno
, new_sysp
);
1117 line_table
->seen_line_directive
= true;
1120 /* Arrange the file_change callback. pfile->line has changed to
1121 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
1122 header, 2 for a system header that needs to be extern "C" protected,
1123 and zero otherwise. */
1125 _cpp_do_file_change (cpp_reader
*pfile
, enum lc_reason reason
,
1126 const char *to_file
, linenum_type file_line
,
1129 linemap_assert (reason
!= LC_ENTER_MACRO
);
1130 const struct line_map
*map
= linemap_add (pfile
->line_table
, reason
, sysp
,
1131 to_file
, file_line
);
1132 const line_map_ordinary
*ord_map
= NULL
;
1135 ord_map
= linemap_check_ordinary (map
);
1136 linemap_line_start (pfile
->line_table
,
1137 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map
),
1141 if (pfile
->cb
.file_change
)
1142 pfile
->cb
.file_change (pfile
, ord_map
);
1145 /* Report a warning or error detected by the program we are
1146 processing. Use the directive's tokens in the error message. */
1148 do_diagnostic (cpp_reader
*pfile
, enum cpp_diagnostic_level code
,
1149 enum cpp_warning_reason reason
, int print_dir
)
1151 const unsigned char *dir_name
;
1152 unsigned char *line
;
1153 location_t src_loc
= pfile
->cur_token
[-1].src_loc
;
1156 dir_name
= pfile
->directive
->name
;
1159 pfile
->state
.prevent_expansion
++;
1160 line
= cpp_output_line_to_string (pfile
, dir_name
);
1161 pfile
->state
.prevent_expansion
--;
1163 if (code
== CPP_DL_WARNING_SYSHDR
&& reason
)
1164 cpp_warning_with_line_syshdr (pfile
, reason
, src_loc
, 0, "%s", line
);
1165 else if (code
== CPP_DL_WARNING
&& reason
)
1166 cpp_warning_with_line (pfile
, reason
, src_loc
, 0, "%s", line
);
1168 cpp_error_with_line (pfile
, code
, src_loc
, 0, "%s", line
);
1173 do_error (cpp_reader
*pfile
)
1175 do_diagnostic (pfile
, CPP_DL_ERROR
, CPP_W_NONE
, 1);
1179 do_warning (cpp_reader
*pfile
)
1181 /* We want #warning diagnostics to be emitted in system headers too. */
1182 do_diagnostic (pfile
, CPP_DL_WARNING_SYSHDR
, CPP_W_WARNING_DIRECTIVE
, 1);
1185 /* Report program identification. */
1187 do_ident (cpp_reader
*pfile
)
1189 const cpp_token
*str
= cpp_get_token (pfile
);
1191 if (str
->type
!= CPP_STRING
)
1192 cpp_error (pfile
, CPP_DL_ERROR
, "invalid #%s directive",
1193 pfile
->directive
->name
);
1194 else if (pfile
->cb
.ident
)
1195 pfile
->cb
.ident (pfile
, pfile
->directive_line
, &str
->val
.str
);
1197 check_eol (pfile
, false);
1200 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1201 matching entry, or NULL if none is found. The returned entry could
1202 be the start of a namespace chain, or a pragma. */
1203 static struct pragma_entry
*
1204 lookup_pragma_entry (struct pragma_entry
*chain
, const cpp_hashnode
*pragma
)
1206 while (chain
&& chain
->pragma
!= pragma
)
1207 chain
= chain
->next
;
1212 /* Create and insert a blank pragma entry at the beginning of a
1213 singly-linked CHAIN. */
1214 static struct pragma_entry
*
1215 new_pragma_entry (cpp_reader
*pfile
, struct pragma_entry
**chain
)
1217 struct pragma_entry
*new_entry
;
1219 new_entry
= (struct pragma_entry
*)
1220 _cpp_aligned_alloc (pfile
, sizeof (struct pragma_entry
));
1222 memset (new_entry
, 0, sizeof (struct pragma_entry
));
1223 new_entry
->next
= *chain
;
1229 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1230 goes in the global namespace. */
1231 static struct pragma_entry
*
1232 register_pragma_1 (cpp_reader
*pfile
, const char *space
, const char *name
,
1233 bool allow_name_expansion
)
1235 struct pragma_entry
**chain
= &pfile
->pragmas
;
1236 struct pragma_entry
*entry
;
1237 const cpp_hashnode
*node
;
1241 node
= cpp_lookup (pfile
, UC space
, strlen (space
));
1242 entry
= lookup_pragma_entry (*chain
, node
);
1245 entry
= new_pragma_entry (pfile
, chain
);
1246 entry
->pragma
= node
;
1247 entry
->is_nspace
= true;
1248 entry
->allow_expansion
= allow_name_expansion
;
1250 else if (!entry
->is_nspace
)
1252 else if (entry
->allow_expansion
!= allow_name_expansion
)
1254 cpp_error (pfile
, CPP_DL_ICE
,
1255 "registering pragmas in namespace \"%s\" with mismatched "
1256 "name expansion", space
);
1259 chain
= &entry
->u
.space
;
1261 else if (allow_name_expansion
)
1263 cpp_error (pfile
, CPP_DL_ICE
,
1264 "registering pragma \"%s\" with name expansion "
1265 "and no namespace", name
);
1269 /* Check for duplicates. */
1270 node
= cpp_lookup (pfile
, UC name
, strlen (name
));
1271 entry
= lookup_pragma_entry (*chain
, node
);
1274 entry
= new_pragma_entry (pfile
, chain
);
1275 entry
->pragma
= node
;
1279 if (entry
->is_nspace
)
1281 cpp_error (pfile
, CPP_DL_ICE
,
1282 "registering \"%s\" as both a pragma and a pragma namespace",
1285 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s %s is already registered",
1288 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s is already registered", name
);
1293 /* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1295 register_pragma_internal (cpp_reader
*pfile
, const char *space
,
1296 const char *name
, pragma_cb handler
)
1298 struct pragma_entry
*entry
;
1300 entry
= register_pragma_1 (pfile
, space
, name
, false);
1301 entry
->is_internal
= true;
1302 entry
->u
.handler
= handler
;
1305 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1306 goes in the global namespace. HANDLER is the handler it will call,
1307 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1308 expansion while parsing pragma NAME. This function is exported
1311 cpp_register_pragma (cpp_reader
*pfile
, const char *space
, const char *name
,
1312 pragma_cb handler
, bool allow_expansion
)
1314 struct pragma_entry
*entry
;
1318 cpp_error (pfile
, CPP_DL_ICE
, "registering pragma with NULL handler");
1322 entry
= register_pragma_1 (pfile
, space
, name
, false);
1325 entry
->allow_expansion
= allow_expansion
;
1326 entry
->u
.handler
= handler
;
1330 /* Similarly, but create mark the pragma for deferred processing.
1331 When found, a CPP_PRAGMA token will be insertted into the stream
1332 with IDENT in the token->u.pragma slot. */
1334 cpp_register_deferred_pragma (cpp_reader
*pfile
, const char *space
,
1335 const char *name
, unsigned int ident
,
1336 bool allow_expansion
, bool allow_name_expansion
)
1338 struct pragma_entry
*entry
;
1340 entry
= register_pragma_1 (pfile
, space
, name
, allow_name_expansion
);
1343 entry
->is_deferred
= true;
1344 entry
->allow_expansion
= allow_expansion
;
1345 entry
->u
.ident
= ident
;
1349 /* Register the pragmas the preprocessor itself handles. */
1351 _cpp_init_internal_pragmas (cpp_reader
*pfile
)
1353 /* Pragmas in the global namespace. */
1354 register_pragma_internal (pfile
, 0, "once", do_pragma_once
);
1355 register_pragma_internal (pfile
, 0, "push_macro", do_pragma_push_macro
);
1356 register_pragma_internal (pfile
, 0, "pop_macro", do_pragma_pop_macro
);
1358 /* New GCC-specific pragmas should be put in the GCC namespace. */
1359 register_pragma_internal (pfile
, "GCC", "poison", do_pragma_poison
);
1360 register_pragma_internal (pfile
, "GCC", "system_header",
1361 do_pragma_system_header
);
1362 register_pragma_internal (pfile
, "GCC", "dependency", do_pragma_dependency
);
1363 register_pragma_internal (pfile
, "GCC", "warning", do_pragma_warning
);
1364 register_pragma_internal (pfile
, "GCC", "error", do_pragma_error
);
1367 /* Return the number of registered pragmas in PE. */
1370 count_registered_pragmas (struct pragma_entry
*pe
)
1373 for (; pe
!= NULL
; pe
= pe
->next
)
1376 ct
+= count_registered_pragmas (pe
->u
.space
);
1382 /* Save into SD the names of the registered pragmas referenced by PE,
1383 and return a pointer to the next free space in SD. */
1386 save_registered_pragmas (struct pragma_entry
*pe
, char **sd
)
1388 for (; pe
!= NULL
; pe
= pe
->next
)
1391 sd
= save_registered_pragmas (pe
->u
.space
, sd
);
1392 *sd
++ = (char *) xmemdup (HT_STR (&pe
->pragma
->ident
),
1393 HT_LEN (&pe
->pragma
->ident
),
1394 HT_LEN (&pe
->pragma
->ident
) + 1);
1399 /* Return a newly-allocated array which saves the names of the
1400 registered pragmas. */
1403 _cpp_save_pragma_names (cpp_reader
*pfile
)
1405 int ct
= count_registered_pragmas (pfile
->pragmas
);
1406 char **result
= XNEWVEC (char *, ct
);
1407 (void) save_registered_pragmas (pfile
->pragmas
, result
);
1411 /* Restore from SD the names of the registered pragmas referenced by PE,
1412 and return a pointer to the next unused name in SD. */
1415 restore_registered_pragmas (cpp_reader
*pfile
, struct pragma_entry
*pe
,
1418 for (; pe
!= NULL
; pe
= pe
->next
)
1421 sd
= restore_registered_pragmas (pfile
, pe
->u
.space
, sd
);
1422 pe
->pragma
= cpp_lookup (pfile
, UC
*sd
, strlen (*sd
));
1429 /* Restore the names of the registered pragmas from SAVED. */
1432 _cpp_restore_pragma_names (cpp_reader
*pfile
, char **saved
)
1434 (void) restore_registered_pragmas (pfile
, pfile
->pragmas
, saved
);
1438 /* Pragmata handling. We handle some, and pass the rest on to the
1439 front end. C99 defines three pragmas and says that no macro
1440 expansion is to be performed on them; whether or not macro
1441 expansion happens for other pragmas is implementation defined.
1442 This implementation allows for a mix of both, since GCC did not
1443 traditionally macro expand its (few) pragmas, whereas OpenMP
1444 specifies that macro expansion should happen. */
1446 do_pragma (cpp_reader
*pfile
)
1448 const struct pragma_entry
*p
= NULL
;
1449 const cpp_token
*token
, *pragma_token
;
1450 location_t pragma_token_virt_loc
= 0;
1452 unsigned int count
= 1;
1454 pfile
->state
.prevent_expansion
++;
1456 pragma_token
= token
= cpp_get_token_with_location (pfile
,
1457 &pragma_token_virt_loc
);
1459 if (token
->type
== CPP_NAME
)
1461 p
= lookup_pragma_entry (pfile
->pragmas
, token
->val
.node
.node
);
1462 if (p
&& p
->is_nspace
)
1464 bool allow_name_expansion
= p
->allow_expansion
;
1465 if (allow_name_expansion
)
1466 pfile
->state
.prevent_expansion
--;
1468 token
= cpp_get_token (pfile
);
1469 if (token
->type
== CPP_NAME
)
1470 p
= lookup_pragma_entry (p
->u
.space
, token
->val
.node
.node
);
1473 if (allow_name_expansion
)
1474 pfile
->state
.prevent_expansion
++;
1483 pfile
->directive_result
.src_loc
= pragma_token_virt_loc
;
1484 pfile
->directive_result
.type
= CPP_PRAGMA
;
1485 pfile
->directive_result
.flags
= pragma_token
->flags
;
1486 pfile
->directive_result
.val
.pragma
= p
->u
.ident
;
1487 pfile
->state
.in_deferred_pragma
= true;
1488 pfile
->state
.pragma_allow_expansion
= p
->allow_expansion
;
1489 if (!p
->allow_expansion
)
1490 pfile
->state
.prevent_expansion
++;
1494 /* Since the handler below doesn't get the line number, that
1495 it might need for diagnostics, make sure it has the right
1496 numbers in place. */
1497 if (pfile
->cb
.line_change
)
1498 (*pfile
->cb
.line_change
) (pfile
, pragma_token
, false);
1499 if (p
->allow_expansion
)
1500 pfile
->state
.prevent_expansion
--;
1501 (*p
->u
.handler
) (pfile
);
1502 if (p
->allow_expansion
)
1503 pfile
->state
.prevent_expansion
++;
1506 else if (pfile
->cb
.def_pragma
)
1508 if (count
== 1 || pfile
->context
->prev
== NULL
)
1509 _cpp_backup_tokens (pfile
, count
);
1512 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1513 won't allow backing 2 tokens. */
1514 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1515 reads both tokens, we could perhaps free it, but if it doesn't,
1516 we don't know the exact lifespan. */
1517 cpp_token
*toks
= XNEWVEC (cpp_token
, 2);
1519 toks
[0].flags
|= NO_EXPAND
;
1521 toks
[1].flags
|= NO_EXPAND
;
1522 _cpp_push_token_context (pfile
, NULL
, toks
, 2);
1524 pfile
->cb
.def_pragma (pfile
, pfile
->directive_line
);
1527 pfile
->state
.prevent_expansion
--;
1530 /* Handle #pragma once. */
1532 do_pragma_once (cpp_reader
*pfile
)
1534 if (cpp_in_primary_file (pfile
))
1535 cpp_error (pfile
, CPP_DL_WARNING
, "#pragma once in main file");
1537 check_eol (pfile
, false);
1538 _cpp_mark_file_once_only (pfile
, pfile
->buffer
->file
);
1541 /* Handle #pragma push_macro(STRING). */
1543 do_pragma_push_macro (cpp_reader
*pfile
)
1547 const uchar
*defn
= NULL
;
1548 char *macroname
, *dest
;
1549 const char *limit
, *src
;
1550 const cpp_token
*txt
;
1551 struct def_pragma_macro
*c
;
1553 txt
= get__Pragma_string (pfile
);
1556 location_t src_loc
= pfile
->cur_token
[-1].src_loc
;
1557 cpp_error_with_line (pfile
, CPP_DL_ERROR
, src_loc
, 0,
1558 "invalid #pragma push_macro directive");
1559 check_eol (pfile
, false);
1560 skip_rest_of_line (pfile
);
1563 dest
= macroname
= (char *) alloca (txt
->val
.str
.len
+ 2);
1564 src
= (const char *) (txt
->val
.str
.text
+ 1 + (txt
->val
.str
.text
[0] == 'L'));
1565 limit
= (const char *) (txt
->val
.str
.text
+ txt
->val
.str
.len
- 1);
1568 /* We know there is a character following the backslash. */
1569 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1574 check_eol (pfile
, false);
1575 skip_rest_of_line (pfile
);
1576 c
= XNEW (struct def_pragma_macro
);
1577 memset (c
, 0, sizeof (struct def_pragma_macro
));
1578 c
->name
= XNEWVAR (char, strlen (macroname
) + 1);
1579 strcpy (c
->name
, macroname
);
1580 c
->next
= pfile
->pushed_macros
;
1581 node
= _cpp_lex_identifier (pfile
, c
->name
);
1582 if (node
->type
== NT_VOID
)
1584 else if (node
->type
== NT_BUILTIN_MACRO
)
1588 defn
= cpp_macro_definition (pfile
, node
);
1589 defnlen
= ustrlen (defn
);
1590 c
->definition
= XNEWVEC (uchar
, defnlen
+ 2);
1591 c
->definition
[defnlen
] = '\n';
1592 c
->definition
[defnlen
+ 1] = 0;
1593 c
->line
= node
->value
.macro
->line
;
1594 c
->syshdr
= node
->value
.macro
->syshdr
;
1595 c
->used
= node
->value
.macro
->used
;
1596 memcpy (c
->definition
, defn
, defnlen
);
1599 pfile
->pushed_macros
= c
;
1602 /* Handle #pragma pop_macro(STRING). */
1604 do_pragma_pop_macro (cpp_reader
*pfile
)
1606 char *macroname
, *dest
;
1607 const char *limit
, *src
;
1608 const cpp_token
*txt
;
1609 struct def_pragma_macro
*l
= NULL
, *c
= pfile
->pushed_macros
;
1610 txt
= get__Pragma_string (pfile
);
1613 location_t src_loc
= pfile
->cur_token
[-1].src_loc
;
1614 cpp_error_with_line (pfile
, CPP_DL_ERROR
, src_loc
, 0,
1615 "invalid #pragma pop_macro directive");
1616 check_eol (pfile
, false);
1617 skip_rest_of_line (pfile
);
1620 dest
= macroname
= (char *) alloca (txt
->val
.str
.len
+ 2);
1621 src
= (const char *) (txt
->val
.str
.text
+ 1 + (txt
->val
.str
.text
[0] == 'L'));
1622 limit
= (const char *) (txt
->val
.str
.text
+ txt
->val
.str
.len
- 1);
1625 /* We know there is a character following the backslash. */
1626 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1631 check_eol (pfile
, false);
1632 skip_rest_of_line (pfile
);
1636 if (!strcmp (c
->name
, macroname
))
1639 pfile
->pushed_macros
= c
->next
;
1642 cpp_pop_definition (pfile
, c
);
1643 free (c
->definition
);
1653 /* Handle #pragma GCC poison, to poison one or more identifiers so
1654 that the lexer produces a hard error for each subsequent usage. */
1656 do_pragma_poison (cpp_reader
*pfile
)
1658 const cpp_token
*tok
;
1661 pfile
->state
.poisoned_ok
= 1;
1664 tok
= _cpp_lex_token (pfile
);
1665 if (tok
->type
== CPP_EOF
)
1667 if (tok
->type
!= CPP_NAME
)
1669 cpp_error (pfile
, CPP_DL_ERROR
,
1670 "invalid #pragma GCC poison directive");
1674 hp
= tok
->val
.node
.node
;
1675 if (hp
->flags
& NODE_POISONED
)
1678 if (cpp_macro_p (hp
))
1679 cpp_error (pfile
, CPP_DL_WARNING
, "poisoning existing macro \"%s\"",
1681 _cpp_free_definition (hp
);
1682 hp
->flags
|= NODE_POISONED
| NODE_DIAGNOSTIC
;
1684 pfile
->state
.poisoned_ok
= 0;
1687 /* Mark the current header as a system header. This will suppress
1688 some categories of warnings (notably those from -pedantic). It is
1689 intended for use in system libraries that cannot be implemented in
1690 conforming C, but cannot be certain that their headers appear in a
1691 system include directory. To prevent abuse, it is rejected in the
1692 primary source file. */
1694 do_pragma_system_header (cpp_reader
*pfile
)
1696 if (cpp_in_primary_file (pfile
))
1697 cpp_error (pfile
, CPP_DL_WARNING
,
1698 "#pragma system_header ignored outside include file");
1701 check_eol (pfile
, false);
1702 skip_rest_of_line (pfile
);
1703 cpp_make_system_header (pfile
, 1, 0);
1707 /* Check the modified date of the current include file against a specified
1708 file. Issue a diagnostic, if the specified file is newer. We use this to
1709 determine if a fixed header should be refixed. */
1711 do_pragma_dependency (cpp_reader
*pfile
)
1714 int angle_brackets
, ordering
;
1715 location_t location
;
1717 fname
= parse_include (pfile
, &angle_brackets
, NULL
, &location
);
1721 ordering
= _cpp_compare_file_date (pfile
, fname
, angle_brackets
);
1723 cpp_error (pfile
, CPP_DL_WARNING
, "cannot find source file %s", fname
);
1724 else if (ordering
> 0)
1726 cpp_error (pfile
, CPP_DL_WARNING
,
1727 "current file is older than %s", fname
);
1728 if (cpp_get_token (pfile
)->type
!= CPP_EOF
)
1730 _cpp_backup_tokens (pfile
, 1);
1731 do_diagnostic (pfile
, CPP_DL_WARNING
, CPP_W_NONE
, 0);
1735 free ((void *) fname
);
1738 /* Issue a diagnostic with the message taken from the pragma. If
1739 ERROR is true, the diagnostic is a warning, otherwise, it is an
1742 do_pragma_warning_or_error (cpp_reader
*pfile
, bool error
)
1744 const cpp_token
*tok
= _cpp_lex_token (pfile
);
1746 if (tok
->type
!= CPP_STRING
1747 || !cpp_interpret_string_notranslate (pfile
, &tok
->val
.str
, 1, &str
,
1751 cpp_error (pfile
, CPP_DL_ERROR
, "invalid \"#pragma GCC %s\" directive",
1752 error
? "error" : "warning");
1755 cpp_error (pfile
, error
? CPP_DL_ERROR
: CPP_DL_WARNING
,
1757 free ((void *)str
.text
);
1760 /* Issue a warning diagnostic. */
1762 do_pragma_warning (cpp_reader
*pfile
)
1764 do_pragma_warning_or_error (pfile
, false);
1767 /* Issue an error diagnostic. */
1769 do_pragma_error (cpp_reader
*pfile
)
1771 do_pragma_warning_or_error (pfile
, true);
1774 /* Get a token but skip padding. */
1775 static const cpp_token
*
1776 get_token_no_padding (cpp_reader
*pfile
)
1780 const cpp_token
*result
= cpp_get_token (pfile
);
1781 if (result
->type
!= CPP_PADDING
)
1786 /* Check syntax is "(string-literal)". Returns the string on success,
1787 or NULL on failure. */
1788 static const cpp_token
*
1789 get__Pragma_string (cpp_reader
*pfile
)
1791 const cpp_token
*string
;
1792 const cpp_token
*paren
;
1794 paren
= get_token_no_padding (pfile
);
1795 if (paren
->type
== CPP_EOF
)
1796 _cpp_backup_tokens (pfile
, 1);
1797 if (paren
->type
!= CPP_OPEN_PAREN
)
1800 string
= get_token_no_padding (pfile
);
1801 if (string
->type
== CPP_EOF
)
1802 _cpp_backup_tokens (pfile
, 1);
1803 if (string
->type
!= CPP_STRING
&& string
->type
!= CPP_WSTRING
1804 && string
->type
!= CPP_STRING32
&& string
->type
!= CPP_STRING16
1805 && string
->type
!= CPP_UTF8STRING
)
1808 paren
= get_token_no_padding (pfile
);
1809 if (paren
->type
== CPP_EOF
)
1810 _cpp_backup_tokens (pfile
, 1);
1811 if (paren
->type
!= CPP_CLOSE_PAREN
)
1817 /* Destringize IN into a temporary buffer, by removing the first \ of
1818 \" and \\ sequences, and process the result as a #pragma directive. */
1820 destringize_and_run (cpp_reader
*pfile
, const cpp_string
*in
,
1821 location_t expansion_loc
)
1823 const unsigned char *src
, *limit
;
1824 char *dest
, *result
;
1825 cpp_context
*saved_context
;
1826 cpp_token
*saved_cur_token
;
1827 tokenrun
*saved_cur_run
;
1830 const struct directive
*save_directive
;
1832 dest
= result
= (char *) alloca (in
->len
- 1);
1833 src
= in
->text
+ 1 + (in
->text
[0] == 'L');
1834 limit
= in
->text
+ in
->len
- 1;
1837 /* We know there is a character following the backslash. */
1838 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1844 /* Ugh; an awful kludge. We are really not set up to be lexing
1845 tokens when in the middle of a macro expansion. Use a new
1846 context to force cpp_get_token to lex, and so skip_rest_of_line
1847 doesn't go beyond the end of the text. Also, remember the
1848 current lexing position so we can return to it later.
1850 Something like line-at-a-time lexing should remove the need for
1852 saved_context
= pfile
->context
;
1853 saved_cur_token
= pfile
->cur_token
;
1854 saved_cur_run
= pfile
->cur_run
;
1856 pfile
->context
= XCNEW (cpp_context
);
1858 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1859 until we've read all of the tokens that we want. */
1860 cpp_push_buffer (pfile
, (const uchar
*) result
, dest
- result
,
1861 /* from_stage3 */ true);
1862 /* ??? Antique Disgusting Hack. What does this do? */
1863 if (pfile
->buffer
->prev
)
1864 pfile
->buffer
->file
= pfile
->buffer
->prev
->file
;
1866 start_directive (pfile
);
1867 _cpp_clean_line (pfile
);
1868 save_directive
= pfile
->directive
;
1869 pfile
->directive
= &dtable
[T_PRAGMA
];
1871 end_directive (pfile
, 1);
1872 pfile
->directive
= save_directive
;
1874 /* We always insert at least one token, the directive result. It'll
1875 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1876 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
1878 /* If we're not handling the pragma internally, read all of the tokens from
1879 the string buffer now, while the string buffer is still installed. */
1880 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1881 to me what the true lifespan of the tokens are. It would appear that
1882 the lifespan is the entire parse of the main input stream, in which case
1883 this may not be wrong. */
1884 if (pfile
->directive_result
.type
== CPP_PRAGMA
)
1890 toks
= XNEWVEC (cpp_token
, maxcount
);
1891 toks
[0] = pfile
->directive_result
;
1895 if (count
== maxcount
)
1897 maxcount
= maxcount
* 3 / 2;
1898 toks
= XRESIZEVEC (cpp_token
, toks
, maxcount
);
1900 toks
[count
] = *cpp_get_token (pfile
);
1901 /* _Pragma is a builtin, so we're not within a macro-map, and so
1902 the token locations are set to bogus ordinary locations
1903 near to, but after that of the "_Pragma".
1904 Paper over this by setting them equal to the location of the
1905 _Pragma itself (PR preprocessor/69126). */
1906 toks
[count
].src_loc
= expansion_loc
;
1907 /* Macros have been already expanded by cpp_get_token
1908 if the pragma allowed expansion. */
1909 toks
[count
++].flags
|= NO_EXPAND
;
1911 while (toks
[count
-1].type
!= CPP_PRAGMA_EOL
);
1916 toks
= XNEW (cpp_token
);
1917 toks
[0] = pfile
->directive_result
;
1919 /* If we handled the entire pragma internally, make sure we get the
1920 line number correct for the next token. */
1921 if (pfile
->cb
.line_change
)
1922 pfile
->cb
.line_change (pfile
, pfile
->cur_token
, false);
1925 /* Finish inlining run_directive. */
1926 pfile
->buffer
->file
= NULL
;
1927 _cpp_pop_buffer (pfile
);
1929 /* Reset the old macro state before ... */
1930 XDELETE (pfile
->context
);
1931 pfile
->context
= saved_context
;
1932 pfile
->cur_token
= saved_cur_token
;
1933 pfile
->cur_run
= saved_cur_run
;
1935 /* ... inserting the new tokens we collected. */
1936 _cpp_push_token_context (pfile
, NULL
, toks
, count
);
1939 /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
1941 _cpp_do__Pragma (cpp_reader
*pfile
, location_t expansion_loc
)
1943 const cpp_token
*string
= get__Pragma_string (pfile
);
1944 pfile
->directive_result
.type
= CPP_PADDING
;
1948 destringize_and_run (pfile
, &string
->val
.str
, expansion_loc
);
1951 cpp_error (pfile
, CPP_DL_ERROR
,
1952 "_Pragma takes a parenthesized string literal");
1956 /* Handle #ifdef. */
1958 do_ifdef (cpp_reader
*pfile
)
1962 if (! pfile
->state
.skipping
)
1964 cpp_hashnode
*node
= lex_macro_node (pfile
, false);
1968 skip
= !_cpp_defined_macro_p (node
);
1969 _cpp_mark_macro_used (node
);
1970 _cpp_maybe_notify_macro_use (pfile
, node
);
1972 pfile
->cb
.used (pfile
, pfile
->directive_line
, node
);
1973 check_eol (pfile
, false);
1977 push_conditional (pfile
, skip
, T_IFDEF
, 0);
1980 /* Handle #ifndef. */
1982 do_ifndef (cpp_reader
*pfile
)
1985 cpp_hashnode
*node
= 0;
1987 if (! pfile
->state
.skipping
)
1989 node
= lex_macro_node (pfile
, false);
1993 /* Do not treat conditional macros as being defined. This is due to
1994 the powerpc port using conditional macros for 'vector', 'bool',
1995 and 'pixel' to act as conditional keywords. This messes up tests
1996 like #ifndef bool. */
1997 skip
= _cpp_defined_macro_p (node
);
1998 _cpp_mark_macro_used (node
);
1999 _cpp_maybe_notify_macro_use (pfile
, node
);
2001 pfile
->cb
.used (pfile
, pfile
->directive_line
, node
);
2002 check_eol (pfile
, false);
2006 push_conditional (pfile
, skip
, T_IFNDEF
, node
);
2009 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
2010 pfile->mi_ind_cmacro so we can handle multiple-include
2011 optimizations. If macro expansion occurs in the expression, we
2012 cannot treat it as a controlling conditional, since the expansion
2013 could change in the future. That is handled by cpp_get_token. */
2015 do_if (cpp_reader
*pfile
)
2019 if (! pfile
->state
.skipping
)
2020 skip
= _cpp_parse_expr (pfile
, true) == false;
2022 push_conditional (pfile
, skip
, T_IF
, pfile
->mi_ind_cmacro
);
2025 /* Flip skipping state if appropriate and continue without changing
2026 if_stack; this is so that the error message for missing #endif's
2027 etc. will point to the original #if. */
2029 do_else (cpp_reader
*pfile
)
2031 cpp_buffer
*buffer
= pfile
->buffer
;
2032 struct if_stack
*ifs
= buffer
->if_stack
;
2035 cpp_error (pfile
, CPP_DL_ERROR
, "#else without #if");
2038 if (ifs
->type
== T_ELSE
)
2040 cpp_error (pfile
, CPP_DL_ERROR
, "#else after #else");
2041 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2042 "the conditional began here");
2046 /* Skip any future (erroneous) #elses or #elifs. */
2047 pfile
->state
.skipping
= ifs
->skip_elses
;
2048 ifs
->skip_elses
= true;
2050 /* Invalidate any controlling macro. */
2053 /* Only check EOL if was not originally skipping. */
2054 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
2055 check_eol_endif_labels (pfile
);
2059 /* Handle a #elif directive by not changing if_stack either. See the
2060 comment above do_else. */
2062 do_elif (cpp_reader
*pfile
)
2064 cpp_buffer
*buffer
= pfile
->buffer
;
2065 struct if_stack
*ifs
= buffer
->if_stack
;
2068 cpp_error (pfile
, CPP_DL_ERROR
, "#elif without #if");
2071 if (ifs
->type
== T_ELSE
)
2073 cpp_error (pfile
, CPP_DL_ERROR
, "#elif after #else");
2074 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2075 "the conditional began here");
2079 /* See DR#412: "Only the first group whose control condition
2080 evaluates to true (nonzero) is processed; any following groups
2081 are skipped and their controlling directives are processed as
2082 if they were in a group that is skipped." */
2083 if (ifs
->skip_elses
)
2084 pfile
->state
.skipping
= 1;
2087 pfile
->state
.skipping
= ! _cpp_parse_expr (pfile
, false);
2088 ifs
->skip_elses
= ! pfile
->state
.skipping
;
2091 /* Invalidate any controlling macro. */
2096 /* #endif pops the if stack and resets pfile->state.skipping. */
2098 do_endif (cpp_reader
*pfile
)
2100 cpp_buffer
*buffer
= pfile
->buffer
;
2101 struct if_stack
*ifs
= buffer
->if_stack
;
2104 cpp_error (pfile
, CPP_DL_ERROR
, "#endif without #if");
2107 /* Only check EOL if was not originally skipping. */
2108 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
2109 check_eol_endif_labels (pfile
);
2111 /* If potential control macro, we go back outside again. */
2112 if (ifs
->next
== 0 && ifs
->mi_cmacro
)
2114 pfile
->mi_valid
= true;
2115 pfile
->mi_cmacro
= ifs
->mi_cmacro
;
2118 buffer
->if_stack
= ifs
->next
;
2119 pfile
->state
.skipping
= ifs
->was_skipping
;
2120 obstack_free (&pfile
->buffer_ob
, ifs
);
2124 /* Push an if_stack entry for a preprocessor conditional, and set
2125 pfile->state.skipping to SKIP. If TYPE indicates the conditional
2126 is #if or #ifndef, CMACRO is a potentially controlling macro, and
2127 we need to check here that we are at the top of the file. */
2129 push_conditional (cpp_reader
*pfile
, int skip
, int type
,
2130 const cpp_hashnode
*cmacro
)
2132 struct if_stack
*ifs
;
2133 cpp_buffer
*buffer
= pfile
->buffer
;
2135 ifs
= XOBNEW (&pfile
->buffer_ob
, struct if_stack
);
2136 ifs
->line
= pfile
->directive_line
;
2137 ifs
->next
= buffer
->if_stack
;
2138 ifs
->skip_elses
= pfile
->state
.skipping
|| !skip
;
2139 ifs
->was_skipping
= pfile
->state
.skipping
;
2141 /* This condition is effectively a test for top-of-file. */
2142 if (pfile
->mi_valid
&& pfile
->mi_cmacro
== 0)
2143 ifs
->mi_cmacro
= cmacro
;
2147 pfile
->state
.skipping
= skip
;
2148 buffer
->if_stack
= ifs
;
2151 /* Read the tokens of the answer into the macro pool, in a directive
2152 of type TYPE. Only commit the memory if we intend it as permanent
2153 storage, i.e. the #assert case. Returns 0 on success, and sets
2154 ANSWERP to point to the answer. PRED_LOC is the location of the
2157 parse_answer (cpp_reader
*pfile
, int type
, location_t pred_loc
,
2158 cpp_macro
**answer_ptr
)
2160 /* In a conditional, it is legal to not have an open paren. We
2161 should save the following token in this case. */
2162 const cpp_token
*paren
= cpp_get_token (pfile
);
2164 /* If not a paren, see if we're OK. */
2165 if (paren
->type
!= CPP_OPEN_PAREN
)
2167 /* In a conditional no answer is a test for any answer. It
2168 could be followed by any token. */
2171 _cpp_backup_tokens (pfile
, 1);
2175 /* #unassert with no answer is valid - it removes all answers. */
2176 if (type
== T_UNASSERT
&& paren
->type
== CPP_EOF
)
2179 cpp_error_with_line (pfile
, CPP_DL_ERROR
, pred_loc
, 0,
2180 "missing '(' after predicate");
2184 cpp_macro
*answer
= _cpp_new_macro (pfile
, cmk_assert
,
2185 _cpp_reserve_room (pfile
, 0,
2186 sizeof (cpp_macro
)));
2187 answer
->parm
.next
= NULL
;
2191 const cpp_token
*token
= cpp_get_token (pfile
);
2193 if (token
->type
== CPP_CLOSE_PAREN
)
2196 if (token
->type
== CPP_EOF
)
2198 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' to complete answer");
2202 answer
= (cpp_macro
*)_cpp_reserve_room
2203 (pfile
, sizeof (cpp_macro
) + count
* sizeof (cpp_token
),
2204 sizeof (cpp_token
));
2205 answer
->exp
.tokens
[count
++] = *token
;
2210 cpp_error (pfile
, CPP_DL_ERROR
, "predicate's answer is empty");
2214 /* Drop whitespace at start, for answer equivalence purposes. */
2215 answer
->exp
.tokens
[0].flags
&= ~PREV_WHITE
;
2217 answer
->count
= count
;
2218 *answer_ptr
= answer
;
2223 /* Parses an assertion directive of type TYPE, returning a pointer to
2224 the hash node of the predicate, or 0 on error. The node is
2225 guaranteed to be disjoint from the macro namespace, so can only
2226 have type 'NT_VOID'. If an answer was supplied, it is placed in
2227 *ANSWER_PTR, which is otherwise set to 0. */
2228 static cpp_hashnode
*
2229 parse_assertion (cpp_reader
*pfile
, int type
, cpp_macro
**answer_ptr
)
2231 cpp_hashnode
*result
= 0;
2233 /* We don't expand predicates or answers. */
2234 pfile
->state
.prevent_expansion
++;
2238 const cpp_token
*predicate
= cpp_get_token (pfile
);
2239 if (predicate
->type
== CPP_EOF
)
2240 cpp_error (pfile
, CPP_DL_ERROR
, "assertion without predicate");
2241 else if (predicate
->type
!= CPP_NAME
)
2242 cpp_error_with_line (pfile
, CPP_DL_ERROR
, predicate
->src_loc
, 0,
2243 "predicate must be an identifier");
2244 else if (parse_answer (pfile
, type
, predicate
->src_loc
, answer_ptr
))
2246 unsigned int len
= NODE_LEN (predicate
->val
.node
.node
);
2247 unsigned char *sym
= (unsigned char *) alloca (len
+ 1);
2249 /* Prefix '#' to get it out of macro namespace. */
2251 memcpy (sym
+ 1, NODE_NAME (predicate
->val
.node
.node
), len
);
2252 result
= cpp_lookup (pfile
, sym
, len
+ 1);
2255 pfile
->state
.prevent_expansion
--;
2260 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
2261 or a pointer to NULL if the answer is not in the chain. */
2263 find_answer (cpp_hashnode
*node
, const cpp_macro
*candidate
)
2266 cpp_macro
**result
= NULL
;
2268 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->parm
.next
)
2270 cpp_macro
*answer
= *result
;
2272 if (answer
->count
== candidate
->count
)
2274 for (i
= 0; i
< answer
->count
; i
++)
2275 if (!_cpp_equiv_tokens (&answer
->exp
.tokens
[i
],
2276 &candidate
->exp
.tokens
[i
]))
2279 if (i
== answer
->count
)
2287 /* Test an assertion within a preprocessor conditional. Returns
2288 nonzero on failure, zero on success. On success, the result of
2289 the test is written into VALUE, otherwise the value 0. */
2291 _cpp_test_assertion (cpp_reader
*pfile
, unsigned int *value
)
2294 cpp_hashnode
*node
= parse_assertion (pfile
, T_IF
, &answer
);
2296 /* For recovery, an erroneous assertion expression is handled as a
2297 failing assertion. */
2302 if (node
->value
.answers
)
2303 *value
= !answer
|| *find_answer (node
, answer
);
2305 else if (pfile
->cur_token
[-1].type
== CPP_EOF
)
2306 _cpp_backup_tokens (pfile
, 1);
2308 /* We don't commit the memory for the answer - it's temporary only. */
2312 /* Handle #assert. */
2314 do_assert (cpp_reader
*pfile
)
2317 cpp_hashnode
*node
= parse_assertion (pfile
, T_ASSERT
, &answer
);
2321 /* Place the new answer in the answer list. First check there
2322 is not a duplicate. */
2323 if (*find_answer (node
, answer
))
2325 cpp_error (pfile
, CPP_DL_WARNING
, "\"%s\" re-asserted",
2326 NODE_NAME (node
) + 1);
2330 /* Commit or allocate storage for the answer. */
2331 answer
= (cpp_macro
*)_cpp_commit_buff
2332 (pfile
, sizeof (cpp_macro
) - sizeof (cpp_token
)
2333 + sizeof (cpp_token
) * answer
->count
);
2335 /* Chain into the list. */
2336 answer
->parm
.next
= node
->value
.answers
;
2337 node
->value
.answers
= answer
;
2339 check_eol (pfile
, false);
2343 /* Handle #unassert. */
2345 do_unassert (cpp_reader
*pfile
)
2348 cpp_hashnode
*node
= parse_assertion (pfile
, T_UNASSERT
, &answer
);
2350 /* It isn't an error to #unassert something that isn't asserted. */
2355 cpp_macro
**p
= find_answer (node
, answer
);
2357 /* Remove the assert from the list. */
2358 if (cpp_macro
*temp
= *p
)
2359 *p
= temp
->parm
.next
;
2361 check_eol (pfile
, false);
2364 _cpp_free_definition (node
);
2367 /* We don't commit the memory for the answer - it's temporary only. */
2370 /* These are for -D, -U, -A. */
2372 /* Process the string STR as if it appeared as the body of a #define.
2373 If STR is just an identifier, define it with value 1.
2374 If STR has anything after the identifier, then it should
2375 be identifier=definition. */
2377 cpp_define (cpp_reader
*pfile
, const char *str
)
2383 /* Copy the entire option so we can modify it.
2384 Change the first "=" in the string to a space. If there is none,
2385 tack " 1" on the end. */
2387 count
= strlen (str
);
2388 buf
= (char *) alloca (count
+ 3);
2389 memcpy (buf
, str
, count
);
2391 p
= strchr (str
, '=');
2401 run_directive (pfile
, T_DEFINE
, buf
, count
);
2405 /* Use to build macros to be run through cpp_define() as
2407 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
2410 cpp_define_formatted (cpp_reader
*pfile
, const char *fmt
, ...)
2416 ptr
= xvasprintf (fmt
, ap
);
2419 cpp_define (pfile
, ptr
);
2424 /* Slight variant of the above for use by initialize_builtins. */
2426 _cpp_define_builtin (cpp_reader
*pfile
, const char *str
)
2428 size_t len
= strlen (str
);
2429 char *buf
= (char *) alloca (len
+ 1);
2430 memcpy (buf
, str
, len
);
2432 run_directive (pfile
, T_DEFINE
, buf
, len
);
2435 /* Process MACRO as if it appeared as the body of an #undef. */
2437 cpp_undef (cpp_reader
*pfile
, const char *macro
)
2439 size_t len
= strlen (macro
);
2440 char *buf
= (char *) alloca (len
+ 1);
2441 memcpy (buf
, macro
, len
);
2443 run_directive (pfile
, T_UNDEF
, buf
, len
);
2446 /* Replace a previous definition DEF of the macro STR. If DEF is NULL,
2447 or first element is zero, then the macro should be undefined. */
2449 cpp_pop_definition (cpp_reader
*pfile
, struct def_pragma_macro
*c
)
2451 cpp_hashnode
*node
= _cpp_lex_identifier (pfile
, c
->name
);
2455 if (pfile
->cb
.before_define
)
2456 pfile
->cb
.before_define (pfile
);
2458 if (cpp_macro_p (node
))
2460 if (pfile
->cb
.undef
)
2461 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
2462 if (CPP_OPTION (pfile
, warn_unused_macros
))
2463 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
2464 _cpp_free_definition (node
);
2471 _cpp_restore_special_builtin (pfile
, c
);
2478 cpp_hashnode
*h
= NULL
;
2481 namelen
= ustrcspn (c
->definition
, "( \n");
2482 h
= cpp_lookup (pfile
, c
->definition
, namelen
);
2483 dn
= c
->definition
+ namelen
;
2485 nbuf
= cpp_push_buffer (pfile
, dn
, ustrchr (dn
, '\n') - dn
, true);
2488 _cpp_clean_line (pfile
);
2490 if (!_cpp_create_definition (pfile
, h
))
2492 _cpp_pop_buffer (pfile
);
2496 h
->value
.macro
->line
= c
->line
;
2497 h
->value
.macro
->syshdr
= c
->syshdr
;
2498 h
->value
.macro
->used
= c
->used
;
2502 /* Process the string STR as if it appeared as the body of a #assert. */
2504 cpp_assert (cpp_reader
*pfile
, const char *str
)
2506 handle_assertion (pfile
, str
, T_ASSERT
);
2509 /* Process STR as if it appeared as the body of an #unassert. */
2511 cpp_unassert (cpp_reader
*pfile
, const char *str
)
2513 handle_assertion (pfile
, str
, T_UNASSERT
);
2516 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2518 handle_assertion (cpp_reader
*pfile
, const char *str
, int type
)
2520 size_t count
= strlen (str
);
2521 const char *p
= strchr (str
, '=');
2523 /* Copy the entire option so we can modify it. Change the first
2524 "=" in the string to a '(', and tack a ')' on the end. */
2525 char *buf
= (char *) alloca (count
+ 2);
2527 memcpy (buf
, str
, count
);
2536 run_directive (pfile
, type
, str
, count
);
2539 /* The options structure. */
2541 cpp_get_options (cpp_reader
*pfile
)
2543 return &pfile
->opts
;
2546 /* The callbacks structure. */
2548 cpp_get_callbacks (cpp_reader
*pfile
)
2553 /* Copy the given callbacks structure to our own. */
2555 cpp_set_callbacks (cpp_reader
*pfile
, cpp_callbacks
*cb
)
2560 /* The dependencies structure. (Creates one if it hasn't already been.) */
2562 cpp_get_deps (cpp_reader
*pfile
)
2565 pfile
->deps
= deps_init ();
2569 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2570 doesn't fail. It does not generate a file change call back; that
2571 is the responsibility of the caller. */
2573 cpp_push_buffer (cpp_reader
*pfile
, const uchar
*buffer
, size_t len
,
2576 cpp_buffer
*new_buffer
= XOBNEW (&pfile
->buffer_ob
, cpp_buffer
);
2578 /* Clears, amongst other things, if_stack and mi_cmacro. */
2579 memset (new_buffer
, 0, sizeof (cpp_buffer
));
2581 new_buffer
->next_line
= new_buffer
->buf
= buffer
;
2582 new_buffer
->rlimit
= buffer
+ len
;
2583 new_buffer
->from_stage3
= from_stage3
;
2584 new_buffer
->prev
= pfile
->buffer
;
2585 new_buffer
->need_line
= true;
2587 pfile
->buffer
= new_buffer
;
2592 /* Pops a single buffer, with a file change call-back if appropriate.
2593 Then pushes the next -include file, if any remain. */
2595 _cpp_pop_buffer (cpp_reader
*pfile
)
2597 cpp_buffer
*buffer
= pfile
->buffer
;
2598 struct _cpp_file
*inc
= buffer
->file
;
2599 struct if_stack
*ifs
;
2600 const unsigned char *to_free
;
2602 /* Walk back up the conditional stack till we reach its level at
2603 entry to this file, issuing error messages. */
2604 for (ifs
= buffer
->if_stack
; ifs
; ifs
= ifs
->next
)
2605 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2606 "unterminated #%s", dtable
[ifs
->type
].name
);
2608 /* In case of a missing #endif. */
2609 pfile
->state
.skipping
= 0;
2611 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2612 pfile
->buffer
= buffer
->prev
;
2614 to_free
= buffer
->to_free
;
2615 free (buffer
->notes
);
2617 /* Free the buffer object now; we may want to push a new buffer
2618 in _cpp_push_next_include_file. */
2619 obstack_free (&pfile
->buffer_ob
, buffer
);
2623 _cpp_pop_file_buffer (pfile
, inc
, to_free
);
2625 _cpp_do_file_change (pfile
, LC_LEAVE
, 0, 0, 0);
2628 free ((void *)to_free
);
2631 /* Enter all recognized directives in the hash table. */
2633 _cpp_init_directives (cpp_reader
*pfile
)
2635 for (int i
= 0; i
< N_DIRECTIVES
; i
++)
2637 cpp_hashnode
*node
= cpp_lookup (pfile
, dtable
[i
].name
, dtable
[i
].length
);
2638 node
->is_directive
= 1;
2639 node
->directive_index
= i
;
2643 /* Extract header file from a bracket include. Parsing starts after '<'.
2644 The string is malloced and must be freed by the caller. */
2646 _cpp_bracket_include(cpp_reader
*pfile
)
2648 return glue_header_name (pfile
);