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