blob: b2b6b32e6c1599549dbed13654ef3c72e188572a [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. */
Zack Weinberg21b11492004-09-09 19:16:56 +000047 bool is_nspace;
48 bool is_internal;
Neil Bootha5da89c2001-10-14 17:44:00 +000049 union {
50 pragma_cb handler;
51 struct pragma_entry *space;
52 } u;
53};
54
Neil Booth93c803682000-10-28 17:59:06 +000055/* Values for the origin field of struct directive. KANDR directives
56 come from traditional (K&R) C. STDC89 directives come from the
57 1989 C standard. EXTENSION directives are extensions. */
58#define KANDR 0
59#define STDC89 1
60#define EXTENSION 2
61
62/* Values for the flags field of struct directive. COND indicates a
63 conditional; IF_COND an opening conditional. INCL means to treat
64 "..." and <...> as q-char and h-char sequences respectively. IN_I
65 means this directive should be handled even if -fpreprocessed is in
Neil Booth1a769162002-06-11 05:36:17 +000066 effect (these are the directives with callback hooks).
67
Neil Boothd97371e2002-06-18 06:27:40 +000068 EXPAND is set on directives that are always macro-expanded. */
Neil Booth93c803682000-10-28 17:59:06 +000069#define COND (1 << 0)
70#define IF_COND (1 << 1)
71#define INCL (1 << 2)
72#define IN_I (1 << 3)
Neil Booth1a769162002-06-11 05:36:17 +000073#define EXPAND (1 << 4)
Neil Booth93c803682000-10-28 17:59:06 +000074
75/* Defines one #-directive, including how to handle it. */
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000076typedef void (*directive_handler) (cpp_reader *);
Neil Booth93c803682000-10-28 17:59:06 +000077typedef struct directive directive;
78struct directive
79{
80 directive_handler handler; /* Function to handle directive. */
Neil Booth562a5c22002-04-21 18:46:42 +000081 const uchar *name; /* Name of directive. */
Neil Booth93c803682000-10-28 17:59:06 +000082 unsigned short length; /* Length of name. */
83 unsigned char origin; /* Origin of directive. */
84 unsigned char flags; /* Flags describing this directive. */
85};
86
Zack Weinberg1316f1f2000-02-06 07:53:50 +000087/* Forward declarations. */
88
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000089static void skip_rest_of_line (cpp_reader *);
90static void check_eol (cpp_reader *);
91static void start_directive (cpp_reader *);
92static void prepare_directive_trad (cpp_reader *);
93static void end_directive (cpp_reader *, int);
94static void directive_diagnostics (cpp_reader *, const directive *, int);
95static void run_directive (cpp_reader *, int, const char *, size_t);
96static char *glue_header_name (cpp_reader *);
97static const char *parse_include (cpp_reader *, int *);
98static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
99static unsigned int read_flag (cpp_reader *, unsigned int);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000100static int strtoul_for_line (const uchar *, unsigned int, unsigned long *);
101static void do_diagnostic (cpp_reader *, int, int);
102static cpp_hashnode *lex_macro_node (cpp_reader *);
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000103static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000104static void do_include_common (cpp_reader *, enum include_type);
105static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
106 const cpp_hashnode *);
107static struct pragma_entry *insert_pragma_entry (cpp_reader *,
108 struct pragma_entry **,
109 const cpp_hashnode *,
Zack Weinberg21b11492004-09-09 19:16:56 +0000110 pragma_cb,
111 bool);
112static void register_pragma (cpp_reader *, const char *, const char *,
113 pragma_cb, bool);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000114static int count_registered_pragmas (struct pragma_entry *);
115static char ** save_registered_pragmas (struct pragma_entry *, char **);
116static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
117 char **);
118static void do_pragma_once (cpp_reader *);
119static void do_pragma_poison (cpp_reader *);
120static void do_pragma_system_header (cpp_reader *);
121static void do_pragma_dependency (cpp_reader *);
122static void do_linemarker (cpp_reader *);
123static const cpp_token *get_token_no_padding (cpp_reader *);
124static const cpp_token *get__Pragma_string (cpp_reader *);
125static void destringize_and_run (cpp_reader *, const cpp_string *);
126static int parse_answer (cpp_reader *, struct answer **, int);
127static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
128static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
129static void handle_assertion (cpp_reader *, const char *, int);
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +0000130
Zack Weinberg168d3732000-03-14 06:34:11 +0000131/* This is the table of directive handlers. It is ordered by
132 frequency of occurrence; the numbers at the end are directive
133 counts from all the source code I have lying around (egcs and libc
134 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
Neil Booth93c803682000-10-28 17:59:06 +0000135 pcmcia-cs-3.0.9). This is no longer important as directive lookup
136 is now O(1). All extensions other than #warning and #include_next
137 are deprecated. The name is where the extension appears to have
138 come from. */
Jeff Lawe9a25f71997-11-02 14:19:36 -0700139
Neil Booth93c803682000-10-28 17:59:06 +0000140#define DIRECTIVE_TABLE \
141D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000142D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000143D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
144D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
Neil Booth1a769162002-06-11 05:36:17 +0000145D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000146D(else, T_ELSE, KANDR, COND) /* 9863 */ \
147D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
148D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
Neil Booth1a769162002-06-11 05:36:17 +0000149D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
150D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000151D(error, T_ERROR, STDC89, 0) /* 475 */ \
152D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
153D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000154D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000155D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000156D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
Neil Booth93c803682000-10-28 17:59:06 +0000157D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
158D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
Neil Booth74d06cf2002-07-17 21:31:42 +0000159D(sccs, T_SCCS, EXTENSION, 0) /* 0 SVR4? */
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000160
Zack Weinberg168d3732000-03-14 06:34:11 +0000161/* Use the table to generate a series of prototypes, an enum for the
162 directive names, and an array of directive handlers. */
163
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000164#define D(name, t, o, f) static void do_##name (cpp_reader *);
Zack Weinberg168d3732000-03-14 06:34:11 +0000165DIRECTIVE_TABLE
166#undef D
167
Zack Weinberg041c3192000-07-04 01:58:21 +0000168#define D(n, tag, o, f) tag,
Zack Weinberg168d3732000-03-14 06:34:11 +0000169enum
170{
171 DIRECTIVE_TABLE
172 N_DIRECTIVES
Per Bothner7f2935c1995-03-16 13:59:07 -0800173};
Zack Weinberg168d3732000-03-14 06:34:11 +0000174#undef D
175
Zack Weinberg041c3192000-07-04 01:58:21 +0000176#define D(name, t, origin, flags) \
Kaveh R. Ghazi9a238582003-06-16 19:14:22 +0000177{ do_##name, (const uchar *) #name, \
178 sizeof #name - 1, origin, flags },
Neil Booth93c803682000-10-28 17:59:06 +0000179static const directive dtable[] =
Zack Weinberg168d3732000-03-14 06:34:11 +0000180{
181DIRECTIVE_TABLE
182};
183#undef D
184#undef DIRECTIVE_TABLE
Per Bothner7f2935c1995-03-16 13:59:07 -0800185
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000186/* Wrapper struct directive for linemarkers.
187 The origin is more or less true - the original K+R cpp
188 did use this notation in its preprocessed output. */
189static const directive linemarker_dir =
190{
191 do_linemarker, U"#", 1, KANDR, IN_I
192};
193
Neil Booth1a769162002-06-11 05:36:17 +0000194#define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
Neil Booth67821e32001-08-05 17:31:25 +0000195
Neil Booth93c803682000-10-28 17:59:06 +0000196/* Skip any remaining tokens in a directive. */
197static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000198skip_rest_of_line (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +0000199{
Neil Booth93c803682000-10-28 17:59:06 +0000200 /* Discard all stacked contexts. */
Neil Booth8128ccc2002-11-18 20:43:40 +0000201 while (pfile->context->prev)
Neil Booth93c803682000-10-28 17:59:06 +0000202 _cpp_pop_context (pfile);
203
Neil Boothb528a072000-11-12 11:46:21 +0000204 /* Sweep up all tokens remaining on the line. */
Neil Booth345894b2001-09-16 13:44:29 +0000205 if (! SEEN_EOL ())
206 while (_cpp_lex_token (pfile)->type != CPP_EOF)
207 ;
Neil Booth93c803682000-10-28 17:59:06 +0000208}
209
210/* Ensure there are no stray tokens at the end of a directive. */
211static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000212check_eol (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +0000213{
Neil Booth345894b2001-09-16 13:44:29 +0000214 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000215 cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
Neil Boothebef4e82002-04-14 18:42:47 +0000216 pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +0000217}
218
Neil Boothfe6c2db2000-11-15 19:25:22 +0000219/* Called when entering a directive, _Pragma or command-line directive. */
220static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000221start_directive (cpp_reader *pfile)
Zack Weinbergc5a04732000-04-25 19:32:36 +0000222{
Neil Booth7f2f1a62000-11-14 18:32:06 +0000223 /* Setup in-directive state. */
224 pfile->state.in_directive = 1;
225 pfile->state.save_comments = 0;
Zack Weinberg21b11492004-09-09 19:16:56 +0000226 pfile->directive_result.type = CPP_PADDING;
Neil Booth7f2f1a62000-11-14 18:32:06 +0000227
Neil Booth93c803682000-10-28 17:59:06 +0000228 /* Some handlers need the position of the # for diagnostics. */
Per Bothner500bee02004-04-22 19:22:27 -0700229 pfile->directive_line = pfile->line_table->highest_line;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000230}
231
232/* Called when leaving a directive, _Pragma or command-line directive. */
233static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000234end_directive (cpp_reader *pfile, int skip_line)
Neil Boothfe6c2db2000-11-15 19:25:22 +0000235{
Neil Booth1a769162002-06-11 05:36:17 +0000236 if (CPP_OPTION (pfile, traditional))
237 {
Neil Boothd97371e2002-06-18 06:27:40 +0000238 /* Revert change of prepare_directive_trad. */
239 pfile->state.prevent_expansion--;
240
Neil Boothb66377c2002-06-13 21:16:00 +0000241 if (pfile->directive != &dtable[T_DEFINE])
Neil Booth1a769162002-06-11 05:36:17 +0000242 _cpp_remove_overlay (pfile);
243 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000244 /* We don't skip for an assembler #. */
Neil Boothb66377c2002-06-13 21:16:00 +0000245 else if (skip_line)
Neil Booth67821e32001-08-05 17:31:25 +0000246 {
247 skip_rest_of_line (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +0000248 if (!pfile->keep_tokens)
249 {
250 pfile->cur_run = &pfile->base_run;
251 pfile->cur_token = pfile->base_run.base;
252 }
Neil Booth67821e32001-08-05 17:31:25 +0000253 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000254
255 /* Restore state. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000256 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
257 pfile->state.in_directive = 0;
Neil Boothd97371e2002-06-18 06:27:40 +0000258 pfile->state.in_expression = 0;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000259 pfile->state.angled_headers = 0;
260 pfile->directive = 0;
261}
262
Neil Booth1a769162002-06-11 05:36:17 +0000263/* Prepare to handle the directive in pfile->directive. */
264static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000265prepare_directive_trad (cpp_reader *pfile)
Neil Booth1a769162002-06-11 05:36:17 +0000266{
Neil Booth951a0762002-06-27 06:01:58 +0000267 if (pfile->directive != &dtable[T_DEFINE])
Neil Booth1a769162002-06-11 05:36:17 +0000268 {
Neil Boothb66377c2002-06-13 21:16:00 +0000269 bool no_expand = (pfile->directive
270 && ! (pfile->directive->flags & EXPAND));
Neil Booth974c43f2002-06-13 06:25:28 +0000271 bool was_skipping = pfile->state.skipping;
Neil Booth1a769162002-06-11 05:36:17 +0000272
Neil Boothd97371e2002-06-18 06:27:40 +0000273 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
274 || pfile->directive == &dtable[T_ELIF]);
Neil Booth45f24922003-12-12 07:00:29 +0000275 if (pfile->state.in_expression)
276 pfile->state.skipping = false;
277
Neil Booth1a769162002-06-11 05:36:17 +0000278 if (no_expand)
279 pfile->state.prevent_expansion++;
Zack Weinberg43839642003-07-13 17:34:18 +0000280 _cpp_scan_out_logical_line (pfile, NULL);
Neil Booth1a769162002-06-11 05:36:17 +0000281 if (no_expand)
282 pfile->state.prevent_expansion--;
Neil Booth45f24922003-12-12 07:00:29 +0000283
Neil Booth974c43f2002-06-13 06:25:28 +0000284 pfile->state.skipping = was_skipping;
Neil Booth1a769162002-06-11 05:36:17 +0000285 _cpp_overlay_buffer (pfile, pfile->out.base,
286 pfile->out.cur - pfile->out.base);
287 }
Neil Boothd97371e2002-06-18 06:27:40 +0000288
289 /* Stop ISO C from expanding anything. */
290 pfile->state.prevent_expansion++;
Neil Booth1a769162002-06-11 05:36:17 +0000291}
292
Kazu Hiratada7d8302002-09-22 02:03:17 +0000293/* Output diagnostics for a directive DIR. INDENTED is nonzero if
Neil Booth18a9d8f2001-09-16 11:23:56 +0000294 the '#' was indented. */
Neil Booth18a9d8f2001-09-16 11:23:56 +0000295static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000296directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000297{
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000298 /* Issue -pedantic warnings for extensions. */
299 if (CPP_PEDANTIC (pfile)
300 && ! pfile->state.skipping
301 && dir->origin == EXTENSION)
John David Anglin0527bc42003-11-01 22:56:54 +0000302 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000303
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000304 /* Traditionally, a directive is ignored unless its # is in
305 column 1. Therefore in code intended to work with K+R
306 compilers, directives added by C89 must have their #
307 indented, and directives present in traditional C must not.
308 This is true even of directives in skipped conditional
309 blocks. #elif cannot be used at all. */
310 if (CPP_WTRADITIONAL (pfile))
311 {
312 if (dir == &dtable[T_ELIF])
John David Anglin0527bc42003-11-01 22:56:54 +0000313 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000314 "suggest not using #elif in traditional C");
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000315 else if (indented && dir->origin == KANDR)
John David Anglin0527bc42003-11-01 22:56:54 +0000316 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000317 "traditional C ignores #%s with the # indented",
318 dir->name);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000319 else if (!indented && dir->origin != KANDR)
John David Anglin0527bc42003-11-01 22:56:54 +0000320 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000321 "suggest hiding #%s from traditional C with an indented #",
322 dir->name);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000323 }
324}
325
Kazu Hiratada7d8302002-09-22 02:03:17 +0000326/* Check if we have a known directive. INDENTED is nonzero if the
Neil Booth18a9d8f2001-09-16 11:23:56 +0000327 '#' of the directive was indented. This function is in this file
328 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +0000329 nonzero if the line of tokens has been handled, zero if we should
Neil Booth18a9d8f2001-09-16 11:23:56 +0000330 continue processing the line. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000331int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000332_cpp_handle_directive (cpp_reader *pfile, int indented)
Neil Boothfe6c2db2000-11-15 19:25:22 +0000333{
Neil Boothfe6c2db2000-11-15 19:25:22 +0000334 const directive *dir = 0;
Neil Booth345894b2001-09-16 13:44:29 +0000335 const cpp_token *dname;
Neil Boothe808ec92002-02-27 07:24:53 +0000336 bool was_parsing_args = pfile->state.parsing_args;
Zack Weinbergc6e83802004-06-05 20:58:06 +0000337 bool was_discarding_output = pfile->state.discarding_output;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000338 int skip = 1;
339
Zack Weinbergc6e83802004-06-05 20:58:06 +0000340 if (was_discarding_output)
341 pfile->state.prevent_expansion = 0;
342
Neil Boothe808ec92002-02-27 07:24:53 +0000343 if (was_parsing_args)
344 {
345 if (CPP_OPTION (pfile, pedantic))
John David Anglin0527bc42003-11-01 22:56:54 +0000346 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothe808ec92002-02-27 07:24:53 +0000347 "embedding a directive within macro arguments is not portable");
348 pfile->state.parsing_args = 0;
349 pfile->state.prevent_expansion = 0;
350 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000351 start_directive (pfile);
Neil Booth345894b2001-09-16 13:44:29 +0000352 dname = _cpp_lex_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000353
Neil Booth345894b2001-09-16 13:44:29 +0000354 if (dname->type == CPP_NAME)
Neil Booth0d9f2342000-09-18 18:43:05 +0000355 {
Zack Weinberg4977bab2002-12-16 18:23:00 +0000356 if (dname->val.node->is_directive)
357 dir = &dtable[dname->val.node->directive_index];
Neil Booth93c803682000-10-28 17:59:06 +0000358 }
Kazu Hirata05713b82002-09-15 18:24:08 +0000359 /* We do not recognize the # followed by a number extension in
Neil Booth18a9d8f2001-09-16 11:23:56 +0000360 assembler code. */
Neil Booth345894b2001-09-16 13:44:29 +0000361 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth93c803682000-10-28 17:59:06 +0000362 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000363 dir = &linemarker_dir;
364 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
365 && ! pfile->state.skipping)
John David Anglin0527bc42003-11-01 22:56:54 +0000366 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothebef4e82002-04-14 18:42:47 +0000367 "style of line directive is a GCC extension");
Neil Booth0d9f2342000-09-18 18:43:05 +0000368 }
369
Neil Booth93c803682000-10-28 17:59:06 +0000370 if (dir)
Neil Booth0d9f2342000-09-18 18:43:05 +0000371 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000372 /* If we have a directive that is not an opening conditional,
373 invalidate any control macro. */
374 if (! (dir->flags & IF_COND))
375 pfile->mi_valid = false;
Neil Booth93c803682000-10-28 17:59:06 +0000376
Neil Booth18a9d8f2001-09-16 11:23:56 +0000377 /* Kluge alert. In order to be sure that code like this
378
379 #define HASH #
380 HASH define foo bar
381
382 does not cause '#define foo bar' to get executed when
383 compiled with -save-temps, we recognize directives in
384 -fpreprocessed mode only if the # is in column 1. cppmacro.c
Joseph Myersa1f300c2001-11-23 02:05:19 +0000385 puts a space in front of any '#' at the start of a macro. */
Neil Booth18a9d8f2001-09-16 11:23:56 +0000386 if (CPP_OPTION (pfile, preprocessed)
387 && (indented || !(dir->flags & IN_I)))
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000388 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000389 skip = 0;
390 dir = 0;
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000391 }
392 else
Neil Booth93c803682000-10-28 17:59:06 +0000393 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000394 /* In failed conditional groups, all non-conditional
395 directives are ignored. Before doing that, whether
396 skipping or not, we should lex angle-bracketed headers
397 correctly, and maybe output some diagnostics. */
398 pfile->state.angled_headers = dir->flags & INCL;
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000399 pfile->state.directive_wants_padding = dir->flags & INCL;
Neil Booth18a9d8f2001-09-16 11:23:56 +0000400 if (! CPP_OPTION (pfile, preprocessed))
401 directive_diagnostics (pfile, dir, indented);
402 if (pfile->state.skipping && !(dir->flags & COND))
403 dir = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000404 }
405 }
Neil Booth345894b2001-09-16 13:44:29 +0000406 else if (dname->type == CPP_EOF)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000407 ; /* CPP_EOF is the "null directive". */
408 else
Neil Booth0d9f2342000-09-18 18:43:05 +0000409 {
Neil Booth93c803682000-10-28 17:59:06 +0000410 /* An unknown directive. Don't complain about it in assembly
411 source: we don't know where the comments are, and # may
412 introduce assembler pseudo-ops. Don't complain about invalid
413 directives in skipped conditional groups (6.10 p4). */
Neil Boothbdb05a72000-11-26 17:31:13 +0000414 if (CPP_OPTION (pfile, lang) == CLK_ASM)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000415 skip = 0;
416 else if (!pfile->state.skipping)
John David Anglin0527bc42003-11-01 22:56:54 +0000417 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
Neil Booth345894b2001-09-16 13:44:29 +0000418 cpp_token_as_text (pfile, dname));
Neil Booth0d9f2342000-09-18 18:43:05 +0000419 }
420
Neil Boothd1a58682002-06-28 06:26:54 +0000421 pfile->directive = dir;
422 if (CPP_OPTION (pfile, traditional))
423 prepare_directive_trad (pfile);
424
Neil Booth18a9d8f2001-09-16 11:23:56 +0000425 if (dir)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000426 pfile->directive->handler (pfile);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000427 else if (skip == 0)
428 _cpp_backup_tokens (pfile, 1);
429
430 end_directive (pfile, skip);
Neil Boothe808ec92002-02-27 07:24:53 +0000431 if (was_parsing_args)
432 {
433 /* Restore state when within macro args. */
434 pfile->state.parsing_args = 2;
435 pfile->state.prevent_expansion = 1;
Neil Boothe808ec92002-02-27 07:24:53 +0000436 }
Zack Weinbergc6e83802004-06-05 20:58:06 +0000437 if (was_discarding_output)
438 pfile->state.prevent_expansion = 1;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000439 return skip;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000440}
441
Neil Booth93c803682000-10-28 17:59:06 +0000442/* Directive handler wrapper used by the command line option
Neil Booth26aea072003-04-19 00:22:51 +0000443 processor. BUF is \n terminated. */
Neil Booth93c803682000-10-28 17:59:06 +0000444static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000445run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000446{
Neil Booth562a5c22002-04-21 18:46:42 +0000447 cpp_push_buffer (pfile, (const uchar *) buf, count,
Per Bothner40de9f72003-10-02 07:30:34 +0000448 /* from_stage3 */ true);
Neil Booth8bfb1462002-08-14 20:17:55 +0000449 /* Disgusting hack. */
Zack Weinberg21b11492004-09-09 19:16:56 +0000450 if (dir_no == T_PRAGMA && pfile->buffer->prev)
Neil Booth8f9b4002003-07-29 22:26:13 +0000451 pfile->buffer->file = pfile->buffer->prev->file;
Neil Booth0bda4762000-12-11 07:45:16 +0000452 start_directive (pfile);
Neil Booth26aea072003-04-19 00:22:51 +0000453
454 /* This is a short-term fix to prevent a leading '#' being
455 interpreted as a directive. */
456 _cpp_clean_line (pfile);
457
Neil Boothf71aebb2001-05-27 18:06:00 +0000458 pfile->directive = &dtable[dir_no];
Neil Booth1a769162002-06-11 05:36:17 +0000459 if (CPP_OPTION (pfile, traditional))
460 prepare_directive_trad (pfile);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000461 pfile->directive->handler (pfile);
Neil Booth0bda4762000-12-11 07:45:16 +0000462 end_directive (pfile, 1);
Neil Booth8bfb1462002-08-14 20:17:55 +0000463 if (dir_no == T_PRAGMA)
Neil Booth8f9b4002003-07-29 22:26:13 +0000464 pfile->buffer->file = NULL;
Neil Boothef6e9582001-08-04 12:01:59 +0000465 _cpp_pop_buffer (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000466}
Per Bothner7f2935c1995-03-16 13:59:07 -0800467
Neil Booth93c803682000-10-28 17:59:06 +0000468/* Checks for validity the macro name in #define, #undef, #ifdef and
469 #ifndef directives. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000470static cpp_hashnode *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000471lex_macro_node (cpp_reader *pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000472{
Neil Booth1a769162002-06-11 05:36:17 +0000473 const cpp_token *token = _cpp_lex_token (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000474
Zack Weinberg92936ec2000-07-19 20:18:08 +0000475 /* The token immediately after #define must be an identifier. That
Zack Weinbergb8363a22001-07-01 18:48:13 +0000476 identifier may not be "defined", per C99 6.10.8p4.
477 In C++, it may not be any of the "named operators" either,
478 per C++98 [lex.digraph], [lex.key].
479 Finally, the identifier may not have been poisoned. (In that case
Neil Booth1d63a282002-06-28 20:27:14 +0000480 the lexer has issued the error message for us.) */
Zack Weinbergb8363a22001-07-01 18:48:13 +0000481
Neil Booth1a769162002-06-11 05:36:17 +0000482 if (token->type == CPP_NAME)
483 {
484 cpp_hashnode *node = token->val.node;
485
486 if (node == pfile->spec_nodes.n_defined)
John David Anglin0527bc42003-11-01 22:56:54 +0000487 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth1a769162002-06-11 05:36:17 +0000488 "\"defined\" cannot be used as a macro name");
489 else if (! (node->flags & NODE_POISONED))
490 return node;
491 }
492 else if (token->flags & NAMED_OP)
John David Anglin0527bc42003-11-01 22:56:54 +0000493 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcbc69f82002-06-05 20:27:12 +0000494 "\"%s\" cannot be used as a macro name as it is an operator in C++",
Neil Booth1a769162002-06-11 05:36:17 +0000495 NODE_NAME (token->val.node));
496 else if (token->type == CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000497 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
Neil Booth1a769162002-06-11 05:36:17 +0000498 pfile->directive->name);
499 else
John David Anglin0527bc42003-11-01 22:56:54 +0000500 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
Zack Weinbergb8363a22001-07-01 18:48:13 +0000501
Neil Boothcbc69f82002-06-05 20:27:12 +0000502 return NULL;
Per Bothner7f2935c1995-03-16 13:59:07 -0800503}
Per Bothner7f2935c1995-03-16 13:59:07 -0800504
Neil Booth93c803682000-10-28 17:59:06 +0000505/* Process a #define directive. Most work is done in cppmacro.c. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000506static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000507do_define (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800508{
Neil Booth93c803682000-10-28 17:59:06 +0000509 cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000510
Neil Booth93c803682000-10-28 17:59:06 +0000511 if (node)
512 {
Neil Booth1d63a282002-06-28 20:27:14 +0000513 /* If we have been requested to expand comments into macros,
514 then re-enable saving of comments. */
515 pfile->state.save_comments =
516 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
517
Neil Booth93c803682000-10-28 17:59:06 +0000518 if (_cpp_create_definition (pfile, node))
519 if (pfile->cb.define)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000520 pfile->cb.define (pfile, pfile->directive_line, node);
Neil Booth93c803682000-10-28 17:59:06 +0000521 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800522}
523
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000524/* Handle #undef. Mark the identifier NT_VOID in the hash table. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000525static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000526do_undef (cpp_reader *pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000527{
Kazu Hiratadf383482002-05-22 22:02:16 +0000528 cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000529
Neil Booth45f24922003-12-12 07:00:29 +0000530 if (node)
Zack Weinberg041c3192000-07-04 01:58:21 +0000531 {
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000532 if (pfile->cb.undef)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000533 pfile->cb.undef (pfile, pfile->directive_line, node);
Zack Weinberg041c3192000-07-04 01:58:21 +0000534
Neil Booth45f24922003-12-12 07:00:29 +0000535 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
536 identifier is not currently defined as a macro name. */
537 if (node->type == NT_MACRO)
538 {
539 if (node->flags & NODE_WARN)
540 cpp_error (pfile, CPP_DL_WARNING,
541 "undefining \"%s\"", NODE_NAME (node));
Zack Weinberg041c3192000-07-04 01:58:21 +0000542
Neil Booth45f24922003-12-12 07:00:29 +0000543 if (CPP_OPTION (pfile, warn_unused_macros))
544 _cpp_warn_if_unused_macro (pfile, node, NULL);
Neil Bootha69cbaa2002-07-23 22:57:49 +0000545
Neil Booth45f24922003-12-12 07:00:29 +0000546 _cpp_free_definition (node);
547 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000548 }
Neil Booth45f24922003-12-12 07:00:29 +0000549
Neil Booth93c803682000-10-28 17:59:06 +0000550 check_eol (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000551}
552
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000553/* Undefine a single macro/assertion/whatever. */
554
555static int
Zack Weinbergc6e83802004-06-05 20:58:06 +0000556undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000557 void *data_p ATTRIBUTE_UNUSED)
558{
Zack Weinbergc6e83802004-06-05 20:58:06 +0000559 /* Body of _cpp_free_definition inlined here for speed.
560 Macros and assertions no longer have anything to free. */
561 h->type = NT_VOID;
562 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED);
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000563 return 1;
564}
565
566/* Undefine all macros and assertions. */
567
568void
569cpp_undef_all (cpp_reader *pfile)
570{
571 cpp_forall_identifiers (pfile, undefine_macros, NULL);
572}
573
574
Neil Booth93c803682000-10-28 17:59:06 +0000575/* Helper routine used by parse_include. Reinterpret the current line
576 as an h-char-sequence (< ... >); we are looking at the first token
Neil Booth74eb4b32003-04-21 19:21:59 +0000577 after the <. Returns a malloced filename. */
578static char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000579glue_header_name (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800580{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000581 const cpp_token *token;
Neil Booth74eb4b32003-04-21 19:21:59 +0000582 char *buffer;
Neil Booth2450e0b2002-02-23 20:21:39 +0000583 size_t len, total_len = 0, capacity = 1024;
Per Bothner7f2935c1995-03-16 13:59:07 -0800584
Neil Booth93c803682000-10-28 17:59:06 +0000585 /* To avoid lexed tokens overwriting our glued name, we can only
586 allocate from the string pool once we've lexed everything. */
Neil Booth74eb4b32003-04-21 19:21:59 +0000587 buffer = xmalloc (capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000588 for (;;)
Zack Weinberg168d3732000-03-14 06:34:11 +0000589 {
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000590 token = get_token_no_padding (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000591
Neil Booth74eb4b32003-04-21 19:21:59 +0000592 if (token->type == CPP_GREATER)
Neil Booth93c803682000-10-28 17:59:06 +0000593 break;
Neil Booth74eb4b32003-04-21 19:21:59 +0000594 if (token->type == CPP_EOF)
595 {
John David Anglin0527bc42003-11-01 22:56:54 +0000596 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
Neil Booth74eb4b32003-04-21 19:21:59 +0000597 break;
598 }
Neil Booth93c803682000-10-28 17:59:06 +0000599
Neil Booth59325652003-04-24 20:03:57 +0000600 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
Neil Booth2450e0b2002-02-23 20:21:39 +0000601 if (total_len + len > capacity)
Neil Booth93c803682000-10-28 17:59:06 +0000602 {
Neil Booth2450e0b2002-02-23 20:21:39 +0000603 capacity = (capacity + len) * 2;
Neil Booth74eb4b32003-04-21 19:21:59 +0000604 buffer = xrealloc (buffer, capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000605 }
606
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000607 if (token->flags & PREV_WHITE)
Neil Booth2450e0b2002-02-23 20:21:39 +0000608 buffer[total_len++] = ' ';
Neil Booth93c803682000-10-28 17:59:06 +0000609
Neil Booth74eb4b32003-04-21 19:21:59 +0000610 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len])
611 - (uchar *) buffer);
Neil Booth93c803682000-10-28 17:59:06 +0000612 }
613
Neil Booth74eb4b32003-04-21 19:21:59 +0000614 buffer[total_len] = '\0';
615 return buffer;
Neil Booth93c803682000-10-28 17:59:06 +0000616}
617
Neil Booth74eb4b32003-04-21 19:21:59 +0000618/* Returns the file name of #include, #include_next, #import and
619 #pragma dependency. The string is malloced and the caller should
620 free it. Returns NULL on error. */
621static const char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000622parse_include (cpp_reader *pfile, int *pangle_brackets)
Neil Booth93c803682000-10-28 17:59:06 +0000623{
Neil Booth74eb4b32003-04-21 19:21:59 +0000624 char *fname;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000625 const cpp_token *header;
Neil Booth93c803682000-10-28 17:59:06 +0000626
Neil Booth93c803682000-10-28 17:59:06 +0000627 /* Allow macro expansion. */
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000628 header = get_token_no_padding (pfile);
Neil Booth74eb4b32003-04-21 19:21:59 +0000629 if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
Neil Booth93c803682000-10-28 17:59:06 +0000630 {
Neil Booth6338b352003-04-23 22:44:06 +0000631 fname = xmalloc (header->val.str.len - 1);
632 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
633 fname[header->val.str.len - 2] = '\0';
Neil Booth74eb4b32003-04-21 19:21:59 +0000634 *pangle_brackets = header->type == CPP_HEADER_NAME;
Zack Weinberg041c3192000-07-04 01:58:21 +0000635 }
Neil Booth74eb4b32003-04-21 19:21:59 +0000636 else if (header->type == CPP_LESS)
Zack Weinberg041c3192000-07-04 01:58:21 +0000637 {
Neil Booth74eb4b32003-04-21 19:21:59 +0000638 fname = glue_header_name (pfile);
639 *pangle_brackets = 1;
640 }
641 else
642 {
643 const unsigned char *dir;
644
645 if (pfile->directive == &dtable[T_PRAGMA])
646 dir = U"pragma dependency";
647 else
648 dir = pfile->directive->name;
John David Anglin0527bc42003-11-01 22:56:54 +0000649 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
Neil Booth74eb4b32003-04-21 19:21:59 +0000650 dir);
651
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000652 return NULL;
Richard Kennercfb3ee11997-04-13 14:30:13 -0400653 }
654
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000655 check_eol (pfile);
Neil Booth74eb4b32003-04-21 19:21:59 +0000656 return fname;
Zack Weinberg168d3732000-03-14 06:34:11 +0000657}
658
Neil Boothba133c92001-03-15 07:57:13 +0000659/* Handle #include, #include_next and #import. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000660static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000661do_include_common (cpp_reader *pfile, enum include_type type)
Zack Weinberg168d3732000-03-14 06:34:11 +0000662{
Neil Booth74eb4b32003-04-21 19:21:59 +0000663 const char *fname;
664 int angle_brackets;
665
666 fname = parse_include (pfile, &angle_brackets);
667 if (!fname)
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000668 return;
Zack Weinberg168d3732000-03-14 06:34:11 +0000669
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000670 /* Prevent #include recursion. */
Per Bothner50f59cd2004-01-19 21:30:18 -0800671 if (pfile->line_table->depth >= CPP_STACK_MAX)
John David Anglin0527bc42003-11-01 22:56:54 +0000672 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
Neil Booth74eb4b32003-04-21 19:21:59 +0000673 else
Neil Booth09b82252001-07-29 22:27:20 +0000674 {
Neil Booth74eb4b32003-04-21 19:21:59 +0000675 /* Get out of macro context, if we are. */
676 skip_rest_of_line (pfile);
677
678 if (pfile->cb.include)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000679 pfile->cb.include (pfile, pfile->directive_line,
680 pfile->directive->name, fname, angle_brackets);
Neil Booth74eb4b32003-04-21 19:21:59 +0000681
Neil Booth8f9b4002003-07-29 22:26:13 +0000682 _cpp_stack_include (pfile, fname, angle_brackets, type);
Neil Booth09b82252001-07-29 22:27:20 +0000683 }
684
Kaveh R. Ghazifad205f2003-06-16 21:41:10 +0000685 free ((void *) fname);
Neil Boothba133c92001-03-15 07:57:13 +0000686}
687
688static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000689do_include (cpp_reader *pfile)
Neil Boothba133c92001-03-15 07:57:13 +0000690{
691 do_include_common (pfile, IT_INCLUDE);
Zack Weinberg168d3732000-03-14 06:34:11 +0000692}
693
Zack Weinberg711b8822000-07-18 00:59:49 +0000694static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000695do_import (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +0000696{
Neil Boothba133c92001-03-15 07:57:13 +0000697 do_include_common (pfile, IT_IMPORT);
Zack Weinberg168d3732000-03-14 06:34:11 +0000698}
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000699
Zack Weinberg711b8822000-07-18 00:59:49 +0000700static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000701do_include_next (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +0000702{
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000703 enum include_type type = IT_INCLUDE_NEXT;
704
705 /* If this is the primary source file, warn and use the normal
706 search logic. */
707 if (! pfile->buffer->prev)
708 {
John David Anglin0527bc42003-11-01 22:56:54 +0000709 cpp_error (pfile, CPP_DL_WARNING,
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000710 "#include_next in primary source file");
711 type = IT_INCLUDE;
712 }
713 do_include_common (pfile, type);
Per Bothner7f2935c1995-03-16 13:59:07 -0800714}
715
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000716/* Subroutine of do_linemarker. Read possible flags after file name.
717 LAST is the last flag seen; 0 if this is the first flag. Return the
718 flag if it is valid, 0 at the end of the directive. Otherwise
719 complain. */
Neil Booth642ce432000-12-07 23:17:56 +0000720static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000721read_flag (cpp_reader *pfile, unsigned int last)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000722{
Neil Booth345894b2001-09-16 13:44:29 +0000723 const cpp_token *token = _cpp_lex_token (pfile);
Jason Merrilld3a34a01999-08-14 00:42:07 +0000724
Neil Booth345894b2001-09-16 13:44:29 +0000725 if (token->type == CPP_NUMBER && token->val.str.len == 1)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000726 {
Neil Booth345894b2001-09-16 13:44:29 +0000727 unsigned int flag = token->val.str.text[0] - '0';
Neil Booth28e0f042000-12-09 12:06:37 +0000728
729 if (flag > last && flag <= 4
730 && (flag != 4 || last == 3)
731 && (flag != 2 || last == 0))
732 return flag;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000733 }
Neil Booth93c803682000-10-28 17:59:06 +0000734
Neil Booth345894b2001-09-16 13:44:29 +0000735 if (token->type != CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000736 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
Neil Booth345894b2001-09-16 13:44:29 +0000737 cpp_token_as_text (pfile, token));
Neil Booth93c803682000-10-28 17:59:06 +0000738 return 0;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000739}
740
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000741/* Subroutine of do_line and do_linemarker. Convert a number in STR,
742 of length LEN, to binary; store it in NUMP, and return 0 if the
743 number was well-formed, 1 if not. Temporary, hopefully. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000744static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000745strtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump)
Zack Weinberg041c3192000-07-04 01:58:21 +0000746{
747 unsigned long reg = 0;
Neil Booth562a5c22002-04-21 18:46:42 +0000748 uchar c;
Zack Weinberg041c3192000-07-04 01:58:21 +0000749 while (len--)
750 {
751 c = *str++;
752 if (!ISDIGIT (c))
753 return 1;
754 reg *= 10;
755 reg += c - '0';
756 }
757 *nump = reg;
758 return 0;
759}
760
Zack Weinberg5538ada1999-02-04 06:36:54 -0500761/* Interpret #line command.
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000762 Note that the filename string (if any) is a true string constant
763 (escapes are interpreted), unlike in #line. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000764static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000765do_line (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800766{
Per Bothner500bee02004-04-22 19:22:27 -0700767 const struct line_maps *line_table = pfile->line_table;
768 const struct line_map *map = &line_table->maps[line_table->used - 1];
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000769 const cpp_token *token;
Per Bothner12f9df42004-02-11 07:29:30 -0800770 const char *new_file = map->to_file;
Neil Boothbb74c962001-08-17 22:23:49 +0000771 unsigned long new_lineno;
Per Bothner7f2935c1995-03-16 13:59:07 -0800772
Neil Booth27e25642000-11-27 08:00:04 +0000773 /* C99 raised the minimum limit on #line numbers. */
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000774 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
Neil Booth18a9d8f2001-09-16 11:23:56 +0000775
Neil Booth93c803682000-10-28 17:59:06 +0000776 /* #line commands expand macros. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000777 token = cpp_get_token (pfile);
778 if (token->type != CPP_NUMBER
779 || strtoul_for_line (token->val.str.text, token->val.str.len,
780 &new_lineno))
Per Bothner7f2935c1995-03-16 13:59:07 -0800781 {
John David Anglin0527bc42003-11-01 22:56:54 +0000782 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +0000783 "\"%s\" after #line is not a positive integer",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000784 cpp_token_as_text (pfile, token));
Zack Weinberg9ec72912000-08-09 19:41:12 +0000785 return;
Kazu Hiratadf383482002-05-22 22:02:16 +0000786 }
Zack Weinberg5538ada1999-02-04 06:36:54 -0500787
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000788 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
John David Anglin0527bc42003-11-01 22:56:54 +0000789 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
Zack Weinberg5538ada1999-02-04 06:36:54 -0500790
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000791 token = cpp_get_token (pfile);
792 if (token->type == CPP_STRING)
Zack Weinberg5538ada1999-02-04 06:36:54 -0500793 {
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000794 cpp_string s = { 0, 0 };
Eric Christopher423e95e2004-02-12 02:25:03 +0000795 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
796 &s, false))
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000797 new_file = (const char *)s.text;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000798 check_eol (pfile);
799 }
800 else if (token->type != CPP_EOF)
801 {
John David Anglin0527bc42003-11-01 22:56:54 +0000802 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000803 cpp_token_as_text (pfile, token));
804 return;
805 }
Neil Booth93c803682000-10-28 17:59:06 +0000806
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000807 skip_rest_of_line (pfile);
808 _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
Per Bothner12f9df42004-02-11 07:29:30 -0800809 map->sysp);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000810}
811
812/* Interpret the # 44 "file" [flags] notation, which has slightly
813 different syntax and semantics from #line: Flags are allowed,
814 and we never complain about the line number being too big. */
815static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000816do_linemarker (cpp_reader *pfile)
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000817{
Per Bothner500bee02004-04-22 19:22:27 -0700818 const struct line_maps *line_table = pfile->line_table;
819 const struct line_map *map = &line_table->maps[line_table->used - 1];
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000820 const cpp_token *token;
Per Bothner12f9df42004-02-11 07:29:30 -0800821 const char *new_file = map->to_file;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000822 unsigned long new_lineno;
Per Bothner12f9df42004-02-11 07:29:30 -0800823 unsigned int new_sysp = map->sysp;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000824 enum lc_reason reason = LC_RENAME;
825 int flag;
826
827 /* Back up so we can get the number again. Putting this in
828 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
829 some circumstances, which can segfault. */
830 _cpp_backup_tokens (pfile, 1);
831
832 /* #line commands expand macros. */
833 token = cpp_get_token (pfile);
834 if (token->type != CPP_NUMBER
835 || strtoul_for_line (token->val.str.text, token->val.str.len,
836 &new_lineno))
837 {
John David Anglin0527bc42003-11-01 22:56:54 +0000838 cpp_error (pfile, CPP_DL_ERROR,
839 "\"%s\" after # is not a positive integer",
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000840 cpp_token_as_text (pfile, token));
841 return;
Kazu Hiratadf383482002-05-22 22:02:16 +0000842 }
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000843
844 token = cpp_get_token (pfile);
845 if (token->type == CPP_STRING)
846 {
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000847 cpp_string s = { 0, 0 };
Eric Christopher423e95e2004-02-12 02:25:03 +0000848 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
849 1, &s, false))
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000850 new_file = (const char *)s.text;
Eric Christophercf551fb2004-01-16 22:37:49 +0000851
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000852 new_sysp = 0;
853 flag = read_flag (pfile, 0);
854 if (flag == 1)
Neil Booth93c803682000-10-28 17:59:06 +0000855 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000856 reason = LC_ENTER;
857 /* Fake an include for cpp_included (). */
858 _cpp_fake_include (pfile, new_file);
859 flag = read_flag (pfile, flag);
Neil Booth93c803682000-10-28 17:59:06 +0000860 }
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000861 else if (flag == 2)
862 {
863 reason = LC_LEAVE;
864 flag = read_flag (pfile, flag);
865 }
866 if (flag == 3)
867 {
868 new_sysp = 1;
869 flag = read_flag (pfile, flag);
870 if (flag == 4)
871 new_sysp = 2;
Per Bothner12f9df42004-02-11 07:29:30 -0800872 pfile->buffer->sysp = new_sysp;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000873 }
874
Neil Boothfde84342001-08-06 21:07:41 +0000875 check_eol (pfile);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000876 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000877 else if (token->type != CPP_EOF)
Neil Booth27e25642000-11-27 08:00:04 +0000878 {
John David Anglin0527bc42003-11-01 22:56:54 +0000879 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000880 cpp_token_as_text (pfile, token));
Neil Booth27e25642000-11-27 08:00:04 +0000881 return;
882 }
Zack Weinberg941e09b1998-12-15 11:17:06 +0000883
Neil Boothbdcbe492001-09-13 20:05:17 +0000884 skip_rest_of_line (pfile);
Neil Boothbb74c962001-08-17 22:23:49 +0000885 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
Neil Booth27e25642000-11-27 08:00:04 +0000886}
887
Neil Booth67821e32001-08-05 17:31:25 +0000888/* Arrange the file_change callback. pfile->line has changed to
Neil Booth47d89cf2001-08-11 07:33:39 +0000889 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
Joseph Myersa1f300c2001-11-23 02:05:19 +0000890 header, 2 for a system header that needs to be extern "C" protected,
Neil Booth47d89cf2001-08-11 07:33:39 +0000891 and zero otherwise. */
Neil Bootheb1f4d92000-12-18 19:00:26 +0000892void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000893_cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
894 const char *to_file, unsigned int file_line,
895 unsigned int sysp)
Neil Booth27e25642000-11-27 08:00:04 +0000896{
Per Bothner12f9df42004-02-11 07:29:30 -0800897 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
898 to_file, file_line);
Per Bothner500bee02004-04-22 19:22:27 -0700899 if (map != NULL)
900 linemap_line_start (pfile->line_table, map->to_line, 127);
Neil Boothd82fc102001-08-02 23:03:31 +0000901
Neil Bootheb1f4d92000-12-18 19:00:26 +0000902 if (pfile->cb.file_change)
Per Bothner12f9df42004-02-11 07:29:30 -0800903 pfile->cb.file_change (pfile, map);
Per Bothner7f2935c1995-03-16 13:59:07 -0800904}
Zack Weinberg941e09b1998-12-15 11:17:06 +0000905
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000906/* Report a warning or error detected by the program we are
907 processing. Use the directive's tokens in the error message. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000908static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000909do_diagnostic (cpp_reader *pfile, int code, int print_dir)
Per Bothner7f2935c1995-03-16 13:59:07 -0800910{
Per Bothner12f9df42004-02-11 07:29:30 -0800911 if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0))
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000912 {
Neil Booth29b10742000-11-13 18:40:37 +0000913 if (print_dir)
914 fprintf (stderr, "#%s ", pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +0000915 pfile->state.prevent_expansion++;
916 cpp_output_line (pfile, stderr);
917 pfile->state.prevent_expansion--;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000918 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800919}
920
Neil Booth838f3132000-09-24 10:42:09 +0000921static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000922do_error (cpp_reader *pfile)
Neil Booth838f3132000-09-24 10:42:09 +0000923{
John David Anglin0527bc42003-11-01 22:56:54 +0000924 do_diagnostic (pfile, CPP_DL_ERROR, 1);
Neil Booth838f3132000-09-24 10:42:09 +0000925}
Per Bothner7f2935c1995-03-16 13:59:07 -0800926
Zack Weinberg711b8822000-07-18 00:59:49 +0000927static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000928do_warning (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800929{
Neil Booth2f878972001-04-08 10:01:18 +0000930 /* We want #warning diagnostics to be emitted in system headers too. */
John David Anglin0527bc42003-11-01 22:56:54 +0000931 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
Per Bothner7f2935c1995-03-16 13:59:07 -0800932}
933
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000934/* Report program identification. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000935static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000936do_ident (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800937{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000938 const cpp_token *str = cpp_get_token (pfile);
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000939
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000940 if (str->type != CPP_STRING)
John David Anglin0527bc42003-11-01 22:56:54 +0000941 cpp_error (pfile, CPP_DL_ERROR, "invalid #ident directive");
Neil Booth93c803682000-10-28 17:59:06 +0000942 else if (pfile->cb.ident)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000943 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000944
Neil Booth93c803682000-10-28 17:59:06 +0000945 check_eol (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -0800946}
947
Neil Bootha5da89c2001-10-14 17:44:00 +0000948/* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
949 matching entry, or NULL if none is found. The returned entry could
950 be the start of a namespace chain, or a pragma. */
951static struct pragma_entry *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000952lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
Nathan Sidwell82443372000-06-23 10:56:09 +0000953{
Neil Booth4b115ff2001-10-14 23:04:13 +0000954 while (chain && chain->pragma != pragma)
955 chain = chain->next;
Neil Bootha5da89c2001-10-14 17:44:00 +0000956
957 return chain;
958}
959
960/* Create and insert a pragma entry for NAME at the beginning of a
961 singly-linked CHAIN. If handler is NULL, it is a namespace,
Zack Weinberg21b11492004-09-09 19:16:56 +0000962 otherwise it is a pragma and its handler. If INTERNAL is true
963 this pragma is being inserted by libcpp itself. */
Neil Bootha5da89c2001-10-14 17:44:00 +0000964static struct pragma_entry *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000965insert_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain,
Zack Weinberg21b11492004-09-09 19:16:56 +0000966 const cpp_hashnode *pragma, pragma_cb handler,
967 bool internal)
Neil Bootha5da89c2001-10-14 17:44:00 +0000968{
969 struct pragma_entry *new;
970
971 new = (struct pragma_entry *)
972 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
Neil Booth4b115ff2001-10-14 23:04:13 +0000973 new->pragma = pragma;
Neil Bootha5da89c2001-10-14 17:44:00 +0000974 if (handler)
975 {
976 new->is_nspace = 0;
977 new->u.handler = handler;
978 }
979 else
980 {
981 new->is_nspace = 1;
982 new->u.space = NULL;
983 }
984
Zack Weinberg21b11492004-09-09 19:16:56 +0000985 new->is_internal = internal;
Neil Bootha5da89c2001-10-14 17:44:00 +0000986 new->next = *chain;
987 *chain = new;
988 return new;
989}
990
991/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
992 goes in the global namespace. HANDLER is the handler it will call,
Zack Weinberg21b11492004-09-09 19:16:56 +0000993 which must be non-NULL. INTERNAL is true if this is a pragma
994 registered by cpplib itself, false if it is registered via
995 cpp_register_pragma */
996static void
997register_pragma (cpp_reader *pfile, const char *space, const char *name,
998 pragma_cb handler, bool internal)
Nathan Sidwell82443372000-06-23 10:56:09 +0000999{
Neil Bootha5da89c2001-10-14 17:44:00 +00001000 struct pragma_entry **chain = &pfile->pragmas;
1001 struct pragma_entry *entry;
Neil Booth4b115ff2001-10-14 23:04:13 +00001002 const cpp_hashnode *node;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001003
Neil Bootha5da89c2001-10-14 17:44:00 +00001004 if (!handler)
1005 abort ();
1006
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001007 if (space)
1008 {
Neil Booth4b115ff2001-10-14 23:04:13 +00001009 node = cpp_lookup (pfile, U space, strlen (space));
1010 entry = lookup_pragma_entry (*chain, node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001011 if (!entry)
Zack Weinberg21b11492004-09-09 19:16:56 +00001012 entry = insert_pragma_entry (pfile, chain, node, NULL, internal);
Neil Bootha5da89c2001-10-14 17:44:00 +00001013 else if (!entry->is_nspace)
1014 goto clash;
1015 chain = &entry->u.space;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001016 }
1017
Neil Bootha5da89c2001-10-14 17:44:00 +00001018 /* Check for duplicates. */
Neil Booth4b115ff2001-10-14 23:04:13 +00001019 node = cpp_lookup (pfile, U name, strlen (name));
1020 entry = lookup_pragma_entry (*chain, node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001021 if (entry)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001022 {
Neil Bootha5da89c2001-10-14 17:44:00 +00001023 if (entry->is_nspace)
1024 clash:
John David Anglin0527bc42003-11-01 22:56:54 +00001025 cpp_error (pfile, CPP_DL_ICE,
Neil Bootha5da89c2001-10-14 17:44:00 +00001026 "registering \"%s\" as both a pragma and a pragma namespace",
Neil Booth4b115ff2001-10-14 23:04:13 +00001027 NODE_NAME (node));
Neil Bootha5da89c2001-10-14 17:44:00 +00001028 else if (space)
John David Anglin0527bc42003-11-01 22:56:54 +00001029 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
Neil Boothebef4e82002-04-14 18:42:47 +00001030 space, name);
Neil Bootha5da89c2001-10-14 17:44:00 +00001031 else
John David Anglin0527bc42003-11-01 22:56:54 +00001032 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001033 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001034 else
Zack Weinberg21b11492004-09-09 19:16:56 +00001035 insert_pragma_entry (pfile, chain, node, handler, internal);
1036}
1037
1038/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1039 goes in the global namespace. HANDLER is the handler it will call,
1040 which must be non-NULL. This function is exported from libcpp. */
1041void
1042cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
1043 pragma_cb handler)
1044{
1045 register_pragma (pfile, space, name, handler, false);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001046}
Neil Bootha5da89c2001-10-14 17:44:00 +00001047
1048/* Register the pragmas the preprocessor itself handles. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001049void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001050_cpp_init_internal_pragmas (cpp_reader *pfile)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001051{
Neil Bootha5da89c2001-10-14 17:44:00 +00001052 /* Pragmas in the global namespace. */
Zack Weinberg21b11492004-09-09 19:16:56 +00001053 register_pragma (pfile, 0, "once", do_pragma_once, true);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001054
Neil Bootha5da89c2001-10-14 17:44:00 +00001055 /* New GCC-specific pragmas should be put in the GCC namespace. */
Zack Weinberg21b11492004-09-09 19:16:56 +00001056 register_pragma (pfile, "GCC", "poison", do_pragma_poison, true);
1057 register_pragma (pfile, "GCC", "system_header", do_pragma_system_header, true);
1058 register_pragma (pfile, "GCC", "dependency", do_pragma_dependency, true);
Nathan Sidwell82443372000-06-23 10:56:09 +00001059}
Per Bothner7f2935c1995-03-16 13:59:07 -08001060
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001061/* Return the number of registered pragmas in PE. */
1062
1063static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001064count_registered_pragmas (struct pragma_entry *pe)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001065{
1066 int ct = 0;
1067 for (; pe != NULL; pe = pe->next)
1068 {
1069 if (pe->is_nspace)
1070 ct += count_registered_pragmas (pe->u.space);
1071 ct++;
1072 }
1073 return ct;
1074}
1075
1076/* Save into SD the names of the registered pragmas referenced by PE,
1077 and return a pointer to the next free space in SD. */
1078
1079static char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001080save_registered_pragmas (struct pragma_entry *pe, char **sd)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001081{
1082 for (; pe != NULL; pe = pe->next)
1083 {
1084 if (pe->is_nspace)
1085 sd = save_registered_pragmas (pe->u.space, sd);
1086 *sd++ = xmemdup (HT_STR (&pe->pragma->ident),
1087 HT_LEN (&pe->pragma->ident),
1088 HT_LEN (&pe->pragma->ident) + 1);
1089 }
1090 return sd;
1091}
1092
1093/* Return a newly-allocated array which saves the names of the
1094 registered pragmas. */
1095
1096char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001097_cpp_save_pragma_names (cpp_reader *pfile)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001098{
1099 int ct = count_registered_pragmas (pfile->pragmas);
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001100 char **result = XNEWVEC (char *, ct);
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001101 (void) save_registered_pragmas (pfile->pragmas, result);
1102 return result;
1103}
1104
1105/* Restore from SD the names of the registered pragmas referenced by PE,
1106 and return a pointer to the next unused name in SD. */
1107
1108static char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001109restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1110 char **sd)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001111{
1112 for (; pe != NULL; pe = pe->next)
1113 {
1114 if (pe->is_nspace)
1115 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1116 pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd));
1117 free (*sd);
1118 sd++;
1119 }
1120 return sd;
1121}
1122
1123/* Restore the names of the registered pragmas from SAVED. */
1124
1125void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001126_cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001127{
1128 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1129 free (saved);
1130}
1131
Neil Bootha5da89c2001-10-14 17:44:00 +00001132/* Pragmata handling. We handle some, and pass the rest on to the
1133 front end. C99 defines three pragmas and says that no macro
1134 expansion is to be performed on them; whether or not macro
1135 expansion happens for other pragmas is implementation defined.
Zack Weinberg21b11492004-09-09 19:16:56 +00001136 This implementation never macro-expands the text after #pragma.
1137
1138 The library user has the option of deferring execution of
1139 #pragmas not handled by cpplib, in which case they are converted
1140 to CPP_PRAGMA tokens and inserted into the output stream. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001141static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001142do_pragma (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001143{
Neil Bootha5da89c2001-10-14 17:44:00 +00001144 const struct pragma_entry *p = NULL;
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001145 const cpp_token *token, *pragma_token = pfile->cur_token;
Neil Bootha5da89c2001-10-14 17:44:00 +00001146 unsigned int count = 1;
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001147
Neil Booth93c803682000-10-28 17:59:06 +00001148 pfile->state.prevent_expansion++;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001149
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001150 token = cpp_get_token (pfile);
1151 if (token->type == CPP_NAME)
Alexandre Oliva0172e2b2000-02-27 06:24:27 +00001152 {
Neil Booth4b115ff2001-10-14 23:04:13 +00001153 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001154 if (p && p->is_nspace)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001155 {
Neil Bootha5da89c2001-10-14 17:44:00 +00001156 count = 2;
1157 token = cpp_get_token (pfile);
1158 if (token->type == CPP_NAME)
Neil Booth4b115ff2001-10-14 23:04:13 +00001159 p = lookup_pragma_entry (p->u.space, token->val.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001160 else
1161 p = NULL;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001162 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001163 }
1164
Zack Weinberg21b11492004-09-09 19:16:56 +00001165 if (p && (p->is_internal || !CPP_OPTION (pfile, defer_pragmas)))
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001166 {
1167 /* Since the handler below doesn't get the line number, that it
1168 might need for diagnostics, make sure it has the right
1169 numbers in place. */
1170 if (pfile->cb.line_change)
1171 (*pfile->cb.line_change) (pfile, pragma_token, false);
1172 (*p->u.handler) (pfile);
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001173 }
Zack Weinberg21b11492004-09-09 19:16:56 +00001174 else if (CPP_OPTION (pfile, defer_pragmas))
1175 {
1176 /* Squirrel away the pragma text. Pragmas are newline-terminated. */
1177 const uchar *line_start, *line_end;
1178 uchar *s;
1179 cpp_string body;
1180 cpp_token *ptok;
1181
1182 _cpp_backup_tokens (pfile, count);
1183 line_start = CPP_BUFFER (pfile)->cur;
1184 line_end = ustrchr (line_start, '\n');
1185
1186 body.len = (line_end - line_start) + 1;
1187 s = _cpp_unaligned_alloc (pfile, body.len + 1);
1188 memcpy (s, line_start, body.len);
1189 s[body.len] = '\0';
1190 body.text = s;
1191
1192 /* Create a CPP_PRAGMA token. */
1193 ptok = &pfile->directive_result;
1194 ptok->src_loc = pragma_token->src_loc;
1195 ptok->type = CPP_PRAGMA;
1196 ptok->flags = pragma_token->flags | NO_EXPAND;
1197 ptok->val.str = body;
1198 }
Neil Boothd82fc102001-08-02 23:03:31 +00001199 else if (pfile->cb.def_pragma)
Neil Boothbdcbe492001-09-13 20:05:17 +00001200 {
1201 _cpp_backup_tokens (pfile, count);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001202 pfile->cb.def_pragma (pfile, pfile->directive_line);
Neil Boothbdcbe492001-09-13 20:05:17 +00001203 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001204
Neil Booth97293892001-09-14 22:04:46 +00001205 pfile->state.prevent_expansion--;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001206}
1207
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001208/* Handle #pragma once. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001209static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001210do_pragma_once (cpp_reader *pfile)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001211{
Neil Booth642ce432000-12-07 23:17:56 +00001212 if (pfile->buffer->prev == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001213 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
Neil Booth93c803682000-10-28 17:59:06 +00001214
1215 check_eol (pfile);
Neil Booth49634b32003-08-02 16:29:46 +00001216 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001217}
1218
Neil Boothc3bf3e62002-05-09 17:14:22 +00001219/* Handle #pragma GCC poison, to poison one or more identifiers so
1220 that the lexer produces a hard error for each subsequent usage. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001221static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001222do_pragma_poison (cpp_reader *pfile)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001223{
Neil Booth345894b2001-09-16 13:44:29 +00001224 const cpp_token *tok;
Zack Weinbergf8f769e2000-05-28 05:56:38 +00001225 cpp_hashnode *hp;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001226
Neil Booth93c803682000-10-28 17:59:06 +00001227 pfile->state.poisoned_ok = 1;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001228 for (;;)
1229 {
Neil Booth345894b2001-09-16 13:44:29 +00001230 tok = _cpp_lex_token (pfile);
1231 if (tok->type == CPP_EOF)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001232 break;
Neil Booth345894b2001-09-16 13:44:29 +00001233 if (tok->type != CPP_NAME)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001234 {
John David Anglin0527bc42003-11-01 22:56:54 +00001235 cpp_error (pfile, CPP_DL_ERROR,
1236 "invalid #pragma GCC poison directive");
Neil Booth93c803682000-10-28 17:59:06 +00001237 break;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001238 }
1239
Neil Booth345894b2001-09-16 13:44:29 +00001240 hp = tok->val.node;
Neil Booth93c803682000-10-28 17:59:06 +00001241 if (hp->flags & NODE_POISONED)
1242 continue;
1243
1244 if (hp->type == NT_MACRO)
John David Anglin0527bc42003-11-01 22:56:54 +00001245 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
Neil Boothebef4e82002-04-14 18:42:47 +00001246 NODE_NAME (hp));
Neil Booth93c803682000-10-28 17:59:06 +00001247 _cpp_free_definition (hp);
1248 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001249 }
Neil Booth93c803682000-10-28 17:59:06 +00001250 pfile->state.poisoned_ok = 0;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001251}
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001252
1253/* Mark the current header as a system header. This will suppress
1254 some categories of warnings (notably those from -pedantic). It is
1255 intended for use in system libraries that cannot be implemented in
1256 conforming C, but cannot be certain that their headers appear in a
1257 system include directory. To prevent abuse, it is rejected in the
1258 primary source file. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001259static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001260do_pragma_system_header (cpp_reader *pfile)
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001261{
Neil Booth614c7d32000-12-04 07:32:04 +00001262 cpp_buffer *buffer = pfile->buffer;
1263
1264 if (buffer->prev == 0)
John David Anglin0527bc42003-11-01 22:56:54 +00001265 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +00001266 "#pragma system_header ignored outside include file");
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001267 else
Neil Boothd82fc102001-08-02 23:03:31 +00001268 {
1269 check_eol (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +00001270 skip_rest_of_line (pfile);
Neil Boothd82fc102001-08-02 23:03:31 +00001271 cpp_make_system_header (pfile, 1, 0);
1272 }
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001273}
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001274
1275/* Check the modified date of the current include file against a specified
1276 file. Issue a diagnostic, if the specified file is newer. We use this to
1277 determine if a fixed header should be refixed. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001278static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001279do_pragma_dependency (cpp_reader *pfile)
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001280{
Neil Booth74eb4b32003-04-21 19:21:59 +00001281 const char *fname;
1282 int angle_brackets, ordering;
Kazu Hiratadf383482002-05-22 22:02:16 +00001283
Neil Booth74eb4b32003-04-21 19:21:59 +00001284 fname = parse_include (pfile, &angle_brackets);
1285 if (!fname)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001286 return;
Zack Weinberg041c3192000-07-04 01:58:21 +00001287
Neil Booth74eb4b32003-04-21 19:21:59 +00001288 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001289 if (ordering < 0)
John David Anglin0527bc42003-11-01 22:56:54 +00001290 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001291 else if (ordering > 0)
1292 {
John David Anglin0527bc42003-11-01 22:56:54 +00001293 cpp_error (pfile, CPP_DL_WARNING,
1294 "current file is older than %s", fname);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001295 if (cpp_get_token (pfile)->type != CPP_EOF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001296 {
1297 _cpp_backup_tokens (pfile, 1);
John David Anglin0527bc42003-11-01 22:56:54 +00001298 do_diagnostic (pfile, CPP_DL_WARNING, 0);
Neil Boothbdcbe492001-09-13 20:05:17 +00001299 }
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001300 }
Neil Booth74eb4b32003-04-21 19:21:59 +00001301
Kaveh R. Ghazifad205f2003-06-16 21:41:10 +00001302 free ((void *) fname);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001303}
1304
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001305/* Get a token but skip padding. */
1306static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001307get_token_no_padding (cpp_reader *pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001308{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001309 for (;;)
1310 {
1311 const cpp_token *result = cpp_get_token (pfile);
1312 if (result->type != CPP_PADDING)
1313 return result;
1314 }
1315}
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001316
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001317/* Check syntax is "(string-literal)". Returns the string on success,
1318 or NULL on failure. */
1319static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001320get__Pragma_string (cpp_reader *pfile)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001321{
1322 const cpp_token *string;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001323
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001324 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1325 return NULL;
1326
1327 string = get_token_no_padding (pfile);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001328 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001329 return NULL;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001330
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001331 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1332 return NULL;
1333
1334 return string;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001335}
1336
Neil Booth87062812001-10-20 09:00:53 +00001337/* Destringize IN into a temporary buffer, by removing the first \ of
1338 \" and \\ sequences, and process the result as a #pragma directive. */
1339static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001340destringize_and_run (cpp_reader *pfile, const cpp_string *in)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001341{
1342 const unsigned char *src, *limit;
Neil Booth87062812001-10-20 09:00:53 +00001343 char *dest, *result;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001344
Neil Booth6338b352003-04-23 22:44:06 +00001345 dest = result = alloca (in->len - 1);
1346 src = in->text + 1 + (in->text[0] == 'L');
1347 limit = in->text + in->len - 1;
1348 while (src < limit)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001349 {
1350 /* We know there is a character following the backslash. */
1351 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1352 src++;
1353 *dest++ = *src++;
1354 }
Neil Booth26aea072003-04-19 00:22:51 +00001355 *dest = '\n';
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001356
Neil Booth8128ccc2002-11-18 20:43:40 +00001357 /* Ugh; an awful kludge. We are really not set up to be lexing
1358 tokens when in the middle of a macro expansion. Use a new
1359 context to force cpp_get_token to lex, and so skip_rest_of_line
1360 doesn't go beyond the end of the text. Also, remember the
1361 current lexing position so we can return to it later.
Neil Booth79ba5e32002-09-03 21:55:40 +00001362
Neil Booth8128ccc2002-11-18 20:43:40 +00001363 Something like line-at-a-time lexing should remove the need for
1364 this. */
1365 {
1366 cpp_context *saved_context = pfile->context;
1367 cpp_token *saved_cur_token = pfile->cur_token;
1368 tokenrun *saved_cur_run = pfile->cur_run;
1369
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001370 pfile->context = XNEW (cpp_context);
Neil Booth8128ccc2002-11-18 20:43:40 +00001371 pfile->context->macro = 0;
1372 pfile->context->prev = 0;
1373 run_directive (pfile, T_PRAGMA, result, dest - result);
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001374 XDELETE (pfile->context);
Neil Booth8128ccc2002-11-18 20:43:40 +00001375 pfile->context = saved_context;
1376 pfile->cur_token = saved_cur_token;
1377 pfile->cur_run = saved_cur_run;
Neil Booth8128ccc2002-11-18 20:43:40 +00001378 }
Neil Booth79ba5e32002-09-03 21:55:40 +00001379
1380 /* See above comment. For the moment, we'd like
1381
1382 token1 _Pragma ("foo") token2
1383
1384 to be output as
1385
1386 token1
1387 # 7 "file.c"
1388 #pragma foo
1389 # 7 "file.c"
1390 token2
1391
1392 Getting the line markers is a little tricky. */
1393 if (pfile->cb.line_change)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001394 pfile->cb.line_change (pfile, pfile->cur_token, false);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001395}
1396
Neil Booth87062812001-10-20 09:00:53 +00001397/* Handle the _Pragma operator. */
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001398void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001399_cpp_do__Pragma (cpp_reader *pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001400{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001401 const cpp_token *string = get__Pragma_string (pfile);
Zack Weinberg21b11492004-09-09 19:16:56 +00001402 pfile->directive_result.type = CPP_PADDING;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001403
Neil Booth79ba5e32002-09-03 21:55:40 +00001404 if (string)
1405 destringize_and_run (pfile, &string->val.str);
1406 else
John David Anglin0527bc42003-11-01 22:56:54 +00001407 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00001408 "_Pragma takes a parenthesized string literal");
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001409}
1410
Zack Weinberg21b11492004-09-09 19:16:56 +00001411/* Handle a pragma that the front end deferred until now. */
1412void
1413cpp_handle_deferred_pragma (cpp_reader *pfile, const cpp_string *s)
1414{
1415 cpp_context *saved_context = pfile->context;
1416 cpp_token *saved_cur_token = pfile->cur_token;
1417 tokenrun *saved_cur_run = pfile->cur_run;
1418 bool saved_defer_pragmas = CPP_OPTION (pfile, defer_pragmas);
1419
1420 pfile->context = XNEW (cpp_context);
1421 pfile->context->macro = 0;
1422 pfile->context->prev = 0;
1423 CPP_OPTION (pfile, defer_pragmas) = false;
1424
1425 run_directive (pfile, T_PRAGMA, s->text, s->len);
1426
1427 XDELETE (pfile->context);
1428 pfile->context = saved_context;
1429 pfile->cur_token = saved_cur_token;
1430 pfile->cur_run = saved_cur_run;
1431 CPP_OPTION (pfile, defer_pragmas) = saved_defer_pragmas;
1432}
1433
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001434/* Ignore #sccs on all systems. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001435static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001436do_sccs (cpp_reader *pfile ATTRIBUTE_UNUSED)
Per Bothner7f2935c1995-03-16 13:59:07 -08001437{
Per Bothner7f2935c1995-03-16 13:59:07 -08001438}
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001439
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001440/* Handle #ifdef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001441static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001442do_ifdef (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +00001443{
Neil Booth93c803682000-10-28 17:59:06 +00001444 int skip = 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001445
Neil Boothcef0d192001-07-26 06:02:47 +00001446 if (! pfile->state.skipping)
Neil Booth93c803682000-10-28 17:59:06 +00001447 {
1448 const cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +00001449
Neil Booth93c803682000-10-28 17:59:06 +00001450 if (node)
Neil Bootha69cbaa2002-07-23 22:57:49 +00001451 {
1452 skip = node->type != NT_MACRO;
1453 _cpp_mark_macro_used (node);
1454 check_eol (pfile);
1455 }
Neil Booth93c803682000-10-28 17:59:06 +00001456 }
1457
1458 push_conditional (pfile, skip, T_IFDEF, 0);
Zack Weinberg168d3732000-03-14 06:34:11 +00001459}
1460
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001461/* Handle #ifndef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001462static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001463do_ifndef (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +00001464{
Neil Booth93c803682000-10-28 17:59:06 +00001465 int skip = 1;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001466 const cpp_hashnode *node = 0;
Zack Weinberg168d3732000-03-14 06:34:11 +00001467
Neil Boothcef0d192001-07-26 06:02:47 +00001468 if (! pfile->state.skipping)
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001469 {
Neil Booth93c803682000-10-28 17:59:06 +00001470 node = lex_macro_node (pfile);
Geoffrey Keatingb43db0b2000-12-02 22:28:44 +00001471
1472 if (node)
Neil Bootha69cbaa2002-07-23 22:57:49 +00001473 {
1474 skip = node->type == NT_MACRO;
1475 _cpp_mark_macro_used (node);
1476 check_eol (pfile);
1477 }
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001478 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001479
Neil Booth93c803682000-10-28 17:59:06 +00001480 push_conditional (pfile, skip, T_IFNDEF, node);
Per Bothner7f2935c1995-03-16 13:59:07 -08001481}
1482
Neil Booth6d18adb2001-07-29 17:27:57 +00001483/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1484 pfile->mi_ind_cmacro so we can handle multiple-include
Kazu Hirata6356f892003-06-12 19:01:08 +00001485 optimizations. If macro expansion occurs in the expression, we
Neil Booth6d18adb2001-07-29 17:27:57 +00001486 cannot treat it as a controlling conditional, since the expansion
1487 could change in the future. That is handled by cpp_get_token. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001488static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001489do_if (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001490{
Neil Booth93c803682000-10-28 17:59:06 +00001491 int skip = 1;
Per Bothner7f2935c1995-03-16 13:59:07 -08001492
Neil Boothcef0d192001-07-26 06:02:47 +00001493 if (! pfile->state.skipping)
Neil Booth87ed1092002-04-28 19:42:54 +00001494 skip = _cpp_parse_expr (pfile) == false;
Neil Booth93c803682000-10-28 17:59:06 +00001495
Neil Booth6d18adb2001-07-29 17:27:57 +00001496 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
Per Bothner7f2935c1995-03-16 13:59:07 -08001497}
1498
Neil Boothb528a072000-11-12 11:46:21 +00001499/* Flip skipping state if appropriate and continue without changing
Zack Weinbergea4a4532000-05-29 16:19:32 +00001500 if_stack; this is so that the error message for missing #endif's
1501 etc. will point to the original #if. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001502static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001503do_else (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001504{
Neil Boothb528a072000-11-12 11:46:21 +00001505 cpp_buffer *buffer = pfile->buffer;
1506 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001507
1508 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001509 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
Neil Booth93c803682000-10-28 17:59:06 +00001510 else
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001511 {
Neil Booth93c803682000-10-28 17:59:06 +00001512 if (ifs->type == T_ELSE)
1513 {
John David Anglin0527bc42003-11-01 22:56:54 +00001514 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1515 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Booth93c803682000-10-28 17:59:06 +00001516 "the conditional began here");
1517 }
Neil Boothb528a072000-11-12 11:46:21 +00001518 ifs->type = T_ELSE;
1519
Neil Boothcef0d192001-07-26 06:02:47 +00001520 /* Skip any future (erroneous) #elses or #elifs. */
1521 pfile->state.skipping = ifs->skip_elses;
1522 ifs->skip_elses = true;
Neil Booth93c803682000-10-28 17:59:06 +00001523
1524 /* Invalidate any controlling macro. */
1525 ifs->mi_cmacro = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001526
Neil Boothcef0d192001-07-26 06:02:47 +00001527 /* Only check EOL if was not originally skipping. */
Phil Edwards909de5d2002-03-22 21:59:04 +00001528 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
Neil Boothcef0d192001-07-26 06:02:47 +00001529 check_eol (pfile);
1530 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001531}
1532
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001533/* Handle a #elif directive by not changing if_stack either. See the
Neil Booth93c803682000-10-28 17:59:06 +00001534 comment above do_else. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001535static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001536do_elif (cpp_reader *pfile)
Zack Weinbergea4a4532000-05-29 16:19:32 +00001537{
Neil Boothb528a072000-11-12 11:46:21 +00001538 cpp_buffer *buffer = pfile->buffer;
1539 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001540
1541 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001542 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
Neil Boothb528a072000-11-12 11:46:21 +00001543 else
Zack Weinbergea4a4532000-05-29 16:19:32 +00001544 {
Neil Boothb528a072000-11-12 11:46:21 +00001545 if (ifs->type == T_ELSE)
1546 {
John David Anglin0527bc42003-11-01 22:56:54 +00001547 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1548 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Boothb528a072000-11-12 11:46:21 +00001549 "the conditional began here");
1550 }
1551 ifs->type = T_ELIF;
1552
Neil Boothcef0d192001-07-26 06:02:47 +00001553 /* Only evaluate this if we aren't skipping elses. During
1554 evaluation, set skipping to false to get lexer warnings. */
1555 if (ifs->skip_elses)
1556 pfile->state.skipping = 1;
1557 else
Neil Boothb528a072000-11-12 11:46:21 +00001558 {
Neil Boothcef0d192001-07-26 06:02:47 +00001559 pfile->state.skipping = 0;
1560 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1561 ifs->skip_elses = ! pfile->state.skipping;
Neil Boothb528a072000-11-12 11:46:21 +00001562 }
Neil Boothcef0d192001-07-26 06:02:47 +00001563
1564 /* Invalidate any controlling macro. */
1565 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001566 }
Zack Weinbergea4a4532000-05-29 16:19:32 +00001567}
1568
Neil Boothcef0d192001-07-26 06:02:47 +00001569/* #endif pops the if stack and resets pfile->state.skipping. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001570static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001571do_endif (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001572{
Neil Boothb528a072000-11-12 11:46:21 +00001573 cpp_buffer *buffer = pfile->buffer;
1574 struct if_stack *ifs = buffer->if_stack;
Per Bothner7f2935c1995-03-16 13:59:07 -08001575
Zack Weinbergea4a4532000-05-29 16:19:32 +00001576 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001577 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
Per Bothner7f2935c1995-03-16 13:59:07 -08001578 else
1579 {
Neil Boothcef0d192001-07-26 06:02:47 +00001580 /* Only check EOL if was not originally skipping. */
Phil Edwards909de5d2002-03-22 21:59:04 +00001581 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
Neil Boothcef0d192001-07-26 06:02:47 +00001582 check_eol (pfile);
1583
Neil Booth93c803682000-10-28 17:59:06 +00001584 /* If potential control macro, we go back outside again. */
1585 if (ifs->next == 0 && ifs->mi_cmacro)
1586 {
Neil Booth6d18adb2001-07-29 17:27:57 +00001587 pfile->mi_valid = true;
Neil Booth93c803682000-10-28 17:59:06 +00001588 pfile->mi_cmacro = ifs->mi_cmacro;
1589 }
1590
Neil Boothb528a072000-11-12 11:46:21 +00001591 buffer->if_stack = ifs->next;
Neil Boothcef0d192001-07-26 06:02:47 +00001592 pfile->state.skipping = ifs->was_skipping;
Neil Booth2a967f32001-05-20 06:26:45 +00001593 obstack_free (&pfile->buffer_ob, ifs);
Per Bothner7f2935c1995-03-16 13:59:07 -08001594 }
Neil Booth93c803682000-10-28 17:59:06 +00001595}
Zack Weinberg041c3192000-07-04 01:58:21 +00001596
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001597/* Push an if_stack entry for a preprocessor conditional, and set
1598 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1599 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1600 we need to check here that we are at the top of the file. */
Zack Weinbergea4a4532000-05-29 16:19:32 +00001601static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001602push_conditional (cpp_reader *pfile, int skip, int type,
1603 const cpp_hashnode *cmacro)
Zack Weinbergea4a4532000-05-29 16:19:32 +00001604{
1605 struct if_stack *ifs;
Neil Boothb528a072000-11-12 11:46:21 +00001606 cpp_buffer *buffer = pfile->buffer;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001607
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001608 ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
Neil Booth50410422001-09-15 10:18:03 +00001609 ifs->line = pfile->directive_line;
Neil Boothb528a072000-11-12 11:46:21 +00001610 ifs->next = buffer->if_stack;
Neil Boothcef0d192001-07-26 06:02:47 +00001611 ifs->skip_elses = pfile->state.skipping || !skip;
1612 ifs->was_skipping = pfile->state.skipping;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001613 ifs->type = type;
Neil Booth6d18adb2001-07-29 17:27:57 +00001614 /* This condition is effectively a test for top-of-file. */
1615 if (pfile->mi_valid && pfile->mi_cmacro == 0)
Neil Booth93c803682000-10-28 17:59:06 +00001616 ifs->mi_cmacro = cmacro;
1617 else
1618 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001619
Neil Boothcef0d192001-07-26 06:02:47 +00001620 pfile->state.skipping = skip;
Neil Boothb528a072000-11-12 11:46:21 +00001621 buffer->if_stack = ifs;
Zack Weinberg7061aa51998-12-15 11:09:16 +00001622}
1623
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001624/* Read the tokens of the answer into the macro pool, in a directive
1625 of type TYPE. Only commit the memory if we intend it as permanent
1626 storage, i.e. the #assert case. Returns 0 on success, and sets
1627 ANSWERP to point to the answer. */
Neil Booth93c803682000-10-28 17:59:06 +00001628static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001629parse_answer (cpp_reader *pfile, struct answer **answerp, int type)
Zack Weinberg041c3192000-07-04 01:58:21 +00001630{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001631 const cpp_token *paren;
Neil Booth93c803682000-10-28 17:59:06 +00001632 struct answer *answer;
Neil Booth8c3b2692001-09-30 10:03:11 +00001633 unsigned int acount;
Zack Weinberg041c3192000-07-04 01:58:21 +00001634
Neil Booth93c803682000-10-28 17:59:06 +00001635 /* In a conditional, it is legal to not have an open paren. We
1636 should save the following token in this case. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001637 paren = cpp_get_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001638
1639 /* If not a paren, see if we're OK. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001640 if (paren->type != CPP_OPEN_PAREN)
Zack Weinberg041c3192000-07-04 01:58:21 +00001641 {
Neil Booth93c803682000-10-28 17:59:06 +00001642 /* In a conditional no answer is a test for any answer. It
1643 could be followed by any token. */
1644 if (type == T_IF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001645 {
1646 _cpp_backup_tokens (pfile, 1);
1647 return 0;
1648 }
Neil Booth93c803682000-10-28 17:59:06 +00001649
1650 /* #unassert with no answer is valid - it removes all answers. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001651 if (type == T_UNASSERT && paren->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +00001652 return 0;
1653
John David Anglin0527bc42003-11-01 22:56:54 +00001654 cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate");
Neil Booth93c803682000-10-28 17:59:06 +00001655 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001656 }
1657
Neil Booth8c3b2692001-09-30 10:03:11 +00001658 for (acount = 0;; acount++)
Zack Weinberg041c3192000-07-04 01:58:21 +00001659 {
Neil Booth8c3b2692001-09-30 10:03:11 +00001660 size_t room_needed;
1661 const cpp_token *token = cpp_get_token (pfile);
1662 cpp_token *dest;
Zack Weinberg041c3192000-07-04 01:58:21 +00001663
Neil Booth93c803682000-10-28 17:59:06 +00001664 if (token->type == CPP_CLOSE_PAREN)
1665 break;
Zack Weinberg041c3192000-07-04 01:58:21 +00001666
1667 if (token->type == CPP_EOF)
1668 {
John David Anglin0527bc42003-11-01 22:56:54 +00001669 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
Neil Booth93c803682000-10-28 17:59:06 +00001670 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001671 }
Neil Booth8c3b2692001-09-30 10:03:11 +00001672
1673 /* struct answer includes the space for one token. */
1674 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1675
1676 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1677 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1678
1679 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1680 *dest = *token;
1681
1682 /* Drop whitespace at start, for answer equivalence purposes. */
1683 if (acount == 0)
1684 dest->flags &= ~PREV_WHITE;
Zack Weinberg041c3192000-07-04 01:58:21 +00001685 }
1686
Neil Booth8c3b2692001-09-30 10:03:11 +00001687 if (acount == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001688 {
John David Anglin0527bc42003-11-01 22:56:54 +00001689 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
Neil Booth93c803682000-10-28 17:59:06 +00001690 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001691 }
1692
Neil Booth8c3b2692001-09-30 10:03:11 +00001693 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1694 answer->count = acount;
1695 answer->next = NULL;
Neil Booth93c803682000-10-28 17:59:06 +00001696 *answerp = answer;
Zack Weinberg041c3192000-07-04 01:58:21 +00001697
Neil Booth93c803682000-10-28 17:59:06 +00001698 return 0;
1699}
1700
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001701/* Parses an assertion directive of type TYPE, returning a pointer to
1702 the hash node of the predicate, or 0 on error. If an answer was
1703 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
Neil Booth93c803682000-10-28 17:59:06 +00001704static cpp_hashnode *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001705parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
Neil Booth93c803682000-10-28 17:59:06 +00001706{
1707 cpp_hashnode *result = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001708 const cpp_token *predicate;
Neil Booth93c803682000-10-28 17:59:06 +00001709
1710 /* We don't expand predicates or answers. */
1711 pfile->state.prevent_expansion++;
1712
Neil Booth93c803682000-10-28 17:59:06 +00001713 *answerp = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001714 predicate = cpp_get_token (pfile);
1715 if (predicate->type == CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +00001716 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001717 else if (predicate->type != CPP_NAME)
John David Anglin0527bc42003-11-01 22:56:54 +00001718 cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier");
Neil Booth93c803682000-10-28 17:59:06 +00001719 else if (parse_answer (pfile, answerp, type) == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001720 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001721 unsigned int len = NODE_LEN (predicate->val.node);
Neil Booth93c803682000-10-28 17:59:06 +00001722 unsigned char *sym = alloca (len + 1);
1723
1724 /* Prefix '#' to get it out of macro namespace. */
1725 sym[0] = '#';
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001726 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
Neil Booth93c803682000-10-28 17:59:06 +00001727 result = cpp_lookup (pfile, sym, len + 1);
Zack Weinberg041c3192000-07-04 01:58:21 +00001728 }
1729
Neil Booth93c803682000-10-28 17:59:06 +00001730 pfile->state.prevent_expansion--;
1731 return result;
Zack Weinberg041c3192000-07-04 01:58:21 +00001732}
1733
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001734/* Returns a pointer to the pointer to CANDIDATE in the answer chain,
Zack Weinberg041c3192000-07-04 01:58:21 +00001735 or a pointer to NULL if the answer is not in the chain. */
Neil Booth93c803682000-10-28 17:59:06 +00001736static struct answer **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001737find_answer (cpp_hashnode *node, const struct answer *candidate)
Zack Weinberg041c3192000-07-04 01:58:21 +00001738{
Neil Booth93c803682000-10-28 17:59:06 +00001739 unsigned int i;
Zack Weinberg041c3192000-07-04 01:58:21 +00001740 struct answer **result;
1741
1742 for (result = &node->value.answers; *result; result = &(*result)->next)
Neil Booth93c803682000-10-28 17:59:06 +00001743 {
1744 struct answer *answer = *result;
1745
1746 if (answer->count == candidate->count)
1747 {
1748 for (i = 0; i < answer->count; i++)
1749 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1750 break;
1751
1752 if (i == answer->count)
1753 break;
1754 }
1755 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001756
1757 return result;
1758}
1759
Neil Booth93c803682000-10-28 17:59:06 +00001760/* Test an assertion within a preprocessor conditional. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +00001761 nonzero on failure, zero on success. On success, the result of
Hans-Peter Nilsson24026452002-11-29 22:41:04 +00001762 the test is written into VALUE, otherwise the value 0. */
Neil Booth93c803682000-10-28 17:59:06 +00001763int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001764_cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
Neil Booth93c803682000-10-28 17:59:06 +00001765{
1766 struct answer *answer;
1767 cpp_hashnode *node;
1768
1769 node = parse_assertion (pfile, &answer, T_IF);
Hans-Peter Nilsson24026452002-11-29 22:41:04 +00001770
1771 /* For recovery, an erroneous assertion expression is handled as a
1772 failing assertion. */
1773 *value = 0;
1774
Neil Booth93c803682000-10-28 17:59:06 +00001775 if (node)
1776 *value = (node->type == NT_ASSERTION &&
1777 (answer == 0 || *find_answer (node, answer) != 0));
Neil Booth91318902002-05-26 18:42:21 +00001778 else if (pfile->cur_token[-1].type == CPP_EOF)
1779 _cpp_backup_tokens (pfile, 1);
Neil Booth93c803682000-10-28 17:59:06 +00001780
1781 /* We don't commit the memory for the answer - it's temporary only. */
1782 return node == 0;
1783}
1784
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001785/* Handle #assert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001786static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001787do_assert (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001788{
Zack Weinberg041c3192000-07-04 01:58:21 +00001789 struct answer *new_answer;
1790 cpp_hashnode *node;
Kazu Hiratadf383482002-05-22 22:02:16 +00001791
Neil Booth93c803682000-10-28 17:59:06 +00001792 node = parse_assertion (pfile, &new_answer, T_ASSERT);
Zack Weinberg041c3192000-07-04 01:58:21 +00001793 if (node)
1794 {
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001795 size_t answer_size;
1796
Neil Booth93c803682000-10-28 17:59:06 +00001797 /* Place the new answer in the answer list. First check there
1798 is not a duplicate. */
Zack Weinberg041c3192000-07-04 01:58:21 +00001799 new_answer->next = 0;
Neil Booth93c803682000-10-28 17:59:06 +00001800 if (node->type == NT_ASSERTION)
Zack Weinberg041c3192000-07-04 01:58:21 +00001801 {
Neil Booth93c803682000-10-28 17:59:06 +00001802 if (*find_answer (node, new_answer))
1803 {
John David Anglin0527bc42003-11-01 22:56:54 +00001804 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
Neil Boothebef4e82002-04-14 18:42:47 +00001805 NODE_NAME (node) + 1);
Neil Booth93c803682000-10-28 17:59:06 +00001806 return;
1807 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001808 new_answer->next = node->value.answers;
1809 }
Neil Booth8c3b2692001-09-30 10:03:11 +00001810
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001811 answer_size = sizeof (struct answer) + ((new_answer->count - 1)
1812 * sizeof (cpp_token));
1813 /* Commit or allocate storage for the object. */
1814 if (pfile->hash_table->alloc_subobject)
1815 {
1816 struct answer *temp_answer = new_answer;
1817 new_answer = pfile->hash_table->alloc_subobject (answer_size);
1818 memcpy (new_answer, temp_answer, answer_size);
1819 }
1820 else
1821 BUFF_FRONT (pfile->a_buff) += answer_size;
1822
Neil Booth93c803682000-10-28 17:59:06 +00001823 node->type = NT_ASSERTION;
Zack Weinberg041c3192000-07-04 01:58:21 +00001824 node->value.answers = new_answer;
Neil Booth8c3b2692001-09-30 10:03:11 +00001825 check_eol (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +00001826 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001827}
Zack Weinberg7061aa51998-12-15 11:09:16 +00001828
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001829/* Handle #unassert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001830static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001831do_unassert (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001832{
Zack Weinberg041c3192000-07-04 01:58:21 +00001833 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00001834 struct answer *answer;
Kazu Hiratadf383482002-05-22 22:02:16 +00001835
Neil Booth93c803682000-10-28 17:59:06 +00001836 node = parse_assertion (pfile, &answer, T_UNASSERT);
1837 /* It isn't an error to #unassert something that isn't asserted. */
1838 if (node && node->type == NT_ASSERTION)
Zack Weinberg7061aa51998-12-15 11:09:16 +00001839 {
Zack Weinberg041c3192000-07-04 01:58:21 +00001840 if (answer)
Neil Booth93c803682000-10-28 17:59:06 +00001841 {
1842 struct answer **p = find_answer (node, answer), *temp;
1843
1844 /* Remove the answer from the list. */
1845 temp = *p;
1846 if (temp)
1847 *p = temp->next;
1848
1849 /* Did we free the last answer? */
1850 if (node->value.answers == 0)
1851 node->type = NT_VOID;
Neil Booth8c3b2692001-09-30 10:03:11 +00001852
1853 check_eol (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001854 }
1855 else
1856 _cpp_free_definition (node);
Zack Weinberg7061aa51998-12-15 11:09:16 +00001857 }
Neil Booth93c803682000-10-28 17:59:06 +00001858
1859 /* We don't commit the memory for the answer - it's temporary only. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001860}
Per Bothner7f2935c1995-03-16 13:59:07 -08001861
Zack Weinberg45b966d2000-03-13 22:01:08 +00001862/* These are for -D, -U, -A. */
1863
1864/* Process the string STR as if it appeared as the body of a #define.
1865 If STR is just an identifier, define it with value 1.
1866 If STR has anything after the identifier, then it should
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001867 be identifier=definition. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001868void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001869cpp_define (cpp_reader *pfile, const char *str)
Zack Weinberg45b966d2000-03-13 22:01:08 +00001870{
1871 char *buf, *p;
1872 size_t count;
1873
Kazu Hiratadf383482002-05-22 22:02:16 +00001874 /* Copy the entire option so we can modify it.
Zack Weinberg45b966d2000-03-13 22:01:08 +00001875 Change the first "=" in the string to a space. If there is none,
Neil Booth86368122000-10-31 23:34:59 +00001876 tack " 1" on the end. */
1877
Neil Booth86368122000-10-31 23:34:59 +00001878 count = strlen (str);
Kaveh R. Ghazi703ad42b2003-07-19 14:47:15 +00001879 buf = alloca (count + 3);
Neil Booth86368122000-10-31 23:34:59 +00001880 memcpy (buf, str, count);
1881
1882 p = strchr (str, '=');
Zack Weinberg45b966d2000-03-13 22:01:08 +00001883 if (p)
Neil Booth86368122000-10-31 23:34:59 +00001884 buf[p - str] = ' ';
Zack Weinberg45b966d2000-03-13 22:01:08 +00001885 else
1886 {
Neil Booth86368122000-10-31 23:34:59 +00001887 buf[count++] = ' ';
1888 buf[count++] = '1';
Zack Weinberg45b966d2000-03-13 22:01:08 +00001889 }
Neil Booth26aea072003-04-19 00:22:51 +00001890 buf[count] = '\n';
Zack Weinberg45b966d2000-03-13 22:01:08 +00001891
Neil Booth29401c32001-08-22 20:37:20 +00001892 run_directive (pfile, T_DEFINE, buf, count);
Zack Weinberg2c8f0512000-08-29 18:37:37 +00001893}
1894
Neil Boothad2a0842000-12-17 00:13:54 +00001895/* Slight variant of the above for use by initialize_builtins. */
Zack Weinberg2c8f0512000-08-29 18:37:37 +00001896void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001897_cpp_define_builtin (cpp_reader *pfile, const char *str)
Zack Weinberg2c8f0512000-08-29 18:37:37 +00001898{
Neil Booth26aea072003-04-19 00:22:51 +00001899 size_t len = strlen (str);
1900 char *buf = alloca (len + 1);
1901 memcpy (buf, str, len);
1902 buf[len] = '\n';
1903 run_directive (pfile, T_DEFINE, buf, len);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001904}
1905
1906/* Process MACRO as if it appeared as the body of an #undef. */
1907void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001908cpp_undef (cpp_reader *pfile, const char *macro)
Zack Weinberg45b966d2000-03-13 22:01:08 +00001909{
Neil Booth26aea072003-04-19 00:22:51 +00001910 size_t len = strlen (macro);
1911 char *buf = alloca (len + 1);
1912 memcpy (buf, macro, len);
1913 buf[len] = '\n';
1914 run_directive (pfile, T_UNDEF, buf, len);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001915}
1916
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001917/* Process the string STR as if it appeared as the body of a #assert. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001918void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001919cpp_assert (cpp_reader *pfile, const char *str)
Zack Weinberg45b966d2000-03-13 22:01:08 +00001920{
Neil Booth86368122000-10-31 23:34:59 +00001921 handle_assertion (pfile, str, T_ASSERT);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001922}
1923
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001924/* Process STR as if it appeared as the body of an #unassert. */
Zack Weinberg0b22d651999-03-15 18:42:46 +00001925void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001926cpp_unassert (cpp_reader *pfile, const char *str)
Zack Weinberg0b22d651999-03-15 18:42:46 +00001927{
Neil Booth86368122000-10-31 23:34:59 +00001928 handle_assertion (pfile, str, T_UNASSERT);
Kazu Hiratadf383482002-05-22 22:02:16 +00001929}
Zack Weinberg0b22d651999-03-15 18:42:46 +00001930
Neil Booth86368122000-10-31 23:34:59 +00001931/* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1932static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001933handle_assertion (cpp_reader *pfile, const char *str, int type)
Neil Booth86368122000-10-31 23:34:59 +00001934{
1935 size_t count = strlen (str);
1936 const char *p = strchr (str, '=');
1937
Neil Booth26aea072003-04-19 00:22:51 +00001938 /* Copy the entire option so we can modify it. Change the first
1939 "=" in the string to a '(', and tack a ')' on the end. */
Kaveh R. Ghazi703ad42b2003-07-19 14:47:15 +00001940 char *buf = alloca (count + 2);
Neil Booth26aea072003-04-19 00:22:51 +00001941
1942 memcpy (buf, str, count);
Neil Booth86368122000-10-31 23:34:59 +00001943 if (p)
1944 {
Neil Booth86368122000-10-31 23:34:59 +00001945 buf[p - str] = '(';
1946 buf[count++] = ')';
Neil Booth86368122000-10-31 23:34:59 +00001947 }
Neil Booth26aea072003-04-19 00:22:51 +00001948 buf[count] = '\n';
1949 str = buf;
Neil Booth86368122000-10-31 23:34:59 +00001950
Neil Booth29401c32001-08-22 20:37:20 +00001951 run_directive (pfile, type, str, count);
Neil Booth86368122000-10-31 23:34:59 +00001952}
1953
Neil Booth7e96d762001-01-13 01:00:01 +00001954/* The number of errors for a given reader. */
1955unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001956cpp_errors (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00001957{
1958 return pfile->errors;
1959}
1960
1961/* The options structure. */
1962cpp_options *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001963cpp_get_options (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00001964{
1965 return &pfile->opts;
1966}
1967
1968/* The callbacks structure. */
1969cpp_callbacks *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001970cpp_get_callbacks (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00001971{
1972 return &pfile->cb;
1973}
1974
1975/* Copy the given callbacks structure to our own. */
1976void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001977cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
Neil Booth7e96d762001-01-13 01:00:01 +00001978{
1979 pfile->cb = *cb;
1980}
1981
Zack Weinbergc6e83802004-06-05 20:58:06 +00001982/* The dependencies structure. (Creates one if it hasn't already been.) */
1983struct deps *
1984cpp_get_deps (cpp_reader *pfile)
1985{
1986 if (!pfile->deps)
1987 pfile->deps = deps_init ();
1988 return pfile->deps;
1989}
1990
Neil Bootheb1f4d92000-12-18 19:00:26 +00001991/* Push a new buffer on the buffer stack. Returns the new buffer; it
1992 doesn't fail. It does not generate a file change call back; that
1993 is the responsibility of the caller. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00001994cpp_buffer *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001995cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
Per Bothner40de9f72003-10-02 07:30:34 +00001996 int from_stage3)
Zack Weinbergc71f8352000-07-05 05:33:57 +00001997{
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001998 cpp_buffer *new = XOBNEW (&pfile->buffer_ob, cpp_buffer);
Neil Booth93c803682000-10-28 17:59:06 +00001999
Neil Boothfde84342001-08-06 21:07:41 +00002000 /* Clears, amongst other things, if_stack and mi_cmacro. */
2001 memset (new, 0, sizeof (cpp_buffer));
Neil Boothad2a0842000-12-17 00:13:54 +00002002
Neil Booth26aea072003-04-19 00:22:51 +00002003 new->next_line = new->buf = buffer;
Neil Boothfde84342001-08-06 21:07:41 +00002004 new->rlimit = buffer + len;
Neil Booth26aea072003-04-19 00:22:51 +00002005 new->from_stage3 = from_stage3;
Neil Booth3cf35932000-12-05 23:42:43 +00002006 new->prev = pfile->buffer;
Neil Booth26aea072003-04-19 00:22:51 +00002007 new->need_line = true;
Neil Booth0bda4762000-12-11 07:45:16 +00002008
Neil Booth3cf35932000-12-05 23:42:43 +00002009 pfile->buffer = new;
Eric Christophercf551fb2004-01-16 22:37:49 +00002010
Zack Weinbergc71f8352000-07-05 05:33:57 +00002011 return new;
2012}
2013
Neil Boothaf0d16c2002-04-22 17:48:02 +00002014/* Pops a single buffer, with a file change call-back if appropriate.
2015 Then pushes the next -include file, if any remain. */
Neil Boothef6e9582001-08-04 12:01:59 +00002016void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002017_cpp_pop_buffer (cpp_reader *pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002018{
Neil Boothfde84342001-08-06 21:07:41 +00002019 cpp_buffer *buffer = pfile->buffer;
Neil Booth8f9b4002003-07-29 22:26:13 +00002020 struct _cpp_file *inc = buffer->file;
Neil Boothad2a0842000-12-17 00:13:54 +00002021 struct if_stack *ifs;
Zack Weinbergc71f8352000-07-05 05:33:57 +00002022
Neil Boothfde84342001-08-06 21:07:41 +00002023 /* Walk back up the conditional stack till we reach its level at
2024 entry to this file, issuing error messages. */
2025 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
John David Anglin0527bc42003-11-01 22:56:54 +00002026 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Boothfde84342001-08-06 21:07:41 +00002027 "unterminated #%s", dtable[ifs->type].name);
2028
Neil Booth97293892001-09-14 22:04:46 +00002029 /* In case of a missing #endif. */
Neil Booth67821e32001-08-05 17:31:25 +00002030 pfile->state.skipping = 0;
Neil Booth29401c32001-08-22 20:37:20 +00002031
Neil Boothaf0d16c2002-04-22 17:48:02 +00002032 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
Neil Booth29401c32001-08-22 20:37:20 +00002033 pfile->buffer = buffer->prev;
2034
Neil Booth26aea072003-04-19 00:22:51 +00002035 free (buffer->notes);
2036
Neil Boothaf0d16c2002-04-22 17:48:02 +00002037 /* Free the buffer object now; we may want to push a new buffer
2038 in _cpp_push_next_include_file. */
2039 obstack_free (&pfile->buffer_ob, buffer);
Neil Booth29401c32001-08-22 20:37:20 +00002040
Neil Boothaf0d16c2002-04-22 17:48:02 +00002041 if (inc)
2042 {
2043 _cpp_pop_file_buffer (pfile, inc);
2044
Per Bothner40de9f72003-10-02 07:30:34 +00002045 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
Neil Boothaf0d16c2002-04-22 17:48:02 +00002046 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00002047}
2048
Kazu Hirata05713b82002-09-15 18:24:08 +00002049/* Enter all recognized directives in the hash table. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00002050void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002051_cpp_init_directives (cpp_reader *pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002052{
Neil Booth766ee682001-01-29 19:20:12 +00002053 unsigned int i;
Neil Booth93c803682000-10-28 17:59:06 +00002054 cpp_hashnode *node;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00002055
John David Anglin37b85242001-03-02 01:11:50 +00002056 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
Neil Booth93c803682000-10-28 17:59:06 +00002057 {
Neil Booth766ee682001-01-29 19:20:12 +00002058 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
Zack Weinberg4977bab2002-12-16 18:23:00 +00002059 node->is_directive = 1;
2060 node->directive_index = i;
Neil Booth93c803682000-10-28 17:59:06 +00002061 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00002062}