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