]> gcc.gnu.org Git - gcc.git/blame - gcc/cpplib.c
Daily bump.
[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,
d9221e01 3 1999, 2000, 2001, 2002, 2003, 2004 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)
0527bc4e 218 cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
ebef4e8c 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
d97371e0
NB
275 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
276 || pfile->directive == &dtable[T_ELIF]);
45f2492c
NB
277 if (pfile->state.in_expression)
278 pfile->state.skipping = false;
279
1a76916c
NB
280 if (no_expand)
281 pfile->state.prevent_expansion++;
43839642 282 _cpp_scan_out_logical_line (pfile, NULL);
1a76916c
NB
283 if (no_expand)
284 pfile->state.prevent_expansion--;
45f2492c 285
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)
0527bc4e 304 cpp_error (pfile, CPP_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])
0527bc4e 315 cpp_error (pfile, CPP_DL_WARNING,
ebef4e8c 316 "suggest not using #elif in traditional C");
dcc229e5 317 else if (indented && dir->origin == KANDR)
0527bc4e 318 cpp_error (pfile, CPP_DL_WARNING,
ebef4e8c
NB
319 "traditional C ignores #%s with the # indented",
320 dir->name);
dcc229e5 321 else if (!indented && dir->origin != KANDR)
0527bc4e 322 cpp_error (pfile, CPP_DL_WARNING,
ebef4e8c
NB
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))
0527bc4e 344 cpp_error (pfile, CPP_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)
0527bc4e 364 cpp_error (pfile, CPP_DL_PEDWARN,
ebef4e8c 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)
0527bc4e 415 cpp_error (pfile, CPP_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,
40de9f76 444 /* from_stage3 */ true);
8bfb1467
NB
445 /* Disgusting hack. */
446 if (dir_no == T_PRAGMA)
8f9b4009 447 pfile->buffer->file = pfile->buffer->prev->file;
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 459 if (dir_no == T_PRAGMA)
8f9b4009 460 pfile->buffer->file = 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 482 if (node == pfile->spec_nodes.n_defined)
0527bc4e 483 cpp_error (pfile, CPP_DL_ERROR,
1a76916c
NB
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)
0527bc4e 489 cpp_error (pfile, CPP_DL_ERROR,
cbc69f84 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)
0527bc4e 493 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
1a76916c
NB
494 pfile->directive->name);
495 else
0527bc4e 496 cpp_error (pfile, CPP_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
45f2492c 526 if (node)
9e62c811 527 {
58fea6af 528 if (pfile->cb.undef)
6cf87ca4 529 pfile->cb.undef (pfile, pfile->directive_line, node);
9e62c811 530
45f2492c
NB
531 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
532 identifier is not currently defined as a macro name. */
533 if (node->type == NT_MACRO)
534 {
535 if (node->flags & NODE_WARN)
536 cpp_error (pfile, CPP_DL_WARNING,
537 "undefining \"%s\"", NODE_NAME (node));
9e62c811 538
45f2492c
NB
539 if (CPP_OPTION (pfile, warn_unused_macros))
540 _cpp_warn_if_unused_macro (pfile, node, NULL);
a69cbaac 541
45f2492c
NB
542 _cpp_free_definition (node);
543 }
9e62c811 544 }
45f2492c 545
93c80368 546 check_eol (pfile);
7f2935c7
PB
547}
548
d1bd0ded
GK
549/* Undefine a single macro/assertion/whatever. */
550
551static int
cf551fba 552undefine_macros (cpp_reader *pfile, cpp_hashnode *h,
d1bd0ded
GK
553 void *data_p ATTRIBUTE_UNUSED)
554{
555 switch (h->type)
556 {
557 case NT_VOID:
558 break;
cf551fba 559
d1bd0ded
GK
560 case NT_MACRO:
561 if (pfile->cb.undef)
562 (*pfile->cb.undef) (pfile, pfile->directive_line, h);
563
564 if (CPP_OPTION (pfile, warn_unused_macros))
565 _cpp_warn_if_unused_macro (pfile, h, NULL);
566
938d968e 567 /* And fall through.... */
d1bd0ded
GK
568 case NT_ASSERTION:
569 _cpp_free_definition (h);
570 break;
571
572 default:
573 abort ();
574 }
575 h->flags &= ~NODE_POISONED;
576 return 1;
577}
578
579/* Undefine all macros and assertions. */
580
581void
582cpp_undef_all (cpp_reader *pfile)
583{
584 cpp_forall_identifiers (pfile, undefine_macros, NULL);
585}
586
587
93c80368
NB
588/* Helper routine used by parse_include. Reinterpret the current line
589 as an h-char-sequence (< ... >); we are looking at the first token
74eb4b3e
NB
590 after the <. Returns a malloced filename. */
591static char *
6cf87ca4 592glue_header_name (cpp_reader *pfile)
93c80368 593{
4ed5bcfb 594 const cpp_token *token;
74eb4b3e 595 char *buffer;
2450e0b8 596 size_t len, total_len = 0, capacity = 1024;
93c80368
NB
597
598 /* To avoid lexed tokens overwriting our glued name, we can only
599 allocate from the string pool once we've lexed everything. */
74eb4b3e 600 buffer = xmalloc (capacity);
93c80368
NB
601 for (;;)
602 {
a8d0ddaf 603 token = get_token_no_padding (pfile);
93c80368 604
74eb4b3e 605 if (token->type == CPP_GREATER)
93c80368 606 break;
74eb4b3e
NB
607 if (token->type == CPP_EOF)
608 {
0527bc4e 609 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
74eb4b3e
NB
610 break;
611 }
93c80368 612
59325650 613 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
2450e0b8 614 if (total_len + len > capacity)
93c80368 615 {
2450e0b8 616 capacity = (capacity + len) * 2;
74eb4b3e 617 buffer = xrealloc (buffer, capacity);
93c80368
NB
618 }
619
4ed5bcfb 620 if (token->flags & PREV_WHITE)
2450e0b8 621 buffer[total_len++] = ' ';
93c80368 622
74eb4b3e
NB
623 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len])
624 - (uchar *) buffer);
93c80368 625 }
041c3194 626
74eb4b3e
NB
627 buffer[total_len] = '\0';
628 return buffer;
93c80368 629}
7f2935c7 630
74eb4b3e
NB
631/* Returns the file name of #include, #include_next, #import and
632 #pragma dependency. The string is malloced and the caller should
633 free it. Returns NULL on error. */
634static const char *
6cf87ca4 635parse_include (cpp_reader *pfile, int *pangle_brackets)
7f2935c7 636{
74eb4b3e 637 char *fname;
4ed5bcfb 638 const cpp_token *header;
7f2935c7 639
93c80368 640 /* Allow macro expansion. */
a8d0ddaf 641 header = get_token_no_padding (pfile);
74eb4b3e 642 if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
7f2935c7 643 {
6338b358
NB
644 fname = xmalloc (header->val.str.len - 1);
645 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
646 fname[header->val.str.len - 2] = '\0';
74eb4b3e 647 *pangle_brackets = header->type == CPP_HEADER_NAME;
7f2935c7 648 }
74eb4b3e 649 else if (header->type == CPP_LESS)
7f2935c7 650 {
74eb4b3e
NB
651 fname = glue_header_name (pfile);
652 *pangle_brackets = 1;
653 }
654 else
655 {
656 const unsigned char *dir;
657
658 if (pfile->directive == &dtable[T_PRAGMA])
659 dir = U"pragma dependency";
660 else
661 dir = pfile->directive->name;
0527bc4e 662 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
74eb4b3e
NB
663 dir);
664
4ed5bcfb 665 return NULL;
7f2935c7 666 }
7f2935c7 667
3963c2e0 668 check_eol (pfile);
74eb4b3e 669 return fname;
168d3732 670}
3caee4a8 671
ba133c96 672/* Handle #include, #include_next and #import. */
711b8824 673static void
6cf87ca4 674do_include_common (cpp_reader *pfile, enum include_type type)
168d3732 675{
74eb4b3e
NB
676 const char *fname;
677 int angle_brackets;
678
679 fname = parse_include (pfile, &angle_brackets);
680 if (!fname)
3963c2e0 681 return;
7f2935c7 682
3963c2e0 683 /* Prevent #include recursion. */
50f59cd7 684 if (pfile->line_table->depth >= CPP_STACK_MAX)
0527bc4e 685 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
74eb4b3e 686 else
09b82253 687 {
74eb4b3e
NB
688 /* Get out of macro context, if we are. */
689 skip_rest_of_line (pfile);
09b82253 690
74eb4b3e 691 if (pfile->cb.include)
6cf87ca4
ZW
692 pfile->cb.include (pfile, pfile->directive_line,
693 pfile->directive->name, fname, angle_brackets);
3963c2e0 694
8f9b4009 695 _cpp_stack_include (pfile, fname, angle_brackets, type);
74eb4b3e 696 }
3963c2e0 697
fad205ff 698 free ((void *) fname);
168d3732 699}
e8037d57 700
711b8824 701static void
6cf87ca4 702do_include (cpp_reader *pfile)
168d3732 703{
ba133c96
NB
704 do_include_common (pfile, IT_INCLUDE);
705}
168d3732 706
ba133c96 707static void
6cf87ca4 708do_import (cpp_reader *pfile)
ba133c96 709{
ba133c96 710 do_include_common (pfile, IT_IMPORT);
168d3732 711}
7f2935c7 712
711b8824 713static void
6cf87ca4 714do_include_next (cpp_reader *pfile)
168d3732 715{
3963c2e0
ZW
716 enum include_type type = IT_INCLUDE_NEXT;
717
718 /* If this is the primary source file, warn and use the normal
719 search logic. */
720 if (! pfile->buffer->prev)
721 {
0527bc4e 722 cpp_error (pfile, CPP_DL_WARNING,
3963c2e0
ZW
723 "#include_next in primary source file");
724 type = IT_INCLUDE;
725 }
726 do_include_common (pfile, type);
7f2935c7 727}
7f2935c7 728
dcc229e5
ZW
729/* Subroutine of do_linemarker. Read possible flags after file name.
730 LAST is the last flag seen; 0 if this is the first flag. Return the
731 flag if it is valid, 0 at the end of the directive. Otherwise
732 complain. */
642ce434 733static unsigned int
6cf87ca4 734read_flag (cpp_reader *pfile, unsigned int last)
d3a34a0a 735{
345894b4 736 const cpp_token *token = _cpp_lex_token (pfile);
d3a34a0a 737
345894b4 738 if (token->type == CPP_NUMBER && token->val.str.len == 1)
d3a34a0a 739 {
345894b4 740 unsigned int flag = token->val.str.text[0] - '0';
28e0f040
NB
741
742 if (flag > last && flag <= 4
743 && (flag != 4 || last == 3)
744 && (flag != 2 || last == 0))
745 return flag;
d3a34a0a 746 }
93c80368 747
345894b4 748 if (token->type != CPP_EOF)
0527bc4e 749 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
345894b4 750 cpp_token_as_text (pfile, token));
93c80368 751 return 0;
d3a34a0a
JM
752}
753
dcc229e5
ZW
754/* Subroutine of do_line and do_linemarker. Convert a number in STR,
755 of length LEN, to binary; store it in NUMP, and return 0 if the
756 number was well-formed, 1 if not. Temporary, hopefully. */
041c3194 757static int
6cf87ca4 758strtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump)
041c3194
ZW
759{
760 unsigned long reg = 0;
562a5c27 761 uchar c;
041c3194
ZW
762 while (len--)
763 {
764 c = *str++;
765 if (!ISDIGIT (c))
766 return 1;
767 reg *= 10;
768 reg += c - '0';
769 }
770 *nump = reg;
771 return 0;
772}
773
6de1e2a9 774/* Interpret #line command.
dcc229e5
ZW
775 Note that the filename string (if any) is a true string constant
776 (escapes are interpreted), unlike in #line. */
711b8824 777static void
6cf87ca4 778do_line (cpp_reader *pfile)
7f2935c7 779{
12f9df4e 780 const struct line_map *map = linemap_lookup (pfile->line_table, pfile->line);
4ed5bcfb 781 const cpp_token *token;
12f9df4e 782 const char *new_file = map->to_file;
bb74c963 783 unsigned long new_lineno;
93c80368 784
27e2564a 785 /* C99 raised the minimum limit on #line numbers. */
dcc229e5 786 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
18a9d8ff 787
93c80368 788 /* #line commands expand macros. */
4ed5bcfb
NB
789 token = cpp_get_token (pfile);
790 if (token->type != CPP_NUMBER
791 || strtoul_for_line (token->val.str.text, token->val.str.len,
792 &new_lineno))
7f2935c7 793 {
0527bc4e 794 cpp_error (pfile, CPP_DL_ERROR,
ebef4e8c 795 "\"%s\" after #line is not a positive integer",
4ed5bcfb 796 cpp_token_as_text (pfile, token));
9ec7291f 797 return;
df383483 798 }
7f2935c7 799
dcc229e5 800 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
0527bc4e 801 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
7f2935c7 802
4ed5bcfb
NB
803 token = cpp_get_token (pfile);
804 if (token->type == CPP_STRING)
5538ada6 805 {
e6cc3a24 806 cpp_string s = { 0, 0 };
423e95e2
EC
807 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
808 &s, false))
e6cc3a24 809 new_file = (const char *)s.text;
dcc229e5
ZW
810 check_eol (pfile);
811 }
812 else if (token->type != CPP_EOF)
813 {
0527bc4e 814 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
dcc229e5
ZW
815 cpp_token_as_text (pfile, token));
816 return;
817 }
818
819 skip_rest_of_line (pfile);
820 _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
12f9df4e 821 map->sysp);
dcc229e5
ZW
822}
823
824/* Interpret the # 44 "file" [flags] notation, which has slightly
825 different syntax and semantics from #line: Flags are allowed,
826 and we never complain about the line number being too big. */
827static void
6cf87ca4 828do_linemarker (cpp_reader *pfile)
dcc229e5 829{
12f9df4e 830 const struct line_map *map = linemap_lookup (pfile->line_table, pfile->line);
dcc229e5 831 const cpp_token *token;
12f9df4e 832 const char *new_file = map->to_file;
dcc229e5 833 unsigned long new_lineno;
12f9df4e 834 unsigned int new_sysp = map->sysp;
dcc229e5
ZW
835 enum lc_reason reason = LC_RENAME;
836 int flag;
837
838 /* Back up so we can get the number again. Putting this in
839 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
840 some circumstances, which can segfault. */
841 _cpp_backup_tokens (pfile, 1);
842
843 /* #line commands expand macros. */
844 token = cpp_get_token (pfile);
845 if (token->type != CPP_NUMBER
846 || strtoul_for_line (token->val.str.text, token->val.str.len,
847 &new_lineno))
848 {
0527bc4e
JDA
849 cpp_error (pfile, CPP_DL_ERROR,
850 "\"%s\" after # is not a positive integer",
dcc229e5
ZW
851 cpp_token_as_text (pfile, token));
852 return;
df383483 853 }
941e09b6 854
dcc229e5
ZW
855 token = cpp_get_token (pfile);
856 if (token->type == CPP_STRING)
857 {
e6cc3a24 858 cpp_string s = { 0, 0 };
423e95e2
EC
859 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
860 1, &s, false))
e6cc3a24 861 new_file = (const char *)s.text;
cf551fba 862
dcc229e5
ZW
863 new_sysp = 0;
864 flag = read_flag (pfile, 0);
865 if (flag == 1)
866 {
867 reason = LC_ENTER;
868 /* Fake an include for cpp_included (). */
869 _cpp_fake_include (pfile, new_file);
870 flag = read_flag (pfile, flag);
871 }
872 else if (flag == 2)
873 {
874 reason = LC_LEAVE;
875 flag = read_flag (pfile, flag);
876 }
877 if (flag == 3)
93c80368 878 {
dcc229e5
ZW
879 new_sysp = 1;
880 flag = read_flag (pfile, flag);
881 if (flag == 4)
882 new_sysp = 2;
12f9df4e 883 pfile->buffer->sysp = new_sysp;
93c80368 884 }
dcc229e5 885
fde84349 886 check_eol (pfile);
041c3194 887 }
4ed5bcfb 888 else if (token->type != CPP_EOF)
27e2564a 889 {
0527bc4e 890 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
4ed5bcfb 891 cpp_token_as_text (pfile, token));
27e2564a
NB
892 return;
893 }
7f2935c7 894
bdcbe496 895 skip_rest_of_line (pfile);
bb74c963 896 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
27e2564a
NB
897}
898
67821e3a 899/* Arrange the file_change callback. pfile->line has changed to
47d89cf3 900 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
a1f300c0 901 header, 2 for a system header that needs to be extern "C" protected,
47d89cf3 902 and zero otherwise. */
eb1f4d9d 903void
6cf87ca4
ZW
904_cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
905 const char *to_file, unsigned int file_line,
906 unsigned int sysp)
27e2564a 907{
12f9df4e
PB
908 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
909 to_file, file_line);
910 if (map == NULL)
911 pfile->line = 0;
912 else
913 pfile->line = linemap_line_start (pfile->line_table, map->to_line, 127);
d82fc108 914
eb1f4d9d 915 if (pfile->cb.file_change)
12f9df4e 916 pfile->cb.file_change (pfile, map);
7f2935c7 917}
941e09b6 918
5d8ebbd8
NB
919/* Report a warning or error detected by the program we are
920 processing. Use the directive's tokens in the error message. */
711b8824 921static void
6cf87ca4 922do_diagnostic (cpp_reader *pfile, int code, int print_dir)
7f2935c7 923{
12f9df4e 924 if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0))
58fea6af 925 {
29b10746
NB
926 if (print_dir)
927 fprintf (stderr, "#%s ", pfile->directive->name);
93c80368
NB
928 pfile->state.prevent_expansion++;
929 cpp_output_line (pfile, stderr);
930 pfile->state.prevent_expansion--;
58fea6af 931 }
7f2935c7
PB
932}
933
838f313b 934static void
6cf87ca4 935do_error (cpp_reader *pfile)
838f313b 936{
0527bc4e 937 do_diagnostic (pfile, CPP_DL_ERROR, 1);
838f313b 938}
7f2935c7 939
711b8824 940static void
6cf87ca4 941do_warning (cpp_reader *pfile)
7f2935c7 942{
2f878973 943 /* We want #warning diagnostics to be emitted in system headers too. */
0527bc4e 944 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
7f2935c7
PB
945}
946
a2a76ce7 947/* Report program identification. */
711b8824 948static void
6cf87ca4 949do_ident (cpp_reader *pfile)
7f2935c7 950{
4ed5bcfb 951 const cpp_token *str = cpp_get_token (pfile);
58fea6af 952
4ed5bcfb 953 if (str->type != CPP_STRING)
0527bc4e 954 cpp_error (pfile, CPP_DL_ERROR, "invalid #ident directive");
93c80368 955 else if (pfile->cb.ident)
6cf87ca4 956 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
a2a76ce7 957
93c80368 958 check_eol (pfile);
7f2935c7
PB
959}
960
a5da89c6
NB
961/* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
962 matching entry, or NULL if none is found. The returned entry could
963 be the start of a namespace chain, or a pragma. */
964static struct pragma_entry *
6cf87ca4 965lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
82443371 966{
4b115ff0
NB
967 while (chain && chain->pragma != pragma)
968 chain = chain->next;
a5da89c6
NB
969
970 return chain;
971}
972
973/* Create and insert a pragma entry for NAME at the beginning of a
974 singly-linked CHAIN. If handler is NULL, it is a namespace,
975 otherwise it is a pragma and its handler. */
976static struct pragma_entry *
6cf87ca4
ZW
977insert_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain,
978 const cpp_hashnode *pragma, pragma_cb handler)
82443371 979{
a5da89c6 980 struct pragma_entry *new;
58fea6af 981
bef985f3 982 new = (struct pragma_entry *)
8c3b2693 983 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
4b115ff0 984 new->pragma = pragma;
a5da89c6
NB
985 if (handler)
986 {
987 new->is_nspace = 0;
988 new->u.handler = handler;
989 }
990 else
991 {
992 new->is_nspace = 1;
993 new->u.space = NULL;
994 }
58fea6af 995
a5da89c6
NB
996 new->next = *chain;
997 *chain = new;
998 return new;
58fea6af 999}
82443371 1000
a5da89c6
NB
1001/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1002 goes in the global namespace. HANDLER is the handler it will call,
1003 which must be non-NULL. */
58fea6af 1004void
6cf87ca4
ZW
1005cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
1006 pragma_cb handler)
82443371 1007{
a5da89c6
NB
1008 struct pragma_entry **chain = &pfile->pragmas;
1009 struct pragma_entry *entry;
4b115ff0 1010 const cpp_hashnode *node;
a5da89c6
NB
1011
1012 if (!handler)
1013 abort ();
58fea6af 1014
a5da89c6 1015 if (space)
58fea6af 1016 {
4b115ff0
NB
1017 node = cpp_lookup (pfile, U space, strlen (space));
1018 entry = lookup_pragma_entry (*chain, node);
a5da89c6 1019 if (!entry)
4b115ff0 1020 entry = insert_pragma_entry (pfile, chain, node, NULL);
a5da89c6
NB
1021 else if (!entry->is_nspace)
1022 goto clash;
1023 chain = &entry->u.space;
58fea6af
ZW
1024 }
1025
a5da89c6 1026 /* Check for duplicates. */
4b115ff0
NB
1027 node = cpp_lookup (pfile, U name, strlen (name));
1028 entry = lookup_pragma_entry (*chain, node);
a5da89c6
NB
1029 if (entry)
1030 {
1031 if (entry->is_nspace)
1032 clash:
0527bc4e 1033 cpp_error (pfile, CPP_DL_ICE,
a5da89c6 1034 "registering \"%s\" as both a pragma and a pragma namespace",
4b115ff0 1035 NODE_NAME (node));
a5da89c6 1036 else if (space)
0527bc4e 1037 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
ebef4e8c 1038 space, name);
a5da89c6 1039 else
0527bc4e 1040 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
a5da89c6
NB
1041 }
1042 else
4b115ff0 1043 insert_pragma_entry (pfile, chain, node, handler);
58fea6af 1044}
a5da89c6
NB
1045
1046/* Register the pragmas the preprocessor itself handles. */
58fea6af 1047void
6cf87ca4 1048_cpp_init_internal_pragmas (cpp_reader *pfile)
58fea6af 1049{
a5da89c6 1050 /* Pragmas in the global namespace. */
58fea6af
ZW
1051 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
1052
a5da89c6 1053 /* New GCC-specific pragmas should be put in the GCC namespace. */
58fea6af
ZW
1054 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
1055 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
1056 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
82443371 1057}
7f2935c7 1058
17211ab5
GK
1059/* Return the number of registered pragmas in PE. */
1060
1061static int
6cf87ca4 1062count_registered_pragmas (struct pragma_entry *pe)
17211ab5
GK
1063{
1064 int ct = 0;
1065 for (; pe != NULL; pe = pe->next)
1066 {
1067 if (pe->is_nspace)
1068 ct += count_registered_pragmas (pe->u.space);
1069 ct++;
1070 }
1071 return ct;
1072}
1073
1074/* Save into SD the names of the registered pragmas referenced by PE,
1075 and return a pointer to the next free space in SD. */
1076
1077static char **
6cf87ca4 1078save_registered_pragmas (struct pragma_entry *pe, char **sd)
17211ab5
GK
1079{
1080 for (; pe != NULL; pe = pe->next)
1081 {
1082 if (pe->is_nspace)
1083 sd = save_registered_pragmas (pe->u.space, sd);
1084 *sd++ = xmemdup (HT_STR (&pe->pragma->ident),
1085 HT_LEN (&pe->pragma->ident),
1086 HT_LEN (&pe->pragma->ident) + 1);
1087 }
1088 return sd;
1089}
1090
1091/* Return a newly-allocated array which saves the names of the
1092 registered pragmas. */
1093
1094char **
6cf87ca4 1095_cpp_save_pragma_names (cpp_reader *pfile)
17211ab5
GK
1096{
1097 int ct = count_registered_pragmas (pfile->pragmas);
1098 char **result = xnewvec (char *, ct);
1099 (void) save_registered_pragmas (pfile->pragmas, result);
1100 return result;
1101}
1102
1103/* Restore from SD the names of the registered pragmas referenced by PE,
1104 and return a pointer to the next unused name in SD. */
1105
1106static char **
6cf87ca4
ZW
1107restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1108 char **sd)
17211ab5
GK
1109{
1110 for (; pe != NULL; pe = pe->next)
1111 {
1112 if (pe->is_nspace)
1113 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1114 pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd));
1115 free (*sd);
1116 sd++;
1117 }
1118 return sd;
1119}
1120
1121/* Restore the names of the registered pragmas from SAVED. */
1122
1123void
6cf87ca4 1124_cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
17211ab5
GK
1125{
1126 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1127 free (saved);
1128}
1129
a5da89c6
NB
1130/* Pragmata handling. We handle some, and pass the rest on to the
1131 front end. C99 defines three pragmas and says that no macro
1132 expansion is to be performed on them; whether or not macro
1133 expansion happens for other pragmas is implementation defined.
1134 This implementation never macro-expands the text after #pragma. */
711b8824 1135static void
6cf87ca4 1136do_pragma (cpp_reader *pfile)
7f2935c7 1137{
a5da89c6 1138 const struct pragma_entry *p = NULL;
e2e1fa50 1139 const cpp_token *token, *pragma_token = pfile->cur_token;
a5da89c6 1140 unsigned int count = 1;
add7091b 1141
93c80368 1142 pfile->state.prevent_expansion++;
58fea6af 1143
4ed5bcfb
NB
1144 token = cpp_get_token (pfile);
1145 if (token->type == CPP_NAME)
0172e2bc 1146 {
4b115ff0 1147 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
a5da89c6 1148 if (p && p->is_nspace)
58fea6af 1149 {
a5da89c6
NB
1150 count = 2;
1151 token = cpp_get_token (pfile);
1152 if (token->type == CPP_NAME)
4b115ff0 1153 p = lookup_pragma_entry (p->u.space, token->val.node);
a5da89c6
NB
1154 else
1155 p = NULL;
58fea6af 1156 }
58fea6af 1157 }
041c3194 1158
a5da89c6 1159 if (p)
e2e1fa50
AO
1160 {
1161 /* Since the handler below doesn't get the line number, that it
1162 might need for diagnostics, make sure it has the right
1163 numbers in place. */
1164 if (pfile->cb.line_change)
1165 (*pfile->cb.line_change) (pfile, pragma_token, false);
1166 (*p->u.handler) (pfile);
1167 if (pfile->cb.line_change)
1168 (*pfile->cb.line_change) (pfile, pfile->cur_token, false);
cf551fba 1169
e2e1fa50 1170 }
d82fc108 1171 else if (pfile->cb.def_pragma)
bdcbe496
NB
1172 {
1173 _cpp_backup_tokens (pfile, count);
6cf87ca4 1174 pfile->cb.def_pragma (pfile, pfile->directive_line);
bdcbe496 1175 }
a5da89c6 1176
97293897 1177 pfile->state.prevent_expansion--;
82443371
NS
1178}
1179
5d8ebbd8 1180/* Handle #pragma once. */
58fea6af 1181static void
6cf87ca4 1182do_pragma_once (cpp_reader *pfile)
a2a76ce7 1183{
642ce434 1184 if (pfile->buffer->prev == NULL)
0527bc4e 1185 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
93c80368
NB
1186
1187 check_eol (pfile);
49634b3a 1188 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
a2a76ce7 1189}
fc009f96 1190
c3bf3e6e
NB
1191/* Handle #pragma GCC poison, to poison one or more identifiers so
1192 that the lexer produces a hard error for each subsequent usage. */
58fea6af 1193static void
6cf87ca4 1194do_pragma_poison (cpp_reader *pfile)
a2a76ce7 1195{
345894b4 1196 const cpp_token *tok;
f8f769ea 1197 cpp_hashnode *hp;
a2a76ce7 1198
93c80368 1199 pfile->state.poisoned_ok = 1;
a2a76ce7
ZW
1200 for (;;)
1201 {
345894b4
NB
1202 tok = _cpp_lex_token (pfile);
1203 if (tok->type == CPP_EOF)
a2a76ce7 1204 break;
345894b4 1205 if (tok->type != CPP_NAME)
fc009f96 1206 {
0527bc4e
JDA
1207 cpp_error (pfile, CPP_DL_ERROR,
1208 "invalid #pragma GCC poison directive");
93c80368 1209 break;
fc009f96
GK
1210 }
1211
345894b4 1212 hp = tok->val.node;
93c80368
NB
1213 if (hp->flags & NODE_POISONED)
1214 continue;
1215
1216 if (hp->type == NT_MACRO)
0527bc4e 1217 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
ebef4e8c 1218 NODE_NAME (hp));
93c80368
NB
1219 _cpp_free_definition (hp);
1220 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
fc009f96 1221 }
93c80368 1222 pfile->state.poisoned_ok = 0;
7f2935c7 1223}
2c0b35cb
ZW
1224
1225/* Mark the current header as a system header. This will suppress
1226 some categories of warnings (notably those from -pedantic). It is
1227 intended for use in system libraries that cannot be implemented in
1228 conforming C, but cannot be certain that their headers appear in a
1229 system include directory. To prevent abuse, it is rejected in the
1230 primary source file. */
58fea6af 1231static void
6cf87ca4 1232do_pragma_system_header (cpp_reader *pfile)
2c0b35cb 1233{
614c7d37
NB
1234 cpp_buffer *buffer = pfile->buffer;
1235
1236 if (buffer->prev == 0)
0527bc4e 1237 cpp_error (pfile, CPP_DL_WARNING,
ebef4e8c 1238 "#pragma system_header ignored outside include file");
2c0b35cb 1239 else
d82fc108
NB
1240 {
1241 check_eol (pfile);
bdcbe496 1242 skip_rest_of_line (pfile);
d82fc108
NB
1243 cpp_make_system_header (pfile, 1, 0);
1244 }
2c0b35cb 1245}
f3f751ad
NS
1246
1247/* Check the modified date of the current include file against a specified
1248 file. Issue a diagnostic, if the specified file is newer. We use this to
1249 determine if a fixed header should be refixed. */
58fea6af 1250static void
6cf87ca4 1251do_pragma_dependency (cpp_reader *pfile)
f3f751ad 1252{
74eb4b3e
NB
1253 const char *fname;
1254 int angle_brackets, ordering;
df383483 1255
74eb4b3e
NB
1256 fname = parse_include (pfile, &angle_brackets);
1257 if (!fname)
58fea6af 1258 return;
041c3194 1259
74eb4b3e 1260 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
f3f751ad 1261 if (ordering < 0)
0527bc4e 1262 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
f3f751ad
NS
1263 else if (ordering > 0)
1264 {
0527bc4e
JDA
1265 cpp_error (pfile, CPP_DL_WARNING,
1266 "current file is older than %s", fname);
4ed5bcfb 1267 if (cpp_get_token (pfile)->type != CPP_EOF)
bdcbe496
NB
1268 {
1269 _cpp_backup_tokens (pfile, 1);
0527bc4e 1270 do_diagnostic (pfile, CPP_DL_WARNING, 0);
bdcbe496 1271 }
f3f751ad 1272 }
74eb4b3e 1273
fad205ff 1274 free ((void *) fname);
f3f751ad
NS
1275}
1276
4ed5bcfb
NB
1277/* Get a token but skip padding. */
1278static const cpp_token *
6cf87ca4 1279get_token_no_padding (cpp_reader *pfile)
a5c3cccd 1280{
4ed5bcfb
NB
1281 for (;;)
1282 {
1283 const cpp_token *result = cpp_get_token (pfile);
1284 if (result->type != CPP_PADDING)
1285 return result;
1286 }
1287}
a5c3cccd 1288
4ed5bcfb
NB
1289/* Check syntax is "(string-literal)". Returns the string on success,
1290 or NULL on failure. */
1291static const cpp_token *
6cf87ca4 1292get__Pragma_string (cpp_reader *pfile)
4ed5bcfb
NB
1293{
1294 const cpp_token *string;
a5c3cccd 1295
4ed5bcfb
NB
1296 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1297 return NULL;
1298
1299 string = get_token_no_padding (pfile);
a5c3cccd 1300 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
4ed5bcfb
NB
1301 return NULL;
1302
1303 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1304 return NULL;
a5c3cccd 1305
4ed5bcfb 1306 return string;
a5c3cccd
NB
1307}
1308
87062813
NB
1309/* Destringize IN into a temporary buffer, by removing the first \ of
1310 \" and \\ sequences, and process the result as a #pragma directive. */
1311static void
6cf87ca4 1312destringize_and_run (cpp_reader *pfile, const cpp_string *in)
a5c3cccd
NB
1313{
1314 const unsigned char *src, *limit;
87062813 1315 char *dest, *result;
a5c3cccd 1316
6338b358
NB
1317 dest = result = alloca (in->len - 1);
1318 src = in->text + 1 + (in->text[0] == 'L');
1319 limit = in->text + in->len - 1;
1320 while (src < limit)
a5c3cccd
NB
1321 {
1322 /* We know there is a character following the backslash. */
1323 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1324 src++;
1325 *dest++ = *src++;
1326 }
26aea073 1327 *dest = '\n';
a5c3cccd 1328
8128cccf
NB
1329 /* Ugh; an awful kludge. We are really not set up to be lexing
1330 tokens when in the middle of a macro expansion. Use a new
1331 context to force cpp_get_token to lex, and so skip_rest_of_line
1332 doesn't go beyond the end of the text. Also, remember the
1333 current lexing position so we can return to it later.
1334
1335 Something like line-at-a-time lexing should remove the need for
1336 this. */
1337 {
1338 cpp_context *saved_context = pfile->context;
1339 cpp_token *saved_cur_token = pfile->cur_token;
1340 tokenrun *saved_cur_run = pfile->cur_run;
1341
1342 pfile->context = xnew (cpp_context);
1343 pfile->context->macro = 0;
1344 pfile->context->prev = 0;
1345 run_directive (pfile, T_PRAGMA, result, dest - result);
1346 free (pfile->context);
1347 pfile->context = saved_context;
1348 pfile->cur_token = saved_cur_token;
1349 pfile->cur_run = saved_cur_run;
8128cccf 1350 }
79ba5e3b
NB
1351
1352 /* See above comment. For the moment, we'd like
1353
1354 token1 _Pragma ("foo") token2
1355
1356 to be output as
1357
1358 token1
1359 # 7 "file.c"
1360 #pragma foo
1361 # 7 "file.c"
1362 token2
1363
1364 Getting the line markers is a little tricky. */
1365 if (pfile->cb.line_change)
6cf87ca4 1366 pfile->cb.line_change (pfile, pfile->cur_token, false);
a5c3cccd
NB
1367}
1368
87062813 1369/* Handle the _Pragma operator. */
a5c3cccd 1370void
6cf87ca4 1371_cpp_do__Pragma (cpp_reader *pfile)
a5c3cccd 1372{
4ed5bcfb 1373 const cpp_token *string = get__Pragma_string (pfile);
a5c3cccd 1374
79ba5e3b
NB
1375 if (string)
1376 destringize_and_run (pfile, &string->val.str);
1377 else
0527bc4e 1378 cpp_error (pfile, CPP_DL_ERROR,
ebef4e8c 1379 "_Pragma takes a parenthesized string literal");
a5c3cccd
NB
1380}
1381
6cf87ca4 1382/* Ignore #sccs on all systems. */
711b8824 1383static void
6cf87ca4 1384do_sccs (cpp_reader *pfile ATTRIBUTE_UNUSED)
7f2935c7 1385{
7f2935c7 1386}
1d0e51ba 1387
5d8ebbd8 1388/* Handle #ifdef. */
711b8824 1389static void
6cf87ca4 1390do_ifdef (cpp_reader *pfile)
168d3732 1391{
93c80368 1392 int skip = 1;
041c3194 1393
cef0d199 1394 if (! pfile->state.skipping)
93c80368
NB
1395 {
1396 const cpp_hashnode *node = lex_macro_node (pfile);
041c3194 1397
93c80368 1398 if (node)
a69cbaac
NB
1399 {
1400 skip = node->type != NT_MACRO;
1401 _cpp_mark_macro_used (node);
1402 check_eol (pfile);
1403 }
93c80368 1404 }
168d3732 1405
93c80368
NB
1406 push_conditional (pfile, skip, T_IFDEF, 0);
1407}
168d3732 1408
5d8ebbd8 1409/* Handle #ifndef. */
711b8824 1410static void
6cf87ca4 1411do_ifndef (cpp_reader *pfile)
168d3732 1412{
93c80368 1413 int skip = 1;
bfb9dc7f 1414 const cpp_hashnode *node = 0;
168d3732 1415
cef0d199 1416 if (! pfile->state.skipping)
5af7e2c2 1417 {
93c80368 1418 node = lex_macro_node (pfile);
b43db0b3
GK
1419
1420 if (node)
a69cbaac
NB
1421 {
1422 skip = node->type == NT_MACRO;
1423 _cpp_mark_macro_used (node);
1424 check_eol (pfile);
1425 }
5af7e2c2 1426 }
041c3194 1427
93c80368 1428 push_conditional (pfile, skip, T_IFNDEF, node);
7f2935c7
PB
1429}
1430
6d18adbc
NB
1431/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1432 pfile->mi_ind_cmacro so we can handle multiple-include
6356f892 1433 optimizations. If macro expansion occurs in the expression, we
6d18adbc
NB
1434 cannot treat it as a controlling conditional, since the expansion
1435 could change in the future. That is handled by cpp_get_token. */
711b8824 1436static void
6cf87ca4 1437do_if (cpp_reader *pfile)
7f2935c7 1438{
93c80368 1439 int skip = 1;
7f2935c7 1440
cef0d199 1441 if (! pfile->state.skipping)
87ed109f 1442 skip = _cpp_parse_expr (pfile) == false;
93c80368 1443
6d18adbc 1444 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
7f2935c7
PB
1445}
1446
b528a07e 1447/* Flip skipping state if appropriate and continue without changing
ea4a453b
ZW
1448 if_stack; this is so that the error message for missing #endif's
1449 etc. will point to the original #if. */
711b8824 1450static void
6cf87ca4 1451do_else (cpp_reader *pfile)
ed705a82 1452{
b528a07e
NB
1453 cpp_buffer *buffer = pfile->buffer;
1454 struct if_stack *ifs = buffer->if_stack;
ff2b53ef 1455
ea4a453b 1456 if (ifs == NULL)
0527bc4e 1457 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
93c80368 1458 else
ff2b53ef 1459 {
93c80368
NB
1460 if (ifs->type == T_ELSE)
1461 {
0527bc4e
JDA
1462 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1463 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
93c80368
NB
1464 "the conditional began here");
1465 }
b528a07e
NB
1466 ifs->type = T_ELSE;
1467
cef0d199
NB
1468 /* Skip any future (erroneous) #elses or #elifs. */
1469 pfile->state.skipping = ifs->skip_elses;
1470 ifs->skip_elses = true;
7f2935c7 1471
93c80368
NB
1472 /* Invalidate any controlling macro. */
1473 ifs->mi_cmacro = 0;
93c80368 1474
cef0d199 1475 /* Only check EOL if was not originally skipping. */
909de5da 1476 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
cef0d199
NB
1477 check_eol (pfile);
1478 }
7f2935c7
PB
1479}
1480
5d8ebbd8 1481/* Handle a #elif directive by not changing if_stack either. See the
93c80368 1482 comment above do_else. */
711b8824 1483static void
6cf87ca4 1484do_elif (cpp_reader *pfile)
7f2935c7 1485{
b528a07e
NB
1486 cpp_buffer *buffer = pfile->buffer;
1487 struct if_stack *ifs = buffer->if_stack;
7f2935c7 1488
ea4a453b 1489 if (ifs == NULL)
0527bc4e 1490 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
b528a07e 1491 else
40ea76de 1492 {
b528a07e
NB
1493 if (ifs->type == T_ELSE)
1494 {
0527bc4e
JDA
1495 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1496 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
b528a07e
NB
1497 "the conditional began here");
1498 }
1499 ifs->type = T_ELIF;
93c80368 1500
cef0d199
NB
1501 /* Only evaluate this if we aren't skipping elses. During
1502 evaluation, set skipping to false to get lexer warnings. */
1503 if (ifs->skip_elses)
1504 pfile->state.skipping = 1;
1505 else
b528a07e 1506 {
cef0d199
NB
1507 pfile->state.skipping = 0;
1508 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1509 ifs->skip_elses = ! pfile->state.skipping;
b528a07e 1510 }
cef0d199
NB
1511
1512 /* Invalidate any controlling macro. */
1513 ifs->mi_cmacro = 0;
40ea76de 1514 }
7f2935c7
PB
1515}
1516
cef0d199 1517/* #endif pops the if stack and resets pfile->state.skipping. */
711b8824 1518static void
6cf87ca4 1519do_endif (cpp_reader *pfile)
7f2935c7 1520{
b528a07e
NB
1521 cpp_buffer *buffer = pfile->buffer;
1522 struct if_stack *ifs = buffer->if_stack;
ea4a453b 1523
ea4a453b 1524 if (ifs == NULL)
0527bc4e 1525 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
7f2935c7
PB
1526 else
1527 {
cef0d199 1528 /* Only check EOL if was not originally skipping. */
909de5da 1529 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
cef0d199
NB
1530 check_eol (pfile);
1531
93c80368
NB
1532 /* If potential control macro, we go back outside again. */
1533 if (ifs->next == 0 && ifs->mi_cmacro)
1534 {
6d18adbc 1535 pfile->mi_valid = true;
93c80368
NB
1536 pfile->mi_cmacro = ifs->mi_cmacro;
1537 }
1538
b528a07e 1539 buffer->if_stack = ifs->next;
cef0d199 1540 pfile->state.skipping = ifs->was_skipping;
2a967f3d 1541 obstack_free (&pfile->buffer_ob, ifs);
7f2935c7 1542 }
93c80368 1543}
041c3194 1544
5d8ebbd8
NB
1545/* Push an if_stack entry for a preprocessor conditional, and set
1546 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1547 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1548 we need to check here that we are at the top of the file. */
ea4a453b 1549static void
6cf87ca4
ZW
1550push_conditional (cpp_reader *pfile, int skip, int type,
1551 const cpp_hashnode *cmacro)
ea4a453b
ZW
1552{
1553 struct if_stack *ifs;
b528a07e 1554 cpp_buffer *buffer = pfile->buffer;
ea4a453b 1555
2a967f3d 1556 ifs = xobnew (&pfile->buffer_ob, struct if_stack);
50410426 1557 ifs->line = pfile->directive_line;
b528a07e 1558 ifs->next = buffer->if_stack;
cef0d199
NB
1559 ifs->skip_elses = pfile->state.skipping || !skip;
1560 ifs->was_skipping = pfile->state.skipping;
ea4a453b 1561 ifs->type = type;
6d18adbc
NB
1562 /* This condition is effectively a test for top-of-file. */
1563 if (pfile->mi_valid && pfile->mi_cmacro == 0)
93c80368
NB
1564 ifs->mi_cmacro = cmacro;
1565 else
1566 ifs->mi_cmacro = 0;
ea4a453b 1567
cef0d199 1568 pfile->state.skipping = skip;
b528a07e 1569 buffer->if_stack = ifs;
782331f4 1570}
7061aa5a 1571
5d8ebbd8
NB
1572/* Read the tokens of the answer into the macro pool, in a directive
1573 of type TYPE. Only commit the memory if we intend it as permanent
1574 storage, i.e. the #assert case. Returns 0 on success, and sets
1575 ANSWERP to point to the answer. */
93c80368 1576static int
6cf87ca4 1577parse_answer (cpp_reader *pfile, struct answer **answerp, int type)
7f2935c7 1578{
4ed5bcfb 1579 const cpp_token *paren;
93c80368 1580 struct answer *answer;
8c3b2693 1581 unsigned int acount;
93c80368
NB
1582
1583 /* In a conditional, it is legal to not have an open paren. We
1584 should save the following token in this case. */
4ed5bcfb 1585 paren = cpp_get_token (pfile);
93c80368
NB
1586
1587 /* If not a paren, see if we're OK. */
4ed5bcfb 1588 if (paren->type != CPP_OPEN_PAREN)
041c3194 1589 {
93c80368
NB
1590 /* In a conditional no answer is a test for any answer. It
1591 could be followed by any token. */
1592 if (type == T_IF)
bdcbe496
NB
1593 {
1594 _cpp_backup_tokens (pfile, 1);
1595 return 0;
1596 }
93c80368
NB
1597
1598 /* #unassert with no answer is valid - it removes all answers. */
4ed5bcfb 1599 if (type == T_UNASSERT && paren->type == CPP_EOF)
93c80368 1600 return 0;
15dad1d9 1601
0527bc4e 1602 cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate");
93c80368 1603 return 1;
041c3194 1604 }
7061aa5a 1605
8c3b2693 1606 for (acount = 0;; acount++)
041c3194 1607 {
8c3b2693
NB
1608 size_t room_needed;
1609 const cpp_token *token = cpp_get_token (pfile);
1610 cpp_token *dest;
93c80368 1611
041c3194
ZW
1612 if (token->type == CPP_CLOSE_PAREN)
1613 break;
a7abcbbf 1614
93c80368 1615 if (token->type == CPP_EOF)
041c3194 1616 {
0527bc4e 1617 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
93c80368 1618 return 1;
041c3194 1619 }
8c3b2693
NB
1620
1621 /* struct answer includes the space for one token. */
1622 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1623
1624 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1625 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1626
1627 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1628 *dest = *token;
1629
1630 /* Drop whitespace at start, for answer equivalence purposes. */
1631 if (acount == 0)
1632 dest->flags &= ~PREV_WHITE;
041c3194 1633 }
15dad1d9 1634
8c3b2693 1635 if (acount == 0)
7061aa5a 1636 {
0527bc4e 1637 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
93c80368 1638 return 1;
7f2935c7 1639 }
041c3194 1640
8c3b2693
NB
1641 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1642 answer->count = acount;
1643 answer->next = NULL;
93c80368 1644 *answerp = answer;
041c3194 1645
93c80368
NB
1646 return 0;
1647}
041c3194 1648
5d8ebbd8
NB
1649/* Parses an assertion directive of type TYPE, returning a pointer to
1650 the hash node of the predicate, or 0 on error. If an answer was
1651 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
93c80368 1652static cpp_hashnode *
6cf87ca4 1653parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
93c80368
NB
1654{
1655 cpp_hashnode *result = 0;
4ed5bcfb 1656 const cpp_token *predicate;
93c80368
NB
1657
1658 /* We don't expand predicates or answers. */
1659 pfile->state.prevent_expansion++;
1660
93c80368 1661 *answerp = 0;
4ed5bcfb
NB
1662 predicate = cpp_get_token (pfile);
1663 if (predicate->type == CPP_EOF)
0527bc4e 1664 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
4ed5bcfb 1665 else if (predicate->type != CPP_NAME)
0527bc4e 1666 cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier");
93c80368
NB
1667 else if (parse_answer (pfile, answerp, type) == 0)
1668 {
4ed5bcfb 1669 unsigned int len = NODE_LEN (predicate->val.node);
93c80368 1670 unsigned char *sym = alloca (len + 1);
041c3194 1671
93c80368
NB
1672 /* Prefix '#' to get it out of macro namespace. */
1673 sym[0] = '#';
4ed5bcfb 1674 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
93c80368
NB
1675 result = cpp_lookup (pfile, sym, len + 1);
1676 }
7061aa5a 1677
93c80368
NB
1678 pfile->state.prevent_expansion--;
1679 return result;
7f2935c7 1680}
7061aa5a 1681
5d8ebbd8 1682/* Returns a pointer to the pointer to CANDIDATE in the answer chain,
041c3194 1683 or a pointer to NULL if the answer is not in the chain. */
93c80368 1684static struct answer **
6cf87ca4 1685find_answer (cpp_hashnode *node, const struct answer *candidate)
7f2935c7 1686{
93c80368 1687 unsigned int i;
041c3194 1688 struct answer **result;
7f2935c7 1689
041c3194 1690 for (result = &node->value.answers; *result; result = &(*result)->next)
93c80368
NB
1691 {
1692 struct answer *answer = *result;
1693
1694 if (answer->count == candidate->count)
1695 {
1696 for (i = 0; i < answer->count; i++)
1697 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1698 break;
1699
1700 if (i == answer->count)
1701 break;
1702 }
1703 }
ff2b53ef 1704
041c3194
ZW
1705 return result;
1706}
15dad1d9 1707
93c80368 1708/* Test an assertion within a preprocessor conditional. Returns
da7d8304 1709 nonzero on failure, zero on success. On success, the result of
2402645b 1710 the test is written into VALUE, otherwise the value 0. */
93c80368 1711int
6cf87ca4 1712_cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
93c80368
NB
1713{
1714 struct answer *answer;
1715 cpp_hashnode *node;
1716
1717 node = parse_assertion (pfile, &answer, T_IF);
2402645b
HPN
1718
1719 /* For recovery, an erroneous assertion expression is handled as a
1720 failing assertion. */
1721 *value = 0;
1722
93c80368
NB
1723 if (node)
1724 *value = (node->type == NT_ASSERTION &&
1725 (answer == 0 || *find_answer (node, answer) != 0));
91318908
NB
1726 else if (pfile->cur_token[-1].type == CPP_EOF)
1727 _cpp_backup_tokens (pfile, 1);
93c80368
NB
1728
1729 /* We don't commit the memory for the answer - it's temporary only. */
1730 return node == 0;
1731}
1732
5d8ebbd8 1733/* Handle #assert. */
711b8824 1734static void
6cf87ca4 1735do_assert (cpp_reader *pfile)
041c3194
ZW
1736{
1737 struct answer *new_answer;
1738 cpp_hashnode *node;
df383483 1739
93c80368 1740 node = parse_assertion (pfile, &new_answer, T_ASSERT);
041c3194 1741 if (node)
ff2b53ef 1742 {
93c80368
NB
1743 /* Place the new answer in the answer list. First check there
1744 is not a duplicate. */
041c3194 1745 new_answer->next = 0;
93c80368 1746 if (node->type == NT_ASSERTION)
041c3194 1747 {
93c80368
NB
1748 if (*find_answer (node, new_answer))
1749 {
0527bc4e 1750 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
ebef4e8c 1751 NODE_NAME (node) + 1);
93c80368
NB
1752 return;
1753 }
041c3194
ZW
1754 new_answer->next = node->value.answers;
1755 }
8c3b2693 1756
93c80368 1757 node->type = NT_ASSERTION;
041c3194 1758 node->value.answers = new_answer;
8c3b2693
NB
1759 BUFF_FRONT (pfile->a_buff) += (sizeof (struct answer)
1760 + (new_answer->count - 1)
1761 * sizeof (cpp_token));
1762 check_eol (pfile);
ff2b53ef 1763 }
041c3194 1764}
15dad1d9 1765
5d8ebbd8 1766/* Handle #unassert. */
711b8824 1767static void
6cf87ca4 1768do_unassert (cpp_reader *pfile)
041c3194
ZW
1769{
1770 cpp_hashnode *node;
93c80368 1771 struct answer *answer;
df383483 1772
93c80368
NB
1773 node = parse_assertion (pfile, &answer, T_UNASSERT);
1774 /* It isn't an error to #unassert something that isn't asserted. */
1775 if (node && node->type == NT_ASSERTION)
7061aa5a 1776 {
93c80368 1777 if (answer)
15dad1d9 1778 {
93c80368 1779 struct answer **p = find_answer (node, answer), *temp;
041c3194 1780
93c80368
NB
1781 /* Remove the answer from the list. */
1782 temp = *p;
1783 if (temp)
1784 *p = temp->next;
a7abcbbf 1785
93c80368
NB
1786 /* Did we free the last answer? */
1787 if (node->value.answers == 0)
1788 node->type = NT_VOID;
8c3b2693
NB
1789
1790 check_eol (pfile);
93c80368
NB
1791 }
1792 else
1793 _cpp_free_definition (node);
041c3194 1794 }
93c80368
NB
1795
1796 /* We don't commit the memory for the answer - it's temporary only. */
7f2935c7 1797}
7f2935c7 1798
45b966db
ZW
1799/* These are for -D, -U, -A. */
1800
1801/* Process the string STR as if it appeared as the body of a #define.
1802 If STR is just an identifier, define it with value 1.
1803 If STR has anything after the identifier, then it should
ec5c56db 1804 be identifier=definition. */
0b22d65c 1805void
6cf87ca4 1806cpp_define (cpp_reader *pfile, const char *str)
0b22d65c 1807{
45b966db
ZW
1808 char *buf, *p;
1809 size_t count;
1810
df383483 1811 /* Copy the entire option so we can modify it.
45b966db 1812 Change the first "=" in the string to a space. If there is none,
86368122
NB
1813 tack " 1" on the end. */
1814
86368122 1815 count = strlen (str);
703ad42b 1816 buf = alloca (count + 3);
86368122
NB
1817 memcpy (buf, str, count);
1818
1819 p = strchr (str, '=');
45b966db 1820 if (p)
86368122 1821 buf[p - str] = ' ';
45b966db
ZW
1822 else
1823 {
86368122
NB
1824 buf[count++] = ' ';
1825 buf[count++] = '1';
0b22d65c 1826 }
26aea073 1827 buf[count] = '\n';
cf4ed945 1828
29401c30 1829 run_directive (pfile, T_DEFINE, buf, count);
2c8f0515
ZW
1830}
1831
ad2a084d 1832/* Slight variant of the above for use by initialize_builtins. */
2c8f0515 1833void
6cf87ca4 1834_cpp_define_builtin (cpp_reader *pfile, const char *str)
2c8f0515 1835{
26aea073
NB
1836 size_t len = strlen (str);
1837 char *buf = alloca (len + 1);
1838 memcpy (buf, str, len);
1839 buf[len] = '\n';
1840 run_directive (pfile, T_DEFINE, buf, len);
45b966db 1841}
0f41302f 1842
45b966db
ZW
1843/* Process MACRO as if it appeared as the body of an #undef. */
1844void
6cf87ca4 1845cpp_undef (cpp_reader *pfile, const char *macro)
7f2935c7 1846{
26aea073
NB
1847 size_t len = strlen (macro);
1848 char *buf = alloca (len + 1);
1849 memcpy (buf, macro, len);
1850 buf[len] = '\n';
1851 run_directive (pfile, T_UNDEF, buf, len);
7f2935c7
PB
1852}
1853
ec5c56db 1854/* Process the string STR as if it appeared as the body of a #assert. */
45b966db 1855void
6cf87ca4 1856cpp_assert (cpp_reader *pfile, const char *str)
45b966db 1857{
86368122 1858 handle_assertion (pfile, str, T_ASSERT);
45b966db 1859}
7f2935c7 1860
ec5c56db 1861/* Process STR as if it appeared as the body of an #unassert. */
45b966db 1862void
6cf87ca4 1863cpp_unassert (cpp_reader *pfile, const char *str)
7f2935c7 1864{
86368122 1865 handle_assertion (pfile, str, T_UNASSERT);
df383483 1866}
3fdc651f 1867
86368122
NB
1868/* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1869static void
6cf87ca4 1870handle_assertion (cpp_reader *pfile, const char *str, int type)
86368122
NB
1871{
1872 size_t count = strlen (str);
1873 const char *p = strchr (str, '=');
1874
26aea073
NB
1875 /* Copy the entire option so we can modify it. Change the first
1876 "=" in the string to a '(', and tack a ')' on the end. */
703ad42b 1877 char *buf = alloca (count + 2);
26aea073
NB
1878
1879 memcpy (buf, str, count);
86368122
NB
1880 if (p)
1881 {
86368122
NB
1882 buf[p - str] = '(';
1883 buf[count++] = ')';
86368122 1884 }
26aea073
NB
1885 buf[count] = '\n';
1886 str = buf;
86368122 1887
29401c30 1888 run_directive (pfile, type, str, count);
86368122
NB
1889}
1890
7e96d768
NB
1891/* The number of errors for a given reader. */
1892unsigned int
6cf87ca4 1893cpp_errors (cpp_reader *pfile)
7e96d768
NB
1894{
1895 return pfile->errors;
1896}
1897
1898/* The options structure. */
1899cpp_options *
6cf87ca4 1900cpp_get_options (cpp_reader *pfile)
7e96d768
NB
1901{
1902 return &pfile->opts;
1903}
1904
1905/* The callbacks structure. */
1906cpp_callbacks *
6cf87ca4 1907cpp_get_callbacks (cpp_reader *pfile)
7e96d768
NB
1908{
1909 return &pfile->cb;
1910}
1911
1912/* Copy the given callbacks structure to our own. */
1913void
6cf87ca4 1914cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
7e96d768
NB
1915{
1916 pfile->cb = *cb;
1917}
1918
eb1f4d9d
NB
1919/* Push a new buffer on the buffer stack. Returns the new buffer; it
1920 doesn't fail. It does not generate a file change call back; that
1921 is the responsibility of the caller. */
c71f835b 1922cpp_buffer *
6cf87ca4 1923cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
40de9f76 1924 int from_stage3)
c71f835b 1925{
2a967f3d 1926 cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
93c80368 1927
fde84349
NB
1928 /* Clears, amongst other things, if_stack and mi_cmacro. */
1929 memset (new, 0, sizeof (cpp_buffer));
1930
26aea073 1931 new->next_line = new->buf = buffer;
fde84349 1932 new->rlimit = buffer + len;
26aea073 1933 new->from_stage3 = from_stage3;
3cf3593f 1934 new->prev = pfile->buffer;
26aea073 1935 new->need_line = true;
0bda4760 1936
3cf3593f 1937 pfile->buffer = new;
cf551fba 1938
c71f835b
ZW
1939 return new;
1940}
1941
af0d16cd
NB
1942/* Pops a single buffer, with a file change call-back if appropriate.
1943 Then pushes the next -include file, if any remain. */
ef6e958a 1944void
6cf87ca4 1945_cpp_pop_buffer (cpp_reader *pfile)
c71f835b 1946{
fde84349 1947 cpp_buffer *buffer = pfile->buffer;
8f9b4009 1948 struct _cpp_file *inc = buffer->file;
ad2a084d 1949 struct if_stack *ifs;
c71f835b 1950
fde84349
NB
1951 /* Walk back up the conditional stack till we reach its level at
1952 entry to this file, issuing error messages. */
1953 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
0527bc4e 1954 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
fde84349 1955 "unterminated #%s", dtable[ifs->type].name);
eb1f4d9d 1956
97293897 1957 /* In case of a missing #endif. */
67821e3a 1958 pfile->state.skipping = 0;
29401c30 1959
af0d16cd 1960 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
29401c30
NB
1961 pfile->buffer = buffer->prev;
1962
26aea073
NB
1963 free (buffer->notes);
1964
af0d16cd
NB
1965 /* Free the buffer object now; we may want to push a new buffer
1966 in _cpp_push_next_include_file. */
1967 obstack_free (&pfile->buffer_ob, buffer);
29401c30 1968
af0d16cd
NB
1969 if (inc)
1970 {
1971 _cpp_pop_file_buffer (pfile, inc);
1972
40de9f76 1973 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
af0d16cd 1974 }
c71f835b
ZW
1975}
1976
05713b80 1977/* Enter all recognized directives in the hash table. */
c71f835b 1978void
6cf87ca4 1979_cpp_init_directives (cpp_reader *pfile)
c71f835b 1980{
766ee681 1981 unsigned int i;
93c80368 1982 cpp_hashnode *node;
bfb9dc7f 1983
37b8524c 1984 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
93c80368 1985 {
766ee681 1986 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
4977bab6
ZW
1987 node->is_directive = 1;
1988 node->directive_index = i;
93c80368 1989 }
c71f835b 1990}
This page took 1.816376 seconds and 5 git commands to generate.