blob: 409d908b4e807bec09366185d76867756d4af382 [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);
Kai Tietz17e7cb82009-11-11 18:37:19 +0000129static void do_pragma_push_macro (cpp_reader *);
130static void do_pragma_pop_macro (cpp_reader *);
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +0000131
Zack Weinberg168d3732000-03-14 06:34:11 +0000132/* This is the table of directive handlers. It is ordered by
133 frequency of occurrence; the numbers at the end are directive
134 counts from all the source code I have lying around (egcs and libc
135 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
Neil Booth93c803682000-10-28 17:59:06 +0000136 pcmcia-cs-3.0.9). This is no longer important as directive lookup
Tom Tromey899015a2008-05-13 14:50:27 +0000137 is now O(1). All extensions other than #warning, #include_next,
138 and #import are deprecated. The name is where the extension
139 appears to have come from. */
Jeff Lawe9a25f71997-11-02 14:19:36 -0700140
Neil Booth93c803682000-10-28 17:59:06 +0000141#define DIRECTIVE_TABLE \
142D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000143D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000144D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
145D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
Neil Booth1a769162002-06-11 05:36:17 +0000146D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000147D(else, T_ELSE, KANDR, COND) /* 9863 */ \
148D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
149D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
Neil Booth1a769162002-06-11 05:36:17 +0000150D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
151D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000152D(error, T_ERROR, STDC89, 0) /* 475 */ \
153D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
154D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000155D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
Neil Vachharajani22143822009-10-10 00:34:21 +0000156D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000157D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
Tom Tromey899015a2008-05-13 14:50:27 +0000158D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
159D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
Neil Vachharajani22143822009-10-10 00:34:21 +0000160D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */
Zack Weinberg1ed17cd2005-05-12 18:31:38 +0000161
162/* #sccs is synonymous with #ident. */
163#define do_sccs do_ident
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000164
Zack Weinberg168d3732000-03-14 06:34:11 +0000165/* Use the table to generate a series of prototypes, an enum for the
166 directive names, and an array of directive handlers. */
167
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000168#define D(name, t, o, f) static void do_##name (cpp_reader *);
Zack Weinberg168d3732000-03-14 06:34:11 +0000169DIRECTIVE_TABLE
170#undef D
171
Zack Weinberg041c3192000-07-04 01:58:21 +0000172#define D(n, tag, o, f) tag,
Zack Weinberg168d3732000-03-14 06:34:11 +0000173enum
174{
175 DIRECTIVE_TABLE
176 N_DIRECTIVES
Per Bothner7f2935c1995-03-16 13:59:07 -0800177};
Zack Weinberg168d3732000-03-14 06:34:11 +0000178#undef D
179
Zack Weinberg041c3192000-07-04 01:58:21 +0000180#define D(name, t, origin, flags) \
Kaveh R. Ghazi9a238582003-06-16 19:14:22 +0000181{ do_##name, (const uchar *) #name, \
182 sizeof #name - 1, origin, flags },
Neil Booth93c803682000-10-28 17:59:06 +0000183static const directive dtable[] =
Zack Weinberg168d3732000-03-14 06:34:11 +0000184{
185DIRECTIVE_TABLE
186};
187#undef D
188#undef DIRECTIVE_TABLE
Per Bothner7f2935c1995-03-16 13:59:07 -0800189
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000190/* Wrapper struct directive for linemarkers.
191 The origin is more or less true - the original K+R cpp
192 did use this notation in its preprocessed output. */
193static const directive linemarker_dir =
194{
Kris Van Heesb6baa672008-04-18 13:58:08 +0000195 do_linemarker, UC"#", 1, KANDR, IN_I
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000196};
197
Neil Booth1a769162002-06-11 05:36:17 +0000198#define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
Neil Booth67821e32001-08-05 17:31:25 +0000199
Neil Booth93c803682000-10-28 17:59:06 +0000200/* Skip any remaining tokens in a directive. */
201static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000202skip_rest_of_line (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +0000203{
Neil Booth93c803682000-10-28 17:59:06 +0000204 /* Discard all stacked contexts. */
Neil Booth8128ccc2002-11-18 20:43:40 +0000205 while (pfile->context->prev)
Neil Booth93c803682000-10-28 17:59:06 +0000206 _cpp_pop_context (pfile);
207
Neil Boothb528a072000-11-12 11:46:21 +0000208 /* Sweep up all tokens remaining on the line. */
Neil Booth345894b2001-09-16 13:44:29 +0000209 if (! SEEN_EOL ())
210 while (_cpp_lex_token (pfile)->type != CPP_EOF)
211 ;
Neil Booth93c803682000-10-28 17:59:06 +0000212}
213
Joseph Myersa5cb5632009-04-18 16:28:40 +0100214/* Ensure there are no stray tokens at the end of a directive. If
215 EXPAND is true, tokens macro-expanding to nothing are allowed. */
Neil Booth93c803682000-10-28 17:59:06 +0000216static void
Joseph Myersa5cb5632009-04-18 16:28:40 +0100217check_eol (cpp_reader *pfile, bool expand)
Neil Booth93c803682000-10-28 17:59:06 +0000218{
Joseph Myersa5cb5632009-04-18 16:28:40 +0100219 if (! SEEN_EOL () && (expand
220 ? cpp_get_token (pfile)
221 : _cpp_lex_token (pfile))->type != CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000222 cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
Neil Boothebef4e82002-04-14 18:42:47 +0000223 pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +0000224}
225
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000226/* Ensure there are no stray tokens other than comments at the end of
227 a directive, and gather the comments. */
228static const cpp_token **
229check_eol_return_comments (cpp_reader *pfile)
230{
231 size_t c;
232 size_t capacity = 8;
233 const cpp_token **buf;
234
235 buf = XNEWVEC (const cpp_token *, capacity);
236 c = 0;
237 if (! SEEN_EOL ())
238 {
239 while (1)
240 {
241 const cpp_token *tok;
242
243 tok = _cpp_lex_token (pfile);
244 if (tok->type == CPP_EOF)
245 break;
246 if (tok->type != CPP_COMMENT)
247 cpp_error (pfile, CPP_DL_PEDWARN,
248 "extra tokens at end of #%s directive",
249 pfile->directive->name);
250 else
251 {
252 if (c + 1 >= capacity)
253 {
254 capacity *= 2;
255 buf = XRESIZEVEC (const cpp_token *, buf, capacity);
256 }
257 buf[c] = tok;
258 ++c;
259 }
260 }
261 }
262 buf[c] = NULL;
263 return buf;
264}
265
Neil Boothfe6c2db2000-11-15 19:25:22 +0000266/* Called when entering a directive, _Pragma or command-line directive. */
267static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000268start_directive (cpp_reader *pfile)
Zack Weinbergc5a04732000-04-25 19:32:36 +0000269{
Neil Booth7f2f1a62000-11-14 18:32:06 +0000270 /* Setup in-directive state. */
271 pfile->state.in_directive = 1;
272 pfile->state.save_comments = 0;
Zack Weinberg21b11492004-09-09 19:16:56 +0000273 pfile->directive_result.type = CPP_PADDING;
Neil Booth7f2f1a62000-11-14 18:32:06 +0000274
Neil Booth93c803682000-10-28 17:59:06 +0000275 /* Some handlers need the position of the # for diagnostics. */
Per Bothner500bee02004-04-22 19:22:27 -0700276 pfile->directive_line = pfile->line_table->highest_line;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000277}
278
279/* Called when leaving a directive, _Pragma or command-line directive. */
280static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000281end_directive (cpp_reader *pfile, int skip_line)
Neil Boothfe6c2db2000-11-15 19:25:22 +0000282{
Richard Hendersonbc4071d2006-01-04 08:33:38 -0800283 if (pfile->state.in_deferred_pragma)
284 ;
285 else if (CPP_OPTION (pfile, traditional))
Neil Booth1a769162002-06-11 05:36:17 +0000286 {
Neil Boothd97371e2002-06-18 06:27:40 +0000287 /* Revert change of prepare_directive_trad. */
288 pfile->state.prevent_expansion--;
289
Neil Boothb66377c2002-06-13 21:16:00 +0000290 if (pfile->directive != &dtable[T_DEFINE])
Neil Booth1a769162002-06-11 05:36:17 +0000291 _cpp_remove_overlay (pfile);
292 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000293 /* We don't skip for an assembler #. */
Neil Boothb66377c2002-06-13 21:16:00 +0000294 else if (skip_line)
Neil Booth67821e32001-08-05 17:31:25 +0000295 {
296 skip_rest_of_line (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +0000297 if (!pfile->keep_tokens)
298 {
299 pfile->cur_run = &pfile->base_run;
300 pfile->cur_token = pfile->base_run.base;
301 }
Neil Booth67821e32001-08-05 17:31:25 +0000302 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000303
304 /* Restore state. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000305 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
306 pfile->state.in_directive = 0;
Neil Boothd97371e2002-06-18 06:27:40 +0000307 pfile->state.in_expression = 0;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000308 pfile->state.angled_headers = 0;
309 pfile->directive = 0;
310}
311
Neil Booth1a769162002-06-11 05:36:17 +0000312/* Prepare to handle the directive in pfile->directive. */
313static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000314prepare_directive_trad (cpp_reader *pfile)
Neil Booth1a769162002-06-11 05:36:17 +0000315{
Neil Booth951a0762002-06-27 06:01:58 +0000316 if (pfile->directive != &dtable[T_DEFINE])
Neil Booth1a769162002-06-11 05:36:17 +0000317 {
Neil Boothb66377c2002-06-13 21:16:00 +0000318 bool no_expand = (pfile->directive
319 && ! (pfile->directive->flags & EXPAND));
Neil Booth974c43f2002-06-13 06:25:28 +0000320 bool was_skipping = pfile->state.skipping;
Neil Booth1a769162002-06-11 05:36:17 +0000321
Neil Boothd97371e2002-06-18 06:27:40 +0000322 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
323 || pfile->directive == &dtable[T_ELIF]);
Neil Booth45f24922003-12-12 07:00:29 +0000324 if (pfile->state.in_expression)
325 pfile->state.skipping = false;
326
Neil Booth1a769162002-06-11 05:36:17 +0000327 if (no_expand)
328 pfile->state.prevent_expansion++;
Zack Weinberg43839642003-07-13 17:34:18 +0000329 _cpp_scan_out_logical_line (pfile, NULL);
Neil Booth1a769162002-06-11 05:36:17 +0000330 if (no_expand)
331 pfile->state.prevent_expansion--;
Neil Booth45f24922003-12-12 07:00:29 +0000332
Neil Booth974c43f2002-06-13 06:25:28 +0000333 pfile->state.skipping = was_skipping;
Neil Booth1a769162002-06-11 05:36:17 +0000334 _cpp_overlay_buffer (pfile, pfile->out.base,
335 pfile->out.cur - pfile->out.base);
336 }
Neil Boothd97371e2002-06-18 06:27:40 +0000337
338 /* Stop ISO C from expanding anything. */
339 pfile->state.prevent_expansion++;
Neil Booth1a769162002-06-11 05:36:17 +0000340}
341
Kazu Hiratada7d8302002-09-22 02:03:17 +0000342/* Output diagnostics for a directive DIR. INDENTED is nonzero if
Neil Booth18a9d8f2001-09-16 11:23:56 +0000343 the '#' was indented. */
Neil Booth18a9d8f2001-09-16 11:23:56 +0000344static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000345directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000346{
Tom Tromey899015a2008-05-13 14:50:27 +0000347 /* Issue -pedantic or deprecated warnings for extensions. We let
348 -pedantic take precedence if both are applicable. */
349 if (! pfile->state.skipping)
350 {
351 if (dir->origin == EXTENSION
352 && !(dir == &dtable[T_IMPORT] && CPP_OPTION (pfile, objc))
353 && CPP_PEDANTIC (pfile))
354 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
355 else if (((dir->flags & DEPRECATED) != 0
356 || (dir == &dtable[T_IMPORT] && !CPP_OPTION (pfile, objc)))
357 && CPP_OPTION (pfile, warn_deprecated))
358 cpp_error (pfile, CPP_DL_WARNING, "#%s is a deprecated GCC extension",
359 dir->name);
360 }
Neil Booth18a9d8f2001-09-16 11:23:56 +0000361
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000362 /* Traditionally, a directive is ignored unless its # is in
363 column 1. Therefore in code intended to work with K+R
364 compilers, directives added by C89 must have their #
365 indented, and directives present in traditional C must not.
366 This is true even of directives in skipped conditional
367 blocks. #elif cannot be used at all. */
368 if (CPP_WTRADITIONAL (pfile))
369 {
370 if (dir == &dtable[T_ELIF])
John David Anglin0527bc42003-11-01 22:56:54 +0000371 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000372 "suggest not using #elif in traditional C");
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000373 else if (indented && dir->origin == KANDR)
John David Anglin0527bc42003-11-01 22:56:54 +0000374 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000375 "traditional C ignores #%s with the # indented",
376 dir->name);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000377 else if (!indented && dir->origin != KANDR)
John David Anglin0527bc42003-11-01 22:56:54 +0000378 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000379 "suggest hiding #%s from traditional C with an indented #",
380 dir->name);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000381 }
382}
383
Kazu Hiratada7d8302002-09-22 02:03:17 +0000384/* Check if we have a known directive. INDENTED is nonzero if the
Neil Booth18a9d8f2001-09-16 11:23:56 +0000385 '#' of the directive was indented. This function is in this file
Gabriel Dos Reisa2566ae2005-01-02 01:32:21 +0000386 to save unnecessarily exporting dtable etc. to lex.c. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +0000387 nonzero if the line of tokens has been handled, zero if we should
Neil Booth18a9d8f2001-09-16 11:23:56 +0000388 continue processing the line. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000389int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000390_cpp_handle_directive (cpp_reader *pfile, int indented)
Neil Boothfe6c2db2000-11-15 19:25:22 +0000391{
Neil Boothfe6c2db2000-11-15 19:25:22 +0000392 const directive *dir = 0;
Neil Booth345894b2001-09-16 13:44:29 +0000393 const cpp_token *dname;
Neil Boothe808ec92002-02-27 07:24:53 +0000394 bool was_parsing_args = pfile->state.parsing_args;
Zack Weinbergc6e83802004-06-05 20:58:06 +0000395 bool was_discarding_output = pfile->state.discarding_output;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000396 int skip = 1;
397
Zack Weinbergc6e83802004-06-05 20:58:06 +0000398 if (was_discarding_output)
399 pfile->state.prevent_expansion = 0;
400
Neil Boothe808ec92002-02-27 07:24:53 +0000401 if (was_parsing_args)
402 {
403 if (CPP_OPTION (pfile, pedantic))
John David Anglin0527bc42003-11-01 22:56:54 +0000404 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothe808ec92002-02-27 07:24:53 +0000405 "embedding a directive within macro arguments is not portable");
406 pfile->state.parsing_args = 0;
407 pfile->state.prevent_expansion = 0;
408 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000409 start_directive (pfile);
Neil Booth345894b2001-09-16 13:44:29 +0000410 dname = _cpp_lex_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000411
Neil Booth345894b2001-09-16 13:44:29 +0000412 if (dname->type == CPP_NAME)
Neil Booth0d9f2342000-09-18 18:43:05 +0000413 {
Joseph Myers9a0c6182009-05-10 15:27:32 +0100414 if (dname->val.node.node->is_directive)
415 dir = &dtable[dname->val.node.node->directive_index];
Neil Booth93c803682000-10-28 17:59:06 +0000416 }
Kazu Hirata05713b82002-09-15 18:24:08 +0000417 /* We do not recognize the # followed by a number extension in
Neil Booth18a9d8f2001-09-16 11:23:56 +0000418 assembler code. */
Neil Booth345894b2001-09-16 13:44:29 +0000419 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth93c803682000-10-28 17:59:06 +0000420 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000421 dir = &linemarker_dir;
422 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
423 && ! pfile->state.skipping)
John David Anglin0527bc42003-11-01 22:56:54 +0000424 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothebef4e82002-04-14 18:42:47 +0000425 "style of line directive is a GCC extension");
Neil Booth0d9f2342000-09-18 18:43:05 +0000426 }
427
Neil Booth93c803682000-10-28 17:59:06 +0000428 if (dir)
Neil Booth0d9f2342000-09-18 18:43:05 +0000429 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000430 /* If we have a directive that is not an opening conditional,
431 invalidate any control macro. */
432 if (! (dir->flags & IF_COND))
433 pfile->mi_valid = false;
Neil Booth93c803682000-10-28 17:59:06 +0000434
Neil Booth18a9d8f2001-09-16 11:23:56 +0000435 /* Kluge alert. In order to be sure that code like this
436
437 #define HASH #
438 HASH define foo bar
439
440 does not cause '#define foo bar' to get executed when
441 compiled with -save-temps, we recognize directives in
Gabriel Dos Reisa2566ae2005-01-02 01:32:21 +0000442 -fpreprocessed mode only if the # is in column 1. macro.c
Ollie Wildccfc4c92007-07-30 18:29:20 +0000443 puts a space in front of any '#' at the start of a macro.
444
445 We exclude the -fdirectives-only case because macro expansion
446 has not been performed yet, and block comments can cause spaces
447 to preceed the directive. */
Neil Booth18a9d8f2001-09-16 11:23:56 +0000448 if (CPP_OPTION (pfile, preprocessed)
Ollie Wildccfc4c92007-07-30 18:29:20 +0000449 && !CPP_OPTION (pfile, directives_only)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000450 && (indented || !(dir->flags & IN_I)))
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000451 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000452 skip = 0;
453 dir = 0;
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000454 }
455 else
Neil Booth93c803682000-10-28 17:59:06 +0000456 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000457 /* In failed conditional groups, all non-conditional
458 directives are ignored. Before doing that, whether
459 skipping or not, we should lex angle-bracketed headers
460 correctly, and maybe output some diagnostics. */
461 pfile->state.angled_headers = dir->flags & INCL;
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000462 pfile->state.directive_wants_padding = dir->flags & INCL;
Neil Booth18a9d8f2001-09-16 11:23:56 +0000463 if (! CPP_OPTION (pfile, preprocessed))
464 directive_diagnostics (pfile, dir, indented);
465 if (pfile->state.skipping && !(dir->flags & COND))
466 dir = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000467 }
468 }
Neil Booth345894b2001-09-16 13:44:29 +0000469 else if (dname->type == CPP_EOF)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000470 ; /* CPP_EOF is the "null directive". */
471 else
Neil Booth0d9f2342000-09-18 18:43:05 +0000472 {
Neil Booth93c803682000-10-28 17:59:06 +0000473 /* An unknown directive. Don't complain about it in assembly
474 source: we don't know where the comments are, and # may
475 introduce assembler pseudo-ops. Don't complain about invalid
476 directives in skipped conditional groups (6.10 p4). */
Neil Boothbdb05a72000-11-26 17:31:13 +0000477 if (CPP_OPTION (pfile, lang) == CLK_ASM)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000478 skip = 0;
479 else if (!pfile->state.skipping)
John David Anglin0527bc42003-11-01 22:56:54 +0000480 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
Neil Booth345894b2001-09-16 13:44:29 +0000481 cpp_token_as_text (pfile, dname));
Neil Booth0d9f2342000-09-18 18:43:05 +0000482 }
483
Neil Boothd1a58682002-06-28 06:26:54 +0000484 pfile->directive = dir;
485 if (CPP_OPTION (pfile, traditional))
486 prepare_directive_trad (pfile);
487
Neil Booth18a9d8f2001-09-16 11:23:56 +0000488 if (dir)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000489 pfile->directive->handler (pfile);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000490 else if (skip == 0)
491 _cpp_backup_tokens (pfile, 1);
492
493 end_directive (pfile, skip);
Jakub Jelinek765d6002008-01-25 10:01:27 +0100494 if (was_parsing_args && !pfile->state.in_deferred_pragma)
Neil Boothe808ec92002-02-27 07:24:53 +0000495 {
496 /* Restore state when within macro args. */
497 pfile->state.parsing_args = 2;
498 pfile->state.prevent_expansion = 1;
Neil Boothe808ec92002-02-27 07:24:53 +0000499 }
Zack Weinbergc6e83802004-06-05 20:58:06 +0000500 if (was_discarding_output)
501 pfile->state.prevent_expansion = 1;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000502 return skip;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000503}
504
Neil Booth93c803682000-10-28 17:59:06 +0000505/* Directive handler wrapper used by the command line option
Neil Booth26aea072003-04-19 00:22:51 +0000506 processor. BUF is \n terminated. */
Neil Booth93c803682000-10-28 17:59:06 +0000507static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000508run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000509{
Neil Booth562a5c22002-04-21 18:46:42 +0000510 cpp_push_buffer (pfile, (const uchar *) buf, count,
Per Bothner40de9f72003-10-02 07:30:34 +0000511 /* from_stage3 */ true);
Neil Booth0bda4762000-12-11 07:45:16 +0000512 start_directive (pfile);
Neil Booth26aea072003-04-19 00:22:51 +0000513
514 /* This is a short-term fix to prevent a leading '#' being
515 interpreted as a directive. */
516 _cpp_clean_line (pfile);
517
Neil Boothf71aebb2001-05-27 18:06:00 +0000518 pfile->directive = &dtable[dir_no];
Neil Booth1a769162002-06-11 05:36:17 +0000519 if (CPP_OPTION (pfile, traditional))
520 prepare_directive_trad (pfile);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000521 pfile->directive->handler (pfile);
Neil Booth0bda4762000-12-11 07:45:16 +0000522 end_directive (pfile, 1);
Neil Boothef6e9582001-08-04 12:01:59 +0000523 _cpp_pop_buffer (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000524}
Per Bothner7f2935c1995-03-16 13:59:07 -0800525
Neil Booth93c803682000-10-28 17:59:06 +0000526/* Checks for validity the macro name in #define, #undef, #ifdef and
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000527 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
528 processing a #define or #undefine directive, and false
529 otherwise. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000530static cpp_hashnode *
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000531lex_macro_node (cpp_reader *pfile, bool is_def_or_undef)
Zack Weinberg041c3192000-07-04 01:58:21 +0000532{
Neil Booth1a769162002-06-11 05:36:17 +0000533 const cpp_token *token = _cpp_lex_token (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000534
Zack Weinberg92936ec2000-07-19 20:18:08 +0000535 /* The token immediately after #define must be an identifier. That
Zack Weinbergb8363a22001-07-01 18:48:13 +0000536 identifier may not be "defined", per C99 6.10.8p4.
537 In C++, it may not be any of the "named operators" either,
538 per C++98 [lex.digraph], [lex.key].
539 Finally, the identifier may not have been poisoned. (In that case
Neil Booth1d63a282002-06-28 20:27:14 +0000540 the lexer has issued the error message for us.) */
Zack Weinbergb8363a22001-07-01 18:48:13 +0000541
Neil Booth1a769162002-06-11 05:36:17 +0000542 if (token->type == CPP_NAME)
543 {
Joseph Myers9a0c6182009-05-10 15:27:32 +0100544 cpp_hashnode *node = token->val.node.node;
Neil Booth1a769162002-06-11 05:36:17 +0000545
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000546 if (is_def_or_undef && node == pfile->spec_nodes.n_defined)
John David Anglin0527bc42003-11-01 22:56:54 +0000547 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth1a769162002-06-11 05:36:17 +0000548 "\"defined\" cannot be used as a macro name");
549 else if (! (node->flags & NODE_POISONED))
550 return node;
551 }
552 else if (token->flags & NAMED_OP)
John David Anglin0527bc42003-11-01 22:56:54 +0000553 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcbc69f82002-06-05 20:27:12 +0000554 "\"%s\" cannot be used as a macro name as it is an operator in C++",
Joseph Myers9a0c6182009-05-10 15:27:32 +0100555 NODE_NAME (token->val.node.node));
Neil Booth1a769162002-06-11 05:36:17 +0000556 else if (token->type == CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000557 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
Neil Booth1a769162002-06-11 05:36:17 +0000558 pfile->directive->name);
559 else
John David Anglin0527bc42003-11-01 22:56:54 +0000560 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
Zack Weinbergb8363a22001-07-01 18:48:13 +0000561
Neil Boothcbc69f82002-06-05 20:27:12 +0000562 return NULL;
Per Bothner7f2935c1995-03-16 13:59:07 -0800563}
Per Bothner7f2935c1995-03-16 13:59:07 -0800564
Gabriel Dos Reisa2566ae2005-01-02 01:32:21 +0000565/* Process a #define directive. Most work is done in macro.c. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000566static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000567do_define (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800568{
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000569 cpp_hashnode *node = lex_macro_node (pfile, true);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000570
Neil Booth93c803682000-10-28 17:59:06 +0000571 if (node)
572 {
Neil Booth1d63a282002-06-28 20:27:14 +0000573 /* If we have been requested to expand comments into macros,
574 then re-enable saving of comments. */
575 pfile->state.save_comments =
576 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
577
Joseph Myers93d45d92008-04-02 20:42:53 +0100578 if (pfile->cb.before_define)
579 pfile->cb.before_define (pfile);
580
Neil Booth93c803682000-10-28 17:59:06 +0000581 if (_cpp_create_definition (pfile, node))
582 if (pfile->cb.define)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000583 pfile->cb.define (pfile, pfile->directive_line, node);
Joseph Myers93d45d92008-04-02 20:42:53 +0100584
585 node->flags &= ~NODE_USED;
Neil Booth93c803682000-10-28 17:59:06 +0000586 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800587}
588
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000589/* Handle #undef. Mark the identifier NT_VOID in the hash table. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000590static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000591do_undef (cpp_reader *pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000592{
Tom Tromeyee1c2a12007-01-12 19:46:49 +0000593 cpp_hashnode *node = lex_macro_node (pfile, true);
Zack Weinberg041c3192000-07-04 01:58:21 +0000594
Neil Booth45f24922003-12-12 07:00:29 +0000595 if (node)
Zack Weinberg041c3192000-07-04 01:58:21 +0000596 {
Joseph Myers93d45d92008-04-02 20:42:53 +0100597 if (pfile->cb.before_define)
598 pfile->cb.before_define (pfile);
599
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000600 if (pfile->cb.undef)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000601 pfile->cb.undef (pfile, pfile->directive_line, node);
Zack Weinberg041c3192000-07-04 01:58:21 +0000602
Neil Booth45f24922003-12-12 07:00:29 +0000603 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
604 identifier is not currently defined as a macro name. */
605 if (node->type == NT_MACRO)
606 {
607 if (node->flags & NODE_WARN)
608 cpp_error (pfile, CPP_DL_WARNING,
609 "undefining \"%s\"", NODE_NAME (node));
Zack Weinberg041c3192000-07-04 01:58:21 +0000610
Neil Booth45f24922003-12-12 07:00:29 +0000611 if (CPP_OPTION (pfile, warn_unused_macros))
612 _cpp_warn_if_unused_macro (pfile, node, NULL);
Neil Bootha69cbaa2002-07-23 22:57:49 +0000613
Neil Booth45f24922003-12-12 07:00:29 +0000614 _cpp_free_definition (node);
615 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000616 }
Neil Booth45f24922003-12-12 07:00:29 +0000617
Joseph Myersa5cb5632009-04-18 16:28:40 +0100618 check_eol (pfile, false);
Zack Weinberg041c3192000-07-04 01:58:21 +0000619}
620
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000621/* Undefine a single macro/assertion/whatever. */
622
623static int
Zack Weinbergc6e83802004-06-05 20:58:06 +0000624undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000625 void *data_p ATTRIBUTE_UNUSED)
626{
Zack Weinbergc6e83802004-06-05 20:58:06 +0000627 /* Body of _cpp_free_definition inlined here for speed.
628 Macros and assertions no longer have anything to free. */
629 h->type = NT_VOID;
Joseph Myers93d45d92008-04-02 20:42:53 +0100630 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000631 return 1;
632}
633
634/* Undefine all macros and assertions. */
635
636void
637cpp_undef_all (cpp_reader *pfile)
638{
639 cpp_forall_identifiers (pfile, undefine_macros, NULL);
640}
641
642
Neil Booth93c803682000-10-28 17:59:06 +0000643/* Helper routine used by parse_include. Reinterpret the current line
644 as an h-char-sequence (< ... >); we are looking at the first token
Neil Booth74eb4b32003-04-21 19:21:59 +0000645 after the <. Returns a malloced filename. */
646static char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000647glue_header_name (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800648{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000649 const cpp_token *token;
Neil Booth74eb4b32003-04-21 19:21:59 +0000650 char *buffer;
Neil Booth2450e0b2002-02-23 20:21:39 +0000651 size_t len, total_len = 0, capacity = 1024;
Per Bothner7f2935c1995-03-16 13:59:07 -0800652
Neil Booth93c803682000-10-28 17:59:06 +0000653 /* To avoid lexed tokens overwriting our glued name, we can only
654 allocate from the string pool once we've lexed everything. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000655 buffer = XNEWVEC (char, capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000656 for (;;)
Zack Weinberg168d3732000-03-14 06:34:11 +0000657 {
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000658 token = get_token_no_padding (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000659
Neil Booth74eb4b32003-04-21 19:21:59 +0000660 if (token->type == CPP_GREATER)
Neil Booth93c803682000-10-28 17:59:06 +0000661 break;
Neil Booth74eb4b32003-04-21 19:21:59 +0000662 if (token->type == CPP_EOF)
663 {
John David Anglin0527bc42003-11-01 22:56:54 +0000664 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
Neil Booth74eb4b32003-04-21 19:21:59 +0000665 break;
666 }
Neil Booth93c803682000-10-28 17:59:06 +0000667
Neil Booth59325652003-04-24 20:03:57 +0000668 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
Neil Booth2450e0b2002-02-23 20:21:39 +0000669 if (total_len + len > capacity)
Neil Booth93c803682000-10-28 17:59:06 +0000670 {
Neil Booth2450e0b2002-02-23 20:21:39 +0000671 capacity = (capacity + len) * 2;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000672 buffer = XRESIZEVEC (char, buffer, capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000673 }
674
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000675 if (token->flags & PREV_WHITE)
Neil Booth2450e0b2002-02-23 20:21:39 +0000676 buffer[total_len++] = ' ';
Neil Booth93c803682000-10-28 17:59:06 +0000677
Geoffrey Keating47e20492005-03-12 10:44:06 +0000678 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
679 true)
Neil Booth74eb4b32003-04-21 19:21:59 +0000680 - (uchar *) buffer);
Neil Booth93c803682000-10-28 17:59:06 +0000681 }
682
Neil Booth74eb4b32003-04-21 19:21:59 +0000683 buffer[total_len] = '\0';
684 return buffer;
Neil Booth93c803682000-10-28 17:59:06 +0000685}
686
Neil Booth74eb4b32003-04-21 19:21:59 +0000687/* Returns the file name of #include, #include_next, #import and
688 #pragma dependency. The string is malloced and the caller should
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +0000689 free it. Returns NULL on error. LOCATION is the source location
690 of the file name. */
691
Neil Booth74eb4b32003-04-21 19:21:59 +0000692static const char *
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000693parse_include (cpp_reader *pfile, int *pangle_brackets,
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +0000694 const cpp_token ***buf, source_location *location)
Neil Booth93c803682000-10-28 17:59:06 +0000695{
Neil Booth74eb4b32003-04-21 19:21:59 +0000696 char *fname;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000697 const cpp_token *header;
Neil Booth93c803682000-10-28 17:59:06 +0000698
Neil Booth93c803682000-10-28 17:59:06 +0000699 /* Allow macro expansion. */
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000700 header = get_token_no_padding (pfile);
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +0000701 *location = header->src_loc;
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +0200702 if ((header->type == CPP_STRING && header->val.str.text[0] != 'R')
703 || header->type == CPP_HEADER_NAME)
Neil Booth93c803682000-10-28 17:59:06 +0000704 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000705 fname = XNEWVEC (char, header->val.str.len - 1);
Neil Booth6338b352003-04-23 22:44:06 +0000706 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
707 fname[header->val.str.len - 2] = '\0';
Neil Booth74eb4b32003-04-21 19:21:59 +0000708 *pangle_brackets = header->type == CPP_HEADER_NAME;
Zack Weinberg041c3192000-07-04 01:58:21 +0000709 }
Neil Booth74eb4b32003-04-21 19:21:59 +0000710 else if (header->type == CPP_LESS)
Zack Weinberg041c3192000-07-04 01:58:21 +0000711 {
Neil Booth74eb4b32003-04-21 19:21:59 +0000712 fname = glue_header_name (pfile);
713 *pangle_brackets = 1;
714 }
715 else
716 {
717 const unsigned char *dir;
718
719 if (pfile->directive == &dtable[T_PRAGMA])
Kris Van Heesb6baa672008-04-18 13:58:08 +0000720 dir = UC"pragma dependency";
Neil Booth74eb4b32003-04-21 19:21:59 +0000721 else
722 dir = pfile->directive->name;
John David Anglin0527bc42003-11-01 22:56:54 +0000723 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
Neil Booth74eb4b32003-04-21 19:21:59 +0000724 dir);
725
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000726 return NULL;
Richard Kennercfb3ee11997-04-13 14:30:13 -0400727 }
728
Tom Tromeycda5e672007-08-18 17:54:11 +0000729 if (pfile->directive == &dtable[T_PRAGMA])
730 {
731 /* This pragma allows extra tokens after the file name. */
732 }
733 else if (buf == NULL || CPP_OPTION (pfile, discard_comments))
Joseph Myers61cc8222009-04-18 21:25:07 +0100734 check_eol (pfile, true);
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000735 else
736 {
737 /* If we are not discarding comments, then gather them while
738 doing the eol check. */
739 *buf = check_eol_return_comments (pfile);
740 }
741
Neil Booth74eb4b32003-04-21 19:21:59 +0000742 return fname;
Zack Weinberg168d3732000-03-14 06:34:11 +0000743}
744
Neil Boothba133c92001-03-15 07:57:13 +0000745/* Handle #include, #include_next and #import. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000746static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000747do_include_common (cpp_reader *pfile, enum include_type type)
Zack Weinberg168d3732000-03-14 06:34:11 +0000748{
Neil Booth74eb4b32003-04-21 19:21:59 +0000749 const char *fname;
750 int angle_brackets;
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000751 const cpp_token **buf = NULL;
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +0000752 source_location location;
Neil Booth74eb4b32003-04-21 19:21:59 +0000753
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000754 /* Re-enable saving of comments if requested, so that the include
755 callback can dump comments which follow #include. */
756 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
757
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +0000758 fname = parse_include (pfile, &angle_brackets, &buf, &location);
Neil Booth74eb4b32003-04-21 19:21:59 +0000759 if (!fname)
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000760 {
761 if (buf)
762 XDELETEVEC (buf);
763 return;
764 }
Zack Weinberg168d3732000-03-14 06:34:11 +0000765
Nathanael Nerode28303822004-11-28 22:28:13 +0000766 if (!*fname)
767 {
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +0000768 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
769 "empty filename in #%s",
770 pfile->directive->name);
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000771 XDELETEVEC (fname);
772 if (buf)
773 XDELETEVEC (buf);
Nathanael Nerode28303822004-11-28 22:28:13 +0000774 return;
775 }
776
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000777 /* Prevent #include recursion. */
Per Bothner50f59cd2004-01-19 21:30:18 -0800778 if (pfile->line_table->depth >= CPP_STACK_MAX)
John David Anglin0527bc42003-11-01 22:56:54 +0000779 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
Neil Booth74eb4b32003-04-21 19:21:59 +0000780 else
Neil Booth09b82252001-07-29 22:27:20 +0000781 {
Neil Booth74eb4b32003-04-21 19:21:59 +0000782 /* Get out of macro context, if we are. */
783 skip_rest_of_line (pfile);
784
785 if (pfile->cb.include)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000786 pfile->cb.include (pfile, pfile->directive_line,
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000787 pfile->directive->name, fname, angle_brackets,
788 buf);
Neil Booth74eb4b32003-04-21 19:21:59 +0000789
Neil Booth8f9b4002003-07-29 22:26:13 +0000790 _cpp_stack_include (pfile, fname, angle_brackets, type);
Neil Booth09b82252001-07-29 22:27:20 +0000791 }
792
Ian Lance Taylorcbc43ae2005-10-04 18:06:19 +0000793 XDELETEVEC (fname);
794 if (buf)
795 XDELETEVEC (buf);
Neil Boothba133c92001-03-15 07:57:13 +0000796}
797
798static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000799do_include (cpp_reader *pfile)
Neil Boothba133c92001-03-15 07:57:13 +0000800{
801 do_include_common (pfile, IT_INCLUDE);
Zack Weinberg168d3732000-03-14 06:34:11 +0000802}
803
Zack Weinberg711b8822000-07-18 00:59:49 +0000804static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000805do_import (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +0000806{
Neil Boothba133c92001-03-15 07:57:13 +0000807 do_include_common (pfile, IT_IMPORT);
Zack Weinberg168d3732000-03-14 06:34:11 +0000808}
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000809
Zack Weinberg711b8822000-07-18 00:59:49 +0000810static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000811do_include_next (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +0000812{
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000813 enum include_type type = IT_INCLUDE_NEXT;
814
815 /* If this is the primary source file, warn and use the normal
816 search logic. */
Tom Tromey705e2d22007-01-04 15:32:26 +0000817 if (cpp_in_primary_file (pfile))
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000818 {
John David Anglin0527bc42003-11-01 22:56:54 +0000819 cpp_error (pfile, CPP_DL_WARNING,
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000820 "#include_next in primary source file");
821 type = IT_INCLUDE;
822 }
823 do_include_common (pfile, type);
Per Bothner7f2935c1995-03-16 13:59:07 -0800824}
825
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000826/* Subroutine of do_linemarker. Read possible flags after file name.
827 LAST is the last flag seen; 0 if this is the first flag. Return the
828 flag if it is valid, 0 at the end of the directive. Otherwise
829 complain. */
Neil Booth642ce432000-12-07 23:17:56 +0000830static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000831read_flag (cpp_reader *pfile, unsigned int last)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000832{
Neil Booth345894b2001-09-16 13:44:29 +0000833 const cpp_token *token = _cpp_lex_token (pfile);
Jason Merrilld3a34a01999-08-14 00:42:07 +0000834
Neil Booth345894b2001-09-16 13:44:29 +0000835 if (token->type == CPP_NUMBER && token->val.str.len == 1)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000836 {
Neil Booth345894b2001-09-16 13:44:29 +0000837 unsigned int flag = token->val.str.text[0] - '0';
Neil Booth28e0f042000-12-09 12:06:37 +0000838
839 if (flag > last && flag <= 4
840 && (flag != 4 || last == 3)
841 && (flag != 2 || last == 0))
842 return flag;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000843 }
Neil Booth93c803682000-10-28 17:59:06 +0000844
Neil Booth345894b2001-09-16 13:44:29 +0000845 if (token->type != CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000846 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
Neil Booth345894b2001-09-16 13:44:29 +0000847 cpp_token_as_text (pfile, token));
Neil Booth93c803682000-10-28 17:59:06 +0000848 return 0;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000849}
850
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000851/* Subroutine of do_line and do_linemarker. Convert a number in STR,
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000852 of length LEN, to binary; store it in NUMP, and return false if the
853 number was well-formed, true if not. WRAPPED is set to true if the
854 number did not fit into 'unsigned long'. */
855static bool
856strtolinenum (const uchar *str, size_t len, linenum_type *nump, bool *wrapped)
Zack Weinberg041c3192000-07-04 01:58:21 +0000857{
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000858 linenum_type reg = 0;
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000859 linenum_type reg_prev = 0;
860
Neil Booth562a5c22002-04-21 18:46:42 +0000861 uchar c;
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000862 *wrapped = false;
Zack Weinberg041c3192000-07-04 01:58:21 +0000863 while (len--)
864 {
865 c = *str++;
866 if (!ISDIGIT (c))
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000867 return true;
Zack Weinberg041c3192000-07-04 01:58:21 +0000868 reg *= 10;
869 reg += c - '0';
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000870 if (reg < reg_prev)
871 *wrapped = true;
872 reg_prev = reg;
Zack Weinberg041c3192000-07-04 01:58:21 +0000873 }
874 *nump = reg;
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000875 return false;
Zack Weinberg041c3192000-07-04 01:58:21 +0000876}
877
Zack Weinberg5538ada1999-02-04 06:36:54 -0500878/* Interpret #line command.
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000879 Note that the filename string (if any) is a true string constant
880 (escapes are interpreted), unlike in #line. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000881static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000882do_line (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800883{
Per Bothner500bee02004-04-22 19:22:27 -0700884 const struct line_maps *line_table = pfile->line_table;
885 const struct line_map *map = &line_table->maps[line_table->used - 1];
Devang Patel2203a882005-02-28 11:04:19 -0800886
887 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
888 sysp right now. */
889
890 unsigned char map_sysp = map->sysp;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000891 const cpp_token *token;
Per Bothner12f9df42004-02-11 07:29:30 -0800892 const char *new_file = map->to_file;
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000893 linenum_type new_lineno;
Per Bothner7f2935c1995-03-16 13:59:07 -0800894
Neil Booth27e25642000-11-27 08:00:04 +0000895 /* C99 raised the minimum limit on #line numbers. */
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000896 linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000897 bool wrapped;
Neil Booth18a9d8f2001-09-16 11:23:56 +0000898
Neil Booth93c803682000-10-28 17:59:06 +0000899 /* #line commands expand macros. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000900 token = cpp_get_token (pfile);
901 if (token->type != CPP_NUMBER
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000902 || strtolinenum (token->val.str.text, token->val.str.len,
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000903 &new_lineno, &wrapped))
Per Bothner7f2935c1995-03-16 13:59:07 -0800904 {
Tom Tromey33ae4832008-01-03 17:58:26 +0000905 if (token->type == CPP_EOF)
906 cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line");
907 else
908 cpp_error (pfile, CPP_DL_ERROR,
909 "\"%s\" after #line is not a positive integer",
910 cpp_token_as_text (pfile, token));
Zack Weinberg9ec72912000-08-09 19:41:12 +0000911 return;
Kazu Hiratadf383482002-05-22 22:02:16 +0000912 }
Zack Weinberg5538ada1999-02-04 06:36:54 -0500913
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000914 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap || wrapped))
John David Anglin0527bc42003-11-01 22:56:54 +0000915 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000916 else if (wrapped)
917 cpp_error (pfile, CPP_DL_WARNING, "line number out of range");
Zack Weinberg5538ada1999-02-04 06:36:54 -0500918
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000919 token = cpp_get_token (pfile);
920 if (token->type == CPP_STRING)
Zack Weinberg5538ada1999-02-04 06:36:54 -0500921 {
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000922 cpp_string s = { 0, 0 };
Eric Christopher423e95e2004-02-12 02:25:03 +0000923 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
Jerry Quinnf1bf4102009-07-18 03:22:16 +0000924 &s, CPP_STRING))
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000925 new_file = (const char *)s.text;
Joseph Myersa5cb5632009-04-18 16:28:40 +0100926 check_eol (pfile, true);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000927 }
928 else if (token->type != CPP_EOF)
929 {
John David Anglin0527bc42003-11-01 22:56:54 +0000930 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000931 cpp_token_as_text (pfile, token));
932 return;
933 }
Neil Booth93c803682000-10-28 17:59:06 +0000934
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000935 skip_rest_of_line (pfile);
Joseph Myersc7f9c0b2009-04-18 18:36:28 +0100936 _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, new_file, new_lineno,
Devang Patel2203a882005-02-28 11:04:19 -0800937 map_sysp);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000938}
939
940/* Interpret the # 44 "file" [flags] notation, which has slightly
941 different syntax and semantics from #line: Flags are allowed,
942 and we never complain about the line number being too big. */
943static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000944do_linemarker (cpp_reader *pfile)
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000945{
Per Bothner500bee02004-04-22 19:22:27 -0700946 const struct line_maps *line_table = pfile->line_table;
947 const struct line_map *map = &line_table->maps[line_table->used - 1];
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000948 const cpp_token *token;
Per Bothner12f9df42004-02-11 07:29:30 -0800949 const char *new_file = map->to_file;
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000950 linenum_type new_lineno;
Per Bothner12f9df42004-02-11 07:29:30 -0800951 unsigned int new_sysp = map->sysp;
Joseph Myersc7f9c0b2009-04-18 18:36:28 +0100952 enum lc_reason reason = LC_RENAME_VERBATIM;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000953 int flag;
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000954 bool wrapped;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000955
956 /* Back up so we can get the number again. Putting this in
957 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
958 some circumstances, which can segfault. */
959 _cpp_backup_tokens (pfile, 1);
960
961 /* #line commands expand macros. */
962 token = cpp_get_token (pfile);
963 if (token->type != CPP_NUMBER
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000964 || strtolinenum (token->val.str.text, token->val.str.len,
Manuel López-Ibáñez3b8f20a2008-07-22 09:45:58 +0000965 &new_lineno, &wrapped))
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000966 {
Tom Tromey33ae4832008-01-03 17:58:26 +0000967 /* Unlike #line, there does not seem to be a way to get an EOF
968 here. So, it should be safe to always spell the token. */
John David Anglin0527bc42003-11-01 22:56:54 +0000969 cpp_error (pfile, CPP_DL_ERROR,
970 "\"%s\" after # is not a positive integer",
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000971 cpp_token_as_text (pfile, token));
972 return;
Kazu Hiratadf383482002-05-22 22:02:16 +0000973 }
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000974
975 token = cpp_get_token (pfile);
976 if (token->type == CPP_STRING)
977 {
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000978 cpp_string s = { 0, 0 };
Eric Christopher423e95e2004-02-12 02:25:03 +0000979 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
Jerry Quinnf1bf4102009-07-18 03:22:16 +0000980 1, &s, CPP_STRING))
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000981 new_file = (const char *)s.text;
Eric Christophercf551fb2004-01-16 22:37:49 +0000982
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000983 new_sysp = 0;
984 flag = read_flag (pfile, 0);
985 if (flag == 1)
Neil Booth93c803682000-10-28 17:59:06 +0000986 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000987 reason = LC_ENTER;
988 /* Fake an include for cpp_included (). */
989 _cpp_fake_include (pfile, new_file);
990 flag = read_flag (pfile, flag);
Neil Booth93c803682000-10-28 17:59:06 +0000991 }
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000992 else if (flag == 2)
993 {
994 reason = LC_LEAVE;
995 flag = read_flag (pfile, flag);
996 }
997 if (flag == 3)
998 {
999 new_sysp = 1;
1000 flag = read_flag (pfile, flag);
1001 if (flag == 4)
1002 new_sysp = 2;
1003 }
Jakub Jelinek9d30f272006-12-29 09:15:08 +01001004 pfile->buffer->sysp = new_sysp;
Zack Weinbergdcc229e2002-03-14 18:17:18 +00001005
Joseph Myersa5cb5632009-04-18 16:28:40 +01001006 check_eol (pfile, false);
Zack Weinbergff2b53e2000-04-06 07:56:14 +00001007 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001008 else if (token->type != CPP_EOF)
Neil Booth27e25642000-11-27 08:00:04 +00001009 {
John David Anglin0527bc42003-11-01 22:56:54 +00001010 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001011 cpp_token_as_text (pfile, token));
Neil Booth27e25642000-11-27 08:00:04 +00001012 return;
1013 }
Zack Weinberg941e09b1998-12-15 11:17:06 +00001014
Neil Boothbdcbe492001-09-13 20:05:17 +00001015 skip_rest_of_line (pfile);
Manuel López-Ibáñez00b0c192009-05-13 23:17:55 +00001016
1017 /* Compensate for the increment in linemap_add that occurs in
1018 _cpp_do_file_change. We're currently at the start of the line
1019 *following* the #line directive. A separate source_location for this
1020 location makes no sense (until we do the LC_LEAVE), and
1021 complicates LAST_SOURCE_LINE_LOCATION. */
1022 pfile->line_table->highest_location--;
1023
Neil Boothbb74c962001-08-17 22:23:49 +00001024 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
Neil Booth27e25642000-11-27 08:00:04 +00001025}
1026
Neil Booth67821e32001-08-05 17:31:25 +00001027/* Arrange the file_change callback. pfile->line has changed to
Neil Booth47d89cf2001-08-11 07:33:39 +00001028 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
Joseph Myersa1f300c2001-11-23 02:05:19 +00001029 header, 2 for a system header that needs to be extern "C" protected,
Neil Booth47d89cf2001-08-11 07:33:39 +00001030 and zero otherwise. */
Neil Bootheb1f4d92000-12-18 19:00:26 +00001031void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001032_cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +00001033 const char *to_file, linenum_type file_line,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001034 unsigned int sysp)
Neil Booth27e25642000-11-27 08:00:04 +00001035{
Per Bothner12f9df42004-02-11 07:29:30 -08001036 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
1037 to_file, file_line);
Per Bothner500bee02004-04-22 19:22:27 -07001038 if (map != NULL)
1039 linemap_line_start (pfile->line_table, map->to_line, 127);
Neil Boothd82fc102001-08-02 23:03:31 +00001040
Neil Bootheb1f4d92000-12-18 19:00:26 +00001041 if (pfile->cb.file_change)
Per Bothner12f9df42004-02-11 07:29:30 -08001042 pfile->cb.file_change (pfile, map);
Per Bothner7f2935c1995-03-16 13:59:07 -08001043}
Zack Weinberg941e09b1998-12-15 11:17:06 +00001044
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001045/* Report a warning or error detected by the program we are
1046 processing. Use the directive's tokens in the error message. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001047static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001048do_diagnostic (cpp_reader *pfile, int code, int print_dir)
Per Bothner7f2935c1995-03-16 13:59:07 -08001049{
Tom Tromey5d6342e2008-05-21 21:52:57 +00001050 const unsigned char *dir_name;
1051 unsigned char *line;
1052 source_location src_loc = pfile->cur_token[-1].src_loc;
1053
1054 if (print_dir)
1055 dir_name = pfile->directive->name;
1056 else
1057 dir_name = NULL;
1058 pfile->state.prevent_expansion++;
1059 line = cpp_output_line_to_string (pfile, dir_name);
1060 pfile->state.prevent_expansion--;
1061
1062 cpp_error_with_line (pfile, code, src_loc, 0, "%s", line);
1063 free (line);
Per Bothner7f2935c1995-03-16 13:59:07 -08001064}
1065
Neil Booth838f3132000-09-24 10:42:09 +00001066static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001067do_error (cpp_reader *pfile)
Neil Booth838f3132000-09-24 10:42:09 +00001068{
John David Anglin0527bc42003-11-01 22:56:54 +00001069 do_diagnostic (pfile, CPP_DL_ERROR, 1);
Neil Booth838f3132000-09-24 10:42:09 +00001070}
Per Bothner7f2935c1995-03-16 13:59:07 -08001071
Zack Weinberg711b8822000-07-18 00:59:49 +00001072static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001073do_warning (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001074{
Neil Booth2f878972001-04-08 10:01:18 +00001075 /* We want #warning diagnostics to be emitted in system headers too. */
John David Anglin0527bc42003-11-01 22:56:54 +00001076 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
Per Bothner7f2935c1995-03-16 13:59:07 -08001077}
1078
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001079/* Report program identification. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001080static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001081do_ident (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001082{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001083 const cpp_token *str = cpp_get_token (pfile);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001084
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001085 if (str->type != CPP_STRING)
Zack Weinberg1ed17cd2005-05-12 18:31:38 +00001086 cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
1087 pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +00001088 else if (pfile->cb.ident)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001089 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001090
Joseph Myersa5cb5632009-04-18 16:28:40 +01001091 check_eol (pfile, false);
Per Bothner7f2935c1995-03-16 13:59:07 -08001092}
1093
Neil Bootha5da89c2001-10-14 17:44:00 +00001094/* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1095 matching entry, or NULL if none is found. The returned entry could
1096 be the start of a namespace chain, or a pragma. */
1097static struct pragma_entry *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001098lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
Nathan Sidwell82443372000-06-23 10:56:09 +00001099{
Neil Booth4b115ff2001-10-14 23:04:13 +00001100 while (chain && chain->pragma != pragma)
1101 chain = chain->next;
Neil Bootha5da89c2001-10-14 17:44:00 +00001102
1103 return chain;
1104}
1105
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001106/* Create and insert a blank pragma entry at the beginning of a
1107 singly-linked CHAIN. */
Neil Bootha5da89c2001-10-14 17:44:00 +00001108static struct pragma_entry *
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001109new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain)
Neil Bootha5da89c2001-10-14 17:44:00 +00001110{
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001111 struct pragma_entry *new_entry;
Neil Bootha5da89c2001-10-14 17:44:00 +00001112
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001113 new_entry = (struct pragma_entry *)
Neil Bootha5da89c2001-10-14 17:44:00 +00001114 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
Neil Bootha5da89c2001-10-14 17:44:00 +00001115
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001116 memset (new_entry, 0, sizeof (struct pragma_entry));
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001117 new_entry->next = *chain;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001118
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001119 *chain = new_entry;
1120 return new_entry;
Neil Bootha5da89c2001-10-14 17:44:00 +00001121}
1122
1123/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001124 goes in the global namespace. */
1125static struct pragma_entry *
1126register_pragma_1 (cpp_reader *pfile, const char *space, const char *name,
1127 bool allow_name_expansion)
Nathan Sidwell82443372000-06-23 10:56:09 +00001128{
Neil Bootha5da89c2001-10-14 17:44:00 +00001129 struct pragma_entry **chain = &pfile->pragmas;
1130 struct pragma_entry *entry;
Neil Booth4b115ff2001-10-14 23:04:13 +00001131 const cpp_hashnode *node;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001132
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001133 if (space)
1134 {
Kris Van Heesb6baa672008-04-18 13:58:08 +00001135 node = cpp_lookup (pfile, UC space, strlen (space));
Neil Booth4b115ff2001-10-14 23:04:13 +00001136 entry = lookup_pragma_entry (*chain, node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001137 if (!entry)
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001138 {
1139 entry = new_pragma_entry (pfile, chain);
1140 entry->pragma = node;
1141 entry->is_nspace = true;
1142 entry->allow_expansion = allow_name_expansion;
1143 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001144 else if (!entry->is_nspace)
1145 goto clash;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001146 else if (entry->allow_expansion != allow_name_expansion)
1147 {
1148 cpp_error (pfile, CPP_DL_ICE,
1149 "registering pragmas in namespace \"%s\" with mismatched "
1150 "name expansion", space);
1151 return NULL;
1152 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001153 chain = &entry->u.space;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001154 }
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001155 else if (allow_name_expansion)
1156 {
1157 cpp_error (pfile, CPP_DL_ICE,
1158 "registering pragma \"%s\" with name expansion "
1159 "and no namespace", name);
1160 return NULL;
1161 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001162
Neil Bootha5da89c2001-10-14 17:44:00 +00001163 /* Check for duplicates. */
Kris Van Heesb6baa672008-04-18 13:58:08 +00001164 node = cpp_lookup (pfile, UC name, strlen (name));
Neil Booth4b115ff2001-10-14 23:04:13 +00001165 entry = lookup_pragma_entry (*chain, node);
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001166 if (entry == NULL)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001167 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001168 entry = new_pragma_entry (pfile, chain);
1169 entry->pragma = node;
1170 return entry;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001171 }
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001172
1173 if (entry->is_nspace)
1174 clash:
1175 cpp_error (pfile, CPP_DL_ICE,
1176 "registering \"%s\" as both a pragma and a pragma namespace",
1177 NODE_NAME (node));
1178 else if (space)
1179 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1180 space, name);
Neil Bootha5da89c2001-10-14 17:44:00 +00001181 else
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001182 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1183
1184 return NULL;
1185}
1186
1187/* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1188static void
1189register_pragma_internal (cpp_reader *pfile, const char *space,
1190 const char *name, pragma_cb handler)
1191{
1192 struct pragma_entry *entry;
1193
1194 entry = register_pragma_1 (pfile, space, name, false);
1195 entry->is_internal = true;
1196 entry->u.handler = handler;
Zack Weinberg21b11492004-09-09 19:16:56 +00001197}
1198
1199/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1200 goes in the global namespace. HANDLER is the handler it will call,
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001201 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1202 expansion while parsing pragma NAME. This function is exported
1203 from libcpp. */
Zack Weinberg21b11492004-09-09 19:16:56 +00001204void
1205cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001206 pragma_cb handler, bool allow_expansion)
Zack Weinberg21b11492004-09-09 19:16:56 +00001207{
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001208 struct pragma_entry *entry;
1209
1210 if (!handler)
1211 {
1212 cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler");
1213 return;
1214 }
1215
1216 entry = register_pragma_1 (pfile, space, name, false);
1217 if (entry)
1218 {
1219 entry->allow_expansion = allow_expansion;
1220 entry->u.handler = handler;
1221 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001222}
Neil Bootha5da89c2001-10-14 17:44:00 +00001223
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001224/* Similarly, but create mark the pragma for deferred processing.
1225 When found, a CPP_PRAGMA token will be insertted into the stream
1226 with IDENT in the token->u.pragma slot. */
1227void
1228cpp_register_deferred_pragma (cpp_reader *pfile, const char *space,
1229 const char *name, unsigned int ident,
1230 bool allow_expansion, bool allow_name_expansion)
1231{
1232 struct pragma_entry *entry;
1233
1234 entry = register_pragma_1 (pfile, space, name, allow_name_expansion);
1235 if (entry)
1236 {
1237 entry->is_deferred = true;
1238 entry->allow_expansion = allow_expansion;
1239 entry->u.ident = ident;
1240 }
1241}
1242
Neil Bootha5da89c2001-10-14 17:44:00 +00001243/* Register the pragmas the preprocessor itself handles. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001244void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001245_cpp_init_internal_pragmas (cpp_reader *pfile)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001246{
Neil Bootha5da89c2001-10-14 17:44:00 +00001247 /* Pragmas in the global namespace. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001248 register_pragma_internal (pfile, 0, "once", do_pragma_once);
Kai Tietz17e7cb82009-11-11 18:37:19 +00001249 register_pragma_internal (pfile, 0, "push_macro", do_pragma_push_macro);
1250 register_pragma_internal (pfile, 0, "pop_macro", do_pragma_pop_macro);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001251
Neil Bootha5da89c2001-10-14 17:44:00 +00001252 /* New GCC-specific pragmas should be put in the GCC namespace. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001253 register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison);
1254 register_pragma_internal (pfile, "GCC", "system_header",
1255 do_pragma_system_header);
1256 register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
Nathan Sidwell82443372000-06-23 10:56:09 +00001257}
Per Bothner7f2935c1995-03-16 13:59:07 -08001258
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001259/* Return the number of registered pragmas in PE. */
1260
1261static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001262count_registered_pragmas (struct pragma_entry *pe)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001263{
1264 int ct = 0;
1265 for (; pe != NULL; pe = pe->next)
1266 {
1267 if (pe->is_nspace)
1268 ct += count_registered_pragmas (pe->u.space);
1269 ct++;
1270 }
1271 return ct;
1272}
1273
1274/* Save into SD the names of the registered pragmas referenced by PE,
1275 and return a pointer to the next free space in SD. */
1276
1277static char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001278save_registered_pragmas (struct pragma_entry *pe, char **sd)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001279{
1280 for (; pe != NULL; pe = pe->next)
1281 {
1282 if (pe->is_nspace)
1283 sd = save_registered_pragmas (pe->u.space, sd);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001284 *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
1285 HT_LEN (&pe->pragma->ident),
1286 HT_LEN (&pe->pragma->ident) + 1);
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001287 }
1288 return sd;
1289}
1290
1291/* Return a newly-allocated array which saves the names of the
1292 registered pragmas. */
1293
1294char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001295_cpp_save_pragma_names (cpp_reader *pfile)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001296{
1297 int ct = count_registered_pragmas (pfile->pragmas);
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001298 char **result = XNEWVEC (char *, ct);
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001299 (void) save_registered_pragmas (pfile->pragmas, result);
1300 return result;
1301}
1302
1303/* Restore from SD the names of the registered pragmas referenced by PE,
1304 and return a pointer to the next unused name in SD. */
1305
1306static char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001307restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1308 char **sd)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001309{
1310 for (; pe != NULL; pe = pe->next)
1311 {
1312 if (pe->is_nspace)
1313 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
Kris Van Heesb6baa672008-04-18 13:58:08 +00001314 pe->pragma = cpp_lookup (pfile, UC *sd, strlen (*sd));
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001315 free (*sd);
1316 sd++;
1317 }
1318 return sd;
1319}
1320
1321/* Restore the names of the registered pragmas from SAVED. */
1322
1323void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001324_cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001325{
1326 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1327 free (saved);
1328}
1329
Neil Bootha5da89c2001-10-14 17:44:00 +00001330/* Pragmata handling. We handle some, and pass the rest on to the
1331 front end. C99 defines three pragmas and says that no macro
1332 expansion is to be performed on them; whether or not macro
1333 expansion happens for other pragmas is implementation defined.
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001334 This implementation allows for a mix of both, since GCC did not
1335 traditionally macro expand its (few) pragmas, whereas OpenMP
1336 specifies that macro expansion should happen. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001337static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001338do_pragma (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001339{
Neil Bootha5da89c2001-10-14 17:44:00 +00001340 const struct pragma_entry *p = NULL;
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001341 const cpp_token *token, *pragma_token = pfile->cur_token;
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001342 cpp_token ns_token;
Neil Bootha5da89c2001-10-14 17:44:00 +00001343 unsigned int count = 1;
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001344
Neil Booth93c803682000-10-28 17:59:06 +00001345 pfile->state.prevent_expansion++;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001346
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001347 token = cpp_get_token (pfile);
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001348 ns_token = *token;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001349 if (token->type == CPP_NAME)
Alexandre Oliva0172e2b2000-02-27 06:24:27 +00001350 {
Joseph Myers9a0c6182009-05-10 15:27:32 +01001351 p = lookup_pragma_entry (pfile->pragmas, token->val.node.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001352 if (p && p->is_nspace)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001353 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001354 bool allow_name_expansion = p->allow_expansion;
1355 if (allow_name_expansion)
1356 pfile->state.prevent_expansion--;
Neil Bootha5da89c2001-10-14 17:44:00 +00001357 token = cpp_get_token (pfile);
1358 if (token->type == CPP_NAME)
Joseph Myers9a0c6182009-05-10 15:27:32 +01001359 p = lookup_pragma_entry (p->u.space, token->val.node.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001360 else
1361 p = NULL;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001362 if (allow_name_expansion)
1363 pfile->state.prevent_expansion++;
1364 count = 2;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001365 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001366 }
1367
Zack Weinberg3da3d582004-10-27 17:29:29 +00001368 if (p)
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001369 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001370 if (p->is_deferred)
Zack Weinberg3da3d582004-10-27 17:29:29 +00001371 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001372 pfile->directive_result.src_loc = pragma_token->src_loc;
1373 pfile->directive_result.type = CPP_PRAGMA;
1374 pfile->directive_result.flags = pragma_token->flags;
1375 pfile->directive_result.val.pragma = p->u.ident;
1376 pfile->state.in_deferred_pragma = true;
1377 pfile->state.pragma_allow_expansion = p->allow_expansion;
1378 if (!p->allow_expansion)
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001379 pfile->state.prevent_expansion++;
Zack Weinberg3da3d582004-10-27 17:29:29 +00001380 }
1381 else
1382 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001383 /* Since the handler below doesn't get the line number, that
1384 it might need for diagnostics, make sure it has the right
1385 numbers in place. */
1386 if (pfile->cb.line_change)
1387 (*pfile->cb.line_change) (pfile, pragma_token, false);
1388 if (p->allow_expansion)
1389 pfile->state.prevent_expansion--;
1390 (*p->u.handler) (pfile);
1391 if (p->allow_expansion)
1392 pfile->state.prevent_expansion++;
Zack Weinberg3da3d582004-10-27 17:29:29 +00001393 }
Zack Weinberg21b11492004-09-09 19:16:56 +00001394 }
Neil Boothd82fc102001-08-02 23:03:31 +00001395 else if (pfile->cb.def_pragma)
Neil Boothbdcbe492001-09-13 20:05:17 +00001396 {
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001397 if (count == 1 || pfile->context->prev == NULL)
1398 _cpp_backup_tokens (pfile, count);
1399 else
1400 {
1401 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1402 won't allow backing 2 tokens. */
1403 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1404 reads both tokens, we could perhaps free it, but if it doesn't,
1405 we don't know the exact lifespan. */
1406 cpp_token *toks = XNEWVEC (cpp_token, 2);
1407 toks[0] = ns_token;
1408 toks[0].flags |= NO_EXPAND;
1409 toks[1] = *token;
1410 toks[1].flags |= NO_EXPAND;
1411 _cpp_push_token_context (pfile, NULL, toks, 2);
1412 }
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001413 pfile->cb.def_pragma (pfile, pfile->directive_line);
Neil Boothbdcbe492001-09-13 20:05:17 +00001414 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001415
Neil Booth97293892001-09-14 22:04:46 +00001416 pfile->state.prevent_expansion--;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001417}
1418
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001419/* Handle #pragma once. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001420static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001421do_pragma_once (cpp_reader *pfile)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001422{
Tom Tromey705e2d22007-01-04 15:32:26 +00001423 if (cpp_in_primary_file (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +00001424 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
Neil Booth93c803682000-10-28 17:59:06 +00001425
Joseph Myersa5cb5632009-04-18 16:28:40 +01001426 check_eol (pfile, false);
Neil Booth49634b32003-08-02 16:29:46 +00001427 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001428}
1429
Kai Tietz17e7cb82009-11-11 18:37:19 +00001430/* Handle #pragma push_macro(STRING). */
1431static void
1432do_pragma_push_macro (cpp_reader *pfile)
1433{
1434 char *macroname, *dest;
1435 const char *limit, *src;
1436 const cpp_token *txt;
1437 struct def_pragma_macro *c;
1438
1439 txt = get__Pragma_string (pfile);
1440 if (!txt)
1441 {
1442 source_location src_loc = pfile->cur_token[-1].src_loc;
1443 cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
1444 "invalid #pragma push_macro directive");
1445 check_eol (pfile, false);
1446 skip_rest_of_line (pfile);
1447 return;
1448 }
1449 dest = macroname = (char *) alloca (txt->val.str.len + 2);
1450 src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L'));
1451 limit = (const char *) (txt->val.str.text + txt->val.str.len - 1);
1452 while (src < limit)
1453 {
1454 /* We know there is a character following the backslash. */
1455 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1456 src++;
1457 *dest++ = *src++;
1458 }
1459 *dest = 0;
1460 check_eol (pfile, false);
1461 skip_rest_of_line (pfile);
1462 c = XNEW (struct def_pragma_macro);
1463 c->name = XNEWVAR (char, strlen (macroname) + 1);
1464 strcpy (c->name, macroname);
1465 c->next = pfile->pushed_macros;
1466 c->value = cpp_push_definition (pfile, c->name);
1467 pfile->pushed_macros = c;
1468}
1469
1470/* Handle #pragma pop_macro(STRING). */
1471static void
1472do_pragma_pop_macro (cpp_reader *pfile)
1473{
1474 char *macroname, *dest;
1475 const char *limit, *src;
1476 const cpp_token *txt;
1477 struct def_pragma_macro *l = NULL, *c = pfile->pushed_macros;
1478 txt = get__Pragma_string (pfile);
1479 if (!txt)
1480 {
1481 source_location src_loc = pfile->cur_token[-1].src_loc;
1482 cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
1483 "invalid #pragma pop_macro directive");
1484 check_eol (pfile, false);
1485 skip_rest_of_line (pfile);
1486 return;
1487 }
1488 dest = macroname = (char *) alloca (txt->val.str.len + 2);
1489 src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L'));
1490 limit = (const char *) (txt->val.str.text + txt->val.str.len - 1);
1491 while (src < limit)
1492 {
1493 /* We know there is a character following the backslash. */
1494 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1495 src++;
1496 *dest++ = *src++;
1497 }
1498 *dest = 0;
1499 check_eol (pfile, false);
1500 skip_rest_of_line (pfile);
1501
1502 while (c != NULL)
1503 {
1504 if (!strcmp (c->name, macroname))
1505 {
1506 if (!l)
1507 pfile->pushed_macros = c->next;
1508 else
1509 l->next = c->next;
1510 cpp_pop_definition (pfile, c->name, c->value);
1511 free (c->name);
1512 free (c);
1513 break;
1514 }
1515 l = c;
1516 c = c->next;
1517 }
1518}
1519
Neil Boothc3bf3e62002-05-09 17:14:22 +00001520/* Handle #pragma GCC poison, to poison one or more identifiers so
1521 that the lexer produces a hard error for each subsequent usage. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001522static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001523do_pragma_poison (cpp_reader *pfile)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001524{
Neil Booth345894b2001-09-16 13:44:29 +00001525 const cpp_token *tok;
Zack Weinbergf8f769e2000-05-28 05:56:38 +00001526 cpp_hashnode *hp;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001527
Neil Booth93c803682000-10-28 17:59:06 +00001528 pfile->state.poisoned_ok = 1;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001529 for (;;)
1530 {
Neil Booth345894b2001-09-16 13:44:29 +00001531 tok = _cpp_lex_token (pfile);
1532 if (tok->type == CPP_EOF)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001533 break;
Neil Booth345894b2001-09-16 13:44:29 +00001534 if (tok->type != CPP_NAME)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001535 {
John David Anglin0527bc42003-11-01 22:56:54 +00001536 cpp_error (pfile, CPP_DL_ERROR,
1537 "invalid #pragma GCC poison directive");
Neil Booth93c803682000-10-28 17:59:06 +00001538 break;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001539 }
1540
Joseph Myers9a0c6182009-05-10 15:27:32 +01001541 hp = tok->val.node.node;
Neil Booth93c803682000-10-28 17:59:06 +00001542 if (hp->flags & NODE_POISONED)
1543 continue;
1544
1545 if (hp->type == NT_MACRO)
John David Anglin0527bc42003-11-01 22:56:54 +00001546 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
Neil Boothebef4e82002-04-14 18:42:47 +00001547 NODE_NAME (hp));
Neil Booth93c803682000-10-28 17:59:06 +00001548 _cpp_free_definition (hp);
1549 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001550 }
Neil Booth93c803682000-10-28 17:59:06 +00001551 pfile->state.poisoned_ok = 0;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001552}
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001553
1554/* Mark the current header as a system header. This will suppress
1555 some categories of warnings (notably those from -pedantic). It is
1556 intended for use in system libraries that cannot be implemented in
1557 conforming C, but cannot be certain that their headers appear in a
1558 system include directory. To prevent abuse, it is rejected in the
1559 primary source file. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001560static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001561do_pragma_system_header (cpp_reader *pfile)
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001562{
Tom Tromey705e2d22007-01-04 15:32:26 +00001563 if (cpp_in_primary_file (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +00001564 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +00001565 "#pragma system_header ignored outside include file");
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001566 else
Neil Boothd82fc102001-08-02 23:03:31 +00001567 {
Joseph Myersa5cb5632009-04-18 16:28:40 +01001568 check_eol (pfile, false);
Neil Boothbdcbe492001-09-13 20:05:17 +00001569 skip_rest_of_line (pfile);
Neil Boothd82fc102001-08-02 23:03:31 +00001570 cpp_make_system_header (pfile, 1, 0);
1571 }
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001572}
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001573
1574/* Check the modified date of the current include file against a specified
1575 file. Issue a diagnostic, if the specified file is newer. We use this to
1576 determine if a fixed header should be refixed. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001577static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001578do_pragma_dependency (cpp_reader *pfile)
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001579{
Neil Booth74eb4b32003-04-21 19:21:59 +00001580 const char *fname;
1581 int angle_brackets, ordering;
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +00001582 source_location location;
Kazu Hiratadf383482002-05-22 22:02:16 +00001583
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +00001584 fname = parse_include (pfile, &angle_brackets, NULL, &location);
Neil Booth74eb4b32003-04-21 19:21:59 +00001585 if (!fname)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001586 return;
Zack Weinberg041c3192000-07-04 01:58:21 +00001587
Neil Booth74eb4b32003-04-21 19:21:59 +00001588 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001589 if (ordering < 0)
John David Anglin0527bc42003-11-01 22:56:54 +00001590 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001591 else if (ordering > 0)
1592 {
John David Anglin0527bc42003-11-01 22:56:54 +00001593 cpp_error (pfile, CPP_DL_WARNING,
1594 "current file is older than %s", fname);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001595 if (cpp_get_token (pfile)->type != CPP_EOF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001596 {
1597 _cpp_backup_tokens (pfile, 1);
John David Anglin0527bc42003-11-01 22:56:54 +00001598 do_diagnostic (pfile, CPP_DL_WARNING, 0);
Neil Boothbdcbe492001-09-13 20:05:17 +00001599 }
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001600 }
Neil Booth74eb4b32003-04-21 19:21:59 +00001601
Kaveh R. Ghazifad205f2003-06-16 21:41:10 +00001602 free ((void *) fname);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001603}
1604
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001605/* Get a token but skip padding. */
1606static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001607get_token_no_padding (cpp_reader *pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001608{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001609 for (;;)
1610 {
1611 const cpp_token *result = cpp_get_token (pfile);
1612 if (result->type != CPP_PADDING)
1613 return result;
1614 }
1615}
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001616
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001617/* Check syntax is "(string-literal)". Returns the string on success,
1618 or NULL on failure. */
1619static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001620get__Pragma_string (cpp_reader *pfile)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001621{
1622 const cpp_token *string;
Tom Tromey5b9a40d2007-10-31 14:50:13 +00001623 const cpp_token *paren;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001624
Tom Tromey5b9a40d2007-10-31 14:50:13 +00001625 paren = get_token_no_padding (pfile);
1626 if (paren->type == CPP_EOF)
1627 _cpp_backup_tokens (pfile, 1);
1628 if (paren->type != CPP_OPEN_PAREN)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001629 return NULL;
1630
1631 string = get_token_no_padding (pfile);
Tom Tromey5b9a40d2007-10-31 14:50:13 +00001632 if (string->type == CPP_EOF)
1633 _cpp_backup_tokens (pfile, 1);
Kris Van Heesb6baa672008-04-18 13:58:08 +00001634 if (string->type != CPP_STRING && string->type != CPP_WSTRING
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001635 && string->type != CPP_STRING32 && string->type != CPP_STRING16
1636 && string->type != CPP_UTF8STRING)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001637 return NULL;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001638
Tom Tromey5b9a40d2007-10-31 14:50:13 +00001639 paren = get_token_no_padding (pfile);
1640 if (paren->type == CPP_EOF)
1641 _cpp_backup_tokens (pfile, 1);
1642 if (paren->type != CPP_CLOSE_PAREN)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001643 return NULL;
1644
1645 return string;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001646}
1647
Neil Booth87062812001-10-20 09:00:53 +00001648/* Destringize IN into a temporary buffer, by removing the first \ of
1649 \" and \\ sequences, and process the result as a #pragma directive. */
1650static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001651destringize_and_run (cpp_reader *pfile, const cpp_string *in)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001652{
1653 const unsigned char *src, *limit;
Neil Booth87062812001-10-20 09:00:53 +00001654 char *dest, *result;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001655 cpp_context *saved_context;
1656 cpp_token *saved_cur_token;
1657 tokenrun *saved_cur_run;
1658 cpp_token *toks;
1659 int count;
Tom Tromey14ccf802008-03-13 21:10:07 +00001660 const struct directive *save_directive;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001661
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001662 dest = result = (char *) alloca (in->len - 1);
Neil Booth6338b352003-04-23 22:44:06 +00001663 src = in->text + 1 + (in->text[0] == 'L');
1664 limit = in->text + in->len - 1;
1665 while (src < limit)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001666 {
1667 /* We know there is a character following the backslash. */
1668 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1669 src++;
1670 *dest++ = *src++;
1671 }
Neil Booth26aea072003-04-19 00:22:51 +00001672 *dest = '\n';
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001673
Neil Booth8128ccc2002-11-18 20:43:40 +00001674 /* Ugh; an awful kludge. We are really not set up to be lexing
1675 tokens when in the middle of a macro expansion. Use a new
1676 context to force cpp_get_token to lex, and so skip_rest_of_line
1677 doesn't go beyond the end of the text. Also, remember the
1678 current lexing position so we can return to it later.
Neil Booth79ba5e32002-09-03 21:55:40 +00001679
Neil Booth8128ccc2002-11-18 20:43:40 +00001680 Something like line-at-a-time lexing should remove the need for
1681 this. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001682 saved_context = pfile->context;
1683 saved_cur_token = pfile->cur_token;
1684 saved_cur_run = pfile->cur_run;
Neil Booth8128ccc2002-11-18 20:43:40 +00001685
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001686 pfile->context = XNEW (cpp_context);
1687 pfile->context->macro = 0;
1688 pfile->context->prev = 0;
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001689 pfile->context->next = 0;
Neil Booth79ba5e32002-09-03 21:55:40 +00001690
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001691 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1692 until we've read all of the tokens that we want. */
1693 cpp_push_buffer (pfile, (const uchar *) result, dest - result,
1694 /* from_stage3 */ true);
1695 /* ??? Antique Disgusting Hack. What does this do? */
1696 if (pfile->buffer->prev)
1697 pfile->buffer->file = pfile->buffer->prev->file;
Neil Booth79ba5e32002-09-03 21:55:40 +00001698
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001699 start_directive (pfile);
1700 _cpp_clean_line (pfile);
Tom Tromey14ccf802008-03-13 21:10:07 +00001701 save_directive = pfile->directive;
1702 pfile->directive = &dtable[T_PRAGMA];
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001703 do_pragma (pfile);
1704 end_directive (pfile, 1);
Tom Tromey14ccf802008-03-13 21:10:07 +00001705 pfile->directive = save_directive;
Neil Booth79ba5e32002-09-03 21:55:40 +00001706
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001707 /* We always insert at least one token, the directive result. It'll
1708 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1709 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
Neil Booth79ba5e32002-09-03 21:55:40 +00001710
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001711 /* If we're not handling the pragma internally, read all of the tokens from
1712 the string buffer now, while the string buffer is still installed. */
1713 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1714 to me what the true lifespan of the tokens are. It would appear that
1715 the lifespan is the entire parse of the main input stream, in which case
1716 this may not be wrong. */
1717 if (pfile->directive_result.type == CPP_PRAGMA)
1718 {
1719 int maxcount;
Neil Booth79ba5e32002-09-03 21:55:40 +00001720
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001721 count = 1;
1722 maxcount = 50;
1723 toks = XNEWVEC (cpp_token, maxcount);
1724 toks[0] = pfile->directive_result;
1725
1726 do
1727 {
1728 if (count == maxcount)
1729 {
1730 maxcount = maxcount * 3 / 2;
1731 toks = XRESIZEVEC (cpp_token, toks, maxcount);
1732 }
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02001733 toks[count] = *cpp_get_token (pfile);
1734 /* Macros have been already expanded by cpp_get_token
1735 if the pragma allowed expansion. */
1736 toks[count++].flags |= NO_EXPAND;
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001737 }
1738 while (toks[count-1].type != CPP_PRAGMA_EOL);
1739 }
1740 else
1741 {
1742 count = 1;
1743 toks = XNEW (cpp_token);
1744 toks[0] = pfile->directive_result;
1745
1746 /* If we handled the entire pragma internally, make sure we get the
1747 line number correct for the next token. */
1748 if (pfile->cb.line_change)
1749 pfile->cb.line_change (pfile, pfile->cur_token, false);
1750 }
1751
1752 /* Finish inlining run_directive. */
1753 pfile->buffer->file = NULL;
1754 _cpp_pop_buffer (pfile);
1755
1756 /* Reset the old macro state before ... */
1757 XDELETE (pfile->context);
1758 pfile->context = saved_context;
1759 pfile->cur_token = saved_cur_token;
1760 pfile->cur_run = saved_cur_run;
1761
1762 /* ... inserting the new tokens we collected. */
1763 _cpp_push_token_context (pfile, NULL, toks, count);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001764}
1765
Tom Tromey5b9a40d2007-10-31 14:50:13 +00001766/* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
1767int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001768_cpp_do__Pragma (cpp_reader *pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001769{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001770 const cpp_token *string = get__Pragma_string (pfile);
Zack Weinberg21b11492004-09-09 19:16:56 +00001771 pfile->directive_result.type = CPP_PADDING;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001772
Neil Booth79ba5e32002-09-03 21:55:40 +00001773 if (string)
Tom Tromey5b9a40d2007-10-31 14:50:13 +00001774 {
1775 destringize_and_run (pfile, &string->val.str);
1776 return 1;
1777 }
1778 cpp_error (pfile, CPP_DL_ERROR,
1779 "_Pragma takes a parenthesized string literal");
1780 return 0;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001781}
1782
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001783/* Handle #ifdef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001784static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001785do_ifdef (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +00001786{
Neil Booth93c803682000-10-28 17:59:06 +00001787 int skip = 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001788
Neil Boothcef0d192001-07-26 06:02:47 +00001789 if (! pfile->state.skipping)
Neil Booth93c803682000-10-28 17:59:06 +00001790 {
Joseph Myers93d45d92008-04-02 20:42:53 +01001791 cpp_hashnode *node = lex_macro_node (pfile, false);
Zack Weinberg041c3192000-07-04 01:58:21 +00001792
Neil Booth93c803682000-10-28 17:59:06 +00001793 if (node)
Neil Bootha69cbaa2002-07-23 22:57:49 +00001794 {
1795 skip = node->type != NT_MACRO;
1796 _cpp_mark_macro_used (node);
Joseph Myers93d45d92008-04-02 20:42:53 +01001797 if (!(node->flags & NODE_USED))
1798 {
1799 node->flags |= NODE_USED;
1800 if (node->type == NT_MACRO)
1801 {
1802 if (pfile->cb.used_define)
1803 pfile->cb.used_define (pfile, pfile->directive_line, node);
1804 }
1805 else
1806 {
1807 if (pfile->cb.used_undef)
1808 pfile->cb.used_undef (pfile, pfile->directive_line, node);
1809 }
1810 }
Arnaud Charlet3de8a542009-11-20 08:18:16 +00001811 if (pfile->cb.used)
1812 pfile->cb.used (pfile, pfile->directive_line, node);
Joseph Myersa5cb5632009-04-18 16:28:40 +01001813 check_eol (pfile, false);
Neil Bootha69cbaa2002-07-23 22:57:49 +00001814 }
Neil Booth93c803682000-10-28 17:59:06 +00001815 }
1816
1817 push_conditional (pfile, skip, T_IFDEF, 0);
Zack Weinberg168d3732000-03-14 06:34:11 +00001818}
1819
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001820/* Handle #ifndef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001821static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001822do_ifndef (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +00001823{
Neil Booth93c803682000-10-28 17:59:06 +00001824 int skip = 1;
Joseph Myers93d45d92008-04-02 20:42:53 +01001825 cpp_hashnode *node = 0;
Zack Weinberg168d3732000-03-14 06:34:11 +00001826
Neil Boothcef0d192001-07-26 06:02:47 +00001827 if (! pfile->state.skipping)
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001828 {
Tom Tromeyee1c2a12007-01-12 19:46:49 +00001829 node = lex_macro_node (pfile, false);
Geoffrey Keatingb43db0b2000-12-02 22:28:44 +00001830
1831 if (node)
Neil Bootha69cbaa2002-07-23 22:57:49 +00001832 {
1833 skip = node->type == NT_MACRO;
1834 _cpp_mark_macro_used (node);
Joseph Myers93d45d92008-04-02 20:42:53 +01001835 if (!(node->flags & NODE_USED))
1836 {
1837 node->flags |= NODE_USED;
1838 if (node->type == NT_MACRO)
1839 {
1840 if (pfile->cb.used_define)
1841 pfile->cb.used_define (pfile, pfile->directive_line, node);
1842 }
1843 else
1844 {
1845 if (pfile->cb.used_undef)
1846 pfile->cb.used_undef (pfile, pfile->directive_line, node);
1847 }
1848 }
Arnaud Charlet3de8a542009-11-20 08:18:16 +00001849 if (pfile->cb.used)
1850 pfile->cb.used (pfile, pfile->directive_line, node);
Joseph Myersa5cb5632009-04-18 16:28:40 +01001851 check_eol (pfile, false);
Neil Bootha69cbaa2002-07-23 22:57:49 +00001852 }
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001853 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001854
Neil Booth93c803682000-10-28 17:59:06 +00001855 push_conditional (pfile, skip, T_IFNDEF, node);
Per Bothner7f2935c1995-03-16 13:59:07 -08001856}
1857
Neil Booth6d18adb2001-07-29 17:27:57 +00001858/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1859 pfile->mi_ind_cmacro so we can handle multiple-include
Kazu Hirata6356f892003-06-12 19:01:08 +00001860 optimizations. If macro expansion occurs in the expression, we
Neil Booth6d18adb2001-07-29 17:27:57 +00001861 cannot treat it as a controlling conditional, since the expansion
1862 could change in the future. That is handled by cpp_get_token. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001863static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001864do_if (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001865{
Neil Booth93c803682000-10-28 17:59:06 +00001866 int skip = 1;
Per Bothner7f2935c1995-03-16 13:59:07 -08001867
Neil Boothcef0d192001-07-26 06:02:47 +00001868 if (! pfile->state.skipping)
Tom Tromeyd7508872008-05-30 14:25:09 +00001869 skip = _cpp_parse_expr (pfile, true) == false;
Neil Booth93c803682000-10-28 17:59:06 +00001870
Neil Booth6d18adb2001-07-29 17:27:57 +00001871 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
Per Bothner7f2935c1995-03-16 13:59:07 -08001872}
1873
Neil Boothb528a072000-11-12 11:46:21 +00001874/* Flip skipping state if appropriate and continue without changing
Zack Weinbergea4a4532000-05-29 16:19:32 +00001875 if_stack; this is so that the error message for missing #endif's
1876 etc. will point to the original #if. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001877static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001878do_else (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001879{
Neil Boothb528a072000-11-12 11:46:21 +00001880 cpp_buffer *buffer = pfile->buffer;
1881 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001882
1883 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001884 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
Neil Booth93c803682000-10-28 17:59:06 +00001885 else
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001886 {
Neil Booth93c803682000-10-28 17:59:06 +00001887 if (ifs->type == T_ELSE)
1888 {
John David Anglin0527bc42003-11-01 22:56:54 +00001889 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1890 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Booth93c803682000-10-28 17:59:06 +00001891 "the conditional began here");
1892 }
Neil Boothb528a072000-11-12 11:46:21 +00001893 ifs->type = T_ELSE;
1894
Neil Boothcef0d192001-07-26 06:02:47 +00001895 /* Skip any future (erroneous) #elses or #elifs. */
1896 pfile->state.skipping = ifs->skip_elses;
1897 ifs->skip_elses = true;
Neil Booth93c803682000-10-28 17:59:06 +00001898
1899 /* Invalidate any controlling macro. */
1900 ifs->mi_cmacro = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001901
Neil Boothcef0d192001-07-26 06:02:47 +00001902 /* Only check EOL if was not originally skipping. */
Phil Edwards909de5d2002-03-22 21:59:04 +00001903 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
Joseph Myersa5cb5632009-04-18 16:28:40 +01001904 check_eol (pfile, false);
Neil Boothcef0d192001-07-26 06:02:47 +00001905 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001906}
1907
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001908/* Handle a #elif directive by not changing if_stack either. See the
Neil Booth93c803682000-10-28 17:59:06 +00001909 comment above do_else. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001910static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001911do_elif (cpp_reader *pfile)
Zack Weinbergea4a4532000-05-29 16:19:32 +00001912{
Neil Boothb528a072000-11-12 11:46:21 +00001913 cpp_buffer *buffer = pfile->buffer;
1914 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001915
1916 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001917 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
Neil Boothb528a072000-11-12 11:46:21 +00001918 else
Zack Weinbergea4a4532000-05-29 16:19:32 +00001919 {
Neil Boothb528a072000-11-12 11:46:21 +00001920 if (ifs->type == T_ELSE)
1921 {
John David Anglin0527bc42003-11-01 22:56:54 +00001922 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1923 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Boothb528a072000-11-12 11:46:21 +00001924 "the conditional began here");
1925 }
1926 ifs->type = T_ELIF;
1927
Tom Tromeyd7508872008-05-30 14:25:09 +00001928 if (! ifs->was_skipping)
Neil Boothb528a072000-11-12 11:46:21 +00001929 {
Tom Tromeyd7508872008-05-30 14:25:09 +00001930 bool value;
1931 /* The standard mandates that the expression be parsed even
1932 if we are skipping elses at this point -- the lexical
1933 restrictions on #elif only apply to skipped groups, but
1934 this group is not being skipped. Temporarily set
1935 skipping to false to get lexer warnings. */
Neil Boothcef0d192001-07-26 06:02:47 +00001936 pfile->state.skipping = 0;
Tom Tromeyd7508872008-05-30 14:25:09 +00001937 value = _cpp_parse_expr (pfile, false);
1938 if (ifs->skip_elses)
1939 pfile->state.skipping = 1;
1940 else
1941 {
1942 pfile->state.skipping = ! value;
1943 ifs->skip_elses = value;
1944 }
Neil Boothb528a072000-11-12 11:46:21 +00001945 }
Neil Boothcef0d192001-07-26 06:02:47 +00001946
1947 /* Invalidate any controlling macro. */
1948 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001949 }
Zack Weinbergea4a4532000-05-29 16:19:32 +00001950}
1951
Neil Boothcef0d192001-07-26 06:02:47 +00001952/* #endif pops the if stack and resets pfile->state.skipping. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001953static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001954do_endif (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001955{
Neil Boothb528a072000-11-12 11:46:21 +00001956 cpp_buffer *buffer = pfile->buffer;
1957 struct if_stack *ifs = buffer->if_stack;
Per Bothner7f2935c1995-03-16 13:59:07 -08001958
Zack Weinbergea4a4532000-05-29 16:19:32 +00001959 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001960 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
Per Bothner7f2935c1995-03-16 13:59:07 -08001961 else
1962 {
Neil Boothcef0d192001-07-26 06:02:47 +00001963 /* Only check EOL if was not originally skipping. */
Phil Edwards909de5d2002-03-22 21:59:04 +00001964 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
Joseph Myersa5cb5632009-04-18 16:28:40 +01001965 check_eol (pfile, false);
Neil Boothcef0d192001-07-26 06:02:47 +00001966
Neil Booth93c803682000-10-28 17:59:06 +00001967 /* If potential control macro, we go back outside again. */
1968 if (ifs->next == 0 && ifs->mi_cmacro)
1969 {
Neil Booth6d18adb2001-07-29 17:27:57 +00001970 pfile->mi_valid = true;
Neil Booth93c803682000-10-28 17:59:06 +00001971 pfile->mi_cmacro = ifs->mi_cmacro;
1972 }
1973
Neil Boothb528a072000-11-12 11:46:21 +00001974 buffer->if_stack = ifs->next;
Neil Boothcef0d192001-07-26 06:02:47 +00001975 pfile->state.skipping = ifs->was_skipping;
Neil Booth2a967f32001-05-20 06:26:45 +00001976 obstack_free (&pfile->buffer_ob, ifs);
Per Bothner7f2935c1995-03-16 13:59:07 -08001977 }
Neil Booth93c803682000-10-28 17:59:06 +00001978}
Zack Weinberg041c3192000-07-04 01:58:21 +00001979
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001980/* Push an if_stack entry for a preprocessor conditional, and set
1981 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1982 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1983 we need to check here that we are at the top of the file. */
Zack Weinbergea4a4532000-05-29 16:19:32 +00001984static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001985push_conditional (cpp_reader *pfile, int skip, int type,
1986 const cpp_hashnode *cmacro)
Zack Weinbergea4a4532000-05-29 16:19:32 +00001987{
1988 struct if_stack *ifs;
Neil Boothb528a072000-11-12 11:46:21 +00001989 cpp_buffer *buffer = pfile->buffer;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001990
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001991 ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
Neil Booth50410422001-09-15 10:18:03 +00001992 ifs->line = pfile->directive_line;
Neil Boothb528a072000-11-12 11:46:21 +00001993 ifs->next = buffer->if_stack;
Neil Boothcef0d192001-07-26 06:02:47 +00001994 ifs->skip_elses = pfile->state.skipping || !skip;
1995 ifs->was_skipping = pfile->state.skipping;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001996 ifs->type = type;
Neil Booth6d18adb2001-07-29 17:27:57 +00001997 /* This condition is effectively a test for top-of-file. */
1998 if (pfile->mi_valid && pfile->mi_cmacro == 0)
Neil Booth93c803682000-10-28 17:59:06 +00001999 ifs->mi_cmacro = cmacro;
2000 else
2001 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00002002
Neil Boothcef0d192001-07-26 06:02:47 +00002003 pfile->state.skipping = skip;
Neil Boothb528a072000-11-12 11:46:21 +00002004 buffer->if_stack = ifs;
Zack Weinberg7061aa51998-12-15 11:09:16 +00002005}
2006
Neil Booth5d8ebbd2002-01-03 21:43:09 +00002007/* Read the tokens of the answer into the macro pool, in a directive
2008 of type TYPE. Only commit the memory if we intend it as permanent
2009 storage, i.e. the #assert case. Returns 0 on success, and sets
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +00002010 ANSWERP to point to the answer. PRED_LOC is the location of the
2011 predicate. */
Neil Booth93c803682000-10-28 17:59:06 +00002012static int
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +00002013parse_answer (cpp_reader *pfile, struct answer **answerp, int type,
2014 source_location pred_loc)
Zack Weinberg041c3192000-07-04 01:58:21 +00002015{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002016 const cpp_token *paren;
Neil Booth93c803682000-10-28 17:59:06 +00002017 struct answer *answer;
Neil Booth8c3b2692001-09-30 10:03:11 +00002018 unsigned int acount;
Zack Weinberg041c3192000-07-04 01:58:21 +00002019
Neil Booth93c803682000-10-28 17:59:06 +00002020 /* In a conditional, it is legal to not have an open paren. We
2021 should save the following token in this case. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002022 paren = cpp_get_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00002023
2024 /* If not a paren, see if we're OK. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002025 if (paren->type != CPP_OPEN_PAREN)
Zack Weinberg041c3192000-07-04 01:58:21 +00002026 {
Neil Booth93c803682000-10-28 17:59:06 +00002027 /* In a conditional no answer is a test for any answer. It
2028 could be followed by any token. */
2029 if (type == T_IF)
Neil Boothbdcbe492001-09-13 20:05:17 +00002030 {
2031 _cpp_backup_tokens (pfile, 1);
2032 return 0;
2033 }
Neil Booth93c803682000-10-28 17:59:06 +00002034
2035 /* #unassert with no answer is valid - it removes all answers. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002036 if (type == T_UNASSERT && paren->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +00002037 return 0;
2038
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +00002039 cpp_error_with_line (pfile, CPP_DL_ERROR, pred_loc, 0,
2040 "missing '(' after predicate");
Neil Booth93c803682000-10-28 17:59:06 +00002041 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00002042 }
2043
Neil Booth8c3b2692001-09-30 10:03:11 +00002044 for (acount = 0;; acount++)
Zack Weinberg041c3192000-07-04 01:58:21 +00002045 {
Neil Booth8c3b2692001-09-30 10:03:11 +00002046 size_t room_needed;
2047 const cpp_token *token = cpp_get_token (pfile);
2048 cpp_token *dest;
Zack Weinberg041c3192000-07-04 01:58:21 +00002049
Neil Booth93c803682000-10-28 17:59:06 +00002050 if (token->type == CPP_CLOSE_PAREN)
2051 break;
Zack Weinberg041c3192000-07-04 01:58:21 +00002052
2053 if (token->type == CPP_EOF)
2054 {
John David Anglin0527bc42003-11-01 22:56:54 +00002055 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
Neil Booth93c803682000-10-28 17:59:06 +00002056 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00002057 }
Neil Booth8c3b2692001-09-30 10:03:11 +00002058
2059 /* struct answer includes the space for one token. */
2060 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
2061
2062 if (BUFF_ROOM (pfile->a_buff) < room_needed)
2063 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
2064
2065 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
2066 *dest = *token;
2067
2068 /* Drop whitespace at start, for answer equivalence purposes. */
2069 if (acount == 0)
2070 dest->flags &= ~PREV_WHITE;
Zack Weinberg041c3192000-07-04 01:58:21 +00002071 }
2072
Neil Booth8c3b2692001-09-30 10:03:11 +00002073 if (acount == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00002074 {
John David Anglin0527bc42003-11-01 22:56:54 +00002075 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
Neil Booth93c803682000-10-28 17:59:06 +00002076 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00002077 }
2078
Neil Booth8c3b2692001-09-30 10:03:11 +00002079 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
2080 answer->count = acount;
2081 answer->next = NULL;
Neil Booth93c803682000-10-28 17:59:06 +00002082 *answerp = answer;
Zack Weinberg041c3192000-07-04 01:58:21 +00002083
Neil Booth93c803682000-10-28 17:59:06 +00002084 return 0;
2085}
2086
Neil Booth5d8ebbd2002-01-03 21:43:09 +00002087/* Parses an assertion directive of type TYPE, returning a pointer to
2088 the hash node of the predicate, or 0 on error. If an answer was
2089 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
Neil Booth93c803682000-10-28 17:59:06 +00002090static cpp_hashnode *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002091parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
Neil Booth93c803682000-10-28 17:59:06 +00002092{
2093 cpp_hashnode *result = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002094 const cpp_token *predicate;
Neil Booth93c803682000-10-28 17:59:06 +00002095
2096 /* We don't expand predicates or answers. */
2097 pfile->state.prevent_expansion++;
2098
Neil Booth93c803682000-10-28 17:59:06 +00002099 *answerp = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002100 predicate = cpp_get_token (pfile);
2101 if (predicate->type == CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +00002102 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002103 else if (predicate->type != CPP_NAME)
Manuel López-Ibáñeza28fbdb2009-06-23 16:30:58 +00002104 cpp_error_with_line (pfile, CPP_DL_ERROR, predicate->src_loc, 0,
2105 "predicate must be an identifier");
2106 else if (parse_answer (pfile, answerp, type, predicate->src_loc) == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00002107 {
Joseph Myers9a0c6182009-05-10 15:27:32 +01002108 unsigned int len = NODE_LEN (predicate->val.node.node);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002109 unsigned char *sym = (unsigned char *) alloca (len + 1);
Neil Booth93c803682000-10-28 17:59:06 +00002110
2111 /* Prefix '#' to get it out of macro namespace. */
2112 sym[0] = '#';
Joseph Myers9a0c6182009-05-10 15:27:32 +01002113 memcpy (sym + 1, NODE_NAME (predicate->val.node.node), len);
Neil Booth93c803682000-10-28 17:59:06 +00002114 result = cpp_lookup (pfile, sym, len + 1);
Zack Weinberg041c3192000-07-04 01:58:21 +00002115 }
2116
Neil Booth93c803682000-10-28 17:59:06 +00002117 pfile->state.prevent_expansion--;
2118 return result;
Zack Weinberg041c3192000-07-04 01:58:21 +00002119}
2120
Neil Booth5d8ebbd2002-01-03 21:43:09 +00002121/* Returns a pointer to the pointer to CANDIDATE in the answer chain,
Zack Weinberg041c3192000-07-04 01:58:21 +00002122 or a pointer to NULL if the answer is not in the chain. */
Neil Booth93c803682000-10-28 17:59:06 +00002123static struct answer **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002124find_answer (cpp_hashnode *node, const struct answer *candidate)
Zack Weinberg041c3192000-07-04 01:58:21 +00002125{
Neil Booth93c803682000-10-28 17:59:06 +00002126 unsigned int i;
Zack Weinberg041c3192000-07-04 01:58:21 +00002127 struct answer **result;
2128
2129 for (result = &node->value.answers; *result; result = &(*result)->next)
Neil Booth93c803682000-10-28 17:59:06 +00002130 {
2131 struct answer *answer = *result;
2132
2133 if (answer->count == candidate->count)
2134 {
2135 for (i = 0; i < answer->count; i++)
2136 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
2137 break;
2138
2139 if (i == answer->count)
2140 break;
2141 }
2142 }
Zack Weinberg041c3192000-07-04 01:58:21 +00002143
2144 return result;
2145}
2146
Neil Booth93c803682000-10-28 17:59:06 +00002147/* Test an assertion within a preprocessor conditional. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +00002148 nonzero on failure, zero on success. On success, the result of
Hans-Peter Nilsson24026452002-11-29 22:41:04 +00002149 the test is written into VALUE, otherwise the value 0. */
Neil Booth93c803682000-10-28 17:59:06 +00002150int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002151_cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
Neil Booth93c803682000-10-28 17:59:06 +00002152{
2153 struct answer *answer;
2154 cpp_hashnode *node;
2155
2156 node = parse_assertion (pfile, &answer, T_IF);
Hans-Peter Nilsson24026452002-11-29 22:41:04 +00002157
2158 /* For recovery, an erroneous assertion expression is handled as a
2159 failing assertion. */
2160 *value = 0;
2161
Neil Booth93c803682000-10-28 17:59:06 +00002162 if (node)
2163 *value = (node->type == NT_ASSERTION &&
2164 (answer == 0 || *find_answer (node, answer) != 0));
Neil Booth91318902002-05-26 18:42:21 +00002165 else if (pfile->cur_token[-1].type == CPP_EOF)
2166 _cpp_backup_tokens (pfile, 1);
Neil Booth93c803682000-10-28 17:59:06 +00002167
2168 /* We don't commit the memory for the answer - it's temporary only. */
2169 return node == 0;
2170}
2171
Neil Booth5d8ebbd2002-01-03 21:43:09 +00002172/* Handle #assert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00002173static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002174do_assert (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08002175{
Zack Weinberg041c3192000-07-04 01:58:21 +00002176 struct answer *new_answer;
2177 cpp_hashnode *node;
Kazu Hiratadf383482002-05-22 22:02:16 +00002178
Neil Booth93c803682000-10-28 17:59:06 +00002179 node = parse_assertion (pfile, &new_answer, T_ASSERT);
Zack Weinberg041c3192000-07-04 01:58:21 +00002180 if (node)
2181 {
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002182 size_t answer_size;
2183
Neil Booth93c803682000-10-28 17:59:06 +00002184 /* Place the new answer in the answer list. First check there
2185 is not a duplicate. */
Zack Weinberg041c3192000-07-04 01:58:21 +00002186 new_answer->next = 0;
Neil Booth93c803682000-10-28 17:59:06 +00002187 if (node->type == NT_ASSERTION)
Zack Weinberg041c3192000-07-04 01:58:21 +00002188 {
Neil Booth93c803682000-10-28 17:59:06 +00002189 if (*find_answer (node, new_answer))
2190 {
John David Anglin0527bc42003-11-01 22:56:54 +00002191 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
Neil Boothebef4e82002-04-14 18:42:47 +00002192 NODE_NAME (node) + 1);
Neil Booth93c803682000-10-28 17:59:06 +00002193 return;
2194 }
Zack Weinberg041c3192000-07-04 01:58:21 +00002195 new_answer->next = node->value.answers;
2196 }
Neil Booth8c3b2692001-09-30 10:03:11 +00002197
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002198 answer_size = sizeof (struct answer) + ((new_answer->count - 1)
2199 * sizeof (cpp_token));
2200 /* Commit or allocate storage for the object. */
2201 if (pfile->hash_table->alloc_subobject)
2202 {
2203 struct answer *temp_answer = new_answer;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002204 new_answer = (struct answer *) pfile->hash_table->alloc_subobject
2205 (answer_size);
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002206 memcpy (new_answer, temp_answer, answer_size);
2207 }
2208 else
2209 BUFF_FRONT (pfile->a_buff) += answer_size;
2210
Neil Booth93c803682000-10-28 17:59:06 +00002211 node->type = NT_ASSERTION;
Zack Weinberg041c3192000-07-04 01:58:21 +00002212 node->value.answers = new_answer;
Joseph Myersa5cb5632009-04-18 16:28:40 +01002213 check_eol (pfile, false);
Zack Weinberg041c3192000-07-04 01:58:21 +00002214 }
Per Bothner7f2935c1995-03-16 13:59:07 -08002215}
Zack Weinberg7061aa51998-12-15 11:09:16 +00002216
Neil Booth5d8ebbd2002-01-03 21:43:09 +00002217/* Handle #unassert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00002218static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002219do_unassert (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08002220{
Zack Weinberg041c3192000-07-04 01:58:21 +00002221 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00002222 struct answer *answer;
Kazu Hiratadf383482002-05-22 22:02:16 +00002223
Neil Booth93c803682000-10-28 17:59:06 +00002224 node = parse_assertion (pfile, &answer, T_UNASSERT);
2225 /* It isn't an error to #unassert something that isn't asserted. */
2226 if (node && node->type == NT_ASSERTION)
Zack Weinberg7061aa51998-12-15 11:09:16 +00002227 {
Zack Weinberg041c3192000-07-04 01:58:21 +00002228 if (answer)
Neil Booth93c803682000-10-28 17:59:06 +00002229 {
2230 struct answer **p = find_answer (node, answer), *temp;
2231
2232 /* Remove the answer from the list. */
2233 temp = *p;
2234 if (temp)
2235 *p = temp->next;
2236
2237 /* Did we free the last answer? */
2238 if (node->value.answers == 0)
2239 node->type = NT_VOID;
Neil Booth8c3b2692001-09-30 10:03:11 +00002240
Joseph Myersa5cb5632009-04-18 16:28:40 +01002241 check_eol (pfile, false);
Neil Booth93c803682000-10-28 17:59:06 +00002242 }
2243 else
2244 _cpp_free_definition (node);
Zack Weinberg7061aa51998-12-15 11:09:16 +00002245 }
Neil Booth93c803682000-10-28 17:59:06 +00002246
2247 /* We don't commit the memory for the answer - it's temporary only. */
Per Bothner7f2935c1995-03-16 13:59:07 -08002248}
Per Bothner7f2935c1995-03-16 13:59:07 -08002249
Zack Weinberg45b966d2000-03-13 22:01:08 +00002250/* These are for -D, -U, -A. */
2251
2252/* Process the string STR as if it appeared as the body of a #define.
2253 If STR is just an identifier, define it with value 1.
2254 If STR has anything after the identifier, then it should
Kazu Hirataec5c56d2001-08-01 17:57:27 +00002255 be identifier=definition. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00002256void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002257cpp_define (cpp_reader *pfile, const char *str)
Zack Weinberg45b966d2000-03-13 22:01:08 +00002258{
Jason Merrill86373e72009-09-12 15:46:45 -04002259 char *buf;
2260 const char *p;
Zack Weinberg45b966d2000-03-13 22:01:08 +00002261 size_t count;
2262
Kazu Hiratadf383482002-05-22 22:02:16 +00002263 /* Copy the entire option so we can modify it.
Zack Weinberg45b966d2000-03-13 22:01:08 +00002264 Change the first "=" in the string to a space. If there is none,
Neil Booth86368122000-10-31 23:34:59 +00002265 tack " 1" on the end. */
2266
Neil Booth86368122000-10-31 23:34:59 +00002267 count = strlen (str);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002268 buf = (char *) alloca (count + 3);
Neil Booth86368122000-10-31 23:34:59 +00002269 memcpy (buf, str, count);
2270
2271 p = strchr (str, '=');
Zack Weinberg45b966d2000-03-13 22:01:08 +00002272 if (p)
Neil Booth86368122000-10-31 23:34:59 +00002273 buf[p - str] = ' ';
Zack Weinberg45b966d2000-03-13 22:01:08 +00002274 else
2275 {
Neil Booth86368122000-10-31 23:34:59 +00002276 buf[count++] = ' ';
2277 buf[count++] = '1';
Zack Weinberg45b966d2000-03-13 22:01:08 +00002278 }
Neil Booth26aea072003-04-19 00:22:51 +00002279 buf[count] = '\n';
Zack Weinberg45b966d2000-03-13 22:01:08 +00002280
Neil Booth29401c32001-08-22 20:37:20 +00002281 run_directive (pfile, T_DEFINE, buf, count);
Zack Weinberg2c8f0512000-08-29 18:37:37 +00002282}
2283
Daniel Franke28f68622008-04-22 14:04:32 -04002284
2285/* Use to build macros to be run through cpp_define() as
2286 described above.
2287 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
2288
2289void
2290cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...)
2291{
2292 char *ptr = NULL;
2293
2294 va_list ap;
2295 va_start (ap, fmt);
2296 vasprintf (&ptr, fmt, ap);
2297 va_end (ap);
2298
2299 cpp_define (pfile, ptr);
2300 free (ptr);
2301}
2302
2303
Neil Boothad2a0842000-12-17 00:13:54 +00002304/* Slight variant of the above for use by initialize_builtins. */
Zack Weinberg2c8f0512000-08-29 18:37:37 +00002305void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002306_cpp_define_builtin (cpp_reader *pfile, const char *str)
Zack Weinberg2c8f0512000-08-29 18:37:37 +00002307{
Neil Booth26aea072003-04-19 00:22:51 +00002308 size_t len = strlen (str);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002309 char *buf = (char *) alloca (len + 1);
Neil Booth26aea072003-04-19 00:22:51 +00002310 memcpy (buf, str, len);
2311 buf[len] = '\n';
2312 run_directive (pfile, T_DEFINE, buf, len);
Zack Weinberg45b966d2000-03-13 22:01:08 +00002313}
2314
2315/* Process MACRO as if it appeared as the body of an #undef. */
2316void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002317cpp_undef (cpp_reader *pfile, const char *macro)
Zack Weinberg45b966d2000-03-13 22:01:08 +00002318{
Neil Booth26aea072003-04-19 00:22:51 +00002319 size_t len = strlen (macro);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002320 char *buf = (char *) alloca (len + 1);
Neil Booth26aea072003-04-19 00:22:51 +00002321 memcpy (buf, macro, len);
2322 buf[len] = '\n';
2323 run_directive (pfile, T_UNDEF, buf, len);
Zack Weinberg45b966d2000-03-13 22:01:08 +00002324}
2325
Richard Henderson121de392007-03-30 14:12:53 -07002326/* If STR is a defined macro, return its definition node, else return NULL. */
2327cpp_macro *
2328cpp_push_definition (cpp_reader *pfile, const char *str)
2329{
Kai Tietz17e7cb82009-11-11 18:37:19 +00002330 cpp_hashnode *node = _cpp_lex_identifier (pfile, str);
Richard Henderson121de392007-03-30 14:12:53 -07002331 if (node && node->type == NT_MACRO)
2332 return node->value.macro;
2333 else
2334 return NULL;
2335}
2336
2337/* Replace a previous definition DFN of the macro STR. If DFN is NULL,
2338 then the macro should be undefined. */
2339void
2340cpp_pop_definition (cpp_reader *pfile, const char *str, cpp_macro *dfn)
2341{
Kai Tietz17e7cb82009-11-11 18:37:19 +00002342 cpp_hashnode *node = _cpp_lex_identifier (pfile, str);
Richard Henderson121de392007-03-30 14:12:53 -07002343 if (node == NULL)
2344 return;
2345
Joseph Myers93d45d92008-04-02 20:42:53 +01002346 if (pfile->cb.before_define)
2347 pfile->cb.before_define (pfile);
2348
Richard Henderson121de392007-03-30 14:12:53 -07002349 if (node->type == NT_MACRO)
2350 {
2351 if (pfile->cb.undef)
2352 pfile->cb.undef (pfile, pfile->directive_line, node);
2353 if (CPP_OPTION (pfile, warn_unused_macros))
2354 _cpp_warn_if_unused_macro (pfile, node, NULL);
2355 }
2356 if (node->type != NT_VOID)
2357 _cpp_free_definition (node);
2358
2359 if (dfn)
2360 {
2361 node->type = NT_MACRO;
2362 node->value.macro = dfn;
2363 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
2364 node->flags |= NODE_WARN;
2365
2366 if (pfile->cb.define)
2367 pfile->cb.define (pfile, pfile->directive_line, node);
2368 }
2369}
2370
Kazu Hirataec5c56d2001-08-01 17:57:27 +00002371/* Process the string STR as if it appeared as the body of a #assert. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00002372void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002373cpp_assert (cpp_reader *pfile, const char *str)
Zack Weinberg45b966d2000-03-13 22:01:08 +00002374{
Neil Booth86368122000-10-31 23:34:59 +00002375 handle_assertion (pfile, str, T_ASSERT);
Zack Weinberg45b966d2000-03-13 22:01:08 +00002376}
2377
Kazu Hirataec5c56d2001-08-01 17:57:27 +00002378/* Process STR as if it appeared as the body of an #unassert. */
Zack Weinberg0b22d651999-03-15 18:42:46 +00002379void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002380cpp_unassert (cpp_reader *pfile, const char *str)
Zack Weinberg0b22d651999-03-15 18:42:46 +00002381{
Neil Booth86368122000-10-31 23:34:59 +00002382 handle_assertion (pfile, str, T_UNASSERT);
Kazu Hiratadf383482002-05-22 22:02:16 +00002383}
Zack Weinberg0b22d651999-03-15 18:42:46 +00002384
Neil Booth86368122000-10-31 23:34:59 +00002385/* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2386static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002387handle_assertion (cpp_reader *pfile, const char *str, int type)
Neil Booth86368122000-10-31 23:34:59 +00002388{
2389 size_t count = strlen (str);
2390 const char *p = strchr (str, '=');
2391
Neil Booth26aea072003-04-19 00:22:51 +00002392 /* Copy the entire option so we can modify it. Change the first
2393 "=" in the string to a '(', and tack a ')' on the end. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002394 char *buf = (char *) alloca (count + 2);
Neil Booth26aea072003-04-19 00:22:51 +00002395
2396 memcpy (buf, str, count);
Neil Booth86368122000-10-31 23:34:59 +00002397 if (p)
2398 {
Neil Booth86368122000-10-31 23:34:59 +00002399 buf[p - str] = '(';
2400 buf[count++] = ')';
Neil Booth86368122000-10-31 23:34:59 +00002401 }
Neil Booth26aea072003-04-19 00:22:51 +00002402 buf[count] = '\n';
2403 str = buf;
Neil Booth86368122000-10-31 23:34:59 +00002404
Neil Booth29401c32001-08-22 20:37:20 +00002405 run_directive (pfile, type, str, count);
Neil Booth86368122000-10-31 23:34:59 +00002406}
2407
Neil Booth7e96d762001-01-13 01:00:01 +00002408/* The options structure. */
2409cpp_options *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002410cpp_get_options (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00002411{
2412 return &pfile->opts;
2413}
2414
2415/* The callbacks structure. */
2416cpp_callbacks *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002417cpp_get_callbacks (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00002418{
2419 return &pfile->cb;
2420}
2421
2422/* Copy the given callbacks structure to our own. */
2423void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002424cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
Neil Booth7e96d762001-01-13 01:00:01 +00002425{
2426 pfile->cb = *cb;
2427}
2428
Zack Weinbergc6e83802004-06-05 20:58:06 +00002429/* The dependencies structure. (Creates one if it hasn't already been.) */
2430struct deps *
2431cpp_get_deps (cpp_reader *pfile)
2432{
2433 if (!pfile->deps)
2434 pfile->deps = deps_init ();
2435 return pfile->deps;
2436}
2437
Neil Bootheb1f4d92000-12-18 19:00:26 +00002438/* Push a new buffer on the buffer stack. Returns the new buffer; it
2439 doesn't fail. It does not generate a file change call back; that
2440 is the responsibility of the caller. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00002441cpp_buffer *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002442cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
Per Bothner40de9f72003-10-02 07:30:34 +00002443 int from_stage3)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002444{
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002445 cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
Neil Booth93c803682000-10-28 17:59:06 +00002446
Neil Boothfde84342001-08-06 21:07:41 +00002447 /* Clears, amongst other things, if_stack and mi_cmacro. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002448 memset (new_buffer, 0, sizeof (cpp_buffer));
Neil Boothad2a0842000-12-17 00:13:54 +00002449
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002450 new_buffer->next_line = new_buffer->buf = buffer;
2451 new_buffer->rlimit = buffer + len;
2452 new_buffer->from_stage3 = from_stage3;
2453 new_buffer->prev = pfile->buffer;
2454 new_buffer->need_line = true;
Neil Booth0bda4762000-12-11 07:45:16 +00002455
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002456 pfile->buffer = new_buffer;
Eric Christophercf551fb2004-01-16 22:37:49 +00002457
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002458 return new_buffer;
Zack Weinbergc71f8352000-07-05 05:33:57 +00002459}
2460
Neil Boothaf0d16c2002-04-22 17:48:02 +00002461/* Pops a single buffer, with a file change call-back if appropriate.
2462 Then pushes the next -include file, if any remain. */
Neil Boothef6e9582001-08-04 12:01:59 +00002463void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002464_cpp_pop_buffer (cpp_reader *pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002465{
Neil Boothfde84342001-08-06 21:07:41 +00002466 cpp_buffer *buffer = pfile->buffer;
Neil Booth8f9b4002003-07-29 22:26:13 +00002467 struct _cpp_file *inc = buffer->file;
Neil Boothad2a0842000-12-17 00:13:54 +00002468 struct if_stack *ifs;
Zack Weinbergc71f8352000-07-05 05:33:57 +00002469
Neil Boothfde84342001-08-06 21:07:41 +00002470 /* Walk back up the conditional stack till we reach its level at
2471 entry to this file, issuing error messages. */
2472 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
John David Anglin0527bc42003-11-01 22:56:54 +00002473 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Boothfde84342001-08-06 21:07:41 +00002474 "unterminated #%s", dtable[ifs->type].name);
2475
Neil Booth97293892001-09-14 22:04:46 +00002476 /* In case of a missing #endif. */
Neil Booth67821e32001-08-05 17:31:25 +00002477 pfile->state.skipping = 0;
Neil Booth29401c32001-08-22 20:37:20 +00002478
Neil Boothaf0d16c2002-04-22 17:48:02 +00002479 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
Neil Booth29401c32001-08-22 20:37:20 +00002480 pfile->buffer = buffer->prev;
2481
Neil Booth26aea072003-04-19 00:22:51 +00002482 free (buffer->notes);
2483
Neil Boothaf0d16c2002-04-22 17:48:02 +00002484 /* Free the buffer object now; we may want to push a new buffer
2485 in _cpp_push_next_include_file. */
2486 obstack_free (&pfile->buffer_ob, buffer);
Neil Booth29401c32001-08-22 20:37:20 +00002487
Neil Boothaf0d16c2002-04-22 17:48:02 +00002488 if (inc)
2489 {
2490 _cpp_pop_file_buffer (pfile, inc);
2491
Per Bothner40de9f72003-10-02 07:30:34 +00002492 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
Neil Boothaf0d16c2002-04-22 17:48:02 +00002493 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00002494}
2495
Kazu Hirata05713b82002-09-15 18:24:08 +00002496/* Enter all recognized directives in the hash table. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00002497void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002498_cpp_init_directives (cpp_reader *pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002499{
Neil Booth766ee682001-01-29 19:20:12 +00002500 unsigned int i;
Neil Booth93c803682000-10-28 17:59:06 +00002501 cpp_hashnode *node;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00002502
John David Anglin37b85242001-03-02 01:11:50 +00002503 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
Neil Booth93c803682000-10-28 17:59:06 +00002504 {
Neil Booth766ee682001-01-29 19:20:12 +00002505 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
Zack Weinberg4977bab2002-12-16 18:23:00 +00002506 node->is_directive = 1;
2507 node->directive_index = i;
Neil Booth93c803682000-10-28 17:59:06 +00002508 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00002509}