1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986-2025 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 location_t def_loc
; /* Locus of the following #define if any. */
35 const cpp_hashnode
*mi_cmacro
;/* Macro name for #ifndef around entire
37 const cpp_hashnode
*mi_def_cmacro
; /* Macro name in the following
39 bool skip_elses
; /* Can future #else / #elif be skipped? */
40 bool was_skipping
; /* If were skipping on entry. */
41 int type
; /* Most recent conditional for diagnostics. */
44 /* Contains a registered pragma or pragma namespace. */
45 typedef void (*pragma_cb
) (cpp_reader
*);
48 struct pragma_entry
*next
;
49 const cpp_hashnode
*pragma
; /* Name and length. */
56 struct pragma_entry
*space
;
61 /* Values for the origin field of struct directive. KANDR directives
62 come from traditional (K&R) C. STDC89 directives come from the
63 1989 C standard. STDC23 directives come from the C23 standard. EXTENSION
64 directives are extensions. */
70 /* Values for the flags field of struct directive. COND indicates a
71 conditional; IF_COND an opening conditional. INCL means to treat
72 "..." and <...> as q-char and h-char sequences respectively. IN_I
73 means this directive should be handled even if -fpreprocessed is in
74 effect (these are the directives with callback hooks).
76 EXPAND is set on directives that are always macro-expanded.
78 ELIFDEF is set on directives that are only handled for standards with the
79 #elifdef / #elifndef feature. */
81 #define IF_COND (1 << 1)
84 #define EXPAND (1 << 4)
85 #define DEPRECATED (1 << 5)
86 #define ELIFDEF (1 << 6)
88 /* Defines one #-directive, including how to handle it. */
89 typedef void (*directive_handler
) (cpp_reader
*);
90 typedef struct directive directive
;
93 directive_handler handler
; /* Function to handle directive. */
94 const uchar
*name
; /* Name of directive. */
95 unsigned short length
; /* Length of name. */
96 unsigned char origin
; /* Origin of directive. */
97 unsigned char flags
; /* Flags describing this directive. */
100 /* Forward declarations. */
102 static void skip_rest_of_line (cpp_reader
*);
103 static void check_eol (cpp_reader
*, bool);
104 static void start_directive (cpp_reader
*);
105 static void prepare_directive_trad (cpp_reader
*);
106 static void end_directive (cpp_reader
*, int);
107 static void directive_diagnostics (cpp_reader
*, const directive
*, int);
108 static void run_directive (cpp_reader
*, int, const char *, size_t);
109 static char *glue_header_name (cpp_reader
*);
110 static const char *parse_include (cpp_reader
*, int *, const cpp_token
***,
112 static void push_conditional (cpp_reader
*, int, int, const cpp_hashnode
*);
113 static unsigned int read_flag (cpp_reader
*, unsigned int);
114 static bool strtolinenum (const uchar
*, size_t, linenum_type
*, bool *);
115 static void do_diagnostic (cpp_reader
*, enum cpp_diagnostic_level code
,
116 enum cpp_warning_reason reason
, int);
117 static cpp_hashnode
*lex_macro_node (cpp_reader
*, bool);
118 static int undefine_macros (cpp_reader
*, cpp_hashnode
*, void *);
119 static void do_include_common (cpp_reader
*, enum include_type
);
120 static struct pragma_entry
*lookup_pragma_entry (struct pragma_entry
*,
121 const cpp_hashnode
*);
122 static int count_registered_pragmas (struct pragma_entry
*);
123 static char ** save_registered_pragmas (struct pragma_entry
*, char **);
124 static char ** restore_registered_pragmas (cpp_reader
*, struct pragma_entry
*,
126 static void do_pragma_once (cpp_reader
*);
127 static void do_pragma_poison (cpp_reader
*);
128 static void do_pragma_system_header (cpp_reader
*);
129 static void do_pragma_dependency (cpp_reader
*);
130 static void do_pragma_warning_or_error (cpp_reader
*, bool error
);
131 static void do_pragma_warning (cpp_reader
*);
132 static void do_pragma_error (cpp_reader
*);
133 static void do_linemarker (cpp_reader
*);
134 static const cpp_token
*get_token_no_padding (cpp_reader
*);
135 static const cpp_token
*get__Pragma_string (cpp_reader
*);
136 static void destringize_and_run (cpp_reader
*, const cpp_string
*,
138 static bool parse_answer (cpp_reader
*, int, location_t
, cpp_macro
**);
139 static cpp_hashnode
*parse_assertion (cpp_reader
*, int, cpp_macro
**);
140 static cpp_macro
**find_answer (cpp_hashnode
*, const cpp_macro
*);
141 static void handle_assertion (cpp_reader
*, const char *, int);
142 static void do_pragma_push_macro (cpp_reader
*);
143 static void do_pragma_pop_macro (cpp_reader
*);
144 static void cpp_pop_definition (cpp_reader
*, def_pragma_macro
*,
147 /* This is the table of directive handlers. All extensions other than
148 #warning, #include_next, and #import are deprecated. The name is
149 where the extension appears to have come from. */
151 #define DIRECTIVE_TABLE \
152 D(define, T_DEFINE = 0, KANDR, IN_I | IF_COND) \
153 D(include, T_INCLUDE, KANDR, INCL | EXPAND) \
154 D(endif, T_ENDIF, KANDR, COND) \
155 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) \
156 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) \
157 D(else, T_ELSE, KANDR, COND) \
158 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) \
159 D(undef, T_UNDEF, KANDR, IN_I) \
160 D(line, T_LINE, KANDR, EXPAND) \
161 D(elif, T_ELIF, STDC89, COND | EXPAND) \
162 D(elifdef, T_ELIFDEF, STDC23, COND | ELIFDEF) \
163 D(elifndef, T_ELIFNDEF, STDC23, COND | ELIFDEF) \
164 D(error, T_ERROR, STDC89, 0) \
165 D(pragma, T_PRAGMA, STDC89, IN_I) \
166 D(warning, T_WARNING, STDC23, 0) \
167 D(embed, T_EMBED, STDC23, IN_I | INCL | EXPAND) \
168 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) \
169 D(ident, T_IDENT, EXTENSION, IN_I) \
170 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* ObjC */ \
171 D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* SVR4 */ \
172 D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* SVR4 */ \
173 D(sccs, T_SCCS, EXTENSION, IN_I) /* SVR4? */
175 /* #sccs is synonymous with #ident. */
176 #define do_sccs do_ident
178 /* Use the table to generate a series of prototypes, an enum for the
179 directive names, and an array of directive handlers. */
181 #define D(name, t, o, f) static void do_##name (cpp_reader *);
185 #define D(n, tag, o, f) tag,
193 #define D(name, t, origin, flags) \
194 { do_##name, (const uchar *) #name, \
195 sizeof #name - 1, origin, flags },
196 static const directive dtable
[] =
202 /* A NULL-terminated array of directive names for use
203 when suggesting corrections for misspelled directives. */
204 #define D(name, t, origin, flags) #name,
205 static const char * const directive_names
[] = {
211 #undef DIRECTIVE_TABLE
213 /* Wrapper struct directive for linemarkers.
214 The origin is more or less true - the original K+R cpp
215 did use this notation in its preprocessed output. */
216 static const directive linemarker_dir
=
218 do_linemarker
, UC
"#", 1, KANDR
, IN_I
221 /* Skip any remaining tokens in a directive. */
223 skip_rest_of_line (cpp_reader
*pfile
)
225 /* Discard all stacked contexts. */
226 while (pfile
->context
->prev
)
227 _cpp_pop_context (pfile
);
229 /* Sweep up all tokens remaining on the line. */
231 while (_cpp_lex_token (pfile
)->type
!= CPP_EOF
)
235 /* Helper function for check_oel. */
238 check_eol_1 (cpp_reader
*pfile
, bool expand
, enum cpp_warning_reason reason
)
240 if (! SEEN_EOL () && (expand
241 ? cpp_get_token (pfile
)
242 : _cpp_lex_token (pfile
))->type
!= CPP_EOF
)
243 cpp_pedwarning (pfile
, reason
, "extra tokens at end of %<#%s%> directive",
244 pfile
->directive
->name
);
247 /* Variant of check_eol used for Wendif-labels warnings. */
250 check_eol_endif_labels (cpp_reader
*pfile
)
252 check_eol_1 (pfile
, false, CPP_W_ENDIF_LABELS
);
255 /* Ensure there are no stray tokens at the end of a directive. If
256 EXPAND is true, tokens macro-expanding to nothing are allowed. */
259 check_eol (cpp_reader
*pfile
, bool expand
)
261 check_eol_1 (pfile
, expand
, CPP_W_NONE
);
264 /* Ensure there are no stray tokens other than comments at the end of
265 a directive, and gather the comments. */
266 static const cpp_token
**
267 check_eol_return_comments (cpp_reader
*pfile
)
271 const cpp_token
**buf
;
273 buf
= XNEWVEC (const cpp_token
*, capacity
);
279 const cpp_token
*tok
;
281 tok
= _cpp_lex_token (pfile
);
282 if (tok
->type
== CPP_EOF
)
284 if (tok
->type
!= CPP_COMMENT
)
285 cpp_error (pfile
, CPP_DL_PEDWARN
,
286 "extra tokens at end of #%s directive",
287 pfile
->directive
->name
);
290 if (c
+ 1 >= capacity
)
293 buf
= XRESIZEVEC (const cpp_token
*, buf
, capacity
);
304 /* Called when entering a directive, _Pragma or command-line directive. */
306 start_directive (cpp_reader
*pfile
)
308 /* Setup in-directive state. */
309 pfile
->state
.in_directive
= 1;
310 pfile
->state
.save_comments
= 0;
311 pfile
->directive_result
.type
= CPP_PADDING
;
313 /* Some handlers need the position of the # for diagnostics. */
314 pfile
->directive_line
= pfile
->line_table
->highest_line
;
317 /* Called when leaving a directive, _Pragma or command-line directive. */
319 end_directive (cpp_reader
*pfile
, int skip_line
)
321 if (CPP_OPTION (pfile
, traditional
))
323 /* Revert change of prepare_directive_trad. */
324 if (!pfile
->state
.in_deferred_pragma
)
325 pfile
->state
.prevent_expansion
--;
327 if (pfile
->directive
!= &dtable
[T_DEFINE
])
328 _cpp_remove_overlay (pfile
);
330 else if (pfile
->state
.in_deferred_pragma
)
332 /* We don't skip for an assembler #. */
335 if (pfile
->directive
!= &dtable
[T_EMBED
])
336 skip_rest_of_line (pfile
);
337 if (!pfile
->keep_tokens
)
339 pfile
->cur_run
= &pfile
->base_run
;
340 pfile
->cur_token
= pfile
->base_run
.base
;
345 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
346 pfile
->state
.in_directive
= 0;
347 pfile
->state
.in_expression
= 0;
348 pfile
->state
.angled_headers
= 0;
349 pfile
->directive
= 0;
352 /* Prepare to handle the directive in pfile->directive. */
354 prepare_directive_trad (cpp_reader
*pfile
)
356 if (pfile
->directive
!= &dtable
[T_DEFINE
])
358 bool no_expand
= (pfile
->directive
359 && ! (pfile
->directive
->flags
& EXPAND
));
360 bool was_skipping
= pfile
->state
.skipping
;
362 pfile
->state
.in_expression
= (pfile
->directive
== &dtable
[T_IF
]
363 || pfile
->directive
== &dtable
[T_ELIF
]);
364 if (pfile
->state
.in_expression
)
365 pfile
->state
.skipping
= false;
368 pfile
->state
.prevent_expansion
++;
369 _cpp_scan_out_logical_line (pfile
, NULL
, false);
371 pfile
->state
.prevent_expansion
--;
373 pfile
->state
.skipping
= was_skipping
;
374 _cpp_overlay_buffer (pfile
, pfile
->out
.base
,
375 pfile
->out
.cur
- pfile
->out
.base
);
378 /* Stop ISO C from expanding anything. */
379 pfile
->state
.prevent_expansion
++;
382 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
383 the '#' was indented. */
385 directive_diagnostics (cpp_reader
*pfile
, const directive
*dir
, int indented
)
387 /* Issue -pedantic or deprecated warnings for extensions. We let
388 -pedantic take precedence if both are applicable. */
389 if (! pfile
->state
.skipping
)
392 if (dir
->origin
== EXTENSION
393 && !(dir
== &dtable
[T_IMPORT
] && CPP_OPTION (pfile
, objc
)))
395 = cpp_pedwarning (pfile
, CPP_W_PEDANTIC
,
396 "%<#%s%> is a GCC extension", dir
->name
);
397 if (!warned
&& dir
== &dtable
[T_WARNING
])
399 if (CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, warning_directive
))
401 if (CPP_OPTION (pfile
, cplusplus
))
403 = cpp_pedwarning (pfile
, CPP_W_CXX23_EXTENSIONS
,
404 "%<#%s%> before C++23 is a GCC extension",
408 = cpp_pedwarning (pfile
, CPP_W_PEDANTIC
,
409 "%<#%s%> before C23 is a GCC extension",
413 if (!warned
&& CPP_OPTION (pfile
, cpp_warn_c11_c23_compat
) > 0)
414 warned
= cpp_warning (pfile
, CPP_W_C11_C23_COMPAT
,
415 "%<#%s%> before C23 is a GCC extension",
419 if (((dir
->flags
& DEPRECATED
) != 0
420 || (dir
== &dtable
[T_IMPORT
] && !CPP_OPTION (pfile
, objc
)))
422 cpp_warning (pfile
, CPP_W_DEPRECATED
,
423 "%<#%s%> is a deprecated GCC extension", dir
->name
);
426 /* Traditionally, a directive is ignored unless its # is in
427 column 1. Therefore in code intended to work with K+R
428 compilers, directives added by C89 must have their #
429 indented, and directives present in traditional C must not.
430 This is true even of directives in skipped conditional
431 blocks. #elif cannot be used at all. */
432 if (CPP_WTRADITIONAL (pfile
))
434 if (dir
== &dtable
[T_ELIF
])
435 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
436 "suggest not using %<#elif%> in traditional C");
437 else if (indented
&& dir
->origin
== KANDR
)
438 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
439 "traditional C ignores %<#%s%> with the %<#%> indented",
441 else if (!indented
&& dir
->origin
!= KANDR
)
442 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
443 "suggest hiding %<#%s%> from traditional C with an "
444 "indented %<#%>", dir
->name
);
448 /* Check if we have a known directive. INDENTED is true if the
449 '#' of the directive was indented. This function is in this file
450 to save unnecessarily exporting dtable etc. to lex.cc. Returns
451 nonzero if the line of tokens has been handled, zero if we should
452 continue processing the line. */
454 _cpp_handle_directive (cpp_reader
*pfile
, bool indented
)
456 const directive
*dir
= 0;
457 const cpp_token
*dname
;
458 bool was_parsing_args
= pfile
->state
.parsing_args
;
459 bool was_discarding_output
= pfile
->state
.discarding_output
;
462 if (was_discarding_output
)
463 pfile
->state
.prevent_expansion
= 0;
465 if (was_parsing_args
)
467 cpp_pedwarning (pfile
, CPP_W_PEDANTIC
,
468 "embedding a directive within macro arguments is not "
470 pfile
->state
.parsing_args
= 0;
471 pfile
->state
.prevent_expansion
= 0;
473 start_directive (pfile
);
474 dname
= _cpp_lex_token (pfile
);
476 if (dname
->type
== CPP_NAME
)
478 if (dname
->val
.node
.node
->is_directive
)
480 dir
= &dtable
[dname
->val
.node
.node
->directive_index
];
481 if ((dir
->flags
& ELIFDEF
)
482 && !CPP_OPTION (pfile
, elifdef
)
483 /* For -std=gnu* modes elifdef is supported with
484 a pedwarn if pedantic. */
485 && CPP_OPTION (pfile
, std
))
489 /* We do not recognize the # followed by a number extension in
491 else if (dname
->type
== CPP_NUMBER
&& CPP_OPTION (pfile
, lang
) != CLK_ASM
)
493 dir
= &linemarker_dir
;
494 if (! CPP_OPTION (pfile
, preprocessed
)
495 && ! pfile
->state
.skipping
)
496 cpp_pedwarning (pfile
, CPP_W_PEDANTIC
,
497 "style of line directive is a GCC extension");
502 /* If we have a directive that is not an opening conditional,
503 invalidate any control macro. */
504 if (! (dir
->flags
& IF_COND
))
505 pfile
->mi_valid
= false;
507 /* Kluge alert. In order to be sure that code like this
512 does not cause '#define foo bar' to get executed when
513 compiled with -save-temps, we recognize directives in
514 -fpreprocessed mode only if the # is in column 1. macro.cc
515 puts a space in front of any '#' at the start of a macro.
517 We exclude the -fdirectives-only case because macro expansion
518 has not been performed yet, and block comments can cause spaces
519 to precede the directive. */
520 if (CPP_OPTION (pfile
, preprocessed
)
521 && !CPP_OPTION (pfile
, directives_only
)
522 && (indented
|| !(dir
->flags
& IN_I
)))
529 /* In failed conditional groups, all non-conditional
530 directives are ignored. Before doing that, whether
531 skipping or not, we should lex angle-bracketed headers
532 correctly, and maybe output some diagnostics. */
533 pfile
->state
.angled_headers
= dir
->flags
& INCL
;
534 pfile
->state
.directive_wants_padding
= dir
->flags
& INCL
;
535 if (! CPP_OPTION (pfile
, preprocessed
))
536 directive_diagnostics (pfile
, dir
, indented
);
537 if (pfile
->state
.skipping
&& !(dir
->flags
& COND
))
541 else if (dname
->type
== CPP_EOF
)
542 ; /* CPP_EOF is the "null directive". */
545 /* An unknown directive. Don't complain about it in assembly
546 source: we don't know where the comments are, and # may
547 introduce assembler pseudo-ops. Don't complain about invalid
548 directives in skipped conditional groups (6.10 p4). */
549 if (CPP_OPTION (pfile
, lang
) == CLK_ASM
)
551 else if (!pfile
->state
.skipping
)
553 const char *unrecognized
554 = (const char *)cpp_token_as_text (pfile
, dname
);
555 const char *hint
= NULL
;
557 /* Call back into gcc to get a spelling suggestion. Ideally
558 we'd just use best_match from gcc/spellcheck.h (and filter
559 out the uncommon directives), but that requires moving it
560 to a support library. */
561 if (pfile
->cb
.get_suggestion
)
562 hint
= pfile
->cb
.get_suggestion (pfile
, unrecognized
,
567 rich_location
richloc (pfile
->line_table
, dname
->src_loc
);
568 source_range misspelled_token_range
569 = get_range_from_loc (pfile
->line_table
, dname
->src_loc
);
570 richloc
.add_fixit_replace (misspelled_token_range
, hint
);
571 cpp_error_at (pfile
, CPP_DL_ERROR
, &richloc
,
572 "invalid preprocessing directive #%s;"
573 " did you mean #%s?",
577 cpp_error (pfile
, CPP_DL_ERROR
,
578 "invalid preprocessing directive #%s",
583 pfile
->directive
= dir
;
584 if (CPP_OPTION (pfile
, traditional
))
585 prepare_directive_trad (pfile
);
589 pfile
->directive
->handler (pfile
);
590 if (pfile
->directive
== &dtable
[T_EMBED
]
592 && CPP_OPTION (pfile
, directives_only
))
593 /* Signal to cpp_directive_only_process it needs to emit
594 the #embed expansion. */
598 _cpp_backup_tokens (pfile
, 1);
600 end_directive (pfile
, skip
);
601 if (was_parsing_args
&& !pfile
->state
.in_deferred_pragma
)
603 /* Restore state when within macro args. */
604 pfile
->state
.parsing_args
= 2;
605 pfile
->state
.prevent_expansion
= 1;
607 if (was_discarding_output
)
608 pfile
->state
.prevent_expansion
= 1;
612 /* Directive handler wrapper used by the command line option
613 processor. BUF is \n terminated. */
615 run_directive (cpp_reader
*pfile
, int dir_no
, const char *buf
, size_t count
)
617 cpp_push_buffer (pfile
, (const uchar
*) buf
, count
,
618 /* from_stage3 */ true);
619 start_directive (pfile
);
621 /* This is a short-term fix to prevent a leading '#' being
622 interpreted as a directive. */
623 _cpp_clean_line (pfile
);
625 pfile
->directive
= &dtable
[dir_no
];
626 if (CPP_OPTION (pfile
, traditional
))
627 prepare_directive_trad (pfile
);
628 pfile
->directive
->handler (pfile
);
629 end_directive (pfile
, 1);
630 _cpp_pop_buffer (pfile
);
633 /* Checks for validity the macro name in #define, #undef, #ifdef and
634 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
635 processing a #define or #undefine directive, and false
637 static cpp_hashnode
*
638 lex_macro_node (cpp_reader
*pfile
, bool is_def_or_undef
)
640 const cpp_token
*token
= _cpp_lex_token (pfile
);
642 /* The token immediately after #define must be an identifier. That
643 identifier may not be "defined", per C99 6.10.8p4.
644 In C++, it may not be any of the "named operators" either,
645 per C++98 [lex.digraph], [lex.key].
646 Finally, the identifier may not have been poisoned. (In that case
647 the lexer has issued the error message for us.) */
649 if (token
->type
== CPP_NAME
)
651 cpp_hashnode
*node
= token
->val
.node
.node
;
654 && node
== pfile
->spec_nodes
.n_defined
)
655 cpp_error (pfile
, CPP_DL_ERROR
,
656 "%qs cannot be used as a macro name",
658 else if (! (node
->flags
& NODE_POISONED
))
661 else if (token
->flags
& NAMED_OP
)
662 cpp_error (pfile
, CPP_DL_ERROR
,
663 "%qs cannot be used as a macro name as it is an operator "
664 "in C++", NODE_NAME (token
->val
.node
.node
));
665 else if (token
->type
== CPP_EOF
)
666 cpp_error (pfile
, CPP_DL_ERROR
, "no macro name given in %<#%s%> directive",
667 pfile
->directive
->name
);
669 cpp_error (pfile
, CPP_DL_ERROR
, "macro names must be identifiers");
674 /* Process a #define directive. Most work is done in macro.cc. */
676 do_define (cpp_reader
*pfile
)
678 cpp_hashnode
*node
= lex_macro_node (pfile
, true);
682 /* This is a better location than pfile->directive_line to store
683 as the macro location. */
684 const location_t name_loc
= cpp_diagnostic_get_current_location (pfile
);
686 /* If we have been requested to expand comments into macros,
687 then re-enable saving of comments. */
688 pfile
->state
.save_comments
689 = ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
);
691 if (pfile
->cb
.before_define
)
692 pfile
->cb
.before_define (pfile
);
694 if (_cpp_create_definition (pfile
, node
, name_loc
))
695 if (pfile
->cb
.define
)
696 pfile
->cb
.define (pfile
, pfile
->directive_line
, node
);
698 node
->flags
&= ~NODE_USED
;
702 && CPP_OPTION (pfile
, warn_header_guard
)
703 && node
->type
== NT_USER_MACRO
705 && node
->value
.macro
->count
== 0
706 && !node
->value
.macro
->fun_like
)
708 cpp_buffer
*buffer
= pfile
->buffer
;
709 struct if_stack
*ifs
= buffer
->if_stack
;
713 && node
!= ifs
->mi_cmacro
)
715 ifs
->mi_def_cmacro
= node
;
716 ifs
->def_loc
= pfile
->directive_line
;
720 pfile
->mi_valid
= false;
723 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
725 do_undef (cpp_reader
*pfile
)
727 cpp_hashnode
*node
= lex_macro_node (pfile
, true);
731 if (pfile
->cb
.before_define
)
732 pfile
->cb
.before_define (pfile
);
735 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
737 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
738 identifier is not currently defined as a macro name. */
739 if (cpp_macro_p (node
))
741 if (node
->flags
& NODE_WARN
)
742 cpp_error (pfile
, CPP_DL_WARNING
,
743 "undefining %qs", NODE_NAME (node
));
744 else if (cpp_builtin_macro_p (node
)
745 && CPP_OPTION (pfile
, warn_builtin_macro_redefined
))
746 cpp_warning (pfile
, CPP_W_BUILTIN_MACRO_REDEFINED
,
747 "undefining %qs", NODE_NAME (node
));
749 if (node
->value
.macro
750 && CPP_OPTION (pfile
, warn_unused_macros
))
751 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
753 _cpp_free_definition (node
);
757 check_eol (pfile
, false);
760 /* Undefine a single macro/assertion/whatever. */
763 undefine_macros (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*h
,
764 void *data_p ATTRIBUTE_UNUSED
)
766 /* Body of _cpp_free_definition inlined here for speed.
767 Macros and assertions no longer have anything to free. */
769 h
->value
.answers
= NULL
;
770 h
->flags
&= ~(NODE_POISONED
|NODE_DISABLED
|NODE_USED
);
774 /* Undefine all macros and assertions. */
777 cpp_undef_all (cpp_reader
*pfile
)
779 cpp_forall_identifiers (pfile
, undefine_macros
, NULL
);
783 /* Helper routine used by parse_include. Reinterpret the current line
784 as an h-char-sequence (< ... >); we are looking at the first token
785 after the <. Returns a malloced filename. */
787 glue_header_name (cpp_reader
*pfile
)
789 const cpp_token
*token
;
791 size_t len
, total_len
= 0, capacity
= 1024;
793 /* To avoid lexed tokens overwriting our glued name, we can only
794 allocate from the string pool once we've lexed everything. */
795 buffer
= XNEWVEC (char, capacity
);
798 token
= get_token_no_padding (pfile
);
800 if (token
->type
== CPP_GREATER
)
802 if (token
->type
== CPP_EOF
)
804 cpp_error (pfile
, CPP_DL_ERROR
,
805 "missing terminating %<>%> character");
809 len
= cpp_token_len (token
) + 2; /* Leading space, terminating \0. */
810 if (total_len
+ len
> capacity
)
812 capacity
= (capacity
+ len
) * 2;
813 buffer
= XRESIZEVEC (char, buffer
, capacity
);
816 if (token
->flags
& PREV_WHITE
)
817 buffer
[total_len
++] = ' ';
819 total_len
= (cpp_spell_token (pfile
, token
, (uchar
*) &buffer
[total_len
],
824 buffer
[total_len
] = '\0';
828 /* Returns the file name of #include, #include_next, #import and
829 #pragma dependency. The string is malloced and the caller should
830 free it. Returns NULL on error. LOCATION is the source location
834 parse_include (cpp_reader
*pfile
, int *pangle_brackets
,
835 const cpp_token
***buf
, location_t
*location
)
838 const cpp_token
*header
;
840 /* Allow macro expansion. */
841 header
= get_token_no_padding (pfile
);
842 *location
= header
->src_loc
;
843 if ((header
->type
== CPP_STRING
&& header
->val
.str
.text
[0] != 'R')
844 || header
->type
== CPP_HEADER_NAME
)
846 fname
= XNEWVEC (char, header
->val
.str
.len
- 1);
847 memcpy (fname
, header
->val
.str
.text
+ 1, header
->val
.str
.len
- 2);
848 fname
[header
->val
.str
.len
- 2] = '\0';
849 *pangle_brackets
= header
->type
== CPP_HEADER_NAME
;
851 else if (header
->type
== CPP_LESS
)
853 fname
= glue_header_name (pfile
);
854 *pangle_brackets
= 1;
858 const unsigned char *dir
;
860 if (pfile
->directive
== &dtable
[T_PRAGMA
])
861 dir
= UC
"pragma GCC dependency";
863 dir
= pfile
->directive
->name
;
864 cpp_error (pfile
, CPP_DL_ERROR
,
865 "%<#%s%> expects %<\"FILENAME\"%> or %<<FILENAME>%>", dir
);
870 if (pfile
->directive
== &dtable
[T_PRAGMA
]
871 || pfile
->directive
== &dtable
[T_EMBED
])
873 /* This pragma or #embed allows extra tokens after the file name. */
875 else if (buf
== NULL
|| CPP_OPTION (pfile
, discard_comments
))
876 check_eol (pfile
, true);
879 /* If we are not discarding comments, then gather them while
880 doing the eol check. */
881 *buf
= check_eol_return_comments (pfile
);
887 /* Handle #include, #include_next and #import. */
889 do_include_common (cpp_reader
*pfile
, enum include_type type
)
893 const cpp_token
**buf
= NULL
;
896 /* Re-enable saving of comments if requested, so that the include
897 callback can dump comments which follow #include. */
898 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
900 /* Tell the lexer this is an include directive -- we want it to
901 increment the line number even if this is the last line of a file. */
902 pfile
->state
.in_directive
= 2;
904 fname
= parse_include (pfile
, &angle_brackets
, &buf
, &location
);
910 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
911 "empty filename in #%s",
912 pfile
->directive
->name
);
916 /* Prevent #include recursion. */
917 if (pfile
->line_table
->depth
>= CPP_OPTION (pfile
, max_include_depth
))
920 "%<#include%> nested depth %u exceeds maximum of %u"
921 " (use %<-fmax-include-depth=DEPTH%> to increase the maximum)",
922 pfile
->line_table
->depth
,
923 CPP_OPTION (pfile
, max_include_depth
));
926 /* Get out of macro context, if we are. */
927 skip_rest_of_line (pfile
);
929 if (pfile
->cb
.include
)
930 pfile
->cb
.include (pfile
, pfile
->directive_line
,
931 pfile
->directive
->name
, fname
, angle_brackets
,
934 _cpp_stack_include (pfile
, fname
, angle_brackets
, type
, location
);
944 do_include (cpp_reader
*pfile
)
946 do_include_common (pfile
, IT_INCLUDE
);
950 do_import (cpp_reader
*pfile
)
952 do_include_common (pfile
, IT_IMPORT
);
956 do_include_next (cpp_reader
*pfile
)
958 enum include_type type
= IT_INCLUDE_NEXT
;
960 /* If this is the primary source file, warn and use the normal
962 if (_cpp_in_main_source_file (pfile
))
964 cpp_error (pfile
, CPP_DL_WARNING
,
965 "%<#include_next%> in primary source file");
968 do_include_common (pfile
, type
);
971 /* Helper function for skip_balanced_token_seq and _cpp_parse_embed_params.
972 Save one token *TOKEN into *SAVE. */
975 save_token_for_embed (cpp_embed_params_tokens
*save
, const cpp_token
*token
)
977 if (save
->count
== 0)
979 _cpp_init_tokenrun (&save
->base_run
, 4);
980 save
->cur_run
= &save
->base_run
;
981 save
->cur_token
= save
->base_run
.base
;
983 else if (save
->cur_token
== save
->cur_run
->limit
)
985 save
->cur_run
->next
= XNEW (tokenrun
);
986 save
->cur_run
->next
->prev
= save
->cur_run
;
987 _cpp_init_tokenrun (save
->cur_run
->next
, 4);
988 save
->cur_run
= save
->cur_run
->next
;
989 save
->cur_token
= save
->cur_run
->base
;
991 *save
->cur_token
= *token
;
992 save
->cur_token
->flags
|= NO_EXPAND
;
997 /* Free memory associated with saved tokens in *SAVE. */
1000 _cpp_free_embed_params_tokens (cpp_embed_params_tokens
*save
)
1002 if (save
->count
== 0)
1005 for (tokenrun
*t
= &save
->base_run
; t
; t
= n
)
1008 XDELETEVEC (t
->base
);
1009 if (t
!= &save
->base_run
)
1015 /* Skip over balanced preprocessing tokens until END is found.
1016 If SAVE is non-NULL, remember the parsed tokens in it. NESTED is
1017 false in the outermost invocation of the function and true
1018 when called recursively. */
1021 skip_balanced_token_seq (cpp_reader
*pfile
, cpp_ttype end
,
1022 cpp_embed_params_tokens
*save
, bool nested
)
1026 const cpp_token
*token
= cpp_peek_token (pfile
, 0);
1027 if (token
->type
== CPP_EOF
)
1032 case CPP_CLOSE_PAREN
: c
= '('; break;
1033 case CPP_CLOSE_SQUARE
: c
= '['; break;
1034 case CPP_CLOSE_BRACE
: c
= '{'; break;
1037 cpp_error (pfile
, CPP_DL_ERROR
, "unbalanced '%c'", c
);
1040 token
= cpp_get_token (pfile
);
1042 && (token
->type
!= CPP_PADDING
|| save
->count
)
1043 && (token
->type
!= end
|| nested
))
1044 save_token_for_embed (save
, token
);
1045 if (token
->type
== end
)
1047 switch (token
->type
)
1049 case CPP_OPEN_PAREN
:
1050 skip_balanced_token_seq (pfile
, CPP_CLOSE_PAREN
, save
, true);
1052 case CPP_OPEN_SQUARE
:
1053 skip_balanced_token_seq (pfile
, CPP_CLOSE_SQUARE
, save
, true);
1055 case CPP_OPEN_BRACE
:
1056 skip_balanced_token_seq (pfile
, CPP_CLOSE_BRACE
, save
, true);
1058 case CPP_CLOSE_PAREN
:
1059 cpp_error (pfile
, CPP_DL_ERROR
, "unbalanced '%c'", ')');
1061 case CPP_CLOSE_SQUARE
:
1062 cpp_error (pfile
, CPP_DL_ERROR
, "unbalanced '%c'", ']');
1064 case CPP_CLOSE_BRACE
:
1065 cpp_error (pfile
, CPP_DL_ERROR
, "unbalanced '%c'", '}');
1074 #define EMBED_PARAMS \
1075 EMBED_PARAM (LIMIT, "limit") \
1076 EMBED_PARAM (PREFIX, "prefix") \
1077 EMBED_PARAM (SUFFIX, "suffix") \
1078 EMBED_PARAM (IF_EMPTY, "if_empty") \
1079 EMBED_PARAM (GNU_BASE64, "base64") \
1080 EMBED_PARAM (GNU_OFFSET, "offset")
1082 enum embed_param_kind
{
1083 #define EMBED_PARAM(c, s) EMBED_PARAM_##c,
1087 NUM_EMBED_STD_PARAMS
= EMBED_PARAM_IF_EMPTY
+ 1
1090 static struct { int len
; const char *name
; } embed_params
[NUM_EMBED_PARAMS
] = {
1091 #define EMBED_PARAM(c, s) { sizeof (s) - 1, s },
1096 /* Parse parameters of #embed directive or __has_embed expression.
1097 Fills in details about parsed parameters in *PARAMS.
1098 Returns true if all the parameters have been successfully parsed,
1102 _cpp_parse_embed_params (cpp_reader
*pfile
, struct cpp_embed_params
*params
)
1104 const cpp_token
*token
= _cpp_get_token_no_padding (pfile
);
1110 const unsigned char *param_name
= NULL
;
1111 const unsigned char *param_prefix
= NULL
;
1112 int param_name_len
= 0, param_prefix_len
= 0;
1113 bool has_scope
= false;
1114 if (token
->type
!= CPP_NAME
)
1116 if (token
->type
== CPP_EOF
)
1118 if (params
->has_embed
)
1120 cpp_error (pfile
, CPP_DL_ERROR
, "expected %<)%>");
1124 else if (token
->type
!= CPP_CLOSE_PAREN
|| !params
->has_embed
)
1126 cpp_error (pfile
, CPP_DL_ERROR
, "expected parameter name");
1129 if (params
->base64
.count
1130 && (seen
& ((1 << EMBED_PARAM_LIMIT
)
1131 | (1 << EMBED_PARAM_GNU_OFFSET
))) != 0)
1134 if (!params
->has_embed
)
1135 cpp_error_with_line (pfile
, CPP_DL_ERROR
,
1136 params
->base64
.base_run
.base
->src_loc
, 0,
1137 "%<gnu::base64%> parameter conflicts "
1138 "with %<limit%> or %<gnu::offset%> "
1141 else if (params
->base64
.count
== 0
1142 && CPP_OPTION (pfile
, preprocessed
))
1145 if (!params
->has_embed
)
1146 cpp_error_with_line (pfile
, CPP_DL_ERROR
, params
->loc
, 0,
1147 "%<gnu::base64%> parameter required in "
1148 "preprocessed source");
1152 param_name
= NODE_NAME (token
->val
.node
.spelling
);
1153 param_name_len
= NODE_LEN (token
->val
.node
.spelling
);
1154 location_t loc
= token
->src_loc
;
1155 token
= _cpp_get_token_no_padding (pfile
);
1156 if (token
->type
== CPP_SCOPE
)
1159 token
= _cpp_get_token_no_padding (pfile
);
1161 else if (token
->type
== CPP_COLON
1162 && (token
->flags
& COLON_SCOPE
) != 0)
1165 token
= _cpp_get_token_no_padding (pfile
);
1166 if (token
->type
!= CPP_COLON
)
1168 cpp_error (pfile
, CPP_DL_ERROR
, "expected %<:%>");
1171 token
= _cpp_get_token_no_padding (pfile
);
1175 if (token
->type
!= CPP_NAME
)
1177 cpp_error (pfile
, CPP_DL_ERROR
, "expected parameter name");
1180 param_prefix
= param_name
;
1181 param_prefix_len
= param_name_len
;
1182 param_name
= NODE_NAME (token
->val
.node
.spelling
);
1183 param_name_len
= NODE_LEN (token
->val
.node
.spelling
);
1184 loc
= token
->src_loc
;
1185 token
= _cpp_get_token_no_padding (pfile
);
1187 if (param_name_len
> 4
1188 && param_name
[0] == '_'
1189 && param_name
[1] == '_'
1190 && param_name
[param_name_len
- 1] == '_'
1191 && param_name
[param_name_len
- 2] == '_')
1194 param_name_len
-= 4;
1197 && param_prefix_len
> 4
1198 && param_prefix
[0] == '_'
1199 && param_prefix
[1] == '_'
1200 && param_prefix
[param_prefix_len
- 1] == '_'
1201 && param_prefix
[param_prefix_len
- 2] == '_')
1204 param_prefix_len
-= 4;
1206 size_t param_kind
= -1;
1207 if (param_prefix
== NULL
)
1209 for (size_t i
= 0; i
< NUM_EMBED_STD_PARAMS
; ++i
)
1210 if (param_name_len
== embed_params
[i
].len
1211 && memcmp (param_name
, embed_params
[i
].name
,
1212 param_name_len
) == 0)
1218 else if (param_prefix_len
== 3 && memcmp (param_prefix
, "gnu", 3) == 0)
1220 for (size_t i
= NUM_EMBED_STD_PARAMS
; i
< NUM_EMBED_PARAMS
; ++i
)
1221 if (param_name_len
== embed_params
[i
].len
1222 && memcmp (param_name
, embed_params
[i
].name
,
1223 param_name_len
) == 0)
1229 if (param_kind
!= (size_t) -1)
1231 if ((seen
& (1 << param_kind
)) == 0)
1232 seen
|= 1 << param_kind
;
1234 cpp_error_with_line (pfile
, CPP_DL_ERROR
, loc
, 0,
1235 "duplicate embed parameter '%.*s%s%.*s'",
1238 ? (const char *) param_prefix
: "",
1239 param_prefix
? "::" : "",
1240 param_name_len
, param_name
);
1245 if (!params
->has_embed
)
1246 cpp_error_with_line (pfile
, CPP_DL_ERROR
, loc
, 0,
1247 "unknown embed parameter '%.*s%s%.*s'",
1250 ? (const char *) param_prefix
: "",
1251 param_prefix
? "::" : "",
1252 param_name_len
, param_name
);
1254 if (param_kind
!= (size_t) -1 && token
->type
!= CPP_OPEN_PAREN
)
1255 cpp_error_with_line (pfile
, CPP_DL_ERROR
, loc
, 0,
1257 else if (param_kind
== EMBED_PARAM_LIMIT
1258 || param_kind
== EMBED_PARAM_GNU_OFFSET
)
1260 if (params
->has_embed
&& pfile
->op_stack
== NULL
)
1261 _cpp_expand_op_stack (pfile
);
1262 cpp_num_part res
= _cpp_parse_expr (pfile
, "#embed", token
);
1263 if (param_kind
== EMBED_PARAM_LIMIT
)
1264 params
->limit
= res
;
1267 if (res
> INTTYPE_MAXIMUM (off_t
))
1268 cpp_error_with_line (pfile
, CPP_DL_ERROR
, loc
, 0,
1269 "too large %<gnu::offset%> argument");
1271 params
->offset
= res
;
1273 token
= _cpp_get_token_no_padding (pfile
);
1275 else if (param_kind
== EMBED_PARAM_GNU_BASE64
)
1277 token
= _cpp_get_token_no_padding (pfile
);
1278 while (token
->type
== CPP_OTHER
1279 && CPP_OPTION (pfile
, preprocessed
)
1280 && !CPP_OPTION (pfile
, directives_only
)
1281 && token
->val
.str
.len
== 1
1282 && token
->val
.str
.text
[0] == '\\')
1284 /* Allow backslash newline inside of gnu::base64 argument
1285 for -fpreprocessed, so that it doesn't have to be
1286 megabytes long line. */
1287 pfile
->state
.in_directive
= 0;
1288 token
= _cpp_get_token_no_padding (pfile
);
1289 pfile
->state
.in_directive
= 3;
1291 if (token
->type
== CPP_STRING
)
1295 save_token_for_embed (¶ms
->base64
, token
);
1296 token
= _cpp_get_token_no_padding (pfile
);
1297 while (token
->type
== CPP_OTHER
1298 && CPP_OPTION (pfile
, preprocessed
)
1299 && !CPP_OPTION (pfile
, directives_only
)
1300 && token
->val
.str
.len
== 1
1301 && token
->val
.str
.text
[0] == '\\')
1303 pfile
->state
.in_directive
= 0;
1304 token
= _cpp_get_token_no_padding (pfile
);
1305 pfile
->state
.in_directive
= 3;
1308 while (token
->type
== CPP_STRING
);
1309 if (token
->type
!= CPP_CLOSE_PAREN
)
1310 cpp_error_with_line (pfile
, CPP_DL_ERROR
, token
->src_loc
, 0,
1315 cpp_error_with_line (pfile
, CPP_DL_ERROR
, token
->src_loc
, 0,
1316 "expected character string literal");
1317 if (token
->type
!= CPP_CLOSE_PAREN
)
1318 token
= _cpp_get_token_no_padding (pfile
);
1320 token
= _cpp_get_token_no_padding (pfile
);
1322 else if (token
->type
== CPP_OPEN_PAREN
)
1324 cpp_embed_params_tokens
*save
= NULL
;
1325 auto save_comments
= pfile
->state
.save_comments
;
1328 case EMBED_PARAM_PREFIX
: save
= ¶ms
->prefix
; break;
1329 case EMBED_PARAM_SUFFIX
: save
= ¶ms
->suffix
; break;
1330 case EMBED_PARAM_IF_EMPTY
: save
= ¶ms
->if_empty
; break;
1333 if (params
->has_embed
)
1336 pfile
->state
.save_comments
= !CPP_OPTION (pfile
, discard_comments
);
1337 skip_balanced_token_seq (pfile
, CPP_CLOSE_PAREN
, save
, false);
1338 pfile
->state
.save_comments
= save_comments
;
1339 token
= _cpp_get_token_no_padding (pfile
);
1345 /* Handle #embed directive. */
1348 do_embed (cpp_reader
*pfile
)
1351 struct cpp_embed_params params
= {};
1353 const char *fname
= NULL
;
1355 /* Tell the lexer this is an embed directive. */
1356 pfile
->state
.in_directive
= 3;
1358 if (CPP_OPTION (pfile
, traditional
))
1360 cpp_error (pfile
, CPP_DL_ERROR
, /* FIXME should be DL_SORRY */
1361 "%<#embed%> not supported in traditional C");
1362 skip_rest_of_line (pfile
);
1366 if (CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, embed
))
1368 if (CPP_OPTION (pfile
, cplusplus
))
1369 cpp_error (pfile
, CPP_DL_PEDWARN
,
1370 "%<#%s%> is a GCC extension", "embed");
1372 cpp_error (pfile
, CPP_DL_PEDWARN
,
1373 "%<#%s%> before C23 is a GCC extension", "embed");
1376 fname
= parse_include (pfile
, &angle_brackets
, NULL
, ¶ms
.loc
);
1379 skip_rest_of_line (pfile
);
1385 cpp_error_with_line (pfile
, CPP_DL_ERROR
, params
.loc
, 0,
1386 "empty filename in #%s",
1387 pfile
->directive
->name
);
1388 skip_rest_of_line (pfile
);
1392 pfile
->state
.angled_headers
= false;
1393 pfile
->state
.directive_wants_padding
= false;
1394 ok
= _cpp_parse_embed_params (pfile
, ¶ms
);
1396 /* Get out of macro context, if we are. */
1397 skip_rest_of_line (pfile
);
1400 _cpp_stack_embed (pfile
, fname
, angle_brackets
, ¶ms
);
1402 _cpp_free_embed_params_tokens (¶ms
.prefix
);
1403 _cpp_free_embed_params_tokens (¶ms
.suffix
);
1404 _cpp_free_embed_params_tokens (¶ms
.if_empty
);
1405 _cpp_free_embed_params_tokens (¶ms
.base64
);
1411 /* Subroutine of do_linemarker. Read possible flags after file name.
1412 LAST is the last flag seen; 0 if this is the first flag. Return the
1413 flag if it is valid, 0 at the end of the directive. Otherwise
1416 read_flag (cpp_reader
*pfile
, unsigned int last
)
1418 const cpp_token
*token
= _cpp_lex_token (pfile
);
1420 if (token
->type
== CPP_NUMBER
&& token
->val
.str
.len
== 1)
1422 unsigned int flag
= token
->val
.str
.text
[0] - '0';
1424 if (flag
> last
&& flag
<= 4
1425 && (flag
!= 4 || last
== 3)
1426 && (flag
!= 2 || last
== 0))
1430 if (token
->type
!= CPP_EOF
)
1431 cpp_error (pfile
, CPP_DL_ERROR
, "invalid flag %qs in line directive",
1432 cpp_token_as_text (pfile
, token
));
1436 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
1437 of length LEN, to binary; store it in NUMP, and return false if the
1438 number was well-formed, true if not. WRAPPED is set to true if the
1439 number did not fit into 'linenum_type'. */
1441 strtolinenum (const uchar
*str
, size_t len
, linenum_type
*nump
, bool *wrapped
)
1443 linenum_type reg
= 0;
1446 bool seen_digit_sep
= false;
1451 if (!seen_digit_sep
&& c
== '\'' && len
)
1453 seen_digit_sep
= true;
1458 seen_digit_sep
= false;
1459 if (reg
> ((linenum_type
) -1) / 10)
1462 if (reg
> ((linenum_type
) -1) - (c
- '0'))
1470 /* Interpret #line command.
1471 Note that the filename string (if any) is a true string constant
1472 (escapes are interpreted). */
1474 do_line (cpp_reader
*pfile
)
1476 class line_maps
*line_table
= pfile
->line_table
;
1477 const line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (line_table
);
1479 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
1482 unsigned char map_sysp
= ORDINARY_MAP_IN_SYSTEM_HEADER_P (map
);
1483 const cpp_token
*token
;
1484 const char *new_file
= ORDINARY_MAP_FILE_NAME (map
);
1485 linenum_type new_lineno
;
1487 /* C99 raised the minimum limit on #line numbers. */
1488 linenum_type cap
= CPP_OPTION (pfile
, c99
) ? 2147483647 : 32767;
1491 /* #line commands expand macros. */
1492 token
= cpp_get_token (pfile
);
1493 if (token
->type
!= CPP_NUMBER
1494 || strtolinenum (token
->val
.str
.text
, token
->val
.str
.len
,
1495 &new_lineno
, &wrapped
))
1497 if (token
->type
== CPP_EOF
)
1498 cpp_error (pfile
, CPP_DL_ERROR
,
1499 "unexpected end of file after %<#line%>");
1501 cpp_error (pfile
, CPP_DL_ERROR
,
1502 "%qs after %<#line%> is not a positive integer",
1503 cpp_token_as_text (pfile
, token
));
1507 if ((new_lineno
== 0 || new_lineno
> cap
|| wrapped
)
1508 && cpp_pedwarning (pfile
, CPP_W_PEDANTIC
, "line number out of range"))
1511 cpp_error (pfile
, CPP_DL_WARNING
, "line number out of range");
1513 token
= cpp_get_token (pfile
);
1514 if (token
->type
== CPP_STRING
)
1516 cpp_string s
= { 0, 0 };
1517 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
, 1,
1519 new_file
= (const char *)s
.text
;
1520 check_eol (pfile
, true);
1522 else if (token
->type
!= CPP_EOF
)
1524 cpp_error (pfile
, CPP_DL_ERROR
, "%qs is not a valid filename",
1525 cpp_token_as_text (pfile
, token
));
1529 skip_rest_of_line (pfile
);
1530 _cpp_do_file_change (pfile
, LC_RENAME_VERBATIM
, new_file
, new_lineno
,
1532 line_table
->seen_line_directive
= true;
1535 /* Interpret the # 44 "file" [flags] notation, which has slightly
1536 different syntax and semantics from #line: Flags are allowed,
1537 and we never complain about the line number being too big. */
1539 do_linemarker (cpp_reader
*pfile
)
1541 class line_maps
*line_table
= pfile
->line_table
;
1542 const line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (line_table
);
1543 const cpp_token
*token
;
1544 const char *new_file
= ORDINARY_MAP_FILE_NAME (map
);
1545 linenum_type new_lineno
;
1546 unsigned int new_sysp
= ORDINARY_MAP_IN_SYSTEM_HEADER_P (map
);
1547 enum lc_reason reason
= LC_RENAME_VERBATIM
;
1551 /* Back up so we can get the number again. Putting this in
1552 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
1553 some circumstances, which can segfault. */
1554 _cpp_backup_tokens (pfile
, 1);
1556 /* #line commands expand macros. */
1557 token
= cpp_get_token (pfile
);
1558 if (token
->type
!= CPP_NUMBER
1559 || strtolinenum (token
->val
.str
.text
, token
->val
.str
.len
,
1560 &new_lineno
, &wrapped
))
1562 /* Unlike #line, there does not seem to be a way to get an EOF
1563 here. So, it should be safe to always spell the token. */
1564 cpp_error (pfile
, CPP_DL_ERROR
,
1565 "%qs after %<#%> is not a positive integer",
1566 cpp_token_as_text (pfile
, token
));
1570 token
= cpp_get_token (pfile
);
1571 if (token
->type
== CPP_STRING
)
1573 cpp_string s
= { 0, 0 };
1574 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
,
1576 new_file
= (const char *)s
.text
;
1579 flag
= read_flag (pfile
, 0);
1583 /* Fake an include for cpp_included (). */
1584 _cpp_fake_include (pfile
, new_file
);
1585 flag
= read_flag (pfile
, flag
);
1590 flag
= read_flag (pfile
, flag
);
1595 flag
= read_flag (pfile
, flag
);
1599 pfile
->buffer
->sysp
= new_sysp
;
1601 check_eol (pfile
, false);
1603 else if (token
->type
!= CPP_EOF
)
1605 cpp_error (pfile
, CPP_DL_ERROR
, "%qs is not a valid filename",
1606 cpp_token_as_text (pfile
, token
));
1610 skip_rest_of_line (pfile
);
1612 if (reason
== LC_LEAVE
)
1614 /* Reread map since cpp_get_token can invalidate it with a
1616 map
= LINEMAPS_LAST_ORDINARY_MAP (line_table
);
1617 const line_map_ordinary
*from
1618 = linemap_included_from_linemap (line_table
, map
);
1622 else if (!new_file
[0])
1623 /* Leaving to "" means fill in the popped-to name. */
1624 new_file
= ORDINARY_MAP_FILE_NAME (from
);
1625 else if (filename_cmp (ORDINARY_MAP_FILE_NAME (from
), new_file
) != 0)
1626 /* It's the wrong name, Grommit! */
1631 cpp_warning (pfile
, CPP_W_NONE
,
1632 "file %qs linemarker ignored due to "
1633 "incorrect nesting", new_file
);
1638 /* Compensate for the increment in linemap_add that occurs in
1639 _cpp_do_file_change. We're currently at the start of the line
1640 *following* the #line directive. A separate location_t for this
1641 location makes no sense (until we do the LC_LEAVE), and
1642 complicates LAST_SOURCE_LINE_LOCATION. */
1643 pfile
->line_table
->highest_location
--;
1645 _cpp_do_file_change (pfile
, reason
, new_file
, new_lineno
, new_sysp
);
1646 line_table
->seen_line_directive
= true;
1649 /* Arrange the file_change callback. Changing to TO_FILE:TO_LINE for
1650 REASON. SYSP is 1 for a system header, 2 for a system header that
1651 needs to be extern "C" protected, and zero otherwise. */
1653 _cpp_do_file_change (cpp_reader
*pfile
, enum lc_reason reason
,
1654 const char *to_file
, linenum_type to_line
,
1657 linemap_assert (reason
!= LC_ENTER_MACRO
);
1659 const line_map_ordinary
*ord_map
= NULL
;
1660 if (!to_line
&& reason
== LC_RENAME_VERBATIM
)
1662 /* A linemarker moving to line zero. If we're on the second
1663 line of the current map, and it also starts at zero, just
1664 rewind -- we're probably reading the builtins of a
1665 preprocessed source. */
1666 line_map_ordinary
*last
= LINEMAPS_LAST_ORDINARY_MAP (pfile
->line_table
);
1667 if (!ORDINARY_MAP_STARTING_LINE_NUMBER (last
)
1668 && 0 == filename_cmp (to_file
, ORDINARY_MAP_FILE_NAME (last
))
1669 && SOURCE_LINE (last
, pfile
->line_table
->highest_line
) == 2)
1672 pfile
->line_table
->highest_location
1673 = pfile
->line_table
->highest_line
= MAP_START_LOCATION (last
);
1678 if (const line_map
*map
= linemap_add (pfile
->line_table
, reason
, sysp
,
1681 ord_map
= linemap_check_ordinary (map
);
1682 linemap_line_start (pfile
->line_table
,
1683 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map
),
1687 if (pfile
->cb
.file_change
)
1688 pfile
->cb
.file_change (pfile
, ord_map
);
1691 /* Report a warning or error detected by the program we are
1692 processing. Use the directive's tokens in the error message. */
1694 do_diagnostic (cpp_reader
*pfile
, enum cpp_diagnostic_level code
,
1695 enum cpp_warning_reason reason
, int print_dir
)
1697 const unsigned char *dir_name
;
1698 unsigned char *line
;
1699 location_t src_loc
= pfile
->cur_token
[-1].src_loc
;
1702 dir_name
= pfile
->directive
->name
;
1705 pfile
->state
.prevent_expansion
++;
1706 line
= cpp_output_line_to_string (pfile
, dir_name
);
1707 pfile
->state
.prevent_expansion
--;
1709 if (code
== CPP_DL_WARNING_SYSHDR
&& reason
)
1710 cpp_warning_with_line_syshdr (pfile
, reason
, src_loc
, 0, "%s", line
);
1711 else if (code
== CPP_DL_WARNING
&& reason
)
1712 cpp_warning_with_line (pfile
, reason
, src_loc
, 0, "%s", line
);
1714 cpp_error_with_line (pfile
, code
, src_loc
, 0, "%s", line
);
1719 do_error (cpp_reader
*pfile
)
1721 do_diagnostic (pfile
, CPP_DL_ERROR
, CPP_W_NONE
, 1);
1725 do_warning (cpp_reader
*pfile
)
1727 /* We want #warning diagnostics to be emitted in system headers too. */
1728 do_diagnostic (pfile
, CPP_DL_WARNING_SYSHDR
, CPP_W_WARNING_DIRECTIVE
, 1);
1731 /* Report program identification. */
1733 do_ident (cpp_reader
*pfile
)
1735 const cpp_token
*str
= cpp_get_token (pfile
);
1737 if (str
->type
!= CPP_STRING
)
1738 cpp_error (pfile
, CPP_DL_ERROR
, "invalid #%s directive",
1739 pfile
->directive
->name
);
1740 else if (pfile
->cb
.ident
)
1741 pfile
->cb
.ident (pfile
, pfile
->directive_line
, &str
->val
.str
);
1743 check_eol (pfile
, false);
1746 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1747 matching entry, or NULL if none is found. The returned entry could
1748 be the start of a namespace chain, or a pragma. */
1749 static struct pragma_entry
*
1750 lookup_pragma_entry (struct pragma_entry
*chain
, const cpp_hashnode
*pragma
)
1752 while (chain
&& chain
->pragma
!= pragma
)
1753 chain
= chain
->next
;
1758 /* Create and insert a blank pragma entry at the beginning of a
1759 singly-linked CHAIN. */
1760 static struct pragma_entry
*
1761 new_pragma_entry (cpp_reader
*pfile
, struct pragma_entry
**chain
)
1763 struct pragma_entry
*new_entry
;
1765 new_entry
= (struct pragma_entry
*)
1766 _cpp_aligned_alloc (pfile
, sizeof (struct pragma_entry
));
1768 memset (new_entry
, 0, sizeof (struct pragma_entry
));
1769 new_entry
->next
= *chain
;
1775 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1776 goes in the global namespace. */
1777 static struct pragma_entry
*
1778 register_pragma_1 (cpp_reader
*pfile
, const char *space
, const char *name
,
1779 bool allow_name_expansion
)
1781 struct pragma_entry
**chain
= &pfile
->pragmas
;
1782 struct pragma_entry
*entry
;
1783 const cpp_hashnode
*node
;
1787 node
= cpp_lookup (pfile
, UC space
, strlen (space
));
1788 entry
= lookup_pragma_entry (*chain
, node
);
1791 entry
= new_pragma_entry (pfile
, chain
);
1792 entry
->pragma
= node
;
1793 entry
->is_nspace
= true;
1794 entry
->allow_expansion
= allow_name_expansion
;
1796 else if (!entry
->is_nspace
)
1798 else if (entry
->allow_expansion
!= allow_name_expansion
)
1800 cpp_error (pfile
, CPP_DL_ICE
,
1801 "registering pragmas in namespace %qs with mismatched "
1802 "name expansion", space
);
1805 chain
= &entry
->u
.space
;
1807 else if (allow_name_expansion
)
1809 cpp_error (pfile
, CPP_DL_ICE
,
1810 "registering pragma %qs with name expansion "
1811 "and no namespace", name
);
1815 /* Check for duplicates. */
1816 node
= cpp_lookup (pfile
, UC name
, strlen (name
));
1817 entry
= lookup_pragma_entry (*chain
, node
);
1820 entry
= new_pragma_entry (pfile
, chain
);
1821 entry
->pragma
= node
;
1825 if (entry
->is_nspace
)
1827 cpp_error (pfile
, CPP_DL_ICE
,
1828 "registering %qs as both a pragma and a pragma namespace",
1831 cpp_error (pfile
, CPP_DL_ICE
, "%<#pragma %s %s%> is already registered",
1834 cpp_error (pfile
, CPP_DL_ICE
, "%<#pragma %s%> is already registered",
1840 /* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1842 register_pragma_internal (cpp_reader
*pfile
, const char *space
,
1843 const char *name
, pragma_cb handler
)
1845 struct pragma_entry
*entry
;
1847 entry
= register_pragma_1 (pfile
, space
, name
, false);
1848 entry
->is_internal
= true;
1849 entry
->u
.handler
= handler
;
1852 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1853 goes in the global namespace. HANDLER is the handler it will call,
1854 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1855 expansion while parsing pragma NAME. This function is exported
1858 cpp_register_pragma (cpp_reader
*pfile
, const char *space
, const char *name
,
1859 pragma_cb handler
, bool allow_expansion
)
1861 struct pragma_entry
*entry
;
1865 cpp_error (pfile
, CPP_DL_ICE
, "registering pragma with NULL handler");
1869 entry
= register_pragma_1 (pfile
, space
, name
, false);
1872 entry
->allow_expansion
= allow_expansion
;
1873 entry
->u
.handler
= handler
;
1877 /* Similarly, but create mark the pragma for deferred processing.
1878 When found, a CPP_PRAGMA token will be insertted into the stream
1879 with IDENT in the token->u.pragma slot. */
1881 cpp_register_deferred_pragma (cpp_reader
*pfile
, const char *space
,
1882 const char *name
, unsigned int ident
,
1883 bool allow_expansion
, bool allow_name_expansion
)
1885 struct pragma_entry
*entry
;
1887 entry
= register_pragma_1 (pfile
, space
, name
, allow_name_expansion
);
1890 entry
->is_deferred
= true;
1891 entry
->allow_expansion
= allow_expansion
;
1892 entry
->u
.ident
= ident
;
1896 /* Register the pragmas the preprocessor itself handles. */
1898 _cpp_init_internal_pragmas (cpp_reader
*pfile
)
1900 /* Pragmas in the global namespace. */
1901 register_pragma_internal (pfile
, 0, "once", do_pragma_once
);
1902 register_pragma_internal (pfile
, 0, "push_macro", do_pragma_push_macro
);
1903 register_pragma_internal (pfile
, 0, "pop_macro", do_pragma_pop_macro
);
1905 /* New GCC-specific pragmas should be put in the GCC namespace. */
1906 register_pragma_internal (pfile
, "GCC", "poison", do_pragma_poison
);
1907 register_pragma_internal (pfile
, "GCC", "system_header",
1908 do_pragma_system_header
);
1909 register_pragma_internal (pfile
, "GCC", "dependency", do_pragma_dependency
);
1910 register_pragma_internal (pfile
, "GCC", "warning", do_pragma_warning
);
1911 register_pragma_internal (pfile
, "GCC", "error", do_pragma_error
);
1914 /* Return the number of registered pragmas in PE. */
1917 count_registered_pragmas (struct pragma_entry
*pe
)
1920 for (; pe
!= NULL
; pe
= pe
->next
)
1923 ct
+= count_registered_pragmas (pe
->u
.space
);
1929 /* Save into SD the names of the registered pragmas referenced by PE,
1930 and return a pointer to the next free space in SD. */
1933 save_registered_pragmas (struct pragma_entry
*pe
, char **sd
)
1935 for (; pe
!= NULL
; pe
= pe
->next
)
1938 sd
= save_registered_pragmas (pe
->u
.space
, sd
);
1939 *sd
++ = (char *) xmemdup (HT_STR (&pe
->pragma
->ident
),
1940 HT_LEN (&pe
->pragma
->ident
),
1941 HT_LEN (&pe
->pragma
->ident
) + 1);
1946 /* Return a newly-allocated array which saves the names of the
1947 registered pragmas. */
1950 _cpp_save_pragma_names (cpp_reader
*pfile
)
1952 int ct
= count_registered_pragmas (pfile
->pragmas
);
1953 char **result
= XNEWVEC (char *, ct
);
1954 (void) save_registered_pragmas (pfile
->pragmas
, result
);
1958 /* Restore from SD the names of the registered pragmas referenced by PE,
1959 and return a pointer to the next unused name in SD. */
1962 restore_registered_pragmas (cpp_reader
*pfile
, struct pragma_entry
*pe
,
1965 for (; pe
!= NULL
; pe
= pe
->next
)
1968 sd
= restore_registered_pragmas (pfile
, pe
->u
.space
, sd
);
1969 pe
->pragma
= cpp_lookup (pfile
, UC
*sd
, strlen (*sd
));
1976 /* Restore the names of the registered pragmas from SAVED. */
1979 _cpp_restore_pragma_names (cpp_reader
*pfile
, char **saved
)
1981 (void) restore_registered_pragmas (pfile
, pfile
->pragmas
, saved
);
1985 /* Pragmata handling. We handle some, and pass the rest on to the
1986 front end. C99 defines three pragmas and says that no macro
1987 expansion is to be performed on them; whether or not macro
1988 expansion happens for other pragmas is implementation defined.
1989 This implementation allows for a mix of both, since GCC did not
1990 traditionally macro expand its (few) pragmas, whereas OpenMP
1991 specifies that macro expansion should happen. */
1993 do_pragma (cpp_reader
*pfile
)
1995 const struct pragma_entry
*p
= NULL
;
1996 const cpp_token
*token
, *pragma_token
;
1997 location_t pragma_token_virt_loc
= 0;
1999 unsigned int count
= 1;
2001 pfile
->state
.prevent_expansion
++;
2003 pragma_token
= token
= cpp_get_token_with_location (pfile
,
2004 &pragma_token_virt_loc
);
2006 if (token
->type
== CPP_NAME
)
2008 p
= lookup_pragma_entry (pfile
->pragmas
, token
->val
.node
.node
);
2009 if (p
&& p
->is_nspace
)
2011 bool allow_name_expansion
= p
->allow_expansion
;
2012 if (allow_name_expansion
)
2013 pfile
->state
.prevent_expansion
--;
2015 token
= cpp_get_token (pfile
);
2016 if (token
->type
== CPP_NAME
)
2017 p
= lookup_pragma_entry (p
->u
.space
, token
->val
.node
.node
);
2020 if (allow_name_expansion
)
2021 pfile
->state
.prevent_expansion
++;
2030 pfile
->directive_result
.src_loc
= pragma_token_virt_loc
;
2031 pfile
->directive_result
.type
= CPP_PRAGMA
;
2032 pfile
->directive_result
.flags
= pragma_token
->flags
;
2033 pfile
->directive_result
.val
.pragma
= p
->u
.ident
;
2034 pfile
->state
.in_deferred_pragma
= true;
2035 pfile
->state
.pragma_allow_expansion
= p
->allow_expansion
;
2036 if (!p
->allow_expansion
)
2037 pfile
->state
.prevent_expansion
++;
2041 /* Since the handler below doesn't get the line number, that
2042 it might need for diagnostics, make sure it has the right
2043 numbers in place. */
2044 if (pfile
->cb
.line_change
)
2045 (*pfile
->cb
.line_change
) (pfile
, pragma_token
, false);
2046 if (p
->allow_expansion
)
2047 pfile
->state
.prevent_expansion
--;
2048 (*p
->u
.handler
) (pfile
);
2049 if (p
->allow_expansion
)
2050 pfile
->state
.prevent_expansion
++;
2053 else if (pfile
->cb
.def_pragma
)
2055 if (count
== 1 || pfile
->context
->prev
== NULL
)
2056 _cpp_backup_tokens (pfile
, count
);
2059 /* Invalid name comes from macro expansion, _cpp_backup_tokens
2060 won't allow backing 2 tokens. */
2061 const auto tok_buff
= _cpp_get_buff (pfile
, 2 * sizeof (cpp_token
));
2062 const auto toks
= (cpp_token
*)tok_buff
->base
;
2064 toks
[0].flags
|= NO_EXPAND
;
2066 toks
[1].flags
|= NO_EXPAND
| PREV_WHITE
;
2067 _cpp_push_token_context (pfile
, NULL
, toks
, 2);
2068 /* Arrange to free this buffer when no longer needed. */
2069 pfile
->context
->buff
= tok_buff
;
2071 pfile
->cb
.def_pragma (pfile
, pfile
->directive_line
);
2074 pfile
->state
.prevent_expansion
--;
2077 /* Handle #pragma once. */
2079 do_pragma_once (cpp_reader
*pfile
)
2081 if (_cpp_in_main_source_file (pfile
))
2082 cpp_warning (pfile
, CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER
,
2083 "%<#pragma once%> in main file");
2085 check_eol (pfile
, false);
2086 _cpp_mark_file_once_only (pfile
, pfile
->buffer
->file
);
2089 /* Helper for #pragma {push,pop}_macro. Destringize STR and
2090 lex it into an identifier, returning the hash node for it. */
2092 static cpp_hashnode
*
2093 lex_identifier_from_string (cpp_reader
*pfile
, cpp_string str
)
2095 auto src
= (const uchar
*) memchr (str
.text
, '"', str
.len
);
2096 gcc_checking_assert (src
);
2098 const auto limit
= str
.text
+ str
.len
- 1;
2099 gcc_checking_assert (*limit
== '"' && limit
>= src
);
2100 const auto ident
= XALLOCAVEC (uchar
, limit
- src
+ 1);
2102 while (src
!= limit
)
2104 /* We know there is a character following the backslash. */
2105 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
2110 /* We reserved a spot for the newline with the + 1 when allocating IDENT.
2111 Push a buffer containing the identifier to lex. */
2113 cpp_push_buffer (pfile
, ident
, dest
- ident
, true);
2114 _cpp_clean_line (pfile
);
2115 pfile
->cur_token
= _cpp_temp_token (pfile
);
2118 /* Suppress diagnostics during lexing so that we silently ignore invalid
2119 input, as seems to be the common practice for this pragma. */
2120 cpp_auto_suppress_diagnostics suppress
{pfile
};
2121 tok
= _cpp_lex_direct (pfile
);
2125 if (tok
->type
!= CPP_NAME
|| pfile
->buffer
->cur
!= pfile
->buffer
->rlimit
)
2128 node
= tok
->val
.node
.node
;
2130 _cpp_pop_buffer (pfile
);
2134 /* Common processing for #pragma {push,pop}_macro. */
2136 static cpp_hashnode
*
2137 push_pop_macro_common (cpp_reader
*pfile
, const char *type
)
2139 const cpp_token
*const txt
= get__Pragma_string (pfile
);
2140 ++pfile
->keep_tokens
;
2144 check_eol (pfile
, false);
2145 skip_rest_of_line (pfile
);
2146 node
= lex_identifier_from_string (pfile
, txt
->val
.str
);
2151 location_t src_loc
= pfile
->cur_token
[-1].src_loc
;
2152 cpp_error_with_line (pfile
, CPP_DL_ERROR
, src_loc
, 0,
2153 "invalid %<#pragma %s_macro%> directive", type
);
2154 skip_rest_of_line (pfile
);
2156 --pfile
->keep_tokens
;
2160 /* Handle #pragma push_macro(STRING). */
2162 do_pragma_push_macro (cpp_reader
*pfile
)
2164 const auto node
= push_pop_macro_common (pfile
, "push");
2167 const auto c
= XCNEW (def_pragma_macro
);
2168 c
->name
= xstrdup ((const char *) NODE_NAME (node
));
2169 c
->next
= pfile
->pushed_macros
;
2170 if (node
->type
== NT_VOID
)
2172 else if (node
->type
== NT_BUILTIN_MACRO
)
2176 const auto defn
= cpp_macro_definition (pfile
, node
);
2177 const size_t defnlen
= ustrlen (defn
);
2178 c
->definition
= XNEWVEC (uchar
, defnlen
+ 2);
2179 c
->definition
[defnlen
] = '\n';
2180 c
->definition
[defnlen
+ 1] = 0;
2181 c
->line
= node
->value
.macro
->line
;
2182 c
->syshdr
= node
->value
.macro
->syshdr
;
2183 c
->used
= node
->value
.macro
->used
;
2184 memcpy (c
->definition
, defn
, defnlen
);
2187 pfile
->pushed_macros
= c
;
2190 /* Handle #pragma pop_macro(STRING). */
2192 do_pragma_pop_macro (cpp_reader
*pfile
)
2194 const auto node
= push_pop_macro_common (pfile
, "pop");
2197 for (def_pragma_macro
*c
= pfile
->pushed_macros
, *l
= nullptr; c
; c
= c
->next
)
2199 if (!strcmp (c
->name
, (const char *) NODE_NAME (node
)))
2202 pfile
->pushed_macros
= c
->next
;
2205 cpp_pop_definition (pfile
, c
, node
);
2206 free (c
->definition
);
2215 /* Handle #pragma GCC poison, to poison one or more identifiers so
2216 that the lexer produces a hard error for each subsequent usage. */
2218 do_pragma_poison (cpp_reader
*pfile
)
2220 const cpp_token
*tok
;
2223 pfile
->state
.poisoned_ok
= 1;
2226 tok
= _cpp_lex_token (pfile
);
2227 if (tok
->type
== CPP_EOF
)
2229 if (tok
->type
!= CPP_NAME
)
2231 cpp_error (pfile
, CPP_DL_ERROR
,
2232 "invalid %<#pragma GCC poison%> directive");
2236 hp
= tok
->val
.node
.node
;
2237 if (hp
->flags
& NODE_POISONED
)
2240 if (cpp_macro_p (hp
))
2241 cpp_error (pfile
, CPP_DL_WARNING
, "poisoning existing macro %qs",
2243 _cpp_free_definition (hp
);
2244 hp
->flags
|= NODE_POISONED
| NODE_DIAGNOSTIC
;
2245 const auto data
= (cpp_hashnode_extra
*)
2246 ht_lookup (pfile
->extra_hash_table
, hp
->ident
, HT_ALLOC
);
2247 data
->poisoned_loc
= tok
->src_loc
;
2249 pfile
->state
.poisoned_ok
= 0;
2252 /* Mark the current header as a system header. This will suppress
2253 some categories of warnings (notably those from -pedantic). It is
2254 intended for use in system libraries that cannot be implemented in
2255 conforming C, but cannot be certain that their headers appear in a
2256 system include directory. To prevent abuse, it is rejected in the
2257 primary source file. */
2259 do_pragma_system_header (cpp_reader
*pfile
)
2261 if (_cpp_in_main_source_file (pfile
))
2262 cpp_error (pfile
, CPP_DL_WARNING
,
2263 "%<#pragma system_header%> ignored outside include file");
2266 check_eol (pfile
, false);
2267 skip_rest_of_line (pfile
);
2268 cpp_make_system_header (pfile
, 1, 0);
2272 /* Check the modified date of the current include file against a specified
2273 file. Issue a diagnostic, if the specified file is newer. We use this to
2274 determine if a fixed header should be refixed. */
2276 do_pragma_dependency (cpp_reader
*pfile
)
2279 int angle_brackets
, ordering
;
2280 location_t location
;
2282 fname
= parse_include (pfile
, &angle_brackets
, NULL
, &location
);
2286 ordering
= _cpp_compare_file_date (pfile
, fname
, angle_brackets
);
2288 cpp_error (pfile
, CPP_DL_WARNING
, "cannot find source file %s", fname
);
2289 else if (ordering
> 0)
2291 cpp_error (pfile
, CPP_DL_WARNING
,
2292 "current file is older than %s", fname
);
2293 if (cpp_get_token (pfile
)->type
!= CPP_EOF
)
2295 _cpp_backup_tokens (pfile
, 1);
2296 do_diagnostic (pfile
, CPP_DL_WARNING
, CPP_W_NONE
, 0);
2300 free ((void *) fname
);
2303 /* Issue a diagnostic with the message taken from the pragma. If
2304 ERROR is true, the diagnostic is a warning, otherwise, it is an
2307 do_pragma_warning_or_error (cpp_reader
*pfile
, bool error
)
2309 const cpp_token
*tok
= _cpp_lex_token (pfile
);
2311 if (tok
->type
!= CPP_STRING
2312 || !cpp_interpret_string_notranslate (pfile
, &tok
->val
.str
, 1, &str
,
2316 cpp_error (pfile
, CPP_DL_ERROR
, "invalid %<#pragma GCC %s%> directive",
2317 error
? "error" : "warning");
2320 cpp_error (pfile
, error
? CPP_DL_ERROR
: CPP_DL_WARNING
,
2322 free ((void *)str
.text
);
2325 /* Issue a warning diagnostic. */
2327 do_pragma_warning (cpp_reader
*pfile
)
2329 do_pragma_warning_or_error (pfile
, false);
2332 /* Issue an error diagnostic. */
2334 do_pragma_error (cpp_reader
*pfile
)
2336 do_pragma_warning_or_error (pfile
, true);
2339 /* Get a token but skip padding. */
2340 static const cpp_token
*
2341 get_token_no_padding (cpp_reader
*pfile
)
2345 const cpp_token
*result
= cpp_get_token (pfile
);
2346 if (result
->type
!= CPP_PADDING
)
2351 /* Check syntax is "(string-literal)". Returns the string on success,
2352 or NULL on failure. */
2353 static const cpp_token
*
2354 get__Pragma_string (cpp_reader
*pfile
)
2356 const cpp_token
*string
;
2357 const cpp_token
*paren
;
2359 paren
= get_token_no_padding (pfile
);
2360 if (paren
->type
== CPP_EOF
)
2361 _cpp_backup_tokens (pfile
, 1);
2362 if (paren
->type
!= CPP_OPEN_PAREN
)
2365 string
= get_token_no_padding (pfile
);
2366 if (string
->type
== CPP_EOF
)
2367 _cpp_backup_tokens (pfile
, 1);
2368 if (string
->type
!= CPP_STRING
&& string
->type
!= CPP_WSTRING
2369 && string
->type
!= CPP_STRING32
&& string
->type
!= CPP_STRING16
2370 && string
->type
!= CPP_UTF8STRING
)
2373 paren
= get_token_no_padding (pfile
);
2374 if (paren
->type
== CPP_EOF
)
2375 _cpp_backup_tokens (pfile
, 1);
2376 if (paren
->type
!= CPP_CLOSE_PAREN
)
2382 /* Destringize IN into a temporary buffer, by removing the first \ of
2383 \" and \\ sequences, and process the result as a #pragma directive. */
2385 destringize_and_run (cpp_reader
*pfile
, const cpp_string
*in
,
2386 location_t expansion_loc
)
2388 const unsigned char *src
, *limit
;
2389 char *dest
, *result
;
2390 cpp_context
*saved_context
;
2391 cpp_token
*saved_cur_token
;
2392 tokenrun
*saved_cur_run
;
2395 const struct directive
*save_directive
;
2397 dest
= result
= (char *) alloca (in
->len
- 1);
2398 src
= in
->text
+ 1 + (in
->text
[0] == 'L');
2399 limit
= in
->text
+ in
->len
- 1;
2402 /* We know there is a character following the backslash. */
2403 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
2409 /* Ugh; an awful kludge. We are really not set up to be lexing
2410 tokens when in the middle of a macro expansion. Use a new
2411 context to force cpp_get_token to lex, and so skip_rest_of_line
2412 doesn't go beyond the end of the text. Also, remember the
2413 current lexing position so we can return to it later.
2415 Something like line-at-a-time lexing should remove the need for
2417 saved_context
= pfile
->context
;
2418 saved_cur_token
= pfile
->cur_token
;
2419 saved_cur_run
= pfile
->cur_run
;
2421 pfile
->context
= XCNEW (cpp_context
);
2423 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
2424 until we've read all of the tokens that we want. */
2425 cpp_push_buffer (pfile
, (const uchar
*) result
, dest
- result
,
2426 /* from_stage3 */ true);
2428 /* This is needed for _Pragma("once") and _Pragma("GCC system_header") to work
2430 pfile
->buffer
->file
= pfile
->buffer
->prev
->file
;
2431 pfile
->buffer
->sysp
= pfile
->buffer
->prev
->sysp
;
2433 /* See comment below regarding the use of expansion_loc as the location
2434 for all tokens; arrange here that diagnostics issued during lexing
2435 get the same treatment. */
2436 const auto prev_loc_override
= pfile
->diagnostic_override_loc
;
2437 pfile
->diagnostic_override_loc
= expansion_loc
;
2439 start_directive (pfile
);
2440 _cpp_clean_line (pfile
);
2441 save_directive
= pfile
->directive
;
2442 pfile
->directive
= &dtable
[T_PRAGMA
];
2444 if (pfile
->directive_result
.type
== CPP_PRAGMA
)
2445 pfile
->directive_result
.flags
|= PRAGMA_OP
;
2446 end_directive (pfile
, 1);
2447 pfile
->directive
= save_directive
;
2449 /* We always insert at least one token, the directive result. It'll
2450 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
2451 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
2453 /* If we're not handling the pragma internally, read all of the tokens from
2454 the string buffer now, while the string buffer is still installed. */
2455 /* ??? Note that the token buffer allocated here is leaked. It's not clear
2456 to me what the true lifespan of the tokens are. It would appear that
2457 the lifespan is the entire parse of the main input stream, in which case
2458 this may not be wrong. */
2459 if (pfile
->directive_result
.type
== CPP_PRAGMA
)
2465 toks
= XNEWVEC (cpp_token
, maxcount
);
2466 toks
[0] = pfile
->directive_result
;
2467 toks
[0].src_loc
= expansion_loc
;
2471 if (count
== maxcount
)
2473 maxcount
= maxcount
* 3 / 2;
2474 toks
= XRESIZEVEC (cpp_token
, toks
, maxcount
);
2476 toks
[count
] = *cpp_get_token (pfile
);
2477 /* _Pragma is a builtin, so we're not within a macro-map, and so
2478 the token locations are set to bogus ordinary locations
2479 near to, but after that of the "_Pragma".
2480 Paper over this by setting them equal to the location of the
2481 _Pragma itself (PR preprocessor/69126). */
2482 toks
[count
].src_loc
= expansion_loc
;
2483 /* Macros have been already expanded by cpp_get_token
2484 if the pragma allowed expansion. */
2485 toks
[count
++].flags
|= NO_EXPAND
;
2487 while (toks
[count
-1].type
!= CPP_PRAGMA_EOL
);
2492 toks
= &pfile
->avoid_paste
;
2494 /* If we handled the entire pragma internally, make sure we get the
2495 line number correct for the next token. */
2496 if (pfile
->cb
.line_change
)
2497 pfile
->cb
.line_change (pfile
, pfile
->cur_token
, false);
2500 /* Finish inlining run_directive. */
2501 pfile
->buffer
->file
= NULL
;
2502 /* If the system header state changed due to #pragma GCC system_header, then
2503 make that applicable to the real buffer too. */
2504 pfile
->buffer
->prev
->sysp
= pfile
->buffer
->sysp
;
2505 _cpp_pop_buffer (pfile
);
2506 pfile
->diagnostic_override_loc
= prev_loc_override
;
2508 /* Reset the old macro state before ... */
2509 XDELETE (pfile
->context
);
2510 pfile
->context
= saved_context
;
2511 pfile
->cur_token
= saved_cur_token
;
2512 pfile
->cur_run
= saved_cur_run
;
2514 /* ... inserting the new tokens we collected. */
2515 _cpp_push_token_context (pfile
, NULL
, toks
, count
);
2518 /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
2520 _cpp_do__Pragma (cpp_reader
*pfile
, location_t expansion_loc
)
2522 /* Make sure we don't invalidate the string token, if the closing parenthesis
2523 ended up on a different line. */
2524 ++pfile
->keep_tokens
;
2525 const cpp_token
*string
= get__Pragma_string (pfile
);
2526 --pfile
->keep_tokens
;
2528 pfile
->directive_result
.type
= CPP_PADDING
;
2532 destringize_and_run (pfile
, &string
->val
.str
, expansion_loc
);
2535 cpp_error (pfile
, CPP_DL_ERROR
,
2536 "%<_Pragma%> takes a parenthesized string literal");
2540 /* Handle #ifdef. */
2542 do_ifdef (cpp_reader
*pfile
)
2546 if (! pfile
->state
.skipping
)
2548 cpp_hashnode
*node
= lex_macro_node (pfile
, false);
2552 skip
= !_cpp_defined_macro_p (node
);
2553 if (!_cpp_maybe_notify_macro_use (pfile
, node
, pfile
->directive_line
))
2554 /* It wasn't a macro after all. */
2556 _cpp_mark_macro_used (node
);
2558 pfile
->cb
.used (pfile
, pfile
->directive_line
, node
);
2559 check_eol (pfile
, false);
2563 push_conditional (pfile
, skip
, T_IFDEF
, 0);
2566 /* Handle #ifndef. */
2568 do_ifndef (cpp_reader
*pfile
)
2571 cpp_hashnode
*node
= 0;
2573 if (! pfile
->state
.skipping
)
2575 node
= lex_macro_node (pfile
, false);
2579 skip
= _cpp_defined_macro_p (node
);
2580 if (!_cpp_maybe_notify_macro_use (pfile
, node
, pfile
->directive_line
))
2581 /* It wasn't a macro after all. */
2583 _cpp_mark_macro_used (node
);
2585 pfile
->cb
.used (pfile
, pfile
->directive_line
, node
);
2586 check_eol (pfile
, false);
2590 push_conditional (pfile
, skip
, T_IFNDEF
, node
);
2593 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
2594 pfile->mi_ind_cmacro so we can handle multiple-include
2595 optimizations. If macro expansion occurs in the expression, we
2596 cannot treat it as a controlling conditional, since the expansion
2597 could change in the future. That is handled by cpp_get_token. */
2599 do_if (cpp_reader
*pfile
)
2603 if (! pfile
->state
.skipping
)
2604 skip
= _cpp_parse_expr (pfile
, "#if", NULL
) == false;
2606 push_conditional (pfile
, skip
, T_IF
, pfile
->mi_ind_cmacro
);
2609 /* Flip skipping state if appropriate and continue without changing
2610 if_stack; this is so that the error message for missing #endif's
2611 etc. will point to the original #if. */
2613 do_else (cpp_reader
*pfile
)
2615 cpp_buffer
*buffer
= pfile
->buffer
;
2616 struct if_stack
*ifs
= buffer
->if_stack
;
2619 cpp_error (pfile
, CPP_DL_ERROR
, "%<#else%> without %<#if%>");
2622 if (ifs
->type
== T_ELSE
)
2624 cpp_error (pfile
, CPP_DL_ERROR
, "%<#else%> after %<#else%>");
2625 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2626 "the conditional began here");
2630 /* Skip any future (erroneous) #elses or #elifs. */
2631 pfile
->state
.skipping
= ifs
->skip_elses
;
2632 ifs
->skip_elses
= true;
2634 /* Invalidate any controlling macro. */
2637 /* Only check EOL if was not originally skipping. */
2638 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
2639 check_eol_endif_labels (pfile
);
2643 /* Handle a #elif, #elifdef or #elifndef directive by not changing if_stack
2644 either. See the comment above do_else. */
2646 do_elif (cpp_reader
*pfile
)
2648 cpp_buffer
*buffer
= pfile
->buffer
;
2649 struct if_stack
*ifs
= buffer
->if_stack
;
2652 cpp_error (pfile
, CPP_DL_ERROR
, "%<#%s%> without %<#if%>",
2653 pfile
->directive
->name
);
2656 if (ifs
->type
== T_ELSE
)
2658 cpp_error (pfile
, CPP_DL_ERROR
, "%<#%s%> after %<#else%>",
2659 pfile
->directive
->name
);
2660 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2661 "the conditional began here");
2665 /* See DR#412: "Only the first group whose control condition
2666 evaluates to true (nonzero) is processed; any following groups
2667 are skipped and their controlling directives are processed as
2668 if they were in a group that is skipped." */
2669 if (ifs
->skip_elses
)
2671 /* In older GNU standards, #elifdef/#elifndef is supported
2672 as an extension, but pedwarn if -pedantic if the presence
2673 of the directive would be rejected. */
2674 if (pfile
->directive
!= &dtable
[T_ELIF
]
2675 && ! CPP_OPTION (pfile
, elifdef
)
2676 && CPP_PEDANTIC (pfile
)
2677 && !pfile
->state
.skipping
)
2679 if (CPP_OPTION (pfile
, cplusplus
))
2680 cpp_pedwarning (pfile
, CPP_W_CXX23_EXTENSIONS
,
2681 "%<#%s%> before C++23 is a GCC extension",
2682 pfile
->directive
->name
);
2684 cpp_pedwarning (pfile
, CPP_W_PEDANTIC
,
2685 "%<#%s%> before C23 is a GCC extension",
2686 pfile
->directive
->name
);
2688 pfile
->state
.skipping
= 1;
2692 if (pfile
->directive
== &dtable
[T_ELIF
])
2693 pfile
->state
.skipping
= !_cpp_parse_expr (pfile
, "#elif", NULL
);
2696 cpp_hashnode
*node
= lex_macro_node (pfile
, false);
2700 bool macro_defined
= _cpp_defined_macro_p (node
);
2701 if (!_cpp_maybe_notify_macro_use (pfile
, node
,
2702 pfile
->directive_line
))
2703 /* It wasn't a macro after all. */
2704 macro_defined
= false;
2705 bool skip
= (pfile
->directive
== &dtable
[T_ELIFDEF
]
2709 pfile
->cb
.used (pfile
, pfile
->directive_line
, node
);
2710 check_eol (pfile
, false);
2711 /* In older GNU standards, #elifdef/#elifndef is supported
2712 as an extension, but pedwarn if -pedantic if the presence
2713 of the directive would change behavior. */
2714 if (! CPP_OPTION (pfile
, elifdef
)
2715 && CPP_PEDANTIC (pfile
)
2716 && pfile
->state
.skipping
!= skip
)
2718 if (CPP_OPTION (pfile
, cplusplus
))
2719 cpp_pedwarning (pfile
, CPP_W_CXX23_EXTENSIONS
,
2720 "%<#%s%> before C++23 is a GCC "
2722 pfile
->directive
->name
);
2724 cpp_pedwarning (pfile
, CPP_W_PEDANTIC
,
2725 "%<#%s%> before C23 is a GCC "
2727 pfile
->directive
->name
);
2729 pfile
->state
.skipping
= skip
;
2732 ifs
->skip_elses
= !pfile
->state
.skipping
;
2735 /* Invalidate any controlling macro. */
2740 /* Handle a #elifdef directive. */
2742 do_elifdef (cpp_reader
*pfile
)
2747 /* Handle a #elifndef directive. */
2749 do_elifndef (cpp_reader
*pfile
)
2754 /* #endif pops the if stack and resets pfile->state.skipping. */
2756 do_endif (cpp_reader
*pfile
)
2758 cpp_buffer
*buffer
= pfile
->buffer
;
2759 struct if_stack
*ifs
= buffer
->if_stack
;
2762 cpp_error (pfile
, CPP_DL_ERROR
, "%<#endif%> without %<#if%>");
2765 /* Only check EOL if was not originally skipping. */
2766 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
2767 check_eol_endif_labels (pfile
);
2769 /* If potential control macro, we go back outside again. */
2770 if (ifs
->next
== 0 && ifs
->mi_cmacro
)
2772 pfile
->mi_valid
= true;
2773 pfile
->mi_cmacro
= ifs
->mi_cmacro
;
2774 pfile
->mi_loc
= ifs
->line
;
2775 pfile
->mi_def_cmacro
= nullptr;
2776 if (ifs
->mi_def_cmacro
&& !_cpp_defined_macro_p (pfile
->mi_cmacro
))
2778 pfile
->mi_def_cmacro
= ifs
->mi_def_cmacro
;
2779 pfile
->mi_def_loc
= ifs
->def_loc
;
2783 buffer
->if_stack
= ifs
->next
;
2784 pfile
->state
.skipping
= ifs
->was_skipping
;
2785 obstack_free (&pfile
->buffer_ob
, ifs
);
2789 /* Push an if_stack entry for a preprocessor conditional, and set
2790 pfile->state.skipping to SKIP. If TYPE indicates the conditional
2791 is #if or #ifndef, CMACRO is a potentially controlling macro, and
2792 we need to check here that we are at the top of the file. */
2794 push_conditional (cpp_reader
*pfile
, int skip
, int type
,
2795 const cpp_hashnode
*cmacro
)
2797 struct if_stack
*ifs
;
2798 cpp_buffer
*buffer
= pfile
->buffer
;
2800 ifs
= XOBNEW (&pfile
->buffer_ob
, struct if_stack
);
2801 ifs
->line
= pfile
->directive_line
;
2803 ifs
->next
= buffer
->if_stack
;
2804 ifs
->skip_elses
= pfile
->state
.skipping
|| !skip
;
2805 ifs
->was_skipping
= pfile
->state
.skipping
;
2807 /* This condition is effectively a test for top-of-file. */
2808 if (pfile
->mi_valid
&& pfile
->mi_cmacro
== 0)
2809 ifs
->mi_cmacro
= cmacro
;
2812 ifs
->mi_def_cmacro
= nullptr;
2814 pfile
->state
.skipping
= skip
;
2815 buffer
->if_stack
= ifs
;
2818 /* Read the tokens of the answer into the macro pool, in a directive
2819 of type TYPE. Only commit the memory if we intend it as permanent
2820 storage, i.e. the #assert case. Returns 0 on success, and sets
2821 ANSWERP to point to the answer. PRED_LOC is the location of the
2824 parse_answer (cpp_reader
*pfile
, int type
, location_t pred_loc
,
2825 cpp_macro
**answer_ptr
)
2827 /* In a conditional, it is legal to not have an open paren. We
2828 should save the following token in this case. */
2829 const cpp_token
*paren
= cpp_get_token (pfile
);
2831 /* If not a paren, see if we're OK. */
2832 if (paren
->type
!= CPP_OPEN_PAREN
)
2834 /* In a conditional no answer is a test for any answer. It
2835 could be followed by any token. */
2838 _cpp_backup_tokens (pfile
, 1);
2842 /* #unassert with no answer is valid - it removes all answers. */
2843 if (type
== T_UNASSERT
&& paren
->type
== CPP_EOF
)
2846 cpp_error_with_line (pfile
, CPP_DL_ERROR
, pred_loc
, 0,
2847 "missing %<(%> after predicate");
2851 cpp_macro
*answer
= _cpp_new_macro (pfile
, cmk_assert
,
2852 _cpp_reserve_room (pfile
, 0,
2853 sizeof (cpp_macro
)));
2854 answer
->parm
.next
= NULL
;
2858 const cpp_token
*token
= cpp_get_token (pfile
);
2860 if (token
->type
== CPP_CLOSE_PAREN
)
2863 if (token
->type
== CPP_EOF
)
2865 cpp_error (pfile
, CPP_DL_ERROR
, "missing %<)%> to complete answer");
2869 answer
= (cpp_macro
*)_cpp_reserve_room
2870 (pfile
, sizeof (cpp_macro
) + count
* sizeof (cpp_token
),
2871 sizeof (cpp_token
));
2872 answer
->exp
.tokens
[count
++] = *token
;
2877 cpp_error (pfile
, CPP_DL_ERROR
, "predicate%'s answer is empty");
2881 /* Drop whitespace at start, for answer equivalence purposes. */
2882 answer
->exp
.tokens
[0].flags
&= ~PREV_WHITE
;
2884 answer
->count
= count
;
2885 *answer_ptr
= answer
;
2890 /* Parses an assertion directive of type TYPE, returning a pointer to
2891 the hash node of the predicate, or 0 on error. The node is
2892 guaranteed to be disjoint from the macro namespace, so can only
2893 have type 'NT_VOID'. If an answer was supplied, it is placed in
2894 *ANSWER_PTR, which is otherwise set to 0. */
2895 static cpp_hashnode
*
2896 parse_assertion (cpp_reader
*pfile
, int type
, cpp_macro
**answer_ptr
)
2898 cpp_hashnode
*result
= 0;
2900 /* We don't expand predicates or answers. */
2901 pfile
->state
.prevent_expansion
++;
2905 const cpp_token
*predicate
= cpp_get_token (pfile
);
2906 if (predicate
->type
== CPP_EOF
)
2907 cpp_error (pfile
, CPP_DL_ERROR
, "assertion without predicate");
2908 else if (predicate
->type
!= CPP_NAME
)
2909 cpp_error_with_line (pfile
, CPP_DL_ERROR
, predicate
->src_loc
, 0,
2910 "predicate must be an identifier");
2911 else if (parse_answer (pfile
, type
, predicate
->src_loc
, answer_ptr
))
2913 unsigned int len
= NODE_LEN (predicate
->val
.node
.node
);
2914 unsigned char *sym
= (unsigned char *) alloca (len
+ 1);
2916 /* Prefix '#' to get it out of macro namespace. */
2918 memcpy (sym
+ 1, NODE_NAME (predicate
->val
.node
.node
), len
);
2919 result
= cpp_lookup (pfile
, sym
, len
+ 1);
2922 pfile
->state
.prevent_expansion
--;
2927 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
2928 or a pointer to NULL if the answer is not in the chain. */
2930 find_answer (cpp_hashnode
*node
, const cpp_macro
*candidate
)
2933 cpp_macro
**result
= NULL
;
2935 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->parm
.next
)
2937 cpp_macro
*answer
= *result
;
2939 if (answer
->count
== candidate
->count
)
2941 for (i
= 0; i
< answer
->count
; i
++)
2942 if (!_cpp_equiv_tokens (&answer
->exp
.tokens
[i
],
2943 &candidate
->exp
.tokens
[i
]))
2946 if (i
== answer
->count
)
2954 /* Test an assertion within a preprocessor conditional. Returns
2955 nonzero on failure, zero on success. On success, the result of
2956 the test is written into VALUE, otherwise the value 0. */
2958 _cpp_test_assertion (cpp_reader
*pfile
, unsigned int *value
)
2961 cpp_hashnode
*node
= parse_assertion (pfile
, T_IF
, &answer
);
2963 /* For recovery, an erroneous assertion expression is handled as a
2964 failing assertion. */
2969 if (node
->value
.answers
)
2970 *value
= !answer
|| *find_answer (node
, answer
);
2972 else if (pfile
->cur_token
[-1].type
== CPP_EOF
)
2973 _cpp_backup_tokens (pfile
, 1);
2975 /* We don't commit the memory for the answer - it's temporary only. */
2979 /* Handle #assert. */
2981 do_assert (cpp_reader
*pfile
)
2984 cpp_hashnode
*node
= parse_assertion (pfile
, T_ASSERT
, &answer
);
2988 /* Place the new answer in the answer list. First check there
2989 is not a duplicate. */
2990 if (*find_answer (node
, answer
))
2992 cpp_error (pfile
, CPP_DL_WARNING
, "%qs re-asserted",
2993 NODE_NAME (node
) + 1);
2997 /* Commit or allocate storage for the answer. */
2998 answer
= (cpp_macro
*)_cpp_commit_buff
2999 (pfile
, sizeof (cpp_macro
) - sizeof (cpp_token
)
3000 + sizeof (cpp_token
) * answer
->count
);
3002 /* Chain into the list. */
3003 answer
->parm
.next
= node
->value
.answers
;
3004 node
->value
.answers
= answer
;
3006 check_eol (pfile
, false);
3010 /* Handle #unassert. */
3012 do_unassert (cpp_reader
*pfile
)
3015 cpp_hashnode
*node
= parse_assertion (pfile
, T_UNASSERT
, &answer
);
3017 /* It isn't an error to #unassert something that isn't asserted. */
3022 cpp_macro
**p
= find_answer (node
, answer
);
3024 /* Remove the assert from the list. */
3025 if (cpp_macro
*temp
= *p
)
3026 *p
= temp
->parm
.next
;
3028 check_eol (pfile
, false);
3031 _cpp_free_definition (node
);
3034 /* We don't commit the memory for the answer - it's temporary only. */
3037 /* These are for -D, -U, -A. */
3039 /* Process the string STR as if it appeared as the body of a #define.
3040 If STR is just an identifier, define it with value 1.
3041 If STR has anything after the identifier, then it should
3042 be identifier=definition. */
3044 cpp_define (cpp_reader
*pfile
, const char *str
)
3050 /* Copy the entire option so we can modify it.
3051 Change the first "=" in the string to a space. If there is none,
3052 tack " 1" on the end. */
3054 count
= strlen (str
);
3055 buf
= (char *) alloca (count
+ 3);
3056 memcpy (buf
, str
, count
);
3058 p
= strchr (str
, '=');
3068 run_directive (pfile
, T_DEFINE
, buf
, count
);
3071 /* Like cpp_define, but does not warn about unused macro. */
3073 cpp_define_unused (cpp_reader
*pfile
, const char *str
)
3075 unsigned char warn_unused_macros
= CPP_OPTION (pfile
, warn_unused_macros
);
3076 CPP_OPTION (pfile
, warn_unused_macros
) = 0;
3077 cpp_define (pfile
, str
);
3078 CPP_OPTION (pfile
, warn_unused_macros
) = warn_unused_macros
;
3081 /* Use to build macros to be run through cpp_define() as
3083 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
3086 cpp_define_formatted (cpp_reader
*pfile
, const char *fmt
, ...)
3092 ptr
= xvasprintf (fmt
, ap
);
3095 cpp_define (pfile
, ptr
);
3099 /* Like cpp_define_formatted, but does not warn about unused macro. */
3101 cpp_define_formatted_unused (cpp_reader
*pfile
, const char *fmt
, ...)
3107 ptr
= xvasprintf (fmt
, ap
);
3110 cpp_define_unused (pfile
, ptr
);
3114 /* Slight variant of the above for use by initialize_builtins. */
3116 _cpp_define_builtin (cpp_reader
*pfile
, const char *str
)
3118 size_t len
= strlen (str
);
3119 char *buf
= (char *) alloca (len
+ 1);
3120 memcpy (buf
, str
, len
);
3122 run_directive (pfile
, T_DEFINE
, buf
, len
);
3125 /* Process MACRO as if it appeared as the body of an #undef. */
3127 cpp_undef (cpp_reader
*pfile
, const char *macro
)
3129 size_t len
= strlen (macro
);
3130 char *buf
= (char *) alloca (len
+ 1);
3131 memcpy (buf
, macro
, len
);
3133 run_directive (pfile
, T_UNDEF
, buf
, len
);
3136 /* Replace a previous definition DEF of the macro STR. If DEF is NULL,
3137 or first element is zero, then the macro should be undefined. */
3139 cpp_pop_definition (cpp_reader
*pfile
, def_pragma_macro
*c
, cpp_hashnode
*node
)
3141 if (pfile
->cb
.before_define
)
3142 pfile
->cb
.before_define (pfile
);
3144 if (cpp_macro_p (node
))
3146 if (pfile
->cb
.undef
)
3147 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
3148 if (CPP_OPTION (pfile
, warn_unused_macros
))
3149 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
3150 _cpp_free_definition (node
);
3157 _cpp_restore_special_builtin (pfile
, c
);
3162 const auto namelen
= ustrcspn (c
->definition
, "( \n");
3163 const auto dn
= c
->definition
+ namelen
;
3164 const auto nbuf
= cpp_push_buffer (pfile
, dn
, ustrchr (dn
, '\n') - dn
,
3168 _cpp_clean_line (pfile
);
3170 if (!_cpp_create_definition (pfile
, node
, 0))
3172 _cpp_pop_buffer (pfile
);
3176 node
->value
.macro
->line
= c
->line
;
3177 node
->value
.macro
->syshdr
= c
->syshdr
;
3178 node
->value
.macro
->used
= c
->used
;
3182 /* Process the string STR as if it appeared as the body of a #assert. */
3184 cpp_assert (cpp_reader
*pfile
, const char *str
)
3186 handle_assertion (pfile
, str
, T_ASSERT
);
3189 /* Process STR as if it appeared as the body of an #unassert. */
3191 cpp_unassert (cpp_reader
*pfile
, const char *str
)
3193 handle_assertion (pfile
, str
, T_UNASSERT
);
3196 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
3198 handle_assertion (cpp_reader
*pfile
, const char *str
, int type
)
3200 size_t count
= strlen (str
);
3201 const char *p
= strchr (str
, '=');
3203 /* Copy the entire option so we can modify it. Change the first
3204 "=" in the string to a '(', and tack a ')' on the end. */
3205 char *buf
= (char *) alloca (count
+ 2);
3207 memcpy (buf
, str
, count
);
3216 run_directive (pfile
, type
, str
, count
);
3219 /* The options structure. */
3221 cpp_get_options (cpp_reader
*pfile
)
3223 return &pfile
->opts
;
3226 /* The callbacks structure. */
3228 cpp_get_callbacks (cpp_reader
*pfile
)
3233 /* Copy the given callbacks structure to our own. */
3235 cpp_set_callbacks (cpp_reader
*pfile
, cpp_callbacks
*cb
)
3240 /* The narrow character set identifier. */
3242 cpp_get_narrow_charset_name (cpp_reader
*pfile
)
3244 return pfile
->narrow_cset_desc
.to
;
3247 /* The wide character set identifier. */
3249 cpp_get_wide_charset_name (cpp_reader
*pfile
)
3251 return pfile
->wide_cset_desc
.to
;
3254 /* The dependencies structure. (Creates one if it hasn't already been.) */
3256 cpp_get_deps (cpp_reader
*pfile
)
3258 if (!pfile
->deps
&& CPP_OPTION (pfile
, deps
.style
) != DEPS_NONE
)
3259 pfile
->deps
= deps_init ();
3263 /* Push a new buffer on the buffer stack. Returns the new buffer; it
3264 doesn't fail. It does not generate a file change call back; that
3265 is the responsibility of the caller. */
3267 cpp_push_buffer (cpp_reader
*pfile
, const uchar
*buffer
, size_t len
,
3270 cpp_buffer
*new_buffer
= XOBNEW (&pfile
->buffer_ob
, cpp_buffer
);
3272 /* Clears, amongst other things, if_stack and mi_cmacro. */
3273 memset (new_buffer
, 0, sizeof (cpp_buffer
));
3275 new_buffer
->next_line
= new_buffer
->buf
= buffer
;
3276 new_buffer
->rlimit
= buffer
+ len
;
3277 new_buffer
->from_stage3
= from_stage3
;
3278 new_buffer
->prev
= pfile
->buffer
;
3279 new_buffer
->need_line
= true;
3281 pfile
->buffer
= new_buffer
;
3286 /* Pops a single buffer, with a file change call-back if appropriate.
3287 Then pushes the next -include file, if any remain. */
3289 _cpp_pop_buffer (cpp_reader
*pfile
)
3291 cpp_buffer
*buffer
= pfile
->buffer
;
3292 struct _cpp_file
*inc
= buffer
->file
;
3293 struct if_stack
*ifs
;
3294 const unsigned char *to_free
;
3296 /* Walk back up the conditional stack till we reach its level at
3297 entry to this file, issuing error messages. */
3298 for (ifs
= buffer
->if_stack
; ifs
; ifs
= ifs
->next
)
3299 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
3300 "unterminated #%s", dtable
[ifs
->type
].name
);
3302 /* In case of a missing #endif. */
3303 pfile
->state
.skipping
= 0;
3305 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
3306 pfile
->buffer
= buffer
->prev
;
3308 to_free
= buffer
->to_free
;
3309 free (buffer
->notes
);
3311 /* Free the buffer object now; we may want to push a new buffer
3312 in _cpp_push_next_include_file. */
3313 obstack_free (&pfile
->buffer_ob
, buffer
);
3317 _cpp_pop_file_buffer (pfile
, inc
, to_free
);
3319 _cpp_do_file_change (pfile
, LC_LEAVE
, 0, 0, 0);
3322 free ((void *)to_free
);
3325 /* Enter all recognized directives in the hash table. */
3327 _cpp_init_directives (cpp_reader
*pfile
)
3329 for (int i
= 0; i
< N_DIRECTIVES
; i
++)
3331 cpp_hashnode
*node
= cpp_lookup (pfile
, dtable
[i
].name
, dtable
[i
].length
);
3332 node
->is_directive
= 1;
3333 node
->directive_index
= i
;
3337 /* Extract header file from a bracket include. Parsing starts after '<'.
3338 The string is malloced and must be freed by the caller. */
3340 _cpp_bracket_include(cpp_reader
*pfile
)
3342 return glue_header_name (pfile
);