1/* CPP Library. (Directive handling.)
2 Copyright (C) 1986-2026 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
6
7This program is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by the
9Free Software Foundation; either version 3, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#include "config.h"
22#include "system.h"
23#include "cpplib.h"
24#include "internal.h"
25#include "mkdeps.h"
26#include "obstack.h"
27
28/* Stack of conditionals currently in progress
29 (including both successful and failing conditionals). */
30struct if_stack
31{
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
36 file. */
37 const cpp_hashnode *mi_def_cmacro; /* Macro name in the following
38 #define. */
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. */
42};
43
44/* Contains a registered pragma or pragma namespace. */
45typedef void (*pragma_cb) (cpp_reader *);
46struct pragma_entry
47{
48 struct pragma_entry *next;
49 const cpp_hashnode *pragma; /* Name and length. */
50 bool is_nspace;
51 bool is_internal;
52 bool is_deferred;
53 bool allow_expansion;
54 union {
55 pragma_cb handler;
56 struct pragma_entry *space;
57 unsigned int ident;
58 } u;
59};
60
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. */
65#define KANDR 0
66#define STDC89 1
67#define STDC23 2
68#define EXTENSION 3
69
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).
75
76 EXPAND is set on directives that are always macro-expanded.
77
78 ELIFDEF is set on directives that are only handled for standards with the
79 #elifdef / #elifndef feature. */
80#define COND (1 << 0)
81#define IF_COND (1 << 1)
82#define INCL (1 << 2)
83#define IN_I (1 << 3)
84#define EXPAND (1 << 4)
85#define DEPRECATED (1 << 5)
86#define ELIFDEF (1 << 6)
87
88/* Defines one #-directive, including how to handle it. */
89typedef void (*directive_handler) (cpp_reader *);
90typedef struct directive directive;
91struct directive
92{
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. */
98};
99
100/* Forward declarations. */
101
102static void skip_rest_of_line (cpp_reader *);
103static void check_eol (cpp_reader *, bool);
104static void start_directive (cpp_reader *);
105static void prepare_directive_trad (cpp_reader *);
106static void end_directive (cpp_reader *, int);
107static void directive_diagnostics (cpp_reader *, const directive *, int);
108static void run_directive (cpp_reader *, int, const char *, size_t);
109static char *glue_header_name (cpp_reader *);
110static const char *parse_include (cpp_reader *, int *, const cpp_token ***,
111 location_t *);
112static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
113static unsigned int read_flag (cpp_reader *, unsigned int);
114static bool strtolinenum (const uchar *, size_t, linenum_type *, bool *);
115static void do_diagnostic (cpp_reader *, enum cpp_diagnostic_level code,
116 enum cpp_warning_reason reason, int);
117static cpp_hashnode *lex_macro_node (cpp_reader *, bool);
118static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
119static void do_include_common (cpp_reader *, enum include_type);
120static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
121 const cpp_hashnode *);
122static int count_registered_pragmas (struct pragma_entry *);
123static char ** save_registered_pragmas (struct pragma_entry *, char **);
124static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
125 char **);
126static void do_pragma_once (cpp_reader *);
127static void do_pragma_poison (cpp_reader *);
128static void do_pragma_system_header (cpp_reader *);
129static void do_pragma_dependency (cpp_reader *);
130static void do_pragma_warning_or_error (cpp_reader *, bool error);
131static void do_pragma_warning (cpp_reader *);
132static void do_pragma_error (cpp_reader *);
133static void do_linemarker (cpp_reader *);
134static const cpp_token *get_token_no_padding (cpp_reader *);
135static const cpp_token *get__Pragma_string (cpp_reader *);
136static void destringize_and_run (cpp_reader *, const cpp_string *,
137 location_t);
138static bool parse_answer (cpp_reader *, int, location_t, cpp_macro **);
139static cpp_hashnode *parse_assertion (cpp_reader *, int, cpp_macro **);
140static cpp_macro **find_answer (cpp_hashnode *, const cpp_macro *);
141static void handle_assertion (cpp_reader *, const char *, int);
142static void do_pragma_push_macro (cpp_reader *);
143static void do_pragma_pop_macro (cpp_reader *);
144static void cpp_pop_definition (cpp_reader *, def_pragma_macro *,
145 cpp_hashnode *);
146
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. */
150
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? */
174
175/* #sccs is synonymous with #ident. */
176#define do_sccs do_ident
177
178/* Use the table to generate a series of prototypes, an enum for the
179 directive names, and an array of directive handlers. */
180
181#define D(name, t, o, f) static void do_##name (cpp_reader *);
182DIRECTIVE_TABLE
183#undef D
184
185#define D(n, tag, o, f) tag,
186enum
187{
188 DIRECTIVE_TABLE
189 N_DIRECTIVES
190};
191#undef D
192
193#define D(name, t, origin, flags) \
194{ do_##name, (const uchar *) #name, \
195 sizeof #name - 1, origin, flags },
196static const directive dtable[] =
197{
198DIRECTIVE_TABLE
199};
200#undef D
201
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,
205static const char * const directive_names[] = {
206DIRECTIVE_TABLE
207 NULL
208};
209#undef D
210
211#undef DIRECTIVE_TABLE
212
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. */
216static const directive linemarker_dir =
217{
218 .handler: do_linemarker, UC"#", .length: 1, KANDR, IN_I
219};
220
221/* Skip any remaining tokens in a directive. */
222static void
223skip_rest_of_line (cpp_reader *pfile)
224{
225 /* Discard all stacked contexts. */
226 while (pfile->context->prev)
227 _cpp_pop_context (pfile);
228
229 /* Sweep up all tokens remaining on the line. */
230 if (! SEEN_EOL ())
231 while (_cpp_lex_token (pfile)->type != CPP_EOF)
232 ;
233}
234
235/* Helper function for check_oel. */
236
237static void
238check_eol_1 (cpp_reader *pfile, bool expand, enum cpp_warning_reason reason)
239{
240 if (! SEEN_EOL () && (expand
241 ? cpp_get_token (pfile)
242 : _cpp_lex_token (pfile))->type != CPP_EOF)
243 cpp_pedwarning (pfile, reason, msgid: "extra tokens at end of %<#%s%> directive",
244 pfile->directive->name);
245}
246
247/* Variant of check_eol used for Wendif-labels warnings. */
248
249static void
250check_eol_endif_labels (cpp_reader *pfile)
251{
252 check_eol_1 (pfile, expand: false, reason: CPP_W_ENDIF_LABELS);
253}
254
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. */
257
258static void
259check_eol (cpp_reader *pfile, bool expand)
260{
261 check_eol_1 (pfile, expand, reason: CPP_W_NONE);
262}
263
264/* Ensure there are no stray tokens other than comments at the end of
265 a directive, and gather the comments. */
266static const cpp_token **
267check_eol_return_comments (cpp_reader *pfile)
268{
269 size_t c;
270 size_t capacity = 8;
271 const cpp_token **buf;
272
273 buf = XNEWVEC (const cpp_token *, capacity);
274 c = 0;
275 if (! SEEN_EOL ())
276 {
277 while (1)
278 {
279 const cpp_token *tok;
280
281 tok = _cpp_lex_token (pfile);
282 if (tok->type == CPP_EOF)
283 break;
284 if (tok->type != CPP_COMMENT)
285 cpp_error (pfile, CPP_DL_PEDWARN,
286 msgid: "extra tokens at end of #%s directive",
287 pfile->directive->name);
288 else
289 {
290 if (c + 1 >= capacity)
291 {
292 capacity *= 2;
293 buf = XRESIZEVEC (const cpp_token *, buf, capacity);
294 }
295 buf[c] = tok;
296 ++c;
297 }
298 }
299 }
300 buf[c] = NULL;
301 return buf;
302}
303
304/* Called when entering a directive, _Pragma or command-line directive. */
305static void
306start_directive (cpp_reader *pfile)
307{
308 /* Setup in-directive state. */
309 pfile->state.in_directive = 1;
310 pfile->state.save_comments = 0;
311 pfile->directive_result.type = CPP_PADDING;
312
313 /* Some handlers need the position of the # for diagnostics. */
314 pfile->directive_line = pfile->line_table->highest_line;
315}
316
317/* Called when leaving a directive, _Pragma or command-line directive. */
318static void
319end_directive (cpp_reader *pfile, int skip_line)
320{
321 if (CPP_OPTION (pfile, traditional))
322 {
323 /* Revert change of prepare_directive_trad. */
324 if (!pfile->state.in_deferred_pragma)
325 pfile->state.prevent_expansion--;
326
327 if (pfile->directive != &dtable[T_DEFINE])
328 _cpp_remove_overlay (pfile);
329 }
330 else if (pfile->state.in_deferred_pragma)
331 ;
332 /* We don't skip for an assembler #. */
333 else if (skip_line)
334 {
335 if (pfile->directive != &dtable[T_EMBED])
336 skip_rest_of_line (pfile);
337 if (!pfile->keep_tokens)
338 {
339 pfile->cur_run = &pfile->base_run;
340 pfile->cur_token = pfile->base_run.base;
341 }
342 }
343
344 /* Restore state. */
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;
350}
351
352/* Prepare to handle the directive in pfile->directive. */
353static void
354prepare_directive_trad (cpp_reader *pfile)
355{
356 if (pfile->directive != &dtable[T_DEFINE])
357 {
358 bool no_expand = (pfile->directive
359 && ! (pfile->directive->flags & EXPAND));
360 bool was_skipping = pfile->state.skipping;
361
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;
366
367 if (no_expand)
368 pfile->state.prevent_expansion++;
369 _cpp_scan_out_logical_line (pfile, NULL, false);
370 if (no_expand)
371 pfile->state.prevent_expansion--;
372
373 pfile->state.skipping = was_skipping;
374 _cpp_overlay_buffer (pfile, pfile->out.base,
375 pfile->out.cur - pfile->out.base);
376 }
377
378 /* Stop ISO C from expanding anything. */
379 pfile->state.prevent_expansion++;
380}
381
382/* Output diagnostics for a directive DIR. INDENTED is nonzero if
383 the '#' was indented. */
384static void
385directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
386{
387 /* Issue -pedantic or deprecated warnings for extensions. We let
388 -pedantic take precedence if both are applicable. */
389 if (! pfile->state.skipping)
390 {
391 bool warned = false;
392 if (dir->origin == EXTENSION
393 && !(dir == &dtable[T_IMPORT] && CPP_OPTION (pfile, objc)))
394 warned
395 = cpp_pedwarning (pfile, CPP_W_PEDANTIC,
396 msgid: "%<#%s%> is a GCC extension", dir->name);
397 if (!warned && dir == &dtable[T_WARNING])
398 {
399 if (CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, warning_directive))
400 {
401 if (CPP_OPTION (pfile, cplusplus))
402 warned
403 = cpp_pedwarning (pfile, CPP_W_CXX23_EXTENSIONS,
404 msgid: "%<#%s%> before C++23 is a GCC extension",
405 dir->name);
406 else
407 warned
408 = cpp_pedwarning (pfile, CPP_W_PEDANTIC,
409 msgid: "%<#%s%> before C23 is a GCC extension",
410 dir->name);
411 }
412
413 if (!warned && CPP_OPTION (pfile, cpp_warn_c11_c23_compat) > 0)
414 warned = cpp_warning (pfile, CPP_W_C11_C23_COMPAT,
415 msgid: "%<#%s%> before C23 is a GCC extension",
416 dir->name);
417 }
418
419 if (((dir->flags & DEPRECATED) != 0
420 || (dir == &dtable[T_IMPORT] && !CPP_OPTION (pfile, objc)))
421 && !warned)
422 cpp_warning (pfile, CPP_W_DEPRECATED,
423 msgid: "%<#%s%> is a deprecated GCC extension", dir->name);
424 }
425
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))
433 {
434 if (dir == &dtable[T_ELIF])
435 cpp_warning (pfile, CPP_W_TRADITIONAL,
436 msgid: "suggest not using %<#elif%> in traditional C");
437 else if (indented && dir->origin == KANDR)
438 cpp_warning (pfile, CPP_W_TRADITIONAL,
439 msgid: "traditional C ignores %<#%s%> with the %<#%> indented",
440 dir->name);
441 else if (!indented && dir->origin != KANDR)
442 cpp_warning (pfile, CPP_W_TRADITIONAL,
443 msgid: "suggest hiding %<#%s%> from traditional C with an "
444 "indented %<#%>", dir->name);
445 }
446}
447
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. */
453int
454_cpp_handle_directive (cpp_reader *pfile, bool indented)
455{
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;
460 int skip = 1;
461
462 if (was_discarding_output)
463 pfile->state.prevent_expansion = 0;
464
465 if (was_parsing_args)
466 {
467 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
468 msgid: "embedding a directive within macro arguments is not "
469 "portable");
470 pfile->state.parsing_args = 0;
471 pfile->state.prevent_expansion = 0;
472 }
473 start_directive (pfile);
474 dname = _cpp_lex_token (pfile);
475
476 if (dname->type == CPP_NAME)
477 {
478 if (dname->val.node.node->is_directive)
479 {
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))
486 dir = 0;
487 }
488 }
489 /* We do not recognize the # followed by a number extension in
490 assembler code. */
491 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
492 {
493 dir = &linemarker_dir;
494 if (! CPP_OPTION (pfile, preprocessed)
495 && ! pfile->state.skipping)
496 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
497 msgid: "style of line directive is a GCC extension");
498 }
499
500 if (dir)
501 {
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;
506
507 /* Kluge alert. In order to be sure that code like this
508
509 #define HASH #
510 HASH define foo bar
511
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.
516
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)))
523 {
524 skip = 0;
525 dir = 0;
526 }
527 else
528 {
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))
538 dir = 0;
539 }
540 }
541 else if (dname->type == CPP_EOF)
542 ; /* CPP_EOF is the "null directive". */
543 else
544 {
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)
550 skip = 0;
551 else if (!pfile->state.skipping)
552 {
553 const char *unrecognized
554 = (const char *)cpp_token_as_text (pfile, dname);
555 const char *hint = NULL;
556
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,
563 directive_names);
564
565 if (hint)
566 {
567 rich_location richloc (pfile->line_table, dname->src_loc);
568 source_range misspelled_token_range
569 = get_range_from_loc (set: pfile->line_table, loc: dname->src_loc);
570 richloc.add_fixit_replace (src_range: misspelled_token_range, new_content: hint);
571 cpp_error_at (pfile, CPP_DL_ERROR, richloc: &richloc,
572 msgid: "invalid preprocessing directive #%s;"
573 " did you mean #%s?",
574 unrecognized, hint);
575 }
576 else
577 cpp_error (pfile, CPP_DL_ERROR,
578 msgid: "invalid preprocessing directive #%s",
579 unrecognized);
580 }
581 }
582
583 pfile->directive = dir;
584 if (CPP_OPTION (pfile, traditional))
585 prepare_directive_trad (pfile);
586
587 if (dir)
588 {
589 pfile->directive->handler (pfile);
590 if (pfile->directive == &dtable[T_EMBED]
591 && skip
592 && CPP_OPTION (pfile, directives_only))
593 /* Signal to cpp_directive_only_process it needs to emit
594 the #embed expansion. */
595 skip = 2;
596 }
597 else if (skip == 0)
598 _cpp_backup_tokens (pfile, 1);
599
600 end_directive (pfile, skip_line: skip);
601 if (was_parsing_args && !pfile->state.in_deferred_pragma)
602 {
603 /* Restore state when within macro args. */
604 pfile->state.parsing_args = 2;
605 pfile->state.prevent_expansion = 1;
606 }
607 if (was_discarding_output)
608 pfile->state.prevent_expansion = 1;
609 return skip;
610}
611
612/* Directive handler wrapper used by the command line option
613 processor. BUF is \n terminated. */
614static void
615run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
616{
617 cpp_push_buffer (pfile, (const uchar *) buf, count,
618 /* from_stage3 */ true);
619 start_directive (pfile);
620
621 /* This is a short-term fix to prevent a leading '#' being
622 interpreted as a directive. */
623 _cpp_clean_line (pfile);
624
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, skip_line: 1);
630 _cpp_pop_buffer (pfile);
631}
632
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
636 otherwise. */
637static cpp_hashnode *
638lex_macro_node (cpp_reader *pfile, bool is_def_or_undef)
639{
640 const cpp_token *token = _cpp_lex_token (pfile);
641
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.) */
648
649 if (token->type == CPP_NAME)
650 {
651 cpp_hashnode *node = token->val.node.node;
652
653 if (is_def_or_undef
654 && node == pfile->spec_nodes.n_defined)
655 cpp_error (pfile, CPP_DL_ERROR,
656 msgid: "%qs cannot be used as a macro name",
657 NODE_NAME (node));
658 else if (! (node->flags & NODE_POISONED))
659 return node;
660 }
661 else if (token->flags & NAMED_OP)
662 cpp_error (pfile, CPP_DL_ERROR,
663 msgid: "%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, msgid: "no macro name given in %<#%s%> directive",
667 pfile->directive->name);
668 else
669 cpp_error (pfile, CPP_DL_ERROR, msgid: "macro names must be identifiers");
670
671 return NULL;
672}
673
674/* Process a #define directive. Most work is done in macro.cc. */
675static void
676do_define (cpp_reader *pfile)
677{
678 cpp_hashnode *node = lex_macro_node (pfile, is_def_or_undef: true);
679
680 if (node)
681 {
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);
685
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);
690
691 if (pfile->cb.before_define)
692 pfile->cb.before_define (pfile);
693
694 if (_cpp_create_definition (pfile, node, name_loc))
695 if (pfile->cb.define)
696 pfile->cb.define (pfile, pfile->directive_line, node);
697
698 node->flags &= ~NODE_USED;
699
700 if (pfile->mi_valid
701 && !pfile->mi_cmacro
702 && CPP_OPTION (pfile, warn_header_guard)
703 && node->type == NT_USER_MACRO
704 && node->value.macro
705 && node->value.macro->count == 0
706 && !node->value.macro->fun_like)
707 {
708 cpp_buffer *buffer = pfile->buffer;
709 struct if_stack *ifs = buffer->if_stack;
710 if (ifs
711 && !ifs->next
712 && ifs->mi_cmacro
713 && node != ifs->mi_cmacro)
714 {
715 ifs->mi_def_cmacro = node;
716 ifs->def_loc = pfile->directive_line;
717 }
718 }
719 }
720 pfile->mi_valid = false;
721}
722
723/* Handle #undef. Mark the identifier NT_VOID in the hash table. */
724static void
725do_undef (cpp_reader *pfile)
726{
727 cpp_hashnode *node = lex_macro_node (pfile, is_def_or_undef: true);
728
729 if (node)
730 {
731 if (pfile->cb.before_define)
732 pfile->cb.before_define (pfile);
733
734 if (pfile->cb.undef)
735 pfile->cb.undef (pfile, pfile->directive_line, node);
736
737 /* Handle -Wkeyword-macro registered identifiers. */
738 bool diagnosed = false;
739 if (CPP_OPTION (pfile, cpp_warn_keyword_macro)
740 && !CPP_OPTION (pfile, suppress_builtin_macro_warnings)
741 && cpp_keyword_p (node))
742 {
743 if (CPP_OPTION (pfile, cplusplus)
744 && (strcmp (s1: (const char *) NODE_NAME (node), s2: "likely") == 0
745 || strcmp (s1: (const char *) NODE_NAME (node),
746 s2: "unlikely") == 0))
747 /* CWG3053: likely and unlikely can be undefined. */;
748 else if (CPP_OPTION (pfile, cpp_pedantic)
749 && CPP_OPTION (pfile, cplusplus)
750 && CPP_OPTION (pfile, lang) >= CLK_GNUCXX26)
751 cpp_pedwarning (pfile, CPP_W_KEYWORD_MACRO,
752 msgid: "undefining keyword %qs", NODE_NAME (node));
753 else
754 cpp_warning (pfile, CPP_W_KEYWORD_MACRO,
755 msgid: "undefining keyword %qs", NODE_NAME (node));
756 diagnosed = true;
757 }
758 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
759 identifier is not currently defined as a macro name. */
760 if (cpp_macro_p (node))
761 {
762 if ((node->flags & NODE_WARN)
763 && !CPP_OPTION (pfile, suppress_builtin_macro_warnings))
764 {
765 if (!diagnosed)
766 cpp_error (pfile, CPP_DL_WARNING,
767 msgid: "undefining %qs", NODE_NAME (node));
768 }
769 else if (cpp_builtin_macro_p (node)
770 && CPP_OPTION (pfile, warn_builtin_macro_redefined))
771 cpp_warning (pfile, CPP_W_BUILTIN_MACRO_REDEFINED,
772 msgid: "undefining %qs", NODE_NAME (node));
773
774 if (node->value.macro
775 && CPP_OPTION (pfile, warn_unused_macros))
776 _cpp_warn_if_unused_macro (pfile, node, NULL);
777
778 _cpp_free_definition (node);
779 }
780 else if ((node->flags & NODE_WARN)
781 && !CPP_OPTION (pfile, suppress_builtin_macro_warnings)
782 && !diagnosed
783 && !cpp_keyword_p (node))
784 cpp_error (pfile, CPP_DL_WARNING, msgid: "undefining %qs", NODE_NAME (node));
785 }
786
787 check_eol (pfile, expand: false);
788}
789
790/* Undefine a single macro/assertion/whatever. */
791
792static int
793undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
794 void *data_p ATTRIBUTE_UNUSED)
795{
796 /* Body of _cpp_free_definition inlined here for speed.
797 Macros and assertions no longer have anything to free. */
798 h->type = NT_VOID;
799 h->value.answers = NULL;
800 h->flags &= ~(NODE_POISONED|NODE_DISABLED|NODE_USED);
801 return 1;
802}
803
804/* Undefine all macros and assertions. */
805
806void
807cpp_undef_all (cpp_reader *pfile)
808{
809 cpp_forall_identifiers (pfile, undefine_macros, NULL);
810}
811
812
813/* Helper routine used by parse_include. Reinterpret the current line
814 as an h-char-sequence (< ... >); we are looking at the first token
815 after the <. Returns a malloced filename. */
816static char *
817glue_header_name (cpp_reader *pfile)
818{
819 const cpp_token *token;
820 char *buffer;
821 size_t len, total_len = 0, capacity = 1024;
822
823 /* To avoid lexed tokens overwriting our glued name, we can only
824 allocate from the string pool once we've lexed everything. */
825 buffer = XNEWVEC (char, capacity);
826 for (;;)
827 {
828 token = get_token_no_padding (pfile);
829
830 if (token->type == CPP_GREATER)
831 break;
832 if (token->type == CPP_EOF)
833 {
834 cpp_error (pfile, CPP_DL_ERROR,
835 msgid: "missing terminating %<>%> character");
836 break;
837 }
838
839 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
840 if (total_len + len > capacity)
841 {
842 capacity = (capacity + len) * 2;
843 buffer = XRESIZEVEC (char, buffer, capacity);
844 }
845
846 if (token->flags & PREV_WHITE)
847 buffer[total_len++] = ' ';
848
849 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
850 true)
851 - (uchar *) buffer);
852 }
853
854 buffer[total_len] = '\0';
855 return buffer;
856}
857
858/* Returns the file name of #include, #include_next, #import and
859 #pragma dependency. The string is malloced and the caller should
860 free it. Returns NULL on error. LOCATION is the source location
861 of the file name. */
862
863static const char *
864parse_include (cpp_reader *pfile, int *pangle_brackets,
865 const cpp_token ***buf, location_t *location)
866{
867 char *fname;
868 const cpp_token *header;
869
870 /* Allow macro expansion. */
871 header = get_token_no_padding (pfile);
872 *location = header->src_loc;
873 if ((header->type == CPP_STRING && header->val.str.text[0] != 'R')
874 || header->type == CPP_HEADER_NAME)
875 {
876 fname = XNEWVEC (char, header->val.str.len - 1);
877 memcpy (dest: fname, src: header->val.str.text + 1, n: header->val.str.len - 2);
878 fname[header->val.str.len - 2] = '\0';
879 *pangle_brackets = header->type == CPP_HEADER_NAME;
880 }
881 else if (header->type == CPP_LESS)
882 {
883 fname = glue_header_name (pfile);
884 *pangle_brackets = 1;
885 }
886 else
887 {
888 const unsigned char *dir;
889
890 if (pfile->directive == &dtable[T_PRAGMA])
891 dir = UC"pragma GCC dependency";
892 else
893 dir = pfile->directive->name;
894 cpp_error (pfile, CPP_DL_ERROR,
895 msgid: "%<#%s%> expects %<\"FILENAME\"%> or %<<FILENAME>%>", dir);
896
897 return NULL;
898 }
899
900 if (pfile->directive == &dtable[T_PRAGMA]
901 || pfile->directive == &dtable[T_EMBED])
902 {
903 /* This pragma or #embed allows extra tokens after the file name. */
904 }
905 else if (buf == NULL || CPP_OPTION (pfile, discard_comments))
906 check_eol (pfile, expand: true);
907 else
908 {
909 /* If we are not discarding comments, then gather them while
910 doing the eol check. */
911 *buf = check_eol_return_comments (pfile);
912 }
913
914 return fname;
915}
916
917/* Handle #include, #include_next and #import. */
918static void
919do_include_common (cpp_reader *pfile, enum include_type type)
920{
921 const char *fname;
922 int angle_brackets;
923 const cpp_token **buf = NULL;
924 location_t location;
925
926 /* Re-enable saving of comments if requested, so that the include
927 callback can dump comments which follow #include. */
928 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
929
930 /* Tell the lexer this is an include directive -- we want it to
931 increment the line number even if this is the last line of a file. */
932 pfile->state.in_directive = 2;
933
934 fname = parse_include (pfile, pangle_brackets: &angle_brackets, buf: &buf, location: &location);
935 if (!fname)
936 goto done;
937
938 if (!*fname)
939 {
940 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
941 msgid: "empty filename in #%s",
942 pfile->directive->name);
943 goto done;
944 }
945
946 /* Prevent #include recursion. */
947 if (pfile->line_table->depth >= CPP_OPTION (pfile, max_include_depth))
948 cpp_error (pfile,
949 CPP_DL_ERROR,
950 msgid: "%<#include%> nested depth %u exceeds maximum of %u"
951 " (use %<-fmax-include-depth=DEPTH%> to increase the maximum)",
952 pfile->line_table->depth,
953 CPP_OPTION (pfile, max_include_depth));
954 else
955 {
956 /* Get out of macro context, if we are. */
957 skip_rest_of_line (pfile);
958
959 if (pfile->cb.include)
960 pfile->cb.include (pfile, pfile->directive_line,
961 pfile->directive->name, fname, angle_brackets,
962 buf);
963
964 _cpp_stack_include (pfile, fname, angle_brackets, type, location);
965 }
966
967 done:
968 XDELETEVEC (fname);
969 if (buf)
970 XDELETEVEC (buf);
971}
972
973static void
974do_include (cpp_reader *pfile)
975{
976 do_include_common (pfile, type: IT_INCLUDE);
977}
978
979static void
980do_import (cpp_reader *pfile)
981{
982 do_include_common (pfile, type: IT_IMPORT);
983}
984
985static void
986do_include_next (cpp_reader *pfile)
987{
988 enum include_type type = IT_INCLUDE_NEXT;
989
990 /* If this is the primary source file, warn and use the normal
991 search logic. */
992 if (_cpp_in_main_source_file (pfile))
993 {
994 cpp_error (pfile, CPP_DL_WARNING,
995 msgid: "%<#include_next%> in primary source file");
996 type = IT_INCLUDE;
997 }
998 do_include_common (pfile, type);
999}
1000
1001/* Helper function for skip_balanced_token_seq and _cpp_parse_embed_params.
1002 Save one token *TOKEN into *SAVE. */
1003
1004static void
1005save_token_for_embed (cpp_embed_params_tokens *save, const cpp_token *token)
1006{
1007 if (save->count == 0)
1008 {
1009 _cpp_init_tokenrun (&save->base_run, 4);
1010 save->cur_run = &save->base_run;
1011 save->cur_token = save->base_run.base;
1012 }
1013 else if (save->cur_token == save->cur_run->limit)
1014 {
1015 save->cur_run->next = XNEW (tokenrun);
1016 save->cur_run->next->prev = save->cur_run;
1017 _cpp_init_tokenrun (save->cur_run->next, 4);
1018 save->cur_run = save->cur_run->next;
1019 save->cur_token = save->cur_run->base;
1020 }
1021 *save->cur_token = *token;
1022 save->cur_token->flags |= NO_EXPAND;
1023 save->cur_token++;
1024 save->count++;
1025}
1026
1027/* Free memory associated with saved tokens in *SAVE. */
1028
1029void
1030_cpp_free_embed_params_tokens (cpp_embed_params_tokens *save)
1031{
1032 if (save->count == 0)
1033 return;
1034 tokenrun *n;
1035 for (tokenrun *t = &save->base_run; t; t = n)
1036 {
1037 n = t->next;
1038 XDELETEVEC (t->base);
1039 if (t != &save->base_run)
1040 XDELETE (t);
1041 }
1042 save->count = 0;
1043}
1044
1045/* Skip over balanced preprocessing tokens until END is found.
1046 If SAVE is non-NULL, remember the parsed tokens in it. NESTED is
1047 false in the outermost invocation of the function and true
1048 when called recursively. */
1049
1050static void
1051skip_balanced_token_seq (cpp_reader *pfile, cpp_ttype end,
1052 cpp_embed_params_tokens *save, bool nested)
1053{
1054 do
1055 {
1056 const cpp_token *token = cpp_peek_token (pfile, 0);
1057 if (token->type == CPP_EOF)
1058 {
1059 char c = 0;
1060 switch (end)
1061 {
1062 case CPP_CLOSE_PAREN: c = '('; break;
1063 case CPP_CLOSE_SQUARE: c = '['; break;
1064 case CPP_CLOSE_BRACE: c = '{'; break;
1065 default: abort ();
1066 }
1067 cpp_error (pfile, CPP_DL_ERROR, msgid: "unbalanced '%c'", c);
1068 return;
1069 }
1070 token = cpp_get_token (pfile);
1071 if (save
1072 && (token->type != CPP_PADDING || save->count)
1073 && (token->type != end || nested))
1074 save_token_for_embed (save, token);
1075 if (token->type == end)
1076 return;
1077 switch (token->type)
1078 {
1079 case CPP_OPEN_PAREN:
1080 skip_balanced_token_seq (pfile, end: CPP_CLOSE_PAREN, save, nested: true);
1081 break;
1082 case CPP_OPEN_SQUARE:
1083 skip_balanced_token_seq (pfile, end: CPP_CLOSE_SQUARE, save, nested: true);
1084 break;
1085 case CPP_OPEN_BRACE:
1086 skip_balanced_token_seq (pfile, end: CPP_CLOSE_BRACE, save, nested: true);
1087 break;
1088 case CPP_CLOSE_PAREN:
1089 cpp_error (pfile, CPP_DL_ERROR, msgid: "unbalanced '%c'", ')');
1090 break;
1091 case CPP_CLOSE_SQUARE:
1092 cpp_error (pfile, CPP_DL_ERROR, msgid: "unbalanced '%c'", ']');
1093 break;
1094 case CPP_CLOSE_BRACE:
1095 cpp_error (pfile, CPP_DL_ERROR, msgid: "unbalanced '%c'", '}');
1096 break;
1097 default:
1098 break;
1099 }
1100 }
1101 while (1);
1102}
1103
1104#define EMBED_PARAMS \
1105 EMBED_PARAM (LIMIT, "limit") \
1106 EMBED_PARAM (PREFIX, "prefix") \
1107 EMBED_PARAM (SUFFIX, "suffix") \
1108 EMBED_PARAM (IF_EMPTY, "if_empty") \
1109 EMBED_PARAM (GNU_BASE64, "base64") \
1110 EMBED_PARAM (GNU_OFFSET, "offset")
1111
1112enum embed_param_kind {
1113#define EMBED_PARAM(c, s) EMBED_PARAM_##c,
1114 EMBED_PARAMS
1115#undef EMBED_PARAM
1116 NUM_EMBED_PARAMS,
1117 NUM_EMBED_STD_PARAMS = EMBED_PARAM_IF_EMPTY + 1
1118};
1119
1120static struct { int len; const char *name; } embed_params[NUM_EMBED_PARAMS] = {
1121#define EMBED_PARAM(c, s) { sizeof (s) - 1, s },
1122 EMBED_PARAMS
1123#undef EMBED_PARAM
1124};
1125
1126/* Parse parameters of #embed directive or __has_embed expression.
1127 Fills in details about parsed parameters in *PARAMS.
1128 Returns true if all the parameters have been successfully parsed,
1129 false on errors. */
1130
1131bool
1132_cpp_parse_embed_params (cpp_reader *pfile, struct cpp_embed_params *params)
1133{
1134 const cpp_token *token = _cpp_get_token_no_padding (pfile);
1135 bool ret = true;
1136 int seen = 0;
1137 params->limit = -1;
1138 do
1139 {
1140 const unsigned char *param_name = NULL;
1141 const unsigned char *param_prefix = NULL;
1142 int param_name_len = 0, param_prefix_len = 0;
1143 bool has_scope = false;
1144 if (token->type != CPP_NAME)
1145 {
1146 if (token->type == CPP_EOF)
1147 {
1148 if (params->has_embed)
1149 {
1150 cpp_error (pfile, CPP_DL_ERROR, msgid: "expected %<)%>");
1151 return false;
1152 }
1153 }
1154 else if (token->type != CPP_CLOSE_PAREN || !params->has_embed)
1155 {
1156 cpp_error (pfile, CPP_DL_ERROR, msgid: "expected parameter name");
1157 return false;
1158 }
1159 if (params->base64.count
1160 && (seen & ((1 << EMBED_PARAM_LIMIT)
1161 | (1 << EMBED_PARAM_GNU_OFFSET))) != 0)
1162 {
1163 ret = false;
1164 if (!params->has_embed)
1165 cpp_error_with_line (pfile, CPP_DL_ERROR,
1166 params->base64.base_run.base->src_loc, 0,
1167 msgid: "%<gnu::base64%> parameter conflicts "
1168 "with %<limit%> or %<gnu::offset%> "
1169 "parameters");
1170 }
1171 else if (params->base64.count == 0
1172 && CPP_OPTION (pfile, preprocessed))
1173 {
1174 ret = false;
1175 if (!params->has_embed)
1176 cpp_error_with_line (pfile, CPP_DL_ERROR, params->loc, 0,
1177 msgid: "%<gnu::base64%> parameter required in "
1178 "preprocessed source");
1179 }
1180 return ret;
1181 }
1182 param_name = NODE_NAME (token->val.node.spelling);
1183 param_name_len = NODE_LEN (token->val.node.spelling);
1184 location_t loc = token->src_loc;
1185 token = _cpp_get_token_no_padding (pfile);
1186 if (token->type == CPP_SCOPE)
1187 {
1188 has_scope = true;
1189 token = _cpp_get_token_no_padding (pfile);
1190 }
1191 else if (token->type == CPP_COLON
1192 && (token->flags & COLON_SCOPE) != 0)
1193 {
1194 has_scope = true;
1195 token = _cpp_get_token_no_padding (pfile);
1196 if (token->type != CPP_COLON)
1197 {
1198 cpp_error (pfile, CPP_DL_ERROR, msgid: "expected %<:%>");
1199 return false;
1200 }
1201 token = _cpp_get_token_no_padding (pfile);
1202 }
1203 if (has_scope)
1204 {
1205 if (token->type != CPP_NAME)
1206 {
1207 cpp_error (pfile, CPP_DL_ERROR, msgid: "expected parameter name");
1208 return false;
1209 }
1210 param_prefix = param_name;
1211 param_prefix_len = param_name_len;
1212 param_name = NODE_NAME (token->val.node.spelling);
1213 param_name_len = NODE_LEN (token->val.node.spelling);
1214 loc = token->src_loc;
1215 token = _cpp_get_token_no_padding (pfile);
1216 }
1217 if (param_name_len > 4
1218 && param_name[0] == '_'
1219 && param_name[1] == '_'
1220 && param_name[param_name_len - 1] == '_'
1221 && param_name[param_name_len - 2] == '_')
1222 {
1223 param_name += 2;
1224 param_name_len -= 4;
1225 }
1226 if (param_prefix
1227 && param_prefix_len > 4
1228 && param_prefix[0] == '_'
1229 && param_prefix[1] == '_'
1230 && param_prefix[param_prefix_len - 1] == '_'
1231 && param_prefix[param_prefix_len - 2] == '_')
1232 {
1233 param_prefix += 2;
1234 param_prefix_len -= 4;
1235 }
1236 size_t param_kind = -1;
1237 if (param_prefix == NULL)
1238 {
1239 for (size_t i = 0; i < NUM_EMBED_STD_PARAMS; ++i)
1240 if (param_name_len == embed_params[i].len
1241 && memcmp (s1: param_name, s2: embed_params[i].name,
1242 n: param_name_len) == 0)
1243 {
1244 param_kind = i;
1245 break;
1246 }
1247 }
1248 else if (param_prefix_len == 3 && memcmp (s1: param_prefix, s2: "gnu", n: 3) == 0)
1249 {
1250 for (size_t i = NUM_EMBED_STD_PARAMS; i < NUM_EMBED_PARAMS; ++i)
1251 if (param_name_len == embed_params[i].len
1252 && memcmp (s1: param_name, s2: embed_params[i].name,
1253 n: param_name_len) == 0)
1254 {
1255 param_kind = i;
1256 break;
1257 }
1258 }
1259 if (param_kind != (size_t) -1)
1260 {
1261 if ((seen & (1 << param_kind)) == 0)
1262 seen |= 1 << param_kind;
1263 else
1264 cpp_error_with_line (pfile, CPP_DL_ERROR, loc, 0,
1265 msgid: "duplicate embed parameter '%.*s%s%.*s'",
1266 param_prefix_len,
1267 param_prefix
1268 ? (const char *) param_prefix : "",
1269 param_prefix ? "::" : "",
1270 param_name_len, param_name);
1271 }
1272 else
1273 {
1274 ret = false;
1275 if (!params->has_embed)
1276 cpp_error_with_line (pfile, CPP_DL_ERROR, loc, 0,
1277 msgid: "unknown embed parameter '%.*s%s%.*s'",
1278 param_prefix_len,
1279 param_prefix
1280 ? (const char *) param_prefix : "",
1281 param_prefix ? "::" : "",
1282 param_name_len, param_name);
1283 }
1284 if (param_kind != (size_t) -1 && token->type != CPP_OPEN_PAREN)
1285 cpp_error_with_line (pfile, CPP_DL_ERROR, loc, 0,
1286 msgid: "expected %<(%>");
1287 else if (param_kind == EMBED_PARAM_LIMIT
1288 || param_kind == EMBED_PARAM_GNU_OFFSET)
1289 {
1290 if (params->has_embed && pfile->op_stack == NULL)
1291 _cpp_expand_op_stack (pfile);
1292 cpp_num_part res = _cpp_parse_expr (pfile, "#embed", token);
1293 if (param_kind == EMBED_PARAM_LIMIT)
1294 params->limit = res;
1295 else
1296 {
1297 if (res > INTTYPE_MAXIMUM (off_t))
1298 cpp_error_with_line (pfile, CPP_DL_ERROR, loc, 0,
1299 msgid: "too large %<gnu::offset%> argument");
1300 else
1301 params->offset = res;
1302 }
1303 token = _cpp_get_token_no_padding (pfile);
1304 }
1305 else if (param_kind == EMBED_PARAM_GNU_BASE64)
1306 {
1307 token = _cpp_get_token_no_padding (pfile);
1308 while (token->type == CPP_OTHER
1309 && CPP_OPTION (pfile, preprocessed)
1310 && !CPP_OPTION (pfile, directives_only)
1311 && token->val.str.len == 1
1312 && token->val.str.text[0] == '\\')
1313 {
1314 /* Allow backslash newline inside of gnu::base64 argument
1315 for -fpreprocessed, so that it doesn't have to be
1316 megabytes long line. */
1317 pfile->state.in_directive = 0;
1318 token = _cpp_get_token_no_padding (pfile);
1319 pfile->state.in_directive = 3;
1320 }
1321 if (token->type == CPP_STRING)
1322 {
1323 do
1324 {
1325 save_token_for_embed (save: &params->base64, token);
1326 token = _cpp_get_token_no_padding (pfile);
1327 while (token->type == CPP_OTHER
1328 && CPP_OPTION (pfile, preprocessed)
1329 && !CPP_OPTION (pfile, directives_only)
1330 && token->val.str.len == 1
1331 && token->val.str.text[0] == '\\')
1332 {
1333 pfile->state.in_directive = 0;
1334 token = _cpp_get_token_no_padding (pfile);
1335 pfile->state.in_directive = 3;
1336 }
1337 }
1338 while (token->type == CPP_STRING);
1339 if (token->type != CPP_CLOSE_PAREN)
1340 cpp_error_with_line (pfile, CPP_DL_ERROR, token->src_loc, 0,
1341 msgid: "expected %<)%>");
1342 }
1343 else
1344 {
1345 cpp_error_with_line (pfile, CPP_DL_ERROR, token->src_loc, 0,
1346 msgid: "expected character string literal");
1347 if (token->type != CPP_CLOSE_PAREN)
1348 token = _cpp_get_token_no_padding (pfile);
1349 }
1350 token = _cpp_get_token_no_padding (pfile);
1351 }
1352 else if (token->type == CPP_OPEN_PAREN)
1353 {
1354 cpp_embed_params_tokens *save = NULL;
1355 auto save_comments = pfile->state.save_comments;
1356 switch (param_kind)
1357 {
1358 case EMBED_PARAM_PREFIX: save = &params->prefix; break;
1359 case EMBED_PARAM_SUFFIX: save = &params->suffix; break;
1360 case EMBED_PARAM_IF_EMPTY: save = &params->if_empty; break;
1361 default: break;
1362 }
1363 if (params->has_embed)
1364 save = NULL;
1365 else if (save)
1366 pfile->state.save_comments = !CPP_OPTION (pfile, discard_comments);
1367 skip_balanced_token_seq (pfile, end: CPP_CLOSE_PAREN, save, nested: false);
1368 pfile->state.save_comments = save_comments;
1369 token = _cpp_get_token_no_padding (pfile);
1370 }
1371 }
1372 while (1);
1373}
1374
1375/* Handle #embed directive. */
1376
1377static void
1378do_embed (cpp_reader *pfile)
1379{
1380 int angle_brackets;
1381 struct cpp_embed_params params = {};
1382 bool ok, warned = false;
1383 const char *fname = NULL;
1384
1385 /* Tell the lexer this is an embed directive. */
1386 pfile->state.in_directive = 3;
1387
1388 if (CPP_OPTION (pfile, traditional))
1389 {
1390 cpp_error (pfile, CPP_DL_ERROR, /* FIXME should be DL_SORRY */
1391 msgid: "%<#embed%> not supported in traditional C");
1392 skip_rest_of_line (pfile);
1393 goto done;
1394 }
1395
1396 if (CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, embed))
1397 {
1398 if (CPP_OPTION (pfile, cplusplus))
1399 warned = cpp_pedwarning (pfile, CPP_W_CXX26_EXTENSIONS,
1400 msgid: "%<#%s%> before C++26 is a GCC extension",
1401 "embed");
1402 else
1403 warned = cpp_pedwarning (pfile, CPP_W_PEDANTIC,
1404 msgid: "%<#%s%> before C23 is a GCC extension",
1405 "embed");
1406 }
1407 if (!warned && CPP_OPTION (pfile, cpp_warn_c11_c23_compat) > 0)
1408 cpp_warning (pfile, CPP_W_C11_C23_COMPAT,
1409 msgid: "%<#%s%> is a C23 feature", "embed");
1410
1411 fname = parse_include (pfile, pangle_brackets: &angle_brackets, NULL, location: &params.loc);
1412 if (!fname)
1413 {
1414 skip_rest_of_line (pfile);
1415 goto done;
1416 }
1417
1418 if (!*fname)
1419 {
1420 cpp_error_with_line (pfile, CPP_DL_ERROR, params.loc, 0,
1421 msgid: "empty filename in #%s",
1422 pfile->directive->name);
1423 skip_rest_of_line (pfile);
1424 goto done;
1425 }
1426
1427 pfile->state.angled_headers = false;
1428 pfile->state.directive_wants_padding = false;
1429 ok = _cpp_parse_embed_params (pfile, params: &params);
1430
1431 /* Get out of macro context, if we are. */
1432 skip_rest_of_line (pfile);
1433
1434 if (ok)
1435 _cpp_stack_embed (pfile, fname, angle_brackets, &params);
1436
1437 _cpp_free_embed_params_tokens (save: &params.prefix);
1438 _cpp_free_embed_params_tokens (save: &params.suffix);
1439 _cpp_free_embed_params_tokens (save: &params.if_empty);
1440 _cpp_free_embed_params_tokens (save: &params.base64);
1441
1442 done:
1443 XDELETEVEC (fname);
1444}
1445
1446/* Subroutine of do_linemarker. Read possible flags after file name.
1447 LAST is the last flag seen; 0 if this is the first flag. Return the
1448 flag if it is valid, 0 at the end of the directive. Otherwise
1449 complain. */
1450static unsigned int
1451read_flag (cpp_reader *pfile, unsigned int last)
1452{
1453 const cpp_token *token = _cpp_lex_token (pfile);
1454
1455 if (token->type == CPP_NUMBER && token->val.str.len == 1)
1456 {
1457 unsigned int flag = token->val.str.text[0] - '0';
1458
1459 if (flag > last && flag <= 4
1460 && (flag != 4 || last == 3)
1461 && (flag != 2 || last == 0))
1462 return flag;
1463 }
1464
1465 if (token->type != CPP_EOF)
1466 cpp_error (pfile, CPP_DL_ERROR, msgid: "invalid flag %qs in line directive",
1467 cpp_token_as_text (pfile, token));
1468 return 0;
1469}
1470
1471/* Subroutine of do_line and do_linemarker. Convert a number in STR,
1472 of length LEN, to binary; store it in NUMP, and return false if the
1473 number was well-formed, true if not. WRAPPED is set to true if the
1474 number did not fit into 'linenum_type'. */
1475static bool
1476strtolinenum (const uchar *str, size_t len, linenum_type *nump, bool *wrapped)
1477{
1478 linenum_type reg = 0;
1479
1480 uchar c;
1481 bool seen_digit_sep = false;
1482 *wrapped = false;
1483 while (len--)
1484 {
1485 c = *str++;
1486 if (!seen_digit_sep && c == '\'' && len)
1487 {
1488 seen_digit_sep = true;
1489 continue;
1490 }
1491 if (!ISDIGIT (c))
1492 return true;
1493 seen_digit_sep = false;
1494 if (reg > ((linenum_type) -1) / 10)
1495 *wrapped = true;
1496 reg *= 10;
1497 if (reg > ((linenum_type) -1) - (c - '0'))
1498 *wrapped = true;
1499 reg += c - '0';
1500 }
1501 *nump = reg;
1502 return false;
1503}
1504
1505/* Interpret #line command.
1506 Note that the filename string (if any) is a true string constant
1507 (escapes are interpreted). */
1508static void
1509do_line (cpp_reader *pfile)
1510{
1511 class line_maps *line_table = pfile->line_table;
1512 const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set: line_table);
1513
1514 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
1515 sysp right now. */
1516
1517 unsigned char map_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (ord_map: map);
1518 const cpp_token *token;
1519 const char *new_file = ORDINARY_MAP_FILE_NAME (ord_map: map);
1520 linenum_type new_lineno;
1521
1522 /* C99 raised the minimum limit on #line numbers. */
1523 linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
1524 bool wrapped;
1525
1526 /* #line commands expand macros. */
1527 token = cpp_get_token (pfile);
1528 if (token->type != CPP_NUMBER
1529 || strtolinenum (str: token->val.str.text, len: token->val.str.len,
1530 nump: &new_lineno, wrapped: &wrapped))
1531 {
1532 if (token->type == CPP_EOF)
1533 cpp_error (pfile, CPP_DL_ERROR,
1534 msgid: "unexpected end of file after %<#line%>");
1535 else
1536 cpp_error (pfile, CPP_DL_ERROR,
1537 msgid: "%qs after %<#line%> is not a positive integer",
1538 cpp_token_as_text (pfile, token));
1539 return;
1540 }
1541
1542 if ((new_lineno == 0 || new_lineno > cap || wrapped)
1543 && cpp_pedwarning (pfile, CPP_W_PEDANTIC, msgid: "line number out of range"))
1544 ;
1545 else if (wrapped)
1546 cpp_error (pfile, CPP_DL_WARNING, msgid: "line number out of range");
1547
1548 token = cpp_get_token (pfile);
1549 if (token->type == CPP_STRING)
1550 {
1551 cpp_string s = { .len: 0, .text: 0 };
1552 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
1553 &s, CPP_STRING))
1554 new_file = (const char *)s.text;
1555 check_eol (pfile, expand: true);
1556 }
1557 else if (token->type != CPP_EOF)
1558 {
1559 cpp_error (pfile, CPP_DL_ERROR, msgid: "%qs is not a valid filename",
1560 cpp_token_as_text (pfile, token));
1561 return;
1562 }
1563
1564 skip_rest_of_line (pfile);
1565 _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, new_file, new_lineno,
1566 map_sysp);
1567 line_table->seen_line_directive = true;
1568}
1569
1570/* Interpret the # 44 "file" [flags] notation, which has slightly
1571 different syntax and semantics from #line: Flags are allowed,
1572 and we never complain about the line number being too big. */
1573static void
1574do_linemarker (cpp_reader *pfile)
1575{
1576 class line_maps *line_table = pfile->line_table;
1577 const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set: line_table);
1578 const cpp_token *token;
1579 const char *new_file = ORDINARY_MAP_FILE_NAME (ord_map: map);
1580 linenum_type new_lineno;
1581 unsigned int new_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (ord_map: map);
1582 enum lc_reason reason = LC_RENAME_VERBATIM;
1583 int flag;
1584 bool wrapped;
1585
1586 /* Back up so we can get the number again. Putting this in
1587 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
1588 some circumstances, which can segfault. */
1589 _cpp_backup_tokens (pfile, 1);
1590
1591 /* #line commands expand macros. */
1592 token = cpp_get_token (pfile);
1593 if (token->type != CPP_NUMBER
1594 || strtolinenum (str: token->val.str.text, len: token->val.str.len,
1595 nump: &new_lineno, wrapped: &wrapped))
1596 {
1597 /* Unlike #line, there does not seem to be a way to get an EOF
1598 here. So, it should be safe to always spell the token. */
1599 cpp_error (pfile, CPP_DL_ERROR,
1600 msgid: "%qs after %<#%> is not a positive integer",
1601 cpp_token_as_text (pfile, token));
1602 return;
1603 }
1604
1605 token = cpp_get_token (pfile);
1606 if (token->type == CPP_STRING)
1607 {
1608 cpp_string s = { .len: 0, .text: 0 };
1609 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
1610 1, &s, CPP_STRING))
1611 new_file = (const char *)s.text;
1612
1613 new_sysp = 0;
1614 flag = read_flag (pfile, last: 0);
1615 if (flag == 1)
1616 {
1617 reason = LC_ENTER;
1618 /* Fake an include for cpp_included (). */
1619 _cpp_fake_include (pfile, new_file);
1620 flag = read_flag (pfile, last: flag);
1621 }
1622 else if (flag == 2)
1623 {
1624 reason = LC_LEAVE;
1625 flag = read_flag (pfile, last: flag);
1626 }
1627 if (flag == 3)
1628 {
1629 new_sysp = 1;
1630 flag = read_flag (pfile, last: flag);
1631 if (flag == 4)
1632 new_sysp = 2;
1633 }
1634 pfile->buffer->sysp = new_sysp;
1635
1636 check_eol (pfile, expand: false);
1637 }
1638 else if (token->type != CPP_EOF)
1639 {
1640 cpp_error (pfile, CPP_DL_ERROR, msgid: "%qs is not a valid filename",
1641 cpp_token_as_text (pfile, token));
1642 return;
1643 }
1644
1645 skip_rest_of_line (pfile);
1646
1647 if (reason == LC_LEAVE)
1648 {
1649 /* Reread map since cpp_get_token can invalidate it with a
1650 reallocation. */
1651 map = LINEMAPS_LAST_ORDINARY_MAP (set: line_table);
1652 const line_map_ordinary *from
1653 = linemap_included_from_linemap (set: line_table, map);
1654
1655 if (!from)
1656 /* Not nested. */;
1657 else if (!new_file[0])
1658 /* Leaving to "" means fill in the popped-to name. */
1659 new_file = ORDINARY_MAP_FILE_NAME (ord_map: from);
1660 else if (filename_cmp (s1: ORDINARY_MAP_FILE_NAME (ord_map: from), s2: new_file) != 0)
1661 /* It's the wrong name, Grommit! */
1662 from = NULL;
1663
1664 if (!from)
1665 {
1666 cpp_warning (pfile, CPP_W_NONE,
1667 msgid: "file %qs linemarker ignored due to "
1668 "incorrect nesting", new_file);
1669 return;
1670 }
1671 }
1672
1673 /* Compensate for the increment in linemap_add that occurs in
1674 _cpp_do_file_change. We're currently at the start of the line
1675 *following* the #line directive. A separate location_t for this
1676 location makes no sense (until we do the LC_LEAVE), and
1677 complicates LAST_SOURCE_LINE_LOCATION. */
1678 pfile->line_table->highest_location--;
1679
1680 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
1681 line_table->seen_line_directive = true;
1682}
1683
1684/* Arrange the file_change callback. Changing to TO_FILE:TO_LINE for
1685 REASON. SYSP is 1 for a system header, 2 for a system header that
1686 needs to be extern "C" protected, and zero otherwise. */
1687void
1688_cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
1689 const char *to_file, linenum_type to_line,
1690 unsigned int sysp)
1691{
1692 linemap_assert (reason != LC_ENTER_MACRO);
1693
1694 const line_map_ordinary *ord_map = NULL;
1695 if (!to_line && reason == LC_RENAME_VERBATIM)
1696 {
1697 /* A linemarker moving to line zero. If we're on the second
1698 line of the current map, and it also starts at zero, just
1699 rewind -- we're probably reading the builtins of a
1700 preprocessed source. */
1701 line_map_ordinary *last = LINEMAPS_LAST_ORDINARY_MAP (set: pfile->line_table);
1702 if (!ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map: last)
1703 && 0 == filename_cmp (s1: to_file, s2: ORDINARY_MAP_FILE_NAME (ord_map: last))
1704 && SOURCE_LINE (ord_map: last, loc: pfile->line_table->highest_line) == 2)
1705 {
1706 ord_map = last;
1707 pfile->line_table->highest_location
1708 = pfile->line_table->highest_line = MAP_START_LOCATION (map: last);
1709 }
1710 }
1711
1712 if (!ord_map)
1713 if (const line_map *map = linemap_add (pfile->line_table, reason, sysp,
1714 to_file, to_line))
1715 {
1716 ord_map = linemap_check_ordinary (map);
1717 linemap_line_start (set: pfile->line_table,
1718 to_line: ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map),
1719 max_column_hint: 127);
1720 }
1721
1722 if (pfile->cb.file_change)
1723 pfile->cb.file_change (pfile, ord_map);
1724}
1725
1726/* Report a warning or error detected by the program we are
1727 processing. Use the directive's tokens in the error message. */
1728static void
1729do_diagnostic (cpp_reader *pfile, enum cpp_diagnostic_level code,
1730 enum cpp_warning_reason reason, int print_dir)
1731{
1732 const unsigned char *dir_name;
1733 unsigned char *line;
1734 location_t src_loc = pfile->cur_token[-1].src_loc;
1735
1736 if (print_dir)
1737 dir_name = pfile->directive->name;
1738 else
1739 dir_name = NULL;
1740 pfile->state.prevent_expansion++;
1741 line = cpp_output_line_to_string (pfile, dir_name);
1742 pfile->state.prevent_expansion--;
1743
1744 if (code == CPP_DL_WARNING_SYSHDR && reason)
1745 cpp_warning_with_line_syshdr (pfile, reason, src_loc, 0, msgid: "%s", line);
1746 else if (code == CPP_DL_WARNING && reason)
1747 cpp_warning_with_line (pfile, reason, src_loc, 0, msgid: "%s", line);
1748 else
1749 cpp_error_with_line (pfile, code, src_loc, 0, msgid: "%s", line);
1750 free (ptr: line);
1751}
1752
1753static void
1754do_error (cpp_reader *pfile)
1755{
1756 do_diagnostic (pfile, code: CPP_DL_ERROR, reason: CPP_W_NONE, print_dir: 1);
1757}
1758
1759static void
1760do_warning (cpp_reader *pfile)
1761{
1762 /* We want #warning diagnostics to be emitted in system headers too. */
1763 do_diagnostic (pfile, code: CPP_DL_WARNING_SYSHDR, reason: CPP_W_WARNING_DIRECTIVE, print_dir: 1);
1764}
1765
1766/* Report program identification. */
1767static void
1768do_ident (cpp_reader *pfile)
1769{
1770 const cpp_token *str = cpp_get_token (pfile);
1771
1772 if (str->type != CPP_STRING)
1773 cpp_error (pfile, CPP_DL_ERROR, msgid: "invalid #%s directive",
1774 pfile->directive->name);
1775 else if (pfile->cb.ident)
1776 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
1777
1778 check_eol (pfile, expand: false);
1779}
1780
1781/* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1782 matching entry, or NULL if none is found. The returned entry could
1783 be the start of a namespace chain, or a pragma. */
1784static struct pragma_entry *
1785lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
1786{
1787 while (chain && chain->pragma != pragma)
1788 chain = chain->next;
1789
1790 return chain;
1791}
1792
1793/* Create and insert a blank pragma entry at the beginning of a
1794 singly-linked CHAIN. */
1795static struct pragma_entry *
1796new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain)
1797{
1798 struct pragma_entry *new_entry;
1799
1800 new_entry = (struct pragma_entry *)
1801 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
1802
1803 memset (s: new_entry, c: 0, n: sizeof (struct pragma_entry));
1804 new_entry->next = *chain;
1805
1806 *chain = new_entry;
1807 return new_entry;
1808}
1809
1810/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1811 goes in the global namespace. */
1812static struct pragma_entry *
1813register_pragma_1 (cpp_reader *pfile, const char *space, const char *name,
1814 bool allow_name_expansion)
1815{
1816 struct pragma_entry **chain = &pfile->pragmas;
1817 struct pragma_entry *entry;
1818 const cpp_hashnode *node;
1819
1820 if (space)
1821 {
1822 node = cpp_lookup (pfile, UC space, strlen (s: space));
1823 entry = lookup_pragma_entry (chain: *chain, pragma: node);
1824 if (!entry)
1825 {
1826 entry = new_pragma_entry (pfile, chain);
1827 entry->pragma = node;
1828 entry->is_nspace = true;
1829 entry->allow_expansion = allow_name_expansion;
1830 }
1831 else if (!entry->is_nspace)
1832 goto clash;
1833 else if (entry->allow_expansion != allow_name_expansion)
1834 {
1835 cpp_error (pfile, CPP_DL_ICE,
1836 msgid: "registering pragmas in namespace %qs with mismatched "
1837 "name expansion", space);
1838 return NULL;
1839 }
1840 chain = &entry->u.space;
1841 }
1842 else if (allow_name_expansion)
1843 {
1844 cpp_error (pfile, CPP_DL_ICE,
1845 msgid: "registering pragma %qs with name expansion "
1846 "and no namespace", name);
1847 return NULL;
1848 }
1849
1850 /* Check for duplicates. */
1851 node = cpp_lookup (pfile, UC name, strlen (s: name));
1852 entry = lookup_pragma_entry (chain: *chain, pragma: node);
1853 if (entry == NULL)
1854 {
1855 entry = new_pragma_entry (pfile, chain);
1856 entry->pragma = node;
1857 return entry;
1858 }
1859
1860 if (entry->is_nspace)
1861 clash:
1862 cpp_error (pfile, CPP_DL_ICE,
1863 msgid: "registering %qs as both a pragma and a pragma namespace",
1864 NODE_NAME (node));
1865 else if (space)
1866 cpp_error (pfile, CPP_DL_ICE, msgid: "%<#pragma %s %s%> is already registered",
1867 space, name);
1868 else
1869 cpp_error (pfile, CPP_DL_ICE, msgid: "%<#pragma %s%> is already registered",
1870 name);
1871
1872 return NULL;
1873}
1874
1875/* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1876static void
1877register_pragma_internal (cpp_reader *pfile, const char *space,
1878 const char *name, pragma_cb handler)
1879{
1880 struct pragma_entry *entry;
1881
1882 entry = register_pragma_1 (pfile, space, name, allow_name_expansion: false);
1883 entry->is_internal = true;
1884 entry->u.handler = handler;
1885}
1886
1887/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1888 goes in the global namespace. HANDLER is the handler it will call,
1889 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1890 expansion while parsing pragma NAME. This function is exported
1891 from libcpp. */
1892void
1893cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
1894 pragma_cb handler, bool allow_expansion)
1895{
1896 struct pragma_entry *entry;
1897
1898 if (!handler)
1899 {
1900 cpp_error (pfile, CPP_DL_ICE, msgid: "registering pragma with NULL handler");
1901 return;
1902 }
1903
1904 entry = register_pragma_1 (pfile, space, name, allow_name_expansion: false);
1905 if (entry)
1906 {
1907 entry->allow_expansion = allow_expansion;
1908 entry->u.handler = handler;
1909 }
1910}
1911
1912/* Similarly, but create mark the pragma for deferred processing.
1913 When found, a CPP_PRAGMA token will be insertted into the stream
1914 with IDENT in the token->u.pragma slot. */
1915void
1916cpp_register_deferred_pragma (cpp_reader *pfile, const char *space,
1917 const char *name, unsigned int ident,
1918 bool allow_expansion, bool allow_name_expansion)
1919{
1920 struct pragma_entry *entry;
1921
1922 entry = register_pragma_1 (pfile, space, name, allow_name_expansion);
1923 if (entry)
1924 {
1925 entry->is_deferred = true;
1926 entry->allow_expansion = allow_expansion;
1927 entry->u.ident = ident;
1928 }
1929}
1930
1931/* Register the pragmas the preprocessor itself handles. */
1932void
1933_cpp_init_internal_pragmas (cpp_reader *pfile)
1934{
1935 /* Pragmas in the global namespace. */
1936 register_pragma_internal (pfile, space: 0, name: "once", handler: do_pragma_once);
1937 register_pragma_internal (pfile, space: 0, name: "push_macro", handler: do_pragma_push_macro);
1938 register_pragma_internal (pfile, space: 0, name: "pop_macro", handler: do_pragma_pop_macro);
1939
1940 /* New GCC-specific pragmas should be put in the GCC namespace. */
1941 register_pragma_internal (pfile, space: "GCC", name: "poison", handler: do_pragma_poison);
1942 register_pragma_internal (pfile, space: "GCC", name: "system_header",
1943 handler: do_pragma_system_header);
1944 register_pragma_internal (pfile, space: "GCC", name: "dependency", handler: do_pragma_dependency);
1945 register_pragma_internal (pfile, space: "GCC", name: "warning", handler: do_pragma_warning);
1946 register_pragma_internal (pfile, space: "GCC", name: "error", handler: do_pragma_error);
1947}
1948
1949/* Return the number of registered pragmas in PE. */
1950
1951static int
1952count_registered_pragmas (struct pragma_entry *pe)
1953{
1954 int ct = 0;
1955 for (; pe != NULL; pe = pe->next)
1956 {
1957 if (pe->is_nspace)
1958 ct += count_registered_pragmas (pe: pe->u.space);
1959 ct++;
1960 }
1961 return ct;
1962}
1963
1964/* Save into SD the names of the registered pragmas referenced by PE,
1965 and return a pointer to the next free space in SD. */
1966
1967static char **
1968save_registered_pragmas (struct pragma_entry *pe, char **sd)
1969{
1970 for (; pe != NULL; pe = pe->next)
1971 {
1972 if (pe->is_nspace)
1973 sd = save_registered_pragmas (pe: pe->u.space, sd);
1974 *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
1975 HT_LEN (&pe->pragma->ident),
1976 HT_LEN (&pe->pragma->ident) + 1);
1977 }
1978 return sd;
1979}
1980
1981/* Return a newly-allocated array which saves the names of the
1982 registered pragmas. */
1983
1984char **
1985_cpp_save_pragma_names (cpp_reader *pfile)
1986{
1987 int ct = count_registered_pragmas (pe: pfile->pragmas);
1988 char **result = XNEWVEC (char *, ct);
1989 (void) save_registered_pragmas (pe: pfile->pragmas, sd: result);
1990 return result;
1991}
1992
1993/* Restore from SD the names of the registered pragmas referenced by PE,
1994 and return a pointer to the next unused name in SD. */
1995
1996static char **
1997restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1998 char **sd)
1999{
2000 for (; pe != NULL; pe = pe->next)
2001 {
2002 if (pe->is_nspace)
2003 sd = restore_registered_pragmas (pfile, pe: pe->u.space, sd);
2004 pe->pragma = cpp_lookup (pfile, UC *sd, strlen (s: *sd));
2005 free (ptr: *sd);
2006 sd++;
2007 }
2008 return sd;
2009}
2010
2011/* Restore the names of the registered pragmas from SAVED. */
2012
2013void
2014_cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
2015{
2016 (void) restore_registered_pragmas (pfile, pe: pfile->pragmas, sd: saved);
2017 free (ptr: saved);
2018}
2019
2020/* Pragmata handling. We handle some, and pass the rest on to the
2021 front end. C99 defines three pragmas and says that no macro
2022 expansion is to be performed on them; whether or not macro
2023 expansion happens for other pragmas is implementation defined.
2024 This implementation allows for a mix of both, since GCC did not
2025 traditionally macro expand its (few) pragmas, whereas OpenMP
2026 specifies that macro expansion should happen. */
2027static void
2028do_pragma (cpp_reader *pfile)
2029{
2030 const struct pragma_entry *p = NULL;
2031 const cpp_token *token, *pragma_token;
2032 location_t pragma_token_virt_loc = 0;
2033 cpp_token ns_token;
2034 unsigned int count = 1;
2035
2036 pfile->state.prevent_expansion++;
2037
2038 pragma_token = token = cpp_get_token_with_location (pfile,
2039 &pragma_token_virt_loc);
2040 ns_token = *token;
2041 if (token->type == CPP_NAME)
2042 {
2043 p = lookup_pragma_entry (chain: pfile->pragmas, pragma: token->val.node.node);
2044 if (p && p->is_nspace)
2045 {
2046 bool allow_name_expansion = p->allow_expansion;
2047 if (allow_name_expansion)
2048 pfile->state.prevent_expansion--;
2049
2050 token = cpp_get_token (pfile);
2051 if (token->type == CPP_NAME)
2052 p = lookup_pragma_entry (chain: p->u.space, pragma: token->val.node.node);
2053 else
2054 p = NULL;
2055 if (allow_name_expansion)
2056 pfile->state.prevent_expansion++;
2057 count = 2;
2058 }
2059 }
2060
2061 if (p)
2062 {
2063 if (p->is_deferred)
2064 {
2065 pfile->directive_result.src_loc = pragma_token_virt_loc;
2066 pfile->directive_result.type = CPP_PRAGMA;
2067 pfile->directive_result.flags = pragma_token->flags;
2068 pfile->directive_result.val.pragma = p->u.ident;
2069 pfile->state.in_deferred_pragma = true;
2070 pfile->state.pragma_allow_expansion = p->allow_expansion;
2071 if (!p->allow_expansion)
2072 pfile->state.prevent_expansion++;
2073 }
2074 else
2075 {
2076 /* Since the handler below doesn't get the line number, that
2077 it might need for diagnostics, make sure it has the right
2078 numbers in place. */
2079 if (pfile->cb.line_change)
2080 (*pfile->cb.line_change) (pfile, pragma_token, false);
2081 if (p->allow_expansion)
2082 pfile->state.prevent_expansion--;
2083 (*p->u.handler) (pfile);
2084 if (p->allow_expansion)
2085 pfile->state.prevent_expansion++;
2086 }
2087 }
2088 else if (pfile->cb.def_pragma)
2089 {
2090 if (count == 1 || pfile->context->prev == NULL)
2091 _cpp_backup_tokens (pfile, count);
2092 else
2093 {
2094 /* Invalid name comes from macro expansion, _cpp_backup_tokens
2095 won't allow backing 2 tokens. */
2096 const auto tok_buff = _cpp_get_buff (pfile, 2 * sizeof (cpp_token));
2097 const auto toks = (cpp_token *)tok_buff->base;
2098 toks[0] = ns_token;
2099 toks[0].flags |= NO_EXPAND;
2100 toks[1] = *token;
2101 toks[1].flags |= NO_EXPAND | PREV_WHITE;
2102 _cpp_push_token_context (pfile, NULL, toks, 2);
2103 /* Arrange to free this buffer when no longer needed. */
2104 pfile->context->buff = tok_buff;
2105 }
2106 pfile->cb.def_pragma (pfile, pfile->directive_line);
2107 }
2108
2109 pfile->state.prevent_expansion--;
2110}
2111
2112/* Handle #pragma once. */
2113static void
2114do_pragma_once (cpp_reader *pfile)
2115{
2116 if (_cpp_in_main_source_file (pfile))
2117 cpp_warning (pfile, CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER,
2118 msgid: "%<#pragma once%> in main file");
2119
2120 check_eol (pfile, expand: false);
2121 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
2122}
2123
2124/* Helper for #pragma {push,pop}_macro. Destringize STR and
2125 lex it into an identifier, returning the hash node for it. */
2126
2127static cpp_hashnode *
2128lex_identifier_from_string (cpp_reader *pfile, cpp_string str)
2129{
2130 auto src = (const uchar *) memchr (s: str.text, c: '"', n: str.len);
2131 gcc_checking_assert (src);
2132 ++src;
2133 const auto limit = str.text + str.len - 1;
2134 gcc_checking_assert (*limit == '"' && limit >= src);
2135 const auto ident = XALLOCAVEC (uchar, limit - src + 1);
2136 auto dest = ident;
2137 while (src != limit)
2138 {
2139 /* We know there is a character following the backslash. */
2140 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
2141 src++;
2142 *dest++ = *src++;
2143 }
2144
2145 /* We reserved a spot for the newline with the + 1 when allocating IDENT.
2146 Push a buffer containing the identifier to lex. */
2147 *dest = '\n';
2148 cpp_push_buffer (pfile, ident, dest - ident, true);
2149 _cpp_clean_line (pfile);
2150 pfile->cur_token = _cpp_temp_token (pfile);
2151 cpp_token *tok;
2152 {
2153 /* Suppress diagnostics during lexing so that we silently ignore invalid
2154 input, as seems to be the common practice for this pragma. */
2155 cpp_auto_suppress_diagnostics suppress {pfile};
2156 tok = _cpp_lex_direct (pfile);
2157 }
2158
2159 cpp_hashnode *node;
2160 if (tok->type != CPP_NAME || pfile->buffer->cur != pfile->buffer->rlimit)
2161 node = nullptr;
2162 else
2163 node = tok->val.node.node;
2164
2165 _cpp_pop_buffer (pfile);
2166 return node;
2167}
2168
2169/* Common processing for #pragma {push,pop}_macro. */
2170
2171static cpp_hashnode *
2172push_pop_macro_common (cpp_reader *pfile, const char *type)
2173{
2174 const cpp_token *const txt = get__Pragma_string (pfile);
2175 ++pfile->keep_tokens;
2176 cpp_hashnode *node;
2177 if (txt)
2178 {
2179 check_eol (pfile, expand: false);
2180 skip_rest_of_line (pfile);
2181 node = lex_identifier_from_string (pfile, str: txt->val.str);
2182 }
2183 else
2184 {
2185 node = nullptr;
2186 location_t src_loc = pfile->cur_token[-1].src_loc;
2187 cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
2188 msgid: "invalid %<#pragma %s_macro%> directive", type);
2189 skip_rest_of_line (pfile);
2190 }
2191 --pfile->keep_tokens;
2192 return node;
2193}
2194
2195/* Handle #pragma push_macro(STRING). */
2196static void
2197do_pragma_push_macro (cpp_reader *pfile)
2198{
2199 const auto node = push_pop_macro_common (pfile, type: "push");
2200 if (!node)
2201 return;
2202 const auto c = XCNEW (def_pragma_macro);
2203 c->name = xstrdup ((const char *) NODE_NAME (node));
2204 c->next = pfile->pushed_macros;
2205 if (node->type == NT_VOID)
2206 c->is_undef = 1;
2207 else if (node->type == NT_BUILTIN_MACRO)
2208 c->is_builtin = 1;
2209 else
2210 {
2211 const auto defn = cpp_macro_definition (pfile, node);
2212 const size_t defnlen = ustrlen (s1: defn);
2213 c->definition = XNEWVEC (uchar, defnlen + 2);
2214 c->definition[defnlen] = '\n';
2215 c->definition[defnlen + 1] = 0;
2216 c->line = node->value.macro->line;
2217 c->syshdr = node->value.macro->syshdr;
2218 c->used = node->value.macro->used;
2219 memcpy (dest: c->definition, src: defn, n: defnlen);
2220 }
2221
2222 pfile->pushed_macros = c;
2223}
2224
2225/* Handle #pragma pop_macro(STRING). */
2226static void
2227do_pragma_pop_macro (cpp_reader *pfile)
2228{
2229 const auto node = push_pop_macro_common (pfile, type: "pop");
2230 if (!node)
2231 return;
2232 for (def_pragma_macro *c = pfile->pushed_macros, *l = nullptr; c; c = c->next)
2233 {
2234 if (!strcmp (s1: c->name, s2: (const char *) NODE_NAME (node)))
2235 {
2236 if (!l)
2237 pfile->pushed_macros = c->next;
2238 else
2239 l->next = c->next;
2240 cpp_pop_definition (pfile, c, node);
2241 free (ptr: c->definition);
2242 free (ptr: c->name);
2243 free (ptr: c);
2244 break;
2245 }
2246 l = c;
2247 }
2248}
2249
2250/* Handle #pragma GCC poison, to poison one or more identifiers so
2251 that the lexer produces a hard error for each subsequent usage. */
2252static void
2253do_pragma_poison (cpp_reader *pfile)
2254{
2255 const cpp_token *tok;
2256 cpp_hashnode *hp;
2257
2258 pfile->state.poisoned_ok = 1;
2259 for (;;)
2260 {
2261 tok = _cpp_lex_token (pfile);
2262 if (tok->type == CPP_EOF)
2263 break;
2264 if (tok->type != CPP_NAME)
2265 {
2266 cpp_error (pfile, CPP_DL_ERROR,
2267 msgid: "invalid %<#pragma GCC poison%> directive");
2268 break;
2269 }
2270
2271 hp = tok->val.node.node;
2272 if (hp->flags & NODE_POISONED)
2273 continue;
2274
2275 if (cpp_macro_p (node: hp))
2276 cpp_error (pfile, CPP_DL_WARNING, msgid: "poisoning existing macro %qs",
2277 NODE_NAME (hp));
2278 _cpp_free_definition (hp);
2279 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
2280 const auto data = (cpp_hashnode_extra *)
2281 ht_lookup (ht: pfile->extra_hash_table, id: hp->ident, opt: HT_ALLOC);
2282 data->poisoned_loc = tok->src_loc;
2283 }
2284 pfile->state.poisoned_ok = 0;
2285}
2286
2287/* Mark the current header as a system header. This will suppress
2288 some categories of warnings (notably those from -pedantic). It is
2289 intended for use in system libraries that cannot be implemented in
2290 conforming C, but cannot be certain that their headers appear in a
2291 system include directory. To prevent abuse, it is rejected in the
2292 primary source file. */
2293static void
2294do_pragma_system_header (cpp_reader *pfile)
2295{
2296 if (_cpp_in_main_source_file (pfile))
2297 cpp_error (pfile, CPP_DL_WARNING,
2298 msgid: "%<#pragma system_header%> ignored outside include file");
2299 else
2300 {
2301 check_eol (pfile, expand: false);
2302 skip_rest_of_line (pfile);
2303 cpp_make_system_header (pfile, 1, 0);
2304 }
2305}
2306
2307/* Check the modified date of the current include file against a specified
2308 file. Issue a diagnostic, if the specified file is newer. We use this to
2309 determine if a fixed header should be refixed. */
2310static void
2311do_pragma_dependency (cpp_reader *pfile)
2312{
2313 const char *fname;
2314 int angle_brackets, ordering;
2315 location_t location;
2316
2317 fname = parse_include (pfile, pangle_brackets: &angle_brackets, NULL, location: &location);
2318 if (!fname)
2319 return;
2320
2321 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
2322 if (ordering < 0)
2323 cpp_error (pfile, CPP_DL_WARNING, msgid: "cannot find source file %s", fname);
2324 else if (ordering > 0)
2325 {
2326 cpp_error (pfile, CPP_DL_WARNING,
2327 msgid: "current file is older than %s", fname);
2328 if (cpp_get_token (pfile)->type != CPP_EOF)
2329 {
2330 _cpp_backup_tokens (pfile, 1);
2331 do_diagnostic (pfile, code: CPP_DL_WARNING, reason: CPP_W_NONE, print_dir: 0);
2332 }
2333 }
2334
2335 free (ptr: (void *) fname);
2336}
2337
2338/* Issue a diagnostic with the message taken from the pragma. If
2339 ERROR is true, the diagnostic is a warning, otherwise, it is an
2340 error. */
2341static void
2342do_pragma_warning_or_error (cpp_reader *pfile, bool error)
2343{
2344 const cpp_token *tok = _cpp_lex_token (pfile);
2345 cpp_string str;
2346 if (tok->type != CPP_STRING
2347 || !cpp_interpret_string_notranslate (pfile, &tok->val.str, 1, &str,
2348 CPP_STRING)
2349 || str.len == 0)
2350 {
2351 cpp_error (pfile, CPP_DL_ERROR, msgid: "invalid %<#pragma GCC %s%> directive",
2352 error ? "error" : "warning");
2353 return;
2354 }
2355 cpp_error (pfile, error ? CPP_DL_ERROR : CPP_DL_WARNING,
2356 msgid: "%s", str.text);
2357 free (ptr: (void *)str.text);
2358}
2359
2360/* Issue a warning diagnostic. */
2361static void
2362do_pragma_warning (cpp_reader *pfile)
2363{
2364 do_pragma_warning_or_error (pfile, error: false);
2365}
2366
2367/* Issue an error diagnostic. */
2368static void
2369do_pragma_error (cpp_reader *pfile)
2370{
2371 do_pragma_warning_or_error (pfile, error: true);
2372}
2373
2374/* Get a token but skip padding. */
2375static const cpp_token *
2376get_token_no_padding (cpp_reader *pfile)
2377{
2378 for (;;)
2379 {
2380 const cpp_token *result = cpp_get_token (pfile);
2381 if (result->type != CPP_PADDING)
2382 return result;
2383 }
2384}
2385
2386/* Check syntax is "(string-literal)". Returns the string on success,
2387 or NULL on failure. */
2388static const cpp_token *
2389get__Pragma_string (cpp_reader *pfile)
2390{
2391 const cpp_token *string;
2392 const cpp_token *paren;
2393
2394 paren = get_token_no_padding (pfile);
2395 if (paren->type == CPP_EOF)
2396 _cpp_backup_tokens (pfile, 1);
2397 if (paren->type != CPP_OPEN_PAREN)
2398 return NULL;
2399
2400 string = get_token_no_padding (pfile);
2401 if (string->type == CPP_EOF)
2402 _cpp_backup_tokens (pfile, 1);
2403 if (string->type != CPP_STRING && string->type != CPP_WSTRING
2404 && string->type != CPP_STRING32 && string->type != CPP_STRING16
2405 && string->type != CPP_UTF8STRING)
2406 return NULL;
2407
2408 paren = get_token_no_padding (pfile);
2409 if (paren->type == CPP_EOF)
2410 _cpp_backup_tokens (pfile, 1);
2411 if (paren->type != CPP_CLOSE_PAREN)
2412 return NULL;
2413
2414 return string;
2415}
2416
2417/* Destringize IN into a temporary buffer, by removing the first \ of
2418 \" and \\ sequences, and process the result as a #pragma directive. */
2419static void
2420destringize_and_run (cpp_reader *pfile, const cpp_string *in,
2421 location_t expansion_loc)
2422{
2423 const unsigned char *src, *limit;
2424 char *dest, *result;
2425 cpp_context *saved_context;
2426 cpp_token *saved_cur_token;
2427 tokenrun *saved_cur_run;
2428 cpp_token *toks;
2429 int count;
2430 const struct directive *save_directive;
2431
2432 dest = result = (char *) alloca (in->len - 1);
2433 src = in->text + 1 + (in->text[0] == 'L');
2434 limit = in->text + in->len - 1;
2435 while (src < limit)
2436 {
2437 /* We know there is a character following the backslash. */
2438 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
2439 src++;
2440 *dest++ = *src++;
2441 }
2442 *dest = '\n';
2443
2444 /* Ugh; an awful kludge. We are really not set up to be lexing
2445 tokens when in the middle of a macro expansion. Use a new
2446 context to force cpp_get_token to lex, and so skip_rest_of_line
2447 doesn't go beyond the end of the text. Also, remember the
2448 current lexing position so we can return to it later.
2449
2450 Something like line-at-a-time lexing should remove the need for
2451 this. */
2452 saved_context = pfile->context;
2453 saved_cur_token = pfile->cur_token;
2454 saved_cur_run = pfile->cur_run;
2455
2456 pfile->context = XCNEW (cpp_context);
2457
2458 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
2459 until we've read all of the tokens that we want. */
2460 cpp_push_buffer (pfile, (const uchar *) result, dest - result,
2461 /* from_stage3 */ true);
2462
2463 /* This is needed for _Pragma("once") and _Pragma("GCC system_header") to work
2464 properly. */
2465 pfile->buffer->file = pfile->buffer->prev->file;
2466 pfile->buffer->sysp = pfile->buffer->prev->sysp;
2467
2468 /* See comment below regarding the use of expansion_loc as the location
2469 for all tokens; arrange here that diagnostics issued during lexing
2470 get the same treatment. */
2471 const auto prev_loc_override = pfile->diagnostic_override_loc;
2472 pfile->diagnostic_override_loc = expansion_loc;
2473
2474 start_directive (pfile);
2475 _cpp_clean_line (pfile);
2476 save_directive = pfile->directive;
2477 pfile->directive = &dtable[T_PRAGMA];
2478 do_pragma (pfile);
2479 if (pfile->directive_result.type == CPP_PRAGMA)
2480 pfile->directive_result.flags |= PRAGMA_OP;
2481 end_directive (pfile, skip_line: 1);
2482 pfile->directive = save_directive;
2483
2484 /* We always insert at least one token, the directive result. It'll
2485 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
2486 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
2487
2488 /* If we're not handling the pragma internally, read all of the tokens from
2489 the string buffer now, while the string buffer is still installed. */
2490 /* ??? Note that the token buffer allocated here is leaked. It's not clear
2491 to me what the true lifespan of the tokens are. It would appear that
2492 the lifespan is the entire parse of the main input stream, in which case
2493 this may not be wrong. */
2494 if (pfile->directive_result.type == CPP_PRAGMA)
2495 {
2496 int maxcount;
2497
2498 count = 1;
2499 maxcount = 50;
2500 toks = XNEWVEC (cpp_token, maxcount);
2501 toks[0] = pfile->directive_result;
2502 toks[0].src_loc = expansion_loc;
2503
2504 do
2505 {
2506 if (count == maxcount)
2507 {
2508 maxcount = maxcount * 3 / 2;
2509 toks = XRESIZEVEC (cpp_token, toks, maxcount);
2510 }
2511 toks[count] = *cpp_get_token (pfile);
2512 /* _Pragma is a builtin, so we're not within a macro-map, and so
2513 the token locations are set to bogus ordinary locations
2514 near to, but after that of the "_Pragma".
2515 Paper over this by setting them equal to the location of the
2516 _Pragma itself (PR preprocessor/69126). */
2517 toks[count].src_loc = expansion_loc;
2518 /* Macros have been already expanded by cpp_get_token
2519 if the pragma allowed expansion. */
2520 toks[count++].flags |= NO_EXPAND;
2521 }
2522 while (toks[count-1].type != CPP_PRAGMA_EOL);
2523 }
2524 else
2525 {
2526 count = 1;
2527 toks = &pfile->avoid_paste;
2528
2529 /* If we handled the entire pragma internally, make sure we get the
2530 line number correct for the next token. */
2531 if (pfile->cb.line_change)
2532 pfile->cb.line_change (pfile, pfile->cur_token, false);
2533 }
2534
2535 /* Finish inlining run_directive. */
2536 pfile->buffer->file = NULL;
2537 /* If the system header state changed due to #pragma GCC system_header, then
2538 make that applicable to the real buffer too. */
2539 pfile->buffer->prev->sysp = pfile->buffer->sysp;
2540 _cpp_pop_buffer (pfile);
2541 pfile->diagnostic_override_loc = prev_loc_override;
2542
2543 /* Reset the old macro state before ... */
2544 XDELETE (pfile->context);
2545 pfile->context = saved_context;
2546 pfile->cur_token = saved_cur_token;
2547 pfile->cur_run = saved_cur_run;
2548
2549 /* ... inserting the new tokens we collected. */
2550 _cpp_push_token_context (pfile, NULL, toks, count);
2551}
2552
2553/* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
2554int
2555_cpp_do__Pragma (cpp_reader *pfile, location_t expansion_loc)
2556{
2557 /* Make sure we don't invalidate the string token, if the closing parenthesis
2558 ended up on a different line. */
2559 ++pfile->keep_tokens;
2560 const cpp_token *string = get__Pragma_string (pfile);
2561 --pfile->keep_tokens;
2562
2563 pfile->directive_result.type = CPP_PADDING;
2564
2565 if (string)
2566 {
2567 destringize_and_run (pfile, in: &string->val.str, expansion_loc);
2568 return 1;
2569 }
2570 cpp_error (pfile, CPP_DL_ERROR,
2571 msgid: "%<_Pragma%> takes a parenthesized string literal");
2572 return 0;
2573}
2574
2575/* Handle #ifdef. */
2576static void
2577do_ifdef (cpp_reader *pfile)
2578{
2579 int skip = 1;
2580
2581 if (! pfile->state.skipping)
2582 {
2583 cpp_hashnode *node = lex_macro_node (pfile, is_def_or_undef: false);
2584
2585 if (node)
2586 {
2587 skip = !_cpp_defined_macro_p (node);
2588 if (!_cpp_maybe_notify_macro_use (pfile, node, loc: pfile->directive_line))
2589 /* It wasn't a macro after all. */
2590 skip = true;
2591 _cpp_mark_macro_used (node);
2592 if (pfile->cb.used)
2593 pfile->cb.used (pfile, pfile->directive_line, node);
2594 check_eol (pfile, expand: false);
2595 }
2596 }
2597
2598 push_conditional (pfile, skip, T_IFDEF, 0);
2599}
2600
2601/* Handle #ifndef. */
2602static void
2603do_ifndef (cpp_reader *pfile)
2604{
2605 int skip = 1;
2606 cpp_hashnode *node = 0;
2607
2608 if (! pfile->state.skipping)
2609 {
2610 node = lex_macro_node (pfile, is_def_or_undef: false);
2611
2612 if (node)
2613 {
2614 skip = _cpp_defined_macro_p (node);
2615 if (!_cpp_maybe_notify_macro_use (pfile, node, loc: pfile->directive_line))
2616 /* It wasn't a macro after all. */
2617 skip = false;
2618 _cpp_mark_macro_used (node);
2619 if (pfile->cb.used)
2620 pfile->cb.used (pfile, pfile->directive_line, node);
2621 check_eol (pfile, expand: false);
2622 }
2623 }
2624
2625 push_conditional (pfile, skip, T_IFNDEF, node);
2626}
2627
2628/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
2629 pfile->mi_ind_cmacro so we can handle multiple-include
2630 optimizations. If macro expansion occurs in the expression, we
2631 cannot treat it as a controlling conditional, since the expansion
2632 could change in the future. That is handled by cpp_get_token. */
2633static void
2634do_if (cpp_reader *pfile)
2635{
2636 int skip = 1;
2637
2638 if (! pfile->state.skipping)
2639 skip = _cpp_parse_expr (pfile, "#if", NULL) == false;
2640
2641 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
2642}
2643
2644/* Flip skipping state if appropriate and continue without changing
2645 if_stack; this is so that the error message for missing #endif's
2646 etc. will point to the original #if. */
2647static void
2648do_else (cpp_reader *pfile)
2649{
2650 cpp_buffer *buffer = pfile->buffer;
2651 struct if_stack *ifs = buffer->if_stack;
2652
2653 if (ifs == NULL)
2654 cpp_error (pfile, CPP_DL_ERROR, msgid: "%<#else%> without %<#if%>");
2655 else
2656 {
2657 if (ifs->type == T_ELSE)
2658 {
2659 cpp_error (pfile, CPP_DL_ERROR, msgid: "%<#else%> after %<#else%>");
2660 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
2661 msgid: "the conditional began here");
2662 }
2663 ifs->type = T_ELSE;
2664
2665 /* Skip any future (erroneous) #elses or #elifs. */
2666 pfile->state.skipping = ifs->skip_elses;
2667 ifs->skip_elses = true;
2668
2669 /* Invalidate any controlling macro. */
2670 ifs->mi_cmacro = 0;
2671
2672 /* Only check EOL if was not originally skipping. */
2673 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
2674 check_eol_endif_labels (pfile);
2675 }
2676}
2677
2678/* Handle a #elif, #elifdef or #elifndef directive by not changing if_stack
2679 either. See the comment above do_else. */
2680static void
2681do_elif (cpp_reader *pfile)
2682{
2683 cpp_buffer *buffer = pfile->buffer;
2684 struct if_stack *ifs = buffer->if_stack;
2685
2686 if (ifs == NULL)
2687 cpp_error (pfile, CPP_DL_ERROR, msgid: "%<#%s%> without %<#if%>",
2688 pfile->directive->name);
2689 else
2690 {
2691 if (ifs->type == T_ELSE)
2692 {
2693 cpp_error (pfile, CPP_DL_ERROR, msgid: "%<#%s%> after %<#else%>",
2694 pfile->directive->name);
2695 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
2696 msgid: "the conditional began here");
2697 }
2698 ifs->type = T_ELIF;
2699
2700 /* See DR#412: "Only the first group whose control condition
2701 evaluates to true (nonzero) is processed; any following groups
2702 are skipped and their controlling directives are processed as
2703 if they were in a group that is skipped." */
2704 if (ifs->skip_elses)
2705 {
2706 /* In older GNU standards, #elifdef/#elifndef is supported
2707 as an extension, but pedwarn if -pedantic if the presence
2708 of the directive would be rejected. */
2709 if (pfile->directive != &dtable[T_ELIF]
2710 && ! CPP_OPTION (pfile, elifdef)
2711 && CPP_PEDANTIC (pfile)
2712 && !pfile->state.skipping)
2713 {
2714 if (CPP_OPTION (pfile, cplusplus))
2715 cpp_pedwarning (pfile, CPP_W_CXX23_EXTENSIONS,
2716 msgid: "%<#%s%> before C++23 is a GCC extension",
2717 pfile->directive->name);
2718 else
2719 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
2720 msgid: "%<#%s%> before C23 is a GCC extension",
2721 pfile->directive->name);
2722 }
2723 pfile->state.skipping = 1;
2724 }
2725 else
2726 {
2727 if (pfile->directive == &dtable[T_ELIF])
2728 pfile->state.skipping = !_cpp_parse_expr (pfile, "#elif", NULL);
2729 else
2730 {
2731 cpp_hashnode *node = lex_macro_node (pfile, is_def_or_undef: false);
2732
2733 if (node)
2734 {
2735 bool macro_defined = _cpp_defined_macro_p (node);
2736 if (!_cpp_maybe_notify_macro_use (pfile, node,
2737 loc: pfile->directive_line))
2738 /* It wasn't a macro after all. */
2739 macro_defined = false;
2740 bool skip = (pfile->directive == &dtable[T_ELIFDEF]
2741 ? !macro_defined
2742 : macro_defined);
2743 if (pfile->cb.used)
2744 pfile->cb.used (pfile, pfile->directive_line, node);
2745 check_eol (pfile, expand: false);
2746 /* In older GNU standards, #elifdef/#elifndef is supported
2747 as an extension, but pedwarn if -pedantic if the presence
2748 of the directive would change behavior. */
2749 if (! CPP_OPTION (pfile, elifdef)
2750 && CPP_PEDANTIC (pfile)
2751 && pfile->state.skipping != skip)
2752 {
2753 if (CPP_OPTION (pfile, cplusplus))
2754 cpp_pedwarning (pfile, CPP_W_CXX23_EXTENSIONS,
2755 msgid: "%<#%s%> before C++23 is a GCC "
2756 "extension",
2757 pfile->directive->name);
2758 else
2759 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
2760 msgid: "%<#%s%> before C23 is a GCC "
2761 "extension",
2762 pfile->directive->name);
2763 }
2764 pfile->state.skipping = skip;
2765 }
2766 }
2767 ifs->skip_elses = !pfile->state.skipping;
2768 }
2769
2770 /* Invalidate any controlling macro. */
2771 ifs->mi_cmacro = 0;
2772 }
2773}
2774
2775/* Handle a #elifdef directive. */
2776static void
2777do_elifdef (cpp_reader *pfile)
2778{
2779 do_elif (pfile);
2780}
2781
2782/* Handle a #elifndef directive. */
2783static void
2784do_elifndef (cpp_reader *pfile)
2785{
2786 do_elif (pfile);
2787}
2788
2789/* #endif pops the if stack and resets pfile->state.skipping. */
2790static void
2791do_endif (cpp_reader *pfile)
2792{
2793 cpp_buffer *buffer = pfile->buffer;
2794 struct if_stack *ifs = buffer->if_stack;
2795
2796 if (ifs == NULL)
2797 cpp_error (pfile, CPP_DL_ERROR, msgid: "%<#endif%> without %<#if%>");
2798 else
2799 {
2800 /* Only check EOL if was not originally skipping. */
2801 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
2802 check_eol_endif_labels (pfile);
2803
2804 /* If potential control macro, we go back outside again. */
2805 if (ifs->next == 0 && ifs->mi_cmacro)
2806 {
2807 pfile->mi_valid = true;
2808 pfile->mi_cmacro = ifs->mi_cmacro;
2809 pfile->mi_loc = ifs->line;
2810 pfile->mi_def_cmacro = nullptr;
2811 if (ifs->mi_def_cmacro && !_cpp_defined_macro_p (node: pfile->mi_cmacro))
2812 {
2813 pfile->mi_def_cmacro = ifs->mi_def_cmacro;
2814 pfile->mi_def_loc = ifs->def_loc;
2815 }
2816 }
2817
2818 buffer->if_stack = ifs->next;
2819 pfile->state.skipping = ifs->was_skipping;
2820 obstack_free (&pfile->buffer_ob, ifs);
2821 }
2822}
2823
2824/* Push an if_stack entry for a preprocessor conditional, and set
2825 pfile->state.skipping to SKIP. If TYPE indicates the conditional
2826 is #if or #ifndef, CMACRO is a potentially controlling macro, and
2827 we need to check here that we are at the top of the file. */
2828static void
2829push_conditional (cpp_reader *pfile, int skip, int type,
2830 const cpp_hashnode *cmacro)
2831{
2832 struct if_stack *ifs;
2833 cpp_buffer *buffer = pfile->buffer;
2834
2835 ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
2836 ifs->line = pfile->directive_line;
2837 ifs->def_loc = 0;
2838 ifs->next = buffer->if_stack;
2839 ifs->skip_elses = pfile->state.skipping || !skip;
2840 ifs->was_skipping = pfile->state.skipping;
2841 ifs->type = type;
2842 /* This condition is effectively a test for top-of-file. */
2843 if (pfile->mi_valid && pfile->mi_cmacro == 0)
2844 ifs->mi_cmacro = cmacro;
2845 else
2846 ifs->mi_cmacro = 0;
2847 ifs->mi_def_cmacro = nullptr;
2848
2849 pfile->state.skipping = skip;
2850 buffer->if_stack = ifs;
2851}
2852
2853/* Read the tokens of the answer into the macro pool, in a directive
2854 of type TYPE. Only commit the memory if we intend it as permanent
2855 storage, i.e. the #assert case. Returns 0 on success, and sets
2856 ANSWERP to point to the answer. PRED_LOC is the location of the
2857 predicate. */
2858static bool
2859parse_answer (cpp_reader *pfile, int type, location_t pred_loc,
2860 cpp_macro **answer_ptr)
2861{
2862 /* In a conditional, it is legal to not have an open paren. We
2863 should save the following token in this case. */
2864 const cpp_token *paren = cpp_get_token (pfile);
2865
2866 /* If not a paren, see if we're OK. */
2867 if (paren->type != CPP_OPEN_PAREN)
2868 {
2869 /* In a conditional no answer is a test for any answer. It
2870 could be followed by any token. */
2871 if (type == T_IF)
2872 {
2873 _cpp_backup_tokens (pfile, 1);
2874 return true;
2875 }
2876
2877 /* #unassert with no answer is valid - it removes all answers. */
2878 if (type == T_UNASSERT && paren->type == CPP_EOF)
2879 return true;
2880
2881 cpp_error_with_line (pfile, CPP_DL_ERROR, pred_loc, 0,
2882 msgid: "missing %<(%> after predicate");
2883 return false;
2884 }
2885
2886 cpp_macro *answer = _cpp_new_macro (pfile, cmk_assert,
2887 _cpp_reserve_room (pfile, have: 0,
2888 extra: sizeof (cpp_macro)));
2889 answer->parm.next = NULL;
2890 unsigned count = 0;
2891 for (;;)
2892 {
2893 const cpp_token *token = cpp_get_token (pfile);
2894
2895 if (token->type == CPP_CLOSE_PAREN)
2896 break;
2897
2898 if (token->type == CPP_EOF)
2899 {
2900 cpp_error (pfile, CPP_DL_ERROR, msgid: "missing %<)%> to complete answer");
2901 return false;
2902 }
2903
2904 answer = (cpp_macro *)_cpp_reserve_room
2905 (pfile, have: sizeof (cpp_macro) + count * sizeof (cpp_token),
2906 extra: sizeof (cpp_token));
2907 answer->exp.tokens[count++] = *token;
2908 }
2909
2910 if (!count)
2911 {
2912 cpp_error (pfile, CPP_DL_ERROR, msgid: "predicate%'s answer is empty");
2913 return false;
2914 }
2915
2916 /* Drop whitespace at start, for answer equivalence purposes. */
2917 answer->exp.tokens[0].flags &= ~PREV_WHITE;
2918
2919 answer->count = count;
2920 *answer_ptr = answer;
2921
2922 return true;
2923}
2924
2925/* Parses an assertion directive of type TYPE, returning a pointer to
2926 the hash node of the predicate, or 0 on error. The node is
2927 guaranteed to be disjoint from the macro namespace, so can only
2928 have type 'NT_VOID'. If an answer was supplied, it is placed in
2929 *ANSWER_PTR, which is otherwise set to 0. */
2930static cpp_hashnode *
2931parse_assertion (cpp_reader *pfile, int type, cpp_macro **answer_ptr)
2932{
2933 cpp_hashnode *result = 0;
2934
2935 /* We don't expand predicates or answers. */
2936 pfile->state.prevent_expansion++;
2937
2938 *answer_ptr = NULL;
2939
2940 const cpp_token *predicate = cpp_get_token (pfile);
2941 if (predicate->type == CPP_EOF)
2942 cpp_error (pfile, CPP_DL_ERROR, msgid: "assertion without predicate");
2943 else if (predicate->type != CPP_NAME)
2944 cpp_error_with_line (pfile, CPP_DL_ERROR, predicate->src_loc, 0,
2945 msgid: "predicate must be an identifier");
2946 else if (parse_answer (pfile, type, pred_loc: predicate->src_loc, answer_ptr))
2947 {
2948 unsigned int len = NODE_LEN (predicate->val.node.node);
2949 unsigned char *sym = (unsigned char *) alloca (len + 1);
2950
2951 /* Prefix '#' to get it out of macro namespace. */
2952 sym[0] = '#';
2953 memcpy (dest: sym + 1, NODE_NAME (predicate->val.node.node), n: len);
2954 result = cpp_lookup (pfile, sym, len + 1);
2955 }
2956
2957 pfile->state.prevent_expansion--;
2958
2959 return result;
2960}
2961
2962/* Returns a pointer to the pointer to CANDIDATE in the answer chain,
2963 or a pointer to NULL if the answer is not in the chain. */
2964static cpp_macro **
2965find_answer (cpp_hashnode *node, const cpp_macro *candidate)
2966{
2967 unsigned int i;
2968 cpp_macro **result = NULL;
2969
2970 for (result = &node->value.answers; *result; result = &(*result)->parm.next)
2971 {
2972 cpp_macro *answer = *result;
2973
2974 if (answer->count == candidate->count)
2975 {
2976 for (i = 0; i < answer->count; i++)
2977 if (!_cpp_equiv_tokens (&answer->exp.tokens[i],
2978 &candidate->exp.tokens[i]))
2979 break;
2980
2981 if (i == answer->count)
2982 break;
2983 }
2984 }
2985
2986 return result;
2987}
2988
2989/* Test an assertion within a preprocessor conditional. Returns
2990 nonzero on failure, zero on success. On success, the result of
2991 the test is written into VALUE, otherwise the value 0. */
2992int
2993_cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
2994{
2995 cpp_macro *answer;
2996 cpp_hashnode *node = parse_assertion (pfile, type: T_IF, answer_ptr: &answer);
2997
2998 /* For recovery, an erroneous assertion expression is handled as a
2999 failing assertion. */
3000 *value = 0;
3001
3002 if (node)
3003 {
3004 if (node->value.answers)
3005 *value = !answer || *find_answer (node, candidate: answer);
3006 }
3007 else if (pfile->cur_token[-1].type == CPP_EOF)
3008 _cpp_backup_tokens (pfile, 1);
3009
3010 /* We don't commit the memory for the answer - it's temporary only. */
3011 return node == 0;
3012}
3013
3014/* Handle #assert. */
3015static void
3016do_assert (cpp_reader *pfile)
3017{
3018 cpp_macro *answer;
3019 cpp_hashnode *node = parse_assertion (pfile, type: T_ASSERT, answer_ptr: &answer);
3020
3021 if (node)
3022 {
3023 /* Place the new answer in the answer list. First check there
3024 is not a duplicate. */
3025 if (*find_answer (node, candidate: answer))
3026 {
3027 cpp_error (pfile, CPP_DL_WARNING, msgid: "%qs re-asserted",
3028 NODE_NAME (node) + 1);
3029 return;
3030 }
3031
3032 /* Commit or allocate storage for the answer. */
3033 answer = (cpp_macro *)_cpp_commit_buff
3034 (pfile, size: sizeof (cpp_macro) - sizeof (cpp_token)
3035 + sizeof (cpp_token) * answer->count);
3036
3037 /* Chain into the list. */
3038 answer->parm.next = node->value.answers;
3039 node->value.answers = answer;
3040
3041 check_eol (pfile, expand: false);
3042 }
3043}
3044
3045/* Handle #unassert. */
3046static void
3047do_unassert (cpp_reader *pfile)
3048{
3049 cpp_macro *answer;
3050 cpp_hashnode *node = parse_assertion (pfile, type: T_UNASSERT, answer_ptr: &answer);
3051
3052 /* It isn't an error to #unassert something that isn't asserted. */
3053 if (node)
3054 {
3055 if (answer)
3056 {
3057 cpp_macro **p = find_answer (node, candidate: answer);
3058
3059 /* Remove the assert from the list. */
3060 if (cpp_macro *temp = *p)
3061 *p = temp->parm.next;
3062
3063 check_eol (pfile, expand: false);
3064 }
3065 else
3066 _cpp_free_definition (node);
3067 }
3068
3069 /* We don't commit the memory for the answer - it's temporary only. */
3070}
3071
3072/* These are for -D, -U, -A. */
3073
3074/* Process the string STR as if it appeared as the body of a #define.
3075 If STR is just an identifier, define it with value 1.
3076 If STR has anything after the identifier, then it should
3077 be identifier=definition. */
3078void
3079cpp_define (cpp_reader *pfile, const char *str)
3080{
3081 char *buf;
3082 const char *p;
3083 size_t count;
3084
3085 /* Copy the entire option so we can modify it.
3086 Change the first "=" in the string to a space. If there is none,
3087 tack " 1" on the end. */
3088
3089 count = strlen (s: str);
3090 buf = (char *) alloca (count + 3);
3091 memcpy (dest: buf, src: str, n: count);
3092
3093 p = strchr (s: str, c: '=');
3094 if (p)
3095 buf[p - str] = ' ';
3096 else
3097 {
3098 buf[count++] = ' ';
3099 buf[count++] = '1';
3100 }
3101 buf[count] = '\n';
3102
3103 CPP_OPTION (pfile, suppress_builtin_macro_warnings) = 1;
3104 run_directive (pfile, dir_no: T_DEFINE, buf, count);
3105 CPP_OPTION (pfile, suppress_builtin_macro_warnings) = 0;
3106}
3107
3108/* Like cpp_define, but does not warn about unused macro. */
3109void
3110cpp_define_unused (cpp_reader *pfile, const char *str)
3111{
3112 unsigned char warn_unused_macros = CPP_OPTION (pfile, warn_unused_macros);
3113 CPP_OPTION (pfile, warn_unused_macros) = 0;
3114 cpp_define (pfile, str);
3115 CPP_OPTION (pfile, warn_unused_macros) = warn_unused_macros;
3116}
3117
3118/* Use to build macros to be run through cpp_define() as
3119 described above.
3120 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
3121
3122void
3123cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...)
3124{
3125 char *ptr;
3126
3127 va_list ap;
3128 va_start (ap, fmt);
3129 ptr = xvasprintf (fmt, ap);
3130 va_end (ap);
3131
3132 cpp_define (pfile, str: ptr);
3133 free (ptr: ptr);
3134}
3135
3136/* Like cpp_define_formatted, but does not warn about unused macro. */
3137void
3138cpp_define_formatted_unused (cpp_reader *pfile, const char *fmt, ...)
3139{
3140 char *ptr;
3141
3142 va_list ap;
3143 va_start (ap, fmt);
3144 ptr = xvasprintf (fmt, ap);
3145 va_end (ap);
3146
3147 cpp_define_unused (pfile, str: ptr);
3148 free (ptr: ptr);
3149}
3150
3151/* Slight variant of the above for use by initialize_builtins. */
3152void
3153_cpp_define_builtin (cpp_reader *pfile, const char *str)
3154{
3155 size_t len = strlen (s: str);
3156 char *buf = (char *) alloca (len + 1);
3157 memcpy (dest: buf, src: str, n: len);
3158 buf[len] = '\n';
3159 CPP_OPTION (pfile, suppress_builtin_macro_warnings) = 1;
3160 run_directive (pfile, dir_no: T_DEFINE, buf, count: len);
3161 CPP_OPTION (pfile, suppress_builtin_macro_warnings) = 0;
3162}
3163
3164/* Process MACRO as if it appeared as the body of an #undef. */
3165void
3166cpp_undef (cpp_reader *pfile, const char *macro)
3167{
3168 size_t len = strlen (s: macro);
3169 char *buf = (char *) alloca (len + 1);
3170 memcpy (dest: buf, src: macro, n: len);
3171 buf[len] = '\n';
3172 CPP_OPTION (pfile, suppress_builtin_macro_warnings) = 1;
3173 run_directive (pfile, dir_no: T_UNDEF, buf, count: len);
3174 CPP_OPTION (pfile, suppress_builtin_macro_warnings) = 0;
3175}
3176
3177/* Replace a previous definition DEF of the macro STR. If DEF is NULL,
3178 or first element is zero, then the macro should be undefined. */
3179static void
3180cpp_pop_definition (cpp_reader *pfile, def_pragma_macro *c, cpp_hashnode *node)
3181{
3182 if (pfile->cb.before_define)
3183 pfile->cb.before_define (pfile);
3184
3185 if (cpp_macro_p (node))
3186 {
3187 if (pfile->cb.undef)
3188 pfile->cb.undef (pfile, pfile->directive_line, node);
3189 if (CPP_OPTION (pfile, warn_unused_macros))
3190 _cpp_warn_if_unused_macro (pfile, node, NULL);
3191 _cpp_free_definition (node);
3192 }
3193
3194 if (c->is_undef)
3195 return;
3196 if (c->is_builtin)
3197 {
3198 _cpp_restore_special_builtin (pfile, c);
3199 return;
3200 }
3201
3202 {
3203 const auto namelen = ustrcspn (s1: c->definition, s2: "( \n");
3204 const auto dn = c->definition + namelen;
3205 const auto nbuf = cpp_push_buffer (pfile, dn, ustrchr (s1: dn, c: '\n') - dn,
3206 true);
3207 if (nbuf != NULL)
3208 {
3209 _cpp_clean_line (pfile);
3210 nbuf->sysp = 1;
3211 if (!_cpp_create_definition (pfile, node, 0))
3212 abort ();
3213 _cpp_pop_buffer (pfile);
3214 }
3215 else
3216 abort ();
3217 node->value.macro->line = c->line;
3218 node->value.macro->syshdr = c->syshdr;
3219 node->value.macro->used = c->used;
3220 }
3221}
3222
3223/* Process the string STR as if it appeared as the body of a #assert. */
3224void
3225cpp_assert (cpp_reader *pfile, const char *str)
3226{
3227 handle_assertion (pfile, str, T_ASSERT);
3228}
3229
3230/* Process STR as if it appeared as the body of an #unassert. */
3231void
3232cpp_unassert (cpp_reader *pfile, const char *str)
3233{
3234 handle_assertion (pfile, str, T_UNASSERT);
3235}
3236
3237/* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
3238static void
3239handle_assertion (cpp_reader *pfile, const char *str, int type)
3240{
3241 size_t count = strlen (s: str);
3242 const char *p = strchr (s: str, c: '=');
3243
3244 /* Copy the entire option so we can modify it. Change the first
3245 "=" in the string to a '(', and tack a ')' on the end. */
3246 char *buf = (char *) alloca (count + 2);
3247
3248 memcpy (dest: buf, src: str, n: count);
3249 if (p)
3250 {
3251 buf[p - str] = '(';
3252 buf[count++] = ')';
3253 }
3254 buf[count] = '\n';
3255 str = buf;
3256
3257 run_directive (pfile, dir_no: type, buf: str, count);
3258}
3259
3260/* The options structure. */
3261cpp_options *
3262cpp_get_options (cpp_reader *pfile)
3263{
3264 return &pfile->opts;
3265}
3266
3267/* The callbacks structure. */
3268cpp_callbacks *
3269cpp_get_callbacks (cpp_reader *pfile)
3270{
3271 return &pfile->cb;
3272}
3273
3274/* Copy the given callbacks structure to our own. */
3275void
3276cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
3277{
3278 pfile->cb = *cb;
3279}
3280
3281/* The narrow character set identifier. */
3282const char *
3283cpp_get_narrow_charset_name (cpp_reader *pfile)
3284{
3285 return pfile->narrow_cset_desc.to;
3286}
3287
3288/* The wide character set identifier. */
3289const char *
3290cpp_get_wide_charset_name (cpp_reader *pfile)
3291{
3292 return pfile->wide_cset_desc.to;
3293}
3294
3295/* The dependencies structure. (Creates one if it hasn't already been.) */
3296class mkdeps *
3297cpp_get_deps (cpp_reader *pfile)
3298{
3299 if (!pfile->deps && CPP_OPTION (pfile, deps.style) != DEPS_NONE)
3300 pfile->deps = deps_init ();
3301 return pfile->deps;
3302}
3303
3304/* Push a new buffer on the buffer stack. Returns the new buffer; it
3305 doesn't fail. It does not generate a file change call back; that
3306 is the responsibility of the caller. */
3307cpp_buffer *
3308cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
3309 int from_stage3)
3310{
3311 cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
3312
3313 /* Clears, amongst other things, if_stack and mi_cmacro. */
3314 memset (s: new_buffer, c: 0, n: sizeof (cpp_buffer));
3315
3316 new_buffer->next_line = new_buffer->buf = buffer;
3317 new_buffer->rlimit = buffer + len;
3318 new_buffer->from_stage3 = from_stage3;
3319 new_buffer->prev = pfile->buffer;
3320 new_buffer->need_line = true;
3321
3322 pfile->buffer = new_buffer;
3323
3324 return new_buffer;
3325}
3326
3327/* Pops a single buffer, with a file change call-back if appropriate.
3328 Then pushes the next -include file, if any remain. */
3329void
3330_cpp_pop_buffer (cpp_reader *pfile)
3331{
3332 cpp_buffer *buffer = pfile->buffer;
3333 struct _cpp_file *inc = buffer->file;
3334 struct if_stack *ifs;
3335 const unsigned char *to_free;
3336
3337 /* Walk back up the conditional stack till we reach its level at
3338 entry to this file, issuing error messages. */
3339 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
3340 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
3341 msgid: "unterminated #%s", dtable[ifs->type].name);
3342
3343 /* In case of a missing #endif. */
3344 pfile->state.skipping = 0;
3345
3346 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
3347 pfile->buffer = buffer->prev;
3348
3349 to_free = buffer->to_free;
3350 free (ptr: buffer->notes);
3351
3352 /* Free the buffer object now; we may want to push a new buffer
3353 in _cpp_push_next_include_file. */
3354 obstack_free (&pfile->buffer_ob, buffer);
3355
3356 if (inc)
3357 {
3358 _cpp_pop_file_buffer (pfile, inc, to_free);
3359
3360 _cpp_do_file_change (pfile, reason: LC_LEAVE, to_file: 0, to_line: 0, sysp: 0);
3361 }
3362 else if (to_free)
3363 free (ptr: (void *)to_free);
3364}
3365
3366/* Enter all recognized directives in the hash table. */
3367void
3368_cpp_init_directives (cpp_reader *pfile)
3369{
3370 for (int i = 0; i < N_DIRECTIVES; i++)
3371 {
3372 cpp_hashnode *node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
3373 node->is_directive = 1;
3374 node->directive_index = i;
3375 }
3376}
3377
3378/* Extract header file from a bracket include. Parsing starts after '<'.
3379 The string is malloced and must be freed by the caller. */
3380char *
3381_cpp_bracket_include(cpp_reader *pfile)
3382{
3383 return glue_header_name (pfile);
3384}
3385
3386

source code of libcpp/directives.cc