cpplib.c (my_strerror, [...]): Move to cpperror.c.
[gcc.git] / gcc / cpplib.c
1 /* CPP Library.
2 Copyright (C) 1986, 87, 89, 92-99, 2000 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include "config.h"
22 #include "system.h"
23
24 #include "cpplib.h"
25 #include "cpphash.h"
26 #include "intl.h"
27
28 #define SKIP_WHITE_SPACE(p) do { while (is_hspace(*p)) p++; } while (0)
29
30 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
31 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
32 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
33 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
34 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
35 (Note that it is false while we're expanding macro *arguments*.) */
36 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->data != NULL)
37
38 /* External declarations. */
39
40 extern HOST_WIDEST_INT cpp_parse_expr PARAMS ((cpp_reader *));
41
42 /* `struct directive' defines one #-directive, including how to handle it. */
43
44 struct directive {
45 int length; /* Length of name */
46 int (*func) /* Function to handle directive */
47 PARAMS ((cpp_reader *, const struct directive *));
48 const char *name; /* Name of directive */
49 enum node_type type; /* Code which describes which directive. */
50 };
51
52 /* These functions are declared to return int instead of void since they
53 are going to be placed in a table and some old compilers have trouble with
54 pointers to functions returning void. */
55
56 static int do_define PARAMS ((cpp_reader *, const struct directive *));
57 static int do_line PARAMS ((cpp_reader *, const struct directive *));
58 static int do_include PARAMS ((cpp_reader *, const struct directive *));
59 static int do_undef PARAMS ((cpp_reader *, const struct directive *));
60 static int do_error PARAMS ((cpp_reader *, const struct directive *));
61 static int do_pragma PARAMS ((cpp_reader *, const struct directive *));
62 static int do_ident PARAMS ((cpp_reader *, const struct directive *));
63 static int do_if PARAMS ((cpp_reader *, const struct directive *));
64 static int do_xifdef PARAMS ((cpp_reader *, const struct directive *));
65 static int do_else PARAMS ((cpp_reader *, const struct directive *));
66 static int do_elif PARAMS ((cpp_reader *, const struct directive *));
67 static int do_endif PARAMS ((cpp_reader *, const struct directive *));
68 #ifdef SCCS_DIRECTIVE
69 static int do_sccs PARAMS ((cpp_reader *, const struct directive *));
70 #endif
71 static int do_assert PARAMS ((cpp_reader *, const struct directive *));
72 static int do_unassert PARAMS ((cpp_reader *, const struct directive *));
73 static int do_warning PARAMS ((cpp_reader *, const struct directive *));
74
75 /* Forward declarations. */
76
77 static void validate_else PARAMS ((cpp_reader *, const char *));
78 static HOST_WIDEST_INT eval_if_expression PARAMS ((cpp_reader *));
79 static void conditional_skip PARAMS ((cpp_reader *, int,
80 enum node_type, U_CHAR *));
81 static void skip_if_group PARAMS ((cpp_reader *));
82 static void parse_name PARAMS ((cpp_reader *, int));
83 static void parse_string PARAMS ((cpp_reader *, int));
84 static int parse_assertion PARAMS ((cpp_reader *));
85 static const char *if_directive_name PARAMS ((cpp_reader *,
86 struct if_stack *));
87 static enum cpp_token null_underflow PARAMS ((cpp_reader *));
88 static int null_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
89 static int skip_comment PARAMS ((cpp_reader *, int));
90 static int copy_comment PARAMS ((cpp_reader *, int));
91 static void copy_rest_of_line PARAMS ((cpp_reader *));
92 static int handle_directive PARAMS ((cpp_reader *));
93 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
94 cpp_reader *,
95 const struct directive *));
96 static enum cpp_token get_directive_token PARAMS ((cpp_reader *));
97 static int read_line_number PARAMS ((cpp_reader *, int *));
98 static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
99 static int consider_directive_while_skipping PARAMS ((cpp_reader *,
100 IF_STACK_FRAME *));
101 static void skip_block_comment PARAMS ((cpp_reader *));
102 static void skip_line_comment PARAMS ((cpp_reader *));
103
104 /* Here is the actual list of #-directives.
105 This table is ordered by frequency of occurrence; the numbers
106 at the end are directive counts from all the source code I have
107 lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
108 linux-2.2.9, and pcmcia-cs-3.0.9). */
109
110 static const struct directive directive_table[] = {
111 /* In C89 */
112 { 6, do_define, "define", T_DEFINE }, /* 270554 */
113 { 7, do_include, "include", T_INCLUDE }, /* 52262 */
114 { 5, do_endif, "endif", T_ENDIF }, /* 45855 */
115 { 5, do_xifdef, "ifdef", T_IFDEF }, /* 22000 */
116 { 2, do_if, "if", T_IF }, /* 18162 */
117 { 4, do_else, "else", T_ELSE }, /* 9863 */
118 { 6, do_xifdef, "ifndef", T_IFNDEF }, /* 9675 */
119 { 5, do_undef, "undef", T_UNDEF }, /* 4837 */
120 { 4, do_line, "line", T_LINE }, /* 2465 */
121 { 4, do_elif, "elif", T_ELIF }, /* 610 */
122 { 5, do_error, "error", T_ERROR }, /* 475 */
123 { 6, do_pragma, "pragma", T_PRAGMA }, /* 195 */
124
125 /* Extensions. All deprecated except #warning and #include_next. */
126 { 7, do_warning, "warning", T_WARNING }, /* 22 - GNU */
127 { 12, do_include, "include_next", T_INCLUDE_NEXT }, /* 19 - GNU */
128 { 5, do_ident, "ident", T_IDENT }, /* 11 - SVR4 */
129 { 6, do_include, "import", T_IMPORT }, /* 0 - ObjC */
130 { 6, do_assert, "assert", T_ASSERT }, /* 0 - SVR4 */
131 { 8, do_unassert, "unassert", T_UNASSERT }, /* 0 - SVR4 */
132 #ifdef SCCS_DIRECTIVE
133 { 4, do_sccs, "sccs", T_SCCS }, /* 0 - SVR2? */
134 #endif
135 { -1, 0, "", T_UNUSED }
136 };
137
138 /* Place into PFILE a quoted string representing the string SRC.
139 Caller must reserve enough space in pfile->token_buffer. */
140
141 void
142 quote_string (pfile, src)
143 cpp_reader *pfile;
144 const char *src;
145 {
146 U_CHAR c;
147
148 CPP_PUTC_Q (pfile, '\"');
149 for (;;)
150 switch ((c = *src++))
151 {
152 default:
153 if (ISPRINT (c))
154 CPP_PUTC_Q (pfile, c);
155 else
156 {
157 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
158 CPP_ADJUST_WRITTEN (pfile, 4);
159 }
160 break;
161
162 case '\"':
163 case '\\':
164 CPP_PUTC_Q (pfile, '\\');
165 CPP_PUTC_Q (pfile, c);
166 break;
167
168 case '\0':
169 CPP_PUTC_Q (pfile, '\"');
170 CPP_NUL_TERMINATE_Q (pfile);
171 return;
172 }
173 }
174
175 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
176
177 void
178 cpp_grow_buffer (pfile, n)
179 cpp_reader *pfile;
180 long n;
181 {
182 long old_written = CPP_WRITTEN (pfile);
183 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
184 pfile->token_buffer = (U_CHAR *)
185 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
186 CPP_SET_WRITTEN (pfile, old_written);
187 }
188
189 /* Process the string STR as if it appeared as the body of a #define
190 If STR is just an identifier, define it with value 1.
191 If STR has anything after the identifier, then it should
192 be identifier=definition. */
193
194 void
195 cpp_define (pfile, str)
196 cpp_reader *pfile;
197 U_CHAR *str;
198 {
199 U_CHAR *buf, *p;
200 size_t count;
201
202 /* Copy the entire option so we can modify it. */
203 count = strlen (str) + 3;
204 buf = (U_CHAR *) alloca (count);
205 memcpy (buf, str, count - 2);
206 /* Change the first "=" in the string to a space. If there is none,
207 tack " 1" on the end. */
208 p = (U_CHAR *) strchr (buf, '=');
209 if (p)
210 {
211 *p = ' ';
212 count -= 2;
213 }
214 else
215 strcpy (&buf[count-3], " 1");
216
217 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
218 {
219 do_define (pfile, NULL);
220 cpp_pop_buffer (pfile);
221 }
222 }
223
224 /* Process the string STR as if it appeared as the body of a #assert. */
225 void
226 cpp_assert (pfile, str)
227 cpp_reader *pfile;
228 U_CHAR *str;
229 {
230 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
231 {
232 do_assert (pfile, NULL);
233 cpp_pop_buffer (pfile);
234 }
235 }
236
237
238 static enum cpp_token
239 null_underflow (pfile)
240 cpp_reader *pfile ATTRIBUTE_UNUSED;
241 {
242 return CPP_EOF;
243 }
244
245 static int
246 null_cleanup (pbuf, pfile)
247 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
248 cpp_reader *pfile ATTRIBUTE_UNUSED;
249 {
250 return 0;
251 }
252
253 /* Skip a C-style block comment. We know it's a comment, and point is
254 at the second character of the starter. */
255 static void
256 skip_block_comment (pfile)
257 cpp_reader *pfile;
258 {
259 int c, prev_c = -1;
260 long line, col;
261
262 FORWARD(1);
263 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
264 for (;;)
265 {
266 c = GETC ();
267 if (c == EOF)
268 {
269 cpp_error_with_line (pfile, line, col, "unterminated comment");
270 return;
271 }
272 else if (c == '\n' || c == '\r')
273 /* \r cannot be a macro escape marker here. */
274 CPP_BUMP_LINE (pfile);
275 else if (c == '/' && prev_c == '*')
276 return;
277 else if (c == '*' && prev_c == '/'
278 && CPP_OPTIONS (pfile)->warn_comments)
279 cpp_warning (pfile, "`/*' within comment");
280
281 prev_c = c;
282 }
283 }
284
285 /* Skip a C++/Chill line comment. We know it's a comment, and point
286 is at the second character of the initiator. */
287 static void
288 skip_line_comment (pfile)
289 cpp_reader *pfile;
290 {
291 FORWARD(1);
292 for (;;)
293 {
294 int c = GETC ();
295
296 /* We don't have to worry about EOF in here. */
297 if (c == '\n')
298 {
299 /* Don't consider final '\n' to be part of comment. */
300 FORWARD(-1);
301 return;
302 }
303 else if (c == '\r')
304 {
305 /* \r cannot be a macro escape marker here. */
306 CPP_BUMP_LINE (pfile);
307 if (CPP_OPTIONS (pfile)->warn_comments)
308 cpp_warning (pfile, "backslash-newline within line comment");
309 }
310 }
311 }
312
313 /* Skip a comment - C, C++, or Chill style. M is the first character
314 of the comment marker. If this really is a comment, skip to its
315 end and return ' '. If this is not a comment, return M (which will
316 be '/' or '-'). */
317
318 static int
319 skip_comment (pfile, m)
320 cpp_reader *pfile;
321 int m;
322 {
323 if (m == '/' && PEEKC() == '*')
324 {
325 skip_block_comment (pfile);
326 return ' ';
327 }
328 else if (m == '/' && PEEKC() == '/')
329 {
330 if (CPP_BUFFER (pfile)->system_header_p)
331 {
332 /* We silently allow C++ comments in system headers, irrespective
333 of conformance mode, because lots of busted systems do that
334 and trying to clean it up in fixincludes is a nightmare. */
335 skip_line_comment (pfile);
336 return ' ';
337 }
338 else if (CPP_OPTIONS (pfile)->cplusplus_comments)
339 {
340 if (CPP_OPTIONS (pfile)->c89
341 && CPP_PEDANTIC (pfile)
342 && ! CPP_BUFFER (pfile)->warned_cplusplus_comments)
343 {
344 cpp_pedwarn (pfile,
345 "C++ style comments are not allowed in ISO C89");
346 cpp_pedwarn (pfile,
347 "(this will be reported only once per input file)");
348 CPP_BUFFER (pfile)->warned_cplusplus_comments = 1;
349 }
350 skip_line_comment (pfile);
351 return ' ';
352 }
353 else
354 return m;
355 }
356 else if (m == '-' && PEEKC() == '-'
357 && CPP_OPTIONS (pfile)->chill)
358 {
359 skip_line_comment (pfile);
360 return ' ';
361 }
362 else
363 return m;
364 }
365
366 /* Identical to skip_comment except that it copies the comment into the
367 token_buffer. This is used if put_out_comments. */
368 static int
369 copy_comment (pfile, m)
370 cpp_reader *pfile;
371 int m;
372 {
373 U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
374 U_CHAR *limit;
375
376 if (skip_comment (pfile, m) == m)
377 return m;
378
379 CPP_PUTC (pfile, m);
380 for (limit = CPP_BUFFER (pfile)->cur; start <= limit; start++)
381 if (*start != '\r')
382 CPP_PUTC (pfile, *start);
383 return ' ';
384 }
385
386 /* Skip whitespace \-newline and comments. Does not macro-expand. */
387
388 void
389 cpp_skip_hspace (pfile)
390 cpp_reader *pfile;
391 {
392 int c;
393 while (1)
394 {
395 c = GETC();
396 if (c == EOF)
397 return;
398 else if (is_hspace(c))
399 {
400 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
401 cpp_pedwarn (pfile, "%s in preprocessing directive",
402 c == '\f' ? "formfeed" : "vertical tab");
403 }
404 else if (c == '\r')
405 {
406 /* \r is a backslash-newline marker if !has_escapes, and
407 a deletable-whitespace or no-reexpansion marker otherwise. */
408 if (CPP_BUFFER (pfile)->has_escapes)
409 {
410 if (PEEKC() == ' ')
411 FORWARD(1);
412 else
413 break;
414 }
415 else
416 CPP_BUFFER (pfile)->lineno++;
417 }
418 else if (c == '/' || c == '-')
419 {
420 c = skip_comment (pfile, c);
421 if (c != ' ')
422 break;
423 }
424 else
425 break;
426 }
427 FORWARD(-1);
428 }
429
430 /* Read the rest of the current line.
431 The line is appended to PFILE's output buffer. */
432
433 static void
434 copy_rest_of_line (pfile)
435 cpp_reader *pfile;
436 {
437 for (;;)
438 {
439 int c = GETC();
440 switch (c)
441 {
442 case '\n':
443 FORWARD(-1);
444 case EOF:
445 CPP_NUL_TERMINATE (pfile);
446 return;
447
448 case '\r':
449 if (CPP_BUFFER (pfile)->has_escapes)
450 break;
451 else
452 {
453 CPP_BUFFER (pfile)->lineno++;
454 continue;
455 }
456 case '\'':
457 case '\"':
458 parse_string (pfile, c);
459 continue;
460 case '/':
461 if (PEEKC() == '*')
462 {
463 if (CPP_TRADITIONAL (pfile))
464 CPP_PUTS (pfile, "/**/", 4);
465 skip_block_comment (pfile);
466 continue;
467 }
468 /* else fall through */
469 case '-':
470 c = skip_comment (pfile, c);
471 break;
472
473 case '\f':
474 case '\v':
475 if (CPP_PEDANTIC (pfile))
476 cpp_pedwarn (pfile, "%s in preprocessing directive",
477 c == '\f' ? "formfeed" : "vertical tab");
478 break;
479
480 }
481 CPP_PUTC (pfile, c);
482 }
483 }
484
485 /* FIXME: It is almost definitely a performance win to make this do
486 the scan itself. >75% of calls to copy_r_o_l are from here or
487 skip_if_group, which means the common case is to copy stuff into the
488 token_buffer only to discard it. */
489 void
490 skip_rest_of_line (pfile)
491 cpp_reader *pfile;
492 {
493 long old = CPP_WRITTEN (pfile);
494 copy_rest_of_line (pfile);
495 CPP_SET_WRITTEN (pfile, old);
496 }
497
498 /* Handle a possible # directive.
499 '#' has already been read. */
500
501 static int
502 handle_directive (pfile)
503 cpp_reader *pfile;
504 {
505 int c;
506 register const struct directive *kt;
507 int ident_length;
508 U_CHAR *ident;
509 long old_written = CPP_WRITTEN (pfile);
510
511 cpp_skip_hspace (pfile);
512
513 c = PEEKC ();
514 /* # followed by a number is equivalent to #line. Do not recognize
515 this form in assembly language source files. Complain about this
516 form if we're being pedantic, but not if this is regurgitated
517 input (preprocessed or fed back in by the C++ frontend). */
518 if (c >= '0' && c <= '9')
519 {
520 if (CPP_OPTIONS (pfile)->lang_asm)
521 return 0;
522
523 if (CPP_PEDANTIC (pfile)
524 && ! CPP_PREPROCESSED (pfile)
525 && ! CPP_BUFFER (pfile)->manual_pop)
526 cpp_pedwarn (pfile, "`#' followed by integer");
527 do_line (pfile, NULL);
528 return 1;
529 }
530
531 /* If we are rescanning preprocessed input, don't obey any directives
532 other than # nnn. */
533 if (CPP_PREPROCESSED (pfile))
534 return 0;
535
536 /* Now find the directive name. */
537 CPP_PUTC (pfile, '#');
538 parse_name (pfile, GETC());
539 ident = pfile->token_buffer + old_written + 1;
540 ident_length = CPP_PWRITTEN (pfile) - ident;
541 if (ident_length == 0)
542 {
543 /* A line of just `#' becomes blank. A line with something
544 other than an identifier after the # is reparsed as a non-
545 directive line. */
546 CPP_SET_WRITTEN (pfile, old_written);
547 return (PEEKC() == '\n');
548 }
549
550 /* Decode the keyword and call the appropriate expansion routine. */
551 for (kt = directive_table; ; kt++)
552 {
553 if (kt->length <= 0)
554 /* # identifier, but not a legit directive. Pass onward as a
555 CPP_DIRECTIVE token anyway - let the consumer worry about it. */
556 return 1;
557 if (kt->length == ident_length
558 && !strncmp (kt->name, ident, ident_length))
559 break;
560 }
561
562 CPP_SET_WRITTEN (pfile, old_written);
563
564 if (pfile->no_directives)
565 {
566 cpp_error (pfile, "`#%s' may not be used inside a macro argument",
567 kt->name);
568 skip_rest_of_line (pfile);
569 }
570 else
571 (*kt->func) (pfile, kt);
572
573 return 1;
574 }
575
576 /* Pass a directive through to the output file.
577 BUF points to the contents of the directive, as a contiguous string.
578 LEN is the length of the string pointed to by BUF.
579 KEYWORD is the keyword-table entry for the directive. */
580
581 static void
582 pass_thru_directive (buf, len, pfile, keyword)
583 const U_CHAR *buf;
584 size_t len;
585 cpp_reader *pfile;
586 const struct directive *keyword;
587 {
588 register unsigned keyword_length = keyword->length;
589
590 CPP_RESERVE (pfile, 1 + keyword_length + len);
591 CPP_PUTC_Q (pfile, '#');
592 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
593 if (len != 0 && buf[0] != ' ')
594 CPP_PUTC_Q (pfile, ' ');
595 CPP_PUTS_Q (pfile, buf, len);
596 }
597
598 /* Check a purported macro name SYMNAME, and yield its length. */
599
600 int
601 check_macro_name (pfile, symname)
602 cpp_reader *pfile;
603 const U_CHAR *symname;
604 {
605 const U_CHAR *p;
606 int sym_length;
607
608 for (p = symname; is_idchar(*p); p++)
609 ;
610 sym_length = p - symname;
611 if (sym_length == 0
612 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
613 cpp_error (pfile, "invalid macro name");
614 else if (!is_idstart(*symname)
615 || (! strncmp (symname, "defined", 7) && sym_length == 7)) {
616 U_CHAR *msg; /* what pain... */
617 msg = (U_CHAR *) alloca (sym_length + 1);
618 bcopy (symname, msg, sym_length);
619 msg[sym_length] = 0;
620 cpp_error (pfile, "invalid macro name `%s'", msg);
621 }
622 return sym_length;
623 }
624
625 /* Process a #define command.
626 KEYWORD is the keyword-table entry for #define,
627 or NULL for a "predefined" macro,
628 or the keyword-table entry for #pragma in the case of a #pragma poison. */
629
630 static int
631 do_define (pfile, keyword)
632 cpp_reader *pfile;
633 const struct directive *keyword;
634 {
635 int hashcode;
636 MACRODEF mdef;
637 HASHNODE *hp;
638 long here;
639 U_CHAR *macro, *buf, *end;
640 enum node_type new_type;
641
642 here = CPP_WRITTEN (pfile);
643 copy_rest_of_line (pfile);
644
645 if (keyword == NULL || keyword->type == T_DEFINE)
646 new_type = T_MACRO;
647 else
648 new_type = T_POISON;
649
650 /* Copy out the line so we can pop the token buffer. */
651 buf = pfile->token_buffer + here;
652 end = CPP_PWRITTEN (pfile);
653 macro = (U_CHAR *) alloca (end - buf + 1);
654 memcpy (macro, buf, end - buf + 1);
655 end = macro + (end - buf);
656
657 CPP_SET_WRITTEN (pfile, here);
658
659 mdef = create_definition (macro, end, pfile, keyword == NULL);
660 if (mdef.defn == 0)
661 return 0;
662
663 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
664
665 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
666 {
667 int ok = 0;
668 /* Redefining a precompiled key is ok. */
669 if (hp->type == T_PCSTRING)
670 ok = 1;
671 /* Redefining a poisoned identifier is even worse than `not ok'. */
672 else if (hp->type == T_POISON)
673 ok = -1;
674 /* Redefining a macro is ok if the definitions are the same. */
675 else if (hp->type == T_MACRO)
676 ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
677 /* Redefining a constant is ok with -D. */
678 else if (hp->type == T_CONST || hp->type == T_STDC)
679 ok = ! CPP_OPTIONS (pfile)->done_initializing;
680 /* Print the warning or error if it's not ok. */
681 if (ok <= 0)
682 {
683 if (hp->type == T_POISON)
684 cpp_error (pfile, "redefining poisoned `%.*s'",
685 mdef.symlen, mdef.symnam);
686 else
687 cpp_pedwarn (pfile, "`%.*s' redefined", mdef.symlen, mdef.symnam);
688 if (hp->type == T_MACRO && CPP_OPTIONS (pfile)->done_initializing)
689 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file,
690 hp->value.defn->line, -1,
691 "this is the location of the previous definition");
692 }
693 if (hp->type != T_POISON)
694 {
695 /* Replace the old definition. */
696 hp->type = new_type;
697 hp->value.defn = mdef.defn;
698 }
699 }
700 else
701 cpp_install (pfile, mdef.symnam, mdef.symlen, new_type,
702 (char *) mdef.defn, hashcode);
703
704 if (keyword != NULL && keyword->type == T_DEFINE)
705 {
706 if (CPP_OPTIONS (pfile)->debug_output
707 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
708 dump_definition (pfile, mdef);
709 else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
710 pass_thru_directive (mdef.symnam, mdef.symlen, pfile, keyword);
711 }
712
713 return 0;
714 }
715
716
717 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
718 If BUFFER != NULL, then use the LENGTH characters in BUFFER
719 as the new input buffer.
720 Return the new buffer, or NULL on failure. */
721
722 cpp_buffer *
723 cpp_push_buffer (pfile, buffer, length)
724 cpp_reader *pfile;
725 U_CHAR *buffer;
726 long length;
727 {
728 cpp_buffer *buf = CPP_BUFFER (pfile);
729 cpp_buffer *new;
730 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
731 {
732 cpp_fatal (pfile, "macro or `#include' recursion too deep");
733 return NULL;
734 }
735
736 new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
737
738 new->if_stack = pfile->if_stack;
739 new->cleanup = null_cleanup;
740 new->underflow = null_underflow;
741 new->buf = new->cur = buffer;
742 new->alimit = new->rlimit = buffer + length;
743 new->prev = buf;
744 new->mark = -1;
745
746 CPP_BUFFER (pfile) = new;
747 return new;
748 }
749
750 cpp_buffer *
751 cpp_pop_buffer (pfile)
752 cpp_reader *pfile;
753 {
754 cpp_buffer *buf = CPP_BUFFER (pfile);
755 (*buf->cleanup) (buf, pfile);
756 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
757 free (buf);
758 pfile->buffer_stack_depth--;
759 return CPP_BUFFER (pfile);
760 }
761
762 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
763 Pop the buffer when done. */
764
765 void
766 cpp_scan_buffer (pfile)
767 cpp_reader *pfile;
768 {
769 cpp_buffer *buffer = CPP_BUFFER (pfile);
770 enum cpp_token token;
771 if (CPP_OPTIONS (pfile)->no_output)
772 {
773 long old_written = CPP_WRITTEN (pfile);
774 /* In no-output mode, we can ignore everything but directives. */
775 for (;;)
776 {
777 if (! pfile->only_seen_white)
778 skip_rest_of_line (pfile);
779 token = cpp_get_token (pfile);
780 if (token == CPP_EOF) /* Should not happen ... */
781 break;
782 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
783 {
784 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
785 != CPP_NULL_BUFFER (pfile))
786 cpp_pop_buffer (pfile);
787 break;
788 }
789 }
790 CPP_SET_WRITTEN (pfile, old_written);
791 }
792 else
793 {
794 for (;;)
795 {
796 token = cpp_get_token (pfile);
797 if (token == CPP_EOF) /* Should not happen ... */
798 break;
799 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
800 {
801 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
802 != CPP_NULL_BUFFER (pfile))
803 cpp_pop_buffer (pfile);
804 break;
805 }
806 }
807 }
808 }
809
810 /*
811 * Rescan a string (which may have escape marks) into pfile's buffer.
812 * Place the result in pfile->token_buffer.
813 *
814 * The input is copied before it is scanned, so it is safe to pass
815 * it something from the token_buffer that will get overwritten
816 * (because it follows CPP_WRITTEN). This is used by do_include.
817 */
818
819 void
820 cpp_expand_to_buffer (pfile, buf, length)
821 cpp_reader *pfile;
822 const U_CHAR *buf;
823 int length;
824 {
825 register cpp_buffer *ip;
826 U_CHAR *buf1;
827 int save_no_output;
828
829 if (length < 0)
830 {
831 cpp_ice (pfile, "length < 0 in cpp_expand_to_buffer");
832 return;
833 }
834
835 /* Set up the input on the input stack. */
836
837 buf1 = (U_CHAR *) alloca (length + 1);
838 memcpy (buf1, buf, length);
839 buf1[length] = 0;
840
841 ip = cpp_push_buffer (pfile, buf1, length);
842 if (ip == NULL)
843 return;
844 ip->has_escapes = 1;
845
846 /* Scan the input, create the output. */
847 save_no_output = CPP_OPTIONS (pfile)->no_output;
848 CPP_OPTIONS (pfile)->no_output = 0;
849 cpp_scan_buffer (pfile);
850 CPP_OPTIONS (pfile)->no_output = save_no_output;
851
852 CPP_NUL_TERMINATE (pfile);
853 }
854
855 void
856 cpp_buf_line_and_col (pbuf, linep, colp)
857 register cpp_buffer *pbuf;
858 long *linep, *colp;
859 {
860 if (pbuf)
861 {
862 *linep = pbuf->lineno;
863 if (colp)
864 *colp = pbuf->cur - pbuf->line_base;
865 }
866 else
867 {
868 *linep = 0;
869 if (colp)
870 *colp = 0;
871 }
872 }
873
874 /* Return the cpp_buffer that corresponds to a file (not a macro). */
875
876 cpp_buffer *
877 cpp_file_buffer (pfile)
878 cpp_reader *pfile;
879 {
880 cpp_buffer *ip = CPP_BUFFER (pfile);
881
882 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
883 if (ip->fname != NULL)
884 return ip;
885 return NULL;
886 }
887
888 /*
889 * write out a #line command, for instance, after an #include file.
890 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
891 */
892
893 void
894 output_line_command (pfile, file_change)
895 cpp_reader *pfile;
896 enum file_change_code file_change;
897 {
898 long line;
899 cpp_buffer *ip = CPP_BUFFER (pfile);
900
901 if (ip->fname == NULL)
902 return;
903
904 if (CPP_OPTIONS (pfile)->no_line_commands
905 || CPP_OPTIONS (pfile)->no_output)
906 return;
907
908 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, NULL);
909
910 /* If the current file has not changed, we omit the #line if it would
911 appear to be a no-op, and we output a few newlines instead
912 if we want to increase the line number by a small amount.
913 We cannot do this if pfile->lineno is zero, because that means we
914 haven't output any line commands yet. (The very first line command
915 output is a `same_file' command.) */
916 if (file_change == same_file && pfile->lineno != 0)
917 {
918 if (line == pfile->lineno)
919 return;
920
921 /* If the inherited line number is a little too small,
922 output some newlines instead of a #line command. */
923 if (line > pfile->lineno && line < pfile->lineno + 8)
924 {
925 CPP_RESERVE (pfile, 20);
926 while (line > pfile->lineno)
927 {
928 CPP_PUTC_Q (pfile, '\n');
929 pfile->lineno++;
930 }
931 return;
932 }
933 }
934
935 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
936 CPP_PUTS_Q (pfile, "# ", 2);
937
938 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
939 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
940
941 quote_string (pfile, ip->nominal_fname);
942 if (file_change != same_file)
943 {
944 CPP_PUTC_Q (pfile, ' ');
945 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
946 }
947 /* Tell cc1 if following text comes from a system header file. */
948 if (ip->system_header_p)
949 {
950 CPP_PUTC_Q (pfile, ' ');
951 CPP_PUTC_Q (pfile, '3');
952 }
953 #ifndef NO_IMPLICIT_EXTERN_C
954 /* Tell cc1plus if following text should be treated as C. */
955 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
956 {
957 CPP_PUTC_Q (pfile, ' ');
958 CPP_PUTC_Q (pfile, '4');
959 }
960 #endif
961 CPP_PUTC_Q (pfile, '\n');
962 pfile->lineno = line;
963 }
964
965
966 /* Like cpp_get_token, except that it does not read past end-of-line.
967 Also, horizontal space is skipped, and macros are popped. */
968
969 static enum cpp_token
970 get_directive_token (pfile)
971 cpp_reader *pfile;
972 {
973 long old_written = CPP_WRITTEN (pfile);
974 enum cpp_token token;
975
976 for (;;)
977 {
978 cpp_skip_hspace (pfile);
979 if (PEEKC () == '\n')
980 return CPP_VSPACE;
981
982 token = cpp_get_token (pfile);
983 /* token could be hspace at the beginning of a macro. */
984 if (token == CPP_HSPACE || token == CPP_COMMENT)
985 {
986 CPP_SET_WRITTEN (pfile, old_written);
987 continue;
988 }
989
990 /* token cannot be vspace, it would have been caught above. */
991 if (token == CPP_VSPACE)
992 {
993 cpp_ice (pfile, "VSPACE in get_directive_token");
994 return token;
995 }
996
997 /* token cannot be POP unless the buffer is a macro buffer. */
998 if (token != CPP_POP)
999 return token;
1000
1001 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1002 {
1003 cpp_ice (pfile, "POP of file buffer in get_directive_token");
1004 return token;
1005 }
1006
1007 /* We must pop the buffer by hand, or else cpp_get_token might
1008 hand us white space or newline on the next invocation. */
1009 cpp_pop_buffer (pfile);
1010 }
1011 }
1012 \f
1013 /* Handle #include and #import.
1014 This function expects to see "fname" or <fname> on the input.
1015
1016 The input is normally in part of the output_buffer following
1017 CPP_WRITTEN, and will get overwritten by output_line_command.
1018 I.e. in input file specification has been popped by handle_directive.
1019 This is safe. */
1020
1021 static int
1022 do_include (pfile, keyword)
1023 cpp_reader *pfile;
1024 const struct directive *keyword;
1025 {
1026 int importing = (keyword->type == T_IMPORT);
1027 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
1028 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
1029 int before; /* included before? */
1030 long flen;
1031 unsigned char *ftok;
1032 cpp_buffer *fp;
1033
1034 enum cpp_token token;
1035
1036 /* Chain of dirs to search */
1037 struct include_hash *ihash;
1038 struct file_name_list *search_start;
1039
1040 long old_written = CPP_WRITTEN (pfile);
1041
1042 int fd;
1043
1044 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1045 {
1046 if (importing)
1047 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
1048 if (skip_dirs)
1049 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
1050 }
1051
1052 if (importing && CPP_OPTIONS (pfile)->warn_import
1053 && !CPP_OPTIONS (pfile)->inhibit_warnings
1054 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
1055 {
1056 pfile->import_warning = 1;
1057 cpp_warning (pfile,
1058 "#import is obsolete, use an #ifndef wrapper in the header file");
1059 }
1060
1061 pfile->parsing_include_directive++;
1062 token = get_directive_token (pfile);
1063 pfile->parsing_include_directive--;
1064
1065 if (token == CPP_STRING)
1066 {
1067 if (pfile->token_buffer[old_written] == '<')
1068 angle_brackets = 1;
1069 }
1070 #ifdef VMS
1071 else if (token == CPP_NAME)
1072 {
1073 /* Support '#include xyz' like VAX-C. It is taken as
1074 '#include <xyz.h>' and generates a warning. */
1075 cpp_warning (pfile,
1076 "`#include filename' is obsolete, use `#include <filename.h>'");
1077 angle_brackets = 1;
1078
1079 /* Append the missing `.h' to the name. */
1080 CPP_PUTS (pfile, ".h", 2);
1081 }
1082 #endif
1083 else
1084 {
1085 cpp_error (pfile,
1086 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1087 CPP_SET_WRITTEN (pfile, old_written);
1088 skip_rest_of_line (pfile);
1089 return 0;
1090 }
1091
1092 flen = CPP_WRITTEN (pfile) - old_written;
1093 ftok = (unsigned char *) alloca (flen + 1);
1094 memcpy (ftok, pfile->token_buffer + old_written, flen);
1095 ftok[flen] = '\0';
1096
1097 if (get_directive_token (pfile) != CPP_VSPACE)
1098 {
1099 cpp_error (pfile, "junk at end of `#include'");
1100 skip_rest_of_line (pfile);
1101 }
1102
1103 CPP_SET_WRITTEN (pfile, old_written);
1104
1105 if (flen == 0)
1106 {
1107 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
1108 return 0;
1109 }
1110
1111 if (CPP_OPTIONS (pfile)->dump_includes)
1112 pass_thru_directive (ftok,
1113 flen
1114 #ifdef VMS
1115 - ((token == CPP_NAME) ? 2 : 0)
1116 #endif
1117 , pfile, keyword);
1118
1119 #ifdef VMS
1120 if (token == CPP_STRING)
1121 #endif
1122 {
1123 ftok++;
1124 flen -= 2;
1125 ftok[flen] = '\0';
1126 }
1127
1128 search_start = 0;
1129
1130 for (fp = CPP_BUFFER (pfile);
1131 fp != CPP_NULL_BUFFER (pfile);
1132 fp = CPP_PREV_BUFFER (fp))
1133 if (fp->fname != NULL)
1134 break;
1135
1136 if (fp == CPP_NULL_BUFFER (pfile))
1137 {
1138 cpp_ice (pfile, "fp == NULL_BUFFER in do_include");
1139 return 0;
1140 }
1141
1142 /* For #include_next, skip in the search path past the dir in which the
1143 containing file was found. Treat files specified using an absolute path
1144 as if there are no more directories to search. Treat the primary source
1145 file like any other included source, but generate a warning. */
1146 if (skip_dirs && CPP_PREV_BUFFER(fp) != CPP_NULL_BUFFER (pfile))
1147 {
1148 if (fp->ihash->foundhere != ABSOLUTE_PATH)
1149 search_start = fp->ihash->foundhere->next;
1150 }
1151 else
1152 {
1153 if (skip_dirs)
1154 cpp_warning (pfile, "#include_next in primary source file");
1155
1156 if (angle_brackets)
1157 search_start = CPP_OPTIONS (pfile)->bracket_include;
1158 else
1159 {
1160 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
1161 {
1162 if (fp)
1163 search_start = fp->actual_dir;
1164 }
1165 else
1166 search_start = CPP_OPTIONS (pfile)->quote_include;
1167 }
1168 }
1169
1170 if (!search_start)
1171 {
1172 cpp_error (pfile, "No include path in which to find %s", ftok);
1173 return 0;
1174 }
1175
1176 fd = find_include_file (pfile, ftok, search_start, &ihash, &before);
1177
1178 if (fd == -2)
1179 return 0;
1180
1181 if (fd == -1)
1182 {
1183 if (CPP_OPTIONS (pfile)->print_deps_missing_files
1184 && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
1185 (pfile->system_include_depth > 0)))
1186 {
1187 if (!angle_brackets)
1188 deps_output (pfile, ftok, ' ');
1189 else
1190 {
1191 char *p;
1192 struct file_name_list *ptr;
1193 /* If requested as a system header, assume it belongs in
1194 the first system header directory. */
1195 if (CPP_OPTIONS (pfile)->bracket_include)
1196 ptr = CPP_OPTIONS (pfile)->bracket_include;
1197 else
1198 ptr = CPP_OPTIONS (pfile)->quote_include;
1199
1200 p = (char *) alloca (strlen (ptr->name)
1201 + strlen (ftok) + 2);
1202 if (*ptr->name != '\0')
1203 {
1204 strcpy (p, ptr->name);
1205 strcat (p, "/");
1206 }
1207 strcat (p, ftok);
1208 deps_output (pfile, p, ' ');
1209 }
1210 }
1211 /* If -M was specified, and this header file won't be added to
1212 the dependency list, then don't count this as an error,
1213 because we can still produce correct output. Otherwise, we
1214 can't produce correct output, because there may be
1215 dependencies we need inside the missing file, and we don't
1216 know what directory this missing file exists in. */
1217 else if (CPP_PRINT_DEPS (pfile)
1218 && (CPP_PRINT_DEPS (pfile)
1219 <= (angle_brackets || (pfile->system_include_depth > 0))))
1220 cpp_warning (pfile, "No include path in which to find %s", ftok);
1221 else
1222 cpp_error_from_errno (pfile, ftok);
1223
1224 return 0;
1225 }
1226
1227 /* For -M, add the file to the dependencies on its first inclusion. */
1228 if (!before && (CPP_PRINT_DEPS (pfile)
1229 > (angle_brackets || (pfile->system_include_depth > 0))))
1230 deps_output (pfile, ihash->name, ' ');
1231
1232 /* Handle -H option. */
1233 if (CPP_OPTIONS(pfile)->print_include_names)
1234 {
1235 fp = CPP_BUFFER (pfile);
1236 while ((fp = CPP_PREV_BUFFER (fp)) != CPP_NULL_BUFFER (pfile))
1237 putc ('.', stderr);
1238 fprintf (stderr, " %s\n", ihash->name);
1239 }
1240
1241 /* Actually process the file */
1242
1243 if (importing)
1244 ihash->control_macro = "";
1245
1246 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
1247 {
1248 close (fd);
1249 return 0;
1250 }
1251
1252 if (angle_brackets)
1253 pfile->system_include_depth++; /* Decremented in file_cleanup. */
1254
1255 if (finclude (pfile, fd, ihash))
1256 {
1257 output_line_command (pfile, enter_file);
1258 pfile->only_seen_white = 2;
1259 }
1260
1261 return 0;
1262 }
1263
1264 /* Subroutine of do_line. Read next token from PFILE without adding it to
1265 the output buffer. If it is a number between 1 and 4, store it in *NUM
1266 and return 1; otherwise, return 0 and complain if we aren't at the end
1267 of the directive. */
1268
1269 static int
1270 read_line_number (pfile, num)
1271 cpp_reader *pfile;
1272 int *num;
1273 {
1274 long save_written = CPP_WRITTEN (pfile);
1275 U_CHAR *p = pfile->token_buffer + save_written;
1276 enum cpp_token token = get_directive_token (pfile);
1277 CPP_SET_WRITTEN (pfile, save_written);
1278
1279 if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
1280 {
1281 *num = p[0] - '0';
1282 return 1;
1283 }
1284 else
1285 {
1286 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
1287 cpp_error (pfile, "invalid format `#line' command");
1288 return 0;
1289 }
1290 }
1291
1292 /* Interpret #line command.
1293 Note that the filename string (if any) is treated as if it were an
1294 include filename. That means no escape handling. */
1295
1296 static int
1297 do_line (pfile, keyword)
1298 cpp_reader *pfile;
1299 const struct directive *keyword ATTRIBUTE_UNUSED;
1300 {
1301 cpp_buffer *ip = CPP_BUFFER (pfile);
1302 int new_lineno;
1303 long old_written = CPP_WRITTEN (pfile);
1304 enum file_change_code file_change = same_file;
1305 enum cpp_token token;
1306 char *x;
1307
1308 token = get_directive_token (pfile);
1309
1310 if (token != CPP_NUMBER)
1311 {
1312 cpp_error (pfile, "token after `#line' is not an integer");
1313 goto bad_line_directive;
1314 }
1315
1316 new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
1317 if (x[0] != '\0')
1318 {
1319 cpp_error (pfile, "token after `#line' is not an integer");
1320 goto bad_line_directive;
1321 }
1322 CPP_SET_WRITTEN (pfile, old_written);
1323
1324 if (CPP_PEDANTIC (pfile) && new_lineno <= 0)
1325 cpp_pedwarn (pfile, "line number out of range in `#line' command");
1326
1327 token = get_directive_token (pfile);
1328
1329 if (token == CPP_STRING)
1330 {
1331 U_CHAR *fname = pfile->token_buffer + old_written + 1;
1332 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
1333 int action_number = 0;
1334
1335 if (read_line_number (pfile, &action_number))
1336 {
1337 if (CPP_PEDANTIC (pfile))
1338 cpp_pedwarn (pfile, "garbage at end of `#line' command");
1339
1340 if (action_number == 1)
1341 {
1342 file_change = enter_file;
1343 read_line_number (pfile, &action_number);
1344 }
1345 else if (action_number == 2)
1346 {
1347 file_change = leave_file;
1348 read_line_number (pfile, &action_number);
1349 }
1350 if (action_number == 3)
1351 {
1352 ip->system_header_p = 1;
1353 read_line_number (pfile, &action_number);
1354 }
1355 if (action_number == 4)
1356 {
1357 ip->system_header_p = 2;
1358 read_line_number (pfile, &action_number);
1359 }
1360 }
1361
1362 *end_name = '\0';
1363
1364 if (strcmp (fname, ip->nominal_fname))
1365 {
1366 const char *newname, *oldname;
1367 if (!strcmp (fname, ip->fname))
1368 newname = ip->fname;
1369 else if (ip->last_nominal_fname
1370 && !strcmp (fname, ip->last_nominal_fname))
1371 newname = ip->last_nominal_fname;
1372 else
1373 newname = xstrdup (fname);
1374
1375 oldname = ip->nominal_fname;
1376 ip->nominal_fname = newname;
1377
1378 if (ip->last_nominal_fname
1379 && ip->last_nominal_fname != oldname
1380 && ip->last_nominal_fname != newname
1381 && ip->last_nominal_fname != ip->fname)
1382 free ((void *) ip->last_nominal_fname);
1383
1384 if (newname == ip->fname)
1385 ip->last_nominal_fname = NULL;
1386 else
1387 ip->last_nominal_fname = oldname;
1388 }
1389 }
1390 else if (token != CPP_VSPACE && token != CPP_EOF)
1391 {
1392 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1393 goto bad_line_directive;
1394 }
1395
1396 /* The Newline at the end of this line remains to be processed.
1397 To put the next line at the specified line number,
1398 we must store a line number now that is one less. */
1399 ip->lineno = new_lineno - 1;
1400 CPP_SET_WRITTEN (pfile, old_written);
1401 output_line_command (pfile, file_change);
1402 return 0;
1403
1404 bad_line_directive:
1405 skip_rest_of_line (pfile);
1406 CPP_SET_WRITTEN (pfile, old_written);
1407 return 0;
1408 }
1409
1410 /* Remove the definition of a symbol from the symbol table.
1411 According to the C standard, it is not an error to undef
1412 something that has no definitions. */
1413 static int
1414 do_undef (pfile, keyword)
1415 cpp_reader *pfile;
1416 const struct directive *keyword;
1417 {
1418 int sym_length;
1419 HASHNODE *hp;
1420 U_CHAR *buf, *name, *limit;
1421 int c;
1422 long here = CPP_WRITTEN (pfile);
1423 enum cpp_token token;
1424
1425 cpp_skip_hspace (pfile);
1426 c = GETC();
1427 if (! is_idstart(c))
1428 {
1429 cpp_error (pfile, "token after #undef is not an identifier");
1430 skip_rest_of_line (pfile);
1431 return 1;
1432 }
1433
1434 parse_name (pfile, c);
1435 buf = pfile->token_buffer + here;
1436 limit = CPP_PWRITTEN(pfile);
1437
1438 /* Copy out the token so we can pop the token buffer. */
1439 name = (U_CHAR *) alloca (limit - buf + 1);
1440 bcopy(buf, name, limit - buf);
1441 name[limit - buf] = '\0';
1442
1443 token = get_directive_token (pfile);
1444 if (token != CPP_VSPACE && token != CPP_POP)
1445 {
1446 cpp_pedwarn (pfile, "junk on line after #undef");
1447 skip_rest_of_line (pfile);
1448 }
1449
1450 CPP_SET_WRITTEN (pfile, here);
1451
1452 sym_length = check_macro_name (pfile, buf);
1453
1454 while ((hp = cpp_lookup (pfile, name, sym_length, -1)) != NULL)
1455 {
1456 /* If we are generating additional info for debugging (with -g) we
1457 need to pass through all effective #undef commands. */
1458 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1459 pass_thru_directive (name, sym_length, pfile, keyword);
1460 if (hp->type == T_POISON)
1461 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
1462 else
1463 {
1464 if (hp->type != T_MACRO)
1465 cpp_warning (pfile, "undefining `%s'", hp->name);
1466 delete_macro (hp);
1467 }
1468 }
1469
1470 return 0;
1471 }
1472
1473 /* Wrap do_undef for -U processing. */
1474 void
1475 cpp_undef (pfile, macro)
1476 cpp_reader *pfile;
1477 U_CHAR *macro;
1478 {
1479 if (cpp_push_buffer (pfile, macro, strlen (macro)))
1480 {
1481 do_undef (pfile, NULL);
1482 cpp_pop_buffer (pfile);
1483 }
1484 }
1485
1486 \f
1487 /*
1488 * Report an error detected by the program we are processing.
1489 * Use the text of the line in the error message.
1490 * (We use error because it prints the filename & line#.)
1491 */
1492
1493 static int
1494 do_error (pfile, keyword)
1495 cpp_reader *pfile;
1496 const struct directive *keyword ATTRIBUTE_UNUSED;
1497 {
1498 long here = CPP_WRITTEN (pfile);
1499 U_CHAR *text;
1500 copy_rest_of_line (pfile);
1501 text = pfile->token_buffer + here;
1502 SKIP_WHITE_SPACE(text);
1503
1504 cpp_error (pfile, "#error %s", text);
1505 CPP_SET_WRITTEN (pfile, here);
1506 return 0;
1507 }
1508
1509 /*
1510 * Report a warning detected by the program we are processing.
1511 * Use the text of the line in the warning message, then continue.
1512 */
1513
1514 static int
1515 do_warning (pfile, keyword)
1516 cpp_reader *pfile;
1517 const struct directive *keyword ATTRIBUTE_UNUSED;
1518 {
1519 U_CHAR *text;
1520 long here = CPP_WRITTEN(pfile);
1521 copy_rest_of_line (pfile);
1522 text = pfile->token_buffer + here;
1523 SKIP_WHITE_SPACE(text);
1524
1525 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1526 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1527
1528 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
1529 if -pedantic-errors is given, #warning should cause an error. */
1530 cpp_pedwarn (pfile, "#warning %s", text);
1531 CPP_SET_WRITTEN (pfile, here);
1532 return 0;
1533 }
1534
1535 /* Report program identification.
1536 This is not precisely what cccp does with #ident, however I believe
1537 it matches `closely enough' (behavior is identical as long as there
1538 are no macros on the #ident line, which is pathological in my opinion). */
1539
1540 static int
1541 do_ident (pfile, keyword)
1542 cpp_reader *pfile;
1543 const struct directive *keyword ATTRIBUTE_UNUSED;
1544 {
1545 /* Allow #ident in system headers, since that's not user's fault. */
1546 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1547 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1548
1549 CPP_PUTS (pfile, "#ident ", 7);
1550 cpp_skip_hspace (pfile);
1551 copy_rest_of_line (pfile);
1552
1553 return 0;
1554 }
1555
1556 /* Just check for some recognized pragmas that need validation here,
1557 and leave the text in the token buffer to be output. */
1558
1559 static int
1560 do_pragma (pfile, keyword)
1561 cpp_reader *pfile;
1562 const struct directive *keyword ATTRIBUTE_UNUSED;
1563 {
1564 long here;
1565 U_CHAR *buf;
1566
1567 CPP_PUTS (pfile, "#pragma ", 8);
1568 cpp_skip_hspace (pfile);
1569
1570 here = CPP_WRITTEN (pfile);
1571 copy_rest_of_line (pfile);
1572 buf = pfile->token_buffer + here;
1573
1574 if (!strncmp (buf, "once", 4))
1575 {
1576 cpp_buffer *ip = NULL;
1577
1578 /* Allow #pragma once in system headers, since that's not the user's
1579 fault. */
1580 if (!CPP_BUFFER (pfile)->system_header_p)
1581 cpp_warning (pfile, "`#pragma once' is obsolete");
1582
1583 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
1584 {
1585 if (ip == CPP_NULL_BUFFER (pfile))
1586 return 0;
1587 if (ip->fname != NULL)
1588 break;
1589 }
1590
1591 if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
1592 cpp_warning (pfile, "`#pragma once' outside include file");
1593 else
1594 ip->ihash->control_macro = ""; /* never repeat */
1595 }
1596 else if (!strncmp (buf, "implementation", 14))
1597 {
1598 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1599 been included yet. */
1600 struct include_hash *ptr;
1601 U_CHAR *p = buf + 14, *fname, *fcopy;
1602 SKIP_WHITE_SPACE (p);
1603 if (*p == '\n' || *p != '\"')
1604 return 0;
1605
1606 fname = p + 1;
1607 p = (U_CHAR *) index (fname, '\"');
1608
1609 fcopy = (U_CHAR *) alloca (p - fname + 1);
1610 bcopy (fname, fcopy, p - fname);
1611 fcopy[p-fname] = '\0';
1612
1613 ptr = include_hash (pfile, fcopy, 0);
1614 if (ptr)
1615 cpp_warning (pfile,
1616 "`#pragma implementation' for `%s' appears after file is included",
1617 fcopy);
1618 }
1619 else if (!strncmp (buf, "poison", 6))
1620 {
1621 /* Poison these symbols so that all subsequent usage produces an
1622 error message. */
1623 U_CHAR *p = buf + 6;
1624 size_t plen;
1625 U_CHAR *syms;
1626 int writeit;
1627
1628 SKIP_WHITE_SPACE (p);
1629 plen = strlen(p) + 1;
1630
1631 syms = (U_CHAR *) alloca (plen);
1632 memcpy (syms, p, plen);
1633
1634 /* As a rule, don't include #pragma poison commands in output,
1635 unless the user asks for them. */
1636 writeit = (CPP_OPTIONS (pfile)->debug_output
1637 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions
1638 || CPP_OPTIONS (pfile)->dump_macros == dump_names);
1639
1640 if (writeit)
1641 CPP_SET_WRITTEN (pfile, here);
1642 else
1643 CPP_SET_WRITTEN (pfile, here-8);
1644
1645 if (writeit)
1646 {
1647 CPP_RESERVE (pfile, plen + 7);
1648 CPP_PUTS_Q (pfile, "poison", 7);
1649 }
1650
1651 while (*syms != '\0')
1652 {
1653 U_CHAR *end = syms;
1654
1655 while (is_idchar(*end))
1656 end++;
1657
1658 if (!is_hspace(*end) && *end != '\0')
1659 {
1660 cpp_error (pfile, "invalid #pragma poison directive");
1661 return 1;
1662 }
1663
1664 if (cpp_push_buffer (pfile, syms, end - syms) != NULL)
1665 {
1666 do_define (pfile, keyword);
1667 cpp_pop_buffer (pfile);
1668 }
1669 if (writeit)
1670 {
1671 CPP_PUTC_Q (pfile, ' ');
1672 CPP_PUTS_Q (pfile, syms, end - syms);
1673 }
1674 syms = end;
1675 SKIP_WHITE_SPACE (syms);
1676 }
1677 }
1678
1679 return 0;
1680 }
1681
1682 #ifdef SCCS_DIRECTIVE
1683 /* Just ignore #sccs, on systems where we define it at all. */
1684
1685 static int
1686 do_sccs (pfile, keyword)
1687 cpp_reader *pfile;
1688 const struct directive *keyword ATTRIBUTE_UNUSED;
1689 {
1690 if (CPP_PEDANTIC (pfile))
1691 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1692 skip_rest_of_line (pfile);
1693 return 0;
1694 }
1695 #endif
1696 \f
1697
1698 /* We've found an `#if' directive. If the only thing before it in
1699 this file is white space, and if it is of the form
1700 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1701 for inclusion of this file. (See redundant_include_p in cppfiles.c
1702 for an explanation of controlling macros.) If so, return a
1703 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1704
1705 static U_CHAR *
1706 detect_if_not_defined (pfile)
1707 cpp_reader *pfile;
1708 {
1709 U_CHAR *control_macro = 0;
1710
1711 if (pfile->only_seen_white == 2)
1712 {
1713 char *ident;
1714 enum cpp_token token;
1715 int base_offset;
1716 int token_offset;
1717 int need_rparen = 0;
1718
1719 /* Save state required for restore. */
1720 pfile->no_macro_expand++;
1721 parse_set_mark (pfile);
1722 base_offset = CPP_WRITTEN (pfile);
1723
1724 /* Look for `!', */
1725 if (get_directive_token (pfile) != CPP_OTHER
1726 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1727 || CPP_PWRITTEN (pfile)[-1] != '!')
1728 goto restore;
1729
1730 /* ...then `defined', */
1731 token_offset = CPP_WRITTEN (pfile);
1732 token = get_directive_token (pfile);
1733 if (token != CPP_NAME)
1734 goto restore;
1735 ident = pfile->token_buffer + token_offset;
1736 CPP_NUL_TERMINATE (pfile);
1737 if (strcmp (ident, "defined"))
1738 goto restore;
1739
1740 /* ...then an optional '(' and the name, */
1741 token_offset = CPP_WRITTEN (pfile);
1742 token = get_directive_token (pfile);
1743 if (token == CPP_LPAREN)
1744 {
1745 token_offset = CPP_WRITTEN (pfile);
1746 token = get_directive_token (pfile);
1747 if (token != CPP_NAME)
1748 goto restore;
1749 need_rparen = 1;
1750 }
1751 else if (token != CPP_NAME)
1752 goto restore;
1753
1754 ident = pfile->token_buffer + token_offset;
1755 CPP_NUL_TERMINATE (pfile);
1756
1757 /* ...then the ')', if necessary, */
1758 if ((!need_rparen || get_directive_token (pfile) == CPP_RPAREN)
1759 /* ...and make sure there's nothing else on the line. */
1760 && get_directive_token (pfile) == CPP_VSPACE)
1761 control_macro = xstrdup (ident);
1762
1763 restore:
1764 CPP_SET_WRITTEN (pfile, base_offset);
1765 pfile->no_macro_expand--;
1766 parse_goto_mark (pfile);
1767 }
1768
1769 return control_macro;
1770 }
1771
1772 /*
1773 * handle #if command by
1774 * 1) inserting special `defined' keyword into the hash table
1775 * that gets turned into 0 or 1 by special_symbol (thus,
1776 * if the luser has a symbol called `defined' already, it won't
1777 * work inside the #if command)
1778 * 2) rescan the input into a temporary output buffer
1779 * 3) pass the output buffer to the yacc parser and collect a value
1780 * 4) clean up the mess left from steps 1 and 2.
1781 * 5) call conditional_skip to skip til the next #endif (etc.),
1782 * or not, depending on the value from step 3.
1783 */
1784
1785 static int
1786 do_if (pfile, keyword)
1787 cpp_reader *pfile;
1788 const struct directive *keyword ATTRIBUTE_UNUSED;
1789 {
1790 U_CHAR *control_macro = detect_if_not_defined (pfile);
1791 HOST_WIDEST_INT value = eval_if_expression (pfile);
1792 conditional_skip (pfile, value == 0, T_IF, control_macro);
1793 return 0;
1794 }
1795
1796 /*
1797 * handle a #elif directive by not changing if_stack either.
1798 * see the comment above do_else.
1799 */
1800
1801 static int
1802 do_elif (pfile, keyword)
1803 cpp_reader *pfile;
1804 const struct directive *keyword ATTRIBUTE_UNUSED;
1805 {
1806 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1807 {
1808 cpp_error (pfile, "`#elif' not within a conditional");
1809 return 0;
1810 }
1811 else
1812 {
1813 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
1814 {
1815 cpp_error (pfile, "`#elif' after `#else'");
1816 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1817 "the conditional began here");
1818 }
1819 pfile->if_stack->type = T_ELIF;
1820 }
1821
1822 if (pfile->if_stack->if_succeeded)
1823 skip_if_group (pfile);
1824 else
1825 {
1826 HOST_WIDEST_INT value = eval_if_expression (pfile);
1827 if (value == 0)
1828 skip_if_group (pfile);
1829 else
1830 {
1831 ++pfile->if_stack->if_succeeded; /* continue processing input */
1832 output_line_command (pfile, same_file);
1833 }
1834 }
1835 return 0;
1836 }
1837
1838 /*
1839 * evaluate a #if expression in BUF, of length LENGTH,
1840 * then parse the result as a C expression and return the value as an int.
1841 */
1842
1843 static HOST_WIDEST_INT
1844 eval_if_expression (pfile)
1845 cpp_reader *pfile;
1846 {
1847 HOST_WIDEST_INT value;
1848 long old_written = CPP_WRITTEN (pfile);
1849
1850 value = cpp_parse_expr (pfile);
1851
1852 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1853
1854 return value;
1855 }
1856
1857 /*
1858 * routine to handle ifdef/ifndef. Try to look up the symbol,
1859 * then do or don't skip to the #endif/#else/#elif depending
1860 * on what directive is actually being processed.
1861 */
1862
1863 static int
1864 do_xifdef (pfile, keyword)
1865 cpp_reader *pfile;
1866 const struct directive *keyword;
1867 {
1868 int skip;
1869 cpp_buffer *ip = CPP_BUFFER (pfile);
1870 U_CHAR *ident;
1871 int ident_length;
1872 enum cpp_token token;
1873 int start_of_file = 0;
1874 U_CHAR *control_macro = 0;
1875 int old_written = CPP_WRITTEN (pfile);
1876
1877 /* Detect a #ifndef at start of file (not counting comments). */
1878 if (ip->fname != 0 && keyword->type == T_IFNDEF)
1879 start_of_file = pfile->only_seen_white == 2;
1880
1881 pfile->no_macro_expand++;
1882 token = get_directive_token (pfile);
1883 pfile->no_macro_expand--;
1884
1885 ident = pfile->token_buffer + old_written;
1886 ident_length = CPP_WRITTEN (pfile) - old_written;
1887 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1888
1889 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1890 {
1891 skip = (keyword->type == T_IFDEF);
1892 if (! CPP_TRADITIONAL (pfile))
1893 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
1894 }
1895 else if (token == CPP_NAME)
1896 {
1897 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
1898 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
1899 if (start_of_file && !skip)
1900 {
1901 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
1902 bcopy (ident, control_macro, ident_length + 1);
1903 }
1904 if (hp != NULL && hp->type == T_POISON)
1905 {
1906 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1907 skip = !skip;
1908 }
1909 }
1910 else
1911 {
1912 skip = (keyword->type == T_IFDEF);
1913 if (! CPP_TRADITIONAL (pfile))
1914 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
1915 }
1916
1917 if (!CPP_TRADITIONAL (pfile))
1918 { int c;
1919 cpp_skip_hspace (pfile);
1920 c = PEEKC ();
1921 if (c != EOF && c != '\n')
1922 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
1923 }
1924 skip_rest_of_line (pfile);
1925
1926 conditional_skip (pfile, skip, T_IF, control_macro);
1927 return 0;
1928 }
1929
1930 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1931 If this is a #ifndef starting at the beginning of a file,
1932 CONTROL_MACRO is the macro name tested by the #ifndef.
1933 Otherwise, CONTROL_MACRO is 0. */
1934
1935 static void
1936 conditional_skip (pfile, skip, type, control_macro)
1937 cpp_reader *pfile;
1938 int skip;
1939 enum node_type type;
1940 U_CHAR *control_macro;
1941 {
1942 IF_STACK_FRAME *temp;
1943
1944 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
1945 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
1946 temp->lineno = CPP_BUFFER (pfile)->lineno;
1947 temp->next = pfile->if_stack;
1948 temp->control_macro = control_macro;
1949 pfile->if_stack = temp;
1950
1951 pfile->if_stack->type = type;
1952
1953 if (skip != 0) {
1954 skip_if_group (pfile);
1955 return;
1956 } else {
1957 ++pfile->if_stack->if_succeeded;
1958 output_line_command (pfile, same_file);
1959 }
1960 }
1961
1962 /* Subroutine of skip_if_group. Examine one preprocessing directive and
1963 return 0 if skipping should continue, 1 if it should halt. Also
1964 adjusts the if_stack as appropriate.
1965 The `#' has been read, but not the identifier. */
1966
1967 static int
1968 consider_directive_while_skipping (pfile, stack)
1969 cpp_reader *pfile;
1970 IF_STACK_FRAME *stack;
1971 {
1972 long ident_len, ident;
1973 const struct directive *kt;
1974 IF_STACK_FRAME *temp;
1975
1976 cpp_skip_hspace (pfile);
1977
1978 ident = CPP_WRITTEN (pfile);
1979 parse_name (pfile, GETC());
1980 ident_len = CPP_WRITTEN (pfile) - ident;
1981
1982 CPP_SET_WRITTEN (pfile, ident);
1983
1984 for (kt = directive_table; kt->length >= 0; kt++)
1985 if (kt->length == ident_len
1986 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
1987 switch (kt->type)
1988 {
1989 case T_IF:
1990 case T_IFDEF:
1991 case T_IFNDEF:
1992 temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
1993 temp->next = pfile->if_stack;
1994 pfile->if_stack = temp;
1995 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
1996 temp->type = kt->type;
1997 return 0;
1998
1999 case T_ELSE:
2000 if (pfile->if_stack != stack)
2001 validate_else (pfile, "#else");
2002 /* fall through */
2003 case T_ELIF:
2004 if (pfile->if_stack == stack)
2005 return 1;
2006 else
2007 {
2008 pfile->if_stack->type = kt->type;
2009 return 0;
2010 }
2011
2012 case T_ENDIF:
2013 if (pfile->if_stack != stack)
2014 validate_else (pfile, "#endif");
2015
2016 if (pfile->if_stack == stack)
2017 return 1;
2018
2019 temp = pfile->if_stack;
2020 pfile->if_stack = temp->next;
2021 free (temp);
2022 return 0;
2023
2024 default:
2025 return 0;
2026 }
2027
2028 /* Don't let erroneous code go by. */
2029 if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
2030 cpp_pedwarn (pfile, "invalid preprocessor directive name");
2031 return 0;
2032 }
2033
2034 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
2035 * leaves input ptr at the sharp sign found.
2036 */
2037 static void
2038 skip_if_group (pfile)
2039 cpp_reader *pfile;
2040 {
2041 int c;
2042 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
2043 U_CHAR *beg_of_line;
2044 long old_written;
2045
2046 if (CPP_OPTIONS (pfile)->output_conditionals)
2047 {
2048 CPP_PUTS (pfile, "#failed\n", 8);
2049 pfile->lineno++;
2050 output_line_command (pfile, same_file);
2051 }
2052
2053 old_written = CPP_WRITTEN (pfile);
2054
2055 for (;;)
2056 {
2057 beg_of_line = CPP_BUFFER (pfile)->cur;
2058
2059 if (! CPP_TRADITIONAL (pfile))
2060 cpp_skip_hspace (pfile);
2061 c = GETC();
2062 if (c == '\n')
2063 {
2064 if (CPP_OPTIONS (pfile)->output_conditionals)
2065 CPP_PUTC (pfile, c);
2066 CPP_BUMP_LINE (pfile);
2067 continue;
2068 }
2069 else if (c == '#')
2070 {
2071 if (consider_directive_while_skipping (pfile, save_if_stack))
2072 break;
2073 }
2074 else if (c == EOF)
2075 return; /* Caller will issue error. */
2076
2077 FORWARD(-1);
2078 if (CPP_OPTIONS (pfile)->output_conditionals)
2079 {
2080 CPP_PUTS (pfile, beg_of_line, CPP_BUFFER (pfile)->cur - beg_of_line);
2081 copy_rest_of_line (pfile);
2082 }
2083 else
2084 {
2085 copy_rest_of_line (pfile);
2086 CPP_SET_WRITTEN (pfile, old_written); /* discard it */
2087 }
2088
2089 c = GETC();
2090 if (c == EOF)
2091 return; /* Caller will issue error. */
2092 else
2093 {
2094 /* \n */
2095 if (CPP_OPTIONS (pfile)->output_conditionals)
2096 {
2097 CPP_PUTC (pfile, c);
2098 pfile->lineno++;
2099 }
2100 CPP_BUMP_LINE (pfile);
2101 }
2102 }
2103
2104 /* Back up to the beginning of this line. Caller will process the
2105 directive. */
2106 CPP_BUFFER (pfile)->cur = beg_of_line;
2107 pfile->only_seen_white = 1;
2108 if (CPP_OPTIONS (pfile)->output_conditionals)
2109 {
2110 CPP_PUTS (pfile, "#endfailed\n", 11);
2111 pfile->lineno++;
2112 }
2113 }
2114
2115 /*
2116 * handle a #else directive. Do this by just continuing processing
2117 * without changing if_stack ; this is so that the error message
2118 * for missing #endif's etc. will point to the original #if. It
2119 * is possible that something different would be better.
2120 */
2121
2122 static int
2123 do_else (pfile, keyword)
2124 cpp_reader *pfile;
2125 const struct directive *keyword ATTRIBUTE_UNUSED;
2126 {
2127 validate_else (pfile, "#else");
2128 skip_rest_of_line (pfile);
2129
2130 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2131 {
2132 cpp_error (pfile, "`#else' not within a conditional");
2133 return 0;
2134 }
2135 else
2136 {
2137 /* #ifndef can't have its special treatment for containing the whole file
2138 if it has a #else clause. */
2139 pfile->if_stack->control_macro = 0;
2140
2141 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
2142 {
2143 cpp_error (pfile, "`#else' after `#else'");
2144 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
2145 "the conditional began here");
2146 }
2147 pfile->if_stack->type = T_ELSE;
2148 }
2149
2150 if (pfile->if_stack->if_succeeded)
2151 skip_if_group (pfile);
2152 else
2153 {
2154 ++pfile->if_stack->if_succeeded; /* continue processing input */
2155 output_line_command (pfile, same_file);
2156 }
2157 return 0;
2158 }
2159
2160 /*
2161 * unstack after #endif command
2162 */
2163
2164 static int
2165 do_endif (pfile, keyword)
2166 cpp_reader *pfile;
2167 const struct directive *keyword ATTRIBUTE_UNUSED;
2168 {
2169 validate_else (pfile, "#endif");
2170 skip_rest_of_line (pfile);
2171
2172 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2173 cpp_error (pfile, "`#endif' not within a conditional");
2174 else
2175 {
2176 IF_STACK_FRAME *temp = pfile->if_stack;
2177 pfile->if_stack = temp->next;
2178 if (temp->control_macro != 0)
2179 {
2180 /* This #endif matched a #ifndef at the start of the file.
2181 See if it is at the end of the file. */
2182 int c;
2183
2184 parse_set_mark (pfile);
2185
2186 for (;;)
2187 {
2188 cpp_skip_hspace (pfile);
2189 c = GETC ();
2190 if (c != '\n')
2191 break;
2192 }
2193 parse_goto_mark (pfile);
2194
2195 if (c == EOF)
2196 {
2197 /* This #endif ends a #ifndef
2198 that contains all of the file (aside from whitespace).
2199 Arrange not to include the file again
2200 if the macro that was tested is defined. */
2201 struct cpp_buffer *ip;
2202 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2203 if (ip->fname != NULL)
2204 break;
2205 ip->ihash->control_macro = (char *) temp->control_macro;
2206 }
2207 }
2208 free (temp);
2209 output_line_command (pfile, same_file);
2210 }
2211 return 0;
2212 }
2213
2214 /* Issue -pedantic warning for text which is not a comment following
2215 an #else or #endif. Do not warn in system headers, as this is harmless
2216 and very common on old systems. */
2217
2218 static void
2219 validate_else (pfile, directive)
2220 cpp_reader *pfile;
2221 const char *directive;
2222 {
2223 if (! CPP_PEDANTIC (pfile) || CPP_BUFFER (pfile)->system_header_p)
2224 return;
2225
2226 cpp_skip_hspace (pfile);
2227 if (PEEKC () != '\n')
2228 cpp_pedwarn (pfile,
2229 "text following `%s' violates ANSI standard", directive);
2230 }
2231
2232 /* Convert T_IF, etc. to a string. Used in error messages. */
2233 static const char *
2234 if_directive_name (pfile, ifs)
2235 cpp_reader *pfile;
2236 struct if_stack *ifs;
2237 {
2238 switch (ifs->type)
2239 {
2240 case T_IF: return "#if";
2241 case T_IFDEF: return "#ifdef";
2242 case T_IFNDEF: return "#ifndef";
2243 case T_ELIF: return "#elif";
2244 case T_ELSE: return "#else";
2245 default:
2246 cpp_ice (pfile, "impossible if_stack->type value %d", ifs->type);
2247 return "unknown";
2248 }
2249 }
2250
2251 /* Get the next token, and add it to the text in pfile->token_buffer.
2252 Return the kind of token we got. */
2253
2254 enum cpp_token
2255 cpp_get_token (pfile)
2256 cpp_reader *pfile;
2257 {
2258 register int c, c2, c3;
2259 enum cpp_token token;
2260 struct cpp_options *opts = CPP_OPTIONS (pfile);
2261
2262 get_next:
2263 c = GETC();
2264 if (c == EOF)
2265 {
2266 if (CPP_BUFFER (pfile)->manual_pop)
2267 /* If we've been reading from redirected input, the
2268 frontend will pop the buffer. */
2269 return CPP_EOF;
2270 else if (CPP_BUFFER (pfile)->seen_eof)
2271 {
2272 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile))
2273 return CPP_EOF;
2274
2275 cpp_pop_buffer (pfile);
2276 goto get_next;
2277 }
2278 else
2279 {
2280 cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2281 struct if_stack *ifs, *nifs;
2282
2283 /* Unwind the conditional stack and generate error messages. */
2284 for (ifs = pfile->if_stack;
2285 ifs != CPP_BUFFER (pfile)->if_stack;
2286 ifs = nifs)
2287 {
2288 cpp_error_with_line (pfile, ifs->lineno, -1,
2289 "unterminated `%s' conditional",
2290 if_directive_name (pfile, ifs));
2291
2292 nifs = ifs->next;
2293 free (ifs);
2294 }
2295 pfile->if_stack = ifs;
2296
2297 if (CPP_BUFFER (pfile)->nominal_fname
2298 && next_buf != CPP_NULL_BUFFER (pfile))
2299 {
2300 /* We're about to return from an #include file.
2301 Emit #line information now (as part of the CPP_POP) result.
2302 But the #line refers to the file we will pop to. */
2303 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2304 CPP_BUFFER (pfile) = next_buf;
2305 pfile->input_stack_listing_current = 0;
2306 output_line_command (pfile, leave_file);
2307 CPP_BUFFER (pfile) = cur_buffer;
2308 }
2309
2310 CPP_BUFFER (pfile)->seen_eof = 1;
2311 return CPP_POP;
2312 }
2313 }
2314 else
2315 {
2316 switch (c)
2317 {
2318 case '/':
2319 if (PEEKC () == '=')
2320 goto op2;
2321
2322 comment:
2323 if (opts->put_out_comments)
2324 c = copy_comment (pfile, c);
2325 else
2326 c = skip_comment (pfile, c);
2327 if (c != ' ')
2328 goto randomchar;
2329
2330 /* Comments are equivalent to spaces.
2331 For -traditional, a comment is equivalent to nothing. */
2332 if (opts->traditional || opts->put_out_comments)
2333 return CPP_COMMENT;
2334 else
2335 {
2336 CPP_PUTC (pfile, c);
2337 return CPP_HSPACE;
2338 }
2339
2340 case '#':
2341 if (!pfile->only_seen_white)
2342 goto randomchar;
2343 /* -traditional directives are recognized only with the # in
2344 column 1.
2345 XXX Layering violation. */
2346 if (CPP_TRADITIONAL (pfile)
2347 && CPP_BUFFER (pfile)->cur - CPP_BUFFER (pfile)->line_base != 1)
2348 goto randomchar;
2349 if (handle_directive (pfile))
2350 return CPP_DIRECTIVE;
2351 pfile->only_seen_white = 0;
2352 goto randomchar;
2353
2354 case '\"':
2355 case '\'':
2356 string:
2357 parse_string (pfile, c);
2358 pfile->only_seen_white = 0;
2359 return c == '\'' ? CPP_CHAR : CPP_STRING;
2360
2361 case '$':
2362 if (!opts->dollars_in_ident)
2363 goto randomchar;
2364 goto letter;
2365
2366 case ':':
2367 if (opts->cplusplus && PEEKC () == ':')
2368 goto op2;
2369 goto randomchar;
2370
2371 case '&':
2372 case '+':
2373 case '|':
2374 c2 = PEEKC ();
2375 if (c2 == c || c2 == '=')
2376 goto op2;
2377 goto randomchar;
2378
2379 case '*':
2380 case '!':
2381 case '%':
2382 case '=':
2383 case '^':
2384 if (PEEKC () == '=')
2385 goto op2;
2386 goto randomchar;
2387
2388 case '-':
2389 c2 = PEEKC ();
2390 if (c2 == '-' && opts->chill)
2391 goto comment; /* Chill style comment */
2392 if (c2 == '-' || c2 == '=')
2393 goto op2;
2394 if (c2 == '>')
2395 {
2396 if (opts->cplusplus && PEEKN (1) == '*')
2397 {
2398 /* In C++, there's a ->* operator. */
2399 token = CPP_OTHER;
2400 pfile->only_seen_white = 0;
2401 CPP_RESERVE (pfile, 4);
2402 CPP_PUTC_Q (pfile, c);
2403 CPP_PUTC_Q (pfile, GETC ());
2404 CPP_PUTC_Q (pfile, GETC ());
2405 CPP_NUL_TERMINATE_Q (pfile);
2406 return token;
2407 }
2408 goto op2;
2409 }
2410 goto randomchar;
2411
2412 case '<':
2413 if (pfile->parsing_include_directive)
2414 {
2415 for (;;)
2416 {
2417 CPP_PUTC (pfile, c);
2418 if (c == '>')
2419 break;
2420 c = GETC ();
2421 if (c == '\n' || c == EOF)
2422 {
2423 cpp_error (pfile,
2424 "missing '>' in `#include <FILENAME>'");
2425 break;
2426 }
2427 else if (c == '\r')
2428 {
2429 if (!CPP_BUFFER (pfile)->has_escapes)
2430 {
2431 /* Backslash newline is replaced by nothing. */
2432 CPP_ADJUST_WRITTEN (pfile, -1);
2433 CPP_BUMP_LINE (pfile);
2434 }
2435 else
2436 {
2437 /* We might conceivably get \r- or \r<space> in
2438 here. Just delete 'em. */
2439 int d = GETC();
2440 if (d != '-' && d != ' ')
2441 cpp_ice (pfile, "unrecognized escape \\r%c", d);
2442 CPP_ADJUST_WRITTEN (pfile, -1);
2443 }
2444 }
2445 }
2446 return CPP_STRING;
2447 }
2448 /* else fall through */
2449 case '>':
2450 c2 = PEEKC ();
2451 if (c2 == '=')
2452 goto op2;
2453 /* GNU C++ supports MIN and MAX operators <? and >?. */
2454 if (c2 != c && (!opts->cplusplus || c2 != '?'))
2455 goto randomchar;
2456 FORWARD(1);
2457 CPP_RESERVE (pfile, 4);
2458 CPP_PUTC (pfile, c);
2459 CPP_PUTC (pfile, c2);
2460 c3 = PEEKC ();
2461 if (c3 == '=')
2462 CPP_PUTC_Q (pfile, GETC ());
2463 CPP_NUL_TERMINATE_Q (pfile);
2464 pfile->only_seen_white = 0;
2465 return CPP_OTHER;
2466
2467 case '.':
2468 c2 = PEEKC ();
2469 if (ISDIGIT(c2))
2470 {
2471 CPP_RESERVE(pfile, 2);
2472 CPP_PUTC_Q (pfile, '.');
2473 c = GETC ();
2474 goto number;
2475 }
2476
2477 /* In C++ there's a .* operator. */
2478 if (opts->cplusplus && c2 == '*')
2479 goto op2;
2480
2481 if (c2 == '.' && PEEKN(1) == '.')
2482 {
2483 CPP_RESERVE(pfile, 4);
2484 CPP_PUTC_Q (pfile, '.');
2485 CPP_PUTC_Q (pfile, '.');
2486 CPP_PUTC_Q (pfile, '.');
2487 FORWARD (2);
2488 CPP_NUL_TERMINATE_Q (pfile);
2489 pfile->only_seen_white = 0;
2490 return CPP_3DOTS;
2491 }
2492 goto randomchar;
2493
2494 op2:
2495 token = CPP_OTHER;
2496 pfile->only_seen_white = 0;
2497 CPP_RESERVE(pfile, 3);
2498 CPP_PUTC_Q (pfile, c);
2499 CPP_PUTC_Q (pfile, GETC ());
2500 CPP_NUL_TERMINATE_Q (pfile);
2501 return token;
2502
2503 case 'L':
2504 c2 = PEEKC ();
2505 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2506 {
2507 CPP_PUTC (pfile, c);
2508 c = GETC ();
2509 goto string;
2510 }
2511 goto letter;
2512
2513 case '0': case '1': case '2': case '3': case '4':
2514 case '5': case '6': case '7': case '8': case '9':
2515 number:
2516 c2 = '.';
2517 for (;;)
2518 {
2519 CPP_RESERVE (pfile, 2);
2520 CPP_PUTC_Q (pfile, c);
2521 c = PEEKC ();
2522 if (c == EOF)
2523 break;
2524 if (!is_idchar(c) && c != '.'
2525 && ((c2 != 'e' && c2 != 'E'
2526 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
2527 || (c != '+' && c != '-')))
2528 break;
2529 FORWARD(1);
2530 c2= c;
2531 }
2532 CPP_NUL_TERMINATE_Q (pfile);
2533 pfile->only_seen_white = 0;
2534 return CPP_NUMBER;
2535 case 'b': case 'c': case 'd': case 'h': case 'o':
2536 case 'B': case 'C': case 'D': case 'H': case 'O':
2537 if (opts->chill && PEEKC () == '\'')
2538 {
2539 pfile->only_seen_white = 0;
2540 CPP_RESERVE (pfile, 2);
2541 CPP_PUTC_Q (pfile, c);
2542 CPP_PUTC_Q (pfile, '\'');
2543 FORWARD(1);
2544 for (;;)
2545 {
2546 c = GETC();
2547 if (c == EOF)
2548 goto chill_number_eof;
2549 if (!is_idchar(c))
2550 break;
2551 CPP_PUTC (pfile, c);
2552 }
2553 if (c == '\'')
2554 {
2555 CPP_RESERVE (pfile, 2);
2556 CPP_PUTC_Q (pfile, c);
2557 CPP_NUL_TERMINATE_Q (pfile);
2558 return CPP_STRING;
2559 }
2560 else
2561 {
2562 FORWARD(-1);
2563 chill_number_eof:
2564 CPP_NUL_TERMINATE (pfile);
2565 return CPP_NUMBER;
2566 }
2567 }
2568 else
2569 goto letter;
2570 case '_':
2571 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2572 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2573 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2574 case 'x': case 'y': case 'z':
2575 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2576 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2577 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2578 case 'Y': case 'Z':
2579 letter:
2580 {
2581 HASHNODE *hp;
2582 unsigned char *ident;
2583 int before_name_written = CPP_WRITTEN (pfile);
2584 int ident_len;
2585 parse_name (pfile, c);
2586 pfile->only_seen_white = 0;
2587 if (pfile->no_macro_expand)
2588 return CPP_NAME;
2589 ident = pfile->token_buffer + before_name_written;
2590 ident_len = CPP_PWRITTEN (pfile) - ident;
2591 hp = cpp_lookup (pfile, ident, ident_len, -1);
2592 if (!hp)
2593 return CPP_NAME;
2594 if (hp->type == T_DISABLED)
2595 {
2596 if (pfile->output_escapes)
2597 { /* Return "\r-IDENT", followed by '\0'. */
2598 int i;
2599 CPP_RESERVE (pfile, 3);
2600 ident = pfile->token_buffer + before_name_written;
2601 CPP_ADJUST_WRITTEN (pfile, 2);
2602 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2603 ident[0] = '\r';
2604 ident[1] = '-';
2605 }
2606 return CPP_NAME;
2607 }
2608
2609 /* If macro wants an arglist, verify that a '(' follows.
2610 first skip all whitespace, copying it to the output
2611 after the macro name. Then, if there is no '(',
2612 decide this is not a macro call and leave things that way. */
2613 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2614 {
2615 int is_macro_call, macbuf_whitespace = 0;
2616
2617 parse_set_mark (pfile);
2618 for (;;)
2619 {
2620 cpp_skip_hspace (pfile);
2621 c = PEEKC ();
2622 is_macro_call = c == '(';
2623 if (c != EOF)
2624 {
2625 if (c != '\n')
2626 break;
2627 FORWARD (1);
2628 }
2629 else
2630 {
2631 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2632 {
2633 if (CPP_BUFFER (pfile)->mark !=
2634 (CPP_BUFFER (pfile)->cur
2635 - CPP_BUFFER (pfile)->buf))
2636 macbuf_whitespace = 1;
2637
2638 /* The mark goes away automatically when
2639 the buffer is popped. */
2640 cpp_pop_buffer (pfile);
2641 parse_set_mark (pfile);
2642 }
2643 else
2644 break;
2645 }
2646 }
2647 if (!is_macro_call)
2648 {
2649 parse_goto_mark (pfile);
2650 if (macbuf_whitespace)
2651 CPP_PUTC (pfile, ' ');
2652 }
2653 else
2654 parse_clear_mark (pfile);
2655 if (!is_macro_call)
2656 return CPP_NAME;
2657 }
2658 /* This is now known to be a macro call.
2659 Expand the macro, reading arguments as needed,
2660 and push the expansion on the input stack. */
2661 macroexpand (pfile, hp);
2662 CPP_SET_WRITTEN (pfile, before_name_written);
2663 }
2664 goto get_next;
2665
2666 case ' ': case '\t': case '\v':
2667 for (;;)
2668 {
2669 CPP_PUTC (pfile, c);
2670 c = PEEKC ();
2671 if (c == EOF || !is_hspace(c))
2672 break;
2673 FORWARD(1);
2674 }
2675 return CPP_HSPACE;
2676
2677 case '\r':
2678 if (CPP_BUFFER (pfile)->has_escapes)
2679 {
2680 c = GETC ();
2681 if (c == '-')
2682 {
2683 if (pfile->output_escapes)
2684 CPP_PUTS (pfile, "\r-", 2);
2685 parse_name (pfile, GETC ());
2686 return CPP_NAME;
2687 }
2688 else if (c == ' ')
2689 {
2690 CPP_RESERVE (pfile, 2);
2691 if (pfile->output_escapes)
2692 CPP_PUTC_Q (pfile, '\r');
2693 CPP_PUTC_Q (pfile, c);
2694 return CPP_HSPACE;
2695 }
2696 else
2697 {
2698 cpp_ice (pfile, "unrecognized escape \\r%c", c);
2699 goto get_next;
2700 }
2701 }
2702 else
2703 {
2704 /* Backslash newline is ignored. */
2705 CPP_BUMP_LINE (pfile);
2706 goto get_next;
2707 }
2708
2709 case '\n':
2710 CPP_PUTC (pfile, c);
2711 if (pfile->only_seen_white == 0)
2712 pfile->only_seen_white = 1;
2713 CPP_BUMP_LINE (pfile);
2714 if (! CPP_OPTIONS (pfile)->no_line_commands)
2715 {
2716 pfile->lineno++;
2717 if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2718 output_line_command (pfile, same_file);
2719 }
2720 return CPP_VSPACE;
2721
2722 case '(': token = CPP_LPAREN; goto char1;
2723 case ')': token = CPP_RPAREN; goto char1;
2724 case '{': token = CPP_LBRACE; goto char1;
2725 case '}': token = CPP_RBRACE; goto char1;
2726 case ',': token = CPP_COMMA; goto char1;
2727 case ';': token = CPP_SEMICOLON; goto char1;
2728
2729 randomchar:
2730 default:
2731 token = CPP_OTHER;
2732 char1:
2733 pfile->only_seen_white = 0;
2734 CPP_PUTC (pfile, c);
2735 return token;
2736 }
2737 }
2738 }
2739
2740 /* Like cpp_get_token, but skip spaces and comments. */
2741
2742 enum cpp_token
2743 cpp_get_non_space_token (pfile)
2744 cpp_reader *pfile;
2745 {
2746 int old_written = CPP_WRITTEN (pfile);
2747 for (;;)
2748 {
2749 enum cpp_token token = cpp_get_token (pfile);
2750 if (token != CPP_COMMENT && token != CPP_POP
2751 && token != CPP_HSPACE && token != CPP_VSPACE)
2752 return token;
2753 CPP_SET_WRITTEN (pfile, old_written);
2754 }
2755 }
2756
2757 /* Parse an identifier starting with C. */
2758
2759 static void
2760 parse_name (pfile, c)
2761 cpp_reader *pfile;
2762 int c;
2763 {
2764 for (;;)
2765 {
2766 if (! is_idchar(c))
2767 {
2768 FORWARD (-1);
2769 break;
2770 }
2771
2772 if (c == '$' && CPP_PEDANTIC (pfile))
2773 cpp_pedwarn (pfile, "`$' in identifier");
2774
2775 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
2776 CPP_PUTC_Q (pfile, c);
2777 c = GETC();
2778 if (c == EOF)
2779 break;
2780 }
2781 CPP_NUL_TERMINATE_Q (pfile);
2782 return;
2783 }
2784
2785 /* Parse a string starting with C. A single quoted string is treated
2786 like a double -- some programs (e.g., troff) are perverse this way.
2787 (However, a single quoted string is not allowed to extend over
2788 multiple lines.) */
2789 static void
2790 parse_string (pfile, c)
2791 cpp_reader *pfile;
2792 int c;
2793 {
2794 long start_line, start_column;
2795
2796 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2797
2798 CPP_PUTC (pfile, c);
2799 while (1)
2800 {
2801 int cc = GETC();
2802 if (cc == EOF)
2803 {
2804 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2805 {
2806 /* try harder: this string crosses a macro expansion
2807 boundary. This can happen naturally if -traditional.
2808 Otherwise, only -D can make a macro with an unmatched
2809 quote. */
2810 cpp_pop_buffer (pfile);
2811 continue;
2812 }
2813
2814 cpp_error_with_line (pfile, start_line, start_column,
2815 "unterminated string or character constant");
2816 if (pfile->multiline_string_line != start_line
2817 && pfile->multiline_string_line != 0)
2818 cpp_error_with_line (pfile,
2819 pfile->multiline_string_line, -1,
2820 "possible real start of unterminated constant");
2821 pfile->multiline_string_line = 0;
2822 break;
2823 }
2824 CPP_PUTC (pfile, cc);
2825 switch (cc)
2826 {
2827 case '\n':
2828 CPP_BUMP_LINE (pfile);
2829 pfile->lineno++;
2830
2831 /* In Fortran and assembly language, silently terminate
2832 strings of either variety at end of line. This is a
2833 kludge around not knowing where comments are in these
2834 languages. */
2835 if (CPP_OPTIONS (pfile)->lang_fortran
2836 || CPP_OPTIONS (pfile)->lang_asm)
2837 return;
2838 /* Character constants may not extend over multiple lines.
2839 In Standard C, neither may strings. We accept multiline
2840 strings as an extension. */
2841 if (c == '\'')
2842 {
2843 cpp_error_with_line (pfile, start_line, start_column,
2844 "unterminated character constant");
2845 return;
2846 }
2847 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2848 cpp_pedwarn_with_line (pfile, start_line, start_column,
2849 "string constant runs past end of line");
2850 if (pfile->multiline_string_line == 0)
2851 pfile->multiline_string_line = start_line;
2852 break;
2853
2854 case '\r':
2855 CPP_ADJUST_WRITTEN (pfile, -1);
2856 if (CPP_BUFFER (pfile)->has_escapes)
2857 {
2858 cpp_ice (pfile, "\\r escape inside string constant");
2859 FORWARD(1);
2860 }
2861 else
2862 /* Backslash newline is replaced by nothing at all. */
2863 CPP_BUMP_LINE (pfile);
2864 break;
2865
2866 case '\\':
2867 cc = GETC();
2868 if (cc != EOF)
2869 CPP_PUTC (pfile, cc);
2870 break;
2871
2872 case '\"':
2873 case '\'':
2874 if (cc == c)
2875 return;
2876 break;
2877 }
2878 }
2879 }
2880
2881 /* Read an assertion into the token buffer, converting to
2882 canonical form: `#predicate(a n swe r)' The next non-whitespace
2883 character to read should be the first letter of the predicate.
2884 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2885 with answer (see callers for why). In case of 0, an error has been
2886 printed. */
2887 static int
2888 parse_assertion (pfile)
2889 cpp_reader *pfile;
2890 {
2891 int c, dropwhite;
2892 cpp_skip_hspace (pfile);
2893 c = PEEKC();
2894 if (! is_idstart(c))
2895 {
2896 cpp_error (pfile, "assertion predicate is not an identifier");
2897 return 0;
2898 }
2899 CPP_PUTC(pfile, '#');
2900 FORWARD(1);
2901 parse_name(pfile, c);
2902
2903 c = PEEKC();
2904 if (c != '(')
2905 {
2906 if (is_hspace(c) || c == '\r')
2907 cpp_skip_hspace (pfile);
2908 c = PEEKC();
2909 }
2910 if (c != '(')
2911 return 1;
2912
2913 CPP_PUTC(pfile, '(');
2914 FORWARD(1);
2915 dropwhite = 1;
2916 while ((c = GETC()) != ')')
2917 {
2918 if (is_space(c))
2919 {
2920 if (! dropwhite)
2921 {
2922 CPP_PUTC(pfile, ' ');
2923 dropwhite = 1;
2924 }
2925 }
2926 else if (c == '\n' || c == EOF)
2927 {
2928 if (c == '\n') FORWARD(-1);
2929 cpp_error (pfile, "un-terminated assertion answer");
2930 return 0;
2931 }
2932 else if (c == '\r')
2933 /* \r cannot be a macro escape here. */
2934 CPP_BUMP_LINE (pfile);
2935 else
2936 {
2937 CPP_PUTC (pfile, c);
2938 dropwhite = 0;
2939 }
2940 }
2941
2942 if (pfile->limit[-1] == ' ')
2943 pfile->limit[-1] = ')';
2944 else if (pfile->limit[-1] == '(')
2945 {
2946 cpp_error (pfile, "empty token sequence in assertion");
2947 return 0;
2948 }
2949 else
2950 CPP_PUTC (pfile, ')');
2951
2952 CPP_NUL_TERMINATE (pfile);
2953 return 2;
2954 }
2955
2956 static int
2957 do_assert (pfile, keyword)
2958 cpp_reader *pfile;
2959 const struct directive *keyword ATTRIBUTE_UNUSED;
2960 {
2961 char *sym;
2962 int ret, c;
2963 HASHNODE *base, *this;
2964 int baselen, thislen;
2965
2966 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
2967 && !CPP_BUFFER (pfile)->system_header_p)
2968 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
2969
2970 cpp_skip_hspace (pfile);
2971 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
2972 ret = parse_assertion (pfile);
2973 if (ret == 0)
2974 goto error;
2975 else if (ret == 1)
2976 {
2977 cpp_error (pfile, "missing token-sequence in `#assert'");
2978 goto error;
2979 }
2980
2981 cpp_skip_hspace (pfile);
2982 c = PEEKC();
2983 if (c != EOF && c != '\n')
2984 {
2985 cpp_error (pfile, "junk at end of `#assert'");
2986 goto error;
2987 }
2988
2989 thislen = strlen (sym);
2990 baselen = index (sym, '(') - sym;
2991 this = cpp_lookup (pfile, sym, thislen, -1);
2992 if (this)
2993 {
2994 cpp_warning (pfile, "`%s' re-asserted", sym);
2995 goto error;
2996 }
2997
2998 base = cpp_lookup (pfile, sym, baselen, -1);
2999 if (! base)
3000 base = cpp_install (pfile, sym, baselen, T_ASSERT, 0, -1);
3001 else if (base->type != T_ASSERT)
3002 {
3003 /* Token clash - but with what?! */
3004 cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
3005 goto error;
3006 }
3007
3008 this = cpp_install (pfile, sym, thislen, T_ASSERT,
3009 (char *)base->value.aschain, -1);
3010 base->value.aschain = this;
3011
3012 pfile->limit = (unsigned char *) sym; /* Pop */
3013 return 0;
3014
3015 error:
3016 skip_rest_of_line (pfile);
3017 pfile->limit = (unsigned char *) sym; /* Pop */
3018 return 0;
3019 }
3020
3021 static int
3022 do_unassert (pfile, keyword)
3023 cpp_reader *pfile;
3024 const struct directive *keyword ATTRIBUTE_UNUSED;
3025 {
3026 int c, ret;
3027 char *sym;
3028 long baselen, thislen;
3029 HASHNODE *base, *this, *next;
3030
3031 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3032 && !CPP_BUFFER (pfile)->system_header_p)
3033 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
3034
3035 cpp_skip_hspace (pfile);
3036
3037 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
3038 ret = parse_assertion (pfile);
3039 if (ret == 0)
3040 goto error;
3041
3042 cpp_skip_hspace (pfile);
3043 c = PEEKC ();
3044 if (c != EOF && c != '\n')
3045 cpp_error (pfile, "junk at end of `#unassert'");
3046
3047 thislen = strlen (sym);
3048 if (ret == 1)
3049 {
3050 base = cpp_lookup (pfile, sym, thislen, -1);
3051 if (! base)
3052 goto error; /* It isn't an error to #undef what isn't #defined,
3053 so it isn't an error to #unassert what isn't
3054 #asserted either. */
3055
3056 for (this = base->value.aschain; this; this = next)
3057 {
3058 next = this->value.aschain;
3059 delete_macro (this);
3060 }
3061 delete_macro (base);
3062 }
3063 else
3064 {
3065 baselen = index (sym, '(') - sym;
3066 base = cpp_lookup (pfile, sym, baselen, -1);
3067 if (! base) goto error;
3068 this = cpp_lookup (pfile, sym, thislen, -1);
3069 if (! this) goto error;
3070
3071 next = base;
3072 while (next->value.aschain != this)
3073 next = next->value.aschain;
3074
3075 next->value.aschain = this->value.aschain;
3076 delete_macro (this);
3077
3078 if (base->value.aschain == NULL)
3079 delete_macro (base); /* Last answer for this predicate deleted. */
3080 }
3081
3082 pfile->limit = (unsigned char *) sym; /* Pop */
3083 return 0;
3084 error:
3085 skip_rest_of_line (pfile);
3086 pfile->limit = (unsigned char *) sym; /* Pop */
3087 return 0;
3088 }
3089
3090 /* Process STR as if it appeared as the body of an #unassert. */
3091 void
3092 cpp_unassert (pfile, str)
3093 cpp_reader *pfile;
3094 unsigned char *str;
3095 {
3096 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
3097 {
3098 do_assert (pfile, NULL);
3099 cpp_pop_buffer (pfile);
3100 }
3101 }
3102
3103 int
3104 cpp_read_check_assertion (pfile)
3105 cpp_reader *pfile;
3106 {
3107 U_CHAR *name = CPP_PWRITTEN (pfile);
3108 int result;
3109 HASHNODE *hp;
3110
3111 FORWARD (1); /* Skip '#' */
3112 cpp_skip_hspace (pfile);
3113 if (! parse_assertion (pfile))
3114 result = 0;
3115 else
3116 {
3117 hp = cpp_lookup (pfile, name, CPP_PWRITTEN (pfile) - name, -1);
3118 result = (hp != 0);
3119 }
3120
3121 pfile->limit = name;
3122 return result;
3123 }
3124
3125 /* Remember the current position of PFILE. */
3126
3127 void
3128 parse_set_mark (pfile)
3129 cpp_reader *pfile;
3130 {
3131 cpp_buffer *ip = CPP_BUFFER (pfile);
3132 if (ip->mark != -1)
3133 cpp_ice (pfile, "ip->mark != -1 in parse_set_mark");
3134
3135 ip->mark = ip->cur - ip->buf;
3136 }
3137
3138 /* Clear the current mark - we no longer need it. */
3139
3140 void
3141 parse_clear_mark (pfile)
3142 cpp_reader *pfile;
3143 {
3144 cpp_buffer *ip = CPP_BUFFER (pfile);
3145 if (ip->mark == -1)
3146 cpp_ice (pfile, "ip->mark == -1 in parse_clear_mark");
3147
3148 ip->mark = -1;
3149 }
3150
3151 /* Backup the current position of PFILE to that saved in its mark,
3152 and clear the mark. */
3153
3154 void
3155 parse_goto_mark (pfile)
3156 cpp_reader *pfile;
3157 {
3158 cpp_buffer *ip = CPP_BUFFER (pfile);
3159 if (ip->mark == -1)
3160 cpp_ice (pfile, "ip->mark == -1 in parse_goto_mark");
3161
3162 ip->cur = ip->buf + ip->mark;
3163 ip->mark = -1;
3164 }