Add FR30 to list of targets for which -fpic is inappropriate.
[official-gcc.git] / gcc / cpplib.c
blobb31518612184e3cda1756fce339766f2970ae7c6
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. */
532 if (CPP_PEDANTIC (pfile))
533 cpp_pedwarn (pfile, "`#' followed by integer");
534 do_line (pfile, NULL);
535 return 1;
538 /* Now find the directive name. */
539 CPP_PUTC (pfile, '#');
540 parse_name (pfile, GETC());
541 ident = pfile->token_buffer + old_written + 1;
542 ident_length = CPP_PWRITTEN (pfile) - ident;
543 if (ident_length == 0)
545 /* A line of just `#' becomes blank. */
546 if (PEEKC() == '\n')
547 return 1;
548 else
549 return 0;
553 * Decode the keyword and call the appropriate expansion
554 * routine, after moving the input pointer up to the next line.
556 for (kt = directive_table; ; kt++)
558 if (kt->length <= 0)
559 return 0;
560 if (kt->length == ident_length
561 && !strncmp (kt->name, ident, ident_length))
562 break;
565 CPP_SET_WRITTEN (pfile, old_written);
566 (*kt->func) (pfile, kt);
568 return 1;
571 /* Pass a directive through to the output file.
572 BUF points to the contents of the directive, as a contiguous string.
573 LEN is the length of the string pointed to by BUF.
574 KEYWORD is the keyword-table entry for the directive. */
576 static void
577 pass_thru_directive (buf, len, pfile, keyword)
578 const U_CHAR *buf;
579 size_t len;
580 cpp_reader *pfile;
581 const struct directive *keyword;
583 register unsigned keyword_length = keyword->length;
585 CPP_RESERVE (pfile, 1 + keyword_length + len);
586 CPP_PUTC_Q (pfile, '#');
587 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
588 if (len != 0 && buf[0] != ' ')
589 CPP_PUTC_Q (pfile, ' ');
590 CPP_PUTS_Q (pfile, buf, len);
593 /* Check a purported macro name SYMNAME, and yield its length.
594 ASSERTION is nonzero if this is really for an assertion name. */
597 check_macro_name (pfile, symname, assertion)
598 cpp_reader *pfile;
599 const U_CHAR *symname;
600 int assertion;
602 const U_CHAR *p;
603 int sym_length;
605 for (p = symname; is_idchar[*p]; p++)
607 sym_length = p - symname;
608 if (sym_length == 0
609 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
610 cpp_error (pfile,
611 assertion ? "invalid assertion name" : "invalid macro name");
612 else if (!is_idstart[*symname]
613 || (! strncmp (symname, "defined", 7) && sym_length == 7)) {
614 U_CHAR *msg; /* what pain... */
615 msg = (U_CHAR *) alloca (sym_length + 1);
616 bcopy (symname, msg, sym_length);
617 msg[sym_length] = 0;
618 cpp_error (pfile,
619 (assertion
620 ? "invalid assertion name `%s'"
621 : "invalid macro name `%s'"),
622 msg);
624 return sym_length;
627 /* Process a #define command.
628 KEYWORD is the keyword-table entry for #define,
629 or NULL for a "predefined" macro,
630 or the keyword-table entry for #pragma in the case of a #pragma poison. */
632 static int
633 do_define (pfile, keyword)
634 cpp_reader *pfile;
635 const struct directive *keyword;
637 int hashcode;
638 MACRODEF mdef;
639 HASHNODE *hp;
640 long here;
641 U_CHAR *macro, *buf, *end;
642 enum node_type new_type;
644 here = CPP_WRITTEN (pfile);
645 copy_rest_of_line (pfile);
647 if (keyword == NULL || keyword->type == T_DEFINE)
648 new_type = T_MACRO;
649 else
650 new_type = T_POISON;
652 /* Copy out the line so we can pop the token buffer. */
653 buf = pfile->token_buffer + here;
654 end = CPP_PWRITTEN (pfile);
655 macro = (U_CHAR *) alloca (end - buf + 1);
656 bcopy (buf, macro, end - buf + 1);
657 end = macro + (end - buf);
659 CPP_SET_WRITTEN (pfile, here);
661 mdef = create_definition (macro, end, pfile, keyword == NULL);
662 if (mdef.defn == 0)
663 return 0;
665 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
667 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
669 int ok = 0;
670 /* Redefining a precompiled key is ok. */
671 if (hp->type == T_PCSTRING)
672 ok = 1;
673 /* Redefining a poisoned identifier is even worse than `not ok'. */
674 else if (hp->type == T_POISON)
675 ok = -1;
676 /* Redefining a macro is ok if the definitions are the same. */
677 else if (hp->type == T_MACRO)
678 ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
679 /* Redefining a constant is ok with -D. */
680 else if (hp->type == T_CONST || hp->type == T_STDC)
681 ok = ! CPP_OPTIONS (pfile)->done_initializing;
682 /* Print the warning or error if it's not ok. */
683 if (ok <= 0)
685 if (hp->type == T_POISON)
686 cpp_error (pfile, "redefining poisoned `%.*s'",
687 mdef.symlen, mdef.symnam);
688 else
689 cpp_pedwarn (pfile, "`%.*s' redefined", mdef.symlen, mdef.symnam);
690 if (hp->type == T_MACRO)
691 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file,
692 hp->value.defn->line,
693 "this is the location of the previous definition");
695 if (hp->type != T_POISON)
697 /* Replace the old definition. */
698 hp->type = new_type;
699 hp->value.defn = mdef.defn;
702 else
703 cpp_install (pfile, mdef.symnam, mdef.symlen, new_type,
704 (char *) mdef.defn, hashcode);
706 if (keyword != NULL && keyword->type == T_DEFINE)
708 if (CPP_OPTIONS (pfile)->debug_output
709 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
710 dump_definition (pfile, mdef);
711 else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
712 pass_thru_directive (mdef.symnam, mdef.symlen, pfile, keyword);
715 return 0;
719 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
720 If BUFFER != NULL, then use the LENGTH characters in BUFFER
721 as the new input buffer.
722 Return the new buffer, or NULL on failure. */
724 cpp_buffer *
725 cpp_push_buffer (pfile, buffer, length)
726 cpp_reader *pfile;
727 U_CHAR *buffer;
728 long length;
730 cpp_buffer *buf = CPP_BUFFER (pfile);
731 cpp_buffer *new;
732 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
734 cpp_fatal (pfile, "macro or `#include' recursion too deep");
735 return NULL;
738 new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
740 new->if_stack = pfile->if_stack;
741 new->cleanup = null_cleanup;
742 new->underflow = null_underflow;
743 new->buf = new->cur = buffer;
744 new->alimit = new->rlimit = buffer + length;
745 new->prev = buf;
746 new->mark = -1;
748 CPP_BUFFER (pfile) = new;
749 return new;
752 cpp_buffer *
753 cpp_pop_buffer (pfile)
754 cpp_reader *pfile;
756 cpp_buffer *buf = CPP_BUFFER (pfile);
757 (*buf->cleanup) (buf, pfile);
758 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
759 free (buf);
760 pfile->buffer_stack_depth--;
761 return CPP_BUFFER (pfile);
764 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
765 Pop the buffer when done. */
767 void
768 cpp_scan_buffer (pfile)
769 cpp_reader *pfile;
771 cpp_buffer *buffer = CPP_BUFFER (pfile);
772 enum cpp_token token;
773 if (CPP_OPTIONS (pfile)->no_output)
775 long old_written = CPP_WRITTEN (pfile);
776 /* In no-output mode, we can ignore everything but directives. */
777 for (;;)
779 if (! pfile->only_seen_white)
780 skip_rest_of_line (pfile);
781 token = cpp_get_token (pfile);
782 if (token == CPP_EOF) /* Should not happen ... */
783 break;
784 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
786 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
787 != CPP_NULL_BUFFER (pfile))
788 cpp_pop_buffer (pfile);
789 break;
792 CPP_SET_WRITTEN (pfile, old_written);
794 else
796 for (;;)
798 token = cpp_get_token (pfile);
799 if (token == CPP_EOF) /* Should not happen ... */
800 break;
801 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
803 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
804 != CPP_NULL_BUFFER (pfile))
805 cpp_pop_buffer (pfile);
806 break;
813 * Rescan a string (which may have escape marks) into pfile's buffer.
814 * Place the result in pfile->token_buffer.
816 * The input is copied before it is scanned, so it is safe to pass
817 * it something from the token_buffer that will get overwritten
818 * (because it follows CPP_WRITTEN). This is used by do_include.
821 void
822 cpp_expand_to_buffer (pfile, buf, length)
823 cpp_reader *pfile;
824 const U_CHAR *buf;
825 int length;
827 register cpp_buffer *ip;
828 U_CHAR *buf1;
829 int save_no_output;
831 if (length < 0)
833 cpp_fatal (pfile, "internal error: length < 0 in cpp_expand_to_buffer");
834 return;
837 /* Set up the input on the input stack. */
839 buf1 = (U_CHAR *) alloca (length + 1);
840 memcpy (buf1, buf, length);
841 buf1[length] = 0;
843 ip = cpp_push_buffer (pfile, buf1, length);
844 if (ip == NULL)
845 return;
846 ip->has_escapes = 1;
848 /* Scan the input, create the output. */
849 save_no_output = CPP_OPTIONS (pfile)->no_output;
850 CPP_OPTIONS (pfile)->no_output = 0;
851 cpp_scan_buffer (pfile);
852 CPP_OPTIONS (pfile)->no_output = save_no_output;
854 CPP_NUL_TERMINATE (pfile);
857 void
858 cpp_buf_line_and_col (pbuf, linep, colp)
859 register cpp_buffer *pbuf;
860 long *linep, *colp;
862 if (pbuf)
864 *linep = pbuf->lineno;
865 if (colp)
866 *colp = pbuf->cur - pbuf->line_base;
868 else
870 *linep = 0;
871 if (colp)
872 *colp = 0;
876 /* Return the cpp_buffer that corresponds to a file (not a macro). */
878 cpp_buffer *
879 cpp_file_buffer (pfile)
880 cpp_reader *pfile;
882 cpp_buffer *ip = CPP_BUFFER (pfile);
884 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
885 if (ip->fname != NULL)
886 return ip;
887 return NULL;
891 * write out a #line command, for instance, after an #include file.
892 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
895 void
896 output_line_command (pfile, file_change)
897 cpp_reader *pfile;
898 enum file_change_code file_change;
900 long line;
901 cpp_buffer *ip = CPP_BUFFER (pfile);
903 if (ip->fname == NULL)
904 return;
906 if (CPP_OPTIONS (pfile)->no_line_commands
907 || CPP_OPTIONS (pfile)->no_output)
908 return;
910 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, NULL);
912 /* If the current file has not changed, we omit the #line if it would
913 appear to be a no-op, and we output a few newlines instead
914 if we want to increase the line number by a small amount.
915 We cannot do this if pfile->lineno is zero, because that means we
916 haven't output any line commands yet. (The very first line command
917 output is a `same_file' command.) */
918 if (file_change == same_file && pfile->lineno != 0)
920 if (line == pfile->lineno)
921 return;
923 /* If the inherited line number is a little too small,
924 output some newlines instead of a #line command. */
925 if (line > pfile->lineno && line < pfile->lineno + 8)
927 CPP_RESERVE (pfile, 20);
928 while (line > pfile->lineno)
930 CPP_PUTC_Q (pfile, '\n');
931 pfile->lineno++;
933 return;
937 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
938 CPP_PUTS_Q (pfile, "# ", 2);
940 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
941 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
943 quote_string (pfile, ip->nominal_fname);
944 if (file_change != same_file)
946 CPP_PUTC_Q (pfile, ' ');
947 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
949 /* Tell cc1 if following text comes from a system header file. */
950 if (ip->system_header_p)
952 CPP_PUTC_Q (pfile, ' ');
953 CPP_PUTC_Q (pfile, '3');
955 #ifndef NO_IMPLICIT_EXTERN_C
956 /* Tell cc1plus if following text should be treated as C. */
957 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
959 CPP_PUTC_Q (pfile, ' ');
960 CPP_PUTC_Q (pfile, '4');
962 #endif
963 CPP_PUTC_Q (pfile, '\n');
964 pfile->lineno = line;
968 /* Like cpp_get_token, except that it does not read past end-of-line.
969 Also, horizontal space is skipped, and macros are popped. */
971 static enum cpp_token
972 get_directive_token (pfile)
973 cpp_reader *pfile;
975 for (;;)
977 long old_written = CPP_WRITTEN (pfile);
978 enum cpp_token token;
979 cpp_skip_hspace (pfile);
980 if (PEEKC () == '\n')
981 return CPP_VSPACE;
982 token = cpp_get_token (pfile);
983 switch (token)
985 case CPP_POP:
986 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
987 return token;
988 /* ... else fall though ... */
989 case CPP_HSPACE: case CPP_COMMENT:
990 CPP_SET_WRITTEN (pfile, old_written);
991 break;
992 default:
993 return token;
998 /* Handle #include and #import.
999 This function expects to see "fname" or <fname> on the input.
1001 The input is normally in part of the output_buffer following
1002 CPP_WRITTEN, and will get overwritten by output_line_command.
1003 I.e. in input file specification has been popped by handle_directive.
1004 This is safe. */
1006 static int
1007 do_include (pfile, keyword)
1008 cpp_reader *pfile;
1009 const struct directive *keyword;
1011 int importing = (keyword->type == T_IMPORT);
1012 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
1013 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
1014 int before; /* included before? */
1015 long flen;
1016 unsigned char *ftok;
1017 cpp_buffer *fp;
1019 enum cpp_token token;
1021 /* Chain of dirs to search */
1022 struct include_hash *ihash;
1023 struct file_name_list *search_start;
1025 long old_written = CPP_WRITTEN (pfile);
1027 int fd;
1029 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1031 if (importing)
1032 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
1033 if (skip_dirs)
1034 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
1037 if (importing && CPP_OPTIONS (pfile)->warn_import
1038 && !CPP_OPTIONS (pfile)->inhibit_warnings
1039 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
1041 pfile->import_warning = 1;
1042 cpp_warning (pfile,
1043 "#import is obsolete, use an #ifndef wrapper in the header file");
1046 pfile->parsing_include_directive++;
1047 token = get_directive_token (pfile);
1048 pfile->parsing_include_directive--;
1050 if (token == CPP_STRING)
1052 if (pfile->token_buffer[old_written] == '<')
1053 angle_brackets = 1;
1055 #ifdef VMS
1056 else if (token == CPP_NAME)
1058 /* Support '#include xyz' like VAX-C. It is taken as
1059 '#include <xyz.h>' and generates a warning. */
1060 cpp_warning (pfile,
1061 "`#include filename' is obsolete, use `#include <filename.h>'");
1062 angle_brackets = 1;
1064 /* Append the missing `.h' to the name. */
1065 CPP_PUTS (pfile, ".h", 2);
1067 #endif
1068 else
1070 cpp_error (pfile,
1071 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1072 CPP_SET_WRITTEN (pfile, old_written);
1073 skip_rest_of_line (pfile);
1074 return 0;
1077 flen = CPP_WRITTEN (pfile) - old_written;
1078 ftok = (unsigned char *) alloca (flen + 1);
1079 memcpy (ftok, pfile->token_buffer + old_written, flen);
1080 ftok[flen] = '\0';
1082 if (get_directive_token (pfile) != CPP_VSPACE)
1084 cpp_error (pfile, "junk at end of `#include'");
1085 skip_rest_of_line (pfile);
1088 CPP_SET_WRITTEN (pfile, old_written);
1090 if (flen == 0)
1092 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
1093 return 0;
1096 if (CPP_OPTIONS (pfile)->dump_includes)
1097 pass_thru_directive (ftok,
1098 flen
1099 #ifdef VMS
1100 - ((token == CPP_NAME) ? 2 : 0)
1101 #endif
1102 , pfile, keyword);
1104 #ifdef VMS
1105 if (token == CPP_STRING)
1106 #endif
1108 ftok++;
1109 flen -= 2;
1110 ftok[flen] = '\0';
1113 search_start = 0;
1115 for (fp = CPP_BUFFER (pfile);
1116 fp != CPP_NULL_BUFFER (pfile);
1117 fp = CPP_PREV_BUFFER (fp))
1118 if (fp->fname != NULL)
1119 break;
1121 if (fp == CPP_NULL_BUFFER (pfile))
1123 cpp_fatal (pfile, "cpp internal error: fp == NULL_BUFFER in do_include");
1124 return 0;
1127 /* For #include_next, skip in the search path past the dir in which the
1128 containing file was found. Treat files specified using an absolute path
1129 as if there are no more directories to search. Treat the primary source
1130 file like any other included source, but generate a warning. */
1131 if (skip_dirs && CPP_PREV_BUFFER(fp) != CPP_NULL_BUFFER (pfile))
1133 if (fp->ihash->foundhere != ABSOLUTE_PATH)
1134 search_start = fp->ihash->foundhere->next;
1136 else
1138 if (skip_dirs)
1139 cpp_warning (pfile, "#include_next in primary source file");
1141 if (angle_brackets)
1142 search_start = CPP_OPTIONS (pfile)->bracket_include;
1143 else
1145 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
1147 if (fp)
1148 search_start = fp->actual_dir;
1150 else
1151 search_start = CPP_OPTIONS (pfile)->quote_include;
1155 if (!search_start)
1157 cpp_error (pfile, "No include path in which to find %s", ftok);
1158 return 0;
1161 fd = find_include_file (pfile, ftok, search_start, &ihash, &before);
1163 if (fd == -2)
1164 return 0;
1166 if (fd == -1)
1168 if (CPP_OPTIONS (pfile)->print_deps_missing_files
1169 && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
1170 (pfile->system_include_depth > 0)))
1172 if (!angle_brackets)
1173 deps_output (pfile, ftok, ' ');
1174 else
1176 char *p;
1177 struct file_name_list *ptr;
1178 /* If requested as a system header, assume it belongs in
1179 the first system header directory. */
1180 if (CPP_OPTIONS (pfile)->bracket_include)
1181 ptr = CPP_OPTIONS (pfile)->bracket_include;
1182 else
1183 ptr = CPP_OPTIONS (pfile)->quote_include;
1185 p = (char *) alloca (strlen (ptr->name)
1186 + strlen (ftok) + 2);
1187 if (*ptr->name != '\0')
1189 strcpy (p, ptr->name);
1190 strcat (p, "/");
1192 strcat (p, ftok);
1193 deps_output (pfile, p, ' ');
1196 /* If -M was specified, and this header file won't be added to
1197 the dependency list, then don't count this as an error,
1198 because we can still produce correct output. Otherwise, we
1199 can't produce correct output, because there may be
1200 dependencies we need inside the missing file, and we don't
1201 know what directory this missing file exists in. */
1202 else if (CPP_PRINT_DEPS (pfile)
1203 && (CPP_PRINT_DEPS (pfile)
1204 <= (angle_brackets || (pfile->system_include_depth > 0))))
1205 cpp_warning (pfile, "No include path in which to find %s", ftok);
1206 else
1207 cpp_error_from_errno (pfile, ftok);
1209 return 0;
1212 /* For -M, add the file to the dependencies on its first inclusion. */
1213 if (!before && (CPP_PRINT_DEPS (pfile)
1214 > (angle_brackets || (pfile->system_include_depth > 0))))
1215 deps_output (pfile, ihash->name, ' ');
1217 /* Handle -H option. */
1218 if (CPP_OPTIONS(pfile)->print_include_names)
1220 fp = CPP_BUFFER (pfile);
1221 while ((fp = CPP_PREV_BUFFER (fp)) != CPP_NULL_BUFFER (pfile))
1222 putc ('.', stderr);
1223 fprintf (stderr, " %s\n", ihash->name);
1226 /* Actually process the file */
1228 if (importing)
1229 ihash->control_macro = "";
1231 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
1233 close (fd);
1234 return 0;
1237 if (angle_brackets)
1238 pfile->system_include_depth++; /* Decremented in file_cleanup. */
1240 if (finclude (pfile, fd, ihash))
1242 output_line_command (pfile, enter_file);
1243 pfile->only_seen_white = 2;
1246 return 0;
1249 /* Subroutine of do_line. Read next token from PFILE without adding it to
1250 the output buffer. If it is a number between 1 and 4, store it in *NUM
1251 and return 1; otherwise, return 0 and complain if we aren't at the end
1252 of the directive. */
1254 static int
1255 read_line_number (pfile, num)
1256 cpp_reader *pfile;
1257 int *num;
1259 long save_written = CPP_WRITTEN (pfile);
1260 U_CHAR *p = pfile->token_buffer + save_written;
1261 enum cpp_token token = get_directive_token (pfile);
1262 CPP_SET_WRITTEN (pfile, save_written);
1264 if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
1266 *num = p[0] - '0';
1267 return 1;
1269 else
1271 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
1272 cpp_error (pfile, "invalid format `#line' command");
1273 return 0;
1277 /* Interpret #line command.
1278 Note that the filename string (if any) is treated as if it were an
1279 include filename. That means no escape handling. */
1281 static int
1282 do_line (pfile, keyword)
1283 cpp_reader *pfile;
1284 const struct directive *keyword ATTRIBUTE_UNUSED;
1286 cpp_buffer *ip = CPP_BUFFER (pfile);
1287 int new_lineno;
1288 long old_written = CPP_WRITTEN (pfile);
1289 enum file_change_code file_change = same_file;
1290 enum cpp_token token;
1291 char *x;
1293 token = get_directive_token (pfile);
1295 if (token != CPP_NUMBER)
1297 cpp_error (pfile, "token after `#line' is not an integer");
1298 goto bad_line_directive;
1301 new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
1302 if (x[0] != '\0')
1304 cpp_error (pfile, "token after `#line' is not an integer");
1305 goto bad_line_directive;
1307 CPP_SET_WRITTEN (pfile, old_written);
1309 if (CPP_PEDANTIC (pfile) && new_lineno <= 0)
1310 cpp_pedwarn (pfile, "line number out of range in `#line' command");
1312 token = get_directive_token (pfile);
1314 if (token == CPP_STRING)
1316 U_CHAR *fname = pfile->token_buffer + old_written + 1;
1317 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
1318 int action_number = 0;
1320 if (read_line_number (pfile, &action_number))
1322 if (CPP_PEDANTIC (pfile))
1323 cpp_pedwarn (pfile, "garbage at end of `#line' command");
1325 if (action_number == 1)
1327 file_change = enter_file;
1328 read_line_number (pfile, &action_number);
1330 else if (action_number == 2)
1332 file_change = leave_file;
1333 read_line_number (pfile, &action_number);
1335 if (action_number == 3)
1337 ip->system_header_p = 1;
1338 read_line_number (pfile, &action_number);
1340 if (action_number == 4)
1342 ip->system_header_p = 2;
1343 read_line_number (pfile, &action_number);
1347 *end_name = '\0';
1349 if (strcmp (fname, ip->nominal_fname))
1351 char *newname, *oldname;
1352 if (!strcmp (fname, ip->fname))
1353 newname = ip->fname;
1354 else if (ip->last_nominal_fname
1355 && !strcmp (fname, ip->last_nominal_fname))
1356 newname = ip->last_nominal_fname;
1357 else
1358 newname = xstrdup (fname);
1360 oldname = ip->nominal_fname;
1361 ip->nominal_fname = newname;
1363 if (ip->last_nominal_fname
1364 && ip->last_nominal_fname != oldname
1365 && ip->last_nominal_fname != newname
1366 && ip->last_nominal_fname != ip->fname)
1367 free (ip->last_nominal_fname);
1369 if (newname == ip->fname)
1370 ip->last_nominal_fname = NULL;
1371 else
1372 ip->last_nominal_fname = oldname;
1375 else if (token != CPP_VSPACE && token != CPP_EOF)
1377 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1378 goto bad_line_directive;
1381 /* The Newline at the end of this line remains to be processed.
1382 To put the next line at the specified line number,
1383 we must store a line number now that is one less. */
1384 ip->lineno = new_lineno - 1;
1385 CPP_SET_WRITTEN (pfile, old_written);
1386 output_line_command (pfile, file_change);
1387 return 0;
1389 bad_line_directive:
1390 skip_rest_of_line (pfile);
1391 CPP_SET_WRITTEN (pfile, old_written);
1392 return 0;
1395 /* Remove the definition of a symbol from the symbol table.
1396 According to the C standard, it is not an error to undef
1397 something that has no definitions. */
1398 static int
1399 do_undef (pfile, keyword)
1400 cpp_reader *pfile;
1401 const struct directive *keyword;
1403 int sym_length;
1404 HASHNODE *hp;
1405 U_CHAR *buf, *name, *limit;
1406 int c;
1407 long here = CPP_WRITTEN (pfile);
1408 enum cpp_token token;
1410 cpp_skip_hspace (pfile);
1411 c = GETC();
1412 if (! is_idstart[c])
1414 cpp_error (pfile, "token after #undef is not an identifier");
1415 skip_rest_of_line (pfile);
1416 return 1;
1419 parse_name (pfile, c);
1420 buf = pfile->token_buffer + here;
1421 limit = CPP_PWRITTEN(pfile);
1423 /* Copy out the token so we can pop the token buffer. */
1424 name = (U_CHAR *) alloca (limit - buf + 1);
1425 bcopy(buf, name, limit - buf);
1426 name[limit - buf] = '\0';
1428 token = get_directive_token (pfile);
1429 if (token != CPP_VSPACE && token != CPP_POP)
1431 cpp_pedwarn (pfile, "junk on line after #undef");
1432 skip_rest_of_line (pfile);
1435 CPP_SET_WRITTEN (pfile, here);
1437 sym_length = check_macro_name (pfile, buf, 0);
1439 while ((hp = cpp_lookup (pfile, name, sym_length, -1)) != NULL)
1441 /* If we are generating additional info for debugging (with -g) we
1442 need to pass through all effective #undef commands. */
1443 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1444 pass_thru_directive (name, sym_length, pfile, keyword);
1445 if (hp->type == T_POISON)
1446 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
1447 else
1449 if (hp->type != T_MACRO)
1450 cpp_warning (pfile, "undefining `%s'", hp->name);
1451 delete_macro (hp);
1455 return 0;
1458 /* Wrap do_undef for -U processing. */
1459 void
1460 cpp_undef (pfile, macro)
1461 cpp_reader *pfile;
1462 U_CHAR *macro;
1464 if (cpp_push_buffer (pfile, macro, strlen (macro)))
1466 do_undef (pfile, NULL);
1467 cpp_pop_buffer (pfile);
1473 * Report an error detected by the program we are processing.
1474 * Use the text of the line in the error message.
1475 * (We use error because it prints the filename & line#.)
1478 static int
1479 do_error (pfile, keyword)
1480 cpp_reader *pfile;
1481 const struct directive *keyword ATTRIBUTE_UNUSED;
1483 long here = CPP_WRITTEN (pfile);
1484 U_CHAR *text;
1485 copy_rest_of_line (pfile);
1486 text = pfile->token_buffer + here;
1487 SKIP_WHITE_SPACE(text);
1489 cpp_error (pfile, "#error %s", text);
1490 CPP_SET_WRITTEN (pfile, here);
1491 return 0;
1495 * Report a warning detected by the program we are processing.
1496 * Use the text of the line in the warning message, then continue.
1499 static int
1500 do_warning (pfile, keyword)
1501 cpp_reader *pfile;
1502 const struct directive *keyword ATTRIBUTE_UNUSED;
1504 U_CHAR *text;
1505 long here = CPP_WRITTEN(pfile);
1506 copy_rest_of_line (pfile);
1507 text = pfile->token_buffer + here;
1508 SKIP_WHITE_SPACE(text);
1510 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1511 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1513 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
1514 if -pedantic-errors is given, #warning should cause an error. */
1515 cpp_pedwarn (pfile, "#warning %s", text);
1516 CPP_SET_WRITTEN (pfile, here);
1517 return 0;
1520 /* Report program identification.
1521 This is not precisely what cccp does with #ident, however I believe
1522 it matches `closely enough' (behavior is identical as long as there
1523 are no macros on the #ident line, which is pathological in my opinion). */
1525 static int
1526 do_ident (pfile, keyword)
1527 cpp_reader *pfile;
1528 const struct directive *keyword ATTRIBUTE_UNUSED;
1530 /* Allow #ident in system headers, since that's not user's fault. */
1531 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1532 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1534 CPP_PUTS (pfile, "#ident ", 7);
1535 cpp_skip_hspace (pfile);
1536 copy_rest_of_line (pfile);
1538 return 0;
1541 /* Just check for some recognized pragmas that need validation here,
1542 and leave the text in the token buffer to be output. */
1544 static int
1545 do_pragma (pfile, keyword)
1546 cpp_reader *pfile;
1547 const struct directive *keyword ATTRIBUTE_UNUSED;
1549 long here;
1550 U_CHAR *buf;
1552 CPP_PUTS (pfile, "#pragma ", 8);
1553 cpp_skip_hspace (pfile);
1555 here = CPP_WRITTEN (pfile);
1556 copy_rest_of_line (pfile);
1557 buf = pfile->token_buffer + here;
1559 if (!strncmp (buf, "once", 4))
1561 cpp_buffer *ip = NULL;
1563 /* Allow #pragma once in system headers, since that's not the user's
1564 fault. */
1565 if (!CPP_BUFFER (pfile)->system_header_p)
1566 cpp_warning (pfile, "`#pragma once' is obsolete");
1568 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
1570 if (ip == CPP_NULL_BUFFER (pfile))
1571 return 0;
1572 if (ip->fname != NULL)
1573 break;
1576 if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
1577 cpp_warning (pfile, "`#pragma once' outside include file");
1578 else
1579 ip->ihash->control_macro = ""; /* never repeat */
1581 else if (!strncmp (buf, "implementation", 14))
1583 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1584 been included yet. */
1585 struct include_hash *ptr;
1586 U_CHAR *p = buf + 14, *fname, *fcopy;
1587 SKIP_WHITE_SPACE (p);
1588 if (*p == '\n' || *p != '\"')
1589 return 0;
1591 fname = p + 1;
1592 p = (U_CHAR *) index (fname, '\"');
1594 fcopy = (U_CHAR *) alloca (p - fname + 1);
1595 bcopy (fname, fcopy, p - fname);
1596 fcopy[p-fname] = '\0';
1598 ptr = include_hash (pfile, fcopy, 0);
1599 if (ptr)
1600 cpp_warning (pfile,
1601 "`#pragma implementation' for `%s' appears after file is included",
1602 fcopy);
1604 else if (!strncmp (buf, "poison", 6))
1606 /* Poison these symbols so that all subsequent usage produces an
1607 error message. */
1608 U_CHAR *p = buf + 6;
1609 size_t plen;
1610 U_CHAR *syms;
1611 int writeit;
1613 SKIP_WHITE_SPACE (p);
1614 plen = strlen(p) + 1;
1616 syms = (U_CHAR *) alloca (plen);
1617 memcpy (syms, p, plen);
1619 /* As a rule, don't include #pragma poison commands in output,
1620 unless the user asks for them. */
1621 writeit = (CPP_OPTIONS (pfile)->debug_output
1622 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions
1623 || CPP_OPTIONS (pfile)->dump_macros == dump_names);
1625 if (writeit)
1626 CPP_SET_WRITTEN (pfile, here);
1627 else
1628 CPP_SET_WRITTEN (pfile, here-8);
1630 if (writeit)
1632 CPP_RESERVE (pfile, plen + 7);
1633 CPP_PUTS_Q (pfile, "poison", 7);
1636 while (*syms != '\0')
1638 U_CHAR *end = syms;
1640 while (is_idchar[*end])
1641 end++;
1643 if (!is_hor_space[*end] && *end != '\0')
1645 cpp_error (pfile, "invalid #pragma poison directive");
1646 return 1;
1649 if (cpp_push_buffer (pfile, syms, end - syms) != NULL)
1651 do_define (pfile, keyword);
1652 cpp_pop_buffer (pfile);
1654 if (writeit)
1656 CPP_PUTC_Q (pfile, ' ');
1657 CPP_PUTS_Q (pfile, syms, end - syms);
1659 syms = end;
1660 SKIP_WHITE_SPACE (syms);
1664 return 0;
1667 #ifdef SCCS_DIRECTIVE
1668 /* Just ignore #sccs, on systems where we define it at all. */
1670 static int
1671 do_sccs (pfile, keyword)
1672 cpp_reader *pfile;
1673 const struct directive *keyword ATTRIBUTE_UNUSED;
1675 if (CPP_PEDANTIC (pfile))
1676 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1677 skip_rest_of_line (pfile);
1678 return 0;
1680 #endif
1683 /* We've found an `#if' directive. If the only thing before it in
1684 this file is white space, and if it is of the form
1685 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1686 for inclusion of this file. (See redundant_include_p in cppfiles.c
1687 for an explanation of controlling macros.) If so, return a
1688 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1690 static U_CHAR *
1691 detect_if_not_defined (pfile)
1692 cpp_reader *pfile;
1694 U_CHAR *control_macro = 0;
1696 if (pfile->only_seen_white == 2)
1698 char *ident;
1699 enum cpp_token token;
1700 int base_offset;
1701 int token_offset;
1702 int need_rparen = 0;
1704 /* Save state required for restore. */
1705 pfile->no_macro_expand++;
1706 parse_set_mark (pfile);
1707 base_offset = CPP_WRITTEN (pfile);
1709 /* Look for `!', */
1710 if (get_directive_token (pfile) != CPP_OTHER
1711 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1712 || CPP_PWRITTEN (pfile)[-1] != '!')
1713 goto restore;
1715 /* ...then `defined', */
1716 token_offset = CPP_WRITTEN (pfile);
1717 token = get_directive_token (pfile);
1718 if (token != CPP_NAME)
1719 goto restore;
1720 ident = pfile->token_buffer + token_offset;
1721 CPP_NUL_TERMINATE (pfile);
1722 if (strcmp (ident, "defined"))
1723 goto restore;
1725 /* ...then an optional '(' and the name, */
1726 token_offset = CPP_WRITTEN (pfile);
1727 token = get_directive_token (pfile);
1728 if (token == CPP_LPAREN)
1730 token_offset = CPP_WRITTEN (pfile);
1731 token = get_directive_token (pfile);
1732 if (token != CPP_NAME)
1733 goto restore;
1734 need_rparen = 1;
1736 else if (token != CPP_NAME)
1737 goto restore;
1739 ident = pfile->token_buffer + token_offset;
1740 CPP_NUL_TERMINATE (pfile);
1742 /* ...then the ')', if necessary, */
1743 if ((!need_rparen || get_directive_token (pfile) == CPP_RPAREN)
1744 /* ...and make sure there's nothing else on the line. */
1745 && get_directive_token (pfile) == CPP_VSPACE)
1746 control_macro = xstrdup (ident);
1748 restore:
1749 CPP_SET_WRITTEN (pfile, base_offset);
1750 pfile->no_macro_expand--;
1751 parse_goto_mark (pfile);
1754 return control_macro;
1758 * handle #if command by
1759 * 1) inserting special `defined' keyword into the hash table
1760 * that gets turned into 0 or 1 by special_symbol (thus,
1761 * if the luser has a symbol called `defined' already, it won't
1762 * work inside the #if command)
1763 * 2) rescan the input into a temporary output buffer
1764 * 3) pass the output buffer to the yacc parser and collect a value
1765 * 4) clean up the mess left from steps 1 and 2.
1766 * 5) call conditional_skip to skip til the next #endif (etc.),
1767 * or not, depending on the value from step 3.
1770 static int
1771 do_if (pfile, keyword)
1772 cpp_reader *pfile;
1773 const struct directive *keyword ATTRIBUTE_UNUSED;
1775 U_CHAR *control_macro = detect_if_not_defined (pfile);
1776 HOST_WIDEST_INT value = eval_if_expression (pfile);
1777 conditional_skip (pfile, value == 0, T_IF, control_macro);
1778 return 0;
1782 * handle a #elif directive by not changing if_stack either.
1783 * see the comment above do_else.
1786 static int
1787 do_elif (pfile, keyword)
1788 cpp_reader *pfile;
1789 const struct directive *keyword ATTRIBUTE_UNUSED;
1791 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
1792 cpp_error (pfile, "`#elif' not within a conditional");
1793 return 0;
1794 } else {
1795 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
1796 cpp_error (pfile, "`#elif' after `#else'");
1797 #if 0
1798 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
1799 #endif
1800 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
1801 && strcmp (pfile->if_stack->fname,
1802 CPP_BUFFER (pfile)->nominal_fname) != 0)
1803 fprintf (stderr, ", file %s", pfile->if_stack->fname);
1804 fprintf (stderr, ")\n");
1806 pfile->if_stack->type = T_ELIF;
1809 if (pfile->if_stack->if_succeeded)
1810 skip_if_group (pfile);
1811 else {
1812 HOST_WIDEST_INT value = eval_if_expression (pfile);
1813 if (value == 0)
1814 skip_if_group (pfile);
1815 else {
1816 ++pfile->if_stack->if_succeeded; /* continue processing input */
1817 output_line_command (pfile, same_file);
1820 return 0;
1824 * evaluate a #if expression in BUF, of length LENGTH,
1825 * then parse the result as a C expression and return the value as an int.
1828 static HOST_WIDEST_INT
1829 eval_if_expression (pfile)
1830 cpp_reader *pfile;
1832 HOST_WIDEST_INT value;
1833 long old_written = CPP_WRITTEN (pfile);
1835 pfile->pcp_inside_if = 1;
1836 value = cpp_parse_expr (pfile);
1837 pfile->pcp_inside_if = 0;
1839 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1841 return value;
1845 * routine to handle ifdef/ifndef. Try to look up the symbol,
1846 * then do or don't skip to the #endif/#else/#elif depending
1847 * on what directive is actually being processed.
1850 static int
1851 do_xifdef (pfile, keyword)
1852 cpp_reader *pfile;
1853 const struct directive *keyword;
1855 int skip;
1856 cpp_buffer *ip = CPP_BUFFER (pfile);
1857 U_CHAR *ident;
1858 int ident_length;
1859 enum cpp_token token;
1860 int start_of_file = 0;
1861 U_CHAR *control_macro = 0;
1862 int old_written = CPP_WRITTEN (pfile);
1864 /* Detect a #ifndef at start of file (not counting comments). */
1865 if (ip->fname != 0 && keyword->type == T_IFNDEF)
1866 start_of_file = pfile->only_seen_white == 2;
1868 pfile->no_macro_expand++;
1869 token = get_directive_token (pfile);
1870 pfile->no_macro_expand--;
1872 ident = pfile->token_buffer + old_written;
1873 ident_length = CPP_WRITTEN (pfile) - old_written;
1874 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1876 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1878 skip = (keyword->type == T_IFDEF);
1879 if (! CPP_TRADITIONAL (pfile))
1880 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
1882 else if (token == CPP_NAME)
1884 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
1885 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
1886 if (start_of_file && !skip)
1888 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
1889 bcopy (ident, control_macro, ident_length + 1);
1891 if (hp != NULL && hp->type == T_POISON)
1893 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1894 skip = !skip;
1897 else
1899 skip = (keyword->type == T_IFDEF);
1900 if (! CPP_TRADITIONAL (pfile))
1901 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
1904 if (!CPP_TRADITIONAL (pfile))
1905 { int c;
1906 cpp_skip_hspace (pfile);
1907 c = PEEKC ();
1908 if (c != EOF && c != '\n')
1909 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
1911 skip_rest_of_line (pfile);
1913 #if 0
1914 if (pcp_outfile) {
1915 /* Output a precondition for this macro. */
1916 if (hp && hp->value.defn->predefined)
1917 fprintf (pcp_outfile, "#define %s\n", hp->name);
1918 else {
1919 U_CHAR *cp = buf;
1920 fprintf (pcp_outfile, "#undef ");
1921 while (is_idchar[*cp]) /* Ick! */
1922 fputc (*cp++, pcp_outfile);
1923 putc ('\n', pcp_outfile);
1925 #endif
1927 conditional_skip (pfile, skip, T_IF, control_macro);
1928 return 0;
1931 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1932 If this is a #ifndef starting at the beginning of a file,
1933 CONTROL_MACRO is the macro name tested by the #ifndef.
1934 Otherwise, CONTROL_MACRO is 0. */
1936 static void
1937 conditional_skip (pfile, skip, type, control_macro)
1938 cpp_reader *pfile;
1939 int skip;
1940 enum node_type type;
1941 U_CHAR *control_macro;
1943 IF_STACK_FRAME *temp;
1945 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
1946 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
1947 temp->lineno = CPP_BUFFER (pfile)->lineno;
1948 temp->next = pfile->if_stack;
1949 temp->control_macro = control_macro;
1950 pfile->if_stack = temp;
1952 pfile->if_stack->type = type;
1954 if (skip != 0) {
1955 skip_if_group (pfile);
1956 return;
1957 } else {
1958 ++pfile->if_stack->if_succeeded;
1959 output_line_command (pfile, same_file);
1963 /* Subroutine of skip_if_group. Examine one preprocessing directive and
1964 return 0 if skipping should continue, 1 if it should halt. Also
1965 adjusts the if_stack as appropriate.
1966 The `#' has been read, but not the identifier. */
1968 static int
1969 consider_directive_while_skipping (pfile, stack)
1970 cpp_reader *pfile;
1971 IF_STACK_FRAME *stack;
1973 long ident_len, ident;
1974 const struct directive *kt;
1975 IF_STACK_FRAME *temp;
1977 cpp_skip_hspace (pfile);
1979 ident = CPP_WRITTEN (pfile);
1980 parse_name (pfile, GETC());
1981 ident_len = CPP_WRITTEN (pfile) - ident;
1983 CPP_SET_WRITTEN (pfile, ident);
1985 for (kt = directive_table; kt->length >= 0; kt++)
1986 if (kt->length == ident_len
1987 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
1988 switch (kt->type)
1990 case T_IF:
1991 case T_IFDEF:
1992 case T_IFNDEF:
1993 temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
1994 temp->next = pfile->if_stack;
1995 pfile->if_stack = temp;
1996 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
1997 temp->type = kt->type;
1998 return 0;
2000 case T_ELSE:
2001 if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
2002 validate_else (pfile, "#else");
2003 /* fall through */
2004 case T_ELIF:
2005 if (pfile->if_stack->type == T_ELSE)
2006 cpp_error (pfile, "`%s' after `#else'", kt->name);
2008 if (pfile->if_stack == stack)
2009 return 1;
2010 else
2012 pfile->if_stack->type = kt->type;
2013 return 0;
2016 case T_ENDIF:
2017 if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
2018 validate_else (pfile, "#endif");
2020 if (pfile->if_stack == stack)
2021 return 1;
2023 temp = pfile->if_stack;
2024 pfile->if_stack = temp->next;
2025 free (temp);
2026 return 0;
2028 default:
2029 return 0;
2032 /* Don't let erroneous code go by. */
2033 if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
2034 cpp_pedwarn (pfile, "invalid preprocessor directive name");
2035 return 0;
2038 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
2039 * leaves input ptr at the sharp sign found.
2041 static void
2042 skip_if_group (pfile)
2043 cpp_reader *pfile;
2045 int c;
2046 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
2047 U_CHAR *beg_of_line;
2048 long old_written;
2050 if (CPP_OPTIONS (pfile)->output_conditionals)
2052 CPP_PUTS (pfile, "#failed\n", 8);
2053 pfile->lineno++;
2054 output_line_command (pfile, same_file);
2057 old_written = CPP_WRITTEN (pfile);
2059 for (;;)
2061 beg_of_line = CPP_BUFFER (pfile)->cur;
2063 if (! CPP_TRADITIONAL (pfile))
2064 cpp_skip_hspace (pfile);
2065 c = GETC();
2066 if (c == '\n')
2068 if (CPP_OPTIONS (pfile)->output_conditionals)
2069 CPP_PUTC (pfile, c);
2070 CPP_BUMP_LINE (pfile);
2071 continue;
2073 else if (c == '#')
2075 if (consider_directive_while_skipping (pfile, save_if_stack))
2076 break;
2078 else if (c == EOF)
2079 return; /* Caller will issue error. */
2081 FORWARD(-1);
2082 if (CPP_OPTIONS (pfile)->output_conditionals)
2084 CPP_PUTS (pfile, beg_of_line, CPP_BUFFER (pfile)->cur - beg_of_line);
2085 copy_rest_of_line (pfile);
2087 else
2089 copy_rest_of_line (pfile);
2090 CPP_SET_WRITTEN (pfile, old_written); /* discard it */
2093 c = GETC();
2094 if (c == EOF)
2095 return; /* Caller will issue error. */
2096 else
2098 /* \n */
2099 if (CPP_OPTIONS (pfile)->output_conditionals)
2101 CPP_PUTC (pfile, c);
2102 pfile->lineno++;
2104 CPP_BUMP_LINE (pfile);
2108 /* Back up to the beginning of this line. Caller will process the
2109 directive. */
2110 CPP_BUFFER (pfile)->cur = beg_of_line;
2111 pfile->only_seen_white = 1;
2112 if (CPP_OPTIONS (pfile)->output_conditionals)
2114 CPP_PUTS (pfile, "#endfailed\n", 11);
2115 pfile->lineno++;
2120 * handle a #else directive. Do this by just continuing processing
2121 * without changing if_stack ; this is so that the error message
2122 * for missing #endif's etc. will point to the original #if. It
2123 * is possible that something different would be better.
2126 static int
2127 do_else (pfile, keyword)
2128 cpp_reader *pfile;
2129 const struct directive *keyword ATTRIBUTE_UNUSED;
2131 cpp_buffer *ip = CPP_BUFFER (pfile);
2133 if (CPP_PEDANTIC (pfile))
2134 validate_else (pfile, "#else");
2135 skip_rest_of_line (pfile);
2137 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
2138 cpp_error (pfile, "`#else' not within a conditional");
2139 return 0;
2140 } else {
2141 /* #ifndef can't have its special treatment for containing the whole file
2142 if it has a #else clause. */
2143 pfile->if_stack->control_macro = 0;
2145 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
2146 cpp_error (pfile, "`#else' after `#else'");
2147 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
2148 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
2149 fprintf (stderr, ", file %s", pfile->if_stack->fname);
2150 fprintf (stderr, ")\n");
2152 pfile->if_stack->type = T_ELSE;
2155 if (pfile->if_stack->if_succeeded)
2156 skip_if_group (pfile);
2157 else {
2158 ++pfile->if_stack->if_succeeded; /* continue processing input */
2159 output_line_command (pfile, same_file);
2161 return 0;
2165 * unstack after #endif command
2168 static int
2169 do_endif (pfile, keyword)
2170 cpp_reader *pfile;
2171 const struct directive *keyword ATTRIBUTE_UNUSED;
2173 if (CPP_PEDANTIC (pfile))
2174 validate_else (pfile, "#endif");
2175 skip_rest_of_line (pfile);
2177 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2178 cpp_error (pfile, "`#endif' not within a conditional");
2179 else
2181 IF_STACK_FRAME *temp = pfile->if_stack;
2182 pfile->if_stack = temp->next;
2183 if (temp->control_macro != 0)
2185 /* This #endif matched a #ifndef at the start of the file.
2186 See if it is at the end of the file. */
2187 int c;
2189 parse_set_mark (pfile);
2191 for (;;)
2193 cpp_skip_hspace (pfile);
2194 c = GETC ();
2195 if (c != '\n')
2196 break;
2198 parse_goto_mark (pfile);
2200 if (c == EOF)
2202 /* This #endif ends a #ifndef
2203 that contains all of the file (aside from whitespace).
2204 Arrange not to include the file again
2205 if the macro that was tested is defined. */
2206 struct cpp_buffer *ip;
2207 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2208 if (ip->fname != NULL)
2209 break;
2210 ip->ihash->control_macro = (char *) temp->control_macro;
2213 free (temp);
2214 output_line_command (pfile, same_file);
2216 return 0;
2219 /* When an #else or #endif is found while skipping failed conditional,
2220 if -pedantic was specified, this is called to warn about text after
2221 the command name. P points to the first char after the command name. */
2223 static void
2224 validate_else (pfile, directive)
2225 cpp_reader *pfile;
2226 const char *directive;
2228 int c;
2229 cpp_skip_hspace (pfile);
2230 c = PEEKC ();
2231 if (c != EOF && c != '\n')
2232 cpp_pedwarn (pfile,
2233 "text following `%s' violates ANSI standard", directive);
2236 /* Convert T_IF, etc. to a string. Used in error messages. */
2237 static const char *
2238 if_directive_name (pfile, ifs)
2239 cpp_reader *pfile;
2240 struct if_stack *ifs;
2242 switch (ifs->type)
2244 case T_IF: return "#if";
2245 case T_IFDEF: return "#ifdef";
2246 case T_IFNDEF: return "#ifndef";
2247 case T_ELIF: return "#elif";
2248 case T_ELSE: return "#else";
2249 default:
2250 cpp_fatal (pfile, "impossible if_stack->type value %d", ifs->type);
2251 return "unknown";
2255 /* Get the next token, and add it to the text in pfile->token_buffer.
2256 Return the kind of token we got. */
2258 enum cpp_token
2259 cpp_get_token (pfile)
2260 cpp_reader *pfile;
2262 register int c, c2, c3;
2263 enum cpp_token token;
2264 struct cpp_options *opts = CPP_OPTIONS (pfile);
2266 get_next:
2267 c = GETC();
2268 if (c == EOF)
2270 handle_eof:
2271 if (CPP_BUFFER (pfile)->manual_pop)
2272 /* If we've been reading from redirected input, the
2273 frontend will pop the buffer. */
2274 return CPP_EOF;
2275 else if (CPP_BUFFER (pfile)->seen_eof)
2277 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile))
2278 return CPP_EOF;
2280 cpp_pop_buffer (pfile);
2281 goto get_next;
2283 else
2285 cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2286 struct if_stack *ifs, *nifs;
2288 /* Unwind the conditional stack and generate error messages. */
2289 for (ifs = pfile->if_stack;
2290 ifs != CPP_BUFFER (pfile)->if_stack;
2291 ifs = nifs)
2293 cpp_error_with_line (pfile, ifs->lineno, -1,
2294 "unterminated `%s' conditional",
2295 if_directive_name (pfile, ifs));
2297 nifs = ifs->next;
2298 free (ifs);
2300 pfile->if_stack = ifs;
2302 if (CPP_BUFFER (pfile)->nominal_fname
2303 && next_buf != CPP_NULL_BUFFER (pfile))
2305 /* We're about to return from an #include file.
2306 Emit #line information now (as part of the CPP_POP) result.
2307 But the #line refers to the file we will pop to. */
2308 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2309 CPP_BUFFER (pfile) = next_buf;
2310 pfile->input_stack_listing_current = 0;
2311 output_line_command (pfile, leave_file);
2312 CPP_BUFFER (pfile) = cur_buffer;
2315 CPP_BUFFER (pfile)->seen_eof = 1;
2316 return CPP_POP;
2319 else
2321 switch (c)
2323 case '/':
2324 if (PEEKC () == '=')
2325 goto op2;
2327 comment:
2328 if (opts->put_out_comments)
2329 c = copy_comment (pfile, c);
2330 else
2331 c = skip_comment (pfile, c);
2332 if (c == EOF)
2333 goto handle_eof;
2334 else if (c != ' ')
2335 goto randomchar;
2337 /* Comments are equivalent to spaces.
2338 For -traditional, a comment is equivalent to nothing. */
2339 if (opts->traditional || opts->put_out_comments)
2340 return CPP_COMMENT;
2341 else
2343 CPP_PUTC (pfile, c);
2344 return CPP_HSPACE;
2346 #if 0
2347 if (opts->for_lint) {
2348 U_CHAR *argbp;
2349 int cmdlen, arglen;
2350 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
2352 if (lintcmd != NULL) {
2353 /* I believe it is always safe to emit this newline: */
2354 obp[-1] = '\n';
2355 bcopy ("#pragma lint ", (char *) obp, 13);
2356 obp += 13;
2357 bcopy (lintcmd, (char *) obp, cmdlen);
2358 obp += cmdlen;
2360 if (arglen != 0) {
2361 *(obp++) = ' ';
2362 bcopy (argbp, (char *) obp, arglen);
2363 obp += arglen;
2366 /* OK, now bring us back to the state we were in before we entered
2367 this branch. We need #line because the newline for the pragma
2368 could mess things up. */
2369 output_line_command (pfile, same_file);
2370 *(obp++) = ' '; /* just in case, if comments are copied thru */
2371 *(obp++) = '/';
2374 #endif
2376 case '#':
2377 #if 0
2378 /* If this is expanding a macro definition, don't recognize
2379 preprocessor directives. */
2380 if (ip->macro != 0)
2381 goto randomchar;
2382 /* If this is expand_into_temp_buffer, recognize them
2383 only after an actual newline at this level,
2384 not at the beginning of the input level. */
2385 if (ip->fname == 0 && beg_of_line == ip->buf)
2386 goto randomchar;
2387 if (ident_length)
2388 goto specialchar;
2389 #endif
2391 if (!pfile->only_seen_white)
2392 goto randomchar;
2393 if (handle_directive (pfile))
2394 return CPP_DIRECTIVE;
2395 pfile->only_seen_white = 0;
2396 return CPP_OTHER;
2398 case '\"':
2399 case '\'':
2400 string:
2401 parse_string (pfile, c);
2402 pfile->only_seen_white = 0;
2403 return c == '\'' ? CPP_CHAR : CPP_STRING;
2405 case '$':
2406 if (!opts->dollars_in_ident)
2407 goto randomchar;
2408 goto letter;
2410 case ':':
2411 if (opts->cplusplus && PEEKC () == ':')
2412 goto op2;
2413 goto randomchar;
2415 case '&':
2416 case '+':
2417 case '|':
2418 c2 = PEEKC ();
2419 if (c2 == c || c2 == '=')
2420 goto op2;
2421 goto randomchar;
2423 case '*':
2424 case '!':
2425 case '%':
2426 case '=':
2427 case '^':
2428 if (PEEKC () == '=')
2429 goto op2;
2430 goto randomchar;
2432 case '-':
2433 c2 = PEEKC ();
2434 if (c2 == '-' && opts->chill)
2435 goto comment; /* Chill style comment */
2436 if (c2 == '-' || c2 == '=')
2437 goto op2;
2438 if (c2 == '>')
2440 if (opts->cplusplus && PEEKN (1) == '*')
2442 /* In C++, there's a ->* operator. */
2443 token = CPP_OTHER;
2444 pfile->only_seen_white = 0;
2445 CPP_RESERVE (pfile, 4);
2446 CPP_PUTC_Q (pfile, c);
2447 CPP_PUTC_Q (pfile, GETC ());
2448 CPP_PUTC_Q (pfile, GETC ());
2449 CPP_NUL_TERMINATE_Q (pfile);
2450 return token;
2452 goto op2;
2454 goto randomchar;
2456 case '<':
2457 if (pfile->parsing_include_directive)
2459 for (;;)
2461 CPP_PUTC (pfile, c);
2462 if (c == '>')
2463 break;
2464 c = GETC ();
2465 if (c == '\n' || c == EOF)
2467 cpp_error (pfile,
2468 "missing '>' in `#include <FILENAME>'");
2469 break;
2471 else if (c == '\r')
2473 if (!CPP_BUFFER (pfile)->has_escapes)
2475 /* Backslash newline is replaced by nothing. */
2476 CPP_ADJUST_WRITTEN (pfile, -1);
2477 CPP_BUMP_LINE (pfile);
2479 else
2481 /* We might conceivably get \r- or \r<space> in
2482 here. Just delete 'em. */
2483 int d = GETC();
2484 if (d != '-' && d != ' ')
2485 cpp_fatal (pfile,
2486 "internal error: unrecognized escape \\r%c",
2488 CPP_ADJUST_WRITTEN (pfile, -1);
2492 return CPP_STRING;
2494 /* else fall through */
2495 case '>':
2496 c2 = PEEKC ();
2497 if (c2 == '=')
2498 goto op2;
2499 /* GNU C++ supports MIN and MAX operators <? and >?. */
2500 if (c2 != c && (!opts->cplusplus || c2 != '?'))
2501 goto randomchar;
2502 FORWARD(1);
2503 CPP_RESERVE (pfile, 4);
2504 CPP_PUTC (pfile, c);
2505 CPP_PUTC (pfile, c2);
2506 c3 = PEEKC ();
2507 if (c3 == '=')
2508 CPP_PUTC_Q (pfile, GETC ());
2509 CPP_NUL_TERMINATE_Q (pfile);
2510 pfile->only_seen_white = 0;
2511 return CPP_OTHER;
2513 case '.':
2514 c2 = PEEKC ();
2515 if (ISDIGIT(c2))
2517 CPP_RESERVE(pfile, 2);
2518 CPP_PUTC_Q (pfile, '.');
2519 c = GETC ();
2520 goto number;
2523 /* In C++ there's a .* operator. */
2524 if (opts->cplusplus && c2 == '*')
2525 goto op2;
2527 if (c2 == '.' && PEEKN(1) == '.')
2529 CPP_RESERVE(pfile, 4);
2530 CPP_PUTC_Q (pfile, '.');
2531 CPP_PUTC_Q (pfile, '.');
2532 CPP_PUTC_Q (pfile, '.');
2533 FORWARD (2);
2534 CPP_NUL_TERMINATE_Q (pfile);
2535 pfile->only_seen_white = 0;
2536 return CPP_3DOTS;
2538 goto randomchar;
2540 op2:
2541 token = CPP_OTHER;
2542 pfile->only_seen_white = 0;
2543 CPP_RESERVE(pfile, 3);
2544 CPP_PUTC_Q (pfile, c);
2545 CPP_PUTC_Q (pfile, GETC ());
2546 CPP_NUL_TERMINATE_Q (pfile);
2547 return token;
2549 case 'L':
2550 c2 = PEEKC ();
2551 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2553 CPP_PUTC (pfile, c);
2554 c = GETC ();
2555 goto string;
2557 goto letter;
2559 case '0': case '1': case '2': case '3': case '4':
2560 case '5': case '6': case '7': case '8': case '9':
2561 number:
2562 c2 = '.';
2563 for (;;)
2565 CPP_RESERVE (pfile, 2);
2566 CPP_PUTC_Q (pfile, c);
2567 c = PEEKC ();
2568 if (c == EOF)
2569 break;
2570 if (!is_idchar[c] && c != '.'
2571 && ((c2 != 'e' && c2 != 'E'
2572 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
2573 || (c != '+' && c != '-')))
2574 break;
2575 FORWARD(1);
2576 c2= c;
2578 CPP_NUL_TERMINATE_Q (pfile);
2579 pfile->only_seen_white = 0;
2580 return CPP_NUMBER;
2581 case 'b': case 'c': case 'd': case 'h': case 'o':
2582 case 'B': case 'C': case 'D': case 'H': case 'O':
2583 if (opts->chill && PEEKC () == '\'')
2585 pfile->only_seen_white = 0;
2586 CPP_RESERVE (pfile, 2);
2587 CPP_PUTC_Q (pfile, c);
2588 CPP_PUTC_Q (pfile, '\'');
2589 FORWARD(1);
2590 for (;;)
2592 c = GETC();
2593 if (c == EOF)
2594 goto chill_number_eof;
2595 if (!is_idchar[c])
2596 break;
2597 CPP_PUTC (pfile, c);
2599 if (c == '\'')
2601 CPP_RESERVE (pfile, 2);
2602 CPP_PUTC_Q (pfile, c);
2603 CPP_NUL_TERMINATE_Q (pfile);
2604 return CPP_STRING;
2606 else
2608 FORWARD(-1);
2609 chill_number_eof:
2610 CPP_NUL_TERMINATE (pfile);
2611 return CPP_NUMBER;
2614 else
2615 goto letter;
2616 case '_':
2617 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2618 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2619 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2620 case 'x': case 'y': case 'z':
2621 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2622 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2623 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2624 case 'Y': case 'Z':
2625 letter:
2627 HASHNODE *hp;
2628 unsigned char *ident;
2629 int before_name_written = CPP_WRITTEN (pfile);
2630 int ident_len;
2631 parse_name (pfile, c);
2632 pfile->only_seen_white = 0;
2633 if (pfile->no_macro_expand)
2634 return CPP_NAME;
2635 ident = pfile->token_buffer + before_name_written;
2636 ident_len = CPP_PWRITTEN (pfile) - ident;
2637 hp = cpp_lookup (pfile, ident, ident_len, -1);
2638 if (!hp)
2639 return CPP_NAME;
2640 if (hp->type == T_DISABLED)
2642 if (pfile->output_escapes)
2643 { /* Return "\r-IDENT", followed by '\0'. */
2644 int i;
2645 CPP_RESERVE (pfile, 3);
2646 ident = pfile->token_buffer + before_name_written;
2647 CPP_ADJUST_WRITTEN (pfile, 2);
2648 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2649 ident[0] = '\r';
2650 ident[1] = '-';
2652 return CPP_NAME;
2655 /* If macro wants an arglist, verify that a '(' follows.
2656 first skip all whitespace, copying it to the output
2657 after the macro name. Then, if there is no '(',
2658 decide this is not a macro call and leave things that way. */
2659 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2661 int is_macro_call, macbuf_whitespace = 0;
2663 parse_set_mark (pfile);
2664 for (;;)
2666 cpp_skip_hspace (pfile);
2667 c = PEEKC ();
2668 is_macro_call = c == '(';
2669 if (c != EOF)
2671 if (c != '\n')
2672 break;
2673 FORWARD (1);
2675 else
2677 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2679 if (CPP_BUFFER (pfile)->mark !=
2680 (CPP_BUFFER (pfile)->cur
2681 - CPP_BUFFER (pfile)->buf))
2682 macbuf_whitespace = 1;
2684 /* The mark goes away automatically when
2685 the buffer is popped. */
2686 cpp_pop_buffer (pfile);
2687 parse_set_mark (pfile);
2689 else
2690 break;
2693 if (!is_macro_call)
2695 parse_goto_mark (pfile);
2696 if (macbuf_whitespace)
2697 CPP_PUTC (pfile, ' ');
2699 else
2700 parse_clear_mark (pfile);
2701 if (!is_macro_call)
2702 return CPP_NAME;
2704 /* This is now known to be a macro call.
2705 Expand the macro, reading arguments as needed,
2706 and push the expansion on the input stack. */
2707 macroexpand (pfile, hp);
2708 CPP_SET_WRITTEN (pfile, before_name_written);
2710 goto get_next;
2712 case ' ': case '\t': case '\v':
2713 for (;;)
2715 CPP_PUTC (pfile, c);
2716 c = PEEKC ();
2717 if (c == EOF || !is_hor_space[c])
2718 break;
2719 FORWARD(1);
2721 return CPP_HSPACE;
2723 case '\r':
2724 if (CPP_BUFFER (pfile)->has_escapes)
2726 c = GETC ();
2727 if (c == '-')
2729 if (pfile->output_escapes)
2730 CPP_PUTS (pfile, "\r-", 2);
2731 parse_name (pfile, GETC ());
2732 return CPP_NAME;
2734 else if (c == ' ')
2736 CPP_RESERVE (pfile, 2);
2737 if (pfile->output_escapes)
2738 CPP_PUTC_Q (pfile, '\r');
2739 CPP_PUTC_Q (pfile, c);
2740 return CPP_HSPACE;
2742 else
2744 cpp_fatal (pfile,
2745 "internal error: unrecognized escape \\r%c", c);
2746 goto get_next;
2749 else
2751 /* Backslash newline is ignored. */
2752 CPP_BUMP_LINE (pfile);
2753 goto get_next;
2756 case '\n':
2757 CPP_PUTC (pfile, c);
2758 if (pfile->only_seen_white == 0)
2759 pfile->only_seen_white = 1;
2760 CPP_BUMP_LINE (pfile);
2761 if (! CPP_OPTIONS (pfile)->no_line_commands)
2763 pfile->lineno++;
2764 if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2765 output_line_command (pfile, same_file);
2767 return CPP_VSPACE;
2769 case '(': token = CPP_LPAREN; goto char1;
2770 case ')': token = CPP_RPAREN; goto char1;
2771 case '{': token = CPP_LBRACE; goto char1;
2772 case '}': token = CPP_RBRACE; goto char1;
2773 case ',': token = CPP_COMMA; goto char1;
2774 case ';': token = CPP_SEMICOLON; goto char1;
2776 randomchar:
2777 default:
2778 token = CPP_OTHER;
2779 char1:
2780 pfile->only_seen_white = 0;
2781 CPP_PUTC (pfile, c);
2782 return token;
2787 /* Like cpp_get_token, but skip spaces and comments. */
2789 enum cpp_token
2790 cpp_get_non_space_token (pfile)
2791 cpp_reader *pfile;
2793 int old_written = CPP_WRITTEN (pfile);
2794 for (;;)
2796 enum cpp_token token = cpp_get_token (pfile);
2797 if (token != CPP_COMMENT && token != CPP_POP
2798 && token != CPP_HSPACE && token != CPP_VSPACE)
2799 return token;
2800 CPP_SET_WRITTEN (pfile, old_written);
2804 /* Parse an identifier starting with C. */
2806 static void
2807 parse_name (pfile, c)
2808 cpp_reader *pfile;
2809 int c;
2811 for (;;)
2813 if (! is_idchar[c])
2815 FORWARD (-1);
2816 break;
2819 if (c == '$' && CPP_PEDANTIC (pfile))
2820 cpp_pedwarn (pfile, "`$' in identifier");
2822 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
2823 CPP_PUTC_Q (pfile, c);
2824 c = GETC();
2825 if (c == EOF)
2826 break;
2828 CPP_NUL_TERMINATE_Q (pfile);
2829 return;
2832 /* Parse a string starting with C. A single quoted string is treated
2833 like a double -- some programs (e.g., troff) are perverse this way.
2834 (However, a single quoted string is not allowed to extend over
2835 multiple lines.) */
2836 static void
2837 parse_string (pfile, c)
2838 cpp_reader *pfile;
2839 int c;
2841 long start_line, start_column;
2843 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2845 CPP_PUTC (pfile, c);
2846 while (1)
2848 int cc = GETC();
2849 if (cc == EOF)
2851 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2853 /* try harder: this string crosses a macro expansion
2854 boundary. This can happen naturally if -traditional.
2855 Otherwise, only -D can make a macro with an unmatched
2856 quote. */
2857 cpp_pop_buffer (pfile);
2858 continue;
2861 cpp_error_with_line (pfile, start_line, start_column,
2862 "unterminated string or character constant");
2863 if (pfile->multiline_string_line != start_line
2864 && pfile->multiline_string_line != 0)
2865 cpp_error_with_line (pfile,
2866 pfile->multiline_string_line, -1,
2867 "possible real start of unterminated constant");
2868 pfile->multiline_string_line = 0;
2869 break;
2871 CPP_PUTC (pfile, cc);
2872 switch (cc)
2874 case '\n':
2875 CPP_BUMP_LINE (pfile);
2876 pfile->lineno++;
2877 /* Character constants may not extend over multiple lines.
2878 In ANSI, neither may strings. We accept multiline strings
2879 as an extension. */
2880 if (c == '\'')
2882 cpp_error_with_line (pfile, start_line, start_column,
2883 "unterminated character constant");
2884 return;
2886 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2888 cpp_pedwarn_with_line (pfile, start_line, start_column,
2889 "string constant runs past end of line");
2891 if (pfile->multiline_string_line == 0)
2892 pfile->multiline_string_line = start_line;
2893 break;
2895 case '\r':
2896 CPP_ADJUST_WRITTEN (pfile, -1);
2897 if (CPP_BUFFER (pfile)->has_escapes)
2899 cpp_fatal (pfile,
2900 "internal error: \\r escape inside string constant");
2901 FORWARD(1);
2903 else
2904 /* Backslash newline is replaced by nothing at all. */
2905 CPP_BUMP_LINE (pfile);
2906 break;
2908 case '\\':
2909 cc = GETC();
2910 if (cc != EOF)
2911 CPP_PUTC (pfile, cc);
2912 break;
2914 case '\"':
2915 case '\'':
2916 if (cc == c)
2917 return;
2918 break;
2923 /* Read an assertion into the token buffer, converting to
2924 canonical form: `#predicate(a n swe r)' The next non-whitespace
2925 character to read should be the first letter of the predicate.
2926 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2927 with answer (see callers for why). In case of 0, an error has been
2928 printed. */
2929 static int
2930 parse_assertion (pfile)
2931 cpp_reader *pfile;
2933 int c, dropwhite;
2934 cpp_skip_hspace (pfile);
2935 c = PEEKC();
2936 if (! is_idstart[c])
2938 cpp_error (pfile, "assertion predicate is not an identifier");
2939 return 0;
2941 CPP_PUTC(pfile, '#');
2942 FORWARD(1);
2943 parse_name(pfile, c);
2945 c = PEEKC();
2946 if (c != '(')
2948 if (is_hor_space[c] || c == '\r')
2949 cpp_skip_hspace (pfile);
2950 c = PEEKC();
2952 if (c != '(')
2953 return 1;
2955 CPP_PUTC(pfile, '(');
2956 FORWARD(1);
2957 dropwhite = 1;
2958 while ((c = GETC()) != ')')
2960 if (is_hor_space[c])
2962 if (! dropwhite)
2964 CPP_PUTC(pfile, ' ');
2965 dropwhite = 1;
2968 else if (c == '\n' || c == EOF)
2970 if (c == '\n') FORWARD(-1);
2971 cpp_error (pfile, "un-terminated assertion answer");
2972 return 0;
2974 else if (c == '\r')
2975 /* \r cannot be a macro escape here. */
2976 CPP_BUMP_LINE (pfile);
2977 else
2979 CPP_PUTC (pfile, c);
2980 dropwhite = 0;
2984 if (pfile->limit[-1] == ' ')
2985 pfile->limit[-1] = ')';
2986 else if (pfile->limit[-1] == '(')
2988 cpp_error (pfile, "empty token sequence in assertion");
2989 return 0;
2991 else
2992 CPP_PUTC (pfile, ')');
2994 CPP_NUL_TERMINATE (pfile);
2995 return 2;
2998 static int
2999 do_assert (pfile, keyword)
3000 cpp_reader *pfile;
3001 const struct directive *keyword ATTRIBUTE_UNUSED;
3003 char *sym;
3004 int ret, c;
3005 HASHNODE *base, *this;
3006 int baselen, thislen;
3008 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3009 && !CPP_BUFFER (pfile)->system_header_p)
3010 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
3012 cpp_skip_hspace (pfile);
3013 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
3014 ret = parse_assertion (pfile);
3015 if (ret == 0)
3016 goto error;
3017 else if (ret == 1)
3019 cpp_error (pfile, "missing token-sequence in `#assert'");
3020 goto error;
3023 cpp_skip_hspace (pfile);
3024 c = PEEKC();
3025 if (c != EOF && c != '\n')
3027 cpp_error (pfile, "junk at end of `#assert'");
3028 goto error;
3031 thislen = strlen (sym);
3032 baselen = index (sym, '(') - sym;
3033 this = cpp_lookup (pfile, sym, thislen, -1);
3034 if (this)
3036 cpp_warning (pfile, "`%s' re-asserted", sym);
3037 goto error;
3040 base = cpp_lookup (pfile, sym, baselen, -1);
3041 if (! base)
3042 base = cpp_install (pfile, sym, baselen, T_ASSERT, 0, -1);
3043 else if (base->type != T_ASSERT)
3045 /* Token clash - but with what?! */
3046 cpp_fatal (pfile,
3047 "cpp internal error: base->type != T_ASSERT in do_assert");
3048 goto error;
3051 this = cpp_install (pfile, sym, thislen, T_ASSERT,
3052 (char *)base->value.aschain, -1);
3053 base->value.aschain = this;
3055 pfile->limit = (unsigned char *) sym; /* Pop */
3056 return 0;
3058 error:
3059 skip_rest_of_line (pfile);
3060 pfile->limit = (unsigned char *) sym; /* Pop */
3061 return 0;
3064 static int
3065 do_unassert (pfile, keyword)
3066 cpp_reader *pfile;
3067 const struct directive *keyword ATTRIBUTE_UNUSED;
3069 int c, ret;
3070 char *sym;
3071 long baselen, thislen;
3072 HASHNODE *base, *this, *next;
3074 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3075 && !CPP_BUFFER (pfile)->system_header_p)
3076 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
3078 cpp_skip_hspace (pfile);
3080 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
3081 ret = parse_assertion (pfile);
3082 if (ret == 0)
3083 goto error;
3085 cpp_skip_hspace (pfile);
3086 c = PEEKC ();
3087 if (c != EOF && c != '\n')
3088 cpp_error (pfile, "junk at end of `#unassert'");
3090 thislen = strlen (sym);
3091 if (ret == 1)
3093 base = cpp_lookup (pfile, sym, thislen, -1);
3094 if (! base)
3095 goto error; /* It isn't an error to #undef what isn't #defined,
3096 so it isn't an error to #unassert what isn't
3097 #asserted either. */
3099 for (this = base->value.aschain; this; this = next)
3101 next = this->value.aschain;
3102 delete_macro (this);
3104 delete_macro (base);
3106 else
3108 baselen = index (sym, '(') - sym;
3109 base = cpp_lookup (pfile, sym, baselen, -1);
3110 if (! base) goto error;
3111 this = cpp_lookup (pfile, sym, thislen, -1);
3112 if (! this) goto error;
3114 next = base;
3115 while (next->value.aschain != this)
3116 next = next->value.aschain;
3118 next->value.aschain = this->value.aschain;
3119 delete_macro (this);
3121 if (base->value.aschain == NULL)
3122 delete_macro (base); /* Last answer for this predicate deleted. */
3125 pfile->limit = (unsigned char *) sym; /* Pop */
3126 return 0;
3127 error:
3128 skip_rest_of_line (pfile);
3129 pfile->limit = (unsigned char *) sym; /* Pop */
3130 return 0;
3133 /* Process STR as if it appeared as the body of an #unassert. */
3134 void
3135 cpp_unassert (pfile, str)
3136 cpp_reader *pfile;
3137 unsigned char *str;
3139 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
3141 do_assert (pfile, NULL);
3142 cpp_pop_buffer (pfile);
3147 cpp_read_check_assertion (pfile)
3148 cpp_reader *pfile;
3150 U_CHAR *name = CPP_PWRITTEN (pfile);
3151 int result;
3152 HASHNODE *hp;
3154 FORWARD (1); /* Skip '#' */
3155 cpp_skip_hspace (pfile);
3156 if (! parse_assertion (pfile))
3157 result = 0;
3158 else
3160 hp = cpp_lookup (pfile, name, CPP_PWRITTEN (pfile) - name, -1);
3161 result = (hp != 0);
3164 pfile->limit = name;
3165 return result;
3168 /* Remember the current position of PFILE. */
3170 void
3171 parse_set_mark (pfile)
3172 cpp_reader *pfile;
3174 cpp_buffer *ip = CPP_BUFFER (pfile);
3175 if (ip->mark != -1)
3176 cpp_fatal (pfile,
3177 "cpp internal error: ip->mark != -1 in parse_set_mark");
3179 ip->mark = ip->cur - ip->buf;
3182 /* Clear the current mark - we no longer need it. */
3184 void
3185 parse_clear_mark (pfile)
3186 cpp_reader *pfile;
3188 cpp_buffer *ip = CPP_BUFFER (pfile);
3189 if (ip->mark == -1)
3190 cpp_fatal (pfile,
3191 "cpp internal error: ip->mark == -1 in parse_clear_mark");
3193 ip->mark = -1;
3196 /* Backup the current position of PFILE to that saved in its mark,
3197 and clear the mark. */
3199 void
3200 parse_goto_mark (pfile)
3201 cpp_reader *pfile;
3203 cpp_buffer *ip = CPP_BUFFER (pfile);
3204 if (ip->mark == -1)
3205 cpp_fatal (pfile,
3206 "cpp internal error: ip->mark == -1 in parse_goto_mark");
3208 ip->cur = ip->buf + ip->mark;
3209 ip->mark = -1;
3212 static void
3213 cpp_print_file_and_line (pfile)
3214 cpp_reader *pfile;
3216 cpp_buffer *ip = cpp_file_buffer (pfile);
3218 if (ip != NULL)
3220 long line, col;
3221 cpp_buf_line_and_col (ip, &line, &col);
3222 cpp_file_line_for_message (pfile, ip->nominal_fname,
3223 line, pfile->show_column ? col : -1);
3227 static void
3228 v_cpp_error (pfile, msgid, ap)
3229 cpp_reader *pfile;
3230 const char *msgid;
3231 va_list ap;
3233 cpp_print_containing_files (pfile);
3234 cpp_print_file_and_line (pfile);
3235 v_cpp_message (pfile, 1, msgid, ap);
3238 void
3239 cpp_error VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3241 #ifndef ANSI_PROTOTYPES
3242 cpp_reader *pfile;
3243 const char *msgid;
3244 #endif
3245 va_list ap;
3247 VA_START(ap, msgid);
3249 #ifndef ANSI_PROTOTYPES
3250 pfile = va_arg (ap, cpp_reader *);
3251 msgid = va_arg (ap, const char *);
3252 #endif
3254 v_cpp_error (pfile, msgid, ap);
3255 va_end(ap);
3258 /* Print error message but don't count it. */
3260 static void
3261 v_cpp_warning (pfile, msgid, ap)
3262 cpp_reader *pfile;
3263 const char *msgid;
3264 va_list ap;
3266 if (CPP_OPTIONS (pfile)->inhibit_warnings)
3267 return;
3269 if (CPP_OPTIONS (pfile)->warnings_are_errors)
3270 pfile->errors++;
3272 cpp_print_containing_files (pfile);
3273 cpp_print_file_and_line (pfile);
3274 v_cpp_message (pfile, 0, msgid, ap);
3277 void
3278 cpp_warning VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3280 #ifndef ANSI_PROTOTYPES
3281 cpp_reader *pfile;
3282 const char *msgid;
3283 #endif
3284 va_list ap;
3286 VA_START (ap, msgid);
3288 #ifndef ANSI_PROTOTYPES
3289 pfile = va_arg (ap, cpp_reader *);
3290 msgid = va_arg (ap, const char *);
3291 #endif
3293 v_cpp_warning (pfile, msgid, ap);
3294 va_end(ap);
3297 /* Print an error message and maybe count it. */
3299 void
3300 cpp_pedwarn VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3302 #ifndef ANSI_PROTOTYPES
3303 cpp_reader *pfile;
3304 const char *msgid;
3305 #endif
3306 va_list ap;
3308 VA_START (ap, msgid);
3310 #ifndef ANSI_PROTOTYPES
3311 pfile = va_arg (ap, cpp_reader *);
3312 msgid = va_arg (ap, const char *);
3313 #endif
3315 if (CPP_OPTIONS (pfile)->pedantic_errors)
3316 v_cpp_error (pfile, msgid, ap);
3317 else
3318 v_cpp_warning (pfile, msgid, ap);
3319 va_end(ap);
3322 static void
3323 v_cpp_error_with_line (pfile, line, column, msgid, ap)
3324 cpp_reader * pfile;
3325 int line;
3326 int column;
3327 const char * msgid;
3328 va_list ap;
3330 cpp_buffer *ip = cpp_file_buffer (pfile);
3332 cpp_print_containing_files (pfile);
3334 if (ip != NULL)
3335 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
3337 v_cpp_message (pfile, 1, msgid, ap);
3340 void
3341 cpp_error_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3342 const char *msgid, ...))
3344 #ifndef ANSI_PROTOTYPES
3345 cpp_reader *pfile;
3346 int line;
3347 int column;
3348 const char *msgid;
3349 #endif
3350 va_list ap;
3352 VA_START (ap, msgid);
3354 #ifndef ANSI_PROTOTYPES
3355 pfile = va_arg (ap, cpp_reader *);
3356 line = va_arg (ap, int);
3357 column = va_arg (ap, int);
3358 msgid = va_arg (ap, const char *);
3359 #endif
3361 v_cpp_error_with_line(pfile, line, column, msgid, ap);
3362 va_end(ap);
3365 static void
3366 v_cpp_warning_with_line (pfile, line, column, msgid, ap)
3367 cpp_reader * pfile;
3368 int line;
3369 int column;
3370 const char *msgid;
3371 va_list ap;
3373 cpp_buffer *ip;
3375 if (CPP_OPTIONS (pfile)->inhibit_warnings)
3376 return;
3378 if (CPP_OPTIONS (pfile)->warnings_are_errors)
3379 pfile->errors++;
3381 cpp_print_containing_files (pfile);
3383 ip = cpp_file_buffer (pfile);
3385 if (ip != NULL)
3386 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
3388 v_cpp_message (pfile, 0, msgid, ap);
3391 void
3392 cpp_warning_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3393 const char *msgid, ...))
3395 #ifndef ANSI_PROTOTYPES
3396 cpp_reader *pfile;
3397 int line;
3398 int column;
3399 const char *msgid;
3400 #endif
3401 va_list ap;
3403 VA_START (ap, msgid);
3405 #ifndef ANSI_PROTOTYPES
3406 pfile = va_arg (ap, cpp_reader *);
3407 line = va_arg (ap, int);
3408 column = va_arg (ap, int);
3409 msgid = va_arg (ap, const char *);
3410 #endif
3412 v_cpp_warning_with_line (pfile, line, column, msgid, ap);
3413 va_end(ap);
3416 void
3417 cpp_pedwarn_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3418 const char *msgid, ...))
3420 #ifndef ANSI_PROTOTYPES
3421 cpp_reader *pfile;
3422 int line;
3423 int column;
3424 const char *msgid;
3425 #endif
3426 va_list ap;
3428 VA_START (ap, msgid);
3430 #ifndef ANSI_PROTOTYPES
3431 pfile = va_arg (ap, cpp_reader *);
3432 line = va_arg (ap, int);
3433 column = va_arg (ap, int);
3434 msgid = va_arg (ap, const char *);
3435 #endif
3437 if (CPP_OPTIONS (pfile)->pedantic_errors)
3438 v_cpp_error_with_line (pfile, column, line, msgid, ap);
3439 else
3440 v_cpp_warning_with_line (pfile, line, column, msgid, ap);
3441 va_end(ap);
3444 /* Report a warning (or an error if pedantic_errors)
3445 giving specified file name and line number, not current. */
3447 void
3448 cpp_pedwarn_with_file_and_line VPROTO ((cpp_reader *pfile, const char *file,
3449 int line, const char *msgid, ...))
3451 #ifndef ANSI_PROTOTYPES
3452 cpp_reader *pfile;
3453 const char *file;
3454 int line;
3455 const char *msgid;
3456 #endif
3457 va_list ap;
3459 VA_START (ap, msgid);
3461 #ifndef ANSI_PROTOTYPES
3462 pfile = va_arg (ap, cpp_reader *);
3463 file = va_arg (ap, const char *);
3464 line = va_arg (ap, int);
3465 msgid = va_arg (ap, const char *);
3466 #endif
3468 if (!CPP_OPTIONS (pfile)->pedantic_errors
3469 && CPP_OPTIONS (pfile)->inhibit_warnings)
3470 return;
3471 if (file != NULL)
3472 cpp_file_line_for_message (pfile, file, line, -1);
3473 v_cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, msgid, ap);
3474 va_end(ap);
3477 /* my_strerror - return the descriptive text associated with an
3478 `errno' code. */
3480 static const char *
3481 my_strerror (errnum)
3482 int errnum;
3484 const char *result;
3486 #ifndef VMS
3487 #ifndef HAVE_STRERROR
3488 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
3489 #else
3490 result = strerror (errnum);
3491 #endif
3492 #else /* VMS */
3493 /* VAXCRTL's strerror() takes an optional second argument, which only
3494 matters when the first argument is EVMSERR. However, it's simplest
3495 just to pass it unconditionally. `vaxc$errno' is declared in
3496 <errno.h>, and maintained by the library in parallel with `errno'.
3497 We assume that caller's `errnum' either matches the last setting of
3498 `errno' by the library or else does not have the value `EVMSERR'. */
3500 result = strerror (errnum, vaxc$errno);
3501 #endif
3503 if (!result)
3504 result = "errno = ?";
3506 return result;
3509 /* Error including a message from `errno'. */
3511 void
3512 cpp_error_from_errno (pfile, name)
3513 cpp_reader *pfile;
3514 const char *name;
3516 cpp_message_from_errno (pfile, 1, name);
3519 void
3520 cpp_message_from_errno (pfile, is_error, name)
3521 cpp_reader *pfile;
3522 int is_error;
3523 const char *name;
3525 int e = errno;
3526 cpp_buffer *ip = cpp_file_buffer (pfile);
3528 cpp_print_containing_files (pfile);
3530 if (ip != NULL)
3531 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
3533 cpp_message (pfile, is_error, "%s: %s", name, my_strerror (e));
3536 void
3537 cpp_perror_with_name (pfile, name)
3538 cpp_reader *pfile;
3539 const char *name;
3541 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
3544 /* TODO:
3545 * No pre-compiled header file support.
3547 * Possibly different enum token codes for each C/C++ token.
3549 * Find and cleanup remaining uses of static variables,
3551 * Support -dM flag (dump_all_macros).
3553 * Support for_lint flag.