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