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