blob: 5a6a342bea5b0b63e5803ffbc96d7eccb10a3447 [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,
Kazu Hiratad9221e012004-01-21 20:40:04 +00003 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
Richard Kenner4c8cc611997-02-16 08:08:25 -05004 Contributed by Per Bothner, 1994-95.
Richard Kennerd8bfa781997-01-03 08:19:34 -05005 Based on CCCP program by Paul Rubin, June 1986
Per Bothner7f2935c1995-03-16 13:59:07 -08006 Adapted to ANSI C, Richard Stallman, Jan 1987
7
8This program is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 2, or (at your option) any
11later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
Jeff Law956d6951997-12-06 17:31:01 -070020Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
Per Bothner7f2935c1995-03-16 13:59:07 -080021
Per Bothner7f2935c1995-03-16 13:59:07 -080022#include "config.h"
Kaveh R. Ghazib04cd5071998-03-30 12:05:54 +000023#include "system.h"
Jeff Law956d6951997-12-06 17:31:01 -070024#include "cpplib.h"
Paolo Bonzini4f4e53dd2004-05-24 10:50:45 +000025#include "internal.h"
Zack Weinbergc6e83802004-06-05 20:58:06 +000026#include "mkdeps.h"
Zack Weinbergc71f8352000-07-05 05:33:57 +000027#include "obstack.h"
Jeff Law956d6951997-12-06 17:31:01 -070028
Neil Booth93c803682000-10-28 17:59:06 +000029/* Chained list of answers to an assertion. */
30struct answer
31{
32 struct answer *next;
33 unsigned int count;
34 cpp_token first[1];
35};
36
Zack Weinberg88ae23e2000-03-08 23:35:19 +000037/* Stack of conditionals currently in progress
38 (including both successful and failing conditionals). */
Zack Weinberg88ae23e2000-03-08 23:35:19 +000039struct if_stack
40{
41 struct if_stack *next;
Neil Booth50410422001-09-15 10:18:03 +000042 unsigned int line; /* Line where condition started. */
Neil Booth93c803682000-10-28 17:59:06 +000043 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
Neil Boothcef0d192001-07-26 06:02:47 +000044 bool skip_elses; /* Can future #else / #elif be skipped? */
45 bool was_skipping; /* If were skipping on entry. */
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000046 int type; /* Most recent conditional for diagnostics. */
Zack Weinberg88ae23e2000-03-08 23:35:19 +000047};
Zack Weinberg88ae23e2000-03-08 23:35:19 +000048
Neil Bootha5da89c2001-10-14 17:44:00 +000049/* Contains a registered pragma or pragma namespace. */
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000050typedef void (*pragma_cb) (cpp_reader *);
Neil Bootha5da89c2001-10-14 17:44:00 +000051struct pragma_entry
52{
53 struct pragma_entry *next;
Neil Booth4b115ff2001-10-14 23:04:13 +000054 const cpp_hashnode *pragma; /* Name and length. */
Neil Bootha5da89c2001-10-14 17:44:00 +000055 int is_nspace;
56 union {
57 pragma_cb handler;
58 struct pragma_entry *space;
59 } u;
60};
61
Neil Booth93c803682000-10-28 17:59:06 +000062/* Values for the origin field of struct directive. KANDR directives
63 come from traditional (K&R) C. STDC89 directives come from the
64 1989 C standard. EXTENSION directives are extensions. */
65#define KANDR 0
66#define STDC89 1
67#define EXTENSION 2
68
69/* Values for the flags field of struct directive. COND indicates a
70 conditional; IF_COND an opening conditional. INCL means to treat
71 "..." and <...> as q-char and h-char sequences respectively. IN_I
72 means this directive should be handled even if -fpreprocessed is in
Neil Booth1a769162002-06-11 05:36:17 +000073 effect (these are the directives with callback hooks).
74
Neil Boothd97371e2002-06-18 06:27:40 +000075 EXPAND is set on directives that are always macro-expanded. */
Neil Booth93c803682000-10-28 17:59:06 +000076#define COND (1 << 0)
77#define IF_COND (1 << 1)
78#define INCL (1 << 2)
79#define IN_I (1 << 3)
Neil Booth1a769162002-06-11 05:36:17 +000080#define EXPAND (1 << 4)
Neil Booth93c803682000-10-28 17:59:06 +000081
82/* Defines one #-directive, including how to handle it. */
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000083typedef void (*directive_handler) (cpp_reader *);
Neil Booth93c803682000-10-28 17:59:06 +000084typedef struct directive directive;
85struct directive
86{
87 directive_handler handler; /* Function to handle directive. */
Neil Booth562a5c22002-04-21 18:46:42 +000088 const uchar *name; /* Name of directive. */
Neil Booth93c803682000-10-28 17:59:06 +000089 unsigned short length; /* Length of name. */
90 unsigned char origin; /* Origin of directive. */
91 unsigned char flags; /* Flags describing this directive. */
92};
93
Zack Weinberg1316f1f2000-02-06 07:53:50 +000094/* Forward declarations. */
95
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000096static void skip_rest_of_line (cpp_reader *);
97static void check_eol (cpp_reader *);
98static void start_directive (cpp_reader *);
99static void prepare_directive_trad (cpp_reader *);
100static void end_directive (cpp_reader *, int);
101static void directive_diagnostics (cpp_reader *, const directive *, int);
102static void run_directive (cpp_reader *, int, const char *, size_t);
103static char *glue_header_name (cpp_reader *);
104static const char *parse_include (cpp_reader *, int *);
105static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
106static unsigned int read_flag (cpp_reader *, unsigned int);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000107static int strtoul_for_line (const uchar *, unsigned int, unsigned long *);
108static void do_diagnostic (cpp_reader *, int, int);
109static cpp_hashnode *lex_macro_node (cpp_reader *);
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000110static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000111static void do_include_common (cpp_reader *, enum include_type);
112static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
113 const cpp_hashnode *);
114static struct pragma_entry *insert_pragma_entry (cpp_reader *,
115 struct pragma_entry **,
116 const cpp_hashnode *,
117 pragma_cb);
118static int count_registered_pragmas (struct pragma_entry *);
119static char ** save_registered_pragmas (struct pragma_entry *, char **);
120static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
121 char **);
122static void do_pragma_once (cpp_reader *);
123static void do_pragma_poison (cpp_reader *);
124static void do_pragma_system_header (cpp_reader *);
125static void do_pragma_dependency (cpp_reader *);
126static void do_linemarker (cpp_reader *);
127static const cpp_token *get_token_no_padding (cpp_reader *);
128static const cpp_token *get__Pragma_string (cpp_reader *);
129static void destringize_and_run (cpp_reader *, const cpp_string *);
130static int parse_answer (cpp_reader *, struct answer **, int);
131static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
132static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
133static void handle_assertion (cpp_reader *, const char *, int);
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +0000134
Zack Weinberg168d3732000-03-14 06:34:11 +0000135/* This is the table of directive handlers. It is ordered by
136 frequency of occurrence; the numbers at the end are directive
137 counts from all the source code I have lying around (egcs and libc
138 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
Neil Booth93c803682000-10-28 17:59:06 +0000139 pcmcia-cs-3.0.9). This is no longer important as directive lookup
140 is now O(1). All extensions other than #warning and #include_next
141 are deprecated. The name is where the extension appears to have
142 come from. */
Jeff Lawe9a25f71997-11-02 14:19:36 -0700143
Neil Booth93c803682000-10-28 17:59:06 +0000144#define DIRECTIVE_TABLE \
145D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000146D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000147D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
148D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
Neil Booth1a769162002-06-11 05:36:17 +0000149D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000150D(else, T_ELSE, KANDR, COND) /* 9863 */ \
151D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
152D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
Neil Booth1a769162002-06-11 05:36:17 +0000153D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
154D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000155D(error, T_ERROR, STDC89, 0) /* 475 */ \
156D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
157D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000158D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000159D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000160D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
Neil Booth93c803682000-10-28 17:59:06 +0000161D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
162D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
Neil Booth74d06cf2002-07-17 21:31:42 +0000163D(sccs, T_SCCS, EXTENSION, 0) /* 0 SVR4? */
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{
195 do_linemarker, U"#", 1, KANDR, IN_I
196};
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
214/* Ensure there are no stray tokens at the end of a directive. */
215static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000216check_eol (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +0000217{
Neil Booth345894b2001-09-16 13:44:29 +0000218 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000219 cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
Neil Boothebef4e82002-04-14 18:42:47 +0000220 pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +0000221}
222
Neil Boothfe6c2db2000-11-15 19:25:22 +0000223/* Called when entering a directive, _Pragma or command-line directive. */
224static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000225start_directive (cpp_reader *pfile)
Zack Weinbergc5a04732000-04-25 19:32:36 +0000226{
Neil Booth7f2f1a62000-11-14 18:32:06 +0000227 /* Setup in-directive state. */
228 pfile->state.in_directive = 1;
229 pfile->state.save_comments = 0;
230
Neil Booth93c803682000-10-28 17:59:06 +0000231 /* Some handlers need the position of the # for diagnostics. */
Per Bothner500bee02004-04-22 19:22:27 -0700232 pfile->directive_line = pfile->line_table->highest_line;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000233}
234
235/* Called when leaving a directive, _Pragma or command-line directive. */
236static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000237end_directive (cpp_reader *pfile, int skip_line)
Neil Boothfe6c2db2000-11-15 19:25:22 +0000238{
Neil Booth1a769162002-06-11 05:36:17 +0000239 if (CPP_OPTION (pfile, traditional))
240 {
Neil Boothd97371e2002-06-18 06:27:40 +0000241 /* Revert change of prepare_directive_trad. */
242 pfile->state.prevent_expansion--;
243
Neil Boothb66377c2002-06-13 21:16:00 +0000244 if (pfile->directive != &dtable[T_DEFINE])
Neil Booth1a769162002-06-11 05:36:17 +0000245 _cpp_remove_overlay (pfile);
246 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000247 /* We don't skip for an assembler #. */
Neil Boothb66377c2002-06-13 21:16:00 +0000248 else if (skip_line)
Neil Booth67821e32001-08-05 17:31:25 +0000249 {
250 skip_rest_of_line (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +0000251 if (!pfile->keep_tokens)
252 {
253 pfile->cur_run = &pfile->base_run;
254 pfile->cur_token = pfile->base_run.base;
255 }
Neil Booth67821e32001-08-05 17:31:25 +0000256 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000257
258 /* Restore state. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000259 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
260 pfile->state.in_directive = 0;
Neil Boothd97371e2002-06-18 06:27:40 +0000261 pfile->state.in_expression = 0;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000262 pfile->state.angled_headers = 0;
263 pfile->directive = 0;
264}
265
Neil Booth1a769162002-06-11 05:36:17 +0000266/* Prepare to handle the directive in pfile->directive. */
267static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000268prepare_directive_trad (cpp_reader *pfile)
Neil Booth1a769162002-06-11 05:36:17 +0000269{
Neil Booth951a0762002-06-27 06:01:58 +0000270 if (pfile->directive != &dtable[T_DEFINE])
Neil Booth1a769162002-06-11 05:36:17 +0000271 {
Neil Boothb66377c2002-06-13 21:16:00 +0000272 bool no_expand = (pfile->directive
273 && ! (pfile->directive->flags & EXPAND));
Neil Booth974c43f2002-06-13 06:25:28 +0000274 bool was_skipping = pfile->state.skipping;
Neil Booth1a769162002-06-11 05:36:17 +0000275
Neil Boothd97371e2002-06-18 06:27:40 +0000276 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
277 || pfile->directive == &dtable[T_ELIF]);
Neil Booth45f24922003-12-12 07:00:29 +0000278 if (pfile->state.in_expression)
279 pfile->state.skipping = false;
280
Neil Booth1a769162002-06-11 05:36:17 +0000281 if (no_expand)
282 pfile->state.prevent_expansion++;
Zack Weinberg43839642003-07-13 17:34:18 +0000283 _cpp_scan_out_logical_line (pfile, NULL);
Neil Booth1a769162002-06-11 05:36:17 +0000284 if (no_expand)
285 pfile->state.prevent_expansion--;
Neil Booth45f24922003-12-12 07:00:29 +0000286
Neil Booth974c43f2002-06-13 06:25:28 +0000287 pfile->state.skipping = was_skipping;
Neil Booth1a769162002-06-11 05:36:17 +0000288 _cpp_overlay_buffer (pfile, pfile->out.base,
289 pfile->out.cur - pfile->out.base);
290 }
Neil Boothd97371e2002-06-18 06:27:40 +0000291
292 /* Stop ISO C from expanding anything. */
293 pfile->state.prevent_expansion++;
Neil Booth1a769162002-06-11 05:36:17 +0000294}
295
Kazu Hiratada7d8302002-09-22 02:03:17 +0000296/* Output diagnostics for a directive DIR. INDENTED is nonzero if
Neil Booth18a9d8f2001-09-16 11:23:56 +0000297 the '#' was indented. */
Neil Booth18a9d8f2001-09-16 11:23:56 +0000298static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000299directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000300{
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000301 /* Issue -pedantic warnings for extensions. */
302 if (CPP_PEDANTIC (pfile)
303 && ! pfile->state.skipping
304 && dir->origin == EXTENSION)
John David Anglin0527bc42003-11-01 22:56:54 +0000305 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000306
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000307 /* Traditionally, a directive is ignored unless its # is in
308 column 1. Therefore in code intended to work with K+R
309 compilers, directives added by C89 must have their #
310 indented, and directives present in traditional C must not.
311 This is true even of directives in skipped conditional
312 blocks. #elif cannot be used at all. */
313 if (CPP_WTRADITIONAL (pfile))
314 {
315 if (dir == &dtable[T_ELIF])
John David Anglin0527bc42003-11-01 22:56:54 +0000316 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000317 "suggest not using #elif in traditional C");
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000318 else if (indented && dir->origin == KANDR)
John David Anglin0527bc42003-11-01 22:56:54 +0000319 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000320 "traditional C ignores #%s with the # indented",
321 dir->name);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000322 else if (!indented && dir->origin != KANDR)
John David Anglin0527bc42003-11-01 22:56:54 +0000323 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000324 "suggest hiding #%s from traditional C with an indented #",
325 dir->name);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000326 }
327}
328
Kazu Hiratada7d8302002-09-22 02:03:17 +0000329/* Check if we have a known directive. INDENTED is nonzero if the
Neil Booth18a9d8f2001-09-16 11:23:56 +0000330 '#' of the directive was indented. This function is in this file
331 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +0000332 nonzero if the line of tokens has been handled, zero if we should
Neil Booth18a9d8f2001-09-16 11:23:56 +0000333 continue processing the line. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000334int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000335_cpp_handle_directive (cpp_reader *pfile, int indented)
Neil Boothfe6c2db2000-11-15 19:25:22 +0000336{
Neil Boothfe6c2db2000-11-15 19:25:22 +0000337 const directive *dir = 0;
Neil Booth345894b2001-09-16 13:44:29 +0000338 const cpp_token *dname;
Neil Boothe808ec92002-02-27 07:24:53 +0000339 bool was_parsing_args = pfile->state.parsing_args;
Zack Weinbergc6e83802004-06-05 20:58:06 +0000340 bool was_discarding_output = pfile->state.discarding_output;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000341 int skip = 1;
342
Zack Weinbergc6e83802004-06-05 20:58:06 +0000343 if (was_discarding_output)
344 pfile->state.prevent_expansion = 0;
345
Neil Boothe808ec92002-02-27 07:24:53 +0000346 if (was_parsing_args)
347 {
348 if (CPP_OPTION (pfile, pedantic))
John David Anglin0527bc42003-11-01 22:56:54 +0000349 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothe808ec92002-02-27 07:24:53 +0000350 "embedding a directive within macro arguments is not portable");
351 pfile->state.parsing_args = 0;
352 pfile->state.prevent_expansion = 0;
353 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000354 start_directive (pfile);
Neil Booth345894b2001-09-16 13:44:29 +0000355 dname = _cpp_lex_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000356
Neil Booth345894b2001-09-16 13:44:29 +0000357 if (dname->type == CPP_NAME)
Neil Booth0d9f2342000-09-18 18:43:05 +0000358 {
Zack Weinberg4977bab2002-12-16 18:23:00 +0000359 if (dname->val.node->is_directive)
360 dir = &dtable[dname->val.node->directive_index];
Neil Booth93c803682000-10-28 17:59:06 +0000361 }
Kazu Hirata05713b82002-09-15 18:24:08 +0000362 /* We do not recognize the # followed by a number extension in
Neil Booth18a9d8f2001-09-16 11:23:56 +0000363 assembler code. */
Neil Booth345894b2001-09-16 13:44:29 +0000364 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth93c803682000-10-28 17:59:06 +0000365 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000366 dir = &linemarker_dir;
367 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
368 && ! pfile->state.skipping)
John David Anglin0527bc42003-11-01 22:56:54 +0000369 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothebef4e82002-04-14 18:42:47 +0000370 "style of line directive is a GCC extension");
Neil Booth0d9f2342000-09-18 18:43:05 +0000371 }
372
Neil Booth93c803682000-10-28 17:59:06 +0000373 if (dir)
Neil Booth0d9f2342000-09-18 18:43:05 +0000374 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000375 /* If we have a directive that is not an opening conditional,
376 invalidate any control macro. */
377 if (! (dir->flags & IF_COND))
378 pfile->mi_valid = false;
Neil Booth93c803682000-10-28 17:59:06 +0000379
Neil Booth18a9d8f2001-09-16 11:23:56 +0000380 /* Kluge alert. In order to be sure that code like this
381
382 #define HASH #
383 HASH define foo bar
384
385 does not cause '#define foo bar' to get executed when
386 compiled with -save-temps, we recognize directives in
387 -fpreprocessed mode only if the # is in column 1. cppmacro.c
Joseph Myersa1f300c2001-11-23 02:05:19 +0000388 puts a space in front of any '#' at the start of a macro. */
Neil Booth18a9d8f2001-09-16 11:23:56 +0000389 if (CPP_OPTION (pfile, preprocessed)
390 && (indented || !(dir->flags & IN_I)))
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000391 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000392 skip = 0;
393 dir = 0;
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000394 }
395 else
Neil Booth93c803682000-10-28 17:59:06 +0000396 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000397 /* In failed conditional groups, all non-conditional
398 directives are ignored. Before doing that, whether
399 skipping or not, we should lex angle-bracketed headers
400 correctly, and maybe output some diagnostics. */
401 pfile->state.angled_headers = dir->flags & INCL;
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000402 pfile->state.directive_wants_padding = dir->flags & INCL;
Neil Booth18a9d8f2001-09-16 11:23:56 +0000403 if (! CPP_OPTION (pfile, preprocessed))
404 directive_diagnostics (pfile, dir, indented);
405 if (pfile->state.skipping && !(dir->flags & COND))
406 dir = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000407 }
408 }
Neil Booth345894b2001-09-16 13:44:29 +0000409 else if (dname->type == CPP_EOF)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000410 ; /* CPP_EOF is the "null directive". */
411 else
Neil Booth0d9f2342000-09-18 18:43:05 +0000412 {
Neil Booth93c803682000-10-28 17:59:06 +0000413 /* An unknown directive. Don't complain about it in assembly
414 source: we don't know where the comments are, and # may
415 introduce assembler pseudo-ops. Don't complain about invalid
416 directives in skipped conditional groups (6.10 p4). */
Neil Boothbdb05a72000-11-26 17:31:13 +0000417 if (CPP_OPTION (pfile, lang) == CLK_ASM)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000418 skip = 0;
419 else if (!pfile->state.skipping)
John David Anglin0527bc42003-11-01 22:56:54 +0000420 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
Neil Booth345894b2001-09-16 13:44:29 +0000421 cpp_token_as_text (pfile, dname));
Neil Booth0d9f2342000-09-18 18:43:05 +0000422 }
423
Neil Boothd1a58682002-06-28 06:26:54 +0000424 pfile->directive = dir;
425 if (CPP_OPTION (pfile, traditional))
426 prepare_directive_trad (pfile);
427
Neil Booth18a9d8f2001-09-16 11:23:56 +0000428 if (dir)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000429 pfile->directive->handler (pfile);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000430 else if (skip == 0)
431 _cpp_backup_tokens (pfile, 1);
432
433 end_directive (pfile, skip);
Neil Boothe808ec92002-02-27 07:24:53 +0000434 if (was_parsing_args)
435 {
436 /* Restore state when within macro args. */
437 pfile->state.parsing_args = 2;
438 pfile->state.prevent_expansion = 1;
Neil Boothe808ec92002-02-27 07:24:53 +0000439 }
Zack Weinbergc6e83802004-06-05 20:58:06 +0000440 if (was_discarding_output)
441 pfile->state.prevent_expansion = 1;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000442 return skip;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000443}
444
Neil Booth93c803682000-10-28 17:59:06 +0000445/* Directive handler wrapper used by the command line option
Neil Booth26aea072003-04-19 00:22:51 +0000446 processor. BUF is \n terminated. */
Neil Booth93c803682000-10-28 17:59:06 +0000447static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000448run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000449{
Neil Booth562a5c22002-04-21 18:46:42 +0000450 cpp_push_buffer (pfile, (const uchar *) buf, count,
Per Bothner40de9f72003-10-02 07:30:34 +0000451 /* from_stage3 */ true);
Neil Booth8bfb1462002-08-14 20:17:55 +0000452 /* Disgusting hack. */
453 if (dir_no == T_PRAGMA)
Neil Booth8f9b4002003-07-29 22:26:13 +0000454 pfile->buffer->file = pfile->buffer->prev->file;
Neil Booth0bda4762000-12-11 07:45:16 +0000455 start_directive (pfile);
Neil Booth26aea072003-04-19 00:22:51 +0000456
457 /* This is a short-term fix to prevent a leading '#' being
458 interpreted as a directive. */
459 _cpp_clean_line (pfile);
460
Neil Boothf71aebb2001-05-27 18:06:00 +0000461 pfile->directive = &dtable[dir_no];
Neil Booth1a769162002-06-11 05:36:17 +0000462 if (CPP_OPTION (pfile, traditional))
463 prepare_directive_trad (pfile);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000464 pfile->directive->handler (pfile);
Neil Booth0bda4762000-12-11 07:45:16 +0000465 end_directive (pfile, 1);
Neil Booth8bfb1462002-08-14 20:17:55 +0000466 if (dir_no == T_PRAGMA)
Neil Booth8f9b4002003-07-29 22:26:13 +0000467 pfile->buffer->file = NULL;
Neil Boothef6e9582001-08-04 12:01:59 +0000468 _cpp_pop_buffer (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000469}
Per Bothner7f2935c1995-03-16 13:59:07 -0800470
Neil Booth93c803682000-10-28 17:59:06 +0000471/* Checks for validity the macro name in #define, #undef, #ifdef and
472 #ifndef directives. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000473static cpp_hashnode *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000474lex_macro_node (cpp_reader *pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000475{
Neil Booth1a769162002-06-11 05:36:17 +0000476 const cpp_token *token = _cpp_lex_token (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000477
Zack Weinberg92936ec2000-07-19 20:18:08 +0000478 /* The token immediately after #define must be an identifier. That
Zack Weinbergb8363a22001-07-01 18:48:13 +0000479 identifier may not be "defined", per C99 6.10.8p4.
480 In C++, it may not be any of the "named operators" either,
481 per C++98 [lex.digraph], [lex.key].
482 Finally, the identifier may not have been poisoned. (In that case
Neil Booth1d63a282002-06-28 20:27:14 +0000483 the lexer has issued the error message for us.) */
Zack Weinbergb8363a22001-07-01 18:48:13 +0000484
Neil Booth1a769162002-06-11 05:36:17 +0000485 if (token->type == CPP_NAME)
486 {
487 cpp_hashnode *node = token->val.node;
488
489 if (node == pfile->spec_nodes.n_defined)
John David Anglin0527bc42003-11-01 22:56:54 +0000490 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth1a769162002-06-11 05:36:17 +0000491 "\"defined\" cannot be used as a macro name");
492 else if (! (node->flags & NODE_POISONED))
493 return node;
494 }
495 else if (token->flags & NAMED_OP)
John David Anglin0527bc42003-11-01 22:56:54 +0000496 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcbc69f82002-06-05 20:27:12 +0000497 "\"%s\" cannot be used as a macro name as it is an operator in C++",
Neil Booth1a769162002-06-11 05:36:17 +0000498 NODE_NAME (token->val.node));
499 else if (token->type == CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000500 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
Neil Booth1a769162002-06-11 05:36:17 +0000501 pfile->directive->name);
502 else
John David Anglin0527bc42003-11-01 22:56:54 +0000503 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
Zack Weinbergb8363a22001-07-01 18:48:13 +0000504
Neil Boothcbc69f82002-06-05 20:27:12 +0000505 return NULL;
Per Bothner7f2935c1995-03-16 13:59:07 -0800506}
Per Bothner7f2935c1995-03-16 13:59:07 -0800507
Neil Booth93c803682000-10-28 17:59:06 +0000508/* Process a #define directive. Most work is done in cppmacro.c. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000509static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000510do_define (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800511{
Neil Booth93c803682000-10-28 17:59:06 +0000512 cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000513
Neil Booth93c803682000-10-28 17:59:06 +0000514 if (node)
515 {
Neil Booth1d63a282002-06-28 20:27:14 +0000516 /* If we have been requested to expand comments into macros,
517 then re-enable saving of comments. */
518 pfile->state.save_comments =
519 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
520
Neil Booth93c803682000-10-28 17:59:06 +0000521 if (_cpp_create_definition (pfile, node))
522 if (pfile->cb.define)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000523 pfile->cb.define (pfile, pfile->directive_line, node);
Neil Booth93c803682000-10-28 17:59:06 +0000524 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800525}
526
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000527/* Handle #undef. Mark the identifier NT_VOID in the hash table. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000528static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000529do_undef (cpp_reader *pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000530{
Kazu Hiratadf383482002-05-22 22:02:16 +0000531 cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000532
Neil Booth45f24922003-12-12 07:00:29 +0000533 if (node)
Zack Weinberg041c3192000-07-04 01:58:21 +0000534 {
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000535 if (pfile->cb.undef)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000536 pfile->cb.undef (pfile, pfile->directive_line, node);
Zack Weinberg041c3192000-07-04 01:58:21 +0000537
Neil Booth45f24922003-12-12 07:00:29 +0000538 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
539 identifier is not currently defined as a macro name. */
540 if (node->type == NT_MACRO)
541 {
542 if (node->flags & NODE_WARN)
543 cpp_error (pfile, CPP_DL_WARNING,
544 "undefining \"%s\"", NODE_NAME (node));
Zack Weinberg041c3192000-07-04 01:58:21 +0000545
Neil Booth45f24922003-12-12 07:00:29 +0000546 if (CPP_OPTION (pfile, warn_unused_macros))
547 _cpp_warn_if_unused_macro (pfile, node, NULL);
Neil Bootha69cbaa2002-07-23 22:57:49 +0000548
Neil Booth45f24922003-12-12 07:00:29 +0000549 _cpp_free_definition (node);
550 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000551 }
Neil Booth45f24922003-12-12 07:00:29 +0000552
Neil Booth93c803682000-10-28 17:59:06 +0000553 check_eol (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000554}
555
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000556/* Undefine a single macro/assertion/whatever. */
557
558static int
Zack Weinbergc6e83802004-06-05 20:58:06 +0000559undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000560 void *data_p ATTRIBUTE_UNUSED)
561{
Zack Weinbergc6e83802004-06-05 20:58:06 +0000562 /* Body of _cpp_free_definition inlined here for speed.
563 Macros and assertions no longer have anything to free. */
564 h->type = NT_VOID;
565 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED);
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000566 return 1;
567}
568
569/* Undefine all macros and assertions. */
570
571void
572cpp_undef_all (cpp_reader *pfile)
573{
574 cpp_forall_identifiers (pfile, undefine_macros, NULL);
575}
576
577
Neil Booth93c803682000-10-28 17:59:06 +0000578/* Helper routine used by parse_include. Reinterpret the current line
579 as an h-char-sequence (< ... >); we are looking at the first token
Neil Booth74eb4b32003-04-21 19:21:59 +0000580 after the <. Returns a malloced filename. */
581static char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000582glue_header_name (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800583{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000584 const cpp_token *token;
Neil Booth74eb4b32003-04-21 19:21:59 +0000585 char *buffer;
Neil Booth2450e0b2002-02-23 20:21:39 +0000586 size_t len, total_len = 0, capacity = 1024;
Per Bothner7f2935c1995-03-16 13:59:07 -0800587
Neil Booth93c803682000-10-28 17:59:06 +0000588 /* To avoid lexed tokens overwriting our glued name, we can only
589 allocate from the string pool once we've lexed everything. */
Neil Booth74eb4b32003-04-21 19:21:59 +0000590 buffer = xmalloc (capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000591 for (;;)
Zack Weinberg168d3732000-03-14 06:34:11 +0000592 {
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000593 token = get_token_no_padding (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000594
Neil Booth74eb4b32003-04-21 19:21:59 +0000595 if (token->type == CPP_GREATER)
Neil Booth93c803682000-10-28 17:59:06 +0000596 break;
Neil Booth74eb4b32003-04-21 19:21:59 +0000597 if (token->type == CPP_EOF)
598 {
John David Anglin0527bc42003-11-01 22:56:54 +0000599 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
Neil Booth74eb4b32003-04-21 19:21:59 +0000600 break;
601 }
Neil Booth93c803682000-10-28 17:59:06 +0000602
Neil Booth59325652003-04-24 20:03:57 +0000603 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
Neil Booth2450e0b2002-02-23 20:21:39 +0000604 if (total_len + len > capacity)
Neil Booth93c803682000-10-28 17:59:06 +0000605 {
Neil Booth2450e0b2002-02-23 20:21:39 +0000606 capacity = (capacity + len) * 2;
Neil Booth74eb4b32003-04-21 19:21:59 +0000607 buffer = xrealloc (buffer, capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000608 }
609
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000610 if (token->flags & PREV_WHITE)
Neil Booth2450e0b2002-02-23 20:21:39 +0000611 buffer[total_len++] = ' ';
Neil Booth93c803682000-10-28 17:59:06 +0000612
Neil Booth74eb4b32003-04-21 19:21:59 +0000613 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len])
614 - (uchar *) buffer);
Neil Booth93c803682000-10-28 17:59:06 +0000615 }
616
Neil Booth74eb4b32003-04-21 19:21:59 +0000617 buffer[total_len] = '\0';
618 return buffer;
Neil Booth93c803682000-10-28 17:59:06 +0000619}
620
Neil Booth74eb4b32003-04-21 19:21:59 +0000621/* Returns the file name of #include, #include_next, #import and
622 #pragma dependency. The string is malloced and the caller should
623 free it. Returns NULL on error. */
624static const char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000625parse_include (cpp_reader *pfile, int *pangle_brackets)
Neil Booth93c803682000-10-28 17:59:06 +0000626{
Neil Booth74eb4b32003-04-21 19:21:59 +0000627 char *fname;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000628 const cpp_token *header;
Neil Booth93c803682000-10-28 17:59:06 +0000629
Neil Booth93c803682000-10-28 17:59:06 +0000630 /* Allow macro expansion. */
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000631 header = get_token_no_padding (pfile);
Neil Booth74eb4b32003-04-21 19:21:59 +0000632 if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
Neil Booth93c803682000-10-28 17:59:06 +0000633 {
Neil Booth6338b352003-04-23 22:44:06 +0000634 fname = xmalloc (header->val.str.len - 1);
635 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
636 fname[header->val.str.len - 2] = '\0';
Neil Booth74eb4b32003-04-21 19:21:59 +0000637 *pangle_brackets = header->type == CPP_HEADER_NAME;
Zack Weinberg041c3192000-07-04 01:58:21 +0000638 }
Neil Booth74eb4b32003-04-21 19:21:59 +0000639 else if (header->type == CPP_LESS)
Zack Weinberg041c3192000-07-04 01:58:21 +0000640 {
Neil Booth74eb4b32003-04-21 19:21:59 +0000641 fname = glue_header_name (pfile);
642 *pangle_brackets = 1;
643 }
644 else
645 {
646 const unsigned char *dir;
647
648 if (pfile->directive == &dtable[T_PRAGMA])
649 dir = U"pragma dependency";
650 else
651 dir = pfile->directive->name;
John David Anglin0527bc42003-11-01 22:56:54 +0000652 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
Neil Booth74eb4b32003-04-21 19:21:59 +0000653 dir);
654
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000655 return NULL;
Richard Kennercfb3ee11997-04-13 14:30:13 -0400656 }
657
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000658 check_eol (pfile);
Neil Booth74eb4b32003-04-21 19:21:59 +0000659 return fname;
Zack Weinberg168d3732000-03-14 06:34:11 +0000660}
661
Neil Boothba133c92001-03-15 07:57:13 +0000662/* Handle #include, #include_next and #import. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000663static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000664do_include_common (cpp_reader *pfile, enum include_type type)
Zack Weinberg168d3732000-03-14 06:34:11 +0000665{
Neil Booth74eb4b32003-04-21 19:21:59 +0000666 const char *fname;
667 int angle_brackets;
668
669 fname = parse_include (pfile, &angle_brackets);
670 if (!fname)
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000671 return;
Zack Weinberg168d3732000-03-14 06:34:11 +0000672
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000673 /* Prevent #include recursion. */
Per Bothner50f59cd2004-01-19 21:30:18 -0800674 if (pfile->line_table->depth >= CPP_STACK_MAX)
John David Anglin0527bc42003-11-01 22:56:54 +0000675 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
Neil Booth74eb4b32003-04-21 19:21:59 +0000676 else
Neil Booth09b82252001-07-29 22:27:20 +0000677 {
Neil Booth74eb4b32003-04-21 19:21:59 +0000678 /* Get out of macro context, if we are. */
679 skip_rest_of_line (pfile);
680
681 if (pfile->cb.include)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000682 pfile->cb.include (pfile, pfile->directive_line,
683 pfile->directive->name, fname, angle_brackets);
Neil Booth74eb4b32003-04-21 19:21:59 +0000684
Neil Booth8f9b4002003-07-29 22:26:13 +0000685 _cpp_stack_include (pfile, fname, angle_brackets, type);
Neil Booth09b82252001-07-29 22:27:20 +0000686 }
687
Kaveh R. Ghazifad205f2003-06-16 21:41:10 +0000688 free ((void *) fname);
Neil Boothba133c92001-03-15 07:57:13 +0000689}
690
691static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000692do_include (cpp_reader *pfile)
Neil Boothba133c92001-03-15 07:57:13 +0000693{
694 do_include_common (pfile, IT_INCLUDE);
Zack Weinberg168d3732000-03-14 06:34:11 +0000695}
696
Zack Weinberg711b8822000-07-18 00:59:49 +0000697static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000698do_import (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +0000699{
Neil Boothba133c92001-03-15 07:57:13 +0000700 do_include_common (pfile, IT_IMPORT);
Zack Weinberg168d3732000-03-14 06:34:11 +0000701}
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000702
Zack Weinberg711b8822000-07-18 00:59:49 +0000703static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000704do_include_next (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +0000705{
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000706 enum include_type type = IT_INCLUDE_NEXT;
707
708 /* If this is the primary source file, warn and use the normal
709 search logic. */
710 if (! pfile->buffer->prev)
711 {
John David Anglin0527bc42003-11-01 22:56:54 +0000712 cpp_error (pfile, CPP_DL_WARNING,
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000713 "#include_next in primary source file");
714 type = IT_INCLUDE;
715 }
716 do_include_common (pfile, type);
Per Bothner7f2935c1995-03-16 13:59:07 -0800717}
718
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000719/* Subroutine of do_linemarker. Read possible flags after file name.
720 LAST is the last flag seen; 0 if this is the first flag. Return the
721 flag if it is valid, 0 at the end of the directive. Otherwise
722 complain. */
Neil Booth642ce432000-12-07 23:17:56 +0000723static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000724read_flag (cpp_reader *pfile, unsigned int last)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000725{
Neil Booth345894b2001-09-16 13:44:29 +0000726 const cpp_token *token = _cpp_lex_token (pfile);
Jason Merrilld3a34a01999-08-14 00:42:07 +0000727
Neil Booth345894b2001-09-16 13:44:29 +0000728 if (token->type == CPP_NUMBER && token->val.str.len == 1)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000729 {
Neil Booth345894b2001-09-16 13:44:29 +0000730 unsigned int flag = token->val.str.text[0] - '0';
Neil Booth28e0f042000-12-09 12:06:37 +0000731
732 if (flag > last && flag <= 4
733 && (flag != 4 || last == 3)
734 && (flag != 2 || last == 0))
735 return flag;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000736 }
Neil Booth93c803682000-10-28 17:59:06 +0000737
Neil Booth345894b2001-09-16 13:44:29 +0000738 if (token->type != CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000739 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
Neil Booth345894b2001-09-16 13:44:29 +0000740 cpp_token_as_text (pfile, token));
Neil Booth93c803682000-10-28 17:59:06 +0000741 return 0;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000742}
743
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000744/* Subroutine of do_line and do_linemarker. Convert a number in STR,
745 of length LEN, to binary; store it in NUMP, and return 0 if the
746 number was well-formed, 1 if not. Temporary, hopefully. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000747static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000748strtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump)
Zack Weinberg041c3192000-07-04 01:58:21 +0000749{
750 unsigned long reg = 0;
Neil Booth562a5c22002-04-21 18:46:42 +0000751 uchar c;
Zack Weinberg041c3192000-07-04 01:58:21 +0000752 while (len--)
753 {
754 c = *str++;
755 if (!ISDIGIT (c))
756 return 1;
757 reg *= 10;
758 reg += c - '0';
759 }
760 *nump = reg;
761 return 0;
762}
763
Zack Weinberg5538ada1999-02-04 06:36:54 -0500764/* Interpret #line command.
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000765 Note that the filename string (if any) is a true string constant
766 (escapes are interpreted), unlike in #line. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000767static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000768do_line (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800769{
Per Bothner500bee02004-04-22 19:22:27 -0700770 const struct line_maps *line_table = pfile->line_table;
771 const struct line_map *map = &line_table->maps[line_table->used - 1];
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000772 const cpp_token *token;
Per Bothner12f9df42004-02-11 07:29:30 -0800773 const char *new_file = map->to_file;
Neil Boothbb74c962001-08-17 22:23:49 +0000774 unsigned long new_lineno;
Per Bothner7f2935c1995-03-16 13:59:07 -0800775
Neil Booth27e25642000-11-27 08:00:04 +0000776 /* C99 raised the minimum limit on #line numbers. */
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000777 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
Neil Booth18a9d8f2001-09-16 11:23:56 +0000778
Neil Booth93c803682000-10-28 17:59:06 +0000779 /* #line commands expand macros. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000780 token = cpp_get_token (pfile);
781 if (token->type != CPP_NUMBER
782 || strtoul_for_line (token->val.str.text, token->val.str.len,
783 &new_lineno))
Per Bothner7f2935c1995-03-16 13:59:07 -0800784 {
John David Anglin0527bc42003-11-01 22:56:54 +0000785 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +0000786 "\"%s\" after #line is not a positive integer",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000787 cpp_token_as_text (pfile, token));
Zack Weinberg9ec72912000-08-09 19:41:12 +0000788 return;
Kazu Hiratadf383482002-05-22 22:02:16 +0000789 }
Zack Weinberg5538ada1999-02-04 06:36:54 -0500790
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000791 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
John David Anglin0527bc42003-11-01 22:56:54 +0000792 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
Zack Weinberg5538ada1999-02-04 06:36:54 -0500793
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000794 token = cpp_get_token (pfile);
795 if (token->type == CPP_STRING)
Zack Weinberg5538ada1999-02-04 06:36:54 -0500796 {
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000797 cpp_string s = { 0, 0 };
Eric Christopher423e95e2004-02-12 02:25:03 +0000798 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
799 &s, false))
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000800 new_file = (const char *)s.text;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000801 check_eol (pfile);
802 }
803 else if (token->type != CPP_EOF)
804 {
John David Anglin0527bc42003-11-01 22:56:54 +0000805 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000806 cpp_token_as_text (pfile, token));
807 return;
808 }
Neil Booth93c803682000-10-28 17:59:06 +0000809
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000810 skip_rest_of_line (pfile);
811 _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
Per Bothner12f9df42004-02-11 07:29:30 -0800812 map->sysp);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000813}
814
815/* Interpret the # 44 "file" [flags] notation, which has slightly
816 different syntax and semantics from #line: Flags are allowed,
817 and we never complain about the line number being too big. */
818static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000819do_linemarker (cpp_reader *pfile)
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000820{
Per Bothner500bee02004-04-22 19:22:27 -0700821 const struct line_maps *line_table = pfile->line_table;
822 const struct line_map *map = &line_table->maps[line_table->used - 1];
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000823 const cpp_token *token;
Per Bothner12f9df42004-02-11 07:29:30 -0800824 const char *new_file = map->to_file;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000825 unsigned long new_lineno;
Per Bothner12f9df42004-02-11 07:29:30 -0800826 unsigned int new_sysp = map->sysp;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000827 enum lc_reason reason = LC_RENAME;
828 int flag;
829
830 /* Back up so we can get the number again. Putting this in
831 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
832 some circumstances, which can segfault. */
833 _cpp_backup_tokens (pfile, 1);
834
835 /* #line commands expand macros. */
836 token = cpp_get_token (pfile);
837 if (token->type != CPP_NUMBER
838 || strtoul_for_line (token->val.str.text, token->val.str.len,
839 &new_lineno))
840 {
John David Anglin0527bc42003-11-01 22:56:54 +0000841 cpp_error (pfile, CPP_DL_ERROR,
842 "\"%s\" after # is not a positive integer",
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000843 cpp_token_as_text (pfile, token));
844 return;
Kazu Hiratadf383482002-05-22 22:02:16 +0000845 }
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000846
847 token = cpp_get_token (pfile);
848 if (token->type == CPP_STRING)
849 {
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000850 cpp_string s = { 0, 0 };
Eric Christopher423e95e2004-02-12 02:25:03 +0000851 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
852 1, &s, false))
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000853 new_file = (const char *)s.text;
Eric Christophercf551fb2004-01-16 22:37:49 +0000854
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000855 new_sysp = 0;
856 flag = read_flag (pfile, 0);
857 if (flag == 1)
Neil Booth93c803682000-10-28 17:59:06 +0000858 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000859 reason = LC_ENTER;
860 /* Fake an include for cpp_included (). */
861 _cpp_fake_include (pfile, new_file);
862 flag = read_flag (pfile, flag);
Neil Booth93c803682000-10-28 17:59:06 +0000863 }
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000864 else if (flag == 2)
865 {
866 reason = LC_LEAVE;
867 flag = read_flag (pfile, flag);
868 }
869 if (flag == 3)
870 {
871 new_sysp = 1;
872 flag = read_flag (pfile, flag);
873 if (flag == 4)
874 new_sysp = 2;
Per Bothner12f9df42004-02-11 07:29:30 -0800875 pfile->buffer->sysp = new_sysp;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000876 }
877
Neil Boothfde84342001-08-06 21:07:41 +0000878 check_eol (pfile);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000879 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000880 else if (token->type != CPP_EOF)
Neil Booth27e25642000-11-27 08:00:04 +0000881 {
John David Anglin0527bc42003-11-01 22:56:54 +0000882 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000883 cpp_token_as_text (pfile, token));
Neil Booth27e25642000-11-27 08:00:04 +0000884 return;
885 }
Zack Weinberg941e09b1998-12-15 11:17:06 +0000886
Neil Boothbdcbe492001-09-13 20:05:17 +0000887 skip_rest_of_line (pfile);
Neil Boothbb74c962001-08-17 22:23:49 +0000888 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
Neil Booth27e25642000-11-27 08:00:04 +0000889}
890
Neil Booth67821e32001-08-05 17:31:25 +0000891/* Arrange the file_change callback. pfile->line has changed to
Neil Booth47d89cf2001-08-11 07:33:39 +0000892 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
Joseph Myersa1f300c2001-11-23 02:05:19 +0000893 header, 2 for a system header that needs to be extern "C" protected,
Neil Booth47d89cf2001-08-11 07:33:39 +0000894 and zero otherwise. */
Neil Bootheb1f4d92000-12-18 19:00:26 +0000895void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000896_cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
897 const char *to_file, unsigned int file_line,
898 unsigned int sysp)
Neil Booth27e25642000-11-27 08:00:04 +0000899{
Per Bothner12f9df42004-02-11 07:29:30 -0800900 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
901 to_file, file_line);
Per Bothner500bee02004-04-22 19:22:27 -0700902 if (map != NULL)
903 linemap_line_start (pfile->line_table, map->to_line, 127);
Neil Boothd82fc102001-08-02 23:03:31 +0000904
Neil Bootheb1f4d92000-12-18 19:00:26 +0000905 if (pfile->cb.file_change)
Per Bothner12f9df42004-02-11 07:29:30 -0800906 pfile->cb.file_change (pfile, map);
Per Bothner7f2935c1995-03-16 13:59:07 -0800907}
Zack Weinberg941e09b1998-12-15 11:17:06 +0000908
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000909/* Report a warning or error detected by the program we are
910 processing. Use the directive's tokens in the error message. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000911static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000912do_diagnostic (cpp_reader *pfile, int code, int print_dir)
Per Bothner7f2935c1995-03-16 13:59:07 -0800913{
Per Bothner12f9df42004-02-11 07:29:30 -0800914 if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0))
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000915 {
Neil Booth29b10742000-11-13 18:40:37 +0000916 if (print_dir)
917 fprintf (stderr, "#%s ", pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +0000918 pfile->state.prevent_expansion++;
919 cpp_output_line (pfile, stderr);
920 pfile->state.prevent_expansion--;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000921 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800922}
923
Neil Booth838f3132000-09-24 10:42:09 +0000924static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000925do_error (cpp_reader *pfile)
Neil Booth838f3132000-09-24 10:42:09 +0000926{
John David Anglin0527bc42003-11-01 22:56:54 +0000927 do_diagnostic (pfile, CPP_DL_ERROR, 1);
Neil Booth838f3132000-09-24 10:42:09 +0000928}
Per Bothner7f2935c1995-03-16 13:59:07 -0800929
Zack Weinberg711b8822000-07-18 00:59:49 +0000930static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000931do_warning (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800932{
Neil Booth2f878972001-04-08 10:01:18 +0000933 /* We want #warning diagnostics to be emitted in system headers too. */
John David Anglin0527bc42003-11-01 22:56:54 +0000934 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
Per Bothner7f2935c1995-03-16 13:59:07 -0800935}
936
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000937/* Report program identification. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000938static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000939do_ident (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800940{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000941 const cpp_token *str = cpp_get_token (pfile);
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000942
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000943 if (str->type != CPP_STRING)
John David Anglin0527bc42003-11-01 22:56:54 +0000944 cpp_error (pfile, CPP_DL_ERROR, "invalid #ident directive");
Neil Booth93c803682000-10-28 17:59:06 +0000945 else if (pfile->cb.ident)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000946 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000947
Neil Booth93c803682000-10-28 17:59:06 +0000948 check_eol (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -0800949}
950
Neil Bootha5da89c2001-10-14 17:44:00 +0000951/* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
952 matching entry, or NULL if none is found. The returned entry could
953 be the start of a namespace chain, or a pragma. */
954static struct pragma_entry *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000955lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
Nathan Sidwell82443372000-06-23 10:56:09 +0000956{
Neil Booth4b115ff2001-10-14 23:04:13 +0000957 while (chain && chain->pragma != pragma)
958 chain = chain->next;
Neil Bootha5da89c2001-10-14 17:44:00 +0000959
960 return chain;
961}
962
963/* Create and insert a pragma entry for NAME at the beginning of a
964 singly-linked CHAIN. If handler is NULL, it is a namespace,
965 otherwise it is a pragma and its handler. */
966static struct pragma_entry *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000967insert_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain,
968 const cpp_hashnode *pragma, pragma_cb handler)
Neil Bootha5da89c2001-10-14 17:44:00 +0000969{
970 struct pragma_entry *new;
971
972 new = (struct pragma_entry *)
973 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
Neil Booth4b115ff2001-10-14 23:04:13 +0000974 new->pragma = pragma;
Neil Bootha5da89c2001-10-14 17:44:00 +0000975 if (handler)
976 {
977 new->is_nspace = 0;
978 new->u.handler = handler;
979 }
980 else
981 {
982 new->is_nspace = 1;
983 new->u.space = NULL;
984 }
985
986 new->next = *chain;
987 *chain = new;
988 return new;
989}
990
991/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
992 goes in the global namespace. HANDLER is the handler it will call,
993 which must be non-NULL. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000994void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000995cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
996 pragma_cb handler)
Nathan Sidwell82443372000-06-23 10:56:09 +0000997{
Neil Bootha5da89c2001-10-14 17:44:00 +0000998 struct pragma_entry **chain = &pfile->pragmas;
999 struct pragma_entry *entry;
Neil Booth4b115ff2001-10-14 23:04:13 +00001000 const cpp_hashnode *node;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001001
Neil Bootha5da89c2001-10-14 17:44:00 +00001002 if (!handler)
1003 abort ();
1004
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001005 if (space)
1006 {
Neil Booth4b115ff2001-10-14 23:04:13 +00001007 node = cpp_lookup (pfile, U space, strlen (space));
1008 entry = lookup_pragma_entry (*chain, node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001009 if (!entry)
Neil Booth4b115ff2001-10-14 23:04:13 +00001010 entry = insert_pragma_entry (pfile, chain, node, NULL);
Neil Bootha5da89c2001-10-14 17:44:00 +00001011 else if (!entry->is_nspace)
1012 goto clash;
1013 chain = &entry->u.space;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001014 }
1015
Neil Bootha5da89c2001-10-14 17:44:00 +00001016 /* Check for duplicates. */
Neil Booth4b115ff2001-10-14 23:04:13 +00001017 node = cpp_lookup (pfile, U name, strlen (name));
1018 entry = lookup_pragma_entry (*chain, node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001019 if (entry)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001020 {
Neil Bootha5da89c2001-10-14 17:44:00 +00001021 if (entry->is_nspace)
1022 clash:
John David Anglin0527bc42003-11-01 22:56:54 +00001023 cpp_error (pfile, CPP_DL_ICE,
Neil Bootha5da89c2001-10-14 17:44:00 +00001024 "registering \"%s\" as both a pragma and a pragma namespace",
Neil Booth4b115ff2001-10-14 23:04:13 +00001025 NODE_NAME (node));
Neil Bootha5da89c2001-10-14 17:44:00 +00001026 else if (space)
John David Anglin0527bc42003-11-01 22:56:54 +00001027 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
Neil Boothebef4e82002-04-14 18:42:47 +00001028 space, name);
Neil Bootha5da89c2001-10-14 17:44:00 +00001029 else
John David Anglin0527bc42003-11-01 22:56:54 +00001030 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001031 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001032 else
Neil Booth4b115ff2001-10-14 23:04:13 +00001033 insert_pragma_entry (pfile, chain, node, handler);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001034}
Neil Bootha5da89c2001-10-14 17:44:00 +00001035
1036/* Register the pragmas the preprocessor itself handles. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001037void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001038_cpp_init_internal_pragmas (cpp_reader *pfile)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001039{
Neil Bootha5da89c2001-10-14 17:44:00 +00001040 /* Pragmas in the global namespace. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001041 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
1042
Neil Bootha5da89c2001-10-14 17:44:00 +00001043 /* New GCC-specific pragmas should be put in the GCC namespace. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001044 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
1045 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
1046 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
Nathan Sidwell82443372000-06-23 10:56:09 +00001047}
Per Bothner7f2935c1995-03-16 13:59:07 -08001048
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001049/* Return the number of registered pragmas in PE. */
1050
1051static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001052count_registered_pragmas (struct pragma_entry *pe)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001053{
1054 int ct = 0;
1055 for (; pe != NULL; pe = pe->next)
1056 {
1057 if (pe->is_nspace)
1058 ct += count_registered_pragmas (pe->u.space);
1059 ct++;
1060 }
1061 return ct;
1062}
1063
1064/* Save into SD the names of the registered pragmas referenced by PE,
1065 and return a pointer to the next free space in SD. */
1066
1067static char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001068save_registered_pragmas (struct pragma_entry *pe, char **sd)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001069{
1070 for (; pe != NULL; pe = pe->next)
1071 {
1072 if (pe->is_nspace)
1073 sd = save_registered_pragmas (pe->u.space, sd);
1074 *sd++ = xmemdup (HT_STR (&pe->pragma->ident),
1075 HT_LEN (&pe->pragma->ident),
1076 HT_LEN (&pe->pragma->ident) + 1);
1077 }
1078 return sd;
1079}
1080
1081/* Return a newly-allocated array which saves the names of the
1082 registered pragmas. */
1083
1084char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001085_cpp_save_pragma_names (cpp_reader *pfile)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001086{
1087 int ct = count_registered_pragmas (pfile->pragmas);
1088 char **result = xnewvec (char *, ct);
1089 (void) save_registered_pragmas (pfile->pragmas, result);
1090 return result;
1091}
1092
1093/* Restore from SD the names of the registered pragmas referenced by PE,
1094 and return a pointer to the next unused name in SD. */
1095
1096static char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001097restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1098 char **sd)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001099{
1100 for (; pe != NULL; pe = pe->next)
1101 {
1102 if (pe->is_nspace)
1103 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1104 pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd));
1105 free (*sd);
1106 sd++;
1107 }
1108 return sd;
1109}
1110
1111/* Restore the names of the registered pragmas from SAVED. */
1112
1113void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001114_cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001115{
1116 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1117 free (saved);
1118}
1119
Neil Bootha5da89c2001-10-14 17:44:00 +00001120/* Pragmata handling. We handle some, and pass the rest on to the
1121 front end. C99 defines three pragmas and says that no macro
1122 expansion is to be performed on them; whether or not macro
1123 expansion happens for other pragmas is implementation defined.
1124 This implementation never macro-expands the text after #pragma. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001125static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001126do_pragma (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001127{
Neil Bootha5da89c2001-10-14 17:44:00 +00001128 const struct pragma_entry *p = NULL;
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001129 const cpp_token *token, *pragma_token = pfile->cur_token;
Neil Bootha5da89c2001-10-14 17:44:00 +00001130 unsigned int count = 1;
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001131
Neil Booth93c803682000-10-28 17:59:06 +00001132 pfile->state.prevent_expansion++;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001133
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001134 token = cpp_get_token (pfile);
1135 if (token->type == CPP_NAME)
Alexandre Oliva0172e2b2000-02-27 06:24:27 +00001136 {
Neil Booth4b115ff2001-10-14 23:04:13 +00001137 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001138 if (p && p->is_nspace)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001139 {
Neil Bootha5da89c2001-10-14 17:44:00 +00001140 count = 2;
1141 token = cpp_get_token (pfile);
1142 if (token->type == CPP_NAME)
Neil Booth4b115ff2001-10-14 23:04:13 +00001143 p = lookup_pragma_entry (p->u.space, token->val.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001144 else
1145 p = NULL;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001146 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001147 }
1148
Neil Bootha5da89c2001-10-14 17:44:00 +00001149 if (p)
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001150 {
1151 /* Since the handler below doesn't get the line number, that it
1152 might need for diagnostics, make sure it has the right
1153 numbers in place. */
1154 if (pfile->cb.line_change)
1155 (*pfile->cb.line_change) (pfile, pragma_token, false);
1156 (*p->u.handler) (pfile);
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001157 }
Neil Boothd82fc102001-08-02 23:03:31 +00001158 else if (pfile->cb.def_pragma)
Neil Boothbdcbe492001-09-13 20:05:17 +00001159 {
1160 _cpp_backup_tokens (pfile, count);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001161 pfile->cb.def_pragma (pfile, pfile->directive_line);
Neil Boothbdcbe492001-09-13 20:05:17 +00001162 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001163
Neil Booth97293892001-09-14 22:04:46 +00001164 pfile->state.prevent_expansion--;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001165}
1166
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001167/* Handle #pragma once. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001168static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001169do_pragma_once (cpp_reader *pfile)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001170{
Neil Booth642ce432000-12-07 23:17:56 +00001171 if (pfile->buffer->prev == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001172 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
Neil Booth93c803682000-10-28 17:59:06 +00001173
1174 check_eol (pfile);
Neil Booth49634b32003-08-02 16:29:46 +00001175 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001176}
1177
Neil Boothc3bf3e62002-05-09 17:14:22 +00001178/* Handle #pragma GCC poison, to poison one or more identifiers so
1179 that the lexer produces a hard error for each subsequent usage. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001180static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001181do_pragma_poison (cpp_reader *pfile)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001182{
Neil Booth345894b2001-09-16 13:44:29 +00001183 const cpp_token *tok;
Zack Weinbergf8f769e2000-05-28 05:56:38 +00001184 cpp_hashnode *hp;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001185
Neil Booth93c803682000-10-28 17:59:06 +00001186 pfile->state.poisoned_ok = 1;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001187 for (;;)
1188 {
Neil Booth345894b2001-09-16 13:44:29 +00001189 tok = _cpp_lex_token (pfile);
1190 if (tok->type == CPP_EOF)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001191 break;
Neil Booth345894b2001-09-16 13:44:29 +00001192 if (tok->type != CPP_NAME)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001193 {
John David Anglin0527bc42003-11-01 22:56:54 +00001194 cpp_error (pfile, CPP_DL_ERROR,
1195 "invalid #pragma GCC poison directive");
Neil Booth93c803682000-10-28 17:59:06 +00001196 break;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001197 }
1198
Neil Booth345894b2001-09-16 13:44:29 +00001199 hp = tok->val.node;
Neil Booth93c803682000-10-28 17:59:06 +00001200 if (hp->flags & NODE_POISONED)
1201 continue;
1202
1203 if (hp->type == NT_MACRO)
John David Anglin0527bc42003-11-01 22:56:54 +00001204 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
Neil Boothebef4e82002-04-14 18:42:47 +00001205 NODE_NAME (hp));
Neil Booth93c803682000-10-28 17:59:06 +00001206 _cpp_free_definition (hp);
1207 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001208 }
Neil Booth93c803682000-10-28 17:59:06 +00001209 pfile->state.poisoned_ok = 0;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001210}
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001211
1212/* Mark the current header as a system header. This will suppress
1213 some categories of warnings (notably those from -pedantic). It is
1214 intended for use in system libraries that cannot be implemented in
1215 conforming C, but cannot be certain that their headers appear in a
1216 system include directory. To prevent abuse, it is rejected in the
1217 primary source file. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001218static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001219do_pragma_system_header (cpp_reader *pfile)
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001220{
Neil Booth614c7d32000-12-04 07:32:04 +00001221 cpp_buffer *buffer = pfile->buffer;
1222
1223 if (buffer->prev == 0)
John David Anglin0527bc42003-11-01 22:56:54 +00001224 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +00001225 "#pragma system_header ignored outside include file");
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001226 else
Neil Boothd82fc102001-08-02 23:03:31 +00001227 {
1228 check_eol (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +00001229 skip_rest_of_line (pfile);
Neil Boothd82fc102001-08-02 23:03:31 +00001230 cpp_make_system_header (pfile, 1, 0);
1231 }
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001232}
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001233
1234/* Check the modified date of the current include file against a specified
1235 file. Issue a diagnostic, if the specified file is newer. We use this to
1236 determine if a fixed header should be refixed. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001237static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001238do_pragma_dependency (cpp_reader *pfile)
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001239{
Neil Booth74eb4b32003-04-21 19:21:59 +00001240 const char *fname;
1241 int angle_brackets, ordering;
Kazu Hiratadf383482002-05-22 22:02:16 +00001242
Neil Booth74eb4b32003-04-21 19:21:59 +00001243 fname = parse_include (pfile, &angle_brackets);
1244 if (!fname)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001245 return;
Zack Weinberg041c3192000-07-04 01:58:21 +00001246
Neil Booth74eb4b32003-04-21 19:21:59 +00001247 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001248 if (ordering < 0)
John David Anglin0527bc42003-11-01 22:56:54 +00001249 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001250 else if (ordering > 0)
1251 {
John David Anglin0527bc42003-11-01 22:56:54 +00001252 cpp_error (pfile, CPP_DL_WARNING,
1253 "current file is older than %s", fname);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001254 if (cpp_get_token (pfile)->type != CPP_EOF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001255 {
1256 _cpp_backup_tokens (pfile, 1);
John David Anglin0527bc42003-11-01 22:56:54 +00001257 do_diagnostic (pfile, CPP_DL_WARNING, 0);
Neil Boothbdcbe492001-09-13 20:05:17 +00001258 }
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001259 }
Neil Booth74eb4b32003-04-21 19:21:59 +00001260
Kaveh R. Ghazifad205f2003-06-16 21:41:10 +00001261 free ((void *) fname);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001262}
1263
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001264/* Get a token but skip padding. */
1265static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001266get_token_no_padding (cpp_reader *pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001267{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001268 for (;;)
1269 {
1270 const cpp_token *result = cpp_get_token (pfile);
1271 if (result->type != CPP_PADDING)
1272 return result;
1273 }
1274}
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001275
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001276/* Check syntax is "(string-literal)". Returns the string on success,
1277 or NULL on failure. */
1278static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001279get__Pragma_string (cpp_reader *pfile)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001280{
1281 const cpp_token *string;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001282
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001283 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1284 return NULL;
1285
1286 string = get_token_no_padding (pfile);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001287 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001288 return NULL;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001289
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001290 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1291 return NULL;
1292
1293 return string;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001294}
1295
Neil Booth87062812001-10-20 09:00:53 +00001296/* Destringize IN into a temporary buffer, by removing the first \ of
1297 \" and \\ sequences, and process the result as a #pragma directive. */
1298static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001299destringize_and_run (cpp_reader *pfile, const cpp_string *in)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001300{
1301 const unsigned char *src, *limit;
Neil Booth87062812001-10-20 09:00:53 +00001302 char *dest, *result;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001303
Neil Booth6338b352003-04-23 22:44:06 +00001304 dest = result = alloca (in->len - 1);
1305 src = in->text + 1 + (in->text[0] == 'L');
1306 limit = in->text + in->len - 1;
1307 while (src < limit)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001308 {
1309 /* We know there is a character following the backslash. */
1310 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1311 src++;
1312 *dest++ = *src++;
1313 }
Neil Booth26aea072003-04-19 00:22:51 +00001314 *dest = '\n';
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001315
Neil Booth8128ccc2002-11-18 20:43:40 +00001316 /* Ugh; an awful kludge. We are really not set up to be lexing
1317 tokens when in the middle of a macro expansion. Use a new
1318 context to force cpp_get_token to lex, and so skip_rest_of_line
1319 doesn't go beyond the end of the text. Also, remember the
1320 current lexing position so we can return to it later.
Neil Booth79ba5e32002-09-03 21:55:40 +00001321
Neil Booth8128ccc2002-11-18 20:43:40 +00001322 Something like line-at-a-time lexing should remove the need for
1323 this. */
1324 {
1325 cpp_context *saved_context = pfile->context;
1326 cpp_token *saved_cur_token = pfile->cur_token;
1327 tokenrun *saved_cur_run = pfile->cur_run;
1328
1329 pfile->context = xnew (cpp_context);
1330 pfile->context->macro = 0;
1331 pfile->context->prev = 0;
1332 run_directive (pfile, T_PRAGMA, result, dest - result);
1333 free (pfile->context);
1334 pfile->context = saved_context;
1335 pfile->cur_token = saved_cur_token;
1336 pfile->cur_run = saved_cur_run;
Neil Booth8128ccc2002-11-18 20:43:40 +00001337 }
Neil Booth79ba5e32002-09-03 21:55:40 +00001338
1339 /* See above comment. For the moment, we'd like
1340
1341 token1 _Pragma ("foo") token2
1342
1343 to be output as
1344
1345 token1
1346 # 7 "file.c"
1347 #pragma foo
1348 # 7 "file.c"
1349 token2
1350
1351 Getting the line markers is a little tricky. */
1352 if (pfile->cb.line_change)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001353 pfile->cb.line_change (pfile, pfile->cur_token, false);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001354}
1355
Neil Booth87062812001-10-20 09:00:53 +00001356/* Handle the _Pragma operator. */
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001357void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001358_cpp_do__Pragma (cpp_reader *pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001359{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001360 const cpp_token *string = get__Pragma_string (pfile);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001361
Neil Booth79ba5e32002-09-03 21:55:40 +00001362 if (string)
1363 destringize_and_run (pfile, &string->val.str);
1364 else
John David Anglin0527bc42003-11-01 22:56:54 +00001365 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00001366 "_Pragma takes a parenthesized string literal");
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001367}
1368
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001369/* Ignore #sccs on all systems. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001370static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001371do_sccs (cpp_reader *pfile ATTRIBUTE_UNUSED)
Per Bothner7f2935c1995-03-16 13:59:07 -08001372{
Per Bothner7f2935c1995-03-16 13:59:07 -08001373}
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001374
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001375/* Handle #ifdef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001376static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001377do_ifdef (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +00001378{
Neil Booth93c803682000-10-28 17:59:06 +00001379 int skip = 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001380
Neil Boothcef0d192001-07-26 06:02:47 +00001381 if (! pfile->state.skipping)
Neil Booth93c803682000-10-28 17:59:06 +00001382 {
1383 const cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +00001384
Neil Booth93c803682000-10-28 17:59:06 +00001385 if (node)
Neil Bootha69cbaa2002-07-23 22:57:49 +00001386 {
1387 skip = node->type != NT_MACRO;
1388 _cpp_mark_macro_used (node);
1389 check_eol (pfile);
1390 }
Neil Booth93c803682000-10-28 17:59:06 +00001391 }
1392
1393 push_conditional (pfile, skip, T_IFDEF, 0);
Zack Weinberg168d3732000-03-14 06:34:11 +00001394}
1395
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001396/* Handle #ifndef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001397static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001398do_ifndef (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +00001399{
Neil Booth93c803682000-10-28 17:59:06 +00001400 int skip = 1;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001401 const cpp_hashnode *node = 0;
Zack Weinberg168d3732000-03-14 06:34:11 +00001402
Neil Boothcef0d192001-07-26 06:02:47 +00001403 if (! pfile->state.skipping)
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001404 {
Neil Booth93c803682000-10-28 17:59:06 +00001405 node = lex_macro_node (pfile);
Geoffrey Keatingb43db0b2000-12-02 22:28:44 +00001406
1407 if (node)
Neil Bootha69cbaa2002-07-23 22:57:49 +00001408 {
1409 skip = node->type == NT_MACRO;
1410 _cpp_mark_macro_used (node);
1411 check_eol (pfile);
1412 }
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001413 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001414
Neil Booth93c803682000-10-28 17:59:06 +00001415 push_conditional (pfile, skip, T_IFNDEF, node);
Per Bothner7f2935c1995-03-16 13:59:07 -08001416}
1417
Neil Booth6d18adb2001-07-29 17:27:57 +00001418/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1419 pfile->mi_ind_cmacro so we can handle multiple-include
Kazu Hirata6356f892003-06-12 19:01:08 +00001420 optimizations. If macro expansion occurs in the expression, we
Neil Booth6d18adb2001-07-29 17:27:57 +00001421 cannot treat it as a controlling conditional, since the expansion
1422 could change in the future. That is handled by cpp_get_token. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001423static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001424do_if (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001425{
Neil Booth93c803682000-10-28 17:59:06 +00001426 int skip = 1;
Per Bothner7f2935c1995-03-16 13:59:07 -08001427
Neil Boothcef0d192001-07-26 06:02:47 +00001428 if (! pfile->state.skipping)
Neil Booth87ed1092002-04-28 19:42:54 +00001429 skip = _cpp_parse_expr (pfile) == false;
Neil Booth93c803682000-10-28 17:59:06 +00001430
Neil Booth6d18adb2001-07-29 17:27:57 +00001431 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
Per Bothner7f2935c1995-03-16 13:59:07 -08001432}
1433
Neil Boothb528a072000-11-12 11:46:21 +00001434/* Flip skipping state if appropriate and continue without changing
Zack Weinbergea4a4532000-05-29 16:19:32 +00001435 if_stack; this is so that the error message for missing #endif's
1436 etc. will point to the original #if. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001437static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001438do_else (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001439{
Neil Boothb528a072000-11-12 11:46:21 +00001440 cpp_buffer *buffer = pfile->buffer;
1441 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001442
1443 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001444 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
Neil Booth93c803682000-10-28 17:59:06 +00001445 else
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001446 {
Neil Booth93c803682000-10-28 17:59:06 +00001447 if (ifs->type == T_ELSE)
1448 {
John David Anglin0527bc42003-11-01 22:56:54 +00001449 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1450 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Booth93c803682000-10-28 17:59:06 +00001451 "the conditional began here");
1452 }
Neil Boothb528a072000-11-12 11:46:21 +00001453 ifs->type = T_ELSE;
1454
Neil Boothcef0d192001-07-26 06:02:47 +00001455 /* Skip any future (erroneous) #elses or #elifs. */
1456 pfile->state.skipping = ifs->skip_elses;
1457 ifs->skip_elses = true;
Neil Booth93c803682000-10-28 17:59:06 +00001458
1459 /* Invalidate any controlling macro. */
1460 ifs->mi_cmacro = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001461
Neil Boothcef0d192001-07-26 06:02:47 +00001462 /* Only check EOL if was not originally skipping. */
Phil Edwards909de5d2002-03-22 21:59:04 +00001463 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
Neil Boothcef0d192001-07-26 06:02:47 +00001464 check_eol (pfile);
1465 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001466}
1467
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001468/* Handle a #elif directive by not changing if_stack either. See the
Neil Booth93c803682000-10-28 17:59:06 +00001469 comment above do_else. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001470static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001471do_elif (cpp_reader *pfile)
Zack Weinbergea4a4532000-05-29 16:19:32 +00001472{
Neil Boothb528a072000-11-12 11:46:21 +00001473 cpp_buffer *buffer = pfile->buffer;
1474 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001475
1476 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001477 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
Neil Boothb528a072000-11-12 11:46:21 +00001478 else
Zack Weinbergea4a4532000-05-29 16:19:32 +00001479 {
Neil Boothb528a072000-11-12 11:46:21 +00001480 if (ifs->type == T_ELSE)
1481 {
John David Anglin0527bc42003-11-01 22:56:54 +00001482 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1483 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Boothb528a072000-11-12 11:46:21 +00001484 "the conditional began here");
1485 }
1486 ifs->type = T_ELIF;
1487
Neil Boothcef0d192001-07-26 06:02:47 +00001488 /* Only evaluate this if we aren't skipping elses. During
1489 evaluation, set skipping to false to get lexer warnings. */
1490 if (ifs->skip_elses)
1491 pfile->state.skipping = 1;
1492 else
Neil Boothb528a072000-11-12 11:46:21 +00001493 {
Neil Boothcef0d192001-07-26 06:02:47 +00001494 pfile->state.skipping = 0;
1495 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1496 ifs->skip_elses = ! pfile->state.skipping;
Neil Boothb528a072000-11-12 11:46:21 +00001497 }
Neil Boothcef0d192001-07-26 06:02:47 +00001498
1499 /* Invalidate any controlling macro. */
1500 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001501 }
Zack Weinbergea4a4532000-05-29 16:19:32 +00001502}
1503
Neil Boothcef0d192001-07-26 06:02:47 +00001504/* #endif pops the if stack and resets pfile->state.skipping. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001505static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001506do_endif (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001507{
Neil Boothb528a072000-11-12 11:46:21 +00001508 cpp_buffer *buffer = pfile->buffer;
1509 struct if_stack *ifs = buffer->if_stack;
Per Bothner7f2935c1995-03-16 13:59:07 -08001510
Zack Weinbergea4a4532000-05-29 16:19:32 +00001511 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001512 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
Per Bothner7f2935c1995-03-16 13:59:07 -08001513 else
1514 {
Neil Boothcef0d192001-07-26 06:02:47 +00001515 /* Only check EOL if was not originally skipping. */
Phil Edwards909de5d2002-03-22 21:59:04 +00001516 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
Neil Boothcef0d192001-07-26 06:02:47 +00001517 check_eol (pfile);
1518
Neil Booth93c803682000-10-28 17:59:06 +00001519 /* If potential control macro, we go back outside again. */
1520 if (ifs->next == 0 && ifs->mi_cmacro)
1521 {
Neil Booth6d18adb2001-07-29 17:27:57 +00001522 pfile->mi_valid = true;
Neil Booth93c803682000-10-28 17:59:06 +00001523 pfile->mi_cmacro = ifs->mi_cmacro;
1524 }
1525
Neil Boothb528a072000-11-12 11:46:21 +00001526 buffer->if_stack = ifs->next;
Neil Boothcef0d192001-07-26 06:02:47 +00001527 pfile->state.skipping = ifs->was_skipping;
Neil Booth2a967f32001-05-20 06:26:45 +00001528 obstack_free (&pfile->buffer_ob, ifs);
Per Bothner7f2935c1995-03-16 13:59:07 -08001529 }
Neil Booth93c803682000-10-28 17:59:06 +00001530}
Zack Weinberg041c3192000-07-04 01:58:21 +00001531
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001532/* Push an if_stack entry for a preprocessor conditional, and set
1533 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1534 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1535 we need to check here that we are at the top of the file. */
Zack Weinbergea4a4532000-05-29 16:19:32 +00001536static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001537push_conditional (cpp_reader *pfile, int skip, int type,
1538 const cpp_hashnode *cmacro)
Zack Weinbergea4a4532000-05-29 16:19:32 +00001539{
1540 struct if_stack *ifs;
Neil Boothb528a072000-11-12 11:46:21 +00001541 cpp_buffer *buffer = pfile->buffer;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001542
Neil Booth2a967f32001-05-20 06:26:45 +00001543 ifs = xobnew (&pfile->buffer_ob, struct if_stack);
Neil Booth50410422001-09-15 10:18:03 +00001544 ifs->line = pfile->directive_line;
Neil Boothb528a072000-11-12 11:46:21 +00001545 ifs->next = buffer->if_stack;
Neil Boothcef0d192001-07-26 06:02:47 +00001546 ifs->skip_elses = pfile->state.skipping || !skip;
1547 ifs->was_skipping = pfile->state.skipping;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001548 ifs->type = type;
Neil Booth6d18adb2001-07-29 17:27:57 +00001549 /* This condition is effectively a test for top-of-file. */
1550 if (pfile->mi_valid && pfile->mi_cmacro == 0)
Neil Booth93c803682000-10-28 17:59:06 +00001551 ifs->mi_cmacro = cmacro;
1552 else
1553 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001554
Neil Boothcef0d192001-07-26 06:02:47 +00001555 pfile->state.skipping = skip;
Neil Boothb528a072000-11-12 11:46:21 +00001556 buffer->if_stack = ifs;
Zack Weinberg7061aa51998-12-15 11:09:16 +00001557}
1558
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001559/* Read the tokens of the answer into the macro pool, in a directive
1560 of type TYPE. Only commit the memory if we intend it as permanent
1561 storage, i.e. the #assert case. Returns 0 on success, and sets
1562 ANSWERP to point to the answer. */
Neil Booth93c803682000-10-28 17:59:06 +00001563static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001564parse_answer (cpp_reader *pfile, struct answer **answerp, int type)
Zack Weinberg041c3192000-07-04 01:58:21 +00001565{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001566 const cpp_token *paren;
Neil Booth93c803682000-10-28 17:59:06 +00001567 struct answer *answer;
Neil Booth8c3b2692001-09-30 10:03:11 +00001568 unsigned int acount;
Zack Weinberg041c3192000-07-04 01:58:21 +00001569
Neil Booth93c803682000-10-28 17:59:06 +00001570 /* In a conditional, it is legal to not have an open paren. We
1571 should save the following token in this case. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001572 paren = cpp_get_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001573
1574 /* If not a paren, see if we're OK. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001575 if (paren->type != CPP_OPEN_PAREN)
Zack Weinberg041c3192000-07-04 01:58:21 +00001576 {
Neil Booth93c803682000-10-28 17:59:06 +00001577 /* In a conditional no answer is a test for any answer. It
1578 could be followed by any token. */
1579 if (type == T_IF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001580 {
1581 _cpp_backup_tokens (pfile, 1);
1582 return 0;
1583 }
Neil Booth93c803682000-10-28 17:59:06 +00001584
1585 /* #unassert with no answer is valid - it removes all answers. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001586 if (type == T_UNASSERT && paren->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +00001587 return 0;
1588
John David Anglin0527bc42003-11-01 22:56:54 +00001589 cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate");
Neil Booth93c803682000-10-28 17:59:06 +00001590 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001591 }
1592
Neil Booth8c3b2692001-09-30 10:03:11 +00001593 for (acount = 0;; acount++)
Zack Weinberg041c3192000-07-04 01:58:21 +00001594 {
Neil Booth8c3b2692001-09-30 10:03:11 +00001595 size_t room_needed;
1596 const cpp_token *token = cpp_get_token (pfile);
1597 cpp_token *dest;
Zack Weinberg041c3192000-07-04 01:58:21 +00001598
Neil Booth93c803682000-10-28 17:59:06 +00001599 if (token->type == CPP_CLOSE_PAREN)
1600 break;
Zack Weinberg041c3192000-07-04 01:58:21 +00001601
1602 if (token->type == CPP_EOF)
1603 {
John David Anglin0527bc42003-11-01 22:56:54 +00001604 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
Neil Booth93c803682000-10-28 17:59:06 +00001605 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001606 }
Neil Booth8c3b2692001-09-30 10:03:11 +00001607
1608 /* struct answer includes the space for one token. */
1609 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1610
1611 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1612 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1613
1614 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1615 *dest = *token;
1616
1617 /* Drop whitespace at start, for answer equivalence purposes. */
1618 if (acount == 0)
1619 dest->flags &= ~PREV_WHITE;
Zack Weinberg041c3192000-07-04 01:58:21 +00001620 }
1621
Neil Booth8c3b2692001-09-30 10:03:11 +00001622 if (acount == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001623 {
John David Anglin0527bc42003-11-01 22:56:54 +00001624 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
Neil Booth93c803682000-10-28 17:59:06 +00001625 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001626 }
1627
Neil Booth8c3b2692001-09-30 10:03:11 +00001628 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1629 answer->count = acount;
1630 answer->next = NULL;
Neil Booth93c803682000-10-28 17:59:06 +00001631 *answerp = answer;
Zack Weinberg041c3192000-07-04 01:58:21 +00001632
Neil Booth93c803682000-10-28 17:59:06 +00001633 return 0;
1634}
1635
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001636/* Parses an assertion directive of type TYPE, returning a pointer to
1637 the hash node of the predicate, or 0 on error. If an answer was
1638 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
Neil Booth93c803682000-10-28 17:59:06 +00001639static cpp_hashnode *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001640parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
Neil Booth93c803682000-10-28 17:59:06 +00001641{
1642 cpp_hashnode *result = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001643 const cpp_token *predicate;
Neil Booth93c803682000-10-28 17:59:06 +00001644
1645 /* We don't expand predicates or answers. */
1646 pfile->state.prevent_expansion++;
1647
Neil Booth93c803682000-10-28 17:59:06 +00001648 *answerp = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001649 predicate = cpp_get_token (pfile);
1650 if (predicate->type == CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +00001651 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001652 else if (predicate->type != CPP_NAME)
John David Anglin0527bc42003-11-01 22:56:54 +00001653 cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier");
Neil Booth93c803682000-10-28 17:59:06 +00001654 else if (parse_answer (pfile, answerp, type) == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001655 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001656 unsigned int len = NODE_LEN (predicate->val.node);
Neil Booth93c803682000-10-28 17:59:06 +00001657 unsigned char *sym = alloca (len + 1);
1658
1659 /* Prefix '#' to get it out of macro namespace. */
1660 sym[0] = '#';
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001661 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
Neil Booth93c803682000-10-28 17:59:06 +00001662 result = cpp_lookup (pfile, sym, len + 1);
Zack Weinberg041c3192000-07-04 01:58:21 +00001663 }
1664
Neil Booth93c803682000-10-28 17:59:06 +00001665 pfile->state.prevent_expansion--;
1666 return result;
Zack Weinberg041c3192000-07-04 01:58:21 +00001667}
1668
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001669/* Returns a pointer to the pointer to CANDIDATE in the answer chain,
Zack Weinberg041c3192000-07-04 01:58:21 +00001670 or a pointer to NULL if the answer is not in the chain. */
Neil Booth93c803682000-10-28 17:59:06 +00001671static struct answer **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001672find_answer (cpp_hashnode *node, const struct answer *candidate)
Zack Weinberg041c3192000-07-04 01:58:21 +00001673{
Neil Booth93c803682000-10-28 17:59:06 +00001674 unsigned int i;
Zack Weinberg041c3192000-07-04 01:58:21 +00001675 struct answer **result;
1676
1677 for (result = &node->value.answers; *result; result = &(*result)->next)
Neil Booth93c803682000-10-28 17:59:06 +00001678 {
1679 struct answer *answer = *result;
1680
1681 if (answer->count == candidate->count)
1682 {
1683 for (i = 0; i < answer->count; i++)
1684 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1685 break;
1686
1687 if (i == answer->count)
1688 break;
1689 }
1690 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001691
1692 return result;
1693}
1694
Neil Booth93c803682000-10-28 17:59:06 +00001695/* Test an assertion within a preprocessor conditional. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +00001696 nonzero on failure, zero on success. On success, the result of
Hans-Peter Nilsson24026452002-11-29 22:41:04 +00001697 the test is written into VALUE, otherwise the value 0. */
Neil Booth93c803682000-10-28 17:59:06 +00001698int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001699_cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
Neil Booth93c803682000-10-28 17:59:06 +00001700{
1701 struct answer *answer;
1702 cpp_hashnode *node;
1703
1704 node = parse_assertion (pfile, &answer, T_IF);
Hans-Peter Nilsson24026452002-11-29 22:41:04 +00001705
1706 /* For recovery, an erroneous assertion expression is handled as a
1707 failing assertion. */
1708 *value = 0;
1709
Neil Booth93c803682000-10-28 17:59:06 +00001710 if (node)
1711 *value = (node->type == NT_ASSERTION &&
1712 (answer == 0 || *find_answer (node, answer) != 0));
Neil Booth91318902002-05-26 18:42:21 +00001713 else if (pfile->cur_token[-1].type == CPP_EOF)
1714 _cpp_backup_tokens (pfile, 1);
Neil Booth93c803682000-10-28 17:59:06 +00001715
1716 /* We don't commit the memory for the answer - it's temporary only. */
1717 return node == 0;
1718}
1719
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001720/* Handle #assert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001721static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001722do_assert (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001723{
Zack Weinberg041c3192000-07-04 01:58:21 +00001724 struct answer *new_answer;
1725 cpp_hashnode *node;
Kazu Hiratadf383482002-05-22 22:02:16 +00001726
Neil Booth93c803682000-10-28 17:59:06 +00001727 node = parse_assertion (pfile, &new_answer, T_ASSERT);
Zack Weinberg041c3192000-07-04 01:58:21 +00001728 if (node)
1729 {
Neil Booth93c803682000-10-28 17:59:06 +00001730 /* Place the new answer in the answer list. First check there
1731 is not a duplicate. */
Zack Weinberg041c3192000-07-04 01:58:21 +00001732 new_answer->next = 0;
Neil Booth93c803682000-10-28 17:59:06 +00001733 if (node->type == NT_ASSERTION)
Zack Weinberg041c3192000-07-04 01:58:21 +00001734 {
Neil Booth93c803682000-10-28 17:59:06 +00001735 if (*find_answer (node, new_answer))
1736 {
John David Anglin0527bc42003-11-01 22:56:54 +00001737 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
Neil Boothebef4e82002-04-14 18:42:47 +00001738 NODE_NAME (node) + 1);
Neil Booth93c803682000-10-28 17:59:06 +00001739 return;
1740 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001741 new_answer->next = node->value.answers;
1742 }
Neil Booth8c3b2692001-09-30 10:03:11 +00001743
Neil Booth93c803682000-10-28 17:59:06 +00001744 node->type = NT_ASSERTION;
Zack Weinberg041c3192000-07-04 01:58:21 +00001745 node->value.answers = new_answer;
Neil Booth8c3b2692001-09-30 10:03:11 +00001746 BUFF_FRONT (pfile->a_buff) += (sizeof (struct answer)
1747 + (new_answer->count - 1)
1748 * sizeof (cpp_token));
1749 check_eol (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +00001750 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001751}
Zack Weinberg7061aa51998-12-15 11:09:16 +00001752
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001753/* Handle #unassert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001754static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001755do_unassert (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001756{
Zack Weinberg041c3192000-07-04 01:58:21 +00001757 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00001758 struct answer *answer;
Kazu Hiratadf383482002-05-22 22:02:16 +00001759
Neil Booth93c803682000-10-28 17:59:06 +00001760 node = parse_assertion (pfile, &answer, T_UNASSERT);
1761 /* It isn't an error to #unassert something that isn't asserted. */
1762 if (node && node->type == NT_ASSERTION)
Zack Weinberg7061aa51998-12-15 11:09:16 +00001763 {
Zack Weinberg041c3192000-07-04 01:58:21 +00001764 if (answer)
Neil Booth93c803682000-10-28 17:59:06 +00001765 {
1766 struct answer **p = find_answer (node, answer), *temp;
1767
1768 /* Remove the answer from the list. */
1769 temp = *p;
1770 if (temp)
1771 *p = temp->next;
1772
1773 /* Did we free the last answer? */
1774 if (node->value.answers == 0)
1775 node->type = NT_VOID;
Neil Booth8c3b2692001-09-30 10:03:11 +00001776
1777 check_eol (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001778 }
1779 else
1780 _cpp_free_definition (node);
Zack Weinberg7061aa51998-12-15 11:09:16 +00001781 }
Neil Booth93c803682000-10-28 17:59:06 +00001782
1783 /* We don't commit the memory for the answer - it's temporary only. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001784}
Per Bothner7f2935c1995-03-16 13:59:07 -08001785
Zack Weinberg45b966d2000-03-13 22:01:08 +00001786/* These are for -D, -U, -A. */
1787
1788/* Process the string STR as if it appeared as the body of a #define.
1789 If STR is just an identifier, define it with value 1.
1790 If STR has anything after the identifier, then it should
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001791 be identifier=definition. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001792void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001793cpp_define (cpp_reader *pfile, const char *str)
Zack Weinberg45b966d2000-03-13 22:01:08 +00001794{
1795 char *buf, *p;
1796 size_t count;
1797
Kazu Hiratadf383482002-05-22 22:02:16 +00001798 /* Copy the entire option so we can modify it.
Zack Weinberg45b966d2000-03-13 22:01:08 +00001799 Change the first "=" in the string to a space. If there is none,
Neil Booth86368122000-10-31 23:34:59 +00001800 tack " 1" on the end. */
1801
Neil Booth86368122000-10-31 23:34:59 +00001802 count = strlen (str);
Kaveh R. Ghazi703ad42b2003-07-19 14:47:15 +00001803 buf = alloca (count + 3);
Neil Booth86368122000-10-31 23:34:59 +00001804 memcpy (buf, str, count);
1805
1806 p = strchr (str, '=');
Zack Weinberg45b966d2000-03-13 22:01:08 +00001807 if (p)
Neil Booth86368122000-10-31 23:34:59 +00001808 buf[p - str] = ' ';
Zack Weinberg45b966d2000-03-13 22:01:08 +00001809 else
1810 {
Neil Booth86368122000-10-31 23:34:59 +00001811 buf[count++] = ' ';
1812 buf[count++] = '1';
Zack Weinberg45b966d2000-03-13 22:01:08 +00001813 }
Neil Booth26aea072003-04-19 00:22:51 +00001814 buf[count] = '\n';
Zack Weinberg45b966d2000-03-13 22:01:08 +00001815
Neil Booth29401c32001-08-22 20:37:20 +00001816 run_directive (pfile, T_DEFINE, buf, count);
Zack Weinberg2c8f0512000-08-29 18:37:37 +00001817}
1818
Neil Boothad2a0842000-12-17 00:13:54 +00001819/* Slight variant of the above for use by initialize_builtins. */
Zack Weinberg2c8f0512000-08-29 18:37:37 +00001820void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001821_cpp_define_builtin (cpp_reader *pfile, const char *str)
Zack Weinberg2c8f0512000-08-29 18:37:37 +00001822{
Neil Booth26aea072003-04-19 00:22:51 +00001823 size_t len = strlen (str);
1824 char *buf = alloca (len + 1);
1825 memcpy (buf, str, len);
1826 buf[len] = '\n';
1827 run_directive (pfile, T_DEFINE, buf, len);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001828}
1829
1830/* Process MACRO as if it appeared as the body of an #undef. */
1831void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001832cpp_undef (cpp_reader *pfile, const char *macro)
Zack Weinberg45b966d2000-03-13 22:01:08 +00001833{
Neil Booth26aea072003-04-19 00:22:51 +00001834 size_t len = strlen (macro);
1835 char *buf = alloca (len + 1);
1836 memcpy (buf, macro, len);
1837 buf[len] = '\n';
1838 run_directive (pfile, T_UNDEF, buf, len);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001839}
1840
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001841/* Process the string STR as if it appeared as the body of a #assert. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001842void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001843cpp_assert (cpp_reader *pfile, const char *str)
Zack Weinberg45b966d2000-03-13 22:01:08 +00001844{
Neil Booth86368122000-10-31 23:34:59 +00001845 handle_assertion (pfile, str, T_ASSERT);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001846}
1847
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001848/* Process STR as if it appeared as the body of an #unassert. */
Zack Weinberg0b22d651999-03-15 18:42:46 +00001849void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001850cpp_unassert (cpp_reader *pfile, const char *str)
Zack Weinberg0b22d651999-03-15 18:42:46 +00001851{
Neil Booth86368122000-10-31 23:34:59 +00001852 handle_assertion (pfile, str, T_UNASSERT);
Kazu Hiratadf383482002-05-22 22:02:16 +00001853}
Zack Weinberg0b22d651999-03-15 18:42:46 +00001854
Neil Booth86368122000-10-31 23:34:59 +00001855/* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1856static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001857handle_assertion (cpp_reader *pfile, const char *str, int type)
Neil Booth86368122000-10-31 23:34:59 +00001858{
1859 size_t count = strlen (str);
1860 const char *p = strchr (str, '=');
1861
Neil Booth26aea072003-04-19 00:22:51 +00001862 /* Copy the entire option so we can modify it. Change the first
1863 "=" in the string to a '(', and tack a ')' on the end. */
Kaveh R. Ghazi703ad42b2003-07-19 14:47:15 +00001864 char *buf = alloca (count + 2);
Neil Booth26aea072003-04-19 00:22:51 +00001865
1866 memcpy (buf, str, count);
Neil Booth86368122000-10-31 23:34:59 +00001867 if (p)
1868 {
Neil Booth86368122000-10-31 23:34:59 +00001869 buf[p - str] = '(';
1870 buf[count++] = ')';
Neil Booth86368122000-10-31 23:34:59 +00001871 }
Neil Booth26aea072003-04-19 00:22:51 +00001872 buf[count] = '\n';
1873 str = buf;
Neil Booth86368122000-10-31 23:34:59 +00001874
Neil Booth29401c32001-08-22 20:37:20 +00001875 run_directive (pfile, type, str, count);
Neil Booth86368122000-10-31 23:34:59 +00001876}
1877
Neil Booth7e96d762001-01-13 01:00:01 +00001878/* The number of errors for a given reader. */
1879unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001880cpp_errors (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00001881{
1882 return pfile->errors;
1883}
1884
1885/* The options structure. */
1886cpp_options *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001887cpp_get_options (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00001888{
1889 return &pfile->opts;
1890}
1891
1892/* The callbacks structure. */
1893cpp_callbacks *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001894cpp_get_callbacks (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00001895{
1896 return &pfile->cb;
1897}
1898
1899/* Copy the given callbacks structure to our own. */
1900void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001901cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
Neil Booth7e96d762001-01-13 01:00:01 +00001902{
1903 pfile->cb = *cb;
1904}
1905
Zack Weinbergc6e83802004-06-05 20:58:06 +00001906/* The dependencies structure. (Creates one if it hasn't already been.) */
1907struct deps *
1908cpp_get_deps (cpp_reader *pfile)
1909{
1910 if (!pfile->deps)
1911 pfile->deps = deps_init ();
1912 return pfile->deps;
1913}
1914
Neil Bootheb1f4d92000-12-18 19:00:26 +00001915/* Push a new buffer on the buffer stack. Returns the new buffer; it
1916 doesn't fail. It does not generate a file change call back; that
1917 is the responsibility of the caller. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00001918cpp_buffer *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001919cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
Per Bothner40de9f72003-10-02 07:30:34 +00001920 int from_stage3)
Zack Weinbergc71f8352000-07-05 05:33:57 +00001921{
Neil Booth2a967f32001-05-20 06:26:45 +00001922 cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
Neil Booth93c803682000-10-28 17:59:06 +00001923
Neil Boothfde84342001-08-06 21:07:41 +00001924 /* Clears, amongst other things, if_stack and mi_cmacro. */
1925 memset (new, 0, sizeof (cpp_buffer));
Neil Boothad2a0842000-12-17 00:13:54 +00001926
Neil Booth26aea072003-04-19 00:22:51 +00001927 new->next_line = new->buf = buffer;
Neil Boothfde84342001-08-06 21:07:41 +00001928 new->rlimit = buffer + len;
Neil Booth26aea072003-04-19 00:22:51 +00001929 new->from_stage3 = from_stage3;
Neil Booth3cf35932000-12-05 23:42:43 +00001930 new->prev = pfile->buffer;
Neil Booth26aea072003-04-19 00:22:51 +00001931 new->need_line = true;
Neil Booth0bda4762000-12-11 07:45:16 +00001932
Neil Booth3cf35932000-12-05 23:42:43 +00001933 pfile->buffer = new;
Eric Christophercf551fb2004-01-16 22:37:49 +00001934
Zack Weinbergc71f8352000-07-05 05:33:57 +00001935 return new;
1936}
1937
Neil Boothaf0d16c2002-04-22 17:48:02 +00001938/* Pops a single buffer, with a file change call-back if appropriate.
1939 Then pushes the next -include file, if any remain. */
Neil Boothef6e9582001-08-04 12:01:59 +00001940void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001941_cpp_pop_buffer (cpp_reader *pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00001942{
Neil Boothfde84342001-08-06 21:07:41 +00001943 cpp_buffer *buffer = pfile->buffer;
Neil Booth8f9b4002003-07-29 22:26:13 +00001944 struct _cpp_file *inc = buffer->file;
Neil Boothad2a0842000-12-17 00:13:54 +00001945 struct if_stack *ifs;
Zack Weinbergc71f8352000-07-05 05:33:57 +00001946
Neil Boothfde84342001-08-06 21:07:41 +00001947 /* Walk back up the conditional stack till we reach its level at
1948 entry to this file, issuing error messages. */
1949 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
John David Anglin0527bc42003-11-01 22:56:54 +00001950 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Boothfde84342001-08-06 21:07:41 +00001951 "unterminated #%s", dtable[ifs->type].name);
1952
Neil Booth97293892001-09-14 22:04:46 +00001953 /* In case of a missing #endif. */
Neil Booth67821e32001-08-05 17:31:25 +00001954 pfile->state.skipping = 0;
Neil Booth29401c32001-08-22 20:37:20 +00001955
Neil Boothaf0d16c2002-04-22 17:48:02 +00001956 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
Neil Booth29401c32001-08-22 20:37:20 +00001957 pfile->buffer = buffer->prev;
1958
Neil Booth26aea072003-04-19 00:22:51 +00001959 free (buffer->notes);
1960
Neil Boothaf0d16c2002-04-22 17:48:02 +00001961 /* Free the buffer object now; we may want to push a new buffer
1962 in _cpp_push_next_include_file. */
1963 obstack_free (&pfile->buffer_ob, buffer);
Neil Booth29401c32001-08-22 20:37:20 +00001964
Neil Boothaf0d16c2002-04-22 17:48:02 +00001965 if (inc)
1966 {
1967 _cpp_pop_file_buffer (pfile, inc);
1968
Per Bothner40de9f72003-10-02 07:30:34 +00001969 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
Neil Boothaf0d16c2002-04-22 17:48:02 +00001970 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00001971}
1972
Kazu Hirata05713b82002-09-15 18:24:08 +00001973/* Enter all recognized directives in the hash table. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00001974void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001975_cpp_init_directives (cpp_reader *pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00001976{
Neil Booth766ee682001-01-29 19:20:12 +00001977 unsigned int i;
Neil Booth93c803682000-10-28 17:59:06 +00001978 cpp_hashnode *node;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001979
John David Anglin37b85242001-03-02 01:11:50 +00001980 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
Neil Booth93c803682000-10-28 17:59:06 +00001981 {
Neil Booth766ee682001-01-29 19:20:12 +00001982 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
Zack Weinberg4977bab2002-12-16 18:23:00 +00001983 node->is_directive = 1;
1984 node->directive_index = i;
Neil Booth93c803682000-10-28 17:59:06 +00001985 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00001986}