blob: d9cf9d27204384ebba3fe1020e6f4caa9cb512cb [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
Joseph Myersa1f300c2001-11-23 02:05:19 +0000427 puts a space in front of any '#' at the start of a macro. */
Neil Booth18a9d8f2001-09-16 11:23:56 +0000428 if (CPP_OPTION (pfile, preprocessed)
429 && (indented || !(dir->flags & IN_I)))
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000430 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000431 skip = 0;
432 dir = 0;
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000433 }
434 else
Neil Booth93c803682000-10-28 17:59:06 +0000435 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000436 /* In failed conditional groups, all non-conditional
437 directives are ignored. Before doing that, whether
438 skipping or not, we should lex angle-bracketed headers
439 correctly, and maybe output some diagnostics. */
440 pfile->state.angled_headers = dir->flags & INCL;
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000441 pfile->state.directive_wants_padding = dir->flags & INCL;
Neil Booth18a9d8f2001-09-16 11:23:56 +0000442 if (! CPP_OPTION (pfile, preprocessed))
443 directive_diagnostics (pfile, dir, indented);
444 if (pfile->state.skipping && !(dir->flags & COND))
445 dir = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000446 }
447 }
Neil Booth345894b2001-09-16 13:44:29 +0000448 else if (dname->type == CPP_EOF)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000449 ; /* CPP_EOF is the "null directive". */
450 else
Neil Booth0d9f2342000-09-18 18:43:05 +0000451 {
Neil Booth93c803682000-10-28 17:59:06 +0000452 /* An unknown directive. Don't complain about it in assembly
453 source: we don't know where the comments are, and # may
454 introduce assembler pseudo-ops. Don't complain about invalid
455 directives in skipped conditional groups (6.10 p4). */
Neil Boothbdb05a72000-11-26 17:31:13 +0000456 if (CPP_OPTION (pfile, lang) == CLK_ASM)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000457 skip = 0;
458 else if (!pfile->state.skipping)
John David Anglin0527bc42003-11-01 22:56:54 +0000459 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
Neil Booth345894b2001-09-16 13:44:29 +0000460 cpp_token_as_text (pfile, dname));
Neil Booth0d9f2342000-09-18 18:43:05 +0000461 }
462
Neil Boothd1a58682002-06-28 06:26:54 +0000463 pfile->directive = dir;
464 if (CPP_OPTION (pfile, traditional))
465 prepare_directive_trad (pfile);
466
Neil Booth18a9d8f2001-09-16 11:23:56 +0000467 if (dir)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000468 pfile->directive->handler (pfile);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000469 else if (skip == 0)
470 _cpp_backup_tokens (pfile, 1);
471
472 end_directive (pfile, skip);
Neil Boothe808ec92002-02-27 07:24:53 +0000473 if (was_parsing_args)
474 {
475 /* Restore state when within macro args. */
476 pfile->state.parsing_args = 2;
477 pfile->state.prevent_expansion = 1;
Neil Boothe808ec92002-02-27 07:24:53 +0000478 }
Zack Weinbergc6e83802004-06-05 20:58:06 +0000479 if (was_discarding_output)
480 pfile->state.prevent_expansion = 1;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000481 return skip;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000482}
483
Neil Booth93c803682000-10-28 17:59:06 +0000484/* Directive handler wrapper used by the command line option
Neil Booth26aea072003-04-19 00:22:51 +0000485 processor. BUF is \n terminated. */
Neil Booth93c803682000-10-28 17:59:06 +0000486static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000487run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000488{
Neil Booth562a5c22002-04-21 18:46:42 +0000489 cpp_push_buffer (pfile, (const uchar *) buf, count,
Per Bothner40de9f72003-10-02 07:30:34 +0000490 /* from_stage3 */ true);
Neil Booth0bda4762000-12-11 07:45:16 +0000491 start_directive (pfile);
Neil Booth26aea072003-04-19 00:22:51 +0000492
493 /* This is a short-term fix to prevent a leading '#' being
494 interpreted as a directive. */
495 _cpp_clean_line (pfile);
496
Neil Boothf71aebb2001-05-27 18:06:00 +0000497 pfile->directive = &dtable[dir_no];
Neil Booth1a769162002-06-11 05:36:17 +0000498 if (CPP_OPTION (pfile, traditional))
499 prepare_directive_trad (pfile);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000500 pfile->directive->handler (pfile);
Neil Booth0bda4762000-12-11 07:45:16 +0000501 end_directive (pfile, 1);
Neil Boothef6e9582001-08-04 12:01:59 +0000502 _cpp_pop_buffer (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000503}
Per Bothner7f2935c1995-03-16 13:59:07 -0800504
Neil Booth93c803682000-10-28 17:59:06 +0000505/* Checks for validity the macro name in #define, #undef, #ifdef and
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000506 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
507 processing a #define or #undefine directive, and false
508 otherwise. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000509static cpp_hashnode *
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000510lex_macro_node (cpp_reader *pfile, bool is_def_or_undef)
Zack Weinberg041c3192000-07-04 01:58:21 +0000511{
Neil Booth1a769162002-06-11 05:36:17 +0000512 const cpp_token *token = _cpp_lex_token (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000513
Zack Weinberg92936ec2000-07-19 20:18:08 +0000514 /* The token immediately after #define must be an identifier. That
Zack Weinbergb8363a22001-07-01 18:48:13 +0000515 identifier may not be "defined", per C99 6.10.8p4.
516 In C++, it may not be any of the "named operators" either,
517 per C++98 [lex.digraph], [lex.key].
518 Finally, the identifier may not have been poisoned. (In that case
Neil Booth1d63a282002-06-28 20:27:14 +0000519 the lexer has issued the error message for us.) */
Zack Weinbergb8363a22001-07-01 18:48:13 +0000520
Neil Booth1a769162002-06-11 05:36:17 +0000521 if (token->type == CPP_NAME)
522 {
523 cpp_hashnode *node = token->val.node;
524
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000525 if (is_def_or_undef && node == pfile->spec_nodes.n_defined)
John David Anglin0527bc42003-11-01 22:56:54 +0000526 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth1a769162002-06-11 05:36:17 +0000527 "\"defined\" cannot be used as a macro name");
528 else if (! (node->flags & NODE_POISONED))
529 return node;
530 }
531 else if (token->flags & NAMED_OP)
John David Anglin0527bc42003-11-01 22:56:54 +0000532 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcbc69f82002-06-05 20:27:12 +0000533 "\"%s\" cannot be used as a macro name as it is an operator in C++",
Neil Booth1a769162002-06-11 05:36:17 +0000534 NODE_NAME (token->val.node));
535 else if (token->type == CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000536 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
Neil Booth1a769162002-06-11 05:36:17 +0000537 pfile->directive->name);
538 else
John David Anglin0527bc42003-11-01 22:56:54 +0000539 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
Zack Weinbergb8363a22001-07-01 18:48:13 +0000540
Neil Boothcbc69f82002-06-05 20:27:12 +0000541 return NULL;
Per Bothner7f2935c1995-03-16 13:59:07 -0800542}
Per Bothner7f2935c1995-03-16 13:59:07 -0800543
Gabriel Dos Reisa2566ae2005-01-02 01:32:21 +0000544/* Process a #define directive. Most work is done in macro.c. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000545static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000546do_define (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800547{
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000548 cpp_hashnode *node = lex_macro_node (pfile, true);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000549
Neil Booth93c803682000-10-28 17:59:06 +0000550 if (node)
551 {
Neil Booth1d63a282002-06-28 20:27:14 +0000552 /* If we have been requested to expand comments into macros,
553 then re-enable saving of comments. */
554 pfile->state.save_comments =
555 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
556
Neil Booth93c803682000-10-28 17:59:06 +0000557 if (_cpp_create_definition (pfile, node))
558 if (pfile->cb.define)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000559 pfile->cb.define (pfile, pfile->directive_line, node);
Neil Booth93c803682000-10-28 17:59:06 +0000560 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800561}
562
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000563/* Handle #undef. Mark the identifier NT_VOID in the hash table. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000564static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000565do_undef (cpp_reader *pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000566{
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000567 cpp_hashnode *node = lex_macro_node (pfile, true);
Zack Weinberg041c3192000-07-04 01:58:21 +0000568
Neil Booth45f24922003-12-12 07:00:29 +0000569 if (node)
Zack Weinberg041c3192000-07-04 01:58:21 +0000570 {
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000571 if (pfile->cb.undef)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000572 pfile->cb.undef (pfile, pfile->directive_line, node);
Zack Weinberg041c3192000-07-04 01:58:21 +0000573
Neil Booth45f24922003-12-12 07:00:29 +0000574 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
575 identifier is not currently defined as a macro name. */
576 if (node->type == NT_MACRO)
577 {
578 if (node->flags & NODE_WARN)
579 cpp_error (pfile, CPP_DL_WARNING,
580 "undefining \"%s\"", NODE_NAME (node));
Zack Weinberg041c3192000-07-04 01:58:21 +0000581
Neil Booth45f24922003-12-12 07:00:29 +0000582 if (CPP_OPTION (pfile, warn_unused_macros))
583 _cpp_warn_if_unused_macro (pfile, node, NULL);
Neil Bootha69cbaa2002-07-23 22:57:49 +0000584
Neil Booth45f24922003-12-12 07:00:29 +0000585 _cpp_free_definition (node);
586 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000587 }
Neil Booth45f24922003-12-12 07:00:29 +0000588
Neil Booth93c803682000-10-28 17:59:06 +0000589 check_eol (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000590}
591
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000592/* Undefine a single macro/assertion/whatever. */
593
594static int
Zack Weinbergc6e83802004-06-05 20:58:06 +0000595undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000596 void *data_p ATTRIBUTE_UNUSED)
597{
Zack Weinbergc6e83802004-06-05 20:58:06 +0000598 /* Body of _cpp_free_definition inlined here for speed.
599 Macros and assertions no longer have anything to free. */
600 h->type = NT_VOID;
601 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED);
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000602 return 1;
603}
604
605/* Undefine all macros and assertions. */
606
607void
608cpp_undef_all (cpp_reader *pfile)
609{
610 cpp_forall_identifiers (pfile, undefine_macros, NULL);
611}
612
613
Neil Booth93c803682000-10-28 17:59:06 +0000614/* Helper routine used by parse_include. Reinterpret the current line
615 as an h-char-sequence (< ... >); we are looking at the first token
Neil Booth74eb4b32003-04-21 19:21:59 +0000616 after the <. Returns a malloced filename. */
617static char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000618glue_header_name (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800619{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000620 const cpp_token *token;
Neil Booth74eb4b32003-04-21 19:21:59 +0000621 char *buffer;
Neil Booth2450e0b2002-02-23 20:21:39 +0000622 size_t len, total_len = 0, capacity = 1024;
Per Bothner7f2935c1995-03-16 13:59:07 -0800623
Neil Booth93c803682000-10-28 17:59:06 +0000624 /* To avoid lexed tokens overwriting our glued name, we can only
625 allocate from the string pool once we've lexed everything. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000626 buffer = XNEWVEC (char, capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000627 for (;;)
Zack Weinberg168d3732000-03-14 06:34:11 +0000628 {
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000629 token = get_token_no_padding (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000630
Neil Booth74eb4b32003-04-21 19:21:59 +0000631 if (token->type == CPP_GREATER)
Neil Booth93c803682000-10-28 17:59:06 +0000632 break;
Neil Booth74eb4b32003-04-21 19:21:59 +0000633 if (token->type == CPP_EOF)
634 {
John David Anglin0527bc42003-11-01 22:56:54 +0000635 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
Neil Booth74eb4b32003-04-21 19:21:59 +0000636 break;
637 }
Neil Booth93c803682000-10-28 17:59:06 +0000638
Neil Booth59325652003-04-24 20:03:57 +0000639 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
Neil Booth2450e0b2002-02-23 20:21:39 +0000640 if (total_len + len > capacity)
Neil Booth93c803682000-10-28 17:59:06 +0000641 {
Neil Booth2450e0b2002-02-23 20:21:39 +0000642 capacity = (capacity + len) * 2;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000643 buffer = XRESIZEVEC (char, buffer, capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000644 }
645
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000646 if (token->flags & PREV_WHITE)
Neil Booth2450e0b2002-02-23 20:21:39 +0000647 buffer[total_len++] = ' ';
Neil Booth93c803682000-10-28 17:59:06 +0000648
Geoffrey Keating47e20492005-03-12 10:44:06 +0000649 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
650 true)
Neil Booth74eb4b32003-04-21 19:21:59 +0000651 - (uchar *) buffer);
Neil Booth93c803682000-10-28 17:59:06 +0000652 }
653
Neil Booth74eb4b32003-04-21 19:21:59 +0000654 buffer[total_len] = '\0';
655 return buffer;
Neil Booth93c803682000-10-28 17:59:06 +0000656}
657
Neil Booth74eb4b32003-04-21 19:21:59 +0000658/* Returns the file name of #include, #include_next, #import and
659 #pragma dependency. The string is malloced and the caller should
660 free it. Returns NULL on error. */
661static const char *
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000662parse_include (cpp_reader *pfile, int *pangle_brackets,
663 const cpp_token ***buf)
Neil Booth93c803682000-10-28 17:59:06 +0000664{
Neil Booth74eb4b32003-04-21 19:21:59 +0000665 char *fname;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000666 const cpp_token *header;
Neil Booth93c803682000-10-28 17:59:06 +0000667
Neil Booth93c803682000-10-28 17:59:06 +0000668 /* Allow macro expansion. */
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000669 header = get_token_no_padding (pfile);
Neil Booth74eb4b32003-04-21 19:21:59 +0000670 if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
Neil Booth93c803682000-10-28 17:59:06 +0000671 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000672 fname = XNEWVEC (char, header->val.str.len - 1);
Neil Booth6338b352003-04-23 22:44:06 +0000673 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
674 fname[header->val.str.len - 2] = '\0';
Neil Booth74eb4b32003-04-21 19:21:59 +0000675 *pangle_brackets = header->type == CPP_HEADER_NAME;
Zack Weinberg041c3192000-07-04 01:58:21 +0000676 }
Neil Booth74eb4b32003-04-21 19:21:59 +0000677 else if (header->type == CPP_LESS)
Zack Weinberg041c3192000-07-04 01:58:21 +0000678 {
Neil Booth74eb4b32003-04-21 19:21:59 +0000679 fname = glue_header_name (pfile);
680 *pangle_brackets = 1;
681 }
682 else
683 {
684 const unsigned char *dir;
685
686 if (pfile->directive == &dtable[T_PRAGMA])
687 dir = U"pragma dependency";
688 else
689 dir = pfile->directive->name;
John David Anglin0527bc42003-11-01 22:56:54 +0000690 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
Neil Booth74eb4b32003-04-21 19:21:59 +0000691 dir);
692
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000693 return NULL;
Richard Kennercfb3ee11997-04-13 14:30:13 -0400694 }
695
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000696 if (buf == NULL || CPP_OPTION (pfile, discard_comments))
697 check_eol (pfile);
698 else
699 {
700 /* If we are not discarding comments, then gather them while
701 doing the eol check. */
702 *buf = check_eol_return_comments (pfile);
703 }
704
Neil Booth74eb4b32003-04-21 19:21:59 +0000705 return fname;
Zack Weinberg168d3732000-03-14 06:34:11 +0000706}
707
Neil Boothba133c92001-03-15 07:57:13 +0000708/* Handle #include, #include_next and #import. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000709static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000710do_include_common (cpp_reader *pfile, enum include_type type)
Zack Weinberg168d3732000-03-14 06:34:11 +0000711{
Neil Booth74eb4b32003-04-21 19:21:59 +0000712 const char *fname;
713 int angle_brackets;
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000714 const cpp_token **buf = NULL;
Neil Booth74eb4b32003-04-21 19:21:59 +0000715
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000716 /* Re-enable saving of comments if requested, so that the include
717 callback can dump comments which follow #include. */
718 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
719
720 fname = parse_include (pfile, &angle_brackets, &buf);
Neil Booth74eb4b32003-04-21 19:21:59 +0000721 if (!fname)
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000722 {
723 if (buf)
724 XDELETEVEC (buf);
725 return;
726 }
Zack Weinberg168d3732000-03-14 06:34:11 +0000727
Nathanael Nerode28303822004-11-28 22:28:13 +0000728 if (!*fname)
729 {
730 cpp_error (pfile, CPP_DL_ERROR, "empty filename in #%s",
731 pfile->directive->name);
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000732 XDELETEVEC (fname);
733 if (buf)
734 XDELETEVEC (buf);
Nathanael Nerode28303822004-11-28 22:28:13 +0000735 return;
736 }
737
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000738 /* Prevent #include recursion. */
Per Bothner50f59cd2004-01-19 21:30:18 -0800739 if (pfile->line_table->depth >= CPP_STACK_MAX)
John David Anglin0527bc42003-11-01 22:56:54 +0000740 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
Neil Booth74eb4b32003-04-21 19:21:59 +0000741 else
Neil Booth09b82252001-07-29 22:27:20 +0000742 {
Neil Booth74eb4b32003-04-21 19:21:59 +0000743 /* Get out of macro context, if we are. */
744 skip_rest_of_line (pfile);
745
746 if (pfile->cb.include)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000747 pfile->cb.include (pfile, pfile->directive_line,
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000748 pfile->directive->name, fname, angle_brackets,
749 buf);
Neil Booth74eb4b32003-04-21 19:21:59 +0000750
Neil Booth8f9b4002003-07-29 22:26:13 +0000751 _cpp_stack_include (pfile, fname, angle_brackets, type);
Neil Booth09b82252001-07-29 22:27:20 +0000752 }
753
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000754 XDELETEVEC (fname);
755 if (buf)
756 XDELETEVEC (buf);
Neil Boothba133c92001-03-15 07:57:13 +0000757}
758
759static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000760do_include (cpp_reader *pfile)
Neil Boothba133c92001-03-15 07:57:13 +0000761{
762 do_include_common (pfile, IT_INCLUDE);
Zack Weinberg168d3732000-03-14 06:34:11 +0000763}
764
Zack Weinberg711b8822000-07-18 00:59:49 +0000765static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000766do_import (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +0000767{
Neil Boothba133c92001-03-15 07:57:13 +0000768 do_include_common (pfile, IT_IMPORT);
Zack Weinberg168d3732000-03-14 06:34:11 +0000769}
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000770
Zack Weinberg711b8822000-07-18 00:59:49 +0000771static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000772do_include_next (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +0000773{
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000774 enum include_type type = IT_INCLUDE_NEXT;
775
776 /* If this is the primary source file, warn and use the normal
777 search logic. */
Tom Tromey705e2d22007-01-04 15:32:26 +0000778 if (cpp_in_primary_file (pfile))
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000779 {
John David Anglin0527bc42003-11-01 22:56:54 +0000780 cpp_error (pfile, CPP_DL_WARNING,
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000781 "#include_next in primary source file");
782 type = IT_INCLUDE;
783 }
784 do_include_common (pfile, type);
Per Bothner7f2935c1995-03-16 13:59:07 -0800785}
786
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000787/* Subroutine of do_linemarker. Read possible flags after file name.
788 LAST is the last flag seen; 0 if this is the first flag. Return the
789 flag if it is valid, 0 at the end of the directive. Otherwise
790 complain. */
Neil Booth642ce432000-12-07 23:17:56 +0000791static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000792read_flag (cpp_reader *pfile, unsigned int last)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000793{
Neil Booth345894b2001-09-16 13:44:29 +0000794 const cpp_token *token = _cpp_lex_token (pfile);
Jason Merrilld3a34a01999-08-14 00:42:07 +0000795
Neil Booth345894b2001-09-16 13:44:29 +0000796 if (token->type == CPP_NUMBER && token->val.str.len == 1)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000797 {
Neil Booth345894b2001-09-16 13:44:29 +0000798 unsigned int flag = token->val.str.text[0] - '0';
Neil Booth28e0f042000-12-09 12:06:37 +0000799
800 if (flag > last && flag <= 4
801 && (flag != 4 || last == 3)
802 && (flag != 2 || last == 0))
803 return flag;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000804 }
Neil Booth93c803682000-10-28 17:59:06 +0000805
Neil Booth345894b2001-09-16 13:44:29 +0000806 if (token->type != CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000807 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
Neil Booth345894b2001-09-16 13:44:29 +0000808 cpp_token_as_text (pfile, token));
Neil Booth93c803682000-10-28 17:59:06 +0000809 return 0;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000810}
811
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000812/* Subroutine of do_line and do_linemarker. Convert a number in STR,
813 of length LEN, to binary; store it in NUMP, and return 0 if the
814 number was well-formed, 1 if not. Temporary, hopefully. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000815static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000816strtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump)
Zack Weinberg041c3192000-07-04 01:58:21 +0000817{
818 unsigned long reg = 0;
Neil Booth562a5c22002-04-21 18:46:42 +0000819 uchar c;
Zack Weinberg041c3192000-07-04 01:58:21 +0000820 while (len--)
821 {
822 c = *str++;
823 if (!ISDIGIT (c))
824 return 1;
825 reg *= 10;
826 reg += c - '0';
827 }
828 *nump = reg;
829 return 0;
830}
831
Zack Weinberg5538ada1999-02-04 06:36:54 -0500832/* Interpret #line command.
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000833 Note that the filename string (if any) is a true string constant
834 (escapes are interpreted), unlike in #line. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000835static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000836do_line (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800837{
Per Bothner500bee02004-04-22 19:22:27 -0700838 const struct line_maps *line_table = pfile->line_table;
839 const struct line_map *map = &line_table->maps[line_table->used - 1];
Devang Patel2203a882005-02-28 11:04:19 -0800840
841 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
842 sysp right now. */
843
844 unsigned char map_sysp = map->sysp;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000845 const cpp_token *token;
Per Bothner12f9df42004-02-11 07:29:30 -0800846 const char *new_file = map->to_file;
Neil Boothbb74c962001-08-17 22:23:49 +0000847 unsigned long new_lineno;
Per Bothner7f2935c1995-03-16 13:59:07 -0800848
Neil Booth27e25642000-11-27 08:00:04 +0000849 /* C99 raised the minimum limit on #line numbers. */
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000850 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
Neil Booth18a9d8f2001-09-16 11:23:56 +0000851
Neil Booth93c803682000-10-28 17:59:06 +0000852 /* #line commands expand macros. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000853 token = cpp_get_token (pfile);
854 if (token->type != CPP_NUMBER
855 || strtoul_for_line (token->val.str.text, token->val.str.len,
856 &new_lineno))
Per Bothner7f2935c1995-03-16 13:59:07 -0800857 {
John David Anglin0527bc42003-11-01 22:56:54 +0000858 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +0000859 "\"%s\" after #line is not a positive integer",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000860 cpp_token_as_text (pfile, token));
Zack Weinberg9ec72912000-08-09 19:41:12 +0000861 return;
Kazu Hiratadf383482002-05-22 22:02:16 +0000862 }
Zack Weinberg5538ada1999-02-04 06:36:54 -0500863
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000864 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
John David Anglin0527bc42003-11-01 22:56:54 +0000865 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
Zack Weinberg5538ada1999-02-04 06:36:54 -0500866
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000867 token = cpp_get_token (pfile);
868 if (token->type == CPP_STRING)
Zack Weinberg5538ada1999-02-04 06:36:54 -0500869 {
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000870 cpp_string s = { 0, 0 };
Eric Christopher423e95e2004-02-12 02:25:03 +0000871 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
872 &s, false))
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000873 new_file = (const char *)s.text;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000874 check_eol (pfile);
875 }
876 else if (token->type != CPP_EOF)
877 {
John David Anglin0527bc42003-11-01 22:56:54 +0000878 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000879 cpp_token_as_text (pfile, token));
880 return;
881 }
Neil Booth93c803682000-10-28 17:59:06 +0000882
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000883 skip_rest_of_line (pfile);
884 _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
Devang Patel2203a882005-02-28 11:04:19 -0800885 map_sysp);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000886}
887
888/* Interpret the # 44 "file" [flags] notation, which has slightly
889 different syntax and semantics from #line: Flags are allowed,
890 and we never complain about the line number being too big. */
891static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000892do_linemarker (cpp_reader *pfile)
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000893{
Per Bothner500bee02004-04-22 19:22:27 -0700894 const struct line_maps *line_table = pfile->line_table;
895 const struct line_map *map = &line_table->maps[line_table->used - 1];
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000896 const cpp_token *token;
Per Bothner12f9df42004-02-11 07:29:30 -0800897 const char *new_file = map->to_file;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000898 unsigned long new_lineno;
Per Bothner12f9df42004-02-11 07:29:30 -0800899 unsigned int new_sysp = map->sysp;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000900 enum lc_reason reason = LC_RENAME;
901 int flag;
902
903 /* Back up so we can get the number again. Putting this in
904 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
905 some circumstances, which can segfault. */
906 _cpp_backup_tokens (pfile, 1);
907
908 /* #line commands expand macros. */
909 token = cpp_get_token (pfile);
910 if (token->type != CPP_NUMBER
911 || strtoul_for_line (token->val.str.text, token->val.str.len,
912 &new_lineno))
913 {
John David Anglin0527bc42003-11-01 22:56:54 +0000914 cpp_error (pfile, CPP_DL_ERROR,
915 "\"%s\" after # is not a positive integer",
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000916 cpp_token_as_text (pfile, token));
917 return;
Kazu Hiratadf383482002-05-22 22:02:16 +0000918 }
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000919
920 token = cpp_get_token (pfile);
921 if (token->type == CPP_STRING)
922 {
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000923 cpp_string s = { 0, 0 };
Eric Christopher423e95e2004-02-12 02:25:03 +0000924 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
925 1, &s, false))
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000926 new_file = (const char *)s.text;
Eric Christophercf551fb2004-01-16 22:37:49 +0000927
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000928 new_sysp = 0;
929 flag = read_flag (pfile, 0);
930 if (flag == 1)
Neil Booth93c803682000-10-28 17:59:06 +0000931 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000932 reason = LC_ENTER;
933 /* Fake an include for cpp_included (). */
934 _cpp_fake_include (pfile, new_file);
935 flag = read_flag (pfile, flag);
Neil Booth93c803682000-10-28 17:59:06 +0000936 }
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000937 else if (flag == 2)
938 {
939 reason = LC_LEAVE;
940 flag = read_flag (pfile, flag);
941 }
942 if (flag == 3)
943 {
944 new_sysp = 1;
945 flag = read_flag (pfile, flag);
946 if (flag == 4)
947 new_sysp = 2;
948 }
Jakub Jelinek9d30f272006-12-29 09:15:08 +0100949 pfile->buffer->sysp = new_sysp;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000950
Neil Boothfde84342001-08-06 21:07:41 +0000951 check_eol (pfile);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000952 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000953 else if (token->type != CPP_EOF)
Neil Booth27e25642000-11-27 08:00:04 +0000954 {
John David Anglin0527bc42003-11-01 22:56:54 +0000955 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000956 cpp_token_as_text (pfile, token));
Neil Booth27e25642000-11-27 08:00:04 +0000957 return;
958 }
Zack Weinberg941e09b1998-12-15 11:17:06 +0000959
Neil Boothbdcbe492001-09-13 20:05:17 +0000960 skip_rest_of_line (pfile);
Neil Boothbb74c962001-08-17 22:23:49 +0000961 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
Neil Booth27e25642000-11-27 08:00:04 +0000962}
963
Neil Booth67821e32001-08-05 17:31:25 +0000964/* Arrange the file_change callback. pfile->line has changed to
Neil Booth47d89cf2001-08-11 07:33:39 +0000965 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
Joseph Myersa1f300c2001-11-23 02:05:19 +0000966 header, 2 for a system header that needs to be extern "C" protected,
Neil Booth47d89cf2001-08-11 07:33:39 +0000967 and zero otherwise. */
Neil Bootheb1f4d92000-12-18 19:00:26 +0000968void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000969_cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
970 const char *to_file, unsigned int file_line,
971 unsigned int sysp)
Neil Booth27e25642000-11-27 08:00:04 +0000972{
Per Bothner12f9df42004-02-11 07:29:30 -0800973 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
974 to_file, file_line);
Per Bothner500bee02004-04-22 19:22:27 -0700975 if (map != NULL)
976 linemap_line_start (pfile->line_table, map->to_line, 127);
Neil Boothd82fc102001-08-02 23:03:31 +0000977
Neil Bootheb1f4d92000-12-18 19:00:26 +0000978 if (pfile->cb.file_change)
Per Bothner12f9df42004-02-11 07:29:30 -0800979 pfile->cb.file_change (pfile, map);
Per Bothner7f2935c1995-03-16 13:59:07 -0800980}
Zack Weinberg941e09b1998-12-15 11:17:06 +0000981
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000982/* Report a warning or error detected by the program we are
983 processing. Use the directive's tokens in the error message. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000984static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000985do_diagnostic (cpp_reader *pfile, int code, int print_dir)
Per Bothner7f2935c1995-03-16 13:59:07 -0800986{
Per Bothner12f9df42004-02-11 07:29:30 -0800987 if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0))
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000988 {
Neil Booth29b10742000-11-13 18:40:37 +0000989 if (print_dir)
990 fprintf (stderr, "#%s ", pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +0000991 pfile->state.prevent_expansion++;
992 cpp_output_line (pfile, stderr);
993 pfile->state.prevent_expansion--;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000994 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800995}
996
Neil Booth838f3132000-09-24 10:42:09 +0000997static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000998do_error (cpp_reader *pfile)
Neil Booth838f3132000-09-24 10:42:09 +0000999{
John David Anglin0527bc42003-11-01 22:56:54 +00001000 do_diagnostic (pfile, CPP_DL_ERROR, 1);
Neil Booth838f3132000-09-24 10:42:09 +00001001}
Per Bothner7f2935c1995-03-16 13:59:07 -08001002
Zack Weinberg711b8822000-07-18 00:59:49 +00001003static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001004do_warning (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001005{
Neil Booth2f878972001-04-08 10:01:18 +00001006 /* We want #warning diagnostics to be emitted in system headers too. */
John David Anglin0527bc42003-11-01 22:56:54 +00001007 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
Per Bothner7f2935c1995-03-16 13:59:07 -08001008}
1009
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001010/* Report program identification. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001011static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001012do_ident (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001013{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001014 const cpp_token *str = cpp_get_token (pfile);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001015
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001016 if (str->type != CPP_STRING)
Zack Weinberg1ed17cd2005-05-12 18:31:38 +00001017 cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
1018 pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +00001019 else if (pfile->cb.ident)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001020 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001021
Neil Booth93c803682000-10-28 17:59:06 +00001022 check_eol (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08001023}
1024
Neil Bootha5da89c2001-10-14 17:44:00 +00001025/* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1026 matching entry, or NULL if none is found. The returned entry could
1027 be the start of a namespace chain, or a pragma. */
1028static struct pragma_entry *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001029lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
Nathan Sidwell82443372000-06-23 10:56:09 +00001030{
Neil Booth4b115ff2001-10-14 23:04:13 +00001031 while (chain && chain->pragma != pragma)
1032 chain = chain->next;
Neil Bootha5da89c2001-10-14 17:44:00 +00001033
1034 return chain;
1035}
1036
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001037/* Create and insert a blank pragma entry at the beginning of a
1038 singly-linked CHAIN. */
Neil Bootha5da89c2001-10-14 17:44:00 +00001039static struct pragma_entry *
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001040new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain)
Neil Bootha5da89c2001-10-14 17:44:00 +00001041{
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001042 struct pragma_entry *new_entry;
Neil Bootha5da89c2001-10-14 17:44:00 +00001043
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001044 new_entry = (struct pragma_entry *)
Neil Bootha5da89c2001-10-14 17:44:00 +00001045 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
Neil Bootha5da89c2001-10-14 17:44:00 +00001046
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001047 memset (new_entry, 0, sizeof (struct pragma_entry));
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001048 new_entry->next = *chain;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001049
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001050 *chain = new_entry;
1051 return new_entry;
Neil Bootha5da89c2001-10-14 17:44:00 +00001052}
1053
1054/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001055 goes in the global namespace. */
1056static struct pragma_entry *
1057register_pragma_1 (cpp_reader *pfile, const char *space, const char *name,
1058 bool allow_name_expansion)
Nathan Sidwell82443372000-06-23 10:56:09 +00001059{
Neil Bootha5da89c2001-10-14 17:44:00 +00001060 struct pragma_entry **chain = &pfile->pragmas;
1061 struct pragma_entry *entry;
Neil Booth4b115ff2001-10-14 23:04:13 +00001062 const cpp_hashnode *node;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001063
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001064 if (space)
1065 {
Neil Booth4b115ff2001-10-14 23:04:13 +00001066 node = cpp_lookup (pfile, U space, strlen (space));
1067 entry = lookup_pragma_entry (*chain, node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001068 if (!entry)
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001069 {
1070 entry = new_pragma_entry (pfile, chain);
1071 entry->pragma = node;
1072 entry->is_nspace = true;
1073 entry->allow_expansion = allow_name_expansion;
1074 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001075 else if (!entry->is_nspace)
1076 goto clash;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001077 else if (entry->allow_expansion != allow_name_expansion)
1078 {
1079 cpp_error (pfile, CPP_DL_ICE,
1080 "registering pragmas in namespace \"%s\" with mismatched "
1081 "name expansion", space);
1082 return NULL;
1083 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001084 chain = &entry->u.space;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001085 }
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001086 else if (allow_name_expansion)
1087 {
1088 cpp_error (pfile, CPP_DL_ICE,
1089 "registering pragma \"%s\" with name expansion "
1090 "and no namespace", name);
1091 return NULL;
1092 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001093
Neil Bootha5da89c2001-10-14 17:44:00 +00001094 /* Check for duplicates. */
Neil Booth4b115ff2001-10-14 23:04:13 +00001095 node = cpp_lookup (pfile, U name, strlen (name));
1096 entry = lookup_pragma_entry (*chain, node);
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001097 if (entry == NULL)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001098 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001099 entry = new_pragma_entry (pfile, chain);
1100 entry->pragma = node;
1101 return entry;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001102 }
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001103
1104 if (entry->is_nspace)
1105 clash:
1106 cpp_error (pfile, CPP_DL_ICE,
1107 "registering \"%s\" as both a pragma and a pragma namespace",
1108 NODE_NAME (node));
1109 else if (space)
1110 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1111 space, name);
Neil Bootha5da89c2001-10-14 17:44:00 +00001112 else
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001113 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1114
1115 return NULL;
1116}
1117
1118/* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1119static void
1120register_pragma_internal (cpp_reader *pfile, const char *space,
1121 const char *name, pragma_cb handler)
1122{
1123 struct pragma_entry *entry;
1124
1125 entry = register_pragma_1 (pfile, space, name, false);
1126 entry->is_internal = true;
1127 entry->u.handler = handler;
Zack Weinberg21b11492004-09-09 19:16:56 +00001128}
1129
1130/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1131 goes in the global namespace. HANDLER is the handler it will call,
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001132 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1133 expansion while parsing pragma NAME. This function is exported
1134 from libcpp. */
Zack Weinberg21b11492004-09-09 19:16:56 +00001135void
1136cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001137 pragma_cb handler, bool allow_expansion)
Zack Weinberg21b11492004-09-09 19:16:56 +00001138{
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001139 struct pragma_entry *entry;
1140
1141 if (!handler)
1142 {
1143 cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler");
1144 return;
1145 }
1146
1147 entry = register_pragma_1 (pfile, space, name, false);
1148 if (entry)
1149 {
1150 entry->allow_expansion = allow_expansion;
1151 entry->u.handler = handler;
1152 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001153}
Neil Bootha5da89c2001-10-14 17:44:00 +00001154
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001155/* Similarly, but create mark the pragma for deferred processing.
1156 When found, a CPP_PRAGMA token will be insertted into the stream
1157 with IDENT in the token->u.pragma slot. */
1158void
1159cpp_register_deferred_pragma (cpp_reader *pfile, const char *space,
1160 const char *name, unsigned int ident,
1161 bool allow_expansion, bool allow_name_expansion)
1162{
1163 struct pragma_entry *entry;
1164
1165 entry = register_pragma_1 (pfile, space, name, allow_name_expansion);
1166 if (entry)
1167 {
1168 entry->is_deferred = true;
1169 entry->allow_expansion = allow_expansion;
1170 entry->u.ident = ident;
1171 }
1172}
1173
Neil Bootha5da89c2001-10-14 17:44:00 +00001174/* Register the pragmas the preprocessor itself handles. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001175void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001176_cpp_init_internal_pragmas (cpp_reader *pfile)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001177{
Neil Bootha5da89c2001-10-14 17:44:00 +00001178 /* Pragmas in the global namespace. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001179 register_pragma_internal (pfile, 0, "once", do_pragma_once);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001180
Neil Bootha5da89c2001-10-14 17:44:00 +00001181 /* New GCC-specific pragmas should be put in the GCC namespace. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001182 register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison);
1183 register_pragma_internal (pfile, "GCC", "system_header",
1184 do_pragma_system_header);
1185 register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
Nathan Sidwell82443372000-06-23 10:56:09 +00001186}
Per Bothner7f2935c1995-03-16 13:59:07 -08001187
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001188/* Return the number of registered pragmas in PE. */
1189
1190static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001191count_registered_pragmas (struct pragma_entry *pe)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001192{
1193 int ct = 0;
1194 for (; pe != NULL; pe = pe->next)
1195 {
1196 if (pe->is_nspace)
1197 ct += count_registered_pragmas (pe->u.space);
1198 ct++;
1199 }
1200 return ct;
1201}
1202
1203/* Save into SD the names of the registered pragmas referenced by PE,
1204 and return a pointer to the next free space in SD. */
1205
1206static char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001207save_registered_pragmas (struct pragma_entry *pe, char **sd)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001208{
1209 for (; pe != NULL; pe = pe->next)
1210 {
1211 if (pe->is_nspace)
1212 sd = save_registered_pragmas (pe->u.space, sd);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001213 *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
1214 HT_LEN (&pe->pragma->ident),
1215 HT_LEN (&pe->pragma->ident) + 1);
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001216 }
1217 return sd;
1218}
1219
1220/* Return a newly-allocated array which saves the names of the
1221 registered pragmas. */
1222
1223char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001224_cpp_save_pragma_names (cpp_reader *pfile)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001225{
1226 int ct = count_registered_pragmas (pfile->pragmas);
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001227 char **result = XNEWVEC (char *, ct);
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001228 (void) save_registered_pragmas (pfile->pragmas, result);
1229 return result;
1230}
1231
1232/* Restore from SD the names of the registered pragmas referenced by PE,
1233 and return a pointer to the next unused name in SD. */
1234
1235static char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001236restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1237 char **sd)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001238{
1239 for (; pe != NULL; pe = pe->next)
1240 {
1241 if (pe->is_nspace)
1242 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1243 pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd));
1244 free (*sd);
1245 sd++;
1246 }
1247 return sd;
1248}
1249
1250/* Restore the names of the registered pragmas from SAVED. */
1251
1252void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001253_cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001254{
1255 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1256 free (saved);
1257}
1258
Neil Bootha5da89c2001-10-14 17:44:00 +00001259/* Pragmata handling. We handle some, and pass the rest on to the
1260 front end. C99 defines three pragmas and says that no macro
1261 expansion is to be performed on them; whether or not macro
1262 expansion happens for other pragmas is implementation defined.
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001263 This implementation allows for a mix of both, since GCC did not
1264 traditionally macro expand its (few) pragmas, whereas OpenMP
1265 specifies that macro expansion should happen. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001266static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001267do_pragma (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001268{
Neil Bootha5da89c2001-10-14 17:44:00 +00001269 const struct pragma_entry *p = NULL;
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001270 const cpp_token *token, *pragma_token = pfile->cur_token;
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001271 cpp_token ns_token;
Neil Bootha5da89c2001-10-14 17:44:00 +00001272 unsigned int count = 1;
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001273
Neil Booth93c803682000-10-28 17:59:06 +00001274 pfile->state.prevent_expansion++;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001275
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001276 token = cpp_get_token (pfile);
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001277 ns_token = *token;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001278 if (token->type == CPP_NAME)
Alexandre Oliva0172e2b2000-02-27 06:24:27 +00001279 {
Neil Booth4b115ff2001-10-14 23:04:13 +00001280 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001281 if (p && p->is_nspace)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001282 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001283 bool allow_name_expansion = p->allow_expansion;
1284 if (allow_name_expansion)
1285 pfile->state.prevent_expansion--;
Neil Bootha5da89c2001-10-14 17:44:00 +00001286 token = cpp_get_token (pfile);
1287 if (token->type == CPP_NAME)
Neil Booth4b115ff2001-10-14 23:04:13 +00001288 p = lookup_pragma_entry (p->u.space, token->val.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001289 else
1290 p = NULL;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001291 if (allow_name_expansion)
1292 pfile->state.prevent_expansion++;
1293 count = 2;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001294 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001295 }
1296
Zack Weinberg3da3d582004-10-27 17:29:29 +00001297 if (p)
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001298 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001299 if (p->is_deferred)
Zack Weinberg3da3d582004-10-27 17:29:29 +00001300 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001301 pfile->directive_result.src_loc = pragma_token->src_loc;
1302 pfile->directive_result.type = CPP_PRAGMA;
1303 pfile->directive_result.flags = pragma_token->flags;
1304 pfile->directive_result.val.pragma = p->u.ident;
1305 pfile->state.in_deferred_pragma = true;
1306 pfile->state.pragma_allow_expansion = p->allow_expansion;
1307 if (!p->allow_expansion)
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001308 pfile->state.prevent_expansion++;
Zack Weinberg3da3d582004-10-27 17:29:29 +00001309 }
1310 else
1311 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001312 /* Since the handler below doesn't get the line number, that
1313 it might need for diagnostics, make sure it has the right
1314 numbers in place. */
1315 if (pfile->cb.line_change)
1316 (*pfile->cb.line_change) (pfile, pragma_token, false);
1317 if (p->allow_expansion)
1318 pfile->state.prevent_expansion--;
1319 (*p->u.handler) (pfile);
1320 if (p->allow_expansion)
1321 pfile->state.prevent_expansion++;
Zack Weinberg3da3d582004-10-27 17:29:29 +00001322 }
Zack Weinberg21b11492004-09-09 19:16:56 +00001323 }
Neil Boothd82fc102001-08-02 23:03:31 +00001324 else if (pfile->cb.def_pragma)
Neil Boothbdcbe492001-09-13 20:05:17 +00001325 {
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001326 if (count == 1 || pfile->context->prev == NULL)
1327 _cpp_backup_tokens (pfile, count);
1328 else
1329 {
1330 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1331 won't allow backing 2 tokens. */
1332 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1333 reads both tokens, we could perhaps free it, but if it doesn't,
1334 we don't know the exact lifespan. */
1335 cpp_token *toks = XNEWVEC (cpp_token, 2);
1336 toks[0] = ns_token;
1337 toks[0].flags |= NO_EXPAND;
1338 toks[1] = *token;
1339 toks[1].flags |= NO_EXPAND;
1340 _cpp_push_token_context (pfile, NULL, toks, 2);
1341 }
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001342 pfile->cb.def_pragma (pfile, pfile->directive_line);
Neil Boothbdcbe492001-09-13 20:05:17 +00001343 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001344
Neil Booth97293892001-09-14 22:04:46 +00001345 pfile->state.prevent_expansion--;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001346}
1347
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001348/* Handle #pragma once. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001349static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001350do_pragma_once (cpp_reader *pfile)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001351{
Tom Tromey705e2d22007-01-04 15:32:26 +00001352 if (cpp_in_primary_file (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +00001353 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
Neil Booth93c803682000-10-28 17:59:06 +00001354
1355 check_eol (pfile);
Neil Booth49634b32003-08-02 16:29:46 +00001356 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001357}
1358
Neil Boothc3bf3e62002-05-09 17:14:22 +00001359/* Handle #pragma GCC poison, to poison one or more identifiers so
1360 that the lexer produces a hard error for each subsequent usage. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001361static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001362do_pragma_poison (cpp_reader *pfile)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001363{
Neil Booth345894b2001-09-16 13:44:29 +00001364 const cpp_token *tok;
Zack Weinbergf8f769e2000-05-28 05:56:38 +00001365 cpp_hashnode *hp;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001366
Neil Booth93c803682000-10-28 17:59:06 +00001367 pfile->state.poisoned_ok = 1;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001368 for (;;)
1369 {
Neil Booth345894b2001-09-16 13:44:29 +00001370 tok = _cpp_lex_token (pfile);
1371 if (tok->type == CPP_EOF)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001372 break;
Neil Booth345894b2001-09-16 13:44:29 +00001373 if (tok->type != CPP_NAME)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001374 {
John David Anglin0527bc42003-11-01 22:56:54 +00001375 cpp_error (pfile, CPP_DL_ERROR,
1376 "invalid #pragma GCC poison directive");
Neil Booth93c803682000-10-28 17:59:06 +00001377 break;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001378 }
1379
Neil Booth345894b2001-09-16 13:44:29 +00001380 hp = tok->val.node;
Neil Booth93c803682000-10-28 17:59:06 +00001381 if (hp->flags & NODE_POISONED)
1382 continue;
1383
1384 if (hp->type == NT_MACRO)
John David Anglin0527bc42003-11-01 22:56:54 +00001385 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
Neil Boothebef4e82002-04-14 18:42:47 +00001386 NODE_NAME (hp));
Neil Booth93c803682000-10-28 17:59:06 +00001387 _cpp_free_definition (hp);
1388 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001389 }
Neil Booth93c803682000-10-28 17:59:06 +00001390 pfile->state.poisoned_ok = 0;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001391}
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001392
1393/* Mark the current header as a system header. This will suppress
1394 some categories of warnings (notably those from -pedantic). It is
1395 intended for use in system libraries that cannot be implemented in
1396 conforming C, but cannot be certain that their headers appear in a
1397 system include directory. To prevent abuse, it is rejected in the
1398 primary source file. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001399static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001400do_pragma_system_header (cpp_reader *pfile)
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001401{
Tom Tromey705e2d22007-01-04 15:32:26 +00001402 if (cpp_in_primary_file (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +00001403 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +00001404 "#pragma system_header ignored outside include file");
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001405 else
Neil Boothd82fc102001-08-02 23:03:31 +00001406 {
1407 check_eol (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +00001408 skip_rest_of_line (pfile);
Neil Boothd82fc102001-08-02 23:03:31 +00001409 cpp_make_system_header (pfile, 1, 0);
1410 }
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001411}
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001412
1413/* Check the modified date of the current include file against a specified
1414 file. Issue a diagnostic, if the specified file is newer. We use this to
1415 determine if a fixed header should be refixed. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001416static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001417do_pragma_dependency (cpp_reader *pfile)
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001418{
Neil Booth74eb4b32003-04-21 19:21:59 +00001419 const char *fname;
1420 int angle_brackets, ordering;
Kazu Hiratadf383482002-05-22 22:02:16 +00001421
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +00001422 fname = parse_include (pfile, &angle_brackets, NULL);
Neil Booth74eb4b32003-04-21 19:21:59 +00001423 if (!fname)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001424 return;
Zack Weinberg041c3192000-07-04 01:58:21 +00001425
Neil Booth74eb4b32003-04-21 19:21:59 +00001426 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001427 if (ordering < 0)
John David Anglin0527bc42003-11-01 22:56:54 +00001428 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001429 else if (ordering > 0)
1430 {
John David Anglin0527bc42003-11-01 22:56:54 +00001431 cpp_error (pfile, CPP_DL_WARNING,
1432 "current file is older than %s", fname);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001433 if (cpp_get_token (pfile)->type != CPP_EOF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001434 {
1435 _cpp_backup_tokens (pfile, 1);
John David Anglin0527bc42003-11-01 22:56:54 +00001436 do_diagnostic (pfile, CPP_DL_WARNING, 0);
Neil Boothbdcbe492001-09-13 20:05:17 +00001437 }
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001438 }
Neil Booth74eb4b32003-04-21 19:21:59 +00001439
Kaveh R. Ghazifad205f2003-06-16 21:41:10 +00001440 free ((void *) fname);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001441}
1442
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001443/* Get a token but skip padding. */
1444static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001445get_token_no_padding (cpp_reader *pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001446{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001447 for (;;)
1448 {
1449 const cpp_token *result = cpp_get_token (pfile);
1450 if (result->type != CPP_PADDING)
1451 return result;
1452 }
1453}
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001454
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001455/* Check syntax is "(string-literal)". Returns the string on success,
1456 or NULL on failure. */
1457static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001458get__Pragma_string (cpp_reader *pfile)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001459{
1460 const cpp_token *string;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001461
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001462 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1463 return NULL;
1464
1465 string = get_token_no_padding (pfile);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001466 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001467 return NULL;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001468
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001469 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1470 return NULL;
1471
1472 return string;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001473}
1474
Neil Booth87062812001-10-20 09:00:53 +00001475/* Destringize IN into a temporary buffer, by removing the first \ of
1476 \" and \\ sequences, and process the result as a #pragma directive. */
1477static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001478destringize_and_run (cpp_reader *pfile, const cpp_string *in)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001479{
1480 const unsigned char *src, *limit;
Neil Booth87062812001-10-20 09:00:53 +00001481 char *dest, *result;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001482 cpp_context *saved_context;
1483 cpp_token *saved_cur_token;
1484 tokenrun *saved_cur_run;
1485 cpp_token *toks;
1486 int count;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001487
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001488 dest = result = (char *) alloca (in->len - 1);
Neil Booth6338b352003-04-23 22:44:06 +00001489 src = in->text + 1 + (in->text[0] == 'L');
1490 limit = in->text + in->len - 1;
1491 while (src < limit)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001492 {
1493 /* We know there is a character following the backslash. */
1494 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1495 src++;
1496 *dest++ = *src++;
1497 }
Neil Booth26aea072003-04-19 00:22:51 +00001498 *dest = '\n';
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001499
Neil Booth8128ccc2002-11-18 20:43:40 +00001500 /* Ugh; an awful kludge. We are really not set up to be lexing
1501 tokens when in the middle of a macro expansion. Use a new
1502 context to force cpp_get_token to lex, and so skip_rest_of_line
1503 doesn't go beyond the end of the text. Also, remember the
1504 current lexing position so we can return to it later.
Neil Booth79ba5e32002-09-03 21:55:40 +00001505
Neil Booth8128ccc2002-11-18 20:43:40 +00001506 Something like line-at-a-time lexing should remove the need for
1507 this. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001508 saved_context = pfile->context;
1509 saved_cur_token = pfile->cur_token;
1510 saved_cur_run = pfile->cur_run;
Neil Booth8128ccc2002-11-18 20:43:40 +00001511
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001512 pfile->context = XNEW (cpp_context);
1513 pfile->context->macro = 0;
1514 pfile->context->prev = 0;
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001515 pfile->context->next = 0;
Neil Booth79ba5e32002-09-03 21:55:40 +00001516
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001517 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1518 until we've read all of the tokens that we want. */
1519 cpp_push_buffer (pfile, (const uchar *) result, dest - result,
1520 /* from_stage3 */ true);
1521 /* ??? Antique Disgusting Hack. What does this do? */
1522 if (pfile->buffer->prev)
1523 pfile->buffer->file = pfile->buffer->prev->file;
Neil Booth79ba5e32002-09-03 21:55:40 +00001524
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001525 start_directive (pfile);
1526 _cpp_clean_line (pfile);
1527 do_pragma (pfile);
1528 end_directive (pfile, 1);
Neil Booth79ba5e32002-09-03 21:55:40 +00001529
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001530 /* We always insert at least one token, the directive result. It'll
1531 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1532 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
Neil Booth79ba5e32002-09-03 21:55:40 +00001533
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001534 /* If we're not handling the pragma internally, read all of the tokens from
1535 the string buffer now, while the string buffer is still installed. */
1536 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1537 to me what the true lifespan of the tokens are. It would appear that
1538 the lifespan is the entire parse of the main input stream, in which case
1539 this may not be wrong. */
1540 if (pfile->directive_result.type == CPP_PRAGMA)
1541 {
1542 int maxcount;
Neil Booth79ba5e32002-09-03 21:55:40 +00001543
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001544 count = 1;
1545 maxcount = 50;
1546 toks = XNEWVEC (cpp_token, maxcount);
1547 toks[0] = pfile->directive_result;
1548
1549 do
1550 {
1551 if (count == maxcount)
1552 {
1553 maxcount = maxcount * 3 / 2;
1554 toks = XRESIZEVEC (cpp_token, toks, maxcount);
1555 }
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001556 toks[count] = *cpp_get_token (pfile);
1557 /* Macros have been already expanded by cpp_get_token
1558 if the pragma allowed expansion. */
1559 toks[count++].flags |= NO_EXPAND;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001560 }
1561 while (toks[count-1].type != CPP_PRAGMA_EOL);
1562 }
1563 else
1564 {
1565 count = 1;
1566 toks = XNEW (cpp_token);
1567 toks[0] = pfile->directive_result;
1568
1569 /* If we handled the entire pragma internally, make sure we get the
1570 line number correct for the next token. */
1571 if (pfile->cb.line_change)
1572 pfile->cb.line_change (pfile, pfile->cur_token, false);
1573 }
1574
1575 /* Finish inlining run_directive. */
1576 pfile->buffer->file = NULL;
1577 _cpp_pop_buffer (pfile);
1578
1579 /* Reset the old macro state before ... */
1580 XDELETE (pfile->context);
1581 pfile->context = saved_context;
1582 pfile->cur_token = saved_cur_token;
1583 pfile->cur_run = saved_cur_run;
1584
1585 /* ... inserting the new tokens we collected. */
1586 _cpp_push_token_context (pfile, NULL, toks, count);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001587}
1588
Neil Booth87062812001-10-20 09:00:53 +00001589/* Handle the _Pragma operator. */
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001590void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001591_cpp_do__Pragma (cpp_reader *pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001592{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001593 const cpp_token *string = get__Pragma_string (pfile);
Zack Weinberg21b11492004-09-09 19:16:56 +00001594 pfile->directive_result.type = CPP_PADDING;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001595
Neil Booth79ba5e32002-09-03 21:55:40 +00001596 if (string)
1597 destringize_and_run (pfile, &string->val.str);
1598 else
John David Anglin0527bc42003-11-01 22:56:54 +00001599 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00001600 "_Pragma takes a parenthesized string literal");
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001601}
1602
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001603/* Handle #ifdef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001604static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001605do_ifdef (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +00001606{
Neil Booth93c803682000-10-28 17:59:06 +00001607 int skip = 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001608
Neil Boothcef0d192001-07-26 06:02:47 +00001609 if (! pfile->state.skipping)
Neil Booth93c803682000-10-28 17:59:06 +00001610 {
Tom Tromeyee1c2a12007-01-12 19:46:49 +00001611 const cpp_hashnode *node = lex_macro_node (pfile, false);
Zack Weinberg041c3192000-07-04 01:58:21 +00001612
Neil Booth93c803682000-10-28 17:59:06 +00001613 if (node)
Neil Bootha69cbaa2002-07-23 22:57:49 +00001614 {
1615 skip = node->type != NT_MACRO;
1616 _cpp_mark_macro_used (node);
1617 check_eol (pfile);
1618 }
Neil Booth93c803682000-10-28 17:59:06 +00001619 }
1620
1621 push_conditional (pfile, skip, T_IFDEF, 0);
Zack Weinberg168d3732000-03-14 06:34:11 +00001622}
1623
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001624/* Handle #ifndef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001625static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001626do_ifndef (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +00001627{
Neil Booth93c803682000-10-28 17:59:06 +00001628 int skip = 1;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001629 const cpp_hashnode *node = 0;
Zack Weinberg168d3732000-03-14 06:34:11 +00001630
Neil Boothcef0d192001-07-26 06:02:47 +00001631 if (! pfile->state.skipping)
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001632 {
Tom Tromeyee1c2a12007-01-12 19:46:49 +00001633 node = lex_macro_node (pfile, false);
Geoffrey Keatingb43db0b2000-12-02 22:28:44 +00001634
1635 if (node)
Neil Bootha69cbaa2002-07-23 22:57:49 +00001636 {
1637 skip = node->type == NT_MACRO;
1638 _cpp_mark_macro_used (node);
1639 check_eol (pfile);
1640 }
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001641 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001642
Neil Booth93c803682000-10-28 17:59:06 +00001643 push_conditional (pfile, skip, T_IFNDEF, node);
Per Bothner7f2935c1995-03-16 13:59:07 -08001644}
1645
Neil Booth6d18adb2001-07-29 17:27:57 +00001646/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1647 pfile->mi_ind_cmacro so we can handle multiple-include
Kazu Hirata6356f892003-06-12 19:01:08 +00001648 optimizations. If macro expansion occurs in the expression, we
Neil Booth6d18adb2001-07-29 17:27:57 +00001649 cannot treat it as a controlling conditional, since the expansion
1650 could change in the future. That is handled by cpp_get_token. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001651static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001652do_if (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001653{
Neil Booth93c803682000-10-28 17:59:06 +00001654 int skip = 1;
Per Bothner7f2935c1995-03-16 13:59:07 -08001655
Neil Boothcef0d192001-07-26 06:02:47 +00001656 if (! pfile->state.skipping)
Neil Booth87ed1092002-04-28 19:42:54 +00001657 skip = _cpp_parse_expr (pfile) == false;
Neil Booth93c803682000-10-28 17:59:06 +00001658
Neil Booth6d18adb2001-07-29 17:27:57 +00001659 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
Per Bothner7f2935c1995-03-16 13:59:07 -08001660}
1661
Neil Boothb528a072000-11-12 11:46:21 +00001662/* Flip skipping state if appropriate and continue without changing
Zack Weinbergea4a4532000-05-29 16:19:32 +00001663 if_stack; this is so that the error message for missing #endif's
1664 etc. will point to the original #if. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001665static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001666do_else (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001667{
Neil Boothb528a072000-11-12 11:46:21 +00001668 cpp_buffer *buffer = pfile->buffer;
1669 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001670
1671 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001672 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
Neil Booth93c803682000-10-28 17:59:06 +00001673 else
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001674 {
Neil Booth93c803682000-10-28 17:59:06 +00001675 if (ifs->type == T_ELSE)
1676 {
John David Anglin0527bc42003-11-01 22:56:54 +00001677 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1678 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Booth93c803682000-10-28 17:59:06 +00001679 "the conditional began here");
1680 }
Neil Boothb528a072000-11-12 11:46:21 +00001681 ifs->type = T_ELSE;
1682
Neil Boothcef0d192001-07-26 06:02:47 +00001683 /* Skip any future (erroneous) #elses or #elifs. */
1684 pfile->state.skipping = ifs->skip_elses;
1685 ifs->skip_elses = true;
Neil Booth93c803682000-10-28 17:59:06 +00001686
1687 /* Invalidate any controlling macro. */
1688 ifs->mi_cmacro = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001689
Neil Boothcef0d192001-07-26 06:02:47 +00001690 /* Only check EOL if was not originally skipping. */
Phil Edwards909de5d2002-03-22 21:59:04 +00001691 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
Neil Boothcef0d192001-07-26 06:02:47 +00001692 check_eol (pfile);
1693 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001694}
1695
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001696/* Handle a #elif directive by not changing if_stack either. See the
Neil Booth93c803682000-10-28 17:59:06 +00001697 comment above do_else. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001698static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001699do_elif (cpp_reader *pfile)
Zack Weinbergea4a4532000-05-29 16:19:32 +00001700{
Neil Boothb528a072000-11-12 11:46:21 +00001701 cpp_buffer *buffer = pfile->buffer;
1702 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001703
1704 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001705 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
Neil Boothb528a072000-11-12 11:46:21 +00001706 else
Zack Weinbergea4a4532000-05-29 16:19:32 +00001707 {
Neil Boothb528a072000-11-12 11:46:21 +00001708 if (ifs->type == T_ELSE)
1709 {
John David Anglin0527bc42003-11-01 22:56:54 +00001710 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1711 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Boothb528a072000-11-12 11:46:21 +00001712 "the conditional began here");
1713 }
1714 ifs->type = T_ELIF;
1715
Neil Boothcef0d192001-07-26 06:02:47 +00001716 /* Only evaluate this if we aren't skipping elses. During
1717 evaluation, set skipping to false to get lexer warnings. */
1718 if (ifs->skip_elses)
1719 pfile->state.skipping = 1;
1720 else
Neil Boothb528a072000-11-12 11:46:21 +00001721 {
Neil Boothcef0d192001-07-26 06:02:47 +00001722 pfile->state.skipping = 0;
1723 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1724 ifs->skip_elses = ! pfile->state.skipping;
Neil Boothb528a072000-11-12 11:46:21 +00001725 }
Neil Boothcef0d192001-07-26 06:02:47 +00001726
1727 /* Invalidate any controlling macro. */
1728 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001729 }
Zack Weinbergea4a4532000-05-29 16:19:32 +00001730}
1731
Neil Boothcef0d192001-07-26 06:02:47 +00001732/* #endif pops the if stack and resets pfile->state.skipping. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001733static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001734do_endif (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001735{
Neil Boothb528a072000-11-12 11:46:21 +00001736 cpp_buffer *buffer = pfile->buffer;
1737 struct if_stack *ifs = buffer->if_stack;
Per Bothner7f2935c1995-03-16 13:59:07 -08001738
Zack Weinbergea4a4532000-05-29 16:19:32 +00001739 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001740 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
Per Bothner7f2935c1995-03-16 13:59:07 -08001741 else
1742 {
Neil Boothcef0d192001-07-26 06:02:47 +00001743 /* Only check EOL if was not originally skipping. */
Phil Edwards909de5d2002-03-22 21:59:04 +00001744 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
Neil Boothcef0d192001-07-26 06:02:47 +00001745 check_eol (pfile);
1746
Neil Booth93c803682000-10-28 17:59:06 +00001747 /* If potential control macro, we go back outside again. */
1748 if (ifs->next == 0 && ifs->mi_cmacro)
1749 {
Neil Booth6d18adb2001-07-29 17:27:57 +00001750 pfile->mi_valid = true;
Neil Booth93c803682000-10-28 17:59:06 +00001751 pfile->mi_cmacro = ifs->mi_cmacro;
1752 }
1753
Neil Boothb528a072000-11-12 11:46:21 +00001754 buffer->if_stack = ifs->next;
Neil Boothcef0d192001-07-26 06:02:47 +00001755 pfile->state.skipping = ifs->was_skipping;
Neil Booth2a967f32001-05-20 06:26:45 +00001756 obstack_free (&pfile->buffer_ob, ifs);
Per Bothner7f2935c1995-03-16 13:59:07 -08001757 }
Neil Booth93c803682000-10-28 17:59:06 +00001758}
Zack Weinberg041c3192000-07-04 01:58:21 +00001759
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001760/* Push an if_stack entry for a preprocessor conditional, and set
1761 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1762 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1763 we need to check here that we are at the top of the file. */
Zack Weinbergea4a4532000-05-29 16:19:32 +00001764static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001765push_conditional (cpp_reader *pfile, int skip, int type,
1766 const cpp_hashnode *cmacro)
Zack Weinbergea4a4532000-05-29 16:19:32 +00001767{
1768 struct if_stack *ifs;
Neil Boothb528a072000-11-12 11:46:21 +00001769 cpp_buffer *buffer = pfile->buffer;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001770
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001771 ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
Neil Booth50410422001-09-15 10:18:03 +00001772 ifs->line = pfile->directive_line;
Neil Boothb528a072000-11-12 11:46:21 +00001773 ifs->next = buffer->if_stack;
Neil Boothcef0d192001-07-26 06:02:47 +00001774 ifs->skip_elses = pfile->state.skipping || !skip;
1775 ifs->was_skipping = pfile->state.skipping;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001776 ifs->type = type;
Neil Booth6d18adb2001-07-29 17:27:57 +00001777 /* This condition is effectively a test for top-of-file. */
1778 if (pfile->mi_valid && pfile->mi_cmacro == 0)
Neil Booth93c803682000-10-28 17:59:06 +00001779 ifs->mi_cmacro = cmacro;
1780 else
1781 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001782
Neil Boothcef0d192001-07-26 06:02:47 +00001783 pfile->state.skipping = skip;
Neil Boothb528a072000-11-12 11:46:21 +00001784 buffer->if_stack = ifs;
Zack Weinberg7061aa51998-12-15 11:09:16 +00001785}
1786
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001787/* Read the tokens of the answer into the macro pool, in a directive
1788 of type TYPE. Only commit the memory if we intend it as permanent
1789 storage, i.e. the #assert case. Returns 0 on success, and sets
1790 ANSWERP to point to the answer. */
Neil Booth93c803682000-10-28 17:59:06 +00001791static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001792parse_answer (cpp_reader *pfile, struct answer **answerp, int type)
Zack Weinberg041c3192000-07-04 01:58:21 +00001793{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001794 const cpp_token *paren;
Neil Booth93c803682000-10-28 17:59:06 +00001795 struct answer *answer;
Neil Booth8c3b2692001-09-30 10:03:11 +00001796 unsigned int acount;
Zack Weinberg041c3192000-07-04 01:58:21 +00001797
Neil Booth93c803682000-10-28 17:59:06 +00001798 /* In a conditional, it is legal to not have an open paren. We
1799 should save the following token in this case. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001800 paren = cpp_get_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001801
1802 /* If not a paren, see if we're OK. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001803 if (paren->type != CPP_OPEN_PAREN)
Zack Weinberg041c3192000-07-04 01:58:21 +00001804 {
Neil Booth93c803682000-10-28 17:59:06 +00001805 /* In a conditional no answer is a test for any answer. It
1806 could be followed by any token. */
1807 if (type == T_IF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001808 {
1809 _cpp_backup_tokens (pfile, 1);
1810 return 0;
1811 }
Neil Booth93c803682000-10-28 17:59:06 +00001812
1813 /* #unassert with no answer is valid - it removes all answers. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001814 if (type == T_UNASSERT && paren->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +00001815 return 0;
1816
John David Anglin0527bc42003-11-01 22:56:54 +00001817 cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate");
Neil Booth93c803682000-10-28 17:59:06 +00001818 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001819 }
1820
Neil Booth8c3b2692001-09-30 10:03:11 +00001821 for (acount = 0;; acount++)
Zack Weinberg041c3192000-07-04 01:58:21 +00001822 {
Neil Booth8c3b2692001-09-30 10:03:11 +00001823 size_t room_needed;
1824 const cpp_token *token = cpp_get_token (pfile);
1825 cpp_token *dest;
Zack Weinberg041c3192000-07-04 01:58:21 +00001826
Neil Booth93c803682000-10-28 17:59:06 +00001827 if (token->type == CPP_CLOSE_PAREN)
1828 break;
Zack Weinberg041c3192000-07-04 01:58:21 +00001829
1830 if (token->type == CPP_EOF)
1831 {
John David Anglin0527bc42003-11-01 22:56:54 +00001832 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
Neil Booth93c803682000-10-28 17:59:06 +00001833 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001834 }
Neil Booth8c3b2692001-09-30 10:03:11 +00001835
1836 /* struct answer includes the space for one token. */
1837 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1838
1839 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1840 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1841
1842 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1843 *dest = *token;
1844
1845 /* Drop whitespace at start, for answer equivalence purposes. */
1846 if (acount == 0)
1847 dest->flags &= ~PREV_WHITE;
Zack Weinberg041c3192000-07-04 01:58:21 +00001848 }
1849
Neil Booth8c3b2692001-09-30 10:03:11 +00001850 if (acount == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001851 {
John David Anglin0527bc42003-11-01 22:56:54 +00001852 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
Neil Booth93c803682000-10-28 17:59:06 +00001853 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001854 }
1855
Neil Booth8c3b2692001-09-30 10:03:11 +00001856 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1857 answer->count = acount;
1858 answer->next = NULL;
Neil Booth93c803682000-10-28 17:59:06 +00001859 *answerp = answer;
Zack Weinberg041c3192000-07-04 01:58:21 +00001860
Neil Booth93c803682000-10-28 17:59:06 +00001861 return 0;
1862}
1863
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001864/* Parses an assertion directive of type TYPE, returning a pointer to
1865 the hash node of the predicate, or 0 on error. If an answer was
1866 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
Neil Booth93c803682000-10-28 17:59:06 +00001867static cpp_hashnode *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001868parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
Neil Booth93c803682000-10-28 17:59:06 +00001869{
1870 cpp_hashnode *result = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001871 const cpp_token *predicate;
Neil Booth93c803682000-10-28 17:59:06 +00001872
1873 /* We don't expand predicates or answers. */
1874 pfile->state.prevent_expansion++;
1875
Neil Booth93c803682000-10-28 17:59:06 +00001876 *answerp = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001877 predicate = cpp_get_token (pfile);
1878 if (predicate->type == CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +00001879 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001880 else if (predicate->type != CPP_NAME)
John David Anglin0527bc42003-11-01 22:56:54 +00001881 cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier");
Neil Booth93c803682000-10-28 17:59:06 +00001882 else if (parse_answer (pfile, answerp, type) == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001883 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001884 unsigned int len = NODE_LEN (predicate->val.node);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001885 unsigned char *sym = (unsigned char *) alloca (len + 1);
Neil Booth93c803682000-10-28 17:59:06 +00001886
1887 /* Prefix '#' to get it out of macro namespace. */
1888 sym[0] = '#';
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001889 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
Neil Booth93c803682000-10-28 17:59:06 +00001890 result = cpp_lookup (pfile, sym, len + 1);
Zack Weinberg041c3192000-07-04 01:58:21 +00001891 }
1892
Neil Booth93c803682000-10-28 17:59:06 +00001893 pfile->state.prevent_expansion--;
1894 return result;
Zack Weinberg041c3192000-07-04 01:58:21 +00001895}
1896
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001897/* Returns a pointer to the pointer to CANDIDATE in the answer chain,
Zack Weinberg041c3192000-07-04 01:58:21 +00001898 or a pointer to NULL if the answer is not in the chain. */
Neil Booth93c803682000-10-28 17:59:06 +00001899static struct answer **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001900find_answer (cpp_hashnode *node, const struct answer *candidate)
Zack Weinberg041c3192000-07-04 01:58:21 +00001901{
Neil Booth93c803682000-10-28 17:59:06 +00001902 unsigned int i;
Zack Weinberg041c3192000-07-04 01:58:21 +00001903 struct answer **result;
1904
1905 for (result = &node->value.answers; *result; result = &(*result)->next)
Neil Booth93c803682000-10-28 17:59:06 +00001906 {
1907 struct answer *answer = *result;
1908
1909 if (answer->count == candidate->count)
1910 {
1911 for (i = 0; i < answer->count; i++)
1912 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1913 break;
1914
1915 if (i == answer->count)
1916 break;
1917 }
1918 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001919
1920 return result;
1921}
1922
Neil Booth93c803682000-10-28 17:59:06 +00001923/* Test an assertion within a preprocessor conditional. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +00001924 nonzero on failure, zero on success. On success, the result of
Hans-Peter Nilsson24026452002-11-29 22:41:04 +00001925 the test is written into VALUE, otherwise the value 0. */
Neil Booth93c803682000-10-28 17:59:06 +00001926int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001927_cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
Neil Booth93c803682000-10-28 17:59:06 +00001928{
1929 struct answer *answer;
1930 cpp_hashnode *node;
1931
1932 node = parse_assertion (pfile, &answer, T_IF);
Hans-Peter Nilsson24026452002-11-29 22:41:04 +00001933
1934 /* For recovery, an erroneous assertion expression is handled as a
1935 failing assertion. */
1936 *value = 0;
1937
Neil Booth93c803682000-10-28 17:59:06 +00001938 if (node)
1939 *value = (node->type == NT_ASSERTION &&
1940 (answer == 0 || *find_answer (node, answer) != 0));
Neil Booth91318902002-05-26 18:42:21 +00001941 else if (pfile->cur_token[-1].type == CPP_EOF)
1942 _cpp_backup_tokens (pfile, 1);
Neil Booth93c803682000-10-28 17:59:06 +00001943
1944 /* We don't commit the memory for the answer - it's temporary only. */
1945 return node == 0;
1946}
1947
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001948/* Handle #assert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001949static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001950do_assert (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001951{
Zack Weinberg041c3192000-07-04 01:58:21 +00001952 struct answer *new_answer;
1953 cpp_hashnode *node;
Kazu Hiratadf383482002-05-22 22:02:16 +00001954
Neil Booth93c803682000-10-28 17:59:06 +00001955 node = parse_assertion (pfile, &new_answer, T_ASSERT);
Zack Weinberg041c3192000-07-04 01:58:21 +00001956 if (node)
1957 {
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001958 size_t answer_size;
1959
Neil Booth93c803682000-10-28 17:59:06 +00001960 /* Place the new answer in the answer list. First check there
1961 is not a duplicate. */
Zack Weinberg041c3192000-07-04 01:58:21 +00001962 new_answer->next = 0;
Neil Booth93c803682000-10-28 17:59:06 +00001963 if (node->type == NT_ASSERTION)
Zack Weinberg041c3192000-07-04 01:58:21 +00001964 {
Neil Booth93c803682000-10-28 17:59:06 +00001965 if (*find_answer (node, new_answer))
1966 {
John David Anglin0527bc42003-11-01 22:56:54 +00001967 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
Neil Boothebef4e82002-04-14 18:42:47 +00001968 NODE_NAME (node) + 1);
Neil Booth93c803682000-10-28 17:59:06 +00001969 return;
1970 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001971 new_answer->next = node->value.answers;
1972 }
Neil Booth8c3b2692001-09-30 10:03:11 +00001973
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001974 answer_size = sizeof (struct answer) + ((new_answer->count - 1)
1975 * sizeof (cpp_token));
1976 /* Commit or allocate storage for the object. */
1977 if (pfile->hash_table->alloc_subobject)
1978 {
1979 struct answer *temp_answer = new_answer;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001980 new_answer = (struct answer *) pfile->hash_table->alloc_subobject
1981 (answer_size);
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001982 memcpy (new_answer, temp_answer, answer_size);
1983 }
1984 else
1985 BUFF_FRONT (pfile->a_buff) += answer_size;
1986
Neil Booth93c803682000-10-28 17:59:06 +00001987 node->type = NT_ASSERTION;
Zack Weinberg041c3192000-07-04 01:58:21 +00001988 node->value.answers = new_answer;
Neil Booth8c3b2692001-09-30 10:03:11 +00001989 check_eol (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +00001990 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001991}
Zack Weinberg7061aa51998-12-15 11:09:16 +00001992
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001993/* Handle #unassert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001994static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001995do_unassert (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001996{
Zack Weinberg041c3192000-07-04 01:58:21 +00001997 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00001998 struct answer *answer;
Kazu Hiratadf383482002-05-22 22:02:16 +00001999
Neil Booth93c803682000-10-28 17:59:06 +00002000 node = parse_assertion (pfile, &answer, T_UNASSERT);
2001 /* It isn't an error to #unassert something that isn't asserted. */
2002 if (node && node->type == NT_ASSERTION)
Zack Weinberg7061aa51998-12-15 11:09:16 +00002003 {
Zack Weinberg041c3192000-07-04 01:58:21 +00002004 if (answer)
Neil Booth93c803682000-10-28 17:59:06 +00002005 {
2006 struct answer **p = find_answer (node, answer), *temp;
2007
2008 /* Remove the answer from the list. */
2009 temp = *p;
2010 if (temp)
2011 *p = temp->next;
2012
2013 /* Did we free the last answer? */
2014 if (node->value.answers == 0)
2015 node->type = NT_VOID;
Neil Booth8c3b2692001-09-30 10:03:11 +00002016
2017 check_eol (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00002018 }
2019 else
2020 _cpp_free_definition (node);
Zack Weinberg7061aa51998-12-15 11:09:16 +00002021 }
Neil Booth93c803682000-10-28 17:59:06 +00002022
2023 /* We don't commit the memory for the answer - it's temporary only. */
Per Bothner7f2935c1995-03-16 13:59:07 -08002024}
Per Bothner7f2935c1995-03-16 13:59:07 -08002025
Zack Weinberg45b966d2000-03-13 22:01:08 +00002026/* These are for -D, -U, -A. */
2027
2028/* Process the string STR as if it appeared as the body of a #define.
2029 If STR is just an identifier, define it with value 1.
2030 If STR has anything after the identifier, then it should
Kazu Hirataec5c56d2001-08-01 17:57:27 +00002031 be identifier=definition. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00002032void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002033cpp_define (cpp_reader *pfile, const char *str)
Zack Weinberg45b966d2000-03-13 22:01:08 +00002034{
2035 char *buf, *p;
2036 size_t count;
2037
Kazu Hiratadf383482002-05-22 22:02:16 +00002038 /* Copy the entire option so we can modify it.
Zack Weinberg45b966d2000-03-13 22:01:08 +00002039 Change the first "=" in the string to a space. If there is none,
Neil Booth86368122000-10-31 23:34:59 +00002040 tack " 1" on the end. */
2041
Neil Booth86368122000-10-31 23:34:59 +00002042 count = strlen (str);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002043 buf = (char *) alloca (count + 3);
Neil Booth86368122000-10-31 23:34:59 +00002044 memcpy (buf, str, count);
2045
2046 p = strchr (str, '=');
Zack Weinberg45b966d2000-03-13 22:01:08 +00002047 if (p)
Neil Booth86368122000-10-31 23:34:59 +00002048 buf[p - str] = ' ';
Zack Weinberg45b966d2000-03-13 22:01:08 +00002049 else
2050 {
Neil Booth86368122000-10-31 23:34:59 +00002051 buf[count++] = ' ';
2052 buf[count++] = '1';
Zack Weinberg45b966d2000-03-13 22:01:08 +00002053 }
Neil Booth26aea072003-04-19 00:22:51 +00002054 buf[count] = '\n';
Zack Weinberg45b966d2000-03-13 22:01:08 +00002055
Neil Booth29401c32001-08-22 20:37:20 +00002056 run_directive (pfile, T_DEFINE, buf, count);
Zack Weinberg2c8f0512000-08-29 18:37:37 +00002057}
2058
Neil Boothad2a0842000-12-17 00:13:54 +00002059/* Slight variant of the above for use by initialize_builtins. */
Zack Weinberg2c8f0512000-08-29 18:37:37 +00002060void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002061_cpp_define_builtin (cpp_reader *pfile, const char *str)
Zack Weinberg2c8f0512000-08-29 18:37:37 +00002062{
Neil Booth26aea072003-04-19 00:22:51 +00002063 size_t len = strlen (str);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002064 char *buf = (char *) alloca (len + 1);
Neil Booth26aea072003-04-19 00:22:51 +00002065 memcpy (buf, str, len);
2066 buf[len] = '\n';
2067 run_directive (pfile, T_DEFINE, buf, len);
Zack Weinberg45b966d2000-03-13 22:01:08 +00002068}
2069
2070/* Process MACRO as if it appeared as the body of an #undef. */
2071void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002072cpp_undef (cpp_reader *pfile, const char *macro)
Zack Weinberg45b966d2000-03-13 22:01:08 +00002073{
Neil Booth26aea072003-04-19 00:22:51 +00002074 size_t len = strlen (macro);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002075 char *buf = (char *) alloca (len + 1);
Neil Booth26aea072003-04-19 00:22:51 +00002076 memcpy (buf, macro, len);
2077 buf[len] = '\n';
2078 run_directive (pfile, T_UNDEF, buf, len);
Zack Weinberg45b966d2000-03-13 22:01:08 +00002079}
2080
Richard Henderson121de392007-03-30 14:12:53 -07002081/* Like lex_macro_node, but read the input from STR. */
2082static cpp_hashnode *
2083lex_macro_node_from_str (cpp_reader *pfile, const char *str)
2084{
2085 size_t len = strlen (str);
2086 uchar *buf = (char *) alloca (len + 1);
2087 cpp_hashnode *node;
2088
2089 memcpy (buf, str, len);
2090 buf[len] = '\n';
2091 cpp_push_buffer (pfile, buf, len, true);
2092 node = lex_macro_node (pfile, true);
2093 _cpp_pop_buffer (pfile);
2094
2095 return node;
2096}
2097
2098/* If STR is a defined macro, return its definition node, else return NULL. */
2099cpp_macro *
2100cpp_push_definition (cpp_reader *pfile, const char *str)
2101{
2102 cpp_hashnode *node = lex_macro_node_from_str (pfile, str);
2103 if (node && node->type == NT_MACRO)
2104 return node->value.macro;
2105 else
2106 return NULL;
2107}
2108
2109/* Replace a previous definition DFN of the macro STR. If DFN is NULL,
2110 then the macro should be undefined. */
2111void
2112cpp_pop_definition (cpp_reader *pfile, const char *str, cpp_macro *dfn)
2113{
2114 cpp_hashnode *node = lex_macro_node_from_str (pfile, str);
2115 if (node == NULL)
2116 return;
2117
2118 if (node->type == NT_MACRO)
2119 {
2120 if (pfile->cb.undef)
2121 pfile->cb.undef (pfile, pfile->directive_line, node);
2122 if (CPP_OPTION (pfile, warn_unused_macros))
2123 _cpp_warn_if_unused_macro (pfile, node, NULL);
2124 }
2125 if (node->type != NT_VOID)
2126 _cpp_free_definition (node);
2127
2128 if (dfn)
2129 {
2130 node->type = NT_MACRO;
2131 node->value.macro = dfn;
2132 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
2133 node->flags |= NODE_WARN;
2134
2135 if (pfile->cb.define)
2136 pfile->cb.define (pfile, pfile->directive_line, node);
2137 }
2138}
2139
Kazu Hirataec5c56d2001-08-01 17:57:27 +00002140/* Process the string STR as if it appeared as the body of a #assert. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00002141void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002142cpp_assert (cpp_reader *pfile, const char *str)
Zack Weinberg45b966d2000-03-13 22:01:08 +00002143{
Neil Booth86368122000-10-31 23:34:59 +00002144 handle_assertion (pfile, str, T_ASSERT);
Zack Weinberg45b966d2000-03-13 22:01:08 +00002145}
2146
Kazu Hirataec5c56d2001-08-01 17:57:27 +00002147/* Process STR as if it appeared as the body of an #unassert. */
Zack Weinberg0b22d651999-03-15 18:42:46 +00002148void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002149cpp_unassert (cpp_reader *pfile, const char *str)
Zack Weinberg0b22d651999-03-15 18:42:46 +00002150{
Neil Booth86368122000-10-31 23:34:59 +00002151 handle_assertion (pfile, str, T_UNASSERT);
Kazu Hiratadf383482002-05-22 22:02:16 +00002152}
Zack Weinberg0b22d651999-03-15 18:42:46 +00002153
Neil Booth86368122000-10-31 23:34:59 +00002154/* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2155static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002156handle_assertion (cpp_reader *pfile, const char *str, int type)
Neil Booth86368122000-10-31 23:34:59 +00002157{
2158 size_t count = strlen (str);
2159 const char *p = strchr (str, '=');
2160
Neil Booth26aea072003-04-19 00:22:51 +00002161 /* Copy the entire option so we can modify it. Change the first
2162 "=" in the string to a '(', and tack a ')' on the end. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002163 char *buf = (char *) alloca (count + 2);
Neil Booth26aea072003-04-19 00:22:51 +00002164
2165 memcpy (buf, str, count);
Neil Booth86368122000-10-31 23:34:59 +00002166 if (p)
2167 {
Neil Booth86368122000-10-31 23:34:59 +00002168 buf[p - str] = '(';
2169 buf[count++] = ')';
Neil Booth86368122000-10-31 23:34:59 +00002170 }
Neil Booth26aea072003-04-19 00:22:51 +00002171 buf[count] = '\n';
2172 str = buf;
Neil Booth86368122000-10-31 23:34:59 +00002173
Neil Booth29401c32001-08-22 20:37:20 +00002174 run_directive (pfile, type, str, count);
Neil Booth86368122000-10-31 23:34:59 +00002175}
2176
Neil Booth7e96d762001-01-13 01:00:01 +00002177/* The number of errors for a given reader. */
2178unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002179cpp_errors (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00002180{
2181 return pfile->errors;
2182}
2183
2184/* The options structure. */
2185cpp_options *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002186cpp_get_options (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00002187{
2188 return &pfile->opts;
2189}
2190
2191/* The callbacks structure. */
2192cpp_callbacks *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002193cpp_get_callbacks (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00002194{
2195 return &pfile->cb;
2196}
2197
2198/* Copy the given callbacks structure to our own. */
2199void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002200cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
Neil Booth7e96d762001-01-13 01:00:01 +00002201{
2202 pfile->cb = *cb;
2203}
2204
Zack Weinbergc6e83802004-06-05 20:58:06 +00002205/* The dependencies structure. (Creates one if it hasn't already been.) */
2206struct deps *
2207cpp_get_deps (cpp_reader *pfile)
2208{
2209 if (!pfile->deps)
2210 pfile->deps = deps_init ();
2211 return pfile->deps;
2212}
2213
Neil Bootheb1f4d92000-12-18 19:00:26 +00002214/* Push a new buffer on the buffer stack. Returns the new buffer; it
2215 doesn't fail. It does not generate a file change call back; that
2216 is the responsibility of the caller. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00002217cpp_buffer *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002218cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
Per Bothner40de9f72003-10-02 07:30:34 +00002219 int from_stage3)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002220{
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002221 cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
Neil Booth93c803682000-10-28 17:59:06 +00002222
Neil Boothfde84342001-08-06 21:07:41 +00002223 /* Clears, amongst other things, if_stack and mi_cmacro. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002224 memset (new_buffer, 0, sizeof (cpp_buffer));
Neil Boothad2a0842000-12-17 00:13:54 +00002225
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002226 new_buffer->next_line = new_buffer->buf = buffer;
2227 new_buffer->rlimit = buffer + len;
2228 new_buffer->from_stage3 = from_stage3;
2229 new_buffer->prev = pfile->buffer;
2230 new_buffer->need_line = true;
Neil Booth0bda4762000-12-11 07:45:16 +00002231
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002232 pfile->buffer = new_buffer;
Eric Christophercf551fb2004-01-16 22:37:49 +00002233
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002234 return new_buffer;
Zack Weinbergc71f8352000-07-05 05:33:57 +00002235}
2236
Neil Boothaf0d16c2002-04-22 17:48:02 +00002237/* Pops a single buffer, with a file change call-back if appropriate.
2238 Then pushes the next -include file, if any remain. */
Neil Boothef6e9582001-08-04 12:01:59 +00002239void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002240_cpp_pop_buffer (cpp_reader *pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002241{
Neil Boothfde84342001-08-06 21:07:41 +00002242 cpp_buffer *buffer = pfile->buffer;
Neil Booth8f9b4002003-07-29 22:26:13 +00002243 struct _cpp_file *inc = buffer->file;
Neil Boothad2a0842000-12-17 00:13:54 +00002244 struct if_stack *ifs;
Zack Weinbergc71f8352000-07-05 05:33:57 +00002245
Neil Boothfde84342001-08-06 21:07:41 +00002246 /* Walk back up the conditional stack till we reach its level at
2247 entry to this file, issuing error messages. */
2248 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
John David Anglin0527bc42003-11-01 22:56:54 +00002249 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Boothfde84342001-08-06 21:07:41 +00002250 "unterminated #%s", dtable[ifs->type].name);
2251
Neil Booth97293892001-09-14 22:04:46 +00002252 /* In case of a missing #endif. */
Neil Booth67821e32001-08-05 17:31:25 +00002253 pfile->state.skipping = 0;
Neil Booth29401c32001-08-22 20:37:20 +00002254
Neil Boothaf0d16c2002-04-22 17:48:02 +00002255 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
Neil Booth29401c32001-08-22 20:37:20 +00002256 pfile->buffer = buffer->prev;
2257
Neil Booth26aea072003-04-19 00:22:51 +00002258 free (buffer->notes);
2259
Neil Boothaf0d16c2002-04-22 17:48:02 +00002260 /* Free the buffer object now; we may want to push a new buffer
2261 in _cpp_push_next_include_file. */
2262 obstack_free (&pfile->buffer_ob, buffer);
Neil Booth29401c32001-08-22 20:37:20 +00002263
Neil Boothaf0d16c2002-04-22 17:48:02 +00002264 if (inc)
2265 {
2266 _cpp_pop_file_buffer (pfile, inc);
2267
Per Bothner40de9f72003-10-02 07:30:34 +00002268 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
Neil Boothaf0d16c2002-04-22 17:48:02 +00002269 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00002270}
2271
Kazu Hirata05713b82002-09-15 18:24:08 +00002272/* Enter all recognized directives in the hash table. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00002273void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002274_cpp_init_directives (cpp_reader *pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002275{
Neil Booth766ee682001-01-29 19:20:12 +00002276 unsigned int i;
Neil Booth93c803682000-10-28 17:59:06 +00002277 cpp_hashnode *node;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00002278
John David Anglin37b85242001-03-02 01:11:50 +00002279 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
Neil Booth93c803682000-10-28 17:59:06 +00002280 {
Neil Booth766ee682001-01-29 19:20:12 +00002281 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
Zack Weinberg4977bab2002-12-16 18:23:00 +00002282 node->is_directive = 1;
2283 node->directive_index = i;
Neil Booth93c803682000-10-28 17:59:06 +00002284 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00002285}