]> git.ipfire.org Git - thirdparty/gcc.git/blame - libcpp/directives.c
Update copyright years.
[thirdparty/gcc.git] / libcpp / directives.c
CommitLineData
a949941c 1/* CPP Library. (Directive handling.)
7adcbafe 2 Copyright (C) 1986-2022 Free Software Foundation, Inc.
4c8cc616 3 Contributed by Per Bothner, 1994-95.
d8bfa78c 4 Based on CCCP program by Paul Rubin, June 1986
7f2935c7
PB
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
748086b7 9Free Software Foundation; either version 3, or (at your option) any
7f2935c7
PB
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
748086b7
JJ
18along with this program; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
7f2935c7 20
956d6950 21#include "config.h"
b04cd507 22#include "system.h"
956d6950 23#include "cpplib.h"
4f4e53dd 24#include "internal.h"
c6e83800 25#include "mkdeps.h"
c71f835b 26#include "obstack.h"
7f2935c7 27
88ae23e7
ZW
28/* Stack of conditionals currently in progress
29 (including both successful and failing conditionals). */
88ae23e7
ZW
30struct if_stack
31{
32 struct if_stack *next;
620e594b 33 location_t line; /* Line where condition started. */
93c80368 34 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
cef0d199
NB
35 bool skip_elses; /* Can future #else / #elif be skipped? */
36 bool was_skipping; /* If were skipping on entry. */
6cf87ca4 37 int type; /* Most recent conditional for diagnostics. */
88ae23e7 38};
88ae23e7 39
a5da89c6 40/* Contains a registered pragma or pragma namespace. */
6cf87ca4 41typedef void (*pragma_cb) (cpp_reader *);
a5da89c6
NB
42struct pragma_entry
43{
44 struct pragma_entry *next;
4b115ff0 45 const cpp_hashnode *pragma; /* Name and length. */
21b11495
ZW
46 bool is_nspace;
47 bool is_internal;
bc4071dd
RH
48 bool is_deferred;
49 bool allow_expansion;
a5da89c6
NB
50 union {
51 pragma_cb handler;
52 struct pragma_entry *space;
bc4071dd 53 unsigned int ident;
a5da89c6
NB
54 } u;
55};
56
93c80368
NB
57/* Values for the origin field of struct directive. KANDR directives
58 come from traditional (K&R) C. STDC89 directives come from the
71d38ec8
JM
59 1989 C standard. STDC2X directives come from the C2X standard. EXTENSION
60 directives are extensions. */
93c80368
NB
61#define KANDR 0
62#define STDC89 1
71d38ec8
JM
63#define STDC2X 2
64#define EXTENSION 3
93c80368
NB
65
66/* Values for the flags field of struct directive. COND indicates a
67 conditional; IF_COND an opening conditional. INCL means to treat
68 "..." and <...> as q-char and h-char sequences respectively. IN_I
69 means this directive should be handled even if -fpreprocessed is in
1a76916c
NB
70 effect (these are the directives with callback hooks).
71
71d38ec8
JM
72 EXPAND is set on directives that are always macro-expanded.
73
74 ELIFDEF is set on directives that are only handled for standards with the
75 #elifdef / #elifndef feature. */
93c80368
NB
76#define COND (1 << 0)
77#define IF_COND (1 << 1)
78#define INCL (1 << 2)
79#define IN_I (1 << 3)
1a76916c 80#define EXPAND (1 << 4)
899015a0 81#define DEPRECATED (1 << 5)
71d38ec8 82#define ELIFDEF (1 << 6)
93c80368
NB
83
84/* Defines one #-directive, including how to handle it. */
6cf87ca4 85typedef void (*directive_handler) (cpp_reader *);
93c80368
NB
86typedef struct directive directive;
87struct directive
88{
89 directive_handler handler; /* Function to handle directive. */
562a5c27 90 const uchar *name; /* Name of directive. */
93c80368
NB
91 unsigned short length; /* Length of name. */
92 unsigned char origin; /* Origin of directive. */
93 unsigned char flags; /* Flags describing this directive. */
94};
95
1316f1f7
ZW
96/* Forward declarations. */
97
6cf87ca4 98static void skip_rest_of_line (cpp_reader *);
a5cb563b 99static void check_eol (cpp_reader *, bool);
6cf87ca4
ZW
100static void start_directive (cpp_reader *);
101static void prepare_directive_trad (cpp_reader *);
102static void end_directive (cpp_reader *, int);
103static void directive_diagnostics (cpp_reader *, const directive *, int);
104static void run_directive (cpp_reader *, int, const char *, size_t);
105static char *glue_header_name (cpp_reader *);
a28fbdba 106static const char *parse_include (cpp_reader *, int *, const cpp_token ***,
620e594b 107 location_t *);
6cf87ca4
ZW
108static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
109static unsigned int read_flag (cpp_reader *, unsigned int);
3b8f20a1 110static bool strtolinenum (const uchar *, size_t, linenum_type *, bool *);
c24300ba
DM
111static void do_diagnostic (cpp_reader *, enum cpp_diagnostic_level code,
112 enum cpp_warning_reason reason, int);
ee1c2a10 113static cpp_hashnode *lex_macro_node (cpp_reader *, bool);
d1bd0ded 114static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
6cf87ca4
ZW
115static void do_include_common (cpp_reader *, enum include_type);
116static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
117 const cpp_hashnode *);
6cf87ca4
ZW
118static int count_registered_pragmas (struct pragma_entry *);
119static char ** save_registered_pragmas (struct pragma_entry *, char **);
120static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
121 char **);
122static void do_pragma_once (cpp_reader *);
123static void do_pragma_poison (cpp_reader *);
124static void do_pragma_system_header (cpp_reader *);
125static void do_pragma_dependency (cpp_reader *);
f591bd8f
FW
126static void do_pragma_warning_or_error (cpp_reader *, bool error);
127static void do_pragma_warning (cpp_reader *);
128static void do_pragma_error (cpp_reader *);
6cf87ca4
ZW
129static void do_linemarker (cpp_reader *);
130static const cpp_token *get_token_no_padding (cpp_reader *);
131static const cpp_token *get__Pragma_string (cpp_reader *);
e2eb5056 132static void destringize_and_run (cpp_reader *, const cpp_string *,
620e594b
DM
133 location_t);
134static bool parse_answer (cpp_reader *, int, location_t, cpp_macro **);
3fb558b1
NS
135static cpp_hashnode *parse_assertion (cpp_reader *, int, cpp_macro **);
136static cpp_macro **find_answer (cpp_hashnode *, const cpp_macro *);
6cf87ca4 137static void handle_assertion (cpp_reader *, const char *, int);
17e7cb85
KT
138static void do_pragma_push_macro (cpp_reader *);
139static void do_pragma_pop_macro (cpp_reader *);
d6874138 140static void cpp_pop_definition (cpp_reader *, struct def_pragma_macro *);
d481b69b 141
ff65e980
NS
142/* This is the table of directive handlers. All extensions other than
143 #warning, #include_next, and #import are deprecated. The name is
144 where the extension appears to have come from. */
168d3732 145
93c80368 146#define DIRECTIVE_TABLE \
ff65e980
NS
147 D(define, T_DEFINE = 0, KANDR, IN_I) \
148 D(include, T_INCLUDE, KANDR, INCL | EXPAND) \
149 D(endif, T_ENDIF, KANDR, COND) \
150 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) \
151 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) \
152 D(else, T_ELSE, KANDR, COND) \
153 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) \
154 D(undef, T_UNDEF, KANDR, IN_I) \
155 D(line, T_LINE, KANDR, EXPAND) \
156 D(elif, T_ELIF, STDC89, COND | EXPAND) \
71d38ec8
JM
157 D(elifdef, T_ELIFDEF, STDC2X, COND | ELIFDEF) \
158 D(elifndef, T_ELIFNDEF, STDC2X, COND | ELIFDEF) \
ff65e980
NS
159 D(error, T_ERROR, STDC89, 0) \
160 D(pragma, T_PRAGMA, STDC89, IN_I) \
161 D(warning, T_WARNING, EXTENSION, 0) \
162 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) \
163 D(ident, T_IDENT, EXTENSION, IN_I) \
164 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* ObjC */ \
165 D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* SVR4 */ \
166 D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* SVR4 */ \
167 D(sccs, T_SCCS, EXTENSION, IN_I) /* SVR4? */
1ed17cd5
ZW
168
169/* #sccs is synonymous with #ident. */
170#define do_sccs do_ident
07aa0b04 171
168d3732
ZW
172/* Use the table to generate a series of prototypes, an enum for the
173 directive names, and an array of directive handlers. */
174
6cf87ca4 175#define D(name, t, o, f) static void do_##name (cpp_reader *);
168d3732
ZW
176DIRECTIVE_TABLE
177#undef D
178
041c3194 179#define D(n, tag, o, f) tag,
168d3732
ZW
180enum
181{
182 DIRECTIVE_TABLE
183 N_DIRECTIVES
7f2935c7 184};
168d3732
ZW
185#undef D
186
041c3194 187#define D(name, t, origin, flags) \
9a238586
KG
188{ do_##name, (const uchar *) #name, \
189 sizeof #name - 1, origin, flags },
93c80368 190static const directive dtable[] =
168d3732
ZW
191{
192DIRECTIVE_TABLE
193};
194#undef D
cb18fd07
DM
195
196/* A NULL-terminated array of directive names for use
197 when suggesting corrections for misspelled directives. */
198#define D(name, t, origin, flags) #name,
199static const char * const directive_names[] = {
200DIRECTIVE_TABLE
201 NULL
202};
203#undef D
204
168d3732 205#undef DIRECTIVE_TABLE
7f2935c7 206
dcc229e5
ZW
207/* Wrapper struct directive for linemarkers.
208 The origin is more or less true - the original K+R cpp
209 did use this notation in its preprocessed output. */
210static const directive linemarker_dir =
211{
b6baa67d 212 do_linemarker, UC"#", 1, KANDR, IN_I
dcc229e5
ZW
213};
214
93c80368
NB
215/* Skip any remaining tokens in a directive. */
216static void
6cf87ca4 217skip_rest_of_line (cpp_reader *pfile)
c5a04734 218{
93c80368 219 /* Discard all stacked contexts. */
8128cccf 220 while (pfile->context->prev)
93c80368
NB
221 _cpp_pop_context (pfile);
222
b528a07e 223 /* Sweep up all tokens remaining on the line. */
345894b4
NB
224 if (! SEEN_EOL ())
225 while (_cpp_lex_token (pfile)->type != CPP_EOF)
226 ;
93c80368 227}
c5a04734 228
81b5d104
MLI
229/* Helper function for check_oel. */
230
93c80368 231static void
c24300ba 232check_eol_1 (cpp_reader *pfile, bool expand, enum cpp_warning_reason reason)
93c80368 233{
a5cb563b
JM
234 if (! SEEN_EOL () && (expand
235 ? cpp_get_token (pfile)
236 : _cpp_lex_token (pfile))->type != CPP_EOF)
81b5d104
MLI
237 cpp_pedwarning (pfile, reason, "extra tokens at end of #%s directive",
238 pfile->directive->name);
239}
240
241/* Variant of check_eol used for Wendif-labels warnings. */
242
243static void
244check_eol_endif_labels (cpp_reader *pfile)
245{
246 check_eol_1 (pfile, false, CPP_W_ENDIF_LABELS);
247}
248
249/* Ensure there are no stray tokens at the end of a directive. If
250 EXPAND is true, tokens macro-expanding to nothing are allowed. */
251
252static void
253check_eol (cpp_reader *pfile, bool expand)
254{
255 check_eol_1 (pfile, expand, CPP_W_NONE);
93c80368
NB
256}
257
cbc43ae0
ILT
258/* Ensure there are no stray tokens other than comments at the end of
259 a directive, and gather the comments. */
260static const cpp_token **
261check_eol_return_comments (cpp_reader *pfile)
262{
263 size_t c;
264 size_t capacity = 8;
265 const cpp_token **buf;
266
267 buf = XNEWVEC (const cpp_token *, capacity);
268 c = 0;
269 if (! SEEN_EOL ())
270 {
271 while (1)
272 {
273 const cpp_token *tok;
274
275 tok = _cpp_lex_token (pfile);
276 if (tok->type == CPP_EOF)
277 break;
278 if (tok->type != CPP_COMMENT)
279 cpp_error (pfile, CPP_DL_PEDWARN,
280 "extra tokens at end of #%s directive",
281 pfile->directive->name);
282 else
283 {
284 if (c + 1 >= capacity)
285 {
286 capacity *= 2;
287 buf = XRESIZEVEC (const cpp_token *, buf, capacity);
288 }
289 buf[c] = tok;
290 ++c;
291 }
292 }
293 }
294 buf[c] = NULL;
295 return buf;
296}
297
fe6c2db9
NB
298/* Called when entering a directive, _Pragma or command-line directive. */
299static void
6cf87ca4 300start_directive (cpp_reader *pfile)
93c80368 301{
7f2f1a66
NB
302 /* Setup in-directive state. */
303 pfile->state.in_directive = 1;
304 pfile->state.save_comments = 0;
21b11495 305 pfile->directive_result.type = CPP_PADDING;
7f2f1a66 306
93c80368 307 /* Some handlers need the position of the # for diagnostics. */
500bee0a 308 pfile->directive_line = pfile->line_table->highest_line;
fe6c2db9
NB
309}
310
311/* Called when leaving a directive, _Pragma or command-line directive. */
312static void
6cf87ca4 313end_directive (cpp_reader *pfile, int skip_line)
fe6c2db9 314{
c5a62c6f 315 if (CPP_OPTION (pfile, traditional))
1a76916c 316 {
d97371e0 317 /* Revert change of prepare_directive_trad. */
c5a62c6f
EB
318 if (!pfile->state.in_deferred_pragma)
319 pfile->state.prevent_expansion--;
d97371e0 320
b66377c1 321 if (pfile->directive != &dtable[T_DEFINE])
1a76916c
NB
322 _cpp_remove_overlay (pfile);
323 }
c5a62c6f
EB
324 else if (pfile->state.in_deferred_pragma)
325 ;
fe6c2db9 326 /* We don't skip for an assembler #. */
b66377c1 327 else if (skip_line)
67821e3a
NB
328 {
329 skip_rest_of_line (pfile);
bdcbe496
NB
330 if (!pfile->keep_tokens)
331 {
332 pfile->cur_run = &pfile->base_run;
333 pfile->cur_token = pfile->base_run.base;
334 }
67821e3a 335 }
fe6c2db9
NB
336
337 /* Restore state. */
fe6c2db9
NB
338 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
339 pfile->state.in_directive = 0;
d97371e0 340 pfile->state.in_expression = 0;
fe6c2db9
NB
341 pfile->state.angled_headers = 0;
342 pfile->directive = 0;
343}
344
1a76916c
NB
345/* Prepare to handle the directive in pfile->directive. */
346static void
6cf87ca4 347prepare_directive_trad (cpp_reader *pfile)
1a76916c 348{
951a0766 349 if (pfile->directive != &dtable[T_DEFINE])
1a76916c 350 {
b66377c1
NB
351 bool no_expand = (pfile->directive
352 && ! (pfile->directive->flags & EXPAND));
974c43f1 353 bool was_skipping = pfile->state.skipping;
1a76916c 354
d97371e0
NB
355 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
356 || pfile->directive == &dtable[T_ELIF]);
45f2492c
NB
357 if (pfile->state.in_expression)
358 pfile->state.skipping = false;
359
1a76916c
NB
360 if (no_expand)
361 pfile->state.prevent_expansion++;
fb136e35 362 _cpp_scan_out_logical_line (pfile, NULL, false);
1a76916c
NB
363 if (no_expand)
364 pfile->state.prevent_expansion--;
45f2492c 365
974c43f1 366 pfile->state.skipping = was_skipping;
1a76916c
NB
367 _cpp_overlay_buffer (pfile, pfile->out.base,
368 pfile->out.cur - pfile->out.base);
369 }
d97371e0
NB
370
371 /* Stop ISO C from expanding anything. */
372 pfile->state.prevent_expansion++;
1a76916c
NB
373}
374
da7d8304 375/* Output diagnostics for a directive DIR. INDENTED is nonzero if
18a9d8ff 376 the '#' was indented. */
18a9d8ff 377static void
6cf87ca4 378directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
18a9d8ff 379{
899015a0
TT
380 /* Issue -pedantic or deprecated warnings for extensions. We let
381 -pedantic take precedence if both are applicable. */
382 if (! pfile->state.skipping)
383 {
384 if (dir->origin == EXTENSION
385 && !(dir == &dtable[T_IMPORT] && CPP_OPTION (pfile, objc))
386 && CPP_PEDANTIC (pfile))
387 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
388 else if (((dir->flags & DEPRECATED) != 0
389 || (dir == &dtable[T_IMPORT] && !CPP_OPTION (pfile, objc)))
e3339d0f 390 && CPP_OPTION (pfile, cpp_warn_deprecated))
87cf0651
SB
391 cpp_warning (pfile, CPP_W_DEPRECATED,
392 "#%s is a deprecated GCC extension", dir->name);
899015a0 393 }
dcc229e5
ZW
394
395 /* Traditionally, a directive is ignored unless its # is in
396 column 1. Therefore in code intended to work with K+R
397 compilers, directives added by C89 must have their #
398 indented, and directives present in traditional C must not.
399 This is true even of directives in skipped conditional
400 blocks. #elif cannot be used at all. */
401 if (CPP_WTRADITIONAL (pfile))
18a9d8ff 402 {
dcc229e5 403 if (dir == &dtable[T_ELIF])
87cf0651
SB
404 cpp_warning (pfile, CPP_W_TRADITIONAL,
405 "suggest not using #elif in traditional C");
dcc229e5 406 else if (indented && dir->origin == KANDR)
87cf0651
SB
407 cpp_warning (pfile, CPP_W_TRADITIONAL,
408 "traditional C ignores #%s with the # indented",
409 dir->name);
dcc229e5 410 else if (!indented && dir->origin != KANDR)
87cf0651
SB
411 cpp_warning (pfile, CPP_W_TRADITIONAL,
412 "suggest hiding #%s from traditional C with an indented #",
413 dir->name);
18a9d8ff
NB
414 }
415}
416
a0be978a 417/* Check if we have a known directive. INDENTED is true if the
18a9d8ff 418 '#' of the directive was indented. This function is in this file
a2566ae9 419 to save unnecessarily exporting dtable etc. to lex.c. Returns
da7d8304 420 nonzero if the line of tokens has been handled, zero if we should
18a9d8ff 421 continue processing the line. */
fe6c2db9 422int
a0be978a 423_cpp_handle_directive (cpp_reader *pfile, bool indented)
fe6c2db9 424{
fe6c2db9 425 const directive *dir = 0;
345894b4 426 const cpp_token *dname;
e808ec9c 427 bool was_parsing_args = pfile->state.parsing_args;
c6e83800 428 bool was_discarding_output = pfile->state.discarding_output;
fe6c2db9
NB
429 int skip = 1;
430
c6e83800
ZW
431 if (was_discarding_output)
432 pfile->state.prevent_expansion = 0;
433
e808ec9c
NB
434 if (was_parsing_args)
435 {
e3339d0f 436 if (CPP_OPTION (pfile, cpp_pedantic))
0527bc4e 437 cpp_error (pfile, CPP_DL_PEDWARN,
e808ec9c
NB
438 "embedding a directive within macro arguments is not portable");
439 pfile->state.parsing_args = 0;
440 pfile->state.prevent_expansion = 0;
441 }
fe6c2db9 442 start_directive (pfile);
345894b4 443 dname = _cpp_lex_token (pfile);
0d9f234d 444
345894b4 445 if (dname->type == CPP_NAME)
0d9f234d 446 {
9a0c6187 447 if (dname->val.node.node->is_directive)
71d38ec8
JM
448 {
449 dir = &dtable[dname->val.node.node->directive_index];
f43eb770
JJ
450 if ((dir->flags & ELIFDEF)
451 && !CPP_OPTION (pfile, elifdef)
452 /* For -std=gnu* modes elifdef is supported with
453 a pedwarn if pedantic. */
454 && CPP_OPTION (pfile, std))
71d38ec8
JM
455 dir = 0;
456 }
0d9f234d 457 }
05713b80 458 /* We do not recognize the # followed by a number extension in
18a9d8ff 459 assembler code. */
345894b4 460 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
0d9f234d 461 {
dcc229e5
ZW
462 dir = &linemarker_dir;
463 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
464 && ! pfile->state.skipping)
0527bc4e 465 cpp_error (pfile, CPP_DL_PEDWARN,
ebef4e8c 466 "style of line directive is a GCC extension");
93c80368 467 }
0d9f234d 468
93c80368
NB
469 if (dir)
470 {
18a9d8ff
NB
471 /* If we have a directive that is not an opening conditional,
472 invalidate any control macro. */
473 if (! (dir->flags & IF_COND))
474 pfile->mi_valid = false;
475
476 /* Kluge alert. In order to be sure that code like this
477
478 #define HASH #
479 HASH define foo bar
480
481 does not cause '#define foo bar' to get executed when
482 compiled with -save-temps, we recognize directives in
a2566ae9 483 -fpreprocessed mode only if the # is in column 1. macro.c
ccfc4c91
OW
484 puts a space in front of any '#' at the start of a macro.
485
486 We exclude the -fdirectives-only case because macro expansion
487 has not been performed yet, and block comments can cause spaces
7d9641cc 488 to precede the directive. */
18a9d8ff 489 if (CPP_OPTION (pfile, preprocessed)
ccfc4c91 490 && !CPP_OPTION (pfile, directives_only)
18a9d8ff 491 && (indented || !(dir->flags & IN_I)))
6d4587f7 492 {
18a9d8ff
NB
493 skip = 0;
494 dir = 0;
6d4587f7
ZW
495 }
496 else
93c80368 497 {
18a9d8ff
NB
498 /* In failed conditional groups, all non-conditional
499 directives are ignored. Before doing that, whether
500 skipping or not, we should lex angle-bracketed headers
501 correctly, and maybe output some diagnostics. */
502 pfile->state.angled_headers = dir->flags & INCL;
a8d0ddaf 503 pfile->state.directive_wants_padding = dir->flags & INCL;
18a9d8ff
NB
504 if (! CPP_OPTION (pfile, preprocessed))
505 directive_diagnostics (pfile, dir, indented);
506 if (pfile->state.skipping && !(dir->flags & COND))
507 dir = 0;
93c80368
NB
508 }
509 }
345894b4 510 else if (dname->type == CPP_EOF)
18a9d8ff
NB
511 ; /* CPP_EOF is the "null directive". */
512 else
93c80368
NB
513 {
514 /* An unknown directive. Don't complain about it in assembly
515 source: we don't know where the comments are, and # may
516 introduce assembler pseudo-ops. Don't complain about invalid
517 directives in skipped conditional groups (6.10 p4). */
bdb05a7b 518 if (CPP_OPTION (pfile, lang) == CLK_ASM)
18a9d8ff
NB
519 skip = 0;
520 else if (!pfile->state.skipping)
cb18fd07
DM
521 {
522 const char *unrecognized
523 = (const char *)cpp_token_as_text (pfile, dname);
524 const char *hint = NULL;
525
526 /* Call back into gcc to get a spelling suggestion. Ideally
527 we'd just use best_match from gcc/spellcheck.h (and filter
528 out the uncommon directives), but that requires moving it
529 to a support library. */
530 if (pfile->cb.get_suggestion)
531 hint = pfile->cb.get_suggestion (pfile, unrecognized,
532 directive_names);
533
534 if (hint)
535 {
536 rich_location richloc (pfile->line_table, dname->src_loc);
537 source_range misspelled_token_range
538 = get_range_from_loc (pfile->line_table, dname->src_loc);
539 richloc.add_fixit_replace (misspelled_token_range, hint);
64a5912c
DM
540 cpp_error_at (pfile, CPP_DL_ERROR, &richloc,
541 "invalid preprocessing directive #%s;"
542 " did you mean #%s?",
543 unrecognized, hint);
cb18fd07
DM
544 }
545 else
546 cpp_error (pfile, CPP_DL_ERROR,
547 "invalid preprocessing directive #%s",
548 unrecognized);
549 }
0d9f234d
NB
550 }
551
d1a58688
NB
552 pfile->directive = dir;
553 if (CPP_OPTION (pfile, traditional))
554 prepare_directive_trad (pfile);
555
18a9d8ff 556 if (dir)
6cf87ca4 557 pfile->directive->handler (pfile);
18a9d8ff
NB
558 else if (skip == 0)
559 _cpp_backup_tokens (pfile, 1);
560
561 end_directive (pfile, skip);
765d600a 562 if (was_parsing_args && !pfile->state.in_deferred_pragma)
e808ec9c
NB
563 {
564 /* Restore state when within macro args. */
565 pfile->state.parsing_args = 2;
566 pfile->state.prevent_expansion = 1;
e808ec9c 567 }
c6e83800
ZW
568 if (was_discarding_output)
569 pfile->state.prevent_expansion = 1;
fe6c2db9 570 return skip;
041c3194 571}
7f2935c7 572
93c80368 573/* Directive handler wrapper used by the command line option
26aea073 574 processor. BUF is \n terminated. */
93c80368 575static void
6cf87ca4 576run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
3fdc651f 577{
562a5c27 578 cpp_push_buffer (pfile, (const uchar *) buf, count,
40de9f76 579 /* from_stage3 */ true);
0bda4760 580 start_directive (pfile);
26aea073
NB
581
582 /* This is a short-term fix to prevent a leading '#' being
583 interpreted as a directive. */
584 _cpp_clean_line (pfile);
585
f71aebba 586 pfile->directive = &dtable[dir_no];
1a76916c
NB
587 if (CPP_OPTION (pfile, traditional))
588 prepare_directive_trad (pfile);
6cf87ca4 589 pfile->directive->handler (pfile);
0bda4760 590 end_directive (pfile, 1);
ef6e958a 591 _cpp_pop_buffer (pfile);
93c80368
NB
592}
593
594/* Checks for validity the macro name in #define, #undef, #ifdef and
ee1c2a10
TT
595 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
596 processing a #define or #undefine directive, and false
597 otherwise. */
041c3194 598static cpp_hashnode *
ee1c2a10 599lex_macro_node (cpp_reader *pfile, bool is_def_or_undef)
041c3194 600{
1a76916c 601 const cpp_token *token = _cpp_lex_token (pfile);
ea4a453b 602
92936ecf 603 /* The token immediately after #define must be an identifier. That
b8363a24
ZW
604 identifier may not be "defined", per C99 6.10.8p4.
605 In C++, it may not be any of the "named operators" either,
606 per C++98 [lex.digraph], [lex.key].
607 Finally, the identifier may not have been poisoned. (In that case
1d63a28a 608 the lexer has issued the error message for us.) */
cbc69f84 609
1a76916c
NB
610 if (token->type == CPP_NAME)
611 {
9a0c6187 612 cpp_hashnode *node = token->val.node.node;
92936ecf 613
ad1a3914 614 if (is_def_or_undef
3d056cbf 615 && node == pfile->spec_nodes.n_defined)
0527bc4e 616 cpp_error (pfile, CPP_DL_ERROR,
ad1a3914
NS
617 "\"%s\" cannot be used as a macro name",
618 NODE_NAME (node));
1a76916c
NB
619 else if (! (node->flags & NODE_POISONED))
620 return node;
ba89d661 621 }
1a76916c 622 else if (token->flags & NAMED_OP)
0527bc4e 623 cpp_error (pfile, CPP_DL_ERROR,
cbc69f84 624 "\"%s\" cannot be used as a macro name as it is an operator in C++",
9a0c6187 625 NODE_NAME (token->val.node.node));
1a76916c 626 else if (token->type == CPP_EOF)
0527bc4e 627 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
1a76916c
NB
628 pfile->directive->name);
629 else
0527bc4e 630 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
07aa0b04 631
cbc69f84 632 return NULL;
7f2935c7 633}
7f2935c7 634
a2566ae9 635/* Process a #define directive. Most work is done in macro.c. */
711b8824 636static void
6cf87ca4 637do_define (cpp_reader *pfile)
7f2935c7 638{
ee1c2a10 639 cpp_hashnode *node = lex_macro_node (pfile, true);
ff2b53ef 640
93c80368
NB
641 if (node)
642 {
1d63a28a
NB
643 /* If we have been requested to expand comments into macros,
644 then re-enable saving of comments. */
645 pfile->state.save_comments =
646 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
647
93d45d9e
JM
648 if (pfile->cb.before_define)
649 pfile->cb.before_define (pfile);
650
93c80368
NB
651 if (_cpp_create_definition (pfile, node))
652 if (pfile->cb.define)
6cf87ca4 653 pfile->cb.define (pfile, pfile->directive_line, node);
93d45d9e
JM
654
655 node->flags &= ~NODE_USED;
93c80368 656 }
041c3194
ZW
657}
658
5d8ebbd8 659/* Handle #undef. Mark the identifier NT_VOID in the hash table. */
711b8824 660static void
6cf87ca4 661do_undef (cpp_reader *pfile)
041c3194 662{
ee1c2a10 663 cpp_hashnode *node = lex_macro_node (pfile, true);
9e62c811 664
45f2492c 665 if (node)
9e62c811 666 {
93d45d9e
JM
667 if (pfile->cb.before_define)
668 pfile->cb.before_define (pfile);
669
58fea6af 670 if (pfile->cb.undef)
6cf87ca4 671 pfile->cb.undef (pfile, pfile->directive_line, node);
9e62c811 672
45f2492c
NB
673 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
674 identifier is not currently defined as a macro name. */
7692e253 675 if (cpp_macro_p (node))
45f2492c
NB
676 {
677 if (node->flags & NODE_WARN)
678 cpp_error (pfile, CPP_DL_WARNING,
679 "undefining \"%s\"", NODE_NAME (node));
7692e253 680 else if (cpp_builtin_macro_p (node)
b753b37b
MLI
681 && CPP_OPTION (pfile, warn_builtin_macro_redefined))
682 cpp_warning_with_line (pfile, CPP_W_BUILTIN_MACRO_REDEFINED,
683 pfile->directive_line, 0,
684 "undefining \"%s\"", NODE_NAME (node));
9e62c811 685
13f93cf5
NS
686 if (node->value.macro
687 && CPP_OPTION (pfile, warn_unused_macros))
45f2492c 688 _cpp_warn_if_unused_macro (pfile, node, NULL);
a69cbaac 689
45f2492c
NB
690 _cpp_free_definition (node);
691 }
9e62c811 692 }
45f2492c 693
a5cb563b 694 check_eol (pfile, false);
7f2935c7
PB
695}
696
d1bd0ded
GK
697/* Undefine a single macro/assertion/whatever. */
698
699static int
c6e83800 700undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
d1bd0ded
GK
701 void *data_p ATTRIBUTE_UNUSED)
702{
c6e83800
ZW
703 /* Body of _cpp_free_definition inlined here for speed.
704 Macros and assertions no longer have anything to free. */
705 h->type = NT_VOID;
a570d97f
NS
706 h->value.answers = NULL;
707 h->flags &= ~(NODE_POISONED|NODE_DISABLED|NODE_USED);
d1bd0ded
GK
708 return 1;
709}
710
711/* Undefine all macros and assertions. */
712
713void
714cpp_undef_all (cpp_reader *pfile)
715{
716 cpp_forall_identifiers (pfile, undefine_macros, NULL);
717}
718
719
93c80368
NB
720/* Helper routine used by parse_include. Reinterpret the current line
721 as an h-char-sequence (< ... >); we are looking at the first token
74eb4b3e
NB
722 after the <. Returns a malloced filename. */
723static char *
6cf87ca4 724glue_header_name (cpp_reader *pfile)
93c80368 725{
4ed5bcfb 726 const cpp_token *token;
74eb4b3e 727 char *buffer;
2450e0b8 728 size_t len, total_len = 0, capacity = 1024;
93c80368
NB
729
730 /* To avoid lexed tokens overwriting our glued name, we can only
731 allocate from the string pool once we've lexed everything. */
c3f829c1 732 buffer = XNEWVEC (char, capacity);
93c80368
NB
733 for (;;)
734 {
a8d0ddaf 735 token = get_token_no_padding (pfile);
93c80368 736
74eb4b3e 737 if (token->type == CPP_GREATER)
93c80368 738 break;
74eb4b3e
NB
739 if (token->type == CPP_EOF)
740 {
0527bc4e 741 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
74eb4b3e
NB
742 break;
743 }
93c80368 744
59325650 745 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
2450e0b8 746 if (total_len + len > capacity)
93c80368 747 {
2450e0b8 748 capacity = (capacity + len) * 2;
c3f829c1 749 buffer = XRESIZEVEC (char, buffer, capacity);
93c80368
NB
750 }
751
4ed5bcfb 752 if (token->flags & PREV_WHITE)
2450e0b8 753 buffer[total_len++] = ' ';
93c80368 754
47e20491
GK
755 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
756 true)
74eb4b3e 757 - (uchar *) buffer);
93c80368 758 }
041c3194 759
74eb4b3e
NB
760 buffer[total_len] = '\0';
761 return buffer;
93c80368 762}
7f2935c7 763
74eb4b3e
NB
764/* Returns the file name of #include, #include_next, #import and
765 #pragma dependency. The string is malloced and the caller should
a28fbdba
MLI
766 free it. Returns NULL on error. LOCATION is the source location
767 of the file name. */
768
74eb4b3e 769static const char *
cbc43ae0 770parse_include (cpp_reader *pfile, int *pangle_brackets,
620e594b 771 const cpp_token ***buf, location_t *location)
7f2935c7 772{
74eb4b3e 773 char *fname;
4ed5bcfb 774 const cpp_token *header;
7f2935c7 775
93c80368 776 /* Allow macro expansion. */
a8d0ddaf 777 header = get_token_no_padding (pfile);
a28fbdba 778 *location = header->src_loc;
2c6e3f55
JJ
779 if ((header->type == CPP_STRING && header->val.str.text[0] != 'R')
780 || header->type == CPP_HEADER_NAME)
7f2935c7 781 {
c3f829c1 782 fname = XNEWVEC (char, header->val.str.len - 1);
6338b358
NB
783 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
784 fname[header->val.str.len - 2] = '\0';
74eb4b3e 785 *pangle_brackets = header->type == CPP_HEADER_NAME;
7f2935c7 786 }
74eb4b3e 787 else if (header->type == CPP_LESS)
7f2935c7 788 {
74eb4b3e
NB
789 fname = glue_header_name (pfile);
790 *pangle_brackets = 1;
791 }
792 else
793 {
794 const unsigned char *dir;
795
796 if (pfile->directive == &dtable[T_PRAGMA])
b6baa67d 797 dir = UC"pragma dependency";
74eb4b3e
NB
798 else
799 dir = pfile->directive->name;
0527bc4e 800 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
74eb4b3e
NB
801 dir);
802
4ed5bcfb 803 return NULL;
7f2935c7 804 }
7f2935c7 805
cda5e672
TT
806 if (pfile->directive == &dtable[T_PRAGMA])
807 {
808 /* This pragma allows extra tokens after the file name. */
809 }
810 else if (buf == NULL || CPP_OPTION (pfile, discard_comments))
61cc8223 811 check_eol (pfile, true);
cbc43ae0
ILT
812 else
813 {
814 /* If we are not discarding comments, then gather them while
815 doing the eol check. */
816 *buf = check_eol_return_comments (pfile);
817 }
818
74eb4b3e 819 return fname;
168d3732 820}
3caee4a8 821
ba133c96 822/* Handle #include, #include_next and #import. */
711b8824 823static void
6cf87ca4 824do_include_common (cpp_reader *pfile, enum include_type type)
168d3732 825{
74eb4b3e
NB
826 const char *fname;
827 int angle_brackets;
cbc43ae0 828 const cpp_token **buf = NULL;
620e594b 829 location_t location;
cbc43ae0
ILT
830
831 /* Re-enable saving of comments if requested, so that the include
832 callback can dump comments which follow #include. */
833 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
74eb4b3e 834
056f95ec
NS
835 /* Tell the lexer this is an include directive -- we want it to
836 increment the line number even if this is the last line of a file. */
837 pfile->state.in_directive = 2;
838
a28fbdba 839 fname = parse_include (pfile, &angle_brackets, &buf, &location);
74eb4b3e 840 if (!fname)
705b0c05 841 goto done;
7f2935c7 842
28303828 843 if (!*fname)
705b0c05
NS
844 {
845 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
846 "empty filename in #%s",
847 pfile->directive->name);
848 goto done;
849 }
28303828 850
3963c2e0 851 /* Prevent #include recursion. */
1c6ffbab
QZ
852 if (pfile->line_table->depth >= CPP_OPTION (pfile, max_include_depth))
853 cpp_error (pfile,
854 CPP_DL_ERROR,
855 "#include nested depth %u exceeds maximum of %u"
856 " (use -fmax-include-depth=DEPTH to increase the maximum)",
857 pfile->line_table->depth,
858 CPP_OPTION (pfile, max_include_depth));
74eb4b3e 859 else
09b82253 860 {
74eb4b3e
NB
861 /* Get out of macro context, if we are. */
862 skip_rest_of_line (pfile);
09b82253 863
74eb4b3e 864 if (pfile->cb.include)
6cf87ca4 865 pfile->cb.include (pfile, pfile->directive_line,
cbc43ae0
ILT
866 pfile->directive->name, fname, angle_brackets,
867 buf);
3963c2e0 868
ac81cf0b 869 _cpp_stack_include (pfile, fname, angle_brackets, type, location);
74eb4b3e 870 }
3963c2e0 871
705b0c05 872 done:
cbc43ae0
ILT
873 XDELETEVEC (fname);
874 if (buf)
875 XDELETEVEC (buf);
168d3732 876}
e8037d57 877
711b8824 878static void
6cf87ca4 879do_include (cpp_reader *pfile)
168d3732 880{
ba133c96
NB
881 do_include_common (pfile, IT_INCLUDE);
882}
168d3732 883
ba133c96 884static void
6cf87ca4 885do_import (cpp_reader *pfile)
ba133c96 886{
ba133c96 887 do_include_common (pfile, IT_IMPORT);
168d3732 888}
7f2935c7 889
711b8824 890static void
6cf87ca4 891do_include_next (cpp_reader *pfile)
168d3732 892{
3963c2e0
ZW
893 enum include_type type = IT_INCLUDE_NEXT;
894
895 /* If this is the primary source file, warn and use the normal
896 search logic. */
bf425849 897 if (_cpp_in_main_source_file (pfile))
3963c2e0 898 {
0527bc4e 899 cpp_error (pfile, CPP_DL_WARNING,
3963c2e0
ZW
900 "#include_next in primary source file");
901 type = IT_INCLUDE;
902 }
903 do_include_common (pfile, type);
7f2935c7 904}
7f2935c7 905
dcc229e5
ZW
906/* Subroutine of do_linemarker. Read possible flags after file name.
907 LAST is the last flag seen; 0 if this is the first flag. Return the
908 flag if it is valid, 0 at the end of the directive. Otherwise
909 complain. */
642ce434 910static unsigned int
6cf87ca4 911read_flag (cpp_reader *pfile, unsigned int last)
d3a34a0a 912{
345894b4 913 const cpp_token *token = _cpp_lex_token (pfile);
d3a34a0a 914
345894b4 915 if (token->type == CPP_NUMBER && token->val.str.len == 1)
d3a34a0a 916 {
345894b4 917 unsigned int flag = token->val.str.text[0] - '0';
28e0f040
NB
918
919 if (flag > last && flag <= 4
920 && (flag != 4 || last == 3)
921 && (flag != 2 || last == 0))
922 return flag;
d3a34a0a 923 }
93c80368 924
345894b4 925 if (token->type != CPP_EOF)
0527bc4e 926 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
345894b4 927 cpp_token_as_text (pfile, token));
93c80368 928 return 0;
d3a34a0a
JM
929}
930
dcc229e5 931/* Subroutine of do_line and do_linemarker. Convert a number in STR,
3b8f20a1
MLI
932 of length LEN, to binary; store it in NUMP, and return false if the
933 number was well-formed, true if not. WRAPPED is set to true if the
9ccffd12 934 number did not fit into 'linenum_type'. */
3b8f20a1
MLI
935static bool
936strtolinenum (const uchar *str, size_t len, linenum_type *nump, bool *wrapped)
041c3194 937{
1bb64668 938 linenum_type reg = 0;
3b8f20a1 939
562a5c27 940 uchar c;
b24d8acb 941 bool seen_digit_sep = false;
3b8f20a1 942 *wrapped = false;
041c3194
ZW
943 while (len--)
944 {
945 c = *str++;
b24d8acb
JM
946 if (!seen_digit_sep && c == '\'' && len)
947 {
948 seen_digit_sep = true;
949 continue;
950 }
041c3194 951 if (!ISDIGIT (c))
3b8f20a1 952 return true;
b24d8acb 953 seen_digit_sep = false;
9ccffd12
JM
954 if (reg > ((linenum_type) -1) / 10)
955 *wrapped = true;
041c3194 956 reg *= 10;
9ccffd12 957 if (reg > ((linenum_type) -1) - (c - '0'))
3b8f20a1 958 *wrapped = true;
9ccffd12 959 reg += c - '0';
041c3194
ZW
960 }
961 *nump = reg;
3b8f20a1 962 return false;
041c3194
ZW
963}
964
6de1e2a9 965/* Interpret #line command.
dcc229e5 966 Note that the filename string (if any) is a true string constant
6bf2ff0d 967 (escapes are interpreted). */
711b8824 968static void
6cf87ca4 969do_line (cpp_reader *pfile)
7f2935c7 970{
99b1c316 971 class line_maps *line_table = pfile->line_table;
0e50b624 972 const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
2203a881
DP
973
974 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
975 sysp right now. */
976
46427374 977 unsigned char map_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map);
4ed5bcfb 978 const cpp_token *token;
46427374 979 const char *new_file = ORDINARY_MAP_FILE_NAME (map);
1bb64668 980 linenum_type new_lineno;
93c80368 981
27e2564a 982 /* C99 raised the minimum limit on #line numbers. */
1bb64668 983 linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
3b8f20a1 984 bool wrapped;
18a9d8ff 985
93c80368 986 /* #line commands expand macros. */
4ed5bcfb
NB
987 token = cpp_get_token (pfile);
988 if (token->type != CPP_NUMBER
1bb64668 989 || strtolinenum (token->val.str.text, token->val.str.len,
3b8f20a1 990 &new_lineno, &wrapped))
7f2935c7 991 {
33ae4837
TT
992 if (token->type == CPP_EOF)
993 cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line");
994 else
995 cpp_error (pfile, CPP_DL_ERROR,
996 "\"%s\" after #line is not a positive integer",
997 cpp_token_as_text (pfile, token));
9ec7291f 998 return;
df383483 999 }
7f2935c7 1000
3b8f20a1 1001 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap || wrapped))
0527bc4e 1002 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
3b8f20a1
MLI
1003 else if (wrapped)
1004 cpp_error (pfile, CPP_DL_WARNING, "line number out of range");
7f2935c7 1005
4ed5bcfb
NB
1006 token = cpp_get_token (pfile);
1007 if (token->type == CPP_STRING)
5538ada6 1008 {
e6cc3a24 1009 cpp_string s = { 0, 0 };
423e95e2 1010 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
f1bf410c 1011 &s, CPP_STRING))
e6cc3a24 1012 new_file = (const char *)s.text;
a5cb563b 1013 check_eol (pfile, true);
dcc229e5
ZW
1014 }
1015 else if (token->type != CPP_EOF)
1016 {
0527bc4e 1017 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
dcc229e5
ZW
1018 cpp_token_as_text (pfile, token));
1019 return;
1020 }
1021
1022 skip_rest_of_line (pfile);
c7f9c0b9 1023 _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, new_file, new_lineno,
2203a881 1024 map_sysp);
c3388e62 1025 line_table->seen_line_directive = true;
dcc229e5
ZW
1026}
1027
1028/* Interpret the # 44 "file" [flags] notation, which has slightly
1029 different syntax and semantics from #line: Flags are allowed,
1030 and we never complain about the line number being too big. */
1031static void
6cf87ca4 1032do_linemarker (cpp_reader *pfile)
dcc229e5 1033{
99b1c316 1034 class line_maps *line_table = pfile->line_table;
0e50b624 1035 const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
dcc229e5 1036 const cpp_token *token;
46427374 1037 const char *new_file = ORDINARY_MAP_FILE_NAME (map);
1bb64668 1038 linenum_type new_lineno;
46427374 1039 unsigned int new_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map);
c7f9c0b9 1040 enum lc_reason reason = LC_RENAME_VERBATIM;
dcc229e5 1041 int flag;
3b8f20a1 1042 bool wrapped;
dcc229e5
ZW
1043
1044 /* Back up so we can get the number again. Putting this in
1045 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
1046 some circumstances, which can segfault. */
1047 _cpp_backup_tokens (pfile, 1);
1048
1049 /* #line commands expand macros. */
1050 token = cpp_get_token (pfile);
1051 if (token->type != CPP_NUMBER
1bb64668 1052 || strtolinenum (token->val.str.text, token->val.str.len,
3b8f20a1 1053 &new_lineno, &wrapped))
dcc229e5 1054 {
33ae4837
TT
1055 /* Unlike #line, there does not seem to be a way to get an EOF
1056 here. So, it should be safe to always spell the token. */
0527bc4e
JDA
1057 cpp_error (pfile, CPP_DL_ERROR,
1058 "\"%s\" after # is not a positive integer",
dcc229e5
ZW
1059 cpp_token_as_text (pfile, token));
1060 return;
df383483 1061 }
941e09b6 1062
dcc229e5
ZW
1063 token = cpp_get_token (pfile);
1064 if (token->type == CPP_STRING)
1065 {
e6cc3a24 1066 cpp_string s = { 0, 0 };
423e95e2 1067 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
f1bf410c 1068 1, &s, CPP_STRING))
e6cc3a24 1069 new_file = (const char *)s.text;
cf551fba 1070
dcc229e5
ZW
1071 new_sysp = 0;
1072 flag = read_flag (pfile, 0);
1073 if (flag == 1)
1074 {
1075 reason = LC_ENTER;
1076 /* Fake an include for cpp_included (). */
1077 _cpp_fake_include (pfile, new_file);
1078 flag = read_flag (pfile, flag);
1079 }
1080 else if (flag == 2)
1081 {
1082 reason = LC_LEAVE;
1083 flag = read_flag (pfile, flag);
1084 }
1085 if (flag == 3)
93c80368 1086 {
dcc229e5
ZW
1087 new_sysp = 1;
1088 flag = read_flag (pfile, flag);
1089 if (flag == 4)
1090 new_sysp = 2;
93c80368 1091 }
9d30f270 1092 pfile->buffer->sysp = new_sysp;
dcc229e5 1093
a5cb563b 1094 check_eol (pfile, false);
041c3194 1095 }
4ed5bcfb 1096 else if (token->type != CPP_EOF)
27e2564a 1097 {
0527bc4e 1098 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
4ed5bcfb 1099 cpp_token_as_text (pfile, token));
27e2564a
NB
1100 return;
1101 }
7f2935c7 1102
bdcbe496 1103 skip_rest_of_line (pfile);
00b0c19b 1104
3caf0ca1
BS
1105 if (reason == LC_LEAVE)
1106 {
12de2245
BS
1107 /* Reread map since cpp_get_token can invalidate it with a
1108 reallocation. */
1109 map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
f10a9135
NS
1110 const line_map_ordinary *from
1111 = linemap_included_from_linemap (line_table, map);
400b8274
NS
1112
1113 if (!from)
1114 /* Not nested. */;
1115 else if (!new_file[0])
1116 /* Leaving to "" means fill in the popped-to name. */
1117 new_file = ORDINARY_MAP_FILE_NAME (from);
1118 else if (filename_cmp (ORDINARY_MAP_FILE_NAME (from), new_file) != 0)
1119 /* It's the wrong name, Grommit! */
1120 from = NULL;
1121
1122 if (!from)
3caf0ca1
BS
1123 {
1124 cpp_warning (pfile, CPP_W_NONE,
12de2245
BS
1125 "file \"%s\" linemarker ignored due to "
1126 "incorrect nesting", new_file);
3caf0ca1
BS
1127 return;
1128 }
1129 }
400b8274 1130
00b0c19b
MLI
1131 /* Compensate for the increment in linemap_add that occurs in
1132 _cpp_do_file_change. We're currently at the start of the line
620e594b 1133 *following* the #line directive. A separate location_t for this
00b0c19b
MLI
1134 location makes no sense (until we do the LC_LEAVE), and
1135 complicates LAST_SOURCE_LINE_LOCATION. */
1136 pfile->line_table->highest_location--;
1137
bb74c963 1138 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
c3388e62 1139 line_table->seen_line_directive = true;
27e2564a
NB
1140}
1141
6bf2ff0d
NS
1142/* Arrange the file_change callback. Changing to TO_FILE:TO_LINE for
1143 REASON. SYSP is 1 for a system header, 2 for a system header that
1144 needs to be extern "C" protected, and zero otherwise. */
eb1f4d9d 1145void
6cf87ca4 1146_cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
6bf2ff0d 1147 const char *to_file, linenum_type to_line,
6cf87ca4 1148 unsigned int sysp)
27e2564a 1149{
0e50b624 1150 linemap_assert (reason != LC_ENTER_MACRO);
6bf2ff0d 1151
0e50b624 1152 const line_map_ordinary *ord_map = NULL;
6bf2ff0d 1153 if (!to_line && reason == LC_RENAME_VERBATIM)
0e50b624 1154 {
6bf2ff0d
NS
1155 /* A linemarker moving to line zero. If we're on the second
1156 line of the current map, and it also starts at zero, just
1157 rewind -- we're probably reading the builtins of a
1158 preprocessed source. */
1159 line_map_ordinary *last = LINEMAPS_LAST_ORDINARY_MAP (pfile->line_table);
1160 if (!ORDINARY_MAP_STARTING_LINE_NUMBER (last)
e9a2e208 1161 && 0 == filename_cmp (to_file, ORDINARY_MAP_FILE_NAME (last))
6bf2ff0d
NS
1162 && SOURCE_LINE (last, pfile->line_table->highest_line) == 2)
1163 {
1164 ord_map = last;
1165 pfile->line_table->highest_location
1166 = pfile->line_table->highest_line = MAP_START_LOCATION (last);
1167 }
0e50b624 1168 }
d82fc108 1169
6bf2ff0d
NS
1170 if (!ord_map)
1171 if (const line_map *map = linemap_add (pfile->line_table, reason, sysp,
1172 to_file, to_line))
1173 {
1174 ord_map = linemap_check_ordinary (map);
1175 linemap_line_start (pfile->line_table,
1176 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map),
1177 127);
1178 }
1179
eb1f4d9d 1180 if (pfile->cb.file_change)
0e50b624 1181 pfile->cb.file_change (pfile, ord_map);
7f2935c7 1182}
941e09b6 1183
5d8ebbd8
NB
1184/* Report a warning or error detected by the program we are
1185 processing. Use the directive's tokens in the error message. */
711b8824 1186static void
c24300ba
DM
1187do_diagnostic (cpp_reader *pfile, enum cpp_diagnostic_level code,
1188 enum cpp_warning_reason reason, int print_dir)
7f2935c7 1189{
5d6342eb
TT
1190 const unsigned char *dir_name;
1191 unsigned char *line;
620e594b 1192 location_t src_loc = pfile->cur_token[-1].src_loc;
5d6342eb
TT
1193
1194 if (print_dir)
1195 dir_name = pfile->directive->name;
1196 else
1197 dir_name = NULL;
1198 pfile->state.prevent_expansion++;
1199 line = cpp_output_line_to_string (pfile, dir_name);
1200 pfile->state.prevent_expansion--;
1201
87cf0651
SB
1202 if (code == CPP_DL_WARNING_SYSHDR && reason)
1203 cpp_warning_with_line_syshdr (pfile, reason, src_loc, 0, "%s", line);
1204 else if (code == CPP_DL_WARNING && reason)
1205 cpp_warning_with_line (pfile, reason, src_loc, 0, "%s", line);
1206 else
1207 cpp_error_with_line (pfile, code, src_loc, 0, "%s", line);
5d6342eb 1208 free (line);
7f2935c7
PB
1209}
1210
838f313b 1211static void
6cf87ca4 1212do_error (cpp_reader *pfile)
838f313b 1213{
c24300ba 1214 do_diagnostic (pfile, CPP_DL_ERROR, CPP_W_NONE, 1);
838f313b 1215}
7f2935c7 1216
711b8824 1217static void
6cf87ca4 1218do_warning (cpp_reader *pfile)
7f2935c7 1219{
2f878973 1220 /* We want #warning diagnostics to be emitted in system headers too. */
87cf0651 1221 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, CPP_W_WARNING_DIRECTIVE, 1);
7f2935c7
PB
1222}
1223
a2a76ce7 1224/* Report program identification. */
711b8824 1225static void
6cf87ca4 1226do_ident (cpp_reader *pfile)
7f2935c7 1227{
4ed5bcfb 1228 const cpp_token *str = cpp_get_token (pfile);
58fea6af 1229
4ed5bcfb 1230 if (str->type != CPP_STRING)
1ed17cd5
ZW
1231 cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
1232 pfile->directive->name);
93c80368 1233 else if (pfile->cb.ident)
6cf87ca4 1234 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
a2a76ce7 1235
a5cb563b 1236 check_eol (pfile, false);
7f2935c7
PB
1237}
1238
a5da89c6
NB
1239/* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1240 matching entry, or NULL if none is found. The returned entry could
1241 be the start of a namespace chain, or a pragma. */
1242static struct pragma_entry *
6cf87ca4 1243lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
82443371 1244{
4b115ff0
NB
1245 while (chain && chain->pragma != pragma)
1246 chain = chain->next;
a5da89c6
NB
1247
1248 return chain;
1249}
1250
bc4071dd
RH
1251/* Create and insert a blank pragma entry at the beginning of a
1252 singly-linked CHAIN. */
a5da89c6 1253static struct pragma_entry *
bc4071dd 1254new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain)
82443371 1255{
c3f829c1 1256 struct pragma_entry *new_entry;
58fea6af 1257
c3f829c1 1258 new_entry = (struct pragma_entry *)
8c3b2693 1259 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
58fea6af 1260
bc4071dd 1261 memset (new_entry, 0, sizeof (struct pragma_entry));
c3f829c1 1262 new_entry->next = *chain;
bc4071dd 1263
c3f829c1
GDR
1264 *chain = new_entry;
1265 return new_entry;
58fea6af 1266}
82443371 1267
a5da89c6 1268/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
bc4071dd
RH
1269 goes in the global namespace. */
1270static struct pragma_entry *
1271register_pragma_1 (cpp_reader *pfile, const char *space, const char *name,
1272 bool allow_name_expansion)
82443371 1273{
a5da89c6
NB
1274 struct pragma_entry **chain = &pfile->pragmas;
1275 struct pragma_entry *entry;
4b115ff0 1276 const cpp_hashnode *node;
a5da89c6 1277
a5da89c6 1278 if (space)
58fea6af 1279 {
b6baa67d 1280 node = cpp_lookup (pfile, UC space, strlen (space));
4b115ff0 1281 entry = lookup_pragma_entry (*chain, node);
a5da89c6 1282 if (!entry)
bc4071dd
RH
1283 {
1284 entry = new_pragma_entry (pfile, chain);
1285 entry->pragma = node;
1286 entry->is_nspace = true;
1287 entry->allow_expansion = allow_name_expansion;
1288 }
a5da89c6
NB
1289 else if (!entry->is_nspace)
1290 goto clash;
bc4071dd
RH
1291 else if (entry->allow_expansion != allow_name_expansion)
1292 {
1293 cpp_error (pfile, CPP_DL_ICE,
1294 "registering pragmas in namespace \"%s\" with mismatched "
1295 "name expansion", space);
1296 return NULL;
1297 }
a5da89c6 1298 chain = &entry->u.space;
58fea6af 1299 }
bc4071dd
RH
1300 else if (allow_name_expansion)
1301 {
1302 cpp_error (pfile, CPP_DL_ICE,
1303 "registering pragma \"%s\" with name expansion "
1304 "and no namespace", name);
1305 return NULL;
1306 }
58fea6af 1307
a5da89c6 1308 /* Check for duplicates. */
b6baa67d 1309 node = cpp_lookup (pfile, UC name, strlen (name));
4b115ff0 1310 entry = lookup_pragma_entry (*chain, node);
bc4071dd 1311 if (entry == NULL)
a5da89c6 1312 {
bc4071dd
RH
1313 entry = new_pragma_entry (pfile, chain);
1314 entry->pragma = node;
1315 return entry;
a5da89c6 1316 }
bc4071dd
RH
1317
1318 if (entry->is_nspace)
1319 clash:
1320 cpp_error (pfile, CPP_DL_ICE,
1321 "registering \"%s\" as both a pragma and a pragma namespace",
1322 NODE_NAME (node));
1323 else if (space)
1324 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1325 space, name);
a5da89c6 1326 else
bc4071dd
RH
1327 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1328
1329 return NULL;
1330}
1331
1332/* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1333static void
1334register_pragma_internal (cpp_reader *pfile, const char *space,
1335 const char *name, pragma_cb handler)
1336{
1337 struct pragma_entry *entry;
1338
1339 entry = register_pragma_1 (pfile, space, name, false);
1340 entry->is_internal = true;
1341 entry->u.handler = handler;
21b11495
ZW
1342}
1343
1344/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1345 goes in the global namespace. HANDLER is the handler it will call,
b5b3e36a
DJ
1346 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1347 expansion while parsing pragma NAME. This function is exported
1348 from libcpp. */
21b11495
ZW
1349void
1350cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
b5b3e36a 1351 pragma_cb handler, bool allow_expansion)
21b11495 1352{
bc4071dd
RH
1353 struct pragma_entry *entry;
1354
1355 if (!handler)
1356 {
1357 cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler");
1358 return;
1359 }
1360
1361 entry = register_pragma_1 (pfile, space, name, false);
1362 if (entry)
1363 {
1364 entry->allow_expansion = allow_expansion;
1365 entry->u.handler = handler;
1366 }
58fea6af 1367}
a5da89c6 1368
bc4071dd
RH
1369/* Similarly, but create mark the pragma for deferred processing.
1370 When found, a CPP_PRAGMA token will be insertted into the stream
1371 with IDENT in the token->u.pragma slot. */
1372void
1373cpp_register_deferred_pragma (cpp_reader *pfile, const char *space,
1374 const char *name, unsigned int ident,
1375 bool allow_expansion, bool allow_name_expansion)
1376{
1377 struct pragma_entry *entry;
1378
1379 entry = register_pragma_1 (pfile, space, name, allow_name_expansion);
1380 if (entry)
1381 {
1382 entry->is_deferred = true;
1383 entry->allow_expansion = allow_expansion;
1384 entry->u.ident = ident;
1385 }
1386}
1387
a5da89c6 1388/* Register the pragmas the preprocessor itself handles. */
58fea6af 1389void
6cf87ca4 1390_cpp_init_internal_pragmas (cpp_reader *pfile)
58fea6af 1391{
a5da89c6 1392 /* Pragmas in the global namespace. */
bc4071dd 1393 register_pragma_internal (pfile, 0, "once", do_pragma_once);
17e7cb85
KT
1394 register_pragma_internal (pfile, 0, "push_macro", do_pragma_push_macro);
1395 register_pragma_internal (pfile, 0, "pop_macro", do_pragma_pop_macro);
58fea6af 1396
a5da89c6 1397 /* New GCC-specific pragmas should be put in the GCC namespace. */
bc4071dd
RH
1398 register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison);
1399 register_pragma_internal (pfile, "GCC", "system_header",
1400 do_pragma_system_header);
1401 register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
f591bd8f
FW
1402 register_pragma_internal (pfile, "GCC", "warning", do_pragma_warning);
1403 register_pragma_internal (pfile, "GCC", "error", do_pragma_error);
82443371 1404}
7f2935c7 1405
17211ab5
GK
1406/* Return the number of registered pragmas in PE. */
1407
1408static int
6cf87ca4 1409count_registered_pragmas (struct pragma_entry *pe)
17211ab5
GK
1410{
1411 int ct = 0;
1412 for (; pe != NULL; pe = pe->next)
1413 {
1414 if (pe->is_nspace)
1415 ct += count_registered_pragmas (pe->u.space);
1416 ct++;
1417 }
1418 return ct;
1419}
1420
1421/* Save into SD the names of the registered pragmas referenced by PE,
1422 and return a pointer to the next free space in SD. */
1423
1424static char **
6cf87ca4 1425save_registered_pragmas (struct pragma_entry *pe, char **sd)
17211ab5
GK
1426{
1427 for (; pe != NULL; pe = pe->next)
1428 {
1429 if (pe->is_nspace)
1430 sd = save_registered_pragmas (pe->u.space, sd);
c3f829c1
GDR
1431 *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
1432 HT_LEN (&pe->pragma->ident),
1433 HT_LEN (&pe->pragma->ident) + 1);
17211ab5
GK
1434 }
1435 return sd;
1436}
1437
1438/* Return a newly-allocated array which saves the names of the
1439 registered pragmas. */
1440
1441char **
6cf87ca4 1442_cpp_save_pragma_names (cpp_reader *pfile)
17211ab5
GK
1443{
1444 int ct = count_registered_pragmas (pfile->pragmas);
72bb2c39 1445 char **result = XNEWVEC (char *, ct);
17211ab5
GK
1446 (void) save_registered_pragmas (pfile->pragmas, result);
1447 return result;
1448}
1449
1450/* Restore from SD the names of the registered pragmas referenced by PE,
1451 and return a pointer to the next unused name in SD. */
1452
1453static char **
6cf87ca4
ZW
1454restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1455 char **sd)
17211ab5
GK
1456{
1457 for (; pe != NULL; pe = pe->next)
1458 {
1459 if (pe->is_nspace)
1460 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
b6baa67d 1461 pe->pragma = cpp_lookup (pfile, UC *sd, strlen (*sd));
17211ab5
GK
1462 free (*sd);
1463 sd++;
1464 }
1465 return sd;
1466}
1467
1468/* Restore the names of the registered pragmas from SAVED. */
1469
1470void
6cf87ca4 1471_cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
17211ab5
GK
1472{
1473 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1474 free (saved);
1475}
1476
a5da89c6
NB
1477/* Pragmata handling. We handle some, and pass the rest on to the
1478 front end. C99 defines three pragmas and says that no macro
1479 expansion is to be performed on them; whether or not macro
1480 expansion happens for other pragmas is implementation defined.
bc4071dd
RH
1481 This implementation allows for a mix of both, since GCC did not
1482 traditionally macro expand its (few) pragmas, whereas OpenMP
1483 specifies that macro expansion should happen. */
711b8824 1484static void
6cf87ca4 1485do_pragma (cpp_reader *pfile)
7f2935c7 1486{
a5da89c6 1487 const struct pragma_entry *p = NULL;
f3d25c65 1488 const cpp_token *token, *pragma_token;
620e594b 1489 location_t pragma_token_virt_loc = 0;
1c90c6f9 1490 cpp_token ns_token;
a5da89c6 1491 unsigned int count = 1;
add7091b 1492
93c80368 1493 pfile->state.prevent_expansion++;
58fea6af 1494
f3d25c65
DS
1495 pragma_token = token = cpp_get_token_with_location (pfile,
1496 &pragma_token_virt_loc);
1c90c6f9 1497 ns_token = *token;
4ed5bcfb 1498 if (token->type == CPP_NAME)
0172e2bc 1499 {
9a0c6187 1500 p = lookup_pragma_entry (pfile->pragmas, token->val.node.node);
a5da89c6 1501 if (p && p->is_nspace)
58fea6af 1502 {
bc4071dd
RH
1503 bool allow_name_expansion = p->allow_expansion;
1504 if (allow_name_expansion)
828a7f76 1505 pfile->state.prevent_expansion--;
38fbfaf6 1506
a5da89c6
NB
1507 token = cpp_get_token (pfile);
1508 if (token->type == CPP_NAME)
9a0c6187 1509 p = lookup_pragma_entry (p->u.space, token->val.node.node);
a5da89c6
NB
1510 else
1511 p = NULL;
bc4071dd
RH
1512 if (allow_name_expansion)
1513 pfile->state.prevent_expansion++;
1514 count = 2;
58fea6af 1515 }
58fea6af 1516 }
041c3194 1517
3da3d587 1518 if (p)
e2e1fa50 1519 {
bc4071dd
RH
1520 if (p->is_deferred)
1521 {
f3d25c65 1522 pfile->directive_result.src_loc = pragma_token_virt_loc;
bc4071dd
RH
1523 pfile->directive_result.type = CPP_PRAGMA;
1524 pfile->directive_result.flags = pragma_token->flags;
1525 pfile->directive_result.val.pragma = p->u.ident;
1526 pfile->state.in_deferred_pragma = true;
1527 pfile->state.pragma_allow_expansion = p->allow_expansion;
1528 if (!p->allow_expansion)
1529 pfile->state.prevent_expansion++;
1530 }
1531 else
3da3d587 1532 {
bc4071dd
RH
1533 /* Since the handler below doesn't get the line number, that
1534 it might need for diagnostics, make sure it has the right
3da3d587
ZW
1535 numbers in place. */
1536 if (pfile->cb.line_change)
1537 (*pfile->cb.line_change) (pfile, pragma_token, false);
bc4071dd 1538 if (p->allow_expansion)
b5b3e36a 1539 pfile->state.prevent_expansion--;
3da3d587 1540 (*p->u.handler) (pfile);
bc4071dd 1541 if (p->allow_expansion)
b5b3e36a 1542 pfile->state.prevent_expansion++;
3da3d587 1543 }
21b11495 1544 }
d82fc108 1545 else if (pfile->cb.def_pragma)
bdcbe496 1546 {
1c90c6f9
JJ
1547 if (count == 1 || pfile->context->prev == NULL)
1548 _cpp_backup_tokens (pfile, count);
1549 else
1550 {
1551 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1552 won't allow backing 2 tokens. */
1553 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1554 reads both tokens, we could perhaps free it, but if it doesn't,
1555 we don't know the exact lifespan. */
1556 cpp_token *toks = XNEWVEC (cpp_token, 2);
1557 toks[0] = ns_token;
1558 toks[0].flags |= NO_EXPAND;
1559 toks[1] = *token;
1560 toks[1].flags |= NO_EXPAND;
1561 _cpp_push_token_context (pfile, NULL, toks, 2);
1562 }
6cf87ca4 1563 pfile->cb.def_pragma (pfile, pfile->directive_line);
bdcbe496 1564 }
a5da89c6 1565
97293897 1566 pfile->state.prevent_expansion--;
82443371
NS
1567}
1568
5d8ebbd8 1569/* Handle #pragma once. */
58fea6af 1570static void
6cf87ca4 1571do_pragma_once (cpp_reader *pfile)
a2a76ce7 1572{
bf425849 1573 if (_cpp_in_main_source_file (pfile))
0527bc4e 1574 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
93c80368 1575
a5cb563b 1576 check_eol (pfile, false);
49634b3a 1577 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
a2a76ce7 1578}
fc009f96 1579
17e7cb85
KT
1580/* Handle #pragma push_macro(STRING). */
1581static void
1582do_pragma_push_macro (cpp_reader *pfile)
1583{
d6874138
KT
1584 cpp_hashnode *node;
1585 size_t defnlen;
1586 const uchar *defn = NULL;
17e7cb85
KT
1587 char *macroname, *dest;
1588 const char *limit, *src;
1589 const cpp_token *txt;
1590 struct def_pragma_macro *c;
1591
1592 txt = get__Pragma_string (pfile);
1593 if (!txt)
1594 {
620e594b 1595 location_t src_loc = pfile->cur_token[-1].src_loc;
17e7cb85
KT
1596 cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
1597 "invalid #pragma push_macro directive");
1598 check_eol (pfile, false);
1599 skip_rest_of_line (pfile);
1600 return;
1601 }
1602 dest = macroname = (char *) alloca (txt->val.str.len + 2);
1603 src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L'));
1604 limit = (const char *) (txt->val.str.text + txt->val.str.len - 1);
1605 while (src < limit)
1606 {
1607 /* We know there is a character following the backslash. */
1608 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1609 src++;
1610 *dest++ = *src++;
1611 }
1612 *dest = 0;
1613 check_eol (pfile, false);
1614 skip_rest_of_line (pfile);
1615 c = XNEW (struct def_pragma_macro);
d6874138 1616 memset (c, 0, sizeof (struct def_pragma_macro));
17e7cb85
KT
1617 c->name = XNEWVAR (char, strlen (macroname) + 1);
1618 strcpy (c->name, macroname);
1619 c->next = pfile->pushed_macros;
d6874138
KT
1620 node = _cpp_lex_identifier (pfile, c->name);
1621 if (node->type == NT_VOID)
1622 c->is_undef = 1;
aa23e73b
JJ
1623 else if (node->type == NT_BUILTIN_MACRO)
1624 c->is_builtin = 1;
d6874138
KT
1625 else
1626 {
1627 defn = cpp_macro_definition (pfile, node);
1628 defnlen = ustrlen (defn);
1629 c->definition = XNEWVEC (uchar, defnlen + 2);
1630 c->definition[defnlen] = '\n';
1631 c->definition[defnlen + 1] = 0;
1632 c->line = node->value.macro->line;
1633 c->syshdr = node->value.macro->syshdr;
1634 c->used = node->value.macro->used;
1635 memcpy (c->definition, defn, defnlen);
1636 }
1637
17e7cb85
KT
1638 pfile->pushed_macros = c;
1639}
1640
1641/* Handle #pragma pop_macro(STRING). */
1642static void
1643do_pragma_pop_macro (cpp_reader *pfile)
1644{
1645 char *macroname, *dest;
1646 const char *limit, *src;
1647 const cpp_token *txt;
1648 struct def_pragma_macro *l = NULL, *c = pfile->pushed_macros;
1649 txt = get__Pragma_string (pfile);
1650 if (!txt)
1651 {
620e594b 1652 location_t src_loc = pfile->cur_token[-1].src_loc;
17e7cb85
KT
1653 cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
1654 "invalid #pragma pop_macro directive");
1655 check_eol (pfile, false);
1656 skip_rest_of_line (pfile);
1657 return;
1658 }
1659 dest = macroname = (char *) alloca (txt->val.str.len + 2);
1660 src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L'));
1661 limit = (const char *) (txt->val.str.text + txt->val.str.len - 1);
1662 while (src < limit)
1663 {
1664 /* We know there is a character following the backslash. */
1665 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1666 src++;
1667 *dest++ = *src++;
1668 }
1669 *dest = 0;
1670 check_eol (pfile, false);
1671 skip_rest_of_line (pfile);
1672
1673 while (c != NULL)
1674 {
1675 if (!strcmp (c->name, macroname))
1676 {
1677 if (!l)
1678 pfile->pushed_macros = c->next;
1679 else
1680 l->next = c->next;
d6874138
KT
1681 cpp_pop_definition (pfile, c);
1682 free (c->definition);
17e7cb85
KT
1683 free (c->name);
1684 free (c);
1685 break;
1686 }
1687 l = c;
1688 c = c->next;
1689 }
1690}
1691
c3bf3e6e
NB
1692/* Handle #pragma GCC poison, to poison one or more identifiers so
1693 that the lexer produces a hard error for each subsequent usage. */
58fea6af 1694static void
6cf87ca4 1695do_pragma_poison (cpp_reader *pfile)
a2a76ce7 1696{
345894b4 1697 const cpp_token *tok;
f8f769ea 1698 cpp_hashnode *hp;
a2a76ce7 1699
93c80368 1700 pfile->state.poisoned_ok = 1;
a2a76ce7
ZW
1701 for (;;)
1702 {
345894b4
NB
1703 tok = _cpp_lex_token (pfile);
1704 if (tok->type == CPP_EOF)
a2a76ce7 1705 break;
345894b4 1706 if (tok->type != CPP_NAME)
fc009f96 1707 {
0527bc4e
JDA
1708 cpp_error (pfile, CPP_DL_ERROR,
1709 "invalid #pragma GCC poison directive");
93c80368 1710 break;
fc009f96
GK
1711 }
1712
9a0c6187 1713 hp = tok->val.node.node;
93c80368
NB
1714 if (hp->flags & NODE_POISONED)
1715 continue;
1716
3f6677f4 1717 if (cpp_macro_p (hp))
0527bc4e 1718 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
ebef4e8c 1719 NODE_NAME (hp));
93c80368
NB
1720 _cpp_free_definition (hp);
1721 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
fc009f96 1722 }
93c80368 1723 pfile->state.poisoned_ok = 0;
7f2935c7 1724}
2c0b35cb
ZW
1725
1726/* Mark the current header as a system header. This will suppress
1727 some categories of warnings (notably those from -pedantic). It is
1728 intended for use in system libraries that cannot be implemented in
1729 conforming C, but cannot be certain that their headers appear in a
1730 system include directory. To prevent abuse, it is rejected in the
1731 primary source file. */
58fea6af 1732static void
6cf87ca4 1733do_pragma_system_header (cpp_reader *pfile)
2c0b35cb 1734{
bf425849 1735 if (_cpp_in_main_source_file (pfile))
0527bc4e 1736 cpp_error (pfile, CPP_DL_WARNING,
ebef4e8c 1737 "#pragma system_header ignored outside include file");
2c0b35cb 1738 else
d82fc108 1739 {
a5cb563b 1740 check_eol (pfile, false);
bdcbe496 1741 skip_rest_of_line (pfile);
d82fc108
NB
1742 cpp_make_system_header (pfile, 1, 0);
1743 }
2c0b35cb 1744}
f3f751ad
NS
1745
1746/* Check the modified date of the current include file against a specified
1747 file. Issue a diagnostic, if the specified file is newer. We use this to
1748 determine if a fixed header should be refixed. */
58fea6af 1749static void
6cf87ca4 1750do_pragma_dependency (cpp_reader *pfile)
f3f751ad 1751{
74eb4b3e
NB
1752 const char *fname;
1753 int angle_brackets, ordering;
620e594b 1754 location_t location;
df383483 1755
a28fbdba 1756 fname = parse_include (pfile, &angle_brackets, NULL, &location);
74eb4b3e 1757 if (!fname)
58fea6af 1758 return;
041c3194 1759
74eb4b3e 1760 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
f3f751ad 1761 if (ordering < 0)
0527bc4e 1762 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
f3f751ad
NS
1763 else if (ordering > 0)
1764 {
0527bc4e
JDA
1765 cpp_error (pfile, CPP_DL_WARNING,
1766 "current file is older than %s", fname);
4ed5bcfb 1767 if (cpp_get_token (pfile)->type != CPP_EOF)
bdcbe496
NB
1768 {
1769 _cpp_backup_tokens (pfile, 1);
c24300ba 1770 do_diagnostic (pfile, CPP_DL_WARNING, CPP_W_NONE, 0);
bdcbe496 1771 }
f3f751ad 1772 }
74eb4b3e 1773
fad205ff 1774 free ((void *) fname);
f3f751ad
NS
1775}
1776
f591bd8f
FW
1777/* Issue a diagnostic with the message taken from the pragma. If
1778 ERROR is true, the diagnostic is a warning, otherwise, it is an
1779 error. */
1780static void
1781do_pragma_warning_or_error (cpp_reader *pfile, bool error)
1782{
1783 const cpp_token *tok = _cpp_lex_token (pfile);
1784 cpp_string str;
1785 if (tok->type != CPP_STRING
1786 || !cpp_interpret_string_notranslate (pfile, &tok->val.str, 1, &str,
1787 CPP_STRING)
1788 || str.len == 0)
1789 {
1790 cpp_error (pfile, CPP_DL_ERROR, "invalid \"#pragma GCC %s\" directive",
1791 error ? "error" : "warning");
1792 return;
1793 }
1794 cpp_error (pfile, error ? CPP_DL_ERROR : CPP_DL_WARNING,
1795 "%s", str.text);
1796 free ((void *)str.text);
1797}
1798
1799/* Issue a warning diagnostic. */
1800static void
1801do_pragma_warning (cpp_reader *pfile)
1802{
1803 do_pragma_warning_or_error (pfile, false);
1804}
1805
1806/* Issue an error diagnostic. */
1807static void
1808do_pragma_error (cpp_reader *pfile)
1809{
1810 do_pragma_warning_or_error (pfile, true);
1811}
1812
4ed5bcfb
NB
1813/* Get a token but skip padding. */
1814static const cpp_token *
6cf87ca4 1815get_token_no_padding (cpp_reader *pfile)
a5c3cccd 1816{
4ed5bcfb
NB
1817 for (;;)
1818 {
1819 const cpp_token *result = cpp_get_token (pfile);
1820 if (result->type != CPP_PADDING)
1821 return result;
1822 }
1823}
a5c3cccd 1824
4ed5bcfb
NB
1825/* Check syntax is "(string-literal)". Returns the string on success,
1826 or NULL on failure. */
1827static const cpp_token *
6cf87ca4 1828get__Pragma_string (cpp_reader *pfile)
4ed5bcfb
NB
1829{
1830 const cpp_token *string;
5b9a40df 1831 const cpp_token *paren;
a5c3cccd 1832
5b9a40df
TT
1833 paren = get_token_no_padding (pfile);
1834 if (paren->type == CPP_EOF)
1835 _cpp_backup_tokens (pfile, 1);
1836 if (paren->type != CPP_OPEN_PAREN)
4ed5bcfb
NB
1837 return NULL;
1838
1839 string = get_token_no_padding (pfile);
5b9a40df
TT
1840 if (string->type == CPP_EOF)
1841 _cpp_backup_tokens (pfile, 1);
b6baa67d 1842 if (string->type != CPP_STRING && string->type != CPP_WSTRING
2c6e3f55
JJ
1843 && string->type != CPP_STRING32 && string->type != CPP_STRING16
1844 && string->type != CPP_UTF8STRING)
4ed5bcfb
NB
1845 return NULL;
1846
5b9a40df
TT
1847 paren = get_token_no_padding (pfile);
1848 if (paren->type == CPP_EOF)
1849 _cpp_backup_tokens (pfile, 1);
1850 if (paren->type != CPP_CLOSE_PAREN)
4ed5bcfb 1851 return NULL;
a5c3cccd 1852
4ed5bcfb 1853 return string;
a5c3cccd
NB
1854}
1855
87062813
NB
1856/* Destringize IN into a temporary buffer, by removing the first \ of
1857 \" and \\ sequences, and process the result as a #pragma directive. */
1858static void
0afff540 1859destringize_and_run (cpp_reader *pfile, const cpp_string *in,
620e594b 1860 location_t expansion_loc)
a5c3cccd
NB
1861{
1862 const unsigned char *src, *limit;
87062813 1863 char *dest, *result;
bc4071dd
RH
1864 cpp_context *saved_context;
1865 cpp_token *saved_cur_token;
1866 tokenrun *saved_cur_run;
1867 cpp_token *toks;
1868 int count;
14ccf800 1869 const struct directive *save_directive;
a5c3cccd 1870
c3f829c1 1871 dest = result = (char *) alloca (in->len - 1);
6338b358
NB
1872 src = in->text + 1 + (in->text[0] == 'L');
1873 limit = in->text + in->len - 1;
1874 while (src < limit)
a5c3cccd
NB
1875 {
1876 /* We know there is a character following the backslash. */
1877 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1878 src++;
1879 *dest++ = *src++;
1880 }
26aea073 1881 *dest = '\n';
a5c3cccd 1882
8128cccf
NB
1883 /* Ugh; an awful kludge. We are really not set up to be lexing
1884 tokens when in the middle of a macro expansion. Use a new
1885 context to force cpp_get_token to lex, and so skip_rest_of_line
1886 doesn't go beyond the end of the text. Also, remember the
1887 current lexing position so we can return to it later.
1888
1889 Something like line-at-a-time lexing should remove the need for
1890 this. */
bc4071dd
RH
1891 saved_context = pfile->context;
1892 saved_cur_token = pfile->cur_token;
1893 saved_cur_run = pfile->cur_run;
79ba5e3b 1894
3ad64f53 1895 pfile->context = XCNEW (cpp_context);
79ba5e3b 1896
bc4071dd
RH
1897 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1898 until we've read all of the tokens that we want. */
1899 cpp_push_buffer (pfile, (const uchar *) result, dest - result,
1900 /* from_stage3 */ true);
1901 /* ??? Antique Disgusting Hack. What does this do? */
1902 if (pfile->buffer->prev)
1903 pfile->buffer->file = pfile->buffer->prev->file;
79ba5e3b 1904
bc4071dd
RH
1905 start_directive (pfile);
1906 _cpp_clean_line (pfile);
14ccf800
TT
1907 save_directive = pfile->directive;
1908 pfile->directive = &dtable[T_PRAGMA];
bc4071dd 1909 do_pragma (pfile);
0078a058
TB
1910 if (pfile->directive_result.type == CPP_PRAGMA)
1911 pfile->directive_result.flags |= PRAGMA_OP;
bc4071dd 1912 end_directive (pfile, 1);
14ccf800 1913 pfile->directive = save_directive;
79ba5e3b 1914
bc4071dd
RH
1915 /* We always insert at least one token, the directive result. It'll
1916 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1917 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
1918
1919 /* If we're not handling the pragma internally, read all of the tokens from
1920 the string buffer now, while the string buffer is still installed. */
1921 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1922 to me what the true lifespan of the tokens are. It would appear that
1923 the lifespan is the entire parse of the main input stream, in which case
1924 this may not be wrong. */
1925 if (pfile->directive_result.type == CPP_PRAGMA)
1926 {
1927 int maxcount;
1928
1929 count = 1;
1930 maxcount = 50;
1931 toks = XNEWVEC (cpp_token, maxcount);
1932 toks[0] = pfile->directive_result;
79ba5e3b 1933
bc4071dd
RH
1934 do
1935 {
1936 if (count == maxcount)
1937 {
1938 maxcount = maxcount * 3 / 2;
1939 toks = XRESIZEVEC (cpp_token, toks, maxcount);
1940 }
1c90c6f9 1941 toks[count] = *cpp_get_token (pfile);
0afff540
DM
1942 /* _Pragma is a builtin, so we're not within a macro-map, and so
1943 the token locations are set to bogus ordinary locations
1944 near to, but after that of the "_Pragma".
1945 Paper over this by setting them equal to the location of the
1946 _Pragma itself (PR preprocessor/69126). */
1947 toks[count].src_loc = expansion_loc;
1c90c6f9
JJ
1948 /* Macros have been already expanded by cpp_get_token
1949 if the pragma allowed expansion. */
1950 toks[count++].flags |= NO_EXPAND;
bc4071dd
RH
1951 }
1952 while (toks[count-1].type != CPP_PRAGMA_EOL);
1953 }
1954 else
1955 {
1956 count = 1;
1957 toks = XNEW (cpp_token);
1958 toks[0] = pfile->directive_result;
1959
1960 /* If we handled the entire pragma internally, make sure we get the
1961 line number correct for the next token. */
1962 if (pfile->cb.line_change)
1963 pfile->cb.line_change (pfile, pfile->cur_token, false);
1964 }
1965
1966 /* Finish inlining run_directive. */
1967 pfile->buffer->file = NULL;
1968 _cpp_pop_buffer (pfile);
1969
1970 /* Reset the old macro state before ... */
1971 XDELETE (pfile->context);
1972 pfile->context = saved_context;
1973 pfile->cur_token = saved_cur_token;
1974 pfile->cur_run = saved_cur_run;
1975
1976 /* ... inserting the new tokens we collected. */
1977 _cpp_push_token_context (pfile, NULL, toks, count);
a5c3cccd
NB
1978}
1979
5b9a40df
TT
1980/* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
1981int
620e594b 1982_cpp_do__Pragma (cpp_reader *pfile, location_t expansion_loc)
a5c3cccd 1983{
4ed5bcfb 1984 const cpp_token *string = get__Pragma_string (pfile);
21b11495 1985 pfile->directive_result.type = CPP_PADDING;
a5c3cccd 1986
79ba5e3b 1987 if (string)
5b9a40df 1988 {
0afff540 1989 destringize_and_run (pfile, &string->val.str, expansion_loc);
5b9a40df
TT
1990 return 1;
1991 }
1992 cpp_error (pfile, CPP_DL_ERROR,
1993 "_Pragma takes a parenthesized string literal");
1994 return 0;
a5c3cccd 1995}
21b11495 1996
5d8ebbd8 1997/* Handle #ifdef. */
711b8824 1998static void
6cf87ca4 1999do_ifdef (cpp_reader *pfile)
168d3732 2000{
93c80368 2001 int skip = 1;
041c3194 2002
cef0d199 2003 if (! pfile->state.skipping)
93c80368 2004 {
93d45d9e 2005 cpp_hashnode *node = lex_macro_node (pfile, false);
041c3194 2006
93c80368 2007 if (node)
a69cbaac 2008 {
ad1a3914 2009 skip = !_cpp_defined_macro_p (node);
13f93cf5
NS
2010 if (!_cpp_maybe_notify_macro_use (pfile, node, pfile->directive_line))
2011 /* It wasn't a macro after all. */
2012 skip = true;
a69cbaac 2013 _cpp_mark_macro_used (node);
3de8a540
AC
2014 if (pfile->cb.used)
2015 pfile->cb.used (pfile, pfile->directive_line, node);
a5cb563b 2016 check_eol (pfile, false);
a69cbaac 2017 }
93c80368 2018 }
168d3732 2019
93c80368
NB
2020 push_conditional (pfile, skip, T_IFDEF, 0);
2021}
168d3732 2022
5d8ebbd8 2023/* Handle #ifndef. */
711b8824 2024static void
6cf87ca4 2025do_ifndef (cpp_reader *pfile)
168d3732 2026{
93c80368 2027 int skip = 1;
93d45d9e 2028 cpp_hashnode *node = 0;
168d3732 2029
cef0d199 2030 if (! pfile->state.skipping)
5af7e2c2 2031 {
ee1c2a10 2032 node = lex_macro_node (pfile, false);
b43db0b3
GK
2033
2034 if (node)
a69cbaac 2035 {
ad1a3914 2036 skip = _cpp_defined_macro_p (node);
13f93cf5
NS
2037 if (!_cpp_maybe_notify_macro_use (pfile, node, pfile->directive_line))
2038 /* It wasn't a macro after all. */
2039 skip = false;
a69cbaac 2040 _cpp_mark_macro_used (node);
3de8a540
AC
2041 if (pfile->cb.used)
2042 pfile->cb.used (pfile, pfile->directive_line, node);
a5cb563b 2043 check_eol (pfile, false);
a69cbaac 2044 }
5af7e2c2 2045 }
041c3194 2046
93c80368 2047 push_conditional (pfile, skip, T_IFNDEF, node);
7f2935c7
PB
2048}
2049
6d18adbc
NB
2050/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
2051 pfile->mi_ind_cmacro so we can handle multiple-include
6356f892 2052 optimizations. If macro expansion occurs in the expression, we
6d18adbc
NB
2053 cannot treat it as a controlling conditional, since the expansion
2054 could change in the future. That is handled by cpp_get_token. */
711b8824 2055static void
6cf87ca4 2056do_if (cpp_reader *pfile)
7f2935c7 2057{
93c80368 2058 int skip = 1;
7f2935c7 2059
cef0d199 2060 if (! pfile->state.skipping)
d750887f 2061 skip = _cpp_parse_expr (pfile, true) == false;
93c80368 2062
6d18adbc 2063 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
7f2935c7
PB
2064}
2065
b528a07e 2066/* Flip skipping state if appropriate and continue without changing
ea4a453b
ZW
2067 if_stack; this is so that the error message for missing #endif's
2068 etc. will point to the original #if. */
711b8824 2069static void
6cf87ca4 2070do_else (cpp_reader *pfile)
ed705a82 2071{
b528a07e
NB
2072 cpp_buffer *buffer = pfile->buffer;
2073 struct if_stack *ifs = buffer->if_stack;
ff2b53ef 2074
ea4a453b 2075 if (ifs == NULL)
0527bc4e 2076 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
93c80368 2077 else
ff2b53ef 2078 {
93c80368
NB
2079 if (ifs->type == T_ELSE)
2080 {
0527bc4e
JDA
2081 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
2082 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
93c80368
NB
2083 "the conditional began here");
2084 }
b528a07e
NB
2085 ifs->type = T_ELSE;
2086
cef0d199
NB
2087 /* Skip any future (erroneous) #elses or #elifs. */
2088 pfile->state.skipping = ifs->skip_elses;
2089 ifs->skip_elses = true;
7f2935c7 2090
93c80368
NB
2091 /* Invalidate any controlling macro. */
2092 ifs->mi_cmacro = 0;
93c80368 2093
cef0d199 2094 /* Only check EOL if was not originally skipping. */
909de5da 2095 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
81b5d104 2096 check_eol_endif_labels (pfile);
cef0d199 2097 }
7f2935c7
PB
2098}
2099
71d38ec8
JM
2100/* Handle a #elif, #elifdef or #elifndef directive by not changing if_stack
2101 either. See the comment above do_else. */
711b8824 2102static void
6cf87ca4 2103do_elif (cpp_reader *pfile)
7f2935c7 2104{
b528a07e
NB
2105 cpp_buffer *buffer = pfile->buffer;
2106 struct if_stack *ifs = buffer->if_stack;
7f2935c7 2107
ea4a453b 2108 if (ifs == NULL)
71d38ec8 2109 cpp_error (pfile, CPP_DL_ERROR, "#%s without #if", pfile->directive->name);
b528a07e 2110 else
40ea76de 2111 {
b528a07e
NB
2112 if (ifs->type == T_ELSE)
2113 {
71d38ec8
JM
2114 cpp_error (pfile, CPP_DL_ERROR, "#%s after #else",
2115 pfile->directive->name);
0527bc4e 2116 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
b528a07e
NB
2117 "the conditional began here");
2118 }
2119 ifs->type = T_ELIF;
93c80368 2120
10ef8f28
MP
2121 /* See DR#412: "Only the first group whose control condition
2122 evaluates to true (nonzero) is processed; any following groups
2123 are skipped and their controlling directives are processed as
2124 if they were in a group that is skipped." */
2125 if (ifs->skip_elses)
f43eb770
JJ
2126 {
2127 /* In older GNU standards, #elifdef/#elifndef is supported
2128 as an extension, but pedwarn if -pedantic if the presence
2129 of the directive would be rejected. */
2130 if (pfile->directive != &dtable[T_ELIF]
2131 && ! CPP_OPTION (pfile, elifdef)
2132 && CPP_PEDANTIC (pfile)
2133 && !pfile->state.skipping)
2134 {
2135 if (CPP_OPTION (pfile, cplusplus))
2136 cpp_error (pfile, CPP_DL_PEDWARN,
2137 "#%s before C++23 is a GCC extension",
2138 pfile->directive->name);
2139 else
2140 cpp_error (pfile, CPP_DL_PEDWARN,
2141 "#%s before C2X is a GCC extension",
2142 pfile->directive->name);
2143 }
2144 pfile->state.skipping = 1;
2145 }
10ef8f28 2146 else
b528a07e 2147 {
71d38ec8
JM
2148 if (pfile->directive == &dtable[T_ELIF])
2149 pfile->state.skipping = !_cpp_parse_expr (pfile, false);
2150 else
2151 {
2152 cpp_hashnode *node = lex_macro_node (pfile, false);
2153
2154 if (node)
2155 {
2156 bool macro_defined = _cpp_defined_macro_p (node);
2157 if (!_cpp_maybe_notify_macro_use (pfile, node,
2158 pfile->directive_line))
2159 /* It wasn't a macro after all. */
2160 macro_defined = false;
2161 bool skip = (pfile->directive == &dtable[T_ELIFDEF]
2162 ? !macro_defined
2163 : macro_defined);
2164 if (pfile->cb.used)
2165 pfile->cb.used (pfile, pfile->directive_line, node);
2166 check_eol (pfile, false);
f43eb770
JJ
2167 /* In older GNU standards, #elifdef/#elifndef is supported
2168 as an extension, but pedwarn if -pedantic if the presence
2169 of the directive would change behavior. */
2170 if (! CPP_OPTION (pfile, elifdef)
2171 && CPP_PEDANTIC (pfile)
2172 && pfile->state.skipping != skip)
2173 {
2174 if (CPP_OPTION (pfile, cplusplus))
2175 cpp_error (pfile, CPP_DL_PEDWARN,
2176 "#%s before C++23 is a GCC extension",
2177 pfile->directive->name);
2178 else
2179 cpp_error (pfile, CPP_DL_PEDWARN,
2180 "#%s before C2X is a GCC extension",
2181 pfile->directive->name);
2182 }
71d38ec8
JM
2183 pfile->state.skipping = skip;
2184 }
2185 }
2186 ifs->skip_elses = !pfile->state.skipping;
b528a07e 2187 }
cef0d199
NB
2188
2189 /* Invalidate any controlling macro. */
2190 ifs->mi_cmacro = 0;
40ea76de 2191 }
7f2935c7
PB
2192}
2193
71d38ec8
JM
2194/* Handle a #elifdef directive. */
2195static void
2196do_elifdef (cpp_reader *pfile)
2197{
2198 do_elif (pfile);
2199}
2200
2201/* Handle a #elifndef directive. */
2202static void
2203do_elifndef (cpp_reader *pfile)
2204{
2205 do_elif (pfile);
2206}
2207
cef0d199 2208/* #endif pops the if stack and resets pfile->state.skipping. */
711b8824 2209static void
6cf87ca4 2210do_endif (cpp_reader *pfile)
7f2935c7 2211{
b528a07e
NB
2212 cpp_buffer *buffer = pfile->buffer;
2213 struct if_stack *ifs = buffer->if_stack;
ea4a453b 2214
ea4a453b 2215 if (ifs == NULL)
0527bc4e 2216 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
7f2935c7
PB
2217 else
2218 {
cef0d199 2219 /* Only check EOL if was not originally skipping. */
909de5da 2220 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
81b5d104 2221 check_eol_endif_labels (pfile);
cef0d199 2222
93c80368
NB
2223 /* If potential control macro, we go back outside again. */
2224 if (ifs->next == 0 && ifs->mi_cmacro)
2225 {
6d18adbc 2226 pfile->mi_valid = true;
93c80368
NB
2227 pfile->mi_cmacro = ifs->mi_cmacro;
2228 }
2229
b528a07e 2230 buffer->if_stack = ifs->next;
cef0d199 2231 pfile->state.skipping = ifs->was_skipping;
2a967f3d 2232 obstack_free (&pfile->buffer_ob, ifs);
7f2935c7 2233 }
93c80368 2234}
041c3194 2235
5d8ebbd8
NB
2236/* Push an if_stack entry for a preprocessor conditional, and set
2237 pfile->state.skipping to SKIP. If TYPE indicates the conditional
2238 is #if or #ifndef, CMACRO is a potentially controlling macro, and
2239 we need to check here that we are at the top of the file. */
ea4a453b 2240static void
6cf87ca4
ZW
2241push_conditional (cpp_reader *pfile, int skip, int type,
2242 const cpp_hashnode *cmacro)
ea4a453b
ZW
2243{
2244 struct if_stack *ifs;
b528a07e 2245 cpp_buffer *buffer = pfile->buffer;
ea4a453b 2246
72bb2c39 2247 ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
50410426 2248 ifs->line = pfile->directive_line;
b528a07e 2249 ifs->next = buffer->if_stack;
cef0d199
NB
2250 ifs->skip_elses = pfile->state.skipping || !skip;
2251 ifs->was_skipping = pfile->state.skipping;
ea4a453b 2252 ifs->type = type;
6d18adbc
NB
2253 /* This condition is effectively a test for top-of-file. */
2254 if (pfile->mi_valid && pfile->mi_cmacro == 0)
93c80368
NB
2255 ifs->mi_cmacro = cmacro;
2256 else
2257 ifs->mi_cmacro = 0;
ea4a453b 2258
cef0d199 2259 pfile->state.skipping = skip;
b528a07e 2260 buffer->if_stack = ifs;
782331f4 2261}
7061aa5a 2262
5d8ebbd8
NB
2263/* Read the tokens of the answer into the macro pool, in a directive
2264 of type TYPE. Only commit the memory if we intend it as permanent
2265 storage, i.e. the #assert case. Returns 0 on success, and sets
a28fbdba
MLI
2266 ANSWERP to point to the answer. PRED_LOC is the location of the
2267 predicate. */
3fb558b1 2268static bool
620e594b 2269parse_answer (cpp_reader *pfile, int type, location_t pred_loc,
3fb558b1 2270 cpp_macro **answer_ptr)
7f2935c7 2271{
93c80368
NB
2272 /* In a conditional, it is legal to not have an open paren. We
2273 should save the following token in this case. */
3fb558b1 2274 const cpp_token *paren = cpp_get_token (pfile);
93c80368
NB
2275
2276 /* If not a paren, see if we're OK. */
4ed5bcfb 2277 if (paren->type != CPP_OPEN_PAREN)
041c3194 2278 {
93c80368
NB
2279 /* In a conditional no answer is a test for any answer. It
2280 could be followed by any token. */
2281 if (type == T_IF)
bdcbe496
NB
2282 {
2283 _cpp_backup_tokens (pfile, 1);
3fb558b1 2284 return true;
bdcbe496 2285 }
93c80368
NB
2286
2287 /* #unassert with no answer is valid - it removes all answers. */
4ed5bcfb 2288 if (type == T_UNASSERT && paren->type == CPP_EOF)
3fb558b1 2289 return true;
15dad1d9 2290
a28fbdba
MLI
2291 cpp_error_with_line (pfile, CPP_DL_ERROR, pred_loc, 0,
2292 "missing '(' after predicate");
3fb558b1 2293 return false;
041c3194 2294 }
7061aa5a 2295
3fb558b1
NS
2296 cpp_macro *answer = _cpp_new_macro (pfile, cmk_assert,
2297 _cpp_reserve_room (pfile, 0,
2298 sizeof (cpp_macro)));
2299 answer->parm.next = NULL;
2300 unsigned count = 0;
2301 for (;;)
041c3194 2302 {
8c3b2693 2303 const cpp_token *token = cpp_get_token (pfile);
93c80368 2304
041c3194
ZW
2305 if (token->type == CPP_CLOSE_PAREN)
2306 break;
a7abcbbf 2307
93c80368 2308 if (token->type == CPP_EOF)
041c3194 2309 {
0527bc4e 2310 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
3fb558b1 2311 return false;
041c3194 2312 }
8c3b2693 2313
3fb558b1
NS
2314 answer = (cpp_macro *)_cpp_reserve_room
2315 (pfile, sizeof (cpp_macro) + count * sizeof (cpp_token),
2316 sizeof (cpp_token));
2317 answer->exp.tokens[count++] = *token;
041c3194 2318 }
15dad1d9 2319
3fb558b1 2320 if (!count)
7061aa5a 2321 {
0527bc4e 2322 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
3fb558b1 2323 return false;
7f2935c7 2324 }
041c3194 2325
3fb558b1
NS
2326 /* Drop whitespace at start, for answer equivalence purposes. */
2327 answer->exp.tokens[0].flags &= ~PREV_WHITE;
041c3194 2328
3fb558b1
NS
2329 answer->count = count;
2330 *answer_ptr = answer;
2331
2332 return true;
93c80368 2333}
041c3194 2334
5d8ebbd8 2335/* Parses an assertion directive of type TYPE, returning a pointer to
a570d97f
NS
2336 the hash node of the predicate, or 0 on error. The node is
2337 guaranteed to be disjoint from the macro namespace, so can only
2338 have type 'NT_VOID'. If an answer was supplied, it is placed in
2339 *ANSWER_PTR, which is otherwise set to 0. */
93c80368 2340static cpp_hashnode *
3fb558b1 2341parse_assertion (cpp_reader *pfile, int type, cpp_macro **answer_ptr)
93c80368
NB
2342{
2343 cpp_hashnode *result = 0;
93c80368
NB
2344
2345 /* We don't expand predicates or answers. */
2346 pfile->state.prevent_expansion++;
2347
3fb558b1
NS
2348 *answer_ptr = NULL;
2349
2350 const cpp_token *predicate = cpp_get_token (pfile);
4ed5bcfb 2351 if (predicate->type == CPP_EOF)
0527bc4e 2352 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
4ed5bcfb 2353 else if (predicate->type != CPP_NAME)
a28fbdba
MLI
2354 cpp_error_with_line (pfile, CPP_DL_ERROR, predicate->src_loc, 0,
2355 "predicate must be an identifier");
3fb558b1 2356 else if (parse_answer (pfile, type, predicate->src_loc, answer_ptr))
93c80368 2357 {
9a0c6187 2358 unsigned int len = NODE_LEN (predicate->val.node.node);
c3f829c1 2359 unsigned char *sym = (unsigned char *) alloca (len + 1);
041c3194 2360
93c80368
NB
2361 /* Prefix '#' to get it out of macro namespace. */
2362 sym[0] = '#';
9a0c6187 2363 memcpy (sym + 1, NODE_NAME (predicate->val.node.node), len);
93c80368
NB
2364 result = cpp_lookup (pfile, sym, len + 1);
2365 }
7061aa5a 2366
93c80368 2367 pfile->state.prevent_expansion--;
3fb558b1 2368
93c80368 2369 return result;
7f2935c7 2370}
7061aa5a 2371
5d8ebbd8 2372/* Returns a pointer to the pointer to CANDIDATE in the answer chain,
041c3194 2373 or a pointer to NULL if the answer is not in the chain. */
3fb558b1
NS
2374static cpp_macro **
2375find_answer (cpp_hashnode *node, const cpp_macro *candidate)
7f2935c7 2376{
93c80368 2377 unsigned int i;
3fb558b1 2378 cpp_macro **result = NULL;
7f2935c7 2379
3fb558b1 2380 for (result = &node->value.answers; *result; result = &(*result)->parm.next)
93c80368 2381 {
3fb558b1 2382 cpp_macro *answer = *result;
93c80368
NB
2383
2384 if (answer->count == candidate->count)
2385 {
2386 for (i = 0; i < answer->count; i++)
3fb558b1
NS
2387 if (!_cpp_equiv_tokens (&answer->exp.tokens[i],
2388 &candidate->exp.tokens[i]))
93c80368
NB
2389 break;
2390
2391 if (i == answer->count)
2392 break;
2393 }
2394 }
ff2b53ef 2395
041c3194
ZW
2396 return result;
2397}
15dad1d9 2398
93c80368 2399/* Test an assertion within a preprocessor conditional. Returns
da7d8304 2400 nonzero on failure, zero on success. On success, the result of
2402645b 2401 the test is written into VALUE, otherwise the value 0. */
93c80368 2402int
6cf87ca4 2403_cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
93c80368 2404{
3fb558b1
NS
2405 cpp_macro *answer;
2406 cpp_hashnode *node = parse_assertion (pfile, T_IF, &answer);
2402645b
HPN
2407
2408 /* For recovery, an erroneous assertion expression is handled as a
2409 failing assertion. */
2410 *value = 0;
2411
93c80368 2412 if (node)
3fb558b1 2413 {
a570d97f 2414 if (node->value.answers)
3fb558b1
NS
2415 *value = !answer || *find_answer (node, answer);
2416 }
91318908
NB
2417 else if (pfile->cur_token[-1].type == CPP_EOF)
2418 _cpp_backup_tokens (pfile, 1);
93c80368
NB
2419
2420 /* We don't commit the memory for the answer - it's temporary only. */
2421 return node == 0;
2422}
2423
5d8ebbd8 2424/* Handle #assert. */
711b8824 2425static void
6cf87ca4 2426do_assert (cpp_reader *pfile)
041c3194 2427{
3fb558b1
NS
2428 cpp_macro *answer;
2429 cpp_hashnode *node = parse_assertion (pfile, T_ASSERT, &answer);
df383483 2430
041c3194 2431 if (node)
ff2b53ef 2432 {
93c80368
NB
2433 /* Place the new answer in the answer list. First check there
2434 is not a duplicate. */
a570d97f 2435 if (*find_answer (node, answer))
041c3194 2436 {
3fb558b1
NS
2437 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
2438 NODE_NAME (node) + 1);
2439 return;
041c3194 2440 }
8c3b2693 2441
3fb558b1
NS
2442 /* Commit or allocate storage for the answer. */
2443 answer = (cpp_macro *)_cpp_commit_buff
2444 (pfile, sizeof (cpp_macro) - sizeof (cpp_token)
2445 + sizeof (cpp_token) * answer->count);
2446
a570d97f
NS
2447 /* Chain into the list. */
2448 answer->parm.next = node->value.answers;
3fb558b1
NS
2449 node->value.answers = answer;
2450
a5cb563b 2451 check_eol (pfile, false);
ff2b53ef 2452 }
041c3194 2453}
15dad1d9 2454
5d8ebbd8 2455/* Handle #unassert. */
711b8824 2456static void
6cf87ca4 2457do_unassert (cpp_reader *pfile)
041c3194 2458{
3fb558b1
NS
2459 cpp_macro *answer;
2460 cpp_hashnode *node = parse_assertion (pfile, T_UNASSERT, &answer);
df383483 2461
93c80368 2462 /* It isn't an error to #unassert something that isn't asserted. */
a570d97f 2463 if (node)
7061aa5a 2464 {
93c80368 2465 if (answer)
15dad1d9 2466 {
3fb558b1 2467 cpp_macro **p = find_answer (node, answer);
a7abcbbf 2468
3fb558b1
NS
2469 /* Remove the assert from the list. */
2470 if (cpp_macro *temp = *p)
a570d97f 2471 *p = temp->parm.next;
8c3b2693 2472
a5cb563b 2473 check_eol (pfile, false);
93c80368
NB
2474 }
2475 else
2476 _cpp_free_definition (node);
041c3194 2477 }
93c80368
NB
2478
2479 /* We don't commit the memory for the answer - it's temporary only. */
7f2935c7 2480}
7f2935c7 2481
45b966db
ZW
2482/* These are for -D, -U, -A. */
2483
2484/* Process the string STR as if it appeared as the body of a #define.
2485 If STR is just an identifier, define it with value 1.
2486 If STR has anything after the identifier, then it should
ec5c56db 2487 be identifier=definition. */
0b22d65c 2488void
6cf87ca4 2489cpp_define (cpp_reader *pfile, const char *str)
0b22d65c 2490{
86373e7e
JM
2491 char *buf;
2492 const char *p;
45b966db
ZW
2493 size_t count;
2494
df383483 2495 /* Copy the entire option so we can modify it.
45b966db 2496 Change the first "=" in the string to a space. If there is none,
86368122
NB
2497 tack " 1" on the end. */
2498
86368122 2499 count = strlen (str);
c3f829c1 2500 buf = (char *) alloca (count + 3);
86368122
NB
2501 memcpy (buf, str, count);
2502
2503 p = strchr (str, '=');
45b966db 2504 if (p)
86368122 2505 buf[p - str] = ' ';
45b966db
ZW
2506 else
2507 {
86368122
NB
2508 buf[count++] = ' ';
2509 buf[count++] = '1';
0b22d65c 2510 }
26aea073 2511 buf[count] = '\n';
cf4ed945 2512
29401c30 2513 run_directive (pfile, T_DEFINE, buf, count);
2c8f0515
ZW
2514}
2515
6f1ae1ec
PD
2516/* Like cpp_define, but does not warn about unused macro. */
2517void
2518cpp_define_unused (cpp_reader *pfile, const char *str)
2519{
2520 unsigned char warn_unused_macros = CPP_OPTION (pfile, warn_unused_macros);
2521 CPP_OPTION (pfile, warn_unused_macros) = 0;
2522 cpp_define (pfile, str);
2523 CPP_OPTION (pfile, warn_unused_macros) = warn_unused_macros;
2524}
28f68625
DF
2525
2526/* Use to build macros to be run through cpp_define() as
2527 described above.
2528 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
2529
2530void
2531cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...)
2532{
01ca36af 2533 char *ptr;
28f68625
DF
2534
2535 va_list ap;
2536 va_start (ap, fmt);
01ca36af 2537 ptr = xvasprintf (fmt, ap);
28f68625
DF
2538 va_end (ap);
2539
2540 cpp_define (pfile, ptr);
2541 free (ptr);
2542}
2543
6f1ae1ec
PD
2544/* Like cpp_define_formatted, but does not warn about unused macro. */
2545void
2546cpp_define_formatted_unused (cpp_reader *pfile, const char *fmt, ...)
2547{
2548 char *ptr;
2549
2550 va_list ap;
2551 va_start (ap, fmt);
2552 ptr = xvasprintf (fmt, ap);
2553 va_end (ap);
2554
2555 cpp_define_unused (pfile, ptr);
2556 free (ptr);
2557}
28f68625 2558
ad2a084d 2559/* Slight variant of the above for use by initialize_builtins. */
2c8f0515 2560void
6cf87ca4 2561_cpp_define_builtin (cpp_reader *pfile, const char *str)
2c8f0515 2562{
26aea073 2563 size_t len = strlen (str);
c3f829c1 2564 char *buf = (char *) alloca (len + 1);
26aea073
NB
2565 memcpy (buf, str, len);
2566 buf[len] = '\n';
2567 run_directive (pfile, T_DEFINE, buf, len);
45b966db 2568}
0f41302f 2569
45b966db
ZW
2570/* Process MACRO as if it appeared as the body of an #undef. */
2571void
6cf87ca4 2572cpp_undef (cpp_reader *pfile, const char *macro)
7f2935c7 2573{
26aea073 2574 size_t len = strlen (macro);
c3f829c1 2575 char *buf = (char *) alloca (len + 1);
26aea073
NB
2576 memcpy (buf, macro, len);
2577 buf[len] = '\n';
2578 run_directive (pfile, T_UNDEF, buf, len);
7f2935c7
PB
2579}
2580
d6874138
KT
2581/* Replace a previous definition DEF of the macro STR. If DEF is NULL,
2582 or first element is zero, then the macro should be undefined. */
2583static void
2584cpp_pop_definition (cpp_reader *pfile, struct def_pragma_macro *c)
121de39f 2585{
d6874138 2586 cpp_hashnode *node = _cpp_lex_identifier (pfile, c->name);
121de39f
RH
2587 if (node == NULL)
2588 return;
2589
93d45d9e
JM
2590 if (pfile->cb.before_define)
2591 pfile->cb.before_define (pfile);
2592
3f6677f4 2593 if (cpp_macro_p (node))
121de39f
RH
2594 {
2595 if (pfile->cb.undef)
2596 pfile->cb.undef (pfile, pfile->directive_line, node);
2597 if (CPP_OPTION (pfile, warn_unused_macros))
2598 _cpp_warn_if_unused_macro (pfile, node, NULL);
3f6677f4 2599 _cpp_free_definition (node);
121de39f 2600 }
121de39f 2601
d6874138
KT
2602 if (c->is_undef)
2603 return;
aa23e73b
JJ
2604 if (c->is_builtin)
2605 {
2606 _cpp_restore_special_builtin (pfile, c);
2607 return;
2608 }
3f6677f4 2609
d6874138
KT
2610 {
2611 size_t namelen;
2612 const uchar *dn;
2613 cpp_hashnode *h = NULL;
2614 cpp_buffer *nbuf;
2615
2616 namelen = ustrcspn (c->definition, "( \n");
2617 h = cpp_lookup (pfile, c->definition, namelen);
2618 dn = c->definition + namelen;
2619
d6874138
KT
2620 nbuf = cpp_push_buffer (pfile, dn, ustrchr (dn, '\n') - dn, true);
2621 if (nbuf != NULL)
2622 {
2623 _cpp_clean_line (pfile);
2624 nbuf->sysp = 1;
2625 if (!_cpp_create_definition (pfile, h))
2626 abort ();
2627 _cpp_pop_buffer (pfile);
2628 }
2629 else
2630 abort ();
2631 h->value.macro->line = c->line;
2632 h->value.macro->syshdr = c->syshdr;
2633 h->value.macro->used = c->used;
2634 }
121de39f
RH
2635}
2636
ec5c56db 2637/* Process the string STR as if it appeared as the body of a #assert. */
45b966db 2638void
6cf87ca4 2639cpp_assert (cpp_reader *pfile, const char *str)
45b966db 2640{
86368122 2641 handle_assertion (pfile, str, T_ASSERT);
45b966db 2642}
7f2935c7 2643
ec5c56db 2644/* Process STR as if it appeared as the body of an #unassert. */
45b966db 2645void
6cf87ca4 2646cpp_unassert (cpp_reader *pfile, const char *str)
7f2935c7 2647{
86368122 2648 handle_assertion (pfile, str, T_UNASSERT);
df383483 2649}
3fdc651f 2650
86368122
NB
2651/* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2652static void
6cf87ca4 2653handle_assertion (cpp_reader *pfile, const char *str, int type)
86368122
NB
2654{
2655 size_t count = strlen (str);
2656 const char *p = strchr (str, '=');
2657
26aea073
NB
2658 /* Copy the entire option so we can modify it. Change the first
2659 "=" in the string to a '(', and tack a ')' on the end. */
c3f829c1 2660 char *buf = (char *) alloca (count + 2);
26aea073
NB
2661
2662 memcpy (buf, str, count);
86368122
NB
2663 if (p)
2664 {
86368122
NB
2665 buf[p - str] = '(';
2666 buf[count++] = ')';
86368122 2667 }
26aea073
NB
2668 buf[count] = '\n';
2669 str = buf;
86368122 2670
29401c30 2671 run_directive (pfile, type, str, count);
86368122
NB
2672}
2673
7e96d768
NB
2674/* The options structure. */
2675cpp_options *
6cf87ca4 2676cpp_get_options (cpp_reader *pfile)
7e96d768
NB
2677{
2678 return &pfile->opts;
2679}
2680
2681/* The callbacks structure. */
2682cpp_callbacks *
6cf87ca4 2683cpp_get_callbacks (cpp_reader *pfile)
7e96d768
NB
2684{
2685 return &pfile->cb;
2686}
2687
2688/* Copy the given callbacks structure to our own. */
2689void
6cf87ca4 2690cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
7e96d768
NB
2691{
2692 pfile->cb = *cb;
2693}
2694
eccec868
JM
2695/* The narrow character set identifier. */
2696const char *
2697cpp_get_narrow_charset_name (cpp_reader *pfile)
2698{
2699 return pfile->narrow_cset_desc.to;
2700}
2701
2702/* The wide character set identifier. */
2703const char *
2704cpp_get_wide_charset_name (cpp_reader *pfile)
2705{
2706 return pfile->wide_cset_desc.to;
2707}
2708
c6e83800 2709/* The dependencies structure. (Creates one if it hasn't already been.) */
99b1c316 2710class mkdeps *
c6e83800
ZW
2711cpp_get_deps (cpp_reader *pfile)
2712{
918e8b10 2713 if (!pfile->deps && CPP_OPTION (pfile, deps.style) != DEPS_NONE)
c6e83800
ZW
2714 pfile->deps = deps_init ();
2715 return pfile->deps;
2716}
2717
eb1f4d9d
NB
2718/* Push a new buffer on the buffer stack. Returns the new buffer; it
2719 doesn't fail. It does not generate a file change call back; that
2720 is the responsibility of the caller. */
c71f835b 2721cpp_buffer *
6cf87ca4 2722cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
40de9f76 2723 int from_stage3)
c71f835b 2724{
c3f829c1 2725 cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
93c80368 2726
fde84349 2727 /* Clears, amongst other things, if_stack and mi_cmacro. */
c3f829c1 2728 memset (new_buffer, 0, sizeof (cpp_buffer));
fde84349 2729
c3f829c1
GDR
2730 new_buffer->next_line = new_buffer->buf = buffer;
2731 new_buffer->rlimit = buffer + len;
2732 new_buffer->from_stage3 = from_stage3;
2733 new_buffer->prev = pfile->buffer;
2734 new_buffer->need_line = true;
0bda4760 2735
c3f829c1 2736 pfile->buffer = new_buffer;
cf551fba 2737
c3f829c1 2738 return new_buffer;
c71f835b
ZW
2739}
2740
af0d16cd
NB
2741/* Pops a single buffer, with a file change call-back if appropriate.
2742 Then pushes the next -include file, if any remain. */
ef6e958a 2743void
6cf87ca4 2744_cpp_pop_buffer (cpp_reader *pfile)
c71f835b 2745{
fde84349 2746 cpp_buffer *buffer = pfile->buffer;
8f9b4009 2747 struct _cpp_file *inc = buffer->file;
ad2a084d 2748 struct if_stack *ifs;
28937f11 2749 const unsigned char *to_free;
c71f835b 2750
fde84349
NB
2751 /* Walk back up the conditional stack till we reach its level at
2752 entry to this file, issuing error messages. */
2753 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
0527bc4e 2754 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
fde84349 2755 "unterminated #%s", dtable[ifs->type].name);
eb1f4d9d 2756
97293897 2757 /* In case of a missing #endif. */
67821e3a 2758 pfile->state.skipping = 0;
29401c30 2759
af0d16cd 2760 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
29401c30
NB
2761 pfile->buffer = buffer->prev;
2762
28937f11 2763 to_free = buffer->to_free;
26aea073
NB
2764 free (buffer->notes);
2765
af0d16cd
NB
2766 /* Free the buffer object now; we may want to push a new buffer
2767 in _cpp_push_next_include_file. */
2768 obstack_free (&pfile->buffer_ob, buffer);
29401c30 2769
af0d16cd
NB
2770 if (inc)
2771 {
28937f11 2772 _cpp_pop_file_buffer (pfile, inc, to_free);
af0d16cd 2773
40de9f76 2774 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
af0d16cd 2775 }
705b0c05
NS
2776 else if (to_free)
2777 free ((void *)to_free);
c71f835b
ZW
2778}
2779
05713b80 2780/* Enter all recognized directives in the hash table. */
c71f835b 2781void
6cf87ca4 2782_cpp_init_directives (cpp_reader *pfile)
c71f835b 2783{
ad1a3914 2784 for (int i = 0; i < N_DIRECTIVES; i++)
93c80368 2785 {
ad1a3914 2786 cpp_hashnode *node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
4977bab6
ZW
2787 node->is_directive = 1;
2788 node->directive_index = i;
93c80368 2789 }
c71f835b 2790}
a15f7cb8
ESR
2791
2792/* Extract header file from a bracket include. Parsing starts after '<'.
2793 The string is malloced and must be freed by the caller. */
2794char *
2795_cpp_bracket_include(cpp_reader *pfile)
2796{
2797 return glue_header_name (pfile);
2798}
2799