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