blob: 7e1167de0b8b67f5c435aee31f5c55b302e909bc [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 Hirata31c3e632005-02-14 14:43:56 +00003 1999, 2000, 2001, 2002, 2003, 2004, 2005 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
Kelley Cook200031d2005-06-29 02:34:39 +000020Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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
Zack Weinberg88ae23e2000-03-08 23:35:19 +000029/* Stack of conditionals currently in progress
30 (including both successful and failing conditionals). */
Zack Weinberg88ae23e2000-03-08 23:35:19 +000031struct if_stack
32{
33 struct if_stack *next;
Neil Booth50410422001-09-15 10:18:03 +000034 unsigned int line; /* Line where condition started. */
Neil Booth93c803682000-10-28 17:59:06 +000035 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
Neil Boothcef0d192001-07-26 06:02:47 +000036 bool skip_elses; /* Can future #else / #elif be skipped? */
37 bool was_skipping; /* If were skipping on entry. */
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000038 int type; /* Most recent conditional for diagnostics. */
Zack Weinberg88ae23e2000-03-08 23:35:19 +000039};
Zack Weinberg88ae23e2000-03-08 23:35:19 +000040
Neil Bootha5da89c2001-10-14 17:44:00 +000041/* Contains a registered pragma or pragma namespace. */
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000042typedef void (*pragma_cb) (cpp_reader *);
Neil Bootha5da89c2001-10-14 17:44:00 +000043struct pragma_entry
44{
45 struct pragma_entry *next;
Neil Booth4b115ff2001-10-14 23:04:13 +000046 const cpp_hashnode *pragma; /* Name and length. */
Zack Weinberg21b11492004-09-09 19:16:56 +000047 bool is_nspace;
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +000048 bool allow_expansion;
Zack Weinberg21b11492004-09-09 19:16:56 +000049 bool is_internal;
Neil Bootha5da89c2001-10-14 17:44:00 +000050 union {
51 pragma_cb handler;
52 struct pragma_entry *space;
53 } u;
54};
55
Neil Booth93c803682000-10-28 17:59:06 +000056/* Values for the origin field of struct directive. KANDR directives
57 come from traditional (K&R) C. STDC89 directives come from the
58 1989 C standard. EXTENSION directives are extensions. */
59#define KANDR 0
60#define STDC89 1
61#define EXTENSION 2
62
63/* Values for the flags field of struct directive. COND indicates a
64 conditional; IF_COND an opening conditional. INCL means to treat
65 "..." and <...> as q-char and h-char sequences respectively. IN_I
66 means this directive should be handled even if -fpreprocessed is in
Neil Booth1a769162002-06-11 05:36:17 +000067 effect (these are the directives with callback hooks).
68
Neil Boothd97371e2002-06-18 06:27:40 +000069 EXPAND is set on directives that are always macro-expanded. */
Neil Booth93c803682000-10-28 17:59:06 +000070#define COND (1 << 0)
71#define IF_COND (1 << 1)
72#define INCL (1 << 2)
73#define IN_I (1 << 3)
Neil Booth1a769162002-06-11 05:36:17 +000074#define EXPAND (1 << 4)
Neil Booth93c803682000-10-28 17:59:06 +000075
76/* Defines one #-directive, including how to handle it. */
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000077typedef void (*directive_handler) (cpp_reader *);
Neil Booth93c803682000-10-28 17:59:06 +000078typedef struct directive directive;
79struct directive
80{
81 directive_handler handler; /* Function to handle directive. */
Neil Booth562a5c22002-04-21 18:46:42 +000082 const uchar *name; /* Name of directive. */
Neil Booth93c803682000-10-28 17:59:06 +000083 unsigned short length; /* Length of name. */
84 unsigned char origin; /* Origin of directive. */
85 unsigned char flags; /* Flags describing this directive. */
86};
87
Zack Weinberg1316f1f2000-02-06 07:53:50 +000088/* Forward declarations. */
89
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000090static void skip_rest_of_line (cpp_reader *);
91static void check_eol (cpp_reader *);
92static void start_directive (cpp_reader *);
93static void prepare_directive_trad (cpp_reader *);
94static void end_directive (cpp_reader *, int);
95static void directive_diagnostics (cpp_reader *, const directive *, int);
96static void run_directive (cpp_reader *, int, const char *, size_t);
97static char *glue_header_name (cpp_reader *);
98static const char *parse_include (cpp_reader *, int *);
99static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
100static unsigned int read_flag (cpp_reader *, unsigned int);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000101static int strtoul_for_line (const uchar *, unsigned int, unsigned long *);
102static void do_diagnostic (cpp_reader *, int, int);
103static cpp_hashnode *lex_macro_node (cpp_reader *);
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000104static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000105static void do_include_common (cpp_reader *, enum include_type);
106static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
107 const cpp_hashnode *);
108static struct pragma_entry *insert_pragma_entry (cpp_reader *,
109 struct pragma_entry **,
110 const cpp_hashnode *,
Zack Weinberg21b11492004-09-09 19:16:56 +0000111 pragma_cb,
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +0000112 bool, bool);
Zack Weinberg21b11492004-09-09 19:16:56 +0000113static void register_pragma (cpp_reader *, const char *, const char *,
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +0000114 pragma_cb, bool, bool);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000115static int count_registered_pragmas (struct pragma_entry *);
116static char ** save_registered_pragmas (struct pragma_entry *, char **);
117static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
118 char **);
119static void do_pragma_once (cpp_reader *);
120static void do_pragma_poison (cpp_reader *);
121static void do_pragma_system_header (cpp_reader *);
122static void do_pragma_dependency (cpp_reader *);
123static void do_linemarker (cpp_reader *);
124static const cpp_token *get_token_no_padding (cpp_reader *);
125static const cpp_token *get__Pragma_string (cpp_reader *);
126static void destringize_and_run (cpp_reader *, const cpp_string *);
127static int parse_answer (cpp_reader *, struct answer **, int);
128static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
129static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
130static void handle_assertion (cpp_reader *, const char *, int);
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +0000131
Zack Weinberg168d3732000-03-14 06:34:11 +0000132/* This is the table of directive handlers. It is ordered by
133 frequency of occurrence; the numbers at the end are directive
134 counts from all the source code I have lying around (egcs and libc
135 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
Neil Booth93c803682000-10-28 17:59:06 +0000136 pcmcia-cs-3.0.9). This is no longer important as directive lookup
137 is now O(1). All extensions other than #warning and #include_next
138 are deprecated. The name is where the extension appears to have
139 come from. */
Jeff Lawe9a25f71997-11-02 14:19:36 -0700140
Neil Booth93c803682000-10-28 17:59:06 +0000141#define DIRECTIVE_TABLE \
142D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000143D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000144D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
145D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
Neil Booth1a769162002-06-11 05:36:17 +0000146D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000147D(else, T_ELSE, KANDR, COND) /* 9863 */ \
148D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
149D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
Neil Booth1a769162002-06-11 05:36:17 +0000150D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
151D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000152D(error, T_ERROR, STDC89, 0) /* 475 */ \
153D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
154D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000155D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000156D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
Neil Boothd97371e2002-06-18 06:27:40 +0000157D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
Neil Booth93c803682000-10-28 17:59:06 +0000158D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
159D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
Zack Weinberg1ed17cd2005-05-12 18:31:38 +0000160D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */
161
162/* #sccs is synonymous with #ident. */
163#define do_sccs do_ident
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000164
Zack Weinberg168d3732000-03-14 06:34:11 +0000165/* Use the table to generate a series of prototypes, an enum for the
166 directive names, and an array of directive handlers. */
167
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000168#define D(name, t, o, f) static void do_##name (cpp_reader *);
Zack Weinberg168d3732000-03-14 06:34:11 +0000169DIRECTIVE_TABLE
170#undef D
171
Zack Weinberg041c3192000-07-04 01:58:21 +0000172#define D(n, tag, o, f) tag,
Zack Weinberg168d3732000-03-14 06:34:11 +0000173enum
174{
175 DIRECTIVE_TABLE
176 N_DIRECTIVES
Per Bothner7f2935c1995-03-16 13:59:07 -0800177};
Zack Weinberg168d3732000-03-14 06:34:11 +0000178#undef D
179
Zack Weinberg041c3192000-07-04 01:58:21 +0000180#define D(name, t, origin, flags) \
Kaveh R. Ghazi9a238582003-06-16 19:14:22 +0000181{ do_##name, (const uchar *) #name, \
182 sizeof #name - 1, origin, flags },
Neil Booth93c803682000-10-28 17:59:06 +0000183static const directive dtable[] =
Zack Weinberg168d3732000-03-14 06:34:11 +0000184{
185DIRECTIVE_TABLE
186};
187#undef D
188#undef DIRECTIVE_TABLE
Per Bothner7f2935c1995-03-16 13:59:07 -0800189
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000190/* Wrapper struct directive for linemarkers.
191 The origin is more or less true - the original K+R cpp
192 did use this notation in its preprocessed output. */
193static const directive linemarker_dir =
194{
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;
Zack Weinberg21b11492004-09-09 19:16:56 +0000230 pfile->directive_result.type = CPP_PADDING;
Neil Booth7f2f1a62000-11-14 18:32:06 +0000231
Neil Booth93c803682000-10-28 17:59:06 +0000232 /* Some handlers need the position of the # for diagnostics. */
Per Bothner500bee02004-04-22 19:22:27 -0700233 pfile->directive_line = pfile->line_table->highest_line;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000234}
235
236/* Called when leaving a directive, _Pragma or command-line directive. */
237static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000238end_directive (cpp_reader *pfile, int skip_line)
Neil Boothfe6c2db2000-11-15 19:25:22 +0000239{
Neil Booth1a769162002-06-11 05:36:17 +0000240 if (CPP_OPTION (pfile, traditional))
241 {
Neil Boothd97371e2002-06-18 06:27:40 +0000242 /* Revert change of prepare_directive_trad. */
243 pfile->state.prevent_expansion--;
244
Neil Boothb66377c2002-06-13 21:16:00 +0000245 if (pfile->directive != &dtable[T_DEFINE])
Neil Booth1a769162002-06-11 05:36:17 +0000246 _cpp_remove_overlay (pfile);
247 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000248 /* We don't skip for an assembler #. */
Neil Boothb66377c2002-06-13 21:16:00 +0000249 else if (skip_line)
Neil Booth67821e32001-08-05 17:31:25 +0000250 {
251 skip_rest_of_line (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +0000252 if (!pfile->keep_tokens)
253 {
254 pfile->cur_run = &pfile->base_run;
255 pfile->cur_token = pfile->base_run.base;
256 }
Neil Booth67821e32001-08-05 17:31:25 +0000257 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000258
259 /* Restore state. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000260 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
261 pfile->state.in_directive = 0;
Neil Boothd97371e2002-06-18 06:27:40 +0000262 pfile->state.in_expression = 0;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000263 pfile->state.angled_headers = 0;
264 pfile->directive = 0;
265}
266
Neil Booth1a769162002-06-11 05:36:17 +0000267/* Prepare to handle the directive in pfile->directive. */
268static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000269prepare_directive_trad (cpp_reader *pfile)
Neil Booth1a769162002-06-11 05:36:17 +0000270{
Neil Booth951a0762002-06-27 06:01:58 +0000271 if (pfile->directive != &dtable[T_DEFINE])
Neil Booth1a769162002-06-11 05:36:17 +0000272 {
Neil Boothb66377c2002-06-13 21:16:00 +0000273 bool no_expand = (pfile->directive
274 && ! (pfile->directive->flags & EXPAND));
Neil Booth974c43f2002-06-13 06:25:28 +0000275 bool was_skipping = pfile->state.skipping;
Neil Booth1a769162002-06-11 05:36:17 +0000276
Neil Boothd97371e2002-06-18 06:27:40 +0000277 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
278 || pfile->directive == &dtable[T_ELIF]);
Neil Booth45f24922003-12-12 07:00:29 +0000279 if (pfile->state.in_expression)
280 pfile->state.skipping = false;
281
Neil Booth1a769162002-06-11 05:36:17 +0000282 if (no_expand)
283 pfile->state.prevent_expansion++;
Zack Weinberg43839642003-07-13 17:34:18 +0000284 _cpp_scan_out_logical_line (pfile, NULL);
Neil Booth1a769162002-06-11 05:36:17 +0000285 if (no_expand)
286 pfile->state.prevent_expansion--;
Neil Booth45f24922003-12-12 07:00:29 +0000287
Neil Booth974c43f2002-06-13 06:25:28 +0000288 pfile->state.skipping = was_skipping;
Neil Booth1a769162002-06-11 05:36:17 +0000289 _cpp_overlay_buffer (pfile, pfile->out.base,
290 pfile->out.cur - pfile->out.base);
291 }
Neil Boothd97371e2002-06-18 06:27:40 +0000292
293 /* Stop ISO C from expanding anything. */
294 pfile->state.prevent_expansion++;
Neil Booth1a769162002-06-11 05:36:17 +0000295}
296
Kazu Hiratada7d8302002-09-22 02:03:17 +0000297/* Output diagnostics for a directive DIR. INDENTED is nonzero if
Neil Booth18a9d8f2001-09-16 11:23:56 +0000298 the '#' was indented. */
Neil Booth18a9d8f2001-09-16 11:23:56 +0000299static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000300directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000301{
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000302 /* Issue -pedantic warnings for extensions. */
303 if (CPP_PEDANTIC (pfile)
304 && ! pfile->state.skipping
305 && dir->origin == EXTENSION)
John David Anglin0527bc42003-11-01 22:56:54 +0000306 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000307
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000308 /* Traditionally, a directive is ignored unless its # is in
309 column 1. Therefore in code intended to work with K+R
310 compilers, directives added by C89 must have their #
311 indented, and directives present in traditional C must not.
312 This is true even of directives in skipped conditional
313 blocks. #elif cannot be used at all. */
314 if (CPP_WTRADITIONAL (pfile))
315 {
316 if (dir == &dtable[T_ELIF])
John David Anglin0527bc42003-11-01 22:56:54 +0000317 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000318 "suggest not using #elif in traditional C");
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000319 else if (indented && dir->origin == KANDR)
John David Anglin0527bc42003-11-01 22:56:54 +0000320 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000321 "traditional C ignores #%s with the # indented",
322 dir->name);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000323 else if (!indented && dir->origin != KANDR)
John David Anglin0527bc42003-11-01 22:56:54 +0000324 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000325 "suggest hiding #%s from traditional C with an indented #",
326 dir->name);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000327 }
328}
329
Kazu Hiratada7d8302002-09-22 02:03:17 +0000330/* Check if we have a known directive. INDENTED is nonzero if the
Neil Booth18a9d8f2001-09-16 11:23:56 +0000331 '#' of the directive was indented. This function is in this file
Gabriel Dos Reisa2566ae2005-01-02 01:32:21 +0000332 to save unnecessarily exporting dtable etc. to lex.c. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +0000333 nonzero if the line of tokens has been handled, zero if we should
Neil Booth18a9d8f2001-09-16 11:23:56 +0000334 continue processing the line. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000335int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000336_cpp_handle_directive (cpp_reader *pfile, int indented)
Neil Boothfe6c2db2000-11-15 19:25:22 +0000337{
Neil Boothfe6c2db2000-11-15 19:25:22 +0000338 const directive *dir = 0;
Neil Booth345894b2001-09-16 13:44:29 +0000339 const cpp_token *dname;
Neil Boothe808ec92002-02-27 07:24:53 +0000340 bool was_parsing_args = pfile->state.parsing_args;
Zack Weinbergc6e83802004-06-05 20:58:06 +0000341 bool was_discarding_output = pfile->state.discarding_output;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000342 int skip = 1;
343
Zack Weinbergc6e83802004-06-05 20:58:06 +0000344 if (was_discarding_output)
345 pfile->state.prevent_expansion = 0;
346
Neil Boothe808ec92002-02-27 07:24:53 +0000347 if (was_parsing_args)
348 {
349 if (CPP_OPTION (pfile, pedantic))
John David Anglin0527bc42003-11-01 22:56:54 +0000350 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothe808ec92002-02-27 07:24:53 +0000351 "embedding a directive within macro arguments is not portable");
352 pfile->state.parsing_args = 0;
353 pfile->state.prevent_expansion = 0;
354 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000355 start_directive (pfile);
Neil Booth345894b2001-09-16 13:44:29 +0000356 dname = _cpp_lex_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000357
Neil Booth345894b2001-09-16 13:44:29 +0000358 if (dname->type == CPP_NAME)
Neil Booth0d9f2342000-09-18 18:43:05 +0000359 {
Zack Weinberg4977bab2002-12-16 18:23:00 +0000360 if (dname->val.node->is_directive)
361 dir = &dtable[dname->val.node->directive_index];
Neil Booth93c803682000-10-28 17:59:06 +0000362 }
Kazu Hirata05713b82002-09-15 18:24:08 +0000363 /* We do not recognize the # followed by a number extension in
Neil Booth18a9d8f2001-09-16 11:23:56 +0000364 assembler code. */
Neil Booth345894b2001-09-16 13:44:29 +0000365 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth93c803682000-10-28 17:59:06 +0000366 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000367 dir = &linemarker_dir;
368 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
369 && ! pfile->state.skipping)
John David Anglin0527bc42003-11-01 22:56:54 +0000370 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothebef4e82002-04-14 18:42:47 +0000371 "style of line directive is a GCC extension");
Neil Booth0d9f2342000-09-18 18:43:05 +0000372 }
373
Neil Booth93c803682000-10-28 17:59:06 +0000374 if (dir)
Neil Booth0d9f2342000-09-18 18:43:05 +0000375 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000376 /* If we have a directive that is not an opening conditional,
377 invalidate any control macro. */
378 if (! (dir->flags & IF_COND))
379 pfile->mi_valid = false;
Neil Booth93c803682000-10-28 17:59:06 +0000380
Neil Booth18a9d8f2001-09-16 11:23:56 +0000381 /* Kluge alert. In order to be sure that code like this
382
383 #define HASH #
384 HASH define foo bar
385
386 does not cause '#define foo bar' to get executed when
387 compiled with -save-temps, we recognize directives in
Gabriel Dos Reisa2566ae2005-01-02 01:32:21 +0000388 -fpreprocessed mode only if the # is in column 1. macro.c
Joseph Myersa1f300c2001-11-23 02:05:19 +0000389 puts a space in front of any '#' at the start of a macro. */
Neil Booth18a9d8f2001-09-16 11:23:56 +0000390 if (CPP_OPTION (pfile, preprocessed)
391 && (indented || !(dir->flags & IN_I)))
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000392 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000393 skip = 0;
394 dir = 0;
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000395 }
396 else
Neil Booth93c803682000-10-28 17:59:06 +0000397 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000398 /* In failed conditional groups, all non-conditional
399 directives are ignored. Before doing that, whether
400 skipping or not, we should lex angle-bracketed headers
401 correctly, and maybe output some diagnostics. */
402 pfile->state.angled_headers = dir->flags & INCL;
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000403 pfile->state.directive_wants_padding = dir->flags & INCL;
Neil Booth18a9d8f2001-09-16 11:23:56 +0000404 if (! CPP_OPTION (pfile, preprocessed))
405 directive_diagnostics (pfile, dir, indented);
406 if (pfile->state.skipping && !(dir->flags & COND))
407 dir = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000408 }
409 }
Neil Booth345894b2001-09-16 13:44:29 +0000410 else if (dname->type == CPP_EOF)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000411 ; /* CPP_EOF is the "null directive". */
412 else
Neil Booth0d9f2342000-09-18 18:43:05 +0000413 {
Neil Booth93c803682000-10-28 17:59:06 +0000414 /* An unknown directive. Don't complain about it in assembly
415 source: we don't know where the comments are, and # may
416 introduce assembler pseudo-ops. Don't complain about invalid
417 directives in skipped conditional groups (6.10 p4). */
Neil Boothbdb05a72000-11-26 17:31:13 +0000418 if (CPP_OPTION (pfile, lang) == CLK_ASM)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000419 skip = 0;
420 else if (!pfile->state.skipping)
John David Anglin0527bc42003-11-01 22:56:54 +0000421 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
Neil Booth345894b2001-09-16 13:44:29 +0000422 cpp_token_as_text (pfile, dname));
Neil Booth0d9f2342000-09-18 18:43:05 +0000423 }
424
Neil Boothd1a58682002-06-28 06:26:54 +0000425 pfile->directive = dir;
426 if (CPP_OPTION (pfile, traditional))
427 prepare_directive_trad (pfile);
428
Neil Booth18a9d8f2001-09-16 11:23:56 +0000429 if (dir)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000430 pfile->directive->handler (pfile);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000431 else if (skip == 0)
432 _cpp_backup_tokens (pfile, 1);
433
434 end_directive (pfile, skip);
Neil Boothe808ec92002-02-27 07:24:53 +0000435 if (was_parsing_args)
436 {
437 /* Restore state when within macro args. */
438 pfile->state.parsing_args = 2;
439 pfile->state.prevent_expansion = 1;
Neil Boothe808ec92002-02-27 07:24:53 +0000440 }
Zack Weinbergc6e83802004-06-05 20:58:06 +0000441 if (was_discarding_output)
442 pfile->state.prevent_expansion = 1;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000443 return skip;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000444}
445
Neil Booth93c803682000-10-28 17:59:06 +0000446/* Directive handler wrapper used by the command line option
Neil Booth26aea072003-04-19 00:22:51 +0000447 processor. BUF is \n terminated. */
Neil Booth93c803682000-10-28 17:59:06 +0000448static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000449run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000450{
Neil Booth562a5c22002-04-21 18:46:42 +0000451 cpp_push_buffer (pfile, (const uchar *) buf, count,
Per Bothner40de9f72003-10-02 07:30:34 +0000452 /* from_stage3 */ true);
Neil Booth8bfb1462002-08-14 20:17:55 +0000453 /* Disgusting hack. */
Zack Weinberg21b11492004-09-09 19:16:56 +0000454 if (dir_no == T_PRAGMA && pfile->buffer->prev)
Neil Booth8f9b4002003-07-29 22:26:13 +0000455 pfile->buffer->file = pfile->buffer->prev->file;
Neil Booth0bda4762000-12-11 07:45:16 +0000456 start_directive (pfile);
Neil Booth26aea072003-04-19 00:22:51 +0000457
458 /* This is a short-term fix to prevent a leading '#' being
459 interpreted as a directive. */
460 _cpp_clean_line (pfile);
461
Neil Boothf71aebb2001-05-27 18:06:00 +0000462 pfile->directive = &dtable[dir_no];
Neil Booth1a769162002-06-11 05:36:17 +0000463 if (CPP_OPTION (pfile, traditional))
464 prepare_directive_trad (pfile);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000465 pfile->directive->handler (pfile);
Neil Booth0bda4762000-12-11 07:45:16 +0000466 end_directive (pfile, 1);
Neil Booth8bfb1462002-08-14 20:17:55 +0000467 if (dir_no == T_PRAGMA)
Neil Booth8f9b4002003-07-29 22:26:13 +0000468 pfile->buffer->file = NULL;
Neil Boothef6e9582001-08-04 12:01:59 +0000469 _cpp_pop_buffer (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000470}
Per Bothner7f2935c1995-03-16 13:59:07 -0800471
Neil Booth93c803682000-10-28 17:59:06 +0000472/* Checks for validity the macro name in #define, #undef, #ifdef and
473 #ifndef directives. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000474static cpp_hashnode *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000475lex_macro_node (cpp_reader *pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000476{
Neil Booth1a769162002-06-11 05:36:17 +0000477 const cpp_token *token = _cpp_lex_token (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000478
Zack Weinberg92936ec2000-07-19 20:18:08 +0000479 /* The token immediately after #define must be an identifier. That
Zack Weinbergb8363a22001-07-01 18:48:13 +0000480 identifier may not be "defined", per C99 6.10.8p4.
481 In C++, it may not be any of the "named operators" either,
482 per C++98 [lex.digraph], [lex.key].
483 Finally, the identifier may not have been poisoned. (In that case
Neil Booth1d63a282002-06-28 20:27:14 +0000484 the lexer has issued the error message for us.) */
Zack Weinbergb8363a22001-07-01 18:48:13 +0000485
Neil Booth1a769162002-06-11 05:36:17 +0000486 if (token->type == CPP_NAME)
487 {
488 cpp_hashnode *node = token->val.node;
489
490 if (node == pfile->spec_nodes.n_defined)
John David Anglin0527bc42003-11-01 22:56:54 +0000491 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth1a769162002-06-11 05:36:17 +0000492 "\"defined\" cannot be used as a macro name");
493 else if (! (node->flags & NODE_POISONED))
494 return node;
495 }
496 else if (token->flags & NAMED_OP)
John David Anglin0527bc42003-11-01 22:56:54 +0000497 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcbc69f82002-06-05 20:27:12 +0000498 "\"%s\" cannot be used as a macro name as it is an operator in C++",
Neil Booth1a769162002-06-11 05:36:17 +0000499 NODE_NAME (token->val.node));
500 else if (token->type == CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000501 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
Neil Booth1a769162002-06-11 05:36:17 +0000502 pfile->directive->name);
503 else
John David Anglin0527bc42003-11-01 22:56:54 +0000504 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
Zack Weinbergb8363a22001-07-01 18:48:13 +0000505
Neil Boothcbc69f82002-06-05 20:27:12 +0000506 return NULL;
Per Bothner7f2935c1995-03-16 13:59:07 -0800507}
Per Bothner7f2935c1995-03-16 13:59:07 -0800508
Gabriel Dos Reisa2566ae2005-01-02 01:32:21 +0000509/* Process a #define directive. Most work is done in macro.c. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000510static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000511do_define (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800512{
Neil Booth93c803682000-10-28 17:59:06 +0000513 cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000514
Neil Booth93c803682000-10-28 17:59:06 +0000515 if (node)
516 {
Neil Booth1d63a282002-06-28 20:27:14 +0000517 /* If we have been requested to expand comments into macros,
518 then re-enable saving of comments. */
519 pfile->state.save_comments =
520 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
521
Neil Booth93c803682000-10-28 17:59:06 +0000522 if (_cpp_create_definition (pfile, node))
523 if (pfile->cb.define)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000524 pfile->cb.define (pfile, pfile->directive_line, node);
Neil Booth93c803682000-10-28 17:59:06 +0000525 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800526}
527
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000528/* Handle #undef. Mark the identifier NT_VOID in the hash table. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000529static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000530do_undef (cpp_reader *pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000531{
Kazu Hiratadf383482002-05-22 22:02:16 +0000532 cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000533
Neil Booth45f24922003-12-12 07:00:29 +0000534 if (node)
Zack Weinberg041c3192000-07-04 01:58:21 +0000535 {
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000536 if (pfile->cb.undef)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000537 pfile->cb.undef (pfile, pfile->directive_line, node);
Zack Weinberg041c3192000-07-04 01:58:21 +0000538
Neil Booth45f24922003-12-12 07:00:29 +0000539 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
540 identifier is not currently defined as a macro name. */
541 if (node->type == NT_MACRO)
542 {
543 if (node->flags & NODE_WARN)
544 cpp_error (pfile, CPP_DL_WARNING,
545 "undefining \"%s\"", NODE_NAME (node));
Zack Weinberg041c3192000-07-04 01:58:21 +0000546
Neil Booth45f24922003-12-12 07:00:29 +0000547 if (CPP_OPTION (pfile, warn_unused_macros))
548 _cpp_warn_if_unused_macro (pfile, node, NULL);
Neil Bootha69cbaa2002-07-23 22:57:49 +0000549
Neil Booth45f24922003-12-12 07:00:29 +0000550 _cpp_free_definition (node);
551 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000552 }
Neil Booth45f24922003-12-12 07:00:29 +0000553
Neil Booth93c803682000-10-28 17:59:06 +0000554 check_eol (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000555}
556
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000557/* Undefine a single macro/assertion/whatever. */
558
559static int
Zack Weinbergc6e83802004-06-05 20:58:06 +0000560undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000561 void *data_p ATTRIBUTE_UNUSED)
562{
Zack Weinbergc6e83802004-06-05 20:58:06 +0000563 /* Body of _cpp_free_definition inlined here for speed.
564 Macros and assertions no longer have anything to free. */
565 h->type = NT_VOID;
566 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED);
Geoffrey Keatingd1bd0de2003-07-11 08:33:21 +0000567 return 1;
568}
569
570/* Undefine all macros and assertions. */
571
572void
573cpp_undef_all (cpp_reader *pfile)
574{
575 cpp_forall_identifiers (pfile, undefine_macros, NULL);
576}
577
578
Neil Booth93c803682000-10-28 17:59:06 +0000579/* Helper routine used by parse_include. Reinterpret the current line
580 as an h-char-sequence (< ... >); we are looking at the first token
Neil Booth74eb4b32003-04-21 19:21:59 +0000581 after the <. Returns a malloced filename. */
582static char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000583glue_header_name (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800584{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000585 const cpp_token *token;
Neil Booth74eb4b32003-04-21 19:21:59 +0000586 char *buffer;
Neil Booth2450e0b2002-02-23 20:21:39 +0000587 size_t len, total_len = 0, capacity = 1024;
Per Bothner7f2935c1995-03-16 13:59:07 -0800588
Neil Booth93c803682000-10-28 17:59:06 +0000589 /* To avoid lexed tokens overwriting our glued name, we can only
590 allocate from the string pool once we've lexed everything. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000591 buffer = XNEWVEC (char, capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000592 for (;;)
Zack Weinberg168d3732000-03-14 06:34:11 +0000593 {
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000594 token = get_token_no_padding (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000595
Neil Booth74eb4b32003-04-21 19:21:59 +0000596 if (token->type == CPP_GREATER)
Neil Booth93c803682000-10-28 17:59:06 +0000597 break;
Neil Booth74eb4b32003-04-21 19:21:59 +0000598 if (token->type == CPP_EOF)
599 {
John David Anglin0527bc42003-11-01 22:56:54 +0000600 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
Neil Booth74eb4b32003-04-21 19:21:59 +0000601 break;
602 }
Neil Booth93c803682000-10-28 17:59:06 +0000603
Neil Booth59325652003-04-24 20:03:57 +0000604 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
Neil Booth2450e0b2002-02-23 20:21:39 +0000605 if (total_len + len > capacity)
Neil Booth93c803682000-10-28 17:59:06 +0000606 {
Neil Booth2450e0b2002-02-23 20:21:39 +0000607 capacity = (capacity + len) * 2;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000608 buffer = XRESIZEVEC (char, buffer, capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000609 }
610
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000611 if (token->flags & PREV_WHITE)
Neil Booth2450e0b2002-02-23 20:21:39 +0000612 buffer[total_len++] = ' ';
Neil Booth93c803682000-10-28 17:59:06 +0000613
Geoffrey Keating47e20492005-03-12 10:44:06 +0000614 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
615 true)
Neil Booth74eb4b32003-04-21 19:21:59 +0000616 - (uchar *) buffer);
Neil Booth93c803682000-10-28 17:59:06 +0000617 }
618
Neil Booth74eb4b32003-04-21 19:21:59 +0000619 buffer[total_len] = '\0';
620 return buffer;
Neil Booth93c803682000-10-28 17:59:06 +0000621}
622
Neil Booth74eb4b32003-04-21 19:21:59 +0000623/* Returns the file name of #include, #include_next, #import and
624 #pragma dependency. The string is malloced and the caller should
625 free it. Returns NULL on error. */
626static const char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000627parse_include (cpp_reader *pfile, int *pangle_brackets)
Neil Booth93c803682000-10-28 17:59:06 +0000628{
Neil Booth74eb4b32003-04-21 19:21:59 +0000629 char *fname;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000630 const cpp_token *header;
Neil Booth93c803682000-10-28 17:59:06 +0000631
Neil Booth93c803682000-10-28 17:59:06 +0000632 /* Allow macro expansion. */
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000633 header = get_token_no_padding (pfile);
Neil Booth74eb4b32003-04-21 19:21:59 +0000634 if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
Neil Booth93c803682000-10-28 17:59:06 +0000635 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000636 fname = XNEWVEC (char, header->val.str.len - 1);
Neil Booth6338b352003-04-23 22:44:06 +0000637 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
638 fname[header->val.str.len - 2] = '\0';
Neil Booth74eb4b32003-04-21 19:21:59 +0000639 *pangle_brackets = header->type == CPP_HEADER_NAME;
Zack Weinberg041c3192000-07-04 01:58:21 +0000640 }
Neil Booth74eb4b32003-04-21 19:21:59 +0000641 else if (header->type == CPP_LESS)
Zack Weinberg041c3192000-07-04 01:58:21 +0000642 {
Neil Booth74eb4b32003-04-21 19:21:59 +0000643 fname = glue_header_name (pfile);
644 *pangle_brackets = 1;
645 }
646 else
647 {
648 const unsigned char *dir;
649
650 if (pfile->directive == &dtable[T_PRAGMA])
651 dir = U"pragma dependency";
652 else
653 dir = pfile->directive->name;
John David Anglin0527bc42003-11-01 22:56:54 +0000654 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
Neil Booth74eb4b32003-04-21 19:21:59 +0000655 dir);
656
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000657 return NULL;
Richard Kennercfb3ee11997-04-13 14:30:13 -0400658 }
659
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000660 check_eol (pfile);
Neil Booth74eb4b32003-04-21 19:21:59 +0000661 return fname;
Zack Weinberg168d3732000-03-14 06:34:11 +0000662}
663
Neil Boothba133c92001-03-15 07:57:13 +0000664/* Handle #include, #include_next and #import. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000665static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000666do_include_common (cpp_reader *pfile, enum include_type type)
Zack Weinberg168d3732000-03-14 06:34:11 +0000667{
Neil Booth74eb4b32003-04-21 19:21:59 +0000668 const char *fname;
669 int angle_brackets;
670
671 fname = parse_include (pfile, &angle_brackets);
672 if (!fname)
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000673 return;
Zack Weinberg168d3732000-03-14 06:34:11 +0000674
Nathanael Nerode28303822004-11-28 22:28:13 +0000675 if (!*fname)
676 {
677 cpp_error (pfile, CPP_DL_ERROR, "empty filename in #%s",
678 pfile->directive->name);
679 free ((void *) fname);
680 return;
681 }
682
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000683 /* Prevent #include recursion. */
Per Bothner50f59cd2004-01-19 21:30:18 -0800684 if (pfile->line_table->depth >= CPP_STACK_MAX)
John David Anglin0527bc42003-11-01 22:56:54 +0000685 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
Neil Booth74eb4b32003-04-21 19:21:59 +0000686 else
Neil Booth09b82252001-07-29 22:27:20 +0000687 {
Neil Booth74eb4b32003-04-21 19:21:59 +0000688 /* Get out of macro context, if we are. */
689 skip_rest_of_line (pfile);
690
691 if (pfile->cb.include)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000692 pfile->cb.include (pfile, pfile->directive_line,
693 pfile->directive->name, fname, angle_brackets);
Neil Booth74eb4b32003-04-21 19:21:59 +0000694
Neil Booth8f9b4002003-07-29 22:26:13 +0000695 _cpp_stack_include (pfile, fname, angle_brackets, type);
Neil Booth09b82252001-07-29 22:27:20 +0000696 }
697
Kaveh R. Ghazifad205f2003-06-16 21:41:10 +0000698 free ((void *) fname);
Neil Boothba133c92001-03-15 07:57:13 +0000699}
700
701static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000702do_include (cpp_reader *pfile)
Neil Boothba133c92001-03-15 07:57:13 +0000703{
704 do_include_common (pfile, IT_INCLUDE);
Zack Weinberg168d3732000-03-14 06:34:11 +0000705}
706
Zack Weinberg711b8822000-07-18 00:59:49 +0000707static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000708do_import (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +0000709{
Neil Boothba133c92001-03-15 07:57:13 +0000710 do_include_common (pfile, IT_IMPORT);
Zack Weinberg168d3732000-03-14 06:34:11 +0000711}
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000712
Zack Weinberg711b8822000-07-18 00:59:49 +0000713static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000714do_include_next (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +0000715{
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000716 enum include_type type = IT_INCLUDE_NEXT;
717
718 /* If this is the primary source file, warn and use the normal
719 search logic. */
720 if (! pfile->buffer->prev)
721 {
John David Anglin0527bc42003-11-01 22:56:54 +0000722 cpp_error (pfile, CPP_DL_WARNING,
Zack Weinberg3963c2e2003-02-12 17:01:53 +0000723 "#include_next in primary source file");
724 type = IT_INCLUDE;
725 }
726 do_include_common (pfile, type);
Per Bothner7f2935c1995-03-16 13:59:07 -0800727}
728
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000729/* Subroutine of do_linemarker. Read possible flags after file name.
730 LAST is the last flag seen; 0 if this is the first flag. Return the
731 flag if it is valid, 0 at the end of the directive. Otherwise
732 complain. */
Neil Booth642ce432000-12-07 23:17:56 +0000733static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000734read_flag (cpp_reader *pfile, unsigned int last)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000735{
Neil Booth345894b2001-09-16 13:44:29 +0000736 const cpp_token *token = _cpp_lex_token (pfile);
Jason Merrilld3a34a01999-08-14 00:42:07 +0000737
Neil Booth345894b2001-09-16 13:44:29 +0000738 if (token->type == CPP_NUMBER && token->val.str.len == 1)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000739 {
Neil Booth345894b2001-09-16 13:44:29 +0000740 unsigned int flag = token->val.str.text[0] - '0';
Neil Booth28e0f042000-12-09 12:06:37 +0000741
742 if (flag > last && flag <= 4
743 && (flag != 4 || last == 3)
744 && (flag != 2 || last == 0))
745 return flag;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000746 }
Neil Booth93c803682000-10-28 17:59:06 +0000747
Neil Booth345894b2001-09-16 13:44:29 +0000748 if (token->type != CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +0000749 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
Neil Booth345894b2001-09-16 13:44:29 +0000750 cpp_token_as_text (pfile, token));
Neil Booth93c803682000-10-28 17:59:06 +0000751 return 0;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000752}
753
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000754/* Subroutine of do_line and do_linemarker. Convert a number in STR,
755 of length LEN, to binary; store it in NUMP, and return 0 if the
756 number was well-formed, 1 if not. Temporary, hopefully. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000757static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000758strtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump)
Zack Weinberg041c3192000-07-04 01:58:21 +0000759{
760 unsigned long reg = 0;
Neil Booth562a5c22002-04-21 18:46:42 +0000761 uchar c;
Zack Weinberg041c3192000-07-04 01:58:21 +0000762 while (len--)
763 {
764 c = *str++;
765 if (!ISDIGIT (c))
766 return 1;
767 reg *= 10;
768 reg += c - '0';
769 }
770 *nump = reg;
771 return 0;
772}
773
Zack Weinberg5538ada1999-02-04 06:36:54 -0500774/* Interpret #line command.
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000775 Note that the filename string (if any) is a true string constant
776 (escapes are interpreted), unlike in #line. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000777static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000778do_line (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800779{
Per Bothner500bee02004-04-22 19:22:27 -0700780 const struct line_maps *line_table = pfile->line_table;
781 const struct line_map *map = &line_table->maps[line_table->used - 1];
Devang Patel2203a882005-02-28 11:04:19 -0800782
783 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
784 sysp right now. */
785
786 unsigned char map_sysp = map->sysp;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000787 const cpp_token *token;
Per Bothner12f9df42004-02-11 07:29:30 -0800788 const char *new_file = map->to_file;
Neil Boothbb74c962001-08-17 22:23:49 +0000789 unsigned long new_lineno;
Per Bothner7f2935c1995-03-16 13:59:07 -0800790
Neil Booth27e25642000-11-27 08:00:04 +0000791 /* C99 raised the minimum limit on #line numbers. */
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000792 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
Neil Booth18a9d8f2001-09-16 11:23:56 +0000793
Neil Booth93c803682000-10-28 17:59:06 +0000794 /* #line commands expand macros. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000795 token = cpp_get_token (pfile);
796 if (token->type != CPP_NUMBER
797 || strtoul_for_line (token->val.str.text, token->val.str.len,
798 &new_lineno))
Per Bothner7f2935c1995-03-16 13:59:07 -0800799 {
John David Anglin0527bc42003-11-01 22:56:54 +0000800 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +0000801 "\"%s\" after #line is not a positive integer",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000802 cpp_token_as_text (pfile, token));
Zack Weinberg9ec72912000-08-09 19:41:12 +0000803 return;
Kazu Hiratadf383482002-05-22 22:02:16 +0000804 }
Zack Weinberg5538ada1999-02-04 06:36:54 -0500805
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000806 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
John David Anglin0527bc42003-11-01 22:56:54 +0000807 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
Zack Weinberg5538ada1999-02-04 06:36:54 -0500808
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000809 token = cpp_get_token (pfile);
810 if (token->type == CPP_STRING)
Zack Weinberg5538ada1999-02-04 06:36:54 -0500811 {
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000812 cpp_string s = { 0, 0 };
Eric Christopher423e95e2004-02-12 02:25:03 +0000813 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
814 &s, false))
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000815 new_file = (const char *)s.text;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000816 check_eol (pfile);
817 }
818 else if (token->type != CPP_EOF)
819 {
John David Anglin0527bc42003-11-01 22:56:54 +0000820 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000821 cpp_token_as_text (pfile, token));
822 return;
823 }
Neil Booth93c803682000-10-28 17:59:06 +0000824
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000825 skip_rest_of_line (pfile);
826 _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
Devang Patel2203a882005-02-28 11:04:19 -0800827 map_sysp);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000828}
829
830/* Interpret the # 44 "file" [flags] notation, which has slightly
831 different syntax and semantics from #line: Flags are allowed,
832 and we never complain about the line number being too big. */
833static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000834do_linemarker (cpp_reader *pfile)
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000835{
Per Bothner500bee02004-04-22 19:22:27 -0700836 const struct line_maps *line_table = pfile->line_table;
837 const struct line_map *map = &line_table->maps[line_table->used - 1];
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000838 const cpp_token *token;
Per Bothner12f9df42004-02-11 07:29:30 -0800839 const char *new_file = map->to_file;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000840 unsigned long new_lineno;
Per Bothner12f9df42004-02-11 07:29:30 -0800841 unsigned int new_sysp = map->sysp;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000842 enum lc_reason reason = LC_RENAME;
843 int flag;
844
845 /* Back up so we can get the number again. Putting this in
846 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
847 some circumstances, which can segfault. */
848 _cpp_backup_tokens (pfile, 1);
849
850 /* #line commands expand macros. */
851 token = cpp_get_token (pfile);
852 if (token->type != CPP_NUMBER
853 || strtoul_for_line (token->val.str.text, token->val.str.len,
854 &new_lineno))
855 {
John David Anglin0527bc42003-11-01 22:56:54 +0000856 cpp_error (pfile, CPP_DL_ERROR,
857 "\"%s\" after # is not a positive integer",
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000858 cpp_token_as_text (pfile, token));
859 return;
Kazu Hiratadf383482002-05-22 22:02:16 +0000860 }
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000861
862 token = cpp_get_token (pfile);
863 if (token->type == CPP_STRING)
864 {
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000865 cpp_string s = { 0, 0 };
Eric Christopher423e95e2004-02-12 02:25:03 +0000866 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
867 1, &s, false))
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000868 new_file = (const char *)s.text;
Eric Christophercf551fb2004-01-16 22:37:49 +0000869
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000870 new_sysp = 0;
871 flag = read_flag (pfile, 0);
872 if (flag == 1)
Neil Booth93c803682000-10-28 17:59:06 +0000873 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000874 reason = LC_ENTER;
875 /* Fake an include for cpp_included (). */
876 _cpp_fake_include (pfile, new_file);
877 flag = read_flag (pfile, flag);
Neil Booth93c803682000-10-28 17:59:06 +0000878 }
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000879 else if (flag == 2)
880 {
881 reason = LC_LEAVE;
882 flag = read_flag (pfile, flag);
883 }
884 if (flag == 3)
885 {
886 new_sysp = 1;
887 flag = read_flag (pfile, flag);
888 if (flag == 4)
889 new_sysp = 2;
Per Bothner12f9df42004-02-11 07:29:30 -0800890 pfile->buffer->sysp = new_sysp;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000891 }
892
Neil Boothfde84342001-08-06 21:07:41 +0000893 check_eol (pfile);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000894 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000895 else if (token->type != CPP_EOF)
Neil Booth27e25642000-11-27 08:00:04 +0000896 {
John David Anglin0527bc42003-11-01 22:56:54 +0000897 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000898 cpp_token_as_text (pfile, token));
Neil Booth27e25642000-11-27 08:00:04 +0000899 return;
900 }
Zack Weinberg941e09b1998-12-15 11:17:06 +0000901
Neil Boothbdcbe492001-09-13 20:05:17 +0000902 skip_rest_of_line (pfile);
Neil Boothbb74c962001-08-17 22:23:49 +0000903 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
Neil Booth27e25642000-11-27 08:00:04 +0000904}
905
Neil Booth67821e32001-08-05 17:31:25 +0000906/* Arrange the file_change callback. pfile->line has changed to
Neil Booth47d89cf2001-08-11 07:33:39 +0000907 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
Joseph Myersa1f300c2001-11-23 02:05:19 +0000908 header, 2 for a system header that needs to be extern "C" protected,
Neil Booth47d89cf2001-08-11 07:33:39 +0000909 and zero otherwise. */
Neil Bootheb1f4d92000-12-18 19:00:26 +0000910void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000911_cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
912 const char *to_file, unsigned int file_line,
913 unsigned int sysp)
Neil Booth27e25642000-11-27 08:00:04 +0000914{
Per Bothner12f9df42004-02-11 07:29:30 -0800915 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
916 to_file, file_line);
Per Bothner500bee02004-04-22 19:22:27 -0700917 if (map != NULL)
918 linemap_line_start (pfile->line_table, map->to_line, 127);
Neil Boothd82fc102001-08-02 23:03:31 +0000919
Neil Bootheb1f4d92000-12-18 19:00:26 +0000920 if (pfile->cb.file_change)
Per Bothner12f9df42004-02-11 07:29:30 -0800921 pfile->cb.file_change (pfile, map);
Per Bothner7f2935c1995-03-16 13:59:07 -0800922}
Zack Weinberg941e09b1998-12-15 11:17:06 +0000923
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000924/* Report a warning or error detected by the program we are
925 processing. Use the directive's tokens in the error message. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000926static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000927do_diagnostic (cpp_reader *pfile, int code, int print_dir)
Per Bothner7f2935c1995-03-16 13:59:07 -0800928{
Per Bothner12f9df42004-02-11 07:29:30 -0800929 if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0))
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000930 {
Neil Booth29b10742000-11-13 18:40:37 +0000931 if (print_dir)
932 fprintf (stderr, "#%s ", pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +0000933 pfile->state.prevent_expansion++;
934 cpp_output_line (pfile, stderr);
935 pfile->state.prevent_expansion--;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000936 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800937}
938
Neil Booth838f3132000-09-24 10:42:09 +0000939static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000940do_error (cpp_reader *pfile)
Neil Booth838f3132000-09-24 10:42:09 +0000941{
John David Anglin0527bc42003-11-01 22:56:54 +0000942 do_diagnostic (pfile, CPP_DL_ERROR, 1);
Neil Booth838f3132000-09-24 10:42:09 +0000943}
Per Bothner7f2935c1995-03-16 13:59:07 -0800944
Zack Weinberg711b8822000-07-18 00:59:49 +0000945static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000946do_warning (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800947{
Neil Booth2f878972001-04-08 10:01:18 +0000948 /* We want #warning diagnostics to be emitted in system headers too. */
John David Anglin0527bc42003-11-01 22:56:54 +0000949 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
Per Bothner7f2935c1995-03-16 13:59:07 -0800950}
951
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000952/* Report program identification. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000953static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000954do_ident (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800955{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000956 const cpp_token *str = cpp_get_token (pfile);
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000957
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000958 if (str->type != CPP_STRING)
Zack Weinberg1ed17cd2005-05-12 18:31:38 +0000959 cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
960 pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +0000961 else if (pfile->cb.ident)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000962 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000963
Neil Booth93c803682000-10-28 17:59:06 +0000964 check_eol (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -0800965}
966
Neil Bootha5da89c2001-10-14 17:44:00 +0000967/* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
968 matching entry, or NULL if none is found. The returned entry could
969 be the start of a namespace chain, or a pragma. */
970static struct pragma_entry *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000971lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
Nathan Sidwell82443372000-06-23 10:56:09 +0000972{
Neil Booth4b115ff2001-10-14 23:04:13 +0000973 while (chain && chain->pragma != pragma)
974 chain = chain->next;
Neil Bootha5da89c2001-10-14 17:44:00 +0000975
976 return chain;
977}
978
979/* Create and insert a pragma entry for NAME at the beginning of a
980 singly-linked CHAIN. If handler is NULL, it is a namespace,
Zack Weinberg21b11492004-09-09 19:16:56 +0000981 otherwise it is a pragma and its handler. If INTERNAL is true
982 this pragma is being inserted by libcpp itself. */
Neil Bootha5da89c2001-10-14 17:44:00 +0000983static struct pragma_entry *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000984insert_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain,
Zack Weinberg21b11492004-09-09 19:16:56 +0000985 const cpp_hashnode *pragma, pragma_cb handler,
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +0000986 bool allow_expansion, bool internal)
Neil Bootha5da89c2001-10-14 17:44:00 +0000987{
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000988 struct pragma_entry *new_entry;
Neil Bootha5da89c2001-10-14 17:44:00 +0000989
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000990 new_entry = (struct pragma_entry *)
Neil Bootha5da89c2001-10-14 17:44:00 +0000991 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000992 new_entry->pragma = pragma;
Neil Bootha5da89c2001-10-14 17:44:00 +0000993 if (handler)
994 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000995 new_entry->is_nspace = 0;
996 new_entry->u.handler = handler;
Neil Bootha5da89c2001-10-14 17:44:00 +0000997 }
998 else
999 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001000 new_entry->is_nspace = 1;
1001 new_entry->u.space = NULL;
Neil Bootha5da89c2001-10-14 17:44:00 +00001002 }
1003
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001004 new_entry->allow_expansion = allow_expansion;
1005 new_entry->is_internal = internal;
1006 new_entry->next = *chain;
1007 *chain = new_entry;
1008 return new_entry;
Neil Bootha5da89c2001-10-14 17:44:00 +00001009}
1010
1011/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1012 goes in the global namespace. HANDLER is the handler it will call,
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001013 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1014 expansion while parsing pragma NAME. INTERNAL is true if this is a
1015 pragma registered by cpplib itself, false if it is registered via
Zack Weinberg21b11492004-09-09 19:16:56 +00001016 cpp_register_pragma */
1017static void
1018register_pragma (cpp_reader *pfile, const char *space, const char *name,
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001019 pragma_cb handler, bool allow_expansion, bool internal)
Nathan Sidwell82443372000-06-23 10:56:09 +00001020{
Neil Bootha5da89c2001-10-14 17:44:00 +00001021 struct pragma_entry **chain = &pfile->pragmas;
1022 struct pragma_entry *entry;
Neil Booth4b115ff2001-10-14 23:04:13 +00001023 const cpp_hashnode *node;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001024
Neil Bootha5da89c2001-10-14 17:44:00 +00001025 if (!handler)
1026 abort ();
1027
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001028 if (space)
1029 {
Neil Booth4b115ff2001-10-14 23:04:13 +00001030 node = cpp_lookup (pfile, U space, strlen (space));
1031 entry = lookup_pragma_entry (*chain, node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001032 if (!entry)
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001033 entry = insert_pragma_entry (pfile, chain, node, NULL,
1034 allow_expansion, internal);
Neil Bootha5da89c2001-10-14 17:44:00 +00001035 else if (!entry->is_nspace)
1036 goto clash;
1037 chain = &entry->u.space;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001038 }
1039
Neil Bootha5da89c2001-10-14 17:44:00 +00001040 /* Check for duplicates. */
Neil Booth4b115ff2001-10-14 23:04:13 +00001041 node = cpp_lookup (pfile, U name, strlen (name));
1042 entry = lookup_pragma_entry (*chain, node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001043 if (entry)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001044 {
Neil Bootha5da89c2001-10-14 17:44:00 +00001045 if (entry->is_nspace)
1046 clash:
John David Anglin0527bc42003-11-01 22:56:54 +00001047 cpp_error (pfile, CPP_DL_ICE,
Neil Bootha5da89c2001-10-14 17:44:00 +00001048 "registering \"%s\" as both a pragma and a pragma namespace",
Neil Booth4b115ff2001-10-14 23:04:13 +00001049 NODE_NAME (node));
Neil Bootha5da89c2001-10-14 17:44:00 +00001050 else if (space)
John David Anglin0527bc42003-11-01 22:56:54 +00001051 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
Neil Boothebef4e82002-04-14 18:42:47 +00001052 space, name);
Neil Bootha5da89c2001-10-14 17:44:00 +00001053 else
John David Anglin0527bc42003-11-01 22:56:54 +00001054 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001055 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001056 else
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001057 insert_pragma_entry (pfile, chain, node, handler, allow_expansion,
1058 internal);
Zack Weinberg21b11492004-09-09 19:16:56 +00001059}
1060
1061/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1062 goes in the global namespace. HANDLER is the handler it will call,
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001063 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1064 expansion while parsing pragma NAME. This function is exported
1065 from libcpp. */
Zack Weinberg21b11492004-09-09 19:16:56 +00001066void
1067cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001068 pragma_cb handler, bool allow_expansion)
Zack Weinberg21b11492004-09-09 19:16:56 +00001069{
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001070 register_pragma (pfile, space, name, handler, allow_expansion, false);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001071}
Neil Bootha5da89c2001-10-14 17:44:00 +00001072
1073/* Register the pragmas the preprocessor itself handles. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001074void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001075_cpp_init_internal_pragmas (cpp_reader *pfile)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001076{
Neil Bootha5da89c2001-10-14 17:44:00 +00001077 /* Pragmas in the global namespace. */
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001078 register_pragma (pfile, 0, "once", do_pragma_once, false, true);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001079
Neil Bootha5da89c2001-10-14 17:44:00 +00001080 /* New GCC-specific pragmas should be put in the GCC namespace. */
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001081 register_pragma (pfile, "GCC", "poison", do_pragma_poison, false, true);
1082 register_pragma (pfile, "GCC", "system_header", do_pragma_system_header,
1083 false, true);
1084 register_pragma (pfile, "GCC", "dependency", do_pragma_dependency,
1085 false, true);
Nathan Sidwell82443372000-06-23 10:56:09 +00001086}
Per Bothner7f2935c1995-03-16 13:59:07 -08001087
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001088/* Return the number of registered pragmas in PE. */
1089
1090static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001091count_registered_pragmas (struct pragma_entry *pe)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001092{
1093 int ct = 0;
1094 for (; pe != NULL; pe = pe->next)
1095 {
1096 if (pe->is_nspace)
1097 ct += count_registered_pragmas (pe->u.space);
1098 ct++;
1099 }
1100 return ct;
1101}
1102
1103/* Save into SD the names of the registered pragmas referenced by PE,
1104 and return a pointer to the next free space in SD. */
1105
1106static char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001107save_registered_pragmas (struct pragma_entry *pe, char **sd)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001108{
1109 for (; pe != NULL; pe = pe->next)
1110 {
1111 if (pe->is_nspace)
1112 sd = save_registered_pragmas (pe->u.space, sd);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001113 *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
1114 HT_LEN (&pe->pragma->ident),
1115 HT_LEN (&pe->pragma->ident) + 1);
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001116 }
1117 return sd;
1118}
1119
1120/* Return a newly-allocated array which saves the names of the
1121 registered pragmas. */
1122
1123char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001124_cpp_save_pragma_names (cpp_reader *pfile)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001125{
1126 int ct = count_registered_pragmas (pfile->pragmas);
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001127 char **result = XNEWVEC (char *, ct);
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001128 (void) save_registered_pragmas (pfile->pragmas, result);
1129 return result;
1130}
1131
1132/* Restore from SD the names of the registered pragmas referenced by PE,
1133 and return a pointer to the next unused name in SD. */
1134
1135static char **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001136restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1137 char **sd)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001138{
1139 for (; pe != NULL; pe = pe->next)
1140 {
1141 if (pe->is_nspace)
1142 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1143 pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd));
1144 free (*sd);
1145 sd++;
1146 }
1147 return sd;
1148}
1149
1150/* Restore the names of the registered pragmas from SAVED. */
1151
1152void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001153_cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
Geoffrey Keating17211ab2003-01-10 02:22:34 +00001154{
1155 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1156 free (saved);
1157}
1158
Neil Bootha5da89c2001-10-14 17:44:00 +00001159/* Pragmata handling. We handle some, and pass the rest on to the
1160 front end. C99 defines three pragmas and says that no macro
1161 expansion is to be performed on them; whether or not macro
1162 expansion happens for other pragmas is implementation defined.
Zack Weinberg21b11492004-09-09 19:16:56 +00001163 This implementation never macro-expands the text after #pragma.
1164
1165 The library user has the option of deferring execution of
1166 #pragmas not handled by cpplib, in which case they are converted
1167 to CPP_PRAGMA tokens and inserted into the output stream. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001168static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001169do_pragma (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001170{
Neil Bootha5da89c2001-10-14 17:44:00 +00001171 const struct pragma_entry *p = NULL;
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001172 const cpp_token *token, *pragma_token = pfile->cur_token;
Neil Bootha5da89c2001-10-14 17:44:00 +00001173 unsigned int count = 1;
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001174
Zack Weinberg8f8e9aa2004-09-18 00:53:50 +00001175 /* Save the current position so that defer_pragmas mode can
1176 copy the entire current line to a string. It will not work
1177 to use _cpp_backup_tokens as that does not reverse buffer->cur. */
1178 const uchar *line_start = CPP_BUFFER (pfile)->cur;
1179
Neil Booth93c803682000-10-28 17:59:06 +00001180 pfile->state.prevent_expansion++;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001181
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001182 token = cpp_get_token (pfile);
1183 if (token->type == CPP_NAME)
Alexandre Oliva0172e2b2000-02-27 06:24:27 +00001184 {
Neil Booth4b115ff2001-10-14 23:04:13 +00001185 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001186 if (p && p->is_nspace)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001187 {
Neil Bootha5da89c2001-10-14 17:44:00 +00001188 count = 2;
1189 token = cpp_get_token (pfile);
1190 if (token->type == CPP_NAME)
Neil Booth4b115ff2001-10-14 23:04:13 +00001191 p = lookup_pragma_entry (p->u.space, token->val.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001192 else
1193 p = NULL;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001194 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001195 }
1196
Zack Weinberg3da3d582004-10-27 17:29:29 +00001197 if (p)
Alexandre Olivae2e1fa52003-09-24 23:53:07 +00001198 {
Zack Weinberg3da3d582004-10-27 17:29:29 +00001199 if (p->is_internal || !CPP_OPTION (pfile, defer_pragmas))
1200 {
1201 /* Since the handler below doesn't get the line number, that it
1202 might need for diagnostics, make sure it has the right
1203 numbers in place. */
1204 if (pfile->cb.line_change)
1205 (*pfile->cb.line_change) (pfile, pragma_token, false);
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001206 /* Never expand macros if handling a deferred pragma, since
1207 the macro definitions now applicable may be different
1208 from those at the point the pragma appeared. */
1209 if (p->allow_expansion && !pfile->state.in_deferred_pragma)
1210 pfile->state.prevent_expansion--;
Zack Weinberg3da3d582004-10-27 17:29:29 +00001211 (*p->u.handler) (pfile);
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001212 if (p->allow_expansion && !pfile->state.in_deferred_pragma)
1213 pfile->state.prevent_expansion++;
Zack Weinberg3da3d582004-10-27 17:29:29 +00001214 }
1215 else
1216 {
1217 /* Squirrel away the pragma text. Pragmas are
1218 newline-terminated. */
1219 const uchar *line_end;
1220 uchar *s;
1221 cpp_string body;
1222 cpp_token *ptok;
Zack Weinberg21b11492004-09-09 19:16:56 +00001223
Zack Weinberg3da3d582004-10-27 17:29:29 +00001224 line_end = ustrchr (line_start, '\n');
Zack Weinberg21b11492004-09-09 19:16:56 +00001225
Zack Weinberg3da3d582004-10-27 17:29:29 +00001226 body.len = (line_end - line_start) + 1;
1227 s = _cpp_unaligned_alloc (pfile, body.len + 1);
1228 memcpy (s, line_start, body.len);
1229 s[body.len] = '\0';
1230 body.text = s;
Zack Weinberg21b11492004-09-09 19:16:56 +00001231
Zack Weinberg3da3d582004-10-27 17:29:29 +00001232 /* Create a CPP_PRAGMA token. */
1233 ptok = &pfile->directive_result;
1234 ptok->src_loc = pragma_token->src_loc;
1235 ptok->type = CPP_PRAGMA;
1236 ptok->flags = pragma_token->flags | NO_EXPAND;
1237 ptok->val.str = body;
1238 }
Zack Weinberg21b11492004-09-09 19:16:56 +00001239 }
Neil Boothd82fc102001-08-02 23:03:31 +00001240 else if (pfile->cb.def_pragma)
Neil Boothbdcbe492001-09-13 20:05:17 +00001241 {
1242 _cpp_backup_tokens (pfile, count);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001243 pfile->cb.def_pragma (pfile, pfile->directive_line);
Neil Boothbdcbe492001-09-13 20:05:17 +00001244 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001245
Neil Booth97293892001-09-14 22:04:46 +00001246 pfile->state.prevent_expansion--;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001247}
1248
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001249/* Handle #pragma once. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001250static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001251do_pragma_once (cpp_reader *pfile)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001252{
Neil Booth642ce432000-12-07 23:17:56 +00001253 if (pfile->buffer->prev == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001254 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
Neil Booth93c803682000-10-28 17:59:06 +00001255
1256 check_eol (pfile);
Neil Booth49634b32003-08-02 16:29:46 +00001257 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001258}
1259
Neil Boothc3bf3e62002-05-09 17:14:22 +00001260/* Handle #pragma GCC poison, to poison one or more identifiers so
1261 that the lexer produces a hard error for each subsequent usage. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001262static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001263do_pragma_poison (cpp_reader *pfile)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001264{
Neil Booth345894b2001-09-16 13:44:29 +00001265 const cpp_token *tok;
Zack Weinbergf8f769e2000-05-28 05:56:38 +00001266 cpp_hashnode *hp;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001267
Neil Booth93c803682000-10-28 17:59:06 +00001268 pfile->state.poisoned_ok = 1;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001269 for (;;)
1270 {
Neil Booth345894b2001-09-16 13:44:29 +00001271 tok = _cpp_lex_token (pfile);
1272 if (tok->type == CPP_EOF)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001273 break;
Neil Booth345894b2001-09-16 13:44:29 +00001274 if (tok->type != CPP_NAME)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001275 {
John David Anglin0527bc42003-11-01 22:56:54 +00001276 cpp_error (pfile, CPP_DL_ERROR,
1277 "invalid #pragma GCC poison directive");
Neil Booth93c803682000-10-28 17:59:06 +00001278 break;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001279 }
1280
Neil Booth345894b2001-09-16 13:44:29 +00001281 hp = tok->val.node;
Neil Booth93c803682000-10-28 17:59:06 +00001282 if (hp->flags & NODE_POISONED)
1283 continue;
1284
1285 if (hp->type == NT_MACRO)
John David Anglin0527bc42003-11-01 22:56:54 +00001286 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
Neil Boothebef4e82002-04-14 18:42:47 +00001287 NODE_NAME (hp));
Neil Booth93c803682000-10-28 17:59:06 +00001288 _cpp_free_definition (hp);
1289 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001290 }
Neil Booth93c803682000-10-28 17:59:06 +00001291 pfile->state.poisoned_ok = 0;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001292}
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001293
1294/* Mark the current header as a system header. This will suppress
1295 some categories of warnings (notably those from -pedantic). It is
1296 intended for use in system libraries that cannot be implemented in
1297 conforming C, but cannot be certain that their headers appear in a
1298 system include directory. To prevent abuse, it is rejected in the
1299 primary source file. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001300static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001301do_pragma_system_header (cpp_reader *pfile)
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001302{
Neil Booth614c7d32000-12-04 07:32:04 +00001303 cpp_buffer *buffer = pfile->buffer;
1304
1305 if (buffer->prev == 0)
John David Anglin0527bc42003-11-01 22:56:54 +00001306 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +00001307 "#pragma system_header ignored outside include file");
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001308 else
Neil Boothd82fc102001-08-02 23:03:31 +00001309 {
1310 check_eol (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +00001311 skip_rest_of_line (pfile);
Neil Boothd82fc102001-08-02 23:03:31 +00001312 cpp_make_system_header (pfile, 1, 0);
1313 }
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001314}
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001315
1316/* Check the modified date of the current include file against a specified
1317 file. Issue a diagnostic, if the specified file is newer. We use this to
1318 determine if a fixed header should be refixed. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001319static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001320do_pragma_dependency (cpp_reader *pfile)
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001321{
Neil Booth74eb4b32003-04-21 19:21:59 +00001322 const char *fname;
1323 int angle_brackets, ordering;
Kazu Hiratadf383482002-05-22 22:02:16 +00001324
Neil Booth74eb4b32003-04-21 19:21:59 +00001325 fname = parse_include (pfile, &angle_brackets);
1326 if (!fname)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001327 return;
Zack Weinberg041c3192000-07-04 01:58:21 +00001328
Neil Booth74eb4b32003-04-21 19:21:59 +00001329 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001330 if (ordering < 0)
John David Anglin0527bc42003-11-01 22:56:54 +00001331 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001332 else if (ordering > 0)
1333 {
John David Anglin0527bc42003-11-01 22:56:54 +00001334 cpp_error (pfile, CPP_DL_WARNING,
1335 "current file is older than %s", fname);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001336 if (cpp_get_token (pfile)->type != CPP_EOF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001337 {
1338 _cpp_backup_tokens (pfile, 1);
John David Anglin0527bc42003-11-01 22:56:54 +00001339 do_diagnostic (pfile, CPP_DL_WARNING, 0);
Neil Boothbdcbe492001-09-13 20:05:17 +00001340 }
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001341 }
Neil Booth74eb4b32003-04-21 19:21:59 +00001342
Kaveh R. Ghazifad205f2003-06-16 21:41:10 +00001343 free ((void *) fname);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001344}
1345
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001346/* Get a token but skip padding. */
1347static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001348get_token_no_padding (cpp_reader *pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001349{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001350 for (;;)
1351 {
1352 const cpp_token *result = cpp_get_token (pfile);
1353 if (result->type != CPP_PADDING)
1354 return result;
1355 }
1356}
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001357
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001358/* Check syntax is "(string-literal)". Returns the string on success,
1359 or NULL on failure. */
1360static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001361get__Pragma_string (cpp_reader *pfile)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001362{
1363 const cpp_token *string;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001364
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001365 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1366 return NULL;
1367
1368 string = get_token_no_padding (pfile);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001369 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001370 return NULL;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001371
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001372 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1373 return NULL;
1374
1375 return string;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001376}
1377
Neil Booth87062812001-10-20 09:00:53 +00001378/* Destringize IN into a temporary buffer, by removing the first \ of
1379 \" and \\ sequences, and process the result as a #pragma directive. */
1380static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001381destringize_and_run (cpp_reader *pfile, const cpp_string *in)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001382{
1383 const unsigned char *src, *limit;
Neil Booth87062812001-10-20 09:00:53 +00001384 char *dest, *result;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001385
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001386 dest = result = (char *) alloca (in->len - 1);
Neil Booth6338b352003-04-23 22:44:06 +00001387 src = in->text + 1 + (in->text[0] == 'L');
1388 limit = in->text + in->len - 1;
1389 while (src < limit)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001390 {
1391 /* We know there is a character following the backslash. */
1392 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1393 src++;
1394 *dest++ = *src++;
1395 }
Neil Booth26aea072003-04-19 00:22:51 +00001396 *dest = '\n';
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001397
Neil Booth8128ccc2002-11-18 20:43:40 +00001398 /* Ugh; an awful kludge. We are really not set up to be lexing
1399 tokens when in the middle of a macro expansion. Use a new
1400 context to force cpp_get_token to lex, and so skip_rest_of_line
1401 doesn't go beyond the end of the text. Also, remember the
1402 current lexing position so we can return to it later.
Neil Booth79ba5e32002-09-03 21:55:40 +00001403
Neil Booth8128ccc2002-11-18 20:43:40 +00001404 Something like line-at-a-time lexing should remove the need for
1405 this. */
1406 {
1407 cpp_context *saved_context = pfile->context;
1408 cpp_token *saved_cur_token = pfile->cur_token;
1409 tokenrun *saved_cur_run = pfile->cur_run;
1410
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001411 pfile->context = XNEW (cpp_context);
Neil Booth8128ccc2002-11-18 20:43:40 +00001412 pfile->context->macro = 0;
1413 pfile->context->prev = 0;
1414 run_directive (pfile, T_PRAGMA, result, dest - result);
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001415 XDELETE (pfile->context);
Neil Booth8128ccc2002-11-18 20:43:40 +00001416 pfile->context = saved_context;
1417 pfile->cur_token = saved_cur_token;
1418 pfile->cur_run = saved_cur_run;
Neil Booth8128ccc2002-11-18 20:43:40 +00001419 }
Neil Booth79ba5e32002-09-03 21:55:40 +00001420
1421 /* See above comment. For the moment, we'd like
1422
1423 token1 _Pragma ("foo") token2
1424
1425 to be output as
1426
1427 token1
1428 # 7 "file.c"
1429 #pragma foo
1430 # 7 "file.c"
1431 token2
1432
1433 Getting the line markers is a little tricky. */
1434 if (pfile->cb.line_change)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001435 pfile->cb.line_change (pfile, pfile->cur_token, false);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001436}
1437
Neil Booth87062812001-10-20 09:00:53 +00001438/* Handle the _Pragma operator. */
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001439void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001440_cpp_do__Pragma (cpp_reader *pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001441{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001442 const cpp_token *string = get__Pragma_string (pfile);
Zack Weinberg21b11492004-09-09 19:16:56 +00001443 pfile->directive_result.type = CPP_PADDING;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001444
Neil Booth79ba5e32002-09-03 21:55:40 +00001445 if (string)
1446 destringize_and_run (pfile, &string->val.str);
1447 else
John David Anglin0527bc42003-11-01 22:56:54 +00001448 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00001449 "_Pragma takes a parenthesized string literal");
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001450}
1451
Zack Weinberg21b11492004-09-09 19:16:56 +00001452/* Handle a pragma that the front end deferred until now. */
1453void
1454cpp_handle_deferred_pragma (cpp_reader *pfile, const cpp_string *s)
1455{
1456 cpp_context *saved_context = pfile->context;
1457 cpp_token *saved_cur_token = pfile->cur_token;
1458 tokenrun *saved_cur_run = pfile->cur_run;
1459 bool saved_defer_pragmas = CPP_OPTION (pfile, defer_pragmas);
Per Bothner67a74142004-09-29 18:19:46 -07001460 void (*saved_line_change) (cpp_reader *, const cpp_token *, int)
1461 = pfile->cb.line_change;
Zack Weinberg21b11492004-09-09 19:16:56 +00001462
1463 pfile->context = XNEW (cpp_context);
1464 pfile->context->macro = 0;
1465 pfile->context->prev = 0;
Per Bothner67a74142004-09-29 18:19:46 -07001466 pfile->cb.line_change = NULL;
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001467 pfile->state.in_deferred_pragma = true;
Zack Weinberg21b11492004-09-09 19:16:56 +00001468 CPP_OPTION (pfile, defer_pragmas) = false;
1469
Zack Weinberg3da3d582004-10-27 17:29:29 +00001470 run_directive (pfile, T_PRAGMA, (const char *)s->text, s->len);
Zack Weinberg21b11492004-09-09 19:16:56 +00001471
1472 XDELETE (pfile->context);
1473 pfile->context = saved_context;
1474 pfile->cur_token = saved_cur_token;
1475 pfile->cur_run = saved_cur_run;
Per Bothner67a74142004-09-29 18:19:46 -07001476 pfile->cb.line_change = saved_line_change;
Daniel Jacobowitzb5b3e362004-11-23 23:25:40 +00001477 pfile->state.in_deferred_pragma = false;
Zack Weinberg21b11492004-09-09 19:16:56 +00001478 CPP_OPTION (pfile, defer_pragmas) = saved_defer_pragmas;
1479}
1480
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001481/* Handle #ifdef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001482static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001483do_ifdef (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +00001484{
Neil Booth93c803682000-10-28 17:59:06 +00001485 int skip = 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001486
Neil Boothcef0d192001-07-26 06:02:47 +00001487 if (! pfile->state.skipping)
Neil Booth93c803682000-10-28 17:59:06 +00001488 {
1489 const cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +00001490
Neil Booth93c803682000-10-28 17:59:06 +00001491 if (node)
Neil Bootha69cbaa2002-07-23 22:57:49 +00001492 {
1493 skip = node->type != NT_MACRO;
1494 _cpp_mark_macro_used (node);
1495 check_eol (pfile);
1496 }
Neil Booth93c803682000-10-28 17:59:06 +00001497 }
1498
1499 push_conditional (pfile, skip, T_IFDEF, 0);
Zack Weinberg168d3732000-03-14 06:34:11 +00001500}
1501
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001502/* Handle #ifndef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001503static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001504do_ifndef (cpp_reader *pfile)
Zack Weinberg168d3732000-03-14 06:34:11 +00001505{
Neil Booth93c803682000-10-28 17:59:06 +00001506 int skip = 1;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001507 const cpp_hashnode *node = 0;
Zack Weinberg168d3732000-03-14 06:34:11 +00001508
Neil Boothcef0d192001-07-26 06:02:47 +00001509 if (! pfile->state.skipping)
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001510 {
Neil Booth93c803682000-10-28 17:59:06 +00001511 node = lex_macro_node (pfile);
Geoffrey Keatingb43db0b2000-12-02 22:28:44 +00001512
1513 if (node)
Neil Bootha69cbaa2002-07-23 22:57:49 +00001514 {
1515 skip = node->type == NT_MACRO;
1516 _cpp_mark_macro_used (node);
1517 check_eol (pfile);
1518 }
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001519 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001520
Neil Booth93c803682000-10-28 17:59:06 +00001521 push_conditional (pfile, skip, T_IFNDEF, node);
Per Bothner7f2935c1995-03-16 13:59:07 -08001522}
1523
Neil Booth6d18adb2001-07-29 17:27:57 +00001524/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1525 pfile->mi_ind_cmacro so we can handle multiple-include
Kazu Hirata6356f892003-06-12 19:01:08 +00001526 optimizations. If macro expansion occurs in the expression, we
Neil Booth6d18adb2001-07-29 17:27:57 +00001527 cannot treat it as a controlling conditional, since the expansion
1528 could change in the future. That is handled by cpp_get_token. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001529static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001530do_if (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001531{
Neil Booth93c803682000-10-28 17:59:06 +00001532 int skip = 1;
Per Bothner7f2935c1995-03-16 13:59:07 -08001533
Neil Boothcef0d192001-07-26 06:02:47 +00001534 if (! pfile->state.skipping)
Neil Booth87ed1092002-04-28 19:42:54 +00001535 skip = _cpp_parse_expr (pfile) == false;
Neil Booth93c803682000-10-28 17:59:06 +00001536
Neil Booth6d18adb2001-07-29 17:27:57 +00001537 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
Per Bothner7f2935c1995-03-16 13:59:07 -08001538}
1539
Neil Boothb528a072000-11-12 11:46:21 +00001540/* Flip skipping state if appropriate and continue without changing
Zack Weinbergea4a4532000-05-29 16:19:32 +00001541 if_stack; this is so that the error message for missing #endif's
1542 etc. will point to the original #if. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001543static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001544do_else (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001545{
Neil Boothb528a072000-11-12 11:46:21 +00001546 cpp_buffer *buffer = pfile->buffer;
1547 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001548
1549 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001550 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
Neil Booth93c803682000-10-28 17:59:06 +00001551 else
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001552 {
Neil Booth93c803682000-10-28 17:59:06 +00001553 if (ifs->type == T_ELSE)
1554 {
John David Anglin0527bc42003-11-01 22:56:54 +00001555 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1556 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Booth93c803682000-10-28 17:59:06 +00001557 "the conditional began here");
1558 }
Neil Boothb528a072000-11-12 11:46:21 +00001559 ifs->type = T_ELSE;
1560
Neil Boothcef0d192001-07-26 06:02:47 +00001561 /* Skip any future (erroneous) #elses or #elifs. */
1562 pfile->state.skipping = ifs->skip_elses;
1563 ifs->skip_elses = true;
Neil Booth93c803682000-10-28 17:59:06 +00001564
1565 /* Invalidate any controlling macro. */
1566 ifs->mi_cmacro = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001567
Neil Boothcef0d192001-07-26 06:02:47 +00001568 /* Only check EOL if was not originally skipping. */
Phil Edwards909de5d2002-03-22 21:59:04 +00001569 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
Neil Boothcef0d192001-07-26 06:02:47 +00001570 check_eol (pfile);
1571 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001572}
1573
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001574/* Handle a #elif directive by not changing if_stack either. See the
Neil Booth93c803682000-10-28 17:59:06 +00001575 comment above do_else. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001576static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001577do_elif (cpp_reader *pfile)
Zack Weinbergea4a4532000-05-29 16:19:32 +00001578{
Neil Boothb528a072000-11-12 11:46:21 +00001579 cpp_buffer *buffer = pfile->buffer;
1580 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001581
1582 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001583 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
Neil Boothb528a072000-11-12 11:46:21 +00001584 else
Zack Weinbergea4a4532000-05-29 16:19:32 +00001585 {
Neil Boothb528a072000-11-12 11:46:21 +00001586 if (ifs->type == T_ELSE)
1587 {
John David Anglin0527bc42003-11-01 22:56:54 +00001588 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1589 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Boothb528a072000-11-12 11:46:21 +00001590 "the conditional began here");
1591 }
1592 ifs->type = T_ELIF;
1593
Neil Boothcef0d192001-07-26 06:02:47 +00001594 /* Only evaluate this if we aren't skipping elses. During
1595 evaluation, set skipping to false to get lexer warnings. */
1596 if (ifs->skip_elses)
1597 pfile->state.skipping = 1;
1598 else
Neil Boothb528a072000-11-12 11:46:21 +00001599 {
Neil Boothcef0d192001-07-26 06:02:47 +00001600 pfile->state.skipping = 0;
1601 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1602 ifs->skip_elses = ! pfile->state.skipping;
Neil Boothb528a072000-11-12 11:46:21 +00001603 }
Neil Boothcef0d192001-07-26 06:02:47 +00001604
1605 /* Invalidate any controlling macro. */
1606 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001607 }
Zack Weinbergea4a4532000-05-29 16:19:32 +00001608}
1609
Neil Boothcef0d192001-07-26 06:02:47 +00001610/* #endif pops the if stack and resets pfile->state.skipping. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001611static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001612do_endif (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001613{
Neil Boothb528a072000-11-12 11:46:21 +00001614 cpp_buffer *buffer = pfile->buffer;
1615 struct if_stack *ifs = buffer->if_stack;
Per Bothner7f2935c1995-03-16 13:59:07 -08001616
Zack Weinbergea4a4532000-05-29 16:19:32 +00001617 if (ifs == NULL)
John David Anglin0527bc42003-11-01 22:56:54 +00001618 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
Per Bothner7f2935c1995-03-16 13:59:07 -08001619 else
1620 {
Neil Boothcef0d192001-07-26 06:02:47 +00001621 /* Only check EOL if was not originally skipping. */
Phil Edwards909de5d2002-03-22 21:59:04 +00001622 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
Neil Boothcef0d192001-07-26 06:02:47 +00001623 check_eol (pfile);
1624
Neil Booth93c803682000-10-28 17:59:06 +00001625 /* If potential control macro, we go back outside again. */
1626 if (ifs->next == 0 && ifs->mi_cmacro)
1627 {
Neil Booth6d18adb2001-07-29 17:27:57 +00001628 pfile->mi_valid = true;
Neil Booth93c803682000-10-28 17:59:06 +00001629 pfile->mi_cmacro = ifs->mi_cmacro;
1630 }
1631
Neil Boothb528a072000-11-12 11:46:21 +00001632 buffer->if_stack = ifs->next;
Neil Boothcef0d192001-07-26 06:02:47 +00001633 pfile->state.skipping = ifs->was_skipping;
Neil Booth2a967f32001-05-20 06:26:45 +00001634 obstack_free (&pfile->buffer_ob, ifs);
Per Bothner7f2935c1995-03-16 13:59:07 -08001635 }
Neil Booth93c803682000-10-28 17:59:06 +00001636}
Zack Weinberg041c3192000-07-04 01:58:21 +00001637
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001638/* Push an if_stack entry for a preprocessor conditional, and set
1639 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1640 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1641 we need to check here that we are at the top of the file. */
Zack Weinbergea4a4532000-05-29 16:19:32 +00001642static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001643push_conditional (cpp_reader *pfile, int skip, int type,
1644 const cpp_hashnode *cmacro)
Zack Weinbergea4a4532000-05-29 16:19:32 +00001645{
1646 struct if_stack *ifs;
Neil Boothb528a072000-11-12 11:46:21 +00001647 cpp_buffer *buffer = pfile->buffer;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001648
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001649 ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
Neil Booth50410422001-09-15 10:18:03 +00001650 ifs->line = pfile->directive_line;
Neil Boothb528a072000-11-12 11:46:21 +00001651 ifs->next = buffer->if_stack;
Neil Boothcef0d192001-07-26 06:02:47 +00001652 ifs->skip_elses = pfile->state.skipping || !skip;
1653 ifs->was_skipping = pfile->state.skipping;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001654 ifs->type = type;
Neil Booth6d18adb2001-07-29 17:27:57 +00001655 /* This condition is effectively a test for top-of-file. */
1656 if (pfile->mi_valid && pfile->mi_cmacro == 0)
Neil Booth93c803682000-10-28 17:59:06 +00001657 ifs->mi_cmacro = cmacro;
1658 else
1659 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001660
Neil Boothcef0d192001-07-26 06:02:47 +00001661 pfile->state.skipping = skip;
Neil Boothb528a072000-11-12 11:46:21 +00001662 buffer->if_stack = ifs;
Zack Weinberg7061aa51998-12-15 11:09:16 +00001663}
1664
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001665/* Read the tokens of the answer into the macro pool, in a directive
1666 of type TYPE. Only commit the memory if we intend it as permanent
1667 storage, i.e. the #assert case. Returns 0 on success, and sets
1668 ANSWERP to point to the answer. */
Neil Booth93c803682000-10-28 17:59:06 +00001669static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001670parse_answer (cpp_reader *pfile, struct answer **answerp, int type)
Zack Weinberg041c3192000-07-04 01:58:21 +00001671{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001672 const cpp_token *paren;
Neil Booth93c803682000-10-28 17:59:06 +00001673 struct answer *answer;
Neil Booth8c3b2692001-09-30 10:03:11 +00001674 unsigned int acount;
Zack Weinberg041c3192000-07-04 01:58:21 +00001675
Neil Booth93c803682000-10-28 17:59:06 +00001676 /* In a conditional, it is legal to not have an open paren. We
1677 should save the following token in this case. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001678 paren = cpp_get_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001679
1680 /* If not a paren, see if we're OK. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001681 if (paren->type != CPP_OPEN_PAREN)
Zack Weinberg041c3192000-07-04 01:58:21 +00001682 {
Neil Booth93c803682000-10-28 17:59:06 +00001683 /* In a conditional no answer is a test for any answer. It
1684 could be followed by any token. */
1685 if (type == T_IF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001686 {
1687 _cpp_backup_tokens (pfile, 1);
1688 return 0;
1689 }
Neil Booth93c803682000-10-28 17:59:06 +00001690
1691 /* #unassert with no answer is valid - it removes all answers. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001692 if (type == T_UNASSERT && paren->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +00001693 return 0;
1694
John David Anglin0527bc42003-11-01 22:56:54 +00001695 cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate");
Neil Booth93c803682000-10-28 17:59:06 +00001696 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001697 }
1698
Neil Booth8c3b2692001-09-30 10:03:11 +00001699 for (acount = 0;; acount++)
Zack Weinberg041c3192000-07-04 01:58:21 +00001700 {
Neil Booth8c3b2692001-09-30 10:03:11 +00001701 size_t room_needed;
1702 const cpp_token *token = cpp_get_token (pfile);
1703 cpp_token *dest;
Zack Weinberg041c3192000-07-04 01:58:21 +00001704
Neil Booth93c803682000-10-28 17:59:06 +00001705 if (token->type == CPP_CLOSE_PAREN)
1706 break;
Zack Weinberg041c3192000-07-04 01:58:21 +00001707
1708 if (token->type == CPP_EOF)
1709 {
John David Anglin0527bc42003-11-01 22:56:54 +00001710 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
Neil Booth93c803682000-10-28 17:59:06 +00001711 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001712 }
Neil Booth8c3b2692001-09-30 10:03:11 +00001713
1714 /* struct answer includes the space for one token. */
1715 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1716
1717 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1718 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1719
1720 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1721 *dest = *token;
1722
1723 /* Drop whitespace at start, for answer equivalence purposes. */
1724 if (acount == 0)
1725 dest->flags &= ~PREV_WHITE;
Zack Weinberg041c3192000-07-04 01:58:21 +00001726 }
1727
Neil Booth8c3b2692001-09-30 10:03:11 +00001728 if (acount == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001729 {
John David Anglin0527bc42003-11-01 22:56:54 +00001730 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
Neil Booth93c803682000-10-28 17:59:06 +00001731 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001732 }
1733
Neil Booth8c3b2692001-09-30 10:03:11 +00001734 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1735 answer->count = acount;
1736 answer->next = NULL;
Neil Booth93c803682000-10-28 17:59:06 +00001737 *answerp = answer;
Zack Weinberg041c3192000-07-04 01:58:21 +00001738
Neil Booth93c803682000-10-28 17:59:06 +00001739 return 0;
1740}
1741
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001742/* Parses an assertion directive of type TYPE, returning a pointer to
1743 the hash node of the predicate, or 0 on error. If an answer was
1744 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
Neil Booth93c803682000-10-28 17:59:06 +00001745static cpp_hashnode *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001746parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
Neil Booth93c803682000-10-28 17:59:06 +00001747{
1748 cpp_hashnode *result = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001749 const cpp_token *predicate;
Neil Booth93c803682000-10-28 17:59:06 +00001750
1751 /* We don't expand predicates or answers. */
1752 pfile->state.prevent_expansion++;
1753
Neil Booth93c803682000-10-28 17:59:06 +00001754 *answerp = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001755 predicate = cpp_get_token (pfile);
1756 if (predicate->type == CPP_EOF)
John David Anglin0527bc42003-11-01 22:56:54 +00001757 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001758 else if (predicate->type != CPP_NAME)
John David Anglin0527bc42003-11-01 22:56:54 +00001759 cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier");
Neil Booth93c803682000-10-28 17:59:06 +00001760 else if (parse_answer (pfile, answerp, type) == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001761 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001762 unsigned int len = NODE_LEN (predicate->val.node);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001763 unsigned char *sym = (unsigned char *) alloca (len + 1);
Neil Booth93c803682000-10-28 17:59:06 +00001764
1765 /* Prefix '#' to get it out of macro namespace. */
1766 sym[0] = '#';
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001767 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
Neil Booth93c803682000-10-28 17:59:06 +00001768 result = cpp_lookup (pfile, sym, len + 1);
Zack Weinberg041c3192000-07-04 01:58:21 +00001769 }
1770
Neil Booth93c803682000-10-28 17:59:06 +00001771 pfile->state.prevent_expansion--;
1772 return result;
Zack Weinberg041c3192000-07-04 01:58:21 +00001773}
1774
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001775/* Returns a pointer to the pointer to CANDIDATE in the answer chain,
Zack Weinberg041c3192000-07-04 01:58:21 +00001776 or a pointer to NULL if the answer is not in the chain. */
Neil Booth93c803682000-10-28 17:59:06 +00001777static struct answer **
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001778find_answer (cpp_hashnode *node, const struct answer *candidate)
Zack Weinberg041c3192000-07-04 01:58:21 +00001779{
Neil Booth93c803682000-10-28 17:59:06 +00001780 unsigned int i;
Zack Weinberg041c3192000-07-04 01:58:21 +00001781 struct answer **result;
1782
1783 for (result = &node->value.answers; *result; result = &(*result)->next)
Neil Booth93c803682000-10-28 17:59:06 +00001784 {
1785 struct answer *answer = *result;
1786
1787 if (answer->count == candidate->count)
1788 {
1789 for (i = 0; i < answer->count; i++)
1790 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1791 break;
1792
1793 if (i == answer->count)
1794 break;
1795 }
1796 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001797
1798 return result;
1799}
1800
Neil Booth93c803682000-10-28 17:59:06 +00001801/* Test an assertion within a preprocessor conditional. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +00001802 nonzero on failure, zero on success. On success, the result of
Hans-Peter Nilsson24026452002-11-29 22:41:04 +00001803 the test is written into VALUE, otherwise the value 0. */
Neil Booth93c803682000-10-28 17:59:06 +00001804int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001805_cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
Neil Booth93c803682000-10-28 17:59:06 +00001806{
1807 struct answer *answer;
1808 cpp_hashnode *node;
1809
1810 node = parse_assertion (pfile, &answer, T_IF);
Hans-Peter Nilsson24026452002-11-29 22:41:04 +00001811
1812 /* For recovery, an erroneous assertion expression is handled as a
1813 failing assertion. */
1814 *value = 0;
1815
Neil Booth93c803682000-10-28 17:59:06 +00001816 if (node)
1817 *value = (node->type == NT_ASSERTION &&
1818 (answer == 0 || *find_answer (node, answer) != 0));
Neil Booth91318902002-05-26 18:42:21 +00001819 else if (pfile->cur_token[-1].type == CPP_EOF)
1820 _cpp_backup_tokens (pfile, 1);
Neil Booth93c803682000-10-28 17:59:06 +00001821
1822 /* We don't commit the memory for the answer - it's temporary only. */
1823 return node == 0;
1824}
1825
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001826/* Handle #assert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001827static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001828do_assert (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001829{
Zack Weinberg041c3192000-07-04 01:58:21 +00001830 struct answer *new_answer;
1831 cpp_hashnode *node;
Kazu Hiratadf383482002-05-22 22:02:16 +00001832
Neil Booth93c803682000-10-28 17:59:06 +00001833 node = parse_assertion (pfile, &new_answer, T_ASSERT);
Zack Weinberg041c3192000-07-04 01:58:21 +00001834 if (node)
1835 {
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001836 size_t answer_size;
1837
Neil Booth93c803682000-10-28 17:59:06 +00001838 /* Place the new answer in the answer list. First check there
1839 is not a duplicate. */
Zack Weinberg041c3192000-07-04 01:58:21 +00001840 new_answer->next = 0;
Neil Booth93c803682000-10-28 17:59:06 +00001841 if (node->type == NT_ASSERTION)
Zack Weinberg041c3192000-07-04 01:58:21 +00001842 {
Neil Booth93c803682000-10-28 17:59:06 +00001843 if (*find_answer (node, new_answer))
1844 {
John David Anglin0527bc42003-11-01 22:56:54 +00001845 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
Neil Boothebef4e82002-04-14 18:42:47 +00001846 NODE_NAME (node) + 1);
Neil Booth93c803682000-10-28 17:59:06 +00001847 return;
1848 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001849 new_answer->next = node->value.answers;
1850 }
Neil Booth8c3b2692001-09-30 10:03:11 +00001851
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001852 answer_size = sizeof (struct answer) + ((new_answer->count - 1)
1853 * sizeof (cpp_token));
1854 /* Commit or allocate storage for the object. */
1855 if (pfile->hash_table->alloc_subobject)
1856 {
1857 struct answer *temp_answer = new_answer;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001858 new_answer = (struct answer *) pfile->hash_table->alloc_subobject
1859 (answer_size);
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001860 memcpy (new_answer, temp_answer, answer_size);
1861 }
1862 else
1863 BUFF_FRONT (pfile->a_buff) += answer_size;
1864
Neil Booth93c803682000-10-28 17:59:06 +00001865 node->type = NT_ASSERTION;
Zack Weinberg041c3192000-07-04 01:58:21 +00001866 node->value.answers = new_answer;
Neil Booth8c3b2692001-09-30 10:03:11 +00001867 check_eol (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +00001868 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001869}
Zack Weinberg7061aa51998-12-15 11:09:16 +00001870
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001871/* Handle #unassert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001872static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001873do_unassert (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001874{
Zack Weinberg041c3192000-07-04 01:58:21 +00001875 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00001876 struct answer *answer;
Kazu Hiratadf383482002-05-22 22:02:16 +00001877
Neil Booth93c803682000-10-28 17:59:06 +00001878 node = parse_assertion (pfile, &answer, T_UNASSERT);
1879 /* It isn't an error to #unassert something that isn't asserted. */
1880 if (node && node->type == NT_ASSERTION)
Zack Weinberg7061aa51998-12-15 11:09:16 +00001881 {
Zack Weinberg041c3192000-07-04 01:58:21 +00001882 if (answer)
Neil Booth93c803682000-10-28 17:59:06 +00001883 {
1884 struct answer **p = find_answer (node, answer), *temp;
1885
1886 /* Remove the answer from the list. */
1887 temp = *p;
1888 if (temp)
1889 *p = temp->next;
1890
1891 /* Did we free the last answer? */
1892 if (node->value.answers == 0)
1893 node->type = NT_VOID;
Neil Booth8c3b2692001-09-30 10:03:11 +00001894
1895 check_eol (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001896 }
1897 else
1898 _cpp_free_definition (node);
Zack Weinberg7061aa51998-12-15 11:09:16 +00001899 }
Neil Booth93c803682000-10-28 17:59:06 +00001900
1901 /* We don't commit the memory for the answer - it's temporary only. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001902}
Per Bothner7f2935c1995-03-16 13:59:07 -08001903
Zack Weinberg45b966d2000-03-13 22:01:08 +00001904/* These are for -D, -U, -A. */
1905
1906/* Process the string STR as if it appeared as the body of a #define.
1907 If STR is just an identifier, define it with value 1.
1908 If STR has anything after the identifier, then it should
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001909 be identifier=definition. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001910void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001911cpp_define (cpp_reader *pfile, const char *str)
Zack Weinberg45b966d2000-03-13 22:01:08 +00001912{
1913 char *buf, *p;
1914 size_t count;
1915
Kazu Hiratadf383482002-05-22 22:02:16 +00001916 /* Copy the entire option so we can modify it.
Zack Weinberg45b966d2000-03-13 22:01:08 +00001917 Change the first "=" in the string to a space. If there is none,
Neil Booth86368122000-10-31 23:34:59 +00001918 tack " 1" on the end. */
1919
Neil Booth86368122000-10-31 23:34:59 +00001920 count = strlen (str);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001921 buf = (char *) alloca (count + 3);
Neil Booth86368122000-10-31 23:34:59 +00001922 memcpy (buf, str, count);
1923
1924 p = strchr (str, '=');
Zack Weinberg45b966d2000-03-13 22:01:08 +00001925 if (p)
Neil Booth86368122000-10-31 23:34:59 +00001926 buf[p - str] = ' ';
Zack Weinberg45b966d2000-03-13 22:01:08 +00001927 else
1928 {
Neil Booth86368122000-10-31 23:34:59 +00001929 buf[count++] = ' ';
1930 buf[count++] = '1';
Zack Weinberg45b966d2000-03-13 22:01:08 +00001931 }
Neil Booth26aea072003-04-19 00:22:51 +00001932 buf[count] = '\n';
Zack Weinberg45b966d2000-03-13 22:01:08 +00001933
Neil Booth29401c32001-08-22 20:37:20 +00001934 run_directive (pfile, T_DEFINE, buf, count);
Zack Weinberg2c8f0512000-08-29 18:37:37 +00001935}
1936
Neil Boothad2a0842000-12-17 00:13:54 +00001937/* Slight variant of the above for use by initialize_builtins. */
Zack Weinberg2c8f0512000-08-29 18:37:37 +00001938void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001939_cpp_define_builtin (cpp_reader *pfile, const char *str)
Zack Weinberg2c8f0512000-08-29 18:37:37 +00001940{
Neil Booth26aea072003-04-19 00:22:51 +00001941 size_t len = strlen (str);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001942 char *buf = (char *) alloca (len + 1);
Neil Booth26aea072003-04-19 00:22:51 +00001943 memcpy (buf, str, len);
1944 buf[len] = '\n';
1945 run_directive (pfile, T_DEFINE, buf, len);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001946}
1947
1948/* Process MACRO as if it appeared as the body of an #undef. */
1949void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001950cpp_undef (cpp_reader *pfile, const char *macro)
Zack Weinberg45b966d2000-03-13 22:01:08 +00001951{
Neil Booth26aea072003-04-19 00:22:51 +00001952 size_t len = strlen (macro);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001953 char *buf = (char *) alloca (len + 1);
Neil Booth26aea072003-04-19 00:22:51 +00001954 memcpy (buf, macro, len);
1955 buf[len] = '\n';
1956 run_directive (pfile, T_UNDEF, buf, len);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001957}
1958
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001959/* Process the string STR as if it appeared as the body of a #assert. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001960void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001961cpp_assert (cpp_reader *pfile, const char *str)
Zack Weinberg45b966d2000-03-13 22:01:08 +00001962{
Neil Booth86368122000-10-31 23:34:59 +00001963 handle_assertion (pfile, str, T_ASSERT);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001964}
1965
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001966/* Process STR as if it appeared as the body of an #unassert. */
Zack Weinberg0b22d651999-03-15 18:42:46 +00001967void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001968cpp_unassert (cpp_reader *pfile, const char *str)
Zack Weinberg0b22d651999-03-15 18:42:46 +00001969{
Neil Booth86368122000-10-31 23:34:59 +00001970 handle_assertion (pfile, str, T_UNASSERT);
Kazu Hiratadf383482002-05-22 22:02:16 +00001971}
Zack Weinberg0b22d651999-03-15 18:42:46 +00001972
Neil Booth86368122000-10-31 23:34:59 +00001973/* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1974static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001975handle_assertion (cpp_reader *pfile, const char *str, int type)
Neil Booth86368122000-10-31 23:34:59 +00001976{
1977 size_t count = strlen (str);
1978 const char *p = strchr (str, '=');
1979
Neil Booth26aea072003-04-19 00:22:51 +00001980 /* Copy the entire option so we can modify it. Change the first
1981 "=" in the string to a '(', and tack a ')' on the end. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001982 char *buf = (char *) alloca (count + 2);
Neil Booth26aea072003-04-19 00:22:51 +00001983
1984 memcpy (buf, str, count);
Neil Booth86368122000-10-31 23:34:59 +00001985 if (p)
1986 {
Neil Booth86368122000-10-31 23:34:59 +00001987 buf[p - str] = '(';
1988 buf[count++] = ')';
Neil Booth86368122000-10-31 23:34:59 +00001989 }
Neil Booth26aea072003-04-19 00:22:51 +00001990 buf[count] = '\n';
1991 str = buf;
Neil Booth86368122000-10-31 23:34:59 +00001992
Neil Booth29401c32001-08-22 20:37:20 +00001993 run_directive (pfile, type, str, count);
Neil Booth86368122000-10-31 23:34:59 +00001994}
1995
Neil Booth7e96d762001-01-13 01:00:01 +00001996/* The number of errors for a given reader. */
1997unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001998cpp_errors (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00001999{
2000 return pfile->errors;
2001}
2002
2003/* The options structure. */
2004cpp_options *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002005cpp_get_options (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00002006{
2007 return &pfile->opts;
2008}
2009
2010/* The callbacks structure. */
2011cpp_callbacks *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002012cpp_get_callbacks (cpp_reader *pfile)
Neil Booth7e96d762001-01-13 01:00:01 +00002013{
2014 return &pfile->cb;
2015}
2016
2017/* Copy the given callbacks structure to our own. */
2018void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002019cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
Neil Booth7e96d762001-01-13 01:00:01 +00002020{
2021 pfile->cb = *cb;
2022}
2023
Zack Weinbergc6e83802004-06-05 20:58:06 +00002024/* The dependencies structure. (Creates one if it hasn't already been.) */
2025struct deps *
2026cpp_get_deps (cpp_reader *pfile)
2027{
2028 if (!pfile->deps)
2029 pfile->deps = deps_init ();
2030 return pfile->deps;
2031}
2032
Neil Bootheb1f4d92000-12-18 19:00:26 +00002033/* Push a new buffer on the buffer stack. Returns the new buffer; it
2034 doesn't fail. It does not generate a file change call back; that
2035 is the responsibility of the caller. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00002036cpp_buffer *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002037cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
Per Bothner40de9f72003-10-02 07:30:34 +00002038 int from_stage3)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002039{
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002040 cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
Neil Booth93c803682000-10-28 17:59:06 +00002041
Neil Boothfde84342001-08-06 21:07:41 +00002042 /* Clears, amongst other things, if_stack and mi_cmacro. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002043 memset (new_buffer, 0, sizeof (cpp_buffer));
Neil Boothad2a0842000-12-17 00:13:54 +00002044
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002045 new_buffer->next_line = new_buffer->buf = buffer;
2046 new_buffer->rlimit = buffer + len;
2047 new_buffer->from_stage3 = from_stage3;
2048 new_buffer->prev = pfile->buffer;
2049 new_buffer->need_line = true;
Neil Booth0bda4762000-12-11 07:45:16 +00002050
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002051 pfile->buffer = new_buffer;
Eric Christophercf551fb2004-01-16 22:37:49 +00002052
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002053 return new_buffer;
Zack Weinbergc71f8352000-07-05 05:33:57 +00002054}
2055
Neil Boothaf0d16c2002-04-22 17:48:02 +00002056/* Pops a single buffer, with a file change call-back if appropriate.
2057 Then pushes the next -include file, if any remain. */
Neil Boothef6e9582001-08-04 12:01:59 +00002058void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002059_cpp_pop_buffer (cpp_reader *pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002060{
Neil Boothfde84342001-08-06 21:07:41 +00002061 cpp_buffer *buffer = pfile->buffer;
Neil Booth8f9b4002003-07-29 22:26:13 +00002062 struct _cpp_file *inc = buffer->file;
Neil Boothad2a0842000-12-17 00:13:54 +00002063 struct if_stack *ifs;
Zack Weinbergc71f8352000-07-05 05:33:57 +00002064
Neil Boothfde84342001-08-06 21:07:41 +00002065 /* Walk back up the conditional stack till we reach its level at
2066 entry to this file, issuing error messages. */
2067 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
John David Anglin0527bc42003-11-01 22:56:54 +00002068 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
Neil Boothfde84342001-08-06 21:07:41 +00002069 "unterminated #%s", dtable[ifs->type].name);
2070
Neil Booth97293892001-09-14 22:04:46 +00002071 /* In case of a missing #endif. */
Neil Booth67821e32001-08-05 17:31:25 +00002072 pfile->state.skipping = 0;
Neil Booth29401c32001-08-22 20:37:20 +00002073
Neil Boothaf0d16c2002-04-22 17:48:02 +00002074 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
Neil Booth29401c32001-08-22 20:37:20 +00002075 pfile->buffer = buffer->prev;
2076
Neil Booth26aea072003-04-19 00:22:51 +00002077 free (buffer->notes);
2078
Neil Boothaf0d16c2002-04-22 17:48:02 +00002079 /* Free the buffer object now; we may want to push a new buffer
2080 in _cpp_push_next_include_file. */
2081 obstack_free (&pfile->buffer_ob, buffer);
Neil Booth29401c32001-08-22 20:37:20 +00002082
Neil Boothaf0d16c2002-04-22 17:48:02 +00002083 if (inc)
2084 {
2085 _cpp_pop_file_buffer (pfile, inc);
2086
Per Bothner40de9f72003-10-02 07:30:34 +00002087 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
Neil Boothaf0d16c2002-04-22 17:48:02 +00002088 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00002089}
2090
Kazu Hirata05713b82002-09-15 18:24:08 +00002091/* Enter all recognized directives in the hash table. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00002092void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002093_cpp_init_directives (cpp_reader *pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00002094{
Neil Booth766ee682001-01-29 19:20:12 +00002095 unsigned int i;
Neil Booth93c803682000-10-28 17:59:06 +00002096 cpp_hashnode *node;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00002097
John David Anglin37b85242001-03-02 01:11:50 +00002098 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
Neil Booth93c803682000-10-28 17:59:06 +00002099 {
Neil Booth766ee682001-01-29 19:20:12 +00002100 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
Zack Weinberg4977bab2002-12-16 18:23:00 +00002101 node->is_directive = 1;
2102 node->directive_index = i;
Neil Booth93c803682000-10-28 17:59:06 +00002103 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00002104}