blob: 63d0c174a8386f27cf97829178faaa719273b8dc [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 Boothd6d52dd2001-01-13 18:39:26 +00003 1999, 2000, 2001 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). */
39
40struct if_stack
41{
42 struct if_stack *next;
Neil Booth50410422001-09-15 10:18:03 +000043 unsigned int line; /* Line where condition started. */
Neil Booth93c803682000-10-28 17:59:06 +000044 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
Neil Boothcef0d192001-07-26 06:02:47 +000045 bool skip_elses; /* Can future #else / #elif be skipped? */
46 bool was_skipping; /* If were skipping on entry. */
47 int type; /* Most recent conditional, for diagnostics. */
Zack Weinberg88ae23e2000-03-08 23:35:19 +000048};
Zack Weinberg88ae23e2000-03-08 23:35:19 +000049
Neil Booth93c803682000-10-28 17:59:06 +000050/* Values for the origin field of struct directive. KANDR directives
51 come from traditional (K&R) C. STDC89 directives come from the
52 1989 C standard. EXTENSION directives are extensions. */
53#define KANDR 0
54#define STDC89 1
55#define EXTENSION 2
56
57/* Values for the flags field of struct directive. COND indicates a
58 conditional; IF_COND an opening conditional. INCL means to treat
59 "..." and <...> as q-char and h-char sequences respectively. IN_I
60 means this directive should be handled even if -fpreprocessed is in
61 effect (these are the directives with callback hooks). */
62#define COND (1 << 0)
63#define IF_COND (1 << 1)
64#define INCL (1 << 2)
65#define IN_I (1 << 3)
66
67/* Defines one #-directive, including how to handle it. */
68typedef void (*directive_handler) PARAMS ((cpp_reader *));
69typedef struct directive directive;
70struct directive
71{
72 directive_handler handler; /* Function to handle directive. */
73 const U_CHAR *name; /* Name of directive. */
74 unsigned short length; /* Length of name. */
75 unsigned char origin; /* Origin of directive. */
76 unsigned char flags; /* Flags describing this directive. */
77};
78
Zack Weinberg1316f1f2000-02-06 07:53:50 +000079/* Forward declarations. */
80
Neil Booth93c803682000-10-28 17:59:06 +000081static void skip_rest_of_line PARAMS ((cpp_reader *));
82static void check_eol PARAMS ((cpp_reader *));
Neil Boothfe6c2db2000-11-15 19:25:22 +000083static void start_directive PARAMS ((cpp_reader *));
84static void end_directive PARAMS ((cpp_reader *, int));
Neil Booth18a9d8f2001-09-16 11:23:56 +000085static void directive_diagnostics
86 PARAMS ((cpp_reader *, const directive *, int));
Neil Booth93c803682000-10-28 17:59:06 +000087static void run_directive PARAMS ((cpp_reader *, int,
Neil Booth0bda4762000-12-11 07:45:16 +000088 const char *, size_t));
Neil Booth93c803682000-10-28 17:59:06 +000089static int glue_header_name PARAMS ((cpp_reader *, cpp_token *));
90static int parse_include PARAMS ((cpp_reader *, cpp_token *));
Zack Weinberg041c3192000-07-04 01:58:21 +000091static void push_conditional PARAMS ((cpp_reader *, int, int,
92 const cpp_hashnode *));
Neil Booth28e0f042000-12-09 12:06:37 +000093static unsigned int read_flag PARAMS ((cpp_reader *, unsigned int));
Zack Weinbergc71f8352000-07-05 05:33:57 +000094static int strtoul_for_line PARAMS ((const U_CHAR *, unsigned int,
Zack Weinberg041c3192000-07-04 01:58:21 +000095 unsigned long *));
Neil Booth29b10742000-11-13 18:40:37 +000096static void do_diagnostic PARAMS ((cpp_reader *, enum error_type, int));
Neil Boothb528a072000-11-12 11:46:21 +000097static cpp_hashnode *lex_macro_node PARAMS ((cpp_reader *));
Neil Boothba133c92001-03-15 07:57:13 +000098static void do_include_common PARAMS ((cpp_reader *, enum include_type));
Neil Booth93c803682000-10-28 17:59:06 +000099static void do_pragma_once PARAMS ((cpp_reader *));
100static void do_pragma_poison PARAMS ((cpp_reader *));
101static void do_pragma_system_header PARAMS ((cpp_reader *));
102static void do_pragma_dependency PARAMS ((cpp_reader *));
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000103static int get__Pragma_string PARAMS ((cpp_reader *, cpp_token *));
104static unsigned char *destringize PARAMS ((const cpp_string *,
105 unsigned int *));
Neil Booth93c803682000-10-28 17:59:06 +0000106static int parse_answer PARAMS ((cpp_reader *, struct answer **, int));
107static cpp_hashnode *parse_assertion PARAMS ((cpp_reader *, struct answer **,
108 int));
109static struct answer ** find_answer PARAMS ((cpp_hashnode *,
110 const struct answer *));
Neil Booth86368122000-10-31 23:34:59 +0000111static void handle_assertion PARAMS ((cpp_reader *, const char *, int));
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +0000112
Zack Weinberg168d3732000-03-14 06:34:11 +0000113/* This is the table of directive handlers. It is ordered by
114 frequency of occurrence; the numbers at the end are directive
115 counts from all the source code I have lying around (egcs and libc
116 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
Neil Booth93c803682000-10-28 17:59:06 +0000117 pcmcia-cs-3.0.9). This is no longer important as directive lookup
118 is now O(1). All extensions other than #warning and #include_next
119 are deprecated. The name is where the extension appears to have
120 come from. */
Jeff Lawe9a25f71997-11-02 14:19:36 -0700121
Neil Booth93c803682000-10-28 17:59:06 +0000122#define DIRECTIVE_TABLE \
123D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
124D(include, T_INCLUDE, KANDR, INCL) /* 52262 */ \
125D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
126D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
127D(if, T_IF, KANDR, COND | IF_COND) /* 18162 */ \
128D(else, T_ELSE, KANDR, COND) /* 9863 */ \
129D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
130D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
131D(line, T_LINE, KANDR, IN_I) /* 2465 */ \
Neil Boothf000294d2001-01-31 07:56:07 +0000132D(elif, T_ELIF, STDC89, COND) /* 610 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000133D(error, T_ERROR, STDC89, 0) /* 475 */ \
134D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
135D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
136D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL) /* 19 */ \
137D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
138D(import, T_IMPORT, EXTENSION, INCL) /* 0 ObjC */ \
139D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
140D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
141SCCS_ENTRY /* 0 SVR4? */
Per Bothner7f2935c1995-03-16 13:59:07 -0800142
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000143/* #sccs is not always recognized. */
144#ifdef SCCS_DIRECTIVE
Zack Weinberg041c3192000-07-04 01:58:21 +0000145# define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000146#else
147# define SCCS_ENTRY /* nothing */
148#endif
149
Zack Weinberg168d3732000-03-14 06:34:11 +0000150/* Use the table to generate a series of prototypes, an enum for the
151 directive names, and an array of directive handlers. */
152
153/* The directive-processing functions are declared to return int
154 instead of void, because some old compilers have trouble with
155 pointers to functions returning void. */
156
Kazu Hirataec5c56d2001-08-01 17:57:27 +0000157/* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000158#define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
Zack Weinberg168d3732000-03-14 06:34:11 +0000159DIRECTIVE_TABLE
160#undef D
161
Zack Weinberg041c3192000-07-04 01:58:21 +0000162#define D(n, tag, o, f) tag,
Zack Weinberg168d3732000-03-14 06:34:11 +0000163enum
164{
165 DIRECTIVE_TABLE
166 N_DIRECTIVES
Per Bothner7f2935c1995-03-16 13:59:07 -0800167};
Zack Weinberg168d3732000-03-14 06:34:11 +0000168#undef D
169
Kazu Hirataec5c56d2001-08-01 17:57:27 +0000170/* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000171#define D(name, t, origin, flags) \
Zack Weinberg12cf91f2000-05-04 04:38:01 +0000172{ CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
Zack Weinberg041c3192000-07-04 01:58:21 +0000173 sizeof STRINGX(name) - 1, origin, flags },
Neil Booth93c803682000-10-28 17:59:06 +0000174static const directive dtable[] =
Zack Weinberg168d3732000-03-14 06:34:11 +0000175{
176DIRECTIVE_TABLE
177};
178#undef D
179#undef DIRECTIVE_TABLE
Per Bothner7f2935c1995-03-16 13:59:07 -0800180
Neil Boothbdcbe492001-09-13 20:05:17 +0000181#define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
Neil Booth67821e32001-08-05 17:31:25 +0000182
Neil Booth93c803682000-10-28 17:59:06 +0000183/* Skip any remaining tokens in a directive. */
184static void
185skip_rest_of_line (pfile)
186 cpp_reader *pfile;
187{
Neil Booth93c803682000-10-28 17:59:06 +0000188 /* Discard all stacked contexts. */
189 while (pfile->context != &pfile->base_context)
190 _cpp_pop_context (pfile);
191
Neil Boothb528a072000-11-12 11:46:21 +0000192 /* Sweep up all tokens remaining on the line. */
Neil Booth345894b2001-09-16 13:44:29 +0000193 if (! SEEN_EOL ())
194 while (_cpp_lex_token (pfile)->type != CPP_EOF)
195 ;
Neil Booth93c803682000-10-28 17:59:06 +0000196}
197
198/* Ensure there are no stray tokens at the end of a directive. */
199static void
200check_eol (pfile)
201 cpp_reader *pfile;
202{
Neil Booth345894b2001-09-16 13:44:29 +0000203 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
204 cpp_pedwarn (pfile, "extra tokens at end of #%s directive",
205 pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +0000206}
207
Neil Boothfe6c2db2000-11-15 19:25:22 +0000208/* Called when entering a directive, _Pragma or command-line directive. */
209static void
210start_directive (pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000211 cpp_reader *pfile;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000212{
Neil Booth7f2f1a62000-11-14 18:32:06 +0000213 /* Setup in-directive state. */
214 pfile->state.in_directive = 1;
215 pfile->state.save_comments = 0;
216
Neil Booth93c803682000-10-28 17:59:06 +0000217 /* Some handlers need the position of the # for diagnostics. */
Neil Booth8bbbef32001-08-04 16:28:14 +0000218 pfile->directive_line = pfile->line;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000219}
220
221/* Called when leaving a directive, _Pragma or command-line directive. */
222static void
223end_directive (pfile, skip_line)
224 cpp_reader *pfile;
225 int skip_line;
226{
Neil Boothfe6c2db2000-11-15 19:25:22 +0000227 /* We don't skip for an assembler #. */
228 if (skip_line)
Neil Booth67821e32001-08-05 17:31:25 +0000229 {
230 skip_rest_of_line (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +0000231 if (!pfile->keep_tokens)
232 {
233 pfile->cur_run = &pfile->base_run;
234 pfile->cur_token = pfile->base_run.base;
235 }
Neil Booth67821e32001-08-05 17:31:25 +0000236 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000237
238 /* Restore state. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000239 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
240 pfile->state.in_directive = 0;
241 pfile->state.angled_headers = 0;
Neil Booth642ce432000-12-07 23:17:56 +0000242 pfile->state.line_extension = 0;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000243 pfile->directive = 0;
244}
245
Neil Booth18a9d8f2001-09-16 11:23:56 +0000246/* Output diagnostics for a directive DIR. INDENTED is non-zero if
247 the '#' was indented. */
248
249static void
250directive_diagnostics (pfile, dir, indented)
251 cpp_reader *pfile;
252 const directive *dir;
253 int indented;
254{
255 if (pfile->state.line_extension)
256 {
257 if (CPP_PEDANTIC (pfile)
258 && ! pfile->state.skipping)
259 cpp_pedwarn (pfile, "style of line directive is a GCC extension");
260 }
261 else
262 {
263 /* Issue -pedantic warnings for extensions. */
264 if (CPP_PEDANTIC (pfile)
265 && ! pfile->state.skipping
266 && dir->origin == EXTENSION)
267 cpp_pedwarn (pfile, "#%s is a GCC extension", dir->name);
268
269 /* Traditionally, a directive is ignored unless its # is in
270 column 1. Therefore in code intended to work with K+R
271 compilers, directives added by C89 must have their #
272 indented, and directives present in traditional C must not.
273 This is true even of directives in skipped conditional
274 blocks. */
275 if (CPP_WTRADITIONAL (pfile))
276 {
277 if (dir == &dtable[T_ELIF])
278 cpp_warning (pfile, "suggest not using #elif in traditional C");
279 else if (indented && dir->origin == KANDR)
280 cpp_warning (pfile,
281 "traditional C ignores #%s with the # indented",
282 dir->name);
283 else if (!indented && dir->origin != KANDR)
284 cpp_warning (pfile,
285 "suggest hiding #%s from traditional C with an indented #",
286 dir->name);
287 }
288 }
289}
290
291/* Check if we have a known directive. INDENTED is non-zero if the
292 '#' of the directive was indented. This function is in this file
293 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
294 non-zero if the line of tokens has been handled, zero if we should
295 continue processing the line. */
296
Neil Boothfe6c2db2000-11-15 19:25:22 +0000297int
298_cpp_handle_directive (pfile, indented)
299 cpp_reader *pfile;
300 int indented;
301{
Neil Boothfe6c2db2000-11-15 19:25:22 +0000302 const directive *dir = 0;
Neil Booth345894b2001-09-16 13:44:29 +0000303 const cpp_token *dname;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000304 int skip = 1;
305
306 start_directive (pfile);
Neil Booth345894b2001-09-16 13:44:29 +0000307 dname = _cpp_lex_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000308
Neil Booth345894b2001-09-16 13:44:29 +0000309 if (dname->type == CPP_NAME)
Neil Booth0d9f2342000-09-18 18:43:05 +0000310 {
Neil Booth345894b2001-09-16 13:44:29 +0000311 if (dname->val.node->directive_index)
312 dir = &dtable[dname->val.node->directive_index - 1];
Neil Booth93c803682000-10-28 17:59:06 +0000313 }
Neil Booth18a9d8f2001-09-16 11:23:56 +0000314 /* We do not recognise the # followed by a number extension in
315 assembler code. */
Neil Booth345894b2001-09-16 13:44:29 +0000316 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth93c803682000-10-28 17:59:06 +0000317 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000318 dir = &dtable[T_LINE];
319 pfile->state.line_extension = 1;
Neil Booth0d9f2342000-09-18 18:43:05 +0000320 }
321
Neil Booth93c803682000-10-28 17:59:06 +0000322 if (dir)
Neil Booth0d9f2342000-09-18 18:43:05 +0000323 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000324 /* If we have a directive that is not an opening conditional,
325 invalidate any control macro. */
326 if (! (dir->flags & IF_COND))
327 pfile->mi_valid = false;
Neil Booth93c803682000-10-28 17:59:06 +0000328
Neil Booth18a9d8f2001-09-16 11:23:56 +0000329 /* Kluge alert. In order to be sure that code like this
330
331 #define HASH #
332 HASH define foo bar
333
334 does not cause '#define foo bar' to get executed when
335 compiled with -save-temps, we recognize directives in
336 -fpreprocessed mode only if the # is in column 1. cppmacro.c
337 puts a space in fron of any '#' at the start of a macro. */
338 if (CPP_OPTION (pfile, preprocessed)
339 && (indented || !(dir->flags & IN_I)))
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000340 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000341 skip = 0;
342 dir = 0;
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000343 }
344 else
Neil Booth93c803682000-10-28 17:59:06 +0000345 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000346 /* In failed conditional groups, all non-conditional
347 directives are ignored. Before doing that, whether
348 skipping or not, we should lex angle-bracketed headers
349 correctly, and maybe output some diagnostics. */
350 pfile->state.angled_headers = dir->flags & INCL;
351 if (! CPP_OPTION (pfile, preprocessed))
352 directive_diagnostics (pfile, dir, indented);
353 if (pfile->state.skipping && !(dir->flags & COND))
354 dir = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000355 }
356 }
Neil Booth345894b2001-09-16 13:44:29 +0000357 else if (dname->type == CPP_EOF)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000358 ; /* CPP_EOF is the "null directive". */
359 else
Neil Booth0d9f2342000-09-18 18:43:05 +0000360 {
Neil Booth93c803682000-10-28 17:59:06 +0000361 /* An unknown directive. Don't complain about it in assembly
362 source: we don't know where the comments are, and # may
363 introduce assembler pseudo-ops. Don't complain about invalid
364 directives in skipped conditional groups (6.10 p4). */
Neil Boothbdb05a72000-11-26 17:31:13 +0000365 if (CPP_OPTION (pfile, lang) == CLK_ASM)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000366 skip = 0;
367 else if (!pfile->state.skipping)
Neil Booth93c803682000-10-28 17:59:06 +0000368 cpp_error (pfile, "invalid preprocessing directive #%s",
Neil Booth345894b2001-09-16 13:44:29 +0000369 cpp_token_as_text (pfile, dname));
Neil Booth0d9f2342000-09-18 18:43:05 +0000370 }
371
Neil Booth18a9d8f2001-09-16 11:23:56 +0000372 if (dir)
373 {
374 pfile->directive = dir;
375 (*pfile->directive->handler) (pfile);
376 }
377 else if (skip == 0)
378 _cpp_backup_tokens (pfile, 1);
379
380 end_directive (pfile, skip);
Neil Boothfe6c2db2000-11-15 19:25:22 +0000381 return skip;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000382}
383
Neil Booth93c803682000-10-28 17:59:06 +0000384/* Directive handler wrapper used by the command line option
385 processor. */
386static void
Neil Booth29401c32001-08-22 20:37:20 +0000387run_directive (pfile, dir_no, buf, count)
Per Bothner7f2935c1995-03-16 13:59:07 -0800388 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +0000389 int dir_no;
390 const char *buf;
391 size_t count;
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000392{
Neil Booth29401c32001-08-22 20:37:20 +0000393 cpp_push_buffer (pfile, (const U_CHAR *) buf, count,
394 /* from_stage3 */ true, 1);
Neil Booth0bda4762000-12-11 07:45:16 +0000395 start_directive (pfile);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000396 /* We don't want a leading # to be interpreted as a directive. */
397 pfile->buffer->saved_flags = 0;
Neil Boothf71aebb2001-05-27 18:06:00 +0000398 pfile->directive = &dtable[dir_no];
399 (void) (*pfile->directive->handler) (pfile);
Neil Booth0bda4762000-12-11 07:45:16 +0000400 end_directive (pfile, 1);
Neil Boothef6e9582001-08-04 12:01:59 +0000401 _cpp_pop_buffer (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000402}
Per Bothner7f2935c1995-03-16 13:59:07 -0800403
Neil Booth93c803682000-10-28 17:59:06 +0000404/* Checks for validity the macro name in #define, #undef, #ifdef and
405 #ifndef directives. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000406static cpp_hashnode *
Neil Booth93c803682000-10-28 17:59:06 +0000407lex_macro_node (pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000408 cpp_reader *pfile;
409{
Zack Weinbergb8363a22001-07-01 18:48:13 +0000410 cpp_hashnode *node;
Neil Booth345894b2001-09-16 13:44:29 +0000411 const cpp_token *token = _cpp_lex_token (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000412
Zack Weinberg92936ec2000-07-19 20:18:08 +0000413 /* The token immediately after #define must be an identifier. That
Zack Weinbergb8363a22001-07-01 18:48:13 +0000414 identifier may not be "defined", per C99 6.10.8p4.
415 In C++, it may not be any of the "named operators" either,
416 per C++98 [lex.digraph], [lex.key].
417 Finally, the identifier may not have been poisoned. (In that case
418 the lexer has issued the error message for us.) */
Zack Weinberg92936ec2000-07-19 20:18:08 +0000419
Neil Booth345894b2001-09-16 13:44:29 +0000420 if (token->type != CPP_NAME)
Zack Weinberg041c3192000-07-04 01:58:21 +0000421 {
Neil Booth345894b2001-09-16 13:44:29 +0000422 if (token->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +0000423 cpp_error (pfile, "no macro name given in #%s directive",
424 pfile->directive->name);
Neil Booth345894b2001-09-16 13:44:29 +0000425 else if (token->flags & NAMED_OP)
Neil Booth93c803682000-10-28 17:59:06 +0000426 cpp_error (pfile,
Zack Weinbergb8363a22001-07-01 18:48:13 +0000427 "\"%s\" cannot be used as a macro name as it is an operator in C++",
Neil Booth345894b2001-09-16 13:44:29 +0000428 NODE_NAME (token->val.node));
Zack Weinberg92936ec2000-07-19 20:18:08 +0000429 else
Neil Booth93c803682000-10-28 17:59:06 +0000430 cpp_error (pfile, "macro names must be identifiers");
Zack Weinbergb8363a22001-07-01 18:48:13 +0000431
432 return 0;
Zack Weinberg041c3192000-07-04 01:58:21 +0000433 }
Zack Weinbergb8363a22001-07-01 18:48:13 +0000434
Neil Booth345894b2001-09-16 13:44:29 +0000435 node = token->val.node;
Zack Weinbergb8363a22001-07-01 18:48:13 +0000436 if (node->flags & NODE_POISONED)
437 return 0;
438
439 if (node == pfile->spec_nodes.n_defined)
Zack Weinbergba89d662000-08-04 01:30:06 +0000440 {
Zack Weinbergb8363a22001-07-01 18:48:13 +0000441 cpp_error (pfile, "\"%s\" cannot be used as a macro name",
442 NODE_NAME (node));
443 return 0;
Zack Weinbergba89d662000-08-04 01:30:06 +0000444 }
445
Zack Weinbergb8363a22001-07-01 18:48:13 +0000446 return node;
Per Bothner7f2935c1995-03-16 13:59:07 -0800447}
Per Bothner7f2935c1995-03-16 13:59:07 -0800448
Neil Booth93c803682000-10-28 17:59:06 +0000449/* Process a #define directive. Most work is done in cppmacro.c. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000450static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000451do_define (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800452 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800453{
Neil Booth93c803682000-10-28 17:59:06 +0000454 cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000455
Neil Booth93c803682000-10-28 17:59:06 +0000456 if (node)
457 {
Neil Booth93c803682000-10-28 17:59:06 +0000458 if (_cpp_create_definition (pfile, node))
459 if (pfile->cb.define)
Neil Booth8bbbef32001-08-04 16:28:14 +0000460 (*pfile->cb.define) (pfile, pfile->directive_line, node);
Neil Booth93c803682000-10-28 17:59:06 +0000461 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800462}
463
Neil Booth93c803682000-10-28 17:59:06 +0000464/* Handle #undef. Marks the identifier NT_VOID in the hash table. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000465static void
Zack Weinberg041c3192000-07-04 01:58:21 +0000466do_undef (pfile)
467 cpp_reader *pfile;
468{
Neil Booth93c803682000-10-28 17:59:06 +0000469 cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000470
471 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
472 is not currently defined as a macro name. */
Neil Booth93c803682000-10-28 17:59:06 +0000473 if (node && node->type == NT_MACRO)
Zack Weinberg041c3192000-07-04 01:58:21 +0000474 {
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000475 if (pfile->cb.undef)
Neil Booth8bbbef32001-08-04 16:28:14 +0000476 (*pfile->cb.undef) (pfile, pfile->directive_line, node);
Zack Weinberg041c3192000-07-04 01:58:21 +0000477
Neil Booth618cdda2001-02-25 09:43:03 +0000478 if (node->flags & NODE_WARN)
Neil Bootha28c50352001-05-16 22:02:09 +0000479 cpp_warning (pfile, "undefining \"%s\"", NODE_NAME (node));
Zack Weinberg041c3192000-07-04 01:58:21 +0000480
481 _cpp_free_definition (node);
Zack Weinberg041c3192000-07-04 01:58:21 +0000482 }
Neil Booth93c803682000-10-28 17:59:06 +0000483 check_eol (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000484}
485
Neil Booth93c803682000-10-28 17:59:06 +0000486/* Helper routine used by parse_include. Reinterpret the current line
487 as an h-char-sequence (< ... >); we are looking at the first token
488 after the <. Returns zero on success. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000489static int
Neil Booth93c803682000-10-28 17:59:06 +0000490glue_header_name (pfile, header)
Per Bothner7f2935c1995-03-16 13:59:07 -0800491 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +0000492 cpp_token *header;
Per Bothner7f2935c1995-03-16 13:59:07 -0800493{
Neil Booth93c803682000-10-28 17:59:06 +0000494 cpp_token token;
495 unsigned char *buffer, *token_mem;
496 size_t len, total_len = 0, capacity = 1024;
Per Bothner7f2935c1995-03-16 13:59:07 -0800497
Neil Booth93c803682000-10-28 17:59:06 +0000498 /* To avoid lexed tokens overwriting our glued name, we can only
499 allocate from the string pool once we've lexed everything. */
500
501 buffer = (unsigned char *) xmalloc (capacity);
502 for (;;)
Zack Weinberg168d3732000-03-14 06:34:11 +0000503 {
Neil Booth7f2f1a62000-11-14 18:32:06 +0000504 cpp_get_token (pfile, &token);
Neil Booth93c803682000-10-28 17:59:06 +0000505
506 if (token.type == CPP_GREATER || token.type == CPP_EOF)
507 break;
508
509 len = cpp_token_len (&token);
510 if (total_len + len > capacity)
511 {
512 capacity = (capacity + len) * 2;
Kaveh R. Ghazidd3b81b2000-11-17 04:16:55 +0000513 buffer = (unsigned char *) xrealloc (buffer, capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000514 }
515
516 if (token.flags & PREV_WHITE)
517 buffer[total_len++] = ' ';
518
519 total_len = cpp_spell_token (pfile, &token, &buffer[total_len]) - buffer;
520 }
521
522 if (token.type == CPP_EOF)
523 cpp_error (pfile, "missing terminating > character");
524 else
525 {
Neil Booth7868b4a2001-03-04 12:02:02 +0000526 token_mem = _cpp_pool_alloc (&pfile->ident_pool, total_len + 1);
Neil Booth93c803682000-10-28 17:59:06 +0000527 memcpy (token_mem, buffer, total_len);
Neil Booth7868b4a2001-03-04 12:02:02 +0000528 token_mem[total_len] = '\0';
Neil Booth93c803682000-10-28 17:59:06 +0000529
530 header->type = CPP_HEADER_NAME;
531 header->flags &= ~PREV_WHITE;
532 header->val.str.len = total_len;
533 header->val.str.text = token_mem;
534 }
535
536 free ((PTR) buffer);
537 return token.type == CPP_EOF;
538}
539
540/* Parse the header name of #include, #include_next, #import and
541 #pragma dependency. Returns zero on success. */
542static int
543parse_include (pfile, header)
544 cpp_reader *pfile;
545 cpp_token *header;
546{
Neil Booth93c803682000-10-28 17:59:06 +0000547 const unsigned char *dir;
548
Neil Booth09b82252001-07-29 22:27:20 +0000549 if (pfile->directive == &dtable[T_PRAGMA])
Neil Booth93c803682000-10-28 17:59:06 +0000550 dir = U"pragma dependency";
551 else
552 dir = pfile->directive->name;
553
554 /* Allow macro expansion. */
555 cpp_get_token (pfile, header);
556 if (header->type != CPP_STRING && header->type != CPP_HEADER_NAME)
557 {
558 if (header->type != CPP_LESS)
Zack Weinberg041c3192000-07-04 01:58:21 +0000559 {
560 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir);
561 return 1;
562 }
Neil Booth93c803682000-10-28 17:59:06 +0000563 if (glue_header_name (pfile, header))
564 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +0000565 }
Neil Booth93c803682000-10-28 17:59:06 +0000566
567 if (header->val.str.len == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +0000568 {
569 cpp_error (pfile, "empty file name in #%s", dir);
570 return 1;
Richard Kennercfb3ee11997-04-13 14:30:13 -0400571 }
572
Zack Weinberg041c3192000-07-04 01:58:21 +0000573 return 0;
Zack Weinberg168d3732000-03-14 06:34:11 +0000574}
575
Neil Boothba133c92001-03-15 07:57:13 +0000576/* Handle #include, #include_next and #import. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000577static void
Neil Boothba133c92001-03-15 07:57:13 +0000578do_include_common (pfile, type)
Zack Weinberg168d3732000-03-14 06:34:11 +0000579 cpp_reader *pfile;
Neil Boothba133c92001-03-15 07:57:13 +0000580 enum include_type type;
Zack Weinberg168d3732000-03-14 06:34:11 +0000581{
Neil Booth93c803682000-10-28 17:59:06 +0000582 cpp_token header;
Zack Weinberg168d3732000-03-14 06:34:11 +0000583
Neil Booth09b82252001-07-29 22:27:20 +0000584 /* For #include_next, if this is the primary source file, warn and
585 use the normal search logic. */
586 if (type == IT_INCLUDE_NEXT && ! pfile->buffer->prev)
587 {
588 cpp_warning (pfile, "#include_next in primary source file");
589 type = IT_INCLUDE;
590 }
591 else if (type == IT_IMPORT && CPP_OPTION (pfile, warn_import))
592 {
593 CPP_OPTION (pfile, warn_import) = 0;
594 cpp_warning (pfile,
595 "#import is obsolete, use an #ifndef wrapper in the header file");
596 }
597
Neil Booth93c803682000-10-28 17:59:06 +0000598 if (!parse_include (pfile, &header))
Neil Boothba133c92001-03-15 07:57:13 +0000599 {
600 /* Prevent #include recursion. */
Neil Boothd8693c62001-08-21 23:05:12 +0000601 if (pfile->line_maps.depth >= CPP_STACK_MAX)
Neil Boothba133c92001-03-15 07:57:13 +0000602 cpp_fatal (pfile, "#include nested too deeply");
Neil Boothba133c92001-03-15 07:57:13 +0000603 else
604 {
Neil Booth09b82252001-07-29 22:27:20 +0000605 check_eol (pfile);
606 /* Get out of macro context, if we are. */
Neil Boothbdcbe492001-09-13 20:05:17 +0000607 skip_rest_of_line (pfile);
Neil Booth09b82252001-07-29 22:27:20 +0000608 if (pfile->cb.include)
Neil Booth8bbbef32001-08-04 16:28:14 +0000609 (*pfile->cb.include) (pfile, pfile->directive_line,
610 pfile->directive->name, &header);
Neil Boothba133c92001-03-15 07:57:13 +0000611
612 _cpp_execute_include (pfile, &header, type);
613 }
614 }
615}
616
617static void
618do_include (pfile)
619 cpp_reader *pfile;
620{
621 do_include_common (pfile, IT_INCLUDE);
Zack Weinberg168d3732000-03-14 06:34:11 +0000622}
623
Zack Weinberg711b8822000-07-18 00:59:49 +0000624static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000625do_import (pfile)
626 cpp_reader *pfile;
627{
Neil Boothba133c92001-03-15 07:57:13 +0000628 do_include_common (pfile, IT_IMPORT);
Zack Weinberg168d3732000-03-14 06:34:11 +0000629}
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000630
Zack Weinberg711b8822000-07-18 00:59:49 +0000631static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000632do_include_next (pfile)
633 cpp_reader *pfile;
634{
Neil Boothba133c92001-03-15 07:57:13 +0000635 do_include_common (pfile, IT_INCLUDE_NEXT);
Per Bothner7f2935c1995-03-16 13:59:07 -0800636}
637
Neil Booth28e0f042000-12-09 12:06:37 +0000638/* Subroutine of do_line. Read possible flags after file name. LAST
639 is the last flag seen; 0 if this is the first flag. Return the flag
640 if it is valid, 0 at the end of the directive. Otherwise complain. */
Jason Merrilld3a34a01999-08-14 00:42:07 +0000641
Neil Booth642ce432000-12-07 23:17:56 +0000642static unsigned int
Neil Booth28e0f042000-12-09 12:06:37 +0000643read_flag (pfile, last)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000644 cpp_reader *pfile;
Neil Booth28e0f042000-12-09 12:06:37 +0000645 unsigned int last;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000646{
Neil Booth345894b2001-09-16 13:44:29 +0000647 const cpp_token *token = _cpp_lex_token (pfile);
Jason Merrilld3a34a01999-08-14 00:42:07 +0000648
Neil Booth345894b2001-09-16 13:44:29 +0000649 if (token->type == CPP_NUMBER && token->val.str.len == 1)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000650 {
Neil Booth345894b2001-09-16 13:44:29 +0000651 unsigned int flag = token->val.str.text[0] - '0';
Neil Booth28e0f042000-12-09 12:06:37 +0000652
653 if (flag > last && flag <= 4
654 && (flag != 4 || last == 3)
655 && (flag != 2 || last == 0))
656 return flag;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000657 }
Neil Booth93c803682000-10-28 17:59:06 +0000658
Neil Booth345894b2001-09-16 13:44:29 +0000659 if (token->type != CPP_EOF)
Neil Booth642ce432000-12-07 23:17:56 +0000660 cpp_error (pfile, "invalid flag \"%s\" in line directive",
Neil Booth345894b2001-09-16 13:44:29 +0000661 cpp_token_as_text (pfile, token));
Neil Booth93c803682000-10-28 17:59:06 +0000662 return 0;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000663}
664
Zack Weinberg041c3192000-07-04 01:58:21 +0000665/* Another subroutine of do_line. Convert a number in STR, of length
666 LEN, to binary; store it in NUMP, and return 0 if the number was
Zack Weinberg5ef865d2000-08-02 07:08:49 +0000667 well-formed, 1 if not. Temporary, hopefully. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000668static int
669strtoul_for_line (str, len, nump)
670 const U_CHAR *str;
671 unsigned int len;
672 unsigned long *nump;
673{
674 unsigned long reg = 0;
675 U_CHAR c;
676 while (len--)
677 {
678 c = *str++;
679 if (!ISDIGIT (c))
680 return 1;
681 reg *= 10;
682 reg += c - '0';
683 }
684 *nump = reg;
685 return 0;
686}
687
Zack Weinberg5538ada1999-02-04 06:36:54 -0500688/* Interpret #line command.
689 Note that the filename string (if any) is treated as if it were an
690 include filename. That means no escape handling. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800691
Zack Weinberg711b8822000-07-18 00:59:49 +0000692static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000693do_line (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800694 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800695{
Neil Booth93c803682000-10-28 17:59:06 +0000696 cpp_token token;
Neil Boothbb74c962001-08-17 22:23:49 +0000697 const char *new_file = pfile->map->to_file;
698 unsigned long new_lineno;
699 unsigned int cap, new_sysp = pfile->map->sysp;
700 enum lc_reason reason = LC_RENAME;
Per Bothner7f2935c1995-03-16 13:59:07 -0800701
Neil Booth27e25642000-11-27 08:00:04 +0000702 /* C99 raised the minimum limit on #line numbers. */
703 cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
704
Neil Booth18a9d8f2001-09-16 11:23:56 +0000705 /* Putting this in _cpp_handle_directive risks two calls to
706 _cpp_backup_tokens in some circumstances, which can segfault. */
707 if (pfile->state.line_extension)
708 _cpp_backup_tokens (pfile, 1);
709
Neil Booth93c803682000-10-28 17:59:06 +0000710 /* #line commands expand macros. */
Neil Booth7f2f1a62000-11-14 18:32:06 +0000711 cpp_get_token (pfile, &token);
Neil Booth93c803682000-10-28 17:59:06 +0000712 if (token.type != CPP_NUMBER
713 || strtoul_for_line (token.val.str.text, token.val.str.len, &new_lineno))
Per Bothner7f2935c1995-03-16 13:59:07 -0800714 {
Neil Booth93c803682000-10-28 17:59:06 +0000715 cpp_error (pfile, "\"%s\" after #line is not a positive integer",
716 cpp_token_as_text (pfile, &token));
Zack Weinberg9ec72912000-08-09 19:41:12 +0000717 return;
Zack Weinberg5538ada1999-02-04 06:36:54 -0500718 }
Zack Weinberg5538ada1999-02-04 06:36:54 -0500719
Neil Boothd82fc102001-08-02 23:03:31 +0000720 if (CPP_PEDANTIC (pfile) && ! pfile->state.line_extension
721 && (new_lineno == 0 || new_lineno > cap))
Zack Weinberg041c3192000-07-04 01:58:21 +0000722 cpp_pedwarn (pfile, "line number out of range");
Zack Weinberg5538ada1999-02-04 06:36:54 -0500723
Neil Booth7f2f1a62000-11-14 18:32:06 +0000724 cpp_get_token (pfile, &token);
Neil Booth27e25642000-11-27 08:00:04 +0000725 if (token.type == CPP_STRING)
Zack Weinberg5538ada1999-02-04 06:36:54 -0500726 {
Neil Boothbb74c962001-08-17 22:23:49 +0000727 new_file = (const char *) token.val.str.text;
Neil Booth93c803682000-10-28 17:59:06 +0000728
Neil Boothd6d52dd2001-01-13 18:39:26 +0000729 /* Only accept flags for the # 55 form. */
Neil Boothfde84342001-08-06 21:07:41 +0000730 if (pfile->state.line_extension)
Neil Booth93c803682000-10-28 17:59:06 +0000731 {
Neil Booth47d89cf2001-08-11 07:33:39 +0000732 int flag;
Neil Booth93c803682000-10-28 17:59:06 +0000733
Neil Boothbb74c962001-08-17 22:23:49 +0000734 new_sysp = 0;
Neil Booth47d89cf2001-08-11 07:33:39 +0000735 flag = read_flag (pfile, 0);
Neil Booth642ce432000-12-07 23:17:56 +0000736 if (flag == 1)
Neil Booth93c803682000-10-28 17:59:06 +0000737 {
Neil Boothd82fc102001-08-02 23:03:31 +0000738 reason = LC_ENTER;
Neil Boothfde84342001-08-06 21:07:41 +0000739 /* Fake an include for cpp_included (). */
Neil Boothbb74c962001-08-17 22:23:49 +0000740 _cpp_fake_include (pfile, new_file);
Neil Booth28e0f042000-12-09 12:06:37 +0000741 flag = read_flag (pfile, flag);
Neil Booth93c803682000-10-28 17:59:06 +0000742 }
Neil Booth642ce432000-12-07 23:17:56 +0000743 else if (flag == 2)
Neil Booth93c803682000-10-28 17:59:06 +0000744 {
Neil Boothd82fc102001-08-02 23:03:31 +0000745 reason = LC_LEAVE;
Neil Booth28e0f042000-12-09 12:06:37 +0000746 flag = read_flag (pfile, flag);
Neil Booth93c803682000-10-28 17:59:06 +0000747 }
Neil Booth642ce432000-12-07 23:17:56 +0000748 if (flag == 3)
Neil Booth93c803682000-10-28 17:59:06 +0000749 {
Neil Boothbb74c962001-08-17 22:23:49 +0000750 new_sysp = 1;
Neil Booth28e0f042000-12-09 12:06:37 +0000751 flag = read_flag (pfile, flag);
752 if (flag == 4)
Neil Boothbb74c962001-08-17 22:23:49 +0000753 new_sysp = 2;
Neil Boothad2a0842000-12-17 00:13:54 +0000754 }
Neil Booth93c803682000-10-28 17:59:06 +0000755 }
Neil Boothfde84342001-08-06 21:07:41 +0000756 check_eol (pfile);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000757 }
Neil Booth27e25642000-11-27 08:00:04 +0000758 else if (token.type != CPP_EOF)
759 {
760 cpp_error (pfile, "\"%s\" is not a valid filename",
761 cpp_token_as_text (pfile, &token));
762 return;
763 }
Zack Weinberg941e09b1998-12-15 11:17:06 +0000764
Neil Boothbdcbe492001-09-13 20:05:17 +0000765 skip_rest_of_line (pfile);
Neil Boothbb74c962001-08-17 22:23:49 +0000766 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
Neil Booth27e25642000-11-27 08:00:04 +0000767}
768
Neil Booth67821e32001-08-05 17:31:25 +0000769/* Arrange the file_change callback. pfile->line has changed to
Neil Booth47d89cf2001-08-11 07:33:39 +0000770 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
771 header, 2 for a sytem header that needs to be extern "C" protected,
772 and zero otherwise. */
Neil Bootheb1f4d92000-12-18 19:00:26 +0000773void
Neil Booth47d89cf2001-08-11 07:33:39 +0000774_cpp_do_file_change (pfile, reason, to_file, file_line, sysp)
Neil Booth27e25642000-11-27 08:00:04 +0000775 cpp_reader *pfile;
Neil Boothd82fc102001-08-02 23:03:31 +0000776 enum lc_reason reason;
Neil Booth47d89cf2001-08-11 07:33:39 +0000777 const char *to_file;
Neil Booth67821e32001-08-05 17:31:25 +0000778 unsigned int file_line;
Neil Booth47d89cf2001-08-11 07:33:39 +0000779 unsigned int sysp;
Neil Booth27e25642000-11-27 08:00:04 +0000780{
Neil Booth47d89cf2001-08-11 07:33:39 +0000781 pfile->map = add_line_map (&pfile->line_maps, reason, sysp,
782 pfile->line, to_file, file_line);
Neil Boothd82fc102001-08-02 23:03:31 +0000783
Neil Bootheb1f4d92000-12-18 19:00:26 +0000784 if (pfile->cb.file_change)
Neil Booth47d89cf2001-08-11 07:33:39 +0000785 (*pfile->cb.file_change) (pfile, pfile->map);
Per Bothner7f2935c1995-03-16 13:59:07 -0800786}
Zack Weinberg941e09b1998-12-15 11:17:06 +0000787
Per Bothner7f2935c1995-03-16 13:59:07 -0800788/*
Neil Booth838f3132000-09-24 10:42:09 +0000789 * Report a warning or error detected by the program we are
790 * processing. Use the directive's tokens in the error message.
Per Bothner7f2935c1995-03-16 13:59:07 -0800791 */
792
Zack Weinberg711b8822000-07-18 00:59:49 +0000793static void
Neil Booth29b10742000-11-13 18:40:37 +0000794do_diagnostic (pfile, code, print_dir)
Per Bothner7f2935c1995-03-16 13:59:07 -0800795 cpp_reader *pfile;
Neil Booth838f3132000-09-24 10:42:09 +0000796 enum error_type code;
Neil Booth29b10742000-11-13 18:40:37 +0000797 int print_dir;
Per Bothner7f2935c1995-03-16 13:59:07 -0800798{
Neil Booth97293892001-09-14 22:04:46 +0000799 if (_cpp_begin_message (pfile, code, 0, 0))
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000800 {
Neil Booth29b10742000-11-13 18:40:37 +0000801 if (print_dir)
802 fprintf (stderr, "#%s ", pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +0000803 pfile->state.prevent_expansion++;
804 cpp_output_line (pfile, stderr);
805 pfile->state.prevent_expansion--;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000806 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800807}
808
Neil Booth838f3132000-09-24 10:42:09 +0000809static void
810do_error (pfile)
811 cpp_reader *pfile;
812{
Neil Booth29b10742000-11-13 18:40:37 +0000813 do_diagnostic (pfile, ERROR, 1);
Neil Booth838f3132000-09-24 10:42:09 +0000814}
Per Bothner7f2935c1995-03-16 13:59:07 -0800815
Zack Weinberg711b8822000-07-18 00:59:49 +0000816static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000817do_warning (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800818 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800819{
Neil Booth2f878972001-04-08 10:01:18 +0000820 /* We want #warning diagnostics to be emitted in system headers too. */
821 do_diagnostic (pfile, WARNING_SYSHDR, 1);
Per Bothner7f2935c1995-03-16 13:59:07 -0800822}
823
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000824/* Report program identification. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800825
Zack Weinberg711b8822000-07-18 00:59:49 +0000826static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000827do_ident (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800828 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800829{
Neil Booth93c803682000-10-28 17:59:06 +0000830 cpp_token str;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000831
Neil Booth7f2f1a62000-11-14 18:32:06 +0000832 cpp_get_token (pfile, &str);
Neil Booth93c803682000-10-28 17:59:06 +0000833 if (str.type != CPP_STRING)
834 cpp_error (pfile, "invalid #ident");
835 else if (pfile->cb.ident)
Neil Booth8bbbef32001-08-04 16:28:14 +0000836 (*pfile->cb.ident) (pfile, pfile->directive_line, &str.val.str);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000837
Neil Booth93c803682000-10-28 17:59:06 +0000838 check_eol (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -0800839}
840
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000841/* Pragmata handling. We handle some of these, and pass the rest on
842 to the front end. C99 defines three pragmas and says that no macro
843 expansion is to be performed on them; whether or not macro
844 expansion happens for other pragmas is implementation defined.
Neil Bootha9499412000-11-09 21:18:15 +0000845 This implementation never macro-expands the text after #pragma. */
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000846
847/* Sub-handlers for the pragmas needing treatment here.
Kazu Hirataec5c56d2001-08-01 17:57:27 +0000848 They return 1 if the token buffer is to be popped, 0 if not. */
Neil Boothd82fc102001-08-02 23:03:31 +0000849typedef void (*pragma_cb) PARAMS ((cpp_reader *));
Nathan Sidwell82443372000-06-23 10:56:09 +0000850struct pragma_entry
851{
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000852 struct pragma_entry *next;
Zack Weinberg041c3192000-07-04 01:58:21 +0000853 const char *name;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000854 size_t len;
855 int isnspace;
856 union {
Neil Boothd82fc102001-08-02 23:03:31 +0000857 pragma_cb handler;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000858 struct pragma_entry *space;
859 } u;
Nathan Sidwell82443372000-06-23 10:56:09 +0000860};
861
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000862void
863cpp_register_pragma (pfile, space, name, handler)
Nathan Sidwell82443372000-06-23 10:56:09 +0000864 cpp_reader *pfile;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000865 const char *space;
866 const char *name;
Neil Boothd82fc102001-08-02 23:03:31 +0000867 pragma_cb handler;
Nathan Sidwell82443372000-06-23 10:56:09 +0000868{
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000869 struct pragma_entry **x, *new;
870 size_t len;
871
872 x = &pfile->pragmas;
873 if (space)
874 {
875 struct pragma_entry *p = pfile->pragmas;
876 len = strlen (space);
877 while (p)
878 {
879 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
880 {
881 x = &p->u.space;
882 goto found;
883 }
884 p = p->next;
885 }
886 cpp_ice (pfile, "unknown #pragma namespace %s", space);
887 return;
888 }
889
890 found:
Neil Boothbef985f2001-08-11 12:37:19 +0000891 new = (struct pragma_entry *)
892 _cpp_pool_alloc (&pfile->macro_pool, sizeof (struct pragma_entry));
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000893 new->name = name;
894 new->len = strlen (name);
895 new->isnspace = 0;
896 new->u.handler = handler;
897
898 new->next = *x;
899 *x = new;
900}
901
902void
903cpp_register_pragma_space (pfile, space)
904 cpp_reader *pfile;
905 const char *space;
906{
907 struct pragma_entry *new;
908 const struct pragma_entry *p = pfile->pragmas;
909 size_t len = strlen (space);
910
911 while (p)
912 {
913 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
Zack Weinberg6e19bb32000-08-18 22:42:14 +0000914 /* Multiple different callers are allowed to register the same
915 namespace. */
916 return;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000917 p = p->next;
918 }
919
Neil Boothbef985f2001-08-11 12:37:19 +0000920 new = (struct pragma_entry *)
921 _cpp_pool_alloc (&pfile->macro_pool, sizeof (struct pragma_entry));
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000922 new->name = space;
923 new->len = len;
924 new->isnspace = 1;
925 new->u.space = 0;
926
927 new->next = pfile->pragmas;
928 pfile->pragmas = new;
929}
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000930
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000931void
932_cpp_init_internal_pragmas (pfile)
933 cpp_reader *pfile;
934{
935 /* top level */
936 cpp_register_pragma (pfile, 0, "poison", do_pragma_poison);
937 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
938
939 /* GCC namespace */
940 cpp_register_pragma_space (pfile, "GCC");
941
942 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
943 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
944 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
Nathan Sidwell82443372000-06-23 10:56:09 +0000945}
Per Bothner7f2935c1995-03-16 13:59:07 -0800946
Zack Weinberg711b8822000-07-18 00:59:49 +0000947static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000948do_pragma (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800949 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800950{
Neil Boothd82fc102001-08-02 23:03:31 +0000951 pragma_cb handler = NULL;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000952 const struct pragma_entry *p;
Neil Booth93c803682000-10-28 17:59:06 +0000953 cpp_token tok;
Neil Boothbdcbe492001-09-13 20:05:17 +0000954 unsigned int count = 0;
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000955
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000956 p = pfile->pragmas;
Neil Booth93c803682000-10-28 17:59:06 +0000957 pfile->state.prevent_expansion++;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000958
959 new_space:
Neil Boothbdcbe492001-09-13 20:05:17 +0000960 count++;
Neil Booth93c803682000-10-28 17:59:06 +0000961 cpp_get_token (pfile, &tok);
962 if (tok.type == CPP_NAME)
Alexandre Oliva0172e2b2000-02-27 06:24:27 +0000963 {
Neil Bootha28c50352001-05-16 22:02:09 +0000964 const cpp_hashnode *node = tok.val.node;
Neil Bootha28c50352001-05-16 22:02:09 +0000965 size_t len = NODE_LEN (node);
966
Neil Booth93c803682000-10-28 17:59:06 +0000967 while (p)
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000968 {
Neil Booth2a967f32001-05-20 06:26:45 +0000969 if (strlen (p->name) == len
970 && !memcmp (p->name, NODE_NAME (node), len))
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000971 {
Neil Booth93c803682000-10-28 17:59:06 +0000972 if (p->isnspace)
973 {
974 p = p->u.space;
975 goto new_space;
976 }
977 else
978 {
Neil Boothd82fc102001-08-02 23:03:31 +0000979 handler = p->u.handler;
Neil Booth93c803682000-10-28 17:59:06 +0000980 break;
981 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000982 }
Neil Booth93c803682000-10-28 17:59:06 +0000983 p = p->next;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000984 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000985 }
986
Neil Booth97293892001-09-14 22:04:46 +0000987 /* FIXME. This is an awful kludge to get the front ends to update
988 their notion of line number for diagnostic purposes. The line
989 number should be passed to the handler and they should do it
990 themselves. Stand-alone CPP must ignore us, otherwise it will
991 prefix the directive with spaces, hence the 1. Ugh. */
992 if (pfile->cb.line_change)
993 (*pfile->cb.line_change)(pfile, &tok, 1);
994
Neil Boothd82fc102001-08-02 23:03:31 +0000995 if (handler)
996 (*handler) (pfile);
997 else if (pfile->cb.def_pragma)
Neil Boothbdcbe492001-09-13 20:05:17 +0000998 {
999 _cpp_backup_tokens (pfile, count);
1000 (*pfile->cb.def_pragma) (pfile, pfile->directive_line);
1001 }
Neil Booth97293892001-09-14 22:04:46 +00001002 pfile->state.prevent_expansion--;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001003}
1004
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001005static void
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001006do_pragma_once (pfile)
1007 cpp_reader *pfile;
1008{
Branko Cibej317639a2000-09-26 00:54:04 +02001009 cpp_warning (pfile, "#pragma once is obsolete");
1010
Neil Booth642ce432000-12-07 23:17:56 +00001011 if (pfile->buffer->prev == NULL)
Neil Booth93c803682000-10-28 17:59:06 +00001012 cpp_warning (pfile, "#pragma once in main file");
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001013 else
Neil Booth642ce432000-12-07 23:17:56 +00001014 _cpp_never_reread (pfile->buffer->inc);
Neil Booth93c803682000-10-28 17:59:06 +00001015
1016 check_eol (pfile);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001017}
1018
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001019static void
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001020do_pragma_poison (pfile)
1021 cpp_reader *pfile;
1022{
1023 /* Poison these symbols so that all subsequent usage produces an
1024 error message. */
Neil Booth345894b2001-09-16 13:44:29 +00001025 const cpp_token *tok;
Zack Weinbergf8f769e2000-05-28 05:56:38 +00001026 cpp_hashnode *hp;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001027
Neil Booth93c803682000-10-28 17:59:06 +00001028 pfile->state.poisoned_ok = 1;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001029 for (;;)
1030 {
Neil Booth345894b2001-09-16 13:44:29 +00001031 tok = _cpp_lex_token (pfile);
1032 if (tok->type == CPP_EOF)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001033 break;
Neil Booth345894b2001-09-16 13:44:29 +00001034 if (tok->type != CPP_NAME)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001035 {
Neil Booth93c803682000-10-28 17:59:06 +00001036 cpp_error (pfile, "invalid #pragma GCC poison directive");
1037 break;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001038 }
1039
Neil Booth345894b2001-09-16 13:44:29 +00001040 hp = tok->val.node;
Neil Booth93c803682000-10-28 17:59:06 +00001041 if (hp->flags & NODE_POISONED)
1042 continue;
1043
1044 if (hp->type == NT_MACRO)
Neil Bootha28c50352001-05-16 22:02:09 +00001045 cpp_warning (pfile, "poisoning existing macro \"%s\"", NODE_NAME (hp));
Neil Booth93c803682000-10-28 17:59:06 +00001046 _cpp_free_definition (hp);
1047 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001048 }
Neil Booth93c803682000-10-28 17:59:06 +00001049 pfile->state.poisoned_ok = 0;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001050}
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001051
1052/* Mark the current header as a system header. This will suppress
1053 some categories of warnings (notably those from -pedantic). It is
1054 intended for use in system libraries that cannot be implemented in
1055 conforming C, but cannot be certain that their headers appear in a
1056 system include directory. To prevent abuse, it is rejected in the
1057 primary source file. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001058static void
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001059do_pragma_system_header (pfile)
1060 cpp_reader *pfile;
1061{
Neil Booth614c7d32000-12-04 07:32:04 +00001062 cpp_buffer *buffer = pfile->buffer;
1063
1064 if (buffer->prev == 0)
1065 cpp_warning (pfile, "#pragma system_header ignored outside include file");
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001066 else
Neil Boothd82fc102001-08-02 23:03:31 +00001067 {
1068 check_eol (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +00001069 skip_rest_of_line (pfile);
Neil Boothd82fc102001-08-02 23:03:31 +00001070 cpp_make_system_header (pfile, 1, 0);
1071 }
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001072}
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001073
1074/* Check the modified date of the current include file against a specified
1075 file. Issue a diagnostic, if the specified file is newer. We use this to
1076 determine if a fixed header should be refixed. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001077static void
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001078do_pragma_dependency (pfile)
1079 cpp_reader *pfile;
1080{
Neil Booth93c803682000-10-28 17:59:06 +00001081 cpp_token header, msg;
1082 int ordering;
Zack Weinberg041c3192000-07-04 01:58:21 +00001083
Neil Booth93c803682000-10-28 17:59:06 +00001084 if (parse_include (pfile, &header))
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001085 return;
Zack Weinberg041c3192000-07-04 01:58:21 +00001086
Neil Booth93c803682000-10-28 17:59:06 +00001087 ordering = _cpp_compare_file_date (pfile, &header);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001088 if (ordering < 0)
Neil Booth93c803682000-10-28 17:59:06 +00001089 cpp_warning (pfile, "cannot find source %s",
1090 cpp_token_as_text (pfile, &header));
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001091 else if (ordering > 0)
1092 {
Neil Booth93c803682000-10-28 17:59:06 +00001093 cpp_warning (pfile, "current file is older than %s",
1094 cpp_token_as_text (pfile, &header));
Neil Booth93c803682000-10-28 17:59:06 +00001095 cpp_get_token (pfile, &msg);
Neil Booth29b10742000-11-13 18:40:37 +00001096 if (msg.type != CPP_EOF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001097 {
1098 _cpp_backup_tokens (pfile, 1);
1099 do_diagnostic (pfile, WARNING, 0);
1100 }
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001101 }
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001102}
1103
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001104/* Check syntax is "(string-literal)". Returns 0 on success. */
1105static int
1106get__Pragma_string (pfile, string)
1107 cpp_reader *pfile;
1108 cpp_token *string;
1109{
1110 cpp_token paren;
1111
1112 cpp_get_token (pfile, &paren);
1113 if (paren.type != CPP_OPEN_PAREN)
1114 return 1;
1115
1116 cpp_get_token (pfile, string);
1117 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
1118 return 1;
1119
1120 cpp_get_token (pfile, &paren);
1121 return paren.type != CPP_CLOSE_PAREN;
1122}
1123
1124/* Returns a malloced buffer containing a destringized cpp_string by
1125 removing the first \ of \" and \\ sequences. */
1126static unsigned char *
1127destringize (in, len)
1128 const cpp_string *in;
1129 unsigned int *len;
1130{
1131 const unsigned char *src, *limit;
1132 unsigned char *dest, *result;
1133
1134 dest = result = (unsigned char *) xmalloc (in->len);
1135 for (src = in->text, limit = src + in->len; src < limit;)
1136 {
1137 /* We know there is a character following the backslash. */
1138 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1139 src++;
1140 *dest++ = *src++;
1141 }
1142
1143 *len = dest - result;
1144 return result;
1145}
1146
1147void
1148_cpp_do__Pragma (pfile)
1149 cpp_reader *pfile;
1150{
1151 cpp_token string;
1152 unsigned char *buffer;
1153 unsigned int len;
1154
1155 if (get__Pragma_string (pfile, &string))
Neil Booth67821e32001-08-05 17:31:25 +00001156 cpp_error (pfile, "_Pragma takes a parenthesized string literal");
1157 else
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001158 {
Neil Booth50410422001-09-15 10:18:03 +00001159 /* Ideally, we'd like
1160 token1 _Pragma ("foo") token2
1161 to be output as
1162 token1
1163 # 7 "file.c"
1164 #pragma foo
1165 # 7 "file.c"
1166 token2
1167 Getting these correct line markers is a little tricky. */
1168
1169 unsigned int orig_line = pfile->line;
Neil Booth67821e32001-08-05 17:31:25 +00001170 buffer = destringize (&string.val.str, &len);
Neil Booth29401c32001-08-22 20:37:20 +00001171 run_directive (pfile, T_PRAGMA, (char *) buffer, len);
Neil Booth67821e32001-08-05 17:31:25 +00001172 free ((PTR) buffer);
Neil Booth50410422001-09-15 10:18:03 +00001173 pfile->line = orig_line;
1174 pfile->buffer->saved_flags = BOL;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001175 }
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001176}
1177
Per Bothner7f2935c1995-03-16 13:59:07 -08001178/* Just ignore #sccs, on systems where we define it at all. */
Zack Weinberg07aa0b02000-04-01 22:55:25 +00001179#ifdef SCCS_DIRECTIVE
Zack Weinberg711b8822000-07-18 00:59:49 +00001180static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001181do_sccs (pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +00001182 cpp_reader *pfile ATTRIBUTE_UNUSED;
Per Bothner7f2935c1995-03-16 13:59:07 -08001183{
Per Bothner7f2935c1995-03-16 13:59:07 -08001184}
Zack Weinberg07aa0b02000-04-01 22:55:25 +00001185#endif
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001186
Zack Weinberg711b8822000-07-18 00:59:49 +00001187static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001188do_ifdef (pfile)
1189 cpp_reader *pfile;
1190{
Neil Booth93c803682000-10-28 17:59:06 +00001191 int skip = 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001192
Neil Boothcef0d192001-07-26 06:02:47 +00001193 if (! pfile->state.skipping)
Neil Booth93c803682000-10-28 17:59:06 +00001194 {
1195 const cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +00001196
Neil Booth93c803682000-10-28 17:59:06 +00001197 if (node)
1198 skip = node->type != NT_MACRO;
Geoffrey Keatingb43db0b2000-12-02 22:28:44 +00001199
1200 if (node)
1201 check_eol (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001202 }
1203
1204 push_conditional (pfile, skip, T_IFDEF, 0);
Zack Weinberg168d3732000-03-14 06:34:11 +00001205}
1206
Zack Weinberg711b8822000-07-18 00:59:49 +00001207static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001208do_ifndef (pfile)
1209 cpp_reader *pfile;
1210{
Neil Booth93c803682000-10-28 17:59:06 +00001211 int skip = 1;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001212 const cpp_hashnode *node = 0;
Zack Weinberg168d3732000-03-14 06:34:11 +00001213
Neil Boothcef0d192001-07-26 06:02:47 +00001214 if (! pfile->state.skipping)
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001215 {
Neil Booth93c803682000-10-28 17:59:06 +00001216 node = lex_macro_node (pfile);
1217 if (node)
1218 skip = node->type == NT_MACRO;
Geoffrey Keatingb43db0b2000-12-02 22:28:44 +00001219
1220 if (node)
1221 check_eol (pfile);
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001222 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001223
Neil Booth93c803682000-10-28 17:59:06 +00001224 push_conditional (pfile, skip, T_IFNDEF, node);
Per Bothner7f2935c1995-03-16 13:59:07 -08001225}
1226
Neil Booth6d18adb2001-07-29 17:27:57 +00001227/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1228 pfile->mi_ind_cmacro so we can handle multiple-include
1229 optimisations. If macro expansion occurs in the expression, we
1230 cannot treat it as a controlling conditional, since the expansion
1231 could change in the future. That is handled by cpp_get_token. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001232
Zack Weinberg711b8822000-07-18 00:59:49 +00001233static void
Zack Weinbergea4a4532000-05-29 16:19:32 +00001234do_if (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001235 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001236{
Neil Booth93c803682000-10-28 17:59:06 +00001237 int skip = 1;
Per Bothner7f2935c1995-03-16 13:59:07 -08001238
Neil Boothcef0d192001-07-26 06:02:47 +00001239 if (! pfile->state.skipping)
Neil Booth6d18adb2001-07-29 17:27:57 +00001240 skip = _cpp_parse_expr (pfile) == 0;
Neil Booth93c803682000-10-28 17:59:06 +00001241
Neil Booth6d18adb2001-07-29 17:27:57 +00001242 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
Per Bothner7f2935c1995-03-16 13:59:07 -08001243}
1244
Neil Boothb528a072000-11-12 11:46:21 +00001245/* Flip skipping state if appropriate and continue without changing
Zack Weinbergea4a4532000-05-29 16:19:32 +00001246 if_stack; this is so that the error message for missing #endif's
1247 etc. will point to the original #if. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001248
Zack Weinberg711b8822000-07-18 00:59:49 +00001249static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001250do_else (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001251 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001252{
Neil Boothb528a072000-11-12 11:46:21 +00001253 cpp_buffer *buffer = pfile->buffer;
1254 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001255
1256 if (ifs == NULL)
Neil Booth93c803682000-10-28 17:59:06 +00001257 cpp_error (pfile, "#else without #if");
1258 else
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001259 {
Neil Booth93c803682000-10-28 17:59:06 +00001260 if (ifs->type == T_ELSE)
1261 {
1262 cpp_error (pfile, "#else after #else");
Neil Booth50410422001-09-15 10:18:03 +00001263 cpp_error_with_line (pfile, ifs->line, 0,
Neil Booth93c803682000-10-28 17:59:06 +00001264 "the conditional began here");
1265 }
Neil Boothb528a072000-11-12 11:46:21 +00001266 ifs->type = T_ELSE;
1267
Neil Boothcef0d192001-07-26 06:02:47 +00001268 /* Skip any future (erroneous) #elses or #elifs. */
1269 pfile->state.skipping = ifs->skip_elses;
1270 ifs->skip_elses = true;
Neil Booth93c803682000-10-28 17:59:06 +00001271
1272 /* Invalidate any controlling macro. */
1273 ifs->mi_cmacro = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001274
Neil Boothcef0d192001-07-26 06:02:47 +00001275 /* Only check EOL if was not originally skipping. */
1276 if (!ifs->was_skipping)
1277 check_eol (pfile);
1278 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001279}
1280
Neil Booth93c803682000-10-28 17:59:06 +00001281/* handle a #elif directive by not changing if_stack either. see the
1282 comment above do_else. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001283
Zack Weinberg711b8822000-07-18 00:59:49 +00001284static void
Zack Weinbergea4a4532000-05-29 16:19:32 +00001285do_elif (pfile)
1286 cpp_reader *pfile;
1287{
Neil Boothb528a072000-11-12 11:46:21 +00001288 cpp_buffer *buffer = pfile->buffer;
1289 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001290
1291 if (ifs == NULL)
Neil Boothb528a072000-11-12 11:46:21 +00001292 cpp_error (pfile, "#elif without #if");
1293 else
Zack Weinbergea4a4532000-05-29 16:19:32 +00001294 {
Neil Boothb528a072000-11-12 11:46:21 +00001295 if (ifs->type == T_ELSE)
1296 {
1297 cpp_error (pfile, "#elif after #else");
Neil Booth50410422001-09-15 10:18:03 +00001298 cpp_error_with_line (pfile, ifs->line, 0,
Neil Boothb528a072000-11-12 11:46:21 +00001299 "the conditional began here");
1300 }
1301 ifs->type = T_ELIF;
1302
Neil Boothcef0d192001-07-26 06:02:47 +00001303 /* Only evaluate this if we aren't skipping elses. During
1304 evaluation, set skipping to false to get lexer warnings. */
1305 if (ifs->skip_elses)
1306 pfile->state.skipping = 1;
1307 else
Neil Boothb528a072000-11-12 11:46:21 +00001308 {
Neil Boothcef0d192001-07-26 06:02:47 +00001309 pfile->state.skipping = 0;
1310 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1311 ifs->skip_elses = ! pfile->state.skipping;
Neil Boothb528a072000-11-12 11:46:21 +00001312 }
Neil Boothcef0d192001-07-26 06:02:47 +00001313
1314 /* Invalidate any controlling macro. */
1315 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001316 }
Zack Weinbergea4a4532000-05-29 16:19:32 +00001317}
1318
Neil Boothcef0d192001-07-26 06:02:47 +00001319/* #endif pops the if stack and resets pfile->state.skipping. */
Zack Weinbergea4a4532000-05-29 16:19:32 +00001320
Zack Weinberg711b8822000-07-18 00:59:49 +00001321static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001322do_endif (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001323 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001324{
Neil Boothb528a072000-11-12 11:46:21 +00001325 cpp_buffer *buffer = pfile->buffer;
1326 struct if_stack *ifs = buffer->if_stack;
Per Bothner7f2935c1995-03-16 13:59:07 -08001327
Zack Weinbergea4a4532000-05-29 16:19:32 +00001328 if (ifs == NULL)
1329 cpp_error (pfile, "#endif without #if");
Per Bothner7f2935c1995-03-16 13:59:07 -08001330 else
1331 {
Neil Boothcef0d192001-07-26 06:02:47 +00001332 /* Only check EOL if was not originally skipping. */
1333 if (!ifs->was_skipping)
1334 check_eol (pfile);
1335
Neil Booth93c803682000-10-28 17:59:06 +00001336 /* If potential control macro, we go back outside again. */
1337 if (ifs->next == 0 && ifs->mi_cmacro)
1338 {
Neil Booth6d18adb2001-07-29 17:27:57 +00001339 pfile->mi_valid = true;
Neil Booth93c803682000-10-28 17:59:06 +00001340 pfile->mi_cmacro = ifs->mi_cmacro;
1341 }
1342
Neil Boothb528a072000-11-12 11:46:21 +00001343 buffer->if_stack = ifs->next;
Neil Boothcef0d192001-07-26 06:02:47 +00001344 pfile->state.skipping = ifs->was_skipping;
Neil Booth2a967f32001-05-20 06:26:45 +00001345 obstack_free (&pfile->buffer_ob, ifs);
Per Bothner7f2935c1995-03-16 13:59:07 -08001346 }
Neil Booth93c803682000-10-28 17:59:06 +00001347}
Zack Weinberg041c3192000-07-04 01:58:21 +00001348
Neil Boothcef0d192001-07-26 06:02:47 +00001349/* Push an if_stack entry and set pfile->state.skipping accordingly.
Neil Booth6d18adb2001-07-29 17:27:57 +00001350 If this is a #if or #ifndef, CMACRO is a potentially controlling
1351 macro - we need to check here that we are at the top of the file. */
Zack Weinbergea4a4532000-05-29 16:19:32 +00001352
1353static void
1354push_conditional (pfile, skip, type, cmacro)
1355 cpp_reader *pfile;
1356 int skip;
1357 int type;
1358 const cpp_hashnode *cmacro;
1359{
1360 struct if_stack *ifs;
Neil Boothb528a072000-11-12 11:46:21 +00001361 cpp_buffer *buffer = pfile->buffer;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001362
Neil Booth2a967f32001-05-20 06:26:45 +00001363 ifs = xobnew (&pfile->buffer_ob, struct if_stack);
Neil Booth50410422001-09-15 10:18:03 +00001364 ifs->line = pfile->directive_line;
Neil Boothb528a072000-11-12 11:46:21 +00001365 ifs->next = buffer->if_stack;
Neil Boothcef0d192001-07-26 06:02:47 +00001366 ifs->skip_elses = pfile->state.skipping || !skip;
1367 ifs->was_skipping = pfile->state.skipping;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001368 ifs->type = type;
Neil Booth6d18adb2001-07-29 17:27:57 +00001369 /* This condition is effectively a test for top-of-file. */
1370 if (pfile->mi_valid && pfile->mi_cmacro == 0)
Neil Booth93c803682000-10-28 17:59:06 +00001371 ifs->mi_cmacro = cmacro;
1372 else
1373 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001374
Neil Boothcef0d192001-07-26 06:02:47 +00001375 pfile->state.skipping = skip;
Neil Boothb528a072000-11-12 11:46:21 +00001376 buffer->if_stack = ifs;
Zack Weinberg7061aa51998-12-15 11:09:16 +00001377}
1378
Neil Booth93c803682000-10-28 17:59:06 +00001379/* Read the tokens of the answer into the macro pool. Only commit the
1380 memory if we intend it as permanent storage, i.e. the #assert case.
1381 Returns 0 on success. */
1382
1383static int
1384parse_answer (pfile, answerp, type)
Zack Weinberg041c3192000-07-04 01:58:21 +00001385 cpp_reader *pfile;
1386 struct answer **answerp;
Neil Booth93c803682000-10-28 17:59:06 +00001387 int type;
Zack Weinberg041c3192000-07-04 01:58:21 +00001388{
Neil Booth93c803682000-10-28 17:59:06 +00001389 cpp_token paren, *token;
1390 struct answer *answer;
Zack Weinberg041c3192000-07-04 01:58:21 +00001391
Neil Booth93c803682000-10-28 17:59:06 +00001392 if (POOL_FRONT (&pfile->macro_pool) + sizeof (struct answer) >
1393 POOL_LIMIT (&pfile->macro_pool))
1394 _cpp_next_chunk (&pfile->macro_pool, sizeof (struct answer), 0);
1395 answer = (struct answer *) POOL_FRONT (&pfile->macro_pool);
1396 answer->count = 0;
Zack Weinberg041c3192000-07-04 01:58:21 +00001397
Neil Booth93c803682000-10-28 17:59:06 +00001398 /* In a conditional, it is legal to not have an open paren. We
1399 should save the following token in this case. */
Neil Booth93c803682000-10-28 17:59:06 +00001400 cpp_get_token (pfile, &paren);
Neil Booth93c803682000-10-28 17:59:06 +00001401
1402 /* If not a paren, see if we're OK. */
1403 if (paren.type != CPP_OPEN_PAREN)
Zack Weinberg041c3192000-07-04 01:58:21 +00001404 {
Neil Booth93c803682000-10-28 17:59:06 +00001405 /* In a conditional no answer is a test for any answer. It
1406 could be followed by any token. */
1407 if (type == T_IF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001408 {
1409 _cpp_backup_tokens (pfile, 1);
1410 return 0;
1411 }
Neil Booth93c803682000-10-28 17:59:06 +00001412
1413 /* #unassert with no answer is valid - it removes all answers. */
1414 if (type == T_UNASSERT && paren.type == CPP_EOF)
1415 return 0;
1416
Zack Weinberg041c3192000-07-04 01:58:21 +00001417 cpp_error (pfile, "missing '(' after predicate");
Neil Booth93c803682000-10-28 17:59:06 +00001418 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001419 }
1420
Zack Weinberg041c3192000-07-04 01:58:21 +00001421 for (;;)
1422 {
Neil Booth93c803682000-10-28 17:59:06 +00001423 token = &answer->first[answer->count];
1424 /* Check we have room for the token. */
1425 if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->macro_pool))
1426 {
1427 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_token),
1428 (unsigned char **) &answer);
1429 token = &answer->first[answer->count];
1430 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001431
Neil Booth7f2f1a62000-11-14 18:32:06 +00001432 cpp_get_token (pfile, token);
Neil Booth93c803682000-10-28 17:59:06 +00001433 if (token->type == CPP_CLOSE_PAREN)
1434 break;
Zack Weinberg041c3192000-07-04 01:58:21 +00001435
1436 if (token->type == CPP_EOF)
1437 {
1438 cpp_error (pfile, "missing ')' to complete answer");
Neil Booth93c803682000-10-28 17:59:06 +00001439 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001440 }
Neil Booth93c803682000-10-28 17:59:06 +00001441 answer->count++;
Zack Weinberg041c3192000-07-04 01:58:21 +00001442 }
1443
Neil Booth93c803682000-10-28 17:59:06 +00001444 if (answer->count == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001445 {
1446 cpp_error (pfile, "predicate's answer is empty");
Neil Booth93c803682000-10-28 17:59:06 +00001447 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001448 }
1449
1450 /* Drop whitespace at start. */
Neil Booth93c803682000-10-28 17:59:06 +00001451 answer->first->flags &= ~PREV_WHITE;
1452 *answerp = answer;
Zack Weinberg041c3192000-07-04 01:58:21 +00001453
Neil Booth93c803682000-10-28 17:59:06 +00001454 if (type == T_ASSERT || type == T_UNASSERT)
1455 check_eol (pfile);
1456 return 0;
1457}
1458
1459/* Parses an assertion, returning a pointer to the hash node of the
1460 predicate, or 0 on error. If an answer was supplied, it is placed
Neil Booth7f2f1a62000-11-14 18:32:06 +00001461 in ANSWERP, otherwise it is set to 0. */
Neil Booth93c803682000-10-28 17:59:06 +00001462static cpp_hashnode *
1463parse_assertion (pfile, answerp, type)
1464 cpp_reader *pfile;
1465 struct answer **answerp;
1466 int type;
1467{
1468 cpp_hashnode *result = 0;
1469 cpp_token predicate;
1470
1471 /* We don't expand predicates or answers. */
1472 pfile->state.prevent_expansion++;
1473
Neil Booth93c803682000-10-28 17:59:06 +00001474 *answerp = 0;
Neil Booth7f2f1a62000-11-14 18:32:06 +00001475 cpp_get_token (pfile, &predicate);
Neil Booth93c803682000-10-28 17:59:06 +00001476 if (predicate.type == CPP_EOF)
1477 cpp_error (pfile, "assertion without predicate");
1478 else if (predicate.type != CPP_NAME)
1479 cpp_error (pfile, "predicate must be an identifier");
1480 else if (parse_answer (pfile, answerp, type) == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001481 {
Neil Bootha28c50352001-05-16 22:02:09 +00001482 unsigned int len = NODE_LEN (predicate.val.node);
Neil Booth93c803682000-10-28 17:59:06 +00001483 unsigned char *sym = alloca (len + 1);
1484
1485 /* Prefix '#' to get it out of macro namespace. */
1486 sym[0] = '#';
Neil Bootha28c50352001-05-16 22:02:09 +00001487 memcpy (sym + 1, NODE_NAME (predicate.val.node), len);
Neil Booth93c803682000-10-28 17:59:06 +00001488 result = cpp_lookup (pfile, sym, len + 1);
Zack Weinberg041c3192000-07-04 01:58:21 +00001489 }
1490
Neil Booth93c803682000-10-28 17:59:06 +00001491 pfile->state.prevent_expansion--;
1492 return result;
Zack Weinberg041c3192000-07-04 01:58:21 +00001493}
1494
1495/* Returns a pointer to the pointer to the answer in the answer chain,
1496 or a pointer to NULL if the answer is not in the chain. */
Neil Booth93c803682000-10-28 17:59:06 +00001497static struct answer **
1498find_answer (node, candidate)
Zack Weinberg041c3192000-07-04 01:58:21 +00001499 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00001500 const struct answer *candidate;
Zack Weinberg041c3192000-07-04 01:58:21 +00001501{
Neil Booth93c803682000-10-28 17:59:06 +00001502 unsigned int i;
Zack Weinberg041c3192000-07-04 01:58:21 +00001503 struct answer **result;
1504
1505 for (result = &node->value.answers; *result; result = &(*result)->next)
Neil Booth93c803682000-10-28 17:59:06 +00001506 {
1507 struct answer *answer = *result;
1508
1509 if (answer->count == candidate->count)
1510 {
1511 for (i = 0; i < answer->count; i++)
1512 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1513 break;
1514
1515 if (i == answer->count)
1516 break;
1517 }
1518 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001519
1520 return result;
1521}
1522
Neil Booth93c803682000-10-28 17:59:06 +00001523/* Test an assertion within a preprocessor conditional. Returns
1524 non-zero on failure, zero on success. On success, the result of
1525 the test is written into VALUE. */
1526int
1527_cpp_test_assertion (pfile, value)
1528 cpp_reader *pfile;
1529 int *value;
1530{
1531 struct answer *answer;
1532 cpp_hashnode *node;
1533
1534 node = parse_assertion (pfile, &answer, T_IF);
1535 if (node)
1536 *value = (node->type == NT_ASSERTION &&
1537 (answer == 0 || *find_answer (node, answer) != 0));
1538
1539 /* We don't commit the memory for the answer - it's temporary only. */
1540 return node == 0;
1541}
1542
Zack Weinberg711b8822000-07-18 00:59:49 +00001543static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001544do_assert (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001545 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001546{
Zack Weinberg041c3192000-07-04 01:58:21 +00001547 struct answer *new_answer;
1548 cpp_hashnode *node;
Zack Weinberg15dad1d2000-05-18 15:55:46 +00001549
Neil Booth93c803682000-10-28 17:59:06 +00001550 node = parse_assertion (pfile, &new_answer, T_ASSERT);
Zack Weinberg041c3192000-07-04 01:58:21 +00001551 if (node)
1552 {
Neil Booth93c803682000-10-28 17:59:06 +00001553 /* Place the new answer in the answer list. First check there
1554 is not a duplicate. */
Zack Weinberg041c3192000-07-04 01:58:21 +00001555 new_answer->next = 0;
Neil Booth93c803682000-10-28 17:59:06 +00001556 if (node->type == NT_ASSERTION)
Zack Weinberg041c3192000-07-04 01:58:21 +00001557 {
Neil Booth93c803682000-10-28 17:59:06 +00001558 if (*find_answer (node, new_answer))
1559 {
Neil Bootha28c50352001-05-16 22:02:09 +00001560 cpp_warning (pfile, "\"%s\" re-asserted", NODE_NAME (node) + 1);
Neil Booth93c803682000-10-28 17:59:06 +00001561 return;
1562 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001563 new_answer->next = node->value.answers;
1564 }
Neil Booth93c803682000-10-28 17:59:06 +00001565 node->type = NT_ASSERTION;
Zack Weinberg041c3192000-07-04 01:58:21 +00001566 node->value.answers = new_answer;
Neil Booth93c803682000-10-28 17:59:06 +00001567 POOL_COMMIT (&pfile->macro_pool, (sizeof (struct answer)
1568 + (new_answer->count - 1)
1569 * sizeof (cpp_token)));
Zack Weinberg041c3192000-07-04 01:58:21 +00001570 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001571}
Zack Weinberg7061aa51998-12-15 11:09:16 +00001572
Zack Weinberg711b8822000-07-18 00:59:49 +00001573static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001574do_unassert (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001575 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001576{
Zack Weinberg041c3192000-07-04 01:58:21 +00001577 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00001578 struct answer *answer;
Zack Weinberg041c3192000-07-04 01:58:21 +00001579
Neil Booth93c803682000-10-28 17:59:06 +00001580 node = parse_assertion (pfile, &answer, T_UNASSERT);
1581 /* It isn't an error to #unassert something that isn't asserted. */
1582 if (node && node->type == NT_ASSERTION)
Zack Weinberg7061aa51998-12-15 11:09:16 +00001583 {
Zack Weinberg041c3192000-07-04 01:58:21 +00001584 if (answer)
Neil Booth93c803682000-10-28 17:59:06 +00001585 {
1586 struct answer **p = find_answer (node, answer), *temp;
1587
1588 /* Remove the answer from the list. */
1589 temp = *p;
1590 if (temp)
1591 *p = temp->next;
1592
1593 /* Did we free the last answer? */
1594 if (node->value.answers == 0)
1595 node->type = NT_VOID;
1596 }
1597 else
1598 _cpp_free_definition (node);
Zack Weinberg7061aa51998-12-15 11:09:16 +00001599 }
Neil Booth93c803682000-10-28 17:59:06 +00001600
1601 /* We don't commit the memory for the answer - it's temporary only. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001602}
Per Bothner7f2935c1995-03-16 13:59:07 -08001603
Zack Weinberg45b966d2000-03-13 22:01:08 +00001604/* These are for -D, -U, -A. */
1605
1606/* Process the string STR as if it appeared as the body of a #define.
1607 If STR is just an identifier, define it with value 1.
1608 If STR has anything after the identifier, then it should
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001609 be identifier=definition. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001610
1611void
1612cpp_define (pfile, str)
1613 cpp_reader *pfile;
1614 const char *str;
1615{
1616 char *buf, *p;
1617 size_t count;
1618
Zack Weinberg45b966d2000-03-13 22:01:08 +00001619 /* Copy the entire option so we can modify it.
1620 Change the first "=" in the string to a space. If there is none,
Neil Booth86368122000-10-31 23:34:59 +00001621 tack " 1" on the end. */
1622
1623 /* Length including the null. */
1624 count = strlen (str);
1625 buf = (char *) alloca (count + 2);
1626 memcpy (buf, str, count);
1627
1628 p = strchr (str, '=');
Zack Weinberg45b966d2000-03-13 22:01:08 +00001629 if (p)
Neil Booth86368122000-10-31 23:34:59 +00001630 buf[p - str] = ' ';
Zack Weinberg45b966d2000-03-13 22:01:08 +00001631 else
1632 {
Neil Booth86368122000-10-31 23:34:59 +00001633 buf[count++] = ' ';
1634 buf[count++] = '1';
Zack Weinberg45b966d2000-03-13 22:01:08 +00001635 }
1636
Neil Booth29401c32001-08-22 20:37:20 +00001637 run_directive (pfile, T_DEFINE, buf, count);
Zack Weinberg2c8f0512000-08-29 18:37:37 +00001638}
1639
Neil Boothad2a0842000-12-17 00:13:54 +00001640/* Slight variant of the above for use by initialize_builtins. */
Zack Weinberg2c8f0512000-08-29 18:37:37 +00001641void
1642_cpp_define_builtin (pfile, str)
1643 cpp_reader *pfile;
1644 const char *str;
1645{
Neil Booth29401c32001-08-22 20:37:20 +00001646 run_directive (pfile, T_DEFINE, str, strlen (str));
Zack Weinberg45b966d2000-03-13 22:01:08 +00001647}
1648
1649/* Process MACRO as if it appeared as the body of an #undef. */
1650void
1651cpp_undef (pfile, macro)
1652 cpp_reader *pfile;
1653 const char *macro;
1654{
Neil Booth29401c32001-08-22 20:37:20 +00001655 run_directive (pfile, T_UNDEF, macro, strlen (macro));
Zack Weinberg45b966d2000-03-13 22:01:08 +00001656}
1657
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001658/* Process the string STR as if it appeared as the body of a #assert. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001659void
1660cpp_assert (pfile, str)
1661 cpp_reader *pfile;
1662 const char *str;
1663{
Neil Booth86368122000-10-31 23:34:59 +00001664 handle_assertion (pfile, str, T_ASSERT);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001665}
1666
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001667/* Process STR as if it appeared as the body of an #unassert. */
Zack Weinberg0b22d651999-03-15 18:42:46 +00001668void
1669cpp_unassert (pfile, str)
1670 cpp_reader *pfile;
Neil Booth7ceb3592000-03-11 00:49:44 +00001671 const char *str;
Zack Weinberg0b22d651999-03-15 18:42:46 +00001672{
Neil Booth86368122000-10-31 23:34:59 +00001673 handle_assertion (pfile, str, T_UNASSERT);
Zack Weinberg0b22d651999-03-15 18:42:46 +00001674}
1675
Neil Booth86368122000-10-31 23:34:59 +00001676/* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1677static void
1678handle_assertion (pfile, str, type)
1679 cpp_reader *pfile;
1680 const char *str;
1681 int type;
1682{
1683 size_t count = strlen (str);
1684 const char *p = strchr (str, '=');
1685
1686 if (p)
1687 {
1688 /* Copy the entire option so we can modify it. Change the first
1689 "=" in the string to a '(', and tack a ')' on the end. */
1690 char *buf = (char *) alloca (count + 1);
1691
1692 memcpy (buf, str, count);
1693 buf[p - str] = '(';
1694 buf[count++] = ')';
1695 str = buf;
1696 }
1697
Neil Booth29401c32001-08-22 20:37:20 +00001698 run_directive (pfile, type, str, count);
Neil Booth86368122000-10-31 23:34:59 +00001699}
1700
Neil Booth7e96d762001-01-13 01:00:01 +00001701/* The number of errors for a given reader. */
1702unsigned int
1703cpp_errors (pfile)
1704 cpp_reader *pfile;
1705{
1706 return pfile->errors;
1707}
1708
1709/* The options structure. */
1710cpp_options *
1711cpp_get_options (pfile)
1712 cpp_reader *pfile;
1713{
1714 return &pfile->opts;
1715}
1716
1717/* The callbacks structure. */
1718cpp_callbacks *
1719cpp_get_callbacks (pfile)
1720 cpp_reader *pfile;
1721{
1722 return &pfile->cb;
1723}
1724
Neil Boothd82fc102001-08-02 23:03:31 +00001725/* The line map set. */
Neil Booth47d89cf2001-08-11 07:33:39 +00001726const struct line_maps *
Neil Boothd82fc102001-08-02 23:03:31 +00001727cpp_get_line_maps (pfile)
1728 cpp_reader *pfile;
1729{
1730 return &pfile->line_maps;
1731}
1732
Neil Booth7e96d762001-01-13 01:00:01 +00001733/* Copy the given callbacks structure to our own. */
1734void
1735cpp_set_callbacks (pfile, cb)
1736 cpp_reader *pfile;
1737 cpp_callbacks *cb;
1738{
1739 pfile->cb = *cb;
1740}
1741
Neil Bootheb1f4d92000-12-18 19:00:26 +00001742/* Push a new buffer on the buffer stack. Returns the new buffer; it
1743 doesn't fail. It does not generate a file change call back; that
1744 is the responsibility of the caller. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00001745cpp_buffer *
Neil Booth29401c32001-08-22 20:37:20 +00001746cpp_push_buffer (pfile, buffer, len, from_stage3, return_at_eof)
Zack Weinbergc71f8352000-07-05 05:33:57 +00001747 cpp_reader *pfile;
1748 const U_CHAR *buffer;
Neil Booth3cf35932000-12-05 23:42:43 +00001749 size_t len;
Neil Booth29401c32001-08-22 20:37:20 +00001750 int from_stage3;
Neil Boothef6e9582001-08-04 12:01:59 +00001751 int return_at_eof;
Zack Weinbergc71f8352000-07-05 05:33:57 +00001752{
Neil Booth2a967f32001-05-20 06:26:45 +00001753 cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
Neil Booth93c803682000-10-28 17:59:06 +00001754
Neil Boothfde84342001-08-06 21:07:41 +00001755 /* Clears, amongst other things, if_stack and mi_cmacro. */
1756 memset (new, 0, sizeof (cpp_buffer));
Neil Boothad2a0842000-12-17 00:13:54 +00001757
Neil Boothfde84342001-08-06 21:07:41 +00001758 new->line_base = new->buf = new->cur = buffer;
1759 new->rlimit = buffer + len;
Neil Boothad2a0842000-12-17 00:13:54 +00001760
Neil Boothfde84342001-08-06 21:07:41 +00001761 /* No read ahead or extra char initially. */
1762 new->read_ahead = EOF;
1763 new->extra_char = EOF;
Neil Booth29401c32001-08-22 20:37:20 +00001764 new->from_stage3 = from_stage3;
Neil Booth3cf35932000-12-05 23:42:43 +00001765 new->prev = pfile->buffer;
Neil Boothef6e9582001-08-04 12:01:59 +00001766 new->return_at_eof = return_at_eof;
Neil Boothbdcbe492001-09-13 20:05:17 +00001767 new->saved_flags = BOL;
Neil Booth0bda4762000-12-11 07:45:16 +00001768
Neil Booth3cf35932000-12-05 23:42:43 +00001769 pfile->buffer = new;
Neil Booth0bda4762000-12-11 07:45:16 +00001770
Zack Weinbergc71f8352000-07-05 05:33:57 +00001771 return new;
1772}
1773
Neil Bootheb1f4d92000-12-18 19:00:26 +00001774/* If called from do_line, pops a single buffer. Otherwise pops all
1775 buffers until a real file is reached. Generates appropriate
1776 call-backs. */
Neil Boothef6e9582001-08-04 12:01:59 +00001777void
1778_cpp_pop_buffer (pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00001779 cpp_reader *pfile;
1780{
Neil Boothfde84342001-08-06 21:07:41 +00001781 cpp_buffer *buffer = pfile->buffer;
Neil Boothad2a0842000-12-17 00:13:54 +00001782 struct if_stack *ifs;
Zack Weinbergc71f8352000-07-05 05:33:57 +00001783
Neil Boothfde84342001-08-06 21:07:41 +00001784 /* Walk back up the conditional stack till we reach its level at
1785 entry to this file, issuing error messages. */
1786 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
Neil Booth50410422001-09-15 10:18:03 +00001787 cpp_error_with_line (pfile, ifs->line, 0,
Neil Boothfde84342001-08-06 21:07:41 +00001788 "unterminated #%s", dtable[ifs->type].name);
1789
Neil Booth97293892001-09-14 22:04:46 +00001790 /* In case of a missing #endif. */
Neil Booth67821e32001-08-05 17:31:25 +00001791 pfile->state.skipping = 0;
Neil Booth29401c32001-08-22 20:37:20 +00001792
1793 /* Update the reader's buffer before _cpp_do_file_change. */
1794 pfile->buffer = buffer->prev;
1795
1796 if (buffer->inc)
1797 _cpp_pop_file_buffer (pfile, buffer->inc);
1798
1799 obstack_free (&pfile->buffer_ob, buffer);
Zack Weinbergc71f8352000-07-05 05:33:57 +00001800}
1801
Zack Weinbergc71f8352000-07-05 05:33:57 +00001802void
Neil Booth2a967f32001-05-20 06:26:45 +00001803_cpp_init_directives (pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00001804 cpp_reader *pfile;
1805{
Neil Booth766ee682001-01-29 19:20:12 +00001806 unsigned int i;
Neil Booth93c803682000-10-28 17:59:06 +00001807 cpp_hashnode *node;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001808
Neil Booth93c803682000-10-28 17:59:06 +00001809 /* Register the directives. */
John David Anglin37b85242001-03-02 01:11:50 +00001810 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
Neil Booth93c803682000-10-28 17:59:06 +00001811 {
Neil Booth766ee682001-01-29 19:20:12 +00001812 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
1813 node->directive_index = i + 1;
Neil Booth93c803682000-10-28 17:59:06 +00001814 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00001815}