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