blob: c2e71016f2417689ad27057d4b251a290e6bcb0b [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,
Joseph Myers148e4212009-03-29 23:56:07 +01004 2007, 2008, 2009 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
Jakub Jelinek748086b2009-04-09 17:00:19 +020011Free Software Foundation; either version 3, or (at your option) any
Per Bothner7f2935c1995-03-16 13:59:07 -080012later 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
Jakub Jelinek748086b2009-04-09 17:00:19 +020020along with this program; see the file COPYING3. If not see
21<http://www.gnu.org/licenses/>. */
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;
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +000035 linenum_type 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)
Tom Tromey899015a2008-05-13 14:50:27 +000078#define DEPRECATED (1 << 5)
Neil Booth93c803682000-10-28 17:59:06 +000079
80/* Defines one #-directive, including how to handle it. */
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000081typedef void (*directive_handler) (cpp_reader *);
Neil Booth93c803682000-10-28 17:59:06 +000082typedef struct directive directive;
83struct directive
84{
85 directive_handler handler; /* Function to handle directive. */
Neil Booth562a5c22002-04-21 18:46:42 +000086 const uchar *name; /* Name of directive. */
Neil Booth93c803682000-10-28 17:59:06 +000087 unsigned short length; /* Length of name. */
88 unsigned char origin; /* Origin of directive. */
89 unsigned char flags; /* Flags describing this directive. */
90};
91
Zack Weinberg1316f1f2000-02-06 07:53:50 +000092/* Forward declarations. */
93
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000094static void skip_rest_of_line (cpp_reader *);
Joseph Myersa5cb5632009-04-18 16:28:40 +010095static void check_eol (cpp_reader *, bool);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000096static void start_directive (cpp_reader *);
97static void prepare_directive_trad (cpp_reader *);
98static void end_directive (cpp_reader *, int);
99static void directive_diagnostics (cpp_reader *, const directive *, int);
100static void run_directive (cpp_reader *, int, const char *, size_t);
101static char *glue_header_name (cpp_reader *);
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000102static const char *parse_include (cpp_reader *, int *, const cpp_token ***);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000103static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
104static unsigned int read_flag (cpp_reader *, unsigned int);
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000105static bool strtolinenum (const uchar *, size_t, linenum_type *, bool *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000106static void do_diagnostic (cpp_reader *, int, int);
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000107static cpp_hashnode *lex_macro_node (cpp_reader *, bool);
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000108static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000109static void do_include_common (cpp_reader *, enum include_type);
110static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
111 const cpp_hashnode *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000112static int count_registered_pragmas (struct pragma_entry *);
113static char ** save_registered_pragmas (struct pragma_entry *, char **);
114static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
115 char **);
116static void do_pragma_once (cpp_reader *);
117static void do_pragma_poison (cpp_reader *);
118static void do_pragma_system_header (cpp_reader *);
119static void do_pragma_dependency (cpp_reader *);
120static void do_linemarker (cpp_reader *);
121static const cpp_token *get_token_no_padding (cpp_reader *);
122static const cpp_token *get__Pragma_string (cpp_reader *);
123static void destringize_and_run (cpp_reader *, const cpp_string *);
124static int parse_answer (cpp_reader *, struct answer **, int);
125static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
126static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
127static void handle_assertion (cpp_reader *, const char *, int);
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +0000128
Zack Weinberg168d3732000-03-14 06:34:11 +0000129/* This is the table of directive handlers. It is ordered by
130 frequency of occurrence; the numbers at the end are directive
131 counts from all the source code I have lying around (egcs and libc
132 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
Neil Booth93c803682000-10-28 17:59:06 +0000133 pcmcia-cs-3.0.9). This is no longer important as directive lookup
Tom Tromey899015a2008-05-13 14:50:27 +0000134 is now O(1). All extensions other than #warning, #include_next,
135 and #import are deprecated. The name is where the extension
136 appears to have come from. */
Jeff Lawe9a25f71997-11-02 14:19:36 -0700137
Neil Booth93c803682000-10-28 17:59:06 +0000138#define DIRECTIVE_TABLE \
139D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000140D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000141D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
142D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
Neil Booth1a769162002-06-11 05:36:17 +0000143D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000144D(else, T_ELSE, KANDR, COND) /* 9863 */ \
145D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
146D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
Neil Booth1a769162002-06-11 05:36:17 +0000147D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
148D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000149D(error, T_ERROR, STDC89, 0) /* 475 */ \
150D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
151D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000152D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
Tom Tromey899015a2008-05-13 14:50:27 +0000153D(ident, T_IDENT, EXTENSION, IN_I | DEPRECATED) /* 11 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000154D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
Tom Tromey899015a2008-05-13 14:50:27 +0000155D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
156D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
157D(sccs, T_SCCS, EXTENSION, IN_I | DEPRECATED) /* 0 SVR4? */
Zack Weinberg1ed17cd2005-05-12 18:31:38 +0000158
159/* #sccs is synonymous with #ident. */
160#define do_sccs do_ident
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000161
Zack Weinberg168d3732000-03-14 06:34:11 +0000162/* Use the table to generate a series of prototypes, an enum for the
163 directive names, and an array of directive handlers. */
164
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000165#define D(name, t, o, f) static void do_##name (cpp_reader *);
Zack Weinberg168d3732000-03-14 06:34:11 +0000166DIRECTIVE_TABLE
167#undef D
168
Zack Weinberg041c3192000-07-04 01:58:21 +0000169#define D(n, tag, o, f) tag,
Zack Weinberg168d3732000-03-14 06:34:11 +0000170enum
171{
172 DIRECTIVE_TABLE
173 N_DIRECTIVES
Per Bothner7f2935c1995-03-16 13:59:07 -0800174};
Zack Weinberg168d3732000-03-14 06:34:11 +0000175#undef D
176
Zack Weinberg041c3192000-07-04 01:58:21 +0000177#define D(name, t, origin, flags) \
Kaveh R. Ghazi9a238582003-06-16 19:14:22 +0000178{ do_##name, (const uchar *) #name, \
179 sizeof #name - 1, origin, flags },
Neil Booth93c803682000-10-28 17:59:06 +0000180static const directive dtable[] =
Zack Weinberg168d3732000-03-14 06:34:11 +0000181{
182DIRECTIVE_TABLE
183};
184#undef D
185#undef DIRECTIVE_TABLE
Per Bothner7f2935c1995-03-16 13:59:07 -0800186
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000187/* Wrapper struct directive for linemarkers.
188 The origin is more or less true - the original K+R cpp
189 did use this notation in its preprocessed output. */
190static const directive linemarker_dir =
191{
Kris Van Heesb6baa672008-04-18 13:58:08 +0000192 do_linemarker, UC"#", 1, KANDR, IN_I
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000193};
194
Neil Booth1a769162002-06-11 05:36:17 +0000195#define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
Neil Booth67821e32001-08-05 17:31:25 +0000196
Neil Booth93c803682000-10-28 17:59:06 +0000197/* Skip any remaining tokens in a directive. */
198static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000199skip_rest_of_line (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +0000200{
Neil Booth93c803682000-10-28 17:59:06 +0000201 /* Discard all stacked contexts. */
Neil Booth8128ccc2002-11-18 20:43:40 +0000202 while (pfile->context->prev)
Neil Booth93c803682000-10-28 17:59:06 +0000203 _cpp_pop_context (pfile);
204
Neil Boothb528a072000-11-12 11:46:21 +0000205 /* Sweep up all tokens remaining on the line. */
Neil Booth345894b2001-09-16 13:44:29 +0000206 if (! SEEN_EOL ())
207 while (_cpp_lex_token (pfile)->type != CPP_EOF)
208 ;
Neil Booth93c803682000-10-28 17:59:06 +0000209}
210
Joseph Myersa5cb5632009-04-18 16:28:40 +0100211/* Ensure there are no stray tokens at the end of a directive. If
212 EXPAND is true, tokens macro-expanding to nothing are allowed. */
Neil Booth93c803682000-10-28 17:59:06 +0000213static void
Joseph Myersa5cb5632009-04-18 16:28:40 +0100214check_eol (cpp_reader *pfile, bool expand)
Neil Booth93c803682000-10-28 17:59:06 +0000215{
Joseph Myersa5cb5632009-04-18 16:28:40 +0100216 if (! SEEN_EOL () && (expand
217 ? cpp_get_token (pfile)
218 : _cpp_lex_token (pfile))->type != CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000219 cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
Neil Boothebef4e82002-04-14 18:42:47 +0000220 pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +0000221}
222
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000223/* Ensure there are no stray tokens other than comments at the end of
224 a directive, and gather the comments. */
225static const cpp_token **
226check_eol_return_comments (cpp_reader *pfile)
227{
228 size_t c;
229 size_t capacity = 8;
230 const cpp_token **buf;
231
232 buf = XNEWVEC (const cpp_token *, capacity);
233 c = 0;
234 if (! SEEN_EOL ())
235 {
236 while (1)
237 {
238 const cpp_token *tok;
239
240 tok = _cpp_lex_token (pfile);
241 if (tok->type == CPP_EOF)
242 break;
243 if (tok->type != CPP_COMMENT)
244 cpp_error (pfile, CPP_DL_PEDWARN,
245 "extra tokens at end of #%s directive",
246 pfile->directive->name);
247 else
248 {
249 if (c + 1 >= capacity)
250 {
251 capacity *= 2;
252 buf = XRESIZEVEC (const cpp_token *, buf, capacity);
253 }
254 buf[c] = tok;
255 ++c;
256 }
257 }
258 }
259 buf[c] = NULL;
260 return buf;
261}
262
Neil Boothfe6c2db2000-11-15 19:25:22 +0000263/* Called when entering a directive, _Pragma or command-line directive. */
264static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000265start_directive (cpp_reader *pfile)
Zack Weinbergc5a04732000-04-25 19:32:36 +0000266{
Neil Booth7f2f1a62000-11-14 18:32:06 +0000267 /* Setup in-directive state. */
268 pfile->state.in_directive = 1;
269 pfile->state.save_comments = 0;
Zack Weinberg21b11492004-09-09 19:16:56 +0000270 pfile->directive_result.type = CPP_PADDING;
Neil Booth7f2f1a62000-11-14 18:32:06 +0000271
Neil Booth93c803682000-10-28 17:59:06 +0000272 /* Some handlers need the position of the # for diagnostics. */
Per Bothner500bee02004-04-22 19:22:27 -0700273 pfile->directive_line = pfile->line_table->highest_line;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000274}
275
276/* Called when leaving a directive, _Pragma or command-line directive. */
277static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000278end_directive (cpp_reader *pfile, int skip_line)
Neil Boothfe6c2db2000-11-15 19:25:22 +0000279{
Richard Hendersonbc4071d2006-01-04 08:33:38 -0800280 if (pfile->state.in_deferred_pragma)
281 ;
282 else if (CPP_OPTION (pfile, traditional))
Neil Booth1a769162002-06-11 05:36:17 +0000283 {
Neil Boothd97371e2002-06-18 06:27:40 +0000284 /* Revert change of prepare_directive_trad. */
285 pfile->state.prevent_expansion--;
286
Neil Boothb66377c2002-06-13 21:16:00 +0000287 if (pfile->directive != &dtable[T_DEFINE])
Neil Booth1a769162002-06-11 05:36:17 +0000288 _cpp_remove_overlay (pfile);
289 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000290 /* We don't skip for an assembler #. */
Neil Boothb66377c2002-06-13 21:16:00 +0000291 else if (skip_line)
Neil Booth67821e32001-08-05 17:31:25 +0000292 {
293 skip_rest_of_line (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +0000294 if (!pfile->keep_tokens)
295 {
296 pfile->cur_run = &pfile->base_run;
297 pfile->cur_token = pfile->base_run.base;
298 }
Neil Booth67821e32001-08-05 17:31:25 +0000299 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000300
301 /* Restore state. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000302 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
303 pfile->state.in_directive = 0;
Neil Boothd97371e2002-06-18 06:27:40 +0000304 pfile->state.in_expression = 0;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000305 pfile->state.angled_headers = 0;
306 pfile->directive = 0;
307}
308
Neil Booth1a769162002-06-11 05:36:17 +0000309/* Prepare to handle the directive in pfile->directive. */
310static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000311prepare_directive_trad (cpp_reader *pfile)
Neil Booth1a769162002-06-11 05:36:17 +0000312{
Neil Booth951a0762002-06-27 06:01:58 +0000313 if (pfile->directive != &dtable[T_DEFINE])
Neil Booth1a769162002-06-11 05:36:17 +0000314 {
Neil Boothb66377c2002-06-13 21:16:00 +0000315 bool no_expand = (pfile->directive
316 && ! (pfile->directive->flags & EXPAND));
Neil Booth974c43f2002-06-13 06:25:28 +0000317 bool was_skipping = pfile->state.skipping;
Neil Booth1a769162002-06-11 05:36:17 +0000318
Neil Boothd97371e2002-06-18 06:27:40 +0000319 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
320 || pfile->directive == &dtable[T_ELIF]);
Neil Booth45f24922003-12-12 07:00:29 +0000321 if (pfile->state.in_expression)
322 pfile->state.skipping = false;
323
Neil Booth1a769162002-06-11 05:36:17 +0000324 if (no_expand)
325 pfile->state.prevent_expansion++;
Zack Weinberg43839642003-07-13 17:34:18 +0000326 _cpp_scan_out_logical_line (pfile, NULL);
Neil Booth1a769162002-06-11 05:36:17 +0000327 if (no_expand)
328 pfile->state.prevent_expansion--;
Neil Booth45f24922003-12-12 07:00:29 +0000329
Neil Booth974c43f2002-06-13 06:25:28 +0000330 pfile->state.skipping = was_skipping;
Neil Booth1a769162002-06-11 05:36:17 +0000331 _cpp_overlay_buffer (pfile, pfile->out.base,
332 pfile->out.cur - pfile->out.base);
333 }
Neil Boothd97371e2002-06-18 06:27:40 +0000334
335 /* Stop ISO C from expanding anything. */
336 pfile->state.prevent_expansion++;
Neil Booth1a769162002-06-11 05:36:17 +0000337}
338
Kazu Hiratada7d8302002-09-22 02:03:17 +0000339/* Output diagnostics for a directive DIR. INDENTED is nonzero if
Neil Booth18a9d8f2001-09-16 11:23:56 +0000340 the '#' was indented. */
Neil Booth18a9d8f2001-09-16 11:23:56 +0000341static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000342directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000343{
Tom Tromey899015a2008-05-13 14:50:27 +0000344 /* Issue -pedantic or deprecated warnings for extensions. We let
345 -pedantic take precedence if both are applicable. */
346 if (! pfile->state.skipping)
347 {
348 if (dir->origin == EXTENSION
349 && !(dir == &dtable[T_IMPORT] && CPP_OPTION (pfile, objc))
350 && CPP_PEDANTIC (pfile))
351 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
352 else if (((dir->flags & DEPRECATED) != 0
353 || (dir == &dtable[T_IMPORT] && !CPP_OPTION (pfile, objc)))
354 && CPP_OPTION (pfile, warn_deprecated))
355 cpp_error (pfile, CPP_DL_WARNING, "#%s is a deprecated GCC extension",
356 dir->name);
357 }
Neil Booth18a9d8f2001-09-16 11:23:56 +0000358
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000359 /* Traditionally, a directive is ignored unless its # is in
360 column 1. Therefore in code intended to work with K+R
361 compilers, directives added by C89 must have their #
362 indented, and directives present in traditional C must not.
363 This is true even of directives in skipped conditional
364 blocks. #elif cannot be used at all. */
365 if (CPP_WTRADITIONAL (pfile))
366 {
367 if (dir == &dtable[T_ELIF])
John David Anglin0527bc42003-11-01 22:56:54 +0000368 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000369 "suggest not using #elif in traditional C");
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000370 else if (indented && dir->origin == KANDR)
John David Anglin0527bc42003-11-01 22:56:54 +0000371 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000372 "traditional C ignores #%s with the # indented",
373 dir->name);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000374 else if (!indented && dir->origin != KANDR)
John David Anglin0527bc42003-11-01 22:56:54 +0000375 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000376 "suggest hiding #%s from traditional C with an indented #",
377 dir->name);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000378 }
379}
380
Kazu Hiratada7d8302002-09-22 02:03:17 +0000381/* Check if we have a known directive. INDENTED is nonzero if the
Neil Booth18a9d8f2001-09-16 11:23:56 +0000382 '#' of the directive was indented. This function is in this file
Gabriel Dos Reisa2566ae2005-01-02 01:32:21 +0000383 to save unnecessarily exporting dtable etc. to lex.c. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +0000384 nonzero if the line of tokens has been handled, zero if we should
Neil Booth18a9d8f2001-09-16 11:23:56 +0000385 continue processing the line. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000386int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000387_cpp_handle_directive (cpp_reader *pfile, int indented)
Neil Boothfe6c2db2000-11-15 19:25:22 +0000388{
Neil Boothfe6c2db2000-11-15 19:25:22 +0000389 const directive *dir = 0;
Neil Booth345894b2001-09-16 13:44:29 +0000390 const cpp_token *dname;
Neil Boothe808ec92002-02-27 07:24:53 +0000391 bool was_parsing_args = pfile->state.parsing_args;
Zack Weinbergc6e83802004-06-05 20:58:06 +0000392 bool was_discarding_output = pfile->state.discarding_output;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000393 int skip = 1;
394
Zack Weinbergc6e83802004-06-05 20:58:06 +0000395 if (was_discarding_output)
396 pfile->state.prevent_expansion = 0;
397
Neil Boothe808ec92002-02-27 07:24:53 +0000398 if (was_parsing_args)
399 {
400 if (CPP_OPTION (pfile, pedantic))
John David Anglin0527bc42003-11-01 22:56:54 +0000401 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothe808ec92002-02-27 07:24:53 +0000402 "embedding a directive within macro arguments is not portable");
403 pfile->state.parsing_args = 0;
404 pfile->state.prevent_expansion = 0;
405 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000406 start_directive (pfile);
Neil Booth345894b2001-09-16 13:44:29 +0000407 dname = _cpp_lex_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000408
Neil Booth345894b2001-09-16 13:44:29 +0000409 if (dname->type == CPP_NAME)
Neil Booth0d9f2342000-09-18 18:43:05 +0000410 {
Zack Weinberg4977bab2002-12-16 18:23:00 +0000411 if (dname->val.node->is_directive)
412 dir = &dtable[dname->val.node->directive_index];
Neil Booth93c803682000-10-28 17:59:06 +0000413 }
Kazu Hirata05713b82002-09-15 18:24:08 +0000414 /* We do not recognize the # followed by a number extension in
Neil Booth18a9d8f2001-09-16 11:23:56 +0000415 assembler code. */
Neil Booth345894b2001-09-16 13:44:29 +0000416 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth93c803682000-10-28 17:59:06 +0000417 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000418 dir = &linemarker_dir;
419 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
420 && ! pfile->state.skipping)
John David Anglin0527bc42003-11-01 22:56:54 +0000421 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothebef4e82002-04-14 18:42:47 +0000422 "style of line directive is a GCC extension");
Neil Booth0d9f2342000-09-18 18:43:05 +0000423 }
424
Neil Booth93c803682000-10-28 17:59:06 +0000425 if (dir)
Neil Booth0d9f2342000-09-18 18:43:05 +0000426 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000427 /* If we have a directive that is not an opening conditional,
428 invalidate any control macro. */
429 if (! (dir->flags & IF_COND))
430 pfile->mi_valid = false;
Neil Booth93c803682000-10-28 17:59:06 +0000431
Neil Booth18a9d8f2001-09-16 11:23:56 +0000432 /* Kluge alert. In order to be sure that code like this
433
434 #define HASH #
435 HASH define foo bar
436
437 does not cause '#define foo bar' to get executed when
438 compiled with -save-temps, we recognize directives in
Gabriel Dos Reisa2566ae2005-01-02 01:32:21 +0000439 -fpreprocessed mode only if the # is in column 1. macro.c
Ollie Wildccfc4c92007-07-30 18:29:20 +0000440 puts a space in front of any '#' at the start of a macro.
441
442 We exclude the -fdirectives-only case because macro expansion
443 has not been performed yet, and block comments can cause spaces
444 to preceed the directive. */
Neil Booth18a9d8f2001-09-16 11:23:56 +0000445 if (CPP_OPTION (pfile, preprocessed)
Ollie Wildccfc4c92007-07-30 18:29:20 +0000446 && !CPP_OPTION (pfile, directives_only)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000447 && (indented || !(dir->flags & IN_I)))
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000448 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000449 skip = 0;
450 dir = 0;
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000451 }
452 else
Neil Booth93c803682000-10-28 17:59:06 +0000453 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000454 /* In failed conditional groups, all non-conditional
455 directives are ignored. Before doing that, whether
456 skipping or not, we should lex angle-bracketed headers
457 correctly, and maybe output some diagnostics. */
458 pfile->state.angled_headers = dir->flags & INCL;
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000459 pfile->state.directive_wants_padding = dir->flags & INCL;
Neil Booth18a9d8f2001-09-16 11:23:56 +0000460 if (! CPP_OPTION (pfile, preprocessed))
461 directive_diagnostics (pfile, dir, indented);
462 if (pfile->state.skipping && !(dir->flags & COND))
463 dir = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000464 }
465 }
Neil Booth345894b2001-09-16 13:44:29 +0000466 else if (dname->type == CPP_EOF)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000467 ; /* CPP_EOF is the "null directive". */
468 else
Neil Booth0d9f2342000-09-18 18:43:05 +0000469 {
Neil Booth93c803682000-10-28 17:59:06 +0000470 /* An unknown directive. Don't complain about it in assembly
471 source: we don't know where the comments are, and # may
472 introduce assembler pseudo-ops. Don't complain about invalid
473 directives in skipped conditional groups (6.10 p4). */
Neil Boothbdb05a72000-11-26 17:31:13 +0000474 if (CPP_OPTION (pfile, lang) == CLK_ASM)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000475 skip = 0;
476 else if (!pfile->state.skipping)
John David Anglin0527bc42003-11-01 22:56:54 +0000477 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
Neil Booth345894b2001-09-16 13:44:29 +0000478 cpp_token_as_text (pfile, dname));
Neil Booth0d9f2342000-09-18 18:43:05 +0000479 }
480
Neil Boothd1a58682002-06-28 06:26:54 +0000481 pfile->directive = dir;
482 if (CPP_OPTION (pfile, traditional))
483 prepare_directive_trad (pfile);
484
Neil Booth18a9d8f2001-09-16 11:23:56 +0000485 if (dir)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000486 pfile->directive->handler (pfile);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000487 else if (skip == 0)
488 _cpp_backup_tokens (pfile, 1);
489
490 end_directive (pfile, skip);
Jakub Jelinek765d6002008-01-25 10:01:27 +0100491 if (was_parsing_args && !pfile->state.in_deferred_pragma)
Neil Boothe808ec92002-02-27 07:24:53 +0000492 {
493 /* Restore state when within macro args. */
494 pfile->state.parsing_args = 2;
495 pfile->state.prevent_expansion = 1;
Neil Boothe808ec92002-02-27 07:24:53 +0000496 }
Zack Weinbergc6e83802004-06-05 20:58:06 +0000497 if (was_discarding_output)
498 pfile->state.prevent_expansion = 1;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000499 return skip;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000500}
501
Neil Booth93c803682000-10-28 17:59:06 +0000502/* Directive handler wrapper used by the command line option
Neil Booth26aea072003-04-19 00:22:51 +0000503 processor. BUF is \n terminated. */
Neil Booth93c803682000-10-28 17:59:06 +0000504static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000505run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000506{
Neil Booth562a5c22002-04-21 18:46:42 +0000507 cpp_push_buffer (pfile, (const uchar *) buf, count,
Per Bothner40de9f72003-10-02 07:30:34 +0000508 /* from_stage3 */ true);
Neil Booth0bda4762000-12-11 07:45:16 +0000509 start_directive (pfile);
Neil Booth26aea072003-04-19 00:22:51 +0000510
511 /* This is a short-term fix to prevent a leading '#' being
512 interpreted as a directive. */
513 _cpp_clean_line (pfile);
514
Neil Boothf71aebb2001-05-27 18:06:00 +0000515 pfile->directive = &dtable[dir_no];
Neil Booth1a769162002-06-11 05:36:17 +0000516 if (CPP_OPTION (pfile, traditional))
517 prepare_directive_trad (pfile);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000518 pfile->directive->handler (pfile);
Neil Booth0bda4762000-12-11 07:45:16 +0000519 end_directive (pfile, 1);
Neil Boothef6e9582001-08-04 12:01:59 +0000520 _cpp_pop_buffer (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000521}
Per Bothner7f2935c1995-03-16 13:59:07 -0800522
Neil Booth93c803682000-10-28 17:59:06 +0000523/* Checks for validity the macro name in #define, #undef, #ifdef and
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000524 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
525 processing a #define or #undefine directive, and false
526 otherwise. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000527static cpp_hashnode *
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000528lex_macro_node (cpp_reader *pfile, bool is_def_or_undef)
Zack Weinberg041c3192000-07-04 01:58:21 +0000529{
Neil Booth1a769162002-06-11 05:36:17 +0000530 const cpp_token *token = _cpp_lex_token (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000531
Zack Weinberg92936ec2000-07-19 20:18:08 +0000532 /* The token immediately after #define must be an identifier. That
Zack Weinbergb8363a22001-07-01 18:48:13 +0000533 identifier may not be "defined", per C99 6.10.8p4.
534 In C++, it may not be any of the "named operators" either,
535 per C++98 [lex.digraph], [lex.key].
536 Finally, the identifier may not have been poisoned. (In that case
Neil Booth1d63a282002-06-28 20:27:14 +0000537 the lexer has issued the error message for us.) */
Zack Weinbergb8363a22001-07-01 18:48:13 +0000538
Neil Booth1a769162002-06-11 05:36:17 +0000539 if (token->type == CPP_NAME)
540 {
541 cpp_hashnode *node = token->val.node;
542
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000543 if (is_def_or_undef && node == pfile->spec_nodes.n_defined)
John David Anglin0527bc42003-11-01 22:56:54 +0000544 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth1a769162002-06-11 05:36:17 +0000545 "\"defined\" cannot be used as a macro name");
546 else if (! (node->flags & NODE_POISONED))
547 return node;
548 }
549 else if (token->flags & NAMED_OP)
John David Anglin0527bc42003-11-01 22:56:54 +0000550 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcbc69f82002-06-05 20:27:12 +0000551 "\"%s\" cannot be used as a macro name as it is an operator in C++",
Neil Booth1a769162002-06-11 05:36:17 +0000552 NODE_NAME (token->val.node));
553 else if (token->type == CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000554 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
Neil Booth1a769162002-06-11 05:36:17 +0000555 pfile->directive->name);
556 else
John David Anglin0527bc42003-11-01 22:56:54 +0000557 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
Zack Weinbergb8363a22001-07-01 18:48:13 +0000558
Neil Boothcbc69f82002-06-05 20:27:12 +0000559 return NULL;
Per Bothner7f2935c1995-03-16 13:59:07 -0800560}
Per Bothner7f2935c1995-03-16 13:59:07 -0800561
Gabriel Dos Reisa2566ae2005-01-02 01:32:21 +0000562/* Process a #define directive. Most work is done in macro.c. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000563static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000564do_define (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800565{
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000566 cpp_hashnode *node = lex_macro_node (pfile, true);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000567
Neil Booth93c803682000-10-28 17:59:06 +0000568 if (node)
569 {
Neil Booth1d63a282002-06-28 20:27:14 +0000570 /* If we have been requested to expand comments into macros,
571 then re-enable saving of comments. */
572 pfile->state.save_comments =
573 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
574
Joseph Myers93d45d92008-04-02 20:42:53 +0100575 if (pfile->cb.before_define)
576 pfile->cb.before_define (pfile);
577
Neil Booth93c803682000-10-28 17:59:06 +0000578 if (_cpp_create_definition (pfile, node))
579 if (pfile->cb.define)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000580 pfile->cb.define (pfile, pfile->directive_line, node);
Joseph Myers93d45d92008-04-02 20:42:53 +0100581
582 node->flags &= ~NODE_USED;
Neil Booth93c803682000-10-28 17:59:06 +0000583 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800584}
585
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000586/* Handle #undef. Mark the identifier NT_VOID in the hash table. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000587static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000588do_undef (cpp_reader *pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000589{
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000590 cpp_hashnode *node = lex_macro_node (pfile, true);
Zack Weinberg041c3192000-07-04 01:58:21 +0000591
Neil Booth45f24922003-12-12 07:00:29 +0000592 if (node)
Zack Weinberg041c3192000-07-04 01:58:21 +0000593 {
Joseph Myers93d45d92008-04-02 20:42:53 +0100594 if (pfile->cb.before_define)
595 pfile->cb.before_define (pfile);
596
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000597 if (pfile->cb.undef)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000598 pfile->cb.undef (pfile, pfile->directive_line, node);
Zack Weinberg041c3192000-07-04 01:58:21 +0000599
Neil Booth45f24922003-12-12 07:00:29 +0000600 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
601 identifier is not currently defined as a macro name. */
602 if (node->type == NT_MACRO)
603 {
604 if (node->flags & NODE_WARN)
605 cpp_error (pfile, CPP_DL_WARNING,
606 "undefining \"%s\"", NODE_NAME (node));
Zack Weinberg041c3192000-07-04 01:58:21 +0000607
Neil Booth45f24922003-12-12 07:00:29 +0000608 if (CPP_OPTION (pfile, warn_unused_macros))
609 _cpp_warn_if_unused_macro (pfile, node, NULL);
Neil Bootha69cbaa2002-07-23 22:57:49 +0000610
Neil Booth45f24922003-12-12 07:00:29 +0000611 _cpp_free_definition (node);
612 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000613 }
Neil Booth45f24922003-12-12 07:00:29 +0000614
Joseph Myersa5cb5632009-04-18 16:28:40 +0100615 check_eol (pfile, false);
Zack Weinberg041c3192000-07-04 01:58:21 +0000616}
617
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000618/* Undefine a single macro/assertion/whatever. */
619
620static int
Zack Weinbergc6e83802004-06-05 20:58:06 +0000621undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000622 void *data_p ATTRIBUTE_UNUSED)
623{
Zack Weinbergc6e83802004-06-05 20:58:06 +0000624 /* Body of _cpp_free_definition inlined here for speed.
625 Macros and assertions no longer have anything to free. */
626 h->type = NT_VOID;
Joseph Myers93d45d92008-04-02 20:42:53 +0100627 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000628 return 1;
629}
630
631/* Undefine all macros and assertions. */
632
633void
634cpp_undef_all (cpp_reader *pfile)
635{
636 cpp_forall_identifiers (pfile, undefine_macros, NULL);
637}
638
639
Neil Booth93c803682000-10-28 17:59:06 +0000640/* Helper routine used by parse_include. Reinterpret the current line
641 as an h-char-sequence (< ... >); we are looking at the first token
Neil Booth74eb4b32003-04-21 19:21:59 +0000642 after the <. Returns a malloced filename. */
643static char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000644glue_header_name (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800645{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000646 const cpp_token *token;
Neil Booth74eb4b32003-04-21 19:21:59 +0000647 char *buffer;
Neil Booth2450e0b2002-02-23 20:21:39 +0000648 size_t len, total_len = 0, capacity = 1024;
Per Bothner7f2935c1995-03-16 13:59:07 -0800649
Neil Booth93c803682000-10-28 17:59:06 +0000650 /* To avoid lexed tokens overwriting our glued name, we can only
651 allocate from the string pool once we've lexed everything. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000652 buffer = XNEWVEC (char, capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000653 for (;;)
Zack Weinberg168d3732000-03-14 06:34:11 +0000654 {
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000655 token = get_token_no_padding (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000656
Neil Booth74eb4b32003-04-21 19:21:59 +0000657 if (token->type == CPP_GREATER)
Neil Booth93c803682000-10-28 17:59:06 +0000658 break;
Neil Booth74eb4b32003-04-21 19:21:59 +0000659 if (token->type == CPP_EOF)
660 {
John David Anglin0527bc42003-11-01 22:56:54 +0000661 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
Neil Booth74eb4b32003-04-21 19:21:59 +0000662 break;
663 }
Neil Booth93c803682000-10-28 17:59:06 +0000664
Neil Booth59325652003-04-24 20:03:57 +0000665 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
Neil Booth2450e0b2002-02-23 20:21:39 +0000666 if (total_len + len > capacity)
Neil Booth93c803682000-10-28 17:59:06 +0000667 {
Neil Booth2450e0b2002-02-23 20:21:39 +0000668 capacity = (capacity + len) * 2;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000669 buffer = XRESIZEVEC (char, buffer, capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000670 }
671
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000672 if (token->flags & PREV_WHITE)
Neil Booth2450e0b2002-02-23 20:21:39 +0000673 buffer[total_len++] = ' ';
Neil Booth93c803682000-10-28 17:59:06 +0000674
Geoffrey Keating47e20492005-03-12 10:44:06 +0000675 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
676 true)
Neil Booth74eb4b32003-04-21 19:21:59 +0000677 - (uchar *) buffer);
Neil Booth93c803682000-10-28 17:59:06 +0000678 }
679
Neil Booth74eb4b32003-04-21 19:21:59 +0000680 buffer[total_len] = '\0';
681 return buffer;
Neil Booth93c803682000-10-28 17:59:06 +0000682}
683
Neil Booth74eb4b32003-04-21 19:21:59 +0000684/* Returns the file name of #include, #include_next, #import and
685 #pragma dependency. The string is malloced and the caller should
686 free it. Returns NULL on error. */
687static const char *
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000688parse_include (cpp_reader *pfile, int *pangle_brackets,
689 const cpp_token ***buf)
Neil Booth93c803682000-10-28 17:59:06 +0000690{
Neil Booth74eb4b32003-04-21 19:21:59 +0000691 char *fname;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000692 const cpp_token *header;
Neil Booth93c803682000-10-28 17:59:06 +0000693
Neil Booth93c803682000-10-28 17:59:06 +0000694 /* Allow macro expansion. */
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000695 header = get_token_no_padding (pfile);
Neil Booth74eb4b32003-04-21 19:21:59 +0000696 if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
Neil Booth93c803682000-10-28 17:59:06 +0000697 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000698 fname = XNEWVEC (char, header->val.str.len - 1);
Neil Booth6338b352003-04-23 22:44:06 +0000699 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
700 fname[header->val.str.len - 2] = '\0';
Neil Booth74eb4b32003-04-21 19:21:59 +0000701 *pangle_brackets = header->type == CPP_HEADER_NAME;
Zack Weinberg041c3192000-07-04 01:58:21 +0000702 }
Neil Booth74eb4b32003-04-21 19:21:59 +0000703 else if (header->type == CPP_LESS)
Zack Weinberg041c3192000-07-04 01:58:21 +0000704 {
Neil Booth74eb4b32003-04-21 19:21:59 +0000705 fname = glue_header_name (pfile);
706 *pangle_brackets = 1;
707 }
708 else
709 {
710 const unsigned char *dir;
711
712 if (pfile->directive == &dtable[T_PRAGMA])
Kris Van Heesb6baa672008-04-18 13:58:08 +0000713 dir = UC"pragma dependency";
Neil Booth74eb4b32003-04-21 19:21:59 +0000714 else
715 dir = pfile->directive->name;
John David Anglin0527bc42003-11-01 22:56:54 +0000716 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
Neil Booth74eb4b32003-04-21 19:21:59 +0000717 dir);
718
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000719 return NULL;
Richard Kennercfb3ee11997-04-13 14:30:13 -0400720 }
721
Tom Tromeycda5e672007-08-18 17:54:11 +0000722 if (pfile->directive == &dtable[T_PRAGMA])
723 {
724 /* This pragma allows extra tokens after the file name. */
725 }
726 else if (buf == NULL || CPP_OPTION (pfile, discard_comments))
Joseph Myers61cc8222009-04-18 21:25:07 +0100727 check_eol (pfile, true);
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000728 else
729 {
730 /* If we are not discarding comments, then gather them while
731 doing the eol check. */
732 *buf = check_eol_return_comments (pfile);
733 }
734
Neil Booth74eb4b32003-04-21 19:21:59 +0000735 return fname;
Zack Weinberg168d3732000-03-14 06:34:11 +0000736}
737
Neil Boothba133c92001-03-15 07:57:13 +0000738/* Handle #include, #include_next and #import. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000739static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000740do_include_common (cpp_reader *pfile, enum include_type type)
Zack Weinberg168d3732000-03-14 06:34:11 +0000741{
Neil Booth74eb4b32003-04-21 19:21:59 +0000742 const char *fname;
743 int angle_brackets;
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000744 const cpp_token **buf = NULL;
Neil Booth74eb4b32003-04-21 19:21:59 +0000745
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000746 /* Re-enable saving of comments if requested, so that the include
747 callback can dump comments which follow #include. */
748 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
749
750 fname = parse_include (pfile, &angle_brackets, &buf);
Neil Booth74eb4b32003-04-21 19:21:59 +0000751 if (!fname)
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000752 {
753 if (buf)
754 XDELETEVEC (buf);
755 return;
756 }
Zack Weinberg168d3732000-03-14 06:34:11 +0000757
Nathanael Nerode28303822004-11-28 22:28:13 +0000758 if (!*fname)
759 {
760 cpp_error (pfile, CPP_DL_ERROR, "empty filename in #%s",
761 pfile->directive->name);
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000762 XDELETEVEC (fname);
763 if (buf)
764 XDELETEVEC (buf);
Nathanael Nerode28303822004-11-28 22:28:13 +0000765 return;
766 }
767
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000768 /* Prevent #include recursion. */
Per Bothner50f59cd2004-01-19 21:30:18 -0800769 if (pfile->line_table->depth >= CPP_STACK_MAX)
John David Anglin0527bc42003-11-01 22:56:54 +0000770 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
Neil Booth74eb4b32003-04-21 19:21:59 +0000771 else
Neil Booth09b82252001-07-29 22:27:20 +0000772 {
Neil Booth74eb4b32003-04-21 19:21:59 +0000773 /* Get out of macro context, if we are. */
774 skip_rest_of_line (pfile);
775
776 if (pfile->cb.include)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000777 pfile->cb.include (pfile, pfile->directive_line,
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000778 pfile->directive->name, fname, angle_brackets,
779 buf);
Neil Booth74eb4b32003-04-21 19:21:59 +0000780
Neil Booth8f9b4002003-07-29 22:26:13 +0000781 _cpp_stack_include (pfile, fname, angle_brackets, type);
Neil Booth09b82252001-07-29 22:27:20 +0000782 }
783
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000784 XDELETEVEC (fname);
785 if (buf)
786 XDELETEVEC (buf);
Neil Boothba133c92001-03-15 07:57:13 +0000787}
788
789static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000790do_include (cpp_reader *pfile)
Neil Boothba133c92001-03-15 07:57:13 +0000791{
792 do_include_common (pfile, IT_INCLUDE);
Zack Weinberg168d3732000-03-14 06:34:11 +0000793}
794
Zack Weinberg711b8822000-07-18 00:59:49 +0000795static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000796do_import (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +0000797{
Neil Boothba133c92001-03-15 07:57:13 +0000798 do_include_common (pfile, IT_IMPORT);
Zack Weinberg168d3732000-03-14 06:34:11 +0000799}
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000800
Zack Weinberg711b8822000-07-18 00:59:49 +0000801static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000802do_include_next (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +0000803{
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000804 enum include_type type = IT_INCLUDE_NEXT;
805
806 /* If this is the primary source file, warn and use the normal
807 search logic. */
Tom Tromey705e2d22007-01-04 15:32:26 +0000808 if (cpp_in_primary_file (pfile))
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000809 {
John David Anglin0527bc42003-11-01 22:56:54 +0000810 cpp_error (pfile, CPP_DL_WARNING,
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000811 "#include_next in primary source file");
812 type = IT_INCLUDE;
813 }
814 do_include_common (pfile, type);
Per Bothner7f2935c1995-03-16 13:59:07 -0800815}
816
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000817/* Subroutine of do_linemarker. Read possible flags after file name.
818 LAST is the last flag seen; 0 if this is the first flag. Return the
819 flag if it is valid, 0 at the end of the directive. Otherwise
820 complain. */
Neil Booth642ce432000-12-07 23:17:56 +0000821static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000822read_flag (cpp_reader *pfile, unsigned int last)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000823{
Neil Booth345894b2001-09-16 13:44:29 +0000824 const cpp_token *token = _cpp_lex_token (pfile);
Jason Merrilld3a34a01999-08-14 00:42:07 +0000825
Neil Booth345894b2001-09-16 13:44:29 +0000826 if (token->type == CPP_NUMBER && token->val.str.len == 1)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000827 {
Neil Booth345894b2001-09-16 13:44:29 +0000828 unsigned int flag = token->val.str.text[0] - '0';
Neil Booth28e0f042000-12-09 12:06:37 +0000829
830 if (flag > last && flag <= 4
831 && (flag != 4 || last == 3)
832 && (flag != 2 || last == 0))
833 return flag;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000834 }
Neil Booth93c803682000-10-28 17:59:06 +0000835
Neil Booth345894b2001-09-16 13:44:29 +0000836 if (token->type != CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000837 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
Neil Booth345894b2001-09-16 13:44:29 +0000838 cpp_token_as_text (pfile, token));
Neil Booth93c803682000-10-28 17:59:06 +0000839 return 0;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000840}
841
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000842/* Subroutine of do_line and do_linemarker. Convert a number in STR,
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000843 of length LEN, to binary; store it in NUMP, and return false if the
844 number was well-formed, true if not. WRAPPED is set to true if the
845 number did not fit into 'unsigned long'. */
846static bool
847strtolinenum (const uchar *str, size_t len, linenum_type *nump, bool *wrapped)
Zack Weinberg041c3192000-07-04 01:58:21 +0000848{
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000849 linenum_type reg = 0;
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000850 linenum_type reg_prev = 0;
851
Neil Booth562a5c22002-04-21 18:46:42 +0000852 uchar c;
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000853 *wrapped = false;
Zack Weinberg041c3192000-07-04 01:58:21 +0000854 while (len--)
855 {
856 c = *str++;
857 if (!ISDIGIT (c))
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000858 return true;
Zack Weinberg041c3192000-07-04 01:58:21 +0000859 reg *= 10;
860 reg += c - '0';
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000861 if (reg < reg_prev)
862 *wrapped = true;
863 reg_prev = reg;
Zack Weinberg041c3192000-07-04 01:58:21 +0000864 }
865 *nump = reg;
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000866 return false;
Zack Weinberg041c3192000-07-04 01:58:21 +0000867}
868
Zack Weinberg5538ada1999-02-04 06:36:54 -0500869/* Interpret #line command.
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000870 Note that the filename string (if any) is a true string constant
871 (escapes are interpreted), unlike in #line. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000872static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000873do_line (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800874{
Per Bothner500bee02004-04-22 19:22:27 -0700875 const struct line_maps *line_table = pfile->line_table;
876 const struct line_map *map = &line_table->maps[line_table->used - 1];
Devang Patel2203a882005-02-28 11:04:19 -0800877
878 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
879 sysp right now. */
880
881 unsigned char map_sysp = map->sysp;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000882 const cpp_token *token;
Per Bothner12f9df42004-02-11 07:29:30 -0800883 const char *new_file = map->to_file;
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000884 linenum_type new_lineno;
Per Bothner7f2935c1995-03-16 13:59:07 -0800885
Neil Booth27e25642000-11-27 08:00:04 +0000886 /* C99 raised the minimum limit on #line numbers. */
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000887 linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000888 bool wrapped;
Neil Booth18a9d8f2001-09-16 11:23:56 +0000889
Neil Booth93c803682000-10-28 17:59:06 +0000890 /* #line commands expand macros. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000891 token = cpp_get_token (pfile);
892 if (token->type != CPP_NUMBER
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000893 || strtolinenum (token->val.str.text, token->val.str.len,
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000894 &new_lineno, &wrapped))
Per Bothner7f2935c1995-03-16 13:59:07 -0800895 {
Tom Tromey33ae4832008-01-03 17:58:26 +0000896 if (token->type == CPP_EOF)
897 cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line");
898 else
899 cpp_error (pfile, CPP_DL_ERROR,
900 "\"%s\" after #line is not a positive integer",
901 cpp_token_as_text (pfile, token));
Zack Weinberg9ec72912000-08-09 19:41:12 +0000902 return;
Kazu Hiratadf383482002-05-22 22:02:16 +0000903 }
Zack Weinberg5538ada1999-02-04 06:36:54 -0500904
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000905 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap || wrapped))
John David Anglin0527bc42003-11-01 22:56:54 +0000906 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000907 else if (wrapped)
908 cpp_error (pfile, CPP_DL_WARNING, "line number out of range");
Zack Weinberg5538ada1999-02-04 06:36:54 -0500909
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000910 token = cpp_get_token (pfile);
911 if (token->type == CPP_STRING)
Zack Weinberg5538ada1999-02-04 06:36:54 -0500912 {
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000913 cpp_string s = { 0, 0 };
Eric Christopher423e95e2004-02-12 02:25:03 +0000914 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
915 &s, false))
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000916 new_file = (const char *)s.text;
Joseph Myersa5cb5632009-04-18 16:28:40 +0100917 check_eol (pfile, true);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000918 }
919 else if (token->type != CPP_EOF)
920 {
John David Anglin0527bc42003-11-01 22:56:54 +0000921 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000922 cpp_token_as_text (pfile, token));
923 return;
924 }
Neil Booth93c803682000-10-28 17:59:06 +0000925
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000926 skip_rest_of_line (pfile);
Joseph Myersc7f9c0b2009-04-18 18:36:28 +0100927 _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, new_file, new_lineno,
Devang Patel2203a882005-02-28 11:04:19 -0800928 map_sysp);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000929}
930
931/* Interpret the # 44 "file" [flags] notation, which has slightly
932 different syntax and semantics from #line: Flags are allowed,
933 and we never complain about the line number being too big. */
934static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000935do_linemarker (cpp_reader *pfile)
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000936{
Per Bothner500bee02004-04-22 19:22:27 -0700937 const struct line_maps *line_table = pfile->line_table;
938 const struct line_map *map = &line_table->maps[line_table->used - 1];
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000939 const cpp_token *token;
Per Bothner12f9df42004-02-11 07:29:30 -0800940 const char *new_file = map->to_file;
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000941 linenum_type new_lineno;
Per Bothner12f9df42004-02-11 07:29:30 -0800942 unsigned int new_sysp = map->sysp;
Joseph Myersc7f9c0b2009-04-18 18:36:28 +0100943 enum lc_reason reason = LC_RENAME_VERBATIM;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000944 int flag;
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000945 bool wrapped;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000946
947 /* Back up so we can get the number again. Putting this in
948 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
949 some circumstances, which can segfault. */
950 _cpp_backup_tokens (pfile, 1);
951
952 /* #line commands expand macros. */
953 token = cpp_get_token (pfile);
954 if (token->type != CPP_NUMBER
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000955 || strtolinenum (token->val.str.text, token->val.str.len,
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000956 &new_lineno, &wrapped))
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000957 {
Tom Tromey33ae4832008-01-03 17:58:26 +0000958 /* Unlike #line, there does not seem to be a way to get an EOF
959 here. So, it should be safe to always spell the token. */
John David Anglin0527bc42003-11-01 22:56:54 +0000960 cpp_error (pfile, CPP_DL_ERROR,
961 "\"%s\" after # is not a positive integer",
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000962 cpp_token_as_text (pfile, token));
963 return;
Kazu Hiratadf383482002-05-22 22:02:16 +0000964 }
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000965
966 token = cpp_get_token (pfile);
967 if (token->type == CPP_STRING)
968 {
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000969 cpp_string s = { 0, 0 };
Eric Christopher423e95e2004-02-12 02:25:03 +0000970 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
971 1, &s, false))
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000972 new_file = (const char *)s.text;
Eric Christophercf551fb2004-01-16 22:37:49 +0000973
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000974 new_sysp = 0;
975 flag = read_flag (pfile, 0);
976 if (flag == 1)
Neil Booth93c803682000-10-28 17:59:06 +0000977 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000978 reason = LC_ENTER;
979 /* Fake an include for cpp_included (). */
980 _cpp_fake_include (pfile, new_file);
981 flag = read_flag (pfile, flag);
Neil Booth93c803682000-10-28 17:59:06 +0000982 }
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000983 else if (flag == 2)
984 {
985 reason = LC_LEAVE;
986 flag = read_flag (pfile, flag);
987 }
988 if (flag == 3)
989 {
990 new_sysp = 1;
991 flag = read_flag (pfile, flag);
992 if (flag == 4)
993 new_sysp = 2;
994 }
Jakub Jelinek9d30f272006-12-29 09:15:08 +0100995 pfile->buffer->sysp = new_sysp;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000996
Joseph Myersa5cb5632009-04-18 16:28:40 +0100997 check_eol (pfile, false);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000998 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000999 else if (token->type != CPP_EOF)
Neil Booth27e25642000-11-27 08:00:04 +00001000 {
John David Anglin0527bc42003-11-01 22:56:54 +00001001 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001002 cpp_token_as_text (pfile, token));
Neil Booth27e25642000-11-27 08:00:04 +00001003 return;
1004 }
Zack Weinberg941e09b1998-12-15 11:17:06 +00001005
Neil Boothbdcbe492001-09-13 20:05:17 +00001006 skip_rest_of_line (pfile);
Neil Boothbb74c962001-08-17 22:23:49 +00001007 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
Neil Booth27e25642000-11-27 08:00:04 +00001008}
1009
Neil Booth67821e32001-08-05 17:31:25 +00001010/* Arrange the file_change callback. pfile->line has changed to
Neil Booth47d89cf2001-08-11 07:33:39 +00001011 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
Joseph Myersa1f300c2001-11-23 02:05:19 +00001012 header, 2 for a system header that needs to be extern "C" protected,
Neil Booth47d89cf2001-08-11 07:33:39 +00001013 and zero otherwise. */
Neil Bootheb1f4d92000-12-18 19:00:26 +00001014void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001015_cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +00001016 const char *to_file, linenum_type file_line,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001017 unsigned int sysp)
Neil Booth27e25642000-11-27 08:00:04 +00001018{
Per Bothner12f9df42004-02-11 07:29:30 -08001019 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
1020 to_file, file_line);
Per Bothner500bee02004-04-22 19:22:27 -07001021 if (map != NULL)
1022 linemap_line_start (pfile->line_table, map->to_line, 127);
Neil Boothd82fc102001-08-02 23:03:31 +00001023
Neil Bootheb1f4d92000-12-18 19:00:26 +00001024 if (pfile->cb.file_change)
Per Bothner12f9df42004-02-11 07:29:30 -08001025 pfile->cb.file_change (pfile, map);
Per Bothner7f2935c1995-03-16 13:59:07 -08001026}
Zack Weinberg941e09b1998-12-15 11:17:06 +00001027
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001028/* Report a warning or error detected by the program we are
1029 processing. Use the directive's tokens in the error message. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001030static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001031do_diagnostic (cpp_reader *pfile, int code, int print_dir)
Per Bothner7f2935c1995-03-16 13:59:07 -08001032{
Tom Tromey5d6342e2008-05-21 21:52:57 +00001033 const unsigned char *dir_name;
1034 unsigned char *line;
1035 source_location src_loc = pfile->cur_token[-1].src_loc;
1036
1037 if (print_dir)
1038 dir_name = pfile->directive->name;
1039 else
1040 dir_name = NULL;
1041 pfile->state.prevent_expansion++;
1042 line = cpp_output_line_to_string (pfile, dir_name);
1043 pfile->state.prevent_expansion--;
1044
1045 cpp_error_with_line (pfile, code, src_loc, 0, "%s", line);
1046 free (line);
Per Bothner7f2935c1995-03-16 13:59:07 -08001047}
1048
Neil Booth838f3132000-09-24 10:42:09 +00001049static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001050do_error (cpp_reader *pfile)
Neil Booth838f3132000-09-24 10:42:09 +00001051{
John David Anglin0527bc42003-11-01 22:56:54 +00001052 do_diagnostic (pfile, CPP_DL_ERROR, 1);
Neil Booth838f3132000-09-24 10:42:09 +00001053}
Per Bothner7f2935c1995-03-16 13:59:07 -08001054
Zack Weinberg711b8822000-07-18 00:59:49 +00001055static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001056do_warning (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001057{
Neil Booth2f878972001-04-08 10:01:18 +00001058 /* We want #warning diagnostics to be emitted in system headers too. */
John David Anglin0527bc42003-11-01 22:56:54 +00001059 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
Per Bothner7f2935c1995-03-16 13:59:07 -08001060}
1061
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001062/* Report program identification. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001063static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001064do_ident (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001065{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001066 const cpp_token *str = cpp_get_token (pfile);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001067
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001068 if (str->type != CPP_STRING)
Zack Weinberg1ed17cd2005-05-12 18:31:38 +00001069 cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
1070 pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +00001071 else if (pfile->cb.ident)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001072 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001073
Joseph Myersa5cb5632009-04-18 16:28:40 +01001074 check_eol (pfile, false);
Per Bothner7f2935c1995-03-16 13:59:07 -08001075}
1076
Neil Bootha5da89c2001-10-14 17:44:00 +00001077/* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1078 matching entry, or NULL if none is found. The returned entry could
1079 be the start of a namespace chain, or a pragma. */
1080static struct pragma_entry *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001081lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
Nathan Sidwell82443372000-06-23 10:56:09 +00001082{
Neil Booth4b115ff2001-10-14 23:04:13 +00001083 while (chain && chain->pragma != pragma)
1084 chain = chain->next;
Neil Bootha5da89c2001-10-14 17:44:00 +00001085
1086 return chain;
1087}
1088
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001089/* Create and insert a blank pragma entry at the beginning of a
1090 singly-linked CHAIN. */
Neil Bootha5da89c2001-10-14 17:44:00 +00001091static struct pragma_entry *
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001092new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain)
Neil Bootha5da89c2001-10-14 17:44:00 +00001093{
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001094 struct pragma_entry *new_entry;
Neil Bootha5da89c2001-10-14 17:44:00 +00001095
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001096 new_entry = (struct pragma_entry *)
Neil Bootha5da89c2001-10-14 17:44:00 +00001097 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
Neil Bootha5da89c2001-10-14 17:44:00 +00001098
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001099 memset (new_entry, 0, sizeof (struct pragma_entry));
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001100 new_entry->next = *chain;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001101
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001102 *chain = new_entry;
1103 return new_entry;
Neil Bootha5da89c2001-10-14 17:44:00 +00001104}
1105
1106/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001107 goes in the global namespace. */
1108static struct pragma_entry *
1109register_pragma_1 (cpp_reader *pfile, const char *space, const char *name,
1110 bool allow_name_expansion)
Nathan Sidwell82443372000-06-23 10:56:09 +00001111{
Neil Bootha5da89c2001-10-14 17:44:00 +00001112 struct pragma_entry **chain = &pfile->pragmas;
1113 struct pragma_entry *entry;
Neil Booth4b115ff2001-10-14 23:04:13 +00001114 const cpp_hashnode *node;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001115
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001116 if (space)
1117 {
Kris Van Heesb6baa672008-04-18 13:58:08 +00001118 node = cpp_lookup (pfile, UC space, strlen (space));
Neil Booth4b115ff2001-10-14 23:04:13 +00001119 entry = lookup_pragma_entry (*chain, node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001120 if (!entry)
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001121 {
1122 entry = new_pragma_entry (pfile, chain);
1123 entry->pragma = node;
1124 entry->is_nspace = true;
1125 entry->allow_expansion = allow_name_expansion;
1126 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001127 else if (!entry->is_nspace)
1128 goto clash;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001129 else if (entry->allow_expansion != allow_name_expansion)
1130 {
1131 cpp_error (pfile, CPP_DL_ICE,
1132 "registering pragmas in namespace \"%s\" with mismatched "
1133 "name expansion", space);
1134 return NULL;
1135 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001136 chain = &entry->u.space;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001137 }
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001138 else if (allow_name_expansion)
1139 {
1140 cpp_error (pfile, CPP_DL_ICE,
1141 "registering pragma \"%s\" with name expansion "
1142 "and no namespace", name);
1143 return NULL;
1144 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001145
Neil Bootha5da89c2001-10-14 17:44:00 +00001146 /* Check for duplicates. */
Kris Van Heesb6baa672008-04-18 13:58:08 +00001147 node = cpp_lookup (pfile, UC name, strlen (name));
Neil Booth4b115ff2001-10-14 23:04:13 +00001148 entry = lookup_pragma_entry (*chain, node);
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001149 if (entry == NULL)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001150 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001151 entry = new_pragma_entry (pfile, chain);
1152 entry->pragma = node;
1153 return entry;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001154 }
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001155
1156 if (entry->is_nspace)
1157 clash:
1158 cpp_error (pfile, CPP_DL_ICE,
1159 "registering \"%s\" as both a pragma and a pragma namespace",
1160 NODE_NAME (node));
1161 else if (space)
1162 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1163 space, name);
Neil Bootha5da89c2001-10-14 17:44:00 +00001164 else
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001165 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1166
1167 return NULL;
1168}
1169
1170/* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1171static void
1172register_pragma_internal (cpp_reader *pfile, const char *space,
1173 const char *name, pragma_cb handler)
1174{
1175 struct pragma_entry *entry;
1176
1177 entry = register_pragma_1 (pfile, space, name, false);
1178 entry->is_internal = true;
1179 entry->u.handler = handler;
Zack Weinberg21b11492004-09-09 19:16:56 +00001180}
1181
1182/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1183 goes in the global namespace. HANDLER is the handler it will call,
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001184 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1185 expansion while parsing pragma NAME. This function is exported
1186 from libcpp. */
Zack Weinberg21b11492004-09-09 19:16:56 +00001187void
1188cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001189 pragma_cb handler, bool allow_expansion)
Zack Weinberg21b11492004-09-09 19:16:56 +00001190{
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001191 struct pragma_entry *entry;
1192
1193 if (!handler)
1194 {
1195 cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler");
1196 return;
1197 }
1198
1199 entry = register_pragma_1 (pfile, space, name, false);
1200 if (entry)
1201 {
1202 entry->allow_expansion = allow_expansion;
1203 entry->u.handler = handler;
1204 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001205}
Neil Bootha5da89c2001-10-14 17:44:00 +00001206
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001207/* Similarly, but create mark the pragma for deferred processing.
1208 When found, a CPP_PRAGMA token will be insertted into the stream
1209 with IDENT in the token->u.pragma slot. */
1210void
1211cpp_register_deferred_pragma (cpp_reader *pfile, const char *space,
1212 const char *name, unsigned int ident,
1213 bool allow_expansion, bool allow_name_expansion)
1214{
1215 struct pragma_entry *entry;
1216
1217 entry = register_pragma_1 (pfile, space, name, allow_name_expansion);
1218 if (entry)
1219 {
1220 entry->is_deferred = true;
1221 entry->allow_expansion = allow_expansion;
1222 entry->u.ident = ident;
1223 }
1224}
1225
Neil Bootha5da89c2001-10-14 17:44:00 +00001226/* Register the pragmas the preprocessor itself handles. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001227void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001228_cpp_init_internal_pragmas (cpp_reader *pfile)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001229{
Neil Bootha5da89c2001-10-14 17:44:00 +00001230 /* Pragmas in the global namespace. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001231 register_pragma_internal (pfile, 0, "once", do_pragma_once);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001232
Neil Bootha5da89c2001-10-14 17:44:00 +00001233 /* New GCC-specific pragmas should be put in the GCC namespace. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001234 register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison);
1235 register_pragma_internal (pfile, "GCC", "system_header",
1236 do_pragma_system_header);
1237 register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
Nathan Sidwell82443372000-06-23 10:56:09 +00001238}
Per Bothner7f2935c1995-03-16 13:59:07 -08001239
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001240/* Return the number of registered pragmas in PE. */
1241
1242static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001243count_registered_pragmas (struct pragma_entry *pe)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001244{
1245 int ct = 0;
1246 for (; pe != NULL; pe = pe->next)
1247 {
1248 if (pe->is_nspace)
1249 ct += count_registered_pragmas (pe->u.space);
1250 ct++;
1251 }
1252 return ct;
1253}
1254
1255/* Save into SD the names of the registered pragmas referenced by PE,
1256 and return a pointer to the next free space in SD. */
1257
1258static char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001259save_registered_pragmas (struct pragma_entry *pe, char **sd)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001260{
1261 for (; pe != NULL; pe = pe->next)
1262 {
1263 if (pe->is_nspace)
1264 sd = save_registered_pragmas (pe->u.space, sd);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001265 *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
1266 HT_LEN (&pe->pragma->ident),
1267 HT_LEN (&pe->pragma->ident) + 1);
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001268 }
1269 return sd;
1270}
1271
1272/* Return a newly-allocated array which saves the names of the
1273 registered pragmas. */
1274
1275char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001276_cpp_save_pragma_names (cpp_reader *pfile)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001277{
1278 int ct = count_registered_pragmas (pfile->pragmas);
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001279 char **result = XNEWVEC (char *, ct);
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001280 (void) save_registered_pragmas (pfile->pragmas, result);
1281 return result;
1282}
1283
1284/* Restore from SD the names of the registered pragmas referenced by PE,
1285 and return a pointer to the next unused name in SD. */
1286
1287static char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001288restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1289 char **sd)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001290{
1291 for (; pe != NULL; pe = pe->next)
1292 {
1293 if (pe->is_nspace)
1294 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
Kris Van Heesb6baa672008-04-18 13:58:08 +00001295 pe->pragma = cpp_lookup (pfile, UC *sd, strlen (*sd));
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001296 free (*sd);
1297 sd++;
1298 }
1299 return sd;
1300}
1301
1302/* Restore the names of the registered pragmas from SAVED. */
1303
1304void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001305_cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001306{
1307 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1308 free (saved);
1309}
1310
Neil Bootha5da89c2001-10-14 17:44:00 +00001311/* Pragmata handling. We handle some, and pass the rest on to the
1312 front end. C99 defines three pragmas and says that no macro
1313 expansion is to be performed on them; whether or not macro
1314 expansion happens for other pragmas is implementation defined.
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001315 This implementation allows for a mix of both, since GCC did not
1316 traditionally macro expand its (few) pragmas, whereas OpenMP
1317 specifies that macro expansion should happen. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001318static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001319do_pragma (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001320{
Neil Bootha5da89c2001-10-14 17:44:00 +00001321 const struct pragma_entry *p = NULL;
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001322 const cpp_token *token, *pragma_token = pfile->cur_token;
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001323 cpp_token ns_token;
Neil Bootha5da89c2001-10-14 17:44:00 +00001324 unsigned int count = 1;
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001325
Neil Booth93c803682000-10-28 17:59:06 +00001326 pfile->state.prevent_expansion++;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001327
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001328 token = cpp_get_token (pfile);
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001329 ns_token = *token;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001330 if (token->type == CPP_NAME)
Alexandre Oliva0172e2b2000-02-27 06:24:27 +00001331 {
Neil Booth4b115ff2001-10-14 23:04:13 +00001332 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001333 if (p && p->is_nspace)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001334 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001335 bool allow_name_expansion = p->allow_expansion;
1336 if (allow_name_expansion)
1337 pfile->state.prevent_expansion--;
Neil Bootha5da89c2001-10-14 17:44:00 +00001338 token = cpp_get_token (pfile);
1339 if (token->type == CPP_NAME)
Neil Booth4b115ff2001-10-14 23:04:13 +00001340 p = lookup_pragma_entry (p->u.space, token->val.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001341 else
1342 p = NULL;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001343 if (allow_name_expansion)
1344 pfile->state.prevent_expansion++;
1345 count = 2;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001346 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001347 }
1348
Zack Weinberg3da3d582004-10-27 17:29:29 +00001349 if (p)
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001350 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001351 if (p->is_deferred)
Zack Weinberg3da3d582004-10-27 17:29:29 +00001352 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001353 pfile->directive_result.src_loc = pragma_token->src_loc;
1354 pfile->directive_result.type = CPP_PRAGMA;
1355 pfile->directive_result.flags = pragma_token->flags;
1356 pfile->directive_result.val.pragma = p->u.ident;
1357 pfile->state.in_deferred_pragma = true;
1358 pfile->state.pragma_allow_expansion = p->allow_expansion;
1359 if (!p->allow_expansion)
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001360 pfile->state.prevent_expansion++;
Zack Weinberg3da3d582004-10-27 17:29:29 +00001361 }
1362 else
1363 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001364 /* Since the handler below doesn't get the line number, that
1365 it might need for diagnostics, make sure it has the right
1366 numbers in place. */
1367 if (pfile->cb.line_change)
1368 (*pfile->cb.line_change) (pfile, pragma_token, false);
1369 if (p->allow_expansion)
1370 pfile->state.prevent_expansion--;
1371 (*p->u.handler) (pfile);
1372 if (p->allow_expansion)
1373 pfile->state.prevent_expansion++;
Zack Weinberg3da3d582004-10-27 17:29:29 +00001374 }
Zack Weinberg21b11492004-09-09 19:16:56 +00001375 }
Neil Boothd82fc102001-08-02 23:03:31 +00001376 else if (pfile->cb.def_pragma)
Neil Boothbdcbe492001-09-13 20:05:17 +00001377 {
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001378 if (count == 1 || pfile->context->prev == NULL)
1379 _cpp_backup_tokens (pfile, count);
1380 else
1381 {
1382 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1383 won't allow backing 2 tokens. */
1384 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1385 reads both tokens, we could perhaps free it, but if it doesn't,
1386 we don't know the exact lifespan. */
1387 cpp_token *toks = XNEWVEC (cpp_token, 2);
1388 toks[0] = ns_token;
1389 toks[0].flags |= NO_EXPAND;
1390 toks[1] = *token;
1391 toks[1].flags |= NO_EXPAND;
1392 _cpp_push_token_context (pfile, NULL, toks, 2);
1393 }
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001394 pfile->cb.def_pragma (pfile, pfile->directive_line);
Neil Boothbdcbe492001-09-13 20:05:17 +00001395 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001396
Neil Booth97293892001-09-14 22:04:46 +00001397 pfile->state.prevent_expansion--;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001398}
1399
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001400/* Handle #pragma once. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001401static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001402do_pragma_once (cpp_reader *pfile)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001403{
Tom Tromey705e2d22007-01-04 15:32:26 +00001404 if (cpp_in_primary_file (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +00001405 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
Neil Booth93c803682000-10-28 17:59:06 +00001406
Joseph Myersa5cb5632009-04-18 16:28:40 +01001407 check_eol (pfile, false);
Neil Booth49634b32003-08-02 16:29:46 +00001408 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001409}
1410
Neil Boothc3bf3e62002-05-09 17:14:22 +00001411/* Handle #pragma GCC poison, to poison one or more identifiers so
1412 that the lexer produces a hard error for each subsequent usage. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001413static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001414do_pragma_poison (cpp_reader *pfile)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001415{
Neil Booth345894b2001-09-16 13:44:29 +00001416 const cpp_token *tok;
Zack Weinbergf8f769e2000-05-28 05:56:38 +00001417 cpp_hashnode *hp;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001418
Neil Booth93c803682000-10-28 17:59:06 +00001419 pfile->state.poisoned_ok = 1;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001420 for (;;)
1421 {
Neil Booth345894b2001-09-16 13:44:29 +00001422 tok = _cpp_lex_token (pfile);
1423 if (tok->type == CPP_EOF)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001424 break;
Neil Booth345894b2001-09-16 13:44:29 +00001425 if (tok->type != CPP_NAME)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001426 {
John David Anglin0527bc42003-11-01 22:56:54 +00001427 cpp_error (pfile, CPP_DL_ERROR,
1428 "invalid #pragma GCC poison directive");
Neil Booth93c803682000-10-28 17:59:06 +00001429 break;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001430 }
1431
Neil Booth345894b2001-09-16 13:44:29 +00001432 hp = tok->val.node;
Neil Booth93c803682000-10-28 17:59:06 +00001433 if (hp->flags & NODE_POISONED)
1434 continue;
1435
1436 if (hp->type == NT_MACRO)
John David Anglin0527bc42003-11-01 22:56:54 +00001437 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
Neil Boothebef4e82002-04-14 18:42:47 +00001438 NODE_NAME (hp));
Neil Booth93c803682000-10-28 17:59:06 +00001439 _cpp_free_definition (hp);
1440 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001441 }
Neil Booth93c803682000-10-28 17:59:06 +00001442 pfile->state.poisoned_ok = 0;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001443}
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001444
1445/* Mark the current header as a system header. This will suppress
1446 some categories of warnings (notably those from -pedantic). It is
1447 intended for use in system libraries that cannot be implemented in
1448 conforming C, but cannot be certain that their headers appear in a
1449 system include directory. To prevent abuse, it is rejected in the
1450 primary source file. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001451static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001452do_pragma_system_header (cpp_reader *pfile)
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001453{
Tom Tromey705e2d22007-01-04 15:32:26 +00001454 if (cpp_in_primary_file (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +00001455 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +00001456 "#pragma system_header ignored outside include file");
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001457 else
Neil Boothd82fc102001-08-02 23:03:31 +00001458 {
Joseph Myersa5cb5632009-04-18 16:28:40 +01001459 check_eol (pfile, false);
Neil Boothbdcbe492001-09-13 20:05:17 +00001460 skip_rest_of_line (pfile);
Neil Boothd82fc102001-08-02 23:03:31 +00001461 cpp_make_system_header (pfile, 1, 0);
1462 }
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001463}
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001464
1465/* Check the modified date of the current include file against a specified
1466 file. Issue a diagnostic, if the specified file is newer. We use this to
1467 determine if a fixed header should be refixed. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001468static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001469do_pragma_dependency (cpp_reader *pfile)
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001470{
Neil Booth74eb4b32003-04-21 19:21:59 +00001471 const char *fname;
1472 int angle_brackets, ordering;
Kazu Hiratadf383482002-05-22 22:02:16 +00001473
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +00001474 fname = parse_include (pfile, &angle_brackets, NULL);
Neil Booth74eb4b32003-04-21 19:21:59 +00001475 if (!fname)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001476 return;
Zack Weinberg041c3192000-07-04 01:58:21 +00001477
Neil Booth74eb4b32003-04-21 19:21:59 +00001478 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001479 if (ordering < 0)
John David Anglin0527bc42003-11-01 22:56:54 +00001480 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001481 else if (ordering > 0)
1482 {
John David Anglin0527bc42003-11-01 22:56:54 +00001483 cpp_error (pfile, CPP_DL_WARNING,
1484 "current file is older than %s", fname);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001485 if (cpp_get_token (pfile)->type != CPP_EOF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001486 {
1487 _cpp_backup_tokens (pfile, 1);
John David Anglin0527bc42003-11-01 22:56:54 +00001488 do_diagnostic (pfile, CPP_DL_WARNING, 0);
Neil Boothbdcbe492001-09-13 20:05:17 +00001489 }
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001490 }
Neil Booth74eb4b32003-04-21 19:21:59 +00001491
Kaveh R. Ghazifad205f2003-06-16 21:41:10 +00001492 free ((void *) fname);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001493}
1494
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001495/* Get a token but skip padding. */
1496static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001497get_token_no_padding (cpp_reader *pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001498{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001499 for (;;)
1500 {
1501 const cpp_token *result = cpp_get_token (pfile);
1502 if (result->type != CPP_PADDING)
1503 return result;
1504 }
1505}
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001506
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001507/* Check syntax is "(string-literal)". Returns the string on success,
1508 or NULL on failure. */
1509static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001510get__Pragma_string (cpp_reader *pfile)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001511{
1512 const cpp_token *string;
Tom Tromey5b9a40d2007-10-31 14:50:13 +00001513 const cpp_token *paren;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001514
Tom Tromey5b9a40d2007-10-31 14:50:13 +00001515 paren = get_token_no_padding (pfile);
1516 if (paren->type == CPP_EOF)
1517 _cpp_backup_tokens (pfile, 1);
1518 if (paren->type != CPP_OPEN_PAREN)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001519 return NULL;
1520
1521 string = get_token_no_padding (pfile);
Tom Tromey5b9a40d2007-10-31 14:50:13 +00001522 if (string->type == CPP_EOF)
1523 _cpp_backup_tokens (pfile, 1);
Kris Van Heesb6baa672008-04-18 13:58:08 +00001524 if (string->type != CPP_STRING && string->type != CPP_WSTRING
1525 && string->type != CPP_STRING32 && string->type != CPP_STRING16)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001526 return NULL;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001527
Tom Tromey5b9a40d2007-10-31 14:50:13 +00001528 paren = get_token_no_padding (pfile);
1529 if (paren->type == CPP_EOF)
1530 _cpp_backup_tokens (pfile, 1);
1531 if (paren->type != CPP_CLOSE_PAREN)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001532 return NULL;
1533
1534 return string;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001535}
1536
Neil Booth87062812001-10-20 09:00:53 +00001537/* Destringize IN into a temporary buffer, by removing the first \ of
1538 \" and \\ sequences, and process the result as a #pragma directive. */
1539static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001540destringize_and_run (cpp_reader *pfile, const cpp_string *in)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001541{
1542 const unsigned char *src, *limit;
Neil Booth87062812001-10-20 09:00:53 +00001543 char *dest, *result;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001544 cpp_context *saved_context;
1545 cpp_token *saved_cur_token;
1546 tokenrun *saved_cur_run;
1547 cpp_token *toks;
1548 int count;
Tom Tromey14ccf802008-03-13 21:10:07 +00001549 const struct directive *save_directive;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001550
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001551 dest = result = (char *) alloca (in->len - 1);
Neil Booth6338b352003-04-23 22:44:06 +00001552 src = in->text + 1 + (in->text[0] == 'L');
1553 limit = in->text + in->len - 1;
1554 while (src < limit)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001555 {
1556 /* We know there is a character following the backslash. */
1557 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1558 src++;
1559 *dest++ = *src++;
1560 }
Neil Booth26aea072003-04-19 00:22:51 +00001561 *dest = '\n';
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001562
Neil Booth8128ccc2002-11-18 20:43:40 +00001563 /* Ugh; an awful kludge. We are really not set up to be lexing
1564 tokens when in the middle of a macro expansion. Use a new
1565 context to force cpp_get_token to lex, and so skip_rest_of_line
1566 doesn't go beyond the end of the text. Also, remember the
1567 current lexing position so we can return to it later.
Neil Booth79ba5e32002-09-03 21:55:40 +00001568
Neil Booth8128ccc2002-11-18 20:43:40 +00001569 Something like line-at-a-time lexing should remove the need for
1570 this. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001571 saved_context = pfile->context;
1572 saved_cur_token = pfile->cur_token;
1573 saved_cur_run = pfile->cur_run;
Neil Booth8128ccc2002-11-18 20:43:40 +00001574
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001575 pfile->context = XNEW (cpp_context);
1576 pfile->context->macro = 0;
1577 pfile->context->prev = 0;
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001578 pfile->context->next = 0;
Neil Booth79ba5e32002-09-03 21:55:40 +00001579
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001580 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1581 until we've read all of the tokens that we want. */
1582 cpp_push_buffer (pfile, (const uchar *) result, dest - result,
1583 /* from_stage3 */ true);
1584 /* ??? Antique Disgusting Hack. What does this do? */
1585 if (pfile->buffer->prev)
1586 pfile->buffer->file = pfile->buffer->prev->file;
Neil Booth79ba5e32002-09-03 21:55:40 +00001587
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001588 start_directive (pfile);
1589 _cpp_clean_line (pfile);
Tom Tromey14ccf802008-03-13 21:10:07 +00001590 save_directive = pfile->directive;
1591 pfile->directive = &dtable[T_PRAGMA];
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001592 do_pragma (pfile);
1593 end_directive (pfile, 1);
Tom Tromey14ccf802008-03-13 21:10:07 +00001594 pfile->directive = save_directive;
Neil Booth79ba5e32002-09-03 21:55:40 +00001595
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001596 /* We always insert at least one token, the directive result. It'll
1597 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1598 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
Neil Booth79ba5e32002-09-03 21:55:40 +00001599
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001600 /* If we're not handling the pragma internally, read all of the tokens from
1601 the string buffer now, while the string buffer is still installed. */
1602 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1603 to me what the true lifespan of the tokens are. It would appear that
1604 the lifespan is the entire parse of the main input stream, in which case
1605 this may not be wrong. */
1606 if (pfile->directive_result.type == CPP_PRAGMA)
1607 {
1608 int maxcount;
Neil Booth79ba5e32002-09-03 21:55:40 +00001609
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001610 count = 1;
1611 maxcount = 50;
1612 toks = XNEWVEC (cpp_token, maxcount);
1613 toks[0] = pfile->directive_result;
1614
1615 do
1616 {
1617 if (count == maxcount)
1618 {
1619 maxcount = maxcount * 3 / 2;
1620 toks = XRESIZEVEC (cpp_token, toks, maxcount);
1621 }
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001622 toks[count] = *cpp_get_token (pfile);
1623 /* Macros have been already expanded by cpp_get_token
1624 if the pragma allowed expansion. */
1625 toks[count++].flags |= NO_EXPAND;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001626 }
1627 while (toks[count-1].type != CPP_PRAGMA_EOL);
1628 }
1629 else
1630 {
1631 count = 1;
1632 toks = XNEW (cpp_token);
1633 toks[0] = pfile->directive_result;
1634
1635 /* If we handled the entire pragma internally, make sure we get the
1636 line number correct for the next token. */
1637 if (pfile->cb.line_change)
1638 pfile->cb.line_change (pfile, pfile->cur_token, false);
1639 }
1640
1641 /* Finish inlining run_directive. */
1642 pfile->buffer->file = NULL;
1643 _cpp_pop_buffer (pfile);
1644
1645 /* Reset the old macro state before ... */
1646 XDELETE (pfile->context);
1647 pfile->context = saved_context;
1648 pfile->cur_token = saved_cur_token;
1649 pfile->cur_run = saved_cur_run;
1650
1651 /* ... inserting the new tokens we collected. */
1652 _cpp_push_token_context (pfile, NULL, toks, count);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001653}
1654
Tom Tromey5b9a40d2007-10-31 14:50:13 +00001655/* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
1656int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001657_cpp_do__Pragma (cpp_reader *pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001658{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001659 const cpp_token *string = get__Pragma_string (pfile);
Zack Weinberg21b11492004-09-09 19:16:56 +00001660 pfile->directive_result.type = CPP_PADDING;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001661
Neil Booth79ba5e32002-09-03 21:55:40 +00001662 if (string)
Tom Tromey5b9a40d2007-10-31 14:50:13 +00001663 {
1664 destringize_and_run (pfile, &string->val.str);
1665 return 1;
1666 }
1667 cpp_error (pfile, CPP_DL_ERROR,
1668 "_Pragma takes a parenthesized string literal");
1669 return 0;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001670}
1671
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001672/* Handle #ifdef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001673static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001674do_ifdef (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +00001675{
Neil Booth93c803682000-10-28 17:59:06 +00001676 int skip = 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001677
Neil Boothcef0d192001-07-26 06:02:47 +00001678 if (! pfile->state.skipping)
Neil Booth93c803682000-10-28 17:59:06 +00001679 {
Joseph Myers93d45d92008-04-02 20:42:53 +01001680 cpp_hashnode *node = lex_macro_node (pfile, false);
Zack Weinberg041c3192000-07-04 01:58:21 +00001681
Neil Booth93c803682000-10-28 17:59:06 +00001682 if (node)
Neil Bootha69cbaa2002-07-23 22:57:49 +00001683 {
1684 skip = node->type != NT_MACRO;
1685 _cpp_mark_macro_used (node);
Joseph Myers93d45d92008-04-02 20:42:53 +01001686 if (!(node->flags & NODE_USED))
1687 {
1688 node->flags |= NODE_USED;
1689 if (node->type == NT_MACRO)
1690 {
1691 if (pfile->cb.used_define)
1692 pfile->cb.used_define (pfile, pfile->directive_line, node);
1693 }
1694 else
1695 {
1696 if (pfile->cb.used_undef)
1697 pfile->cb.used_undef (pfile, pfile->directive_line, node);
1698 }
1699 }
Joseph Myersa5cb5632009-04-18 16:28:40 +01001700 check_eol (pfile, false);
Neil Bootha69cbaa2002-07-23 22:57:49 +00001701 }
Neil Booth93c803682000-10-28 17:59:06 +00001702 }
1703
1704 push_conditional (pfile, skip, T_IFDEF, 0);
Zack Weinberg168d3732000-03-14 06:34:11 +00001705}
1706
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001707/* Handle #ifndef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001708static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001709do_ifndef (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +00001710{
Neil Booth93c803682000-10-28 17:59:06 +00001711 int skip = 1;
Joseph Myers93d45d92008-04-02 20:42:53 +01001712 cpp_hashnode *node = 0;
Zack Weinberg168d3732000-03-14 06:34:11 +00001713
Neil Boothcef0d192001-07-26 06:02:47 +00001714 if (! pfile->state.skipping)
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001715 {
Tom Tromeyee1c2a12007-01-12 19:46:49 +00001716 node = lex_macro_node (pfile, false);
Geoffrey Keatingb43db0b2000-12-02 22:28:44 +00001717
1718 if (node)
Neil Bootha69cbaa2002-07-23 22:57:49 +00001719 {
1720 skip = node->type == NT_MACRO;
1721 _cpp_mark_macro_used (node);
Joseph Myers93d45d92008-04-02 20:42:53 +01001722 if (!(node->flags & NODE_USED))
1723 {
1724 node->flags |= NODE_USED;
1725 if (node->type == NT_MACRO)
1726 {
1727 if (pfile->cb.used_define)
1728 pfile->cb.used_define (pfile, pfile->directive_line, node);
1729 }
1730 else
1731 {
1732 if (pfile->cb.used_undef)
1733 pfile->cb.used_undef (pfile, pfile->directive_line, node);
1734 }
1735 }
Joseph Myersa5cb5632009-04-18 16:28:40 +01001736 check_eol (pfile, false);
Neil Bootha69cbaa2002-07-23 22:57:49 +00001737 }
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001738 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001739
Neil Booth93c803682000-10-28 17:59:06 +00001740 push_conditional (pfile, skip, T_IFNDEF, node);
Per Bothner7f2935c1995-03-16 13:59:07 -08001741}
1742
Neil Booth6d18adb2001-07-29 17:27:57 +00001743/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1744 pfile->mi_ind_cmacro so we can handle multiple-include
Kazu Hirata6356f892003-06-12 19:01:08 +00001745 optimizations. If macro expansion occurs in the expression, we
Neil Booth6d18adb2001-07-29 17:27:57 +00001746 cannot treat it as a controlling conditional, since the expansion
1747 could change in the future. That is handled by cpp_get_token. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001748static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001749do_if (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001750{
Neil Booth93c803682000-10-28 17:59:06 +00001751 int skip = 1;
Per Bothner7f2935c1995-03-16 13:59:07 -08001752
Neil Boothcef0d192001-07-26 06:02:47 +00001753 if (! pfile->state.skipping)
Tom Tromeyd7508872008-05-30 14:25:09 +00001754 skip = _cpp_parse_expr (pfile, true) == false;
Neil Booth93c803682000-10-28 17:59:06 +00001755
Neil Booth6d18adb2001-07-29 17:27:57 +00001756 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
Per Bothner7f2935c1995-03-16 13:59:07 -08001757}
1758
Neil Boothb528a072000-11-12 11:46:21 +00001759/* Flip skipping state if appropriate and continue without changing
Zack Weinbergea4a4532000-05-29 16:19:32 +00001760 if_stack; this is so that the error message for missing #endif's
1761 etc. will point to the original #if. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001762static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001763do_else (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001764{
Neil Boothb528a072000-11-12 11:46:21 +00001765 cpp_buffer *buffer = pfile->buffer;
1766 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001767
1768 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001769 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
Neil Booth93c803682000-10-28 17:59:06 +00001770 else
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001771 {
Neil Booth93c803682000-10-28 17:59:06 +00001772 if (ifs->type == T_ELSE)
1773 {
John David Anglin0527bc42003-11-01 22:56:54 +00001774 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1775 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Booth93c803682000-10-28 17:59:06 +00001776 "the conditional began here");
1777 }
Neil Boothb528a072000-11-12 11:46:21 +00001778 ifs->type = T_ELSE;
1779
Neil Boothcef0d192001-07-26 06:02:47 +00001780 /* Skip any future (erroneous) #elses or #elifs. */
1781 pfile->state.skipping = ifs->skip_elses;
1782 ifs->skip_elses = true;
Neil Booth93c803682000-10-28 17:59:06 +00001783
1784 /* Invalidate any controlling macro. */
1785 ifs->mi_cmacro = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001786
Neil Boothcef0d192001-07-26 06:02:47 +00001787 /* Only check EOL if was not originally skipping. */
Phil Edwards909de5d2002-03-22 21:59:04 +00001788 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
Joseph Myersa5cb5632009-04-18 16:28:40 +01001789 check_eol (pfile, false);
Neil Boothcef0d192001-07-26 06:02:47 +00001790 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001791}
1792
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001793/* Handle a #elif directive by not changing if_stack either. See the
Neil Booth93c803682000-10-28 17:59:06 +00001794 comment above do_else. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001795static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001796do_elif (cpp_reader *pfile)
Zack Weinbergea4a4532000-05-29 16:19:32 +00001797{
Neil Boothb528a072000-11-12 11:46:21 +00001798 cpp_buffer *buffer = pfile->buffer;
1799 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001800
1801 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001802 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
Neil Boothb528a072000-11-12 11:46:21 +00001803 else
Zack Weinbergea4a4532000-05-29 16:19:32 +00001804 {
Neil Boothb528a072000-11-12 11:46:21 +00001805 if (ifs->type == T_ELSE)
1806 {
John David Anglin0527bc42003-11-01 22:56:54 +00001807 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1808 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Boothb528a072000-11-12 11:46:21 +00001809 "the conditional began here");
1810 }
1811 ifs->type = T_ELIF;
1812
Tom Tromeyd7508872008-05-30 14:25:09 +00001813 if (! ifs->was_skipping)
Neil Boothb528a072000-11-12 11:46:21 +00001814 {
Tom Tromeyd7508872008-05-30 14:25:09 +00001815 bool value;
1816 /* The standard mandates that the expression be parsed even
1817 if we are skipping elses at this point -- the lexical
1818 restrictions on #elif only apply to skipped groups, but
1819 this group is not being skipped. Temporarily set
1820 skipping to false to get lexer warnings. */
Neil Boothcef0d192001-07-26 06:02:47 +00001821 pfile->state.skipping = 0;
Tom Tromeyd7508872008-05-30 14:25:09 +00001822 value = _cpp_parse_expr (pfile, false);
1823 if (ifs->skip_elses)
1824 pfile->state.skipping = 1;
1825 else
1826 {
1827 pfile->state.skipping = ! value;
1828 ifs->skip_elses = value;
1829 }
Neil Boothb528a072000-11-12 11:46:21 +00001830 }
Neil Boothcef0d192001-07-26 06:02:47 +00001831
1832 /* Invalidate any controlling macro. */
1833 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001834 }
Zack Weinbergea4a4532000-05-29 16:19:32 +00001835}
1836
Neil Boothcef0d192001-07-26 06:02:47 +00001837/* #endif pops the if stack and resets pfile->state.skipping. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001838static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001839do_endif (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001840{
Neil Boothb528a072000-11-12 11:46:21 +00001841 cpp_buffer *buffer = pfile->buffer;
1842 struct if_stack *ifs = buffer->if_stack;
Per Bothner7f2935c1995-03-16 13:59:07 -08001843
Zack Weinbergea4a4532000-05-29 16:19:32 +00001844 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001845 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
Per Bothner7f2935c1995-03-16 13:59:07 -08001846 else
1847 {
Neil Boothcef0d192001-07-26 06:02:47 +00001848 /* Only check EOL if was not originally skipping. */
Phil Edwards909de5d2002-03-22 21:59:04 +00001849 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
Joseph Myersa5cb5632009-04-18 16:28:40 +01001850 check_eol (pfile, false);
Neil Boothcef0d192001-07-26 06:02:47 +00001851
Neil Booth93c803682000-10-28 17:59:06 +00001852 /* If potential control macro, we go back outside again. */
1853 if (ifs->next == 0 && ifs->mi_cmacro)
1854 {
Neil Booth6d18adb2001-07-29 17:27:57 +00001855 pfile->mi_valid = true;
Neil Booth93c803682000-10-28 17:59:06 +00001856 pfile->mi_cmacro = ifs->mi_cmacro;
1857 }
1858
Neil Boothb528a072000-11-12 11:46:21 +00001859 buffer->if_stack = ifs->next;
Neil Boothcef0d192001-07-26 06:02:47 +00001860 pfile->state.skipping = ifs->was_skipping;
Neil Booth2a967f32001-05-20 06:26:45 +00001861 obstack_free (&pfile->buffer_ob, ifs);
Per Bothner7f2935c1995-03-16 13:59:07 -08001862 }
Neil Booth93c803682000-10-28 17:59:06 +00001863}
Zack Weinberg041c3192000-07-04 01:58:21 +00001864
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001865/* Push an if_stack entry for a preprocessor conditional, and set
1866 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1867 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1868 we need to check here that we are at the top of the file. */
Zack Weinbergea4a4532000-05-29 16:19:32 +00001869static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001870push_conditional (cpp_reader *pfile, int skip, int type,
1871 const cpp_hashnode *cmacro)
Zack Weinbergea4a4532000-05-29 16:19:32 +00001872{
1873 struct if_stack *ifs;
Neil Boothb528a072000-11-12 11:46:21 +00001874 cpp_buffer *buffer = pfile->buffer;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001875
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001876 ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
Neil Booth50410422001-09-15 10:18:03 +00001877 ifs->line = pfile->directive_line;
Neil Boothb528a072000-11-12 11:46:21 +00001878 ifs->next = buffer->if_stack;
Neil Boothcef0d192001-07-26 06:02:47 +00001879 ifs->skip_elses = pfile->state.skipping || !skip;
1880 ifs->was_skipping = pfile->state.skipping;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001881 ifs->type = type;
Neil Booth6d18adb2001-07-29 17:27:57 +00001882 /* This condition is effectively a test for top-of-file. */
1883 if (pfile->mi_valid && pfile->mi_cmacro == 0)
Neil Booth93c803682000-10-28 17:59:06 +00001884 ifs->mi_cmacro = cmacro;
1885 else
1886 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001887
Neil Boothcef0d192001-07-26 06:02:47 +00001888 pfile->state.skipping = skip;
Neil Boothb528a072000-11-12 11:46:21 +00001889 buffer->if_stack = ifs;
Zack Weinberg7061aa51998-12-15 11:09:16 +00001890}
1891
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001892/* Read the tokens of the answer into the macro pool, in a directive
1893 of type TYPE. Only commit the memory if we intend it as permanent
1894 storage, i.e. the #assert case. Returns 0 on success, and sets
1895 ANSWERP to point to the answer. */
Neil Booth93c803682000-10-28 17:59:06 +00001896static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001897parse_answer (cpp_reader *pfile, struct answer **answerp, int type)
Zack Weinberg041c3192000-07-04 01:58:21 +00001898{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001899 const cpp_token *paren;
Neil Booth93c803682000-10-28 17:59:06 +00001900 struct answer *answer;
Neil Booth8c3b2692001-09-30 10:03:11 +00001901 unsigned int acount;
Zack Weinberg041c3192000-07-04 01:58:21 +00001902
Neil Booth93c803682000-10-28 17:59:06 +00001903 /* In a conditional, it is legal to not have an open paren. We
1904 should save the following token in this case. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001905 paren = cpp_get_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001906
1907 /* If not a paren, see if we're OK. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001908 if (paren->type != CPP_OPEN_PAREN)
Zack Weinberg041c3192000-07-04 01:58:21 +00001909 {
Neil Booth93c803682000-10-28 17:59:06 +00001910 /* In a conditional no answer is a test for any answer. It
1911 could be followed by any token. */
1912 if (type == T_IF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001913 {
1914 _cpp_backup_tokens (pfile, 1);
1915 return 0;
1916 }
Neil Booth93c803682000-10-28 17:59:06 +00001917
1918 /* #unassert with no answer is valid - it removes all answers. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001919 if (type == T_UNASSERT && paren->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +00001920 return 0;
1921
John David Anglin0527bc42003-11-01 22:56:54 +00001922 cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate");
Neil Booth93c803682000-10-28 17:59:06 +00001923 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001924 }
1925
Neil Booth8c3b2692001-09-30 10:03:11 +00001926 for (acount = 0;; acount++)
Zack Weinberg041c3192000-07-04 01:58:21 +00001927 {
Neil Booth8c3b2692001-09-30 10:03:11 +00001928 size_t room_needed;
1929 const cpp_token *token = cpp_get_token (pfile);
1930 cpp_token *dest;
Zack Weinberg041c3192000-07-04 01:58:21 +00001931
Neil Booth93c803682000-10-28 17:59:06 +00001932 if (token->type == CPP_CLOSE_PAREN)
1933 break;
Zack Weinberg041c3192000-07-04 01:58:21 +00001934
1935 if (token->type == CPP_EOF)
1936 {
John David Anglin0527bc42003-11-01 22:56:54 +00001937 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
Neil Booth93c803682000-10-28 17:59:06 +00001938 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001939 }
Neil Booth8c3b2692001-09-30 10:03:11 +00001940
1941 /* struct answer includes the space for one token. */
1942 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1943
1944 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1945 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1946
1947 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1948 *dest = *token;
1949
1950 /* Drop whitespace at start, for answer equivalence purposes. */
1951 if (acount == 0)
1952 dest->flags &= ~PREV_WHITE;
Zack Weinberg041c3192000-07-04 01:58:21 +00001953 }
1954
Neil Booth8c3b2692001-09-30 10:03:11 +00001955 if (acount == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001956 {
John David Anglin0527bc42003-11-01 22:56:54 +00001957 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
Neil Booth93c803682000-10-28 17:59:06 +00001958 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001959 }
1960
Neil Booth8c3b2692001-09-30 10:03:11 +00001961 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1962 answer->count = acount;
1963 answer->next = NULL;
Neil Booth93c803682000-10-28 17:59:06 +00001964 *answerp = answer;
Zack Weinberg041c3192000-07-04 01:58:21 +00001965
Neil Booth93c803682000-10-28 17:59:06 +00001966 return 0;
1967}
1968
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001969/* Parses an assertion directive of type TYPE, returning a pointer to
1970 the hash node of the predicate, or 0 on error. If an answer was
1971 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
Neil Booth93c803682000-10-28 17:59:06 +00001972static cpp_hashnode *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001973parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
Neil Booth93c803682000-10-28 17:59:06 +00001974{
1975 cpp_hashnode *result = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001976 const cpp_token *predicate;
Neil Booth93c803682000-10-28 17:59:06 +00001977
1978 /* We don't expand predicates or answers. */
1979 pfile->state.prevent_expansion++;
1980
Neil Booth93c803682000-10-28 17:59:06 +00001981 *answerp = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001982 predicate = cpp_get_token (pfile);
1983 if (predicate->type == CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +00001984 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001985 else if (predicate->type != CPP_NAME)
John David Anglin0527bc42003-11-01 22:56:54 +00001986 cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier");
Neil Booth93c803682000-10-28 17:59:06 +00001987 else if (parse_answer (pfile, answerp, type) == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001988 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001989 unsigned int len = NODE_LEN (predicate->val.node);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001990 unsigned char *sym = (unsigned char *) alloca (len + 1);
Neil Booth93c803682000-10-28 17:59:06 +00001991
1992 /* Prefix '#' to get it out of macro namespace. */
1993 sym[0] = '#';
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001994 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
Neil Booth93c803682000-10-28 17:59:06 +00001995 result = cpp_lookup (pfile, sym, len + 1);
Zack Weinberg041c3192000-07-04 01:58:21 +00001996 }
1997
Neil Booth93c803682000-10-28 17:59:06 +00001998 pfile->state.prevent_expansion--;
1999 return result;
Zack Weinberg041c3192000-07-04 01:58:21 +00002000}
2001
Neil Booth5d8ebbd2002-01-03 21:43:09 +00002002/* Returns a pointer to the pointer to CANDIDATE in the answer chain,
Zack Weinberg041c3192000-07-04 01:58:21 +00002003 or a pointer to NULL if the answer is not in the chain. */
Neil Booth93c803682000-10-28 17:59:06 +00002004static struct answer **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002005find_answer (cpp_hashnode *node, const struct answer *candidate)
Zack Weinberg041c3192000-07-04 01:58:21 +00002006{
Neil Booth93c803682000-10-28 17:59:06 +00002007 unsigned int i;
Zack Weinberg041c3192000-07-04 01:58:21 +00002008 struct answer **result;
2009
2010 for (result = &node->value.answers; *result; result = &(*result)->next)
Neil Booth93c803682000-10-28 17:59:06 +00002011 {
2012 struct answer *answer = *result;
2013
2014 if (answer->count == candidate->count)
2015 {
2016 for (i = 0; i < answer->count; i++)
2017 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
2018 break;
2019
2020 if (i == answer->count)
2021 break;
2022 }
2023 }
Zack Weinberg041c3192000-07-04 01:58:21 +00002024
2025 return result;
2026}
2027
Neil Booth93c803682000-10-28 17:59:06 +00002028/* Test an assertion within a preprocessor conditional. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +00002029 nonzero on failure, zero on success. On success, the result of
Hans-Peter Nilsson24026452002-11-29 22:41:04 +00002030 the test is written into VALUE, otherwise the value 0. */
Neil Booth93c803682000-10-28 17:59:06 +00002031int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002032_cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
Neil Booth93c803682000-10-28 17:59:06 +00002033{
2034 struct answer *answer;
2035 cpp_hashnode *node;
2036
2037 node = parse_assertion (pfile, &answer, T_IF);
Hans-Peter Nilsson24026452002-11-29 22:41:04 +00002038
2039 /* For recovery, an erroneous assertion expression is handled as a
2040 failing assertion. */
2041 *value = 0;
2042
Neil Booth93c803682000-10-28 17:59:06 +00002043 if (node)
2044 *value = (node->type == NT_ASSERTION &&
2045 (answer == 0 || *find_answer (node, answer) != 0));
Neil Booth91318902002-05-26 18:42:21 +00002046 else if (pfile->cur_token[-1].type == CPP_EOF)
2047 _cpp_backup_tokens (pfile, 1);
Neil Booth93c803682000-10-28 17:59:06 +00002048
2049 /* We don't commit the memory for the answer - it's temporary only. */
2050 return node == 0;
2051}
2052
Neil Booth5d8ebbd2002-01-03 21:43:09 +00002053/* Handle #assert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00002054static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002055do_assert (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08002056{
Zack Weinberg041c3192000-07-04 01:58:21 +00002057 struct answer *new_answer;
2058 cpp_hashnode *node;
Kazu Hiratadf383482002-05-22 22:02:16 +00002059
Neil Booth93c803682000-10-28 17:59:06 +00002060 node = parse_assertion (pfile, &new_answer, T_ASSERT);
Zack Weinberg041c3192000-07-04 01:58:21 +00002061 if (node)
2062 {
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002063 size_t answer_size;
2064
Neil Booth93c803682000-10-28 17:59:06 +00002065 /* Place the new answer in the answer list. First check there
2066 is not a duplicate. */
Zack Weinberg041c3192000-07-04 01:58:21 +00002067 new_answer->next = 0;
Neil Booth93c803682000-10-28 17:59:06 +00002068 if (node->type == NT_ASSERTION)
Zack Weinberg041c3192000-07-04 01:58:21 +00002069 {
Neil Booth93c803682000-10-28 17:59:06 +00002070 if (*find_answer (node, new_answer))
2071 {
John David Anglin0527bc42003-11-01 22:56:54 +00002072 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
Neil Boothebef4e82002-04-14 18:42:47 +00002073 NODE_NAME (node) + 1);
Neil Booth93c803682000-10-28 17:59:06 +00002074 return;
2075 }
Zack Weinberg041c3192000-07-04 01:58:21 +00002076 new_answer->next = node->value.answers;
2077 }
Neil Booth8c3b2692001-09-30 10:03:11 +00002078
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002079 answer_size = sizeof (struct answer) + ((new_answer->count - 1)
2080 * sizeof (cpp_token));
2081 /* Commit or allocate storage for the object. */
2082 if (pfile->hash_table->alloc_subobject)
2083 {
2084 struct answer *temp_answer = new_answer;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002085 new_answer = (struct answer *) pfile->hash_table->alloc_subobject
2086 (answer_size);
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002087 memcpy (new_answer, temp_answer, answer_size);
2088 }
2089 else
2090 BUFF_FRONT (pfile->a_buff) += answer_size;
2091
Neil Booth93c803682000-10-28 17:59:06 +00002092 node->type = NT_ASSERTION;
Zack Weinberg041c3192000-07-04 01:58:21 +00002093 node->value.answers = new_answer;
Joseph Myersa5cb5632009-04-18 16:28:40 +01002094 check_eol (pfile, false);
Zack Weinberg041c3192000-07-04 01:58:21 +00002095 }
Per Bothner7f2935c1995-03-16 13:59:07 -08002096}
Zack Weinberg7061aa51998-12-15 11:09:16 +00002097
Neil Booth5d8ebbd2002-01-03 21:43:09 +00002098/* Handle #unassert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00002099static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002100do_unassert (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08002101{
Zack Weinberg041c3192000-07-04 01:58:21 +00002102 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00002103 struct answer *answer;
Kazu Hiratadf383482002-05-22 22:02:16 +00002104
Neil Booth93c803682000-10-28 17:59:06 +00002105 node = parse_assertion (pfile, &answer, T_UNASSERT);
2106 /* It isn't an error to #unassert something that isn't asserted. */
2107 if (node && node->type == NT_ASSERTION)
Zack Weinberg7061aa51998-12-15 11:09:16 +00002108 {
Zack Weinberg041c3192000-07-04 01:58:21 +00002109 if (answer)
Neil Booth93c803682000-10-28 17:59:06 +00002110 {
2111 struct answer **p = find_answer (node, answer), *temp;
2112
2113 /* Remove the answer from the list. */
2114 temp = *p;
2115 if (temp)
2116 *p = temp->next;
2117
2118 /* Did we free the last answer? */
2119 if (node->value.answers == 0)
2120 node->type = NT_VOID;
Neil Booth8c3b2692001-09-30 10:03:11 +00002121
Joseph Myersa5cb5632009-04-18 16:28:40 +01002122 check_eol (pfile, false);
Neil Booth93c803682000-10-28 17:59:06 +00002123 }
2124 else
2125 _cpp_free_definition (node);
Zack Weinberg7061aa51998-12-15 11:09:16 +00002126 }
Neil Booth93c803682000-10-28 17:59:06 +00002127
2128 /* We don't commit the memory for the answer - it's temporary only. */
Per Bothner7f2935c1995-03-16 13:59:07 -08002129}
Per Bothner7f2935c1995-03-16 13:59:07 -08002130
Zack Weinberg45b966d2000-03-13 22:01:08 +00002131/* These are for -D, -U, -A. */
2132
2133/* Process the string STR as if it appeared as the body of a #define.
2134 If STR is just an identifier, define it with value 1.
2135 If STR has anything after the identifier, then it should
Kazu Hirataec5c56d2001-08-01 17:57:27 +00002136 be identifier=definition. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00002137void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002138cpp_define (cpp_reader *pfile, const char *str)
Zack Weinberg45b966d2000-03-13 22:01:08 +00002139{
2140 char *buf, *p;
2141 size_t count;
2142
Kazu Hiratadf383482002-05-22 22:02:16 +00002143 /* Copy the entire option so we can modify it.
Zack Weinberg45b966d2000-03-13 22:01:08 +00002144 Change the first "=" in the string to a space. If there is none,
Neil Booth86368122000-10-31 23:34:59 +00002145 tack " 1" on the end. */
2146
Neil Booth86368122000-10-31 23:34:59 +00002147 count = strlen (str);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002148 buf = (char *) alloca (count + 3);
Neil Booth86368122000-10-31 23:34:59 +00002149 memcpy (buf, str, count);
2150
2151 p = strchr (str, '=');
Zack Weinberg45b966d2000-03-13 22:01:08 +00002152 if (p)
Neil Booth86368122000-10-31 23:34:59 +00002153 buf[p - str] = ' ';
Zack Weinberg45b966d2000-03-13 22:01:08 +00002154 else
2155 {
Neil Booth86368122000-10-31 23:34:59 +00002156 buf[count++] = ' ';
2157 buf[count++] = '1';
Zack Weinberg45b966d2000-03-13 22:01:08 +00002158 }
Neil Booth26aea072003-04-19 00:22:51 +00002159 buf[count] = '\n';
Zack Weinberg45b966d2000-03-13 22:01:08 +00002160
Neil Booth29401c32001-08-22 20:37:20 +00002161 run_directive (pfile, T_DEFINE, buf, count);
Zack Weinberg2c8f0512000-08-29 18:37:37 +00002162}
2163
Daniel Franke28f68622008-04-22 14:04:32 -04002164
2165/* Use to build macros to be run through cpp_define() as
2166 described above.
2167 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
2168
2169void
2170cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...)
2171{
2172 char *ptr = NULL;
2173
2174 va_list ap;
2175 va_start (ap, fmt);
2176 vasprintf (&ptr, fmt, ap);
2177 va_end (ap);
2178
2179 cpp_define (pfile, ptr);
2180 free (ptr);
2181}
2182
2183
Neil Boothad2a0842000-12-17 00:13:54 +00002184/* Slight variant of the above for use by initialize_builtins. */
Zack Weinberg2c8f0512000-08-29 18:37:37 +00002185void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002186_cpp_define_builtin (cpp_reader *pfile, const char *str)
Zack Weinberg2c8f0512000-08-29 18:37:37 +00002187{
Neil Booth26aea072003-04-19 00:22:51 +00002188 size_t len = strlen (str);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002189 char *buf = (char *) alloca (len + 1);
Neil Booth26aea072003-04-19 00:22:51 +00002190 memcpy (buf, str, len);
2191 buf[len] = '\n';
2192 run_directive (pfile, T_DEFINE, buf, len);
Zack Weinberg45b966d2000-03-13 22:01:08 +00002193}
2194
2195/* Process MACRO as if it appeared as the body of an #undef. */
2196void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002197cpp_undef (cpp_reader *pfile, const char *macro)
Zack Weinberg45b966d2000-03-13 22:01:08 +00002198{
Neil Booth26aea072003-04-19 00:22:51 +00002199 size_t len = strlen (macro);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002200 char *buf = (char *) alloca (len + 1);
Neil Booth26aea072003-04-19 00:22:51 +00002201 memcpy (buf, macro, len);
2202 buf[len] = '\n';
2203 run_directive (pfile, T_UNDEF, buf, len);
Zack Weinberg45b966d2000-03-13 22:01:08 +00002204}
2205
Richard Henderson121de392007-03-30 14:12:53 -07002206/* Like lex_macro_node, but read the input from STR. */
2207static cpp_hashnode *
2208lex_macro_node_from_str (cpp_reader *pfile, const char *str)
2209{
2210 size_t len = strlen (str);
Michael Meissner4cd97072007-03-30 22:40:19 +00002211 uchar *buf = (uchar *) alloca (len + 1);
Richard Henderson121de392007-03-30 14:12:53 -07002212 cpp_hashnode *node;
2213
2214 memcpy (buf, str, len);
2215 buf[len] = '\n';
2216 cpp_push_buffer (pfile, buf, len, true);
2217 node = lex_macro_node (pfile, true);
2218 _cpp_pop_buffer (pfile);
2219
2220 return node;
2221}
2222
2223/* If STR is a defined macro, return its definition node, else return NULL. */
2224cpp_macro *
2225cpp_push_definition (cpp_reader *pfile, const char *str)
2226{
2227 cpp_hashnode *node = lex_macro_node_from_str (pfile, str);
2228 if (node && node->type == NT_MACRO)
2229 return node->value.macro;
2230 else
2231 return NULL;
2232}
2233
2234/* Replace a previous definition DFN of the macro STR. If DFN is NULL,
2235 then the macro should be undefined. */
2236void
2237cpp_pop_definition (cpp_reader *pfile, const char *str, cpp_macro *dfn)
2238{
2239 cpp_hashnode *node = lex_macro_node_from_str (pfile, str);
2240 if (node == NULL)
2241 return;
2242
Joseph Myers93d45d92008-04-02 20:42:53 +01002243 if (pfile->cb.before_define)
2244 pfile->cb.before_define (pfile);
2245
Richard Henderson121de392007-03-30 14:12:53 -07002246 if (node->type == NT_MACRO)
2247 {
2248 if (pfile->cb.undef)
2249 pfile->cb.undef (pfile, pfile->directive_line, node);
2250 if (CPP_OPTION (pfile, warn_unused_macros))
2251 _cpp_warn_if_unused_macro (pfile, node, NULL);
2252 }
2253 if (node->type != NT_VOID)
2254 _cpp_free_definition (node);
2255
2256 if (dfn)
2257 {
2258 node->type = NT_MACRO;
2259 node->value.macro = dfn;
2260 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
2261 node->flags |= NODE_WARN;
2262
2263 if (pfile->cb.define)
2264 pfile->cb.define (pfile, pfile->directive_line, node);
2265 }
2266}
2267
Kazu Hirataec5c56d2001-08-01 17:57:27 +00002268/* Process the string STR as if it appeared as the body of a #assert. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00002269void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002270cpp_assert (cpp_reader *pfile, const char *str)
Zack Weinberg45b966d2000-03-13 22:01:08 +00002271{
Neil Booth86368122000-10-31 23:34:59 +00002272 handle_assertion (pfile, str, T_ASSERT);
Zack Weinberg45b966d2000-03-13 22:01:08 +00002273}
2274
Kazu Hirataec5c56d2001-08-01 17:57:27 +00002275/* Process STR as if it appeared as the body of an #unassert. */
Zack Weinberg0b22d651999-03-15 18:42:46 +00002276void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002277cpp_unassert (cpp_reader *pfile, const char *str)
Zack Weinberg0b22d651999-03-15 18:42:46 +00002278{
Neil Booth86368122000-10-31 23:34:59 +00002279 handle_assertion (pfile, str, T_UNASSERT);
Kazu Hiratadf383482002-05-22 22:02:16 +00002280}
Zack Weinberg0b22d651999-03-15 18:42:46 +00002281
Neil Booth86368122000-10-31 23:34:59 +00002282/* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2283static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002284handle_assertion (cpp_reader *pfile, const char *str, int type)
Neil Booth86368122000-10-31 23:34:59 +00002285{
2286 size_t count = strlen (str);
2287 const char *p = strchr (str, '=');
2288
Neil Booth26aea072003-04-19 00:22:51 +00002289 /* Copy the entire option so we can modify it. Change the first
2290 "=" in the string to a '(', and tack a ')' on the end. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002291 char *buf = (char *) alloca (count + 2);
Neil Booth26aea072003-04-19 00:22:51 +00002292
2293 memcpy (buf, str, count);
Neil Booth86368122000-10-31 23:34:59 +00002294 if (p)
2295 {
Neil Booth86368122000-10-31 23:34:59 +00002296 buf[p - str] = '(';
2297 buf[count++] = ')';
Neil Booth86368122000-10-31 23:34:59 +00002298 }
Neil Booth26aea072003-04-19 00:22:51 +00002299 buf[count] = '\n';
2300 str = buf;
Neil Booth86368122000-10-31 23:34:59 +00002301
Neil Booth29401c32001-08-22 20:37:20 +00002302 run_directive (pfile, type, str, count);
Neil Booth86368122000-10-31 23:34:59 +00002303}
2304
Neil Booth7e96d762001-01-13 01:00:01 +00002305/* The options structure. */
2306cpp_options *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002307cpp_get_options (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00002308{
2309 return &pfile->opts;
2310}
2311
2312/* The callbacks structure. */
2313cpp_callbacks *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002314cpp_get_callbacks (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00002315{
2316 return &pfile->cb;
2317}
2318
2319/* Copy the given callbacks structure to our own. */
2320void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002321cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
Neil Booth7e96d762001-01-13 01:00:01 +00002322{
2323 pfile->cb = *cb;
2324}
2325
Zack Weinbergc6e83802004-06-05 20:58:06 +00002326/* The dependencies structure. (Creates one if it hasn't already been.) */
2327struct deps *
2328cpp_get_deps (cpp_reader *pfile)
2329{
2330 if (!pfile->deps)
2331 pfile->deps = deps_init ();
2332 return pfile->deps;
2333}
2334
Neil Bootheb1f4d92000-12-18 19:00:26 +00002335/* Push a new buffer on the buffer stack. Returns the new buffer; it
2336 doesn't fail. It does not generate a file change call back; that
2337 is the responsibility of the caller. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00002338cpp_buffer *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002339cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
Per Bothner40de9f72003-10-02 07:30:34 +00002340 int from_stage3)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002341{
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002342 cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
Neil Booth93c803682000-10-28 17:59:06 +00002343
Neil Boothfde84342001-08-06 21:07:41 +00002344 /* Clears, amongst other things, if_stack and mi_cmacro. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002345 memset (new_buffer, 0, sizeof (cpp_buffer));
Neil Boothad2a0842000-12-17 00:13:54 +00002346
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002347 new_buffer->next_line = new_buffer->buf = buffer;
2348 new_buffer->rlimit = buffer + len;
2349 new_buffer->from_stage3 = from_stage3;
2350 new_buffer->prev = pfile->buffer;
2351 new_buffer->need_line = true;
Neil Booth0bda4762000-12-11 07:45:16 +00002352
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002353 pfile->buffer = new_buffer;
Eric Christophercf551fb2004-01-16 22:37:49 +00002354
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002355 return new_buffer;
Zack Weinbergc71f8352000-07-05 05:33:57 +00002356}
2357
Neil Boothaf0d16c2002-04-22 17:48:02 +00002358/* Pops a single buffer, with a file change call-back if appropriate.
2359 Then pushes the next -include file, if any remain. */
Neil Boothef6e9582001-08-04 12:01:59 +00002360void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002361_cpp_pop_buffer (cpp_reader *pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002362{
Neil Boothfde84342001-08-06 21:07:41 +00002363 cpp_buffer *buffer = pfile->buffer;
Neil Booth8f9b4002003-07-29 22:26:13 +00002364 struct _cpp_file *inc = buffer->file;
Neil Boothad2a0842000-12-17 00:13:54 +00002365 struct if_stack *ifs;
Zack Weinbergc71f8352000-07-05 05:33:57 +00002366
Neil Boothfde84342001-08-06 21:07:41 +00002367 /* Walk back up the conditional stack till we reach its level at
2368 entry to this file, issuing error messages. */
2369 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
John David Anglin0527bc42003-11-01 22:56:54 +00002370 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Boothfde84342001-08-06 21:07:41 +00002371 "unterminated #%s", dtable[ifs->type].name);
2372
Neil Booth97293892001-09-14 22:04:46 +00002373 /* In case of a missing #endif. */
Neil Booth67821e32001-08-05 17:31:25 +00002374 pfile->state.skipping = 0;
Neil Booth29401c32001-08-22 20:37:20 +00002375
Neil Boothaf0d16c2002-04-22 17:48:02 +00002376 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
Neil Booth29401c32001-08-22 20:37:20 +00002377 pfile->buffer = buffer->prev;
2378
Neil Booth26aea072003-04-19 00:22:51 +00002379 free (buffer->notes);
2380
Neil Boothaf0d16c2002-04-22 17:48:02 +00002381 /* Free the buffer object now; we may want to push a new buffer
2382 in _cpp_push_next_include_file. */
2383 obstack_free (&pfile->buffer_ob, buffer);
Neil Booth29401c32001-08-22 20:37:20 +00002384
Neil Boothaf0d16c2002-04-22 17:48:02 +00002385 if (inc)
2386 {
2387 _cpp_pop_file_buffer (pfile, inc);
2388
Per Bothner40de9f72003-10-02 07:30:34 +00002389 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
Neil Boothaf0d16c2002-04-22 17:48:02 +00002390 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00002391}
2392
Kazu Hirata05713b82002-09-15 18:24:08 +00002393/* Enter all recognized directives in the hash table. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00002394void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002395_cpp_init_directives (cpp_reader *pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002396{
Neil Booth766ee682001-01-29 19:20:12 +00002397 unsigned int i;
Neil Booth93c803682000-10-28 17:59:06 +00002398 cpp_hashnode *node;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00002399
John David Anglin37b85242001-03-02 01:11:50 +00002400 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
Neil Booth93c803682000-10-28 17:59:06 +00002401 {
Neil Booth766ee682001-01-29 19:20:12 +00002402 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
Zack Weinberg4977bab2002-12-16 18:23:00 +00002403 node->is_directive = 1;
2404 node->directive_index = i;
Neil Booth93c803682000-10-28 17:59:06 +00002405 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00002406}