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