blob: d8e34330e281935b98a15dcea56e31cdab0bbbe0 [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,
Neil Booth5d8ebbd2002-01-03 21:43:09 +00003 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
Richard Kenner4c8cc611997-02-16 08:08:25 -05004 Contributed by Per Bothner, 1994-95.
Richard Kennerd8bfa781997-01-03 08:19:34 -05005 Based on CCCP program by Paul Rubin, June 1986
Per Bothner7f2935c1995-03-16 13:59:07 -08006 Adapted to ANSI C, Richard Stallman, Jan 1987
7
8This program is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 2, or (at your option) any
11later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
Jeff Law956d6951997-12-06 17:31:01 -070020Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
Per Bothner7f2935c1995-03-16 13:59:07 -080021
Per Bothner7f2935c1995-03-16 13:59:07 -080022#include "config.h"
Kaveh R. Ghazib04cd5071998-03-30 12:05:54 +000023#include "system.h"
Per Bothner7f2935c1995-03-16 13:59:07 -080024
Jeff Law956d6951997-12-06 17:31:01 -070025#include "cpplib.h"
26#include "cpphash.h"
Zack Weinbergc71f8352000-07-05 05:33:57 +000027#include "obstack.h"
Jeff Law956d6951997-12-06 17:31:01 -070028
Neil Booth93c803682000-10-28 17:59:06 +000029/* Chained list of answers to an assertion. */
30struct answer
31{
32 struct answer *next;
33 unsigned int count;
34 cpp_token first[1];
35};
36
Zack Weinberg88ae23e2000-03-08 23:35:19 +000037/* Stack of conditionals currently in progress
38 (including both successful and failing conditionals). */
Zack Weinberg88ae23e2000-03-08 23:35:19 +000039struct if_stack
40{
41 struct if_stack *next;
Neil Booth50410422001-09-15 10:18:03 +000042 unsigned int line; /* Line where condition started. */
Neil Booth93c803682000-10-28 17:59:06 +000043 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
Neil Boothcef0d192001-07-26 06:02:47 +000044 bool skip_elses; /* Can future #else / #elif be skipped? */
45 bool was_skipping; /* If were skipping on entry. */
46 int type; /* Most recent conditional, for diagnostics. */
Zack Weinberg88ae23e2000-03-08 23:35:19 +000047};
Zack Weinberg88ae23e2000-03-08 23:35:19 +000048
Neil Bootha5da89c2001-10-14 17:44:00 +000049/* Contains a registered pragma or pragma namespace. */
50typedef void (*pragma_cb) PARAMS ((cpp_reader *));
51struct pragma_entry
52{
53 struct pragma_entry *next;
Neil Booth4b115ff2001-10-14 23:04:13 +000054 const cpp_hashnode *pragma; /* Name and length. */
Neil Bootha5da89c2001-10-14 17:44:00 +000055 int is_nspace;
56 union {
57 pragma_cb handler;
58 struct pragma_entry *space;
59 } u;
60};
61
Neil Booth93c803682000-10-28 17:59:06 +000062/* Values for the origin field of struct directive. KANDR directives
63 come from traditional (K&R) C. STDC89 directives come from the
64 1989 C standard. EXTENSION directives are extensions. */
65#define KANDR 0
66#define STDC89 1
67#define EXTENSION 2
68
69/* Values for the flags field of struct directive. COND indicates a
70 conditional; IF_COND an opening conditional. INCL means to treat
71 "..." and <...> as q-char and h-char sequences respectively. IN_I
72 means this directive should be handled even if -fpreprocessed is in
73 effect (these are the directives with callback hooks). */
74#define COND (1 << 0)
75#define IF_COND (1 << 1)
76#define INCL (1 << 2)
77#define IN_I (1 << 3)
78
79/* Defines one #-directive, including how to handle it. */
80typedef void (*directive_handler) PARAMS ((cpp_reader *));
81typedef struct directive directive;
82struct directive
83{
84 directive_handler handler; /* Function to handle directive. */
85 const U_CHAR *name; /* Name of directive. */
86 unsigned short length; /* Length of name. */
87 unsigned char origin; /* Origin of directive. */
88 unsigned char flags; /* Flags describing this directive. */
89};
90
Zack Weinberg1316f1f2000-02-06 07:53:50 +000091/* Forward declarations. */
92
Neil Booth93c803682000-10-28 17:59:06 +000093static void skip_rest_of_line PARAMS ((cpp_reader *));
94static void check_eol PARAMS ((cpp_reader *));
Neil Boothfe6c2db2000-11-15 19:25:22 +000095static void start_directive PARAMS ((cpp_reader *));
96static void end_directive PARAMS ((cpp_reader *, int));
Neil Booth18a9d8f2001-09-16 11:23:56 +000097static void directive_diagnostics
98 PARAMS ((cpp_reader *, const directive *, int));
Neil Booth93c803682000-10-28 17:59:06 +000099static void run_directive PARAMS ((cpp_reader *, int,
Neil Booth0bda4762000-12-11 07:45:16 +0000100 const char *, size_t));
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000101static const cpp_token *glue_header_name PARAMS ((cpp_reader *));
102static const cpp_token *parse_include PARAMS ((cpp_reader *));
Zack Weinberg041c3192000-07-04 01:58:21 +0000103static void push_conditional PARAMS ((cpp_reader *, int, int,
104 const cpp_hashnode *));
Neil Booth28e0f042000-12-09 12:06:37 +0000105static unsigned int read_flag PARAMS ((cpp_reader *, unsigned int));
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000106static U_CHAR *dequote_string PARAMS ((cpp_reader *, const U_CHAR *,
107 unsigned int));
Zack Weinbergc71f8352000-07-05 05:33:57 +0000108static int strtoul_for_line PARAMS ((const U_CHAR *, unsigned int,
Zack Weinberg041c3192000-07-04 01:58:21 +0000109 unsigned long *));
Neil Booth29b10742000-11-13 18:40:37 +0000110static void do_diagnostic PARAMS ((cpp_reader *, enum error_type, int));
Neil Boothb528a072000-11-12 11:46:21 +0000111static cpp_hashnode *lex_macro_node PARAMS ((cpp_reader *));
Neil Boothba133c92001-03-15 07:57:13 +0000112static void do_include_common PARAMS ((cpp_reader *, enum include_type));
Neil Bootha5da89c2001-10-14 17:44:00 +0000113static struct pragma_entry *lookup_pragma_entry
Neil Booth4b115ff2001-10-14 23:04:13 +0000114 PARAMS ((struct pragma_entry *, const cpp_hashnode *pragma));
Neil Bootha5da89c2001-10-14 17:44:00 +0000115static struct pragma_entry *insert_pragma_entry
Neil Booth4b115ff2001-10-14 23:04:13 +0000116 PARAMS ((cpp_reader *, struct pragma_entry **, const cpp_hashnode *,
117 pragma_cb));
Neil Booth93c803682000-10-28 17:59:06 +0000118static void do_pragma_once PARAMS ((cpp_reader *));
119static void do_pragma_poison PARAMS ((cpp_reader *));
120static void do_pragma_system_header PARAMS ((cpp_reader *));
121static void do_pragma_dependency PARAMS ((cpp_reader *));
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000122static void do_linemarker PARAMS ((cpp_reader *));
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000123static const cpp_token *get_token_no_padding PARAMS ((cpp_reader *));
124static const cpp_token *get__Pragma_string PARAMS ((cpp_reader *));
Neil Booth87062812001-10-20 09:00:53 +0000125static void destringize_and_run PARAMS ((cpp_reader *, const cpp_string *));
Neil Booth93c803682000-10-28 17:59:06 +0000126static int parse_answer PARAMS ((cpp_reader *, struct answer **, int));
127static cpp_hashnode *parse_assertion PARAMS ((cpp_reader *, struct answer **,
128 int));
129static struct answer ** find_answer PARAMS ((cpp_hashnode *,
130 const struct answer *));
Neil Booth86368122000-10-31 23:34:59 +0000131static void handle_assertion PARAMS ((cpp_reader *, const char *, int));
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +0000132
Zack Weinberg168d3732000-03-14 06:34:11 +0000133/* This is the table of directive handlers. It is ordered by
134 frequency of occurrence; the numbers at the end are directive
135 counts from all the source code I have lying around (egcs and libc
136 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
Neil Booth93c803682000-10-28 17:59:06 +0000137 pcmcia-cs-3.0.9). This is no longer important as directive lookup
138 is now O(1). All extensions other than #warning and #include_next
139 are deprecated. The name is where the extension appears to have
140 come from. */
Jeff Lawe9a25f71997-11-02 14:19:36 -0700141
Neil Booth93c803682000-10-28 17:59:06 +0000142#define DIRECTIVE_TABLE \
143D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
144D(include, T_INCLUDE, KANDR, INCL) /* 52262 */ \
145D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
146D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
147D(if, T_IF, KANDR, COND | IF_COND) /* 18162 */ \
148D(else, T_ELSE, KANDR, COND) /* 9863 */ \
149D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
150D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000151D(line, T_LINE, KANDR, 0) /* 2465 */ \
Neil Boothf000294d2001-01-31 07:56:07 +0000152D(elif, T_ELIF, STDC89, COND) /* 610 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000153D(error, T_ERROR, STDC89, 0) /* 475 */ \
154D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
155D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
156D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL) /* 19 */ \
157D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
158D(import, T_IMPORT, EXTENSION, INCL) /* 0 ObjC */ \
159D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
160D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
161SCCS_ENTRY /* 0 SVR4? */
Per Bothner7f2935c1995-03-16 13:59:07 -0800162
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000163/* #sccs is not always recognized. */
164#ifdef SCCS_DIRECTIVE
Zack Weinberg041c3192000-07-04 01:58:21 +0000165# define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000166#else
167# define SCCS_ENTRY /* nothing */
168#endif
169
Zack Weinberg168d3732000-03-14 06:34:11 +0000170/* Use the table to generate a series of prototypes, an enum for the
171 directive names, and an array of directive handlers. */
172
Kazu Hirataec5c56d2001-08-01 17:57:27 +0000173/* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000174#define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
Zack Weinberg168d3732000-03-14 06:34:11 +0000175DIRECTIVE_TABLE
176#undef D
177
Zack Weinberg041c3192000-07-04 01:58:21 +0000178#define D(n, tag, o, f) tag,
Zack Weinberg168d3732000-03-14 06:34:11 +0000179enum
180{
181 DIRECTIVE_TABLE
182 N_DIRECTIVES
Per Bothner7f2935c1995-03-16 13:59:07 -0800183};
Zack Weinberg168d3732000-03-14 06:34:11 +0000184#undef D
185
Kazu Hirataec5c56d2001-08-01 17:57:27 +0000186/* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000187#define D(name, t, origin, flags) \
Zack Weinberg12cf91f2000-05-04 04:38:01 +0000188{ CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
Zack Weinberg041c3192000-07-04 01:58:21 +0000189 sizeof STRINGX(name) - 1, origin, flags },
Neil Booth93c803682000-10-28 17:59:06 +0000190static const directive dtable[] =
Zack Weinberg168d3732000-03-14 06:34:11 +0000191{
192DIRECTIVE_TABLE
193};
194#undef D
195#undef DIRECTIVE_TABLE
Per Bothner7f2935c1995-03-16 13:59:07 -0800196
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000197/* Wrapper struct directive for linemarkers.
198 The origin is more or less true - the original K+R cpp
199 did use this notation in its preprocessed output. */
200static const directive linemarker_dir =
201{
202 do_linemarker, U"#", 1, KANDR, IN_I
203};
204
Neil Boothbdcbe492001-09-13 20:05:17 +0000205#define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
Neil Booth67821e32001-08-05 17:31:25 +0000206
Neil Booth93c803682000-10-28 17:59:06 +0000207/* Skip any remaining tokens in a directive. */
208static void
209skip_rest_of_line (pfile)
210 cpp_reader *pfile;
211{
Neil Booth93c803682000-10-28 17:59:06 +0000212 /* Discard all stacked contexts. */
213 while (pfile->context != &pfile->base_context)
214 _cpp_pop_context (pfile);
215
Neil Boothb528a072000-11-12 11:46:21 +0000216 /* Sweep up all tokens remaining on the line. */
Neil Booth345894b2001-09-16 13:44:29 +0000217 if (! SEEN_EOL ())
218 while (_cpp_lex_token (pfile)->type != CPP_EOF)
219 ;
Neil Booth93c803682000-10-28 17:59:06 +0000220}
221
222/* Ensure there are no stray tokens at the end of a directive. */
223static void
224check_eol (pfile)
225 cpp_reader *pfile;
226{
Neil Booth345894b2001-09-16 13:44:29 +0000227 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
228 cpp_pedwarn (pfile, "extra tokens at end of #%s directive",
229 pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +0000230}
231
Neil Boothfe6c2db2000-11-15 19:25:22 +0000232/* Called when entering a directive, _Pragma or command-line directive. */
233static void
234start_directive (pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000235 cpp_reader *pfile;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000236{
Neil Booth7f2f1a62000-11-14 18:32:06 +0000237 /* Setup in-directive state. */
238 pfile->state.in_directive = 1;
239 pfile->state.save_comments = 0;
240
Neil Booth93c803682000-10-28 17:59:06 +0000241 /* Some handlers need the position of the # for diagnostics. */
Neil Booth8bbbef32001-08-04 16:28:14 +0000242 pfile->directive_line = pfile->line;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000243}
244
245/* Called when leaving a directive, _Pragma or command-line directive. */
246static void
247end_directive (pfile, skip_line)
248 cpp_reader *pfile;
249 int skip_line;
250{
Neil Boothfe6c2db2000-11-15 19:25:22 +0000251 /* We don't skip for an assembler #. */
252 if (skip_line)
Neil Booth67821e32001-08-05 17:31:25 +0000253 {
254 skip_rest_of_line (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +0000255 if (!pfile->keep_tokens)
256 {
257 pfile->cur_run = &pfile->base_run;
258 pfile->cur_token = pfile->base_run.base;
259 }
Neil Booth67821e32001-08-05 17:31:25 +0000260 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000261
262 /* Restore state. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000263 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
264 pfile->state.in_directive = 0;
265 pfile->state.angled_headers = 0;
266 pfile->directive = 0;
267}
268
Neil Booth18a9d8f2001-09-16 11:23:56 +0000269/* Output diagnostics for a directive DIR. INDENTED is non-zero if
270 the '#' was indented. */
Neil Booth18a9d8f2001-09-16 11:23:56 +0000271static void
272directive_diagnostics (pfile, dir, indented)
273 cpp_reader *pfile;
274 const directive *dir;
275 int indented;
276{
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000277 /* Issue -pedantic warnings for extensions. */
278 if (CPP_PEDANTIC (pfile)
279 && ! pfile->state.skipping
280 && dir->origin == EXTENSION)
281 cpp_pedwarn (pfile, "#%s is a GCC extension", dir->name);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000282
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000283 /* Traditionally, a directive is ignored unless its # is in
284 column 1. Therefore in code intended to work with K+R
285 compilers, directives added by C89 must have their #
286 indented, and directives present in traditional C must not.
287 This is true even of directives in skipped conditional
288 blocks. #elif cannot be used at all. */
289 if (CPP_WTRADITIONAL (pfile))
290 {
291 if (dir == &dtable[T_ELIF])
292 cpp_warning (pfile, "suggest not using #elif in traditional C");
293 else if (indented && dir->origin == KANDR)
294 cpp_warning (pfile,
295 "traditional C ignores #%s with the # indented",
296 dir->name);
297 else if (!indented && dir->origin != KANDR)
298 cpp_warning (pfile,
299 "suggest hiding #%s from traditional C with an indented #",
300 dir->name);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000301 }
302}
303
304/* Check if we have a known directive. INDENTED is non-zero if the
305 '#' of the directive was indented. This function is in this file
306 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
307 non-zero if the line of tokens has been handled, zero if we should
308 continue processing the line. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000309int
310_cpp_handle_directive (pfile, indented)
311 cpp_reader *pfile;
312 int indented;
313{
Neil Boothfe6c2db2000-11-15 19:25:22 +0000314 const directive *dir = 0;
Neil Booth345894b2001-09-16 13:44:29 +0000315 const cpp_token *dname;
Neil Boothe808ec92002-02-27 07:24:53 +0000316 bool was_parsing_args = pfile->state.parsing_args;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000317 int skip = 1;
318
Neil Boothe808ec92002-02-27 07:24:53 +0000319 if (was_parsing_args)
320 {
321 if (CPP_OPTION (pfile, pedantic))
322 cpp_pedwarn (pfile,
323 "embedding a directive within macro arguments is not portable");
324 pfile->state.parsing_args = 0;
325 pfile->state.prevent_expansion = 0;
326 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000327 start_directive (pfile);
Neil Booth345894b2001-09-16 13:44:29 +0000328 dname = _cpp_lex_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000329
Neil Booth345894b2001-09-16 13:44:29 +0000330 if (dname->type == CPP_NAME)
Neil Booth0d9f2342000-09-18 18:43:05 +0000331 {
Neil Booth345894b2001-09-16 13:44:29 +0000332 if (dname->val.node->directive_index)
333 dir = &dtable[dname->val.node->directive_index - 1];
Neil Booth93c803682000-10-28 17:59:06 +0000334 }
Neil Booth18a9d8f2001-09-16 11:23:56 +0000335 /* We do not recognise the # followed by a number extension in
336 assembler code. */
Neil Booth345894b2001-09-16 13:44:29 +0000337 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth93c803682000-10-28 17:59:06 +0000338 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000339 dir = &linemarker_dir;
340 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
341 && ! pfile->state.skipping)
342 cpp_pedwarn (pfile, "style of line directive is a GCC extension");
Neil Booth0d9f2342000-09-18 18:43:05 +0000343 }
344
Neil Booth93c803682000-10-28 17:59:06 +0000345 if (dir)
Neil Booth0d9f2342000-09-18 18:43:05 +0000346 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000347 /* If we have a directive that is not an opening conditional,
348 invalidate any control macro. */
349 if (! (dir->flags & IF_COND))
350 pfile->mi_valid = false;
Neil Booth93c803682000-10-28 17:59:06 +0000351
Neil Booth18a9d8f2001-09-16 11:23:56 +0000352 /* Kluge alert. In order to be sure that code like this
353
354 #define HASH #
355 HASH define foo bar
356
357 does not cause '#define foo bar' to get executed when
358 compiled with -save-temps, we recognize directives in
359 -fpreprocessed mode only if the # is in column 1. cppmacro.c
Joseph Myersa1f300c2001-11-23 02:05:19 +0000360 puts a space in front of any '#' at the start of a macro. */
Neil Booth18a9d8f2001-09-16 11:23:56 +0000361 if (CPP_OPTION (pfile, preprocessed)
362 && (indented || !(dir->flags & IN_I)))
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000363 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000364 skip = 0;
365 dir = 0;
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000366 }
367 else
Neil Booth93c803682000-10-28 17:59:06 +0000368 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000369 /* In failed conditional groups, all non-conditional
370 directives are ignored. Before doing that, whether
371 skipping or not, we should lex angle-bracketed headers
372 correctly, and maybe output some diagnostics. */
373 pfile->state.angled_headers = dir->flags & INCL;
374 if (! CPP_OPTION (pfile, preprocessed))
375 directive_diagnostics (pfile, dir, indented);
376 if (pfile->state.skipping && !(dir->flags & COND))
377 dir = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000378 }
379 }
Neil Booth345894b2001-09-16 13:44:29 +0000380 else if (dname->type == CPP_EOF)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000381 ; /* CPP_EOF is the "null directive". */
382 else
Neil Booth0d9f2342000-09-18 18:43:05 +0000383 {
Neil Booth93c803682000-10-28 17:59:06 +0000384 /* An unknown directive. Don't complain about it in assembly
385 source: we don't know where the comments are, and # may
386 introduce assembler pseudo-ops. Don't complain about invalid
387 directives in skipped conditional groups (6.10 p4). */
Neil Boothbdb05a72000-11-26 17:31:13 +0000388 if (CPP_OPTION (pfile, lang) == CLK_ASM)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000389 skip = 0;
390 else if (!pfile->state.skipping)
Neil Booth93c803682000-10-28 17:59:06 +0000391 cpp_error (pfile, "invalid preprocessing directive #%s",
Neil Booth345894b2001-09-16 13:44:29 +0000392 cpp_token_as_text (pfile, dname));
Neil Booth0d9f2342000-09-18 18:43:05 +0000393 }
394
Neil Booth18a9d8f2001-09-16 11:23:56 +0000395 if (dir)
396 {
397 pfile->directive = dir;
398 (*pfile->directive->handler) (pfile);
399 }
400 else if (skip == 0)
401 _cpp_backup_tokens (pfile, 1);
402
403 end_directive (pfile, skip);
Neil Boothe808ec92002-02-27 07:24:53 +0000404 if (was_parsing_args)
405 {
406 /* Restore state when within macro args. */
407 pfile->state.parsing_args = 2;
408 pfile->state.prevent_expansion = 1;
409 pfile->buffer->saved_flags |= PREV_WHITE;
410 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000411 return skip;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000412}
413
Neil Booth93c803682000-10-28 17:59:06 +0000414/* Directive handler wrapper used by the command line option
415 processor. */
416static void
Neil Booth29401c32001-08-22 20:37:20 +0000417run_directive (pfile, dir_no, buf, count)
Per Bothner7f2935c1995-03-16 13:59:07 -0800418 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +0000419 int dir_no;
420 const char *buf;
421 size_t count;
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000422{
Neil Booth29401c32001-08-22 20:37:20 +0000423 cpp_push_buffer (pfile, (const U_CHAR *) buf, count,
424 /* from_stage3 */ true, 1);
Neil Booth0bda4762000-12-11 07:45:16 +0000425 start_directive (pfile);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000426 /* We don't want a leading # to be interpreted as a directive. */
427 pfile->buffer->saved_flags = 0;
Neil Boothf71aebb2001-05-27 18:06:00 +0000428 pfile->directive = &dtable[dir_no];
429 (void) (*pfile->directive->handler) (pfile);
Neil Booth0bda4762000-12-11 07:45:16 +0000430 end_directive (pfile, 1);
Neil Boothef6e9582001-08-04 12:01:59 +0000431 _cpp_pop_buffer (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000432}
Per Bothner7f2935c1995-03-16 13:59:07 -0800433
Neil Booth93c803682000-10-28 17:59:06 +0000434/* Checks for validity the macro name in #define, #undef, #ifdef and
435 #ifndef directives. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000436static cpp_hashnode *
Neil Booth93c803682000-10-28 17:59:06 +0000437lex_macro_node (pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000438 cpp_reader *pfile;
439{
Zack Weinbergb8363a22001-07-01 18:48:13 +0000440 cpp_hashnode *node;
Neil Booth345894b2001-09-16 13:44:29 +0000441 const cpp_token *token = _cpp_lex_token (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000442
Zack Weinberg92936ec2000-07-19 20:18:08 +0000443 /* The token immediately after #define must be an identifier. That
Zack Weinbergb8363a22001-07-01 18:48:13 +0000444 identifier may not be "defined", per C99 6.10.8p4.
445 In C++, it may not be any of the "named operators" either,
446 per C++98 [lex.digraph], [lex.key].
447 Finally, the identifier may not have been poisoned. (In that case
448 the lexer has issued the error message for us.) */
Zack Weinberg92936ec2000-07-19 20:18:08 +0000449
Neil Booth345894b2001-09-16 13:44:29 +0000450 if (token->type != CPP_NAME)
Zack Weinberg041c3192000-07-04 01:58:21 +0000451 {
Neil Booth345894b2001-09-16 13:44:29 +0000452 if (token->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +0000453 cpp_error (pfile, "no macro name given in #%s directive",
454 pfile->directive->name);
Neil Booth345894b2001-09-16 13:44:29 +0000455 else if (token->flags & NAMED_OP)
Neil Booth93c803682000-10-28 17:59:06 +0000456 cpp_error (pfile,
Zack Weinbergb8363a22001-07-01 18:48:13 +0000457 "\"%s\" cannot be used as a macro name as it is an operator in C++",
Neil Booth345894b2001-09-16 13:44:29 +0000458 NODE_NAME (token->val.node));
Zack Weinberg92936ec2000-07-19 20:18:08 +0000459 else
Neil Booth93c803682000-10-28 17:59:06 +0000460 cpp_error (pfile, "macro names must be identifiers");
Zack Weinbergb8363a22001-07-01 18:48:13 +0000461
462 return 0;
Zack Weinberg041c3192000-07-04 01:58:21 +0000463 }
Zack Weinbergb8363a22001-07-01 18:48:13 +0000464
Neil Booth345894b2001-09-16 13:44:29 +0000465 node = token->val.node;
Zack Weinbergb8363a22001-07-01 18:48:13 +0000466 if (node->flags & NODE_POISONED)
467 return 0;
468
469 if (node == pfile->spec_nodes.n_defined)
Zack Weinbergba89d662000-08-04 01:30:06 +0000470 {
Zack Weinbergb8363a22001-07-01 18:48:13 +0000471 cpp_error (pfile, "\"%s\" cannot be used as a macro name",
472 NODE_NAME (node));
473 return 0;
Zack Weinbergba89d662000-08-04 01:30:06 +0000474 }
475
Zack Weinbergb8363a22001-07-01 18:48:13 +0000476 return node;
Per Bothner7f2935c1995-03-16 13:59:07 -0800477}
Per Bothner7f2935c1995-03-16 13:59:07 -0800478
Neil Booth93c803682000-10-28 17:59:06 +0000479/* Process a #define directive. Most work is done in cppmacro.c. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000480static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000481do_define (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800482 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800483{
Neil Booth93c803682000-10-28 17:59:06 +0000484 cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000485
Neil Booth93c803682000-10-28 17:59:06 +0000486 if (node)
487 {
Neil Booth93c803682000-10-28 17:59:06 +0000488 if (_cpp_create_definition (pfile, node))
489 if (pfile->cb.define)
Neil Booth8bbbef32001-08-04 16:28:14 +0000490 (*pfile->cb.define) (pfile, pfile->directive_line, node);
Neil Booth93c803682000-10-28 17:59:06 +0000491 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800492}
493
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000494/* Handle #undef. Mark the identifier NT_VOID in the hash table. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000495static void
Zack Weinberg041c3192000-07-04 01:58:21 +0000496do_undef (pfile)
497 cpp_reader *pfile;
498{
Neil Booth93c803682000-10-28 17:59:06 +0000499 cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000500
501 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
502 is not currently defined as a macro name. */
Neil Booth93c803682000-10-28 17:59:06 +0000503 if (node && node->type == NT_MACRO)
Zack Weinberg041c3192000-07-04 01:58:21 +0000504 {
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000505 if (pfile->cb.undef)
Neil Booth8bbbef32001-08-04 16:28:14 +0000506 (*pfile->cb.undef) (pfile, pfile->directive_line, node);
Zack Weinberg041c3192000-07-04 01:58:21 +0000507
Neil Booth618cdda2001-02-25 09:43:03 +0000508 if (node->flags & NODE_WARN)
Neil Bootha28c50352001-05-16 22:02:09 +0000509 cpp_warning (pfile, "undefining \"%s\"", NODE_NAME (node));
Zack Weinberg041c3192000-07-04 01:58:21 +0000510
511 _cpp_free_definition (node);
Zack Weinberg041c3192000-07-04 01:58:21 +0000512 }
Neil Booth93c803682000-10-28 17:59:06 +0000513 check_eol (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000514}
515
Neil Booth93c803682000-10-28 17:59:06 +0000516/* Helper routine used by parse_include. Reinterpret the current line
517 as an h-char-sequence (< ... >); we are looking at the first token
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000518 after the <. Returns the header as a token, or NULL on failure. */
519static const cpp_token *
520glue_header_name (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800521 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800522{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000523 cpp_token *header = NULL;
524 const cpp_token *token;
Neil Booth2450e0b2002-02-23 20:21:39 +0000525 unsigned char *buffer;
526 size_t len, total_len = 0, capacity = 1024;
Per Bothner7f2935c1995-03-16 13:59:07 -0800527
Neil Booth93c803682000-10-28 17:59:06 +0000528 /* To avoid lexed tokens overwriting our glued name, we can only
529 allocate from the string pool once we've lexed everything. */
Neil Booth2450e0b2002-02-23 20:21:39 +0000530 buffer = (unsigned char *) xmalloc (capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000531 for (;;)
Zack Weinberg168d3732000-03-14 06:34:11 +0000532 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000533 token = cpp_get_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000534
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000535 if (token->type == CPP_GREATER || token->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +0000536 break;
537
Neil Booth2450e0b2002-02-23 20:21:39 +0000538 len = cpp_token_len (token);
539 if (total_len + len > capacity)
Neil Booth93c803682000-10-28 17:59:06 +0000540 {
Neil Booth2450e0b2002-02-23 20:21:39 +0000541 capacity = (capacity + len) * 2;
542 buffer = (unsigned char *) xrealloc (buffer, capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000543 }
544
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000545 if (token->flags & PREV_WHITE)
Neil Booth2450e0b2002-02-23 20:21:39 +0000546 buffer[total_len++] = ' ';
Neil Booth93c803682000-10-28 17:59:06 +0000547
Neil Booth2450e0b2002-02-23 20:21:39 +0000548 total_len = cpp_spell_token (pfile, token, &buffer[total_len]) - buffer;
Neil Booth93c803682000-10-28 17:59:06 +0000549 }
550
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000551 if (token->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +0000552 cpp_error (pfile, "missing terminating > character");
553 else
554 {
Neil Booth2450e0b2002-02-23 20:21:39 +0000555 unsigned char *token_mem = _cpp_unaligned_alloc (pfile, total_len + 1);
556 memcpy (token_mem, buffer, total_len);
557 token_mem[total_len] = '\0';
558
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000559 header = _cpp_temp_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000560 header->type = CPP_HEADER_NAME;
Neil Boothece54d52001-09-28 09:40:22 +0000561 header->flags = 0;
Neil Booth2450e0b2002-02-23 20:21:39 +0000562 header->val.str.len = total_len;
563 header->val.str.text = token_mem;
Neil Booth93c803682000-10-28 17:59:06 +0000564 }
565
Neil Booth2450e0b2002-02-23 20:21:39 +0000566 free ((PTR) buffer);
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000567 return header;
Neil Booth93c803682000-10-28 17:59:06 +0000568}
569
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000570/* Returns the header string of #include, #include_next, #import and
571 #pragma dependency. Returns NULL on error. */
572static const cpp_token *
573parse_include (pfile)
Neil Booth93c803682000-10-28 17:59:06 +0000574 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +0000575{
Neil Booth93c803682000-10-28 17:59:06 +0000576 const unsigned char *dir;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000577 const cpp_token *header;
Neil Booth93c803682000-10-28 17:59:06 +0000578
Neil Booth09b82252001-07-29 22:27:20 +0000579 if (pfile->directive == &dtable[T_PRAGMA])
Neil Booth93c803682000-10-28 17:59:06 +0000580 dir = U"pragma dependency";
581 else
582 dir = pfile->directive->name;
583
584 /* Allow macro expansion. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000585 header = cpp_get_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000586 if (header->type != CPP_STRING && header->type != CPP_HEADER_NAME)
587 {
588 if (header->type != CPP_LESS)
Zack Weinberg041c3192000-07-04 01:58:21 +0000589 {
590 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir);
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000591 return NULL;
Zack Weinberg041c3192000-07-04 01:58:21 +0000592 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000593
594 header = glue_header_name (pfile);
595 if (header == NULL)
596 return header;
Zack Weinberg041c3192000-07-04 01:58:21 +0000597 }
Neil Booth93c803682000-10-28 17:59:06 +0000598
599 if (header->val.str.len == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +0000600 {
601 cpp_error (pfile, "empty file name in #%s", dir);
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000602 return NULL;
Richard Kennercfb3ee11997-04-13 14:30:13 -0400603 }
604
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000605 return header;
Zack Weinberg168d3732000-03-14 06:34:11 +0000606}
607
Neil Boothba133c92001-03-15 07:57:13 +0000608/* Handle #include, #include_next and #import. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000609static void
Neil Boothba133c92001-03-15 07:57:13 +0000610do_include_common (pfile, type)
Zack Weinberg168d3732000-03-14 06:34:11 +0000611 cpp_reader *pfile;
Neil Boothba133c92001-03-15 07:57:13 +0000612 enum include_type type;
Zack Weinberg168d3732000-03-14 06:34:11 +0000613{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000614 const cpp_token *header;
Zack Weinberg168d3732000-03-14 06:34:11 +0000615
Neil Booth09b82252001-07-29 22:27:20 +0000616 /* For #include_next, if this is the primary source file, warn and
617 use the normal search logic. */
618 if (type == IT_INCLUDE_NEXT && ! pfile->buffer->prev)
619 {
620 cpp_warning (pfile, "#include_next in primary source file");
621 type = IT_INCLUDE;
622 }
623 else if (type == IT_IMPORT && CPP_OPTION (pfile, warn_import))
624 {
625 CPP_OPTION (pfile, warn_import) = 0;
626 cpp_warning (pfile,
627 "#import is obsolete, use an #ifndef wrapper in the header file");
628 }
629
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000630 header = parse_include (pfile);
631 if (header)
Neil Boothba133c92001-03-15 07:57:13 +0000632 {
633 /* Prevent #include recursion. */
Neil Boothd8693c62001-08-21 23:05:12 +0000634 if (pfile->line_maps.depth >= CPP_STACK_MAX)
Neil Boothba133c92001-03-15 07:57:13 +0000635 cpp_fatal (pfile, "#include nested too deeply");
Neil Boothba133c92001-03-15 07:57:13 +0000636 else
637 {
Neil Booth09b82252001-07-29 22:27:20 +0000638 check_eol (pfile);
639 /* Get out of macro context, if we are. */
Neil Boothbdcbe492001-09-13 20:05:17 +0000640 skip_rest_of_line (pfile);
Neil Booth09b82252001-07-29 22:27:20 +0000641 if (pfile->cb.include)
Neil Booth8bbbef32001-08-04 16:28:14 +0000642 (*pfile->cb.include) (pfile, pfile->directive_line,
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000643 pfile->directive->name, header);
Neil Boothba133c92001-03-15 07:57:13 +0000644
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000645 _cpp_execute_include (pfile, header, type);
Neil Boothba133c92001-03-15 07:57:13 +0000646 }
647 }
648}
649
650static void
651do_include (pfile)
652 cpp_reader *pfile;
653{
654 do_include_common (pfile, IT_INCLUDE);
Zack Weinberg168d3732000-03-14 06:34:11 +0000655}
656
Zack Weinberg711b8822000-07-18 00:59:49 +0000657static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000658do_import (pfile)
659 cpp_reader *pfile;
660{
Neil Boothba133c92001-03-15 07:57:13 +0000661 do_include_common (pfile, IT_IMPORT);
Zack Weinberg168d3732000-03-14 06:34:11 +0000662}
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000663
Zack Weinberg711b8822000-07-18 00:59:49 +0000664static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000665do_include_next (pfile)
666 cpp_reader *pfile;
667{
Neil Boothba133c92001-03-15 07:57:13 +0000668 do_include_common (pfile, IT_INCLUDE_NEXT);
Per Bothner7f2935c1995-03-16 13:59:07 -0800669}
670
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000671/* Subroutine of do_linemarker. Read possible flags after file name.
672 LAST is the last flag seen; 0 if this is the first flag. Return the
673 flag if it is valid, 0 at the end of the directive. Otherwise
674 complain. */
Neil Booth642ce432000-12-07 23:17:56 +0000675static unsigned int
Neil Booth28e0f042000-12-09 12:06:37 +0000676read_flag (pfile, last)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000677 cpp_reader *pfile;
Neil Booth28e0f042000-12-09 12:06:37 +0000678 unsigned int last;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000679{
Neil Booth345894b2001-09-16 13:44:29 +0000680 const cpp_token *token = _cpp_lex_token (pfile);
Jason Merrilld3a34a01999-08-14 00:42:07 +0000681
Neil Booth345894b2001-09-16 13:44:29 +0000682 if (token->type == CPP_NUMBER && token->val.str.len == 1)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000683 {
Neil Booth345894b2001-09-16 13:44:29 +0000684 unsigned int flag = token->val.str.text[0] - '0';
Neil Booth28e0f042000-12-09 12:06:37 +0000685
686 if (flag > last && flag <= 4
687 && (flag != 4 || last == 3)
688 && (flag != 2 || last == 0))
689 return flag;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000690 }
Neil Booth93c803682000-10-28 17:59:06 +0000691
Neil Booth345894b2001-09-16 13:44:29 +0000692 if (token->type != CPP_EOF)
Neil Booth642ce432000-12-07 23:17:56 +0000693 cpp_error (pfile, "invalid flag \"%s\" in line directive",
Neil Booth345894b2001-09-16 13:44:29 +0000694 cpp_token_as_text (pfile, token));
Neil Booth93c803682000-10-28 17:59:06 +0000695 return 0;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000696}
697
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000698/* Subroutine of do_line and do_linemarker. Returns a version of STR
699 which has a NUL terminator and all escape sequences converted to
700 their equivalents. Temporary, hopefully. */
701static U_CHAR *
702dequote_string (pfile, str, len)
703 cpp_reader *pfile;
704 const U_CHAR *str;
705 unsigned int len;
706{
707 U_CHAR *result = _cpp_unaligned_alloc (pfile, len + 1);
708 U_CHAR *dst = result;
709 const U_CHAR *limit = str + len;
710 unsigned int c;
711 unsigned HOST_WIDE_INT mask;
712
713 /* We need the mask to match the host's 'unsigned char', not the
714 target's. */
715 if (CHAR_BIT < HOST_BITS_PER_WIDE_INT)
716 mask = ((unsigned HOST_WIDE_INT) 1 << CHAR_BIT) - 1;
717 else
718 mask = ~(unsigned HOST_WIDE_INT)0;
719
720 while (str < limit)
721 {
722 c = *str++;
723 if (c != '\\')
724 *dst++ = c;
725 else
726 *dst++ = cpp_parse_escape (pfile, (const U_CHAR **)&str, limit, mask);
727 }
728 *dst++ = '\0';
729 return result;
730}
731
732/* Subroutine of do_line and do_linemarker. Convert a number in STR,
733 of length LEN, to binary; store it in NUMP, and return 0 if the
734 number was well-formed, 1 if not. Temporary, hopefully. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000735static int
736strtoul_for_line (str, len, nump)
737 const U_CHAR *str;
738 unsigned int len;
739 unsigned long *nump;
740{
741 unsigned long reg = 0;
742 U_CHAR c;
743 while (len--)
744 {
745 c = *str++;
746 if (!ISDIGIT (c))
747 return 1;
748 reg *= 10;
749 reg += c - '0';
750 }
751 *nump = reg;
752 return 0;
753}
754
Zack Weinberg5538ada1999-02-04 06:36:54 -0500755/* Interpret #line command.
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000756 Note that the filename string (if any) is a true string constant
757 (escapes are interpreted), unlike in #line. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000758static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000759do_line (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800760 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800761{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000762 const cpp_token *token;
Neil Boothbb74c962001-08-17 22:23:49 +0000763 const char *new_file = pfile->map->to_file;
764 unsigned long new_lineno;
Per Bothner7f2935c1995-03-16 13:59:07 -0800765
Neil Booth27e25642000-11-27 08:00:04 +0000766 /* C99 raised the minimum limit on #line numbers. */
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000767 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
Neil Booth18a9d8f2001-09-16 11:23:56 +0000768
Neil Booth93c803682000-10-28 17:59:06 +0000769 /* #line commands expand macros. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000770 token = cpp_get_token (pfile);
771 if (token->type != CPP_NUMBER
772 || strtoul_for_line (token->val.str.text, token->val.str.len,
773 &new_lineno))
Per Bothner7f2935c1995-03-16 13:59:07 -0800774 {
Neil Booth93c803682000-10-28 17:59:06 +0000775 cpp_error (pfile, "\"%s\" after #line is not a positive integer",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000776 cpp_token_as_text (pfile, token));
Zack Weinberg9ec72912000-08-09 19:41:12 +0000777 return;
Zack Weinberg5538ada1999-02-04 06:36:54 -0500778 }
Zack Weinberg5538ada1999-02-04 06:36:54 -0500779
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000780 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
Zack Weinberg041c3192000-07-04 01:58:21 +0000781 cpp_pedwarn (pfile, "line number out of range");
Zack Weinberg5538ada1999-02-04 06:36:54 -0500782
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000783 token = cpp_get_token (pfile);
784 if (token->type == CPP_STRING)
Zack Weinberg5538ada1999-02-04 06:36:54 -0500785 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000786 new_file = (const char *) dequote_string (pfile, token->val.str.text,
787 token->val.str.len);
788 check_eol (pfile);
789 }
790 else if (token->type != CPP_EOF)
791 {
792 cpp_error (pfile, "\"%s\" is not a valid filename",
793 cpp_token_as_text (pfile, token));
794 return;
795 }
Neil Booth93c803682000-10-28 17:59:06 +0000796
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000797 skip_rest_of_line (pfile);
798 _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
799 pfile->map->sysp);
800}
801
802/* Interpret the # 44 "file" [flags] notation, which has slightly
803 different syntax and semantics from #line: Flags are allowed,
804 and we never complain about the line number being too big. */
805static void
806do_linemarker (pfile)
807 cpp_reader *pfile;
808{
809 const cpp_token *token;
810 const char *new_file = pfile->map->to_file;
811 unsigned long new_lineno;
812 unsigned int new_sysp = pfile->map->sysp;
813 enum lc_reason reason = LC_RENAME;
814 int flag;
815
816 /* Back up so we can get the number again. Putting this in
817 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
818 some circumstances, which can segfault. */
819 _cpp_backup_tokens (pfile, 1);
820
821 /* #line commands expand macros. */
822 token = cpp_get_token (pfile);
823 if (token->type != CPP_NUMBER
824 || strtoul_for_line (token->val.str.text, token->val.str.len,
825 &new_lineno))
826 {
827 cpp_error (pfile, "\"%s\" after # is not a positive integer",
828 cpp_token_as_text (pfile, token));
829 return;
830 }
831
832 token = cpp_get_token (pfile);
833 if (token->type == CPP_STRING)
834 {
835 new_file = (const char *) dequote_string (pfile, token->val.str.text,
836 token->val.str.len);
837 new_sysp = 0;
838 flag = read_flag (pfile, 0);
839 if (flag == 1)
Neil Booth93c803682000-10-28 17:59:06 +0000840 {
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000841 reason = LC_ENTER;
842 /* Fake an include for cpp_included (). */
843 _cpp_fake_include (pfile, new_file);
844 flag = read_flag (pfile, flag);
Neil Booth93c803682000-10-28 17:59:06 +0000845 }
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000846 else if (flag == 2)
847 {
848 reason = LC_LEAVE;
849 flag = read_flag (pfile, flag);
850 }
851 if (flag == 3)
852 {
853 new_sysp = 1;
854 flag = read_flag (pfile, flag);
855 if (flag == 4)
856 new_sysp = 2;
857 }
858
Neil Boothfde84342001-08-06 21:07:41 +0000859 check_eol (pfile);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000860 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000861 else if (token->type != CPP_EOF)
Neil Booth27e25642000-11-27 08:00:04 +0000862 {
863 cpp_error (pfile, "\"%s\" is not a valid filename",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000864 cpp_token_as_text (pfile, token));
Neil Booth27e25642000-11-27 08:00:04 +0000865 return;
866 }
Zack Weinberg941e09b1998-12-15 11:17:06 +0000867
Neil Boothbdcbe492001-09-13 20:05:17 +0000868 skip_rest_of_line (pfile);
Neil Boothbb74c962001-08-17 22:23:49 +0000869 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
Neil Booth27e25642000-11-27 08:00:04 +0000870}
871
Neil Booth67821e32001-08-05 17:31:25 +0000872/* Arrange the file_change callback. pfile->line has changed to
Neil Booth47d89cf2001-08-11 07:33:39 +0000873 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
Joseph Myersa1f300c2001-11-23 02:05:19 +0000874 header, 2 for a system header that needs to be extern "C" protected,
Neil Booth47d89cf2001-08-11 07:33:39 +0000875 and zero otherwise. */
Neil Bootheb1f4d92000-12-18 19:00:26 +0000876void
Neil Booth47d89cf2001-08-11 07:33:39 +0000877_cpp_do_file_change (pfile, reason, to_file, file_line, sysp)
Neil Booth27e25642000-11-27 08:00:04 +0000878 cpp_reader *pfile;
Neil Boothd82fc102001-08-02 23:03:31 +0000879 enum lc_reason reason;
Neil Booth47d89cf2001-08-11 07:33:39 +0000880 const char *to_file;
Neil Booth67821e32001-08-05 17:31:25 +0000881 unsigned int file_line;
Neil Booth47d89cf2001-08-11 07:33:39 +0000882 unsigned int sysp;
Neil Booth27e25642000-11-27 08:00:04 +0000883{
Neil Booth47d89cf2001-08-11 07:33:39 +0000884 pfile->map = add_line_map (&pfile->line_maps, reason, sysp,
885 pfile->line, to_file, file_line);
Neil Boothd82fc102001-08-02 23:03:31 +0000886
Neil Bootheb1f4d92000-12-18 19:00:26 +0000887 if (pfile->cb.file_change)
Neil Booth47d89cf2001-08-11 07:33:39 +0000888 (*pfile->cb.file_change) (pfile, pfile->map);
Per Bothner7f2935c1995-03-16 13:59:07 -0800889}
Zack Weinberg941e09b1998-12-15 11:17:06 +0000890
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000891/* Report a warning or error detected by the program we are
892 processing. Use the directive's tokens in the error message. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000893static void
Neil Booth29b10742000-11-13 18:40:37 +0000894do_diagnostic (pfile, code, print_dir)
Per Bothner7f2935c1995-03-16 13:59:07 -0800895 cpp_reader *pfile;
Neil Booth838f3132000-09-24 10:42:09 +0000896 enum error_type code;
Neil Booth29b10742000-11-13 18:40:37 +0000897 int print_dir;
Per Bothner7f2935c1995-03-16 13:59:07 -0800898{
Neil Booth97293892001-09-14 22:04:46 +0000899 if (_cpp_begin_message (pfile, code, 0, 0))
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000900 {
Neil Booth29b10742000-11-13 18:40:37 +0000901 if (print_dir)
902 fprintf (stderr, "#%s ", pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +0000903 pfile->state.prevent_expansion++;
904 cpp_output_line (pfile, stderr);
905 pfile->state.prevent_expansion--;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000906 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800907}
908
Neil Booth838f3132000-09-24 10:42:09 +0000909static void
910do_error (pfile)
911 cpp_reader *pfile;
912{
Neil Booth29b10742000-11-13 18:40:37 +0000913 do_diagnostic (pfile, ERROR, 1);
Neil Booth838f3132000-09-24 10:42:09 +0000914}
Per Bothner7f2935c1995-03-16 13:59:07 -0800915
Zack Weinberg711b8822000-07-18 00:59:49 +0000916static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000917do_warning (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800918 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800919{
Neil Booth2f878972001-04-08 10:01:18 +0000920 /* We want #warning diagnostics to be emitted in system headers too. */
921 do_diagnostic (pfile, WARNING_SYSHDR, 1);
Per Bothner7f2935c1995-03-16 13:59:07 -0800922}
923
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000924/* Report program identification. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000925static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000926do_ident (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800927 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800928{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000929 const cpp_token *str = cpp_get_token (pfile);
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000930
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000931 if (str->type != CPP_STRING)
932 cpp_error (pfile, "invalid #ident directive");
Neil Booth93c803682000-10-28 17:59:06 +0000933 else if (pfile->cb.ident)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000934 (*pfile->cb.ident) (pfile, pfile->directive_line, &str->val.str);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000935
Neil Booth93c803682000-10-28 17:59:06 +0000936 check_eol (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -0800937}
938
Neil Bootha5da89c2001-10-14 17:44:00 +0000939/* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
940 matching entry, or NULL if none is found. The returned entry could
941 be the start of a namespace chain, or a pragma. */
942static struct pragma_entry *
943lookup_pragma_entry (chain, pragma)
944 struct pragma_entry *chain;
Neil Booth4b115ff2001-10-14 23:04:13 +0000945 const cpp_hashnode *pragma;
Nathan Sidwell82443372000-06-23 10:56:09 +0000946{
Neil Booth4b115ff2001-10-14 23:04:13 +0000947 while (chain && chain->pragma != pragma)
948 chain = chain->next;
Neil Bootha5da89c2001-10-14 17:44:00 +0000949
950 return chain;
951}
952
953/* Create and insert a pragma entry for NAME at the beginning of a
954 singly-linked CHAIN. If handler is NULL, it is a namespace,
955 otherwise it is a pragma and its handler. */
956static struct pragma_entry *
Neil Booth4b115ff2001-10-14 23:04:13 +0000957insert_pragma_entry (pfile, chain, pragma, handler)
Neil Bootha5da89c2001-10-14 17:44:00 +0000958 cpp_reader *pfile;
959 struct pragma_entry **chain;
Neil Booth4b115ff2001-10-14 23:04:13 +0000960 const cpp_hashnode *pragma;
Neil Bootha5da89c2001-10-14 17:44:00 +0000961 pragma_cb handler;
962{
963 struct pragma_entry *new;
964
965 new = (struct pragma_entry *)
966 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
Neil Booth4b115ff2001-10-14 23:04:13 +0000967 new->pragma = pragma;
Neil Bootha5da89c2001-10-14 17:44:00 +0000968 if (handler)
969 {
970 new->is_nspace = 0;
971 new->u.handler = handler;
972 }
973 else
974 {
975 new->is_nspace = 1;
976 new->u.space = NULL;
977 }
978
979 new->next = *chain;
980 *chain = new;
981 return new;
982}
983
984/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
985 goes in the global namespace. HANDLER is the handler it will call,
986 which must be non-NULL. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000987void
988cpp_register_pragma (pfile, space, name, handler)
Nathan Sidwell82443372000-06-23 10:56:09 +0000989 cpp_reader *pfile;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000990 const char *space;
991 const char *name;
Neil Boothd82fc102001-08-02 23:03:31 +0000992 pragma_cb handler;
Nathan Sidwell82443372000-06-23 10:56:09 +0000993{
Neil Bootha5da89c2001-10-14 17:44:00 +0000994 struct pragma_entry **chain = &pfile->pragmas;
995 struct pragma_entry *entry;
Neil Booth4b115ff2001-10-14 23:04:13 +0000996 const cpp_hashnode *node;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000997
Neil Bootha5da89c2001-10-14 17:44:00 +0000998 if (!handler)
999 abort ();
1000
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001001 if (space)
1002 {
Neil Booth4b115ff2001-10-14 23:04:13 +00001003 node = cpp_lookup (pfile, U space, strlen (space));
1004 entry = lookup_pragma_entry (*chain, node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001005 if (!entry)
Neil Booth4b115ff2001-10-14 23:04:13 +00001006 entry = insert_pragma_entry (pfile, chain, node, NULL);
Neil Bootha5da89c2001-10-14 17:44:00 +00001007 else if (!entry->is_nspace)
1008 goto clash;
1009 chain = &entry->u.space;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001010 }
1011
Neil Bootha5da89c2001-10-14 17:44:00 +00001012 /* Check for duplicates. */
Neil Booth4b115ff2001-10-14 23:04:13 +00001013 node = cpp_lookup (pfile, U name, strlen (name));
1014 entry = lookup_pragma_entry (*chain, node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001015 if (entry)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001016 {
Neil Bootha5da89c2001-10-14 17:44:00 +00001017 if (entry->is_nspace)
1018 clash:
1019 cpp_ice (pfile,
1020 "registering \"%s\" as both a pragma and a pragma namespace",
Neil Booth4b115ff2001-10-14 23:04:13 +00001021 NODE_NAME (node));
Neil Bootha5da89c2001-10-14 17:44:00 +00001022 else if (space)
1023 cpp_ice (pfile, "#pragma %s %s is already registered", space, name);
1024 else
1025 cpp_ice (pfile, "#pragma %s is already registered", name);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001026 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001027 else
Neil Booth4b115ff2001-10-14 23:04:13 +00001028 insert_pragma_entry (pfile, chain, node, handler);
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001029}
Neil Bootha5da89c2001-10-14 17:44:00 +00001030
1031/* Register the pragmas the preprocessor itself handles. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001032void
1033_cpp_init_internal_pragmas (pfile)
1034 cpp_reader *pfile;
1035{
Neil Bootha5da89c2001-10-14 17:44:00 +00001036 /* Pragmas in the global namespace. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001037 cpp_register_pragma (pfile, 0, "poison", do_pragma_poison);
1038 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
1039
Neil Bootha5da89c2001-10-14 17:44:00 +00001040 /* New GCC-specific pragmas should be put in the GCC namespace. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001041 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
1042 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
1043 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
Nathan Sidwell82443372000-06-23 10:56:09 +00001044}
Per Bothner7f2935c1995-03-16 13:59:07 -08001045
Neil Bootha5da89c2001-10-14 17:44:00 +00001046/* Pragmata handling. We handle some, and pass the rest on to the
1047 front end. C99 defines three pragmas and says that no macro
1048 expansion is to be performed on them; whether or not macro
1049 expansion happens for other pragmas is implementation defined.
1050 This implementation never macro-expands the text after #pragma. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001051static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001052do_pragma (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001053 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001054{
Neil Bootha5da89c2001-10-14 17:44:00 +00001055 const struct pragma_entry *p = NULL;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001056 const cpp_token *token;
Neil Bootha5da89c2001-10-14 17:44:00 +00001057 unsigned int count = 1;
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001058
Neil Booth93c803682000-10-28 17:59:06 +00001059 pfile->state.prevent_expansion++;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001060
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001061 token = cpp_get_token (pfile);
1062 if (token->type == CPP_NAME)
Alexandre Oliva0172e2b2000-02-27 06:24:27 +00001063 {
Neil Booth4b115ff2001-10-14 23:04:13 +00001064 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001065 if (p && p->is_nspace)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001066 {
Neil Bootha5da89c2001-10-14 17:44:00 +00001067 count = 2;
1068 token = cpp_get_token (pfile);
1069 if (token->type == CPP_NAME)
Neil Booth4b115ff2001-10-14 23:04:13 +00001070 p = lookup_pragma_entry (p->u.space, token->val.node);
Neil Bootha5da89c2001-10-14 17:44:00 +00001071 else
1072 p = NULL;
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001073 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001074 }
1075
Neil Booth97293892001-09-14 22:04:46 +00001076 /* FIXME. This is an awful kludge to get the front ends to update
1077 their notion of line number for diagnostic purposes. The line
1078 number should be passed to the handler and they should do it
1079 themselves. Stand-alone CPP must ignore us, otherwise it will
1080 prefix the directive with spaces, hence the 1. Ugh. */
1081 if (pfile->cb.line_change)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001082 (*pfile->cb.line_change)(pfile, token, 1);
Neil Booth97293892001-09-14 22:04:46 +00001083
Neil Bootha5da89c2001-10-14 17:44:00 +00001084 if (p)
1085 (*p->u.handler) (pfile);
Neil Boothd82fc102001-08-02 23:03:31 +00001086 else if (pfile->cb.def_pragma)
Neil Boothbdcbe492001-09-13 20:05:17 +00001087 {
1088 _cpp_backup_tokens (pfile, count);
1089 (*pfile->cb.def_pragma) (pfile, pfile->directive_line);
1090 }
Neil Bootha5da89c2001-10-14 17:44:00 +00001091
Neil Booth97293892001-09-14 22:04:46 +00001092 pfile->state.prevent_expansion--;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001093}
1094
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001095/* Handle #pragma once. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001096static void
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001097do_pragma_once (pfile)
1098 cpp_reader *pfile;
1099{
Branko Cibej317639a2000-09-26 00:54:04 +02001100 cpp_warning (pfile, "#pragma once is obsolete");
1101
Neil Booth642ce432000-12-07 23:17:56 +00001102 if (pfile->buffer->prev == NULL)
Neil Booth93c803682000-10-28 17:59:06 +00001103 cpp_warning (pfile, "#pragma once in main file");
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001104 else
Neil Booth642ce432000-12-07 23:17:56 +00001105 _cpp_never_reread (pfile->buffer->inc);
Neil Booth93c803682000-10-28 17:59:06 +00001106
1107 check_eol (pfile);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001108}
1109
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001110/* Handle #pragma poison, to poison one or more identifiers so that
1111 the lexer produces a hard error for each subsequent usage. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001112static void
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001113do_pragma_poison (pfile)
1114 cpp_reader *pfile;
1115{
Neil Booth345894b2001-09-16 13:44:29 +00001116 const cpp_token *tok;
Zack Weinbergf8f769e2000-05-28 05:56:38 +00001117 cpp_hashnode *hp;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001118
Neil Booth93c803682000-10-28 17:59:06 +00001119 pfile->state.poisoned_ok = 1;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001120 for (;;)
1121 {
Neil Booth345894b2001-09-16 13:44:29 +00001122 tok = _cpp_lex_token (pfile);
1123 if (tok->type == CPP_EOF)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001124 break;
Neil Booth345894b2001-09-16 13:44:29 +00001125 if (tok->type != CPP_NAME)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001126 {
Neil Booth93c803682000-10-28 17:59:06 +00001127 cpp_error (pfile, "invalid #pragma GCC poison directive");
1128 break;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001129 }
1130
Neil Booth345894b2001-09-16 13:44:29 +00001131 hp = tok->val.node;
Neil Booth93c803682000-10-28 17:59:06 +00001132 if (hp->flags & NODE_POISONED)
1133 continue;
1134
1135 if (hp->type == NT_MACRO)
Neil Bootha28c50352001-05-16 22:02:09 +00001136 cpp_warning (pfile, "poisoning existing macro \"%s\"", NODE_NAME (hp));
Neil Booth93c803682000-10-28 17:59:06 +00001137 _cpp_free_definition (hp);
1138 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001139 }
Neil Booth93c803682000-10-28 17:59:06 +00001140 pfile->state.poisoned_ok = 0;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001141}
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001142
1143/* Mark the current header as a system header. This will suppress
1144 some categories of warnings (notably those from -pedantic). It is
1145 intended for use in system libraries that cannot be implemented in
1146 conforming C, but cannot be certain that their headers appear in a
1147 system include directory. To prevent abuse, it is rejected in the
1148 primary source file. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001149static void
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001150do_pragma_system_header (pfile)
1151 cpp_reader *pfile;
1152{
Neil Booth614c7d32000-12-04 07:32:04 +00001153 cpp_buffer *buffer = pfile->buffer;
1154
1155 if (buffer->prev == 0)
1156 cpp_warning (pfile, "#pragma system_header ignored outside include file");
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001157 else
Neil Boothd82fc102001-08-02 23:03:31 +00001158 {
1159 check_eol (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +00001160 skip_rest_of_line (pfile);
Neil Boothd82fc102001-08-02 23:03:31 +00001161 cpp_make_system_header (pfile, 1, 0);
1162 }
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001163}
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001164
1165/* Check the modified date of the current include file against a specified
1166 file. Issue a diagnostic, if the specified file is newer. We use this to
1167 determine if a fixed header should be refixed. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001168static void
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001169do_pragma_dependency (pfile)
1170 cpp_reader *pfile;
1171{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001172 const cpp_token *header;
Neil Booth93c803682000-10-28 17:59:06 +00001173 int ordering;
Zack Weinberg041c3192000-07-04 01:58:21 +00001174
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001175 header = parse_include (pfile);
1176 if (!header)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001177 return;
Zack Weinberg041c3192000-07-04 01:58:21 +00001178
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001179 ordering = _cpp_compare_file_date (pfile, header);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001180 if (ordering < 0)
Neil Booth93c803682000-10-28 17:59:06 +00001181 cpp_warning (pfile, "cannot find source %s",
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001182 cpp_token_as_text (pfile, header));
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001183 else if (ordering > 0)
1184 {
Neil Booth93c803682000-10-28 17:59:06 +00001185 cpp_warning (pfile, "current file is older than %s",
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001186 cpp_token_as_text (pfile, header));
1187 if (cpp_get_token (pfile)->type != CPP_EOF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001188 {
1189 _cpp_backup_tokens (pfile, 1);
1190 do_diagnostic (pfile, WARNING, 0);
1191 }
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001192 }
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001193}
1194
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001195/* Get a token but skip padding. */
1196static const cpp_token *
1197get_token_no_padding (pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001198 cpp_reader *pfile;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001199{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001200 for (;;)
1201 {
1202 const cpp_token *result = cpp_get_token (pfile);
1203 if (result->type != CPP_PADDING)
1204 return result;
1205 }
1206}
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001207
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001208/* Check syntax is "(string-literal)". Returns the string on success,
1209 or NULL on failure. */
1210static const cpp_token *
1211get__Pragma_string (pfile)
1212 cpp_reader *pfile;
1213{
1214 const cpp_token *string;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001215
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001216 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1217 return NULL;
1218
1219 string = get_token_no_padding (pfile);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001220 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001221 return NULL;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001222
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001223 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1224 return NULL;
1225
1226 return string;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001227}
1228
Neil Booth87062812001-10-20 09:00:53 +00001229/* Destringize IN into a temporary buffer, by removing the first \ of
1230 \" and \\ sequences, and process the result as a #pragma directive. */
1231static void
1232destringize_and_run (pfile, in)
1233 cpp_reader *pfile;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001234 const cpp_string *in;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001235{
1236 const unsigned char *src, *limit;
Neil Booth87062812001-10-20 09:00:53 +00001237 char *dest, *result;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001238
Neil Booth4d6baaf2001-11-26 23:44:54 +00001239 dest = result = alloca (in->len + 1);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001240 for (src = in->text, limit = src + in->len; src < limit;)
1241 {
1242 /* We know there is a character following the backslash. */
1243 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1244 src++;
1245 *dest++ = *src++;
1246 }
Neil Booth4d6baaf2001-11-26 23:44:54 +00001247 *dest = '\0';
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001248
Neil Booth87062812001-10-20 09:00:53 +00001249 run_directive (pfile, T_PRAGMA, result, dest - result);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001250}
1251
Neil Booth87062812001-10-20 09:00:53 +00001252/* Handle the _Pragma operator. */
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001253void
1254_cpp_do__Pragma (pfile)
1255 cpp_reader *pfile;
1256{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001257 const cpp_token *string = get__Pragma_string (pfile);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001258
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001259 if (!string)
Neil Booth67821e32001-08-05 17:31:25 +00001260 cpp_error (pfile, "_Pragma takes a parenthesized string literal");
1261 else
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001262 {
Neil Booth50410422001-09-15 10:18:03 +00001263 /* Ideally, we'd like
1264 token1 _Pragma ("foo") token2
1265 to be output as
1266 token1
1267 # 7 "file.c"
1268 #pragma foo
1269 # 7 "file.c"
1270 token2
1271 Getting these correct line markers is a little tricky. */
1272
1273 unsigned int orig_line = pfile->line;
Neil Booth87062812001-10-20 09:00:53 +00001274 destringize_and_run (pfile, &string->val.str);
Neil Booth50410422001-09-15 10:18:03 +00001275 pfile->line = orig_line;
1276 pfile->buffer->saved_flags = BOL;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001277 }
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001278}
1279
Per Bothner7f2935c1995-03-16 13:59:07 -08001280/* Just ignore #sccs, on systems where we define it at all. */
Zack Weinberg07aa0b02000-04-01 22:55:25 +00001281#ifdef SCCS_DIRECTIVE
Zack Weinberg711b8822000-07-18 00:59:49 +00001282static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001283do_sccs (pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +00001284 cpp_reader *pfile ATTRIBUTE_UNUSED;
Per Bothner7f2935c1995-03-16 13:59:07 -08001285{
Per Bothner7f2935c1995-03-16 13:59:07 -08001286}
Zack Weinberg07aa0b02000-04-01 22:55:25 +00001287#endif
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001288
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001289/* Handle #ifdef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001290static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001291do_ifdef (pfile)
1292 cpp_reader *pfile;
1293{
Neil Booth93c803682000-10-28 17:59:06 +00001294 int skip = 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001295
Neil Boothcef0d192001-07-26 06:02:47 +00001296 if (! pfile->state.skipping)
Neil Booth93c803682000-10-28 17:59:06 +00001297 {
1298 const cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +00001299
Neil Booth93c803682000-10-28 17:59:06 +00001300 if (node)
1301 skip = node->type != NT_MACRO;
Geoffrey Keatingb43db0b2000-12-02 22:28:44 +00001302
1303 if (node)
1304 check_eol (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001305 }
1306
1307 push_conditional (pfile, skip, T_IFDEF, 0);
Zack Weinberg168d3732000-03-14 06:34:11 +00001308}
1309
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001310/* Handle #ifndef. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001311static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001312do_ifndef (pfile)
1313 cpp_reader *pfile;
1314{
Neil Booth93c803682000-10-28 17:59:06 +00001315 int skip = 1;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001316 const cpp_hashnode *node = 0;
Zack Weinberg168d3732000-03-14 06:34:11 +00001317
Neil Boothcef0d192001-07-26 06:02:47 +00001318 if (! pfile->state.skipping)
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001319 {
Neil Booth93c803682000-10-28 17:59:06 +00001320 node = lex_macro_node (pfile);
1321 if (node)
1322 skip = node->type == NT_MACRO;
Geoffrey Keatingb43db0b2000-12-02 22:28:44 +00001323
1324 if (node)
1325 check_eol (pfile);
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001326 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001327
Neil Booth93c803682000-10-28 17:59:06 +00001328 push_conditional (pfile, skip, T_IFNDEF, node);
Per Bothner7f2935c1995-03-16 13:59:07 -08001329}
1330
Neil Booth6d18adb2001-07-29 17:27:57 +00001331/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1332 pfile->mi_ind_cmacro so we can handle multiple-include
1333 optimisations. If macro expansion occurs in the expression, we
1334 cannot treat it as a controlling conditional, since the expansion
1335 could change in the future. That is handled by cpp_get_token. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001336static void
Zack Weinbergea4a4532000-05-29 16:19:32 +00001337do_if (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001338 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001339{
Neil Booth93c803682000-10-28 17:59:06 +00001340 int skip = 1;
Per Bothner7f2935c1995-03-16 13:59:07 -08001341
Neil Boothcef0d192001-07-26 06:02:47 +00001342 if (! pfile->state.skipping)
Neil Booth6d18adb2001-07-29 17:27:57 +00001343 skip = _cpp_parse_expr (pfile) == 0;
Neil Booth93c803682000-10-28 17:59:06 +00001344
Neil Booth6d18adb2001-07-29 17:27:57 +00001345 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
Per Bothner7f2935c1995-03-16 13:59:07 -08001346}
1347
Neil Boothb528a072000-11-12 11:46:21 +00001348/* Flip skipping state if appropriate and continue without changing
Zack Weinbergea4a4532000-05-29 16:19:32 +00001349 if_stack; this is so that the error message for missing #endif's
1350 etc. will point to the original #if. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001351static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001352do_else (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001353 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001354{
Neil Boothb528a072000-11-12 11:46:21 +00001355 cpp_buffer *buffer = pfile->buffer;
1356 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001357
1358 if (ifs == NULL)
Neil Booth93c803682000-10-28 17:59:06 +00001359 cpp_error (pfile, "#else without #if");
1360 else
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001361 {
Neil Booth93c803682000-10-28 17:59:06 +00001362 if (ifs->type == T_ELSE)
1363 {
1364 cpp_error (pfile, "#else after #else");
Neil Booth50410422001-09-15 10:18:03 +00001365 cpp_error_with_line (pfile, ifs->line, 0,
Neil Booth93c803682000-10-28 17:59:06 +00001366 "the conditional began here");
1367 }
Neil Boothb528a072000-11-12 11:46:21 +00001368 ifs->type = T_ELSE;
1369
Neil Boothcef0d192001-07-26 06:02:47 +00001370 /* Skip any future (erroneous) #elses or #elifs. */
1371 pfile->state.skipping = ifs->skip_elses;
1372 ifs->skip_elses = true;
Neil Booth93c803682000-10-28 17:59:06 +00001373
1374 /* Invalidate any controlling macro. */
1375 ifs->mi_cmacro = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001376
Neil Boothcef0d192001-07-26 06:02:47 +00001377 /* Only check EOL if was not originally skipping. */
1378 if (!ifs->was_skipping)
1379 check_eol (pfile);
1380 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001381}
1382
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001383/* Handle a #elif directive by not changing if_stack either. See the
Neil Booth93c803682000-10-28 17:59:06 +00001384 comment above do_else. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001385static void
Zack Weinbergea4a4532000-05-29 16:19:32 +00001386do_elif (pfile)
1387 cpp_reader *pfile;
1388{
Neil Boothb528a072000-11-12 11:46:21 +00001389 cpp_buffer *buffer = pfile->buffer;
1390 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001391
1392 if (ifs == NULL)
Neil Boothb528a072000-11-12 11:46:21 +00001393 cpp_error (pfile, "#elif without #if");
1394 else
Zack Weinbergea4a4532000-05-29 16:19:32 +00001395 {
Neil Boothb528a072000-11-12 11:46:21 +00001396 if (ifs->type == T_ELSE)
1397 {
1398 cpp_error (pfile, "#elif after #else");
Neil Booth50410422001-09-15 10:18:03 +00001399 cpp_error_with_line (pfile, ifs->line, 0,
Neil Boothb528a072000-11-12 11:46:21 +00001400 "the conditional began here");
1401 }
1402 ifs->type = T_ELIF;
1403
Neil Boothcef0d192001-07-26 06:02:47 +00001404 /* Only evaluate this if we aren't skipping elses. During
1405 evaluation, set skipping to false to get lexer warnings. */
1406 if (ifs->skip_elses)
1407 pfile->state.skipping = 1;
1408 else
Neil Boothb528a072000-11-12 11:46:21 +00001409 {
Neil Boothcef0d192001-07-26 06:02:47 +00001410 pfile->state.skipping = 0;
1411 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1412 ifs->skip_elses = ! pfile->state.skipping;
Neil Boothb528a072000-11-12 11:46:21 +00001413 }
Neil Boothcef0d192001-07-26 06:02:47 +00001414
1415 /* Invalidate any controlling macro. */
1416 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001417 }
Zack Weinbergea4a4532000-05-29 16:19:32 +00001418}
1419
Neil Boothcef0d192001-07-26 06:02:47 +00001420/* #endif pops the if stack and resets pfile->state.skipping. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001421static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001422do_endif (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001423 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001424{
Neil Boothb528a072000-11-12 11:46:21 +00001425 cpp_buffer *buffer = pfile->buffer;
1426 struct if_stack *ifs = buffer->if_stack;
Per Bothner7f2935c1995-03-16 13:59:07 -08001427
Zack Weinbergea4a4532000-05-29 16:19:32 +00001428 if (ifs == NULL)
1429 cpp_error (pfile, "#endif without #if");
Per Bothner7f2935c1995-03-16 13:59:07 -08001430 else
1431 {
Neil Boothcef0d192001-07-26 06:02:47 +00001432 /* Only check EOL if was not originally skipping. */
1433 if (!ifs->was_skipping)
1434 check_eol (pfile);
1435
Neil Booth93c803682000-10-28 17:59:06 +00001436 /* If potential control macro, we go back outside again. */
1437 if (ifs->next == 0 && ifs->mi_cmacro)
1438 {
Neil Booth6d18adb2001-07-29 17:27:57 +00001439 pfile->mi_valid = true;
Neil Booth93c803682000-10-28 17:59:06 +00001440 pfile->mi_cmacro = ifs->mi_cmacro;
1441 }
1442
Neil Boothb528a072000-11-12 11:46:21 +00001443 buffer->if_stack = ifs->next;
Neil Boothcef0d192001-07-26 06:02:47 +00001444 pfile->state.skipping = ifs->was_skipping;
Neil Booth2a967f32001-05-20 06:26:45 +00001445 obstack_free (&pfile->buffer_ob, ifs);
Per Bothner7f2935c1995-03-16 13:59:07 -08001446 }
Neil Booth93c803682000-10-28 17:59:06 +00001447}
Zack Weinberg041c3192000-07-04 01:58:21 +00001448
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001449/* Push an if_stack entry for a preprocessor conditional, and set
1450 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1451 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1452 we need to check here that we are at the top of the file. */
Zack Weinbergea4a4532000-05-29 16:19:32 +00001453static void
1454push_conditional (pfile, skip, type, cmacro)
1455 cpp_reader *pfile;
1456 int skip;
1457 int type;
1458 const cpp_hashnode *cmacro;
1459{
1460 struct if_stack *ifs;
Neil Boothb528a072000-11-12 11:46:21 +00001461 cpp_buffer *buffer = pfile->buffer;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001462
Neil Booth2a967f32001-05-20 06:26:45 +00001463 ifs = xobnew (&pfile->buffer_ob, struct if_stack);
Neil Booth50410422001-09-15 10:18:03 +00001464 ifs->line = pfile->directive_line;
Neil Boothb528a072000-11-12 11:46:21 +00001465 ifs->next = buffer->if_stack;
Neil Boothcef0d192001-07-26 06:02:47 +00001466 ifs->skip_elses = pfile->state.skipping || !skip;
1467 ifs->was_skipping = pfile->state.skipping;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001468 ifs->type = type;
Neil Booth6d18adb2001-07-29 17:27:57 +00001469 /* This condition is effectively a test for top-of-file. */
1470 if (pfile->mi_valid && pfile->mi_cmacro == 0)
Neil Booth93c803682000-10-28 17:59:06 +00001471 ifs->mi_cmacro = cmacro;
1472 else
1473 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001474
Neil Boothcef0d192001-07-26 06:02:47 +00001475 pfile->state.skipping = skip;
Neil Boothb528a072000-11-12 11:46:21 +00001476 buffer->if_stack = ifs;
Zack Weinberg7061aa51998-12-15 11:09:16 +00001477}
1478
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001479/* Read the tokens of the answer into the macro pool, in a directive
1480 of type TYPE. Only commit the memory if we intend it as permanent
1481 storage, i.e. the #assert case. Returns 0 on success, and sets
1482 ANSWERP to point to the answer. */
Neil Booth93c803682000-10-28 17:59:06 +00001483static int
1484parse_answer (pfile, answerp, type)
Zack Weinberg041c3192000-07-04 01:58:21 +00001485 cpp_reader *pfile;
1486 struct answer **answerp;
Neil Booth93c803682000-10-28 17:59:06 +00001487 int type;
Zack Weinberg041c3192000-07-04 01:58:21 +00001488{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001489 const cpp_token *paren;
Neil Booth93c803682000-10-28 17:59:06 +00001490 struct answer *answer;
Neil Booth8c3b2692001-09-30 10:03:11 +00001491 unsigned int acount;
Zack Weinberg041c3192000-07-04 01:58:21 +00001492
Neil Booth93c803682000-10-28 17:59:06 +00001493 /* In a conditional, it is legal to not have an open paren. We
1494 should save the following token in this case. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001495 paren = cpp_get_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001496
1497 /* If not a paren, see if we're OK. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001498 if (paren->type != CPP_OPEN_PAREN)
Zack Weinberg041c3192000-07-04 01:58:21 +00001499 {
Neil Booth93c803682000-10-28 17:59:06 +00001500 /* In a conditional no answer is a test for any answer. It
1501 could be followed by any token. */
1502 if (type == T_IF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001503 {
1504 _cpp_backup_tokens (pfile, 1);
1505 return 0;
1506 }
Neil Booth93c803682000-10-28 17:59:06 +00001507
1508 /* #unassert with no answer is valid - it removes all answers. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001509 if (type == T_UNASSERT && paren->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +00001510 return 0;
1511
Zack Weinberg041c3192000-07-04 01:58:21 +00001512 cpp_error (pfile, "missing '(' after predicate");
Neil Booth93c803682000-10-28 17:59:06 +00001513 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001514 }
1515
Neil Booth8c3b2692001-09-30 10:03:11 +00001516 for (acount = 0;; acount++)
Zack Weinberg041c3192000-07-04 01:58:21 +00001517 {
Neil Booth8c3b2692001-09-30 10:03:11 +00001518 size_t room_needed;
1519 const cpp_token *token = cpp_get_token (pfile);
1520 cpp_token *dest;
Zack Weinberg041c3192000-07-04 01:58:21 +00001521
Neil Booth93c803682000-10-28 17:59:06 +00001522 if (token->type == CPP_CLOSE_PAREN)
1523 break;
Zack Weinberg041c3192000-07-04 01:58:21 +00001524
1525 if (token->type == CPP_EOF)
1526 {
1527 cpp_error (pfile, "missing ')' to complete answer");
Neil Booth93c803682000-10-28 17:59:06 +00001528 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001529 }
Neil Booth8c3b2692001-09-30 10:03:11 +00001530
1531 /* struct answer includes the space for one token. */
1532 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1533
1534 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1535 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1536
1537 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1538 *dest = *token;
1539
1540 /* Drop whitespace at start, for answer equivalence purposes. */
1541 if (acount == 0)
1542 dest->flags &= ~PREV_WHITE;
Zack Weinberg041c3192000-07-04 01:58:21 +00001543 }
1544
Neil Booth8c3b2692001-09-30 10:03:11 +00001545 if (acount == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001546 {
1547 cpp_error (pfile, "predicate's answer is empty");
Neil Booth93c803682000-10-28 17:59:06 +00001548 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001549 }
1550
Neil Booth8c3b2692001-09-30 10:03:11 +00001551 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1552 answer->count = acount;
1553 answer->next = NULL;
Neil Booth93c803682000-10-28 17:59:06 +00001554 *answerp = answer;
Zack Weinberg041c3192000-07-04 01:58:21 +00001555
Neil Booth93c803682000-10-28 17:59:06 +00001556 return 0;
1557}
1558
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001559/* Parses an assertion directive of type TYPE, returning a pointer to
1560 the hash node of the predicate, or 0 on error. If an answer was
1561 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
Neil Booth93c803682000-10-28 17:59:06 +00001562static cpp_hashnode *
1563parse_assertion (pfile, answerp, type)
1564 cpp_reader *pfile;
1565 struct answer **answerp;
1566 int type;
1567{
1568 cpp_hashnode *result = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001569 const cpp_token *predicate;
Neil Booth93c803682000-10-28 17:59:06 +00001570
1571 /* We don't expand predicates or answers. */
1572 pfile->state.prevent_expansion++;
1573
Neil Booth93c803682000-10-28 17:59:06 +00001574 *answerp = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001575 predicate = cpp_get_token (pfile);
1576 if (predicate->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +00001577 cpp_error (pfile, "assertion without predicate");
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001578 else if (predicate->type != CPP_NAME)
Neil Booth93c803682000-10-28 17:59:06 +00001579 cpp_error (pfile, "predicate must be an identifier");
1580 else if (parse_answer (pfile, answerp, type) == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001581 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001582 unsigned int len = NODE_LEN (predicate->val.node);
Neil Booth93c803682000-10-28 17:59:06 +00001583 unsigned char *sym = alloca (len + 1);
1584
1585 /* Prefix '#' to get it out of macro namespace. */
1586 sym[0] = '#';
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001587 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
Neil Booth93c803682000-10-28 17:59:06 +00001588 result = cpp_lookup (pfile, sym, len + 1);
Zack Weinberg041c3192000-07-04 01:58:21 +00001589 }
1590
Neil Booth93c803682000-10-28 17:59:06 +00001591 pfile->state.prevent_expansion--;
1592 return result;
Zack Weinberg041c3192000-07-04 01:58:21 +00001593}
1594
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001595/* Returns a pointer to the pointer to CANDIDATE in the answer chain,
Zack Weinberg041c3192000-07-04 01:58:21 +00001596 or a pointer to NULL if the answer is not in the chain. */
Neil Booth93c803682000-10-28 17:59:06 +00001597static struct answer **
1598find_answer (node, candidate)
Zack Weinberg041c3192000-07-04 01:58:21 +00001599 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00001600 const struct answer *candidate;
Zack Weinberg041c3192000-07-04 01:58:21 +00001601{
Neil Booth93c803682000-10-28 17:59:06 +00001602 unsigned int i;
Zack Weinberg041c3192000-07-04 01:58:21 +00001603 struct answer **result;
1604
1605 for (result = &node->value.answers; *result; result = &(*result)->next)
Neil Booth93c803682000-10-28 17:59:06 +00001606 {
1607 struct answer *answer = *result;
1608
1609 if (answer->count == candidate->count)
1610 {
1611 for (i = 0; i < answer->count; i++)
1612 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1613 break;
1614
1615 if (i == answer->count)
1616 break;
1617 }
1618 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001619
1620 return result;
1621}
1622
Neil Booth93c803682000-10-28 17:59:06 +00001623/* Test an assertion within a preprocessor conditional. Returns
1624 non-zero on failure, zero on success. On success, the result of
1625 the test is written into VALUE. */
1626int
1627_cpp_test_assertion (pfile, value)
1628 cpp_reader *pfile;
1629 int *value;
1630{
1631 struct answer *answer;
1632 cpp_hashnode *node;
1633
1634 node = parse_assertion (pfile, &answer, T_IF);
1635 if (node)
1636 *value = (node->type == NT_ASSERTION &&
1637 (answer == 0 || *find_answer (node, answer) != 0));
1638
1639 /* We don't commit the memory for the answer - it's temporary only. */
1640 return node == 0;
1641}
1642
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001643/* Handle #assert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001644static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001645do_assert (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001646 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001647{
Zack Weinberg041c3192000-07-04 01:58:21 +00001648 struct answer *new_answer;
1649 cpp_hashnode *node;
Zack Weinberg15dad1d2000-05-18 15:55:46 +00001650
Neil Booth93c803682000-10-28 17:59:06 +00001651 node = parse_assertion (pfile, &new_answer, T_ASSERT);
Zack Weinberg041c3192000-07-04 01:58:21 +00001652 if (node)
1653 {
Neil Booth93c803682000-10-28 17:59:06 +00001654 /* Place the new answer in the answer list. First check there
1655 is not a duplicate. */
Zack Weinberg041c3192000-07-04 01:58:21 +00001656 new_answer->next = 0;
Neil Booth93c803682000-10-28 17:59:06 +00001657 if (node->type == NT_ASSERTION)
Zack Weinberg041c3192000-07-04 01:58:21 +00001658 {
Neil Booth93c803682000-10-28 17:59:06 +00001659 if (*find_answer (node, new_answer))
1660 {
Neil Bootha28c50352001-05-16 22:02:09 +00001661 cpp_warning (pfile, "\"%s\" re-asserted", NODE_NAME (node) + 1);
Neil Booth93c803682000-10-28 17:59:06 +00001662 return;
1663 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001664 new_answer->next = node->value.answers;
1665 }
Neil Booth8c3b2692001-09-30 10:03:11 +00001666
Neil Booth93c803682000-10-28 17:59:06 +00001667 node->type = NT_ASSERTION;
Zack Weinberg041c3192000-07-04 01:58:21 +00001668 node->value.answers = new_answer;
Neil Booth8c3b2692001-09-30 10:03:11 +00001669 BUFF_FRONT (pfile->a_buff) += (sizeof (struct answer)
1670 + (new_answer->count - 1)
1671 * sizeof (cpp_token));
1672 check_eol (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +00001673 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001674}
Zack Weinberg7061aa51998-12-15 11:09:16 +00001675
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001676/* Handle #unassert. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001677static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001678do_unassert (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001679 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001680{
Zack Weinberg041c3192000-07-04 01:58:21 +00001681 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00001682 struct answer *answer;
Zack Weinberg041c3192000-07-04 01:58:21 +00001683
Neil Booth93c803682000-10-28 17:59:06 +00001684 node = parse_assertion (pfile, &answer, T_UNASSERT);
1685 /* It isn't an error to #unassert something that isn't asserted. */
1686 if (node && node->type == NT_ASSERTION)
Zack Weinberg7061aa51998-12-15 11:09:16 +00001687 {
Zack Weinberg041c3192000-07-04 01:58:21 +00001688 if (answer)
Neil Booth93c803682000-10-28 17:59:06 +00001689 {
1690 struct answer **p = find_answer (node, answer), *temp;
1691
1692 /* Remove the answer from the list. */
1693 temp = *p;
1694 if (temp)
1695 *p = temp->next;
1696
1697 /* Did we free the last answer? */
1698 if (node->value.answers == 0)
1699 node->type = NT_VOID;
Neil Booth8c3b2692001-09-30 10:03:11 +00001700
1701 check_eol (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001702 }
1703 else
1704 _cpp_free_definition (node);
Zack Weinberg7061aa51998-12-15 11:09:16 +00001705 }
Neil Booth93c803682000-10-28 17:59:06 +00001706
1707 /* We don't commit the memory for the answer - it's temporary only. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001708}
Per Bothner7f2935c1995-03-16 13:59:07 -08001709
Zack Weinberg45b966d2000-03-13 22:01:08 +00001710/* These are for -D, -U, -A. */
1711
1712/* Process the string STR as if it appeared as the body of a #define.
1713 If STR is just an identifier, define it with value 1.
1714 If STR has anything after the identifier, then it should
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001715 be identifier=definition. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001716void
1717cpp_define (pfile, str)
1718 cpp_reader *pfile;
1719 const char *str;
1720{
1721 char *buf, *p;
1722 size_t count;
1723
Zack Weinberg45b966d2000-03-13 22:01:08 +00001724 /* Copy the entire option so we can modify it.
1725 Change the first "=" in the string to a space. If there is none,
Neil Booth86368122000-10-31 23:34:59 +00001726 tack " 1" on the end. */
1727
Neil Booth86368122000-10-31 23:34:59 +00001728 count = strlen (str);
Neil Booth4d6baaf2001-11-26 23:44:54 +00001729 buf = (char *) alloca (count + 3);
Neil Booth86368122000-10-31 23:34:59 +00001730 memcpy (buf, str, count);
1731
1732 p = strchr (str, '=');
Zack Weinberg45b966d2000-03-13 22:01:08 +00001733 if (p)
Neil Booth86368122000-10-31 23:34:59 +00001734 buf[p - str] = ' ';
Zack Weinberg45b966d2000-03-13 22:01:08 +00001735 else
1736 {
Neil Booth86368122000-10-31 23:34:59 +00001737 buf[count++] = ' ';
1738 buf[count++] = '1';
Zack Weinberg45b966d2000-03-13 22:01:08 +00001739 }
Neil Booth4d6baaf2001-11-26 23:44:54 +00001740 buf[count] = '\0';
Zack Weinberg45b966d2000-03-13 22:01:08 +00001741
Neil Booth29401c32001-08-22 20:37:20 +00001742 run_directive (pfile, T_DEFINE, buf, count);
Zack Weinberg2c8f0512000-08-29 18:37:37 +00001743}
1744
Neil Boothad2a0842000-12-17 00:13:54 +00001745/* Slight variant of the above for use by initialize_builtins. */
Zack Weinberg2c8f0512000-08-29 18:37:37 +00001746void
1747_cpp_define_builtin (pfile, str)
1748 cpp_reader *pfile;
1749 const char *str;
1750{
Neil Booth29401c32001-08-22 20:37:20 +00001751 run_directive (pfile, T_DEFINE, str, strlen (str));
Zack Weinberg45b966d2000-03-13 22:01:08 +00001752}
1753
1754/* Process MACRO as if it appeared as the body of an #undef. */
1755void
1756cpp_undef (pfile, macro)
1757 cpp_reader *pfile;
1758 const char *macro;
1759{
Neil Booth29401c32001-08-22 20:37:20 +00001760 run_directive (pfile, T_UNDEF, macro, strlen (macro));
Zack Weinberg45b966d2000-03-13 22:01:08 +00001761}
1762
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001763/* Process the string STR as if it appeared as the body of a #assert. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001764void
1765cpp_assert (pfile, str)
1766 cpp_reader *pfile;
1767 const char *str;
1768{
Neil Booth86368122000-10-31 23:34:59 +00001769 handle_assertion (pfile, str, T_ASSERT);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001770}
1771
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001772/* Process STR as if it appeared as the body of an #unassert. */
Zack Weinberg0b22d651999-03-15 18:42:46 +00001773void
1774cpp_unassert (pfile, str)
1775 cpp_reader *pfile;
Neil Booth7ceb3592000-03-11 00:49:44 +00001776 const char *str;
Zack Weinberg0b22d651999-03-15 18:42:46 +00001777{
Neil Booth86368122000-10-31 23:34:59 +00001778 handle_assertion (pfile, str, T_UNASSERT);
Zack Weinberg0b22d651999-03-15 18:42:46 +00001779}
1780
Neil Booth86368122000-10-31 23:34:59 +00001781/* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1782static void
1783handle_assertion (pfile, str, type)
1784 cpp_reader *pfile;
1785 const char *str;
1786 int type;
1787{
1788 size_t count = strlen (str);
1789 const char *p = strchr (str, '=');
1790
1791 if (p)
1792 {
1793 /* Copy the entire option so we can modify it. Change the first
1794 "=" in the string to a '(', and tack a ')' on the end. */
Neil Booth4d6baaf2001-11-26 23:44:54 +00001795 char *buf = (char *) alloca (count + 2);
Neil Booth86368122000-10-31 23:34:59 +00001796
1797 memcpy (buf, str, count);
1798 buf[p - str] = '(';
1799 buf[count++] = ')';
Neil Booth4d6baaf2001-11-26 23:44:54 +00001800 buf[count] = '\0';
Neil Booth86368122000-10-31 23:34:59 +00001801 str = buf;
1802 }
1803
Neil Booth29401c32001-08-22 20:37:20 +00001804 run_directive (pfile, type, str, count);
Neil Booth86368122000-10-31 23:34:59 +00001805}
1806
Neil Booth7e96d762001-01-13 01:00:01 +00001807/* The number of errors for a given reader. */
1808unsigned int
1809cpp_errors (pfile)
1810 cpp_reader *pfile;
1811{
1812 return pfile->errors;
1813}
1814
1815/* The options structure. */
1816cpp_options *
1817cpp_get_options (pfile)
1818 cpp_reader *pfile;
1819{
1820 return &pfile->opts;
1821}
1822
1823/* The callbacks structure. */
1824cpp_callbacks *
1825cpp_get_callbacks (pfile)
1826 cpp_reader *pfile;
1827{
1828 return &pfile->cb;
1829}
1830
Neil Boothd82fc102001-08-02 23:03:31 +00001831/* The line map set. */
Neil Booth47d89cf2001-08-11 07:33:39 +00001832const struct line_maps *
Neil Boothd82fc102001-08-02 23:03:31 +00001833cpp_get_line_maps (pfile)
1834 cpp_reader *pfile;
1835{
1836 return &pfile->line_maps;
1837}
1838
Neil Booth7e96d762001-01-13 01:00:01 +00001839/* Copy the given callbacks structure to our own. */
1840void
1841cpp_set_callbacks (pfile, cb)
1842 cpp_reader *pfile;
1843 cpp_callbacks *cb;
1844{
1845 pfile->cb = *cb;
1846}
1847
Neil Bootheb1f4d92000-12-18 19:00:26 +00001848/* Push a new buffer on the buffer stack. Returns the new buffer; it
1849 doesn't fail. It does not generate a file change call back; that
1850 is the responsibility of the caller. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00001851cpp_buffer *
Neil Booth29401c32001-08-22 20:37:20 +00001852cpp_push_buffer (pfile, buffer, len, from_stage3, return_at_eof)
Zack Weinbergc71f8352000-07-05 05:33:57 +00001853 cpp_reader *pfile;
1854 const U_CHAR *buffer;
Neil Booth3cf35932000-12-05 23:42:43 +00001855 size_t len;
Neil Booth29401c32001-08-22 20:37:20 +00001856 int from_stage3;
Neil Boothef6e9582001-08-04 12:01:59 +00001857 int return_at_eof;
Zack Weinbergc71f8352000-07-05 05:33:57 +00001858{
Neil Booth2a967f32001-05-20 06:26:45 +00001859 cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
Neil Booth93c803682000-10-28 17:59:06 +00001860
Neil Boothfde84342001-08-06 21:07:41 +00001861 /* Clears, amongst other things, if_stack and mi_cmacro. */
1862 memset (new, 0, sizeof (cpp_buffer));
Neil Boothad2a0842000-12-17 00:13:54 +00001863
Neil Boothfde84342001-08-06 21:07:41 +00001864 new->line_base = new->buf = new->cur = buffer;
1865 new->rlimit = buffer + len;
Neil Booth29401c32001-08-22 20:37:20 +00001866 new->from_stage3 = from_stage3;
Neil Booth3cf35932000-12-05 23:42:43 +00001867 new->prev = pfile->buffer;
Neil Boothef6e9582001-08-04 12:01:59 +00001868 new->return_at_eof = return_at_eof;
Neil Boothbdcbe492001-09-13 20:05:17 +00001869 new->saved_flags = BOL;
Neil Booth0bda4762000-12-11 07:45:16 +00001870
Neil Booth3cf35932000-12-05 23:42:43 +00001871 pfile->buffer = new;
Neil Booth0bda4762000-12-11 07:45:16 +00001872
Zack Weinbergc71f8352000-07-05 05:33:57 +00001873 return new;
1874}
1875
Neil Bootheb1f4d92000-12-18 19:00:26 +00001876/* If called from do_line, pops a single buffer. Otherwise pops all
1877 buffers until a real file is reached. Generates appropriate
1878 call-backs. */
Neil Boothef6e9582001-08-04 12:01:59 +00001879void
1880_cpp_pop_buffer (pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00001881 cpp_reader *pfile;
1882{
Neil Boothfde84342001-08-06 21:07:41 +00001883 cpp_buffer *buffer = pfile->buffer;
Neil Boothad2a0842000-12-17 00:13:54 +00001884 struct if_stack *ifs;
Andreas Schwabb7e30d82002-01-03 09:41:00 +00001885 bool pushed = false;
Zack Weinbergc71f8352000-07-05 05:33:57 +00001886
Neil Boothfde84342001-08-06 21:07:41 +00001887 /* Walk back up the conditional stack till we reach its level at
1888 entry to this file, issuing error messages. */
1889 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
Neil Booth50410422001-09-15 10:18:03 +00001890 cpp_error_with_line (pfile, ifs->line, 0,
Neil Boothfde84342001-08-06 21:07:41 +00001891 "unterminated #%s", dtable[ifs->type].name);
1892
Neil Booth97293892001-09-14 22:04:46 +00001893 /* In case of a missing #endif. */
Neil Booth67821e32001-08-05 17:31:25 +00001894 pfile->state.skipping = 0;
Neil Booth29401c32001-08-22 20:37:20 +00001895
1896 /* Update the reader's buffer before _cpp_do_file_change. */
1897 pfile->buffer = buffer->prev;
1898
1899 if (buffer->inc)
Andreas Schwabb7e30d82002-01-03 09:41:00 +00001900 pushed = _cpp_pop_file_buffer (pfile, buffer->inc);
Neil Booth29401c32001-08-22 20:37:20 +00001901
Andreas Schwabb7e30d82002-01-03 09:41:00 +00001902 if (!pushed)
1903 obstack_free (&pfile->buffer_ob, buffer);
Zack Weinbergc71f8352000-07-05 05:33:57 +00001904}
1905
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001906/* Enter all recognised directives in the hash table. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00001907void
Neil Booth2a967f32001-05-20 06:26:45 +00001908_cpp_init_directives (pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00001909 cpp_reader *pfile;
1910{
Neil Booth766ee682001-01-29 19:20:12 +00001911 unsigned int i;
Neil Booth93c803682000-10-28 17:59:06 +00001912 cpp_hashnode *node;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001913
John David Anglin37b85242001-03-02 01:11:50 +00001914 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
Neil Booth93c803682000-10-28 17:59:06 +00001915 {
Neil Booth766ee682001-01-29 19:20:12 +00001916 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
1917 node->directive_index = i + 1;
Neil Booth93c803682000-10-28 17:59:06 +00001918 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00001919}