blob: 02a93185fce6639c4da42723e41cb47c6429dc36 [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 Weinberg49e6c082000-03-04 19:42:04 +000029#include "mkdeps.h"
Jeff Law956d6951997-12-06 17:31:01 -070030
Zack Weinberga9ae4481999-10-29 04:31:14 +000031#define SKIP_WHITE_SPACE(p) do { while (is_hspace(*p)) p++; } while (0)
Per Bothner7f2935c1995-03-16 13:59:07 -080032
33#define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
34#define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
35#define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
36#define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
37/* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000038 (Note that it is false while we're expanding macro *arguments*.) */
39#define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->data != NULL)
Per Bothner7f2935c1995-03-16 13:59:07 -080040
Zack Weinberg564ad5f2000-02-10 00:26:47 +000041/* ACTIVE_MARK_P is true if there's a live mark in the buffer, in which
42 case CPP_BUMP_LINE must not be called. */
43#define ACTIVE_MARK_P() (CPP_BUFFER (pfile)->mark != -1)
44
Per Bothner7f2935c1995-03-16 13:59:07 -080045/* `struct directive' defines one #-directive, including how to handle it. */
46
Zack Weinbergba412f12000-03-01 00:57:09 +000047struct directive
48{
Per Bothner7f2935c1995-03-16 13:59:07 -080049 int length; /* Length of name */
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +000050 int (*func) /* Function to handle directive */
Zack Weinberg2ac93491999-08-31 19:46:18 +000051 PARAMS ((cpp_reader *, const struct directive *));
52 const char *name; /* Name of directive */
Mike Stump0f413021996-07-03 22:07:53 +000053 enum node_type type; /* Code which describes which directive. */
Per Bothner7f2935c1995-03-16 13:59:07 -080054};
55
Zack Weinberg88ae23e2000-03-08 23:35:19 +000056/* Stack of conditionals currently in progress
57 (including both successful and failing conditionals). */
58
59struct if_stack
60{
61 struct if_stack *next;
62 int lineno; /* line number where condition started */
63 int if_succeeded; /* truth of last condition in this group */
64 const U_CHAR *control_macro; /* macro name for #ifndef around entire file */
65 enum node_type type; /* type of last directive seen in this group */
66};
67typedef struct if_stack IF_STACK;
68
69
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +000070/* These functions are declared to return int instead of void since they
71 are going to be placed in a table and some old compilers have trouble with
72 pointers to functions returning void. */
73
Zack Weinberg2ac93491999-08-31 19:46:18 +000074static int do_define PARAMS ((cpp_reader *, const struct directive *));
75static int do_line PARAMS ((cpp_reader *, const struct directive *));
76static int do_include PARAMS ((cpp_reader *, const struct directive *));
77static int do_undef PARAMS ((cpp_reader *, const struct directive *));
78static int do_error PARAMS ((cpp_reader *, const struct directive *));
79static int do_pragma PARAMS ((cpp_reader *, const struct directive *));
80static int do_ident PARAMS ((cpp_reader *, const struct directive *));
81static int do_if PARAMS ((cpp_reader *, const struct directive *));
Zack Weinberg88ae23e2000-03-08 23:35:19 +000082static int do_ifdef PARAMS ((cpp_reader *, const struct directive *));
Zack Weinberg2ac93491999-08-31 19:46:18 +000083static int do_else PARAMS ((cpp_reader *, const struct directive *));
84static int do_elif PARAMS ((cpp_reader *, const struct directive *));
85static int do_endif PARAMS ((cpp_reader *, const struct directive *));
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +000086#ifdef SCCS_DIRECTIVE
Zack Weinberg2ac93491999-08-31 19:46:18 +000087static int do_sccs PARAMS ((cpp_reader *, const struct directive *));
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +000088#endif
Zack Weinberg2ac93491999-08-31 19:46:18 +000089static int do_assert PARAMS ((cpp_reader *, const struct directive *));
90static int do_unassert PARAMS ((cpp_reader *, const struct directive *));
91static int do_warning PARAMS ((cpp_reader *, const struct directive *));
Zack Weinberg1316f1f2000-02-06 07:53:50 +000092
93/* Forward declarations. */
94
Zack Weinberg1316f1f2000-02-06 07:53:50 +000095static void validate_else PARAMS ((cpp_reader *, const char *));
Zack Weinberg88ae23e2000-03-08 23:35:19 +000096static int eval_if_expr PARAMS ((cpp_reader *));
Zack Weinberg1316f1f2000-02-06 07:53:50 +000097static void conditional_skip PARAMS ((cpp_reader *, int,
98 enum node_type, U_CHAR *));
99static void skip_if_group PARAMS ((cpp_reader *));
100static void parse_name PARAMS ((cpp_reader *, int));
101static void parse_string PARAMS ((cpp_reader *, int));
102static int parse_assertion PARAMS ((cpp_reader *));
103static const char *if_directive_name PARAMS ((cpp_reader *,
104 struct if_stack *));
Zack Weinberg1316f1f2000-02-06 07:53:50 +0000105static int null_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
106static int skip_comment PARAMS ((cpp_reader *, int));
107static int copy_comment PARAMS ((cpp_reader *, int));
Zack Weinbergba412f12000-03-01 00:57:09 +0000108static void skip_string PARAMS ((cpp_reader *, int));
Zack Weinbergcf4ed942000-02-10 23:47:04 +0000109static void skip_rest_of_line PARAMS ((cpp_reader *));
110static void cpp_skip_hspace PARAMS ((cpp_reader *));
Zack Weinberg1316f1f2000-02-06 07:53:50 +0000111static int handle_directive PARAMS ((cpp_reader *));
112static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
113 cpp_reader *,
114 const struct directive *));
Zack Weinberg1316f1f2000-02-06 07:53:50 +0000115static int read_line_number PARAMS ((cpp_reader *, int *));
Zack Weinberg1316f1f2000-02-06 07:53:50 +0000116static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
Zack Weinberg38b24ee2000-03-08 20:37:23 +0000117static int consider_directive_while_skipping
118 PARAMS ((cpp_reader *, IF_STACK *));
Zack Weinberg1316f1f2000-02-06 07:53:50 +0000119static void skip_block_comment PARAMS ((cpp_reader *));
120static void skip_line_comment PARAMS ((cpp_reader *));
Zack Weinberg564ad5f2000-02-10 00:26:47 +0000121static void parse_set_mark PARAMS ((cpp_reader *));
122static void parse_goto_mark PARAMS ((cpp_reader *));
Zack Weinbergba412f12000-03-01 00:57:09 +0000123static int get_macro_name PARAMS ((cpp_reader *));
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +0000124
Zack Weinberg2ac93491999-08-31 19:46:18 +0000125/* Here is the actual list of #-directives.
126 This table is ordered by frequency of occurrence; the numbers
127 at the end are directive counts from all the source code I have
128 lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
129 linux-2.2.9, and pcmcia-cs-3.0.9). */
Jeff Lawe9a25f71997-11-02 14:19:36 -0700130
Zack Weinberg2ac93491999-08-31 19:46:18 +0000131static const struct directive directive_table[] = {
132 /* In C89 */
133 { 6, do_define, "define", T_DEFINE }, /* 270554 */
134 { 7, do_include, "include", T_INCLUDE }, /* 52262 */
135 { 5, do_endif, "endif", T_ENDIF }, /* 45855 */
Zack Weinberg88ae23e2000-03-08 23:35:19 +0000136 { 5, do_ifdef, "ifdef", T_IFDEF }, /* 22000 */
Zack Weinberg2ac93491999-08-31 19:46:18 +0000137 { 2, do_if, "if", T_IF }, /* 18162 */
138 { 4, do_else, "else", T_ELSE }, /* 9863 */
Zack Weinberg88ae23e2000-03-08 23:35:19 +0000139 { 6, do_ifdef, "ifndef", T_IFNDEF }, /* 9675 */
Zack Weinberg2ac93491999-08-31 19:46:18 +0000140 { 5, do_undef, "undef", T_UNDEF }, /* 4837 */
141 { 4, do_line, "line", T_LINE }, /* 2465 */
142 { 4, do_elif, "elif", T_ELIF }, /* 610 */
143 { 5, do_error, "error", T_ERROR }, /* 475 */
144 { 6, do_pragma, "pragma", T_PRAGMA }, /* 195 */
Per Bothner7f2935c1995-03-16 13:59:07 -0800145
Zack Weinberg2ac93491999-08-31 19:46:18 +0000146 /* Extensions. All deprecated except #warning and #include_next. */
147 { 7, do_warning, "warning", T_WARNING }, /* 22 - GNU */
148 { 12, do_include, "include_next", T_INCLUDE_NEXT }, /* 19 - GNU */
149 { 5, do_ident, "ident", T_IDENT }, /* 11 - SVR4 */
150 { 6, do_include, "import", T_IMPORT }, /* 0 - ObjC */
151 { 6, do_assert, "assert", T_ASSERT }, /* 0 - SVR4 */
152 { 8, do_unassert, "unassert", T_UNASSERT }, /* 0 - SVR4 */
Per Bothner7f2935c1995-03-16 13:59:07 -0800153#ifdef SCCS_DIRECTIVE
Zack Weinberg2ac93491999-08-31 19:46:18 +0000154 { 4, do_sccs, "sccs", T_SCCS }, /* 0 - SVR2? */
Per Bothner7f2935c1995-03-16 13:59:07 -0800155#endif
Zack Weinberg941e09b1998-12-15 11:17:06 +0000156 { -1, 0, "", T_UNUSED }
Per Bothner7f2935c1995-03-16 13:59:07 -0800157};
Per Bothner7f2935c1995-03-16 13:59:07 -0800158
159/* Place into PFILE a quoted string representing the string SRC.
Mike Stump0f413021996-07-03 22:07:53 +0000160 Caller must reserve enough space in pfile->token_buffer. */
161
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000162void
Per Bothner7f2935c1995-03-16 13:59:07 -0800163quote_string (pfile, src)
164 cpp_reader *pfile;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000165 const char *src;
Per Bothner7f2935c1995-03-16 13:59:07 -0800166{
167 U_CHAR c;
168
169 CPP_PUTC_Q (pfile, '\"');
170 for (;;)
171 switch ((c = *src++))
172 {
173 default:
Kaveh R. Ghazie9a780e1998-05-06 12:56:58 +0000174 if (ISPRINT (c))
Per Bothner7f2935c1995-03-16 13:59:07 -0800175 CPP_PUTC_Q (pfile, c);
176 else
177 {
Jeff Lawe9a25f71997-11-02 14:19:36 -0700178 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
Per Bothner7f2935c1995-03-16 13:59:07 -0800179 CPP_ADJUST_WRITTEN (pfile, 4);
180 }
181 break;
182
183 case '\"':
184 case '\\':
185 CPP_PUTC_Q (pfile, '\\');
186 CPP_PUTC_Q (pfile, c);
187 break;
188
189 case '\0':
190 CPP_PUTC_Q (pfile, '\"');
191 CPP_NUL_TERMINATE_Q (pfile);
192 return;
193 }
194}
195
Mike Stump0f413021996-07-03 22:07:53 +0000196/* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800197
198void
199cpp_grow_buffer (pfile, n)
200 cpp_reader *pfile;
201 long n;
202{
203 long old_written = CPP_WRITTEN (pfile);
204 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
Mike Stump0f413021996-07-03 22:07:53 +0000205 pfile->token_buffer = (U_CHAR *)
Per Bothner7f2935c1995-03-16 13:59:07 -0800206 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
207 CPP_SET_WRITTEN (pfile, old_written);
208}
209
Zack Weinbergcf4ed942000-02-10 23:47:04 +0000210/* Process the string STR as if it appeared as the body of a #define.
Zack Weinberg2387c1d2000-02-09 18:35:41 +0000211 If STR is just an identifier, define it with value 1.
212 If STR has anything after the identifier, then it should
213 be identifier=definition. */
214
Per Bothnerb13b05f1995-04-17 16:43:46 -0700215void
216cpp_define (pfile, str)
Per Bothner7f2935c1995-03-16 13:59:07 -0800217 cpp_reader *pfile;
Neil Booth7ceb3592000-03-11 00:49:44 +0000218 const char *str;
Per Bothner7f2935c1995-03-16 13:59:07 -0800219{
Neil Booth7ceb3592000-03-11 00:49:44 +0000220 char *buf, *p;
Zack Weinberg5538ada1999-02-04 06:36:54 -0500221 size_t count;
Per Bothner7f2935c1995-03-16 13:59:07 -0800222
Zack Weinberg2387c1d2000-02-09 18:35:41 +0000223 p = strchr (str, '=');
224 /* Copy the entire option so we can modify it.
225 Change the first "=" in the string to a space. If there is none,
226 tack " 1" on the end. Then add a newline and a NUL. */
227
Zack Weinberg5538ada1999-02-04 06:36:54 -0500228 if (p)
Per Bothner7f2935c1995-03-16 13:59:07 -0800229 {
Zack Weinberg2387c1d2000-02-09 18:35:41 +0000230 count = strlen (str) + 2;
Neil Booth7ceb3592000-03-11 00:49:44 +0000231 buf = alloca (count);
Zack Weinberg2387c1d2000-02-09 18:35:41 +0000232 memcpy (buf, str, count - 2);
233 buf[p - str] = ' ';
234 buf[count - 2] = '\n';
235 buf[count - 1] = '\0';
Per Bothner7f2935c1995-03-16 13:59:07 -0800236 }
237 else
Zack Weinberg2387c1d2000-02-09 18:35:41 +0000238 {
239 count = strlen (str) + 4;
Neil Booth7ceb3592000-03-11 00:49:44 +0000240 buf = alloca (count);
Zack Weinberg2387c1d2000-02-09 18:35:41 +0000241 memcpy (buf, str, count - 4);
242 strcpy (&buf[count-4], " 1\n");
243 }
244
Zack Weinberg5538ada1999-02-04 06:36:54 -0500245 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
Zack Weinberg941e09b1998-12-15 11:17:06 +0000246 {
247 do_define (pfile, NULL);
248 cpp_pop_buffer (pfile);
249 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800250}
Per Bothner7f2935c1995-03-16 13:59:07 -0800251
Zack Weinberg5538ada1999-02-04 06:36:54 -0500252/* Process the string STR as if it appeared as the body of a #assert. */
253void
254cpp_assert (pfile, str)
Per Bothner7f2935c1995-03-16 13:59:07 -0800255 cpp_reader *pfile;
Neil Booth7ceb3592000-03-11 00:49:44 +0000256 const char *str;
Per Bothner7f2935c1995-03-16 13:59:07 -0800257{
Zack Weinberg5538ada1999-02-04 06:36:54 -0500258 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
Per Bothnere2f79f31996-06-07 00:30:20 -0700259 {
Zack Weinberg941e09b1998-12-15 11:17:06 +0000260 do_assert (pfile, NULL);
Per Bothnere2f79f31996-06-07 00:30:20 -0700261 cpp_pop_buffer (pfile);
262 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800263}
Per Bothner7f2935c1995-03-16 13:59:07 -0800264
Zack Weinbergcf4ed942000-02-10 23:47:04 +0000265/* Determine whether the identifier ID, of length LEN, is a defined macro. */
266int
267cpp_defined (pfile, id, len)
268 cpp_reader *pfile;
269 const U_CHAR *id;
270 int len;
271{
Zack Weinbergb0699da2000-03-07 20:58:47 +0000272 HASHNODE *hp = _cpp_lookup (pfile, id, len);
Zack Weinbergcf4ed942000-02-10 23:47:04 +0000273 if (hp && hp->type == T_POISON)
274 {
275 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
276 return 0;
277 }
278 return (hp != NULL);
279}
Per Bothner7f2935c1995-03-16 13:59:07 -0800280
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000281static int
Per Bothner7f2935c1995-03-16 13:59:07 -0800282null_cleanup (pbuf, pfile)
Kaveh R. Ghazid6f4ec51998-05-13 12:40:39 +0000283 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
284 cpp_reader *pfile ATTRIBUTE_UNUSED;
Per Bothner7f2935c1995-03-16 13:59:07 -0800285{
286 return 0;
287}
288
Zack Weinberg75ec21d2000-01-27 22:29:07 +0000289/* Skip a C-style block comment. We know it's a comment, and point is
290 at the second character of the starter. */
291static void
292skip_block_comment (pfile)
293 cpp_reader *pfile;
294{
295 int c, prev_c = -1;
296 long line, col;
297
298 FORWARD(1);
299 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
300 for (;;)
301 {
302 c = GETC ();
303 if (c == EOF)
304 {
305 cpp_error_with_line (pfile, line, col, "unterminated comment");
306 return;
307 }
308 else if (c == '\n' || c == '\r')
Zack Weinberg564ad5f2000-02-10 00:26:47 +0000309 {
310 /* \r cannot be a macro escape marker here. */
311 if (!ACTIVE_MARK_P())
312 CPP_BUMP_LINE (pfile);
313 }
Zack Weinberg75ec21d2000-01-27 22:29:07 +0000314 else if (c == '/' && prev_c == '*')
315 return;
316 else if (c == '*' && prev_c == '/'
317 && CPP_OPTIONS (pfile)->warn_comments)
318 cpp_warning (pfile, "`/*' within comment");
319
320 prev_c = c;
321 }
322}
323
324/* Skip a C++/Chill line comment. We know it's a comment, and point
325 is at the second character of the initiator. */
326static void
327skip_line_comment (pfile)
328 cpp_reader *pfile;
329{
330 FORWARD(1);
331 for (;;)
332 {
333 int c = GETC ();
334
335 /* We don't have to worry about EOF in here. */
336 if (c == '\n')
337 {
338 /* Don't consider final '\n' to be part of comment. */
339 FORWARD(-1);
340 return;
341 }
342 else if (c == '\r')
343 {
344 /* \r cannot be a macro escape marker here. */
Zack Weinberg564ad5f2000-02-10 00:26:47 +0000345 if (!ACTIVE_MARK_P())
346 CPP_BUMP_LINE (pfile);
Zack Weinberg75ec21d2000-01-27 22:29:07 +0000347 if (CPP_OPTIONS (pfile)->warn_comments)
348 cpp_warning (pfile, "backslash-newline within line comment");
349 }
350 }
351}
352
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000353/* Skip a comment - C, C++, or Chill style. M is the first character
354 of the comment marker. If this really is a comment, skip to its
Zack Weinberg75ec21d2000-01-27 22:29:07 +0000355 end and return ' '. If this is not a comment, return M (which will
356 be '/' or '-'). */
Per Bothner7f2935c1995-03-16 13:59:07 -0800357
358static int
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000359skip_comment (pfile, m)
Per Bothner7f2935c1995-03-16 13:59:07 -0800360 cpp_reader *pfile;
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000361 int m;
Per Bothner7f2935c1995-03-16 13:59:07 -0800362{
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000363 if (m == '/' && PEEKC() == '*')
Per Bothner7f2935c1995-03-16 13:59:07 -0800364 {
Zack Weinberg75ec21d2000-01-27 22:29:07 +0000365 skip_block_comment (pfile);
366 return ' ';
Per Bothner7f2935c1995-03-16 13:59:07 -0800367 }
Zack Weinberg75ec21d2000-01-27 22:29:07 +0000368 else if (m == '/' && PEEKC() == '/')
Per Bothner7f2935c1995-03-16 13:59:07 -0800369 {
Zack Weinberg75ec21d2000-01-27 22:29:07 +0000370 if (CPP_BUFFER (pfile)->system_header_p)
Per Bothner7f2935c1995-03-16 13:59:07 -0800371 {
Zack Weinberg75ec21d2000-01-27 22:29:07 +0000372 /* We silently allow C++ comments in system headers, irrespective
373 of conformance mode, because lots of busted systems do that
374 and trying to clean it up in fixincludes is a nightmare. */
375 skip_line_comment (pfile);
376 return ' ';
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000377 }
Zack Weinberg75ec21d2000-01-27 22:29:07 +0000378 else if (CPP_OPTIONS (pfile)->cplusplus_comments)
379 {
380 if (CPP_OPTIONS (pfile)->c89
381 && CPP_PEDANTIC (pfile)
382 && ! CPP_BUFFER (pfile)->warned_cplusplus_comments)
383 {
384 cpp_pedwarn (pfile,
385 "C++ style comments are not allowed in ISO C89");
386 cpp_pedwarn (pfile,
387 "(this will be reported only once per input file)");
388 CPP_BUFFER (pfile)->warned_cplusplus_comments = 1;
389 }
390 skip_line_comment (pfile);
391 return ' ';
392 }
393 else
394 return m;
395 }
396 else if (m == '-' && PEEKC() == '-'
397 && CPP_OPTIONS (pfile)->chill)
398 {
399 skip_line_comment (pfile);
400 return ' ';
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000401 }
402 else
403 return m;
404}
405
406/* Identical to skip_comment except that it copies the comment into the
Zack Weinberg564ad5f2000-02-10 00:26:47 +0000407 token_buffer. This is used if !discard_comments. */
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000408static int
409copy_comment (pfile, m)
410 cpp_reader *pfile;
411 int m;
412{
Neil Booth7ceb3592000-03-11 00:49:44 +0000413 const U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
414 const U_CHAR *limit;
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000415
Zack Weinberg75ec21d2000-01-27 22:29:07 +0000416 if (skip_comment (pfile, m) == m)
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000417 return m;
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000418
Zack Weinbergba412f12000-03-01 00:57:09 +0000419 limit = CPP_BUFFER (pfile)->cur;
420 CPP_RESERVE (pfile, limit - start + 2);
421 CPP_PUTC_Q (pfile, m);
422 for (; start <= limit; start++)
Zack Weinberg75ec21d2000-01-27 22:29:07 +0000423 if (*start != '\r')
Zack Weinbergba412f12000-03-01 00:57:09 +0000424 CPP_PUTC_Q (pfile, *start);
425
Zack Weinberg75ec21d2000-01-27 22:29:07 +0000426 return ' ';
427}
Per Bothner7f2935c1995-03-16 13:59:07 -0800428
429/* Skip whitespace \-newline and comments. Does not macro-expand. */
Mike Stump0f413021996-07-03 22:07:53 +0000430
Zack Weinbergcf4ed942000-02-10 23:47:04 +0000431static void
Per Bothner7f2935c1995-03-16 13:59:07 -0800432cpp_skip_hspace (pfile)
433 cpp_reader *pfile;
434{
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000435 int c;
Per Bothner7f2935c1995-03-16 13:59:07 -0800436 while (1)
437 {
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000438 c = GETC();
Per Bothner7f2935c1995-03-16 13:59:07 -0800439 if (c == EOF)
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000440 return;
Zack Weinberga9ae4481999-10-29 04:31:14 +0000441 else if (is_hspace(c))
Per Bothner7f2935c1995-03-16 13:59:07 -0800442 {
443 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
444 cpp_pedwarn (pfile, "%s in preprocessing directive",
445 c == '\f' ? "formfeed" : "vertical tab");
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000446 }
447 else if (c == '\r')
448 {
Zack Weinberged45de91999-04-12 12:03:10 +0000449 /* \r is a backslash-newline marker if !has_escapes, and
450 a deletable-whitespace or no-reexpansion marker otherwise. */
451 if (CPP_BUFFER (pfile)->has_escapes)
452 {
453 if (PEEKC() == ' ')
454 FORWARD(1);
455 else
456 break;
457 }
458 else
Zack Weinbergba412f12000-03-01 00:57:09 +0000459 CPP_BUMP_LINE (pfile);
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000460 }
461 else if (c == '/' || c == '-')
462 {
463 c = skip_comment (pfile, c);
Zack Weinberg75ec21d2000-01-27 22:29:07 +0000464 if (c != ' ')
Zack Weinberged45de91999-04-12 12:03:10 +0000465 break;
Per Bothner7f2935c1995-03-16 13:59:07 -0800466 }
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000467 else
Zack Weinberged45de91999-04-12 12:03:10 +0000468 break;
Per Bothner7f2935c1995-03-16 13:59:07 -0800469 }
Zack Weinberged45de91999-04-12 12:03:10 +0000470 FORWARD(-1);
Per Bothner7f2935c1995-03-16 13:59:07 -0800471}
472
Zack Weinbergba412f12000-03-01 00:57:09 +0000473/* Read and discard the rest of the current line. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800474
Per Bothnere2f79f31996-06-07 00:30:20 -0700475static void
Zack Weinbergba412f12000-03-01 00:57:09 +0000476skip_rest_of_line (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800477 cpp_reader *pfile;
478{
Per Bothner7f2935c1995-03-16 13:59:07 -0800479 for (;;)
480 {
481 int c = GETC();
Per Bothner7f2935c1995-03-16 13:59:07 -0800482 switch (c)
483 {
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000484 case '\n':
485 FORWARD(-1);
Per Bothner7f2935c1995-03-16 13:59:07 -0800486 case EOF:
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000487 return;
488
489 case '\r':
Zack Weinbergba412f12000-03-01 00:57:09 +0000490 if (! CPP_BUFFER (pfile)->has_escapes)
491 CPP_BUMP_LINE (pfile);
492 break;
493
Per Bothner7f2935c1995-03-16 13:59:07 -0800494 case '\'':
495 case '\"':
Zack Weinbergba412f12000-03-01 00:57:09 +0000496 skip_string (pfile, c);
497 break;
498
Per Bothner7f2935c1995-03-16 13:59:07 -0800499 case '/':
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000500 case '-':
Zack Weinbergba412f12000-03-01 00:57:09 +0000501 skip_comment (pfile, c);
Per Bothner7f2935c1995-03-16 13:59:07 -0800502 break;
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000503
Per Bothner7f2935c1995-03-16 13:59:07 -0800504 case '\f':
505 case '\v':
506 if (CPP_PEDANTIC (pfile))
507 cpp_pedwarn (pfile, "%s in preprocessing directive",
508 c == '\f' ? "formfeed" : "vertical tab");
509 break;
510
Per Bothner7f2935c1995-03-16 13:59:07 -0800511 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800512 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800513}
514
Per Bothner7f2935c1995-03-16 13:59:07 -0800515/* Handle a possible # directive.
516 '#' has already been read. */
517
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000518static int
Per Bothner7f2935c1995-03-16 13:59:07 -0800519handle_directive (pfile)
520 cpp_reader *pfile;
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000521{
522 int c;
Zack Weinberg2ac93491999-08-31 19:46:18 +0000523 register const struct directive *kt;
Per Bothner7f2935c1995-03-16 13:59:07 -0800524 int ident_length;
Zack Weinberg941e09b1998-12-15 11:17:06 +0000525 U_CHAR *ident;
Per Bothner7f2935c1995-03-16 13:59:07 -0800526 long old_written = CPP_WRITTEN (pfile);
527
Zack Weinbergba412f12000-03-01 00:57:09 +0000528 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
529 {
530 cpp_ice (pfile, "handle_directive called on macro buffer");
531 return 0;
532 }
533
Per Bothner7f2935c1995-03-16 13:59:07 -0800534 cpp_skip_hspace (pfile);
535
536 c = PEEKC ();
Zack Weinberg40c79d52000-01-12 00:35:36 +0000537 /* # followed by a number is equivalent to #line. Do not recognize
538 this form in assembly language source files. Complain about this
539 form if we're being pedantic, but not if this is regurgitated
540 input (preprocessed or fed back in by the C++ frontend). */
Per Bothner7f2935c1995-03-16 13:59:07 -0800541 if (c >= '0' && c <= '9')
542 {
Zack Weinberg40c79d52000-01-12 00:35:36 +0000543 if (CPP_OPTIONS (pfile)->lang_asm)
Zack Weinbergeaefae02000-02-06 08:24:22 +0000544 return 0;
Zack Weinberg40c79d52000-01-12 00:35:36 +0000545
Jason Merrille6ad5e91999-09-23 20:28:40 +0000546 if (CPP_PEDANTIC (pfile)
Zack Weinberg88ae23e2000-03-08 23:35:19 +0000547 && ! CPP_OPTIONS (pfile)->preprocessed
Jason Merrille6ad5e91999-09-23 20:28:40 +0000548 && ! CPP_BUFFER (pfile)->manual_pop)
Per Bothner7f2935c1995-03-16 13:59:07 -0800549 cpp_pedwarn (pfile, "`#' followed by integer");
Zack Weinberg941e09b1998-12-15 11:17:06 +0000550 do_line (pfile, NULL);
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000551 return 1;
Per Bothner7f2935c1995-03-16 13:59:07 -0800552 }
553
Zack Weinberg40c79d52000-01-12 00:35:36 +0000554 /* If we are rescanning preprocessed input, don't obey any directives
555 other than # nnn. */
Zack Weinberg88ae23e2000-03-08 23:35:19 +0000556 if (CPP_OPTIONS (pfile)->preprocessed)
Zack Weinberg40c79d52000-01-12 00:35:36 +0000557 return 0;
558
Mike Stump0f413021996-07-03 22:07:53 +0000559 /* Now find the directive name. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800560 CPP_PUTC (pfile, '#');
561 parse_name (pfile, GETC());
562 ident = pfile->token_buffer + old_written + 1;
563 ident_length = CPP_PWRITTEN (pfile) - ident;
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000564 if (ident_length == 0)
Per Bothner7f2935c1995-03-16 13:59:07 -0800565 {
Zack Weinbergeaefae02000-02-06 08:24:22 +0000566 /* A line of just `#' becomes blank. A line with something
Zack Weinbergc1212d22000-02-06 23:46:18 +0000567 other than an identifier after the # is reparsed as a non-
568 directive line. */
Zack Weinbergeaefae02000-02-06 08:24:22 +0000569 CPP_SET_WRITTEN (pfile, old_written);
570 return (PEEKC() == '\n');
Per Bothner7f2935c1995-03-16 13:59:07 -0800571 }
572
Zack Weinbergeaefae02000-02-06 08:24:22 +0000573 /* Decode the keyword and call the appropriate expansion routine. */
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000574 for (kt = directive_table; ; kt++)
Per Bothner7f2935c1995-03-16 13:59:07 -0800575 {
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000576 if (kt->length <= 0)
Zack Weinbergeaefae02000-02-06 08:24:22 +0000577 /* # identifier, but not a legit directive. Pass onward as a
578 CPP_DIRECTIVE token anyway - let the consumer worry about it. */
579 return 1;
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000580 if (kt->length == ident_length
581 && !strncmp (kt->name, ident, ident_length))
582 break;
Per Bothner7f2935c1995-03-16 13:59:07 -0800583 }
Jeff Lawe9a25f71997-11-02 14:19:36 -0700584
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000585 CPP_SET_WRITTEN (pfile, old_written);
Zack Weinberg5237f532000-02-02 21:41:35 +0000586
587 if (pfile->no_directives)
588 {
589 cpp_error (pfile, "`#%s' may not be used inside a macro argument",
590 kt->name);
591 skip_rest_of_line (pfile);
592 }
593 else
594 (*kt->func) (pfile, kt);
Per Bothner7f2935c1995-03-16 13:59:07 -0800595
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000596 return 1;
Per Bothner7f2935c1995-03-16 13:59:07 -0800597}
598
599/* Pass a directive through to the output file.
600 BUF points to the contents of the directive, as a contiguous string.
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000601 LEN is the length of the string pointed to by BUF.
Per Bothner7f2935c1995-03-16 13:59:07 -0800602 KEYWORD is the keyword-table entry for the directive. */
603
604static void
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000605pass_thru_directive (buf, len, pfile, keyword)
Kaveh R. Ghazibcc5cac1999-09-07 15:41:26 +0000606 const U_CHAR *buf;
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000607 size_t len;
Per Bothner7f2935c1995-03-16 13:59:07 -0800608 cpp_reader *pfile;
Zack Weinberg2ac93491999-08-31 19:46:18 +0000609 const struct directive *keyword;
Per Bothner7f2935c1995-03-16 13:59:07 -0800610{
611 register unsigned keyword_length = keyword->length;
612
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000613 CPP_RESERVE (pfile, 1 + keyword_length + len);
Per Bothner7f2935c1995-03-16 13:59:07 -0800614 CPP_PUTC_Q (pfile, '#');
615 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000616 if (len != 0 && buf[0] != ' ')
Per Bothner7f2935c1995-03-16 13:59:07 -0800617 CPP_PUTC_Q (pfile, ' ');
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000618 CPP_PUTS_Q (pfile, buf, len);
Per Bothner7f2935c1995-03-16 13:59:07 -0800619}
Per Bothner7f2935c1995-03-16 13:59:07 -0800620
Zack Weinbergba412f12000-03-01 00:57:09 +0000621/* Subroutine of do_define: determine the name of the macro to be
622 defined. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800623
Zack Weinbergba412f12000-03-01 00:57:09 +0000624static int
625get_macro_name (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800626 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800627{
Zack Weinbergba412f12000-03-01 00:57:09 +0000628 long here, len;
Per Bothner7f2935c1995-03-16 13:59:07 -0800629
Zack Weinbergba412f12000-03-01 00:57:09 +0000630 here = CPP_WRITTEN (pfile);
631 pfile->no_macro_expand++;
632 if (get_directive_token (pfile) != CPP_NAME)
633 {
634 cpp_error (pfile, "`#define' must be followed by an identifier");
635 goto invalid;
636 }
637
638 len = CPP_WRITTEN (pfile) - here;
639 if (len == 7 && !strncmp (pfile->token_buffer + here, "defined", 7))
640 {
641 cpp_error (pfile, "`defined' is not a legal macro name");
642 goto invalid;
643 }
644
645 pfile->no_macro_expand--;
646 return len;
647
648 invalid:
649 skip_rest_of_line (pfile);
650 pfile->no_macro_expand--;
651 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -0800652}
653
Per Bothner7f2935c1995-03-16 13:59:07 -0800654/* Process a #define command.
Geoff Keatingfc009f91999-09-09 04:00:37 +0000655 KEYWORD is the keyword-table entry for #define,
Zack Weinbergba412f12000-03-01 00:57:09 +0000656 or NULL for a "predefined" macro. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800657
658static int
Zack Weinberg941e09b1998-12-15 11:17:06 +0000659do_define (pfile, keyword)
Per Bothner7f2935c1995-03-16 13:59:07 -0800660 cpp_reader *pfile;
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000661 const struct directive *keyword ATTRIBUTE_UNUSED;
Per Bothner7f2935c1995-03-16 13:59:07 -0800662{
Zack Weinbergd35364d2000-03-12 23:46:05 +0000663 HASHNODE **slot;
Zack Weinbergba412f12000-03-01 00:57:09 +0000664 DEFINITION *def;
Zack Weinberg941e09b1998-12-15 11:17:06 +0000665 long here;
Zack Weinbergd35364d2000-03-12 23:46:05 +0000666 unsigned long hash;
Zack Weinbergba412f12000-03-01 00:57:09 +0000667 int len, c;
668 int funlike = 0;
669 U_CHAR *sym;
Zack Weinberg941e09b1998-12-15 11:17:06 +0000670
671 here = CPP_WRITTEN (pfile);
Zack Weinbergba412f12000-03-01 00:57:09 +0000672 len = get_macro_name (pfile);
673 if (len == 0)
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000674 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -0800675
Zack Weinbergba412f12000-03-01 00:57:09 +0000676 /* Copy out the name so we can pop the token buffer. */
677 len = CPP_WRITTEN (pfile) - here;
678 sym = (U_CHAR *) alloca (len + 1);
679 memcpy (sym, pfile->token_buffer + here, len);
680 sym[len] = '\0';
681 CPP_SET_WRITTEN (pfile, here);
682
683 /* If the next character, with no intervening whitespace, is '(',
684 then this is a function-like macro. */
685 c = PEEKC ();
686 if (c == '(')
687 funlike = 1;
688 else if (c != '\n' && !is_hspace (c))
689 /* Otherwise, C99 requires white space after the name. We treat it
690 as an object-like macro if this happens, with a warning. */
691 cpp_pedwarn (pfile, "missing white space after `#define %.*s'", len, sym);
692
Zack Weinbergb0699da2000-03-07 20:58:47 +0000693 def = _cpp_create_definition (pfile, funlike);
Zack Weinbergba412f12000-03-01 00:57:09 +0000694 if (def == 0)
695 return 0;
696
Zack Weinbergd35364d2000-03-12 23:46:05 +0000697 slot = _cpp_lookup_slot (pfile, sym, len, 1, &hash);
698 if (*slot)
Per Bothner7f2935c1995-03-16 13:59:07 -0800699 {
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000700 int ok;
Zack Weinbergd35364d2000-03-12 23:46:05 +0000701 HASHNODE *hp = *slot;
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000702
Per Bothner7f2935c1995-03-16 13:59:07 -0800703 /* Redefining a macro is ok if the definitions are the same. */
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000704 if (hp->type == T_MACRO)
Zack Weinbergb0699da2000-03-07 20:58:47 +0000705 ok = ! _cpp_compare_defs (pfile, def, hp->value.defn);
Per Bothner7f2935c1995-03-16 13:59:07 -0800706 /* Redefining a constant is ok with -D. */
Zack Weinberg09618161999-02-09 07:31:59 +0000707 else if (hp->type == T_CONST || hp->type == T_STDC)
Per Bothner7f2935c1995-03-16 13:59:07 -0800708 ok = ! CPP_OPTIONS (pfile)->done_initializing;
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000709 /* Otherwise it's not ok. */
710 else
711 ok = 0;
Geoff Keatingfc009f91999-09-09 04:00:37 +0000712 /* Print the warning or error if it's not ok. */
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000713 if (! ok)
Per Bothner7f2935c1995-03-16 13:59:07 -0800714 {
Geoff Keatingfc009f91999-09-09 04:00:37 +0000715 if (hp->type == T_POISON)
Zack Weinbergba412f12000-03-01 00:57:09 +0000716 cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
Geoff Keatingfc009f91999-09-09 04:00:37 +0000717 else
Zack Weinbergba412f12000-03-01 00:57:09 +0000718 cpp_pedwarn (pfile, "`%.*s' redefined", len, sym);
Zack Weinberg59495f32000-01-30 03:31:59 +0000719 if (hp->type == T_MACRO && CPP_OPTIONS (pfile)->done_initializing)
Zack Weinbergba412f12000-03-01 00:57:09 +0000720 {
721 DEFINITION *d = hp->value.defn;
722 cpp_pedwarn_with_file_and_line (pfile, d->file, d->line, d->col,
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000723 "this is the location of the previous definition");
Zack Weinbergba412f12000-03-01 00:57:09 +0000724 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800725 }
Geoff Keatingfc009f91999-09-09 04:00:37 +0000726 if (hp->type != T_POISON)
727 {
728 /* Replace the old definition. */
Zack Weinbergf9ba4282000-02-14 07:57:30 +0000729 if (hp->type == T_MACRO)
Zack Weinbergb0699da2000-03-07 20:58:47 +0000730 _cpp_free_definition (hp->value.defn);
Zack Weinberga2a76ce2000-02-11 20:17:27 +0000731 hp->type = T_MACRO;
Zack Weinbergba412f12000-03-01 00:57:09 +0000732 hp->value.defn = def;
Geoff Keatingfc009f91999-09-09 04:00:37 +0000733 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800734 }
735 else
Zack Weinbergd35364d2000-03-12 23:46:05 +0000736 {
737 HASHNODE *hp = _cpp_make_hashnode (sym, len, T_MACRO, hash);
738 hp->value.defn = def;
739 *slot = hp;
740 }
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000741
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000742 if (CPP_OPTIONS (pfile)->debug_output
743 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
Zack Weinbergb0699da2000-03-07 20:58:47 +0000744 _cpp_dump_definition (pfile, sym, len, def);
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000745 else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
746 pass_thru_directive (sym, len, pfile, keyword);
Per Bothner7f2935c1995-03-16 13:59:07 -0800747
748 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -0800749}
750
Per Bothner7f2935c1995-03-16 13:59:07 -0800751
Per Bothnerd013f051996-06-05 16:25:48 -0700752/* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
753 If BUFFER != NULL, then use the LENGTH characters in BUFFER
754 as the new input buffer.
Mike Stump0f413021996-07-03 22:07:53 +0000755 Return the new buffer, or NULL on failure. */
Per Bothnerd013f051996-06-05 16:25:48 -0700756
Mike Stump0f413021996-07-03 22:07:53 +0000757cpp_buffer *
Per Bothner7f2935c1995-03-16 13:59:07 -0800758cpp_push_buffer (pfile, buffer, length)
759 cpp_reader *pfile;
Neil Booth7ceb3592000-03-11 00:49:44 +0000760 const U_CHAR *buffer;
Per Bothner7f2935c1995-03-16 13:59:07 -0800761 long length;
762{
Zack Weinberg4d9a1b41999-02-15 14:04:21 +0000763 cpp_buffer *buf = CPP_BUFFER (pfile);
764 cpp_buffer *new;
765 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
Per Bothnere2f79f31996-06-07 00:30:20 -0700766 {
Zack Weinberg4d9a1b41999-02-15 14:04:21 +0000767 cpp_fatal (pfile, "macro or `#include' recursion too deep");
Per Bothnere2f79f31996-06-07 00:30:20 -0700768 return NULL;
769 }
Zack Weinberg4d9a1b41999-02-15 14:04:21 +0000770
Jeffrey A Law236d1431999-09-08 08:24:39 +0000771 new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
Zack Weinberg4d9a1b41999-02-15 14:04:21 +0000772
773 new->if_stack = pfile->if_stack;
774 new->cleanup = null_cleanup;
Zack Weinberg4d9a1b41999-02-15 14:04:21 +0000775 new->buf = new->cur = buffer;
776 new->alimit = new->rlimit = buffer + length;
777 new->prev = buf;
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000778 new->mark = -1;
Zack Weinbergba412f12000-03-01 00:57:09 +0000779 new->line_base = NULL;
Zack Weinberg4d9a1b41999-02-15 14:04:21 +0000780
781 CPP_BUFFER (pfile) = new;
782 return new;
Per Bothner7f2935c1995-03-16 13:59:07 -0800783}
784
Mike Stump0f413021996-07-03 22:07:53 +0000785cpp_buffer *
Per Bothner7f2935c1995-03-16 13:59:07 -0800786cpp_pop_buffer (pfile)
787 cpp_reader *pfile;
788{
789 cpp_buffer *buf = CPP_BUFFER (pfile);
Zack Weinberg564ad5f2000-02-10 00:26:47 +0000790 if (ACTIVE_MARK_P())
791 cpp_ice (pfile, "mark active in cpp_pop_buffer");
Per Bothner7f2935c1995-03-16 13:59:07 -0800792 (*buf->cleanup) (buf, pfile);
Zack Weinberg4d9a1b41999-02-15 14:04:21 +0000793 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
794 free (buf);
795 pfile->buffer_stack_depth--;
796 return CPP_BUFFER (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -0800797}
798
799/* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
Mike Stump0f413021996-07-03 22:07:53 +0000800 Pop the buffer when done. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800801
802void
803cpp_scan_buffer (pfile)
804 cpp_reader *pfile;
805{
806 cpp_buffer *buffer = CPP_BUFFER (pfile);
Zack Weinberg5d83f441999-08-04 20:39:33 +0000807 enum cpp_token token;
808 if (CPP_OPTIONS (pfile)->no_output)
Per Bothner7f2935c1995-03-16 13:59:07 -0800809 {
Zack Weinberg5d83f441999-08-04 20:39:33 +0000810 long old_written = CPP_WRITTEN (pfile);
811 /* In no-output mode, we can ignore everything but directives. */
812 for (;;)
Per Bothner7f2935c1995-03-16 13:59:07 -0800813 {
Zack Weinberg5d83f441999-08-04 20:39:33 +0000814 if (! pfile->only_seen_white)
815 skip_rest_of_line (pfile);
816 token = cpp_get_token (pfile);
817 if (token == CPP_EOF) /* Should not happen ... */
818 break;
819 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
820 {
Zack Weinberg38b24ee2000-03-08 20:37:23 +0000821 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != NULL)
Zack Weinberg5d83f441999-08-04 20:39:33 +0000822 cpp_pop_buffer (pfile);
823 break;
824 }
825 }
826 CPP_SET_WRITTEN (pfile, old_written);
827 }
828 else
829 {
830 for (;;)
831 {
832 token = cpp_get_token (pfile);
833 if (token == CPP_EOF) /* Should not happen ... */
834 break;
835 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
836 {
Zack Weinberg38b24ee2000-03-08 20:37:23 +0000837 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != NULL)
Zack Weinberg5d83f441999-08-04 20:39:33 +0000838 cpp_pop_buffer (pfile);
839 break;
840 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800841 }
842 }
843}
844
845/*
Per Bothner782331f1995-04-30 14:43:12 -0700846 * Rescan a string (which may have escape marks) into pfile's buffer.
Per Bothner7f2935c1995-03-16 13:59:07 -0800847 * Place the result in pfile->token_buffer.
848 *
849 * The input is copied before it is scanned, so it is safe to pass
850 * it something from the token_buffer that will get overwritten
851 * (because it follows CPP_WRITTEN). This is used by do_include.
Per Bothner7f2935c1995-03-16 13:59:07 -0800852 */
853
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000854void
Per Bothner7f2935c1995-03-16 13:59:07 -0800855cpp_expand_to_buffer (pfile, buf, length)
856 cpp_reader *pfile;
Kaveh R. Ghazibcc5cac1999-09-07 15:41:26 +0000857 const U_CHAR *buf;
Per Bothner7f2935c1995-03-16 13:59:07 -0800858 int length;
859{
860 register cpp_buffer *ip;
Per Bothner7f2935c1995-03-16 13:59:07 -0800861 U_CHAR *buf1;
Zack Weinberg5d83f441999-08-04 20:39:33 +0000862 int save_no_output;
Per Bothner7f2935c1995-03-16 13:59:07 -0800863
864 if (length < 0)
Zack Weinberg34ca9541999-04-14 09:40:56 +0000865 {
Zack Weinbergc1212d22000-02-06 23:46:18 +0000866 cpp_ice (pfile, "length < 0 in cpp_expand_to_buffer");
Zack Weinberg34ca9541999-04-14 09:40:56 +0000867 return;
868 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800869
870 /* Set up the input on the input stack. */
871
872 buf1 = (U_CHAR *) alloca (length + 1);
Zack Weinberg5538ada1999-02-04 06:36:54 -0500873 memcpy (buf1, buf, length);
Per Bothner7f2935c1995-03-16 13:59:07 -0800874 buf1[length] = 0;
875
876 ip = cpp_push_buffer (pfile, buf1, length);
Per Bothnere2f79f31996-06-07 00:30:20 -0700877 if (ip == NULL)
878 return;
Per Bothner782331f1995-04-30 14:43:12 -0700879 ip->has_escapes = 1;
Per Bothner7f2935c1995-03-16 13:59:07 -0800880
881 /* Scan the input, create the output. */
Zack Weinberg5d83f441999-08-04 20:39:33 +0000882 save_no_output = CPP_OPTIONS (pfile)->no_output;
883 CPP_OPTIONS (pfile)->no_output = 0;
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000884 CPP_OPTIONS (pfile)->no_line_commands++;
Per Bothner7f2935c1995-03-16 13:59:07 -0800885 cpp_scan_buffer (pfile);
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000886 CPP_OPTIONS (pfile)->no_line_commands--;
Zack Weinberg5d83f441999-08-04 20:39:33 +0000887 CPP_OPTIONS (pfile)->no_output = save_no_output;
Per Bothner7f2935c1995-03-16 13:59:07 -0800888
Per Bothner7f2935c1995-03-16 13:59:07 -0800889 CPP_NUL_TERMINATE (pfile);
890}
891
Per Bothner7f2935c1995-03-16 13:59:07 -0800892void
893cpp_buf_line_and_col (pbuf, linep, colp)
894 register cpp_buffer *pbuf;
895 long *linep, *colp;
896{
Per Bothner7f2935c1995-03-16 13:59:07 -0800897 if (pbuf)
898 {
899 *linep = pbuf->lineno;
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000900 if (colp)
901 *colp = pbuf->cur - pbuf->line_base;
Per Bothner7f2935c1995-03-16 13:59:07 -0800902 }
903 else
904 {
905 *linep = 0;
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000906 if (colp)
907 *colp = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -0800908 }
909}
910
Mike Stump0f413021996-07-03 22:07:53 +0000911/* Return the cpp_buffer that corresponds to a file (not a macro). */
Per Bothner7f2935c1995-03-16 13:59:07 -0800912
Mike Stump0f413021996-07-03 22:07:53 +0000913cpp_buffer *
Per Bothner7f2935c1995-03-16 13:59:07 -0800914cpp_file_buffer (pfile)
915 cpp_reader *pfile;
916{
Zack Weinberg38b24ee2000-03-08 20:37:23 +0000917 cpp_buffer *ip;
Per Bothner7f2935c1995-03-16 13:59:07 -0800918
Zack Weinberg38b24ee2000-03-08 20:37:23 +0000919 for (ip = CPP_BUFFER (pfile); ip; ip = CPP_PREV_BUFFER (ip))
920 if (ip->ihash != NULL)
Per Bothner7f2935c1995-03-16 13:59:07 -0800921 return ip;
922 return NULL;
923}
924
Per Bothner7f2935c1995-03-16 13:59:07 -0800925/*
926 * write out a #line command, for instance, after an #include file.
Per Bothner7f2935c1995-03-16 13:59:07 -0800927 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
928 */
929
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000930void
Zack Weinberg80e9dcb1999-04-19 11:55:04 +0000931output_line_command (pfile, file_change)
Per Bothner7f2935c1995-03-16 13:59:07 -0800932 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800933 enum file_change_code file_change;
934{
Zack Weinberg80e9dcb1999-04-19 11:55:04 +0000935 long line;
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000936 cpp_buffer *ip;
Per Bothner7f2935c1995-03-16 13:59:07 -0800937
Per Bothnere2f79f31996-06-07 00:30:20 -0700938 if (CPP_OPTIONS (pfile)->no_line_commands
939 || CPP_OPTIONS (pfile)->no_output)
940 return;
941
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000942 ip = cpp_file_buffer (pfile);
943 cpp_buf_line_and_col (ip, &line, NULL);
Per Bothner7f2935c1995-03-16 13:59:07 -0800944
Zack Weinberg80e9dcb1999-04-19 11:55:04 +0000945 /* If the current file has not changed, we omit the #line if it would
946 appear to be a no-op, and we output a few newlines instead
947 if we want to increase the line number by a small amount.
948 We cannot do this if pfile->lineno is zero, because that means we
949 haven't output any line commands yet. (The very first line command
950 output is a `same_file' command.) */
951 if (file_change == same_file && pfile->lineno != 0)
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000952 {
953 if (line == pfile->lineno)
954 return;
Richard Kennera8259c71995-07-01 11:57:25 -0400955
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000956 /* If the inherited line number is a little too small,
957 output some newlines instead of a #line command. */
958 if (line > pfile->lineno && line < pfile->lineno + 8)
959 {
960 CPP_RESERVE (pfile, 20);
961 while (line > pfile->lineno)
962 {
963 CPP_PUTC_Q (pfile, '\n');
964 pfile->lineno++;
965 }
966 return;
967 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800968 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800969
970 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000971 CPP_PUTS_Q (pfile, "# ", 2);
Per Bothner7f2935c1995-03-16 13:59:07 -0800972
Jeff Lawe9a25f71997-11-02 14:19:36 -0700973 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
Per Bothner7f2935c1995-03-16 13:59:07 -0800974 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
975
976 quote_string (pfile, ip->nominal_fname);
Alexandre Oliva656ac112000-02-27 07:57:29 +0000977 if (file_change != same_file && file_change != rename_file)
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000978 {
979 CPP_PUTC_Q (pfile, ' ');
980 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
981 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800982 /* Tell cc1 if following text comes from a system header file. */
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000983 if (ip->system_header_p)
984 {
985 CPP_PUTC_Q (pfile, ' ');
986 CPP_PUTC_Q (pfile, '3');
987 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800988#ifndef NO_IMPLICIT_EXTERN_C
989 /* Tell cc1plus if following text should be treated as C. */
Zack Weinberg3fdc6511999-03-16 13:10:15 +0000990 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
991 {
992 CPP_PUTC_Q (pfile, ' ');
993 CPP_PUTC_Q (pfile, '4');
994 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800995#endif
996 CPP_PUTC_Q (pfile, '\n');
997 pfile->lineno = line;
998}
Per Bothner7f2935c1995-03-16 13:59:07 -0800999
Zack Weinberg20dc7361999-01-25 11:23:01 +00001000
Per Bothner7f2935c1995-03-16 13:59:07 -08001001/* Like cpp_get_token, except that it does not read past end-of-line.
1002 Also, horizontal space is skipped, and macros are popped. */
1003
Zack Weinbergcf4ed942000-02-10 23:47:04 +00001004enum cpp_token
Per Bothner7f2935c1995-03-16 13:59:07 -08001005get_directive_token (pfile)
1006 cpp_reader *pfile;
1007{
Zack Weinbergeaefae02000-02-06 08:24:22 +00001008 long old_written = CPP_WRITTEN (pfile);
1009 enum cpp_token token;
1010
Per Bothner7f2935c1995-03-16 13:59:07 -08001011 for (;;)
1012 {
Per Bothner7f2935c1995-03-16 13:59:07 -08001013 cpp_skip_hspace (pfile);
1014 if (PEEKC () == '\n')
Zack Weinbergeaefae02000-02-06 08:24:22 +00001015 return CPP_VSPACE;
1016
Per Bothner7f2935c1995-03-16 13:59:07 -08001017 token = cpp_get_token (pfile);
Zack Weinbergeaefae02000-02-06 08:24:22 +00001018 /* token could be hspace at the beginning of a macro. */
1019 if (token == CPP_HSPACE || token == CPP_COMMENT)
1020 {
Per Bothner7f2935c1995-03-16 13:59:07 -08001021 CPP_SET_WRITTEN (pfile, old_written);
Zack Weinbergeaefae02000-02-06 08:24:22 +00001022 continue;
1023 }
1024
1025 /* token cannot be vspace, it would have been caught above. */
1026 if (token == CPP_VSPACE)
1027 {
Zack Weinbergc1212d22000-02-06 23:46:18 +00001028 cpp_ice (pfile, "VSPACE in get_directive_token");
Per Bothner7f2935c1995-03-16 13:59:07 -08001029 return token;
Zack Weinbergeaefae02000-02-06 08:24:22 +00001030 }
1031
1032 /* token cannot be POP unless the buffer is a macro buffer. */
1033 if (token != CPP_POP)
1034 return token;
1035
1036 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1037 {
Zack Weinbergc1212d22000-02-06 23:46:18 +00001038 cpp_ice (pfile, "POP of file buffer in get_directive_token");
Zack Weinbergeaefae02000-02-06 08:24:22 +00001039 return token;
1040 }
1041
Zack Weinbergc1212d22000-02-06 23:46:18 +00001042 /* We must pop the buffer by hand, or else cpp_get_token might
1043 hand us white space or newline on the next invocation. */
Zack Weinbergeaefae02000-02-06 08:24:22 +00001044 cpp_pop_buffer (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08001045 }
1046}
1047
1048/* Handle #include and #import.
1049 This function expects to see "fname" or <fname> on the input.
1050
1051 The input is normally in part of the output_buffer following
Richard Kennerddd5a7c1995-05-16 08:14:26 -04001052 CPP_WRITTEN, and will get overwritten by output_line_command.
Per Bothner7f2935c1995-03-16 13:59:07 -08001053 I.e. in input file specification has been popped by handle_directive.
Mike Stump0f413021996-07-03 22:07:53 +00001054 This is safe. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001055
1056static int
Zack Weinberg941e09b1998-12-15 11:17:06 +00001057do_include (pfile, keyword)
Per Bothner7f2935c1995-03-16 13:59:07 -08001058 cpp_reader *pfile;
Zack Weinberg2ac93491999-08-31 19:46:18 +00001059 const struct directive *keyword;
Per Bothner7f2935c1995-03-16 13:59:07 -08001060{
1061 int importing = (keyword->type == T_IMPORT);
1062 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
Zack Weinbergadd70911998-10-29 11:54:13 +00001063 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001064 int before; /* included before? */
Zack Weinbergadd70911998-10-29 11:54:13 +00001065 long flen;
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001066 unsigned char *ftok;
Zack Weinbergf1a86df1998-12-07 13:35:20 +00001067 cpp_buffer *fp;
Zack Weinbergadd70911998-10-29 11:54:13 +00001068
Per Bothner7f2935c1995-03-16 13:59:07 -08001069 enum cpp_token token;
1070
1071 /* Chain of dirs to search */
Zack Weinberg38b24ee2000-03-08 20:37:23 +00001072 IHASH *ihash;
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001073 struct file_name_list *search_start;
Zack Weinbergadd70911998-10-29 11:54:13 +00001074
Per Bothner7f2935c1995-03-16 13:59:07 -08001075 long old_written = CPP_WRITTEN (pfile);
1076
Zack Weinbergadd70911998-10-29 11:54:13 +00001077 int fd;
Per Bothner7f2935c1995-03-16 13:59:07 -08001078
Jason Merrill83ecd272000-03-03 00:09:22 +00001079 if (CPP_PEDANTIC (pfile))
Richard Kennercfb3ee11997-04-13 14:30:13 -04001080 {
1081 if (importing)
1082 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
1083 if (skip_dirs)
1084 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
1085 }
1086
Per Bothner7f2935c1995-03-16 13:59:07 -08001087 if (importing && CPP_OPTIONS (pfile)->warn_import
1088 && !CPP_OPTIONS (pfile)->inhibit_warnings
1089 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
1090 {
1091 pfile->import_warning = 1;
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001092 cpp_warning (pfile,
1093 "#import is obsolete, use an #ifndef wrapper in the header file");
Per Bothner7f2935c1995-03-16 13:59:07 -08001094 }
1095
1096 pfile->parsing_include_directive++;
1097 token = get_directive_token (pfile);
1098 pfile->parsing_include_directive--;
1099
1100 if (token == CPP_STRING)
1101 {
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001102 if (pfile->token_buffer[old_written] == '<')
1103 angle_brackets = 1;
Per Bothner7f2935c1995-03-16 13:59:07 -08001104 }
1105#ifdef VMS
1106 else if (token == CPP_NAME)
1107 {
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001108 /* Support '#include xyz' like VAX-C. It is taken as
1109 '#include <xyz.h>' and generates a warning. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001110 cpp_warning (pfile,
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001111 "`#include filename' is obsolete, use `#include <filename.h>'");
Per Bothner7f2935c1995-03-16 13:59:07 -08001112 angle_brackets = 1;
Zack Weinbergadd70911998-10-29 11:54:13 +00001113
1114 /* Append the missing `.h' to the name. */
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001115 CPP_PUTS (pfile, ".h", 2);
Per Bothner7f2935c1995-03-16 13:59:07 -08001116 }
1117#endif
1118 else
1119 {
1120 cpp_error (pfile,
1121 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1122 CPP_SET_WRITTEN (pfile, old_written);
1123 skip_rest_of_line (pfile);
1124 return 0;
1125 }
1126
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001127 flen = CPP_WRITTEN (pfile) - old_written;
Dave Brolleye7553be1999-06-07 10:35:27 +00001128 ftok = (unsigned char *) alloca (flen + 1);
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001129 memcpy (ftok, pfile->token_buffer + old_written, flen);
1130 ftok[flen] = '\0';
1131
1132 if (get_directive_token (pfile) != CPP_VSPACE)
Per Bothner7f2935c1995-03-16 13:59:07 -08001133 {
1134 cpp_error (pfile, "junk at end of `#include'");
Zack Weinbergadd70911998-10-29 11:54:13 +00001135 skip_rest_of_line (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08001136 }
1137
Per Bothner7f2935c1995-03-16 13:59:07 -08001138 CPP_SET_WRITTEN (pfile, old_written);
1139
Per Bothner7f2935c1995-03-16 13:59:07 -08001140 if (flen == 0)
1141 {
1142 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
1143 return 0;
1144 }
Zack Weinbergf1a86df1998-12-07 13:35:20 +00001145
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001146 if (CPP_OPTIONS (pfile)->dump_includes)
1147 pass_thru_directive (ftok,
1148 flen
1149#ifdef VMS
1150 - ((token == CPP_NAME) ? 2 : 0)
1151#endif
1152 , pfile, keyword);
1153
1154#ifdef VMS
1155 if (token == CPP_STRING)
1156#endif
1157 {
1158 ftok++;
1159 flen -= 2;
1160 ftok[flen] = '\0';
1161 }
1162
Zack Weinbergf1a86df1998-12-07 13:35:20 +00001163 search_start = 0;
1164
Zack Weinberg38b24ee2000-03-08 20:37:23 +00001165 fp = cpp_file_buffer (pfile);
Zack Weinbergf1a86df1998-12-07 13:35:20 +00001166
Zack Weinberg692b8721998-12-16 13:23:47 +00001167 /* For #include_next, skip in the search path past the dir in which the
1168 containing file was found. Treat files specified using an absolute path
1169 as if there are no more directories to search. Treat the primary source
1170 file like any other included source, but generate a warning. */
Zack Weinberg38b24ee2000-03-08 20:37:23 +00001171 if (skip_dirs && CPP_PREV_BUFFER (fp))
Zack Weinberg692b8721998-12-16 13:23:47 +00001172 {
1173 if (fp->ihash->foundhere != ABSOLUTE_PATH)
Zack Weinbergf1a86df1998-12-07 13:35:20 +00001174 search_start = fp->ihash->foundhere->next;
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001175 }
1176 else
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001177 {
Zack Weinberg692b8721998-12-16 13:23:47 +00001178 if (skip_dirs)
1179 cpp_warning (pfile, "#include_next in primary source file");
1180
Zack Weinbergf1a86df1998-12-07 13:35:20 +00001181 if (angle_brackets)
1182 search_start = CPP_OPTIONS (pfile)->bracket_include;
1183 else
1184 {
1185 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
1186 {
1187 if (fp)
1188 search_start = fp->actual_dir;
1189 }
1190 else
1191 search_start = CPP_OPTIONS (pfile)->quote_include;
1192 }
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001193 }
1194
1195 if (!search_start)
1196 {
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001197 cpp_error (pfile, "No include path in which to find %s", ftok);
Zack Weinbergadd70911998-10-29 11:54:13 +00001198 return 0;
1199 }
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001200
Zack Weinbergb0699da2000-03-07 20:58:47 +00001201 fd = _cpp_find_include_file (pfile, ftok, search_start, &ihash, &before);
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001202
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001203 if (fd == -2)
1204 return 0;
Zack Weinbergadd70911998-10-29 11:54:13 +00001205
1206 if (fd == -1)
1207 {
1208 if (CPP_OPTIONS (pfile)->print_deps_missing_files
1209 && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
1210 (pfile->system_include_depth > 0)))
1211 {
1212 if (!angle_brackets)
Zack Weinberg49e6c082000-03-04 19:42:04 +00001213 deps_add_dep (pfile->deps, ftok);
Zack Weinbergadd70911998-10-29 11:54:13 +00001214 else
Per Bothner7f2935c1995-03-16 13:59:07 -08001215 {
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001216 char *p;
1217 struct file_name_list *ptr;
Zack Weinbergadd70911998-10-29 11:54:13 +00001218 /* If requested as a system header, assume it belongs in
1219 the first system header directory. */
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001220 if (CPP_OPTIONS (pfile)->bracket_include)
1221 ptr = CPP_OPTIONS (pfile)->bracket_include;
Zack Weinbergadd70911998-10-29 11:54:13 +00001222 else
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001223 ptr = CPP_OPTIONS (pfile)->quote_include;
Per Bothner7f2935c1995-03-16 13:59:07 -08001224
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001225 p = (char *) alloca (strlen (ptr->name)
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001226 + strlen (ftok) + 2);
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001227 if (*ptr->name != '\0')
1228 {
1229 strcpy (p, ptr->name);
1230 strcat (p, "/");
1231 }
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001232 strcat (p, ftok);
Zack Weinberg49e6c082000-03-04 19:42:04 +00001233 deps_add_dep (pfile->deps, p);
Per Bothner7f2935c1995-03-16 13:59:07 -08001234 }
1235 }
Zack Weinbergadd70911998-10-29 11:54:13 +00001236 /* If -M was specified, and this header file won't be added to
1237 the dependency list, then don't count this as an error,
1238 because we can still produce correct output. Otherwise, we
1239 can't produce correct output, because there may be
1240 dependencies we need inside the missing file, and we don't
1241 know what directory this missing file exists in. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001242 else if (CPP_PRINT_DEPS (pfile)
1243 && (CPP_PRINT_DEPS (pfile)
1244 <= (angle_brackets || (pfile->system_include_depth > 0))))
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001245 cpp_warning (pfile, "No include path in which to find %s", ftok);
Per Bothner7f2935c1995-03-16 13:59:07 -08001246 else
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001247 cpp_error_from_errno (pfile, ftok);
Per Bothner7f2935c1995-03-16 13:59:07 -08001248
Zack Weinbergadd70911998-10-29 11:54:13 +00001249 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001250 }
1251
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001252 /* For -M, add the file to the dependencies on its first inclusion. */
1253 if (!before && (CPP_PRINT_DEPS (pfile)
1254 > (angle_brackets || (pfile->system_include_depth > 0))))
Zack Weinberg49e6c082000-03-04 19:42:04 +00001255 deps_add_dep (pfile->deps, ihash->name);
Per Bothner7f2935c1995-03-16 13:59:07 -08001256
Zack Weinbergadd70911998-10-29 11:54:13 +00001257 /* Handle -H option. */
1258 if (CPP_OPTIONS(pfile)->print_include_names)
1259 {
Zack Weinbergf1a86df1998-12-07 13:35:20 +00001260 fp = CPP_BUFFER (pfile);
Zack Weinberg38b24ee2000-03-08 20:37:23 +00001261 while ((fp = CPP_PREV_BUFFER (fp)) != NULL)
Zack Weinbergadd70911998-10-29 11:54:13 +00001262 putc ('.', stderr);
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001263 fprintf (stderr, " %s\n", ihash->name);
Zack Weinbergadd70911998-10-29 11:54:13 +00001264 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001265
Zack Weinbergadd70911998-10-29 11:54:13 +00001266 /* Actually process the file */
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001267
1268 if (importing)
Neil Booth7ceb3592000-03-11 00:49:44 +00001269 ihash->control_macro = (const U_CHAR *) "";
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001270
Zack Weinbergb0699da2000-03-07 20:58:47 +00001271 if (_cpp_read_include_file (pfile, fd, ihash))
Zack Weinbergadd70911998-10-29 11:54:13 +00001272 {
Zack Weinberg80e9dcb1999-04-19 11:55:04 +00001273 output_line_command (pfile, enter_file);
Zack Weinbergd35364d2000-03-12 23:46:05 +00001274 if (angle_brackets)
1275 pfile->system_include_depth++; /* Decremented in file_cleanup. */
Zack Weinbergadd70911998-10-29 11:54:13 +00001276 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001277 return 0;
1278}
1279
Jason Merrilld3a34a01999-08-14 00:42:07 +00001280/* Subroutine of do_line. Read next token from PFILE without adding it to
1281 the output buffer. If it is a number between 1 and 4, store it in *NUM
1282 and return 1; otherwise, return 0 and complain if we aren't at the end
1283 of the directive. */
1284
1285static int
1286read_line_number (pfile, num)
1287 cpp_reader *pfile;
1288 int *num;
1289{
1290 long save_written = CPP_WRITTEN (pfile);
1291 U_CHAR *p = pfile->token_buffer + save_written;
1292 enum cpp_token token = get_directive_token (pfile);
1293 CPP_SET_WRITTEN (pfile, save_written);
1294
1295 if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
1296 {
1297 *num = p[0] - '0';
1298 return 1;
1299 }
1300 else
1301 {
Zack Weinbergba412f12000-03-01 00:57:09 +00001302 if (token != CPP_VSPACE && token != CPP_EOF)
Jason Merrilld3a34a01999-08-14 00:42:07 +00001303 cpp_error (pfile, "invalid format `#line' command");
1304 return 0;
1305 }
1306}
1307
Zack Weinberg5538ada1999-02-04 06:36:54 -05001308/* Interpret #line command.
1309 Note that the filename string (if any) is treated as if it were an
1310 include filename. That means no escape handling. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001311
1312static int
Zack Weinberg941e09b1998-12-15 11:17:06 +00001313do_line (pfile, keyword)
Per Bothner7f2935c1995-03-16 13:59:07 -08001314 cpp_reader *pfile;
Zack Weinberg2ac93491999-08-31 19:46:18 +00001315 const struct directive *keyword ATTRIBUTE_UNUSED;
Per Bothner7f2935c1995-03-16 13:59:07 -08001316{
1317 cpp_buffer *ip = CPP_BUFFER (pfile);
1318 int new_lineno;
1319 long old_written = CPP_WRITTEN (pfile);
1320 enum file_change_code file_change = same_file;
1321 enum cpp_token token;
Zack Weinberg5538ada1999-02-04 06:36:54 -05001322 char *x;
Per Bothner7f2935c1995-03-16 13:59:07 -08001323
1324 token = get_directive_token (pfile);
1325
Zack Weinberg5538ada1999-02-04 06:36:54 -05001326 if (token != CPP_NUMBER)
Per Bothner7f2935c1995-03-16 13:59:07 -08001327 {
Zack Weinberg5538ada1999-02-04 06:36:54 -05001328 cpp_error (pfile, "token after `#line' is not an integer");
1329 goto bad_line_directive;
1330 }
1331
1332 new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
1333 if (x[0] != '\0')
1334 {
1335 cpp_error (pfile, "token after `#line' is not an integer");
1336 goto bad_line_directive;
1337 }
1338 CPP_SET_WRITTEN (pfile, old_written);
1339
Alexandre Oliva7113a162000-02-16 08:43:57 +00001340 if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
Zack Weinberg5538ada1999-02-04 06:36:54 -05001341 cpp_pedwarn (pfile, "line number out of range in `#line' command");
1342
1343 token = get_directive_token (pfile);
1344
1345 if (token == CPP_STRING)
1346 {
1347 U_CHAR *fname = pfile->token_buffer + old_written + 1;
1348 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
Jason Merrilld3a34a01999-08-14 00:42:07 +00001349 int action_number = 0;
Zack Weinberg5538ada1999-02-04 06:36:54 -05001350
Alexandre Oliva656ac112000-02-27 07:57:29 +00001351 file_change = rename_file;
1352
Jason Merrilld3a34a01999-08-14 00:42:07 +00001353 if (read_line_number (pfile, &action_number))
Zack Weinberg5538ada1999-02-04 06:36:54 -05001354 {
Zack Weinberg5538ada1999-02-04 06:36:54 -05001355 if (CPP_PEDANTIC (pfile))
1356 cpp_pedwarn (pfile, "garbage at end of `#line' command");
1357
Jason Merrilld3a34a01999-08-14 00:42:07 +00001358 if (action_number == 1)
Zack Weinberg5538ada1999-02-04 06:36:54 -05001359 {
Jason Merrilld3a34a01999-08-14 00:42:07 +00001360 file_change = enter_file;
1361 read_line_number (pfile, &action_number);
Zack Weinberg5538ada1999-02-04 06:36:54 -05001362 }
Jason Merrilld3a34a01999-08-14 00:42:07 +00001363 else if (action_number == 2)
Zack Weinberg5538ada1999-02-04 06:36:54 -05001364 {
Jason Merrilld3a34a01999-08-14 00:42:07 +00001365 file_change = leave_file;
1366 read_line_number (pfile, &action_number);
Zack Weinberg5538ada1999-02-04 06:36:54 -05001367 }
Jason Merrilld3a34a01999-08-14 00:42:07 +00001368 if (action_number == 3)
Zack Weinberg5538ada1999-02-04 06:36:54 -05001369 {
Jason Merrilld3a34a01999-08-14 00:42:07 +00001370 ip->system_header_p = 1;
1371 read_line_number (pfile, &action_number);
1372 }
1373 if (action_number == 4)
1374 {
1375 ip->system_header_p = 2;
1376 read_line_number (pfile, &action_number);
Zack Weinberg5538ada1999-02-04 06:36:54 -05001377 }
1378 }
1379
1380 *end_name = '\0';
1381
1382 if (strcmp (fname, ip->nominal_fname))
1383 {
Zack Weinberga9ae4481999-10-29 04:31:14 +00001384 const char *newname, *oldname;
Zack Weinberg38b24ee2000-03-08 20:37:23 +00001385 if (!strcmp (fname, ip->ihash->name))
1386 newname = ip->ihash->name;
Zack Weinberg5538ada1999-02-04 06:36:54 -05001387 else if (ip->last_nominal_fname
1388 && !strcmp (fname, ip->last_nominal_fname))
1389 newname = ip->last_nominal_fname;
1390 else
1391 newname = xstrdup (fname);
1392
1393 oldname = ip->nominal_fname;
1394 ip->nominal_fname = newname;
1395
1396 if (ip->last_nominal_fname
1397 && ip->last_nominal_fname != oldname
Zack Weinberg4d9a1b41999-02-15 14:04:21 +00001398 && ip->last_nominal_fname != newname
Zack Weinberg38b24ee2000-03-08 20:37:23 +00001399 && ip->last_nominal_fname != ip->ihash->name)
Zack Weinberga9ae4481999-10-29 04:31:14 +00001400 free ((void *) ip->last_nominal_fname);
Zack Weinberg5538ada1999-02-04 06:36:54 -05001401
Zack Weinberg38b24ee2000-03-08 20:37:23 +00001402 if (newname == ip->ihash->name)
Zack Weinberg5538ada1999-02-04 06:36:54 -05001403 ip->last_nominal_fname = NULL;
1404 else
1405 ip->last_nominal_fname = oldname;
1406 }
1407 }
1408 else if (token != CPP_VSPACE && token != CPP_EOF)
1409 {
1410 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
Per Bothner7f2935c1995-03-16 13:59:07 -08001411 goto bad_line_directive;
1412 }
1413
1414 /* The Newline at the end of this line remains to be processed.
1415 To put the next line at the specified line number,
1416 we must store a line number now that is one less. */
Zack Weinberg5538ada1999-02-04 06:36:54 -05001417 ip->lineno = new_lineno - 1;
Per Bothner7f2935c1995-03-16 13:59:07 -08001418 CPP_SET_WRITTEN (pfile, old_written);
Zack Weinberg80e9dcb1999-04-19 11:55:04 +00001419 output_line_command (pfile, file_change);
Per Bothner7f2935c1995-03-16 13:59:07 -08001420 return 0;
Zack Weinberg5538ada1999-02-04 06:36:54 -05001421
1422 bad_line_directive:
1423 skip_rest_of_line (pfile);
1424 CPP_SET_WRITTEN (pfile, old_written);
1425 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001426}
1427
Zack Weinberg5538ada1999-02-04 06:36:54 -05001428/* Remove the definition of a symbol from the symbol table.
1429 According to the C standard, it is not an error to undef
1430 something that has no definitions. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001431static int
Zack Weinberg941e09b1998-12-15 11:17:06 +00001432do_undef (pfile, keyword)
Per Bothner7f2935c1995-03-16 13:59:07 -08001433 cpp_reader *pfile;
Zack Weinberg2ac93491999-08-31 19:46:18 +00001434 const struct directive *keyword;
Per Bothner7f2935c1995-03-16 13:59:07 -08001435{
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001436 int len;
Zack Weinbergd35364d2000-03-12 23:46:05 +00001437 HASHNODE **slot;
Zack Weinberg941e09b1998-12-15 11:17:06 +00001438 U_CHAR *buf, *name, *limit;
1439 int c;
1440 long here = CPP_WRITTEN (pfile);
1441 enum cpp_token token;
1442
1443 cpp_skip_hspace (pfile);
1444 c = GETC();
Zack Weinberga9ae4481999-10-29 04:31:14 +00001445 if (! is_idstart(c))
Zack Weinberg941e09b1998-12-15 11:17:06 +00001446 {
1447 cpp_error (pfile, "token after #undef is not an identifier");
1448 skip_rest_of_line (pfile);
1449 return 1;
1450 }
1451
1452 parse_name (pfile, c);
1453 buf = pfile->token_buffer + here;
1454 limit = CPP_PWRITTEN(pfile);
1455
1456 /* Copy out the token so we can pop the token buffer. */
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001457 len = limit - buf;
1458 name = (U_CHAR *) alloca (len + 1);
1459 memcpy (name, buf, len);
Zack Weinbergba412f12000-03-01 00:57:09 +00001460 name[len] = '\0';
Zack Weinberg941e09b1998-12-15 11:17:06 +00001461
1462 token = get_directive_token (pfile);
Zack Weinbergba412f12000-03-01 00:57:09 +00001463 if (token != CPP_VSPACE)
Zack Weinberg941e09b1998-12-15 11:17:06 +00001464 {
1465 cpp_pedwarn (pfile, "junk on line after #undef");
1466 skip_rest_of_line (pfile);
1467 }
Zack Weinberg941e09b1998-12-15 11:17:06 +00001468 CPP_SET_WRITTEN (pfile, here);
Per Bothner7f2935c1995-03-16 13:59:07 -08001469
Zack Weinbergd35364d2000-03-12 23:46:05 +00001470 slot = _cpp_lookup_slot (pfile, name, len, 0, 0);
1471 if (slot)
Per Bothner7f2935c1995-03-16 13:59:07 -08001472 {
Zack Weinbergd35364d2000-03-12 23:46:05 +00001473 HASHNODE *hp = *slot;
Per Bothner7f2935c1995-03-16 13:59:07 -08001474 /* If we are generating additional info for debugging (with -g) we
1475 need to pass through all effective #undef commands. */
1476 if (CPP_OPTIONS (pfile)->debug_output && keyword)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001477 pass_thru_directive (name, len, pfile, keyword);
Geoff Keatingfc009f91999-09-09 04:00:37 +00001478 if (hp->type == T_POISON)
1479 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
1480 else
1481 {
1482 if (hp->type != T_MACRO)
1483 cpp_warning (pfile, "undefining `%s'", hp->name);
Zack Weinbergd35364d2000-03-12 23:46:05 +00001484
1485 htab_clear_slot (pfile->hashtab, (void **)slot);
Geoff Keatingfc009f91999-09-09 04:00:37 +00001486 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001487 }
1488
Per Bothner7f2935c1995-03-16 13:59:07 -08001489 return 0;
1490}
Zack Weinberg941e09b1998-12-15 11:17:06 +00001491
1492/* Wrap do_undef for -U processing. */
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001493void
Zack Weinberg941e09b1998-12-15 11:17:06 +00001494cpp_undef (pfile, macro)
1495 cpp_reader *pfile;
Neil Booth7ceb3592000-03-11 00:49:44 +00001496 const char *macro;
Zack Weinberg941e09b1998-12-15 11:17:06 +00001497{
Zack Weinberg2387c1d2000-02-09 18:35:41 +00001498 /* Copy the string so we can append a newline. */
1499 size_t len = strlen (macro);
Neil Booth7ceb3592000-03-11 00:49:44 +00001500 char *buf = alloca (len + 2);
Zack Weinberg2387c1d2000-02-09 18:35:41 +00001501 memcpy (buf, macro, len);
1502 buf[len] = '\n';
1503 buf[len + 1] = '\0';
1504 if (cpp_push_buffer (pfile, buf, len + 1))
Zack Weinberg941e09b1998-12-15 11:17:06 +00001505 {
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001506 do_undef (pfile, NULL);
1507 cpp_pop_buffer (pfile);
Zack Weinberg941e09b1998-12-15 11:17:06 +00001508 }
1509}
1510
Per Bothner7f2935c1995-03-16 13:59:07 -08001511/*
1512 * Report an error detected by the program we are processing.
1513 * Use the text of the line in the error message.
1514 * (We use error because it prints the filename & line#.)
1515 */
1516
1517static int
Zack Weinberg941e09b1998-12-15 11:17:06 +00001518do_error (pfile, keyword)
Per Bothner7f2935c1995-03-16 13:59:07 -08001519 cpp_reader *pfile;
Zack Weinberg2ac93491999-08-31 19:46:18 +00001520 const struct directive *keyword ATTRIBUTE_UNUSED;
Per Bothner7f2935c1995-03-16 13:59:07 -08001521{
Neil Booth7ceb3592000-03-11 00:49:44 +00001522 const U_CHAR *text, *limit;
Zack Weinberg941e09b1998-12-15 11:17:06 +00001523
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001524 cpp_skip_hspace (pfile);
1525 text = CPP_BUFFER (pfile)->cur;
1526 skip_rest_of_line (pfile);
1527 limit = CPP_BUFFER (pfile)->cur;
1528
1529 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
Per Bothner7f2935c1995-03-16 13:59:07 -08001530 return 0;
1531}
1532
1533/*
1534 * Report a warning detected by the program we are processing.
1535 * Use the text of the line in the warning message, then continue.
Per Bothner7f2935c1995-03-16 13:59:07 -08001536 */
1537
1538static int
Zack Weinberg941e09b1998-12-15 11:17:06 +00001539do_warning (pfile, keyword)
Per Bothner7f2935c1995-03-16 13:59:07 -08001540 cpp_reader *pfile;
Zack Weinberg2ac93491999-08-31 19:46:18 +00001541 const struct directive *keyword ATTRIBUTE_UNUSED;
Per Bothner7f2935c1995-03-16 13:59:07 -08001542{
Neil Booth7ceb3592000-03-11 00:49:44 +00001543 const U_CHAR *text, *limit;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001544
1545 cpp_skip_hspace (pfile);
1546 text = CPP_BUFFER (pfile)->cur;
1547 skip_rest_of_line (pfile);
1548 limit = CPP_BUFFER (pfile)->cur;
Jeff Lawf5963e61998-05-05 17:18:02 -06001549
Jason Merrill83ecd272000-03-03 00:09:22 +00001550 if (CPP_PEDANTIC (pfile))
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +00001551 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
Jeff Lawf5963e61998-05-05 17:18:02 -06001552
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001553 cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
Per Bothner7f2935c1995-03-16 13:59:07 -08001554 return 0;
1555}
1556
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001557/* Report program identification. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001558
1559static int
Zack Weinberg941e09b1998-12-15 11:17:06 +00001560do_ident (pfile, keyword)
Per Bothner7f2935c1995-03-16 13:59:07 -08001561 cpp_reader *pfile;
Zack Weinberg2ac93491999-08-31 19:46:18 +00001562 const struct directive *keyword ATTRIBUTE_UNUSED;
Per Bothner7f2935c1995-03-16 13:59:07 -08001563{
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001564 long old_written = CPP_WRITTEN (pfile);
1565
Per Bothner7f2935c1995-03-16 13:59:07 -08001566 /* Allow #ident in system headers, since that's not user's fault. */
Jason Merrill83ecd272000-03-03 00:09:22 +00001567 if (CPP_PEDANTIC (pfile))
Per Bothner7f2935c1995-03-16 13:59:07 -08001568 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1569
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001570 CPP_PUTS (pfile, "#ident ", 7);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001571
1572 /* Next token should be a string constant. */
1573 if (get_directive_token (pfile) == CPP_STRING)
1574 /* And then a newline. */
1575 if (get_directive_token (pfile) == CPP_VSPACE)
1576 /* Good - ship it. */
1577 return 0;
1578
1579 cpp_error (pfile, "invalid #ident");
1580 skip_rest_of_line (pfile);
1581 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
Per Bothner7f2935c1995-03-16 13:59:07 -08001582
1583 return 0;
1584}
1585
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001586/* Pragmata handling. We handle some of these, and pass the rest on
1587 to the front end. C99 defines three pragmas and says that no macro
1588 expansion is to be performed on them; whether or not macro
1589 expansion happens for other pragmas is implementation defined.
1590 This implementation never macro-expands the text after #pragma.
1591
1592 We currently do not support the _Pragma operator. Support for that
1593 has to be coordinated with the front end. Proposed implementation:
1594 both #pragma blah blah and _Pragma("blah blah") become
1595 __builtin_pragma(blah blah) and we teach the parser about that. */
1596
1597/* Sub-handlers for the pragmas needing treatment here.
1598 They return 1 if the token buffer is to be popped, 0 if not. */
1599static int do_pragma_once PARAMS ((cpp_reader *));
1600static int do_pragma_implementation PARAMS ((cpp_reader *));
1601static int do_pragma_poison PARAMS ((cpp_reader *));
1602static int do_pragma_default PARAMS ((cpp_reader *));
Per Bothner7f2935c1995-03-16 13:59:07 -08001603
1604static int
Zack Weinberg941e09b1998-12-15 11:17:06 +00001605do_pragma (pfile, keyword)
Per Bothner7f2935c1995-03-16 13:59:07 -08001606 cpp_reader *pfile;
Zack Weinberg2ac93491999-08-31 19:46:18 +00001607 const struct directive *keyword ATTRIBUTE_UNUSED;
Per Bothner7f2935c1995-03-16 13:59:07 -08001608{
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001609 long here, key;
Zack Weinberg941e09b1998-12-15 11:17:06 +00001610 U_CHAR *buf;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001611 int pop;
Alexandre Oliva0172e2b2000-02-27 06:24:27 +00001612 enum cpp_token token;
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001613
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001614 here = CPP_WRITTEN (pfile);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001615 CPP_PUTS (pfile, "#pragma ", 8);
Zack Weinbergadd70911998-10-29 11:54:13 +00001616
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001617 key = CPP_WRITTEN (pfile);
1618 pfile->no_macro_expand++;
Alexandre Oliva0172e2b2000-02-27 06:24:27 +00001619 token = get_directive_token (pfile);
1620 if (token != CPP_NAME)
1621 {
1622 if (token == CPP_VSPACE)
1623 goto empty;
1624 else
1625 goto skip;
1626 }
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001627
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001628 buf = pfile->token_buffer + key;
1629 CPP_PUTC (pfile, ' ');
Per Bothner7f2935c1995-03-16 13:59:07 -08001630
Neil Booth7ceb3592000-03-11 00:49:44 +00001631#define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001632 if (tokis ("once"))
1633 pop = do_pragma_once (pfile);
1634 else if (tokis ("implementation"))
1635 pop = do_pragma_implementation (pfile);
1636 else if (tokis ("poison"))
1637 pop = do_pragma_poison (pfile);
1638 else
1639 pop = do_pragma_default (pfile);
1640#undef tokis
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001641
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001642 if (get_directive_token (pfile) != CPP_VSPACE)
1643 goto skip;
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001644
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001645 if (pop)
1646 CPP_SET_WRITTEN (pfile, here);
1647 pfile->no_macro_expand--;
1648 return 0;
Geoff Keatingfc009f91999-09-09 04:00:37 +00001649
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001650 skip:
1651 cpp_error (pfile, "malformed #pragma directive");
1652 skip_rest_of_line (pfile);
Alexandre Oliva0172e2b2000-02-27 06:24:27 +00001653 empty:
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001654 CPP_SET_WRITTEN (pfile, here);
1655 pfile->no_macro_expand--;
Per Bothner7f2935c1995-03-16 13:59:07 -08001656 return 0;
1657}
1658
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001659static int
1660do_pragma_default (pfile)
1661 cpp_reader *pfile;
1662{
1663 while (get_directive_token (pfile) != CPP_VSPACE)
1664 CPP_PUTC (pfile, ' ');
1665 return 0;
1666}
1667
1668static int
1669do_pragma_once (pfile)
1670 cpp_reader *pfile;
1671{
1672 cpp_buffer *ip = CPP_BUFFER (pfile);
1673
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001674 /* Allow #pragma once in system headers, since that's not the user's
1675 fault. */
1676 if (!ip->system_header_p)
1677 cpp_warning (pfile, "`#pragma once' is obsolete");
1678
Zack Weinberg38b24ee2000-03-08 20:37:23 +00001679 if (CPP_PREV_BUFFER (ip) == NULL)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001680 cpp_warning (pfile, "`#pragma once' outside include file");
1681 else
Neil Booth7ceb3592000-03-11 00:49:44 +00001682 ip->ihash->control_macro = (const U_CHAR *) ""; /* never repeat */
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001683
1684 return 1;
1685}
1686
1687static int
1688do_pragma_implementation (pfile)
1689 cpp_reader *pfile;
1690{
1691 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1692 been included yet. */
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001693 enum cpp_token token;
1694 long written = CPP_WRITTEN (pfile);
1695 U_CHAR *name;
1696 U_CHAR *copy;
Zack Weinbergd35364d2000-03-12 23:46:05 +00001697 size_t len;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001698
1699 token = get_directive_token (pfile);
1700 if (token == CPP_VSPACE)
1701 return 0;
1702 else if (token != CPP_STRING)
1703 {
1704 cpp_error (pfile, "malformed #pragma implementation");
1705 return 1;
1706 }
1707
Zack Weinberg0e091b52000-03-13 17:25:36 +00001708 /* Trim the leading and trailing quote marks from the string. */
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001709 name = pfile->token_buffer + written + 1;
Zack Weinberg0e091b52000-03-13 17:25:36 +00001710 len = CPP_PWRITTEN (pfile) - name;
Zack Weinbergd35364d2000-03-12 23:46:05 +00001711 copy = (U_CHAR *) alloca (len);
1712 memcpy (copy, name, len - 1);
Zack Weinberg0e091b52000-03-13 17:25:36 +00001713 copy[len - 1] = '\0';
Zack Weinbergd35364d2000-03-12 23:46:05 +00001714
Zack Weinbergb0699da2000-03-07 20:58:47 +00001715 if (cpp_included (pfile, copy))
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001716 cpp_warning (pfile,
1717 "`#pragma implementation' for `%s' appears after file is included",
1718 copy);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001719 return 0;
1720}
1721
1722static int
1723do_pragma_poison (pfile)
1724 cpp_reader *pfile;
1725{
1726 /* Poison these symbols so that all subsequent usage produces an
1727 error message. */
1728 U_CHAR *p;
Zack Weinbergd35364d2000-03-12 23:46:05 +00001729 HASHNODE **slot;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001730 long written;
1731 size_t len;
1732 enum cpp_token token;
1733 int writeit;
Zack Weinbergd35364d2000-03-12 23:46:05 +00001734 unsigned long hash;
1735
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001736 /* As a rule, don't include #pragma poison commands in output,
1737 unless the user asks for them. */
1738 writeit = (CPP_OPTIONS (pfile)->debug_output
1739 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions
1740 || CPP_OPTIONS (pfile)->dump_macros == dump_names);
1741
1742 for (;;)
1743 {
1744 written = CPP_WRITTEN (pfile);
1745 token = get_directive_token (pfile);
1746 if (token == CPP_VSPACE)
1747 break;
1748 if (token != CPP_NAME)
1749 {
1750 cpp_error (pfile, "invalid #pragma poison directive");
1751 skip_rest_of_line (pfile);
1752 return 1;
1753 }
1754
1755 p = pfile->token_buffer + written;
1756 len = strlen (p);
Zack Weinbergd35364d2000-03-12 23:46:05 +00001757 slot = _cpp_lookup_slot (pfile, p, len, 1, &hash);
1758 if (*slot)
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001759 {
Zack Weinbergd35364d2000-03-12 23:46:05 +00001760 HASHNODE *hp = *slot;
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001761 if (hp->type != T_POISON)
1762 {
1763 cpp_warning (pfile, "poisoning existing macro `%s'", p);
Zack Weinbergb0699da2000-03-07 20:58:47 +00001764 if (hp->type == T_MACRO)
1765 _cpp_free_definition (hp->value.defn);
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001766 hp->value.defn = 0;
1767 hp->type = T_POISON;
1768 }
1769 }
1770 else
Zack Weinbergd35364d2000-03-12 23:46:05 +00001771 {
1772 HASHNODE *hp = _cpp_make_hashnode (p, len, T_POISON, hash);
1773 hp->value.cpval = 0;
1774 *slot = hp;
1775 }
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001776 if (writeit)
1777 CPP_PUTC (pfile, ' ');
1778 }
1779 return !writeit;
1780}
1781
Jeffrey A Law72e19471998-03-29 11:51:23 +00001782#ifdef SCCS_DIRECTIVE
Per Bothner7f2935c1995-03-16 13:59:07 -08001783/* Just ignore #sccs, on systems where we define it at all. */
1784
1785static int
Zack Weinberg941e09b1998-12-15 11:17:06 +00001786do_sccs (pfile, keyword)
Per Bothner7f2935c1995-03-16 13:59:07 -08001787 cpp_reader *pfile;
Zack Weinberg2ac93491999-08-31 19:46:18 +00001788 const struct directive *keyword ATTRIBUTE_UNUSED;
Per Bothner7f2935c1995-03-16 13:59:07 -08001789{
1790 if (CPP_PEDANTIC (pfile))
1791 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
Zack Weinberg941e09b1998-12-15 11:17:06 +00001792 skip_rest_of_line (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08001793 return 0;
1794}
Jeffrey A Law72e19471998-03-29 11:51:23 +00001795#endif
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001796
1797/* We've found an `#if' directive. If the only thing before it in
1798 this file is white space, and if it is of the form
1799 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1800 for inclusion of this file. (See redundant_include_p in cppfiles.c
1801 for an explanation of controlling macros.) If so, return a
1802 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1803
1804static U_CHAR *
1805detect_if_not_defined (pfile)
1806 cpp_reader *pfile;
1807{
1808 U_CHAR *control_macro = 0;
1809
1810 if (pfile->only_seen_white == 2)
1811 {
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001812 U_CHAR *ident;
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001813 enum cpp_token token;
1814 int base_offset;
1815 int token_offset;
1816 int need_rparen = 0;
1817
1818 /* Save state required for restore. */
1819 pfile->no_macro_expand++;
1820 parse_set_mark (pfile);
1821 base_offset = CPP_WRITTEN (pfile);
1822
1823 /* Look for `!', */
1824 if (get_directive_token (pfile) != CPP_OTHER
1825 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1826 || CPP_PWRITTEN (pfile)[-1] != '!')
1827 goto restore;
1828
1829 /* ...then `defined', */
1830 token_offset = CPP_WRITTEN (pfile);
1831 token = get_directive_token (pfile);
1832 if (token != CPP_NAME)
1833 goto restore;
1834 ident = pfile->token_buffer + token_offset;
1835 CPP_NUL_TERMINATE (pfile);
1836 if (strcmp (ident, "defined"))
1837 goto restore;
1838
1839 /* ...then an optional '(' and the name, */
1840 token_offset = CPP_WRITTEN (pfile);
1841 token = get_directive_token (pfile);
1842 if (token == CPP_LPAREN)
1843 {
1844 token_offset = CPP_WRITTEN (pfile);
1845 token = get_directive_token (pfile);
1846 if (token != CPP_NAME)
1847 goto restore;
1848 need_rparen = 1;
1849 }
1850 else if (token != CPP_NAME)
1851 goto restore;
1852
1853 ident = pfile->token_buffer + token_offset;
1854 CPP_NUL_TERMINATE (pfile);
1855
1856 /* ...then the ')', if necessary, */
1857 if ((!need_rparen || get_directive_token (pfile) == CPP_RPAREN)
1858 /* ...and make sure there's nothing else on the line. */
1859 && get_directive_token (pfile) == CPP_VSPACE)
Neil Booth7ceb3592000-03-11 00:49:44 +00001860 control_macro = (U_CHAR *) xstrdup (ident);
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001861
1862 restore:
1863 CPP_SET_WRITTEN (pfile, base_offset);
1864 pfile->no_macro_expand--;
1865 parse_goto_mark (pfile);
1866 }
1867
1868 return control_macro;
1869}
1870
Per Bothner7f2935c1995-03-16 13:59:07 -08001871/*
Zack Weinberg88ae23e2000-03-08 23:35:19 +00001872 * #if is straightforward; just call eval_if_expr, then conditional_skip.
1873 * Also, check for a reinclude preventer of the form #if !defined (MACRO).
Per Bothner7f2935c1995-03-16 13:59:07 -08001874 */
1875
1876static int
Zack Weinberg941e09b1998-12-15 11:17:06 +00001877do_if (pfile, keyword)
Per Bothner7f2935c1995-03-16 13:59:07 -08001878 cpp_reader *pfile;
Zack Weinberg2ac93491999-08-31 19:46:18 +00001879 const struct directive *keyword ATTRIBUTE_UNUSED;
Per Bothner7f2935c1995-03-16 13:59:07 -08001880{
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001881 U_CHAR *control_macro = detect_if_not_defined (pfile);
Zack Weinberg88ae23e2000-03-08 23:35:19 +00001882 int value = eval_if_expr (pfile);
Jim Meyering1d0e51b1999-08-25 22:01:36 +00001883 conditional_skip (pfile, value == 0, T_IF, control_macro);
Per Bothner7f2935c1995-03-16 13:59:07 -08001884 return 0;
1885}
1886
1887/*
1888 * handle a #elif directive by not changing if_stack either.
1889 * see the comment above do_else.
1890 */
1891
1892static int
Zack Weinberg941e09b1998-12-15 11:17:06 +00001893do_elif (pfile, keyword)
Per Bothner7f2935c1995-03-16 13:59:07 -08001894 cpp_reader *pfile;
Zack Weinberg2ac93491999-08-31 19:46:18 +00001895 const struct directive *keyword ATTRIBUTE_UNUSED;
Per Bothner7f2935c1995-03-16 13:59:07 -08001896{
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001897 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1898 {
1899 cpp_error (pfile, "`#elif' not within a conditional");
1900 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001901 }
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001902 else
1903 {
1904 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
1905 {
1906 cpp_error (pfile, "`#elif' after `#else'");
1907 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1908 "the conditional began here");
1909 }
1910 pfile->if_stack->type = T_ELIF;
1911 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001912
1913 if (pfile->if_stack->if_succeeded)
Zack Weinberged705a81999-01-04 12:38:22 +00001914 skip_if_group (pfile);
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001915 else
1916 {
Zack Weinberg88ae23e2000-03-08 23:35:19 +00001917 int value = eval_if_expr (pfile);
Zack Weinberg40ea76d2000-02-06 07:30:25 +00001918 if (value == 0)
1919 skip_if_group (pfile);
1920 else
1921 {
1922 ++pfile->if_stack->if_succeeded; /* continue processing input */
1923 output_line_command (pfile, same_file);
1924 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001925 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001926 return 0;
1927}
1928
Zack Weinberg88ae23e2000-03-08 23:35:19 +00001929/* Thin wrapper around _cpp_parse_expr, which doesn't have access to
1930 * skip_rest_of_line. Also centralizes toggling parsing_if_directive.
Per Bothner7f2935c1995-03-16 13:59:07 -08001931 */
Mike Stump0f413021996-07-03 22:07:53 +00001932
Zack Weinberg88ae23e2000-03-08 23:35:19 +00001933static int
Zack Weinberg38b24ee2000-03-08 20:37:23 +00001934eval_if_expr (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08001935 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08001936{
Zack Weinberg88ae23e2000-03-08 23:35:19 +00001937 int value;
Per Bothner7f2935c1995-03-16 13:59:07 -08001938 long old_written = CPP_WRITTEN (pfile);
1939
Zack Weinbergba412f12000-03-01 00:57:09 +00001940 pfile->parsing_if_directive++;
Zack Weinbergb0699da2000-03-07 20:58:47 +00001941 value = _cpp_parse_expr (pfile);
Zack Weinbergba412f12000-03-01 00:57:09 +00001942 pfile->parsing_if_directive--;
Per Bothner7f2935c1995-03-16 13:59:07 -08001943
Zack Weinbergcf4ed942000-02-10 23:47:04 +00001944 skip_rest_of_line (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08001945 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1946
1947 return value;
1948}
1949
1950/*
1951 * routine to handle ifdef/ifndef. Try to look up the symbol,
1952 * then do or don't skip to the #endif/#else/#elif depending
1953 * on what directive is actually being processed.
1954 */
1955
1956static int
Zack Weinberg88ae23e2000-03-08 23:35:19 +00001957do_ifdef (pfile, keyword)
Per Bothner7f2935c1995-03-16 13:59:07 -08001958 cpp_reader *pfile;
Zack Weinberg2ac93491999-08-31 19:46:18 +00001959 const struct directive *keyword;
Per Bothner7f2935c1995-03-16 13:59:07 -08001960{
1961 int skip;
Mike Stump0f413021996-07-03 22:07:53 +00001962 U_CHAR *ident;
Per Bothner7f2935c1995-03-16 13:59:07 -08001963 int ident_length;
1964 enum cpp_token token;
1965 int start_of_file = 0;
1966 U_CHAR *control_macro = 0;
1967 int old_written = CPP_WRITTEN (pfile);
1968
1969 /* Detect a #ifndef at start of file (not counting comments). */
Zack Weinberg38b24ee2000-03-08 20:37:23 +00001970 if (keyword->type == T_IFNDEF)
Per Bothner7f2935c1995-03-16 13:59:07 -08001971 start_of_file = pfile->only_seen_white == 2;
1972
1973 pfile->no_macro_expand++;
1974 token = get_directive_token (pfile);
1975 pfile->no_macro_expand--;
1976
1977 ident = pfile->token_buffer + old_written;
1978 ident_length = CPP_WRITTEN (pfile) - old_written;
1979 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1980
1981 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1982 {
1983 skip = (keyword->type == T_IFDEF);
1984 if (! CPP_TRADITIONAL (pfile))
1985 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
1986 }
1987 else if (token == CPP_NAME)
1988 {
Zack Weinberga2a76ce2000-02-11 20:17:27 +00001989 skip = cpp_defined (pfile, ident, ident_length);
1990 if (keyword->type == T_IFDEF)
1991 skip = !skip;
1992
Per Bothner7f2935c1995-03-16 13:59:07 -08001993 if (start_of_file && !skip)
1994 {
1995 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
Neil Booth7ceb3592000-03-11 00:49:44 +00001996 memcpy (control_macro, ident, ident_length + 1);
Per Bothner7f2935c1995-03-16 13:59:07 -08001997 }
1998 }
1999 else
2000 {
2001 skip = (keyword->type == T_IFDEF);
2002 if (! CPP_TRADITIONAL (pfile))
2003 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
2004 }
2005
2006 if (!CPP_TRADITIONAL (pfile))
2007 { int c;
2008 cpp_skip_hspace (pfile);
2009 c = PEEKC ();
2010 if (c != EOF && c != '\n')
2011 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
2012 }
2013 skip_rest_of_line (pfile);
2014
Per Bothner7f2935c1995-03-16 13:59:07 -08002015 conditional_skip (pfile, skip, T_IF, control_macro);
2016 return 0;
2017}
2018
2019/* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
2020 If this is a #ifndef starting at the beginning of a file,
2021 CONTROL_MACRO is the macro name tested by the #ifndef.
2022 Otherwise, CONTROL_MACRO is 0. */
2023
2024static void
2025conditional_skip (pfile, skip, type, control_macro)
2026 cpp_reader *pfile;
2027 int skip;
2028 enum node_type type;
2029 U_CHAR *control_macro;
2030{
Zack Weinberg38b24ee2000-03-08 20:37:23 +00002031 IF_STACK *temp;
Per Bothner7f2935c1995-03-16 13:59:07 -08002032
Zack Weinberg38b24ee2000-03-08 20:37:23 +00002033 temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
Per Bothner7f2935c1995-03-16 13:59:07 -08002034 temp->lineno = CPP_BUFFER (pfile)->lineno;
Per Bothner7f2935c1995-03-16 13:59:07 -08002035 temp->next = pfile->if_stack;
2036 temp->control_macro = control_macro;
2037 pfile->if_stack = temp;
2038
2039 pfile->if_stack->type = type;
2040
2041 if (skip != 0) {
Zack Weinberged705a81999-01-04 12:38:22 +00002042 skip_if_group (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08002043 return;
2044 } else {
2045 ++pfile->if_stack->if_succeeded;
Zack Weinberg80e9dcb1999-04-19 11:55:04 +00002046 output_line_command (pfile, same_file);
Per Bothner7f2935c1995-03-16 13:59:07 -08002047 }
2048}
2049
Zack Weinberged705a81999-01-04 12:38:22 +00002050/* Subroutine of skip_if_group. Examine one preprocessing directive and
2051 return 0 if skipping should continue, 1 if it should halt. Also
2052 adjusts the if_stack as appropriate.
2053 The `#' has been read, but not the identifier. */
Mike Stump0f413021996-07-03 22:07:53 +00002054
Zack Weinberged705a81999-01-04 12:38:22 +00002055static int
2056consider_directive_while_skipping (pfile, stack)
2057 cpp_reader *pfile;
Zack Weinberg38b24ee2000-03-08 20:37:23 +00002058 IF_STACK *stack;
Zack Weinberged705a81999-01-04 12:38:22 +00002059{
2060 long ident_len, ident;
Zack Weinberg2ac93491999-08-31 19:46:18 +00002061 const struct directive *kt;
Zack Weinberg38b24ee2000-03-08 20:37:23 +00002062 IF_STACK *temp;
Zack Weinberged705a81999-01-04 12:38:22 +00002063
2064 cpp_skip_hspace (pfile);
2065
2066 ident = CPP_WRITTEN (pfile);
2067 parse_name (pfile, GETC());
2068 ident_len = CPP_WRITTEN (pfile) - ident;
2069
2070 CPP_SET_WRITTEN (pfile, ident);
2071
2072 for (kt = directive_table; kt->length >= 0; kt++)
2073 if (kt->length == ident_len
2074 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
2075 switch (kt->type)
2076 {
2077 case T_IF:
2078 case T_IFDEF:
2079 case T_IFNDEF:
Zack Weinberg38b24ee2000-03-08 20:37:23 +00002080 temp = (IF_STACK *) xmalloc (sizeof (IF_STACK));
Zack Weinberged705a81999-01-04 12:38:22 +00002081 temp->next = pfile->if_stack;
2082 pfile->if_stack = temp;
Zack Weinberged705a81999-01-04 12:38:22 +00002083 temp->type = kt->type;
2084 return 0;
2085
2086 case T_ELSE:
Zack Weinberg75ec21d2000-01-27 22:29:07 +00002087 if (pfile->if_stack != stack)
Zack Weinberged705a81999-01-04 12:38:22 +00002088 validate_else (pfile, "#else");
2089 /* fall through */
2090 case T_ELIF:
Zack Weinberged705a81999-01-04 12:38:22 +00002091 if (pfile->if_stack == stack)
2092 return 1;
2093 else
2094 {
2095 pfile->if_stack->type = kt->type;
2096 return 0;
2097 }
2098
2099 case T_ENDIF:
Zack Weinberg75ec21d2000-01-27 22:29:07 +00002100 if (pfile->if_stack != stack)
Zack Weinberged705a81999-01-04 12:38:22 +00002101 validate_else (pfile, "#endif");
2102
2103 if (pfile->if_stack == stack)
2104 return 1;
2105
2106 temp = pfile->if_stack;
2107 pfile->if_stack = temp->next;
2108 free (temp);
2109 return 0;
2110
2111 default:
2112 return 0;
2113 }
2114
2115 /* Don't let erroneous code go by. */
2116 if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
2117 cpp_pedwarn (pfile, "invalid preprocessor directive name");
2118 return 0;
2119}
2120
2121/* skip to #endif, #else, or #elif. adjust line numbers, etc.
2122 * leaves input ptr at the sharp sign found.
2123 */
Per Bothner7f2935c1995-03-16 13:59:07 -08002124static void
Zack Weinberged705a81999-01-04 12:38:22 +00002125skip_if_group (pfile)
2126 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -08002127{
2128 int c;
Zack Weinberg38b24ee2000-03-08 20:37:23 +00002129 IF_STACK *save_if_stack = pfile->if_stack; /* don't pop past here */
Neil Booth7ceb3592000-03-11 00:49:44 +00002130 const U_CHAR *beg_of_line;
Zack Weinberged705a81999-01-04 12:38:22 +00002131 long old_written;
Per Bothner7f2935c1995-03-16 13:59:07 -08002132
Zack Weinberged705a81999-01-04 12:38:22 +00002133 old_written = CPP_WRITTEN (pfile);
2134
2135 for (;;)
Per Bothner7f2935c1995-03-16 13:59:07 -08002136 {
Zack Weinberged705a81999-01-04 12:38:22 +00002137 beg_of_line = CPP_BUFFER (pfile)->cur;
Per Bothner7f2935c1995-03-16 13:59:07 -08002138
Zack Weinberged705a81999-01-04 12:38:22 +00002139 if (! CPP_TRADITIONAL (pfile))
2140 cpp_skip_hspace (pfile);
2141 c = GETC();
2142 if (c == '\n')
Per Bothner7f2935c1995-03-16 13:59:07 -08002143 {
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002144 CPP_BUMP_LINE (pfile);
Zack Weinberged705a81999-01-04 12:38:22 +00002145 continue;
Per Bothner7f2935c1995-03-16 13:59:07 -08002146 }
Zack Weinberged705a81999-01-04 12:38:22 +00002147 else if (c == '#')
2148 {
2149 if (consider_directive_while_skipping (pfile, save_if_stack))
2150 break;
2151 }
2152 else if (c == EOF)
2153 return; /* Caller will issue error. */
2154
2155 FORWARD(-1);
Zack Weinbergba412f12000-03-01 00:57:09 +00002156 skip_rest_of_line (pfile);
Zack Weinberged705a81999-01-04 12:38:22 +00002157
2158 c = GETC();
2159 if (c == EOF)
2160 return; /* Caller will issue error. */
2161 else
Zack Weinbergba412f12000-03-01 00:57:09 +00002162 CPP_BUMP_LINE (pfile);
Zack Weinberged705a81999-01-04 12:38:22 +00002163 }
2164
2165 /* Back up to the beginning of this line. Caller will process the
2166 directive. */
2167 CPP_BUFFER (pfile)->cur = beg_of_line;
Per Bothner7f2935c1995-03-16 13:59:07 -08002168 pfile->only_seen_white = 1;
Per Bothner7f2935c1995-03-16 13:59:07 -08002169}
2170
2171/*
2172 * handle a #else directive. Do this by just continuing processing
2173 * without changing if_stack ; this is so that the error message
2174 * for missing #endif's etc. will point to the original #if. It
2175 * is possible that something different would be better.
2176 */
2177
2178static int
Zack Weinberg941e09b1998-12-15 11:17:06 +00002179do_else (pfile, keyword)
Per Bothner7f2935c1995-03-16 13:59:07 -08002180 cpp_reader *pfile;
Zack Weinberg2ac93491999-08-31 19:46:18 +00002181 const struct directive *keyword ATTRIBUTE_UNUSED;
Per Bothner7f2935c1995-03-16 13:59:07 -08002182{
Zack Weinberg75ec21d2000-01-27 22:29:07 +00002183 validate_else (pfile, "#else");
Per Bothner7f2935c1995-03-16 13:59:07 -08002184 skip_rest_of_line (pfile);
2185
Zack Weinberg40ea76d2000-02-06 07:30:25 +00002186 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2187 {
2188 cpp_error (pfile, "`#else' not within a conditional");
2189 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08002190 }
Zack Weinberg40ea76d2000-02-06 07:30:25 +00002191 else
2192 {
2193 /* #ifndef can't have its special treatment for containing the whole file
2194 if it has a #else clause. */
2195 pfile->if_stack->control_macro = 0;
2196
2197 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
2198 {
2199 cpp_error (pfile, "`#else' after `#else'");
2200 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
2201 "the conditional began here");
2202 }
2203 pfile->if_stack->type = T_ELSE;
2204 }
Per Bothner7f2935c1995-03-16 13:59:07 -08002205
2206 if (pfile->if_stack->if_succeeded)
Zack Weinberged705a81999-01-04 12:38:22 +00002207 skip_if_group (pfile);
Zack Weinberg40ea76d2000-02-06 07:30:25 +00002208 else
2209 {
2210 ++pfile->if_stack->if_succeeded; /* continue processing input */
2211 output_line_command (pfile, same_file);
2212 }
Per Bothner7f2935c1995-03-16 13:59:07 -08002213 return 0;
2214}
2215
2216/*
2217 * unstack after #endif command
2218 */
2219
2220static int
Zack Weinberg941e09b1998-12-15 11:17:06 +00002221do_endif (pfile, keyword)
Per Bothner7f2935c1995-03-16 13:59:07 -08002222 cpp_reader *pfile;
Zack Weinberg2ac93491999-08-31 19:46:18 +00002223 const struct directive *keyword ATTRIBUTE_UNUSED;
Per Bothner7f2935c1995-03-16 13:59:07 -08002224{
Zack Weinberg75ec21d2000-01-27 22:29:07 +00002225 validate_else (pfile, "#endif");
Per Bothner7f2935c1995-03-16 13:59:07 -08002226 skip_rest_of_line (pfile);
2227
2228 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
Zack Weinberg6ee2c971999-09-11 05:38:06 +00002229 cpp_error (pfile, "`#endif' not within a conditional");
Per Bothner7f2935c1995-03-16 13:59:07 -08002230 else
2231 {
Zack Weinberg38b24ee2000-03-08 20:37:23 +00002232 IF_STACK *temp = pfile->if_stack;
Per Bothner7f2935c1995-03-16 13:59:07 -08002233 pfile->if_stack = temp->next;
2234 if (temp->control_macro != 0)
2235 {
2236 /* This #endif matched a #ifndef at the start of the file.
2237 See if it is at the end of the file. */
Per Bothner7f2935c1995-03-16 13:59:07 -08002238 int c;
2239
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002240 parse_set_mark (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08002241
2242 for (;;)
2243 {
2244 cpp_skip_hspace (pfile);
2245 c = GETC ();
2246 if (c != '\n')
2247 break;
2248 }
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002249 parse_goto_mark (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08002250
2251 if (c == EOF)
2252 {
Zack Weinberg0b3d7761998-11-25 11:56:54 +00002253 /* This #endif ends a #ifndef
Per Bothner7f2935c1995-03-16 13:59:07 -08002254 that contains all of the file (aside from whitespace).
2255 Arrange not to include the file again
Zack Weinberg0b3d7761998-11-25 11:56:54 +00002256 if the macro that was tested is defined. */
Zack Weinberg38b24ee2000-03-08 20:37:23 +00002257 CPP_BUFFER (pfile)->ihash->control_macro = temp->control_macro;
Per Bothner7f2935c1995-03-16 13:59:07 -08002258 }
2259 }
2260 free (temp);
Zack Weinberg80e9dcb1999-04-19 11:55:04 +00002261 output_line_command (pfile, same_file);
Per Bothner7f2935c1995-03-16 13:59:07 -08002262 }
2263 return 0;
2264}
2265
Zack Weinberg75ec21d2000-01-27 22:29:07 +00002266/* Issue -pedantic warning for text which is not a comment following
2267 an #else or #endif. Do not warn in system headers, as this is harmless
2268 and very common on old systems. */
Per Bothner7f2935c1995-03-16 13:59:07 -08002269
2270static void
2271validate_else (pfile, directive)
2272 cpp_reader *pfile;
Zack Weinberg2ac93491999-08-31 19:46:18 +00002273 const char *directive;
Per Bothner7f2935c1995-03-16 13:59:07 -08002274{
Jason Merrill83ecd272000-03-03 00:09:22 +00002275 if (! CPP_PEDANTIC (pfile))
Zack Weinberg75ec21d2000-01-27 22:29:07 +00002276 return;
2277
Per Bothner7f2935c1995-03-16 13:59:07 -08002278 cpp_skip_hspace (pfile);
Zack Weinberg75ec21d2000-01-27 22:29:07 +00002279 if (PEEKC () != '\n')
Per Bothner7f2935c1995-03-16 13:59:07 -08002280 cpp_pedwarn (pfile,
2281 "text following `%s' violates ANSI standard", directive);
2282}
2283
Zack Weinberg6ee2c971999-09-11 05:38:06 +00002284/* Convert T_IF, etc. to a string. Used in error messages. */
2285static const char *
2286if_directive_name (pfile, ifs)
2287 cpp_reader *pfile;
2288 struct if_stack *ifs;
2289{
2290 switch (ifs->type)
2291 {
2292 case T_IF: return "#if";
2293 case T_IFDEF: return "#ifdef";
2294 case T_IFNDEF: return "#ifndef";
2295 case T_ELIF: return "#elif";
2296 case T_ELSE: return "#else";
2297 default:
Zack Weinbergc1212d22000-02-06 23:46:18 +00002298 cpp_ice (pfile, "impossible if_stack->type value %d", ifs->type);
Zack Weinberg6ee2c971999-09-11 05:38:06 +00002299 return "unknown";
2300 }
2301}
2302
Per Bothner22bbcea1995-04-11 13:43:19 -07002303/* Get the next token, and add it to the text in pfile->token_buffer.
Mike Stump0f413021996-07-03 22:07:53 +00002304 Return the kind of token we got. */
Zack Weinbergeaefae02000-02-06 08:24:22 +00002305
Per Bothner7f2935c1995-03-16 13:59:07 -08002306enum cpp_token
2307cpp_get_token (pfile)
2308 cpp_reader *pfile;
2309{
2310 register int c, c2, c3;
Per Bothner7f2935c1995-03-16 13:59:07 -08002311 enum cpp_token token;
2312 struct cpp_options *opts = CPP_OPTIONS (pfile);
Zack Weinberg4d9a1b41999-02-15 14:04:21 +00002313
Per Bothner7f2935c1995-03-16 13:59:07 -08002314 get_next:
2315 c = GETC();
2316 if (c == EOF)
2317 {
Jason Merrille7f9dea1999-07-26 22:01:44 +00002318 if (CPP_BUFFER (pfile)->manual_pop)
2319 /* If we've been reading from redirected input, the
2320 frontend will pop the buffer. */
2321 return CPP_EOF;
2322 else if (CPP_BUFFER (pfile)->seen_eof)
Per Bothner7f2935c1995-03-16 13:59:07 -08002323 {
Zack Weinberg38b24ee2000-03-08 20:37:23 +00002324 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == NULL)
Per Bothner7f2935c1995-03-16 13:59:07 -08002325 return CPP_EOF;
Zack Weinberg4d9a1b41999-02-15 14:04:21 +00002326
2327 cpp_pop_buffer (pfile);
2328 goto get_next;
Per Bothner7f2935c1995-03-16 13:59:07 -08002329 }
2330 else
2331 {
Zack Weinberg6ee2c971999-09-11 05:38:06 +00002332 cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2333 struct if_stack *ifs, *nifs;
2334
2335 /* Unwind the conditional stack and generate error messages. */
2336 for (ifs = pfile->if_stack;
2337 ifs != CPP_BUFFER (pfile)->if_stack;
2338 ifs = nifs)
2339 {
2340 cpp_error_with_line (pfile, ifs->lineno, -1,
2341 "unterminated `%s' conditional",
2342 if_directive_name (pfile, ifs));
2343
2344 nifs = ifs->next;
2345 free (ifs);
2346 }
2347 pfile->if_stack = ifs;
2348
Zack Weinberg38b24ee2000-03-08 20:37:23 +00002349 if (CPP_BUFFER (pfile)->nominal_fname && next_buf != NULL)
Per Bothner7f2935c1995-03-16 13:59:07 -08002350 {
2351 /* We're about to return from an #include file.
Richard Kennerddd5a7c1995-05-16 08:14:26 -04002352 Emit #line information now (as part of the CPP_POP) result.
Mike Stump0f413021996-07-03 22:07:53 +00002353 But the #line refers to the file we will pop to. */
Per Bothner7f2935c1995-03-16 13:59:07 -08002354 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2355 CPP_BUFFER (pfile) = next_buf;
2356 pfile->input_stack_listing_current = 0;
Zack Weinberg80e9dcb1999-04-19 11:55:04 +00002357 output_line_command (pfile, leave_file);
Per Bothner7f2935c1995-03-16 13:59:07 -08002358 CPP_BUFFER (pfile) = cur_buffer;
2359 }
Zack Weinberg6ee2c971999-09-11 05:38:06 +00002360
2361 CPP_BUFFER (pfile)->seen_eof = 1;
Per Bothner7f2935c1995-03-16 13:59:07 -08002362 return CPP_POP;
2363 }
2364 }
2365 else
2366 {
2367 switch (c)
2368 {
Per Bothner7f2935c1995-03-16 13:59:07 -08002369 case '/':
2370 if (PEEKC () == '=')
2371 goto op2;
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002372
2373 comment:
Zack Weinberg564ad5f2000-02-10 00:26:47 +00002374 if (opts->discard_comments)
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002375 c = skip_comment (pfile, c);
Zack Weinberg564ad5f2000-02-10 00:26:47 +00002376 else
2377 c = copy_comment (pfile, c);
Zack Weinberg75ec21d2000-01-27 22:29:07 +00002378 if (c != ' ')
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002379 goto randomchar;
2380
Per Bothner7f2935c1995-03-16 13:59:07 -08002381 /* Comments are equivalent to spaces.
2382 For -traditional, a comment is equivalent to nothing. */
Zack Weinberg564ad5f2000-02-10 00:26:47 +00002383 if (opts->traditional || !opts->discard_comments)
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002384 return CPP_COMMENT;
Per Bothner7f2935c1995-03-16 13:59:07 -08002385 else
2386 {
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002387 CPP_PUTC (pfile, c);
Per Bothner7f2935c1995-03-16 13:59:07 -08002388 return CPP_HSPACE;
2389 }
Zack Weinberg1316f1f2000-02-06 07:53:50 +00002390
Per Bothner7f2935c1995-03-16 13:59:07 -08002391 case '#':
Zack Weinbergba412f12000-03-01 00:57:09 +00002392 if (pfile->parsing_if_directive)
2393 {
2394 cpp_skip_hspace (pfile);
2395 parse_assertion (pfile);
2396 return CPP_ASSERTION;
2397 }
2398
2399 if (pfile->parsing_define_directive && ! CPP_TRADITIONAL (pfile))
2400 {
2401 CPP_RESERVE (pfile, 3);
2402 CPP_PUTC_Q (pfile, '#');
2403 CPP_NUL_TERMINATE_Q (pfile);
2404 if (PEEKC () != '#')
2405 return CPP_STRINGIZE;
2406
2407 FORWARD (1);
2408 CPP_PUTC_Q (pfile, '#');
2409 CPP_NUL_TERMINATE_Q (pfile);
2410 return CPP_TOKPASTE;
2411 }
2412
Per Bothner7f2935c1995-03-16 13:59:07 -08002413 if (!pfile->only_seen_white)
2414 goto randomchar;
Zack Weinberg40c79d52000-01-12 00:35:36 +00002415 /* -traditional directives are recognized only with the # in
2416 column 1.
2417 XXX Layering violation. */
2418 if (CPP_TRADITIONAL (pfile)
2419 && CPP_BUFFER (pfile)->cur - CPP_BUFFER (pfile)->line_base != 1)
2420 goto randomchar;
Per Bothner7f2935c1995-03-16 13:59:07 -08002421 if (handle_directive (pfile))
2422 return CPP_DIRECTIVE;
2423 pfile->only_seen_white = 0;
Zack Weinbergeaefae02000-02-06 08:24:22 +00002424 goto randomchar;
Per Bothner7f2935c1995-03-16 13:59:07 -08002425
2426 case '\"':
2427 case '\'':
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002428 parse_string (pfile, c);
Per Bothner7f2935c1995-03-16 13:59:07 -08002429 pfile->only_seen_white = 0;
2430 return c == '\'' ? CPP_CHAR : CPP_STRING;
2431
2432 case '$':
2433 if (!opts->dollars_in_ident)
2434 goto randomchar;
2435 goto letter;
2436
2437 case ':':
2438 if (opts->cplusplus && PEEKC () == ':')
2439 goto op2;
2440 goto randomchar;
2441
2442 case '&':
2443 case '+':
2444 case '|':
Per Bothner7f2935c1995-03-16 13:59:07 -08002445 c2 = PEEKC ();
2446 if (c2 == c || c2 == '=')
2447 goto op2;
2448 goto randomchar;
2449
2450 case '*':
2451 case '!':
2452 case '%':
2453 case '=':
2454 case '^':
Per Bothner7f2935c1995-03-16 13:59:07 -08002455 if (PEEKC () == '=')
2456 goto op2;
2457 goto randomchar;
2458
2459 case '-':
Per Bothner7f2935c1995-03-16 13:59:07 -08002460 c2 = PEEKC ();
2461 if (c2 == '-' && opts->chill)
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002462 goto comment; /* Chill style comment */
Jason Merrill3773a461999-07-20 15:13:01 -04002463 if (c2 == '-' || c2 == '=')
Per Bothner7f2935c1995-03-16 13:59:07 -08002464 goto op2;
Jason Merrill3773a461999-07-20 15:13:01 -04002465 if (c2 == '>')
2466 {
2467 if (opts->cplusplus && PEEKN (1) == '*')
2468 {
2469 /* In C++, there's a ->* operator. */
Jason Merrill3773a461999-07-20 15:13:01 -04002470 token = CPP_OTHER;
2471 pfile->only_seen_white = 0;
2472 CPP_RESERVE (pfile, 4);
2473 CPP_PUTC_Q (pfile, c);
2474 CPP_PUTC_Q (pfile, GETC ());
2475 CPP_PUTC_Q (pfile, GETC ());
2476 CPP_NUL_TERMINATE_Q (pfile);
2477 return token;
2478 }
2479 goto op2;
2480 }
Per Bothner7f2935c1995-03-16 13:59:07 -08002481 goto randomchar;
2482
2483 case '<':
2484 if (pfile->parsing_include_directive)
2485 {
2486 for (;;)
2487 {
2488 CPP_PUTC (pfile, c);
2489 if (c == '>')
2490 break;
2491 c = GETC ();
Per Bothner7f2935c1995-03-16 13:59:07 -08002492 if (c == '\n' || c == EOF)
2493 {
2494 cpp_error (pfile,
2495 "missing '>' in `#include <FILENAME>'");
2496 break;
2497 }
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002498 else if (c == '\r')
2499 {
Zack Weinberged45de91999-04-12 12:03:10 +00002500 if (!CPP_BUFFER (pfile)->has_escapes)
2501 {
2502 /* Backslash newline is replaced by nothing. */
2503 CPP_ADJUST_WRITTEN (pfile, -1);
2504 CPP_BUMP_LINE (pfile);
2505 }
2506 else
2507 {
2508 /* We might conceivably get \r- or \r<space> in
2509 here. Just delete 'em. */
2510 int d = GETC();
2511 if (d != '-' && d != ' ')
Zack Weinbergc1212d22000-02-06 23:46:18 +00002512 cpp_ice (pfile, "unrecognized escape \\r%c", d);
Zack Weinberged45de91999-04-12 12:03:10 +00002513 CPP_ADJUST_WRITTEN (pfile, -1);
2514 }
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002515 }
Per Bothner7f2935c1995-03-16 13:59:07 -08002516 }
2517 return CPP_STRING;
2518 }
2519 /* else fall through */
2520 case '>':
Per Bothner7f2935c1995-03-16 13:59:07 -08002521 c2 = PEEKC ();
2522 if (c2 == '=')
2523 goto op2;
Jason Merrill3773a461999-07-20 15:13:01 -04002524 /* GNU C++ supports MIN and MAX operators <? and >?. */
2525 if (c2 != c && (!opts->cplusplus || c2 != '?'))
Per Bothner7f2935c1995-03-16 13:59:07 -08002526 goto randomchar;
2527 FORWARD(1);
2528 CPP_RESERVE (pfile, 4);
2529 CPP_PUTC (pfile, c);
2530 CPP_PUTC (pfile, c2);
Per Bothner7f2935c1995-03-16 13:59:07 -08002531 c3 = PEEKC ();
2532 if (c3 == '=')
2533 CPP_PUTC_Q (pfile, GETC ());
2534 CPP_NUL_TERMINATE_Q (pfile);
2535 pfile->only_seen_white = 0;
2536 return CPP_OTHER;
2537
Per Bothner7f2935c1995-03-16 13:59:07 -08002538 case '.':
Per Bothner7f2935c1995-03-16 13:59:07 -08002539 c2 = PEEKC ();
Kaveh R. Ghazie9a780e1998-05-06 12:56:58 +00002540 if (ISDIGIT(c2))
Per Bothner7f2935c1995-03-16 13:59:07 -08002541 {
2542 CPP_RESERVE(pfile, 2);
2543 CPP_PUTC_Q (pfile, '.');
2544 c = GETC ();
2545 goto number;
2546 }
Jason Merrill3773a461999-07-20 15:13:01 -04002547
2548 /* In C++ there's a .* operator. */
2549 if (opts->cplusplus && c2 == '*')
2550 goto op2;
2551
Per Bothner7f2935c1995-03-16 13:59:07 -08002552 if (c2 == '.' && PEEKN(1) == '.')
2553 {
2554 CPP_RESERVE(pfile, 4);
2555 CPP_PUTC_Q (pfile, '.');
2556 CPP_PUTC_Q (pfile, '.');
2557 CPP_PUTC_Q (pfile, '.');
2558 FORWARD (2);
2559 CPP_NUL_TERMINATE_Q (pfile);
2560 pfile->only_seen_white = 0;
2561 return CPP_3DOTS;
2562 }
2563 goto randomchar;
2564
2565 op2:
2566 token = CPP_OTHER;
2567 pfile->only_seen_white = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08002568 CPP_RESERVE(pfile, 3);
2569 CPP_PUTC_Q (pfile, c);
2570 CPP_PUTC_Q (pfile, GETC ());
2571 CPP_NUL_TERMINATE_Q (pfile);
2572 return token;
2573
2574 case 'L':
Per Bothner7f2935c1995-03-16 13:59:07 -08002575 c2 = PEEKC ();
2576 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2577 {
2578 CPP_PUTC (pfile, c);
2579 c = GETC ();
Alexandre Oliva525bc952000-02-23 19:21:07 +00002580 parse_string (pfile, c);
2581 pfile->only_seen_white = 0;
2582 return c == '\'' ? CPP_WCHAR : CPP_WSTRING;
Per Bothner7f2935c1995-03-16 13:59:07 -08002583 }
2584 goto letter;
2585
2586 case '0': case '1': case '2': case '3': case '4':
2587 case '5': case '6': case '7': case '8': case '9':
2588 number:
2589 c2 = '.';
2590 for (;;)
2591 {
2592 CPP_RESERVE (pfile, 2);
2593 CPP_PUTC_Q (pfile, c);
Per Bothner7f2935c1995-03-16 13:59:07 -08002594 c = PEEKC ();
2595 if (c == EOF)
2596 break;
Zack Weinberg455d2582000-03-04 01:42:56 +00002597 if (!is_numchar(c) && c != '.'
Richard Kenner641d4441997-03-19 16:59:23 -05002598 && ((c2 != 'e' && c2 != 'E'
Zack Weinberg88ae23e2000-03-08 23:35:19 +00002599 && ((c2 != 'p' && c2 != 'P')
2600 || CPP_OPTIONS (pfile)->c89))
Richard Kenner641d4441997-03-19 16:59:23 -05002601 || (c != '+' && c != '-')))
Per Bothner7f2935c1995-03-16 13:59:07 -08002602 break;
2603 FORWARD(1);
2604 c2= c;
2605 }
2606 CPP_NUL_TERMINATE_Q (pfile);
2607 pfile->only_seen_white = 0;
2608 return CPP_NUMBER;
2609 case 'b': case 'c': case 'd': case 'h': case 'o':
2610 case 'B': case 'C': case 'D': case 'H': case 'O':
2611 if (opts->chill && PEEKC () == '\'')
2612 {
2613 pfile->only_seen_white = 0;
2614 CPP_RESERVE (pfile, 2);
2615 CPP_PUTC_Q (pfile, c);
2616 CPP_PUTC_Q (pfile, '\'');
2617 FORWARD(1);
2618 for (;;)
2619 {
2620 c = GETC();
2621 if (c == EOF)
2622 goto chill_number_eof;
Zack Weinberg455d2582000-03-04 01:42:56 +00002623 if (!is_numchar(c))
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002624 break;
Per Bothner7f2935c1995-03-16 13:59:07 -08002625 CPP_PUTC (pfile, c);
2626 }
2627 if (c == '\'')
2628 {
2629 CPP_RESERVE (pfile, 2);
2630 CPP_PUTC_Q (pfile, c);
2631 CPP_NUL_TERMINATE_Q (pfile);
2632 return CPP_STRING;
2633 }
2634 else
2635 {
2636 FORWARD(-1);
2637 chill_number_eof:
2638 CPP_NUL_TERMINATE (pfile);
2639 return CPP_NUMBER;
2640 }
2641 }
2642 else
2643 goto letter;
2644 case '_':
2645 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2646 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2647 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2648 case 'x': case 'y': case 'z':
2649 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2650 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2651 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2652 case 'Y': case 'Z':
2653 letter:
2654 {
2655 HASHNODE *hp;
2656 unsigned char *ident;
2657 int before_name_written = CPP_WRITTEN (pfile);
2658 int ident_len;
2659 parse_name (pfile, c);
2660 pfile->only_seen_white = 0;
2661 if (pfile->no_macro_expand)
2662 return CPP_NAME;
2663 ident = pfile->token_buffer + before_name_written;
2664 ident_len = CPP_PWRITTEN (pfile) - ident;
Zack Weinbergb0699da2000-03-07 20:58:47 +00002665 hp = _cpp_lookup (pfile, ident, ident_len);
Per Bothner7f2935c1995-03-16 13:59:07 -08002666 if (!hp)
2667 return CPP_NAME;
2668 if (hp->type == T_DISABLED)
2669 {
2670 if (pfile->output_escapes)
Zack Weinberged45de91999-04-12 12:03:10 +00002671 { /* Return "\r-IDENT", followed by '\0'. */
Per Bothner7f2935c1995-03-16 13:59:07 -08002672 int i;
2673 CPP_RESERVE (pfile, 3);
2674 ident = pfile->token_buffer + before_name_written;
2675 CPP_ADJUST_WRITTEN (pfile, 2);
2676 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
Zack Weinberged45de91999-04-12 12:03:10 +00002677 ident[0] = '\r';
Per Bothner7f2935c1995-03-16 13:59:07 -08002678 ident[1] = '-';
2679 }
2680 return CPP_NAME;
2681 }
2682
Zack Weinbergcf4ed942000-02-10 23:47:04 +00002683 /* If macro wants an arglist, verify that a '(' follows. */
Per Bothner7f2935c1995-03-16 13:59:07 -08002684 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2685 {
Zack Weinberg564ad5f2000-02-10 00:26:47 +00002686 int macbuf_whitespace = 0;
2687
2688 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2689 {
Neil Booth7ceb3592000-03-11 00:49:44 +00002690 const U_CHAR *point = CPP_BUFFER (pfile)->cur;
Zack Weinberg564ad5f2000-02-10 00:26:47 +00002691 for (;;)
2692 {
2693 cpp_skip_hspace (pfile);
2694 c = PEEKC ();
2695 if (c == '\n')
2696 FORWARD(1);
2697 else
2698 break;
2699 }
2700 if (point != CPP_BUFFER (pfile)->cur)
2701 macbuf_whitespace = 1;
2702 if (c == '(')
2703 goto is_macro_call;
2704 else if (c != EOF)
2705 goto not_macro_call;
2706 cpp_pop_buffer (pfile);
2707 }
Dave Brolleyc55ade01998-10-16 14:28:40 +00002708
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002709 parse_set_mark (pfile);
Per Bothner7f2935c1995-03-16 13:59:07 -08002710 for (;;)
2711 {
2712 cpp_skip_hspace (pfile);
2713 c = PEEKC ();
Zack Weinberg564ad5f2000-02-10 00:26:47 +00002714 if (c == '\n')
2715 FORWARD(1);
2716 else
2717 break;
Per Bothner7f2935c1995-03-16 13:59:07 -08002718 }
Zack Weinberg564ad5f2000-02-10 00:26:47 +00002719 parse_goto_mark (pfile);
2720
2721 if (c == '(')
2722 goto is_macro_call;
2723
2724 not_macro_call:
2725 if (macbuf_whitespace)
2726 CPP_PUTC (pfile, ' ');
2727 return CPP_NAME;
Per Bothner7f2935c1995-03-16 13:59:07 -08002728 }
Zack Weinberg564ad5f2000-02-10 00:26:47 +00002729 is_macro_call:
Zack Weinberg20dc7361999-01-25 11:23:01 +00002730 /* This is now known to be a macro call.
2731 Expand the macro, reading arguments as needed,
2732 and push the expansion on the input stack. */
Zack Weinbergb0699da2000-03-07 20:58:47 +00002733 _cpp_macroexpand (pfile, hp);
Zack Weinberg20dc7361999-01-25 11:23:01 +00002734 CPP_SET_WRITTEN (pfile, before_name_written);
Per Bothner7f2935c1995-03-16 13:59:07 -08002735 }
Per Bothner782331f1995-04-30 14:43:12 -07002736 goto get_next;
Per Bothner7f2935c1995-03-16 13:59:07 -08002737
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002738 case ' ': case '\t': case '\v':
Per Bothner7f2935c1995-03-16 13:59:07 -08002739 for (;;)
2740 {
2741 CPP_PUTC (pfile, c);
2742 c = PEEKC ();
Zack Weinberga9ae4481999-10-29 04:31:14 +00002743 if (c == EOF || !is_hspace(c))
Per Bothner7f2935c1995-03-16 13:59:07 -08002744 break;
2745 FORWARD(1);
2746 }
2747 return CPP_HSPACE;
2748
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002749 case '\r':
Zack Weinberged45de91999-04-12 12:03:10 +00002750 if (CPP_BUFFER (pfile)->has_escapes)
2751 {
2752 c = GETC ();
2753 if (c == '-')
2754 {
2755 if (pfile->output_escapes)
2756 CPP_PUTS (pfile, "\r-", 2);
2757 parse_name (pfile, GETC ());
2758 return CPP_NAME;
2759 }
2760 else if (c == ' ')
2761 {
2762 CPP_RESERVE (pfile, 2);
2763 if (pfile->output_escapes)
2764 CPP_PUTC_Q (pfile, '\r');
2765 CPP_PUTC_Q (pfile, c);
2766 return CPP_HSPACE;
2767 }
2768 else
2769 {
Zack Weinbergc1212d22000-02-06 23:46:18 +00002770 cpp_ice (pfile, "unrecognized escape \\r%c", c);
Zack Weinberged45de91999-04-12 12:03:10 +00002771 goto get_next;
2772 }
2773 }
2774 else
2775 {
2776 /* Backslash newline is ignored. */
2777 CPP_BUMP_LINE (pfile);
2778 goto get_next;
2779 }
Per Bothner7f2935c1995-03-16 13:59:07 -08002780
2781 case '\n':
2782 CPP_PUTC (pfile, c);
2783 if (pfile->only_seen_white == 0)
2784 pfile->only_seen_white = 1;
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002785 CPP_BUMP_LINE (pfile);
Zack Weinberg80e9dcb1999-04-19 11:55:04 +00002786 if (! CPP_OPTIONS (pfile)->no_line_commands)
2787 {
2788 pfile->lineno++;
2789 if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2790 output_line_command (pfile, same_file);
2791 }
Per Bothner7f2935c1995-03-16 13:59:07 -08002792 return CPP_VSPACE;
2793
2794 case '(': token = CPP_LPAREN; goto char1;
2795 case ')': token = CPP_RPAREN; goto char1;
2796 case '{': token = CPP_LBRACE; goto char1;
2797 case '}': token = CPP_RBRACE; goto char1;
2798 case ',': token = CPP_COMMA; goto char1;
2799 case ';': token = CPP_SEMICOLON; goto char1;
2800
2801 randomchar:
2802 default:
2803 token = CPP_OTHER;
2804 char1:
2805 pfile->only_seen_white = 0;
2806 CPP_PUTC (pfile, c);
2807 return token;
2808 }
2809 }
2810}
2811
Mike Stump0f413021996-07-03 22:07:53 +00002812/* Like cpp_get_token, but skip spaces and comments. */
2813
Per Bothner7f2935c1995-03-16 13:59:07 -08002814enum cpp_token
2815cpp_get_non_space_token (pfile)
2816 cpp_reader *pfile;
2817{
2818 int old_written = CPP_WRITTEN (pfile);
2819 for (;;)
2820 {
2821 enum cpp_token token = cpp_get_token (pfile);
2822 if (token != CPP_COMMENT && token != CPP_POP
2823 && token != CPP_HSPACE && token != CPP_VSPACE)
2824 return token;
2825 CPP_SET_WRITTEN (pfile, old_written);
2826 }
2827}
2828
Mike Stump0f413021996-07-03 22:07:53 +00002829/* Parse an identifier starting with C. */
Per Bothner7f2935c1995-03-16 13:59:07 -08002830
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002831static void
Per Bothner7f2935c1995-03-16 13:59:07 -08002832parse_name (pfile, c)
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002833 cpp_reader *pfile;
2834 int c;
Per Bothner7f2935c1995-03-16 13:59:07 -08002835{
2836 for (;;)
2837 {
Zack Weinberga9ae4481999-10-29 04:31:14 +00002838 if (! is_idchar(c))
Per Bothner7f2935c1995-03-16 13:59:07 -08002839 {
Per Bothner7f2935c1995-03-16 13:59:07 -08002840 FORWARD (-1);
2841 break;
2842 }
2843
Richard Kenner9e979f81996-10-09 16:22:20 -04002844 if (c == '$' && CPP_PEDANTIC (pfile))
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +00002845 cpp_pedwarn (pfile, "`$' in identifier");
Richard Kenner9e979f81996-10-09 16:22:20 -04002846
Mike Stump0f413021996-07-03 22:07:53 +00002847 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
Per Bothner7f2935c1995-03-16 13:59:07 -08002848 CPP_PUTC_Q (pfile, c);
2849 c = GETC();
2850 if (c == EOF)
2851 break;
2852 }
2853 CPP_NUL_TERMINATE_Q (pfile);
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002854 return;
2855}
2856
Zack Weinbergba412f12000-03-01 00:57:09 +00002857/* Parse and skip over a string starting with C. A single quoted
2858 string is treated like a double -- some programs (e.g., troff) are
2859 perverse this way. (However, a single quoted string is not allowed
2860 to extend over multiple lines.) */
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002861static void
Zack Weinbergba412f12000-03-01 00:57:09 +00002862skip_string (pfile, c)
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002863 cpp_reader *pfile;
2864 int c;
2865{
2866 long start_line, start_column;
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002867 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2868
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002869 while (1)
2870 {
2871 int cc = GETC();
Zack Weinbergba412f12000-03-01 00:57:09 +00002872 switch (cc)
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002873 {
Zack Weinbergba412f12000-03-01 00:57:09 +00002874 case EOF:
Zack Weinberg6ee2c971999-09-11 05:38:06 +00002875 cpp_error_with_line (pfile, start_line, start_column,
2876 "unterminated string or character constant");
2877 if (pfile->multiline_string_line != start_line
2878 && pfile->multiline_string_line != 0)
2879 cpp_error_with_line (pfile,
2880 pfile->multiline_string_line, -1,
2881 "possible real start of unterminated constant");
2882 pfile->multiline_string_line = 0;
Zack Weinbergba412f12000-03-01 00:57:09 +00002883 return;
2884
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002885 case '\n':
2886 CPP_BUMP_LINE (pfile);
Zack Weinberg40c79d52000-01-12 00:35:36 +00002887 /* In Fortran and assembly language, silently terminate
2888 strings of either variety at end of line. This is a
2889 kludge around not knowing where comments are in these
2890 languages. */
2891 if (CPP_OPTIONS (pfile)->lang_fortran
2892 || CPP_OPTIONS (pfile)->lang_asm)
Zack Weinbergba412f12000-03-01 00:57:09 +00002893 {
2894 FORWARD(-1);
2895 return;
2896 }
Zack Weinberg6ee2c971999-09-11 05:38:06 +00002897 /* Character constants may not extend over multiple lines.
Zack Weinberg40c79d52000-01-12 00:35:36 +00002898 In Standard C, neither may strings. We accept multiline
2899 strings as an extension. */
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002900 if (c == '\'')
2901 {
2902 cpp_error_with_line (pfile, start_line, start_column,
2903 "unterminated character constant");
Zack Weinbergba412f12000-03-01 00:57:09 +00002904 FORWARD(-1);
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002905 return;
2906 }
2907 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
Zack Weinberg40c79d52000-01-12 00:35:36 +00002908 cpp_pedwarn_with_line (pfile, start_line, start_column,
2909 "string constant runs past end of line");
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002910 if (pfile->multiline_string_line == 0)
2911 pfile->multiline_string_line = start_line;
2912 break;
2913
2914 case '\r':
Zack Weinberged45de91999-04-12 12:03:10 +00002915 if (CPP_BUFFER (pfile)->has_escapes)
2916 {
Zack Weinbergc1212d22000-02-06 23:46:18 +00002917 cpp_ice (pfile, "\\r escape inside string constant");
Zack Weinberged45de91999-04-12 12:03:10 +00002918 FORWARD(1);
2919 }
2920 else
2921 /* Backslash newline is replaced by nothing at all. */
2922 CPP_BUMP_LINE (pfile);
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002923 break;
2924
2925 case '\\':
Zack Weinbergba412f12000-03-01 00:57:09 +00002926 FORWARD(1);
Zack Weinberg3fdc6511999-03-16 13:10:15 +00002927 break;
2928
2929 case '\"':
2930 case '\'':
2931 if (cc == c)
2932 return;
2933 break;
2934 }
2935 }
Per Bothner7f2935c1995-03-16 13:59:07 -08002936}
2937
Zack Weinbergba412f12000-03-01 00:57:09 +00002938/* Parse a string and copy it to the output. */
2939
2940static void
2941parse_string (pfile, c)
2942 cpp_reader *pfile;
2943 int c;
2944{
Neil Booth7ceb3592000-03-11 00:49:44 +00002945 const U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
2946 const U_CHAR *limit;
Zack Weinbergba412f12000-03-01 00:57:09 +00002947
2948 skip_string (pfile, c);
2949
2950 limit = CPP_BUFFER (pfile)->cur;
2951 CPP_RESERVE (pfile, limit - start + 2);
2952 CPP_PUTC_Q (pfile, c);
2953 for (; start < limit; start++)
2954 if (*start != '\r')
2955 CPP_PUTC_Q (pfile, *start);
2956}
2957
Zack Weinberg7061aa51998-12-15 11:09:16 +00002958/* Read an assertion into the token buffer, converting to
2959 canonical form: `#predicate(a n swe r)' The next non-whitespace
2960 character to read should be the first letter of the predicate.
2961 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2962 with answer (see callers for why). In case of 0, an error has been
2963 printed. */
2964static int
2965parse_assertion (pfile)
2966 cpp_reader *pfile;
2967{
2968 int c, dropwhite;
2969 cpp_skip_hspace (pfile);
2970 c = PEEKC();
Zack Weinberga9ae4481999-10-29 04:31:14 +00002971 if (! is_idstart(c))
Zack Weinberg7061aa51998-12-15 11:09:16 +00002972 {
2973 cpp_error (pfile, "assertion predicate is not an identifier");
2974 return 0;
2975 }
2976 CPP_PUTC(pfile, '#');
2977 FORWARD(1);
2978 parse_name(pfile, c);
2979
2980 c = PEEKC();
2981 if (c != '(')
2982 {
Zack Weinberga9ae4481999-10-29 04:31:14 +00002983 if (is_hspace(c) || c == '\r')
Zack Weinberg7061aa51998-12-15 11:09:16 +00002984 cpp_skip_hspace (pfile);
2985 c = PEEKC();
2986 }
2987 if (c != '(')
2988 return 1;
2989
2990 CPP_PUTC(pfile, '(');
2991 FORWARD(1);
2992 dropwhite = 1;
2993 while ((c = GETC()) != ')')
2994 {
Zack Weinberga9ae4481999-10-29 04:31:14 +00002995 if (is_space(c))
Zack Weinberg7061aa51998-12-15 11:09:16 +00002996 {
2997 if (! dropwhite)
2998 {
2999 CPP_PUTC(pfile, ' ');
3000 dropwhite = 1;
3001 }
3002 }
Zack Weinberg7061aa51998-12-15 11:09:16 +00003003 else if (c == '\n' || c == EOF)
3004 {
3005 if (c == '\n') FORWARD(-1);
3006 cpp_error (pfile, "un-terminated assertion answer");
3007 return 0;
3008 }
Zack Weinberg3fdc6511999-03-16 13:10:15 +00003009 else if (c == '\r')
Zack Weinberged45de91999-04-12 12:03:10 +00003010 /* \r cannot be a macro escape here. */
Zack Weinberg3fdc6511999-03-16 13:10:15 +00003011 CPP_BUMP_LINE (pfile);
Zack Weinberg7061aa51998-12-15 11:09:16 +00003012 else
3013 {
Zack Weinberg3fdc6511999-03-16 13:10:15 +00003014 CPP_PUTC (pfile, c);
Zack Weinberg7061aa51998-12-15 11:09:16 +00003015 dropwhite = 0;
3016 }
3017 }
3018
3019 if (pfile->limit[-1] == ' ')
3020 pfile->limit[-1] = ')';
3021 else if (pfile->limit[-1] == '(')
3022 {
3023 cpp_error (pfile, "empty token sequence in assertion");
3024 return 0;
3025 }
3026 else
Zack Weinberg3fdc6511999-03-16 13:10:15 +00003027 CPP_PUTC (pfile, ')');
Zack Weinberg7061aa51998-12-15 11:09:16 +00003028
Zack Weinberg3fdc6511999-03-16 13:10:15 +00003029 CPP_NUL_TERMINATE (pfile);
Zack Weinberg7061aa51998-12-15 11:09:16 +00003030 return 2;
3031}
3032
Per Bothner7f2935c1995-03-16 13:59:07 -08003033static int
Zack Weinberg941e09b1998-12-15 11:17:06 +00003034do_assert (pfile, keyword)
Per Bothner7f2935c1995-03-16 13:59:07 -08003035 cpp_reader *pfile;
Zack Weinberg2ac93491999-08-31 19:46:18 +00003036 const struct directive *keyword ATTRIBUTE_UNUSED;
Per Bothner7f2935c1995-03-16 13:59:07 -08003037{
Zack Weinberge23c0ba2000-03-07 23:11:06 +00003038 U_CHAR *sym;
Zack Weinberg7061aa51998-12-15 11:09:16 +00003039 int ret, c;
3040 HASHNODE *base, *this;
Zack Weinbergd35364d2000-03-12 23:46:05 +00003041 HASHNODE **bslot, **tslot;
3042 size_t blen, tlen;
3043 unsigned long bhash, thash;
Per Bothner7f2935c1995-03-16 13:59:07 -08003044
Jason Merrill83ecd272000-03-03 00:09:22 +00003045 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing)
Per Bothner7f2935c1995-03-16 13:59:07 -08003046 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
3047
3048 cpp_skip_hspace (pfile);
Zack Weinberge23c0ba2000-03-07 23:11:06 +00003049 sym = CPP_PWRITTEN (pfile); /* remember where it starts */
Zack Weinberg7061aa51998-12-15 11:09:16 +00003050 ret = parse_assertion (pfile);
3051 if (ret == 0)
3052 goto error;
3053 else if (ret == 1)
3054 {
3055 cpp_error (pfile, "missing token-sequence in `#assert'");
3056 goto error;
3057 }
Per Bothner7f2935c1995-03-16 13:59:07 -08003058
3059 cpp_skip_hspace (pfile);
Zack Weinberg7061aa51998-12-15 11:09:16 +00003060 c = PEEKC();
3061 if (c != EOF && c != '\n')
3062 {
3063 cpp_error (pfile, "junk at end of `#assert'");
3064 goto error;
3065 }
3066
Zack Weinbergd35364d2000-03-12 23:46:05 +00003067 tlen = strlen (sym);
3068 blen = (U_CHAR *) strchr (sym, '(') - sym;
3069 tslot = _cpp_lookup_slot (pfile, sym, tlen, 1, &thash);
3070 if (*tslot)
Zack Weinberg7061aa51998-12-15 11:09:16 +00003071 {
3072 cpp_warning (pfile, "`%s' re-asserted", sym);
3073 goto error;
3074 }
3075
Zack Weinbergd35364d2000-03-12 23:46:05 +00003076 bslot = _cpp_lookup_slot (pfile, sym, blen, 1, &bhash);
3077 if (! *bslot)
3078 *bslot = base = _cpp_make_hashnode (sym, blen, T_ASSERT, bhash);
3079 else
3080 {
3081 base = *bslot;
3082 if (base->type != T_ASSERT)
3083 {
3084 /* Token clash - but with what?! */
3085 cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
3086 goto error;
3087 }
3088 }
3089 *tslot = this = _cpp_make_hashnode (sym, tlen, T_ASSERT, thash);
3090 this->value.aschain = base->value.aschain;
Zack Weinberg7061aa51998-12-15 11:09:16 +00003091 base->value.aschain = this;
3092
Zack Weinberge23c0ba2000-03-07 23:11:06 +00003093 pfile->limit = sym; /* Pop */
Per Bothner7f2935c1995-03-16 13:59:07 -08003094 return 0;
Zack Weinberg7061aa51998-12-15 11:09:16 +00003095
Per Bothner7f2935c1995-03-16 13:59:07 -08003096 error:
Per Bothner7f2935c1995-03-16 13:59:07 -08003097 skip_rest_of_line (pfile);
Zack Weinberge23c0ba2000-03-07 23:11:06 +00003098 pfile->limit = sym; /* Pop */
Zack Weinberg3caee4a1999-04-26 16:41:02 +00003099 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08003100}
Zack Weinberg7061aa51998-12-15 11:09:16 +00003101
Per Bothner7f2935c1995-03-16 13:59:07 -08003102static int
Zack Weinberg941e09b1998-12-15 11:17:06 +00003103do_unassert (pfile, keyword)
Per Bothner7f2935c1995-03-16 13:59:07 -08003104 cpp_reader *pfile;
Zack Weinberg2ac93491999-08-31 19:46:18 +00003105 const struct directive *keyword ATTRIBUTE_UNUSED;
Per Bothner7f2935c1995-03-16 13:59:07 -08003106{
Zack Weinberg7061aa51998-12-15 11:09:16 +00003107 int c, ret;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00003108 U_CHAR *sym;
Zack Weinberg7061aa51998-12-15 11:09:16 +00003109 long baselen, thislen;
3110 HASHNODE *base, *this, *next;
3111
Jason Merrill83ecd272000-03-03 00:09:22 +00003112 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing)
Per Bothner7f2935c1995-03-16 13:59:07 -08003113 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
3114
3115 cpp_skip_hspace (pfile);
3116
Zack Weinberge23c0ba2000-03-07 23:11:06 +00003117 sym = CPP_PWRITTEN (pfile); /* remember where it starts */
Zack Weinberg7061aa51998-12-15 11:09:16 +00003118 ret = parse_assertion (pfile);
3119 if (ret == 0)
3120 goto error;
3121
Per Bothner7f2935c1995-03-16 13:59:07 -08003122 cpp_skip_hspace (pfile);
3123 c = PEEKC ();
3124 if (c != EOF && c != '\n')
3125 cpp_error (pfile, "junk at end of `#unassert'");
Per Bothner7f2935c1995-03-16 13:59:07 -08003126
Zack Weinberg7061aa51998-12-15 11:09:16 +00003127 thislen = strlen (sym);
3128 if (ret == 1)
3129 {
Zack Weinbergb0699da2000-03-07 20:58:47 +00003130 base = _cpp_lookup (pfile, sym, thislen);
Zack Weinberg7061aa51998-12-15 11:09:16 +00003131 if (! base)
3132 goto error; /* It isn't an error to #undef what isn't #defined,
3133 so it isn't an error to #unassert what isn't
3134 #asserted either. */
3135
3136 for (this = base->value.aschain; this; this = next)
3137 {
3138 next = this->value.aschain;
Zack Weinbergd35364d2000-03-12 23:46:05 +00003139 htab_remove_elt (pfile->hashtab, this);
Per Bothner7f2935c1995-03-16 13:59:07 -08003140 }
Zack Weinbergd35364d2000-03-12 23:46:05 +00003141 htab_remove_elt (pfile->hashtab, base);
Per Bothner7f2935c1995-03-16 13:59:07 -08003142 }
Zack Weinberg7061aa51998-12-15 11:09:16 +00003143 else
3144 {
Neil Booth7ceb3592000-03-11 00:49:44 +00003145 baselen = (U_CHAR *) strchr (sym, '(') - sym;
Zack Weinbergb0699da2000-03-07 20:58:47 +00003146 base = _cpp_lookup (pfile, sym, baselen);
Zack Weinberg7061aa51998-12-15 11:09:16 +00003147 if (! base) goto error;
Zack Weinbergb0699da2000-03-07 20:58:47 +00003148 this = _cpp_lookup (pfile, sym, thislen);
Zack Weinberg7061aa51998-12-15 11:09:16 +00003149 if (! this) goto error;
Per Bothner7f2935c1995-03-16 13:59:07 -08003150
Zack Weinberg7061aa51998-12-15 11:09:16 +00003151 next = base;
3152 while (next->value.aschain != this)
3153 next = next->value.aschain;
3154
3155 next->value.aschain = this->value.aschain;
Zack Weinbergd35364d2000-03-12 23:46:05 +00003156 htab_remove_elt (pfile->hashtab, this);
Zack Weinberg7061aa51998-12-15 11:09:16 +00003157
3158 if (base->value.aschain == NULL)
Zack Weinbergd35364d2000-03-12 23:46:05 +00003159 /* Last answer for this predicate deleted. */
3160 htab_remove_elt (pfile->hashtab, base);
Zack Weinberg7061aa51998-12-15 11:09:16 +00003161 }
3162
Zack Weinberge23c0ba2000-03-07 23:11:06 +00003163 pfile->limit = sym; /* Pop */
Per Bothner7f2935c1995-03-16 13:59:07 -08003164 return 0;
3165 error:
Per Bothner7f2935c1995-03-16 13:59:07 -08003166 skip_rest_of_line (pfile);
Zack Weinberge23c0ba2000-03-07 23:11:06 +00003167 pfile->limit = sym; /* Pop */
Zack Weinberg3caee4a1999-04-26 16:41:02 +00003168 return 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08003169}
Per Bothner7f2935c1995-03-16 13:59:07 -08003170
Zack Weinberg0b22d651999-03-15 18:42:46 +00003171/* Process STR as if it appeared as the body of an #unassert. */
3172void
3173cpp_unassert (pfile, str)
3174 cpp_reader *pfile;
Neil Booth7ceb3592000-03-11 00:49:44 +00003175 const char *str;
Zack Weinberg0b22d651999-03-15 18:42:46 +00003176{
3177 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
3178 {
Zack Weinbergba412f12000-03-01 00:57:09 +00003179 do_unassert (pfile, NULL);
Zack Weinberg0b22d651999-03-15 18:42:46 +00003180 cpp_pop_buffer (pfile);
3181 }
3182}
3183
Zack Weinbergcf4ed942000-02-10 23:47:04 +00003184/* Remember the current position of PFILE so it may be returned to
3185 after looking ahead a bit.
3186
3187 Note that when you set a mark, you _must_ return to that mark. You
3188 may not forget about it and continue parsing. You may not pop a
3189 buffer with an active mark. You may not call CPP_BUMP_LINE while a
3190 mark is active. */
Mike Stump0f413021996-07-03 22:07:53 +00003191
Zack Weinberg564ad5f2000-02-10 00:26:47 +00003192static void
Zack Weinberg3fdc6511999-03-16 13:10:15 +00003193parse_set_mark (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08003194 cpp_reader *pfile;
3195{
Zack Weinberg3fdc6511999-03-16 13:10:15 +00003196 cpp_buffer *ip = CPP_BUFFER (pfile);
Zack Weinberg564ad5f2000-02-10 00:26:47 +00003197 if (ACTIVE_MARK_P())
3198 cpp_ice (pfile, "mark active in parse_set_mark");
Zack Weinberg3fdc6511999-03-16 13:10:15 +00003199
3200 ip->mark = ip->cur - ip->buf;
Per Bothner7f2935c1995-03-16 13:59:07 -08003201}
3202
Zack Weinberg3fdc6511999-03-16 13:10:15 +00003203/* Backup the current position of PFILE to that saved in its mark,
3204 and clear the mark. */
Per Bothner7f2935c1995-03-16 13:59:07 -08003205
Zack Weinberg564ad5f2000-02-10 00:26:47 +00003206static void
Zack Weinberg3fdc6511999-03-16 13:10:15 +00003207parse_goto_mark (pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -08003208 cpp_reader *pfile;
3209{
Zack Weinberg3fdc6511999-03-16 13:10:15 +00003210 cpp_buffer *ip = CPP_BUFFER (pfile);
Zack Weinberg564ad5f2000-02-10 00:26:47 +00003211 if (!ACTIVE_MARK_P())
3212 cpp_ice (pfile, "mark not active in parse_goto_mark");
Zack Weinberg3fdc6511999-03-16 13:10:15 +00003213
3214 ip->cur = ip->buf + ip->mark;
3215 ip->mark = -1;
Per Bothner7f2935c1995-03-16 13:59:07 -08003216}