blob: f6108f468630b342be77795df01639cf2caa00b3 [file] [log] [blame]
Per Bothner7f2935c1995-03-16 13:59:07 -08001/* CPP Library.
Jeff Law5e7b4e22000-02-25 22:59:31 -07002 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 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"
Jeffrey A Lawab87f8c1999-01-27 01:43:17 +000027#include "intl.h"
Zack Weinbergc71f8352000-07-05 05:33:57 +000028#include "obstack.h"
Zack Weinberg07aa0b02000-04-01 22:55:25 +000029#include "symcat.h"
Jeff Law956d6951997-12-06 17:31:01 -070030
Zack Weinbergc71f8352000-07-05 05:33:57 +000031#ifdef HAVE_MMAP_FILE
32# include <sys/mman.h>
33#endif
34
Zack Weinberg88ae23e2000-03-08 23:35:19 +000035/* Stack of conditionals currently in progress
36 (including both successful and failing conditionals). */
37
38struct if_stack
39{
40 struct if_stack *next;
Zack Weinberg041c3192000-07-04 01:58:21 +000041 unsigned int lineno; /* line number where condition started */
42 unsigned int colno; /* and column */
Zack Weinbergea4a4532000-05-29 16:19:32 +000043 int was_skipping; /* value of pfile->skipping before this if */
44 const cpp_hashnode *cmacro; /* macro name for #ifndef around entire file */
Zack Weinberg168d3732000-03-14 06:34:11 +000045 int type; /* type of last directive seen in this group */
Zack Weinberg88ae23e2000-03-08 23:35:19 +000046};
Zack Weinberg88ae23e2000-03-08 23:35:19 +000047
Zack Weinberg1316f1f2000-02-06 07:53:50 +000048/* Forward declarations. */
49
Zack Weinberg041c3192000-07-04 01:58:21 +000050static void validate_else PARAMS ((cpp_reader *, const U_CHAR *));
Zack Weinbergc71f8352000-07-05 05:33:57 +000051static int parse_include PARAMS ((cpp_reader *, const U_CHAR *, int,
Zack Weinberg041c3192000-07-04 01:58:21 +000052 const U_CHAR **, unsigned int *,
53 int *));
54static void push_conditional PARAMS ((cpp_reader *, int, int,
55 const cpp_hashnode *));
56static void pass_thru_directive PARAMS ((cpp_reader *));
Zack Weinbergc71f8352000-07-05 05:33:57 +000057static int read_line_number PARAMS ((cpp_reader *, int *));
58static int strtoul_for_line PARAMS ((const U_CHAR *, unsigned int,
Zack Weinberg041c3192000-07-04 01:58:21 +000059 unsigned long *));
60
Zack Weinbergc71f8352000-07-05 05:33:57 +000061static const cpp_hashnode *
62 parse_ifdef PARAMS ((cpp_reader *, const U_CHAR *));
63static const cpp_hashnode *
64 detect_if_not_defined PARAMS ((cpp_reader *));
65static cpp_hashnode *
66 get_define_node PARAMS ((cpp_reader *));
67static void dump_macro_name PARAMS ((cpp_reader *, cpp_hashnode *));
68static void unwind_if_stack PARAMS ((cpp_reader *, cpp_buffer *));
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +000069
Zack Weinberg041c3192000-07-04 01:58:21 +000070/* Utility. */
71#define str_match(sym, len, str) \
72((len) == (sizeof (str) - 1) && !ustrncmp ((sym), U(str), sizeof (str) - 1))
Zack Weinberg07aa0b02000-04-01 22:55:25 +000073
Zack Weinberg168d3732000-03-14 06:34:11 +000074/* This is the table of directive handlers. It is ordered by
75 frequency of occurrence; the numbers at the end are directive
76 counts from all the source code I have lying around (egcs and libc
77 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
78 pcmcia-cs-3.0.9).
Jeff Lawe9a25f71997-11-02 14:19:36 -070079
Zack Weinberg168d3732000-03-14 06:34:11 +000080 The entries with a dash and a name after the count are extensions,
81 of which all but #warning and #include_next are deprecated. The name
82 is where the extension appears to have come from. */
Per Bothner7f2935c1995-03-16 13:59:07 -080083
Zack Weinberg07aa0b02000-04-01 22:55:25 +000084/* #sccs is not always recognized. */
85#ifdef SCCS_DIRECTIVE
Zack Weinberg041c3192000-07-04 01:58:21 +000086# define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
Zack Weinberg07aa0b02000-04-01 22:55:25 +000087#else
88# define SCCS_ENTRY /* nothing */
89#endif
90
Zack Weinberg041c3192000-07-04 01:58:21 +000091#define DIRECTIVE_TABLE \
92D(define, T_DEFINE = 0, KANDR, COMMENTS) /* 270554 */ \
93D(include, T_INCLUDE, KANDR, EXPAND | INCL) /* 52262 */ \
94D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
95D(ifdef, T_IFDEF, KANDR, COND) /* 22000 */ \
96D(if, T_IF, KANDR, COND | EXPAND) /* 18162 */ \
97D(else, T_ELSE, KANDR, COND) /* 9863 */ \
98D(ifndef, T_IFNDEF, KANDR, COND) /* 9675 */ \
99D(undef, T_UNDEF, KANDR, 0) /* 4837 */ \
100D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
101D(elif, T_ELIF, KANDR, COND | EXPAND) /* 610 */ \
102D(error, T_ERROR, STDC89, 0) /* 475 */ \
103D(pragma, T_PRAGMA, STDC89, 0) /* 195 */ \
104D(warning, T_WARNING, EXTENSION, 0) /* 22 GNU */ \
105D(include_next, T_INCLUDE_NEXT, EXTENSION, EXPAND | INCL) /* 19 GNU */ \
106D(ident, T_IDENT, EXTENSION, 0) /* 11 SVR4 */ \
107D(import, T_IMPORT, EXTENSION, EXPAND | INCL) /* 0 ObjC */ \
108D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
109D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
110SCCS_ENTRY /* 0 SVR2? */
Zack Weinberg168d3732000-03-14 06:34:11 +0000111
112/* Use the table to generate a series of prototypes, an enum for the
113 directive names, and an array of directive handlers. */
114
115/* The directive-processing functions are declared to return int
116 instead of void, because some old compilers have trouble with
117 pointers to functions returning void. */
118
Kaveh R. Ghazi7c324042000-04-04 21:07:17 +0000119/* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000120#define D(name, t, o, f) static int CONCAT2(do_,name) PARAMS ((cpp_reader *));
Zack Weinberg168d3732000-03-14 06:34:11 +0000121DIRECTIVE_TABLE
122#undef D
123
Zack Weinberg041c3192000-07-04 01:58:21 +0000124#define D(n, tag, o, f) tag,
Zack Weinberg168d3732000-03-14 06:34:11 +0000125enum
126{
127 DIRECTIVE_TABLE
128 N_DIRECTIVES
Per Bothner7f2935c1995-03-16 13:59:07 -0800129};
Zack Weinberg168d3732000-03-14 06:34:11 +0000130#undef D
131
Kaveh R. Ghazi7c324042000-04-04 21:07:17 +0000132/* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000133#define D(name, t, origin, flags) \
Zack Weinberg12cf91f2000-05-04 04:38:01 +0000134{ CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
Zack Weinberg041c3192000-07-04 01:58:21 +0000135 sizeof STRINGX(name) - 1, origin, flags },
Zack Weinberg168d3732000-03-14 06:34:11 +0000136static const struct directive dtable[] =
137{
138DIRECTIVE_TABLE
139};
140#undef D
141#undef DIRECTIVE_TABLE
Per Bothner7f2935c1995-03-16 13:59:07 -0800142
Zack Weinbergc5a04732000-04-25 19:32:36 +0000143/* Check if a token's name matches that of a known directive. Put in
144 this file to save exporting dtable and other unneeded information. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000145const struct directive *
146_cpp_check_directive (pfile, token, bol)
147 cpp_reader *pfile;
148 const cpp_token *token;
149 int bol;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000150{
Zack Weinbergc5a04732000-04-25 19:32:36 +0000151 unsigned int i;
152
Zack Weinberg041c3192000-07-04 01:58:21 +0000153 /* If we are rescanning preprocessed input, don't obey any directives
154 other than # nnn. */
155 if (CPP_OPTION (pfile, preprocessed))
156 return 0;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000157
158 for (i = 0; i < N_DIRECTIVES; i++)
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000159 if (pfile->spec_nodes->dirs[i] == token->val.node)
Zack Weinbergc5a04732000-04-25 19:32:36 +0000160 {
Zack Weinberg041c3192000-07-04 01:58:21 +0000161 /* If we are skipping a failed conditional group, all non-conditional
162 directives are ignored. */
163 if (pfile->skipping && !(dtable[i].flags & COND))
164 return 0;
165
166 /* In -traditional mode, a directive is ignored unless its #
167 is in column 1. */
168 if (!bol && dtable[i].origin == KANDR && CPP_WTRADITIONAL (pfile))
169 cpp_warning (pfile, "traditional C ignores #%s with the # indented",
170 dtable[i].name);
171
172 if (!bol && CPP_TRADITIONAL (pfile))
173 return 0;
174
175 /* Issue -pedantic warnings for extended directives. */
176 if (CPP_PEDANTIC (pfile) && dtable[i].origin == EXTENSION)
177 cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name);
178
179 /* -Wtraditional gives warnings about directives with inappropriate
180 indentation of #. */
181 if (bol && dtable[i].origin != KANDR && CPP_WTRADITIONAL (pfile))
182 cpp_warning (pfile,
183 "suggest hiding #%s from traditional C with an indented #",
184 dtable[i].name);
185
186 return &dtable[i];
Zack Weinbergc5a04732000-04-25 19:32:36 +0000187 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000188
189 return 0;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000190}
191
Zack Weinberg041c3192000-07-04 01:58:21 +0000192const struct directive *
193_cpp_check_linemarker (pfile, token, bol)
Per Bothner7f2935c1995-03-16 13:59:07 -0800194 cpp_reader *pfile;
Zack Weinberg041c3192000-07-04 01:58:21 +0000195 const cpp_token *token ATTRIBUTE_UNUSED;
196 int bol;
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000197{
Zack Weinberg40c79d52000-01-12 00:35:36 +0000198 /* # followed by a number is equivalent to #line. Do not recognize
Zack Weinbergea4a4532000-05-29 16:19:32 +0000199 this form in assembly language source files or skipped
200 conditional groups. Complain about this form if we're being
201 pedantic, but not if this is regurgitated input (preprocessed or
202 fed back in by the C++ frontend). */
Zack Weinberg041c3192000-07-04 01:58:21 +0000203 if (pfile->skipping || CPP_OPTION (pfile, lang_asm))
Zack Weinberg40c79d52000-01-12 00:35:36 +0000204 return 0;
205
Zack Weinberg041c3192000-07-04 01:58:21 +0000206 if (CPP_PEDANTIC (pfile) && CPP_BUFFER (pfile)->inc
207 && ! CPP_OPTION (pfile, preprocessed))
208 cpp_pedwarn (pfile, "# followed by integer");
Jeff Lawe9a25f71997-11-02 14:19:36 -0700209
Zack Weinberg041c3192000-07-04 01:58:21 +0000210 /* In -traditional mode, a directive is ignored unless its #
211 is in column 1. */
212 if (!bol && CPP_WTRADITIONAL (pfile))
213 cpp_warning (pfile, "traditional C ignores #%s with the # indented",
214 dtable[T_LINE].name);
215
216 if (!bol && CPP_TRADITIONAL (pfile))
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000217 return 0;
Zack Weinberg041c3192000-07-04 01:58:21 +0000218
219 return &dtable[T_LINE];
220}
Per Bothner7f2935c1995-03-16 13:59:07 -0800221
222static void
Zack Weinberg041c3192000-07-04 01:58:21 +0000223dump_macro_name (pfile, node)
Per Bothner7f2935c1995-03-16 13:59:07 -0800224 cpp_reader *pfile;
Zack Weinberg041c3192000-07-04 01:58:21 +0000225 cpp_hashnode *node;
Per Bothner7f2935c1995-03-16 13:59:07 -0800226{
Zack Weinberg041c3192000-07-04 01:58:21 +0000227 CPP_PUTS (pfile, "#define ", sizeof "#define " - 1);
228 CPP_PUTS (pfile, node->name, node->length);
229}
Per Bothner7f2935c1995-03-16 13:59:07 -0800230
Zack Weinberg041c3192000-07-04 01:58:21 +0000231/* Pass the current directive through to the output file. */
232static void
233pass_thru_directive (pfile)
234 cpp_reader *pfile;
235{
236 /* XXX This output may be genuinely needed even when there is no
237 printer. */
238 if (! pfile->printer)
239 return;
240 /* Flush first (temporary). */
241 cpp_output_tokens (pfile, pfile->printer, pfile->token_list.line);
242 _cpp_dump_list (pfile, &pfile->token_list, pfile->first_directive_token, 1);
243}
244
245static cpp_hashnode *
246get_define_node (pfile)
247 cpp_reader *pfile;
248{
249 cpp_hashnode *node;
250 const cpp_token *token;
Zack Weinberg041c3192000-07-04 01:58:21 +0000251
252 /* Skip any -C comments. */
253 while ((token = cpp_get_token (pfile))->type == CPP_COMMENT)
254 ;
255
256 if (token->type != CPP_NAME)
257 {
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000258 cpp_error_with_line (pfile, token->line, token->col,
Zack Weinberg041c3192000-07-04 01:58:21 +0000259 "macro names must be identifiers");
260 return 0;
261 }
262
263 /* That identifier is not allowed to be "defined". See predefined
264 macro names (6.10.8.4). */
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000265 node = token->val.node;
266
267 if (node == pfile->spec_nodes->n_defined)
Zack Weinberg041c3192000-07-04 01:58:21 +0000268 {
269 cpp_error_with_line (pfile, pfile->token_list.line, token->col,
270 "\"defined\" is not a legal macro name");
271 return 0;
272 }
273
Zack Weinberg041c3192000-07-04 01:58:21 +0000274 /* Check for poisoned identifiers now. */
275 if (node->type == T_POISON)
276 {
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000277 cpp_error (pfile, "attempt to use poisoned \"%s\"", node->name);
Zack Weinberg041c3192000-07-04 01:58:21 +0000278 return 0;
279 }
280
281 return node;
Per Bothner7f2935c1995-03-16 13:59:07 -0800282}
Per Bothner7f2935c1995-03-16 13:59:07 -0800283
Zack Weinberg168d3732000-03-14 06:34:11 +0000284/* Process a #define command. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800285static int
Zack Weinberg168d3732000-03-14 06:34:11 +0000286do_define (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800287 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800288{
Zack Weinbergf8f769e2000-05-28 05:56:38 +0000289 cpp_hashnode *node;
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000290
Zack Weinberg041c3192000-07-04 01:58:21 +0000291 if ((node = get_define_node (pfile)))
292 if (_cpp_create_definition (pfile, node))
293 {
294 if (CPP_OPTION (pfile, debug_output)
295 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
296 _cpp_dump_definition (pfile, node);
297 else if (CPP_OPTION (pfile, dump_macros) == dump_names)
298 dump_macro_name (pfile, node);
299 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800300 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -0800301}
302
Zack Weinberg041c3192000-07-04 01:58:21 +0000303/* Remove the definition of a symbol from the symbol table. */
304static int
305do_undef (pfile)
306 cpp_reader *pfile;
307{
308 cpp_hashnode *node = get_define_node (pfile);
309
310 if (cpp_get_token (pfile)->type != CPP_EOF)
311 cpp_pedwarn (pfile, "junk on line after #undef");
312
313 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
314 is not currently defined as a macro name. */
315 if (node && node->type != T_VOID)
316 {
317 /* If we are generating additional info for debugging (with -g) we
318 need to pass through all effective #undef commands. */
319 if (CPP_OPTION (pfile, debug_output)
320 || CPP_OPTION (pfile, dump_macros) == dump_definitions
321 || CPP_OPTION (pfile, dump_macros) == dump_names)
322 pass_thru_directive (pfile);
323
324 if (node->type != T_MACRO)
325 cpp_warning (pfile, "undefining \"%s\"", node->name);
326
327 _cpp_free_definition (node);
328 node->type = T_VOID;
329 }
330
331 return 0;
332}
333
334
Zack Weinberg45b966d2000-03-13 22:01:08 +0000335/* Handle #include and #import. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800336
Zack Weinberg041c3192000-07-04 01:58:21 +0000337static int
338parse_include (pfile, dir, trail, strp, lenp, abp)
Per Bothner7f2935c1995-03-16 13:59:07 -0800339 cpp_reader *pfile;
Zack Weinberg041c3192000-07-04 01:58:21 +0000340 const U_CHAR *dir;
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000341 int trail;
Zack Weinberg041c3192000-07-04 01:58:21 +0000342 const U_CHAR **strp;
343 unsigned int *lenp;
344 int *abp;
Per Bothner7f2935c1995-03-16 13:59:07 -0800345{
Zack Weinberg041c3192000-07-04 01:58:21 +0000346 const cpp_token *name = cpp_get_token (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -0800347
Zack Weinberg041c3192000-07-04 01:58:21 +0000348 if (name->type != CPP_STRING && name->type != CPP_HEADER_NAME)
Zack Weinberg168d3732000-03-14 06:34:11 +0000349 {
Zack Weinberg041c3192000-07-04 01:58:21 +0000350 if (name->type == CPP_LESS)
351 name = _cpp_glue_header_name (pfile);
352 else
353 {
354 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir);
355 return 1;
356 }
357 }
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000358 if (name->val.str.len == 0)
Zack Weinberg041c3192000-07-04 01:58:21 +0000359 {
360 cpp_error (pfile, "empty file name in #%s", dir);
361 return 1;
Richard Kennercfb3ee11997-04-13 14:30:13 -0400362 }
363
Zack Weinberg041c3192000-07-04 01:58:21 +0000364 if (!trail && cpp_get_token (pfile)->type != CPP_EOF)
365 cpp_error (pfile, "junk at end of #%s", dir);
Zack Weinberg168d3732000-03-14 06:34:11 +0000366
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000367 *lenp = name->val.str.len;
368 *strp = name->val.str.text;
Zack Weinberg041c3192000-07-04 01:58:21 +0000369 *abp = (name->type == CPP_HEADER_NAME);
370 return 0;
Zack Weinberg168d3732000-03-14 06:34:11 +0000371}
372
373static int
374do_include (pfile)
375 cpp_reader *pfile;
376{
377 unsigned int len;
Zack Weinberg041c3192000-07-04 01:58:21 +0000378 const U_CHAR *str;
379 int ab;
Zack Weinberg168d3732000-03-14 06:34:11 +0000380
Zack Weinberg041c3192000-07-04 01:58:21 +0000381 if (parse_include (pfile, dtable[T_INCLUDE].name, 0, &str, &len, &ab))
Zack Weinberg29a72a42000-03-28 21:45:02 +0000382 return 0;
Zack Weinberg168d3732000-03-14 06:34:11 +0000383
Zack Weinberg041c3192000-07-04 01:58:21 +0000384 _cpp_execute_include (pfile, str, len, 0, 0, ab);
385 if (CPP_OPTION (pfile, dump_includes))
386 pass_thru_directive (pfile);
Zack Weinberg168d3732000-03-14 06:34:11 +0000387 return 0;
388}
389
390static int
391do_import (pfile)
392 cpp_reader *pfile;
393{
394 unsigned int len;
Zack Weinberg041c3192000-07-04 01:58:21 +0000395 const U_CHAR *str;
396 int ab;
Zack Weinberg168d3732000-03-14 06:34:11 +0000397
Zack Weinbergae796972000-03-31 23:16:11 +0000398 if (CPP_OPTION (pfile, warn_import)
Zack Weinbergc31a6502000-06-21 18:33:51 +0000399 && !CPP_IN_SYSTEM_HEADER (pfile) && !pfile->import_warning)
Per Bothner7f2935c1995-03-16 13:59:07 -0800400 {
401 pfile->import_warning = 1;
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000402 cpp_warning (pfile,
403 "#import is obsolete, use an #ifndef wrapper in the header file");
Per Bothner7f2935c1995-03-16 13:59:07 -0800404 }
405
Zack Weinberg041c3192000-07-04 01:58:21 +0000406 if (parse_include (pfile, dtable[T_IMPORT].name, 0, &str, &len, &ab))
Zack Weinberg29a72a42000-03-28 21:45:02 +0000407 return 0;
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000408
Zack Weinberg041c3192000-07-04 01:58:21 +0000409 _cpp_execute_include (pfile, str, len, 1, 0, ab);
410 if (CPP_OPTION (pfile, dump_includes))
411 pass_thru_directive (pfile);
Zack Weinberg168d3732000-03-14 06:34:11 +0000412 return 0;
413}
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000414
Zack Weinberg168d3732000-03-14 06:34:11 +0000415static int
416do_include_next (pfile)
417 cpp_reader *pfile;
418{
419 unsigned int len;
Zack Weinberg041c3192000-07-04 01:58:21 +0000420 const U_CHAR *str;
Zack Weinberg168d3732000-03-14 06:34:11 +0000421 struct file_name_list *search_start = 0;
Zack Weinberg041c3192000-07-04 01:58:21 +0000422 int ab;
Zack Weinbergf1a86df1998-12-07 13:35:20 +0000423
Zack Weinberg041c3192000-07-04 01:58:21 +0000424 if (parse_include (pfile, dtable[T_INCLUDE_NEXT].name, 0, &str, &len, &ab))
Zack Weinberg29a72a42000-03-28 21:45:02 +0000425 return 0;
Zack Weinbergf1a86df1998-12-07 13:35:20 +0000426
Zack Weinberg041c3192000-07-04 01:58:21 +0000427 /* For #include_next, skip in the search path past the dir in which
428 the current file was found. If this is the last directory in the
429 search path, don't include anything. If the current file was
430 specified with an absolute path, use the normal search logic. If
431 this is the primary source file, use the normal search logic and
432 generate a warning. */
Zack Weinberg168d3732000-03-14 06:34:11 +0000433 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
Zack Weinberg692b8721998-12-16 13:23:47 +0000434 {
Zack Weinbergc31a6502000-06-21 18:33:51 +0000435 if (CPP_BUFFER (pfile)->inc->foundhere)
Zack Weinberg041c3192000-07-04 01:58:21 +0000436 {
437 search_start = CPP_BUFFER (pfile)->inc->foundhere->next;
438 if (!search_start)
439 return 0;
440 }
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000441 }
442 else
Zack Weinberg168d3732000-03-14 06:34:11 +0000443 cpp_warning (pfile, "#include_next in primary source file");
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000444
Zack Weinberg041c3192000-07-04 01:58:21 +0000445 _cpp_execute_include (pfile, str, len, 0, search_start, ab);
446 if (CPP_OPTION (pfile, dump_includes))
447 pass_thru_directive (pfile);
448
Per Bothner7f2935c1995-03-16 13:59:07 -0800449 return 0;
450}
451
Jason Merrilld3a34a01999-08-14 00:42:07 +0000452/* Subroutine of do_line. Read next token from PFILE without adding it to
453 the output buffer. If it is a number between 1 and 4, store it in *NUM
454 and return 1; otherwise, return 0 and complain if we aren't at the end
455 of the directive. */
456
457static int
458read_line_number (pfile, num)
459 cpp_reader *pfile;
460 int *num;
461{
Zack Weinberg041c3192000-07-04 01:58:21 +0000462 const cpp_token *tok = cpp_get_token (pfile);
463 enum cpp_ttype type = tok->type;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000464 const U_CHAR *p = tok->val.str.text;
465 unsigned int len = tok->val.str.len;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000466
Zack Weinberg041c3192000-07-04 01:58:21 +0000467 if (type == CPP_NUMBER && len == 1 && p[0] >= '1' && p[0] <= '4')
Jason Merrilld3a34a01999-08-14 00:42:07 +0000468 {
469 *num = p[0] - '0';
470 return 1;
471 }
472 else
473 {
Zack Weinbergcf00a882000-07-08 02:33:00 +0000474 if (type != CPP_EOF)
Zack Weinbergea4a4532000-05-29 16:19:32 +0000475 cpp_error (pfile, "invalid format #line");
Jason Merrilld3a34a01999-08-14 00:42:07 +0000476 return 0;
477 }
478}
479
Zack Weinberg041c3192000-07-04 01:58:21 +0000480/* Another subroutine of do_line. Convert a number in STR, of length
481 LEN, to binary; store it in NUMP, and return 0 if the number was
482 legal, 1 if not. Temporary, hopefully. */
483static int
484strtoul_for_line (str, len, nump)
485 const U_CHAR *str;
486 unsigned int len;
487 unsigned long *nump;
488{
489 unsigned long reg = 0;
490 U_CHAR c;
491 while (len--)
492 {
493 c = *str++;
494 if (!ISDIGIT (c))
495 return 1;
496 reg *= 10;
497 reg += c - '0';
498 }
499 *nump = reg;
500 return 0;
501}
502
Zack Weinberg5538ada1999-02-04 06:36:54 -0500503/* Interpret #line command.
504 Note that the filename string (if any) is treated as if it were an
505 include filename. That means no escape handling. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800506
507static int
Zack Weinberg168d3732000-03-14 06:34:11 +0000508do_line (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800509 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800510{
511 cpp_buffer *ip = CPP_BUFFER (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000512 unsigned long new_lineno, old_lineno;
513 /* C99 raised the minimum limit on #line numbers. */
514 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
515 int action_number = 0;
516 enum cpp_ttype type;
517 const U_CHAR *str;
518 char *fname;
519 unsigned int len;
520 const cpp_token *tok;
Per Bothner7f2935c1995-03-16 13:59:07 -0800521
Zack Weinberg041c3192000-07-04 01:58:21 +0000522 tok = cpp_get_token (pfile);
523 type = tok->type;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000524 str = tok->val.str.text;
525 len = tok->val.str.len;
Per Bothner7f2935c1995-03-16 13:59:07 -0800526
Zack Weinberg041c3192000-07-04 01:58:21 +0000527 if (type != CPP_NUMBER || strtoul_for_line (str, len, &new_lineno))
Per Bothner7f2935c1995-03-16 13:59:07 -0800528 {
Zack Weinberg041c3192000-07-04 01:58:21 +0000529 cpp_error (pfile, "token after #line is not a positive integer");
530 goto done;
Zack Weinberg5538ada1999-02-04 06:36:54 -0500531 }
Zack Weinberg5538ada1999-02-04 06:36:54 -0500532
Zack Weinberg041c3192000-07-04 01:58:21 +0000533 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
534 cpp_pedwarn (pfile, "line number out of range");
Zack Weinberg5538ada1999-02-04 06:36:54 -0500535
Zack Weinberg041c3192000-07-04 01:58:21 +0000536 old_lineno = ip->lineno;
537 ip->lineno = new_lineno;
538 tok = cpp_get_token (pfile);
539 type = tok->type;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000540 str = tok->val.str.text;
541 len = tok->val.str.len;
Zack Weinberg5538ada1999-02-04 06:36:54 -0500542
Zack Weinbergcf00a882000-07-08 02:33:00 +0000543 if (type == CPP_EOF)
Zack Weinberg041c3192000-07-04 01:58:21 +0000544 goto done;
545 else if (type != CPP_STRING)
Zack Weinberg5538ada1999-02-04 06:36:54 -0500546 {
Zack Weinbergea4a4532000-05-29 16:19:32 +0000547 cpp_error (pfile, "second token after #line is not a string");
Zack Weinberg041c3192000-07-04 01:58:21 +0000548 ip->lineno = old_lineno; /* malformed #line should have no effect */
549 goto done;
Per Bothner7f2935c1995-03-16 13:59:07 -0800550 }
551
Zack Weinberg041c3192000-07-04 01:58:21 +0000552 fname = alloca (len + 1);
553 memcpy (fname, str, len);
554 fname[len] = '\0';
555
556 if (strcmp (fname, ip->nominal_fname))
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000557 {
Zack Weinberg041c3192000-07-04 01:58:21 +0000558 if (!strcmp (fname, ip->inc->name))
559 ip->nominal_fname = ip->inc->name;
560 else
561 ip->nominal_fname = _cpp_fake_include (pfile, fname);
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000562 }
Zack Weinberg941e09b1998-12-15 11:17:06 +0000563
Zack Weinberg041c3192000-07-04 01:58:21 +0000564 if (read_line_number (pfile, &action_number) == 0)
565 return 0;
Zack Weinbergff2b53e2000-04-06 07:56:14 +0000566
Zack Weinberg041c3192000-07-04 01:58:21 +0000567 if (CPP_PEDANTIC (pfile))
568 cpp_pedwarn (pfile, "garbage at end of #line");
Per Bothner7f2935c1995-03-16 13:59:07 -0800569
Zack Weinberg041c3192000-07-04 01:58:21 +0000570 /* This is somewhat questionable: change the buffer stack
571 depth so that output_line_command thinks we've stacked
572 another buffer. */
573 if (action_number == 1)
Per Bothner7f2935c1995-03-16 13:59:07 -0800574 {
Zack Weinberg041c3192000-07-04 01:58:21 +0000575 pfile->buffer_stack_depth++;
576 cpp_make_system_header (pfile, ip, 0);
577 read_line_number (pfile, &action_number);
Per Bothner7f2935c1995-03-16 13:59:07 -0800578 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000579 else if (action_number == 2)
580 {
581 pfile->buffer_stack_depth--;
582 cpp_make_system_header (pfile, ip, 0);
583 read_line_number (pfile, &action_number);
584 }
585 if (action_number == 3)
586 {
587 cpp_make_system_header (pfile, ip, 1);
588 read_line_number (pfile, &action_number);
589 }
590 if (action_number == 4)
591 {
592 cpp_make_system_header (pfile, ip, 2);
593 read_line_number (pfile, &action_number);
594 }
595 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -0800596
Zack Weinberg041c3192000-07-04 01:58:21 +0000597 done:
Per Bothner7f2935c1995-03-16 13:59:07 -0800598 return 0;
599}
Zack Weinberg941e09b1998-12-15 11:17:06 +0000600
Per Bothner7f2935c1995-03-16 13:59:07 -0800601/*
602 * Report an error detected by the program we are processing.
603 * Use the text of the line in the error message.
604 * (We use error because it prints the filename & line#.)
605 */
606
607static int
Zack Weinberg168d3732000-03-14 06:34:11 +0000608do_error (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800609 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800610{
Zack Weinberg041c3192000-07-04 01:58:21 +0000611 U_CHAR *text, *limit;
Zack Weinberg941e09b1998-12-15 11:17:06 +0000612
Zack Weinberg041c3192000-07-04 01:58:21 +0000613 text = pfile->limit;
614 _cpp_dump_list (pfile, &pfile->token_list, pfile->first_directive_token, 0);
615 limit = pfile->limit;
616 pfile->limit = text;
617 cpp_error (pfile, "%.*s", (int)(limit - text), text);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000618
Per Bothner7f2935c1995-03-16 13:59:07 -0800619 return 0;
620}
621
622/*
623 * Report a warning detected by the program we are processing.
624 * Use the text of the line in the warning message, then continue.
Per Bothner7f2935c1995-03-16 13:59:07 -0800625 */
626
627static int
Zack Weinberg168d3732000-03-14 06:34:11 +0000628do_warning (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800629 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800630{
Zack Weinberg041c3192000-07-04 01:58:21 +0000631 U_CHAR *text, *limit;
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000632
Zack Weinberg041c3192000-07-04 01:58:21 +0000633 text = pfile->limit;
634 _cpp_dump_list (pfile, &pfile->token_list, pfile->first_directive_token, 0);
635 limit = pfile->limit;
636 pfile->limit = text;
637 cpp_warning (pfile, "%.*s", (int)(limit - text), text);
Per Bothner7f2935c1995-03-16 13:59:07 -0800638 return 0;
639}
640
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000641/* Report program identification. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800642
643static int
Zack Weinberg168d3732000-03-14 06:34:11 +0000644do_ident (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800645 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800646{
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000647 /* Next token should be a string constant. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000648 if (cpp_get_token (pfile)->type == CPP_STRING)
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000649 /* And then a newline. */
Zack Weinbergc71f8352000-07-05 05:33:57 +0000650 if (cpp_get_token (pfile)->type == CPP_EOF)
Zack Weinberg041c3192000-07-04 01:58:21 +0000651 {
652 /* Good - ship it. */
653 pass_thru_directive (pfile);
654 return 0;
655 }
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000656
657 cpp_error (pfile, "invalid #ident");
Per Bothner7f2935c1995-03-16 13:59:07 -0800658 return 0;
659}
660
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000661/* Pragmata handling. We handle some of these, and pass the rest on
662 to the front end. C99 defines three pragmas and says that no macro
663 expansion is to be performed on them; whether or not macro
664 expansion happens for other pragmas is implementation defined.
665 This implementation never macro-expands the text after #pragma.
666
667 We currently do not support the _Pragma operator. Support for that
668 has to be coordinated with the front end. Proposed implementation:
669 both #pragma blah blah and _Pragma("blah blah") become
670 __builtin_pragma(blah blah) and we teach the parser about that. */
671
672/* Sub-handlers for the pragmas needing treatment here.
673 They return 1 if the token buffer is to be popped, 0 if not. */
Nathan Sidwell82443372000-06-23 10:56:09 +0000674struct pragma_entry
675{
Zack Weinberg041c3192000-07-04 01:58:21 +0000676 const char *name;
Nathan Sidwell82443372000-06-23 10:56:09 +0000677 int (*handler) PARAMS ((cpp_reader *));
678};
679
680static int pragma_dispatch
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000681 PARAMS ((cpp_reader *, const struct pragma_entry *, const cpp_hashnode *));
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000682static int do_pragma_once PARAMS ((cpp_reader *));
683static int do_pragma_implementation PARAMS ((cpp_reader *));
684static int do_pragma_poison PARAMS ((cpp_reader *));
Zack Weinberg2c0b35c2000-05-17 18:07:16 +0000685static int do_pragma_system_header PARAMS ((cpp_reader *));
Nathan Sidwell82443372000-06-23 10:56:09 +0000686static int do_pragma_gcc PARAMS ((cpp_reader *));
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000687static int do_pragma_dependency PARAMS ((cpp_reader *));
Nathan Sidwell82443372000-06-23 10:56:09 +0000688
689static const struct pragma_entry top_pragmas[] =
690{
691 {"once", do_pragma_once},
692 {"implementation", do_pragma_implementation},
693 {"poison", do_pragma_poison},
Nathan Sidwell82443372000-06-23 10:56:09 +0000694 {"GCC", do_pragma_gcc},
Zack Weinberg041c3192000-07-04 01:58:21 +0000695 {NULL, NULL}
Nathan Sidwell82443372000-06-23 10:56:09 +0000696};
697
698static const struct pragma_entry gcc_pragmas[] =
699{
700 {"implementation", do_pragma_implementation},
701 {"poison", do_pragma_poison},
702 {"system_header", do_pragma_system_header},
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000703 {"dependency", do_pragma_dependency},
Zack Weinberg041c3192000-07-04 01:58:21 +0000704 {NULL, NULL}
Nathan Sidwell82443372000-06-23 10:56:09 +0000705};
706
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000707static int pragma_dispatch (pfile, table, node)
Nathan Sidwell82443372000-06-23 10:56:09 +0000708 cpp_reader *pfile;
709 const struct pragma_entry *table;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000710 const cpp_hashnode *node;
Nathan Sidwell82443372000-06-23 10:56:09 +0000711{
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000712 const U_CHAR *p = node->name;
713 size_t len = node->length;
714
Nathan Sidwell82443372000-06-23 10:56:09 +0000715 for (; table->name; table++)
716 if (strlen (table->name) == len && !memcmp (p, table->name, len))
717 return (*table->handler) (pfile);
Zack Weinberg041c3192000-07-04 01:58:21 +0000718 return 0;
Nathan Sidwell82443372000-06-23 10:56:09 +0000719}
Per Bothner7f2935c1995-03-16 13:59:07 -0800720
721static int
Zack Weinberg168d3732000-03-14 06:34:11 +0000722do_pragma (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800723 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800724{
Zack Weinberg041c3192000-07-04 01:58:21 +0000725 const cpp_token *tok;
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000726 int pop;
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000727
Zack Weinberg041c3192000-07-04 01:58:21 +0000728 tok = cpp_get_token (pfile);
729 if (tok->type == CPP_EOF)
730 return 0;
731 else if (tok->type != CPP_NAME)
Alexandre Oliva0172e2b2000-02-27 06:24:27 +0000732 {
Zack Weinberg041c3192000-07-04 01:58:21 +0000733 cpp_error (pfile, "malformed #pragma directive");
734 return 0;
Alexandre Oliva0172e2b2000-02-27 06:24:27 +0000735 }
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000736
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000737 pop = pragma_dispatch (pfile, top_pragmas, tok->val.node);
Zack Weinberg041c3192000-07-04 01:58:21 +0000738 if (!pop)
739 pass_thru_directive (pfile);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000740 return 0;
741}
742
743static int
Nathan Sidwell82443372000-06-23 10:56:09 +0000744do_pragma_gcc (pfile)
745 cpp_reader *pfile;
746{
Zack Weinberg041c3192000-07-04 01:58:21 +0000747 const cpp_token *tok;
Nathan Sidwell82443372000-06-23 10:56:09 +0000748
Zack Weinberg041c3192000-07-04 01:58:21 +0000749 tok = cpp_get_token (pfile);
750 if (tok->type == CPP_EOF)
751 return 1;
752 else if (tok->type != CPP_NAME)
753 return 0;
Nathan Sidwell82443372000-06-23 10:56:09 +0000754
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000755 return pragma_dispatch (pfile, gcc_pragmas, tok->val.node);
Nathan Sidwell82443372000-06-23 10:56:09 +0000756}
757
758static int
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000759do_pragma_once (pfile)
760 cpp_reader *pfile;
761{
762 cpp_buffer *ip = CPP_BUFFER (pfile);
763
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000764 /* Allow #pragma once in system headers, since that's not the user's
765 fault. */
Zack Weinbergc31a6502000-06-21 18:33:51 +0000766 if (!CPP_IN_SYSTEM_HEADER (pfile))
Zack Weinbergea4a4532000-05-29 16:19:32 +0000767 cpp_warning (pfile, "#pragma once is obsolete");
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000768
Zack Weinberg38b24ee2000-03-08 20:37:23 +0000769 if (CPP_PREV_BUFFER (ip) == NULL)
Zack Weinbergea4a4532000-05-29 16:19:32 +0000770 cpp_warning (pfile, "#pragma once outside include file");
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000771 else
Zack Weinbergc31a6502000-06-21 18:33:51 +0000772 ip->inc->cmacro = NEVER_REREAD;
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000773
774 return 1;
775}
776
777static int
778do_pragma_implementation (pfile)
779 cpp_reader *pfile;
780{
781 /* Be quiet about `#pragma implementation' for a file only if it hasn't
782 been included yet. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000783 const cpp_token *tok = cpp_get_token (pfile);
Zack Weinberg12cf91f2000-05-04 04:38:01 +0000784 char *copy;
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000785
Zack Weinberg041c3192000-07-04 01:58:21 +0000786 if (tok->type == CPP_EOF)
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000787 return 0;
Zack Weinberg041c3192000-07-04 01:58:21 +0000788 else if (tok->type != CPP_STRING
789 || cpp_get_token (pfile)->type != CPP_EOF)
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000790 {
791 cpp_error (pfile, "malformed #pragma implementation");
792 return 1;
793 }
794
Zack Weinberg041c3192000-07-04 01:58:21 +0000795 /* Make a NUL-terminated copy of the string. */
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000796 copy = alloca (tok->val.str.len + 1);
797 memcpy (copy, tok->val.str.text, tok->val.str.len);
798 copy[tok->val.str.len] = '\0';
Zack Weinbergd35364d2000-03-12 23:46:05 +0000799
Zack Weinbergb0699da2000-03-07 20:58:47 +0000800 if (cpp_included (pfile, copy))
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000801 cpp_warning (pfile,
Zack Weinbergea4a4532000-05-29 16:19:32 +0000802 "#pragma implementation for %s appears after file is included",
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000803 copy);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000804 return 0;
805}
806
807static int
808do_pragma_poison (pfile)
809 cpp_reader *pfile;
810{
811 /* Poison these symbols so that all subsequent usage produces an
812 error message. */
Zack Weinberg041c3192000-07-04 01:58:21 +0000813 const cpp_token *tok;
Zack Weinbergf8f769e2000-05-28 05:56:38 +0000814 cpp_hashnode *hp;
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000815 int writeit;
Zack Weinbergd35364d2000-03-12 23:46:05 +0000816
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000817 /* As a rule, don't include #pragma poison commands in output,
818 unless the user asks for them. */
Zack Weinbergae796972000-03-31 23:16:11 +0000819 writeit = (CPP_OPTION (pfile, debug_output)
820 || CPP_OPTION (pfile, dump_macros) == dump_definitions
821 || CPP_OPTION (pfile, dump_macros) == dump_names);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000822
823 for (;;)
824 {
Zack Weinberg041c3192000-07-04 01:58:21 +0000825 tok = cpp_get_token (pfile);
826 if (tok->type == CPP_EOF)
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000827 break;
Zack Weinberg041c3192000-07-04 01:58:21 +0000828 if (tok->type != CPP_NAME)
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000829 {
830 cpp_error (pfile, "invalid #pragma poison directive");
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000831 return 1;
832 }
833
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000834 hp = tok->val.node;
Zack Weinberga7abcbb2000-05-01 18:20:10 +0000835 if (hp->type == T_POISON)
836 ; /* It is allowed to poison the same identifier twice. */
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000837 else
Zack Weinbergd35364d2000-03-12 23:46:05 +0000838 {
Zack Weinberga7abcbb2000-05-01 18:20:10 +0000839 if (hp->type != T_VOID)
Zack Weinberg041c3192000-07-04 01:58:21 +0000840 cpp_warning (pfile, "poisoning existing macro \"%s\"", hp->name);
Zack Weinberga7abcbb2000-05-01 18:20:10 +0000841 _cpp_free_definition (hp);
842 hp->type = T_POISON;
Zack Weinbergd35364d2000-03-12 23:46:05 +0000843 }
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000844 }
845 return !writeit;
846}
Zack Weinberg2c0b35c2000-05-17 18:07:16 +0000847
848/* Mark the current header as a system header. This will suppress
849 some categories of warnings (notably those from -pedantic). It is
850 intended for use in system libraries that cannot be implemented in
851 conforming C, but cannot be certain that their headers appear in a
852 system include directory. To prevent abuse, it is rejected in the
853 primary source file. */
854static int
855do_pragma_system_header (pfile)
856 cpp_reader *pfile;
857{
Zack Weinberg041c3192000-07-04 01:58:21 +0000858 cpp_buffer *ip = CPP_BUFFER (pfile);
Zack Weinberg2c0b35c2000-05-17 18:07:16 +0000859 if (CPP_PREV_BUFFER (ip) == NULL)
860 cpp_warning (pfile, "#pragma system_header outside include file");
861 else
Zack Weinberge605b042000-06-21 23:08:17 +0000862 cpp_make_system_header (pfile, ip, 1);
Zack Weinberg2c0b35c2000-05-17 18:07:16 +0000863
864 return 1;
865}
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000866
867/* Check the modified date of the current include file against a specified
868 file. Issue a diagnostic, if the specified file is newer. We use this to
869 determine if a fixed header should be refixed. */
870static int
871do_pragma_dependency (pfile)
872 cpp_reader *pfile;
873{
Zack Weinberg041c3192000-07-04 01:58:21 +0000874 const U_CHAR *name;
875 unsigned int len;
876 int ordering, ab;
877 char left, right;
878
879 if (parse_include (pfile, U"pragma dependency", 1, &name, &len, &ab))
880 return 1;
881
882 left = ab ? '<' : '"';
883 right = ab ? '>' : '"';
884
885 ordering = _cpp_compare_file_date (pfile, name, len, ab);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000886 if (ordering < 0)
Zack Weinberg041c3192000-07-04 01:58:21 +0000887 cpp_warning (pfile, "cannot find source %c%s%c", left, name, right);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000888 else if (ordering > 0)
889 {
Zack Weinberg041c3192000-07-04 01:58:21 +0000890 const cpp_token *msg = cpp_get_token (pfile);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000891
Zack Weinberg041c3192000-07-04 01:58:21 +0000892 cpp_warning (pfile, "current file is older than %c%s%c",
893 left, name, right);
894 if (msg->type != CPP_EOF)
895 {
896 U_CHAR *text, *limit;
897
898 text = pfile->limit;
899 _cpp_dump_list (pfile, &pfile->token_list, msg, 0);
900 limit = pfile->limit;
901 pfile->limit = text;
902 cpp_warning (pfile, "%.*s", (int)(limit - text), text);
903 }
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000904 }
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000905 return 1;
906}
907
Per Bothner7f2935c1995-03-16 13:59:07 -0800908/* Just ignore #sccs, on systems where we define it at all. */
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000909#ifdef SCCS_DIRECTIVE
Per Bothner7f2935c1995-03-16 13:59:07 -0800910static int
Zack Weinberg168d3732000-03-14 06:34:11 +0000911do_sccs (pfile)
Zack Weinberg041c3192000-07-04 01:58:21 +0000912 cpp_reader *pfile ATTRIBUTE_UNUSED;
Per Bothner7f2935c1995-03-16 13:59:07 -0800913{
Per Bothner7f2935c1995-03-16 13:59:07 -0800914 return 0;
915}
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000916#endif
Jim Meyering1d0e51b1999-08-25 22:01:36 +0000917
918/* We've found an `#if' directive. If the only thing before it in
919 this file is white space, and if it is of the form
920 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
921 for inclusion of this file. (See redundant_include_p in cppfiles.c
Zack Weinbergea4a4532000-05-29 16:19:32 +0000922 for an explanation of controlling macros.) If so, return the
923 hash node for SYMBOL. Otherwise, return NULL. */
Jim Meyering1d0e51b1999-08-25 22:01:36 +0000924
Zack Weinbergea4a4532000-05-29 16:19:32 +0000925static const cpp_hashnode *
Jim Meyering1d0e51b1999-08-25 22:01:36 +0000926detect_if_not_defined (pfile)
927 cpp_reader *pfile;
928{
Zack Weinberg041c3192000-07-04 01:58:21 +0000929 const cpp_token *token;
930 cpp_hashnode *cmacro = 0;
Jim Meyering1d0e51b1999-08-25 22:01:36 +0000931
Zack Weinberg041c3192000-07-04 01:58:21 +0000932 /* We are guaranteed that tokens are consecutive and end in CPP_EOF. */
933 token = pfile->first_directive_token + 2;
Zack Weinberg9cc6e052000-04-18 06:43:41 +0000934
Zack Weinberg041c3192000-07-04 01:58:21 +0000935 if (token->type != CPP_NOT)
936 return 0;
Zack Weinberg9cc6e052000-04-18 06:43:41 +0000937
Zack Weinberg041c3192000-07-04 01:58:21 +0000938 token++;
939 if (token->type != CPP_NAME
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000940 || token->val.node != pfile->spec_nodes->n_defined)
Zack Weinberg041c3192000-07-04 01:58:21 +0000941 return 0;
Zack Weinberg9cc6e052000-04-18 06:43:41 +0000942
Zack Weinberg041c3192000-07-04 01:58:21 +0000943 token++;
944 if (token->type == CPP_OPEN_PAREN)
945 token++;
Zack Weinberg9cc6e052000-04-18 06:43:41 +0000946
Zack Weinberg041c3192000-07-04 01:58:21 +0000947 if (token->type != CPP_NAME)
948 return 0;
949
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000950 cmacro = token->val.node;
Zack Weinberg041c3192000-07-04 01:58:21 +0000951
952 if (token[-1].type == CPP_OPEN_PAREN)
Jim Meyering1d0e51b1999-08-25 22:01:36 +0000953 {
Zack Weinberg041c3192000-07-04 01:58:21 +0000954 token++;
955 if (token->type != CPP_CLOSE_PAREN)
956 return 0;
Jim Meyering1d0e51b1999-08-25 22:01:36 +0000957 }
Zack Weinberg9cc6e052000-04-18 06:43:41 +0000958
Zack Weinberg041c3192000-07-04 01:58:21 +0000959 token++;
960 if (token->type != CPP_EOF)
961 return 0;
Jim Meyering1d0e51b1999-08-25 22:01:36 +0000962
Zack Weinbergea4a4532000-05-29 16:19:32 +0000963 return cmacro;
Per Bothner7f2935c1995-03-16 13:59:07 -0800964}
965
Zack Weinberg041c3192000-07-04 01:58:21 +0000966/* Parse an #ifdef or #ifndef directive. Returns the hash node of the
967 macro being tested, and issues various error messages. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800968
Zack Weinbergea4a4532000-05-29 16:19:32 +0000969static const cpp_hashnode *
Zack Weinberg168d3732000-03-14 06:34:11 +0000970parse_ifdef (pfile, name)
Per Bothner7f2935c1995-03-16 13:59:07 -0800971 cpp_reader *pfile;
Zack Weinberg12cf91f2000-05-04 04:38:01 +0000972 const U_CHAR *name;
Per Bothner7f2935c1995-03-16 13:59:07 -0800973{
Zack Weinberg041c3192000-07-04 01:58:21 +0000974 enum cpp_ttype type;
Zack Weinbergea4a4532000-05-29 16:19:32 +0000975 const cpp_hashnode *node = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -0800976
Zack Weinberg041c3192000-07-04 01:58:21 +0000977 const cpp_token *token = cpp_get_token (pfile);
978 type = token->type;
Per Bothner7f2935c1995-03-16 13:59:07 -0800979
980 if (!CPP_TRADITIONAL (pfile))
Zack Weinberg168d3732000-03-14 06:34:11 +0000981 {
Zack Weinberg041c3192000-07-04 01:58:21 +0000982 if (type == CPP_EOF)
983 cpp_pedwarn (pfile, "#%s with no argument", name);
984 else if (type != CPP_NAME)
985 cpp_pedwarn (pfile, "#%s with invalid argument", name);
986 else if (cpp_get_token (pfile)->type != CPP_EOF)
987 cpp_pedwarn (pfile, "garbage at end of #%s", name);
Per Bothner7f2935c1995-03-16 13:59:07 -0800988 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000989
990 if (type == CPP_NAME)
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000991 node = token->val.node;
Zack Weinberg041c3192000-07-04 01:58:21 +0000992 if (node && node->type == T_POISON)
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000993 {
994 cpp_error (pfile, "attempt to use poisoned identifier \"%s\"",
995 node->name);
996 node = 0;
997 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000998
Zack Weinbergea4a4532000-05-29 16:19:32 +0000999 return node;
Zack Weinberg168d3732000-03-14 06:34:11 +00001000}
Per Bothner7f2935c1995-03-16 13:59:07 -08001001
Zack Weinberg168d3732000-03-14 06:34:11 +00001002/* #ifdef is dead simple. */
1003
1004static int
1005do_ifdef (pfile)
1006 cpp_reader *pfile;
1007{
Zack Weinberg041c3192000-07-04 01:58:21 +00001008 const cpp_hashnode *node = 0;
1009
1010 if (! pfile->skipping)
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001011 node = parse_ifdef (pfile, dtable[T_IFDEF].name);
Zack Weinberg041c3192000-07-04 01:58:21 +00001012
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001013 push_conditional (pfile, !(node && node->type != T_VOID), T_IFDEF, 0);
Zack Weinbergea4a4532000-05-29 16:19:32 +00001014 return 0;
Zack Weinberg168d3732000-03-14 06:34:11 +00001015}
1016
1017/* #ifndef is a tad more complex, because we need to check for a
1018 no-reinclusion wrapper. */
1019
1020static int
1021do_ifndef (pfile)
1022 cpp_reader *pfile;
1023{
Zack Weinberg041c3192000-07-04 01:58:21 +00001024 int start_of_file = 0;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001025 const cpp_hashnode *node = 0;
Zack Weinberg168d3732000-03-14 06:34:11 +00001026
Zack Weinberg041c3192000-07-04 01:58:21 +00001027 if (! pfile->skipping)
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001028 {
Zack Weinberg041c3192000-07-04 01:58:21 +00001029 start_of_file = (pfile->token_list.flags & BEG_OF_FILE);
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001030 node = parse_ifdef (pfile, dtable[T_IFNDEF].name);
Jakub Jelinek5af7e2c2000-06-08 00:27:57 +02001031 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001032
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001033 push_conditional (pfile, node && node->type != T_VOID,
1034 T_IFNDEF, start_of_file ? node : 0);
Zack Weinbergea4a4532000-05-29 16:19:32 +00001035 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001036}
1037
Zack Weinbergea4a4532000-05-29 16:19:32 +00001038/* #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1039 Also, check for a reinclude preventer of the form #if !defined (MACRO). */
Per Bothner7f2935c1995-03-16 13:59:07 -08001040
Zack Weinbergff2b53e2000-04-06 07:56:14 +00001041static int
Zack Weinbergea4a4532000-05-29 16:19:32 +00001042do_if (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001043 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001044{
Zack Weinbergea4a4532000-05-29 16:19:32 +00001045 const cpp_hashnode *cmacro = 0;
1046 int value = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001047
Zack Weinbergea4a4532000-05-29 16:19:32 +00001048 if (! pfile->skipping)
1049 {
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001050 if (pfile->token_list.flags & BEG_OF_FILE)
1051 cmacro = detect_if_not_defined (pfile);
Zack Weinbergea4a4532000-05-29 16:19:32 +00001052 value = _cpp_parse_expr (pfile);
Zack Weinbergea4a4532000-05-29 16:19:32 +00001053 }
1054 push_conditional (pfile, value == 0, T_IF, cmacro);
Zack Weinbergff2b53e2000-04-06 07:56:14 +00001055 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001056}
1057
Zack Weinbergea4a4532000-05-29 16:19:32 +00001058/* #else flips pfile->skipping and continues without changing
1059 if_stack; this is so that the error message for missing #endif's
1060 etc. will point to the original #if. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001061
1062static int
Zack Weinberg168d3732000-03-14 06:34:11 +00001063do_else (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001064 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001065{
Zack Weinbergea4a4532000-05-29 16:19:32 +00001066 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001067 validate_else (pfile, dtable[T_ELSE].name);
1068
1069 if (ifs == NULL)
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001070 {
Zack Weinbergea4a4532000-05-29 16:19:32 +00001071 cpp_error (pfile, "#else without #if");
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001072 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001073 }
Zack Weinbergea4a4532000-05-29 16:19:32 +00001074 if (ifs->type == T_ELSE)
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001075 {
Zack Weinbergea4a4532000-05-29 16:19:32 +00001076 cpp_error (pfile, "#else after #else");
Zack Weinberg041c3192000-07-04 01:58:21 +00001077 cpp_error_with_line (pfile, ifs->lineno, ifs->colno,
1078 "the conditional began here");
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001079 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001080
Zack Weinbergea4a4532000-05-29 16:19:32 +00001081 /* #ifndef can't have its special treatment for containing the whole file
1082 if it has a #else clause. */
1083 ifs->cmacro = 0;
Zack Weinbergea4a4532000-05-29 16:19:32 +00001084 ifs->type = T_ELSE;
1085 if (! ifs->was_skipping)
1086 {
1087 /* If pfile->skipping is 2, one of the blocks in an #if/#elif/... chain
1088 succeeded, so we mustn't do the else block. */
1089 if (pfile->skipping < 2)
1090 pfile->skipping = ! pfile->skipping;
1091 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001092 return 0;
1093}
1094
1095/*
Zack Weinbergea4a4532000-05-29 16:19:32 +00001096 * handle a #elif directive by not changing if_stack either.
1097 * see the comment above do_else.
Per Bothner7f2935c1995-03-16 13:59:07 -08001098 */
1099
1100static int
Zack Weinbergea4a4532000-05-29 16:19:32 +00001101do_elif (pfile)
1102 cpp_reader *pfile;
1103{
1104 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1105
1106 if (ifs == NULL)
1107 {
1108 cpp_error (pfile, "#elif without #if");
1109 return 0;
1110 }
1111 if (ifs->type == T_ELSE)
1112 {
1113 cpp_error (pfile, "#elif after #else");
Zack Weinberg041c3192000-07-04 01:58:21 +00001114 cpp_error_with_line (pfile, ifs->lineno, ifs->colno,
1115 "the conditional began here");
Zack Weinbergea4a4532000-05-29 16:19:32 +00001116 }
1117
1118 ifs->type = T_ELIF;
1119 if (ifs->was_skipping)
Zack Weinberg041c3192000-07-04 01:58:21 +00001120 return 0; /* Don't evaluate a nested #if */
Zack Weinbergea4a4532000-05-29 16:19:32 +00001121
Zack Weinberg041c3192000-07-04 01:58:21 +00001122 if (pfile->skipping != 1)
1123 {
1124 pfile->skipping = 2; /* one block succeeded, so don't do any others */
1125 return 0;
1126 }
1127
1128 pfile->skipping = ! _cpp_parse_expr (pfile);
Zack Weinbergea4a4532000-05-29 16:19:32 +00001129 return 0;
1130}
1131
Zack Weinbergea4a4532000-05-29 16:19:32 +00001132/* #endif pops the if stack and resets pfile->skipping. */
1133
1134static int
Zack Weinberg168d3732000-03-14 06:34:11 +00001135do_endif (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001136 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001137{
Zack Weinbergea4a4532000-05-29 16:19:32 +00001138 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
Per Bothner7f2935c1995-03-16 13:59:07 -08001139
Zack Weinbergea4a4532000-05-29 16:19:32 +00001140 validate_else (pfile, dtable[T_ENDIF].name);
1141
1142 if (ifs == NULL)
1143 cpp_error (pfile, "#endif without #if");
Per Bothner7f2935c1995-03-16 13:59:07 -08001144 else
1145 {
Zack Weinbergea4a4532000-05-29 16:19:32 +00001146 CPP_BUFFER (pfile)->if_stack = ifs->next;
1147 pfile->skipping = ifs->was_skipping;
1148 pfile->potential_control_macro = ifs->cmacro;
Zack Weinbergc71f8352000-07-05 05:33:57 +00001149 obstack_free (pfile->buffer_ob, ifs);
Per Bothner7f2935c1995-03-16 13:59:07 -08001150 }
1151 return 0;
1152}
1153
Zack Weinberg041c3192000-07-04 01:58:21 +00001154
Zack Weinbergea4a4532000-05-29 16:19:32 +00001155/* Push an if_stack entry and set pfile->skipping accordingly.
1156 If this is a #ifndef starting at the beginning of a file,
1157 CMACRO is the macro name tested by the #ifndef. */
1158
1159static void
1160push_conditional (pfile, skip, type, cmacro)
1161 cpp_reader *pfile;
1162 int skip;
1163 int type;
1164 const cpp_hashnode *cmacro;
1165{
1166 struct if_stack *ifs;
1167
Zack Weinbergc71f8352000-07-05 05:33:57 +00001168 ifs = xobnew (pfile->buffer_ob, struct if_stack);
Zack Weinberg041c3192000-07-04 01:58:21 +00001169 ifs->lineno = _cpp_get_line (pfile, &ifs->colno);
Zack Weinbergea4a4532000-05-29 16:19:32 +00001170 ifs->next = CPP_BUFFER (pfile)->if_stack;
1171 ifs->cmacro = cmacro;
1172 ifs->was_skipping = pfile->skipping;
1173 ifs->type = type;
1174
1175 if (!pfile->skipping)
1176 pfile->skipping = skip;
1177
1178 CPP_BUFFER (pfile)->if_stack = ifs;
1179}
1180
Zack Weinberg75ec21d2000-01-27 22:29:07 +00001181/* Issue -pedantic warning for text which is not a comment following
Zack Weinbergea4a4532000-05-29 16:19:32 +00001182 an #else or #endif. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001183
1184static void
1185validate_else (pfile, directive)
1186 cpp_reader *pfile;
Zack Weinberg12cf91f2000-05-04 04:38:01 +00001187 const U_CHAR *directive;
Per Bothner7f2935c1995-03-16 13:59:07 -08001188{
Zack Weinberg041c3192000-07-04 01:58:21 +00001189 if (CPP_PEDANTIC (pfile) && cpp_get_token (pfile)->type != CPP_EOF)
1190 cpp_pedwarn (pfile, "ISO C forbids text after #%s", directive);
Zack Weinberg6ee2c971999-09-11 05:38:06 +00001191}
1192
Zack Weinberg041c3192000-07-04 01:58:21 +00001193/* Called when we reach the end of a file. Walk back up the
Zack Weinbergc56c2072000-05-02 16:09:12 +00001194 conditional stack till we reach its level at entry to this file,
Zack Weinbergea4a4532000-05-29 16:19:32 +00001195 issuing error messages. Then force skipping off. */
Zack Weinbergc71f8352000-07-05 05:33:57 +00001196static void
1197unwind_if_stack (pfile, pbuf)
Per Bothner7f2935c1995-03-16 13:59:07 -08001198 cpp_reader *pfile;
Zack Weinbergc56c2072000-05-02 16:09:12 +00001199 cpp_buffer *pbuf;
Per Bothner7f2935c1995-03-16 13:59:07 -08001200{
Zack Weinberg45b966d2000-03-13 22:01:08 +00001201 struct if_stack *ifs, *nifs;
Zack Weinberg4d9a1b41999-02-15 14:04:21 +00001202
Zack Weinbergea4a4532000-05-29 16:19:32 +00001203 for (ifs = pbuf->if_stack; ifs; ifs = nifs)
Per Bothner7f2935c1995-03-16 13:59:07 -08001204 {
Zack Weinberg041c3192000-07-04 01:58:21 +00001205 cpp_error_with_line (pfile, ifs->lineno, ifs->colno, "unterminated #%s",
Zack Weinberg168d3732000-03-14 06:34:11 +00001206 dtable[ifs->type].name);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001207 nifs = ifs->next;
Zack Weinbergc71f8352000-07-05 05:33:57 +00001208 /* No need to free - they'll all go away with the buffer. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001209 }
Zack Weinbergea4a4532000-05-29 16:19:32 +00001210 pfile->skipping = 0;
Zack Weinberg7061aa51998-12-15 11:09:16 +00001211}
1212
Zack Weinberg041c3192000-07-04 01:58:21 +00001213/* Parses an assertion, returning a pointer to the hash node of the
1214 predicate, or 0 on error. If an answer was supplied, it is
1215 allocated and placed in ANSWERP, otherwise it is set to 0. We use
1216 _cpp_get_raw_token, since we cannot assume tokens are consecutive
1217 in a #if statement (we may be in a macro), and we don't want to
1218 macro expand. */
1219cpp_hashnode *
1220_cpp_parse_assertion (pfile, answerp)
1221 cpp_reader *pfile;
1222 struct answer **answerp;
1223{
1224 struct answer *answer = 0;
1225 cpp_toklist *list;
1226 U_CHAR *sym;
1227 const cpp_token *token, *predicate;
1228 const struct directive *d = pfile->token_list.directive;
1229 unsigned int len = 0;
1230
1231 predicate = _cpp_get_raw_token (pfile);
1232 if (predicate->type == CPP_EOF)
1233 {
1234 cpp_error (pfile, "assertion without predicate");
1235 return 0;
1236 }
1237 else if (predicate->type != CPP_NAME)
1238 {
1239 cpp_error (pfile, "predicate must be an identifier");
1240 return 0;
1241 }
1242
1243 token = _cpp_get_raw_token (pfile);
1244 if (token->type != CPP_OPEN_PAREN)
1245 {
1246 /* #unassert and #if are OK without predicate. */
1247 if (d == &dtable[T_UNASSERT])
1248 {
1249 if (token->type == CPP_EOF)
1250 goto lookup_node;
1251 }
1252 else if (d != &dtable[T_ASSERT])
1253 {
1254 _cpp_push_token (pfile, token);
1255 goto lookup_node;
1256 }
1257 cpp_error (pfile, "missing '(' after predicate");
1258 return 0;
1259 }
1260
1261 /* Allocate a struct answer, and copy the answer to it. */
1262 answer = (struct answer *) xmalloc (sizeof (struct answer));
1263 list = &answer->list;
1264 _cpp_init_toklist (list, NO_DUMMY_TOKEN);
1265
1266 for (;;)
1267 {
1268 cpp_token *dest;
1269
1270 token = _cpp_get_raw_token (pfile);
1271
1272 if (token->type == CPP_EOF)
1273 {
1274 cpp_error (pfile, "missing ')' to complete answer");
1275 goto error;
1276 }
1277 if (token->type == CPP_CLOSE_PAREN)
1278 break;
1279
1280 /* Copy the token. */
1281 _cpp_expand_token_space (list, 1);
1282 dest = &list->tokens[list->tokens_used++];
1283 *dest = *token;
1284
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001285 if (token_spellings[token->type].type == SPELL_STRING)
Zack Weinberg041c3192000-07-04 01:58:21 +00001286 {
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001287 _cpp_expand_name_space (list, token->val.str.len);
1288 dest->val.str.text = list->namebuf + list->name_used;
Zack Weinberg041c3192000-07-04 01:58:21 +00001289 memcpy (list->namebuf + list->name_used,
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001290 token->val.str.text, token->val.str.len);
1291 list->name_used += token->val.str.len;
Zack Weinberg041c3192000-07-04 01:58:21 +00001292 }
1293 }
1294
1295 if (list->tokens_used == 0)
1296 {
1297 cpp_error (pfile, "predicate's answer is empty");
1298 goto error;
1299 }
1300
1301 /* Drop whitespace at start. */
1302 list->tokens[0].flags &= ~PREV_WHITE;
1303
1304 if ((d == &dtable[T_ASSERT] || d == &dtable[T_UNASSERT])
1305 && token[1].type != CPP_EOF)
1306 {
1307 cpp_error (pfile, "junk at end of assertion");
1308 goto error;
1309 }
1310
1311 lookup_node:
1312 *answerp = answer;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001313 len = predicate->val.node->length;
Zack Weinberg385d0612000-07-05 18:12:42 +00001314 sym = alloca (len + 1);
Zack Weinberg041c3192000-07-04 01:58:21 +00001315
1316 /* Prefix '#' to get it out of macro namespace. */
1317 sym[0] = '#';
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001318 memcpy (sym + 1, predicate->val.node->name, len);
Neil Booth09074fd2000-07-06 09:51:57 +00001319 return cpp_lookup (pfile, sym, len + 1);
Zack Weinberg041c3192000-07-04 01:58:21 +00001320
1321 error:
1322 FREE_ANSWER (answer);
1323 return 0;
1324}
1325
1326/* Returns a pointer to the pointer to the answer in the answer chain,
1327 or a pointer to NULL if the answer is not in the chain. */
1328struct answer **
Zack Weinbergc71f8352000-07-05 05:33:57 +00001329_cpp_find_answer (node, candidate)
Zack Weinberg041c3192000-07-04 01:58:21 +00001330 cpp_hashnode *node;
1331 const cpp_toklist *candidate;
1332{
1333 struct answer **result;
1334
1335 for (result = &node->value.answers; *result; result = &(*result)->next)
1336 if (_cpp_equiv_toklists (&(*result)->list, candidate))
1337 break;
1338
1339 return result;
1340}
1341
Zack Weinberg15dad1d2000-05-18 15:55:46 +00001342#define WARNING(msgid) do { cpp_warning(pfile, msgid); goto error; } while (0)
1343#define ERROR(msgid) do { cpp_error(pfile, msgid); goto error; } while (0)
1344#define ICE(msgid) do { cpp_ice(pfile, msgid); goto error; } while (0)
Per Bothner7f2935c1995-03-16 13:59:07 -08001345static int
Zack Weinberg168d3732000-03-14 06:34:11 +00001346do_assert (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001347 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001348{
Zack Weinberg041c3192000-07-04 01:58:21 +00001349 struct answer *new_answer;
1350 cpp_hashnode *node;
Zack Weinberg15dad1d2000-05-18 15:55:46 +00001351
Zack Weinberg041c3192000-07-04 01:58:21 +00001352 node = _cpp_parse_assertion (pfile, &new_answer);
1353 if (node)
1354 {
1355 new_answer->next = 0;
1356 new_answer->list.line = pfile->token_list.line;
1357 new_answer->list.file = pfile->token_list.file;
1358
1359 if (node->type == T_ASSERTION)
1360 {
Zack Weinbergc71f8352000-07-05 05:33:57 +00001361 if (*_cpp_find_answer (node, &new_answer->list))
Zack Weinberg041c3192000-07-04 01:58:21 +00001362 goto err;
1363 new_answer->next = node->value.answers;
1364 }
1365 node->type = T_ASSERTION;
1366 node->value.answers = new_answer;
1367 }
Zack Weinberg15dad1d2000-05-18 15:55:46 +00001368 return 0;
Zack Weinberg7061aa51998-12-15 11:09:16 +00001369
Zack Weinberg041c3192000-07-04 01:58:21 +00001370 err:
1371 cpp_warning (pfile, "\"%.*s\" re-asserted",
1372 node->length - 1, node->name + 1);
1373 FREE_ANSWER (new_answer);
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001374 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001375}
Zack Weinberg7061aa51998-12-15 11:09:16 +00001376
Per Bothner7f2935c1995-03-16 13:59:07 -08001377static int
Zack Weinberg168d3732000-03-14 06:34:11 +00001378do_unassert (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001379 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001380{
Zack Weinberg041c3192000-07-04 01:58:21 +00001381 cpp_hashnode *node;
1382 struct answer *answer, *temp, *next;
1383
1384 node = _cpp_parse_assertion (pfile, &answer);
1385 if (node)
Zack Weinberg7061aa51998-12-15 11:09:16 +00001386 {
Zack Weinberg041c3192000-07-04 01:58:21 +00001387 /* It isn't an error to #unassert something that isn't asserted. */
1388 if (node->type == T_ASSERTION)
Zack Weinberg15dad1d2000-05-18 15:55:46 +00001389 {
Zack Weinberg041c3192000-07-04 01:58:21 +00001390 if (answer)
1391 {
Zack Weinbergc71f8352000-07-05 05:33:57 +00001392 struct answer **p = _cpp_find_answer (node, &answer->list);
Zack Weinberg041c3192000-07-04 01:58:21 +00001393
1394 temp = *p;
1395 if (temp)
1396 {
1397 *p = temp->next;
1398 FREE_ANSWER (temp);
1399 }
1400 if (node->value.answers == 0)
1401 node->type = T_VOID;
1402 }
1403 else
1404 {
1405 for (temp = node->value.answers; temp; temp = next)
1406 {
1407 next = temp->next;
1408 FREE_ANSWER (temp);
1409 }
1410 node->type = T_VOID;
1411 }
Zack Weinberg15dad1d2000-05-18 15:55:46 +00001412 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001413
1414 if (answer)
1415 FREE_ANSWER (answer);
Zack Weinberg7061aa51998-12-15 11:09:16 +00001416 }
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001417 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001418}
Per Bothner7f2935c1995-03-16 13:59:07 -08001419
Zack Weinberg45b966d2000-03-13 22:01:08 +00001420/* These are for -D, -U, -A. */
1421
1422/* Process the string STR as if it appeared as the body of a #define.
1423 If STR is just an identifier, define it with value 1.
1424 If STR has anything after the identifier, then it should
1425 be identifier=definition. */
1426
1427void
1428cpp_define (pfile, str)
1429 cpp_reader *pfile;
1430 const char *str;
1431{
1432 char *buf, *p;
1433 size_t count;
1434
1435 p = strchr (str, '=');
1436 /* Copy the entire option so we can modify it.
1437 Change the first "=" in the string to a space. If there is none,
1438 tack " 1" on the end. Then add a newline and a NUL. */
1439
1440 if (p)
1441 {
1442 count = strlen (str) + 2;
Dave Pitts5f6d3822000-04-16 17:54:25 -07001443 buf = (char *) alloca (count);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001444 memcpy (buf, str, count - 2);
1445 buf[p - str] = ' ';
1446 buf[count - 2] = '\n';
1447 buf[count - 1] = '\0';
1448 }
1449 else
1450 {
1451 count = strlen (str) + 4;
Dave Pitts5f6d3822000-04-16 17:54:25 -07001452 buf = (char *) alloca (count);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001453 memcpy (buf, str, count - 4);
1454 strcpy (&buf[count-4], " 1\n");
1455 }
1456
Zack Weinberg041c3192000-07-04 01:58:21 +00001457 _cpp_run_directive (pfile, &dtable[T_DEFINE], buf, count - 1);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001458}
1459
1460/* Process MACRO as if it appeared as the body of an #undef. */
1461void
1462cpp_undef (pfile, macro)
1463 cpp_reader *pfile;
1464 const char *macro;
1465{
Zack Weinberg041c3192000-07-04 01:58:21 +00001466 _cpp_run_directive (pfile, &dtable[T_UNDEF], macro, strlen (macro));
Zack Weinberg45b966d2000-03-13 22:01:08 +00001467}
1468
1469/* Process the string STR as if it appeared as the body of a #assert. */
1470void
1471cpp_assert (pfile, str)
1472 cpp_reader *pfile;
1473 const char *str;
1474{
Zack Weinberg041c3192000-07-04 01:58:21 +00001475 _cpp_run_directive (pfile, &dtable[T_ASSERT], str, strlen (str));
Zack Weinberg45b966d2000-03-13 22:01:08 +00001476}
1477
Zack Weinberg0b22d651999-03-15 18:42:46 +00001478/* Process STR as if it appeared as the body of an #unassert. */
1479void
1480cpp_unassert (pfile, str)
1481 cpp_reader *pfile;
Neil Booth7ceb3592000-03-11 00:49:44 +00001482 const char *str;
Zack Weinberg0b22d651999-03-15 18:42:46 +00001483{
Zack Weinberg041c3192000-07-04 01:58:21 +00001484 _cpp_run_directive (pfile, &dtable[T_UNASSERT], str, strlen (str));
Zack Weinberg0b22d651999-03-15 18:42:46 +00001485}
1486
Zack Weinberg45b966d2000-03-13 22:01:08 +00001487/* Determine whether the identifier ID, of length LEN, is a defined macro. */
1488int
1489cpp_defined (pfile, id, len)
Per Bothner7f2935c1995-03-16 13:59:07 -08001490 cpp_reader *pfile;
Zack Weinberg45b966d2000-03-13 22:01:08 +00001491 const U_CHAR *id;
1492 int len;
Per Bothner7f2935c1995-03-16 13:59:07 -08001493{
Zack Weinbergf8f769e2000-05-28 05:56:38 +00001494 cpp_hashnode *hp = cpp_lookup (pfile, id, len);
Zack Weinberga7abcbb2000-05-01 18:20:10 +00001495 if (hp->type == T_POISON)
Zack Weinberg45b966d2000-03-13 22:01:08 +00001496 {
Zack Weinberg041c3192000-07-04 01:58:21 +00001497 cpp_error (pfile, "attempt to use poisoned \"%s\"", hp->name);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001498 return 0;
1499 }
Zack Weinberga7abcbb2000-05-01 18:20:10 +00001500 return (hp->type != T_VOID);
Per Bothner7f2935c1995-03-16 13:59:07 -08001501}
Zack Weinbergc71f8352000-07-05 05:33:57 +00001502
1503/* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1504 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1505 as the new input buffer.
1506 Return the new buffer, or NULL on failure. */
1507
1508cpp_buffer *
1509cpp_push_buffer (pfile, buffer, length)
1510 cpp_reader *pfile;
1511 const U_CHAR *buffer;
1512 long length;
1513{
1514 cpp_buffer *buf = CPP_BUFFER (pfile);
1515 cpp_buffer *new;
1516 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
1517 {
1518 cpp_fatal (pfile, "#include recursion too deep");
1519 return NULL;
1520 }
1521
1522 new = xobnew (pfile->buffer_ob, cpp_buffer);
1523 memset (new, 0, sizeof (cpp_buffer));
1524
1525 new->buf = new->cur = buffer;
1526 new->rlimit = buffer + length;
1527 new->prev = buf;
1528
1529 CPP_BUFFER (pfile) = new;
1530 return new;
1531}
1532
1533cpp_buffer *
1534cpp_pop_buffer (pfile)
1535 cpp_reader *pfile;
1536{
1537 cpp_buffer *buf = CPP_BUFFER (pfile);
1538
1539 unwind_if_stack (pfile, buf);
1540#ifdef HAVE_MMAP_FILE
1541 if (buf->mapped)
1542 munmap ((caddr_t) buf->buf, buf->rlimit - buf->buf);
1543 else
1544#endif
1545 if (buf->inc)
1546 free ((PTR) buf->buf);
1547
1548 if (buf->inc)
1549 {
1550 if (pfile->system_include_depth)
1551 pfile->system_include_depth--;
1552 if (pfile->include_depth)
1553 pfile->include_depth--;
1554 if (pfile->potential_control_macro)
1555 {
1556 if (buf->inc->cmacro != NEVER_REREAD)
1557 buf->inc->cmacro = pfile->potential_control_macro;
1558 pfile->potential_control_macro = 0;
1559 }
1560 pfile->input_stack_listing_current = 0;
1561 /* If the file will not be included again, then close it. */
1562 if (DO_NOT_REREAD (buf->inc))
1563 {
1564 close (buf->inc->fd);
1565 buf->inc->fd = -1;
1566 }
1567 }
1568
1569 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
1570 obstack_free (pfile->buffer_ob, buf);
1571 pfile->buffer_stack_depth--;
1572 return CPP_BUFFER (pfile);
1573}
1574
1575#define obstack_chunk_alloc xmalloc
1576#define obstack_chunk_free free
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001577#define DSC(x) U x, sizeof x - 1
Zack Weinbergc71f8352000-07-05 05:33:57 +00001578void
1579_cpp_init_stacks (pfile)
1580 cpp_reader *pfile;
1581{
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001582 int i;
1583 struct spec_nodes *s;
1584
Zack Weinbergc71f8352000-07-05 05:33:57 +00001585 pfile->buffer_ob = xnew (struct obstack);
1586 obstack_init (pfile->buffer_ob);
Zack Weinbergbfb9dc72000-07-08 19:00:39 +00001587
1588 /* Perhaps not the ideal place to put this. */
1589 pfile->spec_nodes = s = xnew (struct spec_nodes);
1590 s->n_L = cpp_lookup (pfile, DSC("L"));
1591 s->n_defined = cpp_lookup (pfile, DSC("defined"));
1592 s->n__STRICT_ANSI__ = cpp_lookup (pfile, DSC("__STRICT_ANSI__"));
1593 s->n__CHAR_UNSIGNED__ = cpp_lookup (pfile, DSC("__CHAR_UNSIGNED__"));
1594 s->n__VA_ARGS__ = cpp_lookup (pfile, DSC("__VA_ARGS__"));
1595 for (i = 0; i < N_DIRECTIVES; i++)
1596 s->dirs[i] = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
Zack Weinbergc71f8352000-07-05 05:33:57 +00001597}
1598
1599void
1600_cpp_cleanup_stacks (pfile)
1601 cpp_reader *pfile;
1602{
1603 obstack_free (pfile->buffer_ob, 0);
1604 free (pfile->buffer_ob);
1605}