]> gcc.gnu.org Git - gcc.git/blame - gcc/cpplib.c
c-opts.c (c_common_post_options): Don't call cpp_find_main_file yet.
[gcc.git] / gcc / cpplib.c
CommitLineData
a949941c 1/* CPP Library. (Directive handling.)
5e7b4e25 2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
23345bbb 3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4c8cc616 4 Contributed by Per Bothner, 1994-95.
d8bfa78c 5 Based on CCCP program by Paul Rubin, June 1986
7f2935c7
PB
6 Adapted to ANSI C, Richard Stallman, Jan 1987
7
8This program is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 2, or (at your option) any
11later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
956d6950 20Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
7f2935c7 21
956d6950 22#include "config.h"
b04cd507 23#include "system.h"
956d6950
JL
24#include "cpplib.h"
25#include "cpphash.h"
c71f835b 26#include "obstack.h"
7f2935c7 27
93c80368
NB
28/* Chained list of answers to an assertion. */
29struct answer
30{
31 struct answer *next;
32 unsigned int count;
33 cpp_token first[1];
34};
35
88ae23e7
ZW
36/* Stack of conditionals currently in progress
37 (including both successful and failing conditionals). */
88ae23e7
ZW
38struct if_stack
39{
40 struct if_stack *next;
50410426 41 unsigned int line; /* Line where condition started. */
93c80368 42 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
cef0d199
NB
43 bool skip_elses; /* Can future #else / #elif be skipped? */
44 bool was_skipping; /* If were skipping on entry. */
6cf87ca4 45 int type; /* Most recent conditional for diagnostics. */
88ae23e7 46};
88ae23e7 47
a5da89c6 48/* Contains a registered pragma or pragma namespace. */
6cf87ca4 49typedef void (*pragma_cb) (cpp_reader *);
a5da89c6
NB
50struct pragma_entry
51{
52 struct pragma_entry *next;
4b115ff0 53 const cpp_hashnode *pragma; /* Name and length. */
a5da89c6
NB
54 int is_nspace;
55 union {
56 pragma_cb handler;
57 struct pragma_entry *space;
58 } u;
59};
60
93c80368
NB
61/* Values for the origin field of struct directive. KANDR directives
62 come from traditional (K&R) C. STDC89 directives come from the
63 1989 C standard. EXTENSION directives are extensions. */
64#define KANDR 0
65#define STDC89 1
66#define EXTENSION 2
67
68/* Values for the flags field of struct directive. COND indicates a
69 conditional; IF_COND an opening conditional. INCL means to treat
70 "..." and <...> as q-char and h-char sequences respectively. IN_I
71 means this directive should be handled even if -fpreprocessed is in
1a76916c
NB
72 effect (these are the directives with callback hooks).
73
d97371e0 74 EXPAND is set on directives that are always macro-expanded. */
93c80368
NB
75#define COND (1 << 0)
76#define IF_COND (1 << 1)
77#define INCL (1 << 2)
78#define IN_I (1 << 3)
1a76916c 79#define EXPAND (1 << 4)
93c80368
NB
80
81/* Defines one #-directive, including how to handle it. */
6cf87ca4 82typedef void (*directive_handler) (cpp_reader *);
93c80368
NB
83typedef struct directive directive;
84struct directive
85{
86 directive_handler handler; /* Function to handle directive. */
562a5c27 87 const uchar *name; /* Name of directive. */
93c80368
NB
88 unsigned short length; /* Length of name. */
89 unsigned char origin; /* Origin of directive. */
90 unsigned char flags; /* Flags describing this directive. */
91};
92
1316f1f7
ZW
93/* Forward declarations. */
94
6cf87ca4
ZW
95static void skip_rest_of_line (cpp_reader *);
96static void check_eol (cpp_reader *);
97static void start_directive (cpp_reader *);
98static void prepare_directive_trad (cpp_reader *);
99static void end_directive (cpp_reader *, int);
100static void directive_diagnostics (cpp_reader *, const directive *, int);
101static void run_directive (cpp_reader *, int, const char *, size_t);
102static char *glue_header_name (cpp_reader *);
103static const char *parse_include (cpp_reader *, int *);
104static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
105static unsigned int read_flag (cpp_reader *, unsigned int);
6cf87ca4
ZW
106static int strtoul_for_line (const uchar *, unsigned int, unsigned long *);
107static void do_diagnostic (cpp_reader *, int, int);
108static cpp_hashnode *lex_macro_node (cpp_reader *);
d1bd0ded 109static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
6cf87ca4
ZW
110static void do_include_common (cpp_reader *, enum include_type);
111static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
112 const cpp_hashnode *);
113static struct pragma_entry *insert_pragma_entry (cpp_reader *,
114 struct pragma_entry **,
115 const cpp_hashnode *,
116 pragma_cb);
117static int count_registered_pragmas (struct pragma_entry *);
118static char ** save_registered_pragmas (struct pragma_entry *, char **);
119static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
120 char **);
121static void do_pragma_once (cpp_reader *);
122static void do_pragma_poison (cpp_reader *);
123static void do_pragma_system_header (cpp_reader *);
124static void do_pragma_dependency (cpp_reader *);
125static void do_linemarker (cpp_reader *);
126static const cpp_token *get_token_no_padding (cpp_reader *);
127static const cpp_token *get__Pragma_string (cpp_reader *);
128static void destringize_and_run (cpp_reader *, const cpp_string *);
129static int parse_answer (cpp_reader *, struct answer **, int);
130static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
131static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
132static void handle_assertion (cpp_reader *, const char *, int);
d481b69b 133
168d3732
ZW
134/* This is the table of directive handlers. It is ordered by
135 frequency of occurrence; the numbers at the end are directive
136 counts from all the source code I have lying around (egcs and libc
137 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
93c80368
NB
138 pcmcia-cs-3.0.9). This is no longer important as directive lookup
139 is now O(1). All extensions other than #warning and #include_next
140 are deprecated. The name is where the extension appears to have
141 come from. */
168d3732 142
93c80368
NB
143#define DIRECTIVE_TABLE \
144D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
d97371e0 145D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
93c80368
NB
146D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
147D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
1a76916c 148D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
93c80368
NB
149D(else, T_ELSE, KANDR, COND) /* 9863 */ \
150D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
151D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
1a76916c
NB
152D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
153D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
93c80368
NB
154D(error, T_ERROR, STDC89, 0) /* 475 */ \
155D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
156D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
d97371e0 157D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
93c80368 158D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
d97371e0 159D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
93c80368
NB
160D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
161D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
74d06cf2 162D(sccs, T_SCCS, EXTENSION, 0) /* 0 SVR4? */
07aa0b04 163
168d3732
ZW
164/* Use the table to generate a series of prototypes, an enum for the
165 directive names, and an array of directive handlers. */
166
6cf87ca4 167#define D(name, t, o, f) static void do_##name (cpp_reader *);
168d3732
ZW
168DIRECTIVE_TABLE
169#undef D
170
041c3194 171#define D(n, tag, o, f) tag,
168d3732
ZW
172enum
173{
174 DIRECTIVE_TABLE
175 N_DIRECTIVES
7f2935c7 176};
168d3732
ZW
177#undef D
178
041c3194 179#define D(name, t, origin, flags) \
9a238586
KG
180{ do_##name, (const uchar *) #name, \
181 sizeof #name - 1, origin, flags },
93c80368 182static const directive dtable[] =
168d3732
ZW
183{
184DIRECTIVE_TABLE
185};
186#undef D
187#undef DIRECTIVE_TABLE
7f2935c7 188
dcc229e5
ZW
189/* Wrapper struct directive for linemarkers.
190 The origin is more or less true - the original K+R cpp
191 did use this notation in its preprocessed output. */
192static const directive linemarker_dir =
193{
194 do_linemarker, U"#", 1, KANDR, IN_I
195};
196
1a76916c 197#define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
67821e3a 198
93c80368
NB
199/* Skip any remaining tokens in a directive. */
200static void
6cf87ca4 201skip_rest_of_line (cpp_reader *pfile)
c5a04734 202{
93c80368 203 /* Discard all stacked contexts. */
8128cccf 204 while (pfile->context->prev)
93c80368
NB
205 _cpp_pop_context (pfile);
206
b528a07e 207 /* Sweep up all tokens remaining on the line. */
345894b4
NB
208 if (! SEEN_EOL ())
209 while (_cpp_lex_token (pfile)->type != CPP_EOF)
210 ;
93c80368 211}
c5a04734 212
93c80368
NB
213/* Ensure there are no stray tokens at the end of a directive. */
214static void
6cf87ca4 215check_eol (cpp_reader *pfile)
93c80368 216{
345894b4 217 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
ebef4e8c
NB
218 cpp_error (pfile, DL_PEDWARN, "extra tokens at end of #%s directive",
219 pfile->directive->name);
93c80368
NB
220}
221
fe6c2db9
NB
222/* Called when entering a directive, _Pragma or command-line directive. */
223static void
6cf87ca4 224start_directive (cpp_reader *pfile)
93c80368 225{
7f2f1a66
NB
226 /* Setup in-directive state. */
227 pfile->state.in_directive = 1;
228 pfile->state.save_comments = 0;
229
93c80368 230 /* Some handlers need the position of the # for diagnostics. */
8bbbef34 231 pfile->directive_line = pfile->line;
fe6c2db9
NB
232}
233
234/* Called when leaving a directive, _Pragma or command-line directive. */
235static void
6cf87ca4 236end_directive (cpp_reader *pfile, int skip_line)
fe6c2db9 237{
1a76916c
NB
238 if (CPP_OPTION (pfile, traditional))
239 {
d97371e0
NB
240 /* Revert change of prepare_directive_trad. */
241 pfile->state.prevent_expansion--;
242
b66377c1 243 if (pfile->directive != &dtable[T_DEFINE])
1a76916c
NB
244 _cpp_remove_overlay (pfile);
245 }
fe6c2db9 246 /* We don't skip for an assembler #. */
b66377c1 247 else if (skip_line)
67821e3a
NB
248 {
249 skip_rest_of_line (pfile);
bdcbe496
NB
250 if (!pfile->keep_tokens)
251 {
252 pfile->cur_run = &pfile->base_run;
253 pfile->cur_token = pfile->base_run.base;
254 }
67821e3a 255 }
fe6c2db9
NB
256
257 /* Restore state. */
fe6c2db9
NB
258 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
259 pfile->state.in_directive = 0;
d97371e0 260 pfile->state.in_expression = 0;
fe6c2db9
NB
261 pfile->state.angled_headers = 0;
262 pfile->directive = 0;
263}
264
1a76916c
NB
265/* Prepare to handle the directive in pfile->directive. */
266static void
6cf87ca4 267prepare_directive_trad (cpp_reader *pfile)
1a76916c 268{
951a0766 269 if (pfile->directive != &dtable[T_DEFINE])
1a76916c 270 {
b66377c1
NB
271 bool no_expand = (pfile->directive
272 && ! (pfile->directive->flags & EXPAND));
974c43f1 273 bool was_skipping = pfile->state.skipping;
1a76916c 274
974c43f1 275 pfile->state.skipping = false;
d97371e0
NB
276 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
277 || pfile->directive == &dtable[T_ELIF]);
1a76916c
NB
278 if (no_expand)
279 pfile->state.prevent_expansion++;
43839642 280 _cpp_scan_out_logical_line (pfile, NULL);
1a76916c
NB
281 if (no_expand)
282 pfile->state.prevent_expansion--;
974c43f1 283 pfile->state.skipping = was_skipping;
1a76916c
NB
284 _cpp_overlay_buffer (pfile, pfile->out.base,
285 pfile->out.cur - pfile->out.base);
286 }
d97371e0
NB
287
288 /* Stop ISO C from expanding anything. */
289 pfile->state.prevent_expansion++;
1a76916c
NB
290}
291
da7d8304 292/* Output diagnostics for a directive DIR. INDENTED is nonzero if
18a9d8ff 293 the '#' was indented. */
18a9d8ff 294static void
6cf87ca4 295directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
18a9d8ff 296{
dcc229e5
ZW
297 /* Issue -pedantic warnings for extensions. */
298 if (CPP_PEDANTIC (pfile)
299 && ! pfile->state.skipping
300 && dir->origin == EXTENSION)
ebef4e8c 301 cpp_error (pfile, DL_PEDWARN, "#%s is a GCC extension", dir->name);
dcc229e5
ZW
302
303 /* Traditionally, a directive is ignored unless its # is in
304 column 1. Therefore in code intended to work with K+R
305 compilers, directives added by C89 must have their #
306 indented, and directives present in traditional C must not.
307 This is true even of directives in skipped conditional
308 blocks. #elif cannot be used at all. */
309 if (CPP_WTRADITIONAL (pfile))
18a9d8ff 310 {
dcc229e5 311 if (dir == &dtable[T_ELIF])
ebef4e8c
NB
312 cpp_error (pfile, DL_WARNING,
313 "suggest not using #elif in traditional C");
dcc229e5 314 else if (indented && dir->origin == KANDR)
ebef4e8c
NB
315 cpp_error (pfile, DL_WARNING,
316 "traditional C ignores #%s with the # indented",
317 dir->name);
dcc229e5 318 else if (!indented && dir->origin != KANDR)
ebef4e8c
NB
319 cpp_error (pfile, DL_WARNING,
320 "suggest hiding #%s from traditional C with an indented #",
321 dir->name);
18a9d8ff
NB
322 }
323}
324
da7d8304 325/* Check if we have a known directive. INDENTED is nonzero if the
18a9d8ff
NB
326 '#' of the directive was indented. This function is in this file
327 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
da7d8304 328 nonzero if the line of tokens has been handled, zero if we should
18a9d8ff 329 continue processing the line. */
fe6c2db9 330int
6cf87ca4 331_cpp_handle_directive (cpp_reader *pfile, int indented)
fe6c2db9 332{
fe6c2db9 333 const directive *dir = 0;
345894b4 334 const cpp_token *dname;
e808ec9c 335 bool was_parsing_args = pfile->state.parsing_args;
fe6c2db9
NB
336 int skip = 1;
337
e808ec9c
NB
338 if (was_parsing_args)
339 {
340 if (CPP_OPTION (pfile, pedantic))
ebef4e8c 341 cpp_error (pfile, DL_PEDWARN,
e808ec9c
NB
342 "embedding a directive within macro arguments is not portable");
343 pfile->state.parsing_args = 0;
344 pfile->state.prevent_expansion = 0;
345 }
fe6c2db9 346 start_directive (pfile);
345894b4 347 dname = _cpp_lex_token (pfile);
0d9f234d 348
345894b4 349 if (dname->type == CPP_NAME)
0d9f234d 350 {
4977bab6
ZW
351 if (dname->val.node->is_directive)
352 dir = &dtable[dname->val.node->directive_index];
0d9f234d 353 }
05713b80 354 /* We do not recognize the # followed by a number extension in
18a9d8ff 355 assembler code. */
345894b4 356 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
0d9f234d 357 {
dcc229e5
ZW
358 dir = &linemarker_dir;
359 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
360 && ! pfile->state.skipping)
ebef4e8c
NB
361 cpp_error (pfile, DL_PEDWARN,
362 "style of line directive is a GCC extension");
93c80368 363 }
0d9f234d 364
93c80368
NB
365 if (dir)
366 {
18a9d8ff
NB
367 /* If we have a directive that is not an opening conditional,
368 invalidate any control macro. */
369 if (! (dir->flags & IF_COND))
370 pfile->mi_valid = false;
371
372 /* Kluge alert. In order to be sure that code like this
373
374 #define HASH #
375 HASH define foo bar
376
377 does not cause '#define foo bar' to get executed when
378 compiled with -save-temps, we recognize directives in
379 -fpreprocessed mode only if the # is in column 1. cppmacro.c
a1f300c0 380 puts a space in front of any '#' at the start of a macro. */
18a9d8ff
NB
381 if (CPP_OPTION (pfile, preprocessed)
382 && (indented || !(dir->flags & IN_I)))
6d4587f7 383 {
18a9d8ff
NB
384 skip = 0;
385 dir = 0;
6d4587f7
ZW
386 }
387 else
93c80368 388 {
18a9d8ff
NB
389 /* In failed conditional groups, all non-conditional
390 directives are ignored. Before doing that, whether
391 skipping or not, we should lex angle-bracketed headers
392 correctly, and maybe output some diagnostics. */
393 pfile->state.angled_headers = dir->flags & INCL;
a8d0ddaf 394 pfile->state.directive_wants_padding = dir->flags & INCL;
18a9d8ff
NB
395 if (! CPP_OPTION (pfile, preprocessed))
396 directive_diagnostics (pfile, dir, indented);
397 if (pfile->state.skipping && !(dir->flags & COND))
398 dir = 0;
93c80368
NB
399 }
400 }
345894b4 401 else if (dname->type == CPP_EOF)
18a9d8ff
NB
402 ; /* CPP_EOF is the "null directive". */
403 else
93c80368
NB
404 {
405 /* An unknown directive. Don't complain about it in assembly
406 source: we don't know where the comments are, and # may
407 introduce assembler pseudo-ops. Don't complain about invalid
408 directives in skipped conditional groups (6.10 p4). */
bdb05a7b 409 if (CPP_OPTION (pfile, lang) == CLK_ASM)
18a9d8ff
NB
410 skip = 0;
411 else if (!pfile->state.skipping)
ebef4e8c 412 cpp_error (pfile, DL_ERROR, "invalid preprocessing directive #%s",
345894b4 413 cpp_token_as_text (pfile, dname));
0d9f234d
NB
414 }
415
d1a58688
NB
416 pfile->directive = dir;
417 if (CPP_OPTION (pfile, traditional))
418 prepare_directive_trad (pfile);
419
18a9d8ff 420 if (dir)
6cf87ca4 421 pfile->directive->handler (pfile);
18a9d8ff
NB
422 else if (skip == 0)
423 _cpp_backup_tokens (pfile, 1);
424
425 end_directive (pfile, skip);
e808ec9c
NB
426 if (was_parsing_args)
427 {
428 /* Restore state when within macro args. */
429 pfile->state.parsing_args = 2;
430 pfile->state.prevent_expansion = 1;
e808ec9c 431 }
fe6c2db9 432 return skip;
041c3194 433}
7f2935c7 434
93c80368 435/* Directive handler wrapper used by the command line option
26aea073 436 processor. BUF is \n terminated. */
93c80368 437static void
6cf87ca4 438run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
3fdc651f 439{
562a5c27 440 cpp_push_buffer (pfile, (const uchar *) buf, count,
29401c30 441 /* from_stage3 */ true, 1);
8bfb1467
NB
442 /* Disgusting hack. */
443 if (dir_no == T_PRAGMA)
8f9b4009 444 pfile->buffer->file = pfile->buffer->prev->file;
0bda4760 445 start_directive (pfile);
26aea073
NB
446
447 /* This is a short-term fix to prevent a leading '#' being
448 interpreted as a directive. */
449 _cpp_clean_line (pfile);
450
f71aebba 451 pfile->directive = &dtable[dir_no];
1a76916c
NB
452 if (CPP_OPTION (pfile, traditional))
453 prepare_directive_trad (pfile);
6cf87ca4 454 pfile->directive->handler (pfile);
0bda4760 455 end_directive (pfile, 1);
8bfb1467 456 if (dir_no == T_PRAGMA)
8f9b4009 457 pfile->buffer->file = NULL;
ef6e958a 458 _cpp_pop_buffer (pfile);
93c80368
NB
459}
460
461/* Checks for validity the macro name in #define, #undef, #ifdef and
462 #ifndef directives. */
041c3194 463static cpp_hashnode *
6cf87ca4 464lex_macro_node (cpp_reader *pfile)
041c3194 465{
1a76916c 466 const cpp_token *token = _cpp_lex_token (pfile);
ea4a453b 467
92936ecf 468 /* The token immediately after #define must be an identifier. That
b8363a24
ZW
469 identifier may not be "defined", per C99 6.10.8p4.
470 In C++, it may not be any of the "named operators" either,
471 per C++98 [lex.digraph], [lex.key].
472 Finally, the identifier may not have been poisoned. (In that case
1d63a28a 473 the lexer has issued the error message for us.) */
cbc69f84 474
1a76916c
NB
475 if (token->type == CPP_NAME)
476 {
477 cpp_hashnode *node = token->val.node;
92936ecf 478
1a76916c
NB
479 if (node == pfile->spec_nodes.n_defined)
480 cpp_error (pfile, DL_ERROR,
481 "\"defined\" cannot be used as a macro name");
482 else if (! (node->flags & NODE_POISONED))
483 return node;
ba89d661 484 }
1a76916c 485 else if (token->flags & NAMED_OP)
cbc69f84
NB
486 cpp_error (pfile, DL_ERROR,
487 "\"%s\" cannot be used as a macro name as it is an operator in C++",
1a76916c
NB
488 NODE_NAME (token->val.node));
489 else if (token->type == CPP_EOF)
490 cpp_error (pfile, DL_ERROR, "no macro name given in #%s directive",
491 pfile->directive->name);
492 else
493 cpp_error (pfile, DL_ERROR, "macro names must be identifiers");
07aa0b04 494
cbc69f84 495 return NULL;
7f2935c7 496}
7f2935c7 497
93c80368 498/* Process a #define directive. Most work is done in cppmacro.c. */
711b8824 499static void
6cf87ca4 500do_define (cpp_reader *pfile)
7f2935c7 501{
93c80368 502 cpp_hashnode *node = lex_macro_node (pfile);
ff2b53ef 503
93c80368
NB
504 if (node)
505 {
1d63a28a
NB
506 /* If we have been requested to expand comments into macros,
507 then re-enable saving of comments. */
508 pfile->state.save_comments =
509 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
510
93c80368
NB
511 if (_cpp_create_definition (pfile, node))
512 if (pfile->cb.define)
6cf87ca4 513 pfile->cb.define (pfile, pfile->directive_line, node);
93c80368 514 }
041c3194
ZW
515}
516
5d8ebbd8 517/* Handle #undef. Mark the identifier NT_VOID in the hash table. */
711b8824 518static void
6cf87ca4 519do_undef (cpp_reader *pfile)
041c3194 520{
df383483 521 cpp_hashnode *node = lex_macro_node (pfile);
9e62c811 522
041c3194
ZW
523 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
524 is not currently defined as a macro name. */
93c80368 525 if (node && node->type == NT_MACRO)
9e62c811 526 {
58fea6af 527 if (pfile->cb.undef)
6cf87ca4 528 pfile->cb.undef (pfile, pfile->directive_line, node);
9e62c811 529
618cdda7 530 if (node->flags & NODE_WARN)
ebef4e8c 531 cpp_error (pfile, DL_WARNING, "undefining \"%s\"", NODE_NAME (node));
9e62c811 532
a69cbaac
NB
533 if (CPP_OPTION (pfile, warn_unused_macros))
534 _cpp_warn_if_unused_macro (pfile, node, NULL);
535
041c3194 536 _cpp_free_definition (node);
9e62c811 537 }
93c80368 538 check_eol (pfile);
7f2935c7
PB
539}
540
d1bd0ded
GK
541/* Undefine a single macro/assertion/whatever. */
542
543static int
544undefine_macros (cpp_reader *pfile, cpp_hashnode *h,
545 void *data_p ATTRIBUTE_UNUSED)
546{
547 switch (h->type)
548 {
549 case NT_VOID:
550 break;
551
552 case NT_MACRO:
553 if (pfile->cb.undef)
554 (*pfile->cb.undef) (pfile, pfile->directive_line, h);
555
556 if (CPP_OPTION (pfile, warn_unused_macros))
557 _cpp_warn_if_unused_macro (pfile, h, NULL);
558
938d968e 559 /* And fall through.... */
d1bd0ded
GK
560 case NT_ASSERTION:
561 _cpp_free_definition (h);
562 break;
563
564 default:
565 abort ();
566 }
567 h->flags &= ~NODE_POISONED;
568 return 1;
569}
570
571/* Undefine all macros and assertions. */
572
573void
574cpp_undef_all (cpp_reader *pfile)
575{
576 cpp_forall_identifiers (pfile, undefine_macros, NULL);
577}
578
579
93c80368
NB
580/* Helper routine used by parse_include. Reinterpret the current line
581 as an h-char-sequence (< ... >); we are looking at the first token
74eb4b3e
NB
582 after the <. Returns a malloced filename. */
583static char *
6cf87ca4 584glue_header_name (cpp_reader *pfile)
93c80368 585{
4ed5bcfb 586 const cpp_token *token;
74eb4b3e 587 char *buffer;
2450e0b8 588 size_t len, total_len = 0, capacity = 1024;
93c80368
NB
589
590 /* To avoid lexed tokens overwriting our glued name, we can only
591 allocate from the string pool once we've lexed everything. */
74eb4b3e 592 buffer = xmalloc (capacity);
93c80368
NB
593 for (;;)
594 {
a8d0ddaf 595 token = get_token_no_padding (pfile);
93c80368 596
74eb4b3e 597 if (token->type == CPP_GREATER)
93c80368 598 break;
74eb4b3e
NB
599 if (token->type == CPP_EOF)
600 {
601 cpp_error (pfile, DL_ERROR, "missing terminating > character");
602 break;
603 }
93c80368 604
59325650 605 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
2450e0b8 606 if (total_len + len > capacity)
93c80368 607 {
2450e0b8 608 capacity = (capacity + len) * 2;
74eb4b3e 609 buffer = xrealloc (buffer, capacity);
93c80368
NB
610 }
611
4ed5bcfb 612 if (token->flags & PREV_WHITE)
2450e0b8 613 buffer[total_len++] = ' ';
93c80368 614
74eb4b3e
NB
615 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len])
616 - (uchar *) buffer);
93c80368 617 }
041c3194 618
74eb4b3e
NB
619 buffer[total_len] = '\0';
620 return buffer;
93c80368 621}
7f2935c7 622
74eb4b3e
NB
623/* Returns the file name of #include, #include_next, #import and
624 #pragma dependency. The string is malloced and the caller should
625 free it. Returns NULL on error. */
626static const char *
6cf87ca4 627parse_include (cpp_reader *pfile, int *pangle_brackets)
7f2935c7 628{
74eb4b3e 629 char *fname;
4ed5bcfb 630 const cpp_token *header;
7f2935c7 631
93c80368 632 /* Allow macro expansion. */
a8d0ddaf 633 header = get_token_no_padding (pfile);
74eb4b3e 634 if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
7f2935c7 635 {
6338b358
NB
636 fname = xmalloc (header->val.str.len - 1);
637 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
638 fname[header->val.str.len - 2] = '\0';
74eb4b3e 639 *pangle_brackets = header->type == CPP_HEADER_NAME;
7f2935c7 640 }
74eb4b3e 641 else if (header->type == CPP_LESS)
7f2935c7 642 {
74eb4b3e
NB
643 fname = glue_header_name (pfile);
644 *pangle_brackets = 1;
645 }
646 else
647 {
648 const unsigned char *dir;
649
650 if (pfile->directive == &dtable[T_PRAGMA])
651 dir = U"pragma dependency";
652 else
653 dir = pfile->directive->name;
654 cpp_error (pfile, DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
655 dir);
656
4ed5bcfb 657 return NULL;
7f2935c7 658 }
7f2935c7 659
3963c2e0 660 check_eol (pfile);
74eb4b3e 661 return fname;
168d3732 662}
3caee4a8 663
ba133c96 664/* Handle #include, #include_next and #import. */
711b8824 665static void
6cf87ca4 666do_include_common (cpp_reader *pfile, enum include_type type)
168d3732 667{
74eb4b3e
NB
668 const char *fname;
669 int angle_brackets;
670
671 fname = parse_include (pfile, &angle_brackets);
672 if (!fname)
3963c2e0 673 return;
7f2935c7 674
3963c2e0
ZW
675 /* Prevent #include recursion. */
676 if (pfile->line_maps.depth >= CPP_STACK_MAX)
74eb4b3e
NB
677 cpp_error (pfile, DL_ERROR, "#include nested too deeply");
678 else
09b82253 679 {
74eb4b3e
NB
680 /* Get out of macro context, if we are. */
681 skip_rest_of_line (pfile);
09b82253 682
74eb4b3e 683 if (pfile->cb.include)
6cf87ca4
ZW
684 pfile->cb.include (pfile, pfile->directive_line,
685 pfile->directive->name, fname, angle_brackets);
3963c2e0 686
8f9b4009 687 _cpp_stack_include (pfile, fname, angle_brackets, type);
74eb4b3e 688 }
3963c2e0 689
fad205ff 690 free ((void *) fname);
168d3732 691}
e8037d57 692
711b8824 693static void
6cf87ca4 694do_include (cpp_reader *pfile)
168d3732 695{
ba133c96
NB
696 do_include_common (pfile, IT_INCLUDE);
697}
168d3732 698
ba133c96 699static void
6cf87ca4 700do_import (cpp_reader *pfile)
ba133c96 701{
ba133c96 702 do_include_common (pfile, IT_IMPORT);
168d3732 703}
7f2935c7 704
711b8824 705static void
6cf87ca4 706do_include_next (cpp_reader *pfile)
168d3732 707{
3963c2e0
ZW
708 enum include_type type = IT_INCLUDE_NEXT;
709
710 /* If this is the primary source file, warn and use the normal
711 search logic. */
712 if (! pfile->buffer->prev)
713 {
714 cpp_error (pfile, DL_WARNING,
715 "#include_next in primary source file");
716 type = IT_INCLUDE;
717 }
718 do_include_common (pfile, type);
7f2935c7 719}
7f2935c7 720
dcc229e5
ZW
721/* Subroutine of do_linemarker. Read possible flags after file name.
722 LAST is the last flag seen; 0 if this is the first flag. Return the
723 flag if it is valid, 0 at the end of the directive. Otherwise
724 complain. */
642ce434 725static unsigned int
6cf87ca4 726read_flag (cpp_reader *pfile, unsigned int last)
d3a34a0a 727{
345894b4 728 const cpp_token *token = _cpp_lex_token (pfile);
d3a34a0a 729
345894b4 730 if (token->type == CPP_NUMBER && token->val.str.len == 1)
d3a34a0a 731 {
345894b4 732 unsigned int flag = token->val.str.text[0] - '0';
28e0f040
NB
733
734 if (flag > last && flag <= 4
735 && (flag != 4 || last == 3)
736 && (flag != 2 || last == 0))
737 return flag;
d3a34a0a 738 }
93c80368 739
345894b4 740 if (token->type != CPP_EOF)
ebef4e8c 741 cpp_error (pfile, DL_ERROR, "invalid flag \"%s\" in line directive",
345894b4 742 cpp_token_as_text (pfile, token));
93c80368 743 return 0;
d3a34a0a
JM
744}
745
dcc229e5
ZW
746/* Subroutine of do_line and do_linemarker. Convert a number in STR,
747 of length LEN, to binary; store it in NUMP, and return 0 if the
748 number was well-formed, 1 if not. Temporary, hopefully. */
041c3194 749static int
6cf87ca4 750strtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump)
041c3194
ZW
751{
752 unsigned long reg = 0;
562a5c27 753 uchar c;
041c3194
ZW
754 while (len--)
755 {
756 c = *str++;
757 if (!ISDIGIT (c))
758 return 1;
759 reg *= 10;
760 reg += c - '0';
761 }
762 *nump = reg;
763 return 0;
764}
765
6de1e2a9 766/* Interpret #line command.
dcc229e5
ZW
767 Note that the filename string (if any) is a true string constant
768 (escapes are interpreted), unlike in #line. */
711b8824 769static void
6cf87ca4 770do_line (cpp_reader *pfile)
7f2935c7 771{
4ed5bcfb 772 const cpp_token *token;
bb74c963
NB
773 const char *new_file = pfile->map->to_file;
774 unsigned long new_lineno;
93c80368 775
27e2564a 776 /* C99 raised the minimum limit on #line numbers. */
dcc229e5 777 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
18a9d8ff 778
93c80368 779 /* #line commands expand macros. */
4ed5bcfb
NB
780 token = cpp_get_token (pfile);
781 if (token->type != CPP_NUMBER
782 || strtoul_for_line (token->val.str.text, token->val.str.len,
783 &new_lineno))
7f2935c7 784 {
ebef4e8c
NB
785 cpp_error (pfile, DL_ERROR,
786 "\"%s\" after #line is not a positive integer",
4ed5bcfb 787 cpp_token_as_text (pfile, token));
9ec7291f 788 return;
df383483 789 }
7f2935c7 790
dcc229e5 791 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
ebef4e8c 792 cpp_error (pfile, DL_PEDWARN, "line number out of range");
7f2935c7 793
4ed5bcfb
NB
794 token = cpp_get_token (pfile);
795 if (token->type == CPP_STRING)
5538ada6 796 {
e6cc3a24 797 cpp_string s = { 0, 0 };
6b88314c 798 if (_cpp_interpret_string_notranslate (pfile, &token->val.str, &s))
e6cc3a24 799 new_file = (const char *)s.text;
dcc229e5
ZW
800 check_eol (pfile);
801 }
802 else if (token->type != CPP_EOF)
803 {
ebef4e8c 804 cpp_error (pfile, DL_ERROR, "\"%s\" is not a valid filename",
dcc229e5
ZW
805 cpp_token_as_text (pfile, token));
806 return;
807 }
808
809 skip_rest_of_line (pfile);
810 _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
811 pfile->map->sysp);
812}
813
814/* Interpret the # 44 "file" [flags] notation, which has slightly
815 different syntax and semantics from #line: Flags are allowed,
816 and we never complain about the line number being too big. */
817static void
6cf87ca4 818do_linemarker (cpp_reader *pfile)
dcc229e5
ZW
819{
820 const cpp_token *token;
821 const char *new_file = pfile->map->to_file;
822 unsigned long new_lineno;
823 unsigned int new_sysp = pfile->map->sysp;
824 enum lc_reason reason = LC_RENAME;
825 int flag;
826
827 /* Back up so we can get the number again. Putting this in
828 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
829 some circumstances, which can segfault. */
830 _cpp_backup_tokens (pfile, 1);
831
832 /* #line commands expand macros. */
833 token = cpp_get_token (pfile);
834 if (token->type != CPP_NUMBER
835 || strtoul_for_line (token->val.str.text, token->val.str.len,
836 &new_lineno))
837 {
ebef4e8c 838 cpp_error (pfile, DL_ERROR, "\"%s\" after # is not a positive integer",
dcc229e5
ZW
839 cpp_token_as_text (pfile, token));
840 return;
df383483 841 }
941e09b6 842
dcc229e5
ZW
843 token = cpp_get_token (pfile);
844 if (token->type == CPP_STRING)
845 {
e6cc3a24 846 cpp_string s = { 0, 0 };
6b88314c 847 if (_cpp_interpret_string_notranslate (pfile, &token->val.str, &s))
e6cc3a24
ZW
848 new_file = (const char *)s.text;
849
dcc229e5
ZW
850 new_sysp = 0;
851 flag = read_flag (pfile, 0);
852 if (flag == 1)
853 {
854 reason = LC_ENTER;
855 /* Fake an include for cpp_included (). */
856 _cpp_fake_include (pfile, new_file);
857 flag = read_flag (pfile, flag);
858 }
859 else if (flag == 2)
860 {
861 reason = LC_LEAVE;
862 flag = read_flag (pfile, flag);
863 }
864 if (flag == 3)
93c80368 865 {
dcc229e5
ZW
866 new_sysp = 1;
867 flag = read_flag (pfile, flag);
868 if (flag == 4)
869 new_sysp = 2;
93c80368 870 }
dcc229e5 871
fde84349 872 check_eol (pfile);
041c3194 873 }
4ed5bcfb 874 else if (token->type != CPP_EOF)
27e2564a 875 {
ebef4e8c 876 cpp_error (pfile, DL_ERROR, "\"%s\" is not a valid filename",
4ed5bcfb 877 cpp_token_as_text (pfile, token));
27e2564a
NB
878 return;
879 }
7f2935c7 880
bdcbe496 881 skip_rest_of_line (pfile);
bb74c963 882 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
27e2564a
NB
883}
884
67821e3a 885/* Arrange the file_change callback. pfile->line has changed to
47d89cf3 886 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
a1f300c0 887 header, 2 for a system header that needs to be extern "C" protected,
47d89cf3 888 and zero otherwise. */
eb1f4d9d 889void
6cf87ca4
ZW
890_cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
891 const char *to_file, unsigned int file_line,
892 unsigned int sysp)
27e2564a 893{
a2f7be91
ZW
894 pfile->map = linemap_add (&pfile->line_maps, reason, sysp,
895 pfile->line, to_file, file_line);
d82fc108 896
eb1f4d9d 897 if (pfile->cb.file_change)
6cf87ca4 898 pfile->cb.file_change (pfile, pfile->map);
7f2935c7 899}
941e09b6 900
5d8ebbd8
NB
901/* Report a warning or error detected by the program we are
902 processing. Use the directive's tokens in the error message. */
711b8824 903static void
6cf87ca4 904do_diagnostic (cpp_reader *pfile, int code, int print_dir)
7f2935c7 905{
ebef4e8c
NB
906 if (_cpp_begin_message (pfile, code,
907 pfile->cur_token[-1].line,
908 pfile->cur_token[-1].col))
58fea6af 909 {
29b10746
NB
910 if (print_dir)
911 fprintf (stderr, "#%s ", pfile->directive->name);
93c80368
NB
912 pfile->state.prevent_expansion++;
913 cpp_output_line (pfile, stderr);
914 pfile->state.prevent_expansion--;
58fea6af 915 }
7f2935c7
PB
916}
917
838f313b 918static void
6cf87ca4 919do_error (cpp_reader *pfile)
838f313b 920{
ebef4e8c 921 do_diagnostic (pfile, DL_ERROR, 1);
838f313b 922}
7f2935c7 923
711b8824 924static void
6cf87ca4 925do_warning (cpp_reader *pfile)
7f2935c7 926{
2f878973 927 /* We want #warning diagnostics to be emitted in system headers too. */
ebef4e8c 928 do_diagnostic (pfile, DL_WARNING_SYSHDR, 1);
7f2935c7
PB
929}
930
a2a76ce7 931/* Report program identification. */
711b8824 932static void
6cf87ca4 933do_ident (cpp_reader *pfile)
7f2935c7 934{
4ed5bcfb 935 const cpp_token *str = cpp_get_token (pfile);
58fea6af 936
4ed5bcfb 937 if (str->type != CPP_STRING)
ebef4e8c 938 cpp_error (pfile, DL_ERROR, "invalid #ident directive");
93c80368 939 else if (pfile->cb.ident)
6cf87ca4 940 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
a2a76ce7 941
93c80368 942 check_eol (pfile);
7f2935c7
PB
943}
944
a5da89c6
NB
945/* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
946 matching entry, or NULL if none is found. The returned entry could
947 be the start of a namespace chain, or a pragma. */
948static struct pragma_entry *
6cf87ca4 949lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
82443371 950{
4b115ff0
NB
951 while (chain && chain->pragma != pragma)
952 chain = chain->next;
a5da89c6
NB
953
954 return chain;
955}
956
957/* Create and insert a pragma entry for NAME at the beginning of a
958 singly-linked CHAIN. If handler is NULL, it is a namespace,
959 otherwise it is a pragma and its handler. */
960static struct pragma_entry *
6cf87ca4
ZW
961insert_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain,
962 const cpp_hashnode *pragma, pragma_cb handler)
82443371 963{
a5da89c6 964 struct pragma_entry *new;
58fea6af 965
bef985f3 966 new = (struct pragma_entry *)
8c3b2693 967 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
4b115ff0 968 new->pragma = pragma;
a5da89c6
NB
969 if (handler)
970 {
971 new->is_nspace = 0;
972 new->u.handler = handler;
973 }
974 else
975 {
976 new->is_nspace = 1;
977 new->u.space = NULL;
978 }
58fea6af 979
a5da89c6
NB
980 new->next = *chain;
981 *chain = new;
982 return new;
58fea6af 983}
82443371 984
a5da89c6
NB
985/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
986 goes in the global namespace. HANDLER is the handler it will call,
987 which must be non-NULL. */
58fea6af 988void
6cf87ca4
ZW
989cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
990 pragma_cb handler)
82443371 991{
a5da89c6
NB
992 struct pragma_entry **chain = &pfile->pragmas;
993 struct pragma_entry *entry;
4b115ff0 994 const cpp_hashnode *node;
a5da89c6
NB
995
996 if (!handler)
997 abort ();
58fea6af 998
a5da89c6 999 if (space)
58fea6af 1000 {
4b115ff0
NB
1001 node = cpp_lookup (pfile, U space, strlen (space));
1002 entry = lookup_pragma_entry (*chain, node);
a5da89c6 1003 if (!entry)
4b115ff0 1004 entry = insert_pragma_entry (pfile, chain, node, NULL);
a5da89c6
NB
1005 else if (!entry->is_nspace)
1006 goto clash;
1007 chain = &entry->u.space;
58fea6af
ZW
1008 }
1009
a5da89c6 1010 /* Check for duplicates. */
4b115ff0
NB
1011 node = cpp_lookup (pfile, U name, strlen (name));
1012 entry = lookup_pragma_entry (*chain, node);
a5da89c6
NB
1013 if (entry)
1014 {
1015 if (entry->is_nspace)
1016 clash:
ebef4e8c 1017 cpp_error (pfile, DL_ICE,
a5da89c6 1018 "registering \"%s\" as both a pragma and a pragma namespace",
4b115ff0 1019 NODE_NAME (node));
a5da89c6 1020 else if (space)
ebef4e8c
NB
1021 cpp_error (pfile, DL_ICE, "#pragma %s %s is already registered",
1022 space, name);
a5da89c6 1023 else
ebef4e8c 1024 cpp_error (pfile, DL_ICE, "#pragma %s is already registered", name);
a5da89c6
NB
1025 }
1026 else
4b115ff0 1027 insert_pragma_entry (pfile, chain, node, handler);
58fea6af 1028}
a5da89c6
NB
1029
1030/* Register the pragmas the preprocessor itself handles. */
58fea6af 1031void
6cf87ca4 1032_cpp_init_internal_pragmas (cpp_reader *pfile)
58fea6af 1033{
a5da89c6 1034 /* Pragmas in the global namespace. */
58fea6af
ZW
1035 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
1036
a5da89c6 1037 /* New GCC-specific pragmas should be put in the GCC namespace. */
58fea6af
ZW
1038 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
1039 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
1040 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
82443371 1041}
7f2935c7 1042
17211ab5
GK
1043/* Return the number of registered pragmas in PE. */
1044
1045static int
6cf87ca4 1046count_registered_pragmas (struct pragma_entry *pe)
17211ab5
GK
1047{
1048 int ct = 0;
1049 for (; pe != NULL; pe = pe->next)
1050 {
1051 if (pe->is_nspace)
1052 ct += count_registered_pragmas (pe->u.space);
1053 ct++;
1054 }
1055 return ct;
1056}
1057
1058/* Save into SD the names of the registered pragmas referenced by PE,
1059 and return a pointer to the next free space in SD. */
1060
1061static char **
6cf87ca4 1062save_registered_pragmas (struct pragma_entry *pe, char **sd)
17211ab5
GK
1063{
1064 for (; pe != NULL; pe = pe->next)
1065 {
1066 if (pe->is_nspace)
1067 sd = save_registered_pragmas (pe->u.space, sd);
1068 *sd++ = xmemdup (HT_STR (&pe->pragma->ident),
1069 HT_LEN (&pe->pragma->ident),
1070 HT_LEN (&pe->pragma->ident) + 1);
1071 }
1072 return sd;
1073}
1074
1075/* Return a newly-allocated array which saves the names of the
1076 registered pragmas. */
1077
1078char **
6cf87ca4 1079_cpp_save_pragma_names (cpp_reader *pfile)
17211ab5
GK
1080{
1081 int ct = count_registered_pragmas (pfile->pragmas);
1082 char **result = xnewvec (char *, ct);
1083 (void) save_registered_pragmas (pfile->pragmas, result);
1084 return result;
1085}
1086
1087/* Restore from SD the names of the registered pragmas referenced by PE,
1088 and return a pointer to the next unused name in SD. */
1089
1090static char **
6cf87ca4
ZW
1091restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1092 char **sd)
17211ab5
GK
1093{
1094 for (; pe != NULL; pe = pe->next)
1095 {
1096 if (pe->is_nspace)
1097 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1098 pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd));
1099 free (*sd);
1100 sd++;
1101 }
1102 return sd;
1103}
1104
1105/* Restore the names of the registered pragmas from SAVED. */
1106
1107void
6cf87ca4 1108_cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
17211ab5
GK
1109{
1110 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1111 free (saved);
1112}
1113
a5da89c6
NB
1114/* Pragmata handling. We handle some, and pass the rest on to the
1115 front end. C99 defines three pragmas and says that no macro
1116 expansion is to be performed on them; whether or not macro
1117 expansion happens for other pragmas is implementation defined.
1118 This implementation never macro-expands the text after #pragma. */
711b8824 1119static void
6cf87ca4 1120do_pragma (cpp_reader *pfile)
7f2935c7 1121{
a5da89c6 1122 const struct pragma_entry *p = NULL;
e2e1fa50 1123 const cpp_token *token, *pragma_token = pfile->cur_token;
a5da89c6 1124 unsigned int count = 1;
add7091b 1125
93c80368 1126 pfile->state.prevent_expansion++;
58fea6af 1127
4ed5bcfb
NB
1128 token = cpp_get_token (pfile);
1129 if (token->type == CPP_NAME)
0172e2bc 1130 {
4b115ff0 1131 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
a5da89c6 1132 if (p && p->is_nspace)
58fea6af 1133 {
a5da89c6
NB
1134 count = 2;
1135 token = cpp_get_token (pfile);
1136 if (token->type == CPP_NAME)
4b115ff0 1137 p = lookup_pragma_entry (p->u.space, token->val.node);
a5da89c6
NB
1138 else
1139 p = NULL;
58fea6af 1140 }
58fea6af 1141 }
041c3194 1142
a5da89c6 1143 if (p)
e2e1fa50
AO
1144 {
1145 /* Since the handler below doesn't get the line number, that it
1146 might need for diagnostics, make sure it has the right
1147 numbers in place. */
1148 if (pfile->cb.line_change)
1149 (*pfile->cb.line_change) (pfile, pragma_token, false);
1150 (*p->u.handler) (pfile);
1151 if (pfile->cb.line_change)
1152 (*pfile->cb.line_change) (pfile, pfile->cur_token, false);
1153
1154 }
d82fc108 1155 else if (pfile->cb.def_pragma)
bdcbe496
NB
1156 {
1157 _cpp_backup_tokens (pfile, count);
6cf87ca4 1158 pfile->cb.def_pragma (pfile, pfile->directive_line);
bdcbe496 1159 }
a5da89c6 1160
97293897 1161 pfile->state.prevent_expansion--;
82443371
NS
1162}
1163
5d8ebbd8 1164/* Handle #pragma once. */
58fea6af 1165static void
6cf87ca4 1166do_pragma_once (cpp_reader *pfile)
a2a76ce7 1167{
642ce434 1168 if (pfile->buffer->prev == NULL)
ebef4e8c 1169 cpp_error (pfile, DL_WARNING, "#pragma once in main file");
93c80368
NB
1170
1171 check_eol (pfile);
49634b3a 1172 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
a2a76ce7 1173}
fc009f96 1174
c3bf3e6e
NB
1175/* Handle #pragma GCC poison, to poison one or more identifiers so
1176 that the lexer produces a hard error for each subsequent usage. */
58fea6af 1177static void
6cf87ca4 1178do_pragma_poison (cpp_reader *pfile)
a2a76ce7 1179{
345894b4 1180 const cpp_token *tok;
f8f769ea 1181 cpp_hashnode *hp;
a2a76ce7 1182
93c80368 1183 pfile->state.poisoned_ok = 1;
a2a76ce7
ZW
1184 for (;;)
1185 {
345894b4
NB
1186 tok = _cpp_lex_token (pfile);
1187 if (tok->type == CPP_EOF)
a2a76ce7 1188 break;
345894b4 1189 if (tok->type != CPP_NAME)
fc009f96 1190 {
ebef4e8c 1191 cpp_error (pfile, DL_ERROR, "invalid #pragma GCC poison directive");
93c80368 1192 break;
fc009f96
GK
1193 }
1194
345894b4 1195 hp = tok->val.node;
93c80368
NB
1196 if (hp->flags & NODE_POISONED)
1197 continue;
1198
1199 if (hp->type == NT_MACRO)
ebef4e8c
NB
1200 cpp_error (pfile, DL_WARNING, "poisoning existing macro \"%s\"",
1201 NODE_NAME (hp));
93c80368
NB
1202 _cpp_free_definition (hp);
1203 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
fc009f96 1204 }
93c80368 1205 pfile->state.poisoned_ok = 0;
7f2935c7 1206}
2c0b35cb
ZW
1207
1208/* Mark the current header as a system header. This will suppress
1209 some categories of warnings (notably those from -pedantic). It is
1210 intended for use in system libraries that cannot be implemented in
1211 conforming C, but cannot be certain that their headers appear in a
1212 system include directory. To prevent abuse, it is rejected in the
1213 primary source file. */
58fea6af 1214static void
6cf87ca4 1215do_pragma_system_header (cpp_reader *pfile)
2c0b35cb 1216{
614c7d37
NB
1217 cpp_buffer *buffer = pfile->buffer;
1218
1219 if (buffer->prev == 0)
ebef4e8c
NB
1220 cpp_error (pfile, DL_WARNING,
1221 "#pragma system_header ignored outside include file");
2c0b35cb 1222 else
d82fc108
NB
1223 {
1224 check_eol (pfile);
bdcbe496 1225 skip_rest_of_line (pfile);
d82fc108
NB
1226 cpp_make_system_header (pfile, 1, 0);
1227 }
2c0b35cb 1228}
f3f751ad
NS
1229
1230/* Check the modified date of the current include file against a specified
1231 file. Issue a diagnostic, if the specified file is newer. We use this to
1232 determine if a fixed header should be refixed. */
58fea6af 1233static void
6cf87ca4 1234do_pragma_dependency (cpp_reader *pfile)
f3f751ad 1235{
74eb4b3e
NB
1236 const char *fname;
1237 int angle_brackets, ordering;
df383483 1238
74eb4b3e
NB
1239 fname = parse_include (pfile, &angle_brackets);
1240 if (!fname)
58fea6af 1241 return;
041c3194 1242
74eb4b3e 1243 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
f3f751ad 1244 if (ordering < 0)
74eb4b3e 1245 cpp_error (pfile, DL_WARNING, "cannot find source file %s", fname);
f3f751ad
NS
1246 else if (ordering > 0)
1247 {
74eb4b3e 1248 cpp_error (pfile, DL_WARNING, "current file is older than %s", fname);
4ed5bcfb 1249 if (cpp_get_token (pfile)->type != CPP_EOF)
bdcbe496
NB
1250 {
1251 _cpp_backup_tokens (pfile, 1);
ebef4e8c 1252 do_diagnostic (pfile, DL_WARNING, 0);
bdcbe496 1253 }
f3f751ad 1254 }
74eb4b3e 1255
fad205ff 1256 free ((void *) fname);
f3f751ad
NS
1257}
1258
4ed5bcfb
NB
1259/* Get a token but skip padding. */
1260static const cpp_token *
6cf87ca4 1261get_token_no_padding (cpp_reader *pfile)
a5c3cccd 1262{
4ed5bcfb
NB
1263 for (;;)
1264 {
1265 const cpp_token *result = cpp_get_token (pfile);
1266 if (result->type != CPP_PADDING)
1267 return result;
1268 }
1269}
a5c3cccd 1270
4ed5bcfb
NB
1271/* Check syntax is "(string-literal)". Returns the string on success,
1272 or NULL on failure. */
1273static const cpp_token *
6cf87ca4 1274get__Pragma_string (cpp_reader *pfile)
4ed5bcfb
NB
1275{
1276 const cpp_token *string;
a5c3cccd 1277
4ed5bcfb
NB
1278 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1279 return NULL;
1280
1281 string = get_token_no_padding (pfile);
a5c3cccd 1282 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
4ed5bcfb
NB
1283 return NULL;
1284
1285 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1286 return NULL;
a5c3cccd 1287
4ed5bcfb 1288 return string;
a5c3cccd
NB
1289}
1290
87062813
NB
1291/* Destringize IN into a temporary buffer, by removing the first \ of
1292 \" and \\ sequences, and process the result as a #pragma directive. */
1293static void
6cf87ca4 1294destringize_and_run (cpp_reader *pfile, const cpp_string *in)
a5c3cccd
NB
1295{
1296 const unsigned char *src, *limit;
87062813 1297 char *dest, *result;
a5c3cccd 1298
6338b358
NB
1299 dest = result = alloca (in->len - 1);
1300 src = in->text + 1 + (in->text[0] == 'L');
1301 limit = in->text + in->len - 1;
1302 while (src < limit)
a5c3cccd
NB
1303 {
1304 /* We know there is a character following the backslash. */
1305 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1306 src++;
1307 *dest++ = *src++;
1308 }
26aea073 1309 *dest = '\n';
a5c3cccd 1310
8128cccf
NB
1311 /* Ugh; an awful kludge. We are really not set up to be lexing
1312 tokens when in the middle of a macro expansion. Use a new
1313 context to force cpp_get_token to lex, and so skip_rest_of_line
1314 doesn't go beyond the end of the text. Also, remember the
1315 current lexing position so we can return to it later.
1316
1317 Something like line-at-a-time lexing should remove the need for
1318 this. */
1319 {
1320 cpp_context *saved_context = pfile->context;
1321 cpp_token *saved_cur_token = pfile->cur_token;
1322 tokenrun *saved_cur_run = pfile->cur_run;
1323
1324 pfile->context = xnew (cpp_context);
1325 pfile->context->macro = 0;
1326 pfile->context->prev = 0;
1327 run_directive (pfile, T_PRAGMA, result, dest - result);
1328 free (pfile->context);
1329 pfile->context = saved_context;
1330 pfile->cur_token = saved_cur_token;
1331 pfile->cur_run = saved_cur_run;
1332 pfile->line--;
1333 }
79ba5e3b
NB
1334
1335 /* See above comment. For the moment, we'd like
1336
1337 token1 _Pragma ("foo") token2
1338
1339 to be output as
1340
1341 token1
1342 # 7 "file.c"
1343 #pragma foo
1344 # 7 "file.c"
1345 token2
1346
1347 Getting the line markers is a little tricky. */
1348 if (pfile->cb.line_change)
6cf87ca4 1349 pfile->cb.line_change (pfile, pfile->cur_token, false);
a5c3cccd
NB
1350}
1351
87062813 1352/* Handle the _Pragma operator. */
a5c3cccd 1353void
6cf87ca4 1354_cpp_do__Pragma (cpp_reader *pfile)
a5c3cccd 1355{
4ed5bcfb 1356 const cpp_token *string = get__Pragma_string (pfile);
a5c3cccd 1357
79ba5e3b
NB
1358 if (string)
1359 destringize_and_run (pfile, &string->val.str);
1360 else
ebef4e8c
NB
1361 cpp_error (pfile, DL_ERROR,
1362 "_Pragma takes a parenthesized string literal");
a5c3cccd
NB
1363}
1364
6cf87ca4 1365/* Ignore #sccs on all systems. */
711b8824 1366static void
6cf87ca4 1367do_sccs (cpp_reader *pfile ATTRIBUTE_UNUSED)
7f2935c7 1368{
7f2935c7 1369}
1d0e51ba 1370
5d8ebbd8 1371/* Handle #ifdef. */
711b8824 1372static void
6cf87ca4 1373do_ifdef (cpp_reader *pfile)
168d3732 1374{
93c80368 1375 int skip = 1;
041c3194 1376
cef0d199 1377 if (! pfile->state.skipping)
93c80368
NB
1378 {
1379 const cpp_hashnode *node = lex_macro_node (pfile);
041c3194 1380
93c80368 1381 if (node)
a69cbaac
NB
1382 {
1383 skip = node->type != NT_MACRO;
1384 _cpp_mark_macro_used (node);
1385 check_eol (pfile);
1386 }
93c80368 1387 }
168d3732 1388
93c80368
NB
1389 push_conditional (pfile, skip, T_IFDEF, 0);
1390}
168d3732 1391
5d8ebbd8 1392/* Handle #ifndef. */
711b8824 1393static void
6cf87ca4 1394do_ifndef (cpp_reader *pfile)
168d3732 1395{
93c80368 1396 int skip = 1;
bfb9dc7f 1397 const cpp_hashnode *node = 0;
168d3732 1398
cef0d199 1399 if (! pfile->state.skipping)
5af7e2c2 1400 {
93c80368 1401 node = lex_macro_node (pfile);
b43db0b3
GK
1402
1403 if (node)
a69cbaac
NB
1404 {
1405 skip = node->type == NT_MACRO;
1406 _cpp_mark_macro_used (node);
1407 check_eol (pfile);
1408 }
5af7e2c2 1409 }
041c3194 1410
93c80368 1411 push_conditional (pfile, skip, T_IFNDEF, node);
7f2935c7
PB
1412}
1413
6d18adbc
NB
1414/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1415 pfile->mi_ind_cmacro so we can handle multiple-include
6356f892 1416 optimizations. If macro expansion occurs in the expression, we
6d18adbc
NB
1417 cannot treat it as a controlling conditional, since the expansion
1418 could change in the future. That is handled by cpp_get_token. */
711b8824 1419static void
6cf87ca4 1420do_if (cpp_reader *pfile)
7f2935c7 1421{
93c80368 1422 int skip = 1;
7f2935c7 1423
cef0d199 1424 if (! pfile->state.skipping)
87ed109f 1425 skip = _cpp_parse_expr (pfile) == false;
93c80368 1426
6d18adbc 1427 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
7f2935c7
PB
1428}
1429
b528a07e 1430/* Flip skipping state if appropriate and continue without changing
ea4a453b
ZW
1431 if_stack; this is so that the error message for missing #endif's
1432 etc. will point to the original #if. */
711b8824 1433static void
6cf87ca4 1434do_else (cpp_reader *pfile)
ed705a82 1435{
b528a07e
NB
1436 cpp_buffer *buffer = pfile->buffer;
1437 struct if_stack *ifs = buffer->if_stack;
ff2b53ef 1438
ea4a453b 1439 if (ifs == NULL)
ebef4e8c 1440 cpp_error (pfile, DL_ERROR, "#else without #if");
93c80368 1441 else
ff2b53ef 1442 {
93c80368
NB
1443 if (ifs->type == T_ELSE)
1444 {
ebef4e8c
NB
1445 cpp_error (pfile, DL_ERROR, "#else after #else");
1446 cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0,
93c80368
NB
1447 "the conditional began here");
1448 }
b528a07e
NB
1449 ifs->type = T_ELSE;
1450
cef0d199
NB
1451 /* Skip any future (erroneous) #elses or #elifs. */
1452 pfile->state.skipping = ifs->skip_elses;
1453 ifs->skip_elses = true;
7f2935c7 1454
93c80368
NB
1455 /* Invalidate any controlling macro. */
1456 ifs->mi_cmacro = 0;
93c80368 1457
cef0d199 1458 /* Only check EOL if was not originally skipping. */
909de5da 1459 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
cef0d199
NB
1460 check_eol (pfile);
1461 }
7f2935c7
PB
1462}
1463
5d8ebbd8 1464/* Handle a #elif directive by not changing if_stack either. See the
93c80368 1465 comment above do_else. */
711b8824 1466static void
6cf87ca4 1467do_elif (cpp_reader *pfile)
7f2935c7 1468{
b528a07e
NB
1469 cpp_buffer *buffer = pfile->buffer;
1470 struct if_stack *ifs = buffer->if_stack;
7f2935c7 1471
ea4a453b 1472 if (ifs == NULL)
ebef4e8c 1473 cpp_error (pfile, DL_ERROR, "#elif without #if");
b528a07e 1474 else
40ea76de 1475 {
b528a07e
NB
1476 if (ifs->type == T_ELSE)
1477 {
ebef4e8c
NB
1478 cpp_error (pfile, DL_ERROR, "#elif after #else");
1479 cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0,
b528a07e
NB
1480 "the conditional began here");
1481 }
1482 ifs->type = T_ELIF;
93c80368 1483
cef0d199
NB
1484 /* Only evaluate this if we aren't skipping elses. During
1485 evaluation, set skipping to false to get lexer warnings. */
1486 if (ifs->skip_elses)
1487 pfile->state.skipping = 1;
1488 else
b528a07e 1489 {
cef0d199
NB
1490 pfile->state.skipping = 0;
1491 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1492 ifs->skip_elses = ! pfile->state.skipping;
b528a07e 1493 }
cef0d199
NB
1494
1495 /* Invalidate any controlling macro. */
1496 ifs->mi_cmacro = 0;
40ea76de 1497 }
7f2935c7
PB
1498}
1499
cef0d199 1500/* #endif pops the if stack and resets pfile->state.skipping. */
711b8824 1501static void
6cf87ca4 1502do_endif (cpp_reader *pfile)
7f2935c7 1503{
b528a07e
NB
1504 cpp_buffer *buffer = pfile->buffer;
1505 struct if_stack *ifs = buffer->if_stack;
ea4a453b 1506
ea4a453b 1507 if (ifs == NULL)
ebef4e8c 1508 cpp_error (pfile, DL_ERROR, "#endif without #if");
7f2935c7
PB
1509 else
1510 {
cef0d199 1511 /* Only check EOL if was not originally skipping. */
909de5da 1512 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
cef0d199
NB
1513 check_eol (pfile);
1514
93c80368
NB
1515 /* If potential control macro, we go back outside again. */
1516 if (ifs->next == 0 && ifs->mi_cmacro)
1517 {
6d18adbc 1518 pfile->mi_valid = true;
93c80368
NB
1519 pfile->mi_cmacro = ifs->mi_cmacro;
1520 }
1521
b528a07e 1522 buffer->if_stack = ifs->next;
cef0d199 1523 pfile->state.skipping = ifs->was_skipping;
2a967f3d 1524 obstack_free (&pfile->buffer_ob, ifs);
7f2935c7 1525 }
93c80368 1526}
041c3194 1527
5d8ebbd8
NB
1528/* Push an if_stack entry for a preprocessor conditional, and set
1529 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1530 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1531 we need to check here that we are at the top of the file. */
ea4a453b 1532static void
6cf87ca4
ZW
1533push_conditional (cpp_reader *pfile, int skip, int type,
1534 const cpp_hashnode *cmacro)
ea4a453b
ZW
1535{
1536 struct if_stack *ifs;
b528a07e 1537 cpp_buffer *buffer = pfile->buffer;
ea4a453b 1538
2a967f3d 1539 ifs = xobnew (&pfile->buffer_ob, struct if_stack);
50410426 1540 ifs->line = pfile->directive_line;
b528a07e 1541 ifs->next = buffer->if_stack;
cef0d199
NB
1542 ifs->skip_elses = pfile->state.skipping || !skip;
1543 ifs->was_skipping = pfile->state.skipping;
ea4a453b 1544 ifs->type = type;
6d18adbc
NB
1545 /* This condition is effectively a test for top-of-file. */
1546 if (pfile->mi_valid && pfile->mi_cmacro == 0)
93c80368
NB
1547 ifs->mi_cmacro = cmacro;
1548 else
1549 ifs->mi_cmacro = 0;
ea4a453b 1550
cef0d199 1551 pfile->state.skipping = skip;
b528a07e 1552 buffer->if_stack = ifs;
782331f4 1553}
7061aa5a 1554
5d8ebbd8
NB
1555/* Read the tokens of the answer into the macro pool, in a directive
1556 of type TYPE. Only commit the memory if we intend it as permanent
1557 storage, i.e. the #assert case. Returns 0 on success, and sets
1558 ANSWERP to point to the answer. */
93c80368 1559static int
6cf87ca4 1560parse_answer (cpp_reader *pfile, struct answer **answerp, int type)
7f2935c7 1561{
4ed5bcfb 1562 const cpp_token *paren;
93c80368 1563 struct answer *answer;
8c3b2693 1564 unsigned int acount;
93c80368
NB
1565
1566 /* In a conditional, it is legal to not have an open paren. We
1567 should save the following token in this case. */
4ed5bcfb 1568 paren = cpp_get_token (pfile);
93c80368
NB
1569
1570 /* If not a paren, see if we're OK. */
4ed5bcfb 1571 if (paren->type != CPP_OPEN_PAREN)
041c3194 1572 {
93c80368
NB
1573 /* In a conditional no answer is a test for any answer. It
1574 could be followed by any token. */
1575 if (type == T_IF)
bdcbe496
NB
1576 {
1577 _cpp_backup_tokens (pfile, 1);
1578 return 0;
1579 }
93c80368
NB
1580
1581 /* #unassert with no answer is valid - it removes all answers. */
4ed5bcfb 1582 if (type == T_UNASSERT && paren->type == CPP_EOF)
93c80368 1583 return 0;
15dad1d9 1584
ebef4e8c 1585 cpp_error (pfile, DL_ERROR, "missing '(' after predicate");
93c80368 1586 return 1;
041c3194 1587 }
7061aa5a 1588
8c3b2693 1589 for (acount = 0;; acount++)
041c3194 1590 {
8c3b2693
NB
1591 size_t room_needed;
1592 const cpp_token *token = cpp_get_token (pfile);
1593 cpp_token *dest;
93c80368 1594
041c3194
ZW
1595 if (token->type == CPP_CLOSE_PAREN)
1596 break;
a7abcbbf 1597
93c80368 1598 if (token->type == CPP_EOF)
041c3194 1599 {
ebef4e8c 1600 cpp_error (pfile, DL_ERROR, "missing ')' to complete answer");
93c80368 1601 return 1;
041c3194 1602 }
8c3b2693
NB
1603
1604 /* struct answer includes the space for one token. */
1605 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1606
1607 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1608 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1609
1610 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1611 *dest = *token;
1612
1613 /* Drop whitespace at start, for answer equivalence purposes. */
1614 if (acount == 0)
1615 dest->flags &= ~PREV_WHITE;
041c3194 1616 }
15dad1d9 1617
8c3b2693 1618 if (acount == 0)
7061aa5a 1619 {
ebef4e8c 1620 cpp_error (pfile, DL_ERROR, "predicate's answer is empty");
93c80368 1621 return 1;
7f2935c7 1622 }
041c3194 1623
8c3b2693
NB
1624 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1625 answer->count = acount;
1626 answer->next = NULL;
93c80368 1627 *answerp = answer;
041c3194 1628
93c80368
NB
1629 return 0;
1630}
041c3194 1631
5d8ebbd8
NB
1632/* Parses an assertion directive of type TYPE, returning a pointer to
1633 the hash node of the predicate, or 0 on error. If an answer was
1634 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
93c80368 1635static cpp_hashnode *
6cf87ca4 1636parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
93c80368
NB
1637{
1638 cpp_hashnode *result = 0;
4ed5bcfb 1639 const cpp_token *predicate;
93c80368
NB
1640
1641 /* We don't expand predicates or answers. */
1642 pfile->state.prevent_expansion++;
1643
93c80368 1644 *answerp = 0;
4ed5bcfb
NB
1645 predicate = cpp_get_token (pfile);
1646 if (predicate->type == CPP_EOF)
ebef4e8c 1647 cpp_error (pfile, DL_ERROR, "assertion without predicate");
4ed5bcfb 1648 else if (predicate->type != CPP_NAME)
ebef4e8c 1649 cpp_error (pfile, DL_ERROR, "predicate must be an identifier");
93c80368
NB
1650 else if (parse_answer (pfile, answerp, type) == 0)
1651 {
4ed5bcfb 1652 unsigned int len = NODE_LEN (predicate->val.node);
93c80368 1653 unsigned char *sym = alloca (len + 1);
041c3194 1654
93c80368
NB
1655 /* Prefix '#' to get it out of macro namespace. */
1656 sym[0] = '#';
4ed5bcfb 1657 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
93c80368
NB
1658 result = cpp_lookup (pfile, sym, len + 1);
1659 }
7061aa5a 1660
93c80368
NB
1661 pfile->state.prevent_expansion--;
1662 return result;
7f2935c7 1663}
7061aa5a 1664
5d8ebbd8 1665/* Returns a pointer to the pointer to CANDIDATE in the answer chain,
041c3194 1666 or a pointer to NULL if the answer is not in the chain. */
93c80368 1667static struct answer **
6cf87ca4 1668find_answer (cpp_hashnode *node, const struct answer *candidate)
7f2935c7 1669{
93c80368 1670 unsigned int i;
041c3194 1671 struct answer **result;
7f2935c7 1672
041c3194 1673 for (result = &node->value.answers; *result; result = &(*result)->next)
93c80368
NB
1674 {
1675 struct answer *answer = *result;
1676
1677 if (answer->count == candidate->count)
1678 {
1679 for (i = 0; i < answer->count; i++)
1680 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1681 break;
1682
1683 if (i == answer->count)
1684 break;
1685 }
1686 }
ff2b53ef 1687
041c3194
ZW
1688 return result;
1689}
15dad1d9 1690
93c80368 1691/* Test an assertion within a preprocessor conditional. Returns
da7d8304 1692 nonzero on failure, zero on success. On success, the result of
2402645b 1693 the test is written into VALUE, otherwise the value 0. */
93c80368 1694int
6cf87ca4 1695_cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
93c80368
NB
1696{
1697 struct answer *answer;
1698 cpp_hashnode *node;
1699
1700 node = parse_assertion (pfile, &answer, T_IF);
2402645b
HPN
1701
1702 /* For recovery, an erroneous assertion expression is handled as a
1703 failing assertion. */
1704 *value = 0;
1705
93c80368
NB
1706 if (node)
1707 *value = (node->type == NT_ASSERTION &&
1708 (answer == 0 || *find_answer (node, answer) != 0));
91318908
NB
1709 else if (pfile->cur_token[-1].type == CPP_EOF)
1710 _cpp_backup_tokens (pfile, 1);
93c80368
NB
1711
1712 /* We don't commit the memory for the answer - it's temporary only. */
1713 return node == 0;
1714}
1715
5d8ebbd8 1716/* Handle #assert. */
711b8824 1717static void
6cf87ca4 1718do_assert (cpp_reader *pfile)
041c3194
ZW
1719{
1720 struct answer *new_answer;
1721 cpp_hashnode *node;
df383483 1722
93c80368 1723 node = parse_assertion (pfile, &new_answer, T_ASSERT);
041c3194 1724 if (node)
ff2b53ef 1725 {
93c80368
NB
1726 /* Place the new answer in the answer list. First check there
1727 is not a duplicate. */
041c3194 1728 new_answer->next = 0;
93c80368 1729 if (node->type == NT_ASSERTION)
041c3194 1730 {
93c80368
NB
1731 if (*find_answer (node, new_answer))
1732 {
ebef4e8c
NB
1733 cpp_error (pfile, DL_WARNING, "\"%s\" re-asserted",
1734 NODE_NAME (node) + 1);
93c80368
NB
1735 return;
1736 }
041c3194
ZW
1737 new_answer->next = node->value.answers;
1738 }
8c3b2693 1739
93c80368 1740 node->type = NT_ASSERTION;
041c3194 1741 node->value.answers = new_answer;
8c3b2693
NB
1742 BUFF_FRONT (pfile->a_buff) += (sizeof (struct answer)
1743 + (new_answer->count - 1)
1744 * sizeof (cpp_token));
1745 check_eol (pfile);
ff2b53ef 1746 }
041c3194 1747}
15dad1d9 1748
5d8ebbd8 1749/* Handle #unassert. */
711b8824 1750static void
6cf87ca4 1751do_unassert (cpp_reader *pfile)
041c3194
ZW
1752{
1753 cpp_hashnode *node;
93c80368 1754 struct answer *answer;
df383483 1755
93c80368
NB
1756 node = parse_assertion (pfile, &answer, T_UNASSERT);
1757 /* It isn't an error to #unassert something that isn't asserted. */
1758 if (node && node->type == NT_ASSERTION)
7061aa5a 1759 {
93c80368 1760 if (answer)
15dad1d9 1761 {
93c80368 1762 struct answer **p = find_answer (node, answer), *temp;
041c3194 1763
93c80368
NB
1764 /* Remove the answer from the list. */
1765 temp = *p;
1766 if (temp)
1767 *p = temp->next;
a7abcbbf 1768
93c80368
NB
1769 /* Did we free the last answer? */
1770 if (node->value.answers == 0)
1771 node->type = NT_VOID;
8c3b2693
NB
1772
1773 check_eol (pfile);
93c80368
NB
1774 }
1775 else
1776 _cpp_free_definition (node);
041c3194 1777 }
93c80368
NB
1778
1779 /* We don't commit the memory for the answer - it's temporary only. */
7f2935c7 1780}
7f2935c7 1781
45b966db
ZW
1782/* These are for -D, -U, -A. */
1783
1784/* Process the string STR as if it appeared as the body of a #define.
1785 If STR is just an identifier, define it with value 1.
1786 If STR has anything after the identifier, then it should
ec5c56db 1787 be identifier=definition. */
0b22d65c 1788void
6cf87ca4 1789cpp_define (cpp_reader *pfile, const char *str)
0b22d65c 1790{
45b966db
ZW
1791 char *buf, *p;
1792 size_t count;
1793
df383483 1794 /* Copy the entire option so we can modify it.
45b966db 1795 Change the first "=" in the string to a space. If there is none,
86368122
NB
1796 tack " 1" on the end. */
1797
86368122 1798 count = strlen (str);
703ad42b 1799 buf = alloca (count + 3);
86368122
NB
1800 memcpy (buf, str, count);
1801
1802 p = strchr (str, '=');
45b966db 1803 if (p)
86368122 1804 buf[p - str] = ' ';
45b966db
ZW
1805 else
1806 {
86368122
NB
1807 buf[count++] = ' ';
1808 buf[count++] = '1';
0b22d65c 1809 }
26aea073 1810 buf[count] = '\n';
cf4ed945 1811
29401c30 1812 run_directive (pfile, T_DEFINE, buf, count);
2c8f0515
ZW
1813}
1814
ad2a084d 1815/* Slight variant of the above for use by initialize_builtins. */
2c8f0515 1816void
6cf87ca4 1817_cpp_define_builtin (cpp_reader *pfile, const char *str)
2c8f0515 1818{
26aea073
NB
1819 size_t len = strlen (str);
1820 char *buf = alloca (len + 1);
1821 memcpy (buf, str, len);
1822 buf[len] = '\n';
1823 run_directive (pfile, T_DEFINE, buf, len);
45b966db 1824}
0f41302f 1825
45b966db
ZW
1826/* Process MACRO as if it appeared as the body of an #undef. */
1827void
6cf87ca4 1828cpp_undef (cpp_reader *pfile, const char *macro)
7f2935c7 1829{
26aea073
NB
1830 size_t len = strlen (macro);
1831 char *buf = alloca (len + 1);
1832 memcpy (buf, macro, len);
1833 buf[len] = '\n';
1834 run_directive (pfile, T_UNDEF, buf, len);
7f2935c7
PB
1835}
1836
ec5c56db 1837/* Process the string STR as if it appeared as the body of a #assert. */
45b966db 1838void
6cf87ca4 1839cpp_assert (cpp_reader *pfile, const char *str)
45b966db 1840{
86368122 1841 handle_assertion (pfile, str, T_ASSERT);
45b966db 1842}
7f2935c7 1843
ec5c56db 1844/* Process STR as if it appeared as the body of an #unassert. */
45b966db 1845void
6cf87ca4 1846cpp_unassert (cpp_reader *pfile, const char *str)
7f2935c7 1847{
86368122 1848 handle_assertion (pfile, str, T_UNASSERT);
df383483 1849}
3fdc651f 1850
86368122
NB
1851/* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1852static void
6cf87ca4 1853handle_assertion (cpp_reader *pfile, const char *str, int type)
86368122
NB
1854{
1855 size_t count = strlen (str);
1856 const char *p = strchr (str, '=');
1857
26aea073
NB
1858 /* Copy the entire option so we can modify it. Change the first
1859 "=" in the string to a '(', and tack a ')' on the end. */
703ad42b 1860 char *buf = alloca (count + 2);
26aea073
NB
1861
1862 memcpy (buf, str, count);
86368122
NB
1863 if (p)
1864 {
86368122
NB
1865 buf[p - str] = '(';
1866 buf[count++] = ')';
86368122 1867 }
26aea073
NB
1868 buf[count] = '\n';
1869 str = buf;
86368122 1870
29401c30 1871 run_directive (pfile, type, str, count);
86368122
NB
1872}
1873
7e96d768
NB
1874/* The number of errors for a given reader. */
1875unsigned int
6cf87ca4 1876cpp_errors (cpp_reader *pfile)
7e96d768
NB
1877{
1878 return pfile->errors;
1879}
1880
1881/* The options structure. */
1882cpp_options *
6cf87ca4 1883cpp_get_options (cpp_reader *pfile)
7e96d768
NB
1884{
1885 return &pfile->opts;
1886}
1887
1888/* The callbacks structure. */
1889cpp_callbacks *
6cf87ca4 1890cpp_get_callbacks (cpp_reader *pfile)
7e96d768
NB
1891{
1892 return &pfile->cb;
1893}
1894
d82fc108 1895/* The line map set. */
47d89cf3 1896const struct line_maps *
6cf87ca4 1897cpp_get_line_maps (cpp_reader *pfile)
d82fc108
NB
1898{
1899 return &pfile->line_maps;
1900}
1901
7e96d768
NB
1902/* Copy the given callbacks structure to our own. */
1903void
6cf87ca4 1904cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
7e96d768
NB
1905{
1906 pfile->cb = *cb;
1907}
1908
eb1f4d9d
NB
1909/* Push a new buffer on the buffer stack. Returns the new buffer; it
1910 doesn't fail. It does not generate a file change call back; that
1911 is the responsibility of the caller. */
c71f835b 1912cpp_buffer *
6cf87ca4
ZW
1913cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
1914 int from_stage3, int return_at_eof)
c71f835b 1915{
2a967f3d 1916 cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
93c80368 1917
fde84349
NB
1918 /* Clears, amongst other things, if_stack and mi_cmacro. */
1919 memset (new, 0, sizeof (cpp_buffer));
1920
26aea073 1921 new->next_line = new->buf = buffer;
fde84349 1922 new->rlimit = buffer + len;
26aea073 1923 new->from_stage3 = from_stage3;
3cf3593f 1924 new->prev = pfile->buffer;
ef6e958a 1925 new->return_at_eof = return_at_eof;
26aea073 1926 new->need_line = true;
0bda4760 1927
3cf3593f 1928 pfile->buffer = new;
c71f835b
ZW
1929 return new;
1930}
1931
af0d16cd
NB
1932/* Pops a single buffer, with a file change call-back if appropriate.
1933 Then pushes the next -include file, if any remain. */
ef6e958a 1934void
6cf87ca4 1935_cpp_pop_buffer (cpp_reader *pfile)
c71f835b 1936{
fde84349 1937 cpp_buffer *buffer = pfile->buffer;
8f9b4009 1938 struct _cpp_file *inc = buffer->file;
ad2a084d 1939 struct if_stack *ifs;
c71f835b 1940
fde84349
NB
1941 /* Walk back up the conditional stack till we reach its level at
1942 entry to this file, issuing error messages. */
1943 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
ebef4e8c 1944 cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0,
fde84349 1945 "unterminated #%s", dtable[ifs->type].name);
eb1f4d9d 1946
97293897 1947 /* In case of a missing #endif. */
67821e3a 1948 pfile->state.skipping = 0;
29401c30 1949
af0d16cd 1950 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
29401c30
NB
1951 pfile->buffer = buffer->prev;
1952
26aea073
NB
1953 free (buffer->notes);
1954
af0d16cd
NB
1955 /* Free the buffer object now; we may want to push a new buffer
1956 in _cpp_push_next_include_file. */
1957 obstack_free (&pfile->buffer_ob, buffer);
29401c30 1958
af0d16cd
NB
1959 if (inc)
1960 {
1961 _cpp_pop_file_buffer (pfile, inc);
1962
1963 /* Don't generate a callback for popping the main file. */
1964 if (pfile->buffer)
23345bbb 1965 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
af0d16cd 1966 }
c71f835b
ZW
1967}
1968
05713b80 1969/* Enter all recognized directives in the hash table. */
c71f835b 1970void
6cf87ca4 1971_cpp_init_directives (cpp_reader *pfile)
c71f835b 1972{
766ee681 1973 unsigned int i;
93c80368 1974 cpp_hashnode *node;
bfb9dc7f 1975
37b8524c 1976 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
93c80368 1977 {
766ee681 1978 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
4977bab6
ZW
1979 node->is_directive = 1;
1980 node->directive_index = i;
93c80368 1981 }
c71f835b 1982}
This page took 1.488529 seconds and 5 git commands to generate.