blob: 01bb599e26696f2f29057d8994feb11fc6c4c882 [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 *);
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +0000102static const char *parse_include (cpp_reader *, int *, const cpp_token ***,
103 source_location *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000104static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
105static unsigned int read_flag (cpp_reader *, unsigned int);
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000106static bool strtolinenum (const uchar *, size_t, linenum_type *, bool *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000107static void do_diagnostic (cpp_reader *, int, int);
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000108static cpp_hashnode *lex_macro_node (cpp_reader *, bool);
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000109static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000110static void do_include_common (cpp_reader *, enum include_type);
111static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
112 const cpp_hashnode *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000113static int count_registered_pragmas (struct pragma_entry *);
114static char ** save_registered_pragmas (struct pragma_entry *, char **);
115static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
116 char **);
117static void do_pragma_once (cpp_reader *);
118static void do_pragma_poison (cpp_reader *);
119static void do_pragma_system_header (cpp_reader *);
120static void do_pragma_dependency (cpp_reader *);
121static void do_linemarker (cpp_reader *);
122static const cpp_token *get_token_no_padding (cpp_reader *);
123static const cpp_token *get__Pragma_string (cpp_reader *);
124static void destringize_and_run (cpp_reader *, const cpp_string *);
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +0000125static int parse_answer (cpp_reader *, struct answer **, int, source_location);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000126static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
127static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
128static void handle_assertion (cpp_reader *, const char *, int);
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +0000129
Zack Weinberg168d3732000-03-14 06:34:11 +0000130/* This is the table of directive handlers. It is ordered by
131 frequency of occurrence; the numbers at the end are directive
132 counts from all the source code I have lying around (egcs and libc
133 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
Neil Booth93c803682000-10-28 17:59:06 +0000134 pcmcia-cs-3.0.9). This is no longer important as directive lookup
Tom Tromey899015a2008-05-13 14:50:27 +0000135 is now O(1). All extensions other than #warning, #include_next,
136 and #import are deprecated. The name is where the extension
137 appears to have come from. */
Jeff Lawe9a25f71997-11-02 14:19:36 -0700138
Neil Booth93c803682000-10-28 17:59:06 +0000139#define DIRECTIVE_TABLE \
140D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000141D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000142D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
143D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
Neil Booth1a769162002-06-11 05:36:17 +0000144D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000145D(else, T_ELSE, KANDR, COND) /* 9863 */ \
146D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
147D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
Neil Booth1a769162002-06-11 05:36:17 +0000148D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
149D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000150D(error, T_ERROR, STDC89, 0) /* 475 */ \
151D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
152D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000153D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
Neil Vachharajani22143822009-10-10 00:34:21 +0000154D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000155D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
Tom Tromey899015a2008-05-13 14:50:27 +0000156D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
157D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
Neil Vachharajani22143822009-10-10 00:34:21 +0000158D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */
Zack Weinberg1ed17cd2005-05-12 18:31:38 +0000159
160/* #sccs is synonymous with #ident. */
161#define do_sccs do_ident
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000162
Zack Weinberg168d3732000-03-14 06:34:11 +0000163/* Use the table to generate a series of prototypes, an enum for the
164 directive names, and an array of directive handlers. */
165
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000166#define D(name, t, o, f) static void do_##name (cpp_reader *);
Zack Weinberg168d3732000-03-14 06:34:11 +0000167DIRECTIVE_TABLE
168#undef D
169
Zack Weinberg041c3192000-07-04 01:58:21 +0000170#define D(n, tag, o, f) tag,
Zack Weinberg168d3732000-03-14 06:34:11 +0000171enum
172{
173 DIRECTIVE_TABLE
174 N_DIRECTIVES
Per Bothner7f2935c1995-03-16 13:59:07 -0800175};
Zack Weinberg168d3732000-03-14 06:34:11 +0000176#undef D
177
Zack Weinberg041c3192000-07-04 01:58:21 +0000178#define D(name, t, origin, flags) \
Kaveh R. Ghazi9a238582003-06-16 19:14:22 +0000179{ do_##name, (const uchar *) #name, \
180 sizeof #name - 1, origin, flags },
Neil Booth93c803682000-10-28 17:59:06 +0000181static const directive dtable[] =
Zack Weinberg168d3732000-03-14 06:34:11 +0000182{
183DIRECTIVE_TABLE
184};
185#undef D
186#undef DIRECTIVE_TABLE
Per Bothner7f2935c1995-03-16 13:59:07 -0800187
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000188/* Wrapper struct directive for linemarkers.
189 The origin is more or less true - the original K+R cpp
190 did use this notation in its preprocessed output. */
191static const directive linemarker_dir =
192{
Kris Van Heesb6baa672008-04-18 13:58:08 +0000193 do_linemarker, UC"#", 1, KANDR, IN_I
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000194};
195
Neil Booth1a769162002-06-11 05:36:17 +0000196#define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
Neil Booth67821e32001-08-05 17:31:25 +0000197
Neil Booth93c803682000-10-28 17:59:06 +0000198/* Skip any remaining tokens in a directive. */
199static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000200skip_rest_of_line (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +0000201{
Neil Booth93c803682000-10-28 17:59:06 +0000202 /* Discard all stacked contexts. */
Neil Booth8128ccc2002-11-18 20:43:40 +0000203 while (pfile->context->prev)
Neil Booth93c803682000-10-28 17:59:06 +0000204 _cpp_pop_context (pfile);
205
Neil Boothb528a072000-11-12 11:46:21 +0000206 /* Sweep up all tokens remaining on the line. */
Neil Booth345894b2001-09-16 13:44:29 +0000207 if (! SEEN_EOL ())
208 while (_cpp_lex_token (pfile)->type != CPP_EOF)
209 ;
Neil Booth93c803682000-10-28 17:59:06 +0000210}
211
Joseph Myersa5cb5632009-04-18 16:28:40 +0100212/* Ensure there are no stray tokens at the end of a directive. If
213 EXPAND is true, tokens macro-expanding to nothing are allowed. */
Neil Booth93c803682000-10-28 17:59:06 +0000214static void
Joseph Myersa5cb5632009-04-18 16:28:40 +0100215check_eol (cpp_reader *pfile, bool expand)
Neil Booth93c803682000-10-28 17:59:06 +0000216{
Joseph Myersa5cb5632009-04-18 16:28:40 +0100217 if (! SEEN_EOL () && (expand
218 ? cpp_get_token (pfile)
219 : _cpp_lex_token (pfile))->type != CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000220 cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
Neil Boothebef4e82002-04-14 18:42:47 +0000221 pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +0000222}
223
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000224/* Ensure there are no stray tokens other than comments at the end of
225 a directive, and gather the comments. */
226static const cpp_token **
227check_eol_return_comments (cpp_reader *pfile)
228{
229 size_t c;
230 size_t capacity = 8;
231 const cpp_token **buf;
232
233 buf = XNEWVEC (const cpp_token *, capacity);
234 c = 0;
235 if (! SEEN_EOL ())
236 {
237 while (1)
238 {
239 const cpp_token *tok;
240
241 tok = _cpp_lex_token (pfile);
242 if (tok->type == CPP_EOF)
243 break;
244 if (tok->type != CPP_COMMENT)
245 cpp_error (pfile, CPP_DL_PEDWARN,
246 "extra tokens at end of #%s directive",
247 pfile->directive->name);
248 else
249 {
250 if (c + 1 >= capacity)
251 {
252 capacity *= 2;
253 buf = XRESIZEVEC (const cpp_token *, buf, capacity);
254 }
255 buf[c] = tok;
256 ++c;
257 }
258 }
259 }
260 buf[c] = NULL;
261 return buf;
262}
263
Neil Boothfe6c2db2000-11-15 19:25:22 +0000264/* Called when entering a directive, _Pragma or command-line directive. */
265static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000266start_directive (cpp_reader *pfile)
Zack Weinbergc5a04732000-04-25 19:32:36 +0000267{
Neil Booth7f2f1a62000-11-14 18:32:06 +0000268 /* Setup in-directive state. */
269 pfile->state.in_directive = 1;
270 pfile->state.save_comments = 0;
Zack Weinberg21b11492004-09-09 19:16:56 +0000271 pfile->directive_result.type = CPP_PADDING;
Neil Booth7f2f1a62000-11-14 18:32:06 +0000272
Neil Booth93c803682000-10-28 17:59:06 +0000273 /* Some handlers need the position of the # for diagnostics. */
Per Bothner500bee02004-04-22 19:22:27 -0700274 pfile->directive_line = pfile->line_table->highest_line;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000275}
276
277/* Called when leaving a directive, _Pragma or command-line directive. */
278static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000279end_directive (cpp_reader *pfile, int skip_line)
Neil Boothfe6c2db2000-11-15 19:25:22 +0000280{
Richard Hendersonbc4071d2006-01-04 08:33:38 -0800281 if (pfile->state.in_deferred_pragma)
282 ;
283 else if (CPP_OPTION (pfile, traditional))
Neil Booth1a769162002-06-11 05:36:17 +0000284 {
Neil Boothd97371e2002-06-18 06:27:40 +0000285 /* Revert change of prepare_directive_trad. */
286 pfile->state.prevent_expansion--;
287
Neil Boothb66377c2002-06-13 21:16:00 +0000288 if (pfile->directive != &dtable[T_DEFINE])
Neil Booth1a769162002-06-11 05:36:17 +0000289 _cpp_remove_overlay (pfile);
290 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000291 /* We don't skip for an assembler #. */
Neil Boothb66377c2002-06-13 21:16:00 +0000292 else if (skip_line)
Neil Booth67821e32001-08-05 17:31:25 +0000293 {
294 skip_rest_of_line (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +0000295 if (!pfile->keep_tokens)
296 {
297 pfile->cur_run = &pfile->base_run;
298 pfile->cur_token = pfile->base_run.base;
299 }
Neil Booth67821e32001-08-05 17:31:25 +0000300 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000301
302 /* Restore state. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000303 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
304 pfile->state.in_directive = 0;
Neil Boothd97371e2002-06-18 06:27:40 +0000305 pfile->state.in_expression = 0;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000306 pfile->state.angled_headers = 0;
307 pfile->directive = 0;
308}
309
Neil Booth1a769162002-06-11 05:36:17 +0000310/* Prepare to handle the directive in pfile->directive. */
311static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000312prepare_directive_trad (cpp_reader *pfile)
Neil Booth1a769162002-06-11 05:36:17 +0000313{
Neil Booth951a0762002-06-27 06:01:58 +0000314 if (pfile->directive != &dtable[T_DEFINE])
Neil Booth1a769162002-06-11 05:36:17 +0000315 {
Neil Boothb66377c2002-06-13 21:16:00 +0000316 bool no_expand = (pfile->directive
317 && ! (pfile->directive->flags & EXPAND));
Neil Booth974c43f2002-06-13 06:25:28 +0000318 bool was_skipping = pfile->state.skipping;
Neil Booth1a769162002-06-11 05:36:17 +0000319
Neil Boothd97371e2002-06-18 06:27:40 +0000320 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
321 || pfile->directive == &dtable[T_ELIF]);
Neil Booth45f24922003-12-12 07:00:29 +0000322 if (pfile->state.in_expression)
323 pfile->state.skipping = false;
324
Neil Booth1a769162002-06-11 05:36:17 +0000325 if (no_expand)
326 pfile->state.prevent_expansion++;
Zack Weinberg43839642003-07-13 17:34:18 +0000327 _cpp_scan_out_logical_line (pfile, NULL);
Neil Booth1a769162002-06-11 05:36:17 +0000328 if (no_expand)
329 pfile->state.prevent_expansion--;
Neil Booth45f24922003-12-12 07:00:29 +0000330
Neil Booth974c43f2002-06-13 06:25:28 +0000331 pfile->state.skipping = was_skipping;
Neil Booth1a769162002-06-11 05:36:17 +0000332 _cpp_overlay_buffer (pfile, pfile->out.base,
333 pfile->out.cur - pfile->out.base);
334 }
Neil Boothd97371e2002-06-18 06:27:40 +0000335
336 /* Stop ISO C from expanding anything. */
337 pfile->state.prevent_expansion++;
Neil Booth1a769162002-06-11 05:36:17 +0000338}
339
Kazu Hiratada7d8302002-09-22 02:03:17 +0000340/* Output diagnostics for a directive DIR. INDENTED is nonzero if
Neil Booth18a9d8f2001-09-16 11:23:56 +0000341 the '#' was indented. */
Neil Booth18a9d8f2001-09-16 11:23:56 +0000342static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000343directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000344{
Tom Tromey899015a2008-05-13 14:50:27 +0000345 /* Issue -pedantic or deprecated warnings for extensions. We let
346 -pedantic take precedence if both are applicable. */
347 if (! pfile->state.skipping)
348 {
349 if (dir->origin == EXTENSION
350 && !(dir == &dtable[T_IMPORT] && CPP_OPTION (pfile, objc))
351 && CPP_PEDANTIC (pfile))
352 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
353 else if (((dir->flags & DEPRECATED) != 0
354 || (dir == &dtable[T_IMPORT] && !CPP_OPTION (pfile, objc)))
355 && CPP_OPTION (pfile, warn_deprecated))
356 cpp_error (pfile, CPP_DL_WARNING, "#%s is a deprecated GCC extension",
357 dir->name);
358 }
Neil Booth18a9d8f2001-09-16 11:23:56 +0000359
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000360 /* Traditionally, a directive is ignored unless its # is in
361 column 1. Therefore in code intended to work with K+R
362 compilers, directives added by C89 must have their #
363 indented, and directives present in traditional C must not.
364 This is true even of directives in skipped conditional
365 blocks. #elif cannot be used at all. */
366 if (CPP_WTRADITIONAL (pfile))
367 {
368 if (dir == &dtable[T_ELIF])
John David Anglin0527bc42003-11-01 22:56:54 +0000369 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000370 "suggest not using #elif in traditional C");
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000371 else if (indented && dir->origin == KANDR)
John David Anglin0527bc42003-11-01 22:56:54 +0000372 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000373 "traditional C ignores #%s with the # indented",
374 dir->name);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000375 else if (!indented && dir->origin != KANDR)
John David Anglin0527bc42003-11-01 22:56:54 +0000376 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000377 "suggest hiding #%s from traditional C with an indented #",
378 dir->name);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000379 }
380}
381
Kazu Hiratada7d8302002-09-22 02:03:17 +0000382/* Check if we have a known directive. INDENTED is nonzero if the
Neil Booth18a9d8f2001-09-16 11:23:56 +0000383 '#' of the directive was indented. This function is in this file
Gabriel Dos Reisa2566ae2005-01-02 01:32:21 +0000384 to save unnecessarily exporting dtable etc. to lex.c. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +0000385 nonzero if the line of tokens has been handled, zero if we should
Neil Booth18a9d8f2001-09-16 11:23:56 +0000386 continue processing the line. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000387int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000388_cpp_handle_directive (cpp_reader *pfile, int indented)
Neil Boothfe6c2db2000-11-15 19:25:22 +0000389{
Neil Boothfe6c2db2000-11-15 19:25:22 +0000390 const directive *dir = 0;
Neil Booth345894b2001-09-16 13:44:29 +0000391 const cpp_token *dname;
Neil Boothe808ec92002-02-27 07:24:53 +0000392 bool was_parsing_args = pfile->state.parsing_args;
Zack Weinbergc6e83802004-06-05 20:58:06 +0000393 bool was_discarding_output = pfile->state.discarding_output;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000394 int skip = 1;
395
Zack Weinbergc6e83802004-06-05 20:58:06 +0000396 if (was_discarding_output)
397 pfile->state.prevent_expansion = 0;
398
Neil Boothe808ec92002-02-27 07:24:53 +0000399 if (was_parsing_args)
400 {
401 if (CPP_OPTION (pfile, pedantic))
John David Anglin0527bc42003-11-01 22:56:54 +0000402 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothe808ec92002-02-27 07:24:53 +0000403 "embedding a directive within macro arguments is not portable");
404 pfile->state.parsing_args = 0;
405 pfile->state.prevent_expansion = 0;
406 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000407 start_directive (pfile);
Neil Booth345894b2001-09-16 13:44:29 +0000408 dname = _cpp_lex_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000409
Neil Booth345894b2001-09-16 13:44:29 +0000410 if (dname->type == CPP_NAME)
Neil Booth0d9f2342000-09-18 18:43:05 +0000411 {
Joseph Myers9a0c6182009-05-10 15:27:32 +0100412 if (dname->val.node.node->is_directive)
413 dir = &dtable[dname->val.node.node->directive_index];
Neil Booth93c803682000-10-28 17:59:06 +0000414 }
Kazu Hirata05713b82002-09-15 18:24:08 +0000415 /* We do not recognize the # followed by a number extension in
Neil Booth18a9d8f2001-09-16 11:23:56 +0000416 assembler code. */
Neil Booth345894b2001-09-16 13:44:29 +0000417 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth93c803682000-10-28 17:59:06 +0000418 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000419 dir = &linemarker_dir;
420 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
421 && ! pfile->state.skipping)
John David Anglin0527bc42003-11-01 22:56:54 +0000422 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothebef4e82002-04-14 18:42:47 +0000423 "style of line directive is a GCC extension");
Neil Booth0d9f2342000-09-18 18:43:05 +0000424 }
425
Neil Booth93c803682000-10-28 17:59:06 +0000426 if (dir)
Neil Booth0d9f2342000-09-18 18:43:05 +0000427 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000428 /* If we have a directive that is not an opening conditional,
429 invalidate any control macro. */
430 if (! (dir->flags & IF_COND))
431 pfile->mi_valid = false;
Neil Booth93c803682000-10-28 17:59:06 +0000432
Neil Booth18a9d8f2001-09-16 11:23:56 +0000433 /* Kluge alert. In order to be sure that code like this
434
435 #define HASH #
436 HASH define foo bar
437
438 does not cause '#define foo bar' to get executed when
439 compiled with -save-temps, we recognize directives in
Gabriel Dos Reisa2566ae2005-01-02 01:32:21 +0000440 -fpreprocessed mode only if the # is in column 1. macro.c
Ollie Wildccfc4c92007-07-30 18:29:20 +0000441 puts a space in front of any '#' at the start of a macro.
442
443 We exclude the -fdirectives-only case because macro expansion
444 has not been performed yet, and block comments can cause spaces
445 to preceed the directive. */
Neil Booth18a9d8f2001-09-16 11:23:56 +0000446 if (CPP_OPTION (pfile, preprocessed)
Ollie Wildccfc4c92007-07-30 18:29:20 +0000447 && !CPP_OPTION (pfile, directives_only)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000448 && (indented || !(dir->flags & IN_I)))
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000449 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000450 skip = 0;
451 dir = 0;
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000452 }
453 else
Neil Booth93c803682000-10-28 17:59:06 +0000454 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000455 /* In failed conditional groups, all non-conditional
456 directives are ignored. Before doing that, whether
457 skipping or not, we should lex angle-bracketed headers
458 correctly, and maybe output some diagnostics. */
459 pfile->state.angled_headers = dir->flags & INCL;
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000460 pfile->state.directive_wants_padding = dir->flags & INCL;
Neil Booth18a9d8f2001-09-16 11:23:56 +0000461 if (! CPP_OPTION (pfile, preprocessed))
462 directive_diagnostics (pfile, dir, indented);
463 if (pfile->state.skipping && !(dir->flags & COND))
464 dir = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000465 }
466 }
Neil Booth345894b2001-09-16 13:44:29 +0000467 else if (dname->type == CPP_EOF)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000468 ; /* CPP_EOF is the "null directive". */
469 else
Neil Booth0d9f2342000-09-18 18:43:05 +0000470 {
Neil Booth93c803682000-10-28 17:59:06 +0000471 /* An unknown directive. Don't complain about it in assembly
472 source: we don't know where the comments are, and # may
473 introduce assembler pseudo-ops. Don't complain about invalid
474 directives in skipped conditional groups (6.10 p4). */
Neil Boothbdb05a72000-11-26 17:31:13 +0000475 if (CPP_OPTION (pfile, lang) == CLK_ASM)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000476 skip = 0;
477 else if (!pfile->state.skipping)
John David Anglin0527bc42003-11-01 22:56:54 +0000478 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
Neil Booth345894b2001-09-16 13:44:29 +0000479 cpp_token_as_text (pfile, dname));
Neil Booth0d9f2342000-09-18 18:43:05 +0000480 }
481
Neil Boothd1a58682002-06-28 06:26:54 +0000482 pfile->directive = dir;
483 if (CPP_OPTION (pfile, traditional))
484 prepare_directive_trad (pfile);
485
Neil Booth18a9d8f2001-09-16 11:23:56 +0000486 if (dir)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000487 pfile->directive->handler (pfile);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000488 else if (skip == 0)
489 _cpp_backup_tokens (pfile, 1);
490
491 end_directive (pfile, skip);
Jakub Jelinek765d6002008-01-25 10:01:27 +0100492 if (was_parsing_args && !pfile->state.in_deferred_pragma)
Neil Boothe808ec92002-02-27 07:24:53 +0000493 {
494 /* Restore state when within macro args. */
495 pfile->state.parsing_args = 2;
496 pfile->state.prevent_expansion = 1;
Neil Boothe808ec92002-02-27 07:24:53 +0000497 }
Zack Weinbergc6e83802004-06-05 20:58:06 +0000498 if (was_discarding_output)
499 pfile->state.prevent_expansion = 1;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000500 return skip;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000501}
502
Neil Booth93c803682000-10-28 17:59:06 +0000503/* Directive handler wrapper used by the command line option
Neil Booth26aea072003-04-19 00:22:51 +0000504 processor. BUF is \n terminated. */
Neil Booth93c803682000-10-28 17:59:06 +0000505static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000506run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000507{
Neil Booth562a5c22002-04-21 18:46:42 +0000508 cpp_push_buffer (pfile, (const uchar *) buf, count,
Per Bothner40de9f72003-10-02 07:30:34 +0000509 /* from_stage3 */ true);
Neil Booth0bda4762000-12-11 07:45:16 +0000510 start_directive (pfile);
Neil Booth26aea072003-04-19 00:22:51 +0000511
512 /* This is a short-term fix to prevent a leading '#' being
513 interpreted as a directive. */
514 _cpp_clean_line (pfile);
515
Neil Boothf71aebb2001-05-27 18:06:00 +0000516 pfile->directive = &dtable[dir_no];
Neil Booth1a769162002-06-11 05:36:17 +0000517 if (CPP_OPTION (pfile, traditional))
518 prepare_directive_trad (pfile);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000519 pfile->directive->handler (pfile);
Neil Booth0bda4762000-12-11 07:45:16 +0000520 end_directive (pfile, 1);
Neil Boothef6e9582001-08-04 12:01:59 +0000521 _cpp_pop_buffer (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000522}
Per Bothner7f2935c1995-03-16 13:59:07 -0800523
Neil Booth93c803682000-10-28 17:59:06 +0000524/* Checks for validity the macro name in #define, #undef, #ifdef and
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000525 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
526 processing a #define or #undefine directive, and false
527 otherwise. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000528static cpp_hashnode *
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000529lex_macro_node (cpp_reader *pfile, bool is_def_or_undef)
Zack Weinberg041c3192000-07-04 01:58:21 +0000530{
Neil Booth1a769162002-06-11 05:36:17 +0000531 const cpp_token *token = _cpp_lex_token (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000532
Zack Weinberg92936ec2000-07-19 20:18:08 +0000533 /* The token immediately after #define must be an identifier. That
Zack Weinbergb8363a22001-07-01 18:48:13 +0000534 identifier may not be "defined", per C99 6.10.8p4.
535 In C++, it may not be any of the "named operators" either,
536 per C++98 [lex.digraph], [lex.key].
537 Finally, the identifier may not have been poisoned. (In that case
Neil Booth1d63a282002-06-28 20:27:14 +0000538 the lexer has issued the error message for us.) */
Zack Weinbergb8363a22001-07-01 18:48:13 +0000539
Neil Booth1a769162002-06-11 05:36:17 +0000540 if (token->type == CPP_NAME)
541 {
Joseph Myers9a0c6182009-05-10 15:27:32 +0100542 cpp_hashnode *node = token->val.node.node;
Neil Booth1a769162002-06-11 05:36:17 +0000543
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000544 if (is_def_or_undef && node == pfile->spec_nodes.n_defined)
John David Anglin0527bc42003-11-01 22:56:54 +0000545 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth1a769162002-06-11 05:36:17 +0000546 "\"defined\" cannot be used as a macro name");
547 else if (! (node->flags & NODE_POISONED))
548 return node;
549 }
550 else if (token->flags & NAMED_OP)
John David Anglin0527bc42003-11-01 22:56:54 +0000551 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcbc69f82002-06-05 20:27:12 +0000552 "\"%s\" cannot be used as a macro name as it is an operator in C++",
Joseph Myers9a0c6182009-05-10 15:27:32 +0100553 NODE_NAME (token->val.node.node));
Neil Booth1a769162002-06-11 05:36:17 +0000554 else if (token->type == CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000555 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
Neil Booth1a769162002-06-11 05:36:17 +0000556 pfile->directive->name);
557 else
John David Anglin0527bc42003-11-01 22:56:54 +0000558 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
Zack Weinbergb8363a22001-07-01 18:48:13 +0000559
Neil Boothcbc69f82002-06-05 20:27:12 +0000560 return NULL;
Per Bothner7f2935c1995-03-16 13:59:07 -0800561}
Per Bothner7f2935c1995-03-16 13:59:07 -0800562
Gabriel Dos Reisa2566ae2005-01-02 01:32:21 +0000563/* Process a #define directive. Most work is done in macro.c. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000564static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000565do_define (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800566{
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000567 cpp_hashnode *node = lex_macro_node (pfile, true);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000568
Neil Booth93c803682000-10-28 17:59:06 +0000569 if (node)
570 {
Neil Booth1d63a282002-06-28 20:27:14 +0000571 /* If we have been requested to expand comments into macros,
572 then re-enable saving of comments. */
573 pfile->state.save_comments =
574 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
575
Joseph Myers93d45d92008-04-02 20:42:53 +0100576 if (pfile->cb.before_define)
577 pfile->cb.before_define (pfile);
578
Neil Booth93c803682000-10-28 17:59:06 +0000579 if (_cpp_create_definition (pfile, node))
580 if (pfile->cb.define)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000581 pfile->cb.define (pfile, pfile->directive_line, node);
Joseph Myers93d45d92008-04-02 20:42:53 +0100582
583 node->flags &= ~NODE_USED;
Neil Booth93c803682000-10-28 17:59:06 +0000584 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800585}
586
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000587/* Handle #undef. Mark the identifier NT_VOID in the hash table. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000588static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000589do_undef (cpp_reader *pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000590{
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000591 cpp_hashnode *node = lex_macro_node (pfile, true);
Zack Weinberg041c3192000-07-04 01:58:21 +0000592
Neil Booth45f24922003-12-12 07:00:29 +0000593 if (node)
Zack Weinberg041c3192000-07-04 01:58:21 +0000594 {
Joseph Myers93d45d92008-04-02 20:42:53 +0100595 if (pfile->cb.before_define)
596 pfile->cb.before_define (pfile);
597
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000598 if (pfile->cb.undef)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000599 pfile->cb.undef (pfile, pfile->directive_line, node);
Zack Weinberg041c3192000-07-04 01:58:21 +0000600
Neil Booth45f24922003-12-12 07:00:29 +0000601 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
602 identifier is not currently defined as a macro name. */
603 if (node->type == NT_MACRO)
604 {
605 if (node->flags & NODE_WARN)
606 cpp_error (pfile, CPP_DL_WARNING,
607 "undefining \"%s\"", NODE_NAME (node));
Zack Weinberg041c3192000-07-04 01:58:21 +0000608
Neil Booth45f24922003-12-12 07:00:29 +0000609 if (CPP_OPTION (pfile, warn_unused_macros))
610 _cpp_warn_if_unused_macro (pfile, node, NULL);
Neil Bootha69cbaa2002-07-23 22:57:49 +0000611
Neil Booth45f24922003-12-12 07:00:29 +0000612 _cpp_free_definition (node);
613 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000614 }
Neil Booth45f24922003-12-12 07:00:29 +0000615
Joseph Myersa5cb5632009-04-18 16:28:40 +0100616 check_eol (pfile, false);
Zack Weinberg041c3192000-07-04 01:58:21 +0000617}
618
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000619/* Undefine a single macro/assertion/whatever. */
620
621static int
Zack Weinbergc6e83802004-06-05 20:58:06 +0000622undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000623 void *data_p ATTRIBUTE_UNUSED)
624{
Zack Weinbergc6e83802004-06-05 20:58:06 +0000625 /* Body of _cpp_free_definition inlined here for speed.
626 Macros and assertions no longer have anything to free. */
627 h->type = NT_VOID;
Joseph Myers93d45d92008-04-02 20:42:53 +0100628 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000629 return 1;
630}
631
632/* Undefine all macros and assertions. */
633
634void
635cpp_undef_all (cpp_reader *pfile)
636{
637 cpp_forall_identifiers (pfile, undefine_macros, NULL);
638}
639
640
Neil Booth93c803682000-10-28 17:59:06 +0000641/* Helper routine used by parse_include. Reinterpret the current line
642 as an h-char-sequence (< ... >); we are looking at the first token
Neil Booth74eb4b32003-04-21 19:21:59 +0000643 after the <. Returns a malloced filename. */
644static char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000645glue_header_name (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800646{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000647 const cpp_token *token;
Neil Booth74eb4b32003-04-21 19:21:59 +0000648 char *buffer;
Neil Booth2450e0b2002-02-23 20:21:39 +0000649 size_t len, total_len = 0, capacity = 1024;
Per Bothner7f2935c1995-03-16 13:59:07 -0800650
Neil Booth93c803682000-10-28 17:59:06 +0000651 /* To avoid lexed tokens overwriting our glued name, we can only
652 allocate from the string pool once we've lexed everything. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000653 buffer = XNEWVEC (char, capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000654 for (;;)
Zack Weinberg168d3732000-03-14 06:34:11 +0000655 {
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000656 token = get_token_no_padding (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000657
Neil Booth74eb4b32003-04-21 19:21:59 +0000658 if (token->type == CPP_GREATER)
Neil Booth93c803682000-10-28 17:59:06 +0000659 break;
Neil Booth74eb4b32003-04-21 19:21:59 +0000660 if (token->type == CPP_EOF)
661 {
John David Anglin0527bc42003-11-01 22:56:54 +0000662 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
Neil Booth74eb4b32003-04-21 19:21:59 +0000663 break;
664 }
Neil Booth93c803682000-10-28 17:59:06 +0000665
Neil Booth59325652003-04-24 20:03:57 +0000666 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
Neil Booth2450e0b2002-02-23 20:21:39 +0000667 if (total_len + len > capacity)
Neil Booth93c803682000-10-28 17:59:06 +0000668 {
Neil Booth2450e0b2002-02-23 20:21:39 +0000669 capacity = (capacity + len) * 2;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000670 buffer = XRESIZEVEC (char, buffer, capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000671 }
672
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000673 if (token->flags & PREV_WHITE)
Neil Booth2450e0b2002-02-23 20:21:39 +0000674 buffer[total_len++] = ' ';
Neil Booth93c803682000-10-28 17:59:06 +0000675
Geoffrey Keating47e20492005-03-12 10:44:06 +0000676 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
677 true)
Neil Booth74eb4b32003-04-21 19:21:59 +0000678 - (uchar *) buffer);
Neil Booth93c803682000-10-28 17:59:06 +0000679 }
680
Neil Booth74eb4b32003-04-21 19:21:59 +0000681 buffer[total_len] = '\0';
682 return buffer;
Neil Booth93c803682000-10-28 17:59:06 +0000683}
684
Neil Booth74eb4b32003-04-21 19:21:59 +0000685/* Returns the file name of #include, #include_next, #import and
686 #pragma dependency. The string is malloced and the caller should
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +0000687 free it. Returns NULL on error. LOCATION is the source location
688 of the file name. */
689
Neil Booth74eb4b32003-04-21 19:21:59 +0000690static const char *
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000691parse_include (cpp_reader *pfile, int *pangle_brackets,
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +0000692 const cpp_token ***buf, source_location *location)
Neil Booth93c803682000-10-28 17:59:06 +0000693{
Neil Booth74eb4b32003-04-21 19:21:59 +0000694 char *fname;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000695 const cpp_token *header;
Neil Booth93c803682000-10-28 17:59:06 +0000696
Neil Booth93c803682000-10-28 17:59:06 +0000697 /* Allow macro expansion. */
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000698 header = get_token_no_padding (pfile);
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +0000699 *location = header->src_loc;
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +0200700 if ((header->type == CPP_STRING && header->val.str.text[0] != 'R')
701 || header->type == CPP_HEADER_NAME)
Neil Booth93c803682000-10-28 17:59:06 +0000702 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000703 fname = XNEWVEC (char, header->val.str.len - 1);
Neil Booth6338b352003-04-23 22:44:06 +0000704 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
705 fname[header->val.str.len - 2] = '\0';
Neil Booth74eb4b32003-04-21 19:21:59 +0000706 *pangle_brackets = header->type == CPP_HEADER_NAME;
Zack Weinberg041c3192000-07-04 01:58:21 +0000707 }
Neil Booth74eb4b32003-04-21 19:21:59 +0000708 else if (header->type == CPP_LESS)
Zack Weinberg041c3192000-07-04 01:58:21 +0000709 {
Neil Booth74eb4b32003-04-21 19:21:59 +0000710 fname = glue_header_name (pfile);
711 *pangle_brackets = 1;
712 }
713 else
714 {
715 const unsigned char *dir;
716
717 if (pfile->directive == &dtable[T_PRAGMA])
Kris Van Heesb6baa672008-04-18 13:58:08 +0000718 dir = UC"pragma dependency";
Neil Booth74eb4b32003-04-21 19:21:59 +0000719 else
720 dir = pfile->directive->name;
John David Anglin0527bc42003-11-01 22:56:54 +0000721 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
Neil Booth74eb4b32003-04-21 19:21:59 +0000722 dir);
723
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000724 return NULL;
Richard Kennercfb3ee11997-04-13 14:30:13 -0400725 }
726
Tom Tromeycda5e672007-08-18 17:54:11 +0000727 if (pfile->directive == &dtable[T_PRAGMA])
728 {
729 /* This pragma allows extra tokens after the file name. */
730 }
731 else if (buf == NULL || CPP_OPTION (pfile, discard_comments))
Joseph Myers61cc8222009-04-18 21:25:07 +0100732 check_eol (pfile, true);
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000733 else
734 {
735 /* If we are not discarding comments, then gather them while
736 doing the eol check. */
737 *buf = check_eol_return_comments (pfile);
738 }
739
Neil Booth74eb4b32003-04-21 19:21:59 +0000740 return fname;
Zack Weinberg168d3732000-03-14 06:34:11 +0000741}
742
Neil Boothba133c92001-03-15 07:57:13 +0000743/* Handle #include, #include_next and #import. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000744static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000745do_include_common (cpp_reader *pfile, enum include_type type)
Zack Weinberg168d3732000-03-14 06:34:11 +0000746{
Neil Booth74eb4b32003-04-21 19:21:59 +0000747 const char *fname;
748 int angle_brackets;
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000749 const cpp_token **buf = NULL;
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +0000750 source_location location;
Neil Booth74eb4b32003-04-21 19:21:59 +0000751
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000752 /* Re-enable saving of comments if requested, so that the include
753 callback can dump comments which follow #include. */
754 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
755
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +0000756 fname = parse_include (pfile, &angle_brackets, &buf, &location);
Neil Booth74eb4b32003-04-21 19:21:59 +0000757 if (!fname)
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000758 {
759 if (buf)
760 XDELETEVEC (buf);
761 return;
762 }
Zack Weinberg168d3732000-03-14 06:34:11 +0000763
Nathanael Nerode28303822004-11-28 22:28:13 +0000764 if (!*fname)
765 {
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +0000766 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
767 "empty filename in #%s",
768 pfile->directive->name);
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000769 XDELETEVEC (fname);
770 if (buf)
771 XDELETEVEC (buf);
Nathanael Nerode28303822004-11-28 22:28:13 +0000772 return;
773 }
774
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000775 /* Prevent #include recursion. */
Per Bothner50f59cd2004-01-19 21:30:18 -0800776 if (pfile->line_table->depth >= CPP_STACK_MAX)
John David Anglin0527bc42003-11-01 22:56:54 +0000777 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
Neil Booth74eb4b32003-04-21 19:21:59 +0000778 else
Neil Booth09b82252001-07-29 22:27:20 +0000779 {
Neil Booth74eb4b32003-04-21 19:21:59 +0000780 /* Get out of macro context, if we are. */
781 skip_rest_of_line (pfile);
782
783 if (pfile->cb.include)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000784 pfile->cb.include (pfile, pfile->directive_line,
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000785 pfile->directive->name, fname, angle_brackets,
786 buf);
Neil Booth74eb4b32003-04-21 19:21:59 +0000787
Neil Booth8f9b4002003-07-29 22:26:13 +0000788 _cpp_stack_include (pfile, fname, angle_brackets, type);
Neil Booth09b82252001-07-29 22:27:20 +0000789 }
790
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000791 XDELETEVEC (fname);
792 if (buf)
793 XDELETEVEC (buf);
Neil Boothba133c92001-03-15 07:57:13 +0000794}
795
796static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000797do_include (cpp_reader *pfile)
Neil Boothba133c92001-03-15 07:57:13 +0000798{
799 do_include_common (pfile, IT_INCLUDE);
Zack Weinberg168d3732000-03-14 06:34:11 +0000800}
801
Zack Weinberg711b8822000-07-18 00:59:49 +0000802static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000803do_import (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +0000804{
Neil Boothba133c92001-03-15 07:57:13 +0000805 do_include_common (pfile, IT_IMPORT);
Zack Weinberg168d3732000-03-14 06:34:11 +0000806}
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000807
Zack Weinberg711b8822000-07-18 00:59:49 +0000808static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000809do_include_next (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +0000810{
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000811 enum include_type type = IT_INCLUDE_NEXT;
812
813 /* If this is the primary source file, warn and use the normal
814 search logic. */
Tom Tromey705e2d22007-01-04 15:32:26 +0000815 if (cpp_in_primary_file (pfile))
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000816 {
John David Anglin0527bc42003-11-01 22:56:54 +0000817 cpp_error (pfile, CPP_DL_WARNING,
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000818 "#include_next in primary source file");
819 type = IT_INCLUDE;
820 }
821 do_include_common (pfile, type);
Per Bothner7f2935c1995-03-16 13:59:07 -0800822}
823
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000824/* Subroutine of do_linemarker. Read possible flags after file name.
825 LAST is the last flag seen; 0 if this is the first flag. Return the
826 flag if it is valid, 0 at the end of the directive. Otherwise
827 complain. */
Neil Booth642ce432000-12-07 23:17:56 +0000828static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000829read_flag (cpp_reader *pfile, unsigned int last)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000830{
Neil Booth345894b2001-09-16 13:44:29 +0000831 const cpp_token *token = _cpp_lex_token (pfile);
Jason Merrilld3a34a01999-08-14 00:42:07 +0000832
Neil Booth345894b2001-09-16 13:44:29 +0000833 if (token->type == CPP_NUMBER && token->val.str.len == 1)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000834 {
Neil Booth345894b2001-09-16 13:44:29 +0000835 unsigned int flag = token->val.str.text[0] - '0';
Neil Booth28e0f042000-12-09 12:06:37 +0000836
837 if (flag > last && flag <= 4
838 && (flag != 4 || last == 3)
839 && (flag != 2 || last == 0))
840 return flag;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000841 }
Neil Booth93c803682000-10-28 17:59:06 +0000842
Neil Booth345894b2001-09-16 13:44:29 +0000843 if (token->type != CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000844 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
Neil Booth345894b2001-09-16 13:44:29 +0000845 cpp_token_as_text (pfile, token));
Neil Booth93c803682000-10-28 17:59:06 +0000846 return 0;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000847}
848
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000849/* Subroutine of do_line and do_linemarker. Convert a number in STR,
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000850 of length LEN, to binary; store it in NUMP, and return false if the
851 number was well-formed, true if not. WRAPPED is set to true if the
852 number did not fit into 'unsigned long'. */
853static bool
854strtolinenum (const uchar *str, size_t len, linenum_type *nump, bool *wrapped)
Zack Weinberg041c3192000-07-04 01:58:21 +0000855{
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000856 linenum_type reg = 0;
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000857 linenum_type reg_prev = 0;
858
Neil Booth562a5c22002-04-21 18:46:42 +0000859 uchar c;
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000860 *wrapped = false;
Zack Weinberg041c3192000-07-04 01:58:21 +0000861 while (len--)
862 {
863 c = *str++;
864 if (!ISDIGIT (c))
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000865 return true;
Zack Weinberg041c3192000-07-04 01:58:21 +0000866 reg *= 10;
867 reg += c - '0';
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000868 if (reg < reg_prev)
869 *wrapped = true;
870 reg_prev = reg;
Zack Weinberg041c3192000-07-04 01:58:21 +0000871 }
872 *nump = reg;
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000873 return false;
Zack Weinberg041c3192000-07-04 01:58:21 +0000874}
875
Zack Weinberg5538ada1999-02-04 06:36:54 -0500876/* Interpret #line command.
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000877 Note that the filename string (if any) is a true string constant
878 (escapes are interpreted), unlike in #line. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000879static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000880do_line (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800881{
Per Bothner500bee02004-04-22 19:22:27 -0700882 const struct line_maps *line_table = pfile->line_table;
883 const struct line_map *map = &line_table->maps[line_table->used - 1];
Devang Patel2203a882005-02-28 11:04:19 -0800884
885 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
886 sysp right now. */
887
888 unsigned char map_sysp = map->sysp;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000889 const cpp_token *token;
Per Bothner12f9df42004-02-11 07:29:30 -0800890 const char *new_file = map->to_file;
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000891 linenum_type new_lineno;
Per Bothner7f2935c1995-03-16 13:59:07 -0800892
Neil Booth27e25642000-11-27 08:00:04 +0000893 /* C99 raised the minimum limit on #line numbers. */
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000894 linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000895 bool wrapped;
Neil Booth18a9d8f2001-09-16 11:23:56 +0000896
Neil Booth93c803682000-10-28 17:59:06 +0000897 /* #line commands expand macros. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000898 token = cpp_get_token (pfile);
899 if (token->type != CPP_NUMBER
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000900 || strtolinenum (token->val.str.text, token->val.str.len,
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000901 &new_lineno, &wrapped))
Per Bothner7f2935c1995-03-16 13:59:07 -0800902 {
Tom Tromey33ae4832008-01-03 17:58:26 +0000903 if (token->type == CPP_EOF)
904 cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line");
905 else
906 cpp_error (pfile, CPP_DL_ERROR,
907 "\"%s\" after #line is not a positive integer",
908 cpp_token_as_text (pfile, token));
Zack Weinberg9ec72912000-08-09 19:41:12 +0000909 return;
Kazu Hiratadf383482002-05-22 22:02:16 +0000910 }
Zack Weinberg5538ada1999-02-04 06:36:54 -0500911
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000912 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap || wrapped))
John David Anglin0527bc42003-11-01 22:56:54 +0000913 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000914 else if (wrapped)
915 cpp_error (pfile, CPP_DL_WARNING, "line number out of range");
Zack Weinberg5538ada1999-02-04 06:36:54 -0500916
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000917 token = cpp_get_token (pfile);
918 if (token->type == CPP_STRING)
Zack Weinberg5538ada1999-02-04 06:36:54 -0500919 {
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000920 cpp_string s = { 0, 0 };
Eric Christopher423e95e2004-02-12 02:25:03 +0000921 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
Jerry Quinnf1bf4102009-07-18 03:22:16 +0000922 &s, CPP_STRING))
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000923 new_file = (const char *)s.text;
Joseph Myersa5cb5632009-04-18 16:28:40 +0100924 check_eol (pfile, true);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000925 }
926 else if (token->type != CPP_EOF)
927 {
John David Anglin0527bc42003-11-01 22:56:54 +0000928 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000929 cpp_token_as_text (pfile, token));
930 return;
931 }
Neil Booth93c803682000-10-28 17:59:06 +0000932
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000933 skip_rest_of_line (pfile);
Joseph Myersc7f9c0b2009-04-18 18:36:28 +0100934 _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, new_file, new_lineno,
Devang Patel2203a882005-02-28 11:04:19 -0800935 map_sysp);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000936}
937
938/* Interpret the # 44 "file" [flags] notation, which has slightly
939 different syntax and semantics from #line: Flags are allowed,
940 and we never complain about the line number being too big. */
941static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000942do_linemarker (cpp_reader *pfile)
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000943{
Per Bothner500bee02004-04-22 19:22:27 -0700944 const struct line_maps *line_table = pfile->line_table;
945 const struct line_map *map = &line_table->maps[line_table->used - 1];
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000946 const cpp_token *token;
Per Bothner12f9df42004-02-11 07:29:30 -0800947 const char *new_file = map->to_file;
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000948 linenum_type new_lineno;
Per Bothner12f9df42004-02-11 07:29:30 -0800949 unsigned int new_sysp = map->sysp;
Joseph Myersc7f9c0b2009-04-18 18:36:28 +0100950 enum lc_reason reason = LC_RENAME_VERBATIM;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000951 int flag;
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000952 bool wrapped;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000953
954 /* Back up so we can get the number again. Putting this in
955 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
956 some circumstances, which can segfault. */
957 _cpp_backup_tokens (pfile, 1);
958
959 /* #line commands expand macros. */
960 token = cpp_get_token (pfile);
961 if (token->type != CPP_NUMBER
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000962 || strtolinenum (token->val.str.text, token->val.str.len,
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000963 &new_lineno, &wrapped))
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000964 {
Tom Tromey33ae4832008-01-03 17:58:26 +0000965 /* Unlike #line, there does not seem to be a way to get an EOF
966 here. So, it should be safe to always spell the token. */
John David Anglin0527bc42003-11-01 22:56:54 +0000967 cpp_error (pfile, CPP_DL_ERROR,
968 "\"%s\" after # is not a positive integer",
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000969 cpp_token_as_text (pfile, token));
970 return;
Kazu Hiratadf383482002-05-22 22:02:16 +0000971 }
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000972
973 token = cpp_get_token (pfile);
974 if (token->type == CPP_STRING)
975 {
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000976 cpp_string s = { 0, 0 };
Eric Christopher423e95e2004-02-12 02:25:03 +0000977 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
Jerry Quinnf1bf4102009-07-18 03:22:16 +0000978 1, &s, CPP_STRING))
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000979 new_file = (const char *)s.text;
Eric Christophercf551fb2004-01-16 22:37:49 +0000980
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000981 new_sysp = 0;
982 flag = read_flag (pfile, 0);
983 if (flag == 1)
Neil Booth93c803682000-10-28 17:59:06 +0000984 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000985 reason = LC_ENTER;
986 /* Fake an include for cpp_included (). */
987 _cpp_fake_include (pfile, new_file);
988 flag = read_flag (pfile, flag);
Neil Booth93c803682000-10-28 17:59:06 +0000989 }
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000990 else if (flag == 2)
991 {
992 reason = LC_LEAVE;
993 flag = read_flag (pfile, flag);
994 }
995 if (flag == 3)
996 {
997 new_sysp = 1;
998 flag = read_flag (pfile, flag);
999 if (flag == 4)
1000 new_sysp = 2;
1001 }
Jakub Jelinek9d30f272006-12-29 09:15:08 +01001002 pfile->buffer->sysp = new_sysp;
Zack Weinbergdcc229e2002-03-14 18:17:18 +00001003
Joseph Myersa5cb5632009-04-18 16:28:40 +01001004 check_eol (pfile, false);
Zack Weinbergff2b53e2000-04-06 07:56:14 +00001005 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001006 else if (token->type != CPP_EOF)
Neil Booth27e25642000-11-27 08:00:04 +00001007 {
John David Anglin0527bc42003-11-01 22:56:54 +00001008 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001009 cpp_token_as_text (pfile, token));
Neil Booth27e25642000-11-27 08:00:04 +00001010 return;
1011 }
Zack Weinberg941e09b1998-12-15 11:17:06 +00001012
Neil Boothbdcbe492001-09-13 20:05:17 +00001013 skip_rest_of_line (pfile);
Manuel López-Ibáñez00b0c192009-05-13 23:17:55 +00001014
1015 /* Compensate for the increment in linemap_add that occurs in
1016 _cpp_do_file_change. We're currently at the start of the line
1017 *following* the #line directive. A separate source_location for this
1018 location makes no sense (until we do the LC_LEAVE), and
1019 complicates LAST_SOURCE_LINE_LOCATION. */
1020 pfile->line_table->highest_location--;
1021
Neil Boothbb74c962001-08-17 22:23:49 +00001022 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
Neil Booth27e25642000-11-27 08:00:04 +00001023}
1024
Neil Booth67821e32001-08-05 17:31:25 +00001025/* Arrange the file_change callback. pfile->line has changed to
Neil Booth47d89cf2001-08-11 07:33:39 +00001026 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
Joseph Myersa1f300c2001-11-23 02:05:19 +00001027 header, 2 for a system header that needs to be extern "C" protected,
Neil Booth47d89cf2001-08-11 07:33:39 +00001028 and zero otherwise. */
Neil Bootheb1f4d92000-12-18 19:00:26 +00001029void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001030_cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +00001031 const char *to_file, linenum_type file_line,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001032 unsigned int sysp)
Neil Booth27e25642000-11-27 08:00:04 +00001033{
Per Bothner12f9df42004-02-11 07:29:30 -08001034 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
1035 to_file, file_line);
Per Bothner500bee02004-04-22 19:22:27 -07001036 if (map != NULL)
1037 linemap_line_start (pfile->line_table, map->to_line, 127);
Neil Boothd82fc102001-08-02 23:03:31 +00001038
Neil Bootheb1f4d92000-12-18 19:00:26 +00001039 if (pfile->cb.file_change)
Per Bothner12f9df42004-02-11 07:29:30 -08001040 pfile->cb.file_change (pfile, map);
Per Bothner7f2935c1995-03-16 13:59:07 -08001041}
Zack Weinberg941e09b1998-12-15 11:17:06 +00001042
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001043/* Report a warning or error detected by the program we are
1044 processing. Use the directive's tokens in the error message. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001045static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001046do_diagnostic (cpp_reader *pfile, int code, int print_dir)
Per Bothner7f2935c1995-03-16 13:59:07 -08001047{
Tom Tromey5d6342e2008-05-21 21:52:57 +00001048 const unsigned char *dir_name;
1049 unsigned char *line;
1050 source_location src_loc = pfile->cur_token[-1].src_loc;
1051
1052 if (print_dir)
1053 dir_name = pfile->directive->name;
1054 else
1055 dir_name = NULL;
1056 pfile->state.prevent_expansion++;
1057 line = cpp_output_line_to_string (pfile, dir_name);
1058 pfile->state.prevent_expansion--;
1059
1060 cpp_error_with_line (pfile, code, src_loc, 0, "%s", line);
1061 free (line);
Per Bothner7f2935c1995-03-16 13:59:07 -08001062}
1063
Neil Booth838f3132000-09-24 10:42:09 +00001064static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001065do_error (cpp_reader *pfile)
Neil Booth838f3132000-09-24 10:42:09 +00001066{
John David Anglin0527bc42003-11-01 22:56:54 +00001067 do_diagnostic (pfile, CPP_DL_ERROR, 1);
Neil Booth838f3132000-09-24 10:42:09 +00001068}
Per Bothner7f2935c1995-03-16 13:59:07 -08001069
Zack Weinberg711b8822000-07-18 00:59:49 +00001070static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001071do_warning (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001072{
Neil Booth2f878972001-04-08 10:01:18 +00001073 /* We want #warning diagnostics to be emitted in system headers too. */
John David Anglin0527bc42003-11-01 22:56:54 +00001074 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
Per Bothner7f2935c1995-03-16 13:59:07 -08001075}
1076
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001077/* Report program identification. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001078static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001079do_ident (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001080{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001081 const cpp_token *str = cpp_get_token (pfile);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001082
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001083 if (str->type != CPP_STRING)
Zack Weinberg1ed17cd2005-05-12 18:31:38 +00001084 cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
1085 pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +00001086 else if (pfile->cb.ident)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001087 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001088
Joseph Myersa5cb5632009-04-18 16:28:40 +01001089 check_eol (pfile, false);
Per Bothner7f2935c1995-03-16 13:59:07 -08001090}
1091
Neil Bootha5da89c2001-10-14 17:44:00 +00001092/* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1093 matching entry, or NULL if none is found. The returned entry could
1094 be the start of a namespace chain, or a pragma. */
1095static struct pragma_entry *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001096lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
Nathan Sidwell82443372000-06-23 10:56:09 +00001097{
Neil Booth4b115ff2001-10-14 23:04:13 +00001098 while (chain && chain->pragma != pragma)
1099 chain = chain->next;
Neil Bootha5da89c2001-10-14 17:44:00 +00001100
1101 return chain;
1102}
1103
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001104/* Create and insert a blank pragma entry at the beginning of a
1105 singly-linked CHAIN. */
Neil Bootha5da89c2001-10-14 17:44:00 +00001106static struct pragma_entry *
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001107new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain)
Neil Bootha5da89c2001-10-14 17:44:00 +00001108{
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001109 struct pragma_entry *new_entry;
Neil Bootha5da89c2001-10-14 17:44:00 +00001110
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001111 new_entry = (struct pragma_entry *)
Neil Bootha5da89c2001-10-14 17:44:00 +00001112 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
Neil Bootha5da89c2001-10-14 17:44:00 +00001113
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001114 memset (new_entry, 0, sizeof (struct pragma_entry));
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001115 new_entry->next = *chain;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001116
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001117 *chain = new_entry;
1118 return new_entry;
Neil Bootha5da89c2001-10-14 17:44:00 +00001119}
1120
1121/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001122 goes in the global namespace. */
1123static struct pragma_entry *
1124register_pragma_1 (cpp_reader *pfile, const char *space, const char *name,
1125 bool allow_name_expansion)
Nathan Sidwell82443372000-06-23 10:56:09 +00001126{
Neil Bootha5da89c2001-10-14 17:44:00 +00001127 struct pragma_entry **chain = &pfile->pragmas;
1128 struct pragma_entry *entry;
Neil Booth4b115ff2001-10-14 23:04:13 +00001129 const cpp_hashnode *node;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001130
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001131 if (space)
1132 {
Kris Van Heesb6baa672008-04-18 13:58:08 +00001133 node = cpp_lookup (pfile, UC space, strlen (space));
Neil Booth4b115ff2001-10-14 23:04:13 +00001134 entry = lookup_pragma_entry (*chain, node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001135 if (!entry)
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001136 {
1137 entry = new_pragma_entry (pfile, chain);
1138 entry->pragma = node;
1139 entry->is_nspace = true;
1140 entry->allow_expansion = allow_name_expansion;
1141 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001142 else if (!entry->is_nspace)
1143 goto clash;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001144 else if (entry->allow_expansion != allow_name_expansion)
1145 {
1146 cpp_error (pfile, CPP_DL_ICE,
1147 "registering pragmas in namespace \"%s\" with mismatched "
1148 "name expansion", space);
1149 return NULL;
1150 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001151 chain = &entry->u.space;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001152 }
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001153 else if (allow_name_expansion)
1154 {
1155 cpp_error (pfile, CPP_DL_ICE,
1156 "registering pragma \"%s\" with name expansion "
1157 "and no namespace", name);
1158 return NULL;
1159 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001160
Neil Bootha5da89c2001-10-14 17:44:00 +00001161 /* Check for duplicates. */
Kris Van Heesb6baa672008-04-18 13:58:08 +00001162 node = cpp_lookup (pfile, UC name, strlen (name));
Neil Booth4b115ff2001-10-14 23:04:13 +00001163 entry = lookup_pragma_entry (*chain, node);
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001164 if (entry == NULL)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001165 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001166 entry = new_pragma_entry (pfile, chain);
1167 entry->pragma = node;
1168 return entry;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001169 }
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001170
1171 if (entry->is_nspace)
1172 clash:
1173 cpp_error (pfile, CPP_DL_ICE,
1174 "registering \"%s\" as both a pragma and a pragma namespace",
1175 NODE_NAME (node));
1176 else if (space)
1177 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1178 space, name);
Neil Bootha5da89c2001-10-14 17:44:00 +00001179 else
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001180 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1181
1182 return NULL;
1183}
1184
1185/* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1186static void
1187register_pragma_internal (cpp_reader *pfile, const char *space,
1188 const char *name, pragma_cb handler)
1189{
1190 struct pragma_entry *entry;
1191
1192 entry = register_pragma_1 (pfile, space, name, false);
1193 entry->is_internal = true;
1194 entry->u.handler = handler;
Zack Weinberg21b11492004-09-09 19:16:56 +00001195}
1196
1197/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1198 goes in the global namespace. HANDLER is the handler it will call,
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001199 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1200 expansion while parsing pragma NAME. This function is exported
1201 from libcpp. */
Zack Weinberg21b11492004-09-09 19:16:56 +00001202void
1203cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001204 pragma_cb handler, bool allow_expansion)
Zack Weinberg21b11492004-09-09 19:16:56 +00001205{
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001206 struct pragma_entry *entry;
1207
1208 if (!handler)
1209 {
1210 cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler");
1211 return;
1212 }
1213
1214 entry = register_pragma_1 (pfile, space, name, false);
1215 if (entry)
1216 {
1217 entry->allow_expansion = allow_expansion;
1218 entry->u.handler = handler;
1219 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001220}
Neil Bootha5da89c2001-10-14 17:44:00 +00001221
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001222/* Similarly, but create mark the pragma for deferred processing.
1223 When found, a CPP_PRAGMA token will be insertted into the stream
1224 with IDENT in the token->u.pragma slot. */
1225void
1226cpp_register_deferred_pragma (cpp_reader *pfile, const char *space,
1227 const char *name, unsigned int ident,
1228 bool allow_expansion, bool allow_name_expansion)
1229{
1230 struct pragma_entry *entry;
1231
1232 entry = register_pragma_1 (pfile, space, name, allow_name_expansion);
1233 if (entry)
1234 {
1235 entry->is_deferred = true;
1236 entry->allow_expansion = allow_expansion;
1237 entry->u.ident = ident;
1238 }
1239}
1240
Neil Bootha5da89c2001-10-14 17:44:00 +00001241/* Register the pragmas the preprocessor itself handles. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001242void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001243_cpp_init_internal_pragmas (cpp_reader *pfile)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001244{
Neil Bootha5da89c2001-10-14 17:44:00 +00001245 /* Pragmas in the global namespace. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001246 register_pragma_internal (pfile, 0, "once", do_pragma_once);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001247
Neil Bootha5da89c2001-10-14 17:44:00 +00001248 /* New GCC-specific pragmas should be put in the GCC namespace. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001249 register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison);
1250 register_pragma_internal (pfile, "GCC", "system_header",
1251 do_pragma_system_header);
1252 register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
Nathan Sidwell82443372000-06-23 10:56:09 +00001253}
Per Bothner7f2935c1995-03-16 13:59:07 -08001254
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001255/* Return the number of registered pragmas in PE. */
1256
1257static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001258count_registered_pragmas (struct pragma_entry *pe)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001259{
1260 int ct = 0;
1261 for (; pe != NULL; pe = pe->next)
1262 {
1263 if (pe->is_nspace)
1264 ct += count_registered_pragmas (pe->u.space);
1265 ct++;
1266 }
1267 return ct;
1268}
1269
1270/* Save into SD the names of the registered pragmas referenced by PE,
1271 and return a pointer to the next free space in SD. */
1272
1273static char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001274save_registered_pragmas (struct pragma_entry *pe, char **sd)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001275{
1276 for (; pe != NULL; pe = pe->next)
1277 {
1278 if (pe->is_nspace)
1279 sd = save_registered_pragmas (pe->u.space, sd);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001280 *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
1281 HT_LEN (&pe->pragma->ident),
1282 HT_LEN (&pe->pragma->ident) + 1);
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001283 }
1284 return sd;
1285}
1286
1287/* Return a newly-allocated array which saves the names of the
1288 registered pragmas. */
1289
1290char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001291_cpp_save_pragma_names (cpp_reader *pfile)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001292{
1293 int ct = count_registered_pragmas (pfile->pragmas);
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001294 char **result = XNEWVEC (char *, ct);
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001295 (void) save_registered_pragmas (pfile->pragmas, result);
1296 return result;
1297}
1298
1299/* Restore from SD the names of the registered pragmas referenced by PE,
1300 and return a pointer to the next unused name in SD. */
1301
1302static char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001303restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1304 char **sd)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001305{
1306 for (; pe != NULL; pe = pe->next)
1307 {
1308 if (pe->is_nspace)
1309 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
Kris Van Heesb6baa672008-04-18 13:58:08 +00001310 pe->pragma = cpp_lookup (pfile, UC *sd, strlen (*sd));
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001311 free (*sd);
1312 sd++;
1313 }
1314 return sd;
1315}
1316
1317/* Restore the names of the registered pragmas from SAVED. */
1318
1319void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001320_cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001321{
1322 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1323 free (saved);
1324}
1325
Neil Bootha5da89c2001-10-14 17:44:00 +00001326/* Pragmata handling. We handle some, and pass the rest on to the
1327 front end. C99 defines three pragmas and says that no macro
1328 expansion is to be performed on them; whether or not macro
1329 expansion happens for other pragmas is implementation defined.
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001330 This implementation allows for a mix of both, since GCC did not
1331 traditionally macro expand its (few) pragmas, whereas OpenMP
1332 specifies that macro expansion should happen. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001333static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001334do_pragma (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001335{
Neil Bootha5da89c2001-10-14 17:44:00 +00001336 const struct pragma_entry *p = NULL;
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001337 const cpp_token *token, *pragma_token = pfile->cur_token;
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001338 cpp_token ns_token;
Neil Bootha5da89c2001-10-14 17:44:00 +00001339 unsigned int count = 1;
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001340
Neil Booth93c803682000-10-28 17:59:06 +00001341 pfile->state.prevent_expansion++;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001342
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001343 token = cpp_get_token (pfile);
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001344 ns_token = *token;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001345 if (token->type == CPP_NAME)
Alexandre Oliva0172e2b2000-02-27 06:24:27 +00001346 {
Joseph Myers9a0c6182009-05-10 15:27:32 +01001347 p = lookup_pragma_entry (pfile->pragmas, token->val.node.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001348 if (p && p->is_nspace)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001349 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001350 bool allow_name_expansion = p->allow_expansion;
1351 if (allow_name_expansion)
1352 pfile->state.prevent_expansion--;
Neil Bootha5da89c2001-10-14 17:44:00 +00001353 token = cpp_get_token (pfile);
1354 if (token->type == CPP_NAME)
Joseph Myers9a0c6182009-05-10 15:27:32 +01001355 p = lookup_pragma_entry (p->u.space, token->val.node.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001356 else
1357 p = NULL;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001358 if (allow_name_expansion)
1359 pfile->state.prevent_expansion++;
1360 count = 2;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001361 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001362 }
1363
Zack Weinberg3da3d582004-10-27 17:29:29 +00001364 if (p)
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001365 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001366 if (p->is_deferred)
Zack Weinberg3da3d582004-10-27 17:29:29 +00001367 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001368 pfile->directive_result.src_loc = pragma_token->src_loc;
1369 pfile->directive_result.type = CPP_PRAGMA;
1370 pfile->directive_result.flags = pragma_token->flags;
1371 pfile->directive_result.val.pragma = p->u.ident;
1372 pfile->state.in_deferred_pragma = true;
1373 pfile->state.pragma_allow_expansion = p->allow_expansion;
1374 if (!p->allow_expansion)
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001375 pfile->state.prevent_expansion++;
Zack Weinberg3da3d582004-10-27 17:29:29 +00001376 }
1377 else
1378 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001379 /* Since the handler below doesn't get the line number, that
1380 it might need for diagnostics, make sure it has the right
1381 numbers in place. */
1382 if (pfile->cb.line_change)
1383 (*pfile->cb.line_change) (pfile, pragma_token, false);
1384 if (p->allow_expansion)
1385 pfile->state.prevent_expansion--;
1386 (*p->u.handler) (pfile);
1387 if (p->allow_expansion)
1388 pfile->state.prevent_expansion++;
Zack Weinberg3da3d582004-10-27 17:29:29 +00001389 }
Zack Weinberg21b11492004-09-09 19:16:56 +00001390 }
Neil Boothd82fc102001-08-02 23:03:31 +00001391 else if (pfile->cb.def_pragma)
Neil Boothbdcbe492001-09-13 20:05:17 +00001392 {
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001393 if (count == 1 || pfile->context->prev == NULL)
1394 _cpp_backup_tokens (pfile, count);
1395 else
1396 {
1397 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1398 won't allow backing 2 tokens. */
1399 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1400 reads both tokens, we could perhaps free it, but if it doesn't,
1401 we don't know the exact lifespan. */
1402 cpp_token *toks = XNEWVEC (cpp_token, 2);
1403 toks[0] = ns_token;
1404 toks[0].flags |= NO_EXPAND;
1405 toks[1] = *token;
1406 toks[1].flags |= NO_EXPAND;
1407 _cpp_push_token_context (pfile, NULL, toks, 2);
1408 }
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001409 pfile->cb.def_pragma (pfile, pfile->directive_line);
Neil Boothbdcbe492001-09-13 20:05:17 +00001410 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001411
Neil Booth97293892001-09-14 22:04:46 +00001412 pfile->state.prevent_expansion--;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001413}
1414
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001415/* Handle #pragma once. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001416static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001417do_pragma_once (cpp_reader *pfile)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001418{
Tom Tromey705e2d22007-01-04 15:32:26 +00001419 if (cpp_in_primary_file (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +00001420 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
Neil Booth93c803682000-10-28 17:59:06 +00001421
Joseph Myersa5cb5632009-04-18 16:28:40 +01001422 check_eol (pfile, false);
Neil Booth49634b32003-08-02 16:29:46 +00001423 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001424}
1425
Neil Boothc3bf3e62002-05-09 17:14:22 +00001426/* Handle #pragma GCC poison, to poison one or more identifiers so
1427 that the lexer produces a hard error for each subsequent usage. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001428static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001429do_pragma_poison (cpp_reader *pfile)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001430{
Neil Booth345894b2001-09-16 13:44:29 +00001431 const cpp_token *tok;
Zack Weinbergf8f769e2000-05-28 05:56:38 +00001432 cpp_hashnode *hp;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001433
Neil Booth93c803682000-10-28 17:59:06 +00001434 pfile->state.poisoned_ok = 1;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001435 for (;;)
1436 {
Neil Booth345894b2001-09-16 13:44:29 +00001437 tok = _cpp_lex_token (pfile);
1438 if (tok->type == CPP_EOF)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001439 break;
Neil Booth345894b2001-09-16 13:44:29 +00001440 if (tok->type != CPP_NAME)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001441 {
John David Anglin0527bc42003-11-01 22:56:54 +00001442 cpp_error (pfile, CPP_DL_ERROR,
1443 "invalid #pragma GCC poison directive");
Neil Booth93c803682000-10-28 17:59:06 +00001444 break;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001445 }
1446
Joseph Myers9a0c6182009-05-10 15:27:32 +01001447 hp = tok->val.node.node;
Neil Booth93c803682000-10-28 17:59:06 +00001448 if (hp->flags & NODE_POISONED)
1449 continue;
1450
1451 if (hp->type == NT_MACRO)
John David Anglin0527bc42003-11-01 22:56:54 +00001452 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
Neil Boothebef4e82002-04-14 18:42:47 +00001453 NODE_NAME (hp));
Neil Booth93c803682000-10-28 17:59:06 +00001454 _cpp_free_definition (hp);
1455 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001456 }
Neil Booth93c803682000-10-28 17:59:06 +00001457 pfile->state.poisoned_ok = 0;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001458}
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001459
1460/* Mark the current header as a system header. This will suppress
1461 some categories of warnings (notably those from -pedantic). It is
1462 intended for use in system libraries that cannot be implemented in
1463 conforming C, but cannot be certain that their headers appear in a
1464 system include directory. To prevent abuse, it is rejected in the
1465 primary source file. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001466static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001467do_pragma_system_header (cpp_reader *pfile)
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001468{
Tom Tromey705e2d22007-01-04 15:32:26 +00001469 if (cpp_in_primary_file (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +00001470 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +00001471 "#pragma system_header ignored outside include file");
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001472 else
Neil Boothd82fc102001-08-02 23:03:31 +00001473 {
Joseph Myersa5cb5632009-04-18 16:28:40 +01001474 check_eol (pfile, false);
Neil Boothbdcbe492001-09-13 20:05:17 +00001475 skip_rest_of_line (pfile);
Neil Boothd82fc102001-08-02 23:03:31 +00001476 cpp_make_system_header (pfile, 1, 0);
1477 }
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001478}
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001479
1480/* Check the modified date of the current include file against a specified
1481 file. Issue a diagnostic, if the specified file is newer. We use this to
1482 determine if a fixed header should be refixed. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001483static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001484do_pragma_dependency (cpp_reader *pfile)
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001485{
Neil Booth74eb4b32003-04-21 19:21:59 +00001486 const char *fname;
1487 int angle_brackets, ordering;
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +00001488 source_location location;
Kazu Hiratadf383482002-05-22 22:02:16 +00001489
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +00001490 fname = parse_include (pfile, &angle_brackets, NULL, &location);
Neil Booth74eb4b32003-04-21 19:21:59 +00001491 if (!fname)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001492 return;
Zack Weinberg041c3192000-07-04 01:58:21 +00001493
Neil Booth74eb4b32003-04-21 19:21:59 +00001494 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001495 if (ordering < 0)
John David Anglin0527bc42003-11-01 22:56:54 +00001496 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001497 else if (ordering > 0)
1498 {
John David Anglin0527bc42003-11-01 22:56:54 +00001499 cpp_error (pfile, CPP_DL_WARNING,
1500 "current file is older than %s", fname);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001501 if (cpp_get_token (pfile)->type != CPP_EOF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001502 {
1503 _cpp_backup_tokens (pfile, 1);
John David Anglin0527bc42003-11-01 22:56:54 +00001504 do_diagnostic (pfile, CPP_DL_WARNING, 0);
Neil Boothbdcbe492001-09-13 20:05:17 +00001505 }
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001506 }
Neil Booth74eb4b32003-04-21 19:21:59 +00001507
Kaveh R. Ghazifad205f2003-06-16 21:41:10 +00001508 free ((void *) fname);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001509}
1510
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001511/* Get a token but skip padding. */
1512static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001513get_token_no_padding (cpp_reader *pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001514{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001515 for (;;)
1516 {
1517 const cpp_token *result = cpp_get_token (pfile);
1518 if (result->type != CPP_PADDING)
1519 return result;
1520 }
1521}
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001522
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001523/* Check syntax is "(string-literal)". Returns the string on success,
1524 or NULL on failure. */
1525static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001526get__Pragma_string (cpp_reader *pfile)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001527{
1528 const cpp_token *string;
Tom Tromey5b9a40d2007-10-31 14:50:13 +00001529 const cpp_token *paren;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001530
Tom Tromey5b9a40d2007-10-31 14:50:13 +00001531 paren = get_token_no_padding (pfile);
1532 if (paren->type == CPP_EOF)
1533 _cpp_backup_tokens (pfile, 1);
1534 if (paren->type != CPP_OPEN_PAREN)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001535 return NULL;
1536
1537 string = get_token_no_padding (pfile);
Tom Tromey5b9a40d2007-10-31 14:50:13 +00001538 if (string->type == CPP_EOF)
1539 _cpp_backup_tokens (pfile, 1);
Kris Van Heesb6baa672008-04-18 13:58:08 +00001540 if (string->type != CPP_STRING && string->type != CPP_WSTRING
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001541 && string->type != CPP_STRING32 && string->type != CPP_STRING16
1542 && string->type != CPP_UTF8STRING)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001543 return NULL;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001544
Tom Tromey5b9a40d2007-10-31 14:50:13 +00001545 paren = get_token_no_padding (pfile);
1546 if (paren->type == CPP_EOF)
1547 _cpp_backup_tokens (pfile, 1);
1548 if (paren->type != CPP_CLOSE_PAREN)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001549 return NULL;
1550
1551 return string;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001552}
1553
Neil Booth87062812001-10-20 09:00:53 +00001554/* Destringize IN into a temporary buffer, by removing the first \ of
1555 \" and \\ sequences, and process the result as a #pragma directive. */
1556static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001557destringize_and_run (cpp_reader *pfile, const cpp_string *in)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001558{
1559 const unsigned char *src, *limit;
Neil Booth87062812001-10-20 09:00:53 +00001560 char *dest, *result;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001561 cpp_context *saved_context;
1562 cpp_token *saved_cur_token;
1563 tokenrun *saved_cur_run;
1564 cpp_token *toks;
1565 int count;
Tom Tromey14ccf802008-03-13 21:10:07 +00001566 const struct directive *save_directive;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001567
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001568 dest = result = (char *) alloca (in->len - 1);
Neil Booth6338b352003-04-23 22:44:06 +00001569 src = in->text + 1 + (in->text[0] == 'L');
1570 limit = in->text + in->len - 1;
1571 while (src < limit)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001572 {
1573 /* We know there is a character following the backslash. */
1574 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1575 src++;
1576 *dest++ = *src++;
1577 }
Neil Booth26aea072003-04-19 00:22:51 +00001578 *dest = '\n';
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001579
Neil Booth8128ccc2002-11-18 20:43:40 +00001580 /* Ugh; an awful kludge. We are really not set up to be lexing
1581 tokens when in the middle of a macro expansion. Use a new
1582 context to force cpp_get_token to lex, and so skip_rest_of_line
1583 doesn't go beyond the end of the text. Also, remember the
1584 current lexing position so we can return to it later.
Neil Booth79ba5e32002-09-03 21:55:40 +00001585
Neil Booth8128ccc2002-11-18 20:43:40 +00001586 Something like line-at-a-time lexing should remove the need for
1587 this. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001588 saved_context = pfile->context;
1589 saved_cur_token = pfile->cur_token;
1590 saved_cur_run = pfile->cur_run;
Neil Booth8128ccc2002-11-18 20:43:40 +00001591
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001592 pfile->context = XNEW (cpp_context);
1593 pfile->context->macro = 0;
1594 pfile->context->prev = 0;
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001595 pfile->context->next = 0;
Neil Booth79ba5e32002-09-03 21:55:40 +00001596
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001597 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1598 until we've read all of the tokens that we want. */
1599 cpp_push_buffer (pfile, (const uchar *) result, dest - result,
1600 /* from_stage3 */ true);
1601 /* ??? Antique Disgusting Hack. What does this do? */
1602 if (pfile->buffer->prev)
1603 pfile->buffer->file = pfile->buffer->prev->file;
Neil Booth79ba5e32002-09-03 21:55:40 +00001604
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001605 start_directive (pfile);
1606 _cpp_clean_line (pfile);
Tom Tromey14ccf802008-03-13 21:10:07 +00001607 save_directive = pfile->directive;
1608 pfile->directive = &dtable[T_PRAGMA];
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001609 do_pragma (pfile);
1610 end_directive (pfile, 1);
Tom Tromey14ccf802008-03-13 21:10:07 +00001611 pfile->directive = save_directive;
Neil Booth79ba5e32002-09-03 21:55:40 +00001612
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001613 /* We always insert at least one token, the directive result. It'll
1614 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1615 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
Neil Booth79ba5e32002-09-03 21:55:40 +00001616
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001617 /* If we're not handling the pragma internally, read all of the tokens from
1618 the string buffer now, while the string buffer is still installed. */
1619 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1620 to me what the true lifespan of the tokens are. It would appear that
1621 the lifespan is the entire parse of the main input stream, in which case
1622 this may not be wrong. */
1623 if (pfile->directive_result.type == CPP_PRAGMA)
1624 {
1625 int maxcount;
Neil Booth79ba5e32002-09-03 21:55:40 +00001626
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001627 count = 1;
1628 maxcount = 50;
1629 toks = XNEWVEC (cpp_token, maxcount);
1630 toks[0] = pfile->directive_result;
1631
1632 do
1633 {
1634 if (count == maxcount)
1635 {
1636 maxcount = maxcount * 3 / 2;
1637 toks = XRESIZEVEC (cpp_token, toks, maxcount);
1638 }
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001639 toks[count] = *cpp_get_token (pfile);
1640 /* Macros have been already expanded by cpp_get_token
1641 if the pragma allowed expansion. */
1642 toks[count++].flags |= NO_EXPAND;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001643 }
1644 while (toks[count-1].type != CPP_PRAGMA_EOL);
1645 }
1646 else
1647 {
1648 count = 1;
1649 toks = XNEW (cpp_token);
1650 toks[0] = pfile->directive_result;
1651
1652 /* If we handled the entire pragma internally, make sure we get the
1653 line number correct for the next token. */
1654 if (pfile->cb.line_change)
1655 pfile->cb.line_change (pfile, pfile->cur_token, false);
1656 }
1657
1658 /* Finish inlining run_directive. */
1659 pfile->buffer->file = NULL;
1660 _cpp_pop_buffer (pfile);
1661
1662 /* Reset the old macro state before ... */
1663 XDELETE (pfile->context);
1664 pfile->context = saved_context;
1665 pfile->cur_token = saved_cur_token;
1666 pfile->cur_run = saved_cur_run;
1667
1668 /* ... inserting the new tokens we collected. */
1669 _cpp_push_token_context (pfile, NULL, toks, count);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001670}
1671
Tom Tromey5b9a40d2007-10-31 14:50:13 +00001672/* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
1673int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001674_cpp_do__Pragma (cpp_reader *pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001675{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001676 const cpp_token *string = get__Pragma_string (pfile);
Zack Weinberg21b11492004-09-09 19:16:56 +00001677 pfile->directive_result.type = CPP_PADDING;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001678
Neil Booth79ba5e32002-09-03 21:55:40 +00001679 if (string)
Tom Tromey5b9a40d2007-10-31 14:50:13 +00001680 {
1681 destringize_and_run (pfile, &string->val.str);
1682 return 1;
1683 }
1684 cpp_error (pfile, CPP_DL_ERROR,
1685 "_Pragma takes a parenthesized string literal");
1686 return 0;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001687}
1688
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001689/* Handle #ifdef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001690static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001691do_ifdef (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +00001692{
Neil Booth93c803682000-10-28 17:59:06 +00001693 int skip = 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001694
Neil Boothcef0d192001-07-26 06:02:47 +00001695 if (! pfile->state.skipping)
Neil Booth93c803682000-10-28 17:59:06 +00001696 {
Joseph Myers93d45d92008-04-02 20:42:53 +01001697 cpp_hashnode *node = lex_macro_node (pfile, false);
Zack Weinberg041c3192000-07-04 01:58:21 +00001698
Neil Booth93c803682000-10-28 17:59:06 +00001699 if (node)
Neil Bootha69cbaa2002-07-23 22:57:49 +00001700 {
1701 skip = node->type != NT_MACRO;
1702 _cpp_mark_macro_used (node);
Joseph Myers93d45d92008-04-02 20:42:53 +01001703 if (!(node->flags & NODE_USED))
1704 {
1705 node->flags |= NODE_USED;
1706 if (node->type == NT_MACRO)
1707 {
1708 if (pfile->cb.used_define)
1709 pfile->cb.used_define (pfile, pfile->directive_line, node);
1710 }
1711 else
1712 {
1713 if (pfile->cb.used_undef)
1714 pfile->cb.used_undef (pfile, pfile->directive_line, node);
1715 }
1716 }
Joseph Myersa5cb5632009-04-18 16:28:40 +01001717 check_eol (pfile, false);
Neil Bootha69cbaa2002-07-23 22:57:49 +00001718 }
Neil Booth93c803682000-10-28 17:59:06 +00001719 }
1720
1721 push_conditional (pfile, skip, T_IFDEF, 0);
Zack Weinberg168d3732000-03-14 06:34:11 +00001722}
1723
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001724/* Handle #ifndef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001725static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001726do_ifndef (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +00001727{
Neil Booth93c803682000-10-28 17:59:06 +00001728 int skip = 1;
Joseph Myers93d45d92008-04-02 20:42:53 +01001729 cpp_hashnode *node = 0;
Zack Weinberg168d3732000-03-14 06:34:11 +00001730
Neil Boothcef0d192001-07-26 06:02:47 +00001731 if (! pfile->state.skipping)
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001732 {
Tom Tromeyee1c2a12007-01-12 19:46:49 +00001733 node = lex_macro_node (pfile, false);
Geoffrey Keatingb43db0b2000-12-02 22:28:44 +00001734
1735 if (node)
Neil Bootha69cbaa2002-07-23 22:57:49 +00001736 {
1737 skip = node->type == NT_MACRO;
1738 _cpp_mark_macro_used (node);
Joseph Myers93d45d92008-04-02 20:42:53 +01001739 if (!(node->flags & NODE_USED))
1740 {
1741 node->flags |= NODE_USED;
1742 if (node->type == NT_MACRO)
1743 {
1744 if (pfile->cb.used_define)
1745 pfile->cb.used_define (pfile, pfile->directive_line, node);
1746 }
1747 else
1748 {
1749 if (pfile->cb.used_undef)
1750 pfile->cb.used_undef (pfile, pfile->directive_line, node);
1751 }
1752 }
Joseph Myersa5cb5632009-04-18 16:28:40 +01001753 check_eol (pfile, false);
Neil Bootha69cbaa2002-07-23 22:57:49 +00001754 }
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001755 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001756
Neil Booth93c803682000-10-28 17:59:06 +00001757 push_conditional (pfile, skip, T_IFNDEF, node);
Per Bothner7f2935c1995-03-16 13:59:07 -08001758}
1759
Neil Booth6d18adb2001-07-29 17:27:57 +00001760/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1761 pfile->mi_ind_cmacro so we can handle multiple-include
Kazu Hirata6356f892003-06-12 19:01:08 +00001762 optimizations. If macro expansion occurs in the expression, we
Neil Booth6d18adb2001-07-29 17:27:57 +00001763 cannot treat it as a controlling conditional, since the expansion
1764 could change in the future. That is handled by cpp_get_token. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001765static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001766do_if (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001767{
Neil Booth93c803682000-10-28 17:59:06 +00001768 int skip = 1;
Per Bothner7f2935c1995-03-16 13:59:07 -08001769
Neil Boothcef0d192001-07-26 06:02:47 +00001770 if (! pfile->state.skipping)
Tom Tromeyd7508872008-05-30 14:25:09 +00001771 skip = _cpp_parse_expr (pfile, true) == false;
Neil Booth93c803682000-10-28 17:59:06 +00001772
Neil Booth6d18adb2001-07-29 17:27:57 +00001773 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
Per Bothner7f2935c1995-03-16 13:59:07 -08001774}
1775
Neil Boothb528a072000-11-12 11:46:21 +00001776/* Flip skipping state if appropriate and continue without changing
Zack Weinbergea4a4532000-05-29 16:19:32 +00001777 if_stack; this is so that the error message for missing #endif's
1778 etc. will point to the original #if. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001779static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001780do_else (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001781{
Neil Boothb528a072000-11-12 11:46:21 +00001782 cpp_buffer *buffer = pfile->buffer;
1783 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001784
1785 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001786 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
Neil Booth93c803682000-10-28 17:59:06 +00001787 else
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001788 {
Neil Booth93c803682000-10-28 17:59:06 +00001789 if (ifs->type == T_ELSE)
1790 {
John David Anglin0527bc42003-11-01 22:56:54 +00001791 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1792 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Booth93c803682000-10-28 17:59:06 +00001793 "the conditional began here");
1794 }
Neil Boothb528a072000-11-12 11:46:21 +00001795 ifs->type = T_ELSE;
1796
Neil Boothcef0d192001-07-26 06:02:47 +00001797 /* Skip any future (erroneous) #elses or #elifs. */
1798 pfile->state.skipping = ifs->skip_elses;
1799 ifs->skip_elses = true;
Neil Booth93c803682000-10-28 17:59:06 +00001800
1801 /* Invalidate any controlling macro. */
1802 ifs->mi_cmacro = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001803
Neil Boothcef0d192001-07-26 06:02:47 +00001804 /* Only check EOL if was not originally skipping. */
Phil Edwards909de5d2002-03-22 21:59:04 +00001805 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
Joseph Myersa5cb5632009-04-18 16:28:40 +01001806 check_eol (pfile, false);
Neil Boothcef0d192001-07-26 06:02:47 +00001807 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001808}
1809
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001810/* Handle a #elif directive by not changing if_stack either. See the
Neil Booth93c803682000-10-28 17:59:06 +00001811 comment above do_else. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001812static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001813do_elif (cpp_reader *pfile)
Zack Weinbergea4a4532000-05-29 16:19:32 +00001814{
Neil Boothb528a072000-11-12 11:46:21 +00001815 cpp_buffer *buffer = pfile->buffer;
1816 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001817
1818 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001819 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
Neil Boothb528a072000-11-12 11:46:21 +00001820 else
Zack Weinbergea4a4532000-05-29 16:19:32 +00001821 {
Neil Boothb528a072000-11-12 11:46:21 +00001822 if (ifs->type == T_ELSE)
1823 {
John David Anglin0527bc42003-11-01 22:56:54 +00001824 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1825 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Boothb528a072000-11-12 11:46:21 +00001826 "the conditional began here");
1827 }
1828 ifs->type = T_ELIF;
1829
Tom Tromeyd7508872008-05-30 14:25:09 +00001830 if (! ifs->was_skipping)
Neil Boothb528a072000-11-12 11:46:21 +00001831 {
Tom Tromeyd7508872008-05-30 14:25:09 +00001832 bool value;
1833 /* The standard mandates that the expression be parsed even
1834 if we are skipping elses at this point -- the lexical
1835 restrictions on #elif only apply to skipped groups, but
1836 this group is not being skipped. Temporarily set
1837 skipping to false to get lexer warnings. */
Neil Boothcef0d192001-07-26 06:02:47 +00001838 pfile->state.skipping = 0;
Tom Tromeyd7508872008-05-30 14:25:09 +00001839 value = _cpp_parse_expr (pfile, false);
1840 if (ifs->skip_elses)
1841 pfile->state.skipping = 1;
1842 else
1843 {
1844 pfile->state.skipping = ! value;
1845 ifs->skip_elses = value;
1846 }
Neil Boothb528a072000-11-12 11:46:21 +00001847 }
Neil Boothcef0d192001-07-26 06:02:47 +00001848
1849 /* Invalidate any controlling macro. */
1850 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001851 }
Zack Weinbergea4a4532000-05-29 16:19:32 +00001852}
1853
Neil Boothcef0d192001-07-26 06:02:47 +00001854/* #endif pops the if stack and resets pfile->state.skipping. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001855static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001856do_endif (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001857{
Neil Boothb528a072000-11-12 11:46:21 +00001858 cpp_buffer *buffer = pfile->buffer;
1859 struct if_stack *ifs = buffer->if_stack;
Per Bothner7f2935c1995-03-16 13:59:07 -08001860
Zack Weinbergea4a4532000-05-29 16:19:32 +00001861 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001862 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
Per Bothner7f2935c1995-03-16 13:59:07 -08001863 else
1864 {
Neil Boothcef0d192001-07-26 06:02:47 +00001865 /* Only check EOL if was not originally skipping. */
Phil Edwards909de5d2002-03-22 21:59:04 +00001866 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
Joseph Myersa5cb5632009-04-18 16:28:40 +01001867 check_eol (pfile, false);
Neil Boothcef0d192001-07-26 06:02:47 +00001868
Neil Booth93c803682000-10-28 17:59:06 +00001869 /* If potential control macro, we go back outside again. */
1870 if (ifs->next == 0 && ifs->mi_cmacro)
1871 {
Neil Booth6d18adb2001-07-29 17:27:57 +00001872 pfile->mi_valid = true;
Neil Booth93c803682000-10-28 17:59:06 +00001873 pfile->mi_cmacro = ifs->mi_cmacro;
1874 }
1875
Neil Boothb528a072000-11-12 11:46:21 +00001876 buffer->if_stack = ifs->next;
Neil Boothcef0d192001-07-26 06:02:47 +00001877 pfile->state.skipping = ifs->was_skipping;
Neil Booth2a967f32001-05-20 06:26:45 +00001878 obstack_free (&pfile->buffer_ob, ifs);
Per Bothner7f2935c1995-03-16 13:59:07 -08001879 }
Neil Booth93c803682000-10-28 17:59:06 +00001880}
Zack Weinberg041c3192000-07-04 01:58:21 +00001881
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001882/* Push an if_stack entry for a preprocessor conditional, and set
1883 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1884 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1885 we need to check here that we are at the top of the file. */
Zack Weinbergea4a4532000-05-29 16:19:32 +00001886static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001887push_conditional (cpp_reader *pfile, int skip, int type,
1888 const cpp_hashnode *cmacro)
Zack Weinbergea4a4532000-05-29 16:19:32 +00001889{
1890 struct if_stack *ifs;
Neil Boothb528a072000-11-12 11:46:21 +00001891 cpp_buffer *buffer = pfile->buffer;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001892
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001893 ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
Neil Booth50410422001-09-15 10:18:03 +00001894 ifs->line = pfile->directive_line;
Neil Boothb528a072000-11-12 11:46:21 +00001895 ifs->next = buffer->if_stack;
Neil Boothcef0d192001-07-26 06:02:47 +00001896 ifs->skip_elses = pfile->state.skipping || !skip;
1897 ifs->was_skipping = pfile->state.skipping;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001898 ifs->type = type;
Neil Booth6d18adb2001-07-29 17:27:57 +00001899 /* This condition is effectively a test for top-of-file. */
1900 if (pfile->mi_valid && pfile->mi_cmacro == 0)
Neil Booth93c803682000-10-28 17:59:06 +00001901 ifs->mi_cmacro = cmacro;
1902 else
1903 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001904
Neil Boothcef0d192001-07-26 06:02:47 +00001905 pfile->state.skipping = skip;
Neil Boothb528a072000-11-12 11:46:21 +00001906 buffer->if_stack = ifs;
Zack Weinberg7061aa51998-12-15 11:09:16 +00001907}
1908
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001909/* Read the tokens of the answer into the macro pool, in a directive
1910 of type TYPE. Only commit the memory if we intend it as permanent
1911 storage, i.e. the #assert case. Returns 0 on success, and sets
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +00001912 ANSWERP to point to the answer. PRED_LOC is the location of the
1913 predicate. */
Neil Booth93c803682000-10-28 17:59:06 +00001914static int
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +00001915parse_answer (cpp_reader *pfile, struct answer **answerp, int type,
1916 source_location pred_loc)
Zack Weinberg041c3192000-07-04 01:58:21 +00001917{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001918 const cpp_token *paren;
Neil Booth93c803682000-10-28 17:59:06 +00001919 struct answer *answer;
Neil Booth8c3b2692001-09-30 10:03:11 +00001920 unsigned int acount;
Zack Weinberg041c3192000-07-04 01:58:21 +00001921
Neil Booth93c803682000-10-28 17:59:06 +00001922 /* In a conditional, it is legal to not have an open paren. We
1923 should save the following token in this case. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001924 paren = cpp_get_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001925
1926 /* If not a paren, see if we're OK. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001927 if (paren->type != CPP_OPEN_PAREN)
Zack Weinberg041c3192000-07-04 01:58:21 +00001928 {
Neil Booth93c803682000-10-28 17:59:06 +00001929 /* In a conditional no answer is a test for any answer. It
1930 could be followed by any token. */
1931 if (type == T_IF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001932 {
1933 _cpp_backup_tokens (pfile, 1);
1934 return 0;
1935 }
Neil Booth93c803682000-10-28 17:59:06 +00001936
1937 /* #unassert with no answer is valid - it removes all answers. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001938 if (type == T_UNASSERT && paren->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +00001939 return 0;
1940
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +00001941 cpp_error_with_line (pfile, CPP_DL_ERROR, pred_loc, 0,
1942 "missing '(' after predicate");
Neil Booth93c803682000-10-28 17:59:06 +00001943 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001944 }
1945
Neil Booth8c3b2692001-09-30 10:03:11 +00001946 for (acount = 0;; acount++)
Zack Weinberg041c3192000-07-04 01:58:21 +00001947 {
Neil Booth8c3b2692001-09-30 10:03:11 +00001948 size_t room_needed;
1949 const cpp_token *token = cpp_get_token (pfile);
1950 cpp_token *dest;
Zack Weinberg041c3192000-07-04 01:58:21 +00001951
Neil Booth93c803682000-10-28 17:59:06 +00001952 if (token->type == CPP_CLOSE_PAREN)
1953 break;
Zack Weinberg041c3192000-07-04 01:58:21 +00001954
1955 if (token->type == CPP_EOF)
1956 {
John David Anglin0527bc42003-11-01 22:56:54 +00001957 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
Neil Booth93c803682000-10-28 17:59:06 +00001958 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001959 }
Neil Booth8c3b2692001-09-30 10:03:11 +00001960
1961 /* struct answer includes the space for one token. */
1962 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1963
1964 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1965 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1966
1967 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1968 *dest = *token;
1969
1970 /* Drop whitespace at start, for answer equivalence purposes. */
1971 if (acount == 0)
1972 dest->flags &= ~PREV_WHITE;
Zack Weinberg041c3192000-07-04 01:58:21 +00001973 }
1974
Neil Booth8c3b2692001-09-30 10:03:11 +00001975 if (acount == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001976 {
John David Anglin0527bc42003-11-01 22:56:54 +00001977 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
Neil Booth93c803682000-10-28 17:59:06 +00001978 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001979 }
1980
Neil Booth8c3b2692001-09-30 10:03:11 +00001981 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1982 answer->count = acount;
1983 answer->next = NULL;
Neil Booth93c803682000-10-28 17:59:06 +00001984 *answerp = answer;
Zack Weinberg041c3192000-07-04 01:58:21 +00001985
Neil Booth93c803682000-10-28 17:59:06 +00001986 return 0;
1987}
1988
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001989/* Parses an assertion directive of type TYPE, returning a pointer to
1990 the hash node of the predicate, or 0 on error. If an answer was
1991 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
Neil Booth93c803682000-10-28 17:59:06 +00001992static cpp_hashnode *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001993parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
Neil Booth93c803682000-10-28 17:59:06 +00001994{
1995 cpp_hashnode *result = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001996 const cpp_token *predicate;
Neil Booth93c803682000-10-28 17:59:06 +00001997
1998 /* We don't expand predicates or answers. */
1999 pfile->state.prevent_expansion++;
2000
Neil Booth93c803682000-10-28 17:59:06 +00002001 *answerp = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002002 predicate = cpp_get_token (pfile);
2003 if (predicate->type == CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +00002004 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002005 else if (predicate->type != CPP_NAME)
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +00002006 cpp_error_with_line (pfile, CPP_DL_ERROR, predicate->src_loc, 0,
2007 "predicate must be an identifier");
2008 else if (parse_answer (pfile, answerp, type, predicate->src_loc) == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00002009 {
Joseph Myers9a0c6182009-05-10 15:27:32 +01002010 unsigned int len = NODE_LEN (predicate->val.node.node);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002011 unsigned char *sym = (unsigned char *) alloca (len + 1);
Neil Booth93c803682000-10-28 17:59:06 +00002012
2013 /* Prefix '#' to get it out of macro namespace. */
2014 sym[0] = '#';
Joseph Myers9a0c6182009-05-10 15:27:32 +01002015 memcpy (sym + 1, NODE_NAME (predicate->val.node.node), len);
Neil Booth93c803682000-10-28 17:59:06 +00002016 result = cpp_lookup (pfile, sym, len + 1);
Zack Weinberg041c3192000-07-04 01:58:21 +00002017 }
2018
Neil Booth93c803682000-10-28 17:59:06 +00002019 pfile->state.prevent_expansion--;
2020 return result;
Zack Weinberg041c3192000-07-04 01:58:21 +00002021}
2022
Neil Booth5d8ebbd2002-01-03 21:43:09 +00002023/* Returns a pointer to the pointer to CANDIDATE in the answer chain,
Zack Weinberg041c3192000-07-04 01:58:21 +00002024 or a pointer to NULL if the answer is not in the chain. */
Neil Booth93c803682000-10-28 17:59:06 +00002025static struct answer **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002026find_answer (cpp_hashnode *node, const struct answer *candidate)
Zack Weinberg041c3192000-07-04 01:58:21 +00002027{
Neil Booth93c803682000-10-28 17:59:06 +00002028 unsigned int i;
Zack Weinberg041c3192000-07-04 01:58:21 +00002029 struct answer **result;
2030
2031 for (result = &node->value.answers; *result; result = &(*result)->next)
Neil Booth93c803682000-10-28 17:59:06 +00002032 {
2033 struct answer *answer = *result;
2034
2035 if (answer->count == candidate->count)
2036 {
2037 for (i = 0; i < answer->count; i++)
2038 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
2039 break;
2040
2041 if (i == answer->count)
2042 break;
2043 }
2044 }
Zack Weinberg041c3192000-07-04 01:58:21 +00002045
2046 return result;
2047}
2048
Neil Booth93c803682000-10-28 17:59:06 +00002049/* Test an assertion within a preprocessor conditional. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +00002050 nonzero on failure, zero on success. On success, the result of
Hans-Peter Nilsson24026452002-11-29 22:41:04 +00002051 the test is written into VALUE, otherwise the value 0. */
Neil Booth93c803682000-10-28 17:59:06 +00002052int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002053_cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
Neil Booth93c803682000-10-28 17:59:06 +00002054{
2055 struct answer *answer;
2056 cpp_hashnode *node;
2057
2058 node = parse_assertion (pfile, &answer, T_IF);
Hans-Peter Nilsson24026452002-11-29 22:41:04 +00002059
2060 /* For recovery, an erroneous assertion expression is handled as a
2061 failing assertion. */
2062 *value = 0;
2063
Neil Booth93c803682000-10-28 17:59:06 +00002064 if (node)
2065 *value = (node->type == NT_ASSERTION &&
2066 (answer == 0 || *find_answer (node, answer) != 0));
Neil Booth91318902002-05-26 18:42:21 +00002067 else if (pfile->cur_token[-1].type == CPP_EOF)
2068 _cpp_backup_tokens (pfile, 1);
Neil Booth93c803682000-10-28 17:59:06 +00002069
2070 /* We don't commit the memory for the answer - it's temporary only. */
2071 return node == 0;
2072}
2073
Neil Booth5d8ebbd2002-01-03 21:43:09 +00002074/* Handle #assert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00002075static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002076do_assert (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08002077{
Zack Weinberg041c3192000-07-04 01:58:21 +00002078 struct answer *new_answer;
2079 cpp_hashnode *node;
Kazu Hiratadf383482002-05-22 22:02:16 +00002080
Neil Booth93c803682000-10-28 17:59:06 +00002081 node = parse_assertion (pfile, &new_answer, T_ASSERT);
Zack Weinberg041c3192000-07-04 01:58:21 +00002082 if (node)
2083 {
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002084 size_t answer_size;
2085
Neil Booth93c803682000-10-28 17:59:06 +00002086 /* Place the new answer in the answer list. First check there
2087 is not a duplicate. */
Zack Weinberg041c3192000-07-04 01:58:21 +00002088 new_answer->next = 0;
Neil Booth93c803682000-10-28 17:59:06 +00002089 if (node->type == NT_ASSERTION)
Zack Weinberg041c3192000-07-04 01:58:21 +00002090 {
Neil Booth93c803682000-10-28 17:59:06 +00002091 if (*find_answer (node, new_answer))
2092 {
John David Anglin0527bc42003-11-01 22:56:54 +00002093 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
Neil Boothebef4e82002-04-14 18:42:47 +00002094 NODE_NAME (node) + 1);
Neil Booth93c803682000-10-28 17:59:06 +00002095 return;
2096 }
Zack Weinberg041c3192000-07-04 01:58:21 +00002097 new_answer->next = node->value.answers;
2098 }
Neil Booth8c3b2692001-09-30 10:03:11 +00002099
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002100 answer_size = sizeof (struct answer) + ((new_answer->count - 1)
2101 * sizeof (cpp_token));
2102 /* Commit or allocate storage for the object. */
2103 if (pfile->hash_table->alloc_subobject)
2104 {
2105 struct answer *temp_answer = new_answer;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002106 new_answer = (struct answer *) pfile->hash_table->alloc_subobject
2107 (answer_size);
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002108 memcpy (new_answer, temp_answer, answer_size);
2109 }
2110 else
2111 BUFF_FRONT (pfile->a_buff) += answer_size;
2112
Neil Booth93c803682000-10-28 17:59:06 +00002113 node->type = NT_ASSERTION;
Zack Weinberg041c3192000-07-04 01:58:21 +00002114 node->value.answers = new_answer;
Joseph Myersa5cb5632009-04-18 16:28:40 +01002115 check_eol (pfile, false);
Zack Weinberg041c3192000-07-04 01:58:21 +00002116 }
Per Bothner7f2935c1995-03-16 13:59:07 -08002117}
Zack Weinberg7061aa51998-12-15 11:09:16 +00002118
Neil Booth5d8ebbd2002-01-03 21:43:09 +00002119/* Handle #unassert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00002120static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002121do_unassert (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08002122{
Zack Weinberg041c3192000-07-04 01:58:21 +00002123 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00002124 struct answer *answer;
Kazu Hiratadf383482002-05-22 22:02:16 +00002125
Neil Booth93c803682000-10-28 17:59:06 +00002126 node = parse_assertion (pfile, &answer, T_UNASSERT);
2127 /* It isn't an error to #unassert something that isn't asserted. */
2128 if (node && node->type == NT_ASSERTION)
Zack Weinberg7061aa51998-12-15 11:09:16 +00002129 {
Zack Weinberg041c3192000-07-04 01:58:21 +00002130 if (answer)
Neil Booth93c803682000-10-28 17:59:06 +00002131 {
2132 struct answer **p = find_answer (node, answer), *temp;
2133
2134 /* Remove the answer from the list. */
2135 temp = *p;
2136 if (temp)
2137 *p = temp->next;
2138
2139 /* Did we free the last answer? */
2140 if (node->value.answers == 0)
2141 node->type = NT_VOID;
Neil Booth8c3b2692001-09-30 10:03:11 +00002142
Joseph Myersa5cb5632009-04-18 16:28:40 +01002143 check_eol (pfile, false);
Neil Booth93c803682000-10-28 17:59:06 +00002144 }
2145 else
2146 _cpp_free_definition (node);
Zack Weinberg7061aa51998-12-15 11:09:16 +00002147 }
Neil Booth93c803682000-10-28 17:59:06 +00002148
2149 /* We don't commit the memory for the answer - it's temporary only. */
Per Bothner7f2935c1995-03-16 13:59:07 -08002150}
Per Bothner7f2935c1995-03-16 13:59:07 -08002151
Zack Weinberg45b966d2000-03-13 22:01:08 +00002152/* These are for -D, -U, -A. */
2153
2154/* Process the string STR as if it appeared as the body of a #define.
2155 If STR is just an identifier, define it with value 1.
2156 If STR has anything after the identifier, then it should
Kazu Hirataec5c56d2001-08-01 17:57:27 +00002157 be identifier=definition. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00002158void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002159cpp_define (cpp_reader *pfile, const char *str)
Zack Weinberg45b966d2000-03-13 22:01:08 +00002160{
Jason Merrill86373e72009-09-12 15:46:45 -04002161 char *buf;
2162 const char *p;
Zack Weinberg45b966d2000-03-13 22:01:08 +00002163 size_t count;
2164
Kazu Hiratadf383482002-05-22 22:02:16 +00002165 /* Copy the entire option so we can modify it.
Zack Weinberg45b966d2000-03-13 22:01:08 +00002166 Change the first "=" in the string to a space. If there is none,
Neil Booth86368122000-10-31 23:34:59 +00002167 tack " 1" on the end. */
2168
Neil Booth86368122000-10-31 23:34:59 +00002169 count = strlen (str);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002170 buf = (char *) alloca (count + 3);
Neil Booth86368122000-10-31 23:34:59 +00002171 memcpy (buf, str, count);
2172
2173 p = strchr (str, '=');
Zack Weinberg45b966d2000-03-13 22:01:08 +00002174 if (p)
Neil Booth86368122000-10-31 23:34:59 +00002175 buf[p - str] = ' ';
Zack Weinberg45b966d2000-03-13 22:01:08 +00002176 else
2177 {
Neil Booth86368122000-10-31 23:34:59 +00002178 buf[count++] = ' ';
2179 buf[count++] = '1';
Zack Weinberg45b966d2000-03-13 22:01:08 +00002180 }
Neil Booth26aea072003-04-19 00:22:51 +00002181 buf[count] = '\n';
Zack Weinberg45b966d2000-03-13 22:01:08 +00002182
Neil Booth29401c32001-08-22 20:37:20 +00002183 run_directive (pfile, T_DEFINE, buf, count);
Zack Weinberg2c8f0512000-08-29 18:37:37 +00002184}
2185
Daniel Franke28f68622008-04-22 14:04:32 -04002186
2187/* Use to build macros to be run through cpp_define() as
2188 described above.
2189 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
2190
2191void
2192cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...)
2193{
2194 char *ptr = NULL;
2195
2196 va_list ap;
2197 va_start (ap, fmt);
2198 vasprintf (&ptr, fmt, ap);
2199 va_end (ap);
2200
2201 cpp_define (pfile, ptr);
2202 free (ptr);
2203}
2204
2205
Neil Boothad2a0842000-12-17 00:13:54 +00002206/* Slight variant of the above for use by initialize_builtins. */
Zack Weinberg2c8f0512000-08-29 18:37:37 +00002207void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002208_cpp_define_builtin (cpp_reader *pfile, const char *str)
Zack Weinberg2c8f0512000-08-29 18:37:37 +00002209{
Neil Booth26aea072003-04-19 00:22:51 +00002210 size_t len = strlen (str);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002211 char *buf = (char *) alloca (len + 1);
Neil Booth26aea072003-04-19 00:22:51 +00002212 memcpy (buf, str, len);
2213 buf[len] = '\n';
2214 run_directive (pfile, T_DEFINE, buf, len);
Zack Weinberg45b966d2000-03-13 22:01:08 +00002215}
2216
2217/* Process MACRO as if it appeared as the body of an #undef. */
2218void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002219cpp_undef (cpp_reader *pfile, const char *macro)
Zack Weinberg45b966d2000-03-13 22:01:08 +00002220{
Neil Booth26aea072003-04-19 00:22:51 +00002221 size_t len = strlen (macro);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002222 char *buf = (char *) alloca (len + 1);
Neil Booth26aea072003-04-19 00:22:51 +00002223 memcpy (buf, macro, len);
2224 buf[len] = '\n';
2225 run_directive (pfile, T_UNDEF, buf, len);
Zack Weinberg45b966d2000-03-13 22:01:08 +00002226}
2227
Richard Henderson121de392007-03-30 14:12:53 -07002228/* Like lex_macro_node, but read the input from STR. */
2229static cpp_hashnode *
2230lex_macro_node_from_str (cpp_reader *pfile, const char *str)
2231{
2232 size_t len = strlen (str);
Michael Meissner4cd97072007-03-30 22:40:19 +00002233 uchar *buf = (uchar *) alloca (len + 1);
Richard Henderson121de392007-03-30 14:12:53 -07002234 cpp_hashnode *node;
2235
2236 memcpy (buf, str, len);
2237 buf[len] = '\n';
2238 cpp_push_buffer (pfile, buf, len, true);
2239 node = lex_macro_node (pfile, true);
2240 _cpp_pop_buffer (pfile);
2241
2242 return node;
2243}
2244
2245/* If STR is a defined macro, return its definition node, else return NULL. */
2246cpp_macro *
2247cpp_push_definition (cpp_reader *pfile, const char *str)
2248{
2249 cpp_hashnode *node = lex_macro_node_from_str (pfile, str);
2250 if (node && node->type == NT_MACRO)
2251 return node->value.macro;
2252 else
2253 return NULL;
2254}
2255
2256/* Replace a previous definition DFN of the macro STR. If DFN is NULL,
2257 then the macro should be undefined. */
2258void
2259cpp_pop_definition (cpp_reader *pfile, const char *str, cpp_macro *dfn)
2260{
2261 cpp_hashnode *node = lex_macro_node_from_str (pfile, str);
2262 if (node == NULL)
2263 return;
2264
Joseph Myers93d45d92008-04-02 20:42:53 +01002265 if (pfile->cb.before_define)
2266 pfile->cb.before_define (pfile);
2267
Richard Henderson121de392007-03-30 14:12:53 -07002268 if (node->type == NT_MACRO)
2269 {
2270 if (pfile->cb.undef)
2271 pfile->cb.undef (pfile, pfile->directive_line, node);
2272 if (CPP_OPTION (pfile, warn_unused_macros))
2273 _cpp_warn_if_unused_macro (pfile, node, NULL);
2274 }
2275 if (node->type != NT_VOID)
2276 _cpp_free_definition (node);
2277
2278 if (dfn)
2279 {
2280 node->type = NT_MACRO;
2281 node->value.macro = dfn;
2282 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
2283 node->flags |= NODE_WARN;
2284
2285 if (pfile->cb.define)
2286 pfile->cb.define (pfile, pfile->directive_line, node);
2287 }
2288}
2289
Kazu Hirataec5c56d2001-08-01 17:57:27 +00002290/* Process the string STR as if it appeared as the body of a #assert. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00002291void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002292cpp_assert (cpp_reader *pfile, const char *str)
Zack Weinberg45b966d2000-03-13 22:01:08 +00002293{
Neil Booth86368122000-10-31 23:34:59 +00002294 handle_assertion (pfile, str, T_ASSERT);
Zack Weinberg45b966d2000-03-13 22:01:08 +00002295}
2296
Kazu Hirataec5c56d2001-08-01 17:57:27 +00002297/* Process STR as if it appeared as the body of an #unassert. */
Zack Weinberg0b22d651999-03-15 18:42:46 +00002298void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002299cpp_unassert (cpp_reader *pfile, const char *str)
Zack Weinberg0b22d651999-03-15 18:42:46 +00002300{
Neil Booth86368122000-10-31 23:34:59 +00002301 handle_assertion (pfile, str, T_UNASSERT);
Kazu Hiratadf383482002-05-22 22:02:16 +00002302}
Zack Weinberg0b22d651999-03-15 18:42:46 +00002303
Neil Booth86368122000-10-31 23:34:59 +00002304/* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2305static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002306handle_assertion (cpp_reader *pfile, const char *str, int type)
Neil Booth86368122000-10-31 23:34:59 +00002307{
2308 size_t count = strlen (str);
2309 const char *p = strchr (str, '=');
2310
Neil Booth26aea072003-04-19 00:22:51 +00002311 /* Copy the entire option so we can modify it. Change the first
2312 "=" in the string to a '(', and tack a ')' on the end. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002313 char *buf = (char *) alloca (count + 2);
Neil Booth26aea072003-04-19 00:22:51 +00002314
2315 memcpy (buf, str, count);
Neil Booth86368122000-10-31 23:34:59 +00002316 if (p)
2317 {
Neil Booth86368122000-10-31 23:34:59 +00002318 buf[p - str] = '(';
2319 buf[count++] = ')';
Neil Booth86368122000-10-31 23:34:59 +00002320 }
Neil Booth26aea072003-04-19 00:22:51 +00002321 buf[count] = '\n';
2322 str = buf;
Neil Booth86368122000-10-31 23:34:59 +00002323
Neil Booth29401c32001-08-22 20:37:20 +00002324 run_directive (pfile, type, str, count);
Neil Booth86368122000-10-31 23:34:59 +00002325}
2326
Neil Booth7e96d762001-01-13 01:00:01 +00002327/* The options structure. */
2328cpp_options *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002329cpp_get_options (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00002330{
2331 return &pfile->opts;
2332}
2333
2334/* The callbacks structure. */
2335cpp_callbacks *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002336cpp_get_callbacks (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00002337{
2338 return &pfile->cb;
2339}
2340
2341/* Copy the given callbacks structure to our own. */
2342void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002343cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
Neil Booth7e96d762001-01-13 01:00:01 +00002344{
2345 pfile->cb = *cb;
2346}
2347
Zack Weinbergc6e83802004-06-05 20:58:06 +00002348/* The dependencies structure. (Creates one if it hasn't already been.) */
2349struct deps *
2350cpp_get_deps (cpp_reader *pfile)
2351{
2352 if (!pfile->deps)
2353 pfile->deps = deps_init ();
2354 return pfile->deps;
2355}
2356
Neil Bootheb1f4d92000-12-18 19:00:26 +00002357/* Push a new buffer on the buffer stack. Returns the new buffer; it
2358 doesn't fail. It does not generate a file change call back; that
2359 is the responsibility of the caller. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00002360cpp_buffer *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002361cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
Per Bothner40de9f72003-10-02 07:30:34 +00002362 int from_stage3)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002363{
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002364 cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
Neil Booth93c803682000-10-28 17:59:06 +00002365
Neil Boothfde84342001-08-06 21:07:41 +00002366 /* Clears, amongst other things, if_stack and mi_cmacro. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002367 memset (new_buffer, 0, sizeof (cpp_buffer));
Neil Boothad2a0842000-12-17 00:13:54 +00002368
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002369 new_buffer->next_line = new_buffer->buf = buffer;
2370 new_buffer->rlimit = buffer + len;
2371 new_buffer->from_stage3 = from_stage3;
2372 new_buffer->prev = pfile->buffer;
2373 new_buffer->need_line = true;
Neil Booth0bda4762000-12-11 07:45:16 +00002374
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002375 pfile->buffer = new_buffer;
Eric Christophercf551fb2004-01-16 22:37:49 +00002376
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002377 return new_buffer;
Zack Weinbergc71f8352000-07-05 05:33:57 +00002378}
2379
Neil Boothaf0d16c2002-04-22 17:48:02 +00002380/* Pops a single buffer, with a file change call-back if appropriate.
2381 Then pushes the next -include file, if any remain. */
Neil Boothef6e9582001-08-04 12:01:59 +00002382void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002383_cpp_pop_buffer (cpp_reader *pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002384{
Neil Boothfde84342001-08-06 21:07:41 +00002385 cpp_buffer *buffer = pfile->buffer;
Neil Booth8f9b4002003-07-29 22:26:13 +00002386 struct _cpp_file *inc = buffer->file;
Neil Boothad2a0842000-12-17 00:13:54 +00002387 struct if_stack *ifs;
Zack Weinbergc71f8352000-07-05 05:33:57 +00002388
Neil Boothfde84342001-08-06 21:07:41 +00002389 /* Walk back up the conditional stack till we reach its level at
2390 entry to this file, issuing error messages. */
2391 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
John David Anglin0527bc42003-11-01 22:56:54 +00002392 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Boothfde84342001-08-06 21:07:41 +00002393 "unterminated #%s", dtable[ifs->type].name);
2394
Neil Booth97293892001-09-14 22:04:46 +00002395 /* In case of a missing #endif. */
Neil Booth67821e32001-08-05 17:31:25 +00002396 pfile->state.skipping = 0;
Neil Booth29401c32001-08-22 20:37:20 +00002397
Neil Boothaf0d16c2002-04-22 17:48:02 +00002398 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
Neil Booth29401c32001-08-22 20:37:20 +00002399 pfile->buffer = buffer->prev;
2400
Neil Booth26aea072003-04-19 00:22:51 +00002401 free (buffer->notes);
2402
Neil Boothaf0d16c2002-04-22 17:48:02 +00002403 /* Free the buffer object now; we may want to push a new buffer
2404 in _cpp_push_next_include_file. */
2405 obstack_free (&pfile->buffer_ob, buffer);
Neil Booth29401c32001-08-22 20:37:20 +00002406
Neil Boothaf0d16c2002-04-22 17:48:02 +00002407 if (inc)
2408 {
2409 _cpp_pop_file_buffer (pfile, inc);
2410
Per Bothner40de9f72003-10-02 07:30:34 +00002411 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
Neil Boothaf0d16c2002-04-22 17:48:02 +00002412 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00002413}
2414
Kazu Hirata05713b82002-09-15 18:24:08 +00002415/* Enter all recognized directives in the hash table. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00002416void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002417_cpp_init_directives (cpp_reader *pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002418{
Neil Booth766ee682001-01-29 19:20:12 +00002419 unsigned int i;
Neil Booth93c803682000-10-28 17:59:06 +00002420 cpp_hashnode *node;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00002421
John David Anglin37b85242001-03-02 01:11:50 +00002422 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
Neil Booth93c803682000-10-28 17:59:06 +00002423 {
Neil Booth766ee682001-01-29 19:20:12 +00002424 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
Zack Weinberg4977bab2002-12-16 18:23:00 +00002425 node->is_directive = 1;
2426 node->directive_index = i;
Neil Booth93c803682000-10-28 17:59:06 +00002427 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00002428}