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