* decl.c (initialize_local_var): Handle static variables here.
[official-gcc.git] / gcc / cpplib.c
blob80f02de69b1b37fd4e228abf5cba60722de26554
1 /* CPP Library.
2 Copyright (C) 1986, 87, 89, 92-98, 1999 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
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.
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.
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. */
21 #include "config.h"
22 #include "system.h"
24 #include "cpplib.h"
25 #include "cpphash.h"
26 #include "intl.h"
28 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
29 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
31 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
32 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
33 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
34 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
35 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
36 (Note that it is false while we're expanding macro *arguments*.) */
37 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->data != NULL)
39 /* Forward declarations. */
41 static const char *my_strerror PROTO ((int));
42 static void validate_else PROTO ((cpp_reader *, const char *));
43 static HOST_WIDEST_INT eval_if_expression PROTO ((cpp_reader *));
45 static void conditional_skip PROTO ((cpp_reader *, int,
46 enum node_type, U_CHAR *));
47 static void skip_if_group PROTO ((cpp_reader *));
49 static void parse_name PARAMS ((cpp_reader *, int));
50 static void parse_string PARAMS ((cpp_reader *, int));
51 static int parse_assertion PARAMS ((cpp_reader *));
53 /* External declarations. */
55 extern HOST_WIDEST_INT cpp_parse_expr PARAMS ((cpp_reader *));
57 /* `struct directive' defines one #-directive, including how to handle it. */
59 struct directive {
60 int length; /* Length of name */
61 int (*func) /* Function to handle directive */
62 PARAMS ((cpp_reader *, const struct directive *));
63 const char *name; /* Name of directive */
64 enum node_type type; /* Code which describes which directive. */
67 /* These functions are declared to return int instead of void since they
68 are going to be placed in a table and some old compilers have trouble with
69 pointers to functions returning void. */
71 static int do_define PARAMS ((cpp_reader *, const struct directive *));
72 static int do_line PARAMS ((cpp_reader *, const struct directive *));
73 static int do_include PARAMS ((cpp_reader *, const struct directive *));
74 static int do_undef PARAMS ((cpp_reader *, const struct directive *));
75 static int do_error PARAMS ((cpp_reader *, const struct directive *));
76 static int do_pragma PARAMS ((cpp_reader *, const struct directive *));
77 static int do_ident PARAMS ((cpp_reader *, const struct directive *));
78 static int do_if PARAMS ((cpp_reader *, const struct directive *));
79 static int do_xifdef PARAMS ((cpp_reader *, const struct directive *));
80 static int do_else PARAMS ((cpp_reader *, const struct directive *));
81 static int do_elif PARAMS ((cpp_reader *, const struct directive *));
82 static int do_endif PARAMS ((cpp_reader *, const struct directive *));
83 #ifdef SCCS_DIRECTIVE
84 static int do_sccs PARAMS ((cpp_reader *, const struct directive *));
85 #endif
86 static int do_assert PARAMS ((cpp_reader *, const struct directive *));
87 static int do_unassert PARAMS ((cpp_reader *, const struct directive *));
88 static int do_warning PARAMS ((cpp_reader *, const struct directive *));
89 static enum cpp_token null_underflow PARAMS ((cpp_reader *));
90 static int null_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
91 static int skip_comment PARAMS ((cpp_reader *, int));
92 static int copy_comment PARAMS ((cpp_reader *, int));
93 static void copy_rest_of_line PARAMS ((cpp_reader *));
94 static int handle_directive PARAMS ((cpp_reader *));
95 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t, cpp_reader *,
96 const struct directive *));
97 static enum cpp_token get_directive_token PARAMS ((cpp_reader *));
98 static int read_line_number PARAMS ((cpp_reader *, int *));
99 static void cpp_print_file_and_line PARAMS ((cpp_reader *));
100 static void v_cpp_error PARAMS ((cpp_reader *, const char *, va_list));
101 static void v_cpp_warning PARAMS ((cpp_reader *, const char *, va_list));
102 static void v_cpp_error_with_line PARAMS ((cpp_reader *, int, int,
103 const char *, va_list));
104 static void v_cpp_warning_with_line PARAMS ((cpp_reader *, int, int, const char *, va_list));
105 static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
106 static int consider_directive_while_skipping PARAMS ((cpp_reader *, IF_STACK_FRAME *));
108 /* Here is the actual list of #-directives.
109 This table is ordered by frequency of occurrence; the numbers
110 at the end are directive counts from all the source code I have
111 lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
112 linux-2.2.9, and pcmcia-cs-3.0.9). */
114 static const struct directive directive_table[] = {
115 /* In C89 */
116 { 6, do_define, "define", T_DEFINE }, /* 270554 */
117 { 7, do_include, "include", T_INCLUDE }, /* 52262 */
118 { 5, do_endif, "endif", T_ENDIF }, /* 45855 */
119 { 5, do_xifdef, "ifdef", T_IFDEF }, /* 22000 */
120 { 2, do_if, "if", T_IF }, /* 18162 */
121 { 4, do_else, "else", T_ELSE }, /* 9863 */
122 { 6, do_xifdef, "ifndef", T_IFNDEF }, /* 9675 */
123 { 5, do_undef, "undef", T_UNDEF }, /* 4837 */
124 { 4, do_line, "line", T_LINE }, /* 2465 */
125 { 4, do_elif, "elif", T_ELIF }, /* 610 */
126 { 5, do_error, "error", T_ERROR }, /* 475 */
127 { 6, do_pragma, "pragma", T_PRAGMA }, /* 195 */
129 /* Extensions. All deprecated except #warning and #include_next. */
130 { 7, do_warning, "warning", T_WARNING }, /* 22 - GNU */
131 { 12, do_include, "include_next", T_INCLUDE_NEXT }, /* 19 - GNU */
132 { 5, do_ident, "ident", T_IDENT }, /* 11 - SVR4 */
133 { 6, do_include, "import", T_IMPORT }, /* 0 - ObjC */
134 { 6, do_assert, "assert", T_ASSERT }, /* 0 - SVR4 */
135 { 8, do_unassert, "unassert", T_UNASSERT }, /* 0 - SVR4 */
136 #ifdef SCCS_DIRECTIVE
137 { 4, do_sccs, "sccs", T_SCCS }, /* 0 - SVR2? */
138 #endif
139 { -1, 0, "", T_UNUSED }
142 /* Place into PFILE a quoted string representing the string SRC.
143 Caller must reserve enough space in pfile->token_buffer. */
145 void
146 quote_string (pfile, src)
147 cpp_reader *pfile;
148 const char *src;
150 U_CHAR c;
152 CPP_PUTC_Q (pfile, '\"');
153 for (;;)
154 switch ((c = *src++))
156 default:
157 if (ISPRINT (c))
158 CPP_PUTC_Q (pfile, c);
159 else
161 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
162 CPP_ADJUST_WRITTEN (pfile, 4);
164 break;
166 case '\"':
167 case '\\':
168 CPP_PUTC_Q (pfile, '\\');
169 CPP_PUTC_Q (pfile, c);
170 break;
172 case '\0':
173 CPP_PUTC_Q (pfile, '\"');
174 CPP_NUL_TERMINATE_Q (pfile);
175 return;
179 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
181 void
182 cpp_grow_buffer (pfile, n)
183 cpp_reader *pfile;
184 long n;
186 long old_written = CPP_WRITTEN (pfile);
187 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
188 pfile->token_buffer = (U_CHAR *)
189 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
190 CPP_SET_WRITTEN (pfile, old_written);
193 /* Process the string STR as if it appeared as the body of a #define
194 If STR is just an identifier, define it with value 1.
195 If STR has anything after the identifier, then it should
196 be identifier=definition. */
198 void
199 cpp_define (pfile, str)
200 cpp_reader *pfile;
201 U_CHAR *str;
203 U_CHAR *buf, *p;
204 size_t count;
206 /* Copy the entire option so we can modify it. */
207 count = strlen (str) + 3;
208 buf = (U_CHAR *) alloca (count);
209 memcpy (buf, str, count - 2);
210 /* Change the first "=" in the string to a space. If there is none,
211 tack " 1" on the end. */
212 p = (U_CHAR *) strchr (buf, '=');
213 if (p)
215 *p = ' ';
216 count -= 2;
218 else
219 strcpy (&buf[count-3], " 1");
221 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
223 do_define (pfile, NULL);
224 cpp_pop_buffer (pfile);
228 /* Process the string STR as if it appeared as the body of a #assert. */
229 void
230 cpp_assert (pfile, str)
231 cpp_reader *pfile;
232 U_CHAR *str;
234 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
236 do_assert (pfile, NULL);
237 cpp_pop_buffer (pfile);
242 static enum cpp_token
243 null_underflow (pfile)
244 cpp_reader *pfile ATTRIBUTE_UNUSED;
246 return CPP_EOF;
249 static int
250 null_cleanup (pbuf, pfile)
251 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
252 cpp_reader *pfile ATTRIBUTE_UNUSED;
254 return 0;
257 /* Skip a comment - C, C++, or Chill style. M is the first character
258 of the comment marker. If this really is a comment, skip to its
259 end and return ' '. If we hit end-of-file before end-of-comment,
260 return EOF. If this is not a comment, return M (which will be
261 '/' or '-'). */
263 static int
264 skip_comment (pfile, m)
265 cpp_reader *pfile;
266 int m;
268 if (m == '/' && PEEKC() == '*')
270 int c, prev_c = -1;
271 long line, col;
273 FORWARD(1);
274 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
275 for (;;)
277 c = GETC ();
278 if (c == EOF)
280 cpp_error_with_line (pfile, line, col, "unterminated comment");
281 return EOF;
283 else if (c == '\n' || c == '\r')
284 /* \r cannot be a macro escape marker here. */
285 CPP_BUMP_LINE (pfile);
286 else if (c == '/' && prev_c == '*')
287 return ' ';
288 else if (c == '*' && prev_c == '/'
289 && CPP_OPTIONS (pfile)->warn_comments)
290 cpp_warning (pfile, "`/*' within comment");
292 prev_c = c;
295 else if ((m == '/' && PEEKC() == '/'
296 && CPP_OPTIONS (pfile)->cplusplus_comments)
297 || (m == '-' && PEEKC() == '-'
298 && CPP_OPTIONS (pfile)->chill))
300 FORWARD(1);
301 for (;;)
303 int c = GETC ();
304 if (c == EOF)
305 return ' '; /* Allow // to be terminated by EOF. */
306 if (c == '\n')
308 /* Don't consider final '\n' to be part of comment. */
309 FORWARD(-1);
310 return ' ';
312 else if (c == '\r')
313 /* \r cannot be a macro escape marker here. */
314 CPP_BUMP_LINE (pfile);
317 else
318 return m;
321 /* Identical to skip_comment except that it copies the comment into the
322 token_buffer. This is used if put_out_comments. */
323 static int
324 copy_comment (pfile, m)
325 cpp_reader *pfile;
326 int m;
328 if (m == '/' && PEEKC() == '*')
330 int c, prev_c = -1;
331 long line, col;
333 CPP_PUTC (pfile, '/');
334 CPP_PUTC (pfile, '*');
335 FORWARD(1);
336 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
337 for (;;)
339 c = GETC ();
340 if (c == EOF)
342 cpp_error_with_line (pfile, line, col, "unterminated comment");
343 /* We must pretend this was a legitimate comment, so that the
344 output in token_buffer is not passed back tagged CPP_POP. */
345 return ' ';
347 else if (c == '\r')
349 /* \r cannot be a macro escape marker here. */
350 CPP_BUMP_LINE (pfile);
351 continue;
354 CPP_PUTC (pfile, c);
355 if (c == '\n')
357 pfile->lineno++;
358 CPP_BUMP_LINE (pfile);
360 else if (c == '/' && prev_c == '*')
361 return ' ';
362 else if (c == '*' && prev_c == '/'
363 && CPP_OPTIONS (pfile)->warn_comments)
364 cpp_warning (pfile, "`/*' within comment");
366 prev_c = c;
369 else if ((m == '/' && PEEKC() == '/'
370 && CPP_OPTIONS (pfile)->cplusplus_comments)
371 || (m == '-' && PEEKC() == '-'
372 && CPP_OPTIONS (pfile)->chill))
374 CPP_PUTC (pfile, m);
375 CPP_PUTC (pfile, m);
376 FORWARD(1);
377 for (;;)
379 int c = GETC ();
380 if (c == EOF)
381 return ' '; /* Allow line comments to be terminated by EOF. */
382 else if (c == '\n')
384 /* Don't consider final '\n' to be part of comment. */
385 FORWARD(-1);
386 return ' ';
388 else if (c == '\r')
389 /* \r cannot be a macro escape marker here. */
390 CPP_BUMP_LINE (pfile);
392 CPP_PUTC (pfile, c);
395 else
396 return m;
400 /* Skip whitespace \-newline and comments. Does not macro-expand. */
402 void
403 cpp_skip_hspace (pfile)
404 cpp_reader *pfile;
406 int c;
407 while (1)
409 c = GETC();
410 if (c == EOF)
411 return;
412 else if (is_hor_space[c])
414 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
415 cpp_pedwarn (pfile, "%s in preprocessing directive",
416 c == '\f' ? "formfeed" : "vertical tab");
418 else if (c == '\r')
420 /* \r is a backslash-newline marker if !has_escapes, and
421 a deletable-whitespace or no-reexpansion marker otherwise. */
422 if (CPP_BUFFER (pfile)->has_escapes)
424 if (PEEKC() == ' ')
425 FORWARD(1);
426 else
427 break;
429 else
430 CPP_BUFFER (pfile)->lineno++;
432 else if (c == '/' || c == '-')
434 c = skip_comment (pfile, c);
435 if (c == EOF)
436 return;
437 else if (c != ' ')
438 break;
440 else
441 break;
443 FORWARD(-1);
446 /* Read the rest of the current line.
447 The line is appended to PFILE's output buffer. */
449 static void
450 copy_rest_of_line (pfile)
451 cpp_reader *pfile;
453 for (;;)
455 int c = GETC();
456 switch (c)
458 case '\n':
459 FORWARD(-1);
460 case EOF:
461 CPP_NUL_TERMINATE (pfile);
462 return;
464 case '\r':
465 if (CPP_BUFFER (pfile)->has_escapes)
466 break;
467 else
469 CPP_BUFFER (pfile)->lineno++;
470 continue;
472 case '\'':
473 case '\"':
474 parse_string (pfile, c);
475 continue;
476 case '/':
477 if (PEEKC() == '*' && CPP_TRADITIONAL (pfile))
479 CPP_PUTS (pfile, "/**/", 4);
480 skip_comment (pfile, c);
481 continue;
483 /* else fall through */
484 case '-':
485 c = skip_comment (pfile, c);
486 break;
488 case '\f':
489 case '\v':
490 if (CPP_PEDANTIC (pfile))
491 cpp_pedwarn (pfile, "%s in preprocessing directive",
492 c == '\f' ? "formfeed" : "vertical tab");
493 break;
496 CPP_PUTC (pfile, c);
500 /* FIXME: It is almost definitely a performance win to make this do
501 the scan itself. >75% of calls to copy_r_o_l are from here or
502 skip_if_group, which means the common case is to copy stuff into the
503 token_buffer only to discard it. */
504 void
505 skip_rest_of_line (pfile)
506 cpp_reader *pfile;
508 long old = CPP_WRITTEN (pfile);
509 copy_rest_of_line (pfile);
510 CPP_SET_WRITTEN (pfile, old);
513 /* Handle a possible # directive.
514 '#' has already been read. */
516 static int
517 handle_directive (pfile)
518 cpp_reader *pfile;
520 int c;
521 register const struct directive *kt;
522 int ident_length;
523 U_CHAR *ident;
524 long old_written = CPP_WRITTEN (pfile);
526 cpp_skip_hspace (pfile);
528 c = PEEKC ();
529 if (c >= '0' && c <= '9')
531 /* Handle # followed by a line number. Complain about using that
532 form if we're being pedantic, but not if this is regurgitated
533 input (preprocessed or fed back in by the C++ frontend). */
534 if (CPP_PEDANTIC (pfile)
535 && ! CPP_PREPROCESSED (pfile)
536 && ! CPP_BUFFER (pfile)->manual_pop)
537 cpp_pedwarn (pfile, "`#' followed by integer");
538 do_line (pfile, NULL);
539 return 1;
542 /* Now find the directive name. */
543 CPP_PUTC (pfile, '#');
544 parse_name (pfile, GETC());
545 ident = pfile->token_buffer + old_written + 1;
546 ident_length = CPP_PWRITTEN (pfile) - ident;
547 if (ident_length == 0)
549 /* A line of just `#' becomes blank. */
550 if (PEEKC() == '\n')
551 return 1;
552 else
553 return 0;
557 * Decode the keyword and call the appropriate expansion
558 * routine, after moving the input pointer up to the next line.
560 for (kt = directive_table; ; kt++)
562 if (kt->length <= 0)
563 return 0;
564 if (kt->length == ident_length
565 && !strncmp (kt->name, ident, ident_length))
566 break;
569 CPP_SET_WRITTEN (pfile, old_written);
570 (*kt->func) (pfile, kt);
572 return 1;
575 /* Pass a directive through to the output file.
576 BUF points to the contents of the directive, as a contiguous string.
577 LEN is the length of the string pointed to by BUF.
578 KEYWORD is the keyword-table entry for the directive. */
580 static void
581 pass_thru_directive (buf, len, pfile, keyword)
582 const U_CHAR *buf;
583 size_t len;
584 cpp_reader *pfile;
585 const struct directive *keyword;
587 register unsigned keyword_length = keyword->length;
589 CPP_RESERVE (pfile, 1 + keyword_length + len);
590 CPP_PUTC_Q (pfile, '#');
591 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
592 if (len != 0 && buf[0] != ' ')
593 CPP_PUTC_Q (pfile, ' ');
594 CPP_PUTS_Q (pfile, buf, len);
597 /* Check a purported macro name SYMNAME, and yield its length.
598 ASSERTION is nonzero if this is really for an assertion name. */
601 check_macro_name (pfile, symname, assertion)
602 cpp_reader *pfile;
603 const U_CHAR *symname;
604 int assertion;
606 const U_CHAR *p;
607 int sym_length;
609 for (p = symname; is_idchar[*p]; p++)
611 sym_length = p - symname;
612 if (sym_length == 0
613 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
614 cpp_error (pfile,
615 assertion ? "invalid assertion name" : "invalid macro name");
616 else if (!is_idstart[*symname]
617 || (! strncmp (symname, "defined", 7) && sym_length == 7)) {
618 U_CHAR *msg; /* what pain... */
619 msg = (U_CHAR *) alloca (sym_length + 1);
620 bcopy (symname, msg, sym_length);
621 msg[sym_length] = 0;
622 cpp_error (pfile,
623 (assertion
624 ? "invalid assertion name `%s'"
625 : "invalid macro name `%s'"),
626 msg);
628 return sym_length;
631 /* Process a #define command.
632 KEYWORD is the keyword-table entry for #define,
633 or NULL for a "predefined" macro,
634 or the keyword-table entry for #pragma in the case of a #pragma poison. */
636 static int
637 do_define (pfile, keyword)
638 cpp_reader *pfile;
639 const struct directive *keyword;
641 int hashcode;
642 MACRODEF mdef;
643 HASHNODE *hp;
644 long here;
645 U_CHAR *macro, *buf, *end;
646 enum node_type new_type;
648 here = CPP_WRITTEN (pfile);
649 copy_rest_of_line (pfile);
651 if (keyword == NULL || keyword->type == T_DEFINE)
652 new_type = T_MACRO;
653 else
654 new_type = T_POISON;
656 /* Copy out the line so we can pop the token buffer. */
657 buf = pfile->token_buffer + here;
658 end = CPP_PWRITTEN (pfile);
659 macro = (U_CHAR *) alloca (end - buf + 1);
660 bcopy (buf, macro, end - buf + 1);
661 end = macro + (end - buf);
663 CPP_SET_WRITTEN (pfile, here);
665 mdef = create_definition (macro, end, pfile, keyword == NULL);
666 if (mdef.defn == 0)
667 return 0;
669 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
671 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
673 int ok = 0;
674 /* Redefining a precompiled key is ok. */
675 if (hp->type == T_PCSTRING)
676 ok = 1;
677 /* Redefining a poisoned identifier is even worse than `not ok'. */
678 else if (hp->type == T_POISON)
679 ok = -1;
680 /* Redefining a macro is ok if the definitions are the same. */
681 else if (hp->type == T_MACRO)
682 ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
683 /* Redefining a constant is ok with -D. */
684 else if (hp->type == T_CONST || hp->type == T_STDC)
685 ok = ! CPP_OPTIONS (pfile)->done_initializing;
686 /* Print the warning or error if it's not ok. */
687 if (ok <= 0)
689 if (hp->type == T_POISON)
690 cpp_error (pfile, "redefining poisoned `%.*s'",
691 mdef.symlen, mdef.symnam);
692 else
693 cpp_pedwarn (pfile, "`%.*s' redefined", mdef.symlen, mdef.symnam);
694 if (hp->type == T_MACRO)
695 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file,
696 hp->value.defn->line,
697 "this is the location of the previous definition");
699 if (hp->type != T_POISON)
701 /* Replace the old definition. */
702 hp->type = new_type;
703 hp->value.defn = mdef.defn;
706 else
707 cpp_install (pfile, mdef.symnam, mdef.symlen, new_type,
708 (char *) mdef.defn, hashcode);
710 if (keyword != NULL && keyword->type == T_DEFINE)
712 if (CPP_OPTIONS (pfile)->debug_output
713 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
714 dump_definition (pfile, mdef);
715 else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
716 pass_thru_directive (mdef.symnam, mdef.symlen, pfile, keyword);
719 return 0;
723 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
724 If BUFFER != NULL, then use the LENGTH characters in BUFFER
725 as the new input buffer.
726 Return the new buffer, or NULL on failure. */
728 cpp_buffer *
729 cpp_push_buffer (pfile, buffer, length)
730 cpp_reader *pfile;
731 U_CHAR *buffer;
732 long length;
734 cpp_buffer *buf = CPP_BUFFER (pfile);
735 cpp_buffer *new;
736 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
738 cpp_fatal (pfile, "macro or `#include' recursion too deep");
739 return NULL;
742 new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
744 new->if_stack = pfile->if_stack;
745 new->cleanup = null_cleanup;
746 new->underflow = null_underflow;
747 new->buf = new->cur = buffer;
748 new->alimit = new->rlimit = buffer + length;
749 new->prev = buf;
750 new->mark = -1;
752 CPP_BUFFER (pfile) = new;
753 return new;
756 cpp_buffer *
757 cpp_pop_buffer (pfile)
758 cpp_reader *pfile;
760 cpp_buffer *buf = CPP_BUFFER (pfile);
761 (*buf->cleanup) (buf, pfile);
762 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
763 free (buf);
764 pfile->buffer_stack_depth--;
765 return CPP_BUFFER (pfile);
768 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
769 Pop the buffer when done. */
771 void
772 cpp_scan_buffer (pfile)
773 cpp_reader *pfile;
775 cpp_buffer *buffer = CPP_BUFFER (pfile);
776 enum cpp_token token;
777 if (CPP_OPTIONS (pfile)->no_output)
779 long old_written = CPP_WRITTEN (pfile);
780 /* In no-output mode, we can ignore everything but directives. */
781 for (;;)
783 if (! pfile->only_seen_white)
784 skip_rest_of_line (pfile);
785 token = cpp_get_token (pfile);
786 if (token == CPP_EOF) /* Should not happen ... */
787 break;
788 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
790 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
791 != CPP_NULL_BUFFER (pfile))
792 cpp_pop_buffer (pfile);
793 break;
796 CPP_SET_WRITTEN (pfile, old_written);
798 else
800 for (;;)
802 token = cpp_get_token (pfile);
803 if (token == CPP_EOF) /* Should not happen ... */
804 break;
805 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
807 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
808 != CPP_NULL_BUFFER (pfile))
809 cpp_pop_buffer (pfile);
810 break;
817 * Rescan a string (which may have escape marks) into pfile's buffer.
818 * Place the result in pfile->token_buffer.
820 * The input is copied before it is scanned, so it is safe to pass
821 * it something from the token_buffer that will get overwritten
822 * (because it follows CPP_WRITTEN). This is used by do_include.
825 void
826 cpp_expand_to_buffer (pfile, buf, length)
827 cpp_reader *pfile;
828 const U_CHAR *buf;
829 int length;
831 register cpp_buffer *ip;
832 U_CHAR *buf1;
833 int save_no_output;
835 if (length < 0)
837 cpp_fatal (pfile, "internal error: length < 0 in cpp_expand_to_buffer");
838 return;
841 /* Set up the input on the input stack. */
843 buf1 = (U_CHAR *) alloca (length + 1);
844 memcpy (buf1, buf, length);
845 buf1[length] = 0;
847 ip = cpp_push_buffer (pfile, buf1, length);
848 if (ip == NULL)
849 return;
850 ip->has_escapes = 1;
852 /* Scan the input, create the output. */
853 save_no_output = CPP_OPTIONS (pfile)->no_output;
854 CPP_OPTIONS (pfile)->no_output = 0;
855 cpp_scan_buffer (pfile);
856 CPP_OPTIONS (pfile)->no_output = save_no_output;
858 CPP_NUL_TERMINATE (pfile);
861 void
862 cpp_buf_line_and_col (pbuf, linep, colp)
863 register cpp_buffer *pbuf;
864 long *linep, *colp;
866 if (pbuf)
868 *linep = pbuf->lineno;
869 if (colp)
870 *colp = pbuf->cur - pbuf->line_base;
872 else
874 *linep = 0;
875 if (colp)
876 *colp = 0;
880 /* Return the cpp_buffer that corresponds to a file (not a macro). */
882 cpp_buffer *
883 cpp_file_buffer (pfile)
884 cpp_reader *pfile;
886 cpp_buffer *ip = CPP_BUFFER (pfile);
888 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
889 if (ip->fname != NULL)
890 return ip;
891 return NULL;
895 * write out a #line command, for instance, after an #include file.
896 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
899 void
900 output_line_command (pfile, file_change)
901 cpp_reader *pfile;
902 enum file_change_code file_change;
904 long line;
905 cpp_buffer *ip = CPP_BUFFER (pfile);
907 if (ip->fname == NULL)
908 return;
910 if (CPP_OPTIONS (pfile)->no_line_commands
911 || CPP_OPTIONS (pfile)->no_output)
912 return;
914 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, NULL);
916 /* If the current file has not changed, we omit the #line if it would
917 appear to be a no-op, and we output a few newlines instead
918 if we want to increase the line number by a small amount.
919 We cannot do this if pfile->lineno is zero, because that means we
920 haven't output any line commands yet. (The very first line command
921 output is a `same_file' command.) */
922 if (file_change == same_file && pfile->lineno != 0)
924 if (line == pfile->lineno)
925 return;
927 /* If the inherited line number is a little too small,
928 output some newlines instead of a #line command. */
929 if (line > pfile->lineno && line < pfile->lineno + 8)
931 CPP_RESERVE (pfile, 20);
932 while (line > pfile->lineno)
934 CPP_PUTC_Q (pfile, '\n');
935 pfile->lineno++;
937 return;
941 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
942 CPP_PUTS_Q (pfile, "# ", 2);
944 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
945 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
947 quote_string (pfile, ip->nominal_fname);
948 if (file_change != same_file)
950 CPP_PUTC_Q (pfile, ' ');
951 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
953 /* Tell cc1 if following text comes from a system header file. */
954 if (ip->system_header_p)
956 CPP_PUTC_Q (pfile, ' ');
957 CPP_PUTC_Q (pfile, '3');
959 #ifndef NO_IMPLICIT_EXTERN_C
960 /* Tell cc1plus if following text should be treated as C. */
961 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
963 CPP_PUTC_Q (pfile, ' ');
964 CPP_PUTC_Q (pfile, '4');
966 #endif
967 CPP_PUTC_Q (pfile, '\n');
968 pfile->lineno = line;
972 /* Like cpp_get_token, except that it does not read past end-of-line.
973 Also, horizontal space is skipped, and macros are popped. */
975 static enum cpp_token
976 get_directive_token (pfile)
977 cpp_reader *pfile;
979 for (;;)
981 long old_written = CPP_WRITTEN (pfile);
982 enum cpp_token token;
983 cpp_skip_hspace (pfile);
984 if (PEEKC () == '\n')
985 return CPP_VSPACE;
986 token = cpp_get_token (pfile);
987 switch (token)
989 case CPP_POP:
990 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
991 return token;
992 /* ... else fall though ... */
993 case CPP_HSPACE: case CPP_COMMENT:
994 CPP_SET_WRITTEN (pfile, old_written);
995 break;
996 default:
997 return token;
1002 /* Handle #include and #import.
1003 This function expects to see "fname" or <fname> on the input.
1005 The input is normally in part of the output_buffer following
1006 CPP_WRITTEN, and will get overwritten by output_line_command.
1007 I.e. in input file specification has been popped by handle_directive.
1008 This is safe. */
1010 static int
1011 do_include (pfile, keyword)
1012 cpp_reader *pfile;
1013 const struct directive *keyword;
1015 int importing = (keyword->type == T_IMPORT);
1016 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
1017 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
1018 int before; /* included before? */
1019 long flen;
1020 unsigned char *ftok;
1021 cpp_buffer *fp;
1023 enum cpp_token token;
1025 /* Chain of dirs to search */
1026 struct include_hash *ihash;
1027 struct file_name_list *search_start;
1029 long old_written = CPP_WRITTEN (pfile);
1031 int fd;
1033 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1035 if (importing)
1036 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
1037 if (skip_dirs)
1038 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
1041 if (importing && CPP_OPTIONS (pfile)->warn_import
1042 && !CPP_OPTIONS (pfile)->inhibit_warnings
1043 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
1045 pfile->import_warning = 1;
1046 cpp_warning (pfile,
1047 "#import is obsolete, use an #ifndef wrapper in the header file");
1050 pfile->parsing_include_directive++;
1051 token = get_directive_token (pfile);
1052 pfile->parsing_include_directive--;
1054 if (token == CPP_STRING)
1056 if (pfile->token_buffer[old_written] == '<')
1057 angle_brackets = 1;
1059 #ifdef VMS
1060 else if (token == CPP_NAME)
1062 /* Support '#include xyz' like VAX-C. It is taken as
1063 '#include <xyz.h>' and generates a warning. */
1064 cpp_warning (pfile,
1065 "`#include filename' is obsolete, use `#include <filename.h>'");
1066 angle_brackets = 1;
1068 /* Append the missing `.h' to the name. */
1069 CPP_PUTS (pfile, ".h", 2);
1071 #endif
1072 else
1074 cpp_error (pfile,
1075 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1076 CPP_SET_WRITTEN (pfile, old_written);
1077 skip_rest_of_line (pfile);
1078 return 0;
1081 flen = CPP_WRITTEN (pfile) - old_written;
1082 ftok = (unsigned char *) alloca (flen + 1);
1083 memcpy (ftok, pfile->token_buffer + old_written, flen);
1084 ftok[flen] = '\0';
1086 if (get_directive_token (pfile) != CPP_VSPACE)
1088 cpp_error (pfile, "junk at end of `#include'");
1089 skip_rest_of_line (pfile);
1092 CPP_SET_WRITTEN (pfile, old_written);
1094 if (flen == 0)
1096 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
1097 return 0;
1100 if (CPP_OPTIONS (pfile)->dump_includes)
1101 pass_thru_directive (ftok,
1102 flen
1103 #ifdef VMS
1104 - ((token == CPP_NAME) ? 2 : 0)
1105 #endif
1106 , pfile, keyword);
1108 #ifdef VMS
1109 if (token == CPP_STRING)
1110 #endif
1112 ftok++;
1113 flen -= 2;
1114 ftok[flen] = '\0';
1117 search_start = 0;
1119 for (fp = CPP_BUFFER (pfile);
1120 fp != CPP_NULL_BUFFER (pfile);
1121 fp = CPP_PREV_BUFFER (fp))
1122 if (fp->fname != NULL)
1123 break;
1125 if (fp == CPP_NULL_BUFFER (pfile))
1127 cpp_fatal (pfile, "cpp internal error: fp == NULL_BUFFER in do_include");
1128 return 0;
1131 /* For #include_next, skip in the search path past the dir in which the
1132 containing file was found. Treat files specified using an absolute path
1133 as if there are no more directories to search. Treat the primary source
1134 file like any other included source, but generate a warning. */
1135 if (skip_dirs && CPP_PREV_BUFFER(fp) != CPP_NULL_BUFFER (pfile))
1137 if (fp->ihash->foundhere != ABSOLUTE_PATH)
1138 search_start = fp->ihash->foundhere->next;
1140 else
1142 if (skip_dirs)
1143 cpp_warning (pfile, "#include_next in primary source file");
1145 if (angle_brackets)
1146 search_start = CPP_OPTIONS (pfile)->bracket_include;
1147 else
1149 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
1151 if (fp)
1152 search_start = fp->actual_dir;
1154 else
1155 search_start = CPP_OPTIONS (pfile)->quote_include;
1159 if (!search_start)
1161 cpp_error (pfile, "No include path in which to find %s", ftok);
1162 return 0;
1165 fd = find_include_file (pfile, ftok, search_start, &ihash, &before);
1167 if (fd == -2)
1168 return 0;
1170 if (fd == -1)
1172 if (CPP_OPTIONS (pfile)->print_deps_missing_files
1173 && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
1174 (pfile->system_include_depth > 0)))
1176 if (!angle_brackets)
1177 deps_output (pfile, ftok, ' ');
1178 else
1180 char *p;
1181 struct file_name_list *ptr;
1182 /* If requested as a system header, assume it belongs in
1183 the first system header directory. */
1184 if (CPP_OPTIONS (pfile)->bracket_include)
1185 ptr = CPP_OPTIONS (pfile)->bracket_include;
1186 else
1187 ptr = CPP_OPTIONS (pfile)->quote_include;
1189 p = (char *) alloca (strlen (ptr->name)
1190 + strlen (ftok) + 2);
1191 if (*ptr->name != '\0')
1193 strcpy (p, ptr->name);
1194 strcat (p, "/");
1196 strcat (p, ftok);
1197 deps_output (pfile, p, ' ');
1200 /* If -M was specified, and this header file won't be added to
1201 the dependency list, then don't count this as an error,
1202 because we can still produce correct output. Otherwise, we
1203 can't produce correct output, because there may be
1204 dependencies we need inside the missing file, and we don't
1205 know what directory this missing file exists in. */
1206 else if (CPP_PRINT_DEPS (pfile)
1207 && (CPP_PRINT_DEPS (pfile)
1208 <= (angle_brackets || (pfile->system_include_depth > 0))))
1209 cpp_warning (pfile, "No include path in which to find %s", ftok);
1210 else
1211 cpp_error_from_errno (pfile, ftok);
1213 return 0;
1216 /* For -M, add the file to the dependencies on its first inclusion. */
1217 if (!before && (CPP_PRINT_DEPS (pfile)
1218 > (angle_brackets || (pfile->system_include_depth > 0))))
1219 deps_output (pfile, ihash->name, ' ');
1221 /* Handle -H option. */
1222 if (CPP_OPTIONS(pfile)->print_include_names)
1224 fp = CPP_BUFFER (pfile);
1225 while ((fp = CPP_PREV_BUFFER (fp)) != CPP_NULL_BUFFER (pfile))
1226 putc ('.', stderr);
1227 fprintf (stderr, " %s\n", ihash->name);
1230 /* Actually process the file */
1232 if (importing)
1233 ihash->control_macro = "";
1235 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
1237 close (fd);
1238 return 0;
1241 if (angle_brackets)
1242 pfile->system_include_depth++; /* Decremented in file_cleanup. */
1244 if (finclude (pfile, fd, ihash))
1246 output_line_command (pfile, enter_file);
1247 pfile->only_seen_white = 2;
1250 return 0;
1253 /* Subroutine of do_line. Read next token from PFILE without adding it to
1254 the output buffer. If it is a number between 1 and 4, store it in *NUM
1255 and return 1; otherwise, return 0 and complain if we aren't at the end
1256 of the directive. */
1258 static int
1259 read_line_number (pfile, num)
1260 cpp_reader *pfile;
1261 int *num;
1263 long save_written = CPP_WRITTEN (pfile);
1264 U_CHAR *p = pfile->token_buffer + save_written;
1265 enum cpp_token token = get_directive_token (pfile);
1266 CPP_SET_WRITTEN (pfile, save_written);
1268 if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
1270 *num = p[0] - '0';
1271 return 1;
1273 else
1275 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
1276 cpp_error (pfile, "invalid format `#line' command");
1277 return 0;
1281 /* Interpret #line command.
1282 Note that the filename string (if any) is treated as if it were an
1283 include filename. That means no escape handling. */
1285 static int
1286 do_line (pfile, keyword)
1287 cpp_reader *pfile;
1288 const struct directive *keyword ATTRIBUTE_UNUSED;
1290 cpp_buffer *ip = CPP_BUFFER (pfile);
1291 int new_lineno;
1292 long old_written = CPP_WRITTEN (pfile);
1293 enum file_change_code file_change = same_file;
1294 enum cpp_token token;
1295 char *x;
1297 token = get_directive_token (pfile);
1299 if (token != CPP_NUMBER)
1301 cpp_error (pfile, "token after `#line' is not an integer");
1302 goto bad_line_directive;
1305 new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
1306 if (x[0] != '\0')
1308 cpp_error (pfile, "token after `#line' is not an integer");
1309 goto bad_line_directive;
1311 CPP_SET_WRITTEN (pfile, old_written);
1313 if (CPP_PEDANTIC (pfile) && new_lineno <= 0)
1314 cpp_pedwarn (pfile, "line number out of range in `#line' command");
1316 token = get_directive_token (pfile);
1318 if (token == CPP_STRING)
1320 U_CHAR *fname = pfile->token_buffer + old_written + 1;
1321 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
1322 int action_number = 0;
1324 if (read_line_number (pfile, &action_number))
1326 if (CPP_PEDANTIC (pfile))
1327 cpp_pedwarn (pfile, "garbage at end of `#line' command");
1329 if (action_number == 1)
1331 file_change = enter_file;
1332 read_line_number (pfile, &action_number);
1334 else if (action_number == 2)
1336 file_change = leave_file;
1337 read_line_number (pfile, &action_number);
1339 if (action_number == 3)
1341 ip->system_header_p = 1;
1342 read_line_number (pfile, &action_number);
1344 if (action_number == 4)
1346 ip->system_header_p = 2;
1347 read_line_number (pfile, &action_number);
1351 *end_name = '\0';
1353 if (strcmp (fname, ip->nominal_fname))
1355 char *newname, *oldname;
1356 if (!strcmp (fname, ip->fname))
1357 newname = ip->fname;
1358 else if (ip->last_nominal_fname
1359 && !strcmp (fname, ip->last_nominal_fname))
1360 newname = ip->last_nominal_fname;
1361 else
1362 newname = xstrdup (fname);
1364 oldname = ip->nominal_fname;
1365 ip->nominal_fname = newname;
1367 if (ip->last_nominal_fname
1368 && ip->last_nominal_fname != oldname
1369 && ip->last_nominal_fname != newname
1370 && ip->last_nominal_fname != ip->fname)
1371 free (ip->last_nominal_fname);
1373 if (newname == ip->fname)
1374 ip->last_nominal_fname = NULL;
1375 else
1376 ip->last_nominal_fname = oldname;
1379 else if (token != CPP_VSPACE && token != CPP_EOF)
1381 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1382 goto bad_line_directive;
1385 /* The Newline at the end of this line remains to be processed.
1386 To put the next line at the specified line number,
1387 we must store a line number now that is one less. */
1388 ip->lineno = new_lineno - 1;
1389 CPP_SET_WRITTEN (pfile, old_written);
1390 output_line_command (pfile, file_change);
1391 return 0;
1393 bad_line_directive:
1394 skip_rest_of_line (pfile);
1395 CPP_SET_WRITTEN (pfile, old_written);
1396 return 0;
1399 /* Remove the definition of a symbol from the symbol table.
1400 According to the C standard, it is not an error to undef
1401 something that has no definitions. */
1402 static int
1403 do_undef (pfile, keyword)
1404 cpp_reader *pfile;
1405 const struct directive *keyword;
1407 int sym_length;
1408 HASHNODE *hp;
1409 U_CHAR *buf, *name, *limit;
1410 int c;
1411 long here = CPP_WRITTEN (pfile);
1412 enum cpp_token token;
1414 cpp_skip_hspace (pfile);
1415 c = GETC();
1416 if (! is_idstart[c])
1418 cpp_error (pfile, "token after #undef is not an identifier");
1419 skip_rest_of_line (pfile);
1420 return 1;
1423 parse_name (pfile, c);
1424 buf = pfile->token_buffer + here;
1425 limit = CPP_PWRITTEN(pfile);
1427 /* Copy out the token so we can pop the token buffer. */
1428 name = (U_CHAR *) alloca (limit - buf + 1);
1429 bcopy(buf, name, limit - buf);
1430 name[limit - buf] = '\0';
1432 token = get_directive_token (pfile);
1433 if (token != CPP_VSPACE && token != CPP_POP)
1435 cpp_pedwarn (pfile, "junk on line after #undef");
1436 skip_rest_of_line (pfile);
1439 CPP_SET_WRITTEN (pfile, here);
1441 sym_length = check_macro_name (pfile, buf, 0);
1443 while ((hp = cpp_lookup (pfile, name, sym_length, -1)) != NULL)
1445 /* If we are generating additional info for debugging (with -g) we
1446 need to pass through all effective #undef commands. */
1447 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1448 pass_thru_directive (name, sym_length, pfile, keyword);
1449 if (hp->type == T_POISON)
1450 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
1451 else
1453 if (hp->type != T_MACRO)
1454 cpp_warning (pfile, "undefining `%s'", hp->name);
1455 delete_macro (hp);
1459 return 0;
1462 /* Wrap do_undef for -U processing. */
1463 void
1464 cpp_undef (pfile, macro)
1465 cpp_reader *pfile;
1466 U_CHAR *macro;
1468 if (cpp_push_buffer (pfile, macro, strlen (macro)))
1470 do_undef (pfile, NULL);
1471 cpp_pop_buffer (pfile);
1477 * Report an error detected by the program we are processing.
1478 * Use the text of the line in the error message.
1479 * (We use error because it prints the filename & line#.)
1482 static int
1483 do_error (pfile, keyword)
1484 cpp_reader *pfile;
1485 const struct directive *keyword ATTRIBUTE_UNUSED;
1487 long here = CPP_WRITTEN (pfile);
1488 U_CHAR *text;
1489 copy_rest_of_line (pfile);
1490 text = pfile->token_buffer + here;
1491 SKIP_WHITE_SPACE(text);
1493 cpp_error (pfile, "#error %s", text);
1494 CPP_SET_WRITTEN (pfile, here);
1495 return 0;
1499 * Report a warning detected by the program we are processing.
1500 * Use the text of the line in the warning message, then continue.
1503 static int
1504 do_warning (pfile, keyword)
1505 cpp_reader *pfile;
1506 const struct directive *keyword ATTRIBUTE_UNUSED;
1508 U_CHAR *text;
1509 long here = CPP_WRITTEN(pfile);
1510 copy_rest_of_line (pfile);
1511 text = pfile->token_buffer + here;
1512 SKIP_WHITE_SPACE(text);
1514 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1515 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1517 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
1518 if -pedantic-errors is given, #warning should cause an error. */
1519 cpp_pedwarn (pfile, "#warning %s", text);
1520 CPP_SET_WRITTEN (pfile, here);
1521 return 0;
1524 /* Report program identification.
1525 This is not precisely what cccp does with #ident, however I believe
1526 it matches `closely enough' (behavior is identical as long as there
1527 are no macros on the #ident line, which is pathological in my opinion). */
1529 static int
1530 do_ident (pfile, keyword)
1531 cpp_reader *pfile;
1532 const struct directive *keyword ATTRIBUTE_UNUSED;
1534 /* Allow #ident in system headers, since that's not user's fault. */
1535 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1536 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1538 CPP_PUTS (pfile, "#ident ", 7);
1539 cpp_skip_hspace (pfile);
1540 copy_rest_of_line (pfile);
1542 return 0;
1545 /* Just check for some recognized pragmas that need validation here,
1546 and leave the text in the token buffer to be output. */
1548 static int
1549 do_pragma (pfile, keyword)
1550 cpp_reader *pfile;
1551 const struct directive *keyword ATTRIBUTE_UNUSED;
1553 long here;
1554 U_CHAR *buf;
1556 CPP_PUTS (pfile, "#pragma ", 8);
1557 cpp_skip_hspace (pfile);
1559 here = CPP_WRITTEN (pfile);
1560 copy_rest_of_line (pfile);
1561 buf = pfile->token_buffer + here;
1563 if (!strncmp (buf, "once", 4))
1565 cpp_buffer *ip = NULL;
1567 /* Allow #pragma once in system headers, since that's not the user's
1568 fault. */
1569 if (!CPP_BUFFER (pfile)->system_header_p)
1570 cpp_warning (pfile, "`#pragma once' is obsolete");
1572 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
1574 if (ip == CPP_NULL_BUFFER (pfile))
1575 return 0;
1576 if (ip->fname != NULL)
1577 break;
1580 if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
1581 cpp_warning (pfile, "`#pragma once' outside include file");
1582 else
1583 ip->ihash->control_macro = ""; /* never repeat */
1585 else if (!strncmp (buf, "implementation", 14))
1587 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1588 been included yet. */
1589 struct include_hash *ptr;
1590 U_CHAR *p = buf + 14, *fname, *fcopy;
1591 SKIP_WHITE_SPACE (p);
1592 if (*p == '\n' || *p != '\"')
1593 return 0;
1595 fname = p + 1;
1596 p = (U_CHAR *) index (fname, '\"');
1598 fcopy = (U_CHAR *) alloca (p - fname + 1);
1599 bcopy (fname, fcopy, p - fname);
1600 fcopy[p-fname] = '\0';
1602 ptr = include_hash (pfile, fcopy, 0);
1603 if (ptr)
1604 cpp_warning (pfile,
1605 "`#pragma implementation' for `%s' appears after file is included",
1606 fcopy);
1608 else if (!strncmp (buf, "poison", 6))
1610 /* Poison these symbols so that all subsequent usage produces an
1611 error message. */
1612 U_CHAR *p = buf + 6;
1613 size_t plen;
1614 U_CHAR *syms;
1615 int writeit;
1617 SKIP_WHITE_SPACE (p);
1618 plen = strlen(p) + 1;
1620 syms = (U_CHAR *) alloca (plen);
1621 memcpy (syms, p, plen);
1623 /* As a rule, don't include #pragma poison commands in output,
1624 unless the user asks for them. */
1625 writeit = (CPP_OPTIONS (pfile)->debug_output
1626 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions
1627 || CPP_OPTIONS (pfile)->dump_macros == dump_names);
1629 if (writeit)
1630 CPP_SET_WRITTEN (pfile, here);
1631 else
1632 CPP_SET_WRITTEN (pfile, here-8);
1634 if (writeit)
1636 CPP_RESERVE (pfile, plen + 7);
1637 CPP_PUTS_Q (pfile, "poison", 7);
1640 while (*syms != '\0')
1642 U_CHAR *end = syms;
1644 while (is_idchar[*end])
1645 end++;
1647 if (!is_hor_space[*end] && *end != '\0')
1649 cpp_error (pfile, "invalid #pragma poison directive");
1650 return 1;
1653 if (cpp_push_buffer (pfile, syms, end - syms) != NULL)
1655 do_define (pfile, keyword);
1656 cpp_pop_buffer (pfile);
1658 if (writeit)
1660 CPP_PUTC_Q (pfile, ' ');
1661 CPP_PUTS_Q (pfile, syms, end - syms);
1663 syms = end;
1664 SKIP_WHITE_SPACE (syms);
1668 return 0;
1671 #ifdef SCCS_DIRECTIVE
1672 /* Just ignore #sccs, on systems where we define it at all. */
1674 static int
1675 do_sccs (pfile, keyword)
1676 cpp_reader *pfile;
1677 const struct directive *keyword ATTRIBUTE_UNUSED;
1679 if (CPP_PEDANTIC (pfile))
1680 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1681 skip_rest_of_line (pfile);
1682 return 0;
1684 #endif
1687 /* We've found an `#if' directive. If the only thing before it in
1688 this file is white space, and if it is of the form
1689 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1690 for inclusion of this file. (See redundant_include_p in cppfiles.c
1691 for an explanation of controlling macros.) If so, return a
1692 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1694 static U_CHAR *
1695 detect_if_not_defined (pfile)
1696 cpp_reader *pfile;
1698 U_CHAR *control_macro = 0;
1700 if (pfile->only_seen_white == 2)
1702 char *ident;
1703 enum cpp_token token;
1704 int base_offset;
1705 int token_offset;
1706 int need_rparen = 0;
1708 /* Save state required for restore. */
1709 pfile->no_macro_expand++;
1710 parse_set_mark (pfile);
1711 base_offset = CPP_WRITTEN (pfile);
1713 /* Look for `!', */
1714 if (get_directive_token (pfile) != CPP_OTHER
1715 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1716 || CPP_PWRITTEN (pfile)[-1] != '!')
1717 goto restore;
1719 /* ...then `defined', */
1720 token_offset = CPP_WRITTEN (pfile);
1721 token = get_directive_token (pfile);
1722 if (token != CPP_NAME)
1723 goto restore;
1724 ident = pfile->token_buffer + token_offset;
1725 CPP_NUL_TERMINATE (pfile);
1726 if (strcmp (ident, "defined"))
1727 goto restore;
1729 /* ...then an optional '(' and the name, */
1730 token_offset = CPP_WRITTEN (pfile);
1731 token = get_directive_token (pfile);
1732 if (token == CPP_LPAREN)
1734 token_offset = CPP_WRITTEN (pfile);
1735 token = get_directive_token (pfile);
1736 if (token != CPP_NAME)
1737 goto restore;
1738 need_rparen = 1;
1740 else if (token != CPP_NAME)
1741 goto restore;
1743 ident = pfile->token_buffer + token_offset;
1744 CPP_NUL_TERMINATE (pfile);
1746 /* ...then the ')', if necessary, */
1747 if ((!need_rparen || get_directive_token (pfile) == CPP_RPAREN)
1748 /* ...and make sure there's nothing else on the line. */
1749 && get_directive_token (pfile) == CPP_VSPACE)
1750 control_macro = xstrdup (ident);
1752 restore:
1753 CPP_SET_WRITTEN (pfile, base_offset);
1754 pfile->no_macro_expand--;
1755 parse_goto_mark (pfile);
1758 return control_macro;
1762 * handle #if command by
1763 * 1) inserting special `defined' keyword into the hash table
1764 * that gets turned into 0 or 1 by special_symbol (thus,
1765 * if the luser has a symbol called `defined' already, it won't
1766 * work inside the #if command)
1767 * 2) rescan the input into a temporary output buffer
1768 * 3) pass the output buffer to the yacc parser and collect a value
1769 * 4) clean up the mess left from steps 1 and 2.
1770 * 5) call conditional_skip to skip til the next #endif (etc.),
1771 * or not, depending on the value from step 3.
1774 static int
1775 do_if (pfile, keyword)
1776 cpp_reader *pfile;
1777 const struct directive *keyword ATTRIBUTE_UNUSED;
1779 U_CHAR *control_macro = detect_if_not_defined (pfile);
1780 HOST_WIDEST_INT value = eval_if_expression (pfile);
1781 conditional_skip (pfile, value == 0, T_IF, control_macro);
1782 return 0;
1786 * handle a #elif directive by not changing if_stack either.
1787 * see the comment above do_else.
1790 static int
1791 do_elif (pfile, keyword)
1792 cpp_reader *pfile;
1793 const struct directive *keyword ATTRIBUTE_UNUSED;
1795 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
1796 cpp_error (pfile, "`#elif' not within a conditional");
1797 return 0;
1798 } else {
1799 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
1800 cpp_error (pfile, "`#elif' after `#else'");
1801 #if 0
1802 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
1803 #endif
1804 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
1805 && strcmp (pfile->if_stack->fname,
1806 CPP_BUFFER (pfile)->nominal_fname) != 0)
1807 fprintf (stderr, ", file %s", pfile->if_stack->fname);
1808 fprintf (stderr, ")\n");
1810 pfile->if_stack->type = T_ELIF;
1813 if (pfile->if_stack->if_succeeded)
1814 skip_if_group (pfile);
1815 else {
1816 HOST_WIDEST_INT value = eval_if_expression (pfile);
1817 if (value == 0)
1818 skip_if_group (pfile);
1819 else {
1820 ++pfile->if_stack->if_succeeded; /* continue processing input */
1821 output_line_command (pfile, same_file);
1824 return 0;
1828 * evaluate a #if expression in BUF, of length LENGTH,
1829 * then parse the result as a C expression and return the value as an int.
1832 static HOST_WIDEST_INT
1833 eval_if_expression (pfile)
1834 cpp_reader *pfile;
1836 HOST_WIDEST_INT value;
1837 long old_written = CPP_WRITTEN (pfile);
1839 pfile->pcp_inside_if = 1;
1840 value = cpp_parse_expr (pfile);
1841 pfile->pcp_inside_if = 0;
1843 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1845 return value;
1849 * routine to handle ifdef/ifndef. Try to look up the symbol,
1850 * then do or don't skip to the #endif/#else/#elif depending
1851 * on what directive is actually being processed.
1854 static int
1855 do_xifdef (pfile, keyword)
1856 cpp_reader *pfile;
1857 const struct directive *keyword;
1859 int skip;
1860 cpp_buffer *ip = CPP_BUFFER (pfile);
1861 U_CHAR *ident;
1862 int ident_length;
1863 enum cpp_token token;
1864 int start_of_file = 0;
1865 U_CHAR *control_macro = 0;
1866 int old_written = CPP_WRITTEN (pfile);
1868 /* Detect a #ifndef at start of file (not counting comments). */
1869 if (ip->fname != 0 && keyword->type == T_IFNDEF)
1870 start_of_file = pfile->only_seen_white == 2;
1872 pfile->no_macro_expand++;
1873 token = get_directive_token (pfile);
1874 pfile->no_macro_expand--;
1876 ident = pfile->token_buffer + old_written;
1877 ident_length = CPP_WRITTEN (pfile) - old_written;
1878 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1880 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1882 skip = (keyword->type == T_IFDEF);
1883 if (! CPP_TRADITIONAL (pfile))
1884 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
1886 else if (token == CPP_NAME)
1888 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
1889 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
1890 if (start_of_file && !skip)
1892 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
1893 bcopy (ident, control_macro, ident_length + 1);
1895 if (hp != NULL && hp->type == T_POISON)
1897 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1898 skip = !skip;
1901 else
1903 skip = (keyword->type == T_IFDEF);
1904 if (! CPP_TRADITIONAL (pfile))
1905 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
1908 if (!CPP_TRADITIONAL (pfile))
1909 { int c;
1910 cpp_skip_hspace (pfile);
1911 c = PEEKC ();
1912 if (c != EOF && c != '\n')
1913 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
1915 skip_rest_of_line (pfile);
1917 #if 0
1918 if (pcp_outfile) {
1919 /* Output a precondition for this macro. */
1920 if (hp && hp->value.defn->predefined)
1921 fprintf (pcp_outfile, "#define %s\n", hp->name);
1922 else {
1923 U_CHAR *cp = buf;
1924 fprintf (pcp_outfile, "#undef ");
1925 while (is_idchar[*cp]) /* Ick! */
1926 fputc (*cp++, pcp_outfile);
1927 putc ('\n', pcp_outfile);
1929 #endif
1931 conditional_skip (pfile, skip, T_IF, control_macro);
1932 return 0;
1935 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1936 If this is a #ifndef starting at the beginning of a file,
1937 CONTROL_MACRO is the macro name tested by the #ifndef.
1938 Otherwise, CONTROL_MACRO is 0. */
1940 static void
1941 conditional_skip (pfile, skip, type, control_macro)
1942 cpp_reader *pfile;
1943 int skip;
1944 enum node_type type;
1945 U_CHAR *control_macro;
1947 IF_STACK_FRAME *temp;
1949 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
1950 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
1951 temp->lineno = CPP_BUFFER (pfile)->lineno;
1952 temp->next = pfile->if_stack;
1953 temp->control_macro = control_macro;
1954 pfile->if_stack = temp;
1956 pfile->if_stack->type = type;
1958 if (skip != 0) {
1959 skip_if_group (pfile);
1960 return;
1961 } else {
1962 ++pfile->if_stack->if_succeeded;
1963 output_line_command (pfile, same_file);
1967 /* Subroutine of skip_if_group. Examine one preprocessing directive and
1968 return 0 if skipping should continue, 1 if it should halt. Also
1969 adjusts the if_stack as appropriate.
1970 The `#' has been read, but not the identifier. */
1972 static int
1973 consider_directive_while_skipping (pfile, stack)
1974 cpp_reader *pfile;
1975 IF_STACK_FRAME *stack;
1977 long ident_len, ident;
1978 const struct directive *kt;
1979 IF_STACK_FRAME *temp;
1981 cpp_skip_hspace (pfile);
1983 ident = CPP_WRITTEN (pfile);
1984 parse_name (pfile, GETC());
1985 ident_len = CPP_WRITTEN (pfile) - ident;
1987 CPP_SET_WRITTEN (pfile, ident);
1989 for (kt = directive_table; kt->length >= 0; kt++)
1990 if (kt->length == ident_len
1991 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
1992 switch (kt->type)
1994 case T_IF:
1995 case T_IFDEF:
1996 case T_IFNDEF:
1997 temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
1998 temp->next = pfile->if_stack;
1999 pfile->if_stack = temp;
2000 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
2001 temp->type = kt->type;
2002 return 0;
2004 case T_ELSE:
2005 if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
2006 validate_else (pfile, "#else");
2007 /* fall through */
2008 case T_ELIF:
2009 if (pfile->if_stack->type == T_ELSE)
2010 cpp_error (pfile, "`%s' after `#else'", kt->name);
2012 if (pfile->if_stack == stack)
2013 return 1;
2014 else
2016 pfile->if_stack->type = kt->type;
2017 return 0;
2020 case T_ENDIF:
2021 if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
2022 validate_else (pfile, "#endif");
2024 if (pfile->if_stack == stack)
2025 return 1;
2027 temp = pfile->if_stack;
2028 pfile->if_stack = temp->next;
2029 free (temp);
2030 return 0;
2032 default:
2033 return 0;
2036 /* Don't let erroneous code go by. */
2037 if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
2038 cpp_pedwarn (pfile, "invalid preprocessor directive name");
2039 return 0;
2042 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
2043 * leaves input ptr at the sharp sign found.
2045 static void
2046 skip_if_group (pfile)
2047 cpp_reader *pfile;
2049 int c;
2050 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
2051 U_CHAR *beg_of_line;
2052 long old_written;
2054 if (CPP_OPTIONS (pfile)->output_conditionals)
2056 CPP_PUTS (pfile, "#failed\n", 8);
2057 pfile->lineno++;
2058 output_line_command (pfile, same_file);
2061 old_written = CPP_WRITTEN (pfile);
2063 for (;;)
2065 beg_of_line = CPP_BUFFER (pfile)->cur;
2067 if (! CPP_TRADITIONAL (pfile))
2068 cpp_skip_hspace (pfile);
2069 c = GETC();
2070 if (c == '\n')
2072 if (CPP_OPTIONS (pfile)->output_conditionals)
2073 CPP_PUTC (pfile, c);
2074 CPP_BUMP_LINE (pfile);
2075 continue;
2077 else if (c == '#')
2079 if (consider_directive_while_skipping (pfile, save_if_stack))
2080 break;
2082 else if (c == EOF)
2083 return; /* Caller will issue error. */
2085 FORWARD(-1);
2086 if (CPP_OPTIONS (pfile)->output_conditionals)
2088 CPP_PUTS (pfile, beg_of_line, CPP_BUFFER (pfile)->cur - beg_of_line);
2089 copy_rest_of_line (pfile);
2091 else
2093 copy_rest_of_line (pfile);
2094 CPP_SET_WRITTEN (pfile, old_written); /* discard it */
2097 c = GETC();
2098 if (c == EOF)
2099 return; /* Caller will issue error. */
2100 else
2102 /* \n */
2103 if (CPP_OPTIONS (pfile)->output_conditionals)
2105 CPP_PUTC (pfile, c);
2106 pfile->lineno++;
2108 CPP_BUMP_LINE (pfile);
2112 /* Back up to the beginning of this line. Caller will process the
2113 directive. */
2114 CPP_BUFFER (pfile)->cur = beg_of_line;
2115 pfile->only_seen_white = 1;
2116 if (CPP_OPTIONS (pfile)->output_conditionals)
2118 CPP_PUTS (pfile, "#endfailed\n", 11);
2119 pfile->lineno++;
2124 * handle a #else directive. Do this by just continuing processing
2125 * without changing if_stack ; this is so that the error message
2126 * for missing #endif's etc. will point to the original #if. It
2127 * is possible that something different would be better.
2130 static int
2131 do_else (pfile, keyword)
2132 cpp_reader *pfile;
2133 const struct directive *keyword ATTRIBUTE_UNUSED;
2135 cpp_buffer *ip = CPP_BUFFER (pfile);
2137 if (CPP_PEDANTIC (pfile))
2138 validate_else (pfile, "#else");
2139 skip_rest_of_line (pfile);
2141 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
2142 cpp_error (pfile, "`#else' not within a conditional");
2143 return 0;
2144 } else {
2145 /* #ifndef can't have its special treatment for containing the whole file
2146 if it has a #else clause. */
2147 pfile->if_stack->control_macro = 0;
2149 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
2150 cpp_error (pfile, "`#else' after `#else'");
2151 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
2152 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
2153 fprintf (stderr, ", file %s", pfile->if_stack->fname);
2154 fprintf (stderr, ")\n");
2156 pfile->if_stack->type = T_ELSE;
2159 if (pfile->if_stack->if_succeeded)
2160 skip_if_group (pfile);
2161 else {
2162 ++pfile->if_stack->if_succeeded; /* continue processing input */
2163 output_line_command (pfile, same_file);
2165 return 0;
2169 * unstack after #endif command
2172 static int
2173 do_endif (pfile, keyword)
2174 cpp_reader *pfile;
2175 const struct directive *keyword ATTRIBUTE_UNUSED;
2177 if (CPP_PEDANTIC (pfile))
2178 validate_else (pfile, "#endif");
2179 skip_rest_of_line (pfile);
2181 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2182 cpp_error (pfile, "`#endif' not within a conditional");
2183 else
2185 IF_STACK_FRAME *temp = pfile->if_stack;
2186 pfile->if_stack = temp->next;
2187 if (temp->control_macro != 0)
2189 /* This #endif matched a #ifndef at the start of the file.
2190 See if it is at the end of the file. */
2191 int c;
2193 parse_set_mark (pfile);
2195 for (;;)
2197 cpp_skip_hspace (pfile);
2198 c = GETC ();
2199 if (c != '\n')
2200 break;
2202 parse_goto_mark (pfile);
2204 if (c == EOF)
2206 /* This #endif ends a #ifndef
2207 that contains all of the file (aside from whitespace).
2208 Arrange not to include the file again
2209 if the macro that was tested is defined. */
2210 struct cpp_buffer *ip;
2211 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2212 if (ip->fname != NULL)
2213 break;
2214 ip->ihash->control_macro = (char *) temp->control_macro;
2217 free (temp);
2218 output_line_command (pfile, same_file);
2220 return 0;
2223 /* When an #else or #endif is found while skipping failed conditional,
2224 if -pedantic was specified, this is called to warn about text after
2225 the command name. P points to the first char after the command name. */
2227 static void
2228 validate_else (pfile, directive)
2229 cpp_reader *pfile;
2230 const char *directive;
2232 int c;
2233 cpp_skip_hspace (pfile);
2234 c = PEEKC ();
2235 if (c != EOF && c != '\n')
2236 cpp_pedwarn (pfile,
2237 "text following `%s' violates ANSI standard", directive);
2240 /* Convert T_IF, etc. to a string. Used in error messages. */
2241 static const char *
2242 if_directive_name (pfile, ifs)
2243 cpp_reader *pfile;
2244 struct if_stack *ifs;
2246 switch (ifs->type)
2248 case T_IF: return "#if";
2249 case T_IFDEF: return "#ifdef";
2250 case T_IFNDEF: return "#ifndef";
2251 case T_ELIF: return "#elif";
2252 case T_ELSE: return "#else";
2253 default:
2254 cpp_fatal (pfile, "impossible if_stack->type value %d", ifs->type);
2255 return "unknown";
2259 /* Get the next token, and add it to the text in pfile->token_buffer.
2260 Return the kind of token we got. */
2262 enum cpp_token
2263 cpp_get_token (pfile)
2264 cpp_reader *pfile;
2266 register int c, c2, c3;
2267 enum cpp_token token;
2268 struct cpp_options *opts = CPP_OPTIONS (pfile);
2270 get_next:
2271 c = GETC();
2272 if (c == EOF)
2274 handle_eof:
2275 if (CPP_BUFFER (pfile)->manual_pop)
2276 /* If we've been reading from redirected input, the
2277 frontend will pop the buffer. */
2278 return CPP_EOF;
2279 else if (CPP_BUFFER (pfile)->seen_eof)
2281 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile))
2282 return CPP_EOF;
2284 cpp_pop_buffer (pfile);
2285 goto get_next;
2287 else
2289 cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2290 struct if_stack *ifs, *nifs;
2292 /* Unwind the conditional stack and generate error messages. */
2293 for (ifs = pfile->if_stack;
2294 ifs != CPP_BUFFER (pfile)->if_stack;
2295 ifs = nifs)
2297 cpp_error_with_line (pfile, ifs->lineno, -1,
2298 "unterminated `%s' conditional",
2299 if_directive_name (pfile, ifs));
2301 nifs = ifs->next;
2302 free (ifs);
2304 pfile->if_stack = ifs;
2306 if (CPP_BUFFER (pfile)->nominal_fname
2307 && next_buf != CPP_NULL_BUFFER (pfile))
2309 /* We're about to return from an #include file.
2310 Emit #line information now (as part of the CPP_POP) result.
2311 But the #line refers to the file we will pop to. */
2312 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2313 CPP_BUFFER (pfile) = next_buf;
2314 pfile->input_stack_listing_current = 0;
2315 output_line_command (pfile, leave_file);
2316 CPP_BUFFER (pfile) = cur_buffer;
2319 CPP_BUFFER (pfile)->seen_eof = 1;
2320 return CPP_POP;
2323 else
2325 switch (c)
2327 case '/':
2328 if (PEEKC () == '=')
2329 goto op2;
2331 comment:
2332 if (opts->put_out_comments)
2333 c = copy_comment (pfile, c);
2334 else
2335 c = skip_comment (pfile, c);
2336 if (c == EOF)
2337 goto handle_eof;
2338 else if (c != ' ')
2339 goto randomchar;
2341 /* Comments are equivalent to spaces.
2342 For -traditional, a comment is equivalent to nothing. */
2343 if (opts->traditional || opts->put_out_comments)
2344 return CPP_COMMENT;
2345 else
2347 CPP_PUTC (pfile, c);
2348 return CPP_HSPACE;
2350 #if 0
2351 if (opts->for_lint) {
2352 U_CHAR *argbp;
2353 int cmdlen, arglen;
2354 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
2356 if (lintcmd != NULL) {
2357 /* I believe it is always safe to emit this newline: */
2358 obp[-1] = '\n';
2359 bcopy ("#pragma lint ", (char *) obp, 13);
2360 obp += 13;
2361 bcopy (lintcmd, (char *) obp, cmdlen);
2362 obp += cmdlen;
2364 if (arglen != 0) {
2365 *(obp++) = ' ';
2366 bcopy (argbp, (char *) obp, arglen);
2367 obp += arglen;
2370 /* OK, now bring us back to the state we were in before we entered
2371 this branch. We need #line because the newline for the pragma
2372 could mess things up. */
2373 output_line_command (pfile, same_file);
2374 *(obp++) = ' '; /* just in case, if comments are copied thru */
2375 *(obp++) = '/';
2378 #endif
2380 case '#':
2381 #if 0
2382 /* If this is expanding a macro definition, don't recognize
2383 preprocessor directives. */
2384 if (ip->macro != 0)
2385 goto randomchar;
2386 /* If this is expand_into_temp_buffer, recognize them
2387 only after an actual newline at this level,
2388 not at the beginning of the input level. */
2389 if (ip->fname == 0 && beg_of_line == ip->buf)
2390 goto randomchar;
2391 if (ident_length)
2392 goto specialchar;
2393 #endif
2395 if (!pfile->only_seen_white)
2396 goto randomchar;
2397 if (handle_directive (pfile))
2398 return CPP_DIRECTIVE;
2399 pfile->only_seen_white = 0;
2400 return CPP_OTHER;
2402 case '\"':
2403 case '\'':
2404 string:
2405 parse_string (pfile, c);
2406 pfile->only_seen_white = 0;
2407 return c == '\'' ? CPP_CHAR : CPP_STRING;
2409 case '$':
2410 if (!opts->dollars_in_ident)
2411 goto randomchar;
2412 goto letter;
2414 case ':':
2415 if (opts->cplusplus && PEEKC () == ':')
2416 goto op2;
2417 goto randomchar;
2419 case '&':
2420 case '+':
2421 case '|':
2422 c2 = PEEKC ();
2423 if (c2 == c || c2 == '=')
2424 goto op2;
2425 goto randomchar;
2427 case '*':
2428 case '!':
2429 case '%':
2430 case '=':
2431 case '^':
2432 if (PEEKC () == '=')
2433 goto op2;
2434 goto randomchar;
2436 case '-':
2437 c2 = PEEKC ();
2438 if (c2 == '-' && opts->chill)
2439 goto comment; /* Chill style comment */
2440 if (c2 == '-' || c2 == '=')
2441 goto op2;
2442 if (c2 == '>')
2444 if (opts->cplusplus && PEEKN (1) == '*')
2446 /* In C++, there's a ->* operator. */
2447 token = CPP_OTHER;
2448 pfile->only_seen_white = 0;
2449 CPP_RESERVE (pfile, 4);
2450 CPP_PUTC_Q (pfile, c);
2451 CPP_PUTC_Q (pfile, GETC ());
2452 CPP_PUTC_Q (pfile, GETC ());
2453 CPP_NUL_TERMINATE_Q (pfile);
2454 return token;
2456 goto op2;
2458 goto randomchar;
2460 case '<':
2461 if (pfile->parsing_include_directive)
2463 for (;;)
2465 CPP_PUTC (pfile, c);
2466 if (c == '>')
2467 break;
2468 c = GETC ();
2469 if (c == '\n' || c == EOF)
2471 cpp_error (pfile,
2472 "missing '>' in `#include <FILENAME>'");
2473 break;
2475 else if (c == '\r')
2477 if (!CPP_BUFFER (pfile)->has_escapes)
2479 /* Backslash newline is replaced by nothing. */
2480 CPP_ADJUST_WRITTEN (pfile, -1);
2481 CPP_BUMP_LINE (pfile);
2483 else
2485 /* We might conceivably get \r- or \r<space> in
2486 here. Just delete 'em. */
2487 int d = GETC();
2488 if (d != '-' && d != ' ')
2489 cpp_fatal (pfile,
2490 "internal error: unrecognized escape \\r%c",
2492 CPP_ADJUST_WRITTEN (pfile, -1);
2496 return CPP_STRING;
2498 /* else fall through */
2499 case '>':
2500 c2 = PEEKC ();
2501 if (c2 == '=')
2502 goto op2;
2503 /* GNU C++ supports MIN and MAX operators <? and >?. */
2504 if (c2 != c && (!opts->cplusplus || c2 != '?'))
2505 goto randomchar;
2506 FORWARD(1);
2507 CPP_RESERVE (pfile, 4);
2508 CPP_PUTC (pfile, c);
2509 CPP_PUTC (pfile, c2);
2510 c3 = PEEKC ();
2511 if (c3 == '=')
2512 CPP_PUTC_Q (pfile, GETC ());
2513 CPP_NUL_TERMINATE_Q (pfile);
2514 pfile->only_seen_white = 0;
2515 return CPP_OTHER;
2517 case '.':
2518 c2 = PEEKC ();
2519 if (ISDIGIT(c2))
2521 CPP_RESERVE(pfile, 2);
2522 CPP_PUTC_Q (pfile, '.');
2523 c = GETC ();
2524 goto number;
2527 /* In C++ there's a .* operator. */
2528 if (opts->cplusplus && c2 == '*')
2529 goto op2;
2531 if (c2 == '.' && PEEKN(1) == '.')
2533 CPP_RESERVE(pfile, 4);
2534 CPP_PUTC_Q (pfile, '.');
2535 CPP_PUTC_Q (pfile, '.');
2536 CPP_PUTC_Q (pfile, '.');
2537 FORWARD (2);
2538 CPP_NUL_TERMINATE_Q (pfile);
2539 pfile->only_seen_white = 0;
2540 return CPP_3DOTS;
2542 goto randomchar;
2544 op2:
2545 token = CPP_OTHER;
2546 pfile->only_seen_white = 0;
2547 CPP_RESERVE(pfile, 3);
2548 CPP_PUTC_Q (pfile, c);
2549 CPP_PUTC_Q (pfile, GETC ());
2550 CPP_NUL_TERMINATE_Q (pfile);
2551 return token;
2553 case 'L':
2554 c2 = PEEKC ();
2555 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2557 CPP_PUTC (pfile, c);
2558 c = GETC ();
2559 goto string;
2561 goto letter;
2563 case '0': case '1': case '2': case '3': case '4':
2564 case '5': case '6': case '7': case '8': case '9':
2565 number:
2566 c2 = '.';
2567 for (;;)
2569 CPP_RESERVE (pfile, 2);
2570 CPP_PUTC_Q (pfile, c);
2571 c = PEEKC ();
2572 if (c == EOF)
2573 break;
2574 if (!is_idchar[c] && c != '.'
2575 && ((c2 != 'e' && c2 != 'E'
2576 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
2577 || (c != '+' && c != '-')))
2578 break;
2579 FORWARD(1);
2580 c2= c;
2582 CPP_NUL_TERMINATE_Q (pfile);
2583 pfile->only_seen_white = 0;
2584 return CPP_NUMBER;
2585 case 'b': case 'c': case 'd': case 'h': case 'o':
2586 case 'B': case 'C': case 'D': case 'H': case 'O':
2587 if (opts->chill && PEEKC () == '\'')
2589 pfile->only_seen_white = 0;
2590 CPP_RESERVE (pfile, 2);
2591 CPP_PUTC_Q (pfile, c);
2592 CPP_PUTC_Q (pfile, '\'');
2593 FORWARD(1);
2594 for (;;)
2596 c = GETC();
2597 if (c == EOF)
2598 goto chill_number_eof;
2599 if (!is_idchar[c])
2600 break;
2601 CPP_PUTC (pfile, c);
2603 if (c == '\'')
2605 CPP_RESERVE (pfile, 2);
2606 CPP_PUTC_Q (pfile, c);
2607 CPP_NUL_TERMINATE_Q (pfile);
2608 return CPP_STRING;
2610 else
2612 FORWARD(-1);
2613 chill_number_eof:
2614 CPP_NUL_TERMINATE (pfile);
2615 return CPP_NUMBER;
2618 else
2619 goto letter;
2620 case '_':
2621 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2622 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2623 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2624 case 'x': case 'y': case 'z':
2625 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2626 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2627 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2628 case 'Y': case 'Z':
2629 letter:
2631 HASHNODE *hp;
2632 unsigned char *ident;
2633 int before_name_written = CPP_WRITTEN (pfile);
2634 int ident_len;
2635 parse_name (pfile, c);
2636 pfile->only_seen_white = 0;
2637 if (pfile->no_macro_expand)
2638 return CPP_NAME;
2639 ident = pfile->token_buffer + before_name_written;
2640 ident_len = CPP_PWRITTEN (pfile) - ident;
2641 hp = cpp_lookup (pfile, ident, ident_len, -1);
2642 if (!hp)
2643 return CPP_NAME;
2644 if (hp->type == T_DISABLED)
2646 if (pfile->output_escapes)
2647 { /* Return "\r-IDENT", followed by '\0'. */
2648 int i;
2649 CPP_RESERVE (pfile, 3);
2650 ident = pfile->token_buffer + before_name_written;
2651 CPP_ADJUST_WRITTEN (pfile, 2);
2652 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2653 ident[0] = '\r';
2654 ident[1] = '-';
2656 return CPP_NAME;
2659 /* If macro wants an arglist, verify that a '(' follows.
2660 first skip all whitespace, copying it to the output
2661 after the macro name. Then, if there is no '(',
2662 decide this is not a macro call and leave things that way. */
2663 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2665 int is_macro_call, macbuf_whitespace = 0;
2667 parse_set_mark (pfile);
2668 for (;;)
2670 cpp_skip_hspace (pfile);
2671 c = PEEKC ();
2672 is_macro_call = c == '(';
2673 if (c != EOF)
2675 if (c != '\n')
2676 break;
2677 FORWARD (1);
2679 else
2681 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2683 if (CPP_BUFFER (pfile)->mark !=
2684 (CPP_BUFFER (pfile)->cur
2685 - CPP_BUFFER (pfile)->buf))
2686 macbuf_whitespace = 1;
2688 /* The mark goes away automatically when
2689 the buffer is popped. */
2690 cpp_pop_buffer (pfile);
2691 parse_set_mark (pfile);
2693 else
2694 break;
2697 if (!is_macro_call)
2699 parse_goto_mark (pfile);
2700 if (macbuf_whitespace)
2701 CPP_PUTC (pfile, ' ');
2703 else
2704 parse_clear_mark (pfile);
2705 if (!is_macro_call)
2706 return CPP_NAME;
2708 /* This is now known to be a macro call.
2709 Expand the macro, reading arguments as needed,
2710 and push the expansion on the input stack. */
2711 macroexpand (pfile, hp);
2712 CPP_SET_WRITTEN (pfile, before_name_written);
2714 goto get_next;
2716 case ' ': case '\t': case '\v':
2717 for (;;)
2719 CPP_PUTC (pfile, c);
2720 c = PEEKC ();
2721 if (c == EOF || !is_hor_space[c])
2722 break;
2723 FORWARD(1);
2725 return CPP_HSPACE;
2727 case '\r':
2728 if (CPP_BUFFER (pfile)->has_escapes)
2730 c = GETC ();
2731 if (c == '-')
2733 if (pfile->output_escapes)
2734 CPP_PUTS (pfile, "\r-", 2);
2735 parse_name (pfile, GETC ());
2736 return CPP_NAME;
2738 else if (c == ' ')
2740 CPP_RESERVE (pfile, 2);
2741 if (pfile->output_escapes)
2742 CPP_PUTC_Q (pfile, '\r');
2743 CPP_PUTC_Q (pfile, c);
2744 return CPP_HSPACE;
2746 else
2748 cpp_fatal (pfile,
2749 "internal error: unrecognized escape \\r%c", c);
2750 goto get_next;
2753 else
2755 /* Backslash newline is ignored. */
2756 CPP_BUMP_LINE (pfile);
2757 goto get_next;
2760 case '\n':
2761 CPP_PUTC (pfile, c);
2762 if (pfile->only_seen_white == 0)
2763 pfile->only_seen_white = 1;
2764 CPP_BUMP_LINE (pfile);
2765 if (! CPP_OPTIONS (pfile)->no_line_commands)
2767 pfile->lineno++;
2768 if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2769 output_line_command (pfile, same_file);
2771 return CPP_VSPACE;
2773 case '(': token = CPP_LPAREN; goto char1;
2774 case ')': token = CPP_RPAREN; goto char1;
2775 case '{': token = CPP_LBRACE; goto char1;
2776 case '}': token = CPP_RBRACE; goto char1;
2777 case ',': token = CPP_COMMA; goto char1;
2778 case ';': token = CPP_SEMICOLON; goto char1;
2780 randomchar:
2781 default:
2782 token = CPP_OTHER;
2783 char1:
2784 pfile->only_seen_white = 0;
2785 CPP_PUTC (pfile, c);
2786 return token;
2791 /* Like cpp_get_token, but skip spaces and comments. */
2793 enum cpp_token
2794 cpp_get_non_space_token (pfile)
2795 cpp_reader *pfile;
2797 int old_written = CPP_WRITTEN (pfile);
2798 for (;;)
2800 enum cpp_token token = cpp_get_token (pfile);
2801 if (token != CPP_COMMENT && token != CPP_POP
2802 && token != CPP_HSPACE && token != CPP_VSPACE)
2803 return token;
2804 CPP_SET_WRITTEN (pfile, old_written);
2808 /* Parse an identifier starting with C. */
2810 static void
2811 parse_name (pfile, c)
2812 cpp_reader *pfile;
2813 int c;
2815 for (;;)
2817 if (! is_idchar[c])
2819 FORWARD (-1);
2820 break;
2823 if (c == '$' && CPP_PEDANTIC (pfile))
2824 cpp_pedwarn (pfile, "`$' in identifier");
2826 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
2827 CPP_PUTC_Q (pfile, c);
2828 c = GETC();
2829 if (c == EOF)
2830 break;
2832 CPP_NUL_TERMINATE_Q (pfile);
2833 return;
2836 /* Parse a string starting with C. A single quoted string is treated
2837 like a double -- some programs (e.g., troff) are perverse this way.
2838 (However, a single quoted string is not allowed to extend over
2839 multiple lines.) */
2840 static void
2841 parse_string (pfile, c)
2842 cpp_reader *pfile;
2843 int c;
2845 long start_line, start_column;
2847 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2849 CPP_PUTC (pfile, c);
2850 while (1)
2852 int cc = GETC();
2853 if (cc == EOF)
2855 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2857 /* try harder: this string crosses a macro expansion
2858 boundary. This can happen naturally if -traditional.
2859 Otherwise, only -D can make a macro with an unmatched
2860 quote. */
2861 cpp_pop_buffer (pfile);
2862 continue;
2865 cpp_error_with_line (pfile, start_line, start_column,
2866 "unterminated string or character constant");
2867 if (pfile->multiline_string_line != start_line
2868 && pfile->multiline_string_line != 0)
2869 cpp_error_with_line (pfile,
2870 pfile->multiline_string_line, -1,
2871 "possible real start of unterminated constant");
2872 pfile->multiline_string_line = 0;
2873 break;
2875 CPP_PUTC (pfile, cc);
2876 switch (cc)
2878 case '\n':
2879 CPP_BUMP_LINE (pfile);
2880 pfile->lineno++;
2881 /* Character constants may not extend over multiple lines.
2882 In ANSI, neither may strings. We accept multiline strings
2883 as an extension. */
2884 if (c == '\'')
2886 cpp_error_with_line (pfile, start_line, start_column,
2887 "unterminated character constant");
2888 return;
2890 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2892 cpp_pedwarn_with_line (pfile, start_line, start_column,
2893 "string constant runs past end of line");
2895 if (pfile->multiline_string_line == 0)
2896 pfile->multiline_string_line = start_line;
2897 break;
2899 case '\r':
2900 CPP_ADJUST_WRITTEN (pfile, -1);
2901 if (CPP_BUFFER (pfile)->has_escapes)
2903 cpp_fatal (pfile,
2904 "internal error: \\r escape inside string constant");
2905 FORWARD(1);
2907 else
2908 /* Backslash newline is replaced by nothing at all. */
2909 CPP_BUMP_LINE (pfile);
2910 break;
2912 case '\\':
2913 cc = GETC();
2914 if (cc != EOF)
2915 CPP_PUTC (pfile, cc);
2916 break;
2918 case '\"':
2919 case '\'':
2920 if (cc == c)
2921 return;
2922 break;
2927 /* Read an assertion into the token buffer, converting to
2928 canonical form: `#predicate(a n swe r)' The next non-whitespace
2929 character to read should be the first letter of the predicate.
2930 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2931 with answer (see callers for why). In case of 0, an error has been
2932 printed. */
2933 static int
2934 parse_assertion (pfile)
2935 cpp_reader *pfile;
2937 int c, dropwhite;
2938 cpp_skip_hspace (pfile);
2939 c = PEEKC();
2940 if (! is_idstart[c])
2942 cpp_error (pfile, "assertion predicate is not an identifier");
2943 return 0;
2945 CPP_PUTC(pfile, '#');
2946 FORWARD(1);
2947 parse_name(pfile, c);
2949 c = PEEKC();
2950 if (c != '(')
2952 if (is_hor_space[c] || c == '\r')
2953 cpp_skip_hspace (pfile);
2954 c = PEEKC();
2956 if (c != '(')
2957 return 1;
2959 CPP_PUTC(pfile, '(');
2960 FORWARD(1);
2961 dropwhite = 1;
2962 while ((c = GETC()) != ')')
2964 if (is_hor_space[c])
2966 if (! dropwhite)
2968 CPP_PUTC(pfile, ' ');
2969 dropwhite = 1;
2972 else if (c == '\n' || c == EOF)
2974 if (c == '\n') FORWARD(-1);
2975 cpp_error (pfile, "un-terminated assertion answer");
2976 return 0;
2978 else if (c == '\r')
2979 /* \r cannot be a macro escape here. */
2980 CPP_BUMP_LINE (pfile);
2981 else
2983 CPP_PUTC (pfile, c);
2984 dropwhite = 0;
2988 if (pfile->limit[-1] == ' ')
2989 pfile->limit[-1] = ')';
2990 else if (pfile->limit[-1] == '(')
2992 cpp_error (pfile, "empty token sequence in assertion");
2993 return 0;
2995 else
2996 CPP_PUTC (pfile, ')');
2998 CPP_NUL_TERMINATE (pfile);
2999 return 2;
3002 static int
3003 do_assert (pfile, keyword)
3004 cpp_reader *pfile;
3005 const struct directive *keyword ATTRIBUTE_UNUSED;
3007 char *sym;
3008 int ret, c;
3009 HASHNODE *base, *this;
3010 int baselen, thislen;
3012 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3013 && !CPP_BUFFER (pfile)->system_header_p)
3014 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
3016 cpp_skip_hspace (pfile);
3017 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
3018 ret = parse_assertion (pfile);
3019 if (ret == 0)
3020 goto error;
3021 else if (ret == 1)
3023 cpp_error (pfile, "missing token-sequence in `#assert'");
3024 goto error;
3027 cpp_skip_hspace (pfile);
3028 c = PEEKC();
3029 if (c != EOF && c != '\n')
3031 cpp_error (pfile, "junk at end of `#assert'");
3032 goto error;
3035 thislen = strlen (sym);
3036 baselen = index (sym, '(') - sym;
3037 this = cpp_lookup (pfile, sym, thislen, -1);
3038 if (this)
3040 cpp_warning (pfile, "`%s' re-asserted", sym);
3041 goto error;
3044 base = cpp_lookup (pfile, sym, baselen, -1);
3045 if (! base)
3046 base = cpp_install (pfile, sym, baselen, T_ASSERT, 0, -1);
3047 else if (base->type != T_ASSERT)
3049 /* Token clash - but with what?! */
3050 cpp_fatal (pfile,
3051 "cpp internal error: base->type != T_ASSERT in do_assert");
3052 goto error;
3055 this = cpp_install (pfile, sym, thislen, T_ASSERT,
3056 (char *)base->value.aschain, -1);
3057 base->value.aschain = this;
3059 pfile->limit = (unsigned char *) sym; /* Pop */
3060 return 0;
3062 error:
3063 skip_rest_of_line (pfile);
3064 pfile->limit = (unsigned char *) sym; /* Pop */
3065 return 0;
3068 static int
3069 do_unassert (pfile, keyword)
3070 cpp_reader *pfile;
3071 const struct directive *keyword ATTRIBUTE_UNUSED;
3073 int c, ret;
3074 char *sym;
3075 long baselen, thislen;
3076 HASHNODE *base, *this, *next;
3078 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3079 && !CPP_BUFFER (pfile)->system_header_p)
3080 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
3082 cpp_skip_hspace (pfile);
3084 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
3085 ret = parse_assertion (pfile);
3086 if (ret == 0)
3087 goto error;
3089 cpp_skip_hspace (pfile);
3090 c = PEEKC ();
3091 if (c != EOF && c != '\n')
3092 cpp_error (pfile, "junk at end of `#unassert'");
3094 thislen = strlen (sym);
3095 if (ret == 1)
3097 base = cpp_lookup (pfile, sym, thislen, -1);
3098 if (! base)
3099 goto error; /* It isn't an error to #undef what isn't #defined,
3100 so it isn't an error to #unassert what isn't
3101 #asserted either. */
3103 for (this = base->value.aschain; this; this = next)
3105 next = this->value.aschain;
3106 delete_macro (this);
3108 delete_macro (base);
3110 else
3112 baselen = index (sym, '(') - sym;
3113 base = cpp_lookup (pfile, sym, baselen, -1);
3114 if (! base) goto error;
3115 this = cpp_lookup (pfile, sym, thislen, -1);
3116 if (! this) goto error;
3118 next = base;
3119 while (next->value.aschain != this)
3120 next = next->value.aschain;
3122 next->value.aschain = this->value.aschain;
3123 delete_macro (this);
3125 if (base->value.aschain == NULL)
3126 delete_macro (base); /* Last answer for this predicate deleted. */
3129 pfile->limit = (unsigned char *) sym; /* Pop */
3130 return 0;
3131 error:
3132 skip_rest_of_line (pfile);
3133 pfile->limit = (unsigned char *) sym; /* Pop */
3134 return 0;
3137 /* Process STR as if it appeared as the body of an #unassert. */
3138 void
3139 cpp_unassert (pfile, str)
3140 cpp_reader *pfile;
3141 unsigned char *str;
3143 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
3145 do_assert (pfile, NULL);
3146 cpp_pop_buffer (pfile);
3151 cpp_read_check_assertion (pfile)
3152 cpp_reader *pfile;
3154 U_CHAR *name = CPP_PWRITTEN (pfile);
3155 int result;
3156 HASHNODE *hp;
3158 FORWARD (1); /* Skip '#' */
3159 cpp_skip_hspace (pfile);
3160 if (! parse_assertion (pfile))
3161 result = 0;
3162 else
3164 hp = cpp_lookup (pfile, name, CPP_PWRITTEN (pfile) - name, -1);
3165 result = (hp != 0);
3168 pfile->limit = name;
3169 return result;
3172 /* Remember the current position of PFILE. */
3174 void
3175 parse_set_mark (pfile)
3176 cpp_reader *pfile;
3178 cpp_buffer *ip = CPP_BUFFER (pfile);
3179 if (ip->mark != -1)
3180 cpp_fatal (pfile,
3181 "cpp internal error: ip->mark != -1 in parse_set_mark");
3183 ip->mark = ip->cur - ip->buf;
3186 /* Clear the current mark - we no longer need it. */
3188 void
3189 parse_clear_mark (pfile)
3190 cpp_reader *pfile;
3192 cpp_buffer *ip = CPP_BUFFER (pfile);
3193 if (ip->mark == -1)
3194 cpp_fatal (pfile,
3195 "cpp internal error: ip->mark == -1 in parse_clear_mark");
3197 ip->mark = -1;
3200 /* Backup the current position of PFILE to that saved in its mark,
3201 and clear the mark. */
3203 void
3204 parse_goto_mark (pfile)
3205 cpp_reader *pfile;
3207 cpp_buffer *ip = CPP_BUFFER (pfile);
3208 if (ip->mark == -1)
3209 cpp_fatal (pfile,
3210 "cpp internal error: ip->mark == -1 in parse_goto_mark");
3212 ip->cur = ip->buf + ip->mark;
3213 ip->mark = -1;
3216 static void
3217 cpp_print_file_and_line (pfile)
3218 cpp_reader *pfile;
3220 cpp_buffer *ip = cpp_file_buffer (pfile);
3222 if (ip != NULL)
3224 long line, col;
3225 cpp_buf_line_and_col (ip, &line, &col);
3226 cpp_file_line_for_message (pfile, ip->nominal_fname,
3227 line, pfile->show_column ? col : -1);
3231 static void
3232 v_cpp_error (pfile, msgid, ap)
3233 cpp_reader *pfile;
3234 const char *msgid;
3235 va_list ap;
3237 cpp_print_containing_files (pfile);
3238 cpp_print_file_and_line (pfile);
3239 v_cpp_message (pfile, 1, msgid, ap);
3242 void
3243 cpp_error VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3245 #ifndef ANSI_PROTOTYPES
3246 cpp_reader *pfile;
3247 const char *msgid;
3248 #endif
3249 va_list ap;
3251 VA_START(ap, msgid);
3253 #ifndef ANSI_PROTOTYPES
3254 pfile = va_arg (ap, cpp_reader *);
3255 msgid = va_arg (ap, const char *);
3256 #endif
3258 v_cpp_error (pfile, msgid, ap);
3259 va_end(ap);
3262 /* Print error message but don't count it. */
3264 static void
3265 v_cpp_warning (pfile, msgid, ap)
3266 cpp_reader *pfile;
3267 const char *msgid;
3268 va_list ap;
3270 if (CPP_OPTIONS (pfile)->inhibit_warnings)
3271 return;
3273 if (CPP_OPTIONS (pfile)->warnings_are_errors)
3274 pfile->errors++;
3276 cpp_print_containing_files (pfile);
3277 cpp_print_file_and_line (pfile);
3278 v_cpp_message (pfile, 0, msgid, ap);
3281 void
3282 cpp_warning VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3284 #ifndef ANSI_PROTOTYPES
3285 cpp_reader *pfile;
3286 const char *msgid;
3287 #endif
3288 va_list ap;
3290 VA_START (ap, msgid);
3292 #ifndef ANSI_PROTOTYPES
3293 pfile = va_arg (ap, cpp_reader *);
3294 msgid = va_arg (ap, const char *);
3295 #endif
3297 v_cpp_warning (pfile, msgid, ap);
3298 va_end(ap);
3301 /* Print an error message and maybe count it. */
3303 void
3304 cpp_pedwarn VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3306 #ifndef ANSI_PROTOTYPES
3307 cpp_reader *pfile;
3308 const char *msgid;
3309 #endif
3310 va_list ap;
3312 VA_START (ap, msgid);
3314 #ifndef ANSI_PROTOTYPES
3315 pfile = va_arg (ap, cpp_reader *);
3316 msgid = va_arg (ap, const char *);
3317 #endif
3319 if (CPP_OPTIONS (pfile)->pedantic_errors)
3320 v_cpp_error (pfile, msgid, ap);
3321 else
3322 v_cpp_warning (pfile, msgid, ap);
3323 va_end(ap);
3326 static void
3327 v_cpp_error_with_line (pfile, line, column, msgid, ap)
3328 cpp_reader * pfile;
3329 int line;
3330 int column;
3331 const char * msgid;
3332 va_list ap;
3334 cpp_buffer *ip = cpp_file_buffer (pfile);
3336 cpp_print_containing_files (pfile);
3338 if (ip != NULL)
3339 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
3341 v_cpp_message (pfile, 1, msgid, ap);
3344 void
3345 cpp_error_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3346 const char *msgid, ...))
3348 #ifndef ANSI_PROTOTYPES
3349 cpp_reader *pfile;
3350 int line;
3351 int column;
3352 const char *msgid;
3353 #endif
3354 va_list ap;
3356 VA_START (ap, msgid);
3358 #ifndef ANSI_PROTOTYPES
3359 pfile = va_arg (ap, cpp_reader *);
3360 line = va_arg (ap, int);
3361 column = va_arg (ap, int);
3362 msgid = va_arg (ap, const char *);
3363 #endif
3365 v_cpp_error_with_line(pfile, line, column, msgid, ap);
3366 va_end(ap);
3369 static void
3370 v_cpp_warning_with_line (pfile, line, column, msgid, ap)
3371 cpp_reader * pfile;
3372 int line;
3373 int column;
3374 const char *msgid;
3375 va_list ap;
3377 cpp_buffer *ip;
3379 if (CPP_OPTIONS (pfile)->inhibit_warnings)
3380 return;
3382 if (CPP_OPTIONS (pfile)->warnings_are_errors)
3383 pfile->errors++;
3385 cpp_print_containing_files (pfile);
3387 ip = cpp_file_buffer (pfile);
3389 if (ip != NULL)
3390 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
3392 v_cpp_message (pfile, 0, msgid, ap);
3395 void
3396 cpp_warning_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3397 const char *msgid, ...))
3399 #ifndef ANSI_PROTOTYPES
3400 cpp_reader *pfile;
3401 int line;
3402 int column;
3403 const char *msgid;
3404 #endif
3405 va_list ap;
3407 VA_START (ap, msgid);
3409 #ifndef ANSI_PROTOTYPES
3410 pfile = va_arg (ap, cpp_reader *);
3411 line = va_arg (ap, int);
3412 column = va_arg (ap, int);
3413 msgid = va_arg (ap, const char *);
3414 #endif
3416 v_cpp_warning_with_line (pfile, line, column, msgid, ap);
3417 va_end(ap);
3420 void
3421 cpp_pedwarn_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3422 const char *msgid, ...))
3424 #ifndef ANSI_PROTOTYPES
3425 cpp_reader *pfile;
3426 int line;
3427 int column;
3428 const char *msgid;
3429 #endif
3430 va_list ap;
3432 VA_START (ap, msgid);
3434 #ifndef ANSI_PROTOTYPES
3435 pfile = va_arg (ap, cpp_reader *);
3436 line = va_arg (ap, int);
3437 column = va_arg (ap, int);
3438 msgid = va_arg (ap, const char *);
3439 #endif
3441 if (CPP_OPTIONS (pfile)->pedantic_errors)
3442 v_cpp_error_with_line (pfile, column, line, msgid, ap);
3443 else
3444 v_cpp_warning_with_line (pfile, line, column, msgid, ap);
3445 va_end(ap);
3448 /* Report a warning (or an error if pedantic_errors)
3449 giving specified file name and line number, not current. */
3451 void
3452 cpp_pedwarn_with_file_and_line VPROTO ((cpp_reader *pfile, const char *file,
3453 int line, const char *msgid, ...))
3455 #ifndef ANSI_PROTOTYPES
3456 cpp_reader *pfile;
3457 const char *file;
3458 int line;
3459 const char *msgid;
3460 #endif
3461 va_list ap;
3463 VA_START (ap, msgid);
3465 #ifndef ANSI_PROTOTYPES
3466 pfile = va_arg (ap, cpp_reader *);
3467 file = va_arg (ap, const char *);
3468 line = va_arg (ap, int);
3469 msgid = va_arg (ap, const char *);
3470 #endif
3472 if (!CPP_OPTIONS (pfile)->pedantic_errors
3473 && CPP_OPTIONS (pfile)->inhibit_warnings)
3474 return;
3475 if (file != NULL)
3476 cpp_file_line_for_message (pfile, file, line, -1);
3477 v_cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, msgid, ap);
3478 va_end(ap);
3481 /* my_strerror - return the descriptive text associated with an
3482 `errno' code. */
3484 static const char *
3485 my_strerror (errnum)
3486 int errnum;
3488 const char *result;
3490 #ifndef VMS
3491 #ifndef HAVE_STRERROR
3492 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
3493 #else
3494 result = strerror (errnum);
3495 #endif
3496 #else /* VMS */
3497 /* VAXCRTL's strerror() takes an optional second argument, which only
3498 matters when the first argument is EVMSERR. However, it's simplest
3499 just to pass it unconditionally. `vaxc$errno' is declared in
3500 <errno.h>, and maintained by the library in parallel with `errno'.
3501 We assume that caller's `errnum' either matches the last setting of
3502 `errno' by the library or else does not have the value `EVMSERR'. */
3504 result = strerror (errnum, vaxc$errno);
3505 #endif
3507 if (!result)
3508 result = "errno = ?";
3510 return result;
3513 /* Error including a message from `errno'. */
3515 void
3516 cpp_error_from_errno (pfile, name)
3517 cpp_reader *pfile;
3518 const char *name;
3520 cpp_message_from_errno (pfile, 1, name);
3523 void
3524 cpp_message_from_errno (pfile, is_error, name)
3525 cpp_reader *pfile;
3526 int is_error;
3527 const char *name;
3529 int e = errno;
3530 cpp_buffer *ip = cpp_file_buffer (pfile);
3532 cpp_print_containing_files (pfile);
3534 if (ip != NULL)
3535 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
3537 cpp_message (pfile, is_error, "%s: %s", name, my_strerror (e));
3540 void
3541 cpp_perror_with_name (pfile, name)
3542 cpp_reader *pfile;
3543 const char *name;
3545 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
3548 /* TODO:
3549 * No pre-compiled header file support.
3551 * Possibly different enum token codes for each C/C++ token.
3553 * Find and cleanup remaining uses of static variables,
3555 * Support -dM flag (dump_all_macros).
3557 * Support for_lint flag.