1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986-2022 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. STDC2X directives come from the C2X standard. EXTENSION
60 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 ELIFDEF is set on directives that are only handled for standards with the
75 #elifdef / #elifndef feature. */
77 #define IF_COND (1 << 1)
80 #define EXPAND (1 << 4)
81 #define DEPRECATED (1 << 5)
82 #define ELIFDEF (1 << 6)
84 /* Defines one #-directive, including how to handle it. */
85 typedef void (*directive_handler
) (cpp_reader
*);
86 typedef struct directive directive
;
89 directive_handler handler
; /* Function to handle directive. */
90 const uchar
*name
; /* Name of directive. */
91 unsigned short length
; /* Length of name. */
92 unsigned char origin
; /* Origin of directive. */
93 unsigned char flags
; /* Flags describing this directive. */
96 /* Forward declarations. */
98 static void skip_rest_of_line (cpp_reader
*);
99 static void check_eol (cpp_reader
*, bool);
100 static void start_directive (cpp_reader
*);
101 static void prepare_directive_trad (cpp_reader
*);
102 static void end_directive (cpp_reader
*, int);
103 static void directive_diagnostics (cpp_reader
*, const directive
*, int);
104 static void run_directive (cpp_reader
*, int, const char *, size_t);
105 static char *glue_header_name (cpp_reader
*);
106 static const char *parse_include (cpp_reader
*, int *, const cpp_token
***,
108 static void push_conditional (cpp_reader
*, int, int, const cpp_hashnode
*);
109 static unsigned int read_flag (cpp_reader
*, unsigned int);
110 static bool strtolinenum (const uchar
*, size_t, linenum_type
*, bool *);
111 static void do_diagnostic (cpp_reader
*, enum cpp_diagnostic_level code
,
112 enum cpp_warning_reason reason
, int);
113 static cpp_hashnode
*lex_macro_node (cpp_reader
*, bool);
114 static int undefine_macros (cpp_reader
*, cpp_hashnode
*, void *);
115 static void do_include_common (cpp_reader
*, enum include_type
);
116 static struct pragma_entry
*lookup_pragma_entry (struct pragma_entry
*,
117 const cpp_hashnode
*);
118 static int count_registered_pragmas (struct pragma_entry
*);
119 static char ** save_registered_pragmas (struct pragma_entry
*, char **);
120 static char ** restore_registered_pragmas (cpp_reader
*, struct pragma_entry
*,
122 static void do_pragma_once (cpp_reader
*);
123 static void do_pragma_poison (cpp_reader
*);
124 static void do_pragma_system_header (cpp_reader
*);
125 static void do_pragma_dependency (cpp_reader
*);
126 static void do_pragma_warning_or_error (cpp_reader
*, bool error
);
127 static void do_pragma_warning (cpp_reader
*);
128 static void do_pragma_error (cpp_reader
*);
129 static void do_linemarker (cpp_reader
*);
130 static const cpp_token
*get_token_no_padding (cpp_reader
*);
131 static const cpp_token
*get__Pragma_string (cpp_reader
*);
132 static void destringize_and_run (cpp_reader
*, const cpp_string
*,
134 static bool parse_answer (cpp_reader
*, int, location_t
, cpp_macro
**);
135 static cpp_hashnode
*parse_assertion (cpp_reader
*, int, cpp_macro
**);
136 static cpp_macro
**find_answer (cpp_hashnode
*, const cpp_macro
*);
137 static void handle_assertion (cpp_reader
*, const char *, int);
138 static void do_pragma_push_macro (cpp_reader
*);
139 static void do_pragma_pop_macro (cpp_reader
*);
140 static void cpp_pop_definition (cpp_reader
*, struct def_pragma_macro
*);
142 /* This is the table of directive handlers. All extensions other than
143 #warning, #include_next, and #import are deprecated. The name is
144 where the extension appears to have come from. */
146 #define DIRECTIVE_TABLE \
147 D(define, T_DEFINE = 0, KANDR, IN_I) \
148 D(include, T_INCLUDE, KANDR, INCL | EXPAND) \
149 D(endif, T_ENDIF, KANDR, COND) \
150 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) \
151 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) \
152 D(else, T_ELSE, KANDR, COND) \
153 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) \
154 D(undef, T_UNDEF, KANDR, IN_I) \
155 D(line, T_LINE, KANDR, EXPAND) \
156 D(elif, T_ELIF, STDC89, COND | EXPAND) \
157 D(elifdef, T_ELIFDEF, STDC2X, COND | ELIFDEF) \
158 D(elifndef, T_ELIFNDEF, STDC2X, COND | ELIFDEF) \
159 D(error, T_ERROR, STDC89, 0) \
160 D(pragma, T_PRAGMA, STDC89, IN_I) \
161 D(warning, T_WARNING, STDC2X, 0) \
162 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) \
163 D(ident, T_IDENT, EXTENSION, IN_I) \
164 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* ObjC */ \
165 D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* SVR4 */ \
166 D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* SVR4 */ \
167 D(sccs, T_SCCS, EXTENSION, IN_I) /* SVR4? */
169 /* #sccs is synonymous with #ident. */
170 #define do_sccs do_ident
172 /* Use the table to generate a series of prototypes, an enum for the
173 directive names, and an array of directive handlers. */
175 #define D(name, t, o, f) static void do_##name (cpp_reader *);
179 #define D(n, tag, o, f) tag,
187 #define D(name, t, origin, flags) \
188 { do_##name, (const uchar *) #name, \
189 sizeof #name - 1, origin, flags },
190 static const directive dtable
[] =
196 /* A NULL-terminated array of directive names for use
197 when suggesting corrections for misspelled directives. */
198 #define D(name, t, origin, flags) #name,
199 static const char * const directive_names
[] = {
205 #undef DIRECTIVE_TABLE
207 /* Wrapper struct directive for linemarkers.
208 The origin is more or less true - the original K+R cpp
209 did use this notation in its preprocessed output. */
210 static const directive linemarker_dir
=
212 do_linemarker
, UC
"#", 1, KANDR
, IN_I
215 /* Skip any remaining tokens in a directive. */
217 skip_rest_of_line (cpp_reader
*pfile
)
219 /* Discard all stacked contexts. */
220 while (pfile
->context
->prev
)
221 _cpp_pop_context (pfile
);
223 /* Sweep up all tokens remaining on the line. */
225 while (_cpp_lex_token (pfile
)->type
!= CPP_EOF
)
229 /* Helper function for check_oel. */
232 check_eol_1 (cpp_reader
*pfile
, bool expand
, enum cpp_warning_reason reason
)
234 if (! SEEN_EOL () && (expand
235 ? cpp_get_token (pfile
)
236 : _cpp_lex_token (pfile
))->type
!= CPP_EOF
)
237 cpp_pedwarning (pfile
, reason
, "extra tokens at end of #%s directive",
238 pfile
->directive
->name
);
241 /* Variant of check_eol used for Wendif-labels warnings. */
244 check_eol_endif_labels (cpp_reader
*pfile
)
246 check_eol_1 (pfile
, false, CPP_W_ENDIF_LABELS
);
249 /* Ensure there are no stray tokens at the end of a directive. If
250 EXPAND is true, tokens macro-expanding to nothing are allowed. */
253 check_eol (cpp_reader
*pfile
, bool expand
)
255 check_eol_1 (pfile
, expand
, CPP_W_NONE
);
258 /* Ensure there are no stray tokens other than comments at the end of
259 a directive, and gather the comments. */
260 static const cpp_token
**
261 check_eol_return_comments (cpp_reader
*pfile
)
265 const cpp_token
**buf
;
267 buf
= XNEWVEC (const cpp_token
*, capacity
);
273 const cpp_token
*tok
;
275 tok
= _cpp_lex_token (pfile
);
276 if (tok
->type
== CPP_EOF
)
278 if (tok
->type
!= CPP_COMMENT
)
279 cpp_error (pfile
, CPP_DL_PEDWARN
,
280 "extra tokens at end of #%s directive",
281 pfile
->directive
->name
);
284 if (c
+ 1 >= capacity
)
287 buf
= XRESIZEVEC (const cpp_token
*, buf
, capacity
);
298 /* Called when entering a directive, _Pragma or command-line directive. */
300 start_directive (cpp_reader
*pfile
)
302 /* Setup in-directive state. */
303 pfile
->state
.in_directive
= 1;
304 pfile
->state
.save_comments
= 0;
305 pfile
->directive_result
.type
= CPP_PADDING
;
307 /* Some handlers need the position of the # for diagnostics. */
308 pfile
->directive_line
= pfile
->line_table
->highest_line
;
311 /* Called when leaving a directive, _Pragma or command-line directive. */
313 end_directive (cpp_reader
*pfile
, int skip_line
)
315 if (CPP_OPTION (pfile
, traditional
))
317 /* Revert change of prepare_directive_trad. */
318 if (!pfile
->state
.in_deferred_pragma
)
319 pfile
->state
.prevent_expansion
--;
321 if (pfile
->directive
!= &dtable
[T_DEFINE
])
322 _cpp_remove_overlay (pfile
);
324 else if (pfile
->state
.in_deferred_pragma
)
326 /* We don't skip for an assembler #. */
329 skip_rest_of_line (pfile
);
330 if (!pfile
->keep_tokens
)
332 pfile
->cur_run
= &pfile
->base_run
;
333 pfile
->cur_token
= pfile
->base_run
.base
;
338 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
339 pfile
->state
.in_directive
= 0;
340 pfile
->state
.in_expression
= 0;
341 pfile
->state
.angled_headers
= 0;
342 pfile
->directive
= 0;
345 /* Prepare to handle the directive in pfile->directive. */
347 prepare_directive_trad (cpp_reader
*pfile
)
349 if (pfile
->directive
!= &dtable
[T_DEFINE
])
351 bool no_expand
= (pfile
->directive
352 && ! (pfile
->directive
->flags
& EXPAND
));
353 bool was_skipping
= pfile
->state
.skipping
;
355 pfile
->state
.in_expression
= (pfile
->directive
== &dtable
[T_IF
]
356 || pfile
->directive
== &dtable
[T_ELIF
]);
357 if (pfile
->state
.in_expression
)
358 pfile
->state
.skipping
= false;
361 pfile
->state
.prevent_expansion
++;
362 _cpp_scan_out_logical_line (pfile
, NULL
, false);
364 pfile
->state
.prevent_expansion
--;
366 pfile
->state
.skipping
= was_skipping
;
367 _cpp_overlay_buffer (pfile
, pfile
->out
.base
,
368 pfile
->out
.cur
- pfile
->out
.base
);
371 /* Stop ISO C from expanding anything. */
372 pfile
->state
.prevent_expansion
++;
375 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
376 the '#' was indented. */
378 directive_diagnostics (cpp_reader
*pfile
, const directive
*dir
, int indented
)
380 /* Issue -pedantic or deprecated warnings for extensions. We let
381 -pedantic take precedence if both are applicable. */
382 if (! pfile
->state
.skipping
)
384 if (dir
->origin
== EXTENSION
385 && !(dir
== &dtable
[T_IMPORT
] && CPP_OPTION (pfile
, objc
))
386 && CPP_PEDANTIC (pfile
))
387 cpp_error (pfile
, CPP_DL_PEDWARN
, "#%s is a GCC extension", dir
->name
);
388 else if (dir
== &dtable
[T_WARNING
])
390 if (CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, warning_directive
))
392 if (CPP_OPTION (pfile
, cplusplus
))
393 cpp_error (pfile
, CPP_DL_PEDWARN
,
394 "#%s before C++23 is a GCC extension", dir
->name
);
396 cpp_error (pfile
, CPP_DL_PEDWARN
,
397 "#%s before C2X is a GCC extension", dir
->name
);
399 else if (CPP_OPTION (pfile
, cpp_warn_c11_c2x_compat
) > 0)
400 cpp_warning (pfile
, CPP_W_C11_C2X_COMPAT
,
401 "#%s before C2X is a GCC extension", dir
->name
);
403 else if (((dir
->flags
& DEPRECATED
) != 0
404 || (dir
== &dtable
[T_IMPORT
] && !CPP_OPTION (pfile
, objc
)))
405 && CPP_OPTION (pfile
, cpp_warn_deprecated
))
406 cpp_warning (pfile
, CPP_W_DEPRECATED
,
407 "#%s is a deprecated GCC extension", dir
->name
);
410 /* Traditionally, a directive is ignored unless its # is in
411 column 1. Therefore in code intended to work with K+R
412 compilers, directives added by C89 must have their #
413 indented, and directives present in traditional C must not.
414 This is true even of directives in skipped conditional
415 blocks. #elif cannot be used at all. */
416 if (CPP_WTRADITIONAL (pfile
))
418 if (dir
== &dtable
[T_ELIF
])
419 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
420 "suggest not using #elif in traditional C");
421 else if (indented
&& dir
->origin
== KANDR
)
422 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
423 "traditional C ignores #%s with the # indented",
425 else if (!indented
&& dir
->origin
!= KANDR
)
426 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
427 "suggest hiding #%s from traditional C with an indented #",
432 /* Check if we have a known directive. INDENTED is true if the
433 '#' of the directive was indented. This function is in this file
434 to save unnecessarily exporting dtable etc. to lex.cc. Returns
435 nonzero if the line of tokens has been handled, zero if we should
436 continue processing the line. */
438 _cpp_handle_directive (cpp_reader
*pfile
, bool indented
)
440 const directive
*dir
= 0;
441 const cpp_token
*dname
;
442 bool was_parsing_args
= pfile
->state
.parsing_args
;
443 bool was_discarding_output
= pfile
->state
.discarding_output
;
446 if (was_discarding_output
)
447 pfile
->state
.prevent_expansion
= 0;
449 if (was_parsing_args
)
451 if (CPP_OPTION (pfile
, cpp_pedantic
))
452 cpp_error (pfile
, CPP_DL_PEDWARN
,
453 "embedding a directive within macro arguments is not portable");
454 pfile
->state
.parsing_args
= 0;
455 pfile
->state
.prevent_expansion
= 0;
457 start_directive (pfile
);
458 dname
= _cpp_lex_token (pfile
);
460 if (dname
->type
== CPP_NAME
)
462 if (dname
->val
.node
.node
->is_directive
)
464 dir
= &dtable
[dname
->val
.node
.node
->directive_index
];
465 if ((dir
->flags
& ELIFDEF
)
466 && !CPP_OPTION (pfile
, elifdef
)
467 /* For -std=gnu* modes elifdef is supported with
468 a pedwarn if pedantic. */
469 && CPP_OPTION (pfile
, std
))
473 /* We do not recognize the # followed by a number extension in
475 else if (dname
->type
== CPP_NUMBER
&& CPP_OPTION (pfile
, lang
) != CLK_ASM
)
477 dir
= &linemarker_dir
;
478 if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, preprocessed
)
479 && ! pfile
->state
.skipping
)
480 cpp_error (pfile
, CPP_DL_PEDWARN
,
481 "style of line directive is a GCC extension");
486 /* If we have a directive that is not an opening conditional,
487 invalidate any control macro. */
488 if (! (dir
->flags
& IF_COND
))
489 pfile
->mi_valid
= false;
491 /* Kluge alert. In order to be sure that code like this
496 does not cause '#define foo bar' to get executed when
497 compiled with -save-temps, we recognize directives in
498 -fpreprocessed mode only if the # is in column 1. macro.cc
499 puts a space in front of any '#' at the start of a macro.
501 We exclude the -fdirectives-only case because macro expansion
502 has not been performed yet, and block comments can cause spaces
503 to precede the directive. */
504 if (CPP_OPTION (pfile
, preprocessed
)
505 && !CPP_OPTION (pfile
, directives_only
)
506 && (indented
|| !(dir
->flags
& IN_I
)))
513 /* In failed conditional groups, all non-conditional
514 directives are ignored. Before doing that, whether
515 skipping or not, we should lex angle-bracketed headers
516 correctly, and maybe output some diagnostics. */
517 pfile
->state
.angled_headers
= dir
->flags
& INCL
;
518 pfile
->state
.directive_wants_padding
= dir
->flags
& INCL
;
519 if (! CPP_OPTION (pfile
, preprocessed
))
520 directive_diagnostics (pfile
, dir
, indented
);
521 if (pfile
->state
.skipping
&& !(dir
->flags
& COND
))
525 else if (dname
->type
== CPP_EOF
)
526 ; /* CPP_EOF is the "null directive". */
529 /* An unknown directive. Don't complain about it in assembly
530 source: we don't know where the comments are, and # may
531 introduce assembler pseudo-ops. Don't complain about invalid
532 directives in skipped conditional groups (6.10 p4). */
533 if (CPP_OPTION (pfile
, lang
) == CLK_ASM
)
535 else if (!pfile
->state
.skipping
)
537 const char *unrecognized
538 = (const char *)cpp_token_as_text (pfile
, dname
);
539 const char *hint
= NULL
;
541 /* Call back into gcc to get a spelling suggestion. Ideally
542 we'd just use best_match from gcc/spellcheck.h (and filter
543 out the uncommon directives), but that requires moving it
544 to a support library. */
545 if (pfile
->cb
.get_suggestion
)
546 hint
= pfile
->cb
.get_suggestion (pfile
, unrecognized
,
551 rich_location
richloc (pfile
->line_table
, dname
->src_loc
);
552 source_range misspelled_token_range
553 = get_range_from_loc (pfile
->line_table
, dname
->src_loc
);
554 richloc
.add_fixit_replace (misspelled_token_range
, hint
);
555 cpp_error_at (pfile
, CPP_DL_ERROR
, &richloc
,
556 "invalid preprocessing directive #%s;"
557 " did you mean #%s?",
561 cpp_error (pfile
, CPP_DL_ERROR
,
562 "invalid preprocessing directive #%s",
567 pfile
->directive
= dir
;
568 if (CPP_OPTION (pfile
, traditional
))
569 prepare_directive_trad (pfile
);
572 pfile
->directive
->handler (pfile
);
574 _cpp_backup_tokens (pfile
, 1);
576 end_directive (pfile
, skip
);
577 if (was_parsing_args
&& !pfile
->state
.in_deferred_pragma
)
579 /* Restore state when within macro args. */
580 pfile
->state
.parsing_args
= 2;
581 pfile
->state
.prevent_expansion
= 1;
583 if (was_discarding_output
)
584 pfile
->state
.prevent_expansion
= 1;
588 /* Directive handler wrapper used by the command line option
589 processor. BUF is \n terminated. */
591 run_directive (cpp_reader
*pfile
, int dir_no
, const char *buf
, size_t count
)
593 cpp_push_buffer (pfile
, (const uchar
*) buf
, count
,
594 /* from_stage3 */ true);
595 start_directive (pfile
);
597 /* This is a short-term fix to prevent a leading '#' being
598 interpreted as a directive. */
599 _cpp_clean_line (pfile
);
601 pfile
->directive
= &dtable
[dir_no
];
602 if (CPP_OPTION (pfile
, traditional
))
603 prepare_directive_trad (pfile
);
604 pfile
->directive
->handler (pfile
);
605 end_directive (pfile
, 1);
606 _cpp_pop_buffer (pfile
);
609 /* Checks for validity the macro name in #define, #undef, #ifdef and
610 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
611 processing a #define or #undefine directive, and false
613 static cpp_hashnode
*
614 lex_macro_node (cpp_reader
*pfile
, bool is_def_or_undef
)
616 const cpp_token
*token
= _cpp_lex_token (pfile
);
618 /* The token immediately after #define must be an identifier. That
619 identifier may not be "defined", per C99 6.10.8p4.
620 In C++, it may not be any of the "named operators" either,
621 per C++98 [lex.digraph], [lex.key].
622 Finally, the identifier may not have been poisoned. (In that case
623 the lexer has issued the error message for us.) */
625 if (token
->type
== CPP_NAME
)
627 cpp_hashnode
*node
= token
->val
.node
.node
;
630 && node
== pfile
->spec_nodes
.n_defined
)
631 cpp_error (pfile
, CPP_DL_ERROR
,
632 "\"%s\" cannot be used as a macro name",
634 else if (! (node
->flags
& NODE_POISONED
))
637 else if (token
->flags
& NAMED_OP
)
638 cpp_error (pfile
, CPP_DL_ERROR
,
639 "\"%s\" cannot be used as a macro name as it is an operator in C++",
640 NODE_NAME (token
->val
.node
.node
));
641 else if (token
->type
== CPP_EOF
)
642 cpp_error (pfile
, CPP_DL_ERROR
, "no macro name given in #%s directive",
643 pfile
->directive
->name
);
645 cpp_error (pfile
, CPP_DL_ERROR
, "macro names must be identifiers");
650 /* Process a #define directive. Most work is done in macro.cc. */
652 do_define (cpp_reader
*pfile
)
654 cpp_hashnode
*node
= lex_macro_node (pfile
, true);
658 /* If we have been requested to expand comments into macros,
659 then re-enable saving of comments. */
660 pfile
->state
.save_comments
=
661 ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
);
663 if (pfile
->cb
.before_define
)
664 pfile
->cb
.before_define (pfile
);
666 if (_cpp_create_definition (pfile
, node
))
667 if (pfile
->cb
.define
)
668 pfile
->cb
.define (pfile
, pfile
->directive_line
, node
);
670 node
->flags
&= ~NODE_USED
;
674 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
676 do_undef (cpp_reader
*pfile
)
678 cpp_hashnode
*node
= lex_macro_node (pfile
, true);
682 if (pfile
->cb
.before_define
)
683 pfile
->cb
.before_define (pfile
);
686 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
688 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
689 identifier is not currently defined as a macro name. */
690 if (cpp_macro_p (node
))
692 if (node
->flags
& NODE_WARN
)
693 cpp_error (pfile
, CPP_DL_WARNING
,
694 "undefining \"%s\"", NODE_NAME (node
));
695 else if (cpp_builtin_macro_p (node
)
696 && CPP_OPTION (pfile
, warn_builtin_macro_redefined
))
697 cpp_warning_with_line (pfile
, CPP_W_BUILTIN_MACRO_REDEFINED
,
698 pfile
->directive_line
, 0,
699 "undefining \"%s\"", NODE_NAME (node
));
701 if (node
->value
.macro
702 && CPP_OPTION (pfile
, warn_unused_macros
))
703 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
705 _cpp_free_definition (node
);
709 check_eol (pfile
, false);
712 /* Undefine a single macro/assertion/whatever. */
715 undefine_macros (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*h
,
716 void *data_p ATTRIBUTE_UNUSED
)
718 /* Body of _cpp_free_definition inlined here for speed.
719 Macros and assertions no longer have anything to free. */
721 h
->value
.answers
= NULL
;
722 h
->flags
&= ~(NODE_POISONED
|NODE_DISABLED
|NODE_USED
);
726 /* Undefine all macros and assertions. */
729 cpp_undef_all (cpp_reader
*pfile
)
731 cpp_forall_identifiers (pfile
, undefine_macros
, NULL
);
735 /* Helper routine used by parse_include. Reinterpret the current line
736 as an h-char-sequence (< ... >); we are looking at the first token
737 after the <. Returns a malloced filename. */
739 glue_header_name (cpp_reader
*pfile
)
741 const cpp_token
*token
;
743 size_t len
, total_len
= 0, capacity
= 1024;
745 /* To avoid lexed tokens overwriting our glued name, we can only
746 allocate from the string pool once we've lexed everything. */
747 buffer
= XNEWVEC (char, capacity
);
750 token
= get_token_no_padding (pfile
);
752 if (token
->type
== CPP_GREATER
)
754 if (token
->type
== CPP_EOF
)
756 cpp_error (pfile
, CPP_DL_ERROR
, "missing terminating > character");
760 len
= cpp_token_len (token
) + 2; /* Leading space, terminating \0. */
761 if (total_len
+ len
> capacity
)
763 capacity
= (capacity
+ len
) * 2;
764 buffer
= XRESIZEVEC (char, buffer
, capacity
);
767 if (token
->flags
& PREV_WHITE
)
768 buffer
[total_len
++] = ' ';
770 total_len
= (cpp_spell_token (pfile
, token
, (uchar
*) &buffer
[total_len
],
775 buffer
[total_len
] = '\0';
779 /* Returns the file name of #include, #include_next, #import and
780 #pragma dependency. The string is malloced and the caller should
781 free it. Returns NULL on error. LOCATION is the source location
785 parse_include (cpp_reader
*pfile
, int *pangle_brackets
,
786 const cpp_token
***buf
, location_t
*location
)
789 const cpp_token
*header
;
791 /* Allow macro expansion. */
792 header
= get_token_no_padding (pfile
);
793 *location
= header
->src_loc
;
794 if ((header
->type
== CPP_STRING
&& header
->val
.str
.text
[0] != 'R')
795 || header
->type
== CPP_HEADER_NAME
)
797 fname
= XNEWVEC (char, header
->val
.str
.len
- 1);
798 memcpy (fname
, header
->val
.str
.text
+ 1, header
->val
.str
.len
- 2);
799 fname
[header
->val
.str
.len
- 2] = '\0';
800 *pangle_brackets
= header
->type
== CPP_HEADER_NAME
;
802 else if (header
->type
== CPP_LESS
)
804 fname
= glue_header_name (pfile
);
805 *pangle_brackets
= 1;
809 const unsigned char *dir
;
811 if (pfile
->directive
== &dtable
[T_PRAGMA
])
812 dir
= UC
"pragma dependency";
814 dir
= pfile
->directive
->name
;
815 cpp_error (pfile
, CPP_DL_ERROR
, "#%s expects \"FILENAME\" or <FILENAME>",
821 if (pfile
->directive
== &dtable
[T_PRAGMA
])
823 /* This pragma allows extra tokens after the file name. */
825 else if (buf
== NULL
|| CPP_OPTION (pfile
, discard_comments
))
826 check_eol (pfile
, true);
829 /* If we are not discarding comments, then gather them while
830 doing the eol check. */
831 *buf
= check_eol_return_comments (pfile
);
837 /* Handle #include, #include_next and #import. */
839 do_include_common (cpp_reader
*pfile
, enum include_type type
)
843 const cpp_token
**buf
= NULL
;
846 /* Re-enable saving of comments if requested, so that the include
847 callback can dump comments which follow #include. */
848 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
850 /* Tell the lexer this is an include directive -- we want it to
851 increment the line number even if this is the last line of a file. */
852 pfile
->state
.in_directive
= 2;
854 fname
= parse_include (pfile
, &angle_brackets
, &buf
, &location
);
860 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
861 "empty filename in #%s",
862 pfile
->directive
->name
);
866 /* Prevent #include recursion. */
867 if (pfile
->line_table
->depth
>= CPP_OPTION (pfile
, max_include_depth
))
870 "#include nested depth %u exceeds maximum of %u"
871 " (use -fmax-include-depth=DEPTH to increase the maximum)",
872 pfile
->line_table
->depth
,
873 CPP_OPTION (pfile
, max_include_depth
));
876 /* Get out of macro context, if we are. */
877 skip_rest_of_line (pfile
);
879 if (pfile
->cb
.include
)
880 pfile
->cb
.include (pfile
, pfile
->directive_line
,
881 pfile
->directive
->name
, fname
, angle_brackets
,
884 _cpp_stack_include (pfile
, fname
, angle_brackets
, type
, location
);
894 do_include (cpp_reader
*pfile
)
896 do_include_common (pfile
, IT_INCLUDE
);
900 do_import (cpp_reader
*pfile
)
902 do_include_common (pfile
, IT_IMPORT
);
906 do_include_next (cpp_reader
*pfile
)
908 enum include_type type
= IT_INCLUDE_NEXT
;
910 /* If this is the primary source file, warn and use the normal
912 if (_cpp_in_main_source_file (pfile
))
914 cpp_error (pfile
, CPP_DL_WARNING
,
915 "#include_next in primary source file");
918 do_include_common (pfile
, type
);
921 /* Subroutine of do_linemarker. Read possible flags after file name.
922 LAST is the last flag seen; 0 if this is the first flag. Return the
923 flag if it is valid, 0 at the end of the directive. Otherwise
926 read_flag (cpp_reader
*pfile
, unsigned int last
)
928 const cpp_token
*token
= _cpp_lex_token (pfile
);
930 if (token
->type
== CPP_NUMBER
&& token
->val
.str
.len
== 1)
932 unsigned int flag
= token
->val
.str
.text
[0] - '0';
934 if (flag
> last
&& flag
<= 4
935 && (flag
!= 4 || last
== 3)
936 && (flag
!= 2 || last
== 0))
940 if (token
->type
!= CPP_EOF
)
941 cpp_error (pfile
, CPP_DL_ERROR
, "invalid flag \"%s\" in line directive",
942 cpp_token_as_text (pfile
, token
));
946 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
947 of length LEN, to binary; store it in NUMP, and return false if the
948 number was well-formed, true if not. WRAPPED is set to true if the
949 number did not fit into 'linenum_type'. */
951 strtolinenum (const uchar
*str
, size_t len
, linenum_type
*nump
, bool *wrapped
)
953 linenum_type reg
= 0;
956 bool seen_digit_sep
= false;
961 if (!seen_digit_sep
&& c
== '\'' && len
)
963 seen_digit_sep
= true;
968 seen_digit_sep
= false;
969 if (reg
> ((linenum_type
) -1) / 10)
972 if (reg
> ((linenum_type
) -1) - (c
- '0'))
980 /* Interpret #line command.
981 Note that the filename string (if any) is a true string constant
982 (escapes are interpreted). */
984 do_line (cpp_reader
*pfile
)
986 class line_maps
*line_table
= pfile
->line_table
;
987 const line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (line_table
);
989 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
992 unsigned char map_sysp
= ORDINARY_MAP_IN_SYSTEM_HEADER_P (map
);
993 const cpp_token
*token
;
994 const char *new_file
= ORDINARY_MAP_FILE_NAME (map
);
995 linenum_type new_lineno
;
997 /* C99 raised the minimum limit on #line numbers. */
998 linenum_type cap
= CPP_OPTION (pfile
, c99
) ? 2147483647 : 32767;
1001 /* #line commands expand macros. */
1002 token
= cpp_get_token (pfile
);
1003 if (token
->type
!= CPP_NUMBER
1004 || strtolinenum (token
->val
.str
.text
, token
->val
.str
.len
,
1005 &new_lineno
, &wrapped
))
1007 if (token
->type
== CPP_EOF
)
1008 cpp_error (pfile
, CPP_DL_ERROR
, "unexpected end of file after #line");
1010 cpp_error (pfile
, CPP_DL_ERROR
,
1011 "\"%s\" after #line is not a positive integer",
1012 cpp_token_as_text (pfile
, token
));
1016 if (CPP_PEDANTIC (pfile
) && (new_lineno
== 0 || new_lineno
> cap
|| wrapped
))
1017 cpp_error (pfile
, CPP_DL_PEDWARN
, "line number out of range");
1019 cpp_error (pfile
, CPP_DL_WARNING
, "line number out of range");
1021 token
= cpp_get_token (pfile
);
1022 if (token
->type
== CPP_STRING
)
1024 cpp_string s
= { 0, 0 };
1025 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
, 1,
1027 new_file
= (const char *)s
.text
;
1028 check_eol (pfile
, true);
1030 else if (token
->type
!= CPP_EOF
)
1032 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
1033 cpp_token_as_text (pfile
, token
));
1037 skip_rest_of_line (pfile
);
1038 _cpp_do_file_change (pfile
, LC_RENAME_VERBATIM
, new_file
, new_lineno
,
1040 line_table
->seen_line_directive
= true;
1043 /* Interpret the # 44 "file" [flags] notation, which has slightly
1044 different syntax and semantics from #line: Flags are allowed,
1045 and we never complain about the line number being too big. */
1047 do_linemarker (cpp_reader
*pfile
)
1049 class line_maps
*line_table
= pfile
->line_table
;
1050 const line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (line_table
);
1051 const cpp_token
*token
;
1052 const char *new_file
= ORDINARY_MAP_FILE_NAME (map
);
1053 linenum_type new_lineno
;
1054 unsigned int new_sysp
= ORDINARY_MAP_IN_SYSTEM_HEADER_P (map
);
1055 enum lc_reason reason
= LC_RENAME_VERBATIM
;
1059 /* Back up so we can get the number again. Putting this in
1060 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
1061 some circumstances, which can segfault. */
1062 _cpp_backup_tokens (pfile
, 1);
1064 /* #line commands expand macros. */
1065 token
= cpp_get_token (pfile
);
1066 if (token
->type
!= CPP_NUMBER
1067 || strtolinenum (token
->val
.str
.text
, token
->val
.str
.len
,
1068 &new_lineno
, &wrapped
))
1070 /* Unlike #line, there does not seem to be a way to get an EOF
1071 here. So, it should be safe to always spell the token. */
1072 cpp_error (pfile
, CPP_DL_ERROR
,
1073 "\"%s\" after # is not a positive integer",
1074 cpp_token_as_text (pfile
, token
));
1078 token
= cpp_get_token (pfile
);
1079 if (token
->type
== CPP_STRING
)
1081 cpp_string s
= { 0, 0 };
1082 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
,
1084 new_file
= (const char *)s
.text
;
1087 flag
= read_flag (pfile
, 0);
1091 /* Fake an include for cpp_included (). */
1092 _cpp_fake_include (pfile
, new_file
);
1093 flag
= read_flag (pfile
, flag
);
1098 flag
= read_flag (pfile
, flag
);
1103 flag
= read_flag (pfile
, flag
);
1107 pfile
->buffer
->sysp
= new_sysp
;
1109 check_eol (pfile
, false);
1111 else if (token
->type
!= CPP_EOF
)
1113 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
1114 cpp_token_as_text (pfile
, token
));
1118 skip_rest_of_line (pfile
);
1120 if (reason
== LC_LEAVE
)
1122 /* Reread map since cpp_get_token can invalidate it with a
1124 map
= LINEMAPS_LAST_ORDINARY_MAP (line_table
);
1125 const line_map_ordinary
*from
1126 = linemap_included_from_linemap (line_table
, map
);
1130 else if (!new_file
[0])
1131 /* Leaving to "" means fill in the popped-to name. */
1132 new_file
= ORDINARY_MAP_FILE_NAME (from
);
1133 else if (filename_cmp (ORDINARY_MAP_FILE_NAME (from
), new_file
) != 0)
1134 /* It's the wrong name, Grommit! */
1139 cpp_warning (pfile
, CPP_W_NONE
,
1140 "file \"%s\" linemarker ignored due to "
1141 "incorrect nesting", new_file
);
1146 /* Compensate for the increment in linemap_add that occurs in
1147 _cpp_do_file_change. We're currently at the start of the line
1148 *following* the #line directive. A separate location_t for this
1149 location makes no sense (until we do the LC_LEAVE), and
1150 complicates LAST_SOURCE_LINE_LOCATION. */
1151 pfile
->line_table
->highest_location
--;
1153 _cpp_do_file_change (pfile
, reason
, new_file
, new_lineno
, new_sysp
);
1154 line_table
->seen_line_directive
= true;
1157 /* Arrange the file_change callback. Changing to TO_FILE:TO_LINE for
1158 REASON. SYSP is 1 for a system header, 2 for a system header that
1159 needs to be extern "C" protected, and zero otherwise. */
1161 _cpp_do_file_change (cpp_reader
*pfile
, enum lc_reason reason
,
1162 const char *to_file
, linenum_type to_line
,
1165 linemap_assert (reason
!= LC_ENTER_MACRO
);
1167 const line_map_ordinary
*ord_map
= NULL
;
1168 if (!to_line
&& reason
== LC_RENAME_VERBATIM
)
1170 /* A linemarker moving to line zero. If we're on the second
1171 line of the current map, and it also starts at zero, just
1172 rewind -- we're probably reading the builtins of a
1173 preprocessed source. */
1174 line_map_ordinary
*last
= LINEMAPS_LAST_ORDINARY_MAP (pfile
->line_table
);
1175 if (!ORDINARY_MAP_STARTING_LINE_NUMBER (last
)
1176 && 0 == filename_cmp (to_file
, ORDINARY_MAP_FILE_NAME (last
))
1177 && SOURCE_LINE (last
, pfile
->line_table
->highest_line
) == 2)
1180 pfile
->line_table
->highest_location
1181 = pfile
->line_table
->highest_line
= MAP_START_LOCATION (last
);
1186 if (const line_map
*map
= linemap_add (pfile
->line_table
, reason
, sysp
,
1189 ord_map
= linemap_check_ordinary (map
);
1190 linemap_line_start (pfile
->line_table
,
1191 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map
),
1195 if (pfile
->cb
.file_change
)
1196 pfile
->cb
.file_change (pfile
, ord_map
);
1199 /* Report a warning or error detected by the program we are
1200 processing. Use the directive's tokens in the error message. */
1202 do_diagnostic (cpp_reader
*pfile
, enum cpp_diagnostic_level code
,
1203 enum cpp_warning_reason reason
, int print_dir
)
1205 const unsigned char *dir_name
;
1206 unsigned char *line
;
1207 location_t src_loc
= pfile
->cur_token
[-1].src_loc
;
1210 dir_name
= pfile
->directive
->name
;
1213 pfile
->state
.prevent_expansion
++;
1214 line
= cpp_output_line_to_string (pfile
, dir_name
);
1215 pfile
->state
.prevent_expansion
--;
1217 if (code
== CPP_DL_WARNING_SYSHDR
&& reason
)
1218 cpp_warning_with_line_syshdr (pfile
, reason
, src_loc
, 0, "%s", line
);
1219 else if (code
== CPP_DL_WARNING
&& reason
)
1220 cpp_warning_with_line (pfile
, reason
, src_loc
, 0, "%s", line
);
1222 cpp_error_with_line (pfile
, code
, src_loc
, 0, "%s", line
);
1227 do_error (cpp_reader
*pfile
)
1229 do_diagnostic (pfile
, CPP_DL_ERROR
, CPP_W_NONE
, 1);
1233 do_warning (cpp_reader
*pfile
)
1235 /* We want #warning diagnostics to be emitted in system headers too. */
1236 do_diagnostic (pfile
, CPP_DL_WARNING_SYSHDR
, CPP_W_WARNING_DIRECTIVE
, 1);
1239 /* Report program identification. */
1241 do_ident (cpp_reader
*pfile
)
1243 const cpp_token
*str
= cpp_get_token (pfile
);
1245 if (str
->type
!= CPP_STRING
)
1246 cpp_error (pfile
, CPP_DL_ERROR
, "invalid #%s directive",
1247 pfile
->directive
->name
);
1248 else if (pfile
->cb
.ident
)
1249 pfile
->cb
.ident (pfile
, pfile
->directive_line
, &str
->val
.str
);
1251 check_eol (pfile
, false);
1254 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1255 matching entry, or NULL if none is found. The returned entry could
1256 be the start of a namespace chain, or a pragma. */
1257 static struct pragma_entry
*
1258 lookup_pragma_entry (struct pragma_entry
*chain
, const cpp_hashnode
*pragma
)
1260 while (chain
&& chain
->pragma
!= pragma
)
1261 chain
= chain
->next
;
1266 /* Create and insert a blank pragma entry at the beginning of a
1267 singly-linked CHAIN. */
1268 static struct pragma_entry
*
1269 new_pragma_entry (cpp_reader
*pfile
, struct pragma_entry
**chain
)
1271 struct pragma_entry
*new_entry
;
1273 new_entry
= (struct pragma_entry
*)
1274 _cpp_aligned_alloc (pfile
, sizeof (struct pragma_entry
));
1276 memset (new_entry
, 0, sizeof (struct pragma_entry
));
1277 new_entry
->next
= *chain
;
1283 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1284 goes in the global namespace. */
1285 static struct pragma_entry
*
1286 register_pragma_1 (cpp_reader
*pfile
, const char *space
, const char *name
,
1287 bool allow_name_expansion
)
1289 struct pragma_entry
**chain
= &pfile
->pragmas
;
1290 struct pragma_entry
*entry
;
1291 const cpp_hashnode
*node
;
1295 node
= cpp_lookup (pfile
, UC space
, strlen (space
));
1296 entry
= lookup_pragma_entry (*chain
, node
);
1299 entry
= new_pragma_entry (pfile
, chain
);
1300 entry
->pragma
= node
;
1301 entry
->is_nspace
= true;
1302 entry
->allow_expansion
= allow_name_expansion
;
1304 else if (!entry
->is_nspace
)
1306 else if (entry
->allow_expansion
!= allow_name_expansion
)
1308 cpp_error (pfile
, CPP_DL_ICE
,
1309 "registering pragmas in namespace \"%s\" with mismatched "
1310 "name expansion", space
);
1313 chain
= &entry
->u
.space
;
1315 else if (allow_name_expansion
)
1317 cpp_error (pfile
, CPP_DL_ICE
,
1318 "registering pragma \"%s\" with name expansion "
1319 "and no namespace", name
);
1323 /* Check for duplicates. */
1324 node
= cpp_lookup (pfile
, UC name
, strlen (name
));
1325 entry
= lookup_pragma_entry (*chain
, node
);
1328 entry
= new_pragma_entry (pfile
, chain
);
1329 entry
->pragma
= node
;
1333 if (entry
->is_nspace
)
1335 cpp_error (pfile
, CPP_DL_ICE
,
1336 "registering \"%s\" as both a pragma and a pragma namespace",
1339 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s %s is already registered",
1342 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s is already registered", name
);
1347 /* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1349 register_pragma_internal (cpp_reader
*pfile
, const char *space
,
1350 const char *name
, pragma_cb handler
)
1352 struct pragma_entry
*entry
;
1354 entry
= register_pragma_1 (pfile
, space
, name
, false);
1355 entry
->is_internal
= true;
1356 entry
->u
.handler
= handler
;
1359 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1360 goes in the global namespace. HANDLER is the handler it will call,
1361 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1362 expansion while parsing pragma NAME. This function is exported
1365 cpp_register_pragma (cpp_reader
*pfile
, const char *space
, const char *name
,
1366 pragma_cb handler
, bool allow_expansion
)
1368 struct pragma_entry
*entry
;
1372 cpp_error (pfile
, CPP_DL_ICE
, "registering pragma with NULL handler");
1376 entry
= register_pragma_1 (pfile
, space
, name
, false);
1379 entry
->allow_expansion
= allow_expansion
;
1380 entry
->u
.handler
= handler
;
1384 /* Similarly, but create mark the pragma for deferred processing.
1385 When found, a CPP_PRAGMA token will be insertted into the stream
1386 with IDENT in the token->u.pragma slot. */
1388 cpp_register_deferred_pragma (cpp_reader
*pfile
, const char *space
,
1389 const char *name
, unsigned int ident
,
1390 bool allow_expansion
, bool allow_name_expansion
)
1392 struct pragma_entry
*entry
;
1394 entry
= register_pragma_1 (pfile
, space
, name
, allow_name_expansion
);
1397 entry
->is_deferred
= true;
1398 entry
->allow_expansion
= allow_expansion
;
1399 entry
->u
.ident
= ident
;
1403 /* Register the pragmas the preprocessor itself handles. */
1405 _cpp_init_internal_pragmas (cpp_reader
*pfile
)
1407 /* Pragmas in the global namespace. */
1408 register_pragma_internal (pfile
, 0, "once", do_pragma_once
);
1409 register_pragma_internal (pfile
, 0, "push_macro", do_pragma_push_macro
);
1410 register_pragma_internal (pfile
, 0, "pop_macro", do_pragma_pop_macro
);
1412 /* New GCC-specific pragmas should be put in the GCC namespace. */
1413 register_pragma_internal (pfile
, "GCC", "poison", do_pragma_poison
);
1414 register_pragma_internal (pfile
, "GCC", "system_header",
1415 do_pragma_system_header
);
1416 register_pragma_internal (pfile
, "GCC", "dependency", do_pragma_dependency
);
1417 register_pragma_internal (pfile
, "GCC", "warning", do_pragma_warning
);
1418 register_pragma_internal (pfile
, "GCC", "error", do_pragma_error
);
1421 /* Return the number of registered pragmas in PE. */
1424 count_registered_pragmas (struct pragma_entry
*pe
)
1427 for (; pe
!= NULL
; pe
= pe
->next
)
1430 ct
+= count_registered_pragmas (pe
->u
.space
);
1436 /* Save into SD the names of the registered pragmas referenced by PE,
1437 and return a pointer to the next free space in SD. */
1440 save_registered_pragmas (struct pragma_entry
*pe
, char **sd
)
1442 for (; pe
!= NULL
; pe
= pe
->next
)
1445 sd
= save_registered_pragmas (pe
->u
.space
, sd
);
1446 *sd
++ = (char *) xmemdup (HT_STR (&pe
->pragma
->ident
),
1447 HT_LEN (&pe
->pragma
->ident
),
1448 HT_LEN (&pe
->pragma
->ident
) + 1);
1453 /* Return a newly-allocated array which saves the names of the
1454 registered pragmas. */
1457 _cpp_save_pragma_names (cpp_reader
*pfile
)
1459 int ct
= count_registered_pragmas (pfile
->pragmas
);
1460 char **result
= XNEWVEC (char *, ct
);
1461 (void) save_registered_pragmas (pfile
->pragmas
, result
);
1465 /* Restore from SD the names of the registered pragmas referenced by PE,
1466 and return a pointer to the next unused name in SD. */
1469 restore_registered_pragmas (cpp_reader
*pfile
, struct pragma_entry
*pe
,
1472 for (; pe
!= NULL
; pe
= pe
->next
)
1475 sd
= restore_registered_pragmas (pfile
, pe
->u
.space
, sd
);
1476 pe
->pragma
= cpp_lookup (pfile
, UC
*sd
, strlen (*sd
));
1483 /* Restore the names of the registered pragmas from SAVED. */
1486 _cpp_restore_pragma_names (cpp_reader
*pfile
, char **saved
)
1488 (void) restore_registered_pragmas (pfile
, pfile
->pragmas
, saved
);
1492 /* Pragmata handling. We handle some, and pass the rest on to the
1493 front end. C99 defines three pragmas and says that no macro
1494 expansion is to be performed on them; whether or not macro
1495 expansion happens for other pragmas is implementation defined.
1496 This implementation allows for a mix of both, since GCC did not
1497 traditionally macro expand its (few) pragmas, whereas OpenMP
1498 specifies that macro expansion should happen. */
1500 do_pragma (cpp_reader
*pfile
)
1502 const struct pragma_entry
*p
= NULL
;
1503 const cpp_token
*token
, *pragma_token
;
1504 location_t pragma_token_virt_loc
= 0;
1506 unsigned int count
= 1;
1508 pfile
->state
.prevent_expansion
++;
1510 pragma_token
= token
= cpp_get_token_with_location (pfile
,
1511 &pragma_token_virt_loc
);
1513 if (token
->type
== CPP_NAME
)
1515 p
= lookup_pragma_entry (pfile
->pragmas
, token
->val
.node
.node
);
1516 if (p
&& p
->is_nspace
)
1518 bool allow_name_expansion
= p
->allow_expansion
;
1519 if (allow_name_expansion
)
1520 pfile
->state
.prevent_expansion
--;
1522 token
= cpp_get_token (pfile
);
1523 if (token
->type
== CPP_NAME
)
1524 p
= lookup_pragma_entry (p
->u
.space
, token
->val
.node
.node
);
1527 if (allow_name_expansion
)
1528 pfile
->state
.prevent_expansion
++;
1537 pfile
->directive_result
.src_loc
= pragma_token_virt_loc
;
1538 pfile
->directive_result
.type
= CPP_PRAGMA
;
1539 pfile
->directive_result
.flags
= pragma_token
->flags
;
1540 pfile
->directive_result
.val
.pragma
= p
->u
.ident
;
1541 pfile
->state
.in_deferred_pragma
= true;
1542 pfile
->state
.pragma_allow_expansion
= p
->allow_expansion
;
1543 if (!p
->allow_expansion
)
1544 pfile
->state
.prevent_expansion
++;
1548 /* Since the handler below doesn't get the line number, that
1549 it might need for diagnostics, make sure it has the right
1550 numbers in place. */
1551 if (pfile
->cb
.line_change
)
1552 (*pfile
->cb
.line_change
) (pfile
, pragma_token
, false);
1553 if (p
->allow_expansion
)
1554 pfile
->state
.prevent_expansion
--;
1555 (*p
->u
.handler
) (pfile
);
1556 if (p
->allow_expansion
)
1557 pfile
->state
.prevent_expansion
++;
1560 else if (pfile
->cb
.def_pragma
)
1562 if (count
== 1 || pfile
->context
->prev
== NULL
)
1563 _cpp_backup_tokens (pfile
, count
);
1566 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1567 won't allow backing 2 tokens. */
1568 const auto tok_buff
= _cpp_get_buff (pfile
, 2 * sizeof (cpp_token
));
1569 const auto toks
= (cpp_token
*)tok_buff
->base
;
1571 toks
[0].flags
|= NO_EXPAND
;
1573 toks
[1].flags
|= NO_EXPAND
| PREV_WHITE
;
1574 _cpp_push_token_context (pfile
, NULL
, toks
, 2);
1575 /* Arrange to free this buffer when no longer needed. */
1576 pfile
->context
->buff
= tok_buff
;
1578 pfile
->cb
.def_pragma (pfile
, pfile
->directive_line
);
1581 pfile
->state
.prevent_expansion
--;
1584 /* Handle #pragma once. */
1586 do_pragma_once (cpp_reader
*pfile
)
1588 if (_cpp_in_main_source_file (pfile
))
1589 cpp_error (pfile
, CPP_DL_WARNING
, "#pragma once in main file");
1591 check_eol (pfile
, false);
1592 _cpp_mark_file_once_only (pfile
, pfile
->buffer
->file
);
1595 /* Handle #pragma push_macro(STRING). */
1597 do_pragma_push_macro (cpp_reader
*pfile
)
1601 const uchar
*defn
= NULL
;
1602 char *macroname
, *dest
;
1603 const char *limit
, *src
;
1604 const cpp_token
*txt
;
1605 struct def_pragma_macro
*c
;
1607 txt
= get__Pragma_string (pfile
);
1610 location_t src_loc
= pfile
->cur_token
[-1].src_loc
;
1611 cpp_error_with_line (pfile
, CPP_DL_ERROR
, src_loc
, 0,
1612 "invalid #pragma push_macro directive");
1613 check_eol (pfile
, false);
1614 skip_rest_of_line (pfile
);
1617 dest
= macroname
= (char *) alloca (txt
->val
.str
.len
+ 2);
1618 src
= (const char *) (txt
->val
.str
.text
+ 1 + (txt
->val
.str
.text
[0] == 'L'));
1619 limit
= (const char *) (txt
->val
.str
.text
+ txt
->val
.str
.len
- 1);
1622 /* We know there is a character following the backslash. */
1623 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1628 check_eol (pfile
, false);
1629 skip_rest_of_line (pfile
);
1630 c
= XNEW (struct def_pragma_macro
);
1631 memset (c
, 0, sizeof (struct def_pragma_macro
));
1632 c
->name
= XNEWVAR (char, strlen (macroname
) + 1);
1633 strcpy (c
->name
, macroname
);
1634 c
->next
= pfile
->pushed_macros
;
1635 node
= _cpp_lex_identifier (pfile
, c
->name
);
1636 if (node
->type
== NT_VOID
)
1638 else if (node
->type
== NT_BUILTIN_MACRO
)
1642 defn
= cpp_macro_definition (pfile
, node
);
1643 defnlen
= ustrlen (defn
);
1644 c
->definition
= XNEWVEC (uchar
, defnlen
+ 2);
1645 c
->definition
[defnlen
] = '\n';
1646 c
->definition
[defnlen
+ 1] = 0;
1647 c
->line
= node
->value
.macro
->line
;
1648 c
->syshdr
= node
->value
.macro
->syshdr
;
1649 c
->used
= node
->value
.macro
->used
;
1650 memcpy (c
->definition
, defn
, defnlen
);
1653 pfile
->pushed_macros
= c
;
1656 /* Handle #pragma pop_macro(STRING). */
1658 do_pragma_pop_macro (cpp_reader
*pfile
)
1660 char *macroname
, *dest
;
1661 const char *limit
, *src
;
1662 const cpp_token
*txt
;
1663 struct def_pragma_macro
*l
= NULL
, *c
= pfile
->pushed_macros
;
1664 txt
= get__Pragma_string (pfile
);
1667 location_t src_loc
= pfile
->cur_token
[-1].src_loc
;
1668 cpp_error_with_line (pfile
, CPP_DL_ERROR
, src_loc
, 0,
1669 "invalid #pragma pop_macro directive");
1670 check_eol (pfile
, false);
1671 skip_rest_of_line (pfile
);
1674 dest
= macroname
= (char *) alloca (txt
->val
.str
.len
+ 2);
1675 src
= (const char *) (txt
->val
.str
.text
+ 1 + (txt
->val
.str
.text
[0] == 'L'));
1676 limit
= (const char *) (txt
->val
.str
.text
+ txt
->val
.str
.len
- 1);
1679 /* We know there is a character following the backslash. */
1680 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1685 check_eol (pfile
, false);
1686 skip_rest_of_line (pfile
);
1690 if (!strcmp (c
->name
, macroname
))
1693 pfile
->pushed_macros
= c
->next
;
1696 cpp_pop_definition (pfile
, c
);
1697 free (c
->definition
);
1707 /* Handle #pragma GCC poison, to poison one or more identifiers so
1708 that the lexer produces a hard error for each subsequent usage. */
1710 do_pragma_poison (cpp_reader
*pfile
)
1712 const cpp_token
*tok
;
1715 pfile
->state
.poisoned_ok
= 1;
1718 tok
= _cpp_lex_token (pfile
);
1719 if (tok
->type
== CPP_EOF
)
1721 if (tok
->type
!= CPP_NAME
)
1723 cpp_error (pfile
, CPP_DL_ERROR
,
1724 "invalid #pragma GCC poison directive");
1728 hp
= tok
->val
.node
.node
;
1729 if (hp
->flags
& NODE_POISONED
)
1732 if (cpp_macro_p (hp
))
1733 cpp_error (pfile
, CPP_DL_WARNING
, "poisoning existing macro \"%s\"",
1735 _cpp_free_definition (hp
);
1736 hp
->flags
|= NODE_POISONED
| NODE_DIAGNOSTIC
;
1738 pfile
->state
.poisoned_ok
= 0;
1741 /* Mark the current header as a system header. This will suppress
1742 some categories of warnings (notably those from -pedantic). It is
1743 intended for use in system libraries that cannot be implemented in
1744 conforming C, but cannot be certain that their headers appear in a
1745 system include directory. To prevent abuse, it is rejected in the
1746 primary source file. */
1748 do_pragma_system_header (cpp_reader
*pfile
)
1750 if (_cpp_in_main_source_file (pfile
))
1751 cpp_error (pfile
, CPP_DL_WARNING
,
1752 "#pragma system_header ignored outside include file");
1755 check_eol (pfile
, false);
1756 skip_rest_of_line (pfile
);
1757 cpp_make_system_header (pfile
, 1, 0);
1761 /* Check the modified date of the current include file against a specified
1762 file. Issue a diagnostic, if the specified file is newer. We use this to
1763 determine if a fixed header should be refixed. */
1765 do_pragma_dependency (cpp_reader
*pfile
)
1768 int angle_brackets
, ordering
;
1769 location_t location
;
1771 fname
= parse_include (pfile
, &angle_brackets
, NULL
, &location
);
1775 ordering
= _cpp_compare_file_date (pfile
, fname
, angle_brackets
);
1777 cpp_error (pfile
, CPP_DL_WARNING
, "cannot find source file %s", fname
);
1778 else if (ordering
> 0)
1780 cpp_error (pfile
, CPP_DL_WARNING
,
1781 "current file is older than %s", fname
);
1782 if (cpp_get_token (pfile
)->type
!= CPP_EOF
)
1784 _cpp_backup_tokens (pfile
, 1);
1785 do_diagnostic (pfile
, CPP_DL_WARNING
, CPP_W_NONE
, 0);
1789 free ((void *) fname
);
1792 /* Issue a diagnostic with the message taken from the pragma. If
1793 ERROR is true, the diagnostic is a warning, otherwise, it is an
1796 do_pragma_warning_or_error (cpp_reader
*pfile
, bool error
)
1798 const cpp_token
*tok
= _cpp_lex_token (pfile
);
1800 if (tok
->type
!= CPP_STRING
1801 || !cpp_interpret_string_notranslate (pfile
, &tok
->val
.str
, 1, &str
,
1805 cpp_error (pfile
, CPP_DL_ERROR
, "invalid \"#pragma GCC %s\" directive",
1806 error
? "error" : "warning");
1809 cpp_error (pfile
, error
? CPP_DL_ERROR
: CPP_DL_WARNING
,
1811 free ((void *)str
.text
);
1814 /* Issue a warning diagnostic. */
1816 do_pragma_warning (cpp_reader
*pfile
)
1818 do_pragma_warning_or_error (pfile
, false);
1821 /* Issue an error diagnostic. */
1823 do_pragma_error (cpp_reader
*pfile
)
1825 do_pragma_warning_or_error (pfile
, true);
1828 /* Get a token but skip padding. */
1829 static const cpp_token
*
1830 get_token_no_padding (cpp_reader
*pfile
)
1834 const cpp_token
*result
= cpp_get_token (pfile
);
1835 if (result
->type
!= CPP_PADDING
)
1840 /* Check syntax is "(string-literal)". Returns the string on success,
1841 or NULL on failure. */
1842 static const cpp_token
*
1843 get__Pragma_string (cpp_reader
*pfile
)
1845 const cpp_token
*string
;
1846 const cpp_token
*paren
;
1848 paren
= get_token_no_padding (pfile
);
1849 if (paren
->type
== CPP_EOF
)
1850 _cpp_backup_tokens (pfile
, 1);
1851 if (paren
->type
!= CPP_OPEN_PAREN
)
1854 string
= get_token_no_padding (pfile
);
1855 if (string
->type
== CPP_EOF
)
1856 _cpp_backup_tokens (pfile
, 1);
1857 if (string
->type
!= CPP_STRING
&& string
->type
!= CPP_WSTRING
1858 && string
->type
!= CPP_STRING32
&& string
->type
!= CPP_STRING16
1859 && string
->type
!= CPP_UTF8STRING
)
1862 paren
= get_token_no_padding (pfile
);
1863 if (paren
->type
== CPP_EOF
)
1864 _cpp_backup_tokens (pfile
, 1);
1865 if (paren
->type
!= CPP_CLOSE_PAREN
)
1871 /* Destringize IN into a temporary buffer, by removing the first \ of
1872 \" and \\ sequences, and process the result as a #pragma directive. */
1874 destringize_and_run (cpp_reader
*pfile
, const cpp_string
*in
,
1875 location_t expansion_loc
)
1877 const unsigned char *src
, *limit
;
1878 char *dest
, *result
;
1879 cpp_context
*saved_context
;
1880 cpp_token
*saved_cur_token
;
1881 tokenrun
*saved_cur_run
;
1884 const struct directive
*save_directive
;
1886 dest
= result
= (char *) alloca (in
->len
- 1);
1887 src
= in
->text
+ 1 + (in
->text
[0] == 'L');
1888 limit
= in
->text
+ in
->len
- 1;
1891 /* We know there is a character following the backslash. */
1892 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1898 /* Ugh; an awful kludge. We are really not set up to be lexing
1899 tokens when in the middle of a macro expansion. Use a new
1900 context to force cpp_get_token to lex, and so skip_rest_of_line
1901 doesn't go beyond the end of the text. Also, remember the
1902 current lexing position so we can return to it later.
1904 Something like line-at-a-time lexing should remove the need for
1906 saved_context
= pfile
->context
;
1907 saved_cur_token
= pfile
->cur_token
;
1908 saved_cur_run
= pfile
->cur_run
;
1910 pfile
->context
= XCNEW (cpp_context
);
1912 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1913 until we've read all of the tokens that we want. */
1914 cpp_push_buffer (pfile
, (const uchar
*) result
, dest
- result
,
1915 /* from_stage3 */ true);
1916 /* ??? Antique Disgusting Hack. What does this do? */
1917 if (pfile
->buffer
->prev
)
1918 pfile
->buffer
->file
= pfile
->buffer
->prev
->file
;
1920 start_directive (pfile
);
1921 _cpp_clean_line (pfile
);
1922 save_directive
= pfile
->directive
;
1923 pfile
->directive
= &dtable
[T_PRAGMA
];
1925 if (pfile
->directive_result
.type
== CPP_PRAGMA
)
1926 pfile
->directive_result
.flags
|= PRAGMA_OP
;
1927 end_directive (pfile
, 1);
1928 pfile
->directive
= save_directive
;
1930 /* We always insert at least one token, the directive result. It'll
1931 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1932 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
1934 /* If we're not handling the pragma internally, read all of the tokens from
1935 the string buffer now, while the string buffer is still installed. */
1936 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1937 to me what the true lifespan of the tokens are. It would appear that
1938 the lifespan is the entire parse of the main input stream, in which case
1939 this may not be wrong. */
1940 if (pfile
->directive_result
.type
== CPP_PRAGMA
)
1946 toks
= XNEWVEC (cpp_token
, maxcount
);
1947 toks
[0] = pfile
->directive_result
;
1948 toks
[0].src_loc
= expansion_loc
;
1952 if (count
== maxcount
)
1954 maxcount
= maxcount
* 3 / 2;
1955 toks
= XRESIZEVEC (cpp_token
, toks
, maxcount
);
1957 toks
[count
] = *cpp_get_token (pfile
);
1958 /* _Pragma is a builtin, so we're not within a macro-map, and so
1959 the token locations are set to bogus ordinary locations
1960 near to, but after that of the "_Pragma".
1961 Paper over this by setting them equal to the location of the
1962 _Pragma itself (PR preprocessor/69126). */
1963 toks
[count
].src_loc
= expansion_loc
;
1964 /* Macros have been already expanded by cpp_get_token
1965 if the pragma allowed expansion. */
1966 toks
[count
++].flags
|= NO_EXPAND
;
1968 while (toks
[count
-1].type
!= CPP_PRAGMA_EOL
);
1973 toks
= &pfile
->avoid_paste
;
1975 /* If we handled the entire pragma internally, make sure we get the
1976 line number correct for the next token. */
1977 if (pfile
->cb
.line_change
)
1978 pfile
->cb
.line_change (pfile
, pfile
->cur_token
, false);
1981 /* Finish inlining run_directive. */
1982 pfile
->buffer
->file
= NULL
;
1983 _cpp_pop_buffer (pfile
);
1985 /* Reset the old macro state before ... */
1986 XDELETE (pfile
->context
);
1987 pfile
->context
= saved_context
;
1988 pfile
->cur_token
= saved_cur_token
;
1989 pfile
->cur_run
= saved_cur_run
;
1991 /* ... inserting the new tokens we collected. */
1992 _cpp_push_token_context (pfile
, NULL
, toks
, count
);
1995 /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
1997 _cpp_do__Pragma (cpp_reader
*pfile
, location_t expansion_loc
)
1999 const cpp_token
*string
= get__Pragma_string (pfile
);
2000 pfile
->directive_result
.type
= CPP_PADDING
;
2004 destringize_and_run (pfile
, &string
->val
.str
, expansion_loc
);
2007 cpp_error (pfile
, CPP_DL_ERROR
,
2008 "_Pragma takes a parenthesized string literal");
2012 /* Handle #ifdef. */
2014 do_ifdef (cpp_reader
*pfile
)
2018 if (! pfile
->state
.skipping
)
2020 cpp_hashnode
*node
= lex_macro_node (pfile
, false);
2024 skip
= !_cpp_defined_macro_p (node
);
2025 if (!_cpp_maybe_notify_macro_use (pfile
, node
, pfile
->directive_line
))
2026 /* It wasn't a macro after all. */
2028 _cpp_mark_macro_used (node
);
2030 pfile
->cb
.used (pfile
, pfile
->directive_line
, node
);
2031 check_eol (pfile
, false);
2035 push_conditional (pfile
, skip
, T_IFDEF
, 0);
2038 /* Handle #ifndef. */
2040 do_ifndef (cpp_reader
*pfile
)
2043 cpp_hashnode
*node
= 0;
2045 if (! pfile
->state
.skipping
)
2047 node
= lex_macro_node (pfile
, false);
2051 skip
= _cpp_defined_macro_p (node
);
2052 if (!_cpp_maybe_notify_macro_use (pfile
, node
, pfile
->directive_line
))
2053 /* It wasn't a macro after all. */
2055 _cpp_mark_macro_used (node
);
2057 pfile
->cb
.used (pfile
, pfile
->directive_line
, node
);
2058 check_eol (pfile
, false);
2062 push_conditional (pfile
, skip
, T_IFNDEF
, node
);
2065 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
2066 pfile->mi_ind_cmacro so we can handle multiple-include
2067 optimizations. If macro expansion occurs in the expression, we
2068 cannot treat it as a controlling conditional, since the expansion
2069 could change in the future. That is handled by cpp_get_token. */
2071 do_if (cpp_reader
*pfile
)
2075 if (! pfile
->state
.skipping
)
2076 skip
= _cpp_parse_expr (pfile
, true) == false;
2078 push_conditional (pfile
, skip
, T_IF
, pfile
->mi_ind_cmacro
);
2081 /* Flip skipping state if appropriate and continue without changing
2082 if_stack; this is so that the error message for missing #endif's
2083 etc. will point to the original #if. */
2085 do_else (cpp_reader
*pfile
)
2087 cpp_buffer
*buffer
= pfile
->buffer
;
2088 struct if_stack
*ifs
= buffer
->if_stack
;
2091 cpp_error (pfile
, CPP_DL_ERROR
, "#else without #if");
2094 if (ifs
->type
== T_ELSE
)
2096 cpp_error (pfile
, CPP_DL_ERROR
, "#else after #else");
2097 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2098 "the conditional began here");
2102 /* Skip any future (erroneous) #elses or #elifs. */
2103 pfile
->state
.skipping
= ifs
->skip_elses
;
2104 ifs
->skip_elses
= true;
2106 /* Invalidate any controlling macro. */
2109 /* Only check EOL if was not originally skipping. */
2110 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
2111 check_eol_endif_labels (pfile
);
2115 /* Handle a #elif, #elifdef or #elifndef directive by not changing if_stack
2116 either. See the comment above do_else. */
2118 do_elif (cpp_reader
*pfile
)
2120 cpp_buffer
*buffer
= pfile
->buffer
;
2121 struct if_stack
*ifs
= buffer
->if_stack
;
2124 cpp_error (pfile
, CPP_DL_ERROR
, "#%s without #if", pfile
->directive
->name
);
2127 if (ifs
->type
== T_ELSE
)
2129 cpp_error (pfile
, CPP_DL_ERROR
, "#%s after #else",
2130 pfile
->directive
->name
);
2131 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2132 "the conditional began here");
2136 /* See DR#412: "Only the first group whose control condition
2137 evaluates to true (nonzero) is processed; any following groups
2138 are skipped and their controlling directives are processed as
2139 if they were in a group that is skipped." */
2140 if (ifs
->skip_elses
)
2142 /* In older GNU standards, #elifdef/#elifndef is supported
2143 as an extension, but pedwarn if -pedantic if the presence
2144 of the directive would be rejected. */
2145 if (pfile
->directive
!= &dtable
[T_ELIF
]
2146 && ! CPP_OPTION (pfile
, elifdef
)
2147 && CPP_PEDANTIC (pfile
)
2148 && !pfile
->state
.skipping
)
2150 if (CPP_OPTION (pfile
, cplusplus
))
2151 cpp_error (pfile
, CPP_DL_PEDWARN
,
2152 "#%s before C++23 is a GCC extension",
2153 pfile
->directive
->name
);
2155 cpp_error (pfile
, CPP_DL_PEDWARN
,
2156 "#%s before C2X is a GCC extension",
2157 pfile
->directive
->name
);
2159 pfile
->state
.skipping
= 1;
2163 if (pfile
->directive
== &dtable
[T_ELIF
])
2164 pfile
->state
.skipping
= !_cpp_parse_expr (pfile
, false);
2167 cpp_hashnode
*node
= lex_macro_node (pfile
, false);
2171 bool macro_defined
= _cpp_defined_macro_p (node
);
2172 if (!_cpp_maybe_notify_macro_use (pfile
, node
,
2173 pfile
->directive_line
))
2174 /* It wasn't a macro after all. */
2175 macro_defined
= false;
2176 bool skip
= (pfile
->directive
== &dtable
[T_ELIFDEF
]
2180 pfile
->cb
.used (pfile
, pfile
->directive_line
, node
);
2181 check_eol (pfile
, false);
2182 /* In older GNU standards, #elifdef/#elifndef is supported
2183 as an extension, but pedwarn if -pedantic if the presence
2184 of the directive would change behavior. */
2185 if (! CPP_OPTION (pfile
, elifdef
)
2186 && CPP_PEDANTIC (pfile
)
2187 && pfile
->state
.skipping
!= skip
)
2189 if (CPP_OPTION (pfile
, cplusplus
))
2190 cpp_error (pfile
, CPP_DL_PEDWARN
,
2191 "#%s before C++23 is a GCC extension",
2192 pfile
->directive
->name
);
2194 cpp_error (pfile
, CPP_DL_PEDWARN
,
2195 "#%s before C2X is a GCC extension",
2196 pfile
->directive
->name
);
2198 pfile
->state
.skipping
= skip
;
2201 ifs
->skip_elses
= !pfile
->state
.skipping
;
2204 /* Invalidate any controlling macro. */
2209 /* Handle a #elifdef directive. */
2211 do_elifdef (cpp_reader
*pfile
)
2216 /* Handle a #elifndef directive. */
2218 do_elifndef (cpp_reader
*pfile
)
2223 /* #endif pops the if stack and resets pfile->state.skipping. */
2225 do_endif (cpp_reader
*pfile
)
2227 cpp_buffer
*buffer
= pfile
->buffer
;
2228 struct if_stack
*ifs
= buffer
->if_stack
;
2231 cpp_error (pfile
, CPP_DL_ERROR
, "#endif without #if");
2234 /* Only check EOL if was not originally skipping. */
2235 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
2236 check_eol_endif_labels (pfile
);
2238 /* If potential control macro, we go back outside again. */
2239 if (ifs
->next
== 0 && ifs
->mi_cmacro
)
2241 pfile
->mi_valid
= true;
2242 pfile
->mi_cmacro
= ifs
->mi_cmacro
;
2245 buffer
->if_stack
= ifs
->next
;
2246 pfile
->state
.skipping
= ifs
->was_skipping
;
2247 obstack_free (&pfile
->buffer_ob
, ifs
);
2251 /* Push an if_stack entry for a preprocessor conditional, and set
2252 pfile->state.skipping to SKIP. If TYPE indicates the conditional
2253 is #if or #ifndef, CMACRO is a potentially controlling macro, and
2254 we need to check here that we are at the top of the file. */
2256 push_conditional (cpp_reader
*pfile
, int skip
, int type
,
2257 const cpp_hashnode
*cmacro
)
2259 struct if_stack
*ifs
;
2260 cpp_buffer
*buffer
= pfile
->buffer
;
2262 ifs
= XOBNEW (&pfile
->buffer_ob
, struct if_stack
);
2263 ifs
->line
= pfile
->directive_line
;
2264 ifs
->next
= buffer
->if_stack
;
2265 ifs
->skip_elses
= pfile
->state
.skipping
|| !skip
;
2266 ifs
->was_skipping
= pfile
->state
.skipping
;
2268 /* This condition is effectively a test for top-of-file. */
2269 if (pfile
->mi_valid
&& pfile
->mi_cmacro
== 0)
2270 ifs
->mi_cmacro
= cmacro
;
2274 pfile
->state
.skipping
= skip
;
2275 buffer
->if_stack
= ifs
;
2278 /* Read the tokens of the answer into the macro pool, in a directive
2279 of type TYPE. Only commit the memory if we intend it as permanent
2280 storage, i.e. the #assert case. Returns 0 on success, and sets
2281 ANSWERP to point to the answer. PRED_LOC is the location of the
2284 parse_answer (cpp_reader
*pfile
, int type
, location_t pred_loc
,
2285 cpp_macro
**answer_ptr
)
2287 /* In a conditional, it is legal to not have an open paren. We
2288 should save the following token in this case. */
2289 const cpp_token
*paren
= cpp_get_token (pfile
);
2291 /* If not a paren, see if we're OK. */
2292 if (paren
->type
!= CPP_OPEN_PAREN
)
2294 /* In a conditional no answer is a test for any answer. It
2295 could be followed by any token. */
2298 _cpp_backup_tokens (pfile
, 1);
2302 /* #unassert with no answer is valid - it removes all answers. */
2303 if (type
== T_UNASSERT
&& paren
->type
== CPP_EOF
)
2306 cpp_error_with_line (pfile
, CPP_DL_ERROR
, pred_loc
, 0,
2307 "missing '(' after predicate");
2311 cpp_macro
*answer
= _cpp_new_macro (pfile
, cmk_assert
,
2312 _cpp_reserve_room (pfile
, 0,
2313 sizeof (cpp_macro
)));
2314 answer
->parm
.next
= NULL
;
2318 const cpp_token
*token
= cpp_get_token (pfile
);
2320 if (token
->type
== CPP_CLOSE_PAREN
)
2323 if (token
->type
== CPP_EOF
)
2325 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' to complete answer");
2329 answer
= (cpp_macro
*)_cpp_reserve_room
2330 (pfile
, sizeof (cpp_macro
) + count
* sizeof (cpp_token
),
2331 sizeof (cpp_token
));
2332 answer
->exp
.tokens
[count
++] = *token
;
2337 cpp_error (pfile
, CPP_DL_ERROR
, "predicate's answer is empty");
2341 /* Drop whitespace at start, for answer equivalence purposes. */
2342 answer
->exp
.tokens
[0].flags
&= ~PREV_WHITE
;
2344 answer
->count
= count
;
2345 *answer_ptr
= answer
;
2350 /* Parses an assertion directive of type TYPE, returning a pointer to
2351 the hash node of the predicate, or 0 on error. The node is
2352 guaranteed to be disjoint from the macro namespace, so can only
2353 have type 'NT_VOID'. If an answer was supplied, it is placed in
2354 *ANSWER_PTR, which is otherwise set to 0. */
2355 static cpp_hashnode
*
2356 parse_assertion (cpp_reader
*pfile
, int type
, cpp_macro
**answer_ptr
)
2358 cpp_hashnode
*result
= 0;
2360 /* We don't expand predicates or answers. */
2361 pfile
->state
.prevent_expansion
++;
2365 const cpp_token
*predicate
= cpp_get_token (pfile
);
2366 if (predicate
->type
== CPP_EOF
)
2367 cpp_error (pfile
, CPP_DL_ERROR
, "assertion without predicate");
2368 else if (predicate
->type
!= CPP_NAME
)
2369 cpp_error_with_line (pfile
, CPP_DL_ERROR
, predicate
->src_loc
, 0,
2370 "predicate must be an identifier");
2371 else if (parse_answer (pfile
, type
, predicate
->src_loc
, answer_ptr
))
2373 unsigned int len
= NODE_LEN (predicate
->val
.node
.node
);
2374 unsigned char *sym
= (unsigned char *) alloca (len
+ 1);
2376 /* Prefix '#' to get it out of macro namespace. */
2378 memcpy (sym
+ 1, NODE_NAME (predicate
->val
.node
.node
), len
);
2379 result
= cpp_lookup (pfile
, sym
, len
+ 1);
2382 pfile
->state
.prevent_expansion
--;
2387 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
2388 or a pointer to NULL if the answer is not in the chain. */
2390 find_answer (cpp_hashnode
*node
, const cpp_macro
*candidate
)
2393 cpp_macro
**result
= NULL
;
2395 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->parm
.next
)
2397 cpp_macro
*answer
= *result
;
2399 if (answer
->count
== candidate
->count
)
2401 for (i
= 0; i
< answer
->count
; i
++)
2402 if (!_cpp_equiv_tokens (&answer
->exp
.tokens
[i
],
2403 &candidate
->exp
.tokens
[i
]))
2406 if (i
== answer
->count
)
2414 /* Test an assertion within a preprocessor conditional. Returns
2415 nonzero on failure, zero on success. On success, the result of
2416 the test is written into VALUE, otherwise the value 0. */
2418 _cpp_test_assertion (cpp_reader
*pfile
, unsigned int *value
)
2421 cpp_hashnode
*node
= parse_assertion (pfile
, T_IF
, &answer
);
2423 /* For recovery, an erroneous assertion expression is handled as a
2424 failing assertion. */
2429 if (node
->value
.answers
)
2430 *value
= !answer
|| *find_answer (node
, answer
);
2432 else if (pfile
->cur_token
[-1].type
== CPP_EOF
)
2433 _cpp_backup_tokens (pfile
, 1);
2435 /* We don't commit the memory for the answer - it's temporary only. */
2439 /* Handle #assert. */
2441 do_assert (cpp_reader
*pfile
)
2444 cpp_hashnode
*node
= parse_assertion (pfile
, T_ASSERT
, &answer
);
2448 /* Place the new answer in the answer list. First check there
2449 is not a duplicate. */
2450 if (*find_answer (node
, answer
))
2452 cpp_error (pfile
, CPP_DL_WARNING
, "\"%s\" re-asserted",
2453 NODE_NAME (node
) + 1);
2457 /* Commit or allocate storage for the answer. */
2458 answer
= (cpp_macro
*)_cpp_commit_buff
2459 (pfile
, sizeof (cpp_macro
) - sizeof (cpp_token
)
2460 + sizeof (cpp_token
) * answer
->count
);
2462 /* Chain into the list. */
2463 answer
->parm
.next
= node
->value
.answers
;
2464 node
->value
.answers
= answer
;
2466 check_eol (pfile
, false);
2470 /* Handle #unassert. */
2472 do_unassert (cpp_reader
*pfile
)
2475 cpp_hashnode
*node
= parse_assertion (pfile
, T_UNASSERT
, &answer
);
2477 /* It isn't an error to #unassert something that isn't asserted. */
2482 cpp_macro
**p
= find_answer (node
, answer
);
2484 /* Remove the assert from the list. */
2485 if (cpp_macro
*temp
= *p
)
2486 *p
= temp
->parm
.next
;
2488 check_eol (pfile
, false);
2491 _cpp_free_definition (node
);
2494 /* We don't commit the memory for the answer - it's temporary only. */
2497 /* These are for -D, -U, -A. */
2499 /* Process the string STR as if it appeared as the body of a #define.
2500 If STR is just an identifier, define it with value 1.
2501 If STR has anything after the identifier, then it should
2502 be identifier=definition. */
2504 cpp_define (cpp_reader
*pfile
, const char *str
)
2510 /* Copy the entire option so we can modify it.
2511 Change the first "=" in the string to a space. If there is none,
2512 tack " 1" on the end. */
2514 count
= strlen (str
);
2515 buf
= (char *) alloca (count
+ 3);
2516 memcpy (buf
, str
, count
);
2518 p
= strchr (str
, '=');
2528 run_directive (pfile
, T_DEFINE
, buf
, count
);
2531 /* Like cpp_define, but does not warn about unused macro. */
2533 cpp_define_unused (cpp_reader
*pfile
, const char *str
)
2535 unsigned char warn_unused_macros
= CPP_OPTION (pfile
, warn_unused_macros
);
2536 CPP_OPTION (pfile
, warn_unused_macros
) = 0;
2537 cpp_define (pfile
, str
);
2538 CPP_OPTION (pfile
, warn_unused_macros
) = warn_unused_macros
;
2541 /* Use to build macros to be run through cpp_define() as
2543 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
2546 cpp_define_formatted (cpp_reader
*pfile
, const char *fmt
, ...)
2552 ptr
= xvasprintf (fmt
, ap
);
2555 cpp_define (pfile
, ptr
);
2559 /* Like cpp_define_formatted, but does not warn about unused macro. */
2561 cpp_define_formatted_unused (cpp_reader
*pfile
, const char *fmt
, ...)
2567 ptr
= xvasprintf (fmt
, ap
);
2570 cpp_define_unused (pfile
, ptr
);
2574 /* Slight variant of the above for use by initialize_builtins. */
2576 _cpp_define_builtin (cpp_reader
*pfile
, const char *str
)
2578 size_t len
= strlen (str
);
2579 char *buf
= (char *) alloca (len
+ 1);
2580 memcpy (buf
, str
, len
);
2582 run_directive (pfile
, T_DEFINE
, buf
, len
);
2585 /* Process MACRO as if it appeared as the body of an #undef. */
2587 cpp_undef (cpp_reader
*pfile
, const char *macro
)
2589 size_t len
= strlen (macro
);
2590 char *buf
= (char *) alloca (len
+ 1);
2591 memcpy (buf
, macro
, len
);
2593 run_directive (pfile
, T_UNDEF
, buf
, len
);
2596 /* Replace a previous definition DEF of the macro STR. If DEF is NULL,
2597 or first element is zero, then the macro should be undefined. */
2599 cpp_pop_definition (cpp_reader
*pfile
, struct def_pragma_macro
*c
)
2601 cpp_hashnode
*node
= _cpp_lex_identifier (pfile
, c
->name
);
2605 if (pfile
->cb
.before_define
)
2606 pfile
->cb
.before_define (pfile
);
2608 if (cpp_macro_p (node
))
2610 if (pfile
->cb
.undef
)
2611 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
2612 if (CPP_OPTION (pfile
, warn_unused_macros
))
2613 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
2614 _cpp_free_definition (node
);
2621 _cpp_restore_special_builtin (pfile
, c
);
2628 cpp_hashnode
*h
= NULL
;
2631 namelen
= ustrcspn (c
->definition
, "( \n");
2632 h
= cpp_lookup (pfile
, c
->definition
, namelen
);
2633 dn
= c
->definition
+ namelen
;
2635 nbuf
= cpp_push_buffer (pfile
, dn
, ustrchr (dn
, '\n') - dn
, true);
2638 _cpp_clean_line (pfile
);
2640 if (!_cpp_create_definition (pfile
, h
))
2642 _cpp_pop_buffer (pfile
);
2646 h
->value
.macro
->line
= c
->line
;
2647 h
->value
.macro
->syshdr
= c
->syshdr
;
2648 h
->value
.macro
->used
= c
->used
;
2652 /* Process the string STR as if it appeared as the body of a #assert. */
2654 cpp_assert (cpp_reader
*pfile
, const char *str
)
2656 handle_assertion (pfile
, str
, T_ASSERT
);
2659 /* Process STR as if it appeared as the body of an #unassert. */
2661 cpp_unassert (cpp_reader
*pfile
, const char *str
)
2663 handle_assertion (pfile
, str
, T_UNASSERT
);
2666 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2668 handle_assertion (cpp_reader
*pfile
, const char *str
, int type
)
2670 size_t count
= strlen (str
);
2671 const char *p
= strchr (str
, '=');
2673 /* Copy the entire option so we can modify it. Change the first
2674 "=" in the string to a '(', and tack a ')' on the end. */
2675 char *buf
= (char *) alloca (count
+ 2);
2677 memcpy (buf
, str
, count
);
2686 run_directive (pfile
, type
, str
, count
);
2689 /* The options structure. */
2691 cpp_get_options (cpp_reader
*pfile
)
2693 return &pfile
->opts
;
2696 /* The callbacks structure. */
2698 cpp_get_callbacks (cpp_reader
*pfile
)
2703 /* Copy the given callbacks structure to our own. */
2705 cpp_set_callbacks (cpp_reader
*pfile
, cpp_callbacks
*cb
)
2710 /* The narrow character set identifier. */
2712 cpp_get_narrow_charset_name (cpp_reader
*pfile
)
2714 return pfile
->narrow_cset_desc
.to
;
2717 /* The wide character set identifier. */
2719 cpp_get_wide_charset_name (cpp_reader
*pfile
)
2721 return pfile
->wide_cset_desc
.to
;
2724 /* The dependencies structure. (Creates one if it hasn't already been.) */
2726 cpp_get_deps (cpp_reader
*pfile
)
2728 if (!pfile
->deps
&& CPP_OPTION (pfile
, deps
.style
) != DEPS_NONE
)
2729 pfile
->deps
= deps_init ();
2733 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2734 doesn't fail. It does not generate a file change call back; that
2735 is the responsibility of the caller. */
2737 cpp_push_buffer (cpp_reader
*pfile
, const uchar
*buffer
, size_t len
,
2740 cpp_buffer
*new_buffer
= XOBNEW (&pfile
->buffer_ob
, cpp_buffer
);
2742 /* Clears, amongst other things, if_stack and mi_cmacro. */
2743 memset (new_buffer
, 0, sizeof (cpp_buffer
));
2745 new_buffer
->next_line
= new_buffer
->buf
= buffer
;
2746 new_buffer
->rlimit
= buffer
+ len
;
2747 new_buffer
->from_stage3
= from_stage3
;
2748 new_buffer
->prev
= pfile
->buffer
;
2749 new_buffer
->need_line
= true;
2751 pfile
->buffer
= new_buffer
;
2756 /* Pops a single buffer, with a file change call-back if appropriate.
2757 Then pushes the next -include file, if any remain. */
2759 _cpp_pop_buffer (cpp_reader
*pfile
)
2761 cpp_buffer
*buffer
= pfile
->buffer
;
2762 struct _cpp_file
*inc
= buffer
->file
;
2763 struct if_stack
*ifs
;
2764 const unsigned char *to_free
;
2766 /* Walk back up the conditional stack till we reach its level at
2767 entry to this file, issuing error messages. */
2768 for (ifs
= buffer
->if_stack
; ifs
; ifs
= ifs
->next
)
2769 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2770 "unterminated #%s", dtable
[ifs
->type
].name
);
2772 /* In case of a missing #endif. */
2773 pfile
->state
.skipping
= 0;
2775 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2776 pfile
->buffer
= buffer
->prev
;
2778 to_free
= buffer
->to_free
;
2779 free (buffer
->notes
);
2781 /* Free the buffer object now; we may want to push a new buffer
2782 in _cpp_push_next_include_file. */
2783 obstack_free (&pfile
->buffer_ob
, buffer
);
2787 _cpp_pop_file_buffer (pfile
, inc
, to_free
);
2789 _cpp_do_file_change (pfile
, LC_LEAVE
, 0, 0, 0);
2792 free ((void *)to_free
);
2795 /* Enter all recognized directives in the hash table. */
2797 _cpp_init_directives (cpp_reader
*pfile
)
2799 for (int i
= 0; i
< N_DIRECTIVES
; i
++)
2801 cpp_hashnode
*node
= cpp_lookup (pfile
, dtable
[i
].name
, dtable
[i
].length
);
2802 node
->is_directive
= 1;
2803 node
->directive_index
= i
;
2807 /* Extract header file from a bracket include. Parsing starts after '<'.
2808 The string is malloced and must be freed by the caller. */
2810 _cpp_bracket_include(cpp_reader
*pfile
)
2812 return glue_header_name (pfile
);