blob: 38ca949f8c9561be9e6acb5501ea3a9a6380a72b [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,
Tom Tromey705e2d22007-01-04 15:32:26 +00003 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 2007 Free Software Foundation, Inc.
Richard Kenner4c8cc611997-02-16 08:08:25 -05005 Contributed by Per Bothner, 1994-95.
Richard Kennerd8bfa781997-01-03 08:19:34 -05006 Based on CCCP program by Paul Rubin, June 1986
Per Bothner7f2935c1995-03-16 13:59:07 -08007 Adapted to ANSI C, Richard Stallman, Jan 1987
8
9This program is free software; you can redistribute it and/or modify it
10under the terms of the GNU General Public License as published by the
11Free Software Foundation; either version 2, or (at your option) any
12later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
Kelley Cook200031d2005-06-29 02:34:39 +000021Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
Per Bothner7f2935c1995-03-16 13:59:07 -080022
Per Bothner7f2935c1995-03-16 13:59:07 -080023#include "config.h"
Kaveh R. Ghazib04cd5071998-03-30 12:05:54 +000024#include "system.h"
Jeff Law956d6951997-12-06 17:31:01 -070025#include "cpplib.h"
Paolo Bonzini4f4e53dd2004-05-24 10:50:45 +000026#include "internal.h"
Zack Weinbergc6e83802004-06-05 20:58:06 +000027#include "mkdeps.h"
Zack Weinbergc71f8352000-07-05 05:33:57 +000028#include "obstack.h"
Jeff Law956d6951997-12-06 17:31:01 -070029
Zack Weinberg88ae23e2000-03-08 23:35:19 +000030/* Stack of conditionals currently in progress
31 (including both successful and failing conditionals). */
Zack Weinberg88ae23e2000-03-08 23:35:19 +000032struct if_stack
33{
34 struct if_stack *next;
Neil Booth50410422001-09-15 10:18:03 +000035 unsigned int line; /* Line where condition started. */
Neil Booth93c803682000-10-28 17:59:06 +000036 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
Neil Boothcef0d192001-07-26 06:02:47 +000037 bool skip_elses; /* Can future #else / #elif be skipped? */
38 bool was_skipping; /* If were skipping on entry. */
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000039 int type; /* Most recent conditional for diagnostics. */
Zack Weinberg88ae23e2000-03-08 23:35:19 +000040};
Zack Weinberg88ae23e2000-03-08 23:35:19 +000041
Neil Bootha5da89c2001-10-14 17:44:00 +000042/* Contains a registered pragma or pragma namespace. */
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000043typedef void (*pragma_cb) (cpp_reader *);
Neil Bootha5da89c2001-10-14 17:44:00 +000044struct pragma_entry
45{
46 struct pragma_entry *next;
Neil Booth4b115ff2001-10-14 23:04:13 +000047 const cpp_hashnode *pragma; /* Name and length. */
Zack Weinberg21b11492004-09-09 19:16:56 +000048 bool is_nspace;
49 bool is_internal;
Richard Hendersonbc4071d2006-01-04 08:33:38 -080050 bool is_deferred;
51 bool allow_expansion;
Neil Bootha5da89c2001-10-14 17:44:00 +000052 union {
53 pragma_cb handler;
54 struct pragma_entry *space;
Richard Hendersonbc4071d2006-01-04 08:33:38 -080055 unsigned int ident;
Neil Bootha5da89c2001-10-14 17:44:00 +000056 } u;
57};
58
Neil Booth93c803682000-10-28 17:59:06 +000059/* Values for the origin field of struct directive. KANDR directives
60 come from traditional (K&R) C. STDC89 directives come from the
61 1989 C standard. EXTENSION directives are extensions. */
62#define KANDR 0
63#define STDC89 1
64#define EXTENSION 2
65
66/* Values for the flags field of struct directive. COND indicates a
67 conditional; IF_COND an opening conditional. INCL means to treat
68 "..." and <...> as q-char and h-char sequences respectively. IN_I
69 means this directive should be handled even if -fpreprocessed is in
Neil Booth1a769162002-06-11 05:36:17 +000070 effect (these are the directives with callback hooks).
71
Neil Boothd97371e2002-06-18 06:27:40 +000072 EXPAND is set on directives that are always macro-expanded. */
Neil Booth93c803682000-10-28 17:59:06 +000073#define COND (1 << 0)
74#define IF_COND (1 << 1)
75#define INCL (1 << 2)
76#define IN_I (1 << 3)
Neil Booth1a769162002-06-11 05:36:17 +000077#define EXPAND (1 << 4)
Neil Booth93c803682000-10-28 17:59:06 +000078
79/* Defines one #-directive, including how to handle it. */
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000080typedef void (*directive_handler) (cpp_reader *);
Neil Booth93c803682000-10-28 17:59:06 +000081typedef struct directive directive;
82struct directive
83{
84 directive_handler handler; /* Function to handle directive. */
Neil Booth562a5c22002-04-21 18:46:42 +000085 const uchar *name; /* Name of directive. */
Neil Booth93c803682000-10-28 17:59:06 +000086 unsigned short length; /* Length of name. */
87 unsigned char origin; /* Origin of directive. */
88 unsigned char flags; /* Flags describing this directive. */
89};
90
Zack Weinberg1316f1f2000-02-06 07:53:50 +000091/* Forward declarations. */
92
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000093static void skip_rest_of_line (cpp_reader *);
94static void check_eol (cpp_reader *);
95static void start_directive (cpp_reader *);
96static void prepare_directive_trad (cpp_reader *);
97static void end_directive (cpp_reader *, int);
98static void directive_diagnostics (cpp_reader *, const directive *, int);
99static void run_directive (cpp_reader *, int, const char *, size_t);
100static char *glue_header_name (cpp_reader *);
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000101static const char *parse_include (cpp_reader *, int *, const cpp_token ***);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000102static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
103static unsigned int read_flag (cpp_reader *, unsigned int);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000104static int strtoul_for_line (const uchar *, unsigned int, unsigned long *);
105static void do_diagnostic (cpp_reader *, int, int);
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000106static cpp_hashnode *lex_macro_node (cpp_reader *, bool);
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000107static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000108static void do_include_common (cpp_reader *, enum include_type);
109static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
110 const cpp_hashnode *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000111static int count_registered_pragmas (struct pragma_entry *);
112static char ** save_registered_pragmas (struct pragma_entry *, char **);
113static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
114 char **);
115static void do_pragma_once (cpp_reader *);
116static void do_pragma_poison (cpp_reader *);
117static void do_pragma_system_header (cpp_reader *);
118static void do_pragma_dependency (cpp_reader *);
119static void do_linemarker (cpp_reader *);
120static const cpp_token *get_token_no_padding (cpp_reader *);
121static const cpp_token *get__Pragma_string (cpp_reader *);
122static void destringize_and_run (cpp_reader *, const cpp_string *);
123static int parse_answer (cpp_reader *, struct answer **, int);
124static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
125static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
126static void handle_assertion (cpp_reader *, const char *, int);
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +0000127
Zack Weinberg168d3732000-03-14 06:34:11 +0000128/* This is the table of directive handlers. It is ordered by
129 frequency of occurrence; the numbers at the end are directive
130 counts from all the source code I have lying around (egcs and libc
131 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
Neil Booth93c803682000-10-28 17:59:06 +0000132 pcmcia-cs-3.0.9). This is no longer important as directive lookup
133 is now O(1). All extensions other than #warning and #include_next
134 are deprecated. The name is where the extension appears to have
135 come from. */
Jeff Lawe9a25f71997-11-02 14:19:36 -0700136
Neil Booth93c803682000-10-28 17:59:06 +0000137#define DIRECTIVE_TABLE \
138D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000139D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000140D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
141D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
Neil Booth1a769162002-06-11 05:36:17 +0000142D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000143D(else, T_ELSE, KANDR, COND) /* 9863 */ \
144D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
145D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
Neil Booth1a769162002-06-11 05:36:17 +0000146D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
147D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000148D(error, T_ERROR, STDC89, 0) /* 475 */ \
149D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
150D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000151D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000152D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000153D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
Neil Booth93c803682000-10-28 17:59:06 +0000154D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
155D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
Zack Weinberg1ed17cd2005-05-12 18:31:38 +0000156D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */
157
158/* #sccs is synonymous with #ident. */
159#define do_sccs do_ident
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
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000219/* Ensure there are no stray tokens other than comments at the end of
220 a directive, and gather the comments. */
221static const cpp_token **
222check_eol_return_comments (cpp_reader *pfile)
223{
224 size_t c;
225 size_t capacity = 8;
226 const cpp_token **buf;
227
228 buf = XNEWVEC (const cpp_token *, capacity);
229 c = 0;
230 if (! SEEN_EOL ())
231 {
232 while (1)
233 {
234 const cpp_token *tok;
235
236 tok = _cpp_lex_token (pfile);
237 if (tok->type == CPP_EOF)
238 break;
239 if (tok->type != CPP_COMMENT)
240 cpp_error (pfile, CPP_DL_PEDWARN,
241 "extra tokens at end of #%s directive",
242 pfile->directive->name);
243 else
244 {
245 if (c + 1 >= capacity)
246 {
247 capacity *= 2;
248 buf = XRESIZEVEC (const cpp_token *, buf, capacity);
249 }
250 buf[c] = tok;
251 ++c;
252 }
253 }
254 }
255 buf[c] = NULL;
256 return buf;
257}
258
Neil Boothfe6c2db2000-11-15 19:25:22 +0000259/* Called when entering a directive, _Pragma or command-line directive. */
260static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000261start_directive (cpp_reader *pfile)
Zack Weinbergc5a04732000-04-25 19:32:36 +0000262{
Neil Booth7f2f1a62000-11-14 18:32:06 +0000263 /* Setup in-directive state. */
264 pfile->state.in_directive = 1;
265 pfile->state.save_comments = 0;
Zack Weinberg21b11492004-09-09 19:16:56 +0000266 pfile->directive_result.type = CPP_PADDING;
Neil Booth7f2f1a62000-11-14 18:32:06 +0000267
Neil Booth93c803682000-10-28 17:59:06 +0000268 /* Some handlers need the position of the # for diagnostics. */
Per Bothner500bee02004-04-22 19:22:27 -0700269 pfile->directive_line = pfile->line_table->highest_line;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000270}
271
272/* Called when leaving a directive, _Pragma or command-line directive. */
273static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000274end_directive (cpp_reader *pfile, int skip_line)
Neil Boothfe6c2db2000-11-15 19:25:22 +0000275{
Richard Hendersonbc4071d2006-01-04 08:33:38 -0800276 if (pfile->state.in_deferred_pragma)
277 ;
278 else if (CPP_OPTION (pfile, traditional))
Neil Booth1a769162002-06-11 05:36:17 +0000279 {
Neil Boothd97371e2002-06-18 06:27:40 +0000280 /* Revert change of prepare_directive_trad. */
281 pfile->state.prevent_expansion--;
282
Neil Boothb66377c2002-06-13 21:16:00 +0000283 if (pfile->directive != &dtable[T_DEFINE])
Neil Booth1a769162002-06-11 05:36:17 +0000284 _cpp_remove_overlay (pfile);
285 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000286 /* We don't skip for an assembler #. */
Neil Boothb66377c2002-06-13 21:16:00 +0000287 else if (skip_line)
Neil Booth67821e32001-08-05 17:31:25 +0000288 {
289 skip_rest_of_line (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +0000290 if (!pfile->keep_tokens)
291 {
292 pfile->cur_run = &pfile->base_run;
293 pfile->cur_token = pfile->base_run.base;
294 }
Neil Booth67821e32001-08-05 17:31:25 +0000295 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000296
297 /* Restore state. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000298 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
299 pfile->state.in_directive = 0;
Neil Boothd97371e2002-06-18 06:27:40 +0000300 pfile->state.in_expression = 0;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000301 pfile->state.angled_headers = 0;
302 pfile->directive = 0;
303}
304
Neil Booth1a769162002-06-11 05:36:17 +0000305/* Prepare to handle the directive in pfile->directive. */
306static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000307prepare_directive_trad (cpp_reader *pfile)
Neil Booth1a769162002-06-11 05:36:17 +0000308{
Neil Booth951a0762002-06-27 06:01:58 +0000309 if (pfile->directive != &dtable[T_DEFINE])
Neil Booth1a769162002-06-11 05:36:17 +0000310 {
Neil Boothb66377c2002-06-13 21:16:00 +0000311 bool no_expand = (pfile->directive
312 && ! (pfile->directive->flags & EXPAND));
Neil Booth974c43f2002-06-13 06:25:28 +0000313 bool was_skipping = pfile->state.skipping;
Neil Booth1a769162002-06-11 05:36:17 +0000314
Neil Boothd97371e2002-06-18 06:27:40 +0000315 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
316 || pfile->directive == &dtable[T_ELIF]);
Neil Booth45f24922003-12-12 07:00:29 +0000317 if (pfile->state.in_expression)
318 pfile->state.skipping = false;
319
Neil Booth1a769162002-06-11 05:36:17 +0000320 if (no_expand)
321 pfile->state.prevent_expansion++;
Zack Weinberg43839642003-07-13 17:34:18 +0000322 _cpp_scan_out_logical_line (pfile, NULL);
Neil Booth1a769162002-06-11 05:36:17 +0000323 if (no_expand)
324 pfile->state.prevent_expansion--;
Neil Booth45f24922003-12-12 07:00:29 +0000325
Neil Booth974c43f2002-06-13 06:25:28 +0000326 pfile->state.skipping = was_skipping;
Neil Booth1a769162002-06-11 05:36:17 +0000327 _cpp_overlay_buffer (pfile, pfile->out.base,
328 pfile->out.cur - pfile->out.base);
329 }
Neil Boothd97371e2002-06-18 06:27:40 +0000330
331 /* Stop ISO C from expanding anything. */
332 pfile->state.prevent_expansion++;
Neil Booth1a769162002-06-11 05:36:17 +0000333}
334
Kazu Hiratada7d8302002-09-22 02:03:17 +0000335/* Output diagnostics for a directive DIR. INDENTED is nonzero if
Neil Booth18a9d8f2001-09-16 11:23:56 +0000336 the '#' was indented. */
Neil Booth18a9d8f2001-09-16 11:23:56 +0000337static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000338directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000339{
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000340 /* Issue -pedantic warnings for extensions. */
341 if (CPP_PEDANTIC (pfile)
342 && ! pfile->state.skipping
343 && dir->origin == EXTENSION)
John David Anglin0527bc42003-11-01 22:56:54 +0000344 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000345
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000346 /* Traditionally, a directive is ignored unless its # is in
347 column 1. Therefore in code intended to work with K+R
348 compilers, directives added by C89 must have their #
349 indented, and directives present in traditional C must not.
350 This is true even of directives in skipped conditional
351 blocks. #elif cannot be used at all. */
352 if (CPP_WTRADITIONAL (pfile))
353 {
354 if (dir == &dtable[T_ELIF])
John David Anglin0527bc42003-11-01 22:56:54 +0000355 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000356 "suggest not using #elif in traditional C");
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000357 else if (indented && dir->origin == KANDR)
John David Anglin0527bc42003-11-01 22:56:54 +0000358 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000359 "traditional C ignores #%s with the # indented",
360 dir->name);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000361 else if (!indented && dir->origin != KANDR)
John David Anglin0527bc42003-11-01 22:56:54 +0000362 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000363 "suggest hiding #%s from traditional C with an indented #",
364 dir->name);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000365 }
366}
367
Kazu Hiratada7d8302002-09-22 02:03:17 +0000368/* Check if we have a known directive. INDENTED is nonzero if the
Neil Booth18a9d8f2001-09-16 11:23:56 +0000369 '#' of the directive was indented. This function is in this file
Gabriel Dos Reisa2566ae2005-01-02 01:32:21 +0000370 to save unnecessarily exporting dtable etc. to lex.c. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +0000371 nonzero if the line of tokens has been handled, zero if we should
Neil Booth18a9d8f2001-09-16 11:23:56 +0000372 continue processing the line. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000373int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000374_cpp_handle_directive (cpp_reader *pfile, int indented)
Neil Boothfe6c2db2000-11-15 19:25:22 +0000375{
Neil Boothfe6c2db2000-11-15 19:25:22 +0000376 const directive *dir = 0;
Neil Booth345894b2001-09-16 13:44:29 +0000377 const cpp_token *dname;
Neil Boothe808ec92002-02-27 07:24:53 +0000378 bool was_parsing_args = pfile->state.parsing_args;
Zack Weinbergc6e83802004-06-05 20:58:06 +0000379 bool was_discarding_output = pfile->state.discarding_output;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000380 int skip = 1;
381
Zack Weinbergc6e83802004-06-05 20:58:06 +0000382 if (was_discarding_output)
383 pfile->state.prevent_expansion = 0;
384
Neil Boothe808ec92002-02-27 07:24:53 +0000385 if (was_parsing_args)
386 {
387 if (CPP_OPTION (pfile, pedantic))
John David Anglin0527bc42003-11-01 22:56:54 +0000388 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothe808ec92002-02-27 07:24:53 +0000389 "embedding a directive within macro arguments is not portable");
390 pfile->state.parsing_args = 0;
391 pfile->state.prevent_expansion = 0;
392 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000393 start_directive (pfile);
Neil Booth345894b2001-09-16 13:44:29 +0000394 dname = _cpp_lex_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000395
Neil Booth345894b2001-09-16 13:44:29 +0000396 if (dname->type == CPP_NAME)
Neil Booth0d9f2342000-09-18 18:43:05 +0000397 {
Zack Weinberg4977bab2002-12-16 18:23:00 +0000398 if (dname->val.node->is_directive)
399 dir = &dtable[dname->val.node->directive_index];
Neil Booth93c803682000-10-28 17:59:06 +0000400 }
Kazu Hirata05713b82002-09-15 18:24:08 +0000401 /* We do not recognize the # followed by a number extension in
Neil Booth18a9d8f2001-09-16 11:23:56 +0000402 assembler code. */
Neil Booth345894b2001-09-16 13:44:29 +0000403 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth93c803682000-10-28 17:59:06 +0000404 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000405 dir = &linemarker_dir;
406 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
407 && ! pfile->state.skipping)
John David Anglin0527bc42003-11-01 22:56:54 +0000408 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothebef4e82002-04-14 18:42:47 +0000409 "style of line directive is a GCC extension");
Neil Booth0d9f2342000-09-18 18:43:05 +0000410 }
411
Neil Booth93c803682000-10-28 17:59:06 +0000412 if (dir)
Neil Booth0d9f2342000-09-18 18:43:05 +0000413 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000414 /* If we have a directive that is not an opening conditional,
415 invalidate any control macro. */
416 if (! (dir->flags & IF_COND))
417 pfile->mi_valid = false;
Neil Booth93c803682000-10-28 17:59:06 +0000418
Neil Booth18a9d8f2001-09-16 11:23:56 +0000419 /* Kluge alert. In order to be sure that code like this
420
421 #define HASH #
422 HASH define foo bar
423
424 does not cause '#define foo bar' to get executed when
425 compiled with -save-temps, we recognize directives in
Gabriel Dos Reisa2566ae2005-01-02 01:32:21 +0000426 -fpreprocessed mode only if the # is in column 1. macro.c
Ollie Wildccfc4c92007-07-30 18:29:20 +0000427 puts a space in front of any '#' at the start of a macro.
428
429 We exclude the -fdirectives-only case because macro expansion
430 has not been performed yet, and block comments can cause spaces
431 to preceed the directive. */
Neil Booth18a9d8f2001-09-16 11:23:56 +0000432 if (CPP_OPTION (pfile, preprocessed)
Ollie Wildccfc4c92007-07-30 18:29:20 +0000433 && !CPP_OPTION (pfile, directives_only)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000434 && (indented || !(dir->flags & IN_I)))
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000435 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000436 skip = 0;
437 dir = 0;
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000438 }
439 else
Neil Booth93c803682000-10-28 17:59:06 +0000440 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000441 /* In failed conditional groups, all non-conditional
442 directives are ignored. Before doing that, whether
443 skipping or not, we should lex angle-bracketed headers
444 correctly, and maybe output some diagnostics. */
445 pfile->state.angled_headers = dir->flags & INCL;
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000446 pfile->state.directive_wants_padding = dir->flags & INCL;
Neil Booth18a9d8f2001-09-16 11:23:56 +0000447 if (! CPP_OPTION (pfile, preprocessed))
448 directive_diagnostics (pfile, dir, indented);
449 if (pfile->state.skipping && !(dir->flags & COND))
450 dir = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000451 }
452 }
Neil Booth345894b2001-09-16 13:44:29 +0000453 else if (dname->type == CPP_EOF)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000454 ; /* CPP_EOF is the "null directive". */
455 else
Neil Booth0d9f2342000-09-18 18:43:05 +0000456 {
Neil Booth93c803682000-10-28 17:59:06 +0000457 /* An unknown directive. Don't complain about it in assembly
458 source: we don't know where the comments are, and # may
459 introduce assembler pseudo-ops. Don't complain about invalid
460 directives in skipped conditional groups (6.10 p4). */
Neil Boothbdb05a72000-11-26 17:31:13 +0000461 if (CPP_OPTION (pfile, lang) == CLK_ASM)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000462 skip = 0;
463 else if (!pfile->state.skipping)
John David Anglin0527bc42003-11-01 22:56:54 +0000464 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
Neil Booth345894b2001-09-16 13:44:29 +0000465 cpp_token_as_text (pfile, dname));
Neil Booth0d9f2342000-09-18 18:43:05 +0000466 }
467
Neil Boothd1a58682002-06-28 06:26:54 +0000468 pfile->directive = dir;
469 if (CPP_OPTION (pfile, traditional))
470 prepare_directive_trad (pfile);
471
Neil Booth18a9d8f2001-09-16 11:23:56 +0000472 if (dir)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000473 pfile->directive->handler (pfile);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000474 else if (skip == 0)
475 _cpp_backup_tokens (pfile, 1);
476
477 end_directive (pfile, skip);
Neil Boothe808ec92002-02-27 07:24:53 +0000478 if (was_parsing_args)
479 {
480 /* Restore state when within macro args. */
481 pfile->state.parsing_args = 2;
482 pfile->state.prevent_expansion = 1;
Neil Boothe808ec92002-02-27 07:24:53 +0000483 }
Zack Weinbergc6e83802004-06-05 20:58:06 +0000484 if (was_discarding_output)
485 pfile->state.prevent_expansion = 1;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000486 return skip;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000487}
488
Neil Booth93c803682000-10-28 17:59:06 +0000489/* Directive handler wrapper used by the command line option
Neil Booth26aea072003-04-19 00:22:51 +0000490 processor. BUF is \n terminated. */
Neil Booth93c803682000-10-28 17:59:06 +0000491static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000492run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000493{
Neil Booth562a5c22002-04-21 18:46:42 +0000494 cpp_push_buffer (pfile, (const uchar *) buf, count,
Per Bothner40de9f72003-10-02 07:30:34 +0000495 /* from_stage3 */ true);
Neil Booth0bda4762000-12-11 07:45:16 +0000496 start_directive (pfile);
Neil Booth26aea072003-04-19 00:22:51 +0000497
498 /* This is a short-term fix to prevent a leading '#' being
499 interpreted as a directive. */
500 _cpp_clean_line (pfile);
501
Neil Boothf71aebb2001-05-27 18:06:00 +0000502 pfile->directive = &dtable[dir_no];
Neil Booth1a769162002-06-11 05:36:17 +0000503 if (CPP_OPTION (pfile, traditional))
504 prepare_directive_trad (pfile);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000505 pfile->directive->handler (pfile);
Neil Booth0bda4762000-12-11 07:45:16 +0000506 end_directive (pfile, 1);
Neil Boothef6e9582001-08-04 12:01:59 +0000507 _cpp_pop_buffer (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000508}
Per Bothner7f2935c1995-03-16 13:59:07 -0800509
Neil Booth93c803682000-10-28 17:59:06 +0000510/* Checks for validity the macro name in #define, #undef, #ifdef and
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000511 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
512 processing a #define or #undefine directive, and false
513 otherwise. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000514static cpp_hashnode *
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000515lex_macro_node (cpp_reader *pfile, bool is_def_or_undef)
Zack Weinberg041c3192000-07-04 01:58:21 +0000516{
Neil Booth1a769162002-06-11 05:36:17 +0000517 const cpp_token *token = _cpp_lex_token (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000518
Zack Weinberg92936ec2000-07-19 20:18:08 +0000519 /* The token immediately after #define must be an identifier. That
Zack Weinbergb8363a22001-07-01 18:48:13 +0000520 identifier may not be "defined", per C99 6.10.8p4.
521 In C++, it may not be any of the "named operators" either,
522 per C++98 [lex.digraph], [lex.key].
523 Finally, the identifier may not have been poisoned. (In that case
Neil Booth1d63a282002-06-28 20:27:14 +0000524 the lexer has issued the error message for us.) */
Zack Weinbergb8363a22001-07-01 18:48:13 +0000525
Neil Booth1a769162002-06-11 05:36:17 +0000526 if (token->type == CPP_NAME)
527 {
528 cpp_hashnode *node = token->val.node;
529
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000530 if (is_def_or_undef && node == pfile->spec_nodes.n_defined)
John David Anglin0527bc42003-11-01 22:56:54 +0000531 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth1a769162002-06-11 05:36:17 +0000532 "\"defined\" cannot be used as a macro name");
533 else if (! (node->flags & NODE_POISONED))
534 return node;
535 }
536 else if (token->flags & NAMED_OP)
John David Anglin0527bc42003-11-01 22:56:54 +0000537 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcbc69f82002-06-05 20:27:12 +0000538 "\"%s\" cannot be used as a macro name as it is an operator in C++",
Neil Booth1a769162002-06-11 05:36:17 +0000539 NODE_NAME (token->val.node));
540 else if (token->type == CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000541 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
Neil Booth1a769162002-06-11 05:36:17 +0000542 pfile->directive->name);
543 else
John David Anglin0527bc42003-11-01 22:56:54 +0000544 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
Zack Weinbergb8363a22001-07-01 18:48:13 +0000545
Neil Boothcbc69f82002-06-05 20:27:12 +0000546 return NULL;
Per Bothner7f2935c1995-03-16 13:59:07 -0800547}
Per Bothner7f2935c1995-03-16 13:59:07 -0800548
Gabriel Dos Reisa2566ae2005-01-02 01:32:21 +0000549/* Process a #define directive. Most work is done in macro.c. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000550static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000551do_define (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800552{
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000553 cpp_hashnode *node = lex_macro_node (pfile, true);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000554
Neil Booth93c803682000-10-28 17:59:06 +0000555 if (node)
556 {
Neil Booth1d63a282002-06-28 20:27:14 +0000557 /* If we have been requested to expand comments into macros,
558 then re-enable saving of comments. */
559 pfile->state.save_comments =
560 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
561
Neil Booth93c803682000-10-28 17:59:06 +0000562 if (_cpp_create_definition (pfile, node))
563 if (pfile->cb.define)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000564 pfile->cb.define (pfile, pfile->directive_line, node);
Neil Booth93c803682000-10-28 17:59:06 +0000565 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800566}
567
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000568/* Handle #undef. Mark the identifier NT_VOID in the hash table. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000569static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000570do_undef (cpp_reader *pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000571{
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000572 cpp_hashnode *node = lex_macro_node (pfile, true);
Zack Weinberg041c3192000-07-04 01:58:21 +0000573
Neil Booth45f24922003-12-12 07:00:29 +0000574 if (node)
Zack Weinberg041c3192000-07-04 01:58:21 +0000575 {
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000576 if (pfile->cb.undef)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000577 pfile->cb.undef (pfile, pfile->directive_line, node);
Zack Weinberg041c3192000-07-04 01:58:21 +0000578
Neil Booth45f24922003-12-12 07:00:29 +0000579 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
580 identifier is not currently defined as a macro name. */
581 if (node->type == NT_MACRO)
582 {
583 if (node->flags & NODE_WARN)
584 cpp_error (pfile, CPP_DL_WARNING,
585 "undefining \"%s\"", NODE_NAME (node));
Zack Weinberg041c3192000-07-04 01:58:21 +0000586
Neil Booth45f24922003-12-12 07:00:29 +0000587 if (CPP_OPTION (pfile, warn_unused_macros))
588 _cpp_warn_if_unused_macro (pfile, node, NULL);
Neil Bootha69cbaa2002-07-23 22:57:49 +0000589
Neil Booth45f24922003-12-12 07:00:29 +0000590 _cpp_free_definition (node);
591 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000592 }
Neil Booth45f24922003-12-12 07:00:29 +0000593
Neil Booth93c803682000-10-28 17:59:06 +0000594 check_eol (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000595}
596
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000597/* Undefine a single macro/assertion/whatever. */
598
599static int
Zack Weinbergc6e83802004-06-05 20:58:06 +0000600undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000601 void *data_p ATTRIBUTE_UNUSED)
602{
Zack Weinbergc6e83802004-06-05 20:58:06 +0000603 /* Body of _cpp_free_definition inlined here for speed.
604 Macros and assertions no longer have anything to free. */
605 h->type = NT_VOID;
606 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED);
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000607 return 1;
608}
609
610/* Undefine all macros and assertions. */
611
612void
613cpp_undef_all (cpp_reader *pfile)
614{
615 cpp_forall_identifiers (pfile, undefine_macros, NULL);
616}
617
618
Neil Booth93c803682000-10-28 17:59:06 +0000619/* Helper routine used by parse_include. Reinterpret the current line
620 as an h-char-sequence (< ... >); we are looking at the first token
Neil Booth74eb4b32003-04-21 19:21:59 +0000621 after the <. Returns a malloced filename. */
622static char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000623glue_header_name (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800624{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000625 const cpp_token *token;
Neil Booth74eb4b32003-04-21 19:21:59 +0000626 char *buffer;
Neil Booth2450e0b2002-02-23 20:21:39 +0000627 size_t len, total_len = 0, capacity = 1024;
Per Bothner7f2935c1995-03-16 13:59:07 -0800628
Neil Booth93c803682000-10-28 17:59:06 +0000629 /* To avoid lexed tokens overwriting our glued name, we can only
630 allocate from the string pool once we've lexed everything. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000631 buffer = XNEWVEC (char, capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000632 for (;;)
Zack Weinberg168d3732000-03-14 06:34:11 +0000633 {
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000634 token = get_token_no_padding (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000635
Neil Booth74eb4b32003-04-21 19:21:59 +0000636 if (token->type == CPP_GREATER)
Neil Booth93c803682000-10-28 17:59:06 +0000637 break;
Neil Booth74eb4b32003-04-21 19:21:59 +0000638 if (token->type == CPP_EOF)
639 {
John David Anglin0527bc42003-11-01 22:56:54 +0000640 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
Neil Booth74eb4b32003-04-21 19:21:59 +0000641 break;
642 }
Neil Booth93c803682000-10-28 17:59:06 +0000643
Neil Booth59325652003-04-24 20:03:57 +0000644 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
Neil Booth2450e0b2002-02-23 20:21:39 +0000645 if (total_len + len > capacity)
Neil Booth93c803682000-10-28 17:59:06 +0000646 {
Neil Booth2450e0b2002-02-23 20:21:39 +0000647 capacity = (capacity + len) * 2;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000648 buffer = XRESIZEVEC (char, buffer, capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000649 }
650
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000651 if (token->flags & PREV_WHITE)
Neil Booth2450e0b2002-02-23 20:21:39 +0000652 buffer[total_len++] = ' ';
Neil Booth93c803682000-10-28 17:59:06 +0000653
Geoffrey Keating47e20492005-03-12 10:44:06 +0000654 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
655 true)
Neil Booth74eb4b32003-04-21 19:21:59 +0000656 - (uchar *) buffer);
Neil Booth93c803682000-10-28 17:59:06 +0000657 }
658
Neil Booth74eb4b32003-04-21 19:21:59 +0000659 buffer[total_len] = '\0';
660 return buffer;
Neil Booth93c803682000-10-28 17:59:06 +0000661}
662
Neil Booth74eb4b32003-04-21 19:21:59 +0000663/* Returns the file name of #include, #include_next, #import and
664 #pragma dependency. The string is malloced and the caller should
665 free it. Returns NULL on error. */
666static const char *
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000667parse_include (cpp_reader *pfile, int *pangle_brackets,
668 const cpp_token ***buf)
Neil Booth93c803682000-10-28 17:59:06 +0000669{
Neil Booth74eb4b32003-04-21 19:21:59 +0000670 char *fname;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000671 const cpp_token *header;
Neil Booth93c803682000-10-28 17:59:06 +0000672
Neil Booth93c803682000-10-28 17:59:06 +0000673 /* Allow macro expansion. */
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000674 header = get_token_no_padding (pfile);
Neil Booth74eb4b32003-04-21 19:21:59 +0000675 if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
Neil Booth93c803682000-10-28 17:59:06 +0000676 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000677 fname = XNEWVEC (char, header->val.str.len - 1);
Neil Booth6338b352003-04-23 22:44:06 +0000678 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
679 fname[header->val.str.len - 2] = '\0';
Neil Booth74eb4b32003-04-21 19:21:59 +0000680 *pangle_brackets = header->type == CPP_HEADER_NAME;
Zack Weinberg041c3192000-07-04 01:58:21 +0000681 }
Neil Booth74eb4b32003-04-21 19:21:59 +0000682 else if (header->type == CPP_LESS)
Zack Weinberg041c3192000-07-04 01:58:21 +0000683 {
Neil Booth74eb4b32003-04-21 19:21:59 +0000684 fname = glue_header_name (pfile);
685 *pangle_brackets = 1;
686 }
687 else
688 {
689 const unsigned char *dir;
690
691 if (pfile->directive == &dtable[T_PRAGMA])
692 dir = U"pragma dependency";
693 else
694 dir = pfile->directive->name;
John David Anglin0527bc42003-11-01 22:56:54 +0000695 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
Neil Booth74eb4b32003-04-21 19:21:59 +0000696 dir);
697
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000698 return NULL;
Richard Kennercfb3ee11997-04-13 14:30:13 -0400699 }
700
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000701 if (buf == NULL || CPP_OPTION (pfile, discard_comments))
702 check_eol (pfile);
703 else
704 {
705 /* If we are not discarding comments, then gather them while
706 doing the eol check. */
707 *buf = check_eol_return_comments (pfile);
708 }
709
Neil Booth74eb4b32003-04-21 19:21:59 +0000710 return fname;
Zack Weinberg168d3732000-03-14 06:34:11 +0000711}
712
Neil Boothba133c92001-03-15 07:57:13 +0000713/* Handle #include, #include_next and #import. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000714static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000715do_include_common (cpp_reader *pfile, enum include_type type)
Zack Weinberg168d3732000-03-14 06:34:11 +0000716{
Neil Booth74eb4b32003-04-21 19:21:59 +0000717 const char *fname;
718 int angle_brackets;
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000719 const cpp_token **buf = NULL;
Neil Booth74eb4b32003-04-21 19:21:59 +0000720
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000721 /* Re-enable saving of comments if requested, so that the include
722 callback can dump comments which follow #include. */
723 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
724
725 fname = parse_include (pfile, &angle_brackets, &buf);
Neil Booth74eb4b32003-04-21 19:21:59 +0000726 if (!fname)
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000727 {
728 if (buf)
729 XDELETEVEC (buf);
730 return;
731 }
Zack Weinberg168d3732000-03-14 06:34:11 +0000732
Nathanael Nerode28303822004-11-28 22:28:13 +0000733 if (!*fname)
734 {
735 cpp_error (pfile, CPP_DL_ERROR, "empty filename in #%s",
736 pfile->directive->name);
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000737 XDELETEVEC (fname);
738 if (buf)
739 XDELETEVEC (buf);
Nathanael Nerode28303822004-11-28 22:28:13 +0000740 return;
741 }
742
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000743 /* Prevent #include recursion. */
Per Bothner50f59cd2004-01-19 21:30:18 -0800744 if (pfile->line_table->depth >= CPP_STACK_MAX)
John David Anglin0527bc42003-11-01 22:56:54 +0000745 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
Neil Booth74eb4b32003-04-21 19:21:59 +0000746 else
Neil Booth09b82252001-07-29 22:27:20 +0000747 {
Neil Booth74eb4b32003-04-21 19:21:59 +0000748 /* Get out of macro context, if we are. */
749 skip_rest_of_line (pfile);
750
751 if (pfile->cb.include)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000752 pfile->cb.include (pfile, pfile->directive_line,
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000753 pfile->directive->name, fname, angle_brackets,
754 buf);
Neil Booth74eb4b32003-04-21 19:21:59 +0000755
Neil Booth8f9b4002003-07-29 22:26:13 +0000756 _cpp_stack_include (pfile, fname, angle_brackets, type);
Neil Booth09b82252001-07-29 22:27:20 +0000757 }
758
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000759 XDELETEVEC (fname);
760 if (buf)
761 XDELETEVEC (buf);
Neil Boothba133c92001-03-15 07:57:13 +0000762}
763
764static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000765do_include (cpp_reader *pfile)
Neil Boothba133c92001-03-15 07:57:13 +0000766{
767 do_include_common (pfile, IT_INCLUDE);
Zack Weinberg168d3732000-03-14 06:34:11 +0000768}
769
Zack Weinberg711b8822000-07-18 00:59:49 +0000770static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000771do_import (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +0000772{
Neil Boothba133c92001-03-15 07:57:13 +0000773 do_include_common (pfile, IT_IMPORT);
Zack Weinberg168d3732000-03-14 06:34:11 +0000774}
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000775
Zack Weinberg711b8822000-07-18 00:59:49 +0000776static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000777do_include_next (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +0000778{
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000779 enum include_type type = IT_INCLUDE_NEXT;
780
781 /* If this is the primary source file, warn and use the normal
782 search logic. */
Tom Tromey705e2d22007-01-04 15:32:26 +0000783 if (cpp_in_primary_file (pfile))
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000784 {
John David Anglin0527bc42003-11-01 22:56:54 +0000785 cpp_error (pfile, CPP_DL_WARNING,
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000786 "#include_next in primary source file");
787 type = IT_INCLUDE;
788 }
789 do_include_common (pfile, type);
Per Bothner7f2935c1995-03-16 13:59:07 -0800790}
791
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000792/* Subroutine of do_linemarker. Read possible flags after file name.
793 LAST is the last flag seen; 0 if this is the first flag. Return the
794 flag if it is valid, 0 at the end of the directive. Otherwise
795 complain. */
Neil Booth642ce432000-12-07 23:17:56 +0000796static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000797read_flag (cpp_reader *pfile, unsigned int last)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000798{
Neil Booth345894b2001-09-16 13:44:29 +0000799 const cpp_token *token = _cpp_lex_token (pfile);
Jason Merrilld3a34a01999-08-14 00:42:07 +0000800
Neil Booth345894b2001-09-16 13:44:29 +0000801 if (token->type == CPP_NUMBER && token->val.str.len == 1)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000802 {
Neil Booth345894b2001-09-16 13:44:29 +0000803 unsigned int flag = token->val.str.text[0] - '0';
Neil Booth28e0f042000-12-09 12:06:37 +0000804
805 if (flag > last && flag <= 4
806 && (flag != 4 || last == 3)
807 && (flag != 2 || last == 0))
808 return flag;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000809 }
Neil Booth93c803682000-10-28 17:59:06 +0000810
Neil Booth345894b2001-09-16 13:44:29 +0000811 if (token->type != CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000812 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
Neil Booth345894b2001-09-16 13:44:29 +0000813 cpp_token_as_text (pfile, token));
Neil Booth93c803682000-10-28 17:59:06 +0000814 return 0;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000815}
816
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000817/* Subroutine of do_line and do_linemarker. Convert a number in STR,
818 of length LEN, to binary; store it in NUMP, and return 0 if the
819 number was well-formed, 1 if not. Temporary, hopefully. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000820static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000821strtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump)
Zack Weinberg041c3192000-07-04 01:58:21 +0000822{
823 unsigned long reg = 0;
Neil Booth562a5c22002-04-21 18:46:42 +0000824 uchar c;
Zack Weinberg041c3192000-07-04 01:58:21 +0000825 while (len--)
826 {
827 c = *str++;
828 if (!ISDIGIT (c))
829 return 1;
830 reg *= 10;
831 reg += c - '0';
832 }
833 *nump = reg;
834 return 0;
835}
836
Zack Weinberg5538ada1999-02-04 06:36:54 -0500837/* Interpret #line command.
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000838 Note that the filename string (if any) is a true string constant
839 (escapes are interpreted), unlike in #line. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000840static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000841do_line (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800842{
Per Bothner500bee02004-04-22 19:22:27 -0700843 const struct line_maps *line_table = pfile->line_table;
844 const struct line_map *map = &line_table->maps[line_table->used - 1];
Devang Patel2203a882005-02-28 11:04:19 -0800845
846 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
847 sysp right now. */
848
849 unsigned char map_sysp = map->sysp;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000850 const cpp_token *token;
Per Bothner12f9df42004-02-11 07:29:30 -0800851 const char *new_file = map->to_file;
Neil Boothbb74c962001-08-17 22:23:49 +0000852 unsigned long new_lineno;
Per Bothner7f2935c1995-03-16 13:59:07 -0800853
Neil Booth27e25642000-11-27 08:00:04 +0000854 /* C99 raised the minimum limit on #line numbers. */
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000855 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
Neil Booth18a9d8f2001-09-16 11:23:56 +0000856
Neil Booth93c803682000-10-28 17:59:06 +0000857 /* #line commands expand macros. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000858 token = cpp_get_token (pfile);
859 if (token->type != CPP_NUMBER
860 || strtoul_for_line (token->val.str.text, token->val.str.len,
861 &new_lineno))
Per Bothner7f2935c1995-03-16 13:59:07 -0800862 {
John David Anglin0527bc42003-11-01 22:56:54 +0000863 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +0000864 "\"%s\" after #line is not a positive integer",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000865 cpp_token_as_text (pfile, token));
Zack Weinberg9ec72912000-08-09 19:41:12 +0000866 return;
Kazu Hiratadf383482002-05-22 22:02:16 +0000867 }
Zack Weinberg5538ada1999-02-04 06:36:54 -0500868
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000869 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
John David Anglin0527bc42003-11-01 22:56:54 +0000870 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
Zack Weinberg5538ada1999-02-04 06:36:54 -0500871
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000872 token = cpp_get_token (pfile);
873 if (token->type == CPP_STRING)
Zack Weinberg5538ada1999-02-04 06:36:54 -0500874 {
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000875 cpp_string s = { 0, 0 };
Eric Christopher423e95e2004-02-12 02:25:03 +0000876 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
877 &s, false))
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000878 new_file = (const char *)s.text;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000879 check_eol (pfile);
880 }
881 else if (token->type != CPP_EOF)
882 {
John David Anglin0527bc42003-11-01 22:56:54 +0000883 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000884 cpp_token_as_text (pfile, token));
885 return;
886 }
Neil Booth93c803682000-10-28 17:59:06 +0000887
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000888 skip_rest_of_line (pfile);
889 _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
Devang Patel2203a882005-02-28 11:04:19 -0800890 map_sysp);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000891}
892
893/* Interpret the # 44 "file" [flags] notation, which has slightly
894 different syntax and semantics from #line: Flags are allowed,
895 and we never complain about the line number being too big. */
896static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000897do_linemarker (cpp_reader *pfile)
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000898{
Per Bothner500bee02004-04-22 19:22:27 -0700899 const struct line_maps *line_table = pfile->line_table;
900 const struct line_map *map = &line_table->maps[line_table->used - 1];
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000901 const cpp_token *token;
Per Bothner12f9df42004-02-11 07:29:30 -0800902 const char *new_file = map->to_file;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000903 unsigned long new_lineno;
Per Bothner12f9df42004-02-11 07:29:30 -0800904 unsigned int new_sysp = map->sysp;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000905 enum lc_reason reason = LC_RENAME;
906 int flag;
907
908 /* Back up so we can get the number again. Putting this in
909 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
910 some circumstances, which can segfault. */
911 _cpp_backup_tokens (pfile, 1);
912
913 /* #line commands expand macros. */
914 token = cpp_get_token (pfile);
915 if (token->type != CPP_NUMBER
916 || strtoul_for_line (token->val.str.text, token->val.str.len,
917 &new_lineno))
918 {
John David Anglin0527bc42003-11-01 22:56:54 +0000919 cpp_error (pfile, CPP_DL_ERROR,
920 "\"%s\" after # is not a positive integer",
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000921 cpp_token_as_text (pfile, token));
922 return;
Kazu Hiratadf383482002-05-22 22:02:16 +0000923 }
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000924
925 token = cpp_get_token (pfile);
926 if (token->type == CPP_STRING)
927 {
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000928 cpp_string s = { 0, 0 };
Eric Christopher423e95e2004-02-12 02:25:03 +0000929 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
930 1, &s, false))
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000931 new_file = (const char *)s.text;
Eric Christophercf551fb2004-01-16 22:37:49 +0000932
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000933 new_sysp = 0;
934 flag = read_flag (pfile, 0);
935 if (flag == 1)
Neil Booth93c803682000-10-28 17:59:06 +0000936 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000937 reason = LC_ENTER;
938 /* Fake an include for cpp_included (). */
939 _cpp_fake_include (pfile, new_file);
940 flag = read_flag (pfile, flag);
Neil Booth93c803682000-10-28 17:59:06 +0000941 }
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000942 else if (flag == 2)
943 {
944 reason = LC_LEAVE;
945 flag = read_flag (pfile, flag);
946 }
947 if (flag == 3)
948 {
949 new_sysp = 1;
950 flag = read_flag (pfile, flag);
951 if (flag == 4)
952 new_sysp = 2;
953 }
Jakub Jelinek9d30f272006-12-29 09:15:08 +0100954 pfile->buffer->sysp = new_sysp;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000955
Neil Boothfde84342001-08-06 21:07:41 +0000956 check_eol (pfile);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000957 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000958 else if (token->type != CPP_EOF)
Neil Booth27e25642000-11-27 08:00:04 +0000959 {
John David Anglin0527bc42003-11-01 22:56:54 +0000960 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000961 cpp_token_as_text (pfile, token));
Neil Booth27e25642000-11-27 08:00:04 +0000962 return;
963 }
Zack Weinberg941e09b1998-12-15 11:17:06 +0000964
Neil Boothbdcbe492001-09-13 20:05:17 +0000965 skip_rest_of_line (pfile);
Neil Boothbb74c962001-08-17 22:23:49 +0000966 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
Neil Booth27e25642000-11-27 08:00:04 +0000967}
968
Neil Booth67821e32001-08-05 17:31:25 +0000969/* Arrange the file_change callback. pfile->line has changed to
Neil Booth47d89cf2001-08-11 07:33:39 +0000970 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
Joseph Myersa1f300c2001-11-23 02:05:19 +0000971 header, 2 for a system header that needs to be extern "C" protected,
Neil Booth47d89cf2001-08-11 07:33:39 +0000972 and zero otherwise. */
Neil Bootheb1f4d92000-12-18 19:00:26 +0000973void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000974_cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
975 const char *to_file, unsigned int file_line,
976 unsigned int sysp)
Neil Booth27e25642000-11-27 08:00:04 +0000977{
Per Bothner12f9df42004-02-11 07:29:30 -0800978 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
979 to_file, file_line);
Per Bothner500bee02004-04-22 19:22:27 -0700980 if (map != NULL)
981 linemap_line_start (pfile->line_table, map->to_line, 127);
Neil Boothd82fc102001-08-02 23:03:31 +0000982
Neil Bootheb1f4d92000-12-18 19:00:26 +0000983 if (pfile->cb.file_change)
Per Bothner12f9df42004-02-11 07:29:30 -0800984 pfile->cb.file_change (pfile, map);
Per Bothner7f2935c1995-03-16 13:59:07 -0800985}
Zack Weinberg941e09b1998-12-15 11:17:06 +0000986
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000987/* Report a warning or error detected by the program we are
988 processing. Use the directive's tokens in the error message. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000989static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000990do_diagnostic (cpp_reader *pfile, int code, int print_dir)
Per Bothner7f2935c1995-03-16 13:59:07 -0800991{
Per Bothner12f9df42004-02-11 07:29:30 -0800992 if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0))
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000993 {
Neil Booth29b10742000-11-13 18:40:37 +0000994 if (print_dir)
995 fprintf (stderr, "#%s ", pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +0000996 pfile->state.prevent_expansion++;
997 cpp_output_line (pfile, stderr);
998 pfile->state.prevent_expansion--;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000999 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001000}
1001
Neil Booth838f3132000-09-24 10:42:09 +00001002static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001003do_error (cpp_reader *pfile)
Neil Booth838f3132000-09-24 10:42:09 +00001004{
John David Anglin0527bc42003-11-01 22:56:54 +00001005 do_diagnostic (pfile, CPP_DL_ERROR, 1);
Neil Booth838f3132000-09-24 10:42:09 +00001006}
Per Bothner7f2935c1995-03-16 13:59:07 -08001007
Zack Weinberg711b8822000-07-18 00:59:49 +00001008static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001009do_warning (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001010{
Neil Booth2f878972001-04-08 10:01:18 +00001011 /* We want #warning diagnostics to be emitted in system headers too. */
John David Anglin0527bc42003-11-01 22:56:54 +00001012 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
Per Bothner7f2935c1995-03-16 13:59:07 -08001013}
1014
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001015/* Report program identification. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001016static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001017do_ident (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001018{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001019 const cpp_token *str = cpp_get_token (pfile);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001020
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001021 if (str->type != CPP_STRING)
Zack Weinberg1ed17cd2005-05-12 18:31:38 +00001022 cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
1023 pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +00001024 else if (pfile->cb.ident)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001025 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001026
Neil Booth93c803682000-10-28 17:59:06 +00001027 check_eol (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08001028}
1029
Neil Bootha5da89c2001-10-14 17:44:00 +00001030/* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1031 matching entry, or NULL if none is found. The returned entry could
1032 be the start of a namespace chain, or a pragma. */
1033static struct pragma_entry *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001034lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
Nathan Sidwell82443372000-06-23 10:56:09 +00001035{
Neil Booth4b115ff2001-10-14 23:04:13 +00001036 while (chain && chain->pragma != pragma)
1037 chain = chain->next;
Neil Bootha5da89c2001-10-14 17:44:00 +00001038
1039 return chain;
1040}
1041
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001042/* Create and insert a blank pragma entry at the beginning of a
1043 singly-linked CHAIN. */
Neil Bootha5da89c2001-10-14 17:44:00 +00001044static struct pragma_entry *
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001045new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain)
Neil Bootha5da89c2001-10-14 17:44:00 +00001046{
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001047 struct pragma_entry *new_entry;
Neil Bootha5da89c2001-10-14 17:44:00 +00001048
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001049 new_entry = (struct pragma_entry *)
Neil Bootha5da89c2001-10-14 17:44:00 +00001050 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
Neil Bootha5da89c2001-10-14 17:44:00 +00001051
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001052 memset (new_entry, 0, sizeof (struct pragma_entry));
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001053 new_entry->next = *chain;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001054
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001055 *chain = new_entry;
1056 return new_entry;
Neil Bootha5da89c2001-10-14 17:44:00 +00001057}
1058
1059/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001060 goes in the global namespace. */
1061static struct pragma_entry *
1062register_pragma_1 (cpp_reader *pfile, const char *space, const char *name,
1063 bool allow_name_expansion)
Nathan Sidwell82443372000-06-23 10:56:09 +00001064{
Neil Bootha5da89c2001-10-14 17:44:00 +00001065 struct pragma_entry **chain = &pfile->pragmas;
1066 struct pragma_entry *entry;
Neil Booth4b115ff2001-10-14 23:04:13 +00001067 const cpp_hashnode *node;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001068
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001069 if (space)
1070 {
Neil Booth4b115ff2001-10-14 23:04:13 +00001071 node = cpp_lookup (pfile, U space, strlen (space));
1072 entry = lookup_pragma_entry (*chain, node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001073 if (!entry)
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001074 {
1075 entry = new_pragma_entry (pfile, chain);
1076 entry->pragma = node;
1077 entry->is_nspace = true;
1078 entry->allow_expansion = allow_name_expansion;
1079 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001080 else if (!entry->is_nspace)
1081 goto clash;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001082 else if (entry->allow_expansion != allow_name_expansion)
1083 {
1084 cpp_error (pfile, CPP_DL_ICE,
1085 "registering pragmas in namespace \"%s\" with mismatched "
1086 "name expansion", space);
1087 return NULL;
1088 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001089 chain = &entry->u.space;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001090 }
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001091 else if (allow_name_expansion)
1092 {
1093 cpp_error (pfile, CPP_DL_ICE,
1094 "registering pragma \"%s\" with name expansion "
1095 "and no namespace", name);
1096 return NULL;
1097 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001098
Neil Bootha5da89c2001-10-14 17:44:00 +00001099 /* Check for duplicates. */
Neil Booth4b115ff2001-10-14 23:04:13 +00001100 node = cpp_lookup (pfile, U name, strlen (name));
1101 entry = lookup_pragma_entry (*chain, node);
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001102 if (entry == NULL)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001103 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001104 entry = new_pragma_entry (pfile, chain);
1105 entry->pragma = node;
1106 return entry;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001107 }
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001108
1109 if (entry->is_nspace)
1110 clash:
1111 cpp_error (pfile, CPP_DL_ICE,
1112 "registering \"%s\" as both a pragma and a pragma namespace",
1113 NODE_NAME (node));
1114 else if (space)
1115 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1116 space, name);
Neil Bootha5da89c2001-10-14 17:44:00 +00001117 else
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001118 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1119
1120 return NULL;
1121}
1122
1123/* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1124static void
1125register_pragma_internal (cpp_reader *pfile, const char *space,
1126 const char *name, pragma_cb handler)
1127{
1128 struct pragma_entry *entry;
1129
1130 entry = register_pragma_1 (pfile, space, name, false);
1131 entry->is_internal = true;
1132 entry->u.handler = handler;
Zack Weinberg21b11492004-09-09 19:16:56 +00001133}
1134
1135/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1136 goes in the global namespace. HANDLER is the handler it will call,
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001137 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1138 expansion while parsing pragma NAME. This function is exported
1139 from libcpp. */
Zack Weinberg21b11492004-09-09 19:16:56 +00001140void
1141cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001142 pragma_cb handler, bool allow_expansion)
Zack Weinberg21b11492004-09-09 19:16:56 +00001143{
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001144 struct pragma_entry *entry;
1145
1146 if (!handler)
1147 {
1148 cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler");
1149 return;
1150 }
1151
1152 entry = register_pragma_1 (pfile, space, name, false);
1153 if (entry)
1154 {
1155 entry->allow_expansion = allow_expansion;
1156 entry->u.handler = handler;
1157 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001158}
Neil Bootha5da89c2001-10-14 17:44:00 +00001159
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001160/* Similarly, but create mark the pragma for deferred processing.
1161 When found, a CPP_PRAGMA token will be insertted into the stream
1162 with IDENT in the token->u.pragma slot. */
1163void
1164cpp_register_deferred_pragma (cpp_reader *pfile, const char *space,
1165 const char *name, unsigned int ident,
1166 bool allow_expansion, bool allow_name_expansion)
1167{
1168 struct pragma_entry *entry;
1169
1170 entry = register_pragma_1 (pfile, space, name, allow_name_expansion);
1171 if (entry)
1172 {
1173 entry->is_deferred = true;
1174 entry->allow_expansion = allow_expansion;
1175 entry->u.ident = ident;
1176 }
1177}
1178
Neil Bootha5da89c2001-10-14 17:44:00 +00001179/* Register the pragmas the preprocessor itself handles. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001180void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001181_cpp_init_internal_pragmas (cpp_reader *pfile)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001182{
Neil Bootha5da89c2001-10-14 17:44:00 +00001183 /* Pragmas in the global namespace. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001184 register_pragma_internal (pfile, 0, "once", do_pragma_once);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001185
Neil Bootha5da89c2001-10-14 17:44:00 +00001186 /* New GCC-specific pragmas should be put in the GCC namespace. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001187 register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison);
1188 register_pragma_internal (pfile, "GCC", "system_header",
1189 do_pragma_system_header);
1190 register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
Nathan Sidwell82443372000-06-23 10:56:09 +00001191}
Per Bothner7f2935c1995-03-16 13:59:07 -08001192
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001193/* Return the number of registered pragmas in PE. */
1194
1195static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001196count_registered_pragmas (struct pragma_entry *pe)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001197{
1198 int ct = 0;
1199 for (; pe != NULL; pe = pe->next)
1200 {
1201 if (pe->is_nspace)
1202 ct += count_registered_pragmas (pe->u.space);
1203 ct++;
1204 }
1205 return ct;
1206}
1207
1208/* Save into SD the names of the registered pragmas referenced by PE,
1209 and return a pointer to the next free space in SD. */
1210
1211static char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001212save_registered_pragmas (struct pragma_entry *pe, char **sd)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001213{
1214 for (; pe != NULL; pe = pe->next)
1215 {
1216 if (pe->is_nspace)
1217 sd = save_registered_pragmas (pe->u.space, sd);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001218 *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
1219 HT_LEN (&pe->pragma->ident),
1220 HT_LEN (&pe->pragma->ident) + 1);
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001221 }
1222 return sd;
1223}
1224
1225/* Return a newly-allocated array which saves the names of the
1226 registered pragmas. */
1227
1228char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001229_cpp_save_pragma_names (cpp_reader *pfile)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001230{
1231 int ct = count_registered_pragmas (pfile->pragmas);
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001232 char **result = XNEWVEC (char *, ct);
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001233 (void) save_registered_pragmas (pfile->pragmas, result);
1234 return result;
1235}
1236
1237/* Restore from SD the names of the registered pragmas referenced by PE,
1238 and return a pointer to the next unused name in SD. */
1239
1240static char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001241restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1242 char **sd)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001243{
1244 for (; pe != NULL; pe = pe->next)
1245 {
1246 if (pe->is_nspace)
1247 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1248 pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd));
1249 free (*sd);
1250 sd++;
1251 }
1252 return sd;
1253}
1254
1255/* Restore the names of the registered pragmas from SAVED. */
1256
1257void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001258_cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001259{
1260 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1261 free (saved);
1262}
1263
Neil Bootha5da89c2001-10-14 17:44:00 +00001264/* Pragmata handling. We handle some, and pass the rest on to the
1265 front end. C99 defines three pragmas and says that no macro
1266 expansion is to be performed on them; whether or not macro
1267 expansion happens for other pragmas is implementation defined.
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001268 This implementation allows for a mix of both, since GCC did not
1269 traditionally macro expand its (few) pragmas, whereas OpenMP
1270 specifies that macro expansion should happen. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001271static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001272do_pragma (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001273{
Neil Bootha5da89c2001-10-14 17:44:00 +00001274 const struct pragma_entry *p = NULL;
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001275 const cpp_token *token, *pragma_token = pfile->cur_token;
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001276 cpp_token ns_token;
Neil Bootha5da89c2001-10-14 17:44:00 +00001277 unsigned int count = 1;
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001278
Neil Booth93c803682000-10-28 17:59:06 +00001279 pfile->state.prevent_expansion++;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001280
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001281 token = cpp_get_token (pfile);
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001282 ns_token = *token;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001283 if (token->type == CPP_NAME)
Alexandre Oliva0172e2b2000-02-27 06:24:27 +00001284 {
Neil Booth4b115ff2001-10-14 23:04:13 +00001285 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001286 if (p && p->is_nspace)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001287 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001288 bool allow_name_expansion = p->allow_expansion;
1289 if (allow_name_expansion)
1290 pfile->state.prevent_expansion--;
Neil Bootha5da89c2001-10-14 17:44:00 +00001291 token = cpp_get_token (pfile);
1292 if (token->type == CPP_NAME)
Neil Booth4b115ff2001-10-14 23:04:13 +00001293 p = lookup_pragma_entry (p->u.space, token->val.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001294 else
1295 p = NULL;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001296 if (allow_name_expansion)
1297 pfile->state.prevent_expansion++;
1298 count = 2;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001299 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001300 }
1301
Zack Weinberg3da3d582004-10-27 17:29:29 +00001302 if (p)
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001303 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001304 if (p->is_deferred)
Zack Weinberg3da3d582004-10-27 17:29:29 +00001305 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001306 pfile->directive_result.src_loc = pragma_token->src_loc;
1307 pfile->directive_result.type = CPP_PRAGMA;
1308 pfile->directive_result.flags = pragma_token->flags;
1309 pfile->directive_result.val.pragma = p->u.ident;
1310 pfile->state.in_deferred_pragma = true;
1311 pfile->state.pragma_allow_expansion = p->allow_expansion;
1312 if (!p->allow_expansion)
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001313 pfile->state.prevent_expansion++;
Zack Weinberg3da3d582004-10-27 17:29:29 +00001314 }
1315 else
1316 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001317 /* Since the handler below doesn't get the line number, that
1318 it might need for diagnostics, make sure it has the right
1319 numbers in place. */
1320 if (pfile->cb.line_change)
1321 (*pfile->cb.line_change) (pfile, pragma_token, false);
1322 if (p->allow_expansion)
1323 pfile->state.prevent_expansion--;
1324 (*p->u.handler) (pfile);
1325 if (p->allow_expansion)
1326 pfile->state.prevent_expansion++;
Zack Weinberg3da3d582004-10-27 17:29:29 +00001327 }
Zack Weinberg21b11492004-09-09 19:16:56 +00001328 }
Neil Boothd82fc102001-08-02 23:03:31 +00001329 else if (pfile->cb.def_pragma)
Neil Boothbdcbe492001-09-13 20:05:17 +00001330 {
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001331 if (count == 1 || pfile->context->prev == NULL)
1332 _cpp_backup_tokens (pfile, count);
1333 else
1334 {
1335 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1336 won't allow backing 2 tokens. */
1337 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1338 reads both tokens, we could perhaps free it, but if it doesn't,
1339 we don't know the exact lifespan. */
1340 cpp_token *toks = XNEWVEC (cpp_token, 2);
1341 toks[0] = ns_token;
1342 toks[0].flags |= NO_EXPAND;
1343 toks[1] = *token;
1344 toks[1].flags |= NO_EXPAND;
1345 _cpp_push_token_context (pfile, NULL, toks, 2);
1346 }
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001347 pfile->cb.def_pragma (pfile, pfile->directive_line);
Neil Boothbdcbe492001-09-13 20:05:17 +00001348 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001349
Neil Booth97293892001-09-14 22:04:46 +00001350 pfile->state.prevent_expansion--;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001351}
1352
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001353/* Handle #pragma once. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001354static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001355do_pragma_once (cpp_reader *pfile)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001356{
Tom Tromey705e2d22007-01-04 15:32:26 +00001357 if (cpp_in_primary_file (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +00001358 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
Neil Booth93c803682000-10-28 17:59:06 +00001359
1360 check_eol (pfile);
Neil Booth49634b32003-08-02 16:29:46 +00001361 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001362}
1363
Neil Boothc3bf3e62002-05-09 17:14:22 +00001364/* Handle #pragma GCC poison, to poison one or more identifiers so
1365 that the lexer produces a hard error for each subsequent usage. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001366static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001367do_pragma_poison (cpp_reader *pfile)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001368{
Neil Booth345894b2001-09-16 13:44:29 +00001369 const cpp_token *tok;
Zack Weinbergf8f769e2000-05-28 05:56:38 +00001370 cpp_hashnode *hp;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001371
Neil Booth93c803682000-10-28 17:59:06 +00001372 pfile->state.poisoned_ok = 1;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001373 for (;;)
1374 {
Neil Booth345894b2001-09-16 13:44:29 +00001375 tok = _cpp_lex_token (pfile);
1376 if (tok->type == CPP_EOF)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001377 break;
Neil Booth345894b2001-09-16 13:44:29 +00001378 if (tok->type != CPP_NAME)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001379 {
John David Anglin0527bc42003-11-01 22:56:54 +00001380 cpp_error (pfile, CPP_DL_ERROR,
1381 "invalid #pragma GCC poison directive");
Neil Booth93c803682000-10-28 17:59:06 +00001382 break;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001383 }
1384
Neil Booth345894b2001-09-16 13:44:29 +00001385 hp = tok->val.node;
Neil Booth93c803682000-10-28 17:59:06 +00001386 if (hp->flags & NODE_POISONED)
1387 continue;
1388
1389 if (hp->type == NT_MACRO)
John David Anglin0527bc42003-11-01 22:56:54 +00001390 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
Neil Boothebef4e82002-04-14 18:42:47 +00001391 NODE_NAME (hp));
Neil Booth93c803682000-10-28 17:59:06 +00001392 _cpp_free_definition (hp);
1393 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001394 }
Neil Booth93c803682000-10-28 17:59:06 +00001395 pfile->state.poisoned_ok = 0;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001396}
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001397
1398/* Mark the current header as a system header. This will suppress
1399 some categories of warnings (notably those from -pedantic). It is
1400 intended for use in system libraries that cannot be implemented in
1401 conforming C, but cannot be certain that their headers appear in a
1402 system include directory. To prevent abuse, it is rejected in the
1403 primary source file. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001404static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001405do_pragma_system_header (cpp_reader *pfile)
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001406{
Tom Tromey705e2d22007-01-04 15:32:26 +00001407 if (cpp_in_primary_file (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +00001408 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +00001409 "#pragma system_header ignored outside include file");
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001410 else
Neil Boothd82fc102001-08-02 23:03:31 +00001411 {
1412 check_eol (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +00001413 skip_rest_of_line (pfile);
Neil Boothd82fc102001-08-02 23:03:31 +00001414 cpp_make_system_header (pfile, 1, 0);
1415 }
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001416}
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001417
1418/* Check the modified date of the current include file against a specified
1419 file. Issue a diagnostic, if the specified file is newer. We use this to
1420 determine if a fixed header should be refixed. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001421static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001422do_pragma_dependency (cpp_reader *pfile)
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001423{
Neil Booth74eb4b32003-04-21 19:21:59 +00001424 const char *fname;
1425 int angle_brackets, ordering;
Kazu Hiratadf383482002-05-22 22:02:16 +00001426
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +00001427 fname = parse_include (pfile, &angle_brackets, NULL);
Neil Booth74eb4b32003-04-21 19:21:59 +00001428 if (!fname)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001429 return;
Zack Weinberg041c3192000-07-04 01:58:21 +00001430
Neil Booth74eb4b32003-04-21 19:21:59 +00001431 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001432 if (ordering < 0)
John David Anglin0527bc42003-11-01 22:56:54 +00001433 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001434 else if (ordering > 0)
1435 {
John David Anglin0527bc42003-11-01 22:56:54 +00001436 cpp_error (pfile, CPP_DL_WARNING,
1437 "current file is older than %s", fname);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001438 if (cpp_get_token (pfile)->type != CPP_EOF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001439 {
1440 _cpp_backup_tokens (pfile, 1);
John David Anglin0527bc42003-11-01 22:56:54 +00001441 do_diagnostic (pfile, CPP_DL_WARNING, 0);
Neil Boothbdcbe492001-09-13 20:05:17 +00001442 }
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001443 }
Neil Booth74eb4b32003-04-21 19:21:59 +00001444
Kaveh R. Ghazifad205f2003-06-16 21:41:10 +00001445 free ((void *) fname);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001446}
1447
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001448/* Get a token but skip padding. */
1449static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001450get_token_no_padding (cpp_reader *pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001451{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001452 for (;;)
1453 {
1454 const cpp_token *result = cpp_get_token (pfile);
1455 if (result->type != CPP_PADDING)
1456 return result;
1457 }
1458}
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001459
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001460/* Check syntax is "(string-literal)". Returns the string on success,
1461 or NULL on failure. */
1462static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001463get__Pragma_string (cpp_reader *pfile)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001464{
1465 const cpp_token *string;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001466
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001467 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1468 return NULL;
1469
1470 string = get_token_no_padding (pfile);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001471 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001472 return NULL;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001473
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001474 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1475 return NULL;
1476
1477 return string;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001478}
1479
Neil Booth87062812001-10-20 09:00:53 +00001480/* Destringize IN into a temporary buffer, by removing the first \ of
1481 \" and \\ sequences, and process the result as a #pragma directive. */
1482static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001483destringize_and_run (cpp_reader *pfile, const cpp_string *in)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001484{
1485 const unsigned char *src, *limit;
Neil Booth87062812001-10-20 09:00:53 +00001486 char *dest, *result;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001487 cpp_context *saved_context;
1488 cpp_token *saved_cur_token;
1489 tokenrun *saved_cur_run;
1490 cpp_token *toks;
1491 int count;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001492
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001493 dest = result = (char *) alloca (in->len - 1);
Neil Booth6338b352003-04-23 22:44:06 +00001494 src = in->text + 1 + (in->text[0] == 'L');
1495 limit = in->text + in->len - 1;
1496 while (src < limit)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001497 {
1498 /* We know there is a character following the backslash. */
1499 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1500 src++;
1501 *dest++ = *src++;
1502 }
Neil Booth26aea072003-04-19 00:22:51 +00001503 *dest = '\n';
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001504
Neil Booth8128ccc2002-11-18 20:43:40 +00001505 /* Ugh; an awful kludge. We are really not set up to be lexing
1506 tokens when in the middle of a macro expansion. Use a new
1507 context to force cpp_get_token to lex, and so skip_rest_of_line
1508 doesn't go beyond the end of the text. Also, remember the
1509 current lexing position so we can return to it later.
Neil Booth79ba5e32002-09-03 21:55:40 +00001510
Neil Booth8128ccc2002-11-18 20:43:40 +00001511 Something like line-at-a-time lexing should remove the need for
1512 this. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001513 saved_context = pfile->context;
1514 saved_cur_token = pfile->cur_token;
1515 saved_cur_run = pfile->cur_run;
Neil Booth8128ccc2002-11-18 20:43:40 +00001516
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001517 pfile->context = XNEW (cpp_context);
1518 pfile->context->macro = 0;
1519 pfile->context->prev = 0;
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001520 pfile->context->next = 0;
Neil Booth79ba5e32002-09-03 21:55:40 +00001521
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001522 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1523 until we've read all of the tokens that we want. */
1524 cpp_push_buffer (pfile, (const uchar *) result, dest - result,
1525 /* from_stage3 */ true);
1526 /* ??? Antique Disgusting Hack. What does this do? */
1527 if (pfile->buffer->prev)
1528 pfile->buffer->file = pfile->buffer->prev->file;
Neil Booth79ba5e32002-09-03 21:55:40 +00001529
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001530 start_directive (pfile);
1531 _cpp_clean_line (pfile);
1532 do_pragma (pfile);
1533 end_directive (pfile, 1);
Neil Booth79ba5e32002-09-03 21:55:40 +00001534
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001535 /* We always insert at least one token, the directive result. It'll
1536 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1537 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
Neil Booth79ba5e32002-09-03 21:55:40 +00001538
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001539 /* If we're not handling the pragma internally, read all of the tokens from
1540 the string buffer now, while the string buffer is still installed. */
1541 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1542 to me what the true lifespan of the tokens are. It would appear that
1543 the lifespan is the entire parse of the main input stream, in which case
1544 this may not be wrong. */
1545 if (pfile->directive_result.type == CPP_PRAGMA)
1546 {
1547 int maxcount;
Neil Booth79ba5e32002-09-03 21:55:40 +00001548
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001549 count = 1;
1550 maxcount = 50;
1551 toks = XNEWVEC (cpp_token, maxcount);
1552 toks[0] = pfile->directive_result;
1553
1554 do
1555 {
1556 if (count == maxcount)
1557 {
1558 maxcount = maxcount * 3 / 2;
1559 toks = XRESIZEVEC (cpp_token, toks, maxcount);
1560 }
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001561 toks[count] = *cpp_get_token (pfile);
1562 /* Macros have been already expanded by cpp_get_token
1563 if the pragma allowed expansion. */
1564 toks[count++].flags |= NO_EXPAND;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001565 }
1566 while (toks[count-1].type != CPP_PRAGMA_EOL);
1567 }
1568 else
1569 {
1570 count = 1;
1571 toks = XNEW (cpp_token);
1572 toks[0] = pfile->directive_result;
1573
1574 /* If we handled the entire pragma internally, make sure we get the
1575 line number correct for the next token. */
1576 if (pfile->cb.line_change)
1577 pfile->cb.line_change (pfile, pfile->cur_token, false);
1578 }
1579
1580 /* Finish inlining run_directive. */
1581 pfile->buffer->file = NULL;
1582 _cpp_pop_buffer (pfile);
1583
1584 /* Reset the old macro state before ... */
1585 XDELETE (pfile->context);
1586 pfile->context = saved_context;
1587 pfile->cur_token = saved_cur_token;
1588 pfile->cur_run = saved_cur_run;
1589
1590 /* ... inserting the new tokens we collected. */
1591 _cpp_push_token_context (pfile, NULL, toks, count);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001592}
1593
Neil Booth87062812001-10-20 09:00:53 +00001594/* Handle the _Pragma operator. */
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001595void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001596_cpp_do__Pragma (cpp_reader *pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001597{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001598 const cpp_token *string = get__Pragma_string (pfile);
Zack Weinberg21b11492004-09-09 19:16:56 +00001599 pfile->directive_result.type = CPP_PADDING;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001600
Neil Booth79ba5e32002-09-03 21:55:40 +00001601 if (string)
1602 destringize_and_run (pfile, &string->val.str);
1603 else
John David Anglin0527bc42003-11-01 22:56:54 +00001604 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00001605 "_Pragma takes a parenthesized string literal");
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001606}
1607
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001608/* Handle #ifdef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001609static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001610do_ifdef (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +00001611{
Neil Booth93c803682000-10-28 17:59:06 +00001612 int skip = 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001613
Neil Boothcef0d192001-07-26 06:02:47 +00001614 if (! pfile->state.skipping)
Neil Booth93c803682000-10-28 17:59:06 +00001615 {
Tom Tromeyee1c2a12007-01-12 19:46:49 +00001616 const cpp_hashnode *node = lex_macro_node (pfile, false);
Zack Weinberg041c3192000-07-04 01:58:21 +00001617
Neil Booth93c803682000-10-28 17:59:06 +00001618 if (node)
Neil Bootha69cbaa2002-07-23 22:57:49 +00001619 {
1620 skip = node->type != NT_MACRO;
1621 _cpp_mark_macro_used (node);
1622 check_eol (pfile);
1623 }
Neil Booth93c803682000-10-28 17:59:06 +00001624 }
1625
1626 push_conditional (pfile, skip, T_IFDEF, 0);
Zack Weinberg168d3732000-03-14 06:34:11 +00001627}
1628
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001629/* Handle #ifndef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001630static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001631do_ifndef (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +00001632{
Neil Booth93c803682000-10-28 17:59:06 +00001633 int skip = 1;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001634 const cpp_hashnode *node = 0;
Zack Weinberg168d3732000-03-14 06:34:11 +00001635
Neil Boothcef0d192001-07-26 06:02:47 +00001636 if (! pfile->state.skipping)
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001637 {
Tom Tromeyee1c2a12007-01-12 19:46:49 +00001638 node = lex_macro_node (pfile, false);
Geoffrey Keatingb43db0b2000-12-02 22:28:44 +00001639
1640 if (node)
Neil Bootha69cbaa2002-07-23 22:57:49 +00001641 {
1642 skip = node->type == NT_MACRO;
1643 _cpp_mark_macro_used (node);
1644 check_eol (pfile);
1645 }
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001646 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001647
Neil Booth93c803682000-10-28 17:59:06 +00001648 push_conditional (pfile, skip, T_IFNDEF, node);
Per Bothner7f2935c1995-03-16 13:59:07 -08001649}
1650
Neil Booth6d18adb2001-07-29 17:27:57 +00001651/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1652 pfile->mi_ind_cmacro so we can handle multiple-include
Kazu Hirata6356f892003-06-12 19:01:08 +00001653 optimizations. If macro expansion occurs in the expression, we
Neil Booth6d18adb2001-07-29 17:27:57 +00001654 cannot treat it as a controlling conditional, since the expansion
1655 could change in the future. That is handled by cpp_get_token. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001656static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001657do_if (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001658{
Neil Booth93c803682000-10-28 17:59:06 +00001659 int skip = 1;
Per Bothner7f2935c1995-03-16 13:59:07 -08001660
Neil Boothcef0d192001-07-26 06:02:47 +00001661 if (! pfile->state.skipping)
Neil Booth87ed1092002-04-28 19:42:54 +00001662 skip = _cpp_parse_expr (pfile) == false;
Neil Booth93c803682000-10-28 17:59:06 +00001663
Neil Booth6d18adb2001-07-29 17:27:57 +00001664 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
Per Bothner7f2935c1995-03-16 13:59:07 -08001665}
1666
Neil Boothb528a072000-11-12 11:46:21 +00001667/* Flip skipping state if appropriate and continue without changing
Zack Weinbergea4a4532000-05-29 16:19:32 +00001668 if_stack; this is so that the error message for missing #endif's
1669 etc. will point to the original #if. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001670static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001671do_else (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001672{
Neil Boothb528a072000-11-12 11:46:21 +00001673 cpp_buffer *buffer = pfile->buffer;
1674 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001675
1676 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001677 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
Neil Booth93c803682000-10-28 17:59:06 +00001678 else
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001679 {
Neil Booth93c803682000-10-28 17:59:06 +00001680 if (ifs->type == T_ELSE)
1681 {
John David Anglin0527bc42003-11-01 22:56:54 +00001682 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1683 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Booth93c803682000-10-28 17:59:06 +00001684 "the conditional began here");
1685 }
Neil Boothb528a072000-11-12 11:46:21 +00001686 ifs->type = T_ELSE;
1687
Neil Boothcef0d192001-07-26 06:02:47 +00001688 /* Skip any future (erroneous) #elses or #elifs. */
1689 pfile->state.skipping = ifs->skip_elses;
1690 ifs->skip_elses = true;
Neil Booth93c803682000-10-28 17:59:06 +00001691
1692 /* Invalidate any controlling macro. */
1693 ifs->mi_cmacro = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001694
Neil Boothcef0d192001-07-26 06:02:47 +00001695 /* Only check EOL if was not originally skipping. */
Phil Edwards909de5d2002-03-22 21:59:04 +00001696 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
Neil Boothcef0d192001-07-26 06:02:47 +00001697 check_eol (pfile);
1698 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001699}
1700
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001701/* Handle a #elif directive by not changing if_stack either. See the
Neil Booth93c803682000-10-28 17:59:06 +00001702 comment above do_else. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001703static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001704do_elif (cpp_reader *pfile)
Zack Weinbergea4a4532000-05-29 16:19:32 +00001705{
Neil Boothb528a072000-11-12 11:46:21 +00001706 cpp_buffer *buffer = pfile->buffer;
1707 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001708
1709 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001710 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
Neil Boothb528a072000-11-12 11:46:21 +00001711 else
Zack Weinbergea4a4532000-05-29 16:19:32 +00001712 {
Neil Boothb528a072000-11-12 11:46:21 +00001713 if (ifs->type == T_ELSE)
1714 {
John David Anglin0527bc42003-11-01 22:56:54 +00001715 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1716 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Boothb528a072000-11-12 11:46:21 +00001717 "the conditional began here");
1718 }
1719 ifs->type = T_ELIF;
1720
Neil Boothcef0d192001-07-26 06:02:47 +00001721 /* Only evaluate this if we aren't skipping elses. During
1722 evaluation, set skipping to false to get lexer warnings. */
1723 if (ifs->skip_elses)
1724 pfile->state.skipping = 1;
1725 else
Neil Boothb528a072000-11-12 11:46:21 +00001726 {
Neil Boothcef0d192001-07-26 06:02:47 +00001727 pfile->state.skipping = 0;
1728 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1729 ifs->skip_elses = ! pfile->state.skipping;
Neil Boothb528a072000-11-12 11:46:21 +00001730 }
Neil Boothcef0d192001-07-26 06:02:47 +00001731
1732 /* Invalidate any controlling macro. */
1733 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001734 }
Zack Weinbergea4a4532000-05-29 16:19:32 +00001735}
1736
Neil Boothcef0d192001-07-26 06:02:47 +00001737/* #endif pops the if stack and resets pfile->state.skipping. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001738static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001739do_endif (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001740{
Neil Boothb528a072000-11-12 11:46:21 +00001741 cpp_buffer *buffer = pfile->buffer;
1742 struct if_stack *ifs = buffer->if_stack;
Per Bothner7f2935c1995-03-16 13:59:07 -08001743
Zack Weinbergea4a4532000-05-29 16:19:32 +00001744 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001745 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
Per Bothner7f2935c1995-03-16 13:59:07 -08001746 else
1747 {
Neil Boothcef0d192001-07-26 06:02:47 +00001748 /* Only check EOL if was not originally skipping. */
Phil Edwards909de5d2002-03-22 21:59:04 +00001749 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
Neil Boothcef0d192001-07-26 06:02:47 +00001750 check_eol (pfile);
1751
Neil Booth93c803682000-10-28 17:59:06 +00001752 /* If potential control macro, we go back outside again. */
1753 if (ifs->next == 0 && ifs->mi_cmacro)
1754 {
Neil Booth6d18adb2001-07-29 17:27:57 +00001755 pfile->mi_valid = true;
Neil Booth93c803682000-10-28 17:59:06 +00001756 pfile->mi_cmacro = ifs->mi_cmacro;
1757 }
1758
Neil Boothb528a072000-11-12 11:46:21 +00001759 buffer->if_stack = ifs->next;
Neil Boothcef0d192001-07-26 06:02:47 +00001760 pfile->state.skipping = ifs->was_skipping;
Neil Booth2a967f32001-05-20 06:26:45 +00001761 obstack_free (&pfile->buffer_ob, ifs);
Per Bothner7f2935c1995-03-16 13:59:07 -08001762 }
Neil Booth93c803682000-10-28 17:59:06 +00001763}
Zack Weinberg041c3192000-07-04 01:58:21 +00001764
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001765/* Push an if_stack entry for a preprocessor conditional, and set
1766 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1767 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1768 we need to check here that we are at the top of the file. */
Zack Weinbergea4a4532000-05-29 16:19:32 +00001769static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001770push_conditional (cpp_reader *pfile, int skip, int type,
1771 const cpp_hashnode *cmacro)
Zack Weinbergea4a4532000-05-29 16:19:32 +00001772{
1773 struct if_stack *ifs;
Neil Boothb528a072000-11-12 11:46:21 +00001774 cpp_buffer *buffer = pfile->buffer;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001775
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001776 ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
Neil Booth50410422001-09-15 10:18:03 +00001777 ifs->line = pfile->directive_line;
Neil Boothb528a072000-11-12 11:46:21 +00001778 ifs->next = buffer->if_stack;
Neil Boothcef0d192001-07-26 06:02:47 +00001779 ifs->skip_elses = pfile->state.skipping || !skip;
1780 ifs->was_skipping = pfile->state.skipping;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001781 ifs->type = type;
Neil Booth6d18adb2001-07-29 17:27:57 +00001782 /* This condition is effectively a test for top-of-file. */
1783 if (pfile->mi_valid && pfile->mi_cmacro == 0)
Neil Booth93c803682000-10-28 17:59:06 +00001784 ifs->mi_cmacro = cmacro;
1785 else
1786 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001787
Neil Boothcef0d192001-07-26 06:02:47 +00001788 pfile->state.skipping = skip;
Neil Boothb528a072000-11-12 11:46:21 +00001789 buffer->if_stack = ifs;
Zack Weinberg7061aa51998-12-15 11:09:16 +00001790}
1791
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001792/* Read the tokens of the answer into the macro pool, in a directive
1793 of type TYPE. Only commit the memory if we intend it as permanent
1794 storage, i.e. the #assert case. Returns 0 on success, and sets
1795 ANSWERP to point to the answer. */
Neil Booth93c803682000-10-28 17:59:06 +00001796static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001797parse_answer (cpp_reader *pfile, struct answer **answerp, int type)
Zack Weinberg041c3192000-07-04 01:58:21 +00001798{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001799 const cpp_token *paren;
Neil Booth93c803682000-10-28 17:59:06 +00001800 struct answer *answer;
Neil Booth8c3b2692001-09-30 10:03:11 +00001801 unsigned int acount;
Zack Weinberg041c3192000-07-04 01:58:21 +00001802
Neil Booth93c803682000-10-28 17:59:06 +00001803 /* In a conditional, it is legal to not have an open paren. We
1804 should save the following token in this case. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001805 paren = cpp_get_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001806
1807 /* If not a paren, see if we're OK. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001808 if (paren->type != CPP_OPEN_PAREN)
Zack Weinberg041c3192000-07-04 01:58:21 +00001809 {
Neil Booth93c803682000-10-28 17:59:06 +00001810 /* In a conditional no answer is a test for any answer. It
1811 could be followed by any token. */
1812 if (type == T_IF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001813 {
1814 _cpp_backup_tokens (pfile, 1);
1815 return 0;
1816 }
Neil Booth93c803682000-10-28 17:59:06 +00001817
1818 /* #unassert with no answer is valid - it removes all answers. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001819 if (type == T_UNASSERT && paren->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +00001820 return 0;
1821
John David Anglin0527bc42003-11-01 22:56:54 +00001822 cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate");
Neil Booth93c803682000-10-28 17:59:06 +00001823 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001824 }
1825
Neil Booth8c3b2692001-09-30 10:03:11 +00001826 for (acount = 0;; acount++)
Zack Weinberg041c3192000-07-04 01:58:21 +00001827 {
Neil Booth8c3b2692001-09-30 10:03:11 +00001828 size_t room_needed;
1829 const cpp_token *token = cpp_get_token (pfile);
1830 cpp_token *dest;
Zack Weinberg041c3192000-07-04 01:58:21 +00001831
Neil Booth93c803682000-10-28 17:59:06 +00001832 if (token->type == CPP_CLOSE_PAREN)
1833 break;
Zack Weinberg041c3192000-07-04 01:58:21 +00001834
1835 if (token->type == CPP_EOF)
1836 {
John David Anglin0527bc42003-11-01 22:56:54 +00001837 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
Neil Booth93c803682000-10-28 17:59:06 +00001838 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001839 }
Neil Booth8c3b2692001-09-30 10:03:11 +00001840
1841 /* struct answer includes the space for one token. */
1842 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1843
1844 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1845 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1846
1847 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1848 *dest = *token;
1849
1850 /* Drop whitespace at start, for answer equivalence purposes. */
1851 if (acount == 0)
1852 dest->flags &= ~PREV_WHITE;
Zack Weinberg041c3192000-07-04 01:58:21 +00001853 }
1854
Neil Booth8c3b2692001-09-30 10:03:11 +00001855 if (acount == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001856 {
John David Anglin0527bc42003-11-01 22:56:54 +00001857 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
Neil Booth93c803682000-10-28 17:59:06 +00001858 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001859 }
1860
Neil Booth8c3b2692001-09-30 10:03:11 +00001861 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1862 answer->count = acount;
1863 answer->next = NULL;
Neil Booth93c803682000-10-28 17:59:06 +00001864 *answerp = answer;
Zack Weinberg041c3192000-07-04 01:58:21 +00001865
Neil Booth93c803682000-10-28 17:59:06 +00001866 return 0;
1867}
1868
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001869/* Parses an assertion directive of type TYPE, returning a pointer to
1870 the hash node of the predicate, or 0 on error. If an answer was
1871 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
Neil Booth93c803682000-10-28 17:59:06 +00001872static cpp_hashnode *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001873parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
Neil Booth93c803682000-10-28 17:59:06 +00001874{
1875 cpp_hashnode *result = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001876 const cpp_token *predicate;
Neil Booth93c803682000-10-28 17:59:06 +00001877
1878 /* We don't expand predicates or answers. */
1879 pfile->state.prevent_expansion++;
1880
Neil Booth93c803682000-10-28 17:59:06 +00001881 *answerp = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001882 predicate = cpp_get_token (pfile);
1883 if (predicate->type == CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +00001884 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001885 else if (predicate->type != CPP_NAME)
John David Anglin0527bc42003-11-01 22:56:54 +00001886 cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier");
Neil Booth93c803682000-10-28 17:59:06 +00001887 else if (parse_answer (pfile, answerp, type) == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001888 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001889 unsigned int len = NODE_LEN (predicate->val.node);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001890 unsigned char *sym = (unsigned char *) alloca (len + 1);
Neil Booth93c803682000-10-28 17:59:06 +00001891
1892 /* Prefix '#' to get it out of macro namespace. */
1893 sym[0] = '#';
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001894 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
Neil Booth93c803682000-10-28 17:59:06 +00001895 result = cpp_lookup (pfile, sym, len + 1);
Zack Weinberg041c3192000-07-04 01:58:21 +00001896 }
1897
Neil Booth93c803682000-10-28 17:59:06 +00001898 pfile->state.prevent_expansion--;
1899 return result;
Zack Weinberg041c3192000-07-04 01:58:21 +00001900}
1901
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001902/* Returns a pointer to the pointer to CANDIDATE in the answer chain,
Zack Weinberg041c3192000-07-04 01:58:21 +00001903 or a pointer to NULL if the answer is not in the chain. */
Neil Booth93c803682000-10-28 17:59:06 +00001904static struct answer **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001905find_answer (cpp_hashnode *node, const struct answer *candidate)
Zack Weinberg041c3192000-07-04 01:58:21 +00001906{
Neil Booth93c803682000-10-28 17:59:06 +00001907 unsigned int i;
Zack Weinberg041c3192000-07-04 01:58:21 +00001908 struct answer **result;
1909
1910 for (result = &node->value.answers; *result; result = &(*result)->next)
Neil Booth93c803682000-10-28 17:59:06 +00001911 {
1912 struct answer *answer = *result;
1913
1914 if (answer->count == candidate->count)
1915 {
1916 for (i = 0; i < answer->count; i++)
1917 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1918 break;
1919
1920 if (i == answer->count)
1921 break;
1922 }
1923 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001924
1925 return result;
1926}
1927
Neil Booth93c803682000-10-28 17:59:06 +00001928/* Test an assertion within a preprocessor conditional. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +00001929 nonzero on failure, zero on success. On success, the result of
Hans-Peter Nilsson24026452002-11-29 22:41:04 +00001930 the test is written into VALUE, otherwise the value 0. */
Neil Booth93c803682000-10-28 17:59:06 +00001931int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001932_cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
Neil Booth93c803682000-10-28 17:59:06 +00001933{
1934 struct answer *answer;
1935 cpp_hashnode *node;
1936
1937 node = parse_assertion (pfile, &answer, T_IF);
Hans-Peter Nilsson24026452002-11-29 22:41:04 +00001938
1939 /* For recovery, an erroneous assertion expression is handled as a
1940 failing assertion. */
1941 *value = 0;
1942
Neil Booth93c803682000-10-28 17:59:06 +00001943 if (node)
1944 *value = (node->type == NT_ASSERTION &&
1945 (answer == 0 || *find_answer (node, answer) != 0));
Neil Booth91318902002-05-26 18:42:21 +00001946 else if (pfile->cur_token[-1].type == CPP_EOF)
1947 _cpp_backup_tokens (pfile, 1);
Neil Booth93c803682000-10-28 17:59:06 +00001948
1949 /* We don't commit the memory for the answer - it's temporary only. */
1950 return node == 0;
1951}
1952
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001953/* Handle #assert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001954static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001955do_assert (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001956{
Zack Weinberg041c3192000-07-04 01:58:21 +00001957 struct answer *new_answer;
1958 cpp_hashnode *node;
Kazu Hiratadf383482002-05-22 22:02:16 +00001959
Neil Booth93c803682000-10-28 17:59:06 +00001960 node = parse_assertion (pfile, &new_answer, T_ASSERT);
Zack Weinberg041c3192000-07-04 01:58:21 +00001961 if (node)
1962 {
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001963 size_t answer_size;
1964
Neil Booth93c803682000-10-28 17:59:06 +00001965 /* Place the new answer in the answer list. First check there
1966 is not a duplicate. */
Zack Weinberg041c3192000-07-04 01:58:21 +00001967 new_answer->next = 0;
Neil Booth93c803682000-10-28 17:59:06 +00001968 if (node->type == NT_ASSERTION)
Zack Weinberg041c3192000-07-04 01:58:21 +00001969 {
Neil Booth93c803682000-10-28 17:59:06 +00001970 if (*find_answer (node, new_answer))
1971 {
John David Anglin0527bc42003-11-01 22:56:54 +00001972 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
Neil Boothebef4e82002-04-14 18:42:47 +00001973 NODE_NAME (node) + 1);
Neil Booth93c803682000-10-28 17:59:06 +00001974 return;
1975 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001976 new_answer->next = node->value.answers;
1977 }
Neil Booth8c3b2692001-09-30 10:03:11 +00001978
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001979 answer_size = sizeof (struct answer) + ((new_answer->count - 1)
1980 * sizeof (cpp_token));
1981 /* Commit or allocate storage for the object. */
1982 if (pfile->hash_table->alloc_subobject)
1983 {
1984 struct answer *temp_answer = new_answer;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001985 new_answer = (struct answer *) pfile->hash_table->alloc_subobject
1986 (answer_size);
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001987 memcpy (new_answer, temp_answer, answer_size);
1988 }
1989 else
1990 BUFF_FRONT (pfile->a_buff) += answer_size;
1991
Neil Booth93c803682000-10-28 17:59:06 +00001992 node->type = NT_ASSERTION;
Zack Weinberg041c3192000-07-04 01:58:21 +00001993 node->value.answers = new_answer;
Neil Booth8c3b2692001-09-30 10:03:11 +00001994 check_eol (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +00001995 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001996}
Zack Weinberg7061aa51998-12-15 11:09:16 +00001997
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001998/* Handle #unassert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001999static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002000do_unassert (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08002001{
Zack Weinberg041c3192000-07-04 01:58:21 +00002002 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00002003 struct answer *answer;
Kazu Hiratadf383482002-05-22 22:02:16 +00002004
Neil Booth93c803682000-10-28 17:59:06 +00002005 node = parse_assertion (pfile, &answer, T_UNASSERT);
2006 /* It isn't an error to #unassert something that isn't asserted. */
2007 if (node && node->type == NT_ASSERTION)
Zack Weinberg7061aa51998-12-15 11:09:16 +00002008 {
Zack Weinberg041c3192000-07-04 01:58:21 +00002009 if (answer)
Neil Booth93c803682000-10-28 17:59:06 +00002010 {
2011 struct answer **p = find_answer (node, answer), *temp;
2012
2013 /* Remove the answer from the list. */
2014 temp = *p;
2015 if (temp)
2016 *p = temp->next;
2017
2018 /* Did we free the last answer? */
2019 if (node->value.answers == 0)
2020 node->type = NT_VOID;
Neil Booth8c3b2692001-09-30 10:03:11 +00002021
2022 check_eol (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00002023 }
2024 else
2025 _cpp_free_definition (node);
Zack Weinberg7061aa51998-12-15 11:09:16 +00002026 }
Neil Booth93c803682000-10-28 17:59:06 +00002027
2028 /* We don't commit the memory for the answer - it's temporary only. */
Per Bothner7f2935c1995-03-16 13:59:07 -08002029}
Per Bothner7f2935c1995-03-16 13:59:07 -08002030
Zack Weinberg45b966d2000-03-13 22:01:08 +00002031/* These are for -D, -U, -A. */
2032
2033/* Process the string STR as if it appeared as the body of a #define.
2034 If STR is just an identifier, define it with value 1.
2035 If STR has anything after the identifier, then it should
Kazu Hirataec5c56d2001-08-01 17:57:27 +00002036 be identifier=definition. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00002037void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002038cpp_define (cpp_reader *pfile, const char *str)
Zack Weinberg45b966d2000-03-13 22:01:08 +00002039{
2040 char *buf, *p;
2041 size_t count;
2042
Kazu Hiratadf383482002-05-22 22:02:16 +00002043 /* Copy the entire option so we can modify it.
Zack Weinberg45b966d2000-03-13 22:01:08 +00002044 Change the first "=" in the string to a space. If there is none,
Neil Booth86368122000-10-31 23:34:59 +00002045 tack " 1" on the end. */
2046
Neil Booth86368122000-10-31 23:34:59 +00002047 count = strlen (str);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002048 buf = (char *) alloca (count + 3);
Neil Booth86368122000-10-31 23:34:59 +00002049 memcpy (buf, str, count);
2050
2051 p = strchr (str, '=');
Zack Weinberg45b966d2000-03-13 22:01:08 +00002052 if (p)
Neil Booth86368122000-10-31 23:34:59 +00002053 buf[p - str] = ' ';
Zack Weinberg45b966d2000-03-13 22:01:08 +00002054 else
2055 {
Neil Booth86368122000-10-31 23:34:59 +00002056 buf[count++] = ' ';
2057 buf[count++] = '1';
Zack Weinberg45b966d2000-03-13 22:01:08 +00002058 }
Neil Booth26aea072003-04-19 00:22:51 +00002059 buf[count] = '\n';
Zack Weinberg45b966d2000-03-13 22:01:08 +00002060
Neil Booth29401c32001-08-22 20:37:20 +00002061 run_directive (pfile, T_DEFINE, buf, count);
Zack Weinberg2c8f0512000-08-29 18:37:37 +00002062}
2063
Neil Boothad2a0842000-12-17 00:13:54 +00002064/* Slight variant of the above for use by initialize_builtins. */
Zack Weinberg2c8f0512000-08-29 18:37:37 +00002065void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002066_cpp_define_builtin (cpp_reader *pfile, const char *str)
Zack Weinberg2c8f0512000-08-29 18:37:37 +00002067{
Neil Booth26aea072003-04-19 00:22:51 +00002068 size_t len = strlen (str);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002069 char *buf = (char *) alloca (len + 1);
Neil Booth26aea072003-04-19 00:22:51 +00002070 memcpy (buf, str, len);
2071 buf[len] = '\n';
2072 run_directive (pfile, T_DEFINE, buf, len);
Zack Weinberg45b966d2000-03-13 22:01:08 +00002073}
2074
2075/* Process MACRO as if it appeared as the body of an #undef. */
2076void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002077cpp_undef (cpp_reader *pfile, const char *macro)
Zack Weinberg45b966d2000-03-13 22:01:08 +00002078{
Neil Booth26aea072003-04-19 00:22:51 +00002079 size_t len = strlen (macro);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002080 char *buf = (char *) alloca (len + 1);
Neil Booth26aea072003-04-19 00:22:51 +00002081 memcpy (buf, macro, len);
2082 buf[len] = '\n';
2083 run_directive (pfile, T_UNDEF, buf, len);
Zack Weinberg45b966d2000-03-13 22:01:08 +00002084}
2085
Richard Henderson121de392007-03-30 14:12:53 -07002086/* Like lex_macro_node, but read the input from STR. */
2087static cpp_hashnode *
2088lex_macro_node_from_str (cpp_reader *pfile, const char *str)
2089{
2090 size_t len = strlen (str);
Michael Meissner4cd97072007-03-30 22:40:19 +00002091 uchar *buf = (uchar *) alloca (len + 1);
Richard Henderson121de392007-03-30 14:12:53 -07002092 cpp_hashnode *node;
2093
2094 memcpy (buf, str, len);
2095 buf[len] = '\n';
2096 cpp_push_buffer (pfile, buf, len, true);
2097 node = lex_macro_node (pfile, true);
2098 _cpp_pop_buffer (pfile);
2099
2100 return node;
2101}
2102
2103/* If STR is a defined macro, return its definition node, else return NULL. */
2104cpp_macro *
2105cpp_push_definition (cpp_reader *pfile, const char *str)
2106{
2107 cpp_hashnode *node = lex_macro_node_from_str (pfile, str);
2108 if (node && node->type == NT_MACRO)
2109 return node->value.macro;
2110 else
2111 return NULL;
2112}
2113
2114/* Replace a previous definition DFN of the macro STR. If DFN is NULL,
2115 then the macro should be undefined. */
2116void
2117cpp_pop_definition (cpp_reader *pfile, const char *str, cpp_macro *dfn)
2118{
2119 cpp_hashnode *node = lex_macro_node_from_str (pfile, str);
2120 if (node == NULL)
2121 return;
2122
2123 if (node->type == NT_MACRO)
2124 {
2125 if (pfile->cb.undef)
2126 pfile->cb.undef (pfile, pfile->directive_line, node);
2127 if (CPP_OPTION (pfile, warn_unused_macros))
2128 _cpp_warn_if_unused_macro (pfile, node, NULL);
2129 }
2130 if (node->type != NT_VOID)
2131 _cpp_free_definition (node);
2132
2133 if (dfn)
2134 {
2135 node->type = NT_MACRO;
2136 node->value.macro = dfn;
2137 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
2138 node->flags |= NODE_WARN;
2139
2140 if (pfile->cb.define)
2141 pfile->cb.define (pfile, pfile->directive_line, node);
2142 }
2143}
2144
Kazu Hirataec5c56d2001-08-01 17:57:27 +00002145/* Process the string STR as if it appeared as the body of a #assert. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00002146void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002147cpp_assert (cpp_reader *pfile, const char *str)
Zack Weinberg45b966d2000-03-13 22:01:08 +00002148{
Neil Booth86368122000-10-31 23:34:59 +00002149 handle_assertion (pfile, str, T_ASSERT);
Zack Weinberg45b966d2000-03-13 22:01:08 +00002150}
2151
Kazu Hirataec5c56d2001-08-01 17:57:27 +00002152/* Process STR as if it appeared as the body of an #unassert. */
Zack Weinberg0b22d651999-03-15 18:42:46 +00002153void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002154cpp_unassert (cpp_reader *pfile, const char *str)
Zack Weinberg0b22d651999-03-15 18:42:46 +00002155{
Neil Booth86368122000-10-31 23:34:59 +00002156 handle_assertion (pfile, str, T_UNASSERT);
Kazu Hiratadf383482002-05-22 22:02:16 +00002157}
Zack Weinberg0b22d651999-03-15 18:42:46 +00002158
Neil Booth86368122000-10-31 23:34:59 +00002159/* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2160static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002161handle_assertion (cpp_reader *pfile, const char *str, int type)
Neil Booth86368122000-10-31 23:34:59 +00002162{
2163 size_t count = strlen (str);
2164 const char *p = strchr (str, '=');
2165
Neil Booth26aea072003-04-19 00:22:51 +00002166 /* Copy the entire option so we can modify it. Change the first
2167 "=" in the string to a '(', and tack a ')' on the end. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002168 char *buf = (char *) alloca (count + 2);
Neil Booth26aea072003-04-19 00:22:51 +00002169
2170 memcpy (buf, str, count);
Neil Booth86368122000-10-31 23:34:59 +00002171 if (p)
2172 {
Neil Booth86368122000-10-31 23:34:59 +00002173 buf[p - str] = '(';
2174 buf[count++] = ')';
Neil Booth86368122000-10-31 23:34:59 +00002175 }
Neil Booth26aea072003-04-19 00:22:51 +00002176 buf[count] = '\n';
2177 str = buf;
Neil Booth86368122000-10-31 23:34:59 +00002178
Neil Booth29401c32001-08-22 20:37:20 +00002179 run_directive (pfile, type, str, count);
Neil Booth86368122000-10-31 23:34:59 +00002180}
2181
Neil Booth7e96d762001-01-13 01:00:01 +00002182/* The number of errors for a given reader. */
2183unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002184cpp_errors (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00002185{
2186 return pfile->errors;
2187}
2188
2189/* The options structure. */
2190cpp_options *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002191cpp_get_options (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00002192{
2193 return &pfile->opts;
2194}
2195
2196/* The callbacks structure. */
2197cpp_callbacks *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002198cpp_get_callbacks (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00002199{
2200 return &pfile->cb;
2201}
2202
2203/* Copy the given callbacks structure to our own. */
2204void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002205cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
Neil Booth7e96d762001-01-13 01:00:01 +00002206{
2207 pfile->cb = *cb;
2208}
2209
Zack Weinbergc6e83802004-06-05 20:58:06 +00002210/* The dependencies structure. (Creates one if it hasn't already been.) */
2211struct deps *
2212cpp_get_deps (cpp_reader *pfile)
2213{
2214 if (!pfile->deps)
2215 pfile->deps = deps_init ();
2216 return pfile->deps;
2217}
2218
Neil Bootheb1f4d92000-12-18 19:00:26 +00002219/* Push a new buffer on the buffer stack. Returns the new buffer; it
2220 doesn't fail. It does not generate a file change call back; that
2221 is the responsibility of the caller. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00002222cpp_buffer *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002223cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
Per Bothner40de9f72003-10-02 07:30:34 +00002224 int from_stage3)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002225{
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002226 cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
Neil Booth93c803682000-10-28 17:59:06 +00002227
Neil Boothfde84342001-08-06 21:07:41 +00002228 /* Clears, amongst other things, if_stack and mi_cmacro. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002229 memset (new_buffer, 0, sizeof (cpp_buffer));
Neil Boothad2a0842000-12-17 00:13:54 +00002230
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002231 new_buffer->next_line = new_buffer->buf = buffer;
2232 new_buffer->rlimit = buffer + len;
2233 new_buffer->from_stage3 = from_stage3;
2234 new_buffer->prev = pfile->buffer;
2235 new_buffer->need_line = true;
Neil Booth0bda4762000-12-11 07:45:16 +00002236
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002237 pfile->buffer = new_buffer;
Eric Christophercf551fb2004-01-16 22:37:49 +00002238
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002239 return new_buffer;
Zack Weinbergc71f8352000-07-05 05:33:57 +00002240}
2241
Neil Boothaf0d16c2002-04-22 17:48:02 +00002242/* Pops a single buffer, with a file change call-back if appropriate.
2243 Then pushes the next -include file, if any remain. */
Neil Boothef6e9582001-08-04 12:01:59 +00002244void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002245_cpp_pop_buffer (cpp_reader *pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002246{
Neil Boothfde84342001-08-06 21:07:41 +00002247 cpp_buffer *buffer = pfile->buffer;
Neil Booth8f9b4002003-07-29 22:26:13 +00002248 struct _cpp_file *inc = buffer->file;
Neil Boothad2a0842000-12-17 00:13:54 +00002249 struct if_stack *ifs;
Zack Weinbergc71f8352000-07-05 05:33:57 +00002250
Neil Boothfde84342001-08-06 21:07:41 +00002251 /* Walk back up the conditional stack till we reach its level at
2252 entry to this file, issuing error messages. */
2253 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
John David Anglin0527bc42003-11-01 22:56:54 +00002254 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Boothfde84342001-08-06 21:07:41 +00002255 "unterminated #%s", dtable[ifs->type].name);
2256
Neil Booth97293892001-09-14 22:04:46 +00002257 /* In case of a missing #endif. */
Neil Booth67821e32001-08-05 17:31:25 +00002258 pfile->state.skipping = 0;
Neil Booth29401c32001-08-22 20:37:20 +00002259
Neil Boothaf0d16c2002-04-22 17:48:02 +00002260 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
Neil Booth29401c32001-08-22 20:37:20 +00002261 pfile->buffer = buffer->prev;
2262
Neil Booth26aea072003-04-19 00:22:51 +00002263 free (buffer->notes);
2264
Neil Boothaf0d16c2002-04-22 17:48:02 +00002265 /* Free the buffer object now; we may want to push a new buffer
2266 in _cpp_push_next_include_file. */
2267 obstack_free (&pfile->buffer_ob, buffer);
Neil Booth29401c32001-08-22 20:37:20 +00002268
Neil Boothaf0d16c2002-04-22 17:48:02 +00002269 if (inc)
2270 {
2271 _cpp_pop_file_buffer (pfile, inc);
2272
Per Bothner40de9f72003-10-02 07:30:34 +00002273 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
Neil Boothaf0d16c2002-04-22 17:48:02 +00002274 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00002275}
2276
Kazu Hirata05713b82002-09-15 18:24:08 +00002277/* Enter all recognized directives in the hash table. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00002278void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002279_cpp_init_directives (cpp_reader *pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002280{
Neil Booth766ee682001-01-29 19:20:12 +00002281 unsigned int i;
Neil Booth93c803682000-10-28 17:59:06 +00002282 cpp_hashnode *node;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00002283
John David Anglin37b85242001-03-02 01:11:50 +00002284 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
Neil Booth93c803682000-10-28 17:59:06 +00002285 {
Neil Booth766ee682001-01-29 19:20:12 +00002286 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
Zack Weinberg4977bab2002-12-16 18:23:00 +00002287 node->is_directive = 1;
2288 node->directive_index = i;
Neil Booth93c803682000-10-28 17:59:06 +00002289 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00002290}