blob: 747f6f5282172b388419f10bc76a7a10de944186 [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"
Zack Weinbergd35364d2000-03-12 23:46:05 +000027#include "hashtab.h"
Jeffrey A Lawab87f8c1999-01-27 01:43:17 +000028#include "intl.h"
Zack Weinberg07aa0b02000-04-01 22:55:25 +000029#include "symcat.h"
Jeff Law956d6951997-12-06 17:31:01 -070030
Zack Weinberg45b966d2000-03-13 22:01:08 +000031#define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) \
32 ? CPP_BUFFER (pfile)->cur[N] : EOF)
Per Bothner7f2935c1995-03-16 13:59:07 -080033#define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
34#define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
35#define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
Zack Weinberg564ad5f2000-02-10 00:26:47 +000036
Per Bothner7f2935c1995-03-16 13:59:07 -080037/* `struct directive' defines one #-directive, including how to handle it. */
38
Zack Weinbergba412f12000-03-01 00:57:09 +000039struct directive
40{
Zack Weinberg168d3732000-03-14 06:34:11 +000041 int (*func) /* Function to handle directive */
42 PARAMS ((cpp_reader *));
Zack Weinberg07aa0b02000-04-01 22:55:25 +000043 const char *name; /* Name of directive */
44 unsigned short length; /* Length of name */
45 unsigned short origin; /* Origin of this directive */
Per Bothner7f2935c1995-03-16 13:59:07 -080046};
47
Zack Weinberg88ae23e2000-03-08 23:35:19 +000048/* Stack of conditionals currently in progress
49 (including both successful and failing conditionals). */
50
51struct if_stack
52{
53 struct if_stack *next;
54 int lineno; /* line number where condition started */
55 int if_succeeded; /* truth of last condition in this group */
56 const U_CHAR *control_macro; /* macro name for #ifndef around entire file */
Zack Weinberg168d3732000-03-14 06:34:11 +000057 int type; /* type of last directive seen in this group */
Zack Weinberg88ae23e2000-03-08 23:35:19 +000058};
59typedef struct if_stack IF_STACK;
60
Zack Weinberg1316f1f2000-02-06 07:53:50 +000061/* Forward declarations. */
62
Zack Weinberg1316f1f2000-02-06 07:53:50 +000063static void validate_else PARAMS ((cpp_reader *, const char *));
Zack Weinberg168d3732000-03-14 06:34:11 +000064static int parse_ifdef PARAMS ((cpp_reader *, const char *));
65static unsigned int parse_include PARAMS ((cpp_reader *, const char *));
66static void conditional_skip PARAMS ((cpp_reader *, int, int,
67 U_CHAR *));
Zack Weinberg1316f1f2000-02-06 07:53:50 +000068static void skip_if_group PARAMS ((cpp_reader *));
Zack Weinberg1316f1f2000-02-06 07:53:50 +000069static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
Zack Weinberg168d3732000-03-14 06:34:11 +000070 cpp_reader *, int));
Zack Weinberg1316f1f2000-02-06 07:53:50 +000071static int read_line_number PARAMS ((cpp_reader *, int *));
Zack Weinberg1316f1f2000-02-06 07:53:50 +000072static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
Zack Weinberg38b24ee2000-03-08 20:37:23 +000073static int consider_directive_while_skipping
74 PARAMS ((cpp_reader *, IF_STACK *));
Zack Weinbergba412f12000-03-01 00:57:09 +000075static int get_macro_name PARAMS ((cpp_reader *));
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +000076
Zack Weinberg07aa0b02000-04-01 22:55:25 +000077/* Values for the "origin" field of the table below. */
78enum { KANDR = 0, STDC89, EXTENSION };
79
Zack Weinberg168d3732000-03-14 06:34:11 +000080/* This is the table of directive handlers. It is ordered by
81 frequency of occurrence; the numbers at the end are directive
82 counts from all the source code I have lying around (egcs and libc
83 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
84 pcmcia-cs-3.0.9).
Jeff Lawe9a25f71997-11-02 14:19:36 -070085
Zack Weinberg168d3732000-03-14 06:34:11 +000086 The entries with a dash and a name after the count are extensions,
87 of which all but #warning and #include_next are deprecated. The name
88 is where the extension appears to have come from. */
Per Bothner7f2935c1995-03-16 13:59:07 -080089
Zack Weinberg07aa0b02000-04-01 22:55:25 +000090/* #sccs is not always recognized. */
91#ifdef SCCS_DIRECTIVE
92# define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION) /* 0 - SVR2? */
93#else
94# define SCCS_ENTRY /* nothing */
95#endif
96
Zack Weinberg168d3732000-03-14 06:34:11 +000097#define DIRECTIVE_TABLE \
Zack Weinberg07aa0b02000-04-01 22:55:25 +000098D(define, T_DEFINE, KANDR) /* 270554 */ \
99D(include, T_INCLUDE, KANDR) /* 52262 */ \
100D(endif, T_ENDIF, KANDR) /* 45855 */ \
101D(ifdef, T_IFDEF, KANDR) /* 22000 */ \
102D(if, T_IF, KANDR) /* 18162 */ \
103D(else, T_ELSE, KANDR) /* 9863 */ \
104D(ifndef, T_IFNDEF, KANDR) /* 9675 */ \
105D(undef, T_UNDEF, KANDR) /* 4837 */ \
106D(line, T_LINE, KANDR) /* 2465 */ \
107D(elif, T_ELIF, KANDR) /* 610 */ \
108D(error, T_ERROR, STDC89) /* 475 */ \
109D(pragma, T_PRAGMA, STDC89) /* 195 */ \
110D(warning, T_WARNING, EXTENSION) /* 22 - GNU */ \
111D(include_next, T_INCLUDE_NEXT, EXTENSION) /* 19 - GNU */ \
112D(ident, T_IDENT, EXTENSION) /* 11 - SVR4 */ \
113D(import, T_IMPORT, EXTENSION) /* 0 - ObjC */ \
114D(assert, T_ASSERT, EXTENSION) /* 0 - SVR4 */ \
115D(unassert, T_UNASSERT, EXTENSION) /* 0 - SVR4 */ \
116SCCS_ENTRY
Zack Weinberg168d3732000-03-14 06:34:11 +0000117
118/* Use the table to generate a series of prototypes, an enum for the
119 directive names, and an array of directive handlers. */
120
121/* The directive-processing functions are declared to return int
122 instead of void, because some old compilers have trouble with
123 pointers to functions returning void. */
124
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000125#define D(name, t, o) static int CONCAT2(do_, name) PARAMS ((cpp_reader *));
Zack Weinberg168d3732000-03-14 06:34:11 +0000126DIRECTIVE_TABLE
127#undef D
128
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000129#define D(n, tag, o) tag,
Zack Weinberg168d3732000-03-14 06:34:11 +0000130enum
131{
132 DIRECTIVE_TABLE
133 N_DIRECTIVES
Per Bothner7f2935c1995-03-16 13:59:07 -0800134};
Zack Weinberg168d3732000-03-14 06:34:11 +0000135#undef D
136
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000137#define D(name, t, origin) \
138{ CONCAT2(do_, name), STRINGX(name), sizeof STRINGX(name) - 1, origin },
Zack Weinberg168d3732000-03-14 06:34:11 +0000139static const struct directive dtable[] =
140{
141DIRECTIVE_TABLE
142};
143#undef D
144#undef DIRECTIVE_TABLE
Per Bothner7f2935c1995-03-16 13:59:07 -0800145
Per Bothner7f2935c1995-03-16 13:59:07 -0800146/* Handle a possible # directive.
147 '#' has already been read. */
148
Zack Weinberg45b966d2000-03-13 22:01:08 +0000149int
150_cpp_handle_directive (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800151 cpp_reader *pfile;
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000152{
Zack Weinberg168d3732000-03-14 06:34:11 +0000153 int c, i;
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000154 int hash_at_bol;
Zack Weinberg168d3732000-03-14 06:34:11 +0000155 unsigned int len;
Zack Weinberg941e09b1998-12-15 11:17:06 +0000156 U_CHAR *ident;
Per Bothner7f2935c1995-03-16 13:59:07 -0800157 long old_written = CPP_WRITTEN (pfile);
158
Zack Weinbergba412f12000-03-01 00:57:09 +0000159 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
160 {
161 cpp_ice (pfile, "handle_directive called on macro buffer");
162 return 0;
163 }
164
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000165 /* -traditional directives are recognized only with the # in column 1.
166 XXX Layering violation. */
167 hash_at_bol = (CPP_BUFFER (pfile)->cur - CPP_BUFFER (pfile)->line_base == 1);
168
Zack Weinberg45b966d2000-03-13 22:01:08 +0000169 _cpp_skip_hspace (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -0800170
171 c = PEEKC ();
Zack Weinberg40c79d52000-01-12 00:35:36 +0000172 /* # followed by a number is equivalent to #line. Do not recognize
173 this form in assembly language source files. Complain about this
174 form if we're being pedantic, but not if this is regurgitated
175 input (preprocessed or fed back in by the C++ frontend). */
Per Bothner7f2935c1995-03-16 13:59:07 -0800176 if (c >= '0' && c <= '9')
177 {
Zack Weinbergae796972000-03-31 23:16:11 +0000178 if (CPP_OPTION (pfile, lang_asm))
Zack Weinbergeaefae02000-02-06 08:24:22 +0000179 return 0;
Zack Weinberg40c79d52000-01-12 00:35:36 +0000180
Jason Merrille6ad5e91999-09-23 20:28:40 +0000181 if (CPP_PEDANTIC (pfile)
Zack Weinbergae796972000-03-31 23:16:11 +0000182 && ! CPP_OPTION (pfile, preprocessed)
Jason Merrille6ad5e91999-09-23 20:28:40 +0000183 && ! CPP_BUFFER (pfile)->manual_pop)
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000184 cpp_pedwarn (pfile, "# followed by integer");
Zack Weinberg168d3732000-03-14 06:34:11 +0000185 do_line (pfile);
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000186 return 1;
Per Bothner7f2935c1995-03-16 13:59:07 -0800187 }
188
Zack Weinberg40c79d52000-01-12 00:35:36 +0000189 /* If we are rescanning preprocessed input, don't obey any directives
190 other than # nnn. */
Zack Weinbergae796972000-03-31 23:16:11 +0000191 if (CPP_OPTION (pfile, preprocessed))
Zack Weinberg40c79d52000-01-12 00:35:36 +0000192 return 0;
193
Mike Stump0f413021996-07-03 22:07:53 +0000194 /* Now find the directive name. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800195 CPP_PUTC (pfile, '#');
Zack Weinberg45b966d2000-03-13 22:01:08 +0000196 _cpp_parse_name (pfile, GETC());
Per Bothner7f2935c1995-03-16 13:59:07 -0800197 ident = pfile->token_buffer + old_written + 1;
Zack Weinberg168d3732000-03-14 06:34:11 +0000198 len = CPP_PWRITTEN (pfile) - ident;
199 if (len == 0)
Per Bothner7f2935c1995-03-16 13:59:07 -0800200 {
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000201 /* A line of just # becomes blank. A line with something
Zack Weinbergc1212d22000-02-06 23:46:18 +0000202 other than an identifier after the # is reparsed as a non-
203 directive line. */
Zack Weinbergeaefae02000-02-06 08:24:22 +0000204 CPP_SET_WRITTEN (pfile, old_written);
205 return (PEEKC() == '\n');
Per Bothner7f2935c1995-03-16 13:59:07 -0800206 }
207
Zack Weinbergeaefae02000-02-06 08:24:22 +0000208 /* Decode the keyword and call the appropriate expansion routine. */
Zack Weinberg168d3732000-03-14 06:34:11 +0000209 for (i = 0; i < N_DIRECTIVES; i++)
Per Bothner7f2935c1995-03-16 13:59:07 -0800210 {
Zack Weinberg168d3732000-03-14 06:34:11 +0000211 if (dtable[i].length == len
212 && !strncmp (dtable[i].name, ident, len))
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000213 break;
Per Bothner7f2935c1995-03-16 13:59:07 -0800214 }
Zack Weinberg168d3732000-03-14 06:34:11 +0000215 if (i == N_DIRECTIVES)
216 /* # identifier, but not a legit directive. Pass onward as a
217 CPP_DIRECTIVE token anyway - let the consumer worry about it. */
218 return 1;
Jeff Lawe9a25f71997-11-02 14:19:36 -0700219
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000220 CPP_SET_WRITTEN (pfile, old_written);
Zack Weinberg5237f532000-02-02 21:41:35 +0000221
222 if (pfile->no_directives)
223 {
224 cpp_error (pfile, "`#%s' may not be used inside a macro argument",
Zack Weinberg168d3732000-03-14 06:34:11 +0000225 dtable[i].name);
Zack Weinberg45b966d2000-03-13 22:01:08 +0000226 _cpp_skip_rest_of_line (pfile);
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000227 return 1;
Zack Weinberg5237f532000-02-02 21:41:35 +0000228 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800229
Zack Weinberg07aa0b02000-04-01 22:55:25 +0000230 if (CPP_PEDANTIC (pfile) && dtable[i].origin == EXTENSION)
231 cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name);
232 if (CPP_WTRADITIONAL (pfile))
233 {
234 if (!hash_at_bol && dtable[i].origin == KANDR)
235 cpp_warning (pfile, "the # in #%s should be at the left margin",
236 dtable[i].name);
237 else if (hash_at_bol && dtable[i].origin != KANDR)
238 cpp_warning (pfile,
239 "the # in #%s should not be at the left margin",
240 dtable[i].name);
241 }
242
243 if (CPP_TRADITIONAL (pfile) && !hash_at_bol)
244 return 0;
245
246 (*dtable[i].func) (pfile);
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000247 return 1;
Per Bothner7f2935c1995-03-16 13:59:07 -0800248}
249
250/* Pass a directive through to the output file.
251 BUF points to the contents of the directive, as a contiguous string.
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000252 LEN is the length of the string pointed to by BUF.
Per Bothner7f2935c1995-03-16 13:59:07 -0800253 KEYWORD is the keyword-table entry for the directive. */
254
255static void
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000256pass_thru_directive (buf, len, pfile, keyword)
Kaveh R. Ghazibcc5cac1999-09-07 15:41:26 +0000257 const U_CHAR *buf;
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000258 size_t len;
Per Bothner7f2935c1995-03-16 13:59:07 -0800259 cpp_reader *pfile;
Zack Weinberg168d3732000-03-14 06:34:11 +0000260 int keyword;
Per Bothner7f2935c1995-03-16 13:59:07 -0800261{
Zack Weinberg168d3732000-03-14 06:34:11 +0000262 const struct directive *kt = &dtable[keyword];
263 register unsigned klen = kt->length;
Per Bothner7f2935c1995-03-16 13:59:07 -0800264
Zack Weinberg168d3732000-03-14 06:34:11 +0000265 CPP_RESERVE (pfile, 1 + klen + len);
Per Bothner7f2935c1995-03-16 13:59:07 -0800266 CPP_PUTC_Q (pfile, '#');
Zack Weinberg168d3732000-03-14 06:34:11 +0000267 CPP_PUTS_Q (pfile, kt->name, klen);
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000268 if (len != 0 && buf[0] != ' ')
Per Bothner7f2935c1995-03-16 13:59:07 -0800269 CPP_PUTC_Q (pfile, ' ');
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000270 CPP_PUTS_Q (pfile, buf, len);
Per Bothner7f2935c1995-03-16 13:59:07 -0800271}
Per Bothner7f2935c1995-03-16 13:59:07 -0800272
Zack Weinbergba412f12000-03-01 00:57:09 +0000273/* Subroutine of do_define: determine the name of the macro to be
274 defined. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800275
Zack Weinbergba412f12000-03-01 00:57:09 +0000276static int
277get_macro_name (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800278 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800279{
Zack Weinbergba412f12000-03-01 00:57:09 +0000280 long here, len;
Per Bothner7f2935c1995-03-16 13:59:07 -0800281
Zack Weinbergba412f12000-03-01 00:57:09 +0000282 here = CPP_WRITTEN (pfile);
283 pfile->no_macro_expand++;
Zack Weinberg45b966d2000-03-13 22:01:08 +0000284 if (_cpp_get_directive_token (pfile) != CPP_NAME)
Zack Weinbergba412f12000-03-01 00:57:09 +0000285 {
286 cpp_error (pfile, "`#define' must be followed by an identifier");
287 goto invalid;
288 }
289
290 len = CPP_WRITTEN (pfile) - here;
291 if (len == 7 && !strncmp (pfile->token_buffer + here, "defined", 7))
292 {
293 cpp_error (pfile, "`defined' is not a legal macro name");
294 goto invalid;
295 }
296
297 pfile->no_macro_expand--;
298 return len;
299
300 invalid:
Zack Weinberg45b966d2000-03-13 22:01:08 +0000301 _cpp_skip_rest_of_line (pfile);
Zack Weinbergba412f12000-03-01 00:57:09 +0000302 pfile->no_macro_expand--;
303 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -0800304}
305
Zack Weinberg168d3732000-03-14 06:34:11 +0000306/* Process a #define command. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800307
308static int
Zack Weinberg168d3732000-03-14 06:34:11 +0000309do_define (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800310 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800311{
Zack Weinbergd35364d2000-03-12 23:46:05 +0000312 HASHNODE **slot;
Zack Weinbergba412f12000-03-01 00:57:09 +0000313 DEFINITION *def;
Zack Weinberg941e09b1998-12-15 11:17:06 +0000314 long here;
Zack Weinbergd35364d2000-03-12 23:46:05 +0000315 unsigned long hash;
Zack Weinbergba412f12000-03-01 00:57:09 +0000316 int len, c;
317 int funlike = 0;
318 U_CHAR *sym;
Zack Weinberg941e09b1998-12-15 11:17:06 +0000319
320 here = CPP_WRITTEN (pfile);
Zack Weinbergba412f12000-03-01 00:57:09 +0000321 len = get_macro_name (pfile);
322 if (len == 0)
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000323 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -0800324
Zack Weinbergba412f12000-03-01 00:57:09 +0000325 /* Copy out the name so we can pop the token buffer. */
326 len = CPP_WRITTEN (pfile) - here;
327 sym = (U_CHAR *) alloca (len + 1);
328 memcpy (sym, pfile->token_buffer + here, len);
329 sym[len] = '\0';
330 CPP_SET_WRITTEN (pfile, here);
331
332 /* If the next character, with no intervening whitespace, is '(',
333 then this is a function-like macro. */
334 c = PEEKC ();
335 if (c == '(')
336 funlike = 1;
337 else if (c != '\n' && !is_hspace (c))
338 /* Otherwise, C99 requires white space after the name. We treat it
339 as an object-like macro if this happens, with a warning. */
340 cpp_pedwarn (pfile, "missing white space after `#define %.*s'", len, sym);
341
Zack Weinbergb0699da2000-03-07 20:58:47 +0000342 def = _cpp_create_definition (pfile, funlike);
Zack Weinbergba412f12000-03-01 00:57:09 +0000343 if (def == 0)
344 return 0;
345
Zack Weinbergd35364d2000-03-12 23:46:05 +0000346 slot = _cpp_lookup_slot (pfile, sym, len, 1, &hash);
347 if (*slot)
Per Bothner7f2935c1995-03-16 13:59:07 -0800348 {
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000349 int ok;
Zack Weinbergd35364d2000-03-12 23:46:05 +0000350 HASHNODE *hp = *slot;
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000351
Per Bothner7f2935c1995-03-16 13:59:07 -0800352 /* Redefining a macro is ok if the definitions are the same. */
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000353 if (hp->type == T_MACRO)
Zack Weinbergb0699da2000-03-07 20:58:47 +0000354 ok = ! _cpp_compare_defs (pfile, def, hp->value.defn);
Per Bothner7f2935c1995-03-16 13:59:07 -0800355 /* Redefining a constant is ok with -D. */
Zack Weinberg09618161999-02-09 07:31:59 +0000356 else if (hp->type == T_CONST || hp->type == T_STDC)
Zack Weinbergae796972000-03-31 23:16:11 +0000357 ok = ! pfile->done_initializing;
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000358 /* Otherwise it's not ok. */
359 else
360 ok = 0;
Geoff Keatingfc009f91999-09-09 04:00:37 +0000361 /* Print the warning or error if it's not ok. */
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000362 if (! ok)
Per Bothner7f2935c1995-03-16 13:59:07 -0800363 {
Geoff Keatingfc009f91999-09-09 04:00:37 +0000364 if (hp->type == T_POISON)
Zack Weinbergba412f12000-03-01 00:57:09 +0000365 cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
Geoff Keatingfc009f91999-09-09 04:00:37 +0000366 else
Zack Weinbergba412f12000-03-01 00:57:09 +0000367 cpp_pedwarn (pfile, "`%.*s' redefined", len, sym);
Zack Weinbergae796972000-03-31 23:16:11 +0000368 if (hp->type == T_MACRO && pfile->done_initializing)
Zack Weinbergba412f12000-03-01 00:57:09 +0000369 {
370 DEFINITION *d = hp->value.defn;
371 cpp_pedwarn_with_file_and_line (pfile, d->file, d->line, d->col,
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000372 "this is the location of the previous definition");
Zack Weinbergba412f12000-03-01 00:57:09 +0000373 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800374 }
Geoff Keatingfc009f91999-09-09 04:00:37 +0000375 if (hp->type != T_POISON)
376 {
377 /* Replace the old definition. */
Zack Weinbergf9ba4282000-02-14 07:57:30 +0000378 if (hp->type == T_MACRO)
Zack Weinbergb0699da2000-03-07 20:58:47 +0000379 _cpp_free_definition (hp->value.defn);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000380 hp->type = T_MACRO;
Zack Weinbergba412f12000-03-01 00:57:09 +0000381 hp->value.defn = def;
Geoff Keatingfc009f91999-09-09 04:00:37 +0000382 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800383 }
384 else
Zack Weinbergd35364d2000-03-12 23:46:05 +0000385 {
386 HASHNODE *hp = _cpp_make_hashnode (sym, len, T_MACRO, hash);
387 hp->value.defn = def;
388 *slot = hp;
389 }
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000390
Zack Weinbergae796972000-03-31 23:16:11 +0000391 if (CPP_OPTION (pfile, debug_output)
392 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
Zack Weinbergb0699da2000-03-07 20:58:47 +0000393 _cpp_dump_definition (pfile, sym, len, def);
Zack Weinbergae796972000-03-31 23:16:11 +0000394 else if (CPP_OPTION (pfile, dump_macros) == dump_names)
Zack Weinberg168d3732000-03-14 06:34:11 +0000395 pass_thru_directive (sym, len, pfile, T_DEFINE);
Per Bothner7f2935c1995-03-16 13:59:07 -0800396
397 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -0800398}
399
Per Bothner7f2935c1995-03-16 13:59:07 -0800400/*
401 * write out a #line command, for instance, after an #include file.
Per Bothner7f2935c1995-03-16 13:59:07 -0800402 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
403 */
404
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000405void
Zack Weinberg45b966d2000-03-13 22:01:08 +0000406_cpp_output_line_command (pfile, file_change)
Per Bothner7f2935c1995-03-16 13:59:07 -0800407 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800408 enum file_change_code file_change;
409{
Zack Weinberg80e9dcb1999-04-19 11:55:04 +0000410 long line;
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000411 cpp_buffer *ip;
Per Bothner7f2935c1995-03-16 13:59:07 -0800412
Zack Weinbergae796972000-03-31 23:16:11 +0000413 if (CPP_OPTION (pfile, no_line_commands)
414 || CPP_OPTION (pfile, no_output))
Per Bothnere2f79f31996-06-07 00:30:20 -0700415 return;
416
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000417 ip = cpp_file_buffer (pfile);
418 cpp_buf_line_and_col (ip, &line, NULL);
Per Bothner7f2935c1995-03-16 13:59:07 -0800419
Zack Weinberg80e9dcb1999-04-19 11:55:04 +0000420 /* If the current file has not changed, we omit the #line if it would
421 appear to be a no-op, and we output a few newlines instead
422 if we want to increase the line number by a small amount.
423 We cannot do this if pfile->lineno is zero, because that means we
424 haven't output any line commands yet. (The very first line command
425 output is a `same_file' command.) */
426 if (file_change == same_file && pfile->lineno != 0)
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000427 {
428 if (line == pfile->lineno)
429 return;
Richard Kennera8259c71995-07-01 11:57:25 -0400430
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000431 /* If the inherited line number is a little too small,
432 output some newlines instead of a #line command. */
433 if (line > pfile->lineno && line < pfile->lineno + 8)
434 {
435 CPP_RESERVE (pfile, 20);
436 while (line > pfile->lineno)
437 {
438 CPP_PUTC_Q (pfile, '\n');
439 pfile->lineno++;
440 }
441 return;
442 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800443 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800444
445 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000446 CPP_PUTS_Q (pfile, "# ", 2);
Per Bothner7f2935c1995-03-16 13:59:07 -0800447
Jeff Lawe9a25f71997-11-02 14:19:36 -0700448 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
Per Bothner7f2935c1995-03-16 13:59:07 -0800449 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
450
Zack Weinberg45b966d2000-03-13 22:01:08 +0000451 _cpp_quote_string (pfile, ip->nominal_fname);
Alexandre Oliva656ac112000-02-27 07:57:29 +0000452 if (file_change != same_file && file_change != rename_file)
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000453 {
454 CPP_PUTC_Q (pfile, ' ');
455 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
456 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800457 /* Tell cc1 if following text comes from a system header file. */
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000458 if (ip->system_header_p)
459 {
460 CPP_PUTC_Q (pfile, ' ');
461 CPP_PUTC_Q (pfile, '3');
462 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800463#ifndef NO_IMPLICIT_EXTERN_C
464 /* Tell cc1plus if following text should be treated as C. */
Zack Weinbergae796972000-03-31 23:16:11 +0000465 if (ip->system_header_p == 2 && CPP_OPTION (pfile, cplusplus))
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000466 {
467 CPP_PUTC_Q (pfile, ' ');
468 CPP_PUTC_Q (pfile, '4');
469 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800470#endif
471 CPP_PUTC_Q (pfile, '\n');
472 pfile->lineno = line;
473}
Per Bothner7f2935c1995-03-16 13:59:07 -0800474
Zack Weinberg45b966d2000-03-13 22:01:08 +0000475/* Handle #include and #import. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800476
Zack Weinberg168d3732000-03-14 06:34:11 +0000477static unsigned int
478parse_include (pfile, name)
Per Bothner7f2935c1995-03-16 13:59:07 -0800479 cpp_reader *pfile;
Zack Weinberg168d3732000-03-14 06:34:11 +0000480 const char *name;
Per Bothner7f2935c1995-03-16 13:59:07 -0800481{
Per Bothner7f2935c1995-03-16 13:59:07 -0800482 long old_written = CPP_WRITTEN (pfile);
Zack Weinberg168d3732000-03-14 06:34:11 +0000483 enum cpp_token token;
484 int len;
Per Bothner7f2935c1995-03-16 13:59:07 -0800485
Zack Weinberg168d3732000-03-14 06:34:11 +0000486 pfile->parsing_include_directive++;
487 token = _cpp_get_directive_token (pfile);
488 pfile->parsing_include_directive--;
Per Bothner7f2935c1995-03-16 13:59:07 -0800489
Zack Weinberg168d3732000-03-14 06:34:11 +0000490 len = CPP_WRITTEN (pfile) - old_written;
491
492 if (token == CPP_STRING)
493 ; /* No special treatment required. */
494#ifdef VMS
495 else if (token == CPP_NAME)
Richard Kennercfb3ee11997-04-13 14:30:13 -0400496 {
Zack Weinberg168d3732000-03-14 06:34:11 +0000497 /* Support '#include xyz' like VAX-C. It is taken as
498 '#include <xyz.h>' and generates a warning. */
499 cpp_warning (pfile, "#%s filename is obsolete, use #%s <filename.h>",
500 name, name);
501
502 /* Rewrite the token to <xyz.h>. */
503 CPP_RESERVE (pfile, 4);
504 len += 4;
505 memmove (pfile->token_buffer + old_written + 1,
506 pfile->token_buffer + old_written,
507 CPP_WRITTEN (pfile) - old_written);
508 pfile->token_buffer[old_written] = '<';
509 CPP_PUTS_Q (pfile, ".h>", 2);
510 }
511#endif
512 else
513 {
514 cpp_error (pfile, "`#%s' expects \"FILENAME\" or <FILENAME>", name);
515 CPP_SET_WRITTEN (pfile, old_written);
516 _cpp_skip_rest_of_line (pfile);
517 return 0;
Richard Kennercfb3ee11997-04-13 14:30:13 -0400518 }
519
Zack Weinberg168d3732000-03-14 06:34:11 +0000520 CPP_NUL_TERMINATE (pfile);
521 CPP_ADJUST_WRITTEN (pfile, 1);
522
523 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
524 {
525 cpp_error (pfile, "junk at end of `#%s'", name);
526 _cpp_skip_rest_of_line (pfile);
527 }
528
529 CPP_SET_WRITTEN (pfile, old_written);
530
531 if (len == 0)
532 cpp_error (pfile, "empty file name in `#%s'", name);
533
534 return len;
535}
536
537static int
538do_include (pfile)
539 cpp_reader *pfile;
540{
541 unsigned int len;
542 char *token;
543
544 len = parse_include (pfile, dtable[T_INCLUDE].name);
Zack Weinberg29a72a42000-03-28 21:45:02 +0000545 if (len == 0)
546 return 0;
Zack Weinberg168d3732000-03-14 06:34:11 +0000547 token = alloca (len + 1);
548 strcpy (token, CPP_PWRITTEN (pfile));
549
Zack Weinbergae796972000-03-31 23:16:11 +0000550 if (CPP_OPTION (pfile, dump_includes))
Zack Weinberg168d3732000-03-14 06:34:11 +0000551 pass_thru_directive (token, len, pfile, T_INCLUDE);
552
553 _cpp_execute_include (pfile, token, len, 0, 0);
554 return 0;
555}
556
557static int
558do_import (pfile)
559 cpp_reader *pfile;
560{
561 unsigned int len;
562 char *token;
563
Zack Weinbergae796972000-03-31 23:16:11 +0000564 if (CPP_OPTION (pfile, warn_import)
Per Bothner7f2935c1995-03-16 13:59:07 -0800565 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
566 {
567 pfile->import_warning = 1;
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000568 cpp_warning (pfile,
569 "#import is obsolete, use an #ifndef wrapper in the header file");
Per Bothner7f2935c1995-03-16 13:59:07 -0800570 }
571
Zack Weinberg168d3732000-03-14 06:34:11 +0000572 len = parse_include (pfile, dtable[T_IMPORT].name);
Zack Weinberg29a72a42000-03-28 21:45:02 +0000573 if (len == 0)
574 return 0;
Zack Weinberg168d3732000-03-14 06:34:11 +0000575 token = alloca (len + 1);
576 strcpy (token, CPP_PWRITTEN (pfile));
577
Zack Weinbergae796972000-03-31 23:16:11 +0000578 if (CPP_OPTION (pfile, dump_includes))
Zack Weinberg168d3732000-03-14 06:34:11 +0000579 pass_thru_directive (token, len, pfile, T_IMPORT);
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000580
Zack Weinberg168d3732000-03-14 06:34:11 +0000581 _cpp_execute_include (pfile, token, len, 1, 0);
582 return 0;
583}
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000584
Zack Weinberg168d3732000-03-14 06:34:11 +0000585static int
586do_include_next (pfile)
587 cpp_reader *pfile;
588{
589 unsigned int len;
590 char *token;
591 struct file_name_list *search_start = 0;
Zack Weinbergf1a86df1998-12-07 13:35:20 +0000592
Zack Weinberg168d3732000-03-14 06:34:11 +0000593 len = parse_include (pfile, dtable[T_INCLUDE_NEXT].name);
Zack Weinberg29a72a42000-03-28 21:45:02 +0000594 if (len == 0)
595 return 0;
Zack Weinberg168d3732000-03-14 06:34:11 +0000596 token = alloca (len + 1);
597 strcpy (token, CPP_PWRITTEN (pfile));
598
Zack Weinbergae796972000-03-31 23:16:11 +0000599 if (CPP_OPTION (pfile, dump_includes))
Zack Weinberg168d3732000-03-14 06:34:11 +0000600 pass_thru_directive (token, len, pfile, T_INCLUDE_NEXT);
Zack Weinbergf1a86df1998-12-07 13:35:20 +0000601
Zack Weinberg692b8721998-12-16 13:23:47 +0000602 /* For #include_next, skip in the search path past the dir in which the
603 containing file was found. Treat files specified using an absolute path
604 as if there are no more directories to search. Treat the primary source
605 file like any other included source, but generate a warning. */
Zack Weinberg168d3732000-03-14 06:34:11 +0000606 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
Zack Weinberg692b8721998-12-16 13:23:47 +0000607 {
Zack Weinberg168d3732000-03-14 06:34:11 +0000608 if (CPP_BUFFER (pfile)->ihash->foundhere != ABSOLUTE_PATH)
609 search_start = CPP_BUFFER (pfile)->ihash->foundhere->next;
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000610 }
611 else
Zack Weinberg168d3732000-03-14 06:34:11 +0000612 cpp_warning (pfile, "#include_next in primary source file");
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000613
Zack Weinberg168d3732000-03-14 06:34:11 +0000614 _cpp_execute_include (pfile, token, len, 0, search_start);
Per Bothner7f2935c1995-03-16 13:59:07 -0800615 return 0;
616}
617
Jason Merrilld3a34a01999-08-14 00:42:07 +0000618/* Subroutine of do_line. Read next token from PFILE without adding it to
619 the output buffer. If it is a number between 1 and 4, store it in *NUM
620 and return 1; otherwise, return 0 and complain if we aren't at the end
621 of the directive. */
622
623static int
624read_line_number (pfile, num)
625 cpp_reader *pfile;
626 int *num;
627{
628 long save_written = CPP_WRITTEN (pfile);
Zack Weinberg45b966d2000-03-13 22:01:08 +0000629 U_CHAR *p;
630 enum cpp_token token = _cpp_get_directive_token (pfile);
Jason Merrilld3a34a01999-08-14 00:42:07 +0000631 CPP_SET_WRITTEN (pfile, save_written);
Zack Weinberg45b966d2000-03-13 22:01:08 +0000632 p = pfile->token_buffer + save_written;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000633
634 if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
635 {
636 *num = p[0] - '0';
637 return 1;
638 }
639 else
640 {
Zack Weinbergba412f12000-03-01 00:57:09 +0000641 if (token != CPP_VSPACE && token != CPP_EOF)
Jason Merrilld3a34a01999-08-14 00:42:07 +0000642 cpp_error (pfile, "invalid format `#line' command");
643 return 0;
644 }
645}
646
Zack Weinberg5538ada1999-02-04 06:36:54 -0500647/* Interpret #line command.
648 Note that the filename string (if any) is treated as if it were an
649 include filename. That means no escape handling. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800650
651static int
Zack Weinberg168d3732000-03-14 06:34:11 +0000652do_line (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800653 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800654{
655 cpp_buffer *ip = CPP_BUFFER (pfile);
656 int new_lineno;
657 long old_written = CPP_WRITTEN (pfile);
658 enum file_change_code file_change = same_file;
659 enum cpp_token token;
Zack Weinberg5538ada1999-02-04 06:36:54 -0500660 char *x;
Per Bothner7f2935c1995-03-16 13:59:07 -0800661
Zack Weinberg45b966d2000-03-13 22:01:08 +0000662 token = _cpp_get_directive_token (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -0800663
Zack Weinberg5538ada1999-02-04 06:36:54 -0500664 if (token != CPP_NUMBER)
Per Bothner7f2935c1995-03-16 13:59:07 -0800665 {
Zack Weinberg5538ada1999-02-04 06:36:54 -0500666 cpp_error (pfile, "token after `#line' is not an integer");
667 goto bad_line_directive;
668 }
669
670 new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
671 if (x[0] != '\0')
672 {
673 cpp_error (pfile, "token after `#line' is not an integer");
674 goto bad_line_directive;
675 }
676 CPP_SET_WRITTEN (pfile, old_written);
677
Alexandre Oliva7113a162000-02-16 08:43:57 +0000678 if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
Zack Weinberg5538ada1999-02-04 06:36:54 -0500679 cpp_pedwarn (pfile, "line number out of range in `#line' command");
680
Zack Weinberg45b966d2000-03-13 22:01:08 +0000681 token = _cpp_get_directive_token (pfile);
Zack Weinberg5538ada1999-02-04 06:36:54 -0500682
683 if (token == CPP_STRING)
684 {
685 U_CHAR *fname = pfile->token_buffer + old_written + 1;
686 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
Jason Merrilld3a34a01999-08-14 00:42:07 +0000687 int action_number = 0;
Zack Weinberg5538ada1999-02-04 06:36:54 -0500688
Alexandre Oliva656ac112000-02-27 07:57:29 +0000689 file_change = rename_file;
690
Jason Merrilld3a34a01999-08-14 00:42:07 +0000691 if (read_line_number (pfile, &action_number))
Zack Weinberg5538ada1999-02-04 06:36:54 -0500692 {
Zack Weinberg5538ada1999-02-04 06:36:54 -0500693 if (CPP_PEDANTIC (pfile))
694 cpp_pedwarn (pfile, "garbage at end of `#line' command");
695
Jason Merrilld3a34a01999-08-14 00:42:07 +0000696 if (action_number == 1)
Zack Weinberg5538ada1999-02-04 06:36:54 -0500697 {
Jason Merrilld3a34a01999-08-14 00:42:07 +0000698 file_change = enter_file;
699 read_line_number (pfile, &action_number);
Zack Weinberg5538ada1999-02-04 06:36:54 -0500700 }
Jason Merrilld3a34a01999-08-14 00:42:07 +0000701 else if (action_number == 2)
Zack Weinberg5538ada1999-02-04 06:36:54 -0500702 {
Jason Merrilld3a34a01999-08-14 00:42:07 +0000703 file_change = leave_file;
704 read_line_number (pfile, &action_number);
Zack Weinberg5538ada1999-02-04 06:36:54 -0500705 }
Jason Merrilld3a34a01999-08-14 00:42:07 +0000706 if (action_number == 3)
Zack Weinberg5538ada1999-02-04 06:36:54 -0500707 {
Jason Merrilld3a34a01999-08-14 00:42:07 +0000708 ip->system_header_p = 1;
709 read_line_number (pfile, &action_number);
710 }
711 if (action_number == 4)
712 {
713 ip->system_header_p = 2;
714 read_line_number (pfile, &action_number);
Zack Weinberg5538ada1999-02-04 06:36:54 -0500715 }
716 }
717
718 *end_name = '\0';
719
720 if (strcmp (fname, ip->nominal_fname))
721 {
Zack Weinberga9ae4481999-10-29 04:31:14 +0000722 const char *newname, *oldname;
Zack Weinberg38b24ee2000-03-08 20:37:23 +0000723 if (!strcmp (fname, ip->ihash->name))
724 newname = ip->ihash->name;
Zack Weinberg5538ada1999-02-04 06:36:54 -0500725 else if (ip->last_nominal_fname
726 && !strcmp (fname, ip->last_nominal_fname))
727 newname = ip->last_nominal_fname;
728 else
729 newname = xstrdup (fname);
730
731 oldname = ip->nominal_fname;
732 ip->nominal_fname = newname;
733
734 if (ip->last_nominal_fname
735 && ip->last_nominal_fname != oldname
Zack Weinberg4d9a1b41999-02-15 14:04:21 +0000736 && ip->last_nominal_fname != newname
Zack Weinberg38b24ee2000-03-08 20:37:23 +0000737 && ip->last_nominal_fname != ip->ihash->name)
Zack Weinberga9ae4481999-10-29 04:31:14 +0000738 free ((void *) ip->last_nominal_fname);
Zack Weinberg5538ada1999-02-04 06:36:54 -0500739
Zack Weinberg38b24ee2000-03-08 20:37:23 +0000740 if (newname == ip->ihash->name)
Zack Weinberg5538ada1999-02-04 06:36:54 -0500741 ip->last_nominal_fname = NULL;
742 else
743 ip->last_nominal_fname = oldname;
744 }
745 }
746 else if (token != CPP_VSPACE && token != CPP_EOF)
747 {
748 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
Per Bothner7f2935c1995-03-16 13:59:07 -0800749 goto bad_line_directive;
750 }
751
752 /* The Newline at the end of this line remains to be processed.
753 To put the next line at the specified line number,
754 we must store a line number now that is one less. */
Zack Weinberg5538ada1999-02-04 06:36:54 -0500755 ip->lineno = new_lineno - 1;
Per Bothner7f2935c1995-03-16 13:59:07 -0800756 CPP_SET_WRITTEN (pfile, old_written);
Zack Weinberg45b966d2000-03-13 22:01:08 +0000757 _cpp_output_line_command (pfile, file_change);
Per Bothner7f2935c1995-03-16 13:59:07 -0800758 return 0;
Zack Weinberg5538ada1999-02-04 06:36:54 -0500759
760 bad_line_directive:
Zack Weinberg45b966d2000-03-13 22:01:08 +0000761 _cpp_skip_rest_of_line (pfile);
Zack Weinberg5538ada1999-02-04 06:36:54 -0500762 CPP_SET_WRITTEN (pfile, old_written);
763 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -0800764}
765
Zack Weinberg5538ada1999-02-04 06:36:54 -0500766/* Remove the definition of a symbol from the symbol table.
767 According to the C standard, it is not an error to undef
768 something that has no definitions. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800769static int
Zack Weinberg168d3732000-03-14 06:34:11 +0000770do_undef (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800771 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800772{
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000773 int len;
Zack Weinbergd35364d2000-03-12 23:46:05 +0000774 HASHNODE **slot;
Zack Weinberg941e09b1998-12-15 11:17:06 +0000775 U_CHAR *buf, *name, *limit;
776 int c;
777 long here = CPP_WRITTEN (pfile);
778 enum cpp_token token;
779
Zack Weinberg45b966d2000-03-13 22:01:08 +0000780 _cpp_skip_hspace (pfile);
Zack Weinberg941e09b1998-12-15 11:17:06 +0000781 c = GETC();
Zack Weinberga9ae4481999-10-29 04:31:14 +0000782 if (! is_idstart(c))
Zack Weinberg941e09b1998-12-15 11:17:06 +0000783 {
784 cpp_error (pfile, "token after #undef is not an identifier");
Zack Weinberg45b966d2000-03-13 22:01:08 +0000785 _cpp_skip_rest_of_line (pfile);
Zack Weinberg941e09b1998-12-15 11:17:06 +0000786 return 1;
787 }
788
Zack Weinberg45b966d2000-03-13 22:01:08 +0000789 _cpp_parse_name (pfile, c);
Zack Weinberg941e09b1998-12-15 11:17:06 +0000790 buf = pfile->token_buffer + here;
791 limit = CPP_PWRITTEN(pfile);
792
793 /* Copy out the token so we can pop the token buffer. */
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000794 len = limit - buf;
795 name = (U_CHAR *) alloca (len + 1);
796 memcpy (name, buf, len);
Zack Weinbergba412f12000-03-01 00:57:09 +0000797 name[len] = '\0';
Zack Weinberg941e09b1998-12-15 11:17:06 +0000798
Zack Weinberg45b966d2000-03-13 22:01:08 +0000799 token = _cpp_get_directive_token (pfile);
Zack Weinbergba412f12000-03-01 00:57:09 +0000800 if (token != CPP_VSPACE)
Zack Weinberg941e09b1998-12-15 11:17:06 +0000801 {
802 cpp_pedwarn (pfile, "junk on line after #undef");
Zack Weinberg45b966d2000-03-13 22:01:08 +0000803 _cpp_skip_rest_of_line (pfile);
Zack Weinberg941e09b1998-12-15 11:17:06 +0000804 }
Zack Weinberg941e09b1998-12-15 11:17:06 +0000805 CPP_SET_WRITTEN (pfile, here);
Per Bothner7f2935c1995-03-16 13:59:07 -0800806
Zack Weinbergd35364d2000-03-12 23:46:05 +0000807 slot = _cpp_lookup_slot (pfile, name, len, 0, 0);
808 if (slot)
Per Bothner7f2935c1995-03-16 13:59:07 -0800809 {
Zack Weinbergd35364d2000-03-12 23:46:05 +0000810 HASHNODE *hp = *slot;
Per Bothner7f2935c1995-03-16 13:59:07 -0800811 /* If we are generating additional info for debugging (with -g) we
812 need to pass through all effective #undef commands. */
Zack Weinbergae796972000-03-31 23:16:11 +0000813 if (CPP_OPTION (pfile, debug_output))
Zack Weinberg168d3732000-03-14 06:34:11 +0000814 pass_thru_directive (name, len, pfile, T_UNDEF);
Geoff Keatingfc009f91999-09-09 04:00:37 +0000815 if (hp->type == T_POISON)
816 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
817 else
818 {
819 if (hp->type != T_MACRO)
820 cpp_warning (pfile, "undefining `%s'", hp->name);
Zack Weinbergd35364d2000-03-12 23:46:05 +0000821
822 htab_clear_slot (pfile->hashtab, (void **)slot);
Geoff Keatingfc009f91999-09-09 04:00:37 +0000823 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800824 }
825
Per Bothner7f2935c1995-03-16 13:59:07 -0800826 return 0;
827}
Zack Weinberg941e09b1998-12-15 11:17:06 +0000828
Per Bothner7f2935c1995-03-16 13:59:07 -0800829/*
830 * Report an error detected by the program we are processing.
831 * Use the text of the line in the error message.
832 * (We use error because it prints the filename & line#.)
833 */
834
835static int
Zack Weinberg168d3732000-03-14 06:34:11 +0000836do_error (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800837 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800838{
Neil Booth7ceb3592000-03-11 00:49:44 +0000839 const U_CHAR *text, *limit;
Zack Weinberg941e09b1998-12-15 11:17:06 +0000840
Zack Weinberg45b966d2000-03-13 22:01:08 +0000841 _cpp_skip_hspace (pfile);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000842 text = CPP_BUFFER (pfile)->cur;
Zack Weinberg45b966d2000-03-13 22:01:08 +0000843 _cpp_skip_rest_of_line (pfile);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000844 limit = CPP_BUFFER (pfile)->cur;
845
846 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
Per Bothner7f2935c1995-03-16 13:59:07 -0800847 return 0;
848}
849
850/*
851 * Report a warning detected by the program we are processing.
852 * Use the text of the line in the warning message, then continue.
Per Bothner7f2935c1995-03-16 13:59:07 -0800853 */
854
855static int
Zack Weinberg168d3732000-03-14 06:34:11 +0000856do_warning (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800857 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800858{
Neil Booth7ceb3592000-03-11 00:49:44 +0000859 const U_CHAR *text, *limit;
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000860
Zack Weinberg45b966d2000-03-13 22:01:08 +0000861 _cpp_skip_hspace (pfile);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000862 text = CPP_BUFFER (pfile)->cur;
Zack Weinberg45b966d2000-03-13 22:01:08 +0000863 _cpp_skip_rest_of_line (pfile);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000864 limit = CPP_BUFFER (pfile)->cur;
Jeff Lawf5963e61998-05-05 17:18:02 -0600865
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000866 cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
Per Bothner7f2935c1995-03-16 13:59:07 -0800867 return 0;
868}
869
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000870/* Report program identification. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800871
872static int
Zack Weinberg168d3732000-03-14 06:34:11 +0000873do_ident (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800874 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800875{
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000876 long old_written = CPP_WRITTEN (pfile);
877
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000878 CPP_PUTS (pfile, "#ident ", 7);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000879
880 /* Next token should be a string constant. */
Zack Weinberg45b966d2000-03-13 22:01:08 +0000881 if (_cpp_get_directive_token (pfile) == CPP_STRING)
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000882 /* And then a newline. */
Zack Weinberg45b966d2000-03-13 22:01:08 +0000883 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000884 /* Good - ship it. */
885 return 0;
886
887 cpp_error (pfile, "invalid #ident");
Zack Weinberg45b966d2000-03-13 22:01:08 +0000888 _cpp_skip_rest_of_line (pfile);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000889 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
Per Bothner7f2935c1995-03-16 13:59:07 -0800890
891 return 0;
892}
893
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000894/* Pragmata handling. We handle some of these, and pass the rest on
895 to the front end. C99 defines three pragmas and says that no macro
896 expansion is to be performed on them; whether or not macro
897 expansion happens for other pragmas is implementation defined.
898 This implementation never macro-expands the text after #pragma.
899
900 We currently do not support the _Pragma operator. Support for that
901 has to be coordinated with the front end. Proposed implementation:
902 both #pragma blah blah and _Pragma("blah blah") become
903 __builtin_pragma(blah blah) and we teach the parser about that. */
904
905/* Sub-handlers for the pragmas needing treatment here.
906 They return 1 if the token buffer is to be popped, 0 if not. */
907static int do_pragma_once PARAMS ((cpp_reader *));
908static int do_pragma_implementation PARAMS ((cpp_reader *));
909static int do_pragma_poison PARAMS ((cpp_reader *));
910static int do_pragma_default PARAMS ((cpp_reader *));
Per Bothner7f2935c1995-03-16 13:59:07 -0800911
912static int
Zack Weinberg168d3732000-03-14 06:34:11 +0000913do_pragma (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800914 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800915{
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000916 long here, key;
Zack Weinberg941e09b1998-12-15 11:17:06 +0000917 U_CHAR *buf;
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000918 int pop;
Alexandre Oliva0172e2b2000-02-27 06:24:27 +0000919 enum cpp_token token;
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000920
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000921 here = CPP_WRITTEN (pfile);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000922 CPP_PUTS (pfile, "#pragma ", 8);
Zack Weinbergadd70911998-10-29 11:54:13 +0000923
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000924 key = CPP_WRITTEN (pfile);
925 pfile->no_macro_expand++;
Zack Weinberg45b966d2000-03-13 22:01:08 +0000926 token = _cpp_get_directive_token (pfile);
Alexandre Oliva0172e2b2000-02-27 06:24:27 +0000927 if (token != CPP_NAME)
928 {
929 if (token == CPP_VSPACE)
930 goto empty;
931 else
932 goto skip;
933 }
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000934
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000935 buf = pfile->token_buffer + key;
936 CPP_PUTC (pfile, ' ');
Per Bothner7f2935c1995-03-16 13:59:07 -0800937
Neil Booth7ceb3592000-03-11 00:49:44 +0000938#define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000939 if (tokis ("once"))
940 pop = do_pragma_once (pfile);
941 else if (tokis ("implementation"))
942 pop = do_pragma_implementation (pfile);
943 else if (tokis ("poison"))
944 pop = do_pragma_poison (pfile);
945 else
946 pop = do_pragma_default (pfile);
947#undef tokis
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000948
Zack Weinberg45b966d2000-03-13 22:01:08 +0000949 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000950 goto skip;
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000951
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000952 if (pop)
953 CPP_SET_WRITTEN (pfile, here);
954 pfile->no_macro_expand--;
955 return 0;
Geoff Keatingfc009f91999-09-09 04:00:37 +0000956
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000957 skip:
958 cpp_error (pfile, "malformed #pragma directive");
Zack Weinberg45b966d2000-03-13 22:01:08 +0000959 _cpp_skip_rest_of_line (pfile);
Alexandre Oliva0172e2b2000-02-27 06:24:27 +0000960 empty:
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000961 CPP_SET_WRITTEN (pfile, here);
962 pfile->no_macro_expand--;
Per Bothner7f2935c1995-03-16 13:59:07 -0800963 return 0;
964}
965
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000966static int
967do_pragma_default (pfile)
968 cpp_reader *pfile;
969{
Zack Weinberg45b966d2000-03-13 22:01:08 +0000970 while (_cpp_get_directive_token (pfile) != CPP_VSPACE)
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000971 CPP_PUTC (pfile, ' ');
972 return 0;
973}
974
975static int
976do_pragma_once (pfile)
977 cpp_reader *pfile;
978{
979 cpp_buffer *ip = CPP_BUFFER (pfile);
980
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000981 /* Allow #pragma once in system headers, since that's not the user's
982 fault. */
983 if (!ip->system_header_p)
984 cpp_warning (pfile, "`#pragma once' is obsolete");
985
Zack Weinberg38b24ee2000-03-08 20:37:23 +0000986 if (CPP_PREV_BUFFER (ip) == NULL)
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000987 cpp_warning (pfile, "`#pragma once' outside include file");
988 else
Neil Booth7ceb3592000-03-11 00:49:44 +0000989 ip->ihash->control_macro = (const U_CHAR *) ""; /* never repeat */
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000990
991 return 1;
992}
993
994static int
995do_pragma_implementation (pfile)
996 cpp_reader *pfile;
997{
998 /* Be quiet about `#pragma implementation' for a file only if it hasn't
999 been included yet. */
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001000 enum cpp_token token;
1001 long written = CPP_WRITTEN (pfile);
1002 U_CHAR *name;
1003 U_CHAR *copy;
Zack Weinbergd35364d2000-03-12 23:46:05 +00001004 size_t len;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001005
Zack Weinberg45b966d2000-03-13 22:01:08 +00001006 token = _cpp_get_directive_token (pfile);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001007 if (token == CPP_VSPACE)
1008 return 0;
1009 else if (token != CPP_STRING)
1010 {
1011 cpp_error (pfile, "malformed #pragma implementation");
1012 return 1;
1013 }
1014
Zack Weinberg0e091b52000-03-13 17:25:36 +00001015 /* Trim the leading and trailing quote marks from the string. */
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001016 name = pfile->token_buffer + written + 1;
Zack Weinberg0e091b52000-03-13 17:25:36 +00001017 len = CPP_PWRITTEN (pfile) - name;
Zack Weinbergd35364d2000-03-12 23:46:05 +00001018 copy = (U_CHAR *) alloca (len);
1019 memcpy (copy, name, len - 1);
Zack Weinberg0e091b52000-03-13 17:25:36 +00001020 copy[len - 1] = '\0';
Zack Weinbergd35364d2000-03-12 23:46:05 +00001021
Zack Weinbergb0699da2000-03-07 20:58:47 +00001022 if (cpp_included (pfile, copy))
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001023 cpp_warning (pfile,
1024 "`#pragma implementation' for `%s' appears after file is included",
1025 copy);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001026 return 0;
1027}
1028
1029static int
1030do_pragma_poison (pfile)
1031 cpp_reader *pfile;
1032{
1033 /* Poison these symbols so that all subsequent usage produces an
1034 error message. */
1035 U_CHAR *p;
Zack Weinbergd35364d2000-03-12 23:46:05 +00001036 HASHNODE **slot;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001037 long written;
1038 size_t len;
1039 enum cpp_token token;
1040 int writeit;
Zack Weinbergd35364d2000-03-12 23:46:05 +00001041 unsigned long hash;
1042
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001043 /* As a rule, don't include #pragma poison commands in output,
1044 unless the user asks for them. */
Zack Weinbergae796972000-03-31 23:16:11 +00001045 writeit = (CPP_OPTION (pfile, debug_output)
1046 || CPP_OPTION (pfile, dump_macros) == dump_definitions
1047 || CPP_OPTION (pfile, dump_macros) == dump_names);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001048
1049 for (;;)
1050 {
1051 written = CPP_WRITTEN (pfile);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001052 token = _cpp_get_directive_token (pfile);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001053 if (token == CPP_VSPACE)
1054 break;
1055 if (token != CPP_NAME)
1056 {
1057 cpp_error (pfile, "invalid #pragma poison directive");
Zack Weinberg45b966d2000-03-13 22:01:08 +00001058 _cpp_skip_rest_of_line (pfile);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001059 return 1;
1060 }
1061
1062 p = pfile->token_buffer + written;
1063 len = strlen (p);
Zack Weinbergd35364d2000-03-12 23:46:05 +00001064 slot = _cpp_lookup_slot (pfile, p, len, 1, &hash);
1065 if (*slot)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001066 {
Zack Weinbergd35364d2000-03-12 23:46:05 +00001067 HASHNODE *hp = *slot;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001068 if (hp->type != T_POISON)
1069 {
1070 cpp_warning (pfile, "poisoning existing macro `%s'", p);
Zack Weinbergb0699da2000-03-07 20:58:47 +00001071 if (hp->type == T_MACRO)
1072 _cpp_free_definition (hp->value.defn);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001073 hp->value.defn = 0;
1074 hp->type = T_POISON;
1075 }
1076 }
1077 else
Zack Weinbergd35364d2000-03-12 23:46:05 +00001078 {
1079 HASHNODE *hp = _cpp_make_hashnode (p, len, T_POISON, hash);
1080 hp->value.cpval = 0;
1081 *slot = hp;
1082 }
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001083 if (writeit)
1084 CPP_PUTC (pfile, ' ');
1085 }
1086 return !writeit;
1087}
1088
Per Bothner7f2935c1995-03-16 13:59:07 -08001089/* Just ignore #sccs, on systems where we define it at all. */
Zack Weinberg07aa0b02000-04-01 22:55:25 +00001090#ifdef SCCS_DIRECTIVE
Per Bothner7f2935c1995-03-16 13:59:07 -08001091static int
Zack Weinberg168d3732000-03-14 06:34:11 +00001092do_sccs (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001093 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001094{
Zack Weinberg45b966d2000-03-13 22:01:08 +00001095 _cpp_skip_rest_of_line (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08001096 return 0;
1097}
Zack Weinberg07aa0b02000-04-01 22:55:25 +00001098#endif
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001099
1100/* We've found an `#if' directive. If the only thing before it in
1101 this file is white space, and if it is of the form
1102 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1103 for inclusion of this file. (See redundant_include_p in cppfiles.c
1104 for an explanation of controlling macros.) If so, return a
1105 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1106
1107static U_CHAR *
1108detect_if_not_defined (pfile)
1109 cpp_reader *pfile;
1110{
1111 U_CHAR *control_macro = 0;
1112
1113 if (pfile->only_seen_white == 2)
1114 {
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001115 U_CHAR *ident;
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001116 enum cpp_token token;
1117 int base_offset;
1118 int token_offset;
1119 int need_rparen = 0;
1120
1121 /* Save state required for restore. */
1122 pfile->no_macro_expand++;
Zack Weinberg45b966d2000-03-13 22:01:08 +00001123 CPP_SET_MARK (pfile);
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001124 base_offset = CPP_WRITTEN (pfile);
1125
1126 /* Look for `!', */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001127 if (_cpp_get_directive_token (pfile) != CPP_OTHER
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001128 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1129 || CPP_PWRITTEN (pfile)[-1] != '!')
1130 goto restore;
1131
1132 /* ...then `defined', */
1133 token_offset = CPP_WRITTEN (pfile);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001134 token = _cpp_get_directive_token (pfile);
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001135 if (token != CPP_NAME)
1136 goto restore;
1137 ident = pfile->token_buffer + token_offset;
1138 CPP_NUL_TERMINATE (pfile);
1139 if (strcmp (ident, "defined"))
1140 goto restore;
1141
1142 /* ...then an optional '(' and the name, */
1143 token_offset = CPP_WRITTEN (pfile);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001144 token = _cpp_get_directive_token (pfile);
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001145 if (token == CPP_LPAREN)
1146 {
1147 token_offset = CPP_WRITTEN (pfile);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001148 token = _cpp_get_directive_token (pfile);
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001149 if (token != CPP_NAME)
1150 goto restore;
1151 need_rparen = 1;
1152 }
1153 else if (token != CPP_NAME)
1154 goto restore;
1155
1156 ident = pfile->token_buffer + token_offset;
1157 CPP_NUL_TERMINATE (pfile);
1158
1159 /* ...then the ')', if necessary, */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001160 if ((!need_rparen || _cpp_get_directive_token (pfile) == CPP_RPAREN)
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001161 /* ...and make sure there's nothing else on the line. */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001162 && _cpp_get_directive_token (pfile) == CPP_VSPACE)
Neil Booth7ceb3592000-03-11 00:49:44 +00001163 control_macro = (U_CHAR *) xstrdup (ident);
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001164
1165 restore:
1166 CPP_SET_WRITTEN (pfile, base_offset);
1167 pfile->no_macro_expand--;
Zack Weinberg45b966d2000-03-13 22:01:08 +00001168 CPP_GOTO_MARK (pfile);
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001169 }
1170
1171 return control_macro;
1172}
1173
Per Bothner7f2935c1995-03-16 13:59:07 -08001174/*
Zack Weinberg45b966d2000-03-13 22:01:08 +00001175 * #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
Zack Weinberg88ae23e2000-03-08 23:35:19 +00001176 * Also, check for a reinclude preventer of the form #if !defined (MACRO).
Per Bothner7f2935c1995-03-16 13:59:07 -08001177 */
1178
1179static int
Zack Weinberg168d3732000-03-14 06:34:11 +00001180do_if (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001181 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001182{
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001183 U_CHAR *control_macro = detect_if_not_defined (pfile);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001184 int value = _cpp_parse_expr (pfile);
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001185 conditional_skip (pfile, value == 0, T_IF, control_macro);
Per Bothner7f2935c1995-03-16 13:59:07 -08001186 return 0;
1187}
1188
1189/*
1190 * handle a #elif directive by not changing if_stack either.
1191 * see the comment above do_else.
1192 */
1193
1194static int
Zack Weinberg168d3732000-03-14 06:34:11 +00001195do_elif (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001196 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001197{
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001198 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1199 {
1200 cpp_error (pfile, "`#elif' not within a conditional");
1201 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001202 }
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001203 else
1204 {
Zack Weinberg168d3732000-03-14 06:34:11 +00001205 if (pfile->if_stack->type == T_ELSE)
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001206 {
1207 cpp_error (pfile, "`#elif' after `#else'");
1208 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1209 "the conditional began here");
1210 }
1211 pfile->if_stack->type = T_ELIF;
1212 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001213
1214 if (pfile->if_stack->if_succeeded)
Zack Weinberged705a81999-01-04 12:38:22 +00001215 skip_if_group (pfile);
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001216 else
1217 {
Zack Weinberg45b966d2000-03-13 22:01:08 +00001218 if (_cpp_parse_expr (pfile) == 0)
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001219 skip_if_group (pfile);
1220 else
1221 {
1222 ++pfile->if_stack->if_succeeded; /* continue processing input */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001223 _cpp_output_line_command (pfile, same_file);
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001224 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001225 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001226 return 0;
1227}
1228
Zack Weinberg168d3732000-03-14 06:34:11 +00001229/* Parse an #ifdef or #ifndef directive. Returns 1 for defined, 0 for
1230 not defined; the macro tested is left in the token buffer (but
1231 popped). */
Per Bothner7f2935c1995-03-16 13:59:07 -08001232
1233static int
Zack Weinberg168d3732000-03-14 06:34:11 +00001234parse_ifdef (pfile, name)
Per Bothner7f2935c1995-03-16 13:59:07 -08001235 cpp_reader *pfile;
Zack Weinberg168d3732000-03-14 06:34:11 +00001236 const char *name;
Per Bothner7f2935c1995-03-16 13:59:07 -08001237{
Mike Stump0f413021996-07-03 22:07:53 +00001238 U_CHAR *ident;
Zack Weinberg168d3732000-03-14 06:34:11 +00001239 unsigned int len;
Per Bothner7f2935c1995-03-16 13:59:07 -08001240 enum cpp_token token;
Zack Weinberg168d3732000-03-14 06:34:11 +00001241 long old_written = CPP_WRITTEN (pfile);
1242 int defined;
Per Bothner7f2935c1995-03-16 13:59:07 -08001243
1244 pfile->no_macro_expand++;
Zack Weinberg45b966d2000-03-13 22:01:08 +00001245 token = _cpp_get_directive_token (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08001246 pfile->no_macro_expand--;
1247
1248 ident = pfile->token_buffer + old_written;
Zack Weinberg168d3732000-03-14 06:34:11 +00001249 len = CPP_WRITTEN (pfile) - old_written;
Per Bothner7f2935c1995-03-16 13:59:07 -08001250
Zack Weinberg168d3732000-03-14 06:34:11 +00001251 if (token == CPP_VSPACE)
Per Bothner7f2935c1995-03-16 13:59:07 -08001252 {
Per Bothner7f2935c1995-03-16 13:59:07 -08001253 if (! CPP_TRADITIONAL (pfile))
Zack Weinberg168d3732000-03-14 06:34:11 +00001254 cpp_pedwarn (pfile, "`#%s' with no argument", name);
1255 defined = 0;
1256 goto done;
Per Bothner7f2935c1995-03-16 13:59:07 -08001257 }
1258 else if (token == CPP_NAME)
1259 {
Zack Weinberg168d3732000-03-14 06:34:11 +00001260 defined = cpp_defined (pfile, ident, len);
1261 CPP_NUL_TERMINATE (pfile);
1262 CPP_ADJUST_WRITTEN (pfile, 1);
Per Bothner7f2935c1995-03-16 13:59:07 -08001263 }
1264 else
1265 {
Zack Weinberg168d3732000-03-14 06:34:11 +00001266 defined = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001267 if (! CPP_TRADITIONAL (pfile))
Zack Weinberg168d3732000-03-14 06:34:11 +00001268 cpp_error (pfile, "`#%s' with invalid argument", name);
Per Bothner7f2935c1995-03-16 13:59:07 -08001269 }
1270
1271 if (!CPP_TRADITIONAL (pfile))
Zack Weinberg168d3732000-03-14 06:34:11 +00001272 {
1273 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
1274 goto done;
1275
1276 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", name);
Per Bothner7f2935c1995-03-16 13:59:07 -08001277 }
Zack Weinberg45b966d2000-03-13 22:01:08 +00001278 _cpp_skip_rest_of_line (pfile);
Zack Weinberg168d3732000-03-14 06:34:11 +00001279
1280 done:
1281 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1282 return defined;
1283}
Per Bothner7f2935c1995-03-16 13:59:07 -08001284
Zack Weinberg168d3732000-03-14 06:34:11 +00001285/* #ifdef is dead simple. */
1286
1287static int
1288do_ifdef (pfile)
1289 cpp_reader *pfile;
1290{
1291 int skip = ! parse_ifdef (pfile, dtable[T_IFDEF].name);
1292 conditional_skip (pfile, skip, T_IFDEF, 0);
1293 return 0;
1294}
1295
1296/* #ifndef is a tad more complex, because we need to check for a
1297 no-reinclusion wrapper. */
1298
1299static int
1300do_ifndef (pfile)
1301 cpp_reader *pfile;
1302{
1303 int start_of_file, skip;
1304 U_CHAR *control_macro = 0;
1305
1306 start_of_file = pfile->only_seen_white == 2;
1307 skip = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1308
1309 if (start_of_file && !skip)
1310 control_macro = xstrdup (CPP_PWRITTEN (pfile));
1311
1312 conditional_skip (pfile, skip, T_IFNDEF, control_macro);
Per Bothner7f2935c1995-03-16 13:59:07 -08001313 return 0;
1314}
1315
1316/* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1317 If this is a #ifndef starting at the beginning of a file,
1318 CONTROL_MACRO is the macro name tested by the #ifndef.
1319 Otherwise, CONTROL_MACRO is 0. */
1320
1321static void
1322conditional_skip (pfile, skip, type, control_macro)
1323 cpp_reader *pfile;
1324 int skip;
Zack Weinberg168d3732000-03-14 06:34:11 +00001325 int type;
Per Bothner7f2935c1995-03-16 13:59:07 -08001326 U_CHAR *control_macro;
1327{
Zack Weinberg38b24ee2000-03-08 20:37:23 +00001328 IF_STACK *temp;
Per Bothner7f2935c1995-03-16 13:59:07 -08001329
Zack Weinberg38b24ee2000-03-08 20:37:23 +00001330 temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
Per Bothner7f2935c1995-03-16 13:59:07 -08001331 temp->lineno = CPP_BUFFER (pfile)->lineno;
Per Bothner7f2935c1995-03-16 13:59:07 -08001332 temp->next = pfile->if_stack;
1333 temp->control_macro = control_macro;
1334 pfile->if_stack = temp;
1335
1336 pfile->if_stack->type = type;
1337
1338 if (skip != 0) {
Zack Weinberged705a81999-01-04 12:38:22 +00001339 skip_if_group (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08001340 return;
1341 } else {
1342 ++pfile->if_stack->if_succeeded;
Zack Weinberg45b966d2000-03-13 22:01:08 +00001343 _cpp_output_line_command (pfile, same_file);
Per Bothner7f2935c1995-03-16 13:59:07 -08001344 }
1345}
1346
Zack Weinberged705a81999-01-04 12:38:22 +00001347/* Subroutine of skip_if_group. Examine one preprocessing directive and
1348 return 0 if skipping should continue, 1 if it should halt. Also
1349 adjusts the if_stack as appropriate.
1350 The `#' has been read, but not the identifier. */
Mike Stump0f413021996-07-03 22:07:53 +00001351
Zack Weinberged705a81999-01-04 12:38:22 +00001352static int
1353consider_directive_while_skipping (pfile, stack)
1354 cpp_reader *pfile;
Zack Weinberg38b24ee2000-03-08 20:37:23 +00001355 IF_STACK *stack;
Zack Weinberged705a81999-01-04 12:38:22 +00001356{
Zack Weinberg168d3732000-03-14 06:34:11 +00001357 long ident;
Zack Weinberg2ac93491999-08-31 19:46:18 +00001358 const struct directive *kt;
Zack Weinberg168d3732000-03-14 06:34:11 +00001359 int i;
1360 unsigned int len;
Zack Weinberg38b24ee2000-03-08 20:37:23 +00001361 IF_STACK *temp;
Zack Weinberged705a81999-01-04 12:38:22 +00001362
Zack Weinberg45b966d2000-03-13 22:01:08 +00001363 _cpp_skip_hspace (pfile);
Zack Weinberged705a81999-01-04 12:38:22 +00001364
1365 ident = CPP_WRITTEN (pfile);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001366 _cpp_parse_name (pfile, GETC());
Zack Weinberg168d3732000-03-14 06:34:11 +00001367 len = CPP_WRITTEN (pfile) - ident;
Zack Weinberged705a81999-01-04 12:38:22 +00001368
1369 CPP_SET_WRITTEN (pfile, ident);
1370
Zack Weinberg168d3732000-03-14 06:34:11 +00001371 for (i = 0; i < N_DIRECTIVES; i++)
1372 {
1373 kt = &dtable[i];
1374 if (kt->length == len
1375 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
1376 switch (i)
1377 {
1378 case T_IF:
1379 case T_IFDEF:
1380 case T_IFNDEF:
Zack Weinberg38b24ee2000-03-08 20:37:23 +00001381 temp = (IF_STACK *) xmalloc (sizeof (IF_STACK));
Zack Weinberged705a81999-01-04 12:38:22 +00001382 temp->next = pfile->if_stack;
1383 pfile->if_stack = temp;
Zack Weinberg168d3732000-03-14 06:34:11 +00001384 temp->type = i;
Zack Weinberged705a81999-01-04 12:38:22 +00001385 return 0;
1386
Zack Weinberg168d3732000-03-14 06:34:11 +00001387 case T_ELSE:
Zack Weinberg75ec21d2000-01-27 22:29:07 +00001388 if (pfile->if_stack != stack)
Zack Weinberg168d3732000-03-14 06:34:11 +00001389 validate_else (pfile, dtable[i].name);
Zack Weinberged705a81999-01-04 12:38:22 +00001390 /* fall through */
Zack Weinberg168d3732000-03-14 06:34:11 +00001391 case T_ELIF:
Zack Weinberged705a81999-01-04 12:38:22 +00001392 if (pfile->if_stack == stack)
1393 return 1;
1394 else
1395 {
Zack Weinberg168d3732000-03-14 06:34:11 +00001396 pfile->if_stack->type = i;
Zack Weinberged705a81999-01-04 12:38:22 +00001397 return 0;
1398 }
1399
Zack Weinberg168d3732000-03-14 06:34:11 +00001400 case T_ENDIF:
1401 if (pfile->if_stack != stack)
1402 validate_else (pfile, dtable[i].name);
Zack Weinberged705a81999-01-04 12:38:22 +00001403
Zack Weinberg168d3732000-03-14 06:34:11 +00001404 if (pfile->if_stack == stack)
1405 return 1;
Zack Weinberged705a81999-01-04 12:38:22 +00001406
Zack Weinberg168d3732000-03-14 06:34:11 +00001407 temp = pfile->if_stack;
1408 pfile->if_stack = temp->next;
1409 free (temp);
1410 return 0;
Zack Weinberged705a81999-01-04 12:38:22 +00001411
Zack Weinberg168d3732000-03-14 06:34:11 +00001412 default:
1413 return 0;
1414 }
1415 }
Zack Weinberged705a81999-01-04 12:38:22 +00001416
1417 /* Don't let erroneous code go by. */
Zack Weinbergae796972000-03-31 23:16:11 +00001418 if (!CPP_OPTION (pfile, lang_asm) && CPP_PEDANTIC (pfile))
Zack Weinberged705a81999-01-04 12:38:22 +00001419 cpp_pedwarn (pfile, "invalid preprocessor directive name");
1420 return 0;
1421}
1422
1423/* skip to #endif, #else, or #elif. adjust line numbers, etc.
1424 * leaves input ptr at the sharp sign found.
1425 */
Per Bothner7f2935c1995-03-16 13:59:07 -08001426static void
Zack Weinberged705a81999-01-04 12:38:22 +00001427skip_if_group (pfile)
1428 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001429{
1430 int c;
Zack Weinberg38b24ee2000-03-08 20:37:23 +00001431 IF_STACK *save_if_stack = pfile->if_stack; /* don't pop past here */
Neil Booth7ceb3592000-03-11 00:49:44 +00001432 const U_CHAR *beg_of_line;
Zack Weinberged705a81999-01-04 12:38:22 +00001433 long old_written;
Per Bothner7f2935c1995-03-16 13:59:07 -08001434
Zack Weinberged705a81999-01-04 12:38:22 +00001435 old_written = CPP_WRITTEN (pfile);
1436
1437 for (;;)
Per Bothner7f2935c1995-03-16 13:59:07 -08001438 {
Zack Weinberged705a81999-01-04 12:38:22 +00001439 beg_of_line = CPP_BUFFER (pfile)->cur;
Per Bothner7f2935c1995-03-16 13:59:07 -08001440
Zack Weinberged705a81999-01-04 12:38:22 +00001441 if (! CPP_TRADITIONAL (pfile))
Zack Weinberg45b966d2000-03-13 22:01:08 +00001442 _cpp_skip_hspace (pfile);
Zack Weinberged705a81999-01-04 12:38:22 +00001443 c = GETC();
1444 if (c == '\n')
Per Bothner7f2935c1995-03-16 13:59:07 -08001445 {
Zack Weinberg3fdc6511999-03-16 13:10:15 +00001446 CPP_BUMP_LINE (pfile);
Zack Weinberged705a81999-01-04 12:38:22 +00001447 continue;
Per Bothner7f2935c1995-03-16 13:59:07 -08001448 }
Zack Weinberged705a81999-01-04 12:38:22 +00001449 else if (c == '#')
1450 {
1451 if (consider_directive_while_skipping (pfile, save_if_stack))
1452 break;
1453 }
1454 else if (c == EOF)
1455 return; /* Caller will issue error. */
1456
1457 FORWARD(-1);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001458 _cpp_skip_rest_of_line (pfile);
Zack Weinberged705a81999-01-04 12:38:22 +00001459
1460 c = GETC();
1461 if (c == EOF)
1462 return; /* Caller will issue error. */
1463 else
Zack Weinbergba412f12000-03-01 00:57:09 +00001464 CPP_BUMP_LINE (pfile);
Zack Weinberged705a81999-01-04 12:38:22 +00001465 }
1466
1467 /* Back up to the beginning of this line. Caller will process the
1468 directive. */
1469 CPP_BUFFER (pfile)->cur = beg_of_line;
Per Bothner7f2935c1995-03-16 13:59:07 -08001470 pfile->only_seen_white = 1;
Per Bothner7f2935c1995-03-16 13:59:07 -08001471}
1472
1473/*
1474 * handle a #else directive. Do this by just continuing processing
1475 * without changing if_stack ; this is so that the error message
1476 * for missing #endif's etc. will point to the original #if. It
1477 * is possible that something different would be better.
1478 */
1479
1480static int
Zack Weinberg168d3732000-03-14 06:34:11 +00001481do_else (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001482 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001483{
Zack Weinberg168d3732000-03-14 06:34:11 +00001484 validate_else (pfile, dtable[T_ELSE].name);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001485 _cpp_skip_rest_of_line (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08001486
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001487 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1488 {
1489 cpp_error (pfile, "`#else' not within a conditional");
1490 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001491 }
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001492 else
1493 {
1494 /* #ifndef can't have its special treatment for containing the whole file
1495 if it has a #else clause. */
1496 pfile->if_stack->control_macro = 0;
1497
Zack Weinberg168d3732000-03-14 06:34:11 +00001498 if (pfile->if_stack->type == T_ELSE)
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001499 {
1500 cpp_error (pfile, "`#else' after `#else'");
1501 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1502 "the conditional began here");
1503 }
1504 pfile->if_stack->type = T_ELSE;
1505 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001506
1507 if (pfile->if_stack->if_succeeded)
Zack Weinberged705a81999-01-04 12:38:22 +00001508 skip_if_group (pfile);
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001509 else
1510 {
1511 ++pfile->if_stack->if_succeeded; /* continue processing input */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001512 _cpp_output_line_command (pfile, same_file);
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001513 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001514 return 0;
1515}
1516
1517/*
1518 * unstack after #endif command
1519 */
1520
1521static int
Zack Weinberg168d3732000-03-14 06:34:11 +00001522do_endif (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001523 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001524{
Zack Weinberg168d3732000-03-14 06:34:11 +00001525 validate_else (pfile, dtable[T_ENDIF].name);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001526 _cpp_skip_rest_of_line (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08001527
1528 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
Zack Weinberg6ee2c971999-09-11 05:38:06 +00001529 cpp_error (pfile, "`#endif' not within a conditional");
Per Bothner7f2935c1995-03-16 13:59:07 -08001530 else
1531 {
Zack Weinberg38b24ee2000-03-08 20:37:23 +00001532 IF_STACK *temp = pfile->if_stack;
Per Bothner7f2935c1995-03-16 13:59:07 -08001533 pfile->if_stack = temp->next;
1534 if (temp->control_macro != 0)
1535 {
1536 /* This #endif matched a #ifndef at the start of the file.
1537 See if it is at the end of the file. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001538 int c;
1539
Zack Weinberg45b966d2000-03-13 22:01:08 +00001540 CPP_SET_MARK (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08001541
1542 for (;;)
1543 {
Zack Weinberg45b966d2000-03-13 22:01:08 +00001544 _cpp_skip_hspace (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08001545 c = GETC ();
1546 if (c != '\n')
1547 break;
1548 }
Zack Weinberg45b966d2000-03-13 22:01:08 +00001549 CPP_GOTO_MARK (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08001550
1551 if (c == EOF)
1552 {
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001553 /* This #endif ends a #ifndef
Per Bothner7f2935c1995-03-16 13:59:07 -08001554 that contains all of the file (aside from whitespace).
1555 Arrange not to include the file again
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001556 if the macro that was tested is defined. */
Zack Weinberg38b24ee2000-03-08 20:37:23 +00001557 CPP_BUFFER (pfile)->ihash->control_macro = temp->control_macro;
Per Bothner7f2935c1995-03-16 13:59:07 -08001558 }
1559 }
1560 free (temp);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001561 _cpp_output_line_command (pfile, same_file);
Per Bothner7f2935c1995-03-16 13:59:07 -08001562 }
1563 return 0;
1564}
1565
Zack Weinberg75ec21d2000-01-27 22:29:07 +00001566/* Issue -pedantic warning for text which is not a comment following
1567 an #else or #endif. Do not warn in system headers, as this is harmless
1568 and very common on old systems. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001569
1570static void
1571validate_else (pfile, directive)
1572 cpp_reader *pfile;
Zack Weinberg2ac93491999-08-31 19:46:18 +00001573 const char *directive;
Per Bothner7f2935c1995-03-16 13:59:07 -08001574{
Jason Merrill83ecd272000-03-03 00:09:22 +00001575 if (! CPP_PEDANTIC (pfile))
Zack Weinberg75ec21d2000-01-27 22:29:07 +00001576 return;
1577
Zack Weinberg45b966d2000-03-13 22:01:08 +00001578 _cpp_skip_hspace (pfile);
Zack Weinberg75ec21d2000-01-27 22:29:07 +00001579 if (PEEKC () != '\n')
Per Bothner7f2935c1995-03-16 13:59:07 -08001580 cpp_pedwarn (pfile,
Zack Weinberg168d3732000-03-14 06:34:11 +00001581 "text following `#%s' violates ANSI standard", directive);
Zack Weinberg6ee2c971999-09-11 05:38:06 +00001582}
1583
Zack Weinberg45b966d2000-03-13 22:01:08 +00001584void
1585_cpp_handle_eof (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001586 cpp_reader *pfile;
1587{
Zack Weinberg45b966d2000-03-13 22:01:08 +00001588 cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
1589 struct if_stack *ifs, *nifs;
Zack Weinberg4d9a1b41999-02-15 14:04:21 +00001590
Zack Weinberg45b966d2000-03-13 22:01:08 +00001591 /* Unwind the conditional stack and generate error messages. */
1592 for (ifs = pfile->if_stack;
1593 ifs != CPP_BUFFER (pfile)->if_stack;
1594 ifs = nifs)
Per Bothner7f2935c1995-03-16 13:59:07 -08001595 {
Zack Weinberg45b966d2000-03-13 22:01:08 +00001596 cpp_error_with_line (pfile, ifs->lineno, -1,
Zack Weinberg168d3732000-03-14 06:34:11 +00001597 "unterminated `#%s' conditional",
1598 dtable[ifs->type].name);
Zack Weinberg4d9a1b41999-02-15 14:04:21 +00001599
Zack Weinberg45b966d2000-03-13 22:01:08 +00001600 nifs = ifs->next;
1601 free (ifs);
Per Bothner7f2935c1995-03-16 13:59:07 -08001602 }
Zack Weinberg45b966d2000-03-13 22:01:08 +00001603 pfile->if_stack = ifs;
1604
1605 if (CPP_BUFFER (pfile)->nominal_fname && next_buf != NULL)
Per Bothner7f2935c1995-03-16 13:59:07 -08001606 {
Zack Weinberg45b966d2000-03-13 22:01:08 +00001607 /* We're about to return from an #include file.
1608 Emit #line information now (as part of the CPP_POP) result.
1609 But the #line refers to the file we will pop to. */
1610 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
1611 CPP_BUFFER (pfile) = next_buf;
1612 pfile->input_stack_listing_current = 0;
1613 _cpp_output_line_command (pfile, leave_file);
1614 CPP_BUFFER (pfile) = cur_buffer;
Zack Weinberg7061aa51998-12-15 11:09:16 +00001615 }
1616
Zack Weinberg45b966d2000-03-13 22:01:08 +00001617 CPP_BUFFER (pfile)->seen_eof = 1;
Zack Weinberg7061aa51998-12-15 11:09:16 +00001618}
1619
Per Bothner7f2935c1995-03-16 13:59:07 -08001620static int
Zack Weinberg168d3732000-03-14 06:34:11 +00001621do_assert (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001622 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001623{
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001624 U_CHAR *sym;
Zack Weinberg7061aa51998-12-15 11:09:16 +00001625 int ret, c;
1626 HASHNODE *base, *this;
Zack Weinbergd35364d2000-03-12 23:46:05 +00001627 HASHNODE **bslot, **tslot;
1628 size_t blen, tlen;
1629 unsigned long bhash, thash;
Per Bothner7f2935c1995-03-16 13:59:07 -08001630
Zack Weinberg45b966d2000-03-13 22:01:08 +00001631 _cpp_skip_hspace (pfile);
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001632 sym = CPP_PWRITTEN (pfile); /* remember where it starts */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001633 ret = _cpp_parse_assertion (pfile);
Zack Weinberg7061aa51998-12-15 11:09:16 +00001634 if (ret == 0)
1635 goto error;
1636 else if (ret == 1)
1637 {
1638 cpp_error (pfile, "missing token-sequence in `#assert'");
1639 goto error;
1640 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001641
Zack Weinberg45b966d2000-03-13 22:01:08 +00001642 _cpp_skip_hspace (pfile);
Zack Weinberg7061aa51998-12-15 11:09:16 +00001643 c = PEEKC();
1644 if (c != EOF && c != '\n')
1645 {
1646 cpp_error (pfile, "junk at end of `#assert'");
1647 goto error;
1648 }
1649
Zack Weinbergd35364d2000-03-12 23:46:05 +00001650 tlen = strlen (sym);
1651 blen = (U_CHAR *) strchr (sym, '(') - sym;
1652 tslot = _cpp_lookup_slot (pfile, sym, tlen, 1, &thash);
1653 if (*tslot)
Zack Weinberg7061aa51998-12-15 11:09:16 +00001654 {
1655 cpp_warning (pfile, "`%s' re-asserted", sym);
1656 goto error;
1657 }
1658
Zack Weinbergd35364d2000-03-12 23:46:05 +00001659 bslot = _cpp_lookup_slot (pfile, sym, blen, 1, &bhash);
1660 if (! *bslot)
1661 *bslot = base = _cpp_make_hashnode (sym, blen, T_ASSERT, bhash);
1662 else
1663 {
1664 base = *bslot;
1665 if (base->type != T_ASSERT)
1666 {
1667 /* Token clash - but with what?! */
1668 cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
1669 goto error;
1670 }
1671 }
1672 *tslot = this = _cpp_make_hashnode (sym, tlen, T_ASSERT, thash);
1673 this->value.aschain = base->value.aschain;
Zack Weinberg7061aa51998-12-15 11:09:16 +00001674 base->value.aschain = this;
1675
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001676 pfile->limit = sym; /* Pop */
Per Bothner7f2935c1995-03-16 13:59:07 -08001677 return 0;
Zack Weinberg7061aa51998-12-15 11:09:16 +00001678
Per Bothner7f2935c1995-03-16 13:59:07 -08001679 error:
Zack Weinberg45b966d2000-03-13 22:01:08 +00001680 _cpp_skip_rest_of_line (pfile);
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001681 pfile->limit = sym; /* Pop */
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001682 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001683}
Zack Weinberg7061aa51998-12-15 11:09:16 +00001684
Per Bothner7f2935c1995-03-16 13:59:07 -08001685static int
Zack Weinberg168d3732000-03-14 06:34:11 +00001686do_unassert (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001687 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001688{
Zack Weinberg7061aa51998-12-15 11:09:16 +00001689 int c, ret;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001690 U_CHAR *sym;
Zack Weinberg7061aa51998-12-15 11:09:16 +00001691 long baselen, thislen;
1692 HASHNODE *base, *this, *next;
1693
Zack Weinberg45b966d2000-03-13 22:01:08 +00001694 _cpp_skip_hspace (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08001695
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001696 sym = CPP_PWRITTEN (pfile); /* remember where it starts */
Zack Weinberg45b966d2000-03-13 22:01:08 +00001697 ret = _cpp_parse_assertion (pfile);
Zack Weinberg7061aa51998-12-15 11:09:16 +00001698 if (ret == 0)
1699 goto error;
1700
Zack Weinberg45b966d2000-03-13 22:01:08 +00001701 _cpp_skip_hspace (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08001702 c = PEEKC ();
1703 if (c != EOF && c != '\n')
1704 cpp_error (pfile, "junk at end of `#unassert'");
Per Bothner7f2935c1995-03-16 13:59:07 -08001705
Zack Weinberg7061aa51998-12-15 11:09:16 +00001706 thislen = strlen (sym);
1707 if (ret == 1)
1708 {
Zack Weinbergb0699da2000-03-07 20:58:47 +00001709 base = _cpp_lookup (pfile, sym, thislen);
Zack Weinberg7061aa51998-12-15 11:09:16 +00001710 if (! base)
1711 goto error; /* It isn't an error to #undef what isn't #defined,
1712 so it isn't an error to #unassert what isn't
1713 #asserted either. */
1714
1715 for (this = base->value.aschain; this; this = next)
1716 {
1717 next = this->value.aschain;
Zack Weinbergd35364d2000-03-12 23:46:05 +00001718 htab_remove_elt (pfile->hashtab, this);
Per Bothner7f2935c1995-03-16 13:59:07 -08001719 }
Zack Weinbergd35364d2000-03-12 23:46:05 +00001720 htab_remove_elt (pfile->hashtab, base);
Per Bothner7f2935c1995-03-16 13:59:07 -08001721 }
Zack Weinberg7061aa51998-12-15 11:09:16 +00001722 else
1723 {
Neil Booth7ceb3592000-03-11 00:49:44 +00001724 baselen = (U_CHAR *) strchr (sym, '(') - sym;
Zack Weinbergb0699da2000-03-07 20:58:47 +00001725 base = _cpp_lookup (pfile, sym, baselen);
Zack Weinberg7061aa51998-12-15 11:09:16 +00001726 if (! base) goto error;
Zack Weinbergb0699da2000-03-07 20:58:47 +00001727 this = _cpp_lookup (pfile, sym, thislen);
Zack Weinberg7061aa51998-12-15 11:09:16 +00001728 if (! this) goto error;
Per Bothner7f2935c1995-03-16 13:59:07 -08001729
Zack Weinberg7061aa51998-12-15 11:09:16 +00001730 next = base;
1731 while (next->value.aschain != this)
1732 next = next->value.aschain;
1733
1734 next->value.aschain = this->value.aschain;
Zack Weinbergd35364d2000-03-12 23:46:05 +00001735 htab_remove_elt (pfile->hashtab, this);
Zack Weinberg7061aa51998-12-15 11:09:16 +00001736
1737 if (base->value.aschain == NULL)
Zack Weinbergd35364d2000-03-12 23:46:05 +00001738 /* Last answer for this predicate deleted. */
1739 htab_remove_elt (pfile->hashtab, base);
Zack Weinberg7061aa51998-12-15 11:09:16 +00001740 }
1741
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001742 pfile->limit = sym; /* Pop */
Per Bothner7f2935c1995-03-16 13:59:07 -08001743 return 0;
1744 error:
Zack Weinberg45b966d2000-03-13 22:01:08 +00001745 _cpp_skip_rest_of_line (pfile);
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001746 pfile->limit = sym; /* Pop */
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001747 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001748}
Per Bothner7f2935c1995-03-16 13:59:07 -08001749
Zack Weinberg45b966d2000-03-13 22:01:08 +00001750/* These are for -D, -U, -A. */
1751
1752/* Process the string STR as if it appeared as the body of a #define.
1753 If STR is just an identifier, define it with value 1.
1754 If STR has anything after the identifier, then it should
1755 be identifier=definition. */
1756
1757void
1758cpp_define (pfile, str)
1759 cpp_reader *pfile;
1760 const char *str;
1761{
1762 char *buf, *p;
1763 size_t count;
1764
1765 p = strchr (str, '=');
1766 /* Copy the entire option so we can modify it.
1767 Change the first "=" in the string to a space. If there is none,
1768 tack " 1" on the end. Then add a newline and a NUL. */
1769
1770 if (p)
1771 {
1772 count = strlen (str) + 2;
1773 buf = alloca (count);
1774 memcpy (buf, str, count - 2);
1775 buf[p - str] = ' ';
1776 buf[count - 2] = '\n';
1777 buf[count - 1] = '\0';
1778 }
1779 else
1780 {
1781 count = strlen (str) + 4;
1782 buf = alloca (count);
1783 memcpy (buf, str, count - 4);
1784 strcpy (&buf[count-4], " 1\n");
1785 }
1786
1787 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
1788 {
Zack Weinberg168d3732000-03-14 06:34:11 +00001789 do_define (pfile);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001790 cpp_pop_buffer (pfile);
1791 }
1792}
1793
1794/* Process MACRO as if it appeared as the body of an #undef. */
1795void
1796cpp_undef (pfile, macro)
1797 cpp_reader *pfile;
1798 const char *macro;
1799{
1800 /* Copy the string so we can append a newline. */
1801 size_t len = strlen (macro);
1802 char *buf = alloca (len + 2);
1803 memcpy (buf, macro, len);
1804 buf[len] = '\n';
1805 buf[len + 1] = '\0';
1806 if (cpp_push_buffer (pfile, buf, len + 1))
1807 {
Zack Weinberg168d3732000-03-14 06:34:11 +00001808 do_undef (pfile);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001809 cpp_pop_buffer (pfile);
1810 }
1811}
1812
1813/* Process the string STR as if it appeared as the body of a #assert. */
1814void
1815cpp_assert (pfile, str)
1816 cpp_reader *pfile;
1817 const char *str;
1818{
1819 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1820 {
Zack Weinberg168d3732000-03-14 06:34:11 +00001821 do_assert (pfile);
Zack Weinberg45b966d2000-03-13 22:01:08 +00001822 cpp_pop_buffer (pfile);
1823 }
1824}
1825
Zack Weinberg0b22d651999-03-15 18:42:46 +00001826/* Process STR as if it appeared as the body of an #unassert. */
1827void
1828cpp_unassert (pfile, str)
1829 cpp_reader *pfile;
Neil Booth7ceb3592000-03-11 00:49:44 +00001830 const char *str;
Zack Weinberg0b22d651999-03-15 18:42:46 +00001831{
1832 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1833 {
Zack Weinberg168d3732000-03-14 06:34:11 +00001834 do_unassert (pfile);
Zack Weinberg0b22d651999-03-15 18:42:46 +00001835 cpp_pop_buffer (pfile);
1836 }
1837}
1838
Zack Weinberg45b966d2000-03-13 22:01:08 +00001839/* Determine whether the identifier ID, of length LEN, is a defined macro. */
1840int
1841cpp_defined (pfile, id, len)
Per Bothner7f2935c1995-03-16 13:59:07 -08001842 cpp_reader *pfile;
Zack Weinberg45b966d2000-03-13 22:01:08 +00001843 const U_CHAR *id;
1844 int len;
Per Bothner7f2935c1995-03-16 13:59:07 -08001845{
Zack Weinberg45b966d2000-03-13 22:01:08 +00001846 HASHNODE *hp = _cpp_lookup (pfile, id, len);
1847 if (hp && hp->type == T_POISON)
1848 {
1849 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1850 return 0;
1851 }
1852 return (hp != NULL);
Per Bothner7f2935c1995-03-16 13:59:07 -08001853}