blob: 76055a6fc3acdd172ff82b1e9566b7966526a0a7 [file] [log] [blame]
Neil Bootha9499412000-11-09 21:18:15 +00001/* CPP Library. (Directive handling.)
Jeff Law5e7b4e22000-02-25 22:59:31 -07002 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
Kazu Hiratad9221e012004-01-21 20:40:04 +00003 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
Richard Kenner4c8cc611997-02-16 08:08:25 -05004 Contributed by Per Bothner, 1994-95.
Richard Kennerd8bfa781997-01-03 08:19:34 -05005 Based on CCCP program by Paul Rubin, June 1986
Per Bothner7f2935c1995-03-16 13:59:07 -08006 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
Jeff Law956d6951997-12-06 17:31:01 -070020Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
Per Bothner7f2935c1995-03-16 13:59:07 -080021
Per Bothner7f2935c1995-03-16 13:59:07 -080022#include "config.h"
Kaveh R. Ghazib04cd5071998-03-30 12:05:54 +000023#include "system.h"
Jeff Law956d6951997-12-06 17:31:01 -070024#include "cpplib.h"
Paolo Bonzini4f4e53dd2004-05-24 10:50:45 +000025#include "internal.h"
Zack Weinbergc6e83802004-06-05 20:58:06 +000026#include "mkdeps.h"
Zack Weinbergc71f8352000-07-05 05:33:57 +000027#include "obstack.h"
Jeff Law956d6951997-12-06 17:31:01 -070028
Zack Weinberg88ae23e2000-03-08 23:35:19 +000029/* Stack of conditionals currently in progress
30 (including both successful and failing conditionals). */
Zack Weinberg88ae23e2000-03-08 23:35:19 +000031struct if_stack
32{
33 struct if_stack *next;
Neil Booth50410422001-09-15 10:18:03 +000034 unsigned int line; /* Line where condition started. */
Neil Booth93c803682000-10-28 17:59:06 +000035 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
Neil Boothcef0d192001-07-26 06:02:47 +000036 bool skip_elses; /* Can future #else / #elif be skipped? */
37 bool was_skipping; /* If were skipping on entry. */
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000038 int type; /* Most recent conditional for diagnostics. */
Zack Weinberg88ae23e2000-03-08 23:35:19 +000039};
Zack Weinberg88ae23e2000-03-08 23:35:19 +000040
Neil Bootha5da89c2001-10-14 17:44:00 +000041/* Contains a registered pragma or pragma namespace. */
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000042typedef void (*pragma_cb) (cpp_reader *);
Neil Bootha5da89c2001-10-14 17:44:00 +000043struct pragma_entry
44{
45 struct pragma_entry *next;
Neil Booth4b115ff2001-10-14 23:04:13 +000046 const cpp_hashnode *pragma; /* Name and length. */
Neil Bootha5da89c2001-10-14 17:44:00 +000047 int is_nspace;
48 union {
49 pragma_cb handler;
50 struct pragma_entry *space;
51 } u;
52};
53
Neil Booth93c803682000-10-28 17:59:06 +000054/* Values for the origin field of struct directive. KANDR directives
55 come from traditional (K&R) C. STDC89 directives come from the
56 1989 C standard. EXTENSION directives are extensions. */
57#define KANDR 0
58#define STDC89 1
59#define EXTENSION 2
60
61/* Values for the flags field of struct directive. COND indicates a
62 conditional; IF_COND an opening conditional. INCL means to treat
63 "..." and <...> as q-char and h-char sequences respectively. IN_I
64 means this directive should be handled even if -fpreprocessed is in
Neil Booth1a769162002-06-11 05:36:17 +000065 effect (these are the directives with callback hooks).
66
Neil Boothd97371e2002-06-18 06:27:40 +000067 EXPAND is set on directives that are always macro-expanded. */
Neil Booth93c803682000-10-28 17:59:06 +000068#define COND (1 << 0)
69#define IF_COND (1 << 1)
70#define INCL (1 << 2)
71#define IN_I (1 << 3)
Neil Booth1a769162002-06-11 05:36:17 +000072#define EXPAND (1 << 4)
Neil Booth93c803682000-10-28 17:59:06 +000073
74/* Defines one #-directive, including how to handle it. */
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000075typedef void (*directive_handler) (cpp_reader *);
Neil Booth93c803682000-10-28 17:59:06 +000076typedef struct directive directive;
77struct directive
78{
79 directive_handler handler; /* Function to handle directive. */
Neil Booth562a5c22002-04-21 18:46:42 +000080 const uchar *name; /* Name of directive. */
Neil Booth93c803682000-10-28 17:59:06 +000081 unsigned short length; /* Length of name. */
82 unsigned char origin; /* Origin of directive. */
83 unsigned char flags; /* Flags describing this directive. */
84};
85
Zack Weinberg1316f1f2000-02-06 07:53:50 +000086/* Forward declarations. */
87
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000088static void skip_rest_of_line (cpp_reader *);
89static void check_eol (cpp_reader *);
90static void start_directive (cpp_reader *);
91static void prepare_directive_trad (cpp_reader *);
92static void end_directive (cpp_reader *, int);
93static void directive_diagnostics (cpp_reader *, const directive *, int);
94static void run_directive (cpp_reader *, int, const char *, size_t);
95static char *glue_header_name (cpp_reader *);
96static const char *parse_include (cpp_reader *, int *);
97static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
98static unsigned int read_flag (cpp_reader *, unsigned int);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000099static int strtoul_for_line (const uchar *, unsigned int, unsigned long *);
100static void do_diagnostic (cpp_reader *, int, int);
101static cpp_hashnode *lex_macro_node (cpp_reader *);
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000102static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000103static void do_include_common (cpp_reader *, enum include_type);
104static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
105 const cpp_hashnode *);
106static struct pragma_entry *insert_pragma_entry (cpp_reader *,
107 struct pragma_entry **,
108 const cpp_hashnode *,
109 pragma_cb);
110static int count_registered_pragmas (struct pragma_entry *);
111static char ** save_registered_pragmas (struct pragma_entry *, char **);
112static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
113 char **);
114static void do_pragma_once (cpp_reader *);
115static void do_pragma_poison (cpp_reader *);
116static void do_pragma_system_header (cpp_reader *);
117static void do_pragma_dependency (cpp_reader *);
118static void do_linemarker (cpp_reader *);
119static const cpp_token *get_token_no_padding (cpp_reader *);
120static const cpp_token *get__Pragma_string (cpp_reader *);
121static void destringize_and_run (cpp_reader *, const cpp_string *);
122static int parse_answer (cpp_reader *, struct answer **, int);
123static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
124static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
125static void handle_assertion (cpp_reader *, const char *, int);
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +0000126
Zack Weinberg168d3732000-03-14 06:34:11 +0000127/* This is the table of directive handlers. It is ordered by
128 frequency of occurrence; the numbers at the end are directive
129 counts from all the source code I have lying around (egcs and libc
130 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
Neil Booth93c803682000-10-28 17:59:06 +0000131 pcmcia-cs-3.0.9). This is no longer important as directive lookup
132 is now O(1). All extensions other than #warning and #include_next
133 are deprecated. The name is where the extension appears to have
134 come from. */
Jeff Lawe9a25f71997-11-02 14:19:36 -0700135
Neil Booth93c803682000-10-28 17:59:06 +0000136#define DIRECTIVE_TABLE \
137D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000138D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000139D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
140D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
Neil Booth1a769162002-06-11 05:36:17 +0000141D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000142D(else, T_ELSE, KANDR, COND) /* 9863 */ \
143D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
144D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
Neil Booth1a769162002-06-11 05:36:17 +0000145D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
146D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000147D(error, T_ERROR, STDC89, 0) /* 475 */ \
148D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
149D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000150D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000151D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000152D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
Neil Booth93c803682000-10-28 17:59:06 +0000153D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
154D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
Neil Booth74d06cf2002-07-17 21:31:42 +0000155D(sccs, T_SCCS, EXTENSION, 0) /* 0 SVR4? */
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000156
Zack Weinberg168d3732000-03-14 06:34:11 +0000157/* Use the table to generate a series of prototypes, an enum for the
158 directive names, and an array of directive handlers. */
159
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000160#define D(name, t, o, f) static void do_##name (cpp_reader *);
Zack Weinberg168d3732000-03-14 06:34:11 +0000161DIRECTIVE_TABLE
162#undef D
163
Zack Weinberg041c3192000-07-04 01:58:21 +0000164#define D(n, tag, o, f) tag,
Zack Weinberg168d3732000-03-14 06:34:11 +0000165enum
166{
167 DIRECTIVE_TABLE
168 N_DIRECTIVES
Per Bothner7f2935c1995-03-16 13:59:07 -0800169};
Zack Weinberg168d3732000-03-14 06:34:11 +0000170#undef D
171
Zack Weinberg041c3192000-07-04 01:58:21 +0000172#define D(name, t, origin, flags) \
Kaveh R. Ghazi9a238582003-06-16 19:14:22 +0000173{ do_##name, (const uchar *) #name, \
174 sizeof #name - 1, origin, flags },
Neil Booth93c803682000-10-28 17:59:06 +0000175static const directive dtable[] =
Zack Weinberg168d3732000-03-14 06:34:11 +0000176{
177DIRECTIVE_TABLE
178};
179#undef D
180#undef DIRECTIVE_TABLE
Per Bothner7f2935c1995-03-16 13:59:07 -0800181
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000182/* Wrapper struct directive for linemarkers.
183 The origin is more or less true - the original K+R cpp
184 did use this notation in its preprocessed output. */
185static const directive linemarker_dir =
186{
187 do_linemarker, U"#", 1, KANDR, IN_I
188};
189
Neil Booth1a769162002-06-11 05:36:17 +0000190#define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
Neil Booth67821e32001-08-05 17:31:25 +0000191
Neil Booth93c803682000-10-28 17:59:06 +0000192/* Skip any remaining tokens in a directive. */
193static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000194skip_rest_of_line (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +0000195{
Neil Booth93c803682000-10-28 17:59:06 +0000196 /* Discard all stacked contexts. */
Neil Booth8128ccc2002-11-18 20:43:40 +0000197 while (pfile->context->prev)
Neil Booth93c803682000-10-28 17:59:06 +0000198 _cpp_pop_context (pfile);
199
Neil Boothb528a072000-11-12 11:46:21 +0000200 /* Sweep up all tokens remaining on the line. */
Neil Booth345894b2001-09-16 13:44:29 +0000201 if (! SEEN_EOL ())
202 while (_cpp_lex_token (pfile)->type != CPP_EOF)
203 ;
Neil Booth93c803682000-10-28 17:59:06 +0000204}
205
206/* Ensure there are no stray tokens at the end of a directive. */
207static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000208check_eol (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +0000209{
Neil Booth345894b2001-09-16 13:44:29 +0000210 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000211 cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
Neil Boothebef4e82002-04-14 18:42:47 +0000212 pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +0000213}
214
Neil Boothfe6c2db2000-11-15 19:25:22 +0000215/* Called when entering a directive, _Pragma or command-line directive. */
216static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000217start_directive (cpp_reader *pfile)
Zack Weinbergc5a04732000-04-25 19:32:36 +0000218{
Neil Booth7f2f1a62000-11-14 18:32:06 +0000219 /* Setup in-directive state. */
220 pfile->state.in_directive = 1;
221 pfile->state.save_comments = 0;
222
Neil Booth93c803682000-10-28 17:59:06 +0000223 /* Some handlers need the position of the # for diagnostics. */
Per Bothner500bee02004-04-22 19:22:27 -0700224 pfile->directive_line = pfile->line_table->highest_line;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000225}
226
227/* Called when leaving a directive, _Pragma or command-line directive. */
228static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000229end_directive (cpp_reader *pfile, int skip_line)
Neil Boothfe6c2db2000-11-15 19:25:22 +0000230{
Neil Booth1a769162002-06-11 05:36:17 +0000231 if (CPP_OPTION (pfile, traditional))
232 {
Neil Boothd97371e2002-06-18 06:27:40 +0000233 /* Revert change of prepare_directive_trad. */
234 pfile->state.prevent_expansion--;
235
Neil Boothb66377c2002-06-13 21:16:00 +0000236 if (pfile->directive != &dtable[T_DEFINE])
Neil Booth1a769162002-06-11 05:36:17 +0000237 _cpp_remove_overlay (pfile);
238 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000239 /* We don't skip for an assembler #. */
Neil Boothb66377c2002-06-13 21:16:00 +0000240 else if (skip_line)
Neil Booth67821e32001-08-05 17:31:25 +0000241 {
242 skip_rest_of_line (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +0000243 if (!pfile->keep_tokens)
244 {
245 pfile->cur_run = &pfile->base_run;
246 pfile->cur_token = pfile->base_run.base;
247 }
Neil Booth67821e32001-08-05 17:31:25 +0000248 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000249
250 /* Restore state. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000251 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
252 pfile->state.in_directive = 0;
Neil Boothd97371e2002-06-18 06:27:40 +0000253 pfile->state.in_expression = 0;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000254 pfile->state.angled_headers = 0;
255 pfile->directive = 0;
256}
257
Neil Booth1a769162002-06-11 05:36:17 +0000258/* Prepare to handle the directive in pfile->directive. */
259static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000260prepare_directive_trad (cpp_reader *pfile)
Neil Booth1a769162002-06-11 05:36:17 +0000261{
Neil Booth951a0762002-06-27 06:01:58 +0000262 if (pfile->directive != &dtable[T_DEFINE])
Neil Booth1a769162002-06-11 05:36:17 +0000263 {
Neil Boothb66377c2002-06-13 21:16:00 +0000264 bool no_expand = (pfile->directive
265 && ! (pfile->directive->flags & EXPAND));
Neil Booth974c43f2002-06-13 06:25:28 +0000266 bool was_skipping = pfile->state.skipping;
Neil Booth1a769162002-06-11 05:36:17 +0000267
Neil Boothd97371e2002-06-18 06:27:40 +0000268 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
269 || pfile->directive == &dtable[T_ELIF]);
Neil Booth45f24922003-12-12 07:00:29 +0000270 if (pfile->state.in_expression)
271 pfile->state.skipping = false;
272
Neil Booth1a769162002-06-11 05:36:17 +0000273 if (no_expand)
274 pfile->state.prevent_expansion++;
Zack Weinberg43839642003-07-13 17:34:18 +0000275 _cpp_scan_out_logical_line (pfile, NULL);
Neil Booth1a769162002-06-11 05:36:17 +0000276 if (no_expand)
277 pfile->state.prevent_expansion--;
Neil Booth45f24922003-12-12 07:00:29 +0000278
Neil Booth974c43f2002-06-13 06:25:28 +0000279 pfile->state.skipping = was_skipping;
Neil Booth1a769162002-06-11 05:36:17 +0000280 _cpp_overlay_buffer (pfile, pfile->out.base,
281 pfile->out.cur - pfile->out.base);
282 }
Neil Boothd97371e2002-06-18 06:27:40 +0000283
284 /* Stop ISO C from expanding anything. */
285 pfile->state.prevent_expansion++;
Neil Booth1a769162002-06-11 05:36:17 +0000286}
287
Kazu Hiratada7d8302002-09-22 02:03:17 +0000288/* Output diagnostics for a directive DIR. INDENTED is nonzero if
Neil Booth18a9d8f2001-09-16 11:23:56 +0000289 the '#' was indented. */
Neil Booth18a9d8f2001-09-16 11:23:56 +0000290static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000291directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000292{
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000293 /* Issue -pedantic warnings for extensions. */
294 if (CPP_PEDANTIC (pfile)
295 && ! pfile->state.skipping
296 && dir->origin == EXTENSION)
John David Anglin0527bc42003-11-01 22:56:54 +0000297 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000298
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000299 /* Traditionally, a directive is ignored unless its # is in
300 column 1. Therefore in code intended to work with K+R
301 compilers, directives added by C89 must have their #
302 indented, and directives present in traditional C must not.
303 This is true even of directives in skipped conditional
304 blocks. #elif cannot be used at all. */
305 if (CPP_WTRADITIONAL (pfile))
306 {
307 if (dir == &dtable[T_ELIF])
John David Anglin0527bc42003-11-01 22:56:54 +0000308 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000309 "suggest not using #elif in traditional C");
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000310 else if (indented && dir->origin == KANDR)
John David Anglin0527bc42003-11-01 22:56:54 +0000311 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000312 "traditional C ignores #%s with the # indented",
313 dir->name);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000314 else if (!indented && dir->origin != KANDR)
John David Anglin0527bc42003-11-01 22:56:54 +0000315 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000316 "suggest hiding #%s from traditional C with an indented #",
317 dir->name);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000318 }
319}
320
Kazu Hiratada7d8302002-09-22 02:03:17 +0000321/* Check if we have a known directive. INDENTED is nonzero if the
Neil Booth18a9d8f2001-09-16 11:23:56 +0000322 '#' of the directive was indented. This function is in this file
323 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +0000324 nonzero if the line of tokens has been handled, zero if we should
Neil Booth18a9d8f2001-09-16 11:23:56 +0000325 continue processing the line. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000326int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000327_cpp_handle_directive (cpp_reader *pfile, int indented)
Neil Boothfe6c2db2000-11-15 19:25:22 +0000328{
Neil Boothfe6c2db2000-11-15 19:25:22 +0000329 const directive *dir = 0;
Neil Booth345894b2001-09-16 13:44:29 +0000330 const cpp_token *dname;
Neil Boothe808ec92002-02-27 07:24:53 +0000331 bool was_parsing_args = pfile->state.parsing_args;
Zack Weinbergc6e83802004-06-05 20:58:06 +0000332 bool was_discarding_output = pfile->state.discarding_output;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000333 int skip = 1;
334
Zack Weinbergc6e83802004-06-05 20:58:06 +0000335 if (was_discarding_output)
336 pfile->state.prevent_expansion = 0;
337
Neil Boothe808ec92002-02-27 07:24:53 +0000338 if (was_parsing_args)
339 {
340 if (CPP_OPTION (pfile, pedantic))
John David Anglin0527bc42003-11-01 22:56:54 +0000341 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothe808ec92002-02-27 07:24:53 +0000342 "embedding a directive within macro arguments is not portable");
343 pfile->state.parsing_args = 0;
344 pfile->state.prevent_expansion = 0;
345 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000346 start_directive (pfile);
Neil Booth345894b2001-09-16 13:44:29 +0000347 dname = _cpp_lex_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000348
Neil Booth345894b2001-09-16 13:44:29 +0000349 if (dname->type == CPP_NAME)
Neil Booth0d9f2342000-09-18 18:43:05 +0000350 {
Zack Weinberg4977bab2002-12-16 18:23:00 +0000351 if (dname->val.node->is_directive)
352 dir = &dtable[dname->val.node->directive_index];
Neil Booth93c803682000-10-28 17:59:06 +0000353 }
Kazu Hirata05713b82002-09-15 18:24:08 +0000354 /* We do not recognize the # followed by a number extension in
Neil Booth18a9d8f2001-09-16 11:23:56 +0000355 assembler code. */
Neil Booth345894b2001-09-16 13:44:29 +0000356 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth93c803682000-10-28 17:59:06 +0000357 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000358 dir = &linemarker_dir;
359 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
360 && ! pfile->state.skipping)
John David Anglin0527bc42003-11-01 22:56:54 +0000361 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothebef4e82002-04-14 18:42:47 +0000362 "style of line directive is a GCC extension");
Neil Booth0d9f2342000-09-18 18:43:05 +0000363 }
364
Neil Booth93c803682000-10-28 17:59:06 +0000365 if (dir)
Neil Booth0d9f2342000-09-18 18:43:05 +0000366 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000367 /* If we have a directive that is not an opening conditional,
368 invalidate any control macro. */
369 if (! (dir->flags & IF_COND))
370 pfile->mi_valid = false;
Neil Booth93c803682000-10-28 17:59:06 +0000371
Neil Booth18a9d8f2001-09-16 11:23:56 +0000372 /* Kluge alert. In order to be sure that code like this
373
374 #define HASH #
375 HASH define foo bar
376
377 does not cause '#define foo bar' to get executed when
378 compiled with -save-temps, we recognize directives in
379 -fpreprocessed mode only if the # is in column 1. cppmacro.c
Joseph Myersa1f300c2001-11-23 02:05:19 +0000380 puts a space in front of any '#' at the start of a macro. */
Neil Booth18a9d8f2001-09-16 11:23:56 +0000381 if (CPP_OPTION (pfile, preprocessed)
382 && (indented || !(dir->flags & IN_I)))
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000383 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000384 skip = 0;
385 dir = 0;
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000386 }
387 else
Neil Booth93c803682000-10-28 17:59:06 +0000388 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000389 /* In failed conditional groups, all non-conditional
390 directives are ignored. Before doing that, whether
391 skipping or not, we should lex angle-bracketed headers
392 correctly, and maybe output some diagnostics. */
393 pfile->state.angled_headers = dir->flags & INCL;
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000394 pfile->state.directive_wants_padding = dir->flags & INCL;
Neil Booth18a9d8f2001-09-16 11:23:56 +0000395 if (! CPP_OPTION (pfile, preprocessed))
396 directive_diagnostics (pfile, dir, indented);
397 if (pfile->state.skipping && !(dir->flags & COND))
398 dir = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000399 }
400 }
Neil Booth345894b2001-09-16 13:44:29 +0000401 else if (dname->type == CPP_EOF)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000402 ; /* CPP_EOF is the "null directive". */
403 else
Neil Booth0d9f2342000-09-18 18:43:05 +0000404 {
Neil Booth93c803682000-10-28 17:59:06 +0000405 /* An unknown directive. Don't complain about it in assembly
406 source: we don't know where the comments are, and # may
407 introduce assembler pseudo-ops. Don't complain about invalid
408 directives in skipped conditional groups (6.10 p4). */
Neil Boothbdb05a72000-11-26 17:31:13 +0000409 if (CPP_OPTION (pfile, lang) == CLK_ASM)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000410 skip = 0;
411 else if (!pfile->state.skipping)
John David Anglin0527bc42003-11-01 22:56:54 +0000412 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
Neil Booth345894b2001-09-16 13:44:29 +0000413 cpp_token_as_text (pfile, dname));
Neil Booth0d9f2342000-09-18 18:43:05 +0000414 }
415
Neil Boothd1a58682002-06-28 06:26:54 +0000416 pfile->directive = dir;
417 if (CPP_OPTION (pfile, traditional))
418 prepare_directive_trad (pfile);
419
Neil Booth18a9d8f2001-09-16 11:23:56 +0000420 if (dir)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000421 pfile->directive->handler (pfile);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000422 else if (skip == 0)
423 _cpp_backup_tokens (pfile, 1);
424
425 end_directive (pfile, skip);
Neil Boothe808ec92002-02-27 07:24:53 +0000426 if (was_parsing_args)
427 {
428 /* Restore state when within macro args. */
429 pfile->state.parsing_args = 2;
430 pfile->state.prevent_expansion = 1;
Neil Boothe808ec92002-02-27 07:24:53 +0000431 }
Zack Weinbergc6e83802004-06-05 20:58:06 +0000432 if (was_discarding_output)
433 pfile->state.prevent_expansion = 1;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000434 return skip;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000435}
436
Neil Booth93c803682000-10-28 17:59:06 +0000437/* Directive handler wrapper used by the command line option
Neil Booth26aea072003-04-19 00:22:51 +0000438 processor. BUF is \n terminated. */
Neil Booth93c803682000-10-28 17:59:06 +0000439static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000440run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000441{
Neil Booth562a5c22002-04-21 18:46:42 +0000442 cpp_push_buffer (pfile, (const uchar *) buf, count,
Per Bothner40de9f72003-10-02 07:30:34 +0000443 /* from_stage3 */ true);
Neil Booth8bfb1462002-08-14 20:17:55 +0000444 /* Disgusting hack. */
445 if (dir_no == T_PRAGMA)
Neil Booth8f9b4002003-07-29 22:26:13 +0000446 pfile->buffer->file = pfile->buffer->prev->file;
Neil Booth0bda4762000-12-11 07:45:16 +0000447 start_directive (pfile);
Neil Booth26aea072003-04-19 00:22:51 +0000448
449 /* This is a short-term fix to prevent a leading '#' being
450 interpreted as a directive. */
451 _cpp_clean_line (pfile);
452
Neil Boothf71aebb2001-05-27 18:06:00 +0000453 pfile->directive = &dtable[dir_no];
Neil Booth1a769162002-06-11 05:36:17 +0000454 if (CPP_OPTION (pfile, traditional))
455 prepare_directive_trad (pfile);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000456 pfile->directive->handler (pfile);
Neil Booth0bda4762000-12-11 07:45:16 +0000457 end_directive (pfile, 1);
Neil Booth8bfb1462002-08-14 20:17:55 +0000458 if (dir_no == T_PRAGMA)
Neil Booth8f9b4002003-07-29 22:26:13 +0000459 pfile->buffer->file = NULL;
Neil Boothef6e9582001-08-04 12:01:59 +0000460 _cpp_pop_buffer (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000461}
Per Bothner7f2935c1995-03-16 13:59:07 -0800462
Neil Booth93c803682000-10-28 17:59:06 +0000463/* Checks for validity the macro name in #define, #undef, #ifdef and
464 #ifndef directives. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000465static cpp_hashnode *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000466lex_macro_node (cpp_reader *pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000467{
Neil Booth1a769162002-06-11 05:36:17 +0000468 const cpp_token *token = _cpp_lex_token (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000469
Zack Weinberg92936ec2000-07-19 20:18:08 +0000470 /* The token immediately after #define must be an identifier. That
Zack Weinbergb8363a22001-07-01 18:48:13 +0000471 identifier may not be "defined", per C99 6.10.8p4.
472 In C++, it may not be any of the "named operators" either,
473 per C++98 [lex.digraph], [lex.key].
474 Finally, the identifier may not have been poisoned. (In that case
Neil Booth1d63a282002-06-28 20:27:14 +0000475 the lexer has issued the error message for us.) */
Zack Weinbergb8363a22001-07-01 18:48:13 +0000476
Neil Booth1a769162002-06-11 05:36:17 +0000477 if (token->type == CPP_NAME)
478 {
479 cpp_hashnode *node = token->val.node;
480
481 if (node == pfile->spec_nodes.n_defined)
John David Anglin0527bc42003-11-01 22:56:54 +0000482 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth1a769162002-06-11 05:36:17 +0000483 "\"defined\" cannot be used as a macro name");
484 else if (! (node->flags & NODE_POISONED))
485 return node;
486 }
487 else if (token->flags & NAMED_OP)
John David Anglin0527bc42003-11-01 22:56:54 +0000488 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcbc69f82002-06-05 20:27:12 +0000489 "\"%s\" cannot be used as a macro name as it is an operator in C++",
Neil Booth1a769162002-06-11 05:36:17 +0000490 NODE_NAME (token->val.node));
491 else if (token->type == CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000492 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
Neil Booth1a769162002-06-11 05:36:17 +0000493 pfile->directive->name);
494 else
John David Anglin0527bc42003-11-01 22:56:54 +0000495 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
Zack Weinbergb8363a22001-07-01 18:48:13 +0000496
Neil Boothcbc69f82002-06-05 20:27:12 +0000497 return NULL;
Per Bothner7f2935c1995-03-16 13:59:07 -0800498}
Per Bothner7f2935c1995-03-16 13:59:07 -0800499
Neil Booth93c803682000-10-28 17:59:06 +0000500/* Process a #define directive. Most work is done in cppmacro.c. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000501static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000502do_define (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800503{
Neil Booth93c803682000-10-28 17:59:06 +0000504 cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000505
Neil Booth93c803682000-10-28 17:59:06 +0000506 if (node)
507 {
Neil Booth1d63a282002-06-28 20:27:14 +0000508 /* If we have been requested to expand comments into macros,
509 then re-enable saving of comments. */
510 pfile->state.save_comments =
511 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
512
Neil Booth93c803682000-10-28 17:59:06 +0000513 if (_cpp_create_definition (pfile, node))
514 if (pfile->cb.define)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000515 pfile->cb.define (pfile, pfile->directive_line, node);
Neil Booth93c803682000-10-28 17:59:06 +0000516 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800517}
518
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000519/* Handle #undef. Mark the identifier NT_VOID in the hash table. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000520static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000521do_undef (cpp_reader *pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000522{
Kazu Hiratadf383482002-05-22 22:02:16 +0000523 cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000524
Neil Booth45f24922003-12-12 07:00:29 +0000525 if (node)
Zack Weinberg041c3192000-07-04 01:58:21 +0000526 {
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000527 if (pfile->cb.undef)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000528 pfile->cb.undef (pfile, pfile->directive_line, node);
Zack Weinberg041c3192000-07-04 01:58:21 +0000529
Neil Booth45f24922003-12-12 07:00:29 +0000530 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
531 identifier is not currently defined as a macro name. */
532 if (node->type == NT_MACRO)
533 {
534 if (node->flags & NODE_WARN)
535 cpp_error (pfile, CPP_DL_WARNING,
536 "undefining \"%s\"", NODE_NAME (node));
Zack Weinberg041c3192000-07-04 01:58:21 +0000537
Neil Booth45f24922003-12-12 07:00:29 +0000538 if (CPP_OPTION (pfile, warn_unused_macros))
539 _cpp_warn_if_unused_macro (pfile, node, NULL);
Neil Bootha69cbaa2002-07-23 22:57:49 +0000540
Neil Booth45f24922003-12-12 07:00:29 +0000541 _cpp_free_definition (node);
542 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000543 }
Neil Booth45f24922003-12-12 07:00:29 +0000544
Neil Booth93c803682000-10-28 17:59:06 +0000545 check_eol (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000546}
547
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000548/* Undefine a single macro/assertion/whatever. */
549
550static int
Zack Weinbergc6e83802004-06-05 20:58:06 +0000551undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000552 void *data_p ATTRIBUTE_UNUSED)
553{
Zack Weinbergc6e83802004-06-05 20:58:06 +0000554 /* Body of _cpp_free_definition inlined here for speed.
555 Macros and assertions no longer have anything to free. */
556 h->type = NT_VOID;
557 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED);
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000558 return 1;
559}
560
561/* Undefine all macros and assertions. */
562
563void
564cpp_undef_all (cpp_reader *pfile)
565{
566 cpp_forall_identifiers (pfile, undefine_macros, NULL);
567}
568
569
Neil Booth93c803682000-10-28 17:59:06 +0000570/* Helper routine used by parse_include. Reinterpret the current line
571 as an h-char-sequence (< ... >); we are looking at the first token
Neil Booth74eb4b32003-04-21 19:21:59 +0000572 after the <. Returns a malloced filename. */
573static char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000574glue_header_name (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800575{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000576 const cpp_token *token;
Neil Booth74eb4b32003-04-21 19:21:59 +0000577 char *buffer;
Neil Booth2450e0b2002-02-23 20:21:39 +0000578 size_t len, total_len = 0, capacity = 1024;
Per Bothner7f2935c1995-03-16 13:59:07 -0800579
Neil Booth93c803682000-10-28 17:59:06 +0000580 /* To avoid lexed tokens overwriting our glued name, we can only
581 allocate from the string pool once we've lexed everything. */
Neil Booth74eb4b32003-04-21 19:21:59 +0000582 buffer = xmalloc (capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000583 for (;;)
Zack Weinberg168d3732000-03-14 06:34:11 +0000584 {
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000585 token = get_token_no_padding (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000586
Neil Booth74eb4b32003-04-21 19:21:59 +0000587 if (token->type == CPP_GREATER)
Neil Booth93c803682000-10-28 17:59:06 +0000588 break;
Neil Booth74eb4b32003-04-21 19:21:59 +0000589 if (token->type == CPP_EOF)
590 {
John David Anglin0527bc42003-11-01 22:56:54 +0000591 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
Neil Booth74eb4b32003-04-21 19:21:59 +0000592 break;
593 }
Neil Booth93c803682000-10-28 17:59:06 +0000594
Neil Booth59325652003-04-24 20:03:57 +0000595 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
Neil Booth2450e0b2002-02-23 20:21:39 +0000596 if (total_len + len > capacity)
Neil Booth93c803682000-10-28 17:59:06 +0000597 {
Neil Booth2450e0b2002-02-23 20:21:39 +0000598 capacity = (capacity + len) * 2;
Neil Booth74eb4b32003-04-21 19:21:59 +0000599 buffer = xrealloc (buffer, capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000600 }
601
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000602 if (token->flags & PREV_WHITE)
Neil Booth2450e0b2002-02-23 20:21:39 +0000603 buffer[total_len++] = ' ';
Neil Booth93c803682000-10-28 17:59:06 +0000604
Neil Booth74eb4b32003-04-21 19:21:59 +0000605 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len])
606 - (uchar *) buffer);
Neil Booth93c803682000-10-28 17:59:06 +0000607 }
608
Neil Booth74eb4b32003-04-21 19:21:59 +0000609 buffer[total_len] = '\0';
610 return buffer;
Neil Booth93c803682000-10-28 17:59:06 +0000611}
612
Neil Booth74eb4b32003-04-21 19:21:59 +0000613/* Returns the file name of #include, #include_next, #import and
614 #pragma dependency. The string is malloced and the caller should
615 free it. Returns NULL on error. */
616static const char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000617parse_include (cpp_reader *pfile, int *pangle_brackets)
Neil Booth93c803682000-10-28 17:59:06 +0000618{
Neil Booth74eb4b32003-04-21 19:21:59 +0000619 char *fname;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000620 const cpp_token *header;
Neil Booth93c803682000-10-28 17:59:06 +0000621
Neil Booth93c803682000-10-28 17:59:06 +0000622 /* Allow macro expansion. */
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000623 header = get_token_no_padding (pfile);
Neil Booth74eb4b32003-04-21 19:21:59 +0000624 if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
Neil Booth93c803682000-10-28 17:59:06 +0000625 {
Neil Booth6338b352003-04-23 22:44:06 +0000626 fname = xmalloc (header->val.str.len - 1);
627 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
628 fname[header->val.str.len - 2] = '\0';
Neil Booth74eb4b32003-04-21 19:21:59 +0000629 *pangle_brackets = header->type == CPP_HEADER_NAME;
Zack Weinberg041c3192000-07-04 01:58:21 +0000630 }
Neil Booth74eb4b32003-04-21 19:21:59 +0000631 else if (header->type == CPP_LESS)
Zack Weinberg041c3192000-07-04 01:58:21 +0000632 {
Neil Booth74eb4b32003-04-21 19:21:59 +0000633 fname = glue_header_name (pfile);
634 *pangle_brackets = 1;
635 }
636 else
637 {
638 const unsigned char *dir;
639
640 if (pfile->directive == &dtable[T_PRAGMA])
641 dir = U"pragma dependency";
642 else
643 dir = pfile->directive->name;
John David Anglin0527bc42003-11-01 22:56:54 +0000644 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
Neil Booth74eb4b32003-04-21 19:21:59 +0000645 dir);
646
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000647 return NULL;
Richard Kennercfb3ee11997-04-13 14:30:13 -0400648 }
649
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000650 check_eol (pfile);
Neil Booth74eb4b32003-04-21 19:21:59 +0000651 return fname;
Zack Weinberg168d3732000-03-14 06:34:11 +0000652}
653
Neil Boothba133c92001-03-15 07:57:13 +0000654/* Handle #include, #include_next and #import. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000655static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000656do_include_common (cpp_reader *pfile, enum include_type type)
Zack Weinberg168d3732000-03-14 06:34:11 +0000657{
Neil Booth74eb4b32003-04-21 19:21:59 +0000658 const char *fname;
659 int angle_brackets;
660
661 fname = parse_include (pfile, &angle_brackets);
662 if (!fname)
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000663 return;
Zack Weinberg168d3732000-03-14 06:34:11 +0000664
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000665 /* Prevent #include recursion. */
Per Bothner50f59cd2004-01-19 21:30:18 -0800666 if (pfile->line_table->depth >= CPP_STACK_MAX)
John David Anglin0527bc42003-11-01 22:56:54 +0000667 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
Neil Booth74eb4b32003-04-21 19:21:59 +0000668 else
Neil Booth09b82252001-07-29 22:27:20 +0000669 {
Neil Booth74eb4b32003-04-21 19:21:59 +0000670 /* Get out of macro context, if we are. */
671 skip_rest_of_line (pfile);
672
673 if (pfile->cb.include)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000674 pfile->cb.include (pfile, pfile->directive_line,
675 pfile->directive->name, fname, angle_brackets);
Neil Booth74eb4b32003-04-21 19:21:59 +0000676
Neil Booth8f9b4002003-07-29 22:26:13 +0000677 _cpp_stack_include (pfile, fname, angle_brackets, type);
Neil Booth09b82252001-07-29 22:27:20 +0000678 }
679
Kaveh R. Ghazifad205f2003-06-16 21:41:10 +0000680 free ((void *) fname);
Neil Boothba133c92001-03-15 07:57:13 +0000681}
682
683static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000684do_include (cpp_reader *pfile)
Neil Boothba133c92001-03-15 07:57:13 +0000685{
686 do_include_common (pfile, IT_INCLUDE);
Zack Weinberg168d3732000-03-14 06:34:11 +0000687}
688
Zack Weinberg711b8822000-07-18 00:59:49 +0000689static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000690do_import (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +0000691{
Neil Boothba133c92001-03-15 07:57:13 +0000692 do_include_common (pfile, IT_IMPORT);
Zack Weinberg168d3732000-03-14 06:34:11 +0000693}
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000694
Zack Weinberg711b8822000-07-18 00:59:49 +0000695static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000696do_include_next (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +0000697{
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000698 enum include_type type = IT_INCLUDE_NEXT;
699
700 /* If this is the primary source file, warn and use the normal
701 search logic. */
702 if (! pfile->buffer->prev)
703 {
John David Anglin0527bc42003-11-01 22:56:54 +0000704 cpp_error (pfile, CPP_DL_WARNING,
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000705 "#include_next in primary source file");
706 type = IT_INCLUDE;
707 }
708 do_include_common (pfile, type);
Per Bothner7f2935c1995-03-16 13:59:07 -0800709}
710
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000711/* Subroutine of do_linemarker. Read possible flags after file name.
712 LAST is the last flag seen; 0 if this is the first flag. Return the
713 flag if it is valid, 0 at the end of the directive. Otherwise
714 complain. */
Neil Booth642ce432000-12-07 23:17:56 +0000715static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000716read_flag (cpp_reader *pfile, unsigned int last)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000717{
Neil Booth345894b2001-09-16 13:44:29 +0000718 const cpp_token *token = _cpp_lex_token (pfile);
Jason Merrilld3a34a01999-08-14 00:42:07 +0000719
Neil Booth345894b2001-09-16 13:44:29 +0000720 if (token->type == CPP_NUMBER && token->val.str.len == 1)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000721 {
Neil Booth345894b2001-09-16 13:44:29 +0000722 unsigned int flag = token->val.str.text[0] - '0';
Neil Booth28e0f042000-12-09 12:06:37 +0000723
724 if (flag > last && flag <= 4
725 && (flag != 4 || last == 3)
726 && (flag != 2 || last == 0))
727 return flag;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000728 }
Neil Booth93c803682000-10-28 17:59:06 +0000729
Neil Booth345894b2001-09-16 13:44:29 +0000730 if (token->type != CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000731 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
Neil Booth345894b2001-09-16 13:44:29 +0000732 cpp_token_as_text (pfile, token));
Neil Booth93c803682000-10-28 17:59:06 +0000733 return 0;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000734}
735
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000736/* Subroutine of do_line and do_linemarker. Convert a number in STR,
737 of length LEN, to binary; store it in NUMP, and return 0 if the
738 number was well-formed, 1 if not. Temporary, hopefully. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000739static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000740strtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump)
Zack Weinberg041c3192000-07-04 01:58:21 +0000741{
742 unsigned long reg = 0;
Neil Booth562a5c22002-04-21 18:46:42 +0000743 uchar c;
Zack Weinberg041c3192000-07-04 01:58:21 +0000744 while (len--)
745 {
746 c = *str++;
747 if (!ISDIGIT (c))
748 return 1;
749 reg *= 10;
750 reg += c - '0';
751 }
752 *nump = reg;
753 return 0;
754}
755
Zack Weinberg5538ada1999-02-04 06:36:54 -0500756/* Interpret #line command.
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000757 Note that the filename string (if any) is a true string constant
758 (escapes are interpreted), unlike in #line. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000759static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000760do_line (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800761{
Per Bothner500bee02004-04-22 19:22:27 -0700762 const struct line_maps *line_table = pfile->line_table;
763 const struct line_map *map = &line_table->maps[line_table->used - 1];
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000764 const cpp_token *token;
Per Bothner12f9df42004-02-11 07:29:30 -0800765 const char *new_file = map->to_file;
Neil Boothbb74c962001-08-17 22:23:49 +0000766 unsigned long new_lineno;
Per Bothner7f2935c1995-03-16 13:59:07 -0800767
Neil Booth27e25642000-11-27 08:00:04 +0000768 /* C99 raised the minimum limit on #line numbers. */
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000769 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
Neil Booth18a9d8f2001-09-16 11:23:56 +0000770
Neil Booth93c803682000-10-28 17:59:06 +0000771 /* #line commands expand macros. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000772 token = cpp_get_token (pfile);
773 if (token->type != CPP_NUMBER
774 || strtoul_for_line (token->val.str.text, token->val.str.len,
775 &new_lineno))
Per Bothner7f2935c1995-03-16 13:59:07 -0800776 {
John David Anglin0527bc42003-11-01 22:56:54 +0000777 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +0000778 "\"%s\" after #line is not a positive integer",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000779 cpp_token_as_text (pfile, token));
Zack Weinberg9ec72912000-08-09 19:41:12 +0000780 return;
Kazu Hiratadf383482002-05-22 22:02:16 +0000781 }
Zack Weinberg5538ada1999-02-04 06:36:54 -0500782
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000783 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
John David Anglin0527bc42003-11-01 22:56:54 +0000784 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
Zack Weinberg5538ada1999-02-04 06:36:54 -0500785
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000786 token = cpp_get_token (pfile);
787 if (token->type == CPP_STRING)
Zack Weinberg5538ada1999-02-04 06:36:54 -0500788 {
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000789 cpp_string s = { 0, 0 };
Eric Christopher423e95e2004-02-12 02:25:03 +0000790 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
791 &s, false))
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000792 new_file = (const char *)s.text;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000793 check_eol (pfile);
794 }
795 else if (token->type != CPP_EOF)
796 {
John David Anglin0527bc42003-11-01 22:56:54 +0000797 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000798 cpp_token_as_text (pfile, token));
799 return;
800 }
Neil Booth93c803682000-10-28 17:59:06 +0000801
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000802 skip_rest_of_line (pfile);
803 _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
Per Bothner12f9df42004-02-11 07:29:30 -0800804 map->sysp);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000805}
806
807/* Interpret the # 44 "file" [flags] notation, which has slightly
808 different syntax and semantics from #line: Flags are allowed,
809 and we never complain about the line number being too big. */
810static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000811do_linemarker (cpp_reader *pfile)
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000812{
Per Bothner500bee02004-04-22 19:22:27 -0700813 const struct line_maps *line_table = pfile->line_table;
814 const struct line_map *map = &line_table->maps[line_table->used - 1];
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000815 const cpp_token *token;
Per Bothner12f9df42004-02-11 07:29:30 -0800816 const char *new_file = map->to_file;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000817 unsigned long new_lineno;
Per Bothner12f9df42004-02-11 07:29:30 -0800818 unsigned int new_sysp = map->sysp;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000819 enum lc_reason reason = LC_RENAME;
820 int flag;
821
822 /* Back up so we can get the number again. Putting this in
823 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
824 some circumstances, which can segfault. */
825 _cpp_backup_tokens (pfile, 1);
826
827 /* #line commands expand macros. */
828 token = cpp_get_token (pfile);
829 if (token->type != CPP_NUMBER
830 || strtoul_for_line (token->val.str.text, token->val.str.len,
831 &new_lineno))
832 {
John David Anglin0527bc42003-11-01 22:56:54 +0000833 cpp_error (pfile, CPP_DL_ERROR,
834 "\"%s\" after # is not a positive integer",
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000835 cpp_token_as_text (pfile, token));
836 return;
Kazu Hiratadf383482002-05-22 22:02:16 +0000837 }
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000838
839 token = cpp_get_token (pfile);
840 if (token->type == CPP_STRING)
841 {
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000842 cpp_string s = { 0, 0 };
Eric Christopher423e95e2004-02-12 02:25:03 +0000843 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
844 1, &s, false))
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000845 new_file = (const char *)s.text;
Eric Christophercf551fb2004-01-16 22:37:49 +0000846
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000847 new_sysp = 0;
848 flag = read_flag (pfile, 0);
849 if (flag == 1)
Neil Booth93c803682000-10-28 17:59:06 +0000850 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000851 reason = LC_ENTER;
852 /* Fake an include for cpp_included (). */
853 _cpp_fake_include (pfile, new_file);
854 flag = read_flag (pfile, flag);
Neil Booth93c803682000-10-28 17:59:06 +0000855 }
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000856 else if (flag == 2)
857 {
858 reason = LC_LEAVE;
859 flag = read_flag (pfile, flag);
860 }
861 if (flag == 3)
862 {
863 new_sysp = 1;
864 flag = read_flag (pfile, flag);
865 if (flag == 4)
866 new_sysp = 2;
Per Bothner12f9df42004-02-11 07:29:30 -0800867 pfile->buffer->sysp = new_sysp;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000868 }
869
Neil Boothfde84342001-08-06 21:07:41 +0000870 check_eol (pfile);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000871 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000872 else if (token->type != CPP_EOF)
Neil Booth27e25642000-11-27 08:00:04 +0000873 {
John David Anglin0527bc42003-11-01 22:56:54 +0000874 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000875 cpp_token_as_text (pfile, token));
Neil Booth27e25642000-11-27 08:00:04 +0000876 return;
877 }
Zack Weinberg941e09b1998-12-15 11:17:06 +0000878
Neil Boothbdcbe492001-09-13 20:05:17 +0000879 skip_rest_of_line (pfile);
Neil Boothbb74c962001-08-17 22:23:49 +0000880 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
Neil Booth27e25642000-11-27 08:00:04 +0000881}
882
Neil Booth67821e32001-08-05 17:31:25 +0000883/* Arrange the file_change callback. pfile->line has changed to
Neil Booth47d89cf2001-08-11 07:33:39 +0000884 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
Joseph Myersa1f300c2001-11-23 02:05:19 +0000885 header, 2 for a system header that needs to be extern "C" protected,
Neil Booth47d89cf2001-08-11 07:33:39 +0000886 and zero otherwise. */
Neil Bootheb1f4d92000-12-18 19:00:26 +0000887void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000888_cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
889 const char *to_file, unsigned int file_line,
890 unsigned int sysp)
Neil Booth27e25642000-11-27 08:00:04 +0000891{
Per Bothner12f9df42004-02-11 07:29:30 -0800892 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
893 to_file, file_line);
Per Bothner500bee02004-04-22 19:22:27 -0700894 if (map != NULL)
895 linemap_line_start (pfile->line_table, map->to_line, 127);
Neil Boothd82fc102001-08-02 23:03:31 +0000896
Neil Bootheb1f4d92000-12-18 19:00:26 +0000897 if (pfile->cb.file_change)
Per Bothner12f9df42004-02-11 07:29:30 -0800898 pfile->cb.file_change (pfile, map);
Per Bothner7f2935c1995-03-16 13:59:07 -0800899}
Zack Weinberg941e09b1998-12-15 11:17:06 +0000900
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000901/* Report a warning or error detected by the program we are
902 processing. Use the directive's tokens in the error message. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000903static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000904do_diagnostic (cpp_reader *pfile, int code, int print_dir)
Per Bothner7f2935c1995-03-16 13:59:07 -0800905{
Per Bothner12f9df42004-02-11 07:29:30 -0800906 if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0))
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000907 {
Neil Booth29b10742000-11-13 18:40:37 +0000908 if (print_dir)
909 fprintf (stderr, "#%s ", pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +0000910 pfile->state.prevent_expansion++;
911 cpp_output_line (pfile, stderr);
912 pfile->state.prevent_expansion--;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000913 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800914}
915
Neil Booth838f3132000-09-24 10:42:09 +0000916static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000917do_error (cpp_reader *pfile)
Neil Booth838f3132000-09-24 10:42:09 +0000918{
John David Anglin0527bc42003-11-01 22:56:54 +0000919 do_diagnostic (pfile, CPP_DL_ERROR, 1);
Neil Booth838f3132000-09-24 10:42:09 +0000920}
Per Bothner7f2935c1995-03-16 13:59:07 -0800921
Zack Weinberg711b8822000-07-18 00:59:49 +0000922static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000923do_warning (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800924{
Neil Booth2f878972001-04-08 10:01:18 +0000925 /* We want #warning diagnostics to be emitted in system headers too. */
John David Anglin0527bc42003-11-01 22:56:54 +0000926 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
Per Bothner7f2935c1995-03-16 13:59:07 -0800927}
928
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000929/* Report program identification. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000930static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000931do_ident (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800932{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000933 const cpp_token *str = cpp_get_token (pfile);
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000934
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000935 if (str->type != CPP_STRING)
John David Anglin0527bc42003-11-01 22:56:54 +0000936 cpp_error (pfile, CPP_DL_ERROR, "invalid #ident directive");
Neil Booth93c803682000-10-28 17:59:06 +0000937 else if (pfile->cb.ident)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000938 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000939
Neil Booth93c803682000-10-28 17:59:06 +0000940 check_eol (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -0800941}
942
Neil Bootha5da89c2001-10-14 17:44:00 +0000943/* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
944 matching entry, or NULL if none is found. The returned entry could
945 be the start of a namespace chain, or a pragma. */
946static struct pragma_entry *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000947lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
Nathan Sidwell82443372000-06-23 10:56:09 +0000948{
Neil Booth4b115ff2001-10-14 23:04:13 +0000949 while (chain && chain->pragma != pragma)
950 chain = chain->next;
Neil Bootha5da89c2001-10-14 17:44:00 +0000951
952 return chain;
953}
954
955/* Create and insert a pragma entry for NAME at the beginning of a
956 singly-linked CHAIN. If handler is NULL, it is a namespace,
957 otherwise it is a pragma and its handler. */
958static struct pragma_entry *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000959insert_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain,
960 const cpp_hashnode *pragma, pragma_cb handler)
Neil Bootha5da89c2001-10-14 17:44:00 +0000961{
962 struct pragma_entry *new;
963
964 new = (struct pragma_entry *)
965 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
Neil Booth4b115ff2001-10-14 23:04:13 +0000966 new->pragma = pragma;
Neil Bootha5da89c2001-10-14 17:44:00 +0000967 if (handler)
968 {
969 new->is_nspace = 0;
970 new->u.handler = handler;
971 }
972 else
973 {
974 new->is_nspace = 1;
975 new->u.space = NULL;
976 }
977
978 new->next = *chain;
979 *chain = new;
980 return new;
981}
982
983/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
984 goes in the global namespace. HANDLER is the handler it will call,
985 which must be non-NULL. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000986void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000987cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
988 pragma_cb handler)
Nathan Sidwell82443372000-06-23 10:56:09 +0000989{
Neil Bootha5da89c2001-10-14 17:44:00 +0000990 struct pragma_entry **chain = &pfile->pragmas;
991 struct pragma_entry *entry;
Neil Booth4b115ff2001-10-14 23:04:13 +0000992 const cpp_hashnode *node;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000993
Neil Bootha5da89c2001-10-14 17:44:00 +0000994 if (!handler)
995 abort ();
996
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000997 if (space)
998 {
Neil Booth4b115ff2001-10-14 23:04:13 +0000999 node = cpp_lookup (pfile, U space, strlen (space));
1000 entry = lookup_pragma_entry (*chain, node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001001 if (!entry)
Neil Booth4b115ff2001-10-14 23:04:13 +00001002 entry = insert_pragma_entry (pfile, chain, node, NULL);
Neil Bootha5da89c2001-10-14 17:44:00 +00001003 else if (!entry->is_nspace)
1004 goto clash;
1005 chain = &entry->u.space;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001006 }
1007
Neil Bootha5da89c2001-10-14 17:44:00 +00001008 /* Check for duplicates. */
Neil Booth4b115ff2001-10-14 23:04:13 +00001009 node = cpp_lookup (pfile, U name, strlen (name));
1010 entry = lookup_pragma_entry (*chain, node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001011 if (entry)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001012 {
Neil Bootha5da89c2001-10-14 17:44:00 +00001013 if (entry->is_nspace)
1014 clash:
John David Anglin0527bc42003-11-01 22:56:54 +00001015 cpp_error (pfile, CPP_DL_ICE,
Neil Bootha5da89c2001-10-14 17:44:00 +00001016 "registering \"%s\" as both a pragma and a pragma namespace",
Neil Booth4b115ff2001-10-14 23:04:13 +00001017 NODE_NAME (node));
Neil Bootha5da89c2001-10-14 17:44:00 +00001018 else if (space)
John David Anglin0527bc42003-11-01 22:56:54 +00001019 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
Neil Boothebef4e82002-04-14 18:42:47 +00001020 space, name);
Neil Bootha5da89c2001-10-14 17:44:00 +00001021 else
John David Anglin0527bc42003-11-01 22:56:54 +00001022 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001023 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001024 else
Neil Booth4b115ff2001-10-14 23:04:13 +00001025 insert_pragma_entry (pfile, chain, node, handler);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001026}
Neil Bootha5da89c2001-10-14 17:44:00 +00001027
1028/* Register the pragmas the preprocessor itself handles. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001029void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001030_cpp_init_internal_pragmas (cpp_reader *pfile)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001031{
Neil Bootha5da89c2001-10-14 17:44:00 +00001032 /* Pragmas in the global namespace. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001033 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
1034
Neil Bootha5da89c2001-10-14 17:44:00 +00001035 /* New GCC-specific pragmas should be put in the GCC namespace. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001036 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
1037 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
1038 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
Nathan Sidwell82443372000-06-23 10:56:09 +00001039}
Per Bothner7f2935c1995-03-16 13:59:07 -08001040
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001041/* Return the number of registered pragmas in PE. */
1042
1043static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001044count_registered_pragmas (struct pragma_entry *pe)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001045{
1046 int ct = 0;
1047 for (; pe != NULL; pe = pe->next)
1048 {
1049 if (pe->is_nspace)
1050 ct += count_registered_pragmas (pe->u.space);
1051 ct++;
1052 }
1053 return ct;
1054}
1055
1056/* Save into SD the names of the registered pragmas referenced by PE,
1057 and return a pointer to the next free space in SD. */
1058
1059static char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001060save_registered_pragmas (struct pragma_entry *pe, char **sd)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001061{
1062 for (; pe != NULL; pe = pe->next)
1063 {
1064 if (pe->is_nspace)
1065 sd = save_registered_pragmas (pe->u.space, sd);
1066 *sd++ = xmemdup (HT_STR (&pe->pragma->ident),
1067 HT_LEN (&pe->pragma->ident),
1068 HT_LEN (&pe->pragma->ident) + 1);
1069 }
1070 return sd;
1071}
1072
1073/* Return a newly-allocated array which saves the names of the
1074 registered pragmas. */
1075
1076char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001077_cpp_save_pragma_names (cpp_reader *pfile)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001078{
1079 int ct = count_registered_pragmas (pfile->pragmas);
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001080 char **result = XNEWVEC (char *, ct);
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001081 (void) save_registered_pragmas (pfile->pragmas, result);
1082 return result;
1083}
1084
1085/* Restore from SD the names of the registered pragmas referenced by PE,
1086 and return a pointer to the next unused name in SD. */
1087
1088static char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001089restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1090 char **sd)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001091{
1092 for (; pe != NULL; pe = pe->next)
1093 {
1094 if (pe->is_nspace)
1095 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1096 pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd));
1097 free (*sd);
1098 sd++;
1099 }
1100 return sd;
1101}
1102
1103/* Restore the names of the registered pragmas from SAVED. */
1104
1105void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001106_cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001107{
1108 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1109 free (saved);
1110}
1111
Neil Bootha5da89c2001-10-14 17:44:00 +00001112/* Pragmata handling. We handle some, and pass the rest on to the
1113 front end. C99 defines three pragmas and says that no macro
1114 expansion is to be performed on them; whether or not macro
1115 expansion happens for other pragmas is implementation defined.
1116 This implementation never macro-expands the text after #pragma. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001117static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001118do_pragma (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001119{
Neil Bootha5da89c2001-10-14 17:44:00 +00001120 const struct pragma_entry *p = NULL;
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001121 const cpp_token *token, *pragma_token = pfile->cur_token;
Neil Bootha5da89c2001-10-14 17:44:00 +00001122 unsigned int count = 1;
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001123
Neil Booth93c803682000-10-28 17:59:06 +00001124 pfile->state.prevent_expansion++;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001125
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001126 token = cpp_get_token (pfile);
1127 if (token->type == CPP_NAME)
Alexandre Oliva0172e2b2000-02-27 06:24:27 +00001128 {
Neil Booth4b115ff2001-10-14 23:04:13 +00001129 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001130 if (p && p->is_nspace)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001131 {
Neil Bootha5da89c2001-10-14 17:44:00 +00001132 count = 2;
1133 token = cpp_get_token (pfile);
1134 if (token->type == CPP_NAME)
Neil Booth4b115ff2001-10-14 23:04:13 +00001135 p = lookup_pragma_entry (p->u.space, token->val.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001136 else
1137 p = NULL;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001138 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001139 }
1140
Neil Bootha5da89c2001-10-14 17:44:00 +00001141 if (p)
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001142 {
1143 /* Since the handler below doesn't get the line number, that it
1144 might need for diagnostics, make sure it has the right
1145 numbers in place. */
1146 if (pfile->cb.line_change)
1147 (*pfile->cb.line_change) (pfile, pragma_token, false);
1148 (*p->u.handler) (pfile);
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001149 }
Neil Boothd82fc102001-08-02 23:03:31 +00001150 else if (pfile->cb.def_pragma)
Neil Boothbdcbe492001-09-13 20:05:17 +00001151 {
1152 _cpp_backup_tokens (pfile, count);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001153 pfile->cb.def_pragma (pfile, pfile->directive_line);
Neil Boothbdcbe492001-09-13 20:05:17 +00001154 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001155
Neil Booth97293892001-09-14 22:04:46 +00001156 pfile->state.prevent_expansion--;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001157}
1158
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001159/* Handle #pragma once. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001160static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001161do_pragma_once (cpp_reader *pfile)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001162{
Neil Booth642ce432000-12-07 23:17:56 +00001163 if (pfile->buffer->prev == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001164 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
Neil Booth93c803682000-10-28 17:59:06 +00001165
1166 check_eol (pfile);
Neil Booth49634b32003-08-02 16:29:46 +00001167 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001168}
1169
Neil Boothc3bf3e62002-05-09 17:14:22 +00001170/* Handle #pragma GCC poison, to poison one or more identifiers so
1171 that the lexer produces a hard error for each subsequent usage. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001172static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001173do_pragma_poison (cpp_reader *pfile)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001174{
Neil Booth345894b2001-09-16 13:44:29 +00001175 const cpp_token *tok;
Zack Weinbergf8f769e2000-05-28 05:56:38 +00001176 cpp_hashnode *hp;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001177
Neil Booth93c803682000-10-28 17:59:06 +00001178 pfile->state.poisoned_ok = 1;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001179 for (;;)
1180 {
Neil Booth345894b2001-09-16 13:44:29 +00001181 tok = _cpp_lex_token (pfile);
1182 if (tok->type == CPP_EOF)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001183 break;
Neil Booth345894b2001-09-16 13:44:29 +00001184 if (tok->type != CPP_NAME)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001185 {
John David Anglin0527bc42003-11-01 22:56:54 +00001186 cpp_error (pfile, CPP_DL_ERROR,
1187 "invalid #pragma GCC poison directive");
Neil Booth93c803682000-10-28 17:59:06 +00001188 break;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001189 }
1190
Neil Booth345894b2001-09-16 13:44:29 +00001191 hp = tok->val.node;
Neil Booth93c803682000-10-28 17:59:06 +00001192 if (hp->flags & NODE_POISONED)
1193 continue;
1194
1195 if (hp->type == NT_MACRO)
John David Anglin0527bc42003-11-01 22:56:54 +00001196 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
Neil Boothebef4e82002-04-14 18:42:47 +00001197 NODE_NAME (hp));
Neil Booth93c803682000-10-28 17:59:06 +00001198 _cpp_free_definition (hp);
1199 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001200 }
Neil Booth93c803682000-10-28 17:59:06 +00001201 pfile->state.poisoned_ok = 0;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001202}
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001203
1204/* Mark the current header as a system header. This will suppress
1205 some categories of warnings (notably those from -pedantic). It is
1206 intended for use in system libraries that cannot be implemented in
1207 conforming C, but cannot be certain that their headers appear in a
1208 system include directory. To prevent abuse, it is rejected in the
1209 primary source file. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001210static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001211do_pragma_system_header (cpp_reader *pfile)
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001212{
Neil Booth614c7d32000-12-04 07:32:04 +00001213 cpp_buffer *buffer = pfile->buffer;
1214
1215 if (buffer->prev == 0)
John David Anglin0527bc42003-11-01 22:56:54 +00001216 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +00001217 "#pragma system_header ignored outside include file");
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001218 else
Neil Boothd82fc102001-08-02 23:03:31 +00001219 {
1220 check_eol (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +00001221 skip_rest_of_line (pfile);
Neil Boothd82fc102001-08-02 23:03:31 +00001222 cpp_make_system_header (pfile, 1, 0);
1223 }
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001224}
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001225
1226/* Check the modified date of the current include file against a specified
1227 file. Issue a diagnostic, if the specified file is newer. We use this to
1228 determine if a fixed header should be refixed. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001229static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001230do_pragma_dependency (cpp_reader *pfile)
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001231{
Neil Booth74eb4b32003-04-21 19:21:59 +00001232 const char *fname;
1233 int angle_brackets, ordering;
Kazu Hiratadf383482002-05-22 22:02:16 +00001234
Neil Booth74eb4b32003-04-21 19:21:59 +00001235 fname = parse_include (pfile, &angle_brackets);
1236 if (!fname)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001237 return;
Zack Weinberg041c3192000-07-04 01:58:21 +00001238
Neil Booth74eb4b32003-04-21 19:21:59 +00001239 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001240 if (ordering < 0)
John David Anglin0527bc42003-11-01 22:56:54 +00001241 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001242 else if (ordering > 0)
1243 {
John David Anglin0527bc42003-11-01 22:56:54 +00001244 cpp_error (pfile, CPP_DL_WARNING,
1245 "current file is older than %s", fname);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001246 if (cpp_get_token (pfile)->type != CPP_EOF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001247 {
1248 _cpp_backup_tokens (pfile, 1);
John David Anglin0527bc42003-11-01 22:56:54 +00001249 do_diagnostic (pfile, CPP_DL_WARNING, 0);
Neil Boothbdcbe492001-09-13 20:05:17 +00001250 }
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001251 }
Neil Booth74eb4b32003-04-21 19:21:59 +00001252
Kaveh R. Ghazifad205f2003-06-16 21:41:10 +00001253 free ((void *) fname);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001254}
1255
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001256/* Get a token but skip padding. */
1257static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001258get_token_no_padding (cpp_reader *pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001259{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001260 for (;;)
1261 {
1262 const cpp_token *result = cpp_get_token (pfile);
1263 if (result->type != CPP_PADDING)
1264 return result;
1265 }
1266}
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001267
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001268/* Check syntax is "(string-literal)". Returns the string on success,
1269 or NULL on failure. */
1270static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001271get__Pragma_string (cpp_reader *pfile)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001272{
1273 const cpp_token *string;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001274
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001275 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1276 return NULL;
1277
1278 string = get_token_no_padding (pfile);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001279 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001280 return NULL;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001281
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001282 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1283 return NULL;
1284
1285 return string;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001286}
1287
Neil Booth87062812001-10-20 09:00:53 +00001288/* Destringize IN into a temporary buffer, by removing the first \ of
1289 \" and \\ sequences, and process the result as a #pragma directive. */
1290static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001291destringize_and_run (cpp_reader *pfile, const cpp_string *in)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001292{
1293 const unsigned char *src, *limit;
Neil Booth87062812001-10-20 09:00:53 +00001294 char *dest, *result;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001295
Neil Booth6338b352003-04-23 22:44:06 +00001296 dest = result = alloca (in->len - 1);
1297 src = in->text + 1 + (in->text[0] == 'L');
1298 limit = in->text + in->len - 1;
1299 while (src < limit)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001300 {
1301 /* We know there is a character following the backslash. */
1302 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1303 src++;
1304 *dest++ = *src++;
1305 }
Neil Booth26aea072003-04-19 00:22:51 +00001306 *dest = '\n';
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001307
Neil Booth8128ccc2002-11-18 20:43:40 +00001308 /* Ugh; an awful kludge. We are really not set up to be lexing
1309 tokens when in the middle of a macro expansion. Use a new
1310 context to force cpp_get_token to lex, and so skip_rest_of_line
1311 doesn't go beyond the end of the text. Also, remember the
1312 current lexing position so we can return to it later.
Neil Booth79ba5e32002-09-03 21:55:40 +00001313
Neil Booth8128ccc2002-11-18 20:43:40 +00001314 Something like line-at-a-time lexing should remove the need for
1315 this. */
1316 {
1317 cpp_context *saved_context = pfile->context;
1318 cpp_token *saved_cur_token = pfile->cur_token;
1319 tokenrun *saved_cur_run = pfile->cur_run;
1320
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001321 pfile->context = XNEW (cpp_context);
Neil Booth8128ccc2002-11-18 20:43:40 +00001322 pfile->context->macro = 0;
1323 pfile->context->prev = 0;
1324 run_directive (pfile, T_PRAGMA, result, dest - result);
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001325 XDELETE (pfile->context);
Neil Booth8128ccc2002-11-18 20:43:40 +00001326 pfile->context = saved_context;
1327 pfile->cur_token = saved_cur_token;
1328 pfile->cur_run = saved_cur_run;
Neil Booth8128ccc2002-11-18 20:43:40 +00001329 }
Neil Booth79ba5e32002-09-03 21:55:40 +00001330
1331 /* See above comment. For the moment, we'd like
1332
1333 token1 _Pragma ("foo") token2
1334
1335 to be output as
1336
1337 token1
1338 # 7 "file.c"
1339 #pragma foo
1340 # 7 "file.c"
1341 token2
1342
1343 Getting the line markers is a little tricky. */
1344 if (pfile->cb.line_change)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001345 pfile->cb.line_change (pfile, pfile->cur_token, false);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001346}
1347
Neil Booth87062812001-10-20 09:00:53 +00001348/* Handle the _Pragma operator. */
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001349void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001350_cpp_do__Pragma (cpp_reader *pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001351{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001352 const cpp_token *string = get__Pragma_string (pfile);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001353
Neil Booth79ba5e32002-09-03 21:55:40 +00001354 if (string)
1355 destringize_and_run (pfile, &string->val.str);
1356 else
John David Anglin0527bc42003-11-01 22:56:54 +00001357 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00001358 "_Pragma takes a parenthesized string literal");
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001359}
1360
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001361/* Ignore #sccs on all systems. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001362static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001363do_sccs (cpp_reader *pfile ATTRIBUTE_UNUSED)
Per Bothner7f2935c1995-03-16 13:59:07 -08001364{
Per Bothner7f2935c1995-03-16 13:59:07 -08001365}
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001366
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001367/* Handle #ifdef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001368static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001369do_ifdef (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +00001370{
Neil Booth93c803682000-10-28 17:59:06 +00001371 int skip = 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001372
Neil Boothcef0d192001-07-26 06:02:47 +00001373 if (! pfile->state.skipping)
Neil Booth93c803682000-10-28 17:59:06 +00001374 {
1375 const cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +00001376
Neil Booth93c803682000-10-28 17:59:06 +00001377 if (node)
Neil Bootha69cbaa2002-07-23 22:57:49 +00001378 {
1379 skip = node->type != NT_MACRO;
1380 _cpp_mark_macro_used (node);
1381 check_eol (pfile);
1382 }
Neil Booth93c803682000-10-28 17:59:06 +00001383 }
1384
1385 push_conditional (pfile, skip, T_IFDEF, 0);
Zack Weinberg168d3732000-03-14 06:34:11 +00001386}
1387
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001388/* Handle #ifndef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001389static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001390do_ifndef (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +00001391{
Neil Booth93c803682000-10-28 17:59:06 +00001392 int skip = 1;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001393 const cpp_hashnode *node = 0;
Zack Weinberg168d3732000-03-14 06:34:11 +00001394
Neil Boothcef0d192001-07-26 06:02:47 +00001395 if (! pfile->state.skipping)
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001396 {
Neil Booth93c803682000-10-28 17:59:06 +00001397 node = lex_macro_node (pfile);
Geoffrey Keatingb43db0b2000-12-02 22:28:44 +00001398
1399 if (node)
Neil Bootha69cbaa2002-07-23 22:57:49 +00001400 {
1401 skip = node->type == NT_MACRO;
1402 _cpp_mark_macro_used (node);
1403 check_eol (pfile);
1404 }
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001405 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001406
Neil Booth93c803682000-10-28 17:59:06 +00001407 push_conditional (pfile, skip, T_IFNDEF, node);
Per Bothner7f2935c1995-03-16 13:59:07 -08001408}
1409
Neil Booth6d18adb2001-07-29 17:27:57 +00001410/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1411 pfile->mi_ind_cmacro so we can handle multiple-include
Kazu Hirata6356f892003-06-12 19:01:08 +00001412 optimizations. If macro expansion occurs in the expression, we
Neil Booth6d18adb2001-07-29 17:27:57 +00001413 cannot treat it as a controlling conditional, since the expansion
1414 could change in the future. That is handled by cpp_get_token. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001415static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001416do_if (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001417{
Neil Booth93c803682000-10-28 17:59:06 +00001418 int skip = 1;
Per Bothner7f2935c1995-03-16 13:59:07 -08001419
Neil Boothcef0d192001-07-26 06:02:47 +00001420 if (! pfile->state.skipping)
Neil Booth87ed1092002-04-28 19:42:54 +00001421 skip = _cpp_parse_expr (pfile) == false;
Neil Booth93c803682000-10-28 17:59:06 +00001422
Neil Booth6d18adb2001-07-29 17:27:57 +00001423 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
Per Bothner7f2935c1995-03-16 13:59:07 -08001424}
1425
Neil Boothb528a072000-11-12 11:46:21 +00001426/* Flip skipping state if appropriate and continue without changing
Zack Weinbergea4a4532000-05-29 16:19:32 +00001427 if_stack; this is so that the error message for missing #endif's
1428 etc. will point to the original #if. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001429static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001430do_else (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001431{
Neil Boothb528a072000-11-12 11:46:21 +00001432 cpp_buffer *buffer = pfile->buffer;
1433 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001434
1435 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001436 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
Neil Booth93c803682000-10-28 17:59:06 +00001437 else
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001438 {
Neil Booth93c803682000-10-28 17:59:06 +00001439 if (ifs->type == T_ELSE)
1440 {
John David Anglin0527bc42003-11-01 22:56:54 +00001441 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1442 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Booth93c803682000-10-28 17:59:06 +00001443 "the conditional began here");
1444 }
Neil Boothb528a072000-11-12 11:46:21 +00001445 ifs->type = T_ELSE;
1446
Neil Boothcef0d192001-07-26 06:02:47 +00001447 /* Skip any future (erroneous) #elses or #elifs. */
1448 pfile->state.skipping = ifs->skip_elses;
1449 ifs->skip_elses = true;
Neil Booth93c803682000-10-28 17:59:06 +00001450
1451 /* Invalidate any controlling macro. */
1452 ifs->mi_cmacro = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001453
Neil Boothcef0d192001-07-26 06:02:47 +00001454 /* Only check EOL if was not originally skipping. */
Phil Edwards909de5d2002-03-22 21:59:04 +00001455 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
Neil Boothcef0d192001-07-26 06:02:47 +00001456 check_eol (pfile);
1457 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001458}
1459
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001460/* Handle a #elif directive by not changing if_stack either. See the
Neil Booth93c803682000-10-28 17:59:06 +00001461 comment above do_else. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001462static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001463do_elif (cpp_reader *pfile)
Zack Weinbergea4a4532000-05-29 16:19:32 +00001464{
Neil Boothb528a072000-11-12 11:46:21 +00001465 cpp_buffer *buffer = pfile->buffer;
1466 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001467
1468 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001469 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
Neil Boothb528a072000-11-12 11:46:21 +00001470 else
Zack Weinbergea4a4532000-05-29 16:19:32 +00001471 {
Neil Boothb528a072000-11-12 11:46:21 +00001472 if (ifs->type == T_ELSE)
1473 {
John David Anglin0527bc42003-11-01 22:56:54 +00001474 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1475 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Boothb528a072000-11-12 11:46:21 +00001476 "the conditional began here");
1477 }
1478 ifs->type = T_ELIF;
1479
Neil Boothcef0d192001-07-26 06:02:47 +00001480 /* Only evaluate this if we aren't skipping elses. During
1481 evaluation, set skipping to false to get lexer warnings. */
1482 if (ifs->skip_elses)
1483 pfile->state.skipping = 1;
1484 else
Neil Boothb528a072000-11-12 11:46:21 +00001485 {
Neil Boothcef0d192001-07-26 06:02:47 +00001486 pfile->state.skipping = 0;
1487 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1488 ifs->skip_elses = ! pfile->state.skipping;
Neil Boothb528a072000-11-12 11:46:21 +00001489 }
Neil Boothcef0d192001-07-26 06:02:47 +00001490
1491 /* Invalidate any controlling macro. */
1492 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001493 }
Zack Weinbergea4a4532000-05-29 16:19:32 +00001494}
1495
Neil Boothcef0d192001-07-26 06:02:47 +00001496/* #endif pops the if stack and resets pfile->state.skipping. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001497static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001498do_endif (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001499{
Neil Boothb528a072000-11-12 11:46:21 +00001500 cpp_buffer *buffer = pfile->buffer;
1501 struct if_stack *ifs = buffer->if_stack;
Per Bothner7f2935c1995-03-16 13:59:07 -08001502
Zack Weinbergea4a4532000-05-29 16:19:32 +00001503 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001504 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
Per Bothner7f2935c1995-03-16 13:59:07 -08001505 else
1506 {
Neil Boothcef0d192001-07-26 06:02:47 +00001507 /* Only check EOL if was not originally skipping. */
Phil Edwards909de5d2002-03-22 21:59:04 +00001508 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
Neil Boothcef0d192001-07-26 06:02:47 +00001509 check_eol (pfile);
1510
Neil Booth93c803682000-10-28 17:59:06 +00001511 /* If potential control macro, we go back outside again. */
1512 if (ifs->next == 0 && ifs->mi_cmacro)
1513 {
Neil Booth6d18adb2001-07-29 17:27:57 +00001514 pfile->mi_valid = true;
Neil Booth93c803682000-10-28 17:59:06 +00001515 pfile->mi_cmacro = ifs->mi_cmacro;
1516 }
1517
Neil Boothb528a072000-11-12 11:46:21 +00001518 buffer->if_stack = ifs->next;
Neil Boothcef0d192001-07-26 06:02:47 +00001519 pfile->state.skipping = ifs->was_skipping;
Neil Booth2a967f32001-05-20 06:26:45 +00001520 obstack_free (&pfile->buffer_ob, ifs);
Per Bothner7f2935c1995-03-16 13:59:07 -08001521 }
Neil Booth93c803682000-10-28 17:59:06 +00001522}
Zack Weinberg041c3192000-07-04 01:58:21 +00001523
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001524/* Push an if_stack entry for a preprocessor conditional, and set
1525 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1526 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1527 we need to check here that we are at the top of the file. */
Zack Weinbergea4a4532000-05-29 16:19:32 +00001528static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001529push_conditional (cpp_reader *pfile, int skip, int type,
1530 const cpp_hashnode *cmacro)
Zack Weinbergea4a4532000-05-29 16:19:32 +00001531{
1532 struct if_stack *ifs;
Neil Boothb528a072000-11-12 11:46:21 +00001533 cpp_buffer *buffer = pfile->buffer;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001534
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001535 ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
Neil Booth50410422001-09-15 10:18:03 +00001536 ifs->line = pfile->directive_line;
Neil Boothb528a072000-11-12 11:46:21 +00001537 ifs->next = buffer->if_stack;
Neil Boothcef0d192001-07-26 06:02:47 +00001538 ifs->skip_elses = pfile->state.skipping || !skip;
1539 ifs->was_skipping = pfile->state.skipping;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001540 ifs->type = type;
Neil Booth6d18adb2001-07-29 17:27:57 +00001541 /* This condition is effectively a test for top-of-file. */
1542 if (pfile->mi_valid && pfile->mi_cmacro == 0)
Neil Booth93c803682000-10-28 17:59:06 +00001543 ifs->mi_cmacro = cmacro;
1544 else
1545 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001546
Neil Boothcef0d192001-07-26 06:02:47 +00001547 pfile->state.skipping = skip;
Neil Boothb528a072000-11-12 11:46:21 +00001548 buffer->if_stack = ifs;
Zack Weinberg7061aa51998-12-15 11:09:16 +00001549}
1550
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001551/* Read the tokens of the answer into the macro pool, in a directive
1552 of type TYPE. Only commit the memory if we intend it as permanent
1553 storage, i.e. the #assert case. Returns 0 on success, and sets
1554 ANSWERP to point to the answer. */
Neil Booth93c803682000-10-28 17:59:06 +00001555static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001556parse_answer (cpp_reader *pfile, struct answer **answerp, int type)
Zack Weinberg041c3192000-07-04 01:58:21 +00001557{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001558 const cpp_token *paren;
Neil Booth93c803682000-10-28 17:59:06 +00001559 struct answer *answer;
Neil Booth8c3b2692001-09-30 10:03:11 +00001560 unsigned int acount;
Zack Weinberg041c3192000-07-04 01:58:21 +00001561
Neil Booth93c803682000-10-28 17:59:06 +00001562 /* In a conditional, it is legal to not have an open paren. We
1563 should save the following token in this case. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001564 paren = cpp_get_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001565
1566 /* If not a paren, see if we're OK. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001567 if (paren->type != CPP_OPEN_PAREN)
Zack Weinberg041c3192000-07-04 01:58:21 +00001568 {
Neil Booth93c803682000-10-28 17:59:06 +00001569 /* In a conditional no answer is a test for any answer. It
1570 could be followed by any token. */
1571 if (type == T_IF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001572 {
1573 _cpp_backup_tokens (pfile, 1);
1574 return 0;
1575 }
Neil Booth93c803682000-10-28 17:59:06 +00001576
1577 /* #unassert with no answer is valid - it removes all answers. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001578 if (type == T_UNASSERT && paren->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +00001579 return 0;
1580
John David Anglin0527bc42003-11-01 22:56:54 +00001581 cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate");
Neil Booth93c803682000-10-28 17:59:06 +00001582 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001583 }
1584
Neil Booth8c3b2692001-09-30 10:03:11 +00001585 for (acount = 0;; acount++)
Zack Weinberg041c3192000-07-04 01:58:21 +00001586 {
Neil Booth8c3b2692001-09-30 10:03:11 +00001587 size_t room_needed;
1588 const cpp_token *token = cpp_get_token (pfile);
1589 cpp_token *dest;
Zack Weinberg041c3192000-07-04 01:58:21 +00001590
Neil Booth93c803682000-10-28 17:59:06 +00001591 if (token->type == CPP_CLOSE_PAREN)
1592 break;
Zack Weinberg041c3192000-07-04 01:58:21 +00001593
1594 if (token->type == CPP_EOF)
1595 {
John David Anglin0527bc42003-11-01 22:56:54 +00001596 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
Neil Booth93c803682000-10-28 17:59:06 +00001597 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001598 }
Neil Booth8c3b2692001-09-30 10:03:11 +00001599
1600 /* struct answer includes the space for one token. */
1601 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1602
1603 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1604 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1605
1606 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1607 *dest = *token;
1608
1609 /* Drop whitespace at start, for answer equivalence purposes. */
1610 if (acount == 0)
1611 dest->flags &= ~PREV_WHITE;
Zack Weinberg041c3192000-07-04 01:58:21 +00001612 }
1613
Neil Booth8c3b2692001-09-30 10:03:11 +00001614 if (acount == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001615 {
John David Anglin0527bc42003-11-01 22:56:54 +00001616 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
Neil Booth93c803682000-10-28 17:59:06 +00001617 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001618 }
1619
Neil Booth8c3b2692001-09-30 10:03:11 +00001620 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1621 answer->count = acount;
1622 answer->next = NULL;
Neil Booth93c803682000-10-28 17:59:06 +00001623 *answerp = answer;
Zack Weinberg041c3192000-07-04 01:58:21 +00001624
Neil Booth93c803682000-10-28 17:59:06 +00001625 return 0;
1626}
1627
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001628/* Parses an assertion directive of type TYPE, returning a pointer to
1629 the hash node of the predicate, or 0 on error. If an answer was
1630 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
Neil Booth93c803682000-10-28 17:59:06 +00001631static cpp_hashnode *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001632parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
Neil Booth93c803682000-10-28 17:59:06 +00001633{
1634 cpp_hashnode *result = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001635 const cpp_token *predicate;
Neil Booth93c803682000-10-28 17:59:06 +00001636
1637 /* We don't expand predicates or answers. */
1638 pfile->state.prevent_expansion++;
1639
Neil Booth93c803682000-10-28 17:59:06 +00001640 *answerp = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001641 predicate = cpp_get_token (pfile);
1642 if (predicate->type == CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +00001643 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001644 else if (predicate->type != CPP_NAME)
John David Anglin0527bc42003-11-01 22:56:54 +00001645 cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier");
Neil Booth93c803682000-10-28 17:59:06 +00001646 else if (parse_answer (pfile, answerp, type) == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001647 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001648 unsigned int len = NODE_LEN (predicate->val.node);
Neil Booth93c803682000-10-28 17:59:06 +00001649 unsigned char *sym = alloca (len + 1);
1650
1651 /* Prefix '#' to get it out of macro namespace. */
1652 sym[0] = '#';
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001653 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
Neil Booth93c803682000-10-28 17:59:06 +00001654 result = cpp_lookup (pfile, sym, len + 1);
Zack Weinberg041c3192000-07-04 01:58:21 +00001655 }
1656
Neil Booth93c803682000-10-28 17:59:06 +00001657 pfile->state.prevent_expansion--;
1658 return result;
Zack Weinberg041c3192000-07-04 01:58:21 +00001659}
1660
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001661/* Returns a pointer to the pointer to CANDIDATE in the answer chain,
Zack Weinberg041c3192000-07-04 01:58:21 +00001662 or a pointer to NULL if the answer is not in the chain. */
Neil Booth93c803682000-10-28 17:59:06 +00001663static struct answer **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001664find_answer (cpp_hashnode *node, const struct answer *candidate)
Zack Weinberg041c3192000-07-04 01:58:21 +00001665{
Neil Booth93c803682000-10-28 17:59:06 +00001666 unsigned int i;
Zack Weinberg041c3192000-07-04 01:58:21 +00001667 struct answer **result;
1668
1669 for (result = &node->value.answers; *result; result = &(*result)->next)
Neil Booth93c803682000-10-28 17:59:06 +00001670 {
1671 struct answer *answer = *result;
1672
1673 if (answer->count == candidate->count)
1674 {
1675 for (i = 0; i < answer->count; i++)
1676 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1677 break;
1678
1679 if (i == answer->count)
1680 break;
1681 }
1682 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001683
1684 return result;
1685}
1686
Neil Booth93c803682000-10-28 17:59:06 +00001687/* Test an assertion within a preprocessor conditional. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +00001688 nonzero on failure, zero on success. On success, the result of
Hans-Peter Nilsson24026452002-11-29 22:41:04 +00001689 the test is written into VALUE, otherwise the value 0. */
Neil Booth93c803682000-10-28 17:59:06 +00001690int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001691_cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
Neil Booth93c803682000-10-28 17:59:06 +00001692{
1693 struct answer *answer;
1694 cpp_hashnode *node;
1695
1696 node = parse_assertion (pfile, &answer, T_IF);
Hans-Peter Nilsson24026452002-11-29 22:41:04 +00001697
1698 /* For recovery, an erroneous assertion expression is handled as a
1699 failing assertion. */
1700 *value = 0;
1701
Neil Booth93c803682000-10-28 17:59:06 +00001702 if (node)
1703 *value = (node->type == NT_ASSERTION &&
1704 (answer == 0 || *find_answer (node, answer) != 0));
Neil Booth91318902002-05-26 18:42:21 +00001705 else if (pfile->cur_token[-1].type == CPP_EOF)
1706 _cpp_backup_tokens (pfile, 1);
Neil Booth93c803682000-10-28 17:59:06 +00001707
1708 /* We don't commit the memory for the answer - it's temporary only. */
1709 return node == 0;
1710}
1711
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001712/* Handle #assert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001713static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001714do_assert (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001715{
Zack Weinberg041c3192000-07-04 01:58:21 +00001716 struct answer *new_answer;
1717 cpp_hashnode *node;
Kazu Hiratadf383482002-05-22 22:02:16 +00001718
Neil Booth93c803682000-10-28 17:59:06 +00001719 node = parse_assertion (pfile, &new_answer, T_ASSERT);
Zack Weinberg041c3192000-07-04 01:58:21 +00001720 if (node)
1721 {
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001722 size_t answer_size;
1723
Neil Booth93c803682000-10-28 17:59:06 +00001724 /* Place the new answer in the answer list. First check there
1725 is not a duplicate. */
Zack Weinberg041c3192000-07-04 01:58:21 +00001726 new_answer->next = 0;
Neil Booth93c803682000-10-28 17:59:06 +00001727 if (node->type == NT_ASSERTION)
Zack Weinberg041c3192000-07-04 01:58:21 +00001728 {
Neil Booth93c803682000-10-28 17:59:06 +00001729 if (*find_answer (node, new_answer))
1730 {
John David Anglin0527bc42003-11-01 22:56:54 +00001731 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
Neil Boothebef4e82002-04-14 18:42:47 +00001732 NODE_NAME (node) + 1);
Neil Booth93c803682000-10-28 17:59:06 +00001733 return;
1734 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001735 new_answer->next = node->value.answers;
1736 }
Neil Booth8c3b2692001-09-30 10:03:11 +00001737
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001738 answer_size = sizeof (struct answer) + ((new_answer->count - 1)
1739 * sizeof (cpp_token));
1740 /* Commit or allocate storage for the object. */
1741 if (pfile->hash_table->alloc_subobject)
1742 {
1743 struct answer *temp_answer = new_answer;
1744 new_answer = pfile->hash_table->alloc_subobject (answer_size);
1745 memcpy (new_answer, temp_answer, answer_size);
1746 }
1747 else
1748 BUFF_FRONT (pfile->a_buff) += answer_size;
1749
Neil Booth93c803682000-10-28 17:59:06 +00001750 node->type = NT_ASSERTION;
Zack Weinberg041c3192000-07-04 01:58:21 +00001751 node->value.answers = new_answer;
Neil Booth8c3b2692001-09-30 10:03:11 +00001752 check_eol (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +00001753 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001754}
Zack Weinberg7061aa51998-12-15 11:09:16 +00001755
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001756/* Handle #unassert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001757static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001758do_unassert (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001759{
Zack Weinberg041c3192000-07-04 01:58:21 +00001760 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00001761 struct answer *answer;
Kazu Hiratadf383482002-05-22 22:02:16 +00001762
Neil Booth93c803682000-10-28 17:59:06 +00001763 node = parse_assertion (pfile, &answer, T_UNASSERT);
1764 /* It isn't an error to #unassert something that isn't asserted. */
1765 if (node && node->type == NT_ASSERTION)
Zack Weinberg7061aa51998-12-15 11:09:16 +00001766 {
Zack Weinberg041c3192000-07-04 01:58:21 +00001767 if (answer)
Neil Booth93c803682000-10-28 17:59:06 +00001768 {
1769 struct answer **p = find_answer (node, answer), *temp;
1770
1771 /* Remove the answer from the list. */
1772 temp = *p;
1773 if (temp)
1774 *p = temp->next;
1775
1776 /* Did we free the last answer? */
1777 if (node->value.answers == 0)
1778 node->type = NT_VOID;
Neil Booth8c3b2692001-09-30 10:03:11 +00001779
1780 check_eol (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001781 }
1782 else
1783 _cpp_free_definition (node);
Zack Weinberg7061aa51998-12-15 11:09:16 +00001784 }
Neil Booth93c803682000-10-28 17:59:06 +00001785
1786 /* We don't commit the memory for the answer - it's temporary only. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001787}
Per Bothner7f2935c1995-03-16 13:59:07 -08001788
Zack Weinberg45b966d2000-03-13 22:01:08 +00001789/* These are for -D, -U, -A. */
1790
1791/* Process the string STR as if it appeared as the body of a #define.
1792 If STR is just an identifier, define it with value 1.
1793 If STR has anything after the identifier, then it should
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001794 be identifier=definition. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001795void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001796cpp_define (cpp_reader *pfile, const char *str)
Zack Weinberg45b966d2000-03-13 22:01:08 +00001797{
1798 char *buf, *p;
1799 size_t count;
1800
Kazu Hiratadf383482002-05-22 22:02:16 +00001801 /* Copy the entire option so we can modify it.
Zack Weinberg45b966d2000-03-13 22:01:08 +00001802 Change the first "=" in the string to a space. If there is none,
Neil Booth86368122000-10-31 23:34:59 +00001803 tack " 1" on the end. */
1804
Neil Booth86368122000-10-31 23:34:59 +00001805 count = strlen (str);
Kaveh R. Ghazi703ad42b2003-07-19 14:47:15 +00001806 buf = alloca (count + 3);
Neil Booth86368122000-10-31 23:34:59 +00001807 memcpy (buf, str, count);
1808
1809 p = strchr (str, '=');
Zack Weinberg45b966d2000-03-13 22:01:08 +00001810 if (p)
Neil Booth86368122000-10-31 23:34:59 +00001811 buf[p - str] = ' ';
Zack Weinberg45b966d2000-03-13 22:01:08 +00001812 else
1813 {
Neil Booth86368122000-10-31 23:34:59 +00001814 buf[count++] = ' ';
1815 buf[count++] = '1';
Zack Weinberg45b966d2000-03-13 22:01:08 +00001816 }
Neil Booth26aea072003-04-19 00:22:51 +00001817 buf[count] = '\n';
Zack Weinberg45b966d2000-03-13 22:01:08 +00001818
Neil Booth29401c32001-08-22 20:37:20 +00001819 run_directive (pfile, T_DEFINE, buf, count);
Zack Weinberg2c8f0512000-08-29 18:37:37 +00001820}
1821
Neil Boothad2a0842000-12-17 00:13:54 +00001822/* Slight variant of the above for use by initialize_builtins. */
Zack Weinberg2c8f0512000-08-29 18:37:37 +00001823void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001824_cpp_define_builtin (cpp_reader *pfile, const char *str)
Zack Weinberg2c8f0512000-08-29 18:37:37 +00001825{
Neil Booth26aea072003-04-19 00:22:51 +00001826 size_t len = strlen (str);
1827 char *buf = alloca (len + 1);
1828 memcpy (buf, str, len);
1829 buf[len] = '\n';
1830 run_directive (pfile, T_DEFINE, buf, len);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001831}
1832
1833/* Process MACRO as if it appeared as the body of an #undef. */
1834void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001835cpp_undef (cpp_reader *pfile, const char *macro)
Zack Weinberg45b966d2000-03-13 22:01:08 +00001836{
Neil Booth26aea072003-04-19 00:22:51 +00001837 size_t len = strlen (macro);
1838 char *buf = alloca (len + 1);
1839 memcpy (buf, macro, len);
1840 buf[len] = '\n';
1841 run_directive (pfile, T_UNDEF, buf, len);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001842}
1843
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001844/* Process the string STR as if it appeared as the body of a #assert. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001845void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001846cpp_assert (cpp_reader *pfile, const char *str)
Zack Weinberg45b966d2000-03-13 22:01:08 +00001847{
Neil Booth86368122000-10-31 23:34:59 +00001848 handle_assertion (pfile, str, T_ASSERT);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001849}
1850
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001851/* Process STR as if it appeared as the body of an #unassert. */
Zack Weinberg0b22d651999-03-15 18:42:46 +00001852void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001853cpp_unassert (cpp_reader *pfile, const char *str)
Zack Weinberg0b22d651999-03-15 18:42:46 +00001854{
Neil Booth86368122000-10-31 23:34:59 +00001855 handle_assertion (pfile, str, T_UNASSERT);
Kazu Hiratadf383482002-05-22 22:02:16 +00001856}
Zack Weinberg0b22d651999-03-15 18:42:46 +00001857
Neil Booth86368122000-10-31 23:34:59 +00001858/* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1859static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001860handle_assertion (cpp_reader *pfile, const char *str, int type)
Neil Booth86368122000-10-31 23:34:59 +00001861{
1862 size_t count = strlen (str);
1863 const char *p = strchr (str, '=');
1864
Neil Booth26aea072003-04-19 00:22:51 +00001865 /* Copy the entire option so we can modify it. Change the first
1866 "=" in the string to a '(', and tack a ')' on the end. */
Kaveh R. Ghazi703ad42b2003-07-19 14:47:15 +00001867 char *buf = alloca (count + 2);
Neil Booth26aea072003-04-19 00:22:51 +00001868
1869 memcpy (buf, str, count);
Neil Booth86368122000-10-31 23:34:59 +00001870 if (p)
1871 {
Neil Booth86368122000-10-31 23:34:59 +00001872 buf[p - str] = '(';
1873 buf[count++] = ')';
Neil Booth86368122000-10-31 23:34:59 +00001874 }
Neil Booth26aea072003-04-19 00:22:51 +00001875 buf[count] = '\n';
1876 str = buf;
Neil Booth86368122000-10-31 23:34:59 +00001877
Neil Booth29401c32001-08-22 20:37:20 +00001878 run_directive (pfile, type, str, count);
Neil Booth86368122000-10-31 23:34:59 +00001879}
1880
Neil Booth7e96d762001-01-13 01:00:01 +00001881/* The number of errors for a given reader. */
1882unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001883cpp_errors (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00001884{
1885 return pfile->errors;
1886}
1887
1888/* The options structure. */
1889cpp_options *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001890cpp_get_options (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00001891{
1892 return &pfile->opts;
1893}
1894
1895/* The callbacks structure. */
1896cpp_callbacks *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001897cpp_get_callbacks (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00001898{
1899 return &pfile->cb;
1900}
1901
1902/* Copy the given callbacks structure to our own. */
1903void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001904cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
Neil Booth7e96d762001-01-13 01:00:01 +00001905{
1906 pfile->cb = *cb;
1907}
1908
Zack Weinbergc6e83802004-06-05 20:58:06 +00001909/* The dependencies structure. (Creates one if it hasn't already been.) */
1910struct deps *
1911cpp_get_deps (cpp_reader *pfile)
1912{
1913 if (!pfile->deps)
1914 pfile->deps = deps_init ();
1915 return pfile->deps;
1916}
1917
Neil Bootheb1f4d92000-12-18 19:00:26 +00001918/* Push a new buffer on the buffer stack. Returns the new buffer; it
1919 doesn't fail. It does not generate a file change call back; that
1920 is the responsibility of the caller. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00001921cpp_buffer *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001922cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
Per Bothner40de9f72003-10-02 07:30:34 +00001923 int from_stage3)
Zack Weinbergc71f8352000-07-05 05:33:57 +00001924{
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001925 cpp_buffer *new = XOBNEW (&pfile->buffer_ob, cpp_buffer);
Neil Booth93c803682000-10-28 17:59:06 +00001926
Neil Boothfde84342001-08-06 21:07:41 +00001927 /* Clears, amongst other things, if_stack and mi_cmacro. */
1928 memset (new, 0, sizeof (cpp_buffer));
Neil Boothad2a0842000-12-17 00:13:54 +00001929
Neil Booth26aea072003-04-19 00:22:51 +00001930 new->next_line = new->buf = buffer;
Neil Boothfde84342001-08-06 21:07:41 +00001931 new->rlimit = buffer + len;
Neil Booth26aea072003-04-19 00:22:51 +00001932 new->from_stage3 = from_stage3;
Neil Booth3cf35932000-12-05 23:42:43 +00001933 new->prev = pfile->buffer;
Neil Booth26aea072003-04-19 00:22:51 +00001934 new->need_line = true;
Neil Booth0bda4762000-12-11 07:45:16 +00001935
Neil Booth3cf35932000-12-05 23:42:43 +00001936 pfile->buffer = new;
Eric Christophercf551fb2004-01-16 22:37:49 +00001937
Zack Weinbergc71f8352000-07-05 05:33:57 +00001938 return new;
1939}
1940
Neil Boothaf0d16c2002-04-22 17:48:02 +00001941/* Pops a single buffer, with a file change call-back if appropriate.
1942 Then pushes the next -include file, if any remain. */
Neil Boothef6e9582001-08-04 12:01:59 +00001943void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001944_cpp_pop_buffer (cpp_reader *pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00001945{
Neil Boothfde84342001-08-06 21:07:41 +00001946 cpp_buffer *buffer = pfile->buffer;
Neil Booth8f9b4002003-07-29 22:26:13 +00001947 struct _cpp_file *inc = buffer->file;
Neil Boothad2a0842000-12-17 00:13:54 +00001948 struct if_stack *ifs;
Zack Weinbergc71f8352000-07-05 05:33:57 +00001949
Neil Boothfde84342001-08-06 21:07:41 +00001950 /* Walk back up the conditional stack till we reach its level at
1951 entry to this file, issuing error messages. */
1952 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
John David Anglin0527bc42003-11-01 22:56:54 +00001953 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Boothfde84342001-08-06 21:07:41 +00001954 "unterminated #%s", dtable[ifs->type].name);
1955
Neil Booth97293892001-09-14 22:04:46 +00001956 /* In case of a missing #endif. */
Neil Booth67821e32001-08-05 17:31:25 +00001957 pfile->state.skipping = 0;
Neil Booth29401c32001-08-22 20:37:20 +00001958
Neil Boothaf0d16c2002-04-22 17:48:02 +00001959 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
Neil Booth29401c32001-08-22 20:37:20 +00001960 pfile->buffer = buffer->prev;
1961
Neil Booth26aea072003-04-19 00:22:51 +00001962 free (buffer->notes);
1963
Neil Boothaf0d16c2002-04-22 17:48:02 +00001964 /* Free the buffer object now; we may want to push a new buffer
1965 in _cpp_push_next_include_file. */
1966 obstack_free (&pfile->buffer_ob, buffer);
Neil Booth29401c32001-08-22 20:37:20 +00001967
Neil Boothaf0d16c2002-04-22 17:48:02 +00001968 if (inc)
1969 {
1970 _cpp_pop_file_buffer (pfile, inc);
1971
Per Bothner40de9f72003-10-02 07:30:34 +00001972 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
Neil Boothaf0d16c2002-04-22 17:48:02 +00001973 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00001974}
1975
Kazu Hirata05713b82002-09-15 18:24:08 +00001976/* Enter all recognized directives in the hash table. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00001977void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001978_cpp_init_directives (cpp_reader *pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00001979{
Neil Booth766ee682001-01-29 19:20:12 +00001980 unsigned int i;
Neil Booth93c803682000-10-28 17:59:06 +00001981 cpp_hashnode *node;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001982
John David Anglin37b85242001-03-02 01:11:50 +00001983 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
Neil Booth93c803682000-10-28 17:59:06 +00001984 {
Neil Booth766ee682001-01-29 19:20:12 +00001985 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
Zack Weinberg4977bab2002-12-16 18:23:00 +00001986 node->is_directive = 1;
1987 node->directive_index = i;
Neil Booth93c803682000-10-28 17:59:06 +00001988 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00001989}