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