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

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of libcpp/directives.cc