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