blob: f5d10e0633acf28f0e630d8862bf7a6cd42425fd [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 Booth4ed5bcf2001-09-24 22:53:12 +000089static const cpp_token *glue_header_name PARAMS ((cpp_reader *));
90static const cpp_token *parse_include PARAMS ((cpp_reader *));
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 Booth4ed5bcf2001-09-24 22:53:12 +0000103static const cpp_token *get_token_no_padding PARAMS ((cpp_reader *));
104static const cpp_token *get__Pragma_string PARAMS ((cpp_reader *));
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000105static unsigned char *destringize PARAMS ((const cpp_string *,
106 unsigned int *));
Neil Booth93c803682000-10-28 17:59:06 +0000107static int parse_answer PARAMS ((cpp_reader *, struct answer **, int));
108static cpp_hashnode *parse_assertion PARAMS ((cpp_reader *, struct answer **,
109 int));
110static struct answer ** find_answer PARAMS ((cpp_hashnode *,
111 const struct answer *));
Neil Booth86368122000-10-31 23:34:59 +0000112static void handle_assertion PARAMS ((cpp_reader *, const char *, int));
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +0000113
Zack Weinberg168d3732000-03-14 06:34:11 +0000114/* This is the table of directive handlers. It is ordered by
115 frequency of occurrence; the numbers at the end are directive
116 counts from all the source code I have lying around (egcs and libc
117 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
Neil Booth93c803682000-10-28 17:59:06 +0000118 pcmcia-cs-3.0.9). This is no longer important as directive lookup
119 is now O(1). All extensions other than #warning and #include_next
120 are deprecated. The name is where the extension appears to have
121 come from. */
Jeff Lawe9a25f71997-11-02 14:19:36 -0700122
Neil Booth93c803682000-10-28 17:59:06 +0000123#define DIRECTIVE_TABLE \
124D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
125D(include, T_INCLUDE, KANDR, INCL) /* 52262 */ \
126D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
127D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
128D(if, T_IF, KANDR, COND | IF_COND) /* 18162 */ \
129D(else, T_ELSE, KANDR, COND) /* 9863 */ \
130D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
131D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
132D(line, T_LINE, KANDR, IN_I) /* 2465 */ \
Neil Boothf000294d2001-01-31 07:56:07 +0000133D(elif, T_ELIF, STDC89, COND) /* 610 */ \
Neil Booth93c803682000-10-28 17:59:06 +0000134D(error, T_ERROR, STDC89, 0) /* 475 */ \
135D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
136D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
137D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL) /* 19 */ \
138D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
139D(import, T_IMPORT, EXTENSION, INCL) /* 0 ObjC */ \
140D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
141D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
142SCCS_ENTRY /* 0 SVR4? */
Per Bothner7f2935c1995-03-16 13:59:07 -0800143
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000144/* #sccs is not always recognized. */
145#ifdef SCCS_DIRECTIVE
Zack Weinberg041c3192000-07-04 01:58:21 +0000146# define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000147#else
148# define SCCS_ENTRY /* nothing */
149#endif
150
Zack Weinberg168d3732000-03-14 06:34:11 +0000151/* Use the table to generate a series of prototypes, an enum for the
152 directive names, and an array of directive handlers. */
153
154/* The directive-processing functions are declared to return int
155 instead of void, because some old compilers have trouble with
156 pointers to functions returning void. */
157
Kazu Hirataec5c56d2001-08-01 17:57:27 +0000158/* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000159#define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
Zack Weinberg168d3732000-03-14 06:34:11 +0000160DIRECTIVE_TABLE
161#undef D
162
Zack Weinberg041c3192000-07-04 01:58:21 +0000163#define D(n, tag, o, f) tag,
Zack Weinberg168d3732000-03-14 06:34:11 +0000164enum
165{
166 DIRECTIVE_TABLE
167 N_DIRECTIVES
Per Bothner7f2935c1995-03-16 13:59:07 -0800168};
Zack Weinberg168d3732000-03-14 06:34:11 +0000169#undef D
170
Kazu Hirataec5c56d2001-08-01 17:57:27 +0000171/* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000172#define D(name, t, origin, flags) \
Zack Weinberg12cf91f2000-05-04 04:38:01 +0000173{ CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
Zack Weinberg041c3192000-07-04 01:58:21 +0000174 sizeof STRINGX(name) - 1, origin, flags },
Neil Booth93c803682000-10-28 17:59:06 +0000175static const directive dtable[] =
Zack Weinberg168d3732000-03-14 06:34:11 +0000176{
177DIRECTIVE_TABLE
178};
179#undef D
180#undef DIRECTIVE_TABLE
Per Bothner7f2935c1995-03-16 13:59:07 -0800181
Neil Boothbdcbe492001-09-13 20:05:17 +0000182#define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
Neil Booth67821e32001-08-05 17:31:25 +0000183
Neil Booth93c803682000-10-28 17:59:06 +0000184/* Skip any remaining tokens in a directive. */
185static void
186skip_rest_of_line (pfile)
187 cpp_reader *pfile;
188{
Neil Booth93c803682000-10-28 17:59:06 +0000189 /* Discard all stacked contexts. */
190 while (pfile->context != &pfile->base_context)
191 _cpp_pop_context (pfile);
192
Neil Boothb528a072000-11-12 11:46:21 +0000193 /* Sweep up all tokens remaining on the line. */
Neil Booth345894b2001-09-16 13:44:29 +0000194 if (! SEEN_EOL ())
195 while (_cpp_lex_token (pfile)->type != CPP_EOF)
196 ;
Neil Booth93c803682000-10-28 17:59:06 +0000197}
198
199/* Ensure there are no stray tokens at the end of a directive. */
200static void
201check_eol (pfile)
202 cpp_reader *pfile;
203{
Neil Booth345894b2001-09-16 13:44:29 +0000204 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
205 cpp_pedwarn (pfile, "extra tokens at end of #%s directive",
206 pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +0000207}
208
Neil Boothfe6c2db2000-11-15 19:25:22 +0000209/* Called when entering a directive, _Pragma or command-line directive. */
210static void
211start_directive (pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000212 cpp_reader *pfile;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000213{
Neil Booth7f2f1a62000-11-14 18:32:06 +0000214 /* Setup in-directive state. */
215 pfile->state.in_directive = 1;
216 pfile->state.save_comments = 0;
217
Neil Booth93c803682000-10-28 17:59:06 +0000218 /* Some handlers need the position of the # for diagnostics. */
Neil Booth8bbbef32001-08-04 16:28:14 +0000219 pfile->directive_line = pfile->line;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000220}
221
222/* Called when leaving a directive, _Pragma or command-line directive. */
223static void
224end_directive (pfile, skip_line)
225 cpp_reader *pfile;
226 int skip_line;
227{
Neil Boothfe6c2db2000-11-15 19:25:22 +0000228 /* We don't skip for an assembler #. */
229 if (skip_line)
Neil Booth67821e32001-08-05 17:31:25 +0000230 {
231 skip_rest_of_line (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +0000232 if (!pfile->keep_tokens)
233 {
234 pfile->cur_run = &pfile->base_run;
235 pfile->cur_token = pfile->base_run.base;
236 }
Neil Booth67821e32001-08-05 17:31:25 +0000237 }
Neil Boothfe6c2db2000-11-15 19:25:22 +0000238
239 /* Restore state. */
Neil Boothfe6c2db2000-11-15 19:25:22 +0000240 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
241 pfile->state.in_directive = 0;
242 pfile->state.angled_headers = 0;
Neil Booth642ce432000-12-07 23:17:56 +0000243 pfile->state.line_extension = 0;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000244 pfile->directive = 0;
245}
246
Neil Booth18a9d8f2001-09-16 11:23:56 +0000247/* Output diagnostics for a directive DIR. INDENTED is non-zero if
248 the '#' was indented. */
249
250static void
251directive_diagnostics (pfile, dir, indented)
252 cpp_reader *pfile;
253 const directive *dir;
254 int indented;
255{
256 if (pfile->state.line_extension)
257 {
258 if (CPP_PEDANTIC (pfile)
259 && ! pfile->state.skipping)
260 cpp_pedwarn (pfile, "style of line directive is a GCC extension");
261 }
262 else
263 {
264 /* Issue -pedantic warnings for extensions. */
265 if (CPP_PEDANTIC (pfile)
266 && ! pfile->state.skipping
267 && dir->origin == EXTENSION)
268 cpp_pedwarn (pfile, "#%s is a GCC extension", dir->name);
269
270 /* Traditionally, a directive is ignored unless its # is in
271 column 1. Therefore in code intended to work with K+R
272 compilers, directives added by C89 must have their #
273 indented, and directives present in traditional C must not.
274 This is true even of directives in skipped conditional
275 blocks. */
276 if (CPP_WTRADITIONAL (pfile))
277 {
278 if (dir == &dtable[T_ELIF])
279 cpp_warning (pfile, "suggest not using #elif in traditional C");
280 else if (indented && dir->origin == KANDR)
281 cpp_warning (pfile,
282 "traditional C ignores #%s with the # indented",
283 dir->name);
284 else if (!indented && dir->origin != KANDR)
285 cpp_warning (pfile,
286 "suggest hiding #%s from traditional C with an indented #",
287 dir->name);
288 }
289 }
290}
291
292/* Check if we have a known directive. INDENTED is non-zero if the
293 '#' of the directive was indented. This function is in this file
294 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
295 non-zero if the line of tokens has been handled, zero if we should
296 continue processing the line. */
297
Neil Boothfe6c2db2000-11-15 19:25:22 +0000298int
299_cpp_handle_directive (pfile, indented)
300 cpp_reader *pfile;
301 int indented;
302{
Neil Boothfe6c2db2000-11-15 19:25:22 +0000303 const directive *dir = 0;
Neil Booth345894b2001-09-16 13:44:29 +0000304 const cpp_token *dname;
Neil Boothfe6c2db2000-11-15 19:25:22 +0000305 int skip = 1;
306
307 start_directive (pfile);
Neil Booth345894b2001-09-16 13:44:29 +0000308 dname = _cpp_lex_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000309
Neil Booth345894b2001-09-16 13:44:29 +0000310 if (dname->type == CPP_NAME)
Neil Booth0d9f2342000-09-18 18:43:05 +0000311 {
Neil Booth345894b2001-09-16 13:44:29 +0000312 if (dname->val.node->directive_index)
313 dir = &dtable[dname->val.node->directive_index - 1];
Neil Booth93c803682000-10-28 17:59:06 +0000314 }
Neil Booth18a9d8f2001-09-16 11:23:56 +0000315 /* We do not recognise the # followed by a number extension in
316 assembler code. */
Neil Booth345894b2001-09-16 13:44:29 +0000317 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth93c803682000-10-28 17:59:06 +0000318 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000319 dir = &dtable[T_LINE];
320 pfile->state.line_extension = 1;
Neil Booth0d9f2342000-09-18 18:43:05 +0000321 }
322
Neil Booth93c803682000-10-28 17:59:06 +0000323 if (dir)
Neil Booth0d9f2342000-09-18 18:43:05 +0000324 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000325 /* If we have a directive that is not an opening conditional,
326 invalidate any control macro. */
327 if (! (dir->flags & IF_COND))
328 pfile->mi_valid = false;
Neil Booth93c803682000-10-28 17:59:06 +0000329
Neil Booth18a9d8f2001-09-16 11:23:56 +0000330 /* Kluge alert. In order to be sure that code like this
331
332 #define HASH #
333 HASH define foo bar
334
335 does not cause '#define foo bar' to get executed when
336 compiled with -save-temps, we recognize directives in
337 -fpreprocessed mode only if the # is in column 1. cppmacro.c
338 puts a space in fron of any '#' at the start of a macro. */
339 if (CPP_OPTION (pfile, preprocessed)
340 && (indented || !(dir->flags & IN_I)))
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000341 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000342 skip = 0;
343 dir = 0;
Zack Weinberg6d4587f2001-05-10 00:07:23 +0000344 }
345 else
Neil Booth93c803682000-10-28 17:59:06 +0000346 {
Neil Booth18a9d8f2001-09-16 11:23:56 +0000347 /* In failed conditional groups, all non-conditional
348 directives are ignored. Before doing that, whether
349 skipping or not, we should lex angle-bracketed headers
350 correctly, and maybe output some diagnostics. */
351 pfile->state.angled_headers = dir->flags & INCL;
352 if (! CPP_OPTION (pfile, preprocessed))
353 directive_diagnostics (pfile, dir, indented);
354 if (pfile->state.skipping && !(dir->flags & COND))
355 dir = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000356 }
357 }
Neil Booth345894b2001-09-16 13:44:29 +0000358 else if (dname->type == CPP_EOF)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000359 ; /* CPP_EOF is the "null directive". */
360 else
Neil Booth0d9f2342000-09-18 18:43:05 +0000361 {
Neil Booth93c803682000-10-28 17:59:06 +0000362 /* An unknown directive. Don't complain about it in assembly
363 source: we don't know where the comments are, and # may
364 introduce assembler pseudo-ops. Don't complain about invalid
365 directives in skipped conditional groups (6.10 p4). */
Neil Boothbdb05a72000-11-26 17:31:13 +0000366 if (CPP_OPTION (pfile, lang) == CLK_ASM)
Neil Booth18a9d8f2001-09-16 11:23:56 +0000367 skip = 0;
368 else if (!pfile->state.skipping)
Neil Booth93c803682000-10-28 17:59:06 +0000369 cpp_error (pfile, "invalid preprocessing directive #%s",
Neil Booth345894b2001-09-16 13:44:29 +0000370 cpp_token_as_text (pfile, dname));
Neil Booth0d9f2342000-09-18 18:43:05 +0000371 }
372
Neil Booth18a9d8f2001-09-16 11:23:56 +0000373 if (dir)
374 {
375 pfile->directive = dir;
376 (*pfile->directive->handler) (pfile);
377 }
378 else if (skip == 0)
379 _cpp_backup_tokens (pfile, 1);
380
381 end_directive (pfile, skip);
Neil Boothfe6c2db2000-11-15 19:25:22 +0000382 return skip;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000383}
384
Neil Booth93c803682000-10-28 17:59:06 +0000385/* Directive handler wrapper used by the command line option
386 processor. */
387static void
Neil Booth29401c32001-08-22 20:37:20 +0000388run_directive (pfile, dir_no, buf, count)
Per Bothner7f2935c1995-03-16 13:59:07 -0800389 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +0000390 int dir_no;
391 const char *buf;
392 size_t count;
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000393{
Neil Booth29401c32001-08-22 20:37:20 +0000394 cpp_push_buffer (pfile, (const U_CHAR *) buf, count,
395 /* from_stage3 */ true, 1);
Neil Booth0bda4762000-12-11 07:45:16 +0000396 start_directive (pfile);
Neil Booth18a9d8f2001-09-16 11:23:56 +0000397 /* We don't want a leading # to be interpreted as a directive. */
398 pfile->buffer->saved_flags = 0;
Neil Boothf71aebb2001-05-27 18:06:00 +0000399 pfile->directive = &dtable[dir_no];
400 (void) (*pfile->directive->handler) (pfile);
Neil Booth0bda4762000-12-11 07:45:16 +0000401 end_directive (pfile, 1);
Neil Boothef6e9582001-08-04 12:01:59 +0000402 _cpp_pop_buffer (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000403}
Per Bothner7f2935c1995-03-16 13:59:07 -0800404
Neil Booth93c803682000-10-28 17:59:06 +0000405/* Checks for validity the macro name in #define, #undef, #ifdef and
406 #ifndef directives. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000407static cpp_hashnode *
Neil Booth93c803682000-10-28 17:59:06 +0000408lex_macro_node (pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000409 cpp_reader *pfile;
410{
Zack Weinbergb8363a22001-07-01 18:48:13 +0000411 cpp_hashnode *node;
Neil Booth345894b2001-09-16 13:44:29 +0000412 const cpp_token *token = _cpp_lex_token (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000413
Zack Weinberg92936ec2000-07-19 20:18:08 +0000414 /* The token immediately after #define must be an identifier. That
Zack Weinbergb8363a22001-07-01 18:48:13 +0000415 identifier may not be "defined", per C99 6.10.8p4.
416 In C++, it may not be any of the "named operators" either,
417 per C++98 [lex.digraph], [lex.key].
418 Finally, the identifier may not have been poisoned. (In that case
419 the lexer has issued the error message for us.) */
Zack Weinberg92936ec2000-07-19 20:18:08 +0000420
Neil Booth345894b2001-09-16 13:44:29 +0000421 if (token->type != CPP_NAME)
Zack Weinberg041c3192000-07-04 01:58:21 +0000422 {
Neil Booth345894b2001-09-16 13:44:29 +0000423 if (token->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +0000424 cpp_error (pfile, "no macro name given in #%s directive",
425 pfile->directive->name);
Neil Booth345894b2001-09-16 13:44:29 +0000426 else if (token->flags & NAMED_OP)
Neil Booth93c803682000-10-28 17:59:06 +0000427 cpp_error (pfile,
Zack Weinbergb8363a22001-07-01 18:48:13 +0000428 "\"%s\" cannot be used as a macro name as it is an operator in C++",
Neil Booth345894b2001-09-16 13:44:29 +0000429 NODE_NAME (token->val.node));
Zack Weinberg92936ec2000-07-19 20:18:08 +0000430 else
Neil Booth93c803682000-10-28 17:59:06 +0000431 cpp_error (pfile, "macro names must be identifiers");
Zack Weinbergb8363a22001-07-01 18:48:13 +0000432
433 return 0;
Zack Weinberg041c3192000-07-04 01:58:21 +0000434 }
Zack Weinbergb8363a22001-07-01 18:48:13 +0000435
Neil Booth345894b2001-09-16 13:44:29 +0000436 node = token->val.node;
Zack Weinbergb8363a22001-07-01 18:48:13 +0000437 if (node->flags & NODE_POISONED)
438 return 0;
439
440 if (node == pfile->spec_nodes.n_defined)
Zack Weinbergba89d662000-08-04 01:30:06 +0000441 {
Zack Weinbergb8363a22001-07-01 18:48:13 +0000442 cpp_error (pfile, "\"%s\" cannot be used as a macro name",
443 NODE_NAME (node));
444 return 0;
Zack Weinbergba89d662000-08-04 01:30:06 +0000445 }
446
Zack Weinbergb8363a22001-07-01 18:48:13 +0000447 return node;
Per Bothner7f2935c1995-03-16 13:59:07 -0800448}
Per Bothner7f2935c1995-03-16 13:59:07 -0800449
Neil Booth93c803682000-10-28 17:59:06 +0000450/* Process a #define directive. Most work is done in cppmacro.c. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000451static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000452do_define (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800453 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800454{
Neil Booth93c803682000-10-28 17:59:06 +0000455 cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000456
Neil Booth93c803682000-10-28 17:59:06 +0000457 if (node)
458 {
Neil Booth93c803682000-10-28 17:59:06 +0000459 if (_cpp_create_definition (pfile, node))
460 if (pfile->cb.define)
Neil Booth8bbbef32001-08-04 16:28:14 +0000461 (*pfile->cb.define) (pfile, pfile->directive_line, node);
Neil Booth93c803682000-10-28 17:59:06 +0000462 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800463}
464
Neil Booth93c803682000-10-28 17:59:06 +0000465/* Handle #undef. Marks the identifier NT_VOID in the hash table. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000466static void
Zack Weinberg041c3192000-07-04 01:58:21 +0000467do_undef (pfile)
468 cpp_reader *pfile;
469{
Neil Booth93c803682000-10-28 17:59:06 +0000470 cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000471
472 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
473 is not currently defined as a macro name. */
Neil Booth93c803682000-10-28 17:59:06 +0000474 if (node && node->type == NT_MACRO)
Zack Weinberg041c3192000-07-04 01:58:21 +0000475 {
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000476 if (pfile->cb.undef)
Neil Booth8bbbef32001-08-04 16:28:14 +0000477 (*pfile->cb.undef) (pfile, pfile->directive_line, node);
Zack Weinberg041c3192000-07-04 01:58:21 +0000478
Neil Booth618cdda2001-02-25 09:43:03 +0000479 if (node->flags & NODE_WARN)
Neil Bootha28c50352001-05-16 22:02:09 +0000480 cpp_warning (pfile, "undefining \"%s\"", NODE_NAME (node));
Zack Weinberg041c3192000-07-04 01:58:21 +0000481
482 _cpp_free_definition (node);
Zack Weinberg041c3192000-07-04 01:58:21 +0000483 }
Neil Booth93c803682000-10-28 17:59:06 +0000484 check_eol (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000485}
486
Neil Booth93c803682000-10-28 17:59:06 +0000487/* Helper routine used by parse_include. Reinterpret the current line
488 as an h-char-sequence (< ... >); we are looking at the first token
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000489 after the <. Returns the header as a token, or NULL on failure. */
490static const cpp_token *
491glue_header_name (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800492 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800493{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000494 cpp_token *header = NULL;
495 const cpp_token *token;
Neil Boothece54d52001-09-28 09:40:22 +0000496 unsigned char *dest;
497 size_t len;
Per Bothner7f2935c1995-03-16 13:59:07 -0800498
Neil Booth93c803682000-10-28 17:59:06 +0000499 /* To avoid lexed tokens overwriting our glued name, we can only
500 allocate from the string pool once we've lexed everything. */
501
Neil Boothece54d52001-09-28 09:40:22 +0000502 dest = BUFF_FRONT (pfile->u_buff);
Neil Booth93c803682000-10-28 17:59:06 +0000503 for (;;)
Zack Weinberg168d3732000-03-14 06:34:11 +0000504 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000505 token = cpp_get_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000506
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000507 if (token->type == CPP_GREATER || token->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +0000508 break;
509
Neil Boothece54d52001-09-28 09:40:22 +0000510 /* + 1 for terminating NUL. */
511 len = cpp_token_len (token) + 1;
512 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
Neil Booth93c803682000-10-28 17:59:06 +0000513 {
Neil Boothece54d52001-09-28 09:40:22 +0000514 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
Neil Booth8c3b2692001-09-30 10:03:11 +0000515 _cpp_extend_buff (pfile, &pfile->u_buff, len);
Neil Boothece54d52001-09-28 09:40:22 +0000516 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
Neil Booth93c803682000-10-28 17:59:06 +0000517 }
518
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000519 if (token->flags & PREV_WHITE)
Neil Boothece54d52001-09-28 09:40:22 +0000520 *dest++ = ' ';
Neil Booth93c803682000-10-28 17:59:06 +0000521
Neil Boothece54d52001-09-28 09:40:22 +0000522 dest = cpp_spell_token (pfile, token, dest);
Neil Booth93c803682000-10-28 17:59:06 +0000523 }
524
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000525 if (token->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +0000526 cpp_error (pfile, "missing terminating > character");
527 else
528 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000529 header = _cpp_temp_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000530 header->type = CPP_HEADER_NAME;
Neil Boothece54d52001-09-28 09:40:22 +0000531 header->flags = 0;
532 header->val.str.len = dest - BUFF_FRONT (pfile->u_buff);
533 header->val.str.text = BUFF_FRONT (pfile->u_buff);
534 *dest++ = '\0';
535 BUFF_FRONT (pfile->u_buff) = dest;
Neil Booth93c803682000-10-28 17:59:06 +0000536 }
537
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000538 return header;
Neil Booth93c803682000-10-28 17:59:06 +0000539}
540
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000541/* Returns the header string of #include, #include_next, #import and
542 #pragma dependency. Returns NULL on error. */
543static const cpp_token *
544parse_include (pfile)
Neil Booth93c803682000-10-28 17:59:06 +0000545 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +0000546{
Neil Booth93c803682000-10-28 17:59:06 +0000547 const unsigned char *dir;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000548 const cpp_token *header;
Neil Booth93c803682000-10-28 17:59:06 +0000549
Neil Booth09b82252001-07-29 22:27:20 +0000550 if (pfile->directive == &dtable[T_PRAGMA])
Neil Booth93c803682000-10-28 17:59:06 +0000551 dir = U"pragma dependency";
552 else
553 dir = pfile->directive->name;
554
555 /* Allow macro expansion. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000556 header = cpp_get_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000557 if (header->type != CPP_STRING && header->type != CPP_HEADER_NAME)
558 {
559 if (header->type != CPP_LESS)
Zack Weinberg041c3192000-07-04 01:58:21 +0000560 {
561 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir);
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000562 return NULL;
Zack Weinberg041c3192000-07-04 01:58:21 +0000563 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000564
565 header = glue_header_name (pfile);
566 if (header == NULL)
567 return header;
Zack Weinberg041c3192000-07-04 01:58:21 +0000568 }
Neil Booth93c803682000-10-28 17:59:06 +0000569
570 if (header->val.str.len == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +0000571 {
572 cpp_error (pfile, "empty file name in #%s", dir);
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000573 return NULL;
Richard Kennercfb3ee11997-04-13 14:30:13 -0400574 }
575
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000576 return header;
Zack Weinberg168d3732000-03-14 06:34:11 +0000577}
578
Neil Boothba133c92001-03-15 07:57:13 +0000579/* Handle #include, #include_next and #import. */
Zack Weinberg711b8822000-07-18 00:59:49 +0000580static void
Neil Boothba133c92001-03-15 07:57:13 +0000581do_include_common (pfile, type)
Zack Weinberg168d3732000-03-14 06:34:11 +0000582 cpp_reader *pfile;
Neil Boothba133c92001-03-15 07:57:13 +0000583 enum include_type type;
Zack Weinberg168d3732000-03-14 06:34:11 +0000584{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000585 const cpp_token *header;
Zack Weinberg168d3732000-03-14 06:34:11 +0000586
Neil Booth09b82252001-07-29 22:27:20 +0000587 /* For #include_next, if this is the primary source file, warn and
588 use the normal search logic. */
589 if (type == IT_INCLUDE_NEXT && ! pfile->buffer->prev)
590 {
591 cpp_warning (pfile, "#include_next in primary source file");
592 type = IT_INCLUDE;
593 }
594 else if (type == IT_IMPORT && CPP_OPTION (pfile, warn_import))
595 {
596 CPP_OPTION (pfile, warn_import) = 0;
597 cpp_warning (pfile,
598 "#import is obsolete, use an #ifndef wrapper in the header file");
599 }
600
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000601 header = parse_include (pfile);
602 if (header)
Neil Boothba133c92001-03-15 07:57:13 +0000603 {
604 /* Prevent #include recursion. */
Neil Boothd8693c62001-08-21 23:05:12 +0000605 if (pfile->line_maps.depth >= CPP_STACK_MAX)
Neil Boothba133c92001-03-15 07:57:13 +0000606 cpp_fatal (pfile, "#include nested too deeply");
Neil Boothba133c92001-03-15 07:57:13 +0000607 else
608 {
Neil Booth09b82252001-07-29 22:27:20 +0000609 check_eol (pfile);
610 /* Get out of macro context, if we are. */
Neil Boothbdcbe492001-09-13 20:05:17 +0000611 skip_rest_of_line (pfile);
Neil Booth09b82252001-07-29 22:27:20 +0000612 if (pfile->cb.include)
Neil Booth8bbbef32001-08-04 16:28:14 +0000613 (*pfile->cb.include) (pfile, pfile->directive_line,
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000614 pfile->directive->name, header);
Neil Boothba133c92001-03-15 07:57:13 +0000615
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000616 _cpp_execute_include (pfile, header, type);
Neil Boothba133c92001-03-15 07:57:13 +0000617 }
618 }
619}
620
621static void
622do_include (pfile)
623 cpp_reader *pfile;
624{
625 do_include_common (pfile, IT_INCLUDE);
Zack Weinberg168d3732000-03-14 06:34:11 +0000626}
627
Zack Weinberg711b8822000-07-18 00:59:49 +0000628static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000629do_import (pfile)
630 cpp_reader *pfile;
631{
Neil Boothba133c92001-03-15 07:57:13 +0000632 do_include_common (pfile, IT_IMPORT);
Zack Weinberg168d3732000-03-14 06:34:11 +0000633}
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000634
Zack Weinberg711b8822000-07-18 00:59:49 +0000635static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000636do_include_next (pfile)
637 cpp_reader *pfile;
638{
Neil Boothba133c92001-03-15 07:57:13 +0000639 do_include_common (pfile, IT_INCLUDE_NEXT);
Per Bothner7f2935c1995-03-16 13:59:07 -0800640}
641
Neil Booth28e0f042000-12-09 12:06:37 +0000642/* Subroutine of do_line. Read possible flags after file name. LAST
643 is the last flag seen; 0 if this is the first flag. Return the flag
644 if it is valid, 0 at the end of the directive. Otherwise complain. */
Jason Merrilld3a34a01999-08-14 00:42:07 +0000645
Neil Booth642ce432000-12-07 23:17:56 +0000646static unsigned int
Neil Booth28e0f042000-12-09 12:06:37 +0000647read_flag (pfile, last)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000648 cpp_reader *pfile;
Neil Booth28e0f042000-12-09 12:06:37 +0000649 unsigned int last;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000650{
Neil Booth345894b2001-09-16 13:44:29 +0000651 const cpp_token *token = _cpp_lex_token (pfile);
Jason Merrilld3a34a01999-08-14 00:42:07 +0000652
Neil Booth345894b2001-09-16 13:44:29 +0000653 if (token->type == CPP_NUMBER && token->val.str.len == 1)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000654 {
Neil Booth345894b2001-09-16 13:44:29 +0000655 unsigned int flag = token->val.str.text[0] - '0';
Neil Booth28e0f042000-12-09 12:06:37 +0000656
657 if (flag > last && flag <= 4
658 && (flag != 4 || last == 3)
659 && (flag != 2 || last == 0))
660 return flag;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000661 }
Neil Booth93c803682000-10-28 17:59:06 +0000662
Neil Booth345894b2001-09-16 13:44:29 +0000663 if (token->type != CPP_EOF)
Neil Booth642ce432000-12-07 23:17:56 +0000664 cpp_error (pfile, "invalid flag \"%s\" in line directive",
Neil Booth345894b2001-09-16 13:44:29 +0000665 cpp_token_as_text (pfile, token));
Neil Booth93c803682000-10-28 17:59:06 +0000666 return 0;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000667}
668
Zack Weinberg041c3192000-07-04 01:58:21 +0000669/* Another subroutine of do_line. Convert a number in STR, of length
670 LEN, to binary; store it in NUMP, and return 0 if the number was
Zack Weinberg5ef865d2000-08-02 07:08:49 +0000671 well-formed, 1 if not. Temporary, hopefully. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000672static int
673strtoul_for_line (str, len, nump)
674 const U_CHAR *str;
675 unsigned int len;
676 unsigned long *nump;
677{
678 unsigned long reg = 0;
679 U_CHAR c;
680 while (len--)
681 {
682 c = *str++;
683 if (!ISDIGIT (c))
684 return 1;
685 reg *= 10;
686 reg += c - '0';
687 }
688 *nump = reg;
689 return 0;
690}
691
Zack Weinberg5538ada1999-02-04 06:36:54 -0500692/* Interpret #line command.
693 Note that the filename string (if any) is treated as if it were an
694 include filename. That means no escape handling. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800695
Zack Weinberg711b8822000-07-18 00:59:49 +0000696static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000697do_line (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800698 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800699{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000700 const cpp_token *token;
Neil Boothbb74c962001-08-17 22:23:49 +0000701 const char *new_file = pfile->map->to_file;
702 unsigned long new_lineno;
703 unsigned int cap, new_sysp = pfile->map->sysp;
704 enum lc_reason reason = LC_RENAME;
Per Bothner7f2935c1995-03-16 13:59:07 -0800705
Neil Booth27e25642000-11-27 08:00:04 +0000706 /* C99 raised the minimum limit on #line numbers. */
707 cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
708
Neil Booth18a9d8f2001-09-16 11:23:56 +0000709 /* Putting this in _cpp_handle_directive risks two calls to
710 _cpp_backup_tokens in some circumstances, which can segfault. */
711 if (pfile->state.line_extension)
712 _cpp_backup_tokens (pfile, 1);
713
Neil Booth93c803682000-10-28 17:59:06 +0000714 /* #line commands expand macros. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000715 token = cpp_get_token (pfile);
716 if (token->type != CPP_NUMBER
717 || strtoul_for_line (token->val.str.text, token->val.str.len,
718 &new_lineno))
Per Bothner7f2935c1995-03-16 13:59:07 -0800719 {
Neil Booth93c803682000-10-28 17:59:06 +0000720 cpp_error (pfile, "\"%s\" after #line is not a positive integer",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000721 cpp_token_as_text (pfile, token));
Zack Weinberg9ec72912000-08-09 19:41:12 +0000722 return;
Zack Weinberg5538ada1999-02-04 06:36:54 -0500723 }
Zack Weinberg5538ada1999-02-04 06:36:54 -0500724
Neil Boothd82fc102001-08-02 23:03:31 +0000725 if (CPP_PEDANTIC (pfile) && ! pfile->state.line_extension
726 && (new_lineno == 0 || new_lineno > cap))
Zack Weinberg041c3192000-07-04 01:58:21 +0000727 cpp_pedwarn (pfile, "line number out of range");
Zack Weinberg5538ada1999-02-04 06:36:54 -0500728
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000729 token = cpp_get_token (pfile);
730 if (token->type == CPP_STRING)
Zack Weinberg5538ada1999-02-04 06:36:54 -0500731 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000732 new_file = (const char *) token->val.str.text;
Neil Booth93c803682000-10-28 17:59:06 +0000733
Neil Boothd6d52dd2001-01-13 18:39:26 +0000734 /* Only accept flags for the # 55 form. */
Neil Boothfde84342001-08-06 21:07:41 +0000735 if (pfile->state.line_extension)
Neil Booth93c803682000-10-28 17:59:06 +0000736 {
Neil Booth47d89cf2001-08-11 07:33:39 +0000737 int flag;
Neil Booth93c803682000-10-28 17:59:06 +0000738
Neil Boothbb74c962001-08-17 22:23:49 +0000739 new_sysp = 0;
Neil Booth47d89cf2001-08-11 07:33:39 +0000740 flag = read_flag (pfile, 0);
Neil Booth642ce432000-12-07 23:17:56 +0000741 if (flag == 1)
Neil Booth93c803682000-10-28 17:59:06 +0000742 {
Neil Boothd82fc102001-08-02 23:03:31 +0000743 reason = LC_ENTER;
Neil Boothfde84342001-08-06 21:07:41 +0000744 /* Fake an include for cpp_included (). */
Neil Boothbb74c962001-08-17 22:23:49 +0000745 _cpp_fake_include (pfile, new_file);
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 else if (flag == 2)
Neil Booth93c803682000-10-28 17:59:06 +0000749 {
Neil Boothd82fc102001-08-02 23:03:31 +0000750 reason = LC_LEAVE;
Neil Booth28e0f042000-12-09 12:06:37 +0000751 flag = read_flag (pfile, flag);
Neil Booth93c803682000-10-28 17:59:06 +0000752 }
Neil Booth642ce432000-12-07 23:17:56 +0000753 if (flag == 3)
Neil Booth93c803682000-10-28 17:59:06 +0000754 {
Neil Boothbb74c962001-08-17 22:23:49 +0000755 new_sysp = 1;
Neil Booth28e0f042000-12-09 12:06:37 +0000756 flag = read_flag (pfile, flag);
757 if (flag == 4)
Neil Boothbb74c962001-08-17 22:23:49 +0000758 new_sysp = 2;
Neil Boothad2a0842000-12-17 00:13:54 +0000759 }
Neil Booth93c803682000-10-28 17:59:06 +0000760 }
Neil Boothfde84342001-08-06 21:07:41 +0000761 check_eol (pfile);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000762 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000763 else if (token->type != CPP_EOF)
Neil Booth27e25642000-11-27 08:00:04 +0000764 {
765 cpp_error (pfile, "\"%s\" is not a valid filename",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000766 cpp_token_as_text (pfile, token));
Neil Booth27e25642000-11-27 08:00:04 +0000767 return;
768 }
Zack Weinberg941e09b1998-12-15 11:17:06 +0000769
Neil Boothbdcbe492001-09-13 20:05:17 +0000770 skip_rest_of_line (pfile);
Neil Boothbb74c962001-08-17 22:23:49 +0000771 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
Neil Booth27e25642000-11-27 08:00:04 +0000772}
773
Neil Booth67821e32001-08-05 17:31:25 +0000774/* Arrange the file_change callback. pfile->line has changed to
Neil Booth47d89cf2001-08-11 07:33:39 +0000775 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
776 header, 2 for a sytem header that needs to be extern "C" protected,
777 and zero otherwise. */
Neil Bootheb1f4d92000-12-18 19:00:26 +0000778void
Neil Booth47d89cf2001-08-11 07:33:39 +0000779_cpp_do_file_change (pfile, reason, to_file, file_line, sysp)
Neil Booth27e25642000-11-27 08:00:04 +0000780 cpp_reader *pfile;
Neil Boothd82fc102001-08-02 23:03:31 +0000781 enum lc_reason reason;
Neil Booth47d89cf2001-08-11 07:33:39 +0000782 const char *to_file;
Neil Booth67821e32001-08-05 17:31:25 +0000783 unsigned int file_line;
Neil Booth47d89cf2001-08-11 07:33:39 +0000784 unsigned int sysp;
Neil Booth27e25642000-11-27 08:00:04 +0000785{
Neil Booth47d89cf2001-08-11 07:33:39 +0000786 pfile->map = add_line_map (&pfile->line_maps, reason, sysp,
787 pfile->line, to_file, file_line);
Neil Boothd82fc102001-08-02 23:03:31 +0000788
Neil Bootheb1f4d92000-12-18 19:00:26 +0000789 if (pfile->cb.file_change)
Neil Booth47d89cf2001-08-11 07:33:39 +0000790 (*pfile->cb.file_change) (pfile, pfile->map);
Per Bothner7f2935c1995-03-16 13:59:07 -0800791}
Zack Weinberg941e09b1998-12-15 11:17:06 +0000792
Per Bothner7f2935c1995-03-16 13:59:07 -0800793/*
Neil Booth838f3132000-09-24 10:42:09 +0000794 * Report a warning or error detected by the program we are
795 * processing. Use the directive's tokens in the error message.
Per Bothner7f2935c1995-03-16 13:59:07 -0800796 */
797
Zack Weinberg711b8822000-07-18 00:59:49 +0000798static void
Neil Booth29b10742000-11-13 18:40:37 +0000799do_diagnostic (pfile, code, print_dir)
Per Bothner7f2935c1995-03-16 13:59:07 -0800800 cpp_reader *pfile;
Neil Booth838f3132000-09-24 10:42:09 +0000801 enum error_type code;
Neil Booth29b10742000-11-13 18:40:37 +0000802 int print_dir;
Per Bothner7f2935c1995-03-16 13:59:07 -0800803{
Neil Booth97293892001-09-14 22:04:46 +0000804 if (_cpp_begin_message (pfile, code, 0, 0))
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000805 {
Neil Booth29b10742000-11-13 18:40:37 +0000806 if (print_dir)
807 fprintf (stderr, "#%s ", pfile->directive->name);
Neil Booth93c803682000-10-28 17:59:06 +0000808 pfile->state.prevent_expansion++;
809 cpp_output_line (pfile, stderr);
810 pfile->state.prevent_expansion--;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000811 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800812}
813
Neil Booth838f3132000-09-24 10:42:09 +0000814static void
815do_error (pfile)
816 cpp_reader *pfile;
817{
Neil Booth29b10742000-11-13 18:40:37 +0000818 do_diagnostic (pfile, ERROR, 1);
Neil Booth838f3132000-09-24 10:42:09 +0000819}
Per Bothner7f2935c1995-03-16 13:59:07 -0800820
Zack Weinberg711b8822000-07-18 00:59:49 +0000821static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000822do_warning (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800823 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800824{
Neil Booth2f878972001-04-08 10:01:18 +0000825 /* We want #warning diagnostics to be emitted in system headers too. */
826 do_diagnostic (pfile, WARNING_SYSHDR, 1);
Per Bothner7f2935c1995-03-16 13:59:07 -0800827}
828
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000829/* Report program identification. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800830
Zack Weinberg711b8822000-07-18 00:59:49 +0000831static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000832do_ident (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800833 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800834{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000835 const cpp_token *str = cpp_get_token (pfile);
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000836
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000837 if (str->type != CPP_STRING)
838 cpp_error (pfile, "invalid #ident directive");
Neil Booth93c803682000-10-28 17:59:06 +0000839 else if (pfile->cb.ident)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000840 (*pfile->cb.ident) (pfile, pfile->directive_line, &str->val.str);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000841
Neil Booth93c803682000-10-28 17:59:06 +0000842 check_eol (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -0800843}
844
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000845/* Pragmata handling. We handle some of these, and pass the rest on
846 to the front end. C99 defines three pragmas and says that no macro
847 expansion is to be performed on them; whether or not macro
848 expansion happens for other pragmas is implementation defined.
Neil Bootha9499412000-11-09 21:18:15 +0000849 This implementation never macro-expands the text after #pragma. */
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000850
851/* Sub-handlers for the pragmas needing treatment here.
Kazu Hirataec5c56d2001-08-01 17:57:27 +0000852 They return 1 if the token buffer is to be popped, 0 if not. */
Neil Boothd82fc102001-08-02 23:03:31 +0000853typedef void (*pragma_cb) PARAMS ((cpp_reader *));
Nathan Sidwell82443372000-06-23 10:56:09 +0000854struct pragma_entry
855{
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000856 struct pragma_entry *next;
Zack Weinberg041c3192000-07-04 01:58:21 +0000857 const char *name;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000858 size_t len;
859 int isnspace;
860 union {
Neil Boothd82fc102001-08-02 23:03:31 +0000861 pragma_cb handler;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000862 struct pragma_entry *space;
863 } u;
Nathan Sidwell82443372000-06-23 10:56:09 +0000864};
865
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000866void
867cpp_register_pragma (pfile, space, name, handler)
Nathan Sidwell82443372000-06-23 10:56:09 +0000868 cpp_reader *pfile;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000869 const char *space;
870 const char *name;
Neil Boothd82fc102001-08-02 23:03:31 +0000871 pragma_cb handler;
Nathan Sidwell82443372000-06-23 10:56:09 +0000872{
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000873 struct pragma_entry **x, *new;
874 size_t len;
875
876 x = &pfile->pragmas;
877 if (space)
878 {
879 struct pragma_entry *p = pfile->pragmas;
880 len = strlen (space);
881 while (p)
882 {
883 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
884 {
885 x = &p->u.space;
886 goto found;
887 }
888 p = p->next;
889 }
890 cpp_ice (pfile, "unknown #pragma namespace %s", space);
891 return;
892 }
893
894 found:
Neil Boothbef985f2001-08-11 12:37:19 +0000895 new = (struct pragma_entry *)
Neil Booth8c3b2692001-09-30 10:03:11 +0000896 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000897 new->name = name;
898 new->len = strlen (name);
899 new->isnspace = 0;
900 new->u.handler = handler;
901
902 new->next = *x;
903 *x = new;
904}
905
906void
907cpp_register_pragma_space (pfile, space)
908 cpp_reader *pfile;
909 const char *space;
910{
911 struct pragma_entry *new;
912 const struct pragma_entry *p = pfile->pragmas;
913 size_t len = strlen (space);
914
915 while (p)
916 {
917 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
Zack Weinberg6e19bb32000-08-18 22:42:14 +0000918 /* Multiple different callers are allowed to register the same
919 namespace. */
920 return;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000921 p = p->next;
922 }
923
Neil Boothbef985f2001-08-11 12:37:19 +0000924 new = (struct pragma_entry *)
Neil Booth8c3b2692001-09-30 10:03:11 +0000925 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000926 new->name = space;
927 new->len = len;
928 new->isnspace = 1;
929 new->u.space = 0;
930
931 new->next = pfile->pragmas;
932 pfile->pragmas = new;
933}
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000934
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000935void
936_cpp_init_internal_pragmas (pfile)
937 cpp_reader *pfile;
938{
939 /* top level */
940 cpp_register_pragma (pfile, 0, "poison", do_pragma_poison);
941 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
942
943 /* GCC namespace */
944 cpp_register_pragma_space (pfile, "GCC");
945
946 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
947 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
948 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
Nathan Sidwell82443372000-06-23 10:56:09 +0000949}
Per Bothner7f2935c1995-03-16 13:59:07 -0800950
Zack Weinberg711b8822000-07-18 00:59:49 +0000951static void
Zack Weinberg168d3732000-03-14 06:34:11 +0000952do_pragma (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800953 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800954{
Neil Boothd82fc102001-08-02 23:03:31 +0000955 pragma_cb handler = NULL;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000956 const struct pragma_entry *p;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000957 const cpp_token *token;
Neil Boothbdcbe492001-09-13 20:05:17 +0000958 unsigned int count = 0;
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000959
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000960 p = pfile->pragmas;
Neil Booth93c803682000-10-28 17:59:06 +0000961 pfile->state.prevent_expansion++;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000962
963 new_space:
Neil Boothbdcbe492001-09-13 20:05:17 +0000964 count++;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000965 token = cpp_get_token (pfile);
966 if (token->type == CPP_NAME)
Alexandre Oliva0172e2b2000-02-27 06:24:27 +0000967 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000968 const cpp_hashnode *node = token->val.node;
Neil Bootha28c50352001-05-16 22:02:09 +0000969 size_t len = NODE_LEN (node);
970
Neil Booth93c803682000-10-28 17:59:06 +0000971 while (p)
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000972 {
Neil Booth2a967f32001-05-20 06:26:45 +0000973 if (strlen (p->name) == len
974 && !memcmp (p->name, NODE_NAME (node), len))
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000975 {
Neil Booth93c803682000-10-28 17:59:06 +0000976 if (p->isnspace)
977 {
978 p = p->u.space;
979 goto new_space;
980 }
981 else
982 {
Neil Boothd82fc102001-08-02 23:03:31 +0000983 handler = p->u.handler;
Neil Booth93c803682000-10-28 17:59:06 +0000984 break;
985 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000986 }
Neil Booth93c803682000-10-28 17:59:06 +0000987 p = p->next;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000988 }
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000989 }
990
Neil Booth97293892001-09-14 22:04:46 +0000991 /* FIXME. This is an awful kludge to get the front ends to update
992 their notion of line number for diagnostic purposes. The line
993 number should be passed to the handler and they should do it
994 themselves. Stand-alone CPP must ignore us, otherwise it will
995 prefix the directive with spaces, hence the 1. Ugh. */
996 if (pfile->cb.line_change)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000997 (*pfile->cb.line_change)(pfile, token, 1);
Neil Booth97293892001-09-14 22:04:46 +0000998
Neil Boothd82fc102001-08-02 23:03:31 +0000999 if (handler)
1000 (*handler) (pfile);
1001 else if (pfile->cb.def_pragma)
Neil Boothbdcbe492001-09-13 20:05:17 +00001002 {
1003 _cpp_backup_tokens (pfile, count);
1004 (*pfile->cb.def_pragma) (pfile, pfile->directive_line);
1005 }
Neil Booth97293892001-09-14 22:04:46 +00001006 pfile->state.prevent_expansion--;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001007}
1008
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001009static void
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001010do_pragma_once (pfile)
1011 cpp_reader *pfile;
1012{
Branko Cibej317639a2000-09-26 00:54:04 +02001013 cpp_warning (pfile, "#pragma once is obsolete");
1014
Neil Booth642ce432000-12-07 23:17:56 +00001015 if (pfile->buffer->prev == NULL)
Neil Booth93c803682000-10-28 17:59:06 +00001016 cpp_warning (pfile, "#pragma once in main file");
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001017 else
Neil Booth642ce432000-12-07 23:17:56 +00001018 _cpp_never_reread (pfile->buffer->inc);
Neil Booth93c803682000-10-28 17:59:06 +00001019
1020 check_eol (pfile);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001021}
1022
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001023static void
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001024do_pragma_poison (pfile)
1025 cpp_reader *pfile;
1026{
1027 /* Poison these symbols so that all subsequent usage produces an
1028 error message. */
Neil Booth345894b2001-09-16 13:44:29 +00001029 const cpp_token *tok;
Zack Weinbergf8f769e2000-05-28 05:56:38 +00001030 cpp_hashnode *hp;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001031
Neil Booth93c803682000-10-28 17:59:06 +00001032 pfile->state.poisoned_ok = 1;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001033 for (;;)
1034 {
Neil Booth345894b2001-09-16 13:44:29 +00001035 tok = _cpp_lex_token (pfile);
1036 if (tok->type == CPP_EOF)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001037 break;
Neil Booth345894b2001-09-16 13:44:29 +00001038 if (tok->type != CPP_NAME)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001039 {
Neil Booth93c803682000-10-28 17:59:06 +00001040 cpp_error (pfile, "invalid #pragma GCC poison directive");
1041 break;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001042 }
1043
Neil Booth345894b2001-09-16 13:44:29 +00001044 hp = tok->val.node;
Neil Booth93c803682000-10-28 17:59:06 +00001045 if (hp->flags & NODE_POISONED)
1046 continue;
1047
1048 if (hp->type == NT_MACRO)
Neil Bootha28c50352001-05-16 22:02:09 +00001049 cpp_warning (pfile, "poisoning existing macro \"%s\"", NODE_NAME (hp));
Neil Booth93c803682000-10-28 17:59:06 +00001050 _cpp_free_definition (hp);
1051 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001052 }
Neil Booth93c803682000-10-28 17:59:06 +00001053 pfile->state.poisoned_ok = 0;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001054}
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001055
1056/* Mark the current header as a system header. This will suppress
1057 some categories of warnings (notably those from -pedantic). It is
1058 intended for use in system libraries that cannot be implemented in
1059 conforming C, but cannot be certain that their headers appear in a
1060 system include directory. To prevent abuse, it is rejected in the
1061 primary source file. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001062static void
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001063do_pragma_system_header (pfile)
1064 cpp_reader *pfile;
1065{
Neil Booth614c7d32000-12-04 07:32:04 +00001066 cpp_buffer *buffer = pfile->buffer;
1067
1068 if (buffer->prev == 0)
1069 cpp_warning (pfile, "#pragma system_header ignored outside include file");
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001070 else
Neil Boothd82fc102001-08-02 23:03:31 +00001071 {
1072 check_eol (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +00001073 skip_rest_of_line (pfile);
Neil Boothd82fc102001-08-02 23:03:31 +00001074 cpp_make_system_header (pfile, 1, 0);
1075 }
Zack Weinberg2c0b35c2000-05-17 18:07:16 +00001076}
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001077
1078/* Check the modified date of the current include file against a specified
1079 file. Issue a diagnostic, if the specified file is newer. We use this to
1080 determine if a fixed header should be refixed. */
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001081static void
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001082do_pragma_dependency (pfile)
1083 cpp_reader *pfile;
1084{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001085 const cpp_token *header;
Neil Booth93c803682000-10-28 17:59:06 +00001086 int ordering;
Zack Weinberg041c3192000-07-04 01:58:21 +00001087
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001088 header = parse_include (pfile);
1089 if (!header)
Zack Weinberg58fea6a2000-08-02 01:13:45 +00001090 return;
Zack Weinberg041c3192000-07-04 01:58:21 +00001091
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001092 ordering = _cpp_compare_file_date (pfile, header);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001093 if (ordering < 0)
Neil Booth93c803682000-10-28 17:59:06 +00001094 cpp_warning (pfile, "cannot find source %s",
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001095 cpp_token_as_text (pfile, header));
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001096 else if (ordering > 0)
1097 {
Neil Booth93c803682000-10-28 17:59:06 +00001098 cpp_warning (pfile, "current file is older than %s",
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001099 cpp_token_as_text (pfile, header));
1100 if (cpp_get_token (pfile)->type != CPP_EOF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001101 {
1102 _cpp_backup_tokens (pfile, 1);
1103 do_diagnostic (pfile, WARNING, 0);
1104 }
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001105 }
Nathan Sidwellf3f751a2000-06-30 09:47:49 +00001106}
1107
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001108/* Get a token but skip padding. */
1109static const cpp_token *
1110get_token_no_padding (pfile)
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001111 cpp_reader *pfile;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001112{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001113 for (;;)
1114 {
1115 const cpp_token *result = cpp_get_token (pfile);
1116 if (result->type != CPP_PADDING)
1117 return result;
1118 }
1119}
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001120
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001121/* Check syntax is "(string-literal)". Returns the string on success,
1122 or NULL on failure. */
1123static const cpp_token *
1124get__Pragma_string (pfile)
1125 cpp_reader *pfile;
1126{
1127 const cpp_token *string;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001128
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001129 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1130 return NULL;
1131
1132 string = get_token_no_padding (pfile);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001133 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001134 return NULL;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001135
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001136 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1137 return NULL;
1138
1139 return string;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001140}
1141
1142/* Returns a malloced buffer containing a destringized cpp_string by
1143 removing the first \ of \" and \\ sequences. */
1144static unsigned char *
1145destringize (in, len)
1146 const cpp_string *in;
1147 unsigned int *len;
1148{
1149 const unsigned char *src, *limit;
1150 unsigned char *dest, *result;
1151
1152 dest = result = (unsigned char *) xmalloc (in->len);
1153 for (src = in->text, limit = src + in->len; src < limit;)
1154 {
1155 /* We know there is a character following the backslash. */
1156 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1157 src++;
1158 *dest++ = *src++;
1159 }
1160
1161 *len = dest - result;
1162 return result;
1163}
1164
1165void
1166_cpp_do__Pragma (pfile)
1167 cpp_reader *pfile;
1168{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001169 const cpp_token *string = get__Pragma_string (pfile);
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001170 unsigned char *buffer;
1171 unsigned int len;
1172
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001173 if (!string)
Neil Booth67821e32001-08-05 17:31:25 +00001174 cpp_error (pfile, "_Pragma takes a parenthesized string literal");
1175 else
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001176 {
Neil Booth50410422001-09-15 10:18:03 +00001177 /* Ideally, we'd like
1178 token1 _Pragma ("foo") token2
1179 to be output as
1180 token1
1181 # 7 "file.c"
1182 #pragma foo
1183 # 7 "file.c"
1184 token2
1185 Getting these correct line markers is a little tricky. */
1186
1187 unsigned int orig_line = pfile->line;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001188 buffer = destringize (&string->val.str, &len);
Neil Booth29401c32001-08-22 20:37:20 +00001189 run_directive (pfile, T_PRAGMA, (char *) buffer, len);
Neil Booth67821e32001-08-05 17:31:25 +00001190 free ((PTR) buffer);
Neil Booth50410422001-09-15 10:18:03 +00001191 pfile->line = orig_line;
1192 pfile->buffer->saved_flags = BOL;
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001193 }
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001194}
1195
Per Bothner7f2935c1995-03-16 13:59:07 -08001196/* Just ignore #sccs, on systems where we define it at all. */
Zack Weinberg07aa0b02000-04-01 22:55:25 +00001197#ifdef SCCS_DIRECTIVE
Zack Weinberg711b8822000-07-18 00:59:49 +00001198static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001199do_sccs (pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +00001200 cpp_reader *pfile ATTRIBUTE_UNUSED;
Per Bothner7f2935c1995-03-16 13:59:07 -08001201{
Per Bothner7f2935c1995-03-16 13:59:07 -08001202}
Zack Weinberg07aa0b02000-04-01 22:55:25 +00001203#endif
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001204
Zack Weinberg711b8822000-07-18 00:59:49 +00001205static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001206do_ifdef (pfile)
1207 cpp_reader *pfile;
1208{
Neil Booth93c803682000-10-28 17:59:06 +00001209 int skip = 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001210
Neil Boothcef0d192001-07-26 06:02:47 +00001211 if (! pfile->state.skipping)
Neil Booth93c803682000-10-28 17:59:06 +00001212 {
1213 const cpp_hashnode *node = lex_macro_node (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +00001214
Neil Booth93c803682000-10-28 17:59:06 +00001215 if (node)
1216 skip = node->type != NT_MACRO;
Geoffrey Keatingb43db0b2000-12-02 22:28:44 +00001217
1218 if (node)
1219 check_eol (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001220 }
1221
1222 push_conditional (pfile, skip, T_IFDEF, 0);
Zack Weinberg168d3732000-03-14 06:34:11 +00001223}
1224
Zack Weinberg711b8822000-07-18 00:59:49 +00001225static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001226do_ifndef (pfile)
1227 cpp_reader *pfile;
1228{
Neil Booth93c803682000-10-28 17:59:06 +00001229 int skip = 1;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001230 const cpp_hashnode *node = 0;
Zack Weinberg168d3732000-03-14 06:34:11 +00001231
Neil Boothcef0d192001-07-26 06:02:47 +00001232 if (! pfile->state.skipping)
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001233 {
Neil Booth93c803682000-10-28 17:59:06 +00001234 node = lex_macro_node (pfile);
1235 if (node)
1236 skip = node->type == NT_MACRO;
Geoffrey Keatingb43db0b2000-12-02 22:28:44 +00001237
1238 if (node)
1239 check_eol (pfile);
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001240 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001241
Neil Booth93c803682000-10-28 17:59:06 +00001242 push_conditional (pfile, skip, T_IFNDEF, node);
Per Bothner7f2935c1995-03-16 13:59:07 -08001243}
1244
Neil Booth6d18adb2001-07-29 17:27:57 +00001245/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1246 pfile->mi_ind_cmacro so we can handle multiple-include
1247 optimisations. If macro expansion occurs in the expression, we
1248 cannot treat it as a controlling conditional, since the expansion
1249 could change in the future. That is handled by cpp_get_token. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001250
Zack Weinberg711b8822000-07-18 00:59:49 +00001251static void
Zack Weinbergea4a4532000-05-29 16:19:32 +00001252do_if (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001253 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001254{
Neil Booth93c803682000-10-28 17:59:06 +00001255 int skip = 1;
Per Bothner7f2935c1995-03-16 13:59:07 -08001256
Neil Boothcef0d192001-07-26 06:02:47 +00001257 if (! pfile->state.skipping)
Neil Booth6d18adb2001-07-29 17:27:57 +00001258 skip = _cpp_parse_expr (pfile) == 0;
Neil Booth93c803682000-10-28 17:59:06 +00001259
Neil Booth6d18adb2001-07-29 17:27:57 +00001260 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
Per Bothner7f2935c1995-03-16 13:59:07 -08001261}
1262
Neil Boothb528a072000-11-12 11:46:21 +00001263/* Flip skipping state if appropriate and continue without changing
Zack Weinbergea4a4532000-05-29 16:19:32 +00001264 if_stack; this is so that the error message for missing #endif's
1265 etc. will point to the original #if. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001266
Zack Weinberg711b8822000-07-18 00:59:49 +00001267static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001268do_else (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001269 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001270{
Neil Boothb528a072000-11-12 11:46:21 +00001271 cpp_buffer *buffer = pfile->buffer;
1272 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001273
1274 if (ifs == NULL)
Neil Booth93c803682000-10-28 17:59:06 +00001275 cpp_error (pfile, "#else without #if");
1276 else
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001277 {
Neil Booth93c803682000-10-28 17:59:06 +00001278 if (ifs->type == T_ELSE)
1279 {
1280 cpp_error (pfile, "#else after #else");
Neil Booth50410422001-09-15 10:18:03 +00001281 cpp_error_with_line (pfile, ifs->line, 0,
Neil Booth93c803682000-10-28 17:59:06 +00001282 "the conditional began here");
1283 }
Neil Boothb528a072000-11-12 11:46:21 +00001284 ifs->type = T_ELSE;
1285
Neil Boothcef0d192001-07-26 06:02:47 +00001286 /* Skip any future (erroneous) #elses or #elifs. */
1287 pfile->state.skipping = ifs->skip_elses;
1288 ifs->skip_elses = true;
Neil Booth93c803682000-10-28 17:59:06 +00001289
1290 /* Invalidate any controlling macro. */
1291 ifs->mi_cmacro = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001292
Neil Boothcef0d192001-07-26 06:02:47 +00001293 /* Only check EOL if was not originally skipping. */
1294 if (!ifs->was_skipping)
1295 check_eol (pfile);
1296 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001297}
1298
Neil Booth93c803682000-10-28 17:59:06 +00001299/* handle a #elif directive by not changing if_stack either. see the
1300 comment above do_else. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001301
Zack Weinberg711b8822000-07-18 00:59:49 +00001302static void
Zack Weinbergea4a4532000-05-29 16:19:32 +00001303do_elif (pfile)
1304 cpp_reader *pfile;
1305{
Neil Boothb528a072000-11-12 11:46:21 +00001306 cpp_buffer *buffer = pfile->buffer;
1307 struct if_stack *ifs = buffer->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001308
1309 if (ifs == NULL)
Neil Boothb528a072000-11-12 11:46:21 +00001310 cpp_error (pfile, "#elif without #if");
1311 else
Zack Weinbergea4a4532000-05-29 16:19:32 +00001312 {
Neil Boothb528a072000-11-12 11:46:21 +00001313 if (ifs->type == T_ELSE)
1314 {
1315 cpp_error (pfile, "#elif after #else");
Neil Booth50410422001-09-15 10:18:03 +00001316 cpp_error_with_line (pfile, ifs->line, 0,
Neil Boothb528a072000-11-12 11:46:21 +00001317 "the conditional began here");
1318 }
1319 ifs->type = T_ELIF;
1320
Neil Boothcef0d192001-07-26 06:02:47 +00001321 /* Only evaluate this if we aren't skipping elses. During
1322 evaluation, set skipping to false to get lexer warnings. */
1323 if (ifs->skip_elses)
1324 pfile->state.skipping = 1;
1325 else
Neil Boothb528a072000-11-12 11:46:21 +00001326 {
Neil Boothcef0d192001-07-26 06:02:47 +00001327 pfile->state.skipping = 0;
1328 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1329 ifs->skip_elses = ! pfile->state.skipping;
Neil Boothb528a072000-11-12 11:46:21 +00001330 }
Neil Boothcef0d192001-07-26 06:02:47 +00001331
1332 /* Invalidate any controlling macro. */
1333 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001334 }
Zack Weinbergea4a4532000-05-29 16:19:32 +00001335}
1336
Neil Boothcef0d192001-07-26 06:02:47 +00001337/* #endif pops the if stack and resets pfile->state.skipping. */
Zack Weinbergea4a4532000-05-29 16:19:32 +00001338
Zack Weinberg711b8822000-07-18 00:59:49 +00001339static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001340do_endif (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001341 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001342{
Neil Boothb528a072000-11-12 11:46:21 +00001343 cpp_buffer *buffer = pfile->buffer;
1344 struct if_stack *ifs = buffer->if_stack;
Per Bothner7f2935c1995-03-16 13:59:07 -08001345
Zack Weinbergea4a4532000-05-29 16:19:32 +00001346 if (ifs == NULL)
1347 cpp_error (pfile, "#endif without #if");
Per Bothner7f2935c1995-03-16 13:59:07 -08001348 else
1349 {
Neil Boothcef0d192001-07-26 06:02:47 +00001350 /* Only check EOL if was not originally skipping. */
1351 if (!ifs->was_skipping)
1352 check_eol (pfile);
1353
Neil Booth93c803682000-10-28 17:59:06 +00001354 /* If potential control macro, we go back outside again. */
1355 if (ifs->next == 0 && ifs->mi_cmacro)
1356 {
Neil Booth6d18adb2001-07-29 17:27:57 +00001357 pfile->mi_valid = true;
Neil Booth93c803682000-10-28 17:59:06 +00001358 pfile->mi_cmacro = ifs->mi_cmacro;
1359 }
1360
Neil Boothb528a072000-11-12 11:46:21 +00001361 buffer->if_stack = ifs->next;
Neil Boothcef0d192001-07-26 06:02:47 +00001362 pfile->state.skipping = ifs->was_skipping;
Neil Booth2a967f32001-05-20 06:26:45 +00001363 obstack_free (&pfile->buffer_ob, ifs);
Per Bothner7f2935c1995-03-16 13:59:07 -08001364 }
Neil Booth93c803682000-10-28 17:59:06 +00001365}
Zack Weinberg041c3192000-07-04 01:58:21 +00001366
Neil Boothcef0d192001-07-26 06:02:47 +00001367/* Push an if_stack entry and set pfile->state.skipping accordingly.
Neil Booth6d18adb2001-07-29 17:27:57 +00001368 If this is a #if or #ifndef, CMACRO is a potentially controlling
1369 macro - we need to check here that we are at the top of the file. */
Zack Weinbergea4a4532000-05-29 16:19:32 +00001370
1371static void
1372push_conditional (pfile, skip, type, cmacro)
1373 cpp_reader *pfile;
1374 int skip;
1375 int type;
1376 const cpp_hashnode *cmacro;
1377{
1378 struct if_stack *ifs;
Neil Boothb528a072000-11-12 11:46:21 +00001379 cpp_buffer *buffer = pfile->buffer;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001380
Neil Booth2a967f32001-05-20 06:26:45 +00001381 ifs = xobnew (&pfile->buffer_ob, struct if_stack);
Neil Booth50410422001-09-15 10:18:03 +00001382 ifs->line = pfile->directive_line;
Neil Boothb528a072000-11-12 11:46:21 +00001383 ifs->next = buffer->if_stack;
Neil Boothcef0d192001-07-26 06:02:47 +00001384 ifs->skip_elses = pfile->state.skipping || !skip;
1385 ifs->was_skipping = pfile->state.skipping;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001386 ifs->type = type;
Neil Booth6d18adb2001-07-29 17:27:57 +00001387 /* This condition is effectively a test for top-of-file. */
1388 if (pfile->mi_valid && pfile->mi_cmacro == 0)
Neil Booth93c803682000-10-28 17:59:06 +00001389 ifs->mi_cmacro = cmacro;
1390 else
1391 ifs->mi_cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001392
Neil Boothcef0d192001-07-26 06:02:47 +00001393 pfile->state.skipping = skip;
Neil Boothb528a072000-11-12 11:46:21 +00001394 buffer->if_stack = ifs;
Zack Weinberg7061aa51998-12-15 11:09:16 +00001395}
1396
Neil Booth93c803682000-10-28 17:59:06 +00001397/* Read the tokens of the answer into the macro pool. Only commit the
1398 memory if we intend it as permanent storage, i.e. the #assert case.
1399 Returns 0 on success. */
1400
1401static int
1402parse_answer (pfile, answerp, type)
Zack Weinberg041c3192000-07-04 01:58:21 +00001403 cpp_reader *pfile;
1404 struct answer **answerp;
Neil Booth93c803682000-10-28 17:59:06 +00001405 int type;
Zack Weinberg041c3192000-07-04 01:58:21 +00001406{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001407 const cpp_token *paren;
Neil Booth93c803682000-10-28 17:59:06 +00001408 struct answer *answer;
Neil Booth8c3b2692001-09-30 10:03:11 +00001409 unsigned int acount;
Zack Weinberg041c3192000-07-04 01:58:21 +00001410
Neil Booth93c803682000-10-28 17:59:06 +00001411 /* In a conditional, it is legal to not have an open paren. We
1412 should save the following token in this case. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001413 paren = cpp_get_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001414
1415 /* If not a paren, see if we're OK. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001416 if (paren->type != CPP_OPEN_PAREN)
Zack Weinberg041c3192000-07-04 01:58:21 +00001417 {
Neil Booth93c803682000-10-28 17:59:06 +00001418 /* In a conditional no answer is a test for any answer. It
1419 could be followed by any token. */
1420 if (type == T_IF)
Neil Boothbdcbe492001-09-13 20:05:17 +00001421 {
1422 _cpp_backup_tokens (pfile, 1);
1423 return 0;
1424 }
Neil Booth93c803682000-10-28 17:59:06 +00001425
1426 /* #unassert with no answer is valid - it removes all answers. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001427 if (type == T_UNASSERT && paren->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +00001428 return 0;
1429
Zack Weinberg041c3192000-07-04 01:58:21 +00001430 cpp_error (pfile, "missing '(' after predicate");
Neil Booth93c803682000-10-28 17:59:06 +00001431 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001432 }
1433
Neil Booth8c3b2692001-09-30 10:03:11 +00001434 for (acount = 0;; acount++)
Zack Weinberg041c3192000-07-04 01:58:21 +00001435 {
Neil Booth8c3b2692001-09-30 10:03:11 +00001436 size_t room_needed;
1437 const cpp_token *token = cpp_get_token (pfile);
1438 cpp_token *dest;
Zack Weinberg041c3192000-07-04 01:58:21 +00001439
Neil Booth93c803682000-10-28 17:59:06 +00001440 if (token->type == CPP_CLOSE_PAREN)
1441 break;
Zack Weinberg041c3192000-07-04 01:58:21 +00001442
1443 if (token->type == CPP_EOF)
1444 {
1445 cpp_error (pfile, "missing ')' to complete answer");
Neil Booth93c803682000-10-28 17:59:06 +00001446 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001447 }
Neil Booth8c3b2692001-09-30 10:03:11 +00001448
1449 /* struct answer includes the space for one token. */
1450 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1451
1452 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1453 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1454
1455 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1456 *dest = *token;
1457
1458 /* Drop whitespace at start, for answer equivalence purposes. */
1459 if (acount == 0)
1460 dest->flags &= ~PREV_WHITE;
Zack Weinberg041c3192000-07-04 01:58:21 +00001461 }
1462
Neil Booth8c3b2692001-09-30 10:03:11 +00001463 if (acount == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001464 {
1465 cpp_error (pfile, "predicate's answer is empty");
Neil Booth93c803682000-10-28 17:59:06 +00001466 return 1;
Zack Weinberg041c3192000-07-04 01:58:21 +00001467 }
1468
Neil Booth8c3b2692001-09-30 10:03:11 +00001469 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1470 answer->count = acount;
1471 answer->next = NULL;
Neil Booth93c803682000-10-28 17:59:06 +00001472 *answerp = answer;
Zack Weinberg041c3192000-07-04 01:58:21 +00001473
Neil Booth93c803682000-10-28 17:59:06 +00001474 return 0;
1475}
1476
1477/* Parses an assertion, returning a pointer to the hash node of the
1478 predicate, or 0 on error. If an answer was supplied, it is placed
Neil Booth7f2f1a62000-11-14 18:32:06 +00001479 in ANSWERP, otherwise it is set to 0. */
Neil Booth93c803682000-10-28 17:59:06 +00001480static cpp_hashnode *
1481parse_assertion (pfile, answerp, type)
1482 cpp_reader *pfile;
1483 struct answer **answerp;
1484 int type;
1485{
1486 cpp_hashnode *result = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001487 const cpp_token *predicate;
Neil Booth93c803682000-10-28 17:59:06 +00001488
1489 /* We don't expand predicates or answers. */
1490 pfile->state.prevent_expansion++;
1491
Neil Booth93c803682000-10-28 17:59:06 +00001492 *answerp = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001493 predicate = cpp_get_token (pfile);
1494 if (predicate->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +00001495 cpp_error (pfile, "assertion without predicate");
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001496 else if (predicate->type != CPP_NAME)
Neil Booth93c803682000-10-28 17:59:06 +00001497 cpp_error (pfile, "predicate must be an identifier");
1498 else if (parse_answer (pfile, answerp, type) == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +00001499 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001500 unsigned int len = NODE_LEN (predicate->val.node);
Neil Booth93c803682000-10-28 17:59:06 +00001501 unsigned char *sym = alloca (len + 1);
1502
1503 /* Prefix '#' to get it out of macro namespace. */
1504 sym[0] = '#';
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001505 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
Neil Booth93c803682000-10-28 17:59:06 +00001506 result = cpp_lookup (pfile, sym, len + 1);
Zack Weinberg041c3192000-07-04 01:58:21 +00001507 }
1508
Neil Booth93c803682000-10-28 17:59:06 +00001509 pfile->state.prevent_expansion--;
1510 return result;
Zack Weinberg041c3192000-07-04 01:58:21 +00001511}
1512
1513/* Returns a pointer to the pointer to the answer in the answer chain,
1514 or a pointer to NULL if the answer is not in the chain. */
Neil Booth93c803682000-10-28 17:59:06 +00001515static struct answer **
1516find_answer (node, candidate)
Zack Weinberg041c3192000-07-04 01:58:21 +00001517 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00001518 const struct answer *candidate;
Zack Weinberg041c3192000-07-04 01:58:21 +00001519{
Neil Booth93c803682000-10-28 17:59:06 +00001520 unsigned int i;
Zack Weinberg041c3192000-07-04 01:58:21 +00001521 struct answer **result;
1522
1523 for (result = &node->value.answers; *result; result = &(*result)->next)
Neil Booth93c803682000-10-28 17:59:06 +00001524 {
1525 struct answer *answer = *result;
1526
1527 if (answer->count == candidate->count)
1528 {
1529 for (i = 0; i < answer->count; i++)
1530 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1531 break;
1532
1533 if (i == answer->count)
1534 break;
1535 }
1536 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001537
1538 return result;
1539}
1540
Neil Booth93c803682000-10-28 17:59:06 +00001541/* Test an assertion within a preprocessor conditional. Returns
1542 non-zero on failure, zero on success. On success, the result of
1543 the test is written into VALUE. */
1544int
1545_cpp_test_assertion (pfile, value)
1546 cpp_reader *pfile;
1547 int *value;
1548{
1549 struct answer *answer;
1550 cpp_hashnode *node;
1551
1552 node = parse_assertion (pfile, &answer, T_IF);
1553 if (node)
1554 *value = (node->type == NT_ASSERTION &&
1555 (answer == 0 || *find_answer (node, answer) != 0));
1556
1557 /* We don't commit the memory for the answer - it's temporary only. */
1558 return node == 0;
1559}
1560
Zack Weinberg711b8822000-07-18 00:59:49 +00001561static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001562do_assert (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001563 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001564{
Zack Weinberg041c3192000-07-04 01:58:21 +00001565 struct answer *new_answer;
1566 cpp_hashnode *node;
Zack Weinberg15dad1d2000-05-18 15:55:46 +00001567
Neil Booth93c803682000-10-28 17:59:06 +00001568 node = parse_assertion (pfile, &new_answer, T_ASSERT);
Zack Weinberg041c3192000-07-04 01:58:21 +00001569 if (node)
1570 {
Neil Booth93c803682000-10-28 17:59:06 +00001571 /* Place the new answer in the answer list. First check there
1572 is not a duplicate. */
Zack Weinberg041c3192000-07-04 01:58:21 +00001573 new_answer->next = 0;
Neil Booth93c803682000-10-28 17:59:06 +00001574 if (node->type == NT_ASSERTION)
Zack Weinberg041c3192000-07-04 01:58:21 +00001575 {
Neil Booth93c803682000-10-28 17:59:06 +00001576 if (*find_answer (node, new_answer))
1577 {
Neil Bootha28c50352001-05-16 22:02:09 +00001578 cpp_warning (pfile, "\"%s\" re-asserted", NODE_NAME (node) + 1);
Neil Booth93c803682000-10-28 17:59:06 +00001579 return;
1580 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001581 new_answer->next = node->value.answers;
1582 }
Neil Booth8c3b2692001-09-30 10:03:11 +00001583
Neil Booth93c803682000-10-28 17:59:06 +00001584 node->type = NT_ASSERTION;
Zack Weinberg041c3192000-07-04 01:58:21 +00001585 node->value.answers = new_answer;
Neil Booth8c3b2692001-09-30 10:03:11 +00001586 BUFF_FRONT (pfile->a_buff) += (sizeof (struct answer)
1587 + (new_answer->count - 1)
1588 * sizeof (cpp_token));
1589 check_eol (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +00001590 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001591}
Zack Weinberg7061aa51998-12-15 11:09:16 +00001592
Zack Weinberg711b8822000-07-18 00:59:49 +00001593static void
Zack Weinberg168d3732000-03-14 06:34:11 +00001594do_unassert (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001595 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001596{
Zack Weinberg041c3192000-07-04 01:58:21 +00001597 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00001598 struct answer *answer;
Zack Weinberg041c3192000-07-04 01:58:21 +00001599
Neil Booth93c803682000-10-28 17:59:06 +00001600 node = parse_assertion (pfile, &answer, T_UNASSERT);
1601 /* It isn't an error to #unassert something that isn't asserted. */
1602 if (node && node->type == NT_ASSERTION)
Zack Weinberg7061aa51998-12-15 11:09:16 +00001603 {
Zack Weinberg041c3192000-07-04 01:58:21 +00001604 if (answer)
Neil Booth93c803682000-10-28 17:59:06 +00001605 {
1606 struct answer **p = find_answer (node, answer), *temp;
1607
1608 /* Remove the answer from the list. */
1609 temp = *p;
1610 if (temp)
1611 *p = temp->next;
1612
1613 /* Did we free the last answer? */
1614 if (node->value.answers == 0)
1615 node->type = NT_VOID;
Neil Booth8c3b2692001-09-30 10:03:11 +00001616
1617 check_eol (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001618 }
1619 else
1620 _cpp_free_definition (node);
Zack Weinberg7061aa51998-12-15 11:09:16 +00001621 }
Neil Booth93c803682000-10-28 17:59:06 +00001622
1623 /* We don't commit the memory for the answer - it's temporary only. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001624}
Per Bothner7f2935c1995-03-16 13:59:07 -08001625
Zack Weinberg45b966d2000-03-13 22:01:08 +00001626/* These are for -D, -U, -A. */
1627
1628/* Process the string STR as if it appeared as the body of a #define.
1629 If STR is just an identifier, define it with value 1.
1630 If STR has anything after the identifier, then it should
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001631 be identifier=definition. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001632
1633void
1634cpp_define (pfile, str)
1635 cpp_reader *pfile;
1636 const char *str;
1637{
1638 char *buf, *p;
1639 size_t count;
1640
Zack Weinberg45b966d2000-03-13 22:01:08 +00001641 /* Copy the entire option so we can modify it.
1642 Change the first "=" in the string to a space. If there is none,
Neil Booth86368122000-10-31 23:34:59 +00001643 tack " 1" on the end. */
1644
1645 /* Length including the null. */
1646 count = strlen (str);
1647 buf = (char *) alloca (count + 2);
1648 memcpy (buf, str, count);
1649
1650 p = strchr (str, '=');
Zack Weinberg45b966d2000-03-13 22:01:08 +00001651 if (p)
Neil Booth86368122000-10-31 23:34:59 +00001652 buf[p - str] = ' ';
Zack Weinberg45b966d2000-03-13 22:01:08 +00001653 else
1654 {
Neil Booth86368122000-10-31 23:34:59 +00001655 buf[count++] = ' ';
1656 buf[count++] = '1';
Zack Weinberg45b966d2000-03-13 22:01:08 +00001657 }
1658
Neil Booth29401c32001-08-22 20:37:20 +00001659 run_directive (pfile, T_DEFINE, buf, count);
Zack Weinberg2c8f0512000-08-29 18:37:37 +00001660}
1661
Neil Boothad2a0842000-12-17 00:13:54 +00001662/* Slight variant of the above for use by initialize_builtins. */
Zack Weinberg2c8f0512000-08-29 18:37:37 +00001663void
1664_cpp_define_builtin (pfile, str)
1665 cpp_reader *pfile;
1666 const char *str;
1667{
Neil Booth29401c32001-08-22 20:37:20 +00001668 run_directive (pfile, T_DEFINE, str, strlen (str));
Zack Weinberg45b966d2000-03-13 22:01:08 +00001669}
1670
1671/* Process MACRO as if it appeared as the body of an #undef. */
1672void
1673cpp_undef (pfile, macro)
1674 cpp_reader *pfile;
1675 const char *macro;
1676{
Neil Booth29401c32001-08-22 20:37:20 +00001677 run_directive (pfile, T_UNDEF, macro, strlen (macro));
Zack Weinberg45b966d2000-03-13 22:01:08 +00001678}
1679
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001680/* Process the string STR as if it appeared as the body of a #assert. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001681void
1682cpp_assert (pfile, str)
1683 cpp_reader *pfile;
1684 const char *str;
1685{
Neil Booth86368122000-10-31 23:34:59 +00001686 handle_assertion (pfile, str, T_ASSERT);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001687}
1688
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001689/* Process STR as if it appeared as the body of an #unassert. */
Zack Weinberg0b22d651999-03-15 18:42:46 +00001690void
1691cpp_unassert (pfile, str)
1692 cpp_reader *pfile;
Neil Booth7ceb3592000-03-11 00:49:44 +00001693 const char *str;
Zack Weinberg0b22d651999-03-15 18:42:46 +00001694{
Neil Booth86368122000-10-31 23:34:59 +00001695 handle_assertion (pfile, str, T_UNASSERT);
Zack Weinberg0b22d651999-03-15 18:42:46 +00001696}
1697
Neil Booth86368122000-10-31 23:34:59 +00001698/* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1699static void
1700handle_assertion (pfile, str, type)
1701 cpp_reader *pfile;
1702 const char *str;
1703 int type;
1704{
1705 size_t count = strlen (str);
1706 const char *p = strchr (str, '=');
1707
1708 if (p)
1709 {
1710 /* Copy the entire option so we can modify it. Change the first
1711 "=" in the string to a '(', and tack a ')' on the end. */
1712 char *buf = (char *) alloca (count + 1);
1713
1714 memcpy (buf, str, count);
1715 buf[p - str] = '(';
1716 buf[count++] = ')';
1717 str = buf;
1718 }
1719
Neil Booth29401c32001-08-22 20:37:20 +00001720 run_directive (pfile, type, str, count);
Neil Booth86368122000-10-31 23:34:59 +00001721}
1722
Neil Booth7e96d762001-01-13 01:00:01 +00001723/* The number of errors for a given reader. */
1724unsigned int
1725cpp_errors (pfile)
1726 cpp_reader *pfile;
1727{
1728 return pfile->errors;
1729}
1730
1731/* The options structure. */
1732cpp_options *
1733cpp_get_options (pfile)
1734 cpp_reader *pfile;
1735{
1736 return &pfile->opts;
1737}
1738
1739/* The callbacks structure. */
1740cpp_callbacks *
1741cpp_get_callbacks (pfile)
1742 cpp_reader *pfile;
1743{
1744 return &pfile->cb;
1745}
1746
Neil Boothd82fc102001-08-02 23:03:31 +00001747/* The line map set. */
Neil Booth47d89cf2001-08-11 07:33:39 +00001748const struct line_maps *
Neil Boothd82fc102001-08-02 23:03:31 +00001749cpp_get_line_maps (pfile)
1750 cpp_reader *pfile;
1751{
1752 return &pfile->line_maps;
1753}
1754
Neil Booth7e96d762001-01-13 01:00:01 +00001755/* Copy the given callbacks structure to our own. */
1756void
1757cpp_set_callbacks (pfile, cb)
1758 cpp_reader *pfile;
1759 cpp_callbacks *cb;
1760{
1761 pfile->cb = *cb;
1762}
1763
Neil Bootheb1f4d92000-12-18 19:00:26 +00001764/* Push a new buffer on the buffer stack. Returns the new buffer; it
1765 doesn't fail. It does not generate a file change call back; that
1766 is the responsibility of the caller. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00001767cpp_buffer *
Neil Booth29401c32001-08-22 20:37:20 +00001768cpp_push_buffer (pfile, buffer, len, from_stage3, return_at_eof)
Zack Weinbergc71f8352000-07-05 05:33:57 +00001769 cpp_reader *pfile;
1770 const U_CHAR *buffer;
Neil Booth3cf35932000-12-05 23:42:43 +00001771 size_t len;
Neil Booth29401c32001-08-22 20:37:20 +00001772 int from_stage3;
Neil Boothef6e9582001-08-04 12:01:59 +00001773 int return_at_eof;
Zack Weinbergc71f8352000-07-05 05:33:57 +00001774{
Neil Booth2a967f32001-05-20 06:26:45 +00001775 cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
Neil Booth93c803682000-10-28 17:59:06 +00001776
Neil Boothfde84342001-08-06 21:07:41 +00001777 /* Clears, amongst other things, if_stack and mi_cmacro. */
1778 memset (new, 0, sizeof (cpp_buffer));
Neil Boothad2a0842000-12-17 00:13:54 +00001779
Neil Boothfde84342001-08-06 21:07:41 +00001780 new->line_base = new->buf = new->cur = buffer;
1781 new->rlimit = buffer + len;
Neil Boothad2a0842000-12-17 00:13:54 +00001782
Neil Boothfde84342001-08-06 21:07:41 +00001783 /* No read ahead or extra char initially. */
1784 new->read_ahead = EOF;
1785 new->extra_char = EOF;
Neil Booth29401c32001-08-22 20:37:20 +00001786 new->from_stage3 = from_stage3;
Neil Booth3cf35932000-12-05 23:42:43 +00001787 new->prev = pfile->buffer;
Neil Boothef6e9582001-08-04 12:01:59 +00001788 new->return_at_eof = return_at_eof;
Neil Boothbdcbe492001-09-13 20:05:17 +00001789 new->saved_flags = BOL;
Neil Booth0bda4762000-12-11 07:45:16 +00001790
Neil Booth3cf35932000-12-05 23:42:43 +00001791 pfile->buffer = new;
Neil Booth0bda4762000-12-11 07:45:16 +00001792
Zack Weinbergc71f8352000-07-05 05:33:57 +00001793 return new;
1794}
1795
Neil Bootheb1f4d92000-12-18 19:00:26 +00001796/* If called from do_line, pops a single buffer. Otherwise pops all
1797 buffers until a real file is reached. Generates appropriate
1798 call-backs. */
Neil Boothef6e9582001-08-04 12:01:59 +00001799void
1800_cpp_pop_buffer (pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00001801 cpp_reader *pfile;
1802{
Neil Boothfde84342001-08-06 21:07:41 +00001803 cpp_buffer *buffer = pfile->buffer;
Neil Boothad2a0842000-12-17 00:13:54 +00001804 struct if_stack *ifs;
Zack Weinbergc71f8352000-07-05 05:33:57 +00001805
Neil Boothfde84342001-08-06 21:07:41 +00001806 /* Walk back up the conditional stack till we reach its level at
1807 entry to this file, issuing error messages. */
1808 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
Neil Booth50410422001-09-15 10:18:03 +00001809 cpp_error_with_line (pfile, ifs->line, 0,
Neil Boothfde84342001-08-06 21:07:41 +00001810 "unterminated #%s", dtable[ifs->type].name);
1811
Neil Booth97293892001-09-14 22:04:46 +00001812 /* In case of a missing #endif. */
Neil Booth67821e32001-08-05 17:31:25 +00001813 pfile->state.skipping = 0;
Neil Booth29401c32001-08-22 20:37:20 +00001814
1815 /* Update the reader's buffer before _cpp_do_file_change. */
1816 pfile->buffer = buffer->prev;
1817
1818 if (buffer->inc)
1819 _cpp_pop_file_buffer (pfile, buffer->inc);
1820
1821 obstack_free (&pfile->buffer_ob, buffer);
Zack Weinbergc71f8352000-07-05 05:33:57 +00001822}
1823
Zack Weinbergc71f8352000-07-05 05:33:57 +00001824void
Neil Booth2a967f32001-05-20 06:26:45 +00001825_cpp_init_directives (pfile)
Zack Weinbergc71f8352000-07-05 05:33:57 +00001826 cpp_reader *pfile;
1827{
Neil Booth766ee682001-01-29 19:20:12 +00001828 unsigned int i;
Neil Booth93c803682000-10-28 17:59:06 +00001829 cpp_hashnode *node;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001830
Neil Booth93c803682000-10-28 17:59:06 +00001831 /* Register the directives. */
John David Anglin37b85242001-03-02 01:11:50 +00001832 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
Neil Booth93c803682000-10-28 17:59:06 +00001833 {
Neil Booth766ee682001-01-29 19:20:12 +00001834 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
1835 node->directive_index = i + 1;
Neil Booth93c803682000-10-28 17:59:06 +00001836 }
Zack Weinbergc71f8352000-07-05 05:33:57 +00001837}