* mips.md: Define conditional move patterns for floating point
[official-gcc.git] / gcc / cpplib.c
blob7e41d1b4228a6de2ddc736ebad65a13553a2737b
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 *));
52 static const char *if_directive_name PARAMS ((cpp_reader *,
53 struct if_stack *));
55 /* External declarations. */
57 extern HOST_WIDEST_INT cpp_parse_expr PARAMS ((cpp_reader *));
59 /* `struct directive' defines one #-directive, including how to handle it. */
61 struct directive {
62 int length; /* Length of name */
63 int (*func) /* Function to handle directive */
64 PARAMS ((cpp_reader *, const struct directive *));
65 const char *name; /* Name of directive */
66 enum node_type type; /* Code which describes which directive. */
69 /* These functions are declared to return int instead of void since they
70 are going to be placed in a table and some old compilers have trouble with
71 pointers to functions returning void. */
73 static int do_define PARAMS ((cpp_reader *, const struct directive *));
74 static int do_line PARAMS ((cpp_reader *, const struct directive *));
75 static int do_include PARAMS ((cpp_reader *, const struct directive *));
76 static int do_undef PARAMS ((cpp_reader *, const struct directive *));
77 static int do_error PARAMS ((cpp_reader *, const struct directive *));
78 static int do_pragma PARAMS ((cpp_reader *, const struct directive *));
79 static int do_ident PARAMS ((cpp_reader *, const struct directive *));
80 static int do_if PARAMS ((cpp_reader *, const struct directive *));
81 static int do_xifdef PARAMS ((cpp_reader *, const struct directive *));
82 static int do_else PARAMS ((cpp_reader *, const struct directive *));
83 static int do_elif PARAMS ((cpp_reader *, const struct directive *));
84 static int do_endif PARAMS ((cpp_reader *, const struct directive *));
85 #ifdef SCCS_DIRECTIVE
86 static int do_sccs PARAMS ((cpp_reader *, const struct directive *));
87 #endif
88 static int do_assert PARAMS ((cpp_reader *, const struct directive *));
89 static int do_unassert PARAMS ((cpp_reader *, const struct directive *));
90 static int do_warning PARAMS ((cpp_reader *, const struct directive *));
91 static enum cpp_token null_underflow PARAMS ((cpp_reader *));
92 static int null_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
93 static int skip_comment PARAMS ((cpp_reader *, int));
94 static int copy_comment PARAMS ((cpp_reader *, int));
95 static void copy_rest_of_line PARAMS ((cpp_reader *));
96 static int handle_directive PARAMS ((cpp_reader *));
97 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t, cpp_reader *,
98 const struct directive *));
99 static enum cpp_token get_directive_token PARAMS ((cpp_reader *));
100 static int read_line_number PARAMS ((cpp_reader *, int *));
101 static void cpp_print_file_and_line PARAMS ((cpp_reader *));
102 static void v_cpp_error PARAMS ((cpp_reader *, const char *, va_list));
103 static void v_cpp_warning PARAMS ((cpp_reader *, const char *, va_list));
104 static void v_cpp_error_with_line PARAMS ((cpp_reader *, int, int,
105 const char *, va_list));
106 static void v_cpp_warning_with_line PARAMS ((cpp_reader *, int, int, const char *, va_list));
107 static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
108 static int consider_directive_while_skipping PARAMS ((cpp_reader *, IF_STACK_FRAME *));
110 /* Here is the actual list of #-directives.
111 This table is ordered by frequency of occurrence; the numbers
112 at the end are directive counts from all the source code I have
113 lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
114 linux-2.2.9, and pcmcia-cs-3.0.9). */
116 static const struct directive directive_table[] = {
117 /* In C89 */
118 { 6, do_define, "define", T_DEFINE }, /* 270554 */
119 { 7, do_include, "include", T_INCLUDE }, /* 52262 */
120 { 5, do_endif, "endif", T_ENDIF }, /* 45855 */
121 { 5, do_xifdef, "ifdef", T_IFDEF }, /* 22000 */
122 { 2, do_if, "if", T_IF }, /* 18162 */
123 { 4, do_else, "else", T_ELSE }, /* 9863 */
124 { 6, do_xifdef, "ifndef", T_IFNDEF }, /* 9675 */
125 { 5, do_undef, "undef", T_UNDEF }, /* 4837 */
126 { 4, do_line, "line", T_LINE }, /* 2465 */
127 { 4, do_elif, "elif", T_ELIF }, /* 610 */
128 { 5, do_error, "error", T_ERROR }, /* 475 */
129 { 6, do_pragma, "pragma", T_PRAGMA }, /* 195 */
131 /* Extensions. All deprecated except #warning and #include_next. */
132 { 7, do_warning, "warning", T_WARNING }, /* 22 - GNU */
133 { 12, do_include, "include_next", T_INCLUDE_NEXT }, /* 19 - GNU */
134 { 5, do_ident, "ident", T_IDENT }, /* 11 - SVR4 */
135 { 6, do_include, "import", T_IMPORT }, /* 0 - ObjC */
136 { 6, do_assert, "assert", T_ASSERT }, /* 0 - SVR4 */
137 { 8, do_unassert, "unassert", T_UNASSERT }, /* 0 - SVR4 */
138 #ifdef SCCS_DIRECTIVE
139 { 4, do_sccs, "sccs", T_SCCS }, /* 0 - SVR2? */
140 #endif
141 { -1, 0, "", T_UNUSED }
144 /* Place into PFILE a quoted string representing the string SRC.
145 Caller must reserve enough space in pfile->token_buffer. */
147 void
148 quote_string (pfile, src)
149 cpp_reader *pfile;
150 const char *src;
152 U_CHAR c;
154 CPP_PUTC_Q (pfile, '\"');
155 for (;;)
156 switch ((c = *src++))
158 default:
159 if (ISPRINT (c))
160 CPP_PUTC_Q (pfile, c);
161 else
163 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
164 CPP_ADJUST_WRITTEN (pfile, 4);
166 break;
168 case '\"':
169 case '\\':
170 CPP_PUTC_Q (pfile, '\\');
171 CPP_PUTC_Q (pfile, c);
172 break;
174 case '\0':
175 CPP_PUTC_Q (pfile, '\"');
176 CPP_NUL_TERMINATE_Q (pfile);
177 return;
181 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
183 void
184 cpp_grow_buffer (pfile, n)
185 cpp_reader *pfile;
186 long n;
188 long old_written = CPP_WRITTEN (pfile);
189 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
190 pfile->token_buffer = (U_CHAR *)
191 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
192 CPP_SET_WRITTEN (pfile, old_written);
195 /* Process the string STR as if it appeared as the body of a #define
196 If STR is just an identifier, define it with value 1.
197 If STR has anything after the identifier, then it should
198 be identifier=definition. */
200 void
201 cpp_define (pfile, str)
202 cpp_reader *pfile;
203 U_CHAR *str;
205 U_CHAR *buf, *p;
206 size_t count;
208 /* Copy the entire option so we can modify it. */
209 count = strlen (str) + 3;
210 buf = (U_CHAR *) alloca (count);
211 memcpy (buf, str, count - 2);
212 /* Change the first "=" in the string to a space. If there is none,
213 tack " 1" on the end. */
214 p = (U_CHAR *) strchr (buf, '=');
215 if (p)
217 *p = ' ';
218 count -= 2;
220 else
221 strcpy (&buf[count-3], " 1");
223 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
225 do_define (pfile, NULL);
226 cpp_pop_buffer (pfile);
230 /* Process the string STR as if it appeared as the body of a #assert. */
231 void
232 cpp_assert (pfile, str)
233 cpp_reader *pfile;
234 U_CHAR *str;
236 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
238 do_assert (pfile, NULL);
239 cpp_pop_buffer (pfile);
244 static enum cpp_token
245 null_underflow (pfile)
246 cpp_reader *pfile ATTRIBUTE_UNUSED;
248 return CPP_EOF;
251 static int
252 null_cleanup (pbuf, pfile)
253 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
254 cpp_reader *pfile ATTRIBUTE_UNUSED;
256 return 0;
259 /* Skip a comment - C, C++, or Chill style. M is the first character
260 of the comment marker. If this really is a comment, skip to its
261 end and return ' '. If we hit end-of-file before end-of-comment,
262 return EOF. If this is not a comment, return M (which will be
263 '/' or '-'). */
265 static int
266 skip_comment (pfile, m)
267 cpp_reader *pfile;
268 int m;
270 if (m == '/' && PEEKC() == '*')
272 int c, prev_c = -1;
273 long line, col;
275 FORWARD(1);
276 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
277 for (;;)
279 c = GETC ();
280 if (c == EOF)
282 cpp_error_with_line (pfile, line, col, "unterminated comment");
283 return EOF;
285 else if (c == '\n' || c == '\r')
286 /* \r cannot be a macro escape marker here. */
287 CPP_BUMP_LINE (pfile);
288 else if (c == '/' && prev_c == '*')
289 return ' ';
290 else if (c == '*' && prev_c == '/'
291 && CPP_OPTIONS (pfile)->warn_comments)
292 cpp_warning (pfile, "`/*' within comment");
294 prev_c = c;
297 else if ((m == '/' && PEEKC() == '/'
298 && CPP_OPTIONS (pfile)->cplusplus_comments)
299 || (m == '-' && PEEKC() == '-'
300 && CPP_OPTIONS (pfile)->chill))
302 FORWARD(1);
303 for (;;)
305 int c = GETC ();
306 if (c == EOF)
307 return ' '; /* Allow // to be terminated by EOF. */
308 if (c == '\n')
310 /* Don't consider final '\n' to be part of comment. */
311 FORWARD(-1);
312 return ' ';
314 else if (c == '\r')
315 /* \r cannot be a macro escape marker here. */
316 CPP_BUMP_LINE (pfile);
319 else
320 return m;
323 /* Identical to skip_comment except that it copies the comment into the
324 token_buffer. This is used if put_out_comments. */
325 static int
326 copy_comment (pfile, m)
327 cpp_reader *pfile;
328 int m;
330 if (m == '/' && PEEKC() == '*')
332 int c, prev_c = -1;
333 long line, col;
335 CPP_PUTC (pfile, '/');
336 CPP_PUTC (pfile, '*');
337 FORWARD(1);
338 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
339 for (;;)
341 c = GETC ();
342 if (c == EOF)
344 cpp_error_with_line (pfile, line, col, "unterminated comment");
345 /* We must pretend this was a legitimate comment, so that the
346 output in token_buffer is not passed back tagged CPP_POP. */
347 return ' ';
349 else if (c == '\r')
351 /* \r cannot be a macro escape marker here. */
352 CPP_BUMP_LINE (pfile);
353 continue;
356 CPP_PUTC (pfile, c);
357 if (c == '\n')
359 pfile->lineno++;
360 CPP_BUMP_LINE (pfile);
362 else if (c == '/' && prev_c == '*')
363 return ' ';
364 else if (c == '*' && prev_c == '/'
365 && CPP_OPTIONS (pfile)->warn_comments)
366 cpp_warning (pfile, "`/*' within comment");
368 prev_c = c;
371 else if ((m == '/' && PEEKC() == '/'
372 && CPP_OPTIONS (pfile)->cplusplus_comments)
373 || (m == '-' && PEEKC() == '-'
374 && CPP_OPTIONS (pfile)->chill))
376 CPP_PUTC (pfile, m);
377 CPP_PUTC (pfile, m);
378 FORWARD(1);
379 for (;;)
381 int c = GETC ();
382 if (c == EOF)
383 return ' '; /* Allow line comments to be terminated by EOF. */
384 else if (c == '\n')
386 /* Don't consider final '\n' to be part of comment. */
387 FORWARD(-1);
388 return ' ';
390 else if (c == '\r')
391 /* \r cannot be a macro escape marker here. */
392 CPP_BUMP_LINE (pfile);
394 CPP_PUTC (pfile, c);
397 else
398 return m;
402 /* Skip whitespace \-newline and comments. Does not macro-expand. */
404 void
405 cpp_skip_hspace (pfile)
406 cpp_reader *pfile;
408 int c;
409 while (1)
411 c = GETC();
412 if (c == EOF)
413 return;
414 else if (is_hor_space[c])
416 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
417 cpp_pedwarn (pfile, "%s in preprocessing directive",
418 c == '\f' ? "formfeed" : "vertical tab");
420 else if (c == '\r')
422 /* \r is a backslash-newline marker if !has_escapes, and
423 a deletable-whitespace or no-reexpansion marker otherwise. */
424 if (CPP_BUFFER (pfile)->has_escapes)
426 if (PEEKC() == ' ')
427 FORWARD(1);
428 else
429 break;
431 else
432 CPP_BUFFER (pfile)->lineno++;
434 else if (c == '/' || c == '-')
436 c = skip_comment (pfile, c);
437 if (c == EOF)
438 return;
439 else if (c != ' ')
440 break;
442 else
443 break;
445 FORWARD(-1);
448 /* Read the rest of the current line.
449 The line is appended to PFILE's output buffer. */
451 static void
452 copy_rest_of_line (pfile)
453 cpp_reader *pfile;
455 for (;;)
457 int c = GETC();
458 switch (c)
460 case '\n':
461 FORWARD(-1);
462 case EOF:
463 CPP_NUL_TERMINATE (pfile);
464 return;
466 case '\r':
467 if (CPP_BUFFER (pfile)->has_escapes)
468 break;
469 else
471 CPP_BUFFER (pfile)->lineno++;
472 continue;
474 case '\'':
475 case '\"':
476 parse_string (pfile, c);
477 continue;
478 case '/':
479 if (PEEKC() == '*' && CPP_TRADITIONAL (pfile))
481 CPP_PUTS (pfile, "/**/", 4);
482 skip_comment (pfile, c);
483 continue;
485 /* else fall through */
486 case '-':
487 c = skip_comment (pfile, c);
488 break;
490 case '\f':
491 case '\v':
492 if (CPP_PEDANTIC (pfile))
493 cpp_pedwarn (pfile, "%s in preprocessing directive",
494 c == '\f' ? "formfeed" : "vertical tab");
495 break;
498 CPP_PUTC (pfile, c);
502 /* FIXME: It is almost definitely a performance win to make this do
503 the scan itself. >75% of calls to copy_r_o_l are from here or
504 skip_if_group, which means the common case is to copy stuff into the
505 token_buffer only to discard it. */
506 void
507 skip_rest_of_line (pfile)
508 cpp_reader *pfile;
510 long old = CPP_WRITTEN (pfile);
511 copy_rest_of_line (pfile);
512 CPP_SET_WRITTEN (pfile, old);
515 /* Handle a possible # directive.
516 '#' has already been read. */
518 static int
519 handle_directive (pfile)
520 cpp_reader *pfile;
522 int c;
523 register const struct directive *kt;
524 int ident_length;
525 U_CHAR *ident;
526 long old_written = CPP_WRITTEN (pfile);
528 cpp_skip_hspace (pfile);
530 c = PEEKC ();
531 if (c >= '0' && c <= '9')
533 /* Handle # followed by a line number. Complain about using that
534 form if we're being pedantic, but not if this is regurgitated
535 input (preprocessed or fed back in by the C++ frontend). */
536 if (CPP_PEDANTIC (pfile)
537 && ! CPP_PREPROCESSED (pfile)
538 && ! CPP_BUFFER (pfile)->manual_pop)
539 cpp_pedwarn (pfile, "`#' followed by integer");
540 do_line (pfile, NULL);
541 return 1;
544 /* Now find the directive name. */
545 CPP_PUTC (pfile, '#');
546 parse_name (pfile, GETC());
547 ident = pfile->token_buffer + old_written + 1;
548 ident_length = CPP_PWRITTEN (pfile) - ident;
549 if (ident_length == 0)
551 /* A line of just `#' becomes blank. */
552 if (PEEKC() == '\n')
553 return 1;
554 else
555 return 0;
559 * Decode the keyword and call the appropriate expansion
560 * routine, after moving the input pointer up to the next line.
562 for (kt = directive_table; ; kt++)
564 if (kt->length <= 0)
565 return 0;
566 if (kt->length == ident_length
567 && !strncmp (kt->name, ident, ident_length))
568 break;
571 CPP_SET_WRITTEN (pfile, old_written);
572 (*kt->func) (pfile, kt);
574 return 1;
577 /* Pass a directive through to the output file.
578 BUF points to the contents of the directive, as a contiguous string.
579 LEN is the length of the string pointed to by BUF.
580 KEYWORD is the keyword-table entry for the directive. */
582 static void
583 pass_thru_directive (buf, len, pfile, keyword)
584 const U_CHAR *buf;
585 size_t len;
586 cpp_reader *pfile;
587 const struct directive *keyword;
589 register unsigned keyword_length = keyword->length;
591 CPP_RESERVE (pfile, 1 + keyword_length + len);
592 CPP_PUTC_Q (pfile, '#');
593 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
594 if (len != 0 && buf[0] != ' ')
595 CPP_PUTC_Q (pfile, ' ');
596 CPP_PUTS_Q (pfile, buf, len);
599 /* Check a purported macro name SYMNAME, and yield its length.
600 ASSERTION is nonzero if this is really for an assertion name. */
603 check_macro_name (pfile, symname, assertion)
604 cpp_reader *pfile;
605 const U_CHAR *symname;
606 int assertion;
608 const U_CHAR *p;
609 int sym_length;
611 for (p = symname; is_idchar[*p]; p++)
613 sym_length = p - symname;
614 if (sym_length == 0
615 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
616 cpp_error (pfile,
617 assertion ? "invalid assertion name" : "invalid macro name");
618 else if (!is_idstart[*symname]
619 || (! strncmp (symname, "defined", 7) && sym_length == 7)) {
620 U_CHAR *msg; /* what pain... */
621 msg = (U_CHAR *) alloca (sym_length + 1);
622 bcopy (symname, msg, sym_length);
623 msg[sym_length] = 0;
624 cpp_error (pfile,
625 (assertion
626 ? "invalid assertion name `%s'"
627 : "invalid macro name `%s'"),
628 msg);
630 return sym_length;
633 /* Process a #define command.
634 KEYWORD is the keyword-table entry for #define,
635 or NULL for a "predefined" macro,
636 or the keyword-table entry for #pragma in the case of a #pragma poison. */
638 static int
639 do_define (pfile, keyword)
640 cpp_reader *pfile;
641 const struct directive *keyword;
643 int hashcode;
644 MACRODEF mdef;
645 HASHNODE *hp;
646 long here;
647 U_CHAR *macro, *buf, *end;
648 enum node_type new_type;
650 here = CPP_WRITTEN (pfile);
651 copy_rest_of_line (pfile);
653 if (keyword == NULL || keyword->type == T_DEFINE)
654 new_type = T_MACRO;
655 else
656 new_type = T_POISON;
658 /* Copy out the line so we can pop the token buffer. */
659 buf = pfile->token_buffer + here;
660 end = CPP_PWRITTEN (pfile);
661 macro = (U_CHAR *) alloca (end - buf + 1);
662 bcopy (buf, macro, end - buf + 1);
663 end = macro + (end - buf);
665 CPP_SET_WRITTEN (pfile, here);
667 mdef = create_definition (macro, end, pfile, keyword == NULL);
668 if (mdef.defn == 0)
669 return 0;
671 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
673 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
675 int ok = 0;
676 /* Redefining a precompiled key is ok. */
677 if (hp->type == T_PCSTRING)
678 ok = 1;
679 /* Redefining a poisoned identifier is even worse than `not ok'. */
680 else if (hp->type == T_POISON)
681 ok = -1;
682 /* Redefining a macro is ok if the definitions are the same. */
683 else if (hp->type == T_MACRO)
684 ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
685 /* Redefining a constant is ok with -D. */
686 else if (hp->type == T_CONST || hp->type == T_STDC)
687 ok = ! CPP_OPTIONS (pfile)->done_initializing;
688 /* Print the warning or error if it's not ok. */
689 if (ok <= 0)
691 if (hp->type == T_POISON)
692 cpp_error (pfile, "redefining poisoned `%.*s'",
693 mdef.symlen, mdef.symnam);
694 else
695 cpp_pedwarn (pfile, "`%.*s' redefined", mdef.symlen, mdef.symnam);
696 if (hp->type == T_MACRO)
697 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file,
698 hp->value.defn->line,
699 "this is the location of the previous definition");
701 if (hp->type != T_POISON)
703 /* Replace the old definition. */
704 hp->type = new_type;
705 hp->value.defn = mdef.defn;
708 else
709 cpp_install (pfile, mdef.symnam, mdef.symlen, new_type,
710 (char *) mdef.defn, hashcode);
712 if (keyword != NULL && keyword->type == T_DEFINE)
714 if (CPP_OPTIONS (pfile)->debug_output
715 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
716 dump_definition (pfile, mdef);
717 else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
718 pass_thru_directive (mdef.symnam, mdef.symlen, pfile, keyword);
721 return 0;
725 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
726 If BUFFER != NULL, then use the LENGTH characters in BUFFER
727 as the new input buffer.
728 Return the new buffer, or NULL on failure. */
730 cpp_buffer *
731 cpp_push_buffer (pfile, buffer, length)
732 cpp_reader *pfile;
733 U_CHAR *buffer;
734 long length;
736 cpp_buffer *buf = CPP_BUFFER (pfile);
737 cpp_buffer *new;
738 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
740 cpp_fatal (pfile, "macro or `#include' recursion too deep");
741 return NULL;
744 new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
746 new->if_stack = pfile->if_stack;
747 new->cleanup = null_cleanup;
748 new->underflow = null_underflow;
749 new->buf = new->cur = buffer;
750 new->alimit = new->rlimit = buffer + length;
751 new->prev = buf;
752 new->mark = -1;
754 CPP_BUFFER (pfile) = new;
755 return new;
758 cpp_buffer *
759 cpp_pop_buffer (pfile)
760 cpp_reader *pfile;
762 cpp_buffer *buf = CPP_BUFFER (pfile);
763 (*buf->cleanup) (buf, pfile);
764 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
765 free (buf);
766 pfile->buffer_stack_depth--;
767 return CPP_BUFFER (pfile);
770 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
771 Pop the buffer when done. */
773 void
774 cpp_scan_buffer (pfile)
775 cpp_reader *pfile;
777 cpp_buffer *buffer = CPP_BUFFER (pfile);
778 enum cpp_token token;
779 if (CPP_OPTIONS (pfile)->no_output)
781 long old_written = CPP_WRITTEN (pfile);
782 /* In no-output mode, we can ignore everything but directives. */
783 for (;;)
785 if (! pfile->only_seen_white)
786 skip_rest_of_line (pfile);
787 token = cpp_get_token (pfile);
788 if (token == CPP_EOF) /* Should not happen ... */
789 break;
790 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
792 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
793 != CPP_NULL_BUFFER (pfile))
794 cpp_pop_buffer (pfile);
795 break;
798 CPP_SET_WRITTEN (pfile, old_written);
800 else
802 for (;;)
804 token = cpp_get_token (pfile);
805 if (token == CPP_EOF) /* Should not happen ... */
806 break;
807 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
809 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
810 != CPP_NULL_BUFFER (pfile))
811 cpp_pop_buffer (pfile);
812 break;
819 * Rescan a string (which may have escape marks) into pfile's buffer.
820 * Place the result in pfile->token_buffer.
822 * The input is copied before it is scanned, so it is safe to pass
823 * it something from the token_buffer that will get overwritten
824 * (because it follows CPP_WRITTEN). This is used by do_include.
827 void
828 cpp_expand_to_buffer (pfile, buf, length)
829 cpp_reader *pfile;
830 const U_CHAR *buf;
831 int length;
833 register cpp_buffer *ip;
834 U_CHAR *buf1;
835 int save_no_output;
837 if (length < 0)
839 cpp_fatal (pfile, "internal error: length < 0 in cpp_expand_to_buffer");
840 return;
843 /* Set up the input on the input stack. */
845 buf1 = (U_CHAR *) alloca (length + 1);
846 memcpy (buf1, buf, length);
847 buf1[length] = 0;
849 ip = cpp_push_buffer (pfile, buf1, length);
850 if (ip == NULL)
851 return;
852 ip->has_escapes = 1;
854 /* Scan the input, create the output. */
855 save_no_output = CPP_OPTIONS (pfile)->no_output;
856 CPP_OPTIONS (pfile)->no_output = 0;
857 cpp_scan_buffer (pfile);
858 CPP_OPTIONS (pfile)->no_output = save_no_output;
860 CPP_NUL_TERMINATE (pfile);
863 void
864 cpp_buf_line_and_col (pbuf, linep, colp)
865 register cpp_buffer *pbuf;
866 long *linep, *colp;
868 if (pbuf)
870 *linep = pbuf->lineno;
871 if (colp)
872 *colp = pbuf->cur - pbuf->line_base;
874 else
876 *linep = 0;
877 if (colp)
878 *colp = 0;
882 /* Return the cpp_buffer that corresponds to a file (not a macro). */
884 cpp_buffer *
885 cpp_file_buffer (pfile)
886 cpp_reader *pfile;
888 cpp_buffer *ip = CPP_BUFFER (pfile);
890 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
891 if (ip->fname != NULL)
892 return ip;
893 return NULL;
897 * write out a #line command, for instance, after an #include file.
898 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
901 void
902 output_line_command (pfile, file_change)
903 cpp_reader *pfile;
904 enum file_change_code file_change;
906 long line;
907 cpp_buffer *ip = CPP_BUFFER (pfile);
909 if (ip->fname == NULL)
910 return;
912 if (CPP_OPTIONS (pfile)->no_line_commands
913 || CPP_OPTIONS (pfile)->no_output)
914 return;
916 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, NULL);
918 /* If the current file has not changed, we omit the #line if it would
919 appear to be a no-op, and we output a few newlines instead
920 if we want to increase the line number by a small amount.
921 We cannot do this if pfile->lineno is zero, because that means we
922 haven't output any line commands yet. (The very first line command
923 output is a `same_file' command.) */
924 if (file_change == same_file && pfile->lineno != 0)
926 if (line == pfile->lineno)
927 return;
929 /* If the inherited line number is a little too small,
930 output some newlines instead of a #line command. */
931 if (line > pfile->lineno && line < pfile->lineno + 8)
933 CPP_RESERVE (pfile, 20);
934 while (line > pfile->lineno)
936 CPP_PUTC_Q (pfile, '\n');
937 pfile->lineno++;
939 return;
943 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
944 CPP_PUTS_Q (pfile, "# ", 2);
946 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
947 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
949 quote_string (pfile, ip->nominal_fname);
950 if (file_change != same_file)
952 CPP_PUTC_Q (pfile, ' ');
953 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
955 /* Tell cc1 if following text comes from a system header file. */
956 if (ip->system_header_p)
958 CPP_PUTC_Q (pfile, ' ');
959 CPP_PUTC_Q (pfile, '3');
961 #ifndef NO_IMPLICIT_EXTERN_C
962 /* Tell cc1plus if following text should be treated as C. */
963 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
965 CPP_PUTC_Q (pfile, ' ');
966 CPP_PUTC_Q (pfile, '4');
968 #endif
969 CPP_PUTC_Q (pfile, '\n');
970 pfile->lineno = line;
974 /* Like cpp_get_token, except that it does not read past end-of-line.
975 Also, horizontal space is skipped, and macros are popped. */
977 static enum cpp_token
978 get_directive_token (pfile)
979 cpp_reader *pfile;
981 for (;;)
983 long old_written = CPP_WRITTEN (pfile);
984 enum cpp_token token;
985 cpp_skip_hspace (pfile);
986 if (PEEKC () == '\n')
987 return CPP_VSPACE;
988 token = cpp_get_token (pfile);
989 switch (token)
991 case CPP_POP:
992 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
993 return token;
994 /* ... else fall though ... */
995 case CPP_HSPACE: case CPP_COMMENT:
996 CPP_SET_WRITTEN (pfile, old_written);
997 break;
998 default:
999 return token;
1004 /* Handle #include and #import.
1005 This function expects to see "fname" or <fname> on the input.
1007 The input is normally in part of the output_buffer following
1008 CPP_WRITTEN, and will get overwritten by output_line_command.
1009 I.e. in input file specification has been popped by handle_directive.
1010 This is safe. */
1012 static int
1013 do_include (pfile, keyword)
1014 cpp_reader *pfile;
1015 const struct directive *keyword;
1017 int importing = (keyword->type == T_IMPORT);
1018 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
1019 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
1020 int before; /* included before? */
1021 long flen;
1022 unsigned char *ftok;
1023 cpp_buffer *fp;
1025 enum cpp_token token;
1027 /* Chain of dirs to search */
1028 struct include_hash *ihash;
1029 struct file_name_list *search_start;
1031 long old_written = CPP_WRITTEN (pfile);
1033 int fd;
1035 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1037 if (importing)
1038 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
1039 if (skip_dirs)
1040 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
1043 if (importing && CPP_OPTIONS (pfile)->warn_import
1044 && !CPP_OPTIONS (pfile)->inhibit_warnings
1045 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
1047 pfile->import_warning = 1;
1048 cpp_warning (pfile,
1049 "#import is obsolete, use an #ifndef wrapper in the header file");
1052 pfile->parsing_include_directive++;
1053 token = get_directive_token (pfile);
1054 pfile->parsing_include_directive--;
1056 if (token == CPP_STRING)
1058 if (pfile->token_buffer[old_written] == '<')
1059 angle_brackets = 1;
1061 #ifdef VMS
1062 else if (token == CPP_NAME)
1064 /* Support '#include xyz' like VAX-C. It is taken as
1065 '#include <xyz.h>' and generates a warning. */
1066 cpp_warning (pfile,
1067 "`#include filename' is obsolete, use `#include <filename.h>'");
1068 angle_brackets = 1;
1070 /* Append the missing `.h' to the name. */
1071 CPP_PUTS (pfile, ".h", 2);
1073 #endif
1074 else
1076 cpp_error (pfile,
1077 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1078 CPP_SET_WRITTEN (pfile, old_written);
1079 skip_rest_of_line (pfile);
1080 return 0;
1083 flen = CPP_WRITTEN (pfile) - old_written;
1084 ftok = (unsigned char *) alloca (flen + 1);
1085 memcpy (ftok, pfile->token_buffer + old_written, flen);
1086 ftok[flen] = '\0';
1088 if (get_directive_token (pfile) != CPP_VSPACE)
1090 cpp_error (pfile, "junk at end of `#include'");
1091 skip_rest_of_line (pfile);
1094 CPP_SET_WRITTEN (pfile, old_written);
1096 if (flen == 0)
1098 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
1099 return 0;
1102 if (CPP_OPTIONS (pfile)->dump_includes)
1103 pass_thru_directive (ftok,
1104 flen
1105 #ifdef VMS
1106 - ((token == CPP_NAME) ? 2 : 0)
1107 #endif
1108 , pfile, keyword);
1110 #ifdef VMS
1111 if (token == CPP_STRING)
1112 #endif
1114 ftok++;
1115 flen -= 2;
1116 ftok[flen] = '\0';
1119 search_start = 0;
1121 for (fp = CPP_BUFFER (pfile);
1122 fp != CPP_NULL_BUFFER (pfile);
1123 fp = CPP_PREV_BUFFER (fp))
1124 if (fp->fname != NULL)
1125 break;
1127 if (fp == CPP_NULL_BUFFER (pfile))
1129 cpp_fatal (pfile, "cpp internal error: fp == NULL_BUFFER in do_include");
1130 return 0;
1133 /* For #include_next, skip in the search path past the dir in which the
1134 containing file was found. Treat files specified using an absolute path
1135 as if there are no more directories to search. Treat the primary source
1136 file like any other included source, but generate a warning. */
1137 if (skip_dirs && CPP_PREV_BUFFER(fp) != CPP_NULL_BUFFER (pfile))
1139 if (fp->ihash->foundhere != ABSOLUTE_PATH)
1140 search_start = fp->ihash->foundhere->next;
1142 else
1144 if (skip_dirs)
1145 cpp_warning (pfile, "#include_next in primary source file");
1147 if (angle_brackets)
1148 search_start = CPP_OPTIONS (pfile)->bracket_include;
1149 else
1151 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
1153 if (fp)
1154 search_start = fp->actual_dir;
1156 else
1157 search_start = CPP_OPTIONS (pfile)->quote_include;
1161 if (!search_start)
1163 cpp_error (pfile, "No include path in which to find %s", ftok);
1164 return 0;
1167 fd = find_include_file (pfile, ftok, search_start, &ihash, &before);
1169 if (fd == -2)
1170 return 0;
1172 if (fd == -1)
1174 if (CPP_OPTIONS (pfile)->print_deps_missing_files
1175 && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
1176 (pfile->system_include_depth > 0)))
1178 if (!angle_brackets)
1179 deps_output (pfile, ftok, ' ');
1180 else
1182 char *p;
1183 struct file_name_list *ptr;
1184 /* If requested as a system header, assume it belongs in
1185 the first system header directory. */
1186 if (CPP_OPTIONS (pfile)->bracket_include)
1187 ptr = CPP_OPTIONS (pfile)->bracket_include;
1188 else
1189 ptr = CPP_OPTIONS (pfile)->quote_include;
1191 p = (char *) alloca (strlen (ptr->name)
1192 + strlen (ftok) + 2);
1193 if (*ptr->name != '\0')
1195 strcpy (p, ptr->name);
1196 strcat (p, "/");
1198 strcat (p, ftok);
1199 deps_output (pfile, p, ' ');
1202 /* If -M was specified, and this header file won't be added to
1203 the dependency list, then don't count this as an error,
1204 because we can still produce correct output. Otherwise, we
1205 can't produce correct output, because there may be
1206 dependencies we need inside the missing file, and we don't
1207 know what directory this missing file exists in. */
1208 else if (CPP_PRINT_DEPS (pfile)
1209 && (CPP_PRINT_DEPS (pfile)
1210 <= (angle_brackets || (pfile->system_include_depth > 0))))
1211 cpp_warning (pfile, "No include path in which to find %s", ftok);
1212 else
1213 cpp_error_from_errno (pfile, ftok);
1215 return 0;
1218 /* For -M, add the file to the dependencies on its first inclusion. */
1219 if (!before && (CPP_PRINT_DEPS (pfile)
1220 > (angle_brackets || (pfile->system_include_depth > 0))))
1221 deps_output (pfile, ihash->name, ' ');
1223 /* Handle -H option. */
1224 if (CPP_OPTIONS(pfile)->print_include_names)
1226 fp = CPP_BUFFER (pfile);
1227 while ((fp = CPP_PREV_BUFFER (fp)) != CPP_NULL_BUFFER (pfile))
1228 putc ('.', stderr);
1229 fprintf (stderr, " %s\n", ihash->name);
1232 /* Actually process the file */
1234 if (importing)
1235 ihash->control_macro = "";
1237 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
1239 close (fd);
1240 return 0;
1243 if (angle_brackets)
1244 pfile->system_include_depth++; /* Decremented in file_cleanup. */
1246 if (finclude (pfile, fd, ihash))
1248 output_line_command (pfile, enter_file);
1249 pfile->only_seen_white = 2;
1252 return 0;
1255 /* Subroutine of do_line. Read next token from PFILE without adding it to
1256 the output buffer. If it is a number between 1 and 4, store it in *NUM
1257 and return 1; otherwise, return 0 and complain if we aren't at the end
1258 of the directive. */
1260 static int
1261 read_line_number (pfile, num)
1262 cpp_reader *pfile;
1263 int *num;
1265 long save_written = CPP_WRITTEN (pfile);
1266 U_CHAR *p = pfile->token_buffer + save_written;
1267 enum cpp_token token = get_directive_token (pfile);
1268 CPP_SET_WRITTEN (pfile, save_written);
1270 if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
1272 *num = p[0] - '0';
1273 return 1;
1275 else
1277 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
1278 cpp_error (pfile, "invalid format `#line' command");
1279 return 0;
1283 /* Interpret #line command.
1284 Note that the filename string (if any) is treated as if it were an
1285 include filename. That means no escape handling. */
1287 static int
1288 do_line (pfile, keyword)
1289 cpp_reader *pfile;
1290 const struct directive *keyword ATTRIBUTE_UNUSED;
1292 cpp_buffer *ip = CPP_BUFFER (pfile);
1293 int new_lineno;
1294 long old_written = CPP_WRITTEN (pfile);
1295 enum file_change_code file_change = same_file;
1296 enum cpp_token token;
1297 char *x;
1299 token = get_directive_token (pfile);
1301 if (token != CPP_NUMBER)
1303 cpp_error (pfile, "token after `#line' is not an integer");
1304 goto bad_line_directive;
1307 new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
1308 if (x[0] != '\0')
1310 cpp_error (pfile, "token after `#line' is not an integer");
1311 goto bad_line_directive;
1313 CPP_SET_WRITTEN (pfile, old_written);
1315 if (CPP_PEDANTIC (pfile) && new_lineno <= 0)
1316 cpp_pedwarn (pfile, "line number out of range in `#line' command");
1318 token = get_directive_token (pfile);
1320 if (token == CPP_STRING)
1322 U_CHAR *fname = pfile->token_buffer + old_written + 1;
1323 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
1324 int action_number = 0;
1326 if (read_line_number (pfile, &action_number))
1328 if (CPP_PEDANTIC (pfile))
1329 cpp_pedwarn (pfile, "garbage at end of `#line' command");
1331 if (action_number == 1)
1333 file_change = enter_file;
1334 read_line_number (pfile, &action_number);
1336 else if (action_number == 2)
1338 file_change = leave_file;
1339 read_line_number (pfile, &action_number);
1341 if (action_number == 3)
1343 ip->system_header_p = 1;
1344 read_line_number (pfile, &action_number);
1346 if (action_number == 4)
1348 ip->system_header_p = 2;
1349 read_line_number (pfile, &action_number);
1353 *end_name = '\0';
1355 if (strcmp (fname, ip->nominal_fname))
1357 char *newname, *oldname;
1358 if (!strcmp (fname, ip->fname))
1359 newname = ip->fname;
1360 else if (ip->last_nominal_fname
1361 && !strcmp (fname, ip->last_nominal_fname))
1362 newname = ip->last_nominal_fname;
1363 else
1364 newname = xstrdup (fname);
1366 oldname = ip->nominal_fname;
1367 ip->nominal_fname = newname;
1369 if (ip->last_nominal_fname
1370 && ip->last_nominal_fname != oldname
1371 && ip->last_nominal_fname != newname
1372 && ip->last_nominal_fname != ip->fname)
1373 free (ip->last_nominal_fname);
1375 if (newname == ip->fname)
1376 ip->last_nominal_fname = NULL;
1377 else
1378 ip->last_nominal_fname = oldname;
1381 else if (token != CPP_VSPACE && token != CPP_EOF)
1383 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1384 goto bad_line_directive;
1387 /* The Newline at the end of this line remains to be processed.
1388 To put the next line at the specified line number,
1389 we must store a line number now that is one less. */
1390 ip->lineno = new_lineno - 1;
1391 CPP_SET_WRITTEN (pfile, old_written);
1392 output_line_command (pfile, file_change);
1393 return 0;
1395 bad_line_directive:
1396 skip_rest_of_line (pfile);
1397 CPP_SET_WRITTEN (pfile, old_written);
1398 return 0;
1401 /* Remove the definition of a symbol from the symbol table.
1402 According to the C standard, it is not an error to undef
1403 something that has no definitions. */
1404 static int
1405 do_undef (pfile, keyword)
1406 cpp_reader *pfile;
1407 const struct directive *keyword;
1409 int sym_length;
1410 HASHNODE *hp;
1411 U_CHAR *buf, *name, *limit;
1412 int c;
1413 long here = CPP_WRITTEN (pfile);
1414 enum cpp_token token;
1416 cpp_skip_hspace (pfile);
1417 c = GETC();
1418 if (! is_idstart[c])
1420 cpp_error (pfile, "token after #undef is not an identifier");
1421 skip_rest_of_line (pfile);
1422 return 1;
1425 parse_name (pfile, c);
1426 buf = pfile->token_buffer + here;
1427 limit = CPP_PWRITTEN(pfile);
1429 /* Copy out the token so we can pop the token buffer. */
1430 name = (U_CHAR *) alloca (limit - buf + 1);
1431 bcopy(buf, name, limit - buf);
1432 name[limit - buf] = '\0';
1434 token = get_directive_token (pfile);
1435 if (token != CPP_VSPACE && token != CPP_POP)
1437 cpp_pedwarn (pfile, "junk on line after #undef");
1438 skip_rest_of_line (pfile);
1441 CPP_SET_WRITTEN (pfile, here);
1443 sym_length = check_macro_name (pfile, buf, 0);
1445 while ((hp = cpp_lookup (pfile, name, sym_length, -1)) != NULL)
1447 /* If we are generating additional info for debugging (with -g) we
1448 need to pass through all effective #undef commands. */
1449 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1450 pass_thru_directive (name, sym_length, pfile, keyword);
1451 if (hp->type == T_POISON)
1452 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
1453 else
1455 if (hp->type != T_MACRO)
1456 cpp_warning (pfile, "undefining `%s'", hp->name);
1457 delete_macro (hp);
1461 return 0;
1464 /* Wrap do_undef for -U processing. */
1465 void
1466 cpp_undef (pfile, macro)
1467 cpp_reader *pfile;
1468 U_CHAR *macro;
1470 if (cpp_push_buffer (pfile, macro, strlen (macro)))
1472 do_undef (pfile, NULL);
1473 cpp_pop_buffer (pfile);
1479 * Report an error detected by the program we are processing.
1480 * Use the text of the line in the error message.
1481 * (We use error because it prints the filename & line#.)
1484 static int
1485 do_error (pfile, keyword)
1486 cpp_reader *pfile;
1487 const struct directive *keyword ATTRIBUTE_UNUSED;
1489 long here = CPP_WRITTEN (pfile);
1490 U_CHAR *text;
1491 copy_rest_of_line (pfile);
1492 text = pfile->token_buffer + here;
1493 SKIP_WHITE_SPACE(text);
1495 cpp_error (pfile, "#error %s", text);
1496 CPP_SET_WRITTEN (pfile, here);
1497 return 0;
1501 * Report a warning detected by the program we are processing.
1502 * Use the text of the line in the warning message, then continue.
1505 static int
1506 do_warning (pfile, keyword)
1507 cpp_reader *pfile;
1508 const struct directive *keyword ATTRIBUTE_UNUSED;
1510 U_CHAR *text;
1511 long here = CPP_WRITTEN(pfile);
1512 copy_rest_of_line (pfile);
1513 text = pfile->token_buffer + here;
1514 SKIP_WHITE_SPACE(text);
1516 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1517 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1519 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
1520 if -pedantic-errors is given, #warning should cause an error. */
1521 cpp_pedwarn (pfile, "#warning %s", text);
1522 CPP_SET_WRITTEN (pfile, here);
1523 return 0;
1526 /* Report program identification.
1527 This is not precisely what cccp does with #ident, however I believe
1528 it matches `closely enough' (behavior is identical as long as there
1529 are no macros on the #ident line, which is pathological in my opinion). */
1531 static int
1532 do_ident (pfile, keyword)
1533 cpp_reader *pfile;
1534 const struct directive *keyword ATTRIBUTE_UNUSED;
1536 /* Allow #ident in system headers, since that's not user's fault. */
1537 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1538 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1540 CPP_PUTS (pfile, "#ident ", 7);
1541 cpp_skip_hspace (pfile);
1542 copy_rest_of_line (pfile);
1544 return 0;
1547 /* Just check for some recognized pragmas that need validation here,
1548 and leave the text in the token buffer to be output. */
1550 static int
1551 do_pragma (pfile, keyword)
1552 cpp_reader *pfile;
1553 const struct directive *keyword ATTRIBUTE_UNUSED;
1555 long here;
1556 U_CHAR *buf;
1558 CPP_PUTS (pfile, "#pragma ", 8);
1559 cpp_skip_hspace (pfile);
1561 here = CPP_WRITTEN (pfile);
1562 copy_rest_of_line (pfile);
1563 buf = pfile->token_buffer + here;
1565 if (!strncmp (buf, "once", 4))
1567 cpp_buffer *ip = NULL;
1569 /* Allow #pragma once in system headers, since that's not the user's
1570 fault. */
1571 if (!CPP_BUFFER (pfile)->system_header_p)
1572 cpp_warning (pfile, "`#pragma once' is obsolete");
1574 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
1576 if (ip == CPP_NULL_BUFFER (pfile))
1577 return 0;
1578 if (ip->fname != NULL)
1579 break;
1582 if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
1583 cpp_warning (pfile, "`#pragma once' outside include file");
1584 else
1585 ip->ihash->control_macro = ""; /* never repeat */
1587 else if (!strncmp (buf, "implementation", 14))
1589 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1590 been included yet. */
1591 struct include_hash *ptr;
1592 U_CHAR *p = buf + 14, *fname, *fcopy;
1593 SKIP_WHITE_SPACE (p);
1594 if (*p == '\n' || *p != '\"')
1595 return 0;
1597 fname = p + 1;
1598 p = (U_CHAR *) index (fname, '\"');
1600 fcopy = (U_CHAR *) alloca (p - fname + 1);
1601 bcopy (fname, fcopy, p - fname);
1602 fcopy[p-fname] = '\0';
1604 ptr = include_hash (pfile, fcopy, 0);
1605 if (ptr)
1606 cpp_warning (pfile,
1607 "`#pragma implementation' for `%s' appears after file is included",
1608 fcopy);
1610 else if (!strncmp (buf, "poison", 6))
1612 /* Poison these symbols so that all subsequent usage produces an
1613 error message. */
1614 U_CHAR *p = buf + 6;
1615 size_t plen;
1616 U_CHAR *syms;
1617 int writeit;
1619 SKIP_WHITE_SPACE (p);
1620 plen = strlen(p) + 1;
1622 syms = (U_CHAR *) alloca (plen);
1623 memcpy (syms, p, plen);
1625 /* As a rule, don't include #pragma poison commands in output,
1626 unless the user asks for them. */
1627 writeit = (CPP_OPTIONS (pfile)->debug_output
1628 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions
1629 || CPP_OPTIONS (pfile)->dump_macros == dump_names);
1631 if (writeit)
1632 CPP_SET_WRITTEN (pfile, here);
1633 else
1634 CPP_SET_WRITTEN (pfile, here-8);
1636 if (writeit)
1638 CPP_RESERVE (pfile, plen + 7);
1639 CPP_PUTS_Q (pfile, "poison", 7);
1642 while (*syms != '\0')
1644 U_CHAR *end = syms;
1646 while (is_idchar[*end])
1647 end++;
1649 if (!is_hor_space[*end] && *end != '\0')
1651 cpp_error (pfile, "invalid #pragma poison directive");
1652 return 1;
1655 if (cpp_push_buffer (pfile, syms, end - syms) != NULL)
1657 do_define (pfile, keyword);
1658 cpp_pop_buffer (pfile);
1660 if (writeit)
1662 CPP_PUTC_Q (pfile, ' ');
1663 CPP_PUTS_Q (pfile, syms, end - syms);
1665 syms = end;
1666 SKIP_WHITE_SPACE (syms);
1670 return 0;
1673 #ifdef SCCS_DIRECTIVE
1674 /* Just ignore #sccs, on systems where we define it at all. */
1676 static int
1677 do_sccs (pfile, keyword)
1678 cpp_reader *pfile;
1679 const struct directive *keyword ATTRIBUTE_UNUSED;
1681 if (CPP_PEDANTIC (pfile))
1682 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1683 skip_rest_of_line (pfile);
1684 return 0;
1686 #endif
1689 /* We've found an `#if' directive. If the only thing before it in
1690 this file is white space, and if it is of the form
1691 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1692 for inclusion of this file. (See redundant_include_p in cppfiles.c
1693 for an explanation of controlling macros.) If so, return a
1694 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1696 static U_CHAR *
1697 detect_if_not_defined (pfile)
1698 cpp_reader *pfile;
1700 U_CHAR *control_macro = 0;
1702 if (pfile->only_seen_white == 2)
1704 char *ident;
1705 enum cpp_token token;
1706 int base_offset;
1707 int token_offset;
1708 int need_rparen = 0;
1710 /* Save state required for restore. */
1711 pfile->no_macro_expand++;
1712 parse_set_mark (pfile);
1713 base_offset = CPP_WRITTEN (pfile);
1715 /* Look for `!', */
1716 if (get_directive_token (pfile) != CPP_OTHER
1717 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1718 || CPP_PWRITTEN (pfile)[-1] != '!')
1719 goto restore;
1721 /* ...then `defined', */
1722 token_offset = CPP_WRITTEN (pfile);
1723 token = get_directive_token (pfile);
1724 if (token != CPP_NAME)
1725 goto restore;
1726 ident = pfile->token_buffer + token_offset;
1727 CPP_NUL_TERMINATE (pfile);
1728 if (strcmp (ident, "defined"))
1729 goto restore;
1731 /* ...then an optional '(' and the name, */
1732 token_offset = CPP_WRITTEN (pfile);
1733 token = get_directive_token (pfile);
1734 if (token == CPP_LPAREN)
1736 token_offset = CPP_WRITTEN (pfile);
1737 token = get_directive_token (pfile);
1738 if (token != CPP_NAME)
1739 goto restore;
1740 need_rparen = 1;
1742 else if (token != CPP_NAME)
1743 goto restore;
1745 ident = pfile->token_buffer + token_offset;
1746 CPP_NUL_TERMINATE (pfile);
1748 /* ...then the ')', if necessary, */
1749 if ((!need_rparen || get_directive_token (pfile) == CPP_RPAREN)
1750 /* ...and make sure there's nothing else on the line. */
1751 && get_directive_token (pfile) == CPP_VSPACE)
1752 control_macro = xstrdup (ident);
1754 restore:
1755 CPP_SET_WRITTEN (pfile, base_offset);
1756 pfile->no_macro_expand--;
1757 parse_goto_mark (pfile);
1760 return control_macro;
1764 * handle #if command by
1765 * 1) inserting special `defined' keyword into the hash table
1766 * that gets turned into 0 or 1 by special_symbol (thus,
1767 * if the luser has a symbol called `defined' already, it won't
1768 * work inside the #if command)
1769 * 2) rescan the input into a temporary output buffer
1770 * 3) pass the output buffer to the yacc parser and collect a value
1771 * 4) clean up the mess left from steps 1 and 2.
1772 * 5) call conditional_skip to skip til the next #endif (etc.),
1773 * or not, depending on the value from step 3.
1776 static int
1777 do_if (pfile, keyword)
1778 cpp_reader *pfile;
1779 const struct directive *keyword ATTRIBUTE_UNUSED;
1781 U_CHAR *control_macro = detect_if_not_defined (pfile);
1782 HOST_WIDEST_INT value = eval_if_expression (pfile);
1783 conditional_skip (pfile, value == 0, T_IF, control_macro);
1784 return 0;
1788 * handle a #elif directive by not changing if_stack either.
1789 * see the comment above do_else.
1792 static int
1793 do_elif (pfile, keyword)
1794 cpp_reader *pfile;
1795 const struct directive *keyword ATTRIBUTE_UNUSED;
1797 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
1798 cpp_error (pfile, "`#elif' not within a conditional");
1799 return 0;
1800 } else {
1801 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
1802 cpp_error (pfile, "`#elif' after `#else'");
1803 #if 0
1804 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
1805 #endif
1806 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
1807 && strcmp (pfile->if_stack->fname,
1808 CPP_BUFFER (pfile)->nominal_fname) != 0)
1809 fprintf (stderr, ", file %s", pfile->if_stack->fname);
1810 fprintf (stderr, ")\n");
1812 pfile->if_stack->type = T_ELIF;
1815 if (pfile->if_stack->if_succeeded)
1816 skip_if_group (pfile);
1817 else {
1818 HOST_WIDEST_INT value = eval_if_expression (pfile);
1819 if (value == 0)
1820 skip_if_group (pfile);
1821 else {
1822 ++pfile->if_stack->if_succeeded; /* continue processing input */
1823 output_line_command (pfile, same_file);
1826 return 0;
1830 * evaluate a #if expression in BUF, of length LENGTH,
1831 * then parse the result as a C expression and return the value as an int.
1834 static HOST_WIDEST_INT
1835 eval_if_expression (pfile)
1836 cpp_reader *pfile;
1838 HOST_WIDEST_INT value;
1839 long old_written = CPP_WRITTEN (pfile);
1841 pfile->pcp_inside_if = 1;
1842 value = cpp_parse_expr (pfile);
1843 pfile->pcp_inside_if = 0;
1845 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1847 return value;
1851 * routine to handle ifdef/ifndef. Try to look up the symbol,
1852 * then do or don't skip to the #endif/#else/#elif depending
1853 * on what directive is actually being processed.
1856 static int
1857 do_xifdef (pfile, keyword)
1858 cpp_reader *pfile;
1859 const struct directive *keyword;
1861 int skip;
1862 cpp_buffer *ip = CPP_BUFFER (pfile);
1863 U_CHAR *ident;
1864 int ident_length;
1865 enum cpp_token token;
1866 int start_of_file = 0;
1867 U_CHAR *control_macro = 0;
1868 int old_written = CPP_WRITTEN (pfile);
1870 /* Detect a #ifndef at start of file (not counting comments). */
1871 if (ip->fname != 0 && keyword->type == T_IFNDEF)
1872 start_of_file = pfile->only_seen_white == 2;
1874 pfile->no_macro_expand++;
1875 token = get_directive_token (pfile);
1876 pfile->no_macro_expand--;
1878 ident = pfile->token_buffer + old_written;
1879 ident_length = CPP_WRITTEN (pfile) - old_written;
1880 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1882 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1884 skip = (keyword->type == T_IFDEF);
1885 if (! CPP_TRADITIONAL (pfile))
1886 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
1888 else if (token == CPP_NAME)
1890 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
1891 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
1892 if (start_of_file && !skip)
1894 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
1895 bcopy (ident, control_macro, ident_length + 1);
1897 if (hp != NULL && hp->type == T_POISON)
1899 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1900 skip = !skip;
1903 else
1905 skip = (keyword->type == T_IFDEF);
1906 if (! CPP_TRADITIONAL (pfile))
1907 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
1910 if (!CPP_TRADITIONAL (pfile))
1911 { int c;
1912 cpp_skip_hspace (pfile);
1913 c = PEEKC ();
1914 if (c != EOF && c != '\n')
1915 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
1917 skip_rest_of_line (pfile);
1919 #if 0
1920 if (pcp_outfile) {
1921 /* Output a precondition for this macro. */
1922 if (hp && hp->value.defn->predefined)
1923 fprintf (pcp_outfile, "#define %s\n", hp->name);
1924 else {
1925 U_CHAR *cp = buf;
1926 fprintf (pcp_outfile, "#undef ");
1927 while (is_idchar[*cp]) /* Ick! */
1928 fputc (*cp++, pcp_outfile);
1929 putc ('\n', pcp_outfile);
1931 #endif
1933 conditional_skip (pfile, skip, T_IF, control_macro);
1934 return 0;
1937 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1938 If this is a #ifndef starting at the beginning of a file,
1939 CONTROL_MACRO is the macro name tested by the #ifndef.
1940 Otherwise, CONTROL_MACRO is 0. */
1942 static void
1943 conditional_skip (pfile, skip, type, control_macro)
1944 cpp_reader *pfile;
1945 int skip;
1946 enum node_type type;
1947 U_CHAR *control_macro;
1949 IF_STACK_FRAME *temp;
1951 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
1952 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
1953 temp->lineno = CPP_BUFFER (pfile)->lineno;
1954 temp->next = pfile->if_stack;
1955 temp->control_macro = control_macro;
1956 pfile->if_stack = temp;
1958 pfile->if_stack->type = type;
1960 if (skip != 0) {
1961 skip_if_group (pfile);
1962 return;
1963 } else {
1964 ++pfile->if_stack->if_succeeded;
1965 output_line_command (pfile, same_file);
1969 /* Subroutine of skip_if_group. Examine one preprocessing directive and
1970 return 0 if skipping should continue, 1 if it should halt. Also
1971 adjusts the if_stack as appropriate.
1972 The `#' has been read, but not the identifier. */
1974 static int
1975 consider_directive_while_skipping (pfile, stack)
1976 cpp_reader *pfile;
1977 IF_STACK_FRAME *stack;
1979 long ident_len, ident;
1980 const struct directive *kt;
1981 IF_STACK_FRAME *temp;
1983 cpp_skip_hspace (pfile);
1985 ident = CPP_WRITTEN (pfile);
1986 parse_name (pfile, GETC());
1987 ident_len = CPP_WRITTEN (pfile) - ident;
1989 CPP_SET_WRITTEN (pfile, ident);
1991 for (kt = directive_table; kt->length >= 0; kt++)
1992 if (kt->length == ident_len
1993 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
1994 switch (kt->type)
1996 case T_IF:
1997 case T_IFDEF:
1998 case T_IFNDEF:
1999 temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
2000 temp->next = pfile->if_stack;
2001 pfile->if_stack = temp;
2002 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
2003 temp->type = kt->type;
2004 return 0;
2006 case T_ELSE:
2007 if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
2008 validate_else (pfile, "#else");
2009 /* fall through */
2010 case T_ELIF:
2011 if (pfile->if_stack->type == T_ELSE)
2012 cpp_error (pfile, "`%s' after `#else'", kt->name);
2014 if (pfile->if_stack == stack)
2015 return 1;
2016 else
2018 pfile->if_stack->type = kt->type;
2019 return 0;
2022 case T_ENDIF:
2023 if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
2024 validate_else (pfile, "#endif");
2026 if (pfile->if_stack == stack)
2027 return 1;
2029 temp = pfile->if_stack;
2030 pfile->if_stack = temp->next;
2031 free (temp);
2032 return 0;
2034 default:
2035 return 0;
2038 /* Don't let erroneous code go by. */
2039 if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
2040 cpp_pedwarn (pfile, "invalid preprocessor directive name");
2041 return 0;
2044 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
2045 * leaves input ptr at the sharp sign found.
2047 static void
2048 skip_if_group (pfile)
2049 cpp_reader *pfile;
2051 int c;
2052 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
2053 U_CHAR *beg_of_line;
2054 long old_written;
2056 if (CPP_OPTIONS (pfile)->output_conditionals)
2058 CPP_PUTS (pfile, "#failed\n", 8);
2059 pfile->lineno++;
2060 output_line_command (pfile, same_file);
2063 old_written = CPP_WRITTEN (pfile);
2065 for (;;)
2067 beg_of_line = CPP_BUFFER (pfile)->cur;
2069 if (! CPP_TRADITIONAL (pfile))
2070 cpp_skip_hspace (pfile);
2071 c = GETC();
2072 if (c == '\n')
2074 if (CPP_OPTIONS (pfile)->output_conditionals)
2075 CPP_PUTC (pfile, c);
2076 CPP_BUMP_LINE (pfile);
2077 continue;
2079 else if (c == '#')
2081 if (consider_directive_while_skipping (pfile, save_if_stack))
2082 break;
2084 else if (c == EOF)
2085 return; /* Caller will issue error. */
2087 FORWARD(-1);
2088 if (CPP_OPTIONS (pfile)->output_conditionals)
2090 CPP_PUTS (pfile, beg_of_line, CPP_BUFFER (pfile)->cur - beg_of_line);
2091 copy_rest_of_line (pfile);
2093 else
2095 copy_rest_of_line (pfile);
2096 CPP_SET_WRITTEN (pfile, old_written); /* discard it */
2099 c = GETC();
2100 if (c == EOF)
2101 return; /* Caller will issue error. */
2102 else
2104 /* \n */
2105 if (CPP_OPTIONS (pfile)->output_conditionals)
2107 CPP_PUTC (pfile, c);
2108 pfile->lineno++;
2110 CPP_BUMP_LINE (pfile);
2114 /* Back up to the beginning of this line. Caller will process the
2115 directive. */
2116 CPP_BUFFER (pfile)->cur = beg_of_line;
2117 pfile->only_seen_white = 1;
2118 if (CPP_OPTIONS (pfile)->output_conditionals)
2120 CPP_PUTS (pfile, "#endfailed\n", 11);
2121 pfile->lineno++;
2126 * handle a #else directive. Do this by just continuing processing
2127 * without changing if_stack ; this is so that the error message
2128 * for missing #endif's etc. will point to the original #if. It
2129 * is possible that something different would be better.
2132 static int
2133 do_else (pfile, keyword)
2134 cpp_reader *pfile;
2135 const struct directive *keyword ATTRIBUTE_UNUSED;
2137 cpp_buffer *ip = CPP_BUFFER (pfile);
2139 if (CPP_PEDANTIC (pfile))
2140 validate_else (pfile, "#else");
2141 skip_rest_of_line (pfile);
2143 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
2144 cpp_error (pfile, "`#else' not within a conditional");
2145 return 0;
2146 } else {
2147 /* #ifndef can't have its special treatment for containing the whole file
2148 if it has a #else clause. */
2149 pfile->if_stack->control_macro = 0;
2151 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
2152 cpp_error (pfile, "`#else' after `#else'");
2153 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
2154 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
2155 fprintf (stderr, ", file %s", pfile->if_stack->fname);
2156 fprintf (stderr, ")\n");
2158 pfile->if_stack->type = T_ELSE;
2161 if (pfile->if_stack->if_succeeded)
2162 skip_if_group (pfile);
2163 else {
2164 ++pfile->if_stack->if_succeeded; /* continue processing input */
2165 output_line_command (pfile, same_file);
2167 return 0;
2171 * unstack after #endif command
2174 static int
2175 do_endif (pfile, keyword)
2176 cpp_reader *pfile;
2177 const struct directive *keyword ATTRIBUTE_UNUSED;
2179 if (CPP_PEDANTIC (pfile))
2180 validate_else (pfile, "#endif");
2181 skip_rest_of_line (pfile);
2183 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2184 cpp_error (pfile, "`#endif' not within a conditional");
2185 else
2187 IF_STACK_FRAME *temp = pfile->if_stack;
2188 pfile->if_stack = temp->next;
2189 if (temp->control_macro != 0)
2191 /* This #endif matched a #ifndef at the start of the file.
2192 See if it is at the end of the file. */
2193 int c;
2195 parse_set_mark (pfile);
2197 for (;;)
2199 cpp_skip_hspace (pfile);
2200 c = GETC ();
2201 if (c != '\n')
2202 break;
2204 parse_goto_mark (pfile);
2206 if (c == EOF)
2208 /* This #endif ends a #ifndef
2209 that contains all of the file (aside from whitespace).
2210 Arrange not to include the file again
2211 if the macro that was tested is defined. */
2212 struct cpp_buffer *ip;
2213 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2214 if (ip->fname != NULL)
2215 break;
2216 ip->ihash->control_macro = (char *) temp->control_macro;
2219 free (temp);
2220 output_line_command (pfile, same_file);
2222 return 0;
2225 /* When an #else or #endif is found while skipping failed conditional,
2226 if -pedantic was specified, this is called to warn about text after
2227 the command name. P points to the first char after the command name. */
2229 static void
2230 validate_else (pfile, directive)
2231 cpp_reader *pfile;
2232 const char *directive;
2234 int c;
2235 cpp_skip_hspace (pfile);
2236 c = PEEKC ();
2237 if (c != EOF && c != '\n')
2238 cpp_pedwarn (pfile,
2239 "text following `%s' violates ANSI standard", directive);
2242 /* Convert T_IF, etc. to a string. Used in error messages. */
2243 static const char *
2244 if_directive_name (pfile, ifs)
2245 cpp_reader *pfile;
2246 struct if_stack *ifs;
2248 switch (ifs->type)
2250 case T_IF: return "#if";
2251 case T_IFDEF: return "#ifdef";
2252 case T_IFNDEF: return "#ifndef";
2253 case T_ELIF: return "#elif";
2254 case T_ELSE: return "#else";
2255 default:
2256 cpp_fatal (pfile, "impossible if_stack->type value %d", ifs->type);
2257 return "unknown";
2261 /* Get the next token, and add it to the text in pfile->token_buffer.
2262 Return the kind of token we got. */
2264 enum cpp_token
2265 cpp_get_token (pfile)
2266 cpp_reader *pfile;
2268 register int c, c2, c3;
2269 enum cpp_token token;
2270 struct cpp_options *opts = CPP_OPTIONS (pfile);
2272 get_next:
2273 c = GETC();
2274 if (c == EOF)
2276 handle_eof:
2277 if (CPP_BUFFER (pfile)->manual_pop)
2278 /* If we've been reading from redirected input, the
2279 frontend will pop the buffer. */
2280 return CPP_EOF;
2281 else if (CPP_BUFFER (pfile)->seen_eof)
2283 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile))
2284 return CPP_EOF;
2286 cpp_pop_buffer (pfile);
2287 goto get_next;
2289 else
2291 cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2292 struct if_stack *ifs, *nifs;
2294 /* Unwind the conditional stack and generate error messages. */
2295 for (ifs = pfile->if_stack;
2296 ifs != CPP_BUFFER (pfile)->if_stack;
2297 ifs = nifs)
2299 cpp_error_with_line (pfile, ifs->lineno, -1,
2300 "unterminated `%s' conditional",
2301 if_directive_name (pfile, ifs));
2303 nifs = ifs->next;
2304 free (ifs);
2306 pfile->if_stack = ifs;
2308 if (CPP_BUFFER (pfile)->nominal_fname
2309 && next_buf != CPP_NULL_BUFFER (pfile))
2311 /* We're about to return from an #include file.
2312 Emit #line information now (as part of the CPP_POP) result.
2313 But the #line refers to the file we will pop to. */
2314 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2315 CPP_BUFFER (pfile) = next_buf;
2316 pfile->input_stack_listing_current = 0;
2317 output_line_command (pfile, leave_file);
2318 CPP_BUFFER (pfile) = cur_buffer;
2321 CPP_BUFFER (pfile)->seen_eof = 1;
2322 return CPP_POP;
2325 else
2327 switch (c)
2329 case '/':
2330 if (PEEKC () == '=')
2331 goto op2;
2333 comment:
2334 if (opts->put_out_comments)
2335 c = copy_comment (pfile, c);
2336 else
2337 c = skip_comment (pfile, c);
2338 if (c == EOF)
2339 goto handle_eof;
2340 else if (c != ' ')
2341 goto randomchar;
2343 /* Comments are equivalent to spaces.
2344 For -traditional, a comment is equivalent to nothing. */
2345 if (opts->traditional || opts->put_out_comments)
2346 return CPP_COMMENT;
2347 else
2349 CPP_PUTC (pfile, c);
2350 return CPP_HSPACE;
2352 #if 0
2353 if (opts->for_lint) {
2354 U_CHAR *argbp;
2355 int cmdlen, arglen;
2356 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
2358 if (lintcmd != NULL) {
2359 /* I believe it is always safe to emit this newline: */
2360 obp[-1] = '\n';
2361 bcopy ("#pragma lint ", (char *) obp, 13);
2362 obp += 13;
2363 bcopy (lintcmd, (char *) obp, cmdlen);
2364 obp += cmdlen;
2366 if (arglen != 0) {
2367 *(obp++) = ' ';
2368 bcopy (argbp, (char *) obp, arglen);
2369 obp += arglen;
2372 /* OK, now bring us back to the state we were in before we entered
2373 this branch. We need #line because the newline for the pragma
2374 could mess things up. */
2375 output_line_command (pfile, same_file);
2376 *(obp++) = ' '; /* just in case, if comments are copied thru */
2377 *(obp++) = '/';
2380 #endif
2382 case '#':
2383 #if 0
2384 /* If this is expanding a macro definition, don't recognize
2385 preprocessor directives. */
2386 if (ip->macro != 0)
2387 goto randomchar;
2388 /* If this is expand_into_temp_buffer, recognize them
2389 only after an actual newline at this level,
2390 not at the beginning of the input level. */
2391 if (ip->fname == 0 && beg_of_line == ip->buf)
2392 goto randomchar;
2393 if (ident_length)
2394 goto specialchar;
2395 #endif
2397 if (!pfile->only_seen_white)
2398 goto randomchar;
2399 if (handle_directive (pfile))
2400 return CPP_DIRECTIVE;
2401 pfile->only_seen_white = 0;
2402 return CPP_OTHER;
2404 case '\"':
2405 case '\'':
2406 string:
2407 parse_string (pfile, c);
2408 pfile->only_seen_white = 0;
2409 return c == '\'' ? CPP_CHAR : CPP_STRING;
2411 case '$':
2412 if (!opts->dollars_in_ident)
2413 goto randomchar;
2414 goto letter;
2416 case ':':
2417 if (opts->cplusplus && PEEKC () == ':')
2418 goto op2;
2419 goto randomchar;
2421 case '&':
2422 case '+':
2423 case '|':
2424 c2 = PEEKC ();
2425 if (c2 == c || c2 == '=')
2426 goto op2;
2427 goto randomchar;
2429 case '*':
2430 case '!':
2431 case '%':
2432 case '=':
2433 case '^':
2434 if (PEEKC () == '=')
2435 goto op2;
2436 goto randomchar;
2438 case '-':
2439 c2 = PEEKC ();
2440 if (c2 == '-' && opts->chill)
2441 goto comment; /* Chill style comment */
2442 if (c2 == '-' || c2 == '=')
2443 goto op2;
2444 if (c2 == '>')
2446 if (opts->cplusplus && PEEKN (1) == '*')
2448 /* In C++, there's a ->* operator. */
2449 token = CPP_OTHER;
2450 pfile->only_seen_white = 0;
2451 CPP_RESERVE (pfile, 4);
2452 CPP_PUTC_Q (pfile, c);
2453 CPP_PUTC_Q (pfile, GETC ());
2454 CPP_PUTC_Q (pfile, GETC ());
2455 CPP_NUL_TERMINATE_Q (pfile);
2456 return token;
2458 goto op2;
2460 goto randomchar;
2462 case '<':
2463 if (pfile->parsing_include_directive)
2465 for (;;)
2467 CPP_PUTC (pfile, c);
2468 if (c == '>')
2469 break;
2470 c = GETC ();
2471 if (c == '\n' || c == EOF)
2473 cpp_error (pfile,
2474 "missing '>' in `#include <FILENAME>'");
2475 break;
2477 else if (c == '\r')
2479 if (!CPP_BUFFER (pfile)->has_escapes)
2481 /* Backslash newline is replaced by nothing. */
2482 CPP_ADJUST_WRITTEN (pfile, -1);
2483 CPP_BUMP_LINE (pfile);
2485 else
2487 /* We might conceivably get \r- or \r<space> in
2488 here. Just delete 'em. */
2489 int d = GETC();
2490 if (d != '-' && d != ' ')
2491 cpp_fatal (pfile,
2492 "internal error: unrecognized escape \\r%c",
2494 CPP_ADJUST_WRITTEN (pfile, -1);
2498 return CPP_STRING;
2500 /* else fall through */
2501 case '>':
2502 c2 = PEEKC ();
2503 if (c2 == '=')
2504 goto op2;
2505 /* GNU C++ supports MIN and MAX operators <? and >?. */
2506 if (c2 != c && (!opts->cplusplus || c2 != '?'))
2507 goto randomchar;
2508 FORWARD(1);
2509 CPP_RESERVE (pfile, 4);
2510 CPP_PUTC (pfile, c);
2511 CPP_PUTC (pfile, c2);
2512 c3 = PEEKC ();
2513 if (c3 == '=')
2514 CPP_PUTC_Q (pfile, GETC ());
2515 CPP_NUL_TERMINATE_Q (pfile);
2516 pfile->only_seen_white = 0;
2517 return CPP_OTHER;
2519 case '.':
2520 c2 = PEEKC ();
2521 if (ISDIGIT(c2))
2523 CPP_RESERVE(pfile, 2);
2524 CPP_PUTC_Q (pfile, '.');
2525 c = GETC ();
2526 goto number;
2529 /* In C++ there's a .* operator. */
2530 if (opts->cplusplus && c2 == '*')
2531 goto op2;
2533 if (c2 == '.' && PEEKN(1) == '.')
2535 CPP_RESERVE(pfile, 4);
2536 CPP_PUTC_Q (pfile, '.');
2537 CPP_PUTC_Q (pfile, '.');
2538 CPP_PUTC_Q (pfile, '.');
2539 FORWARD (2);
2540 CPP_NUL_TERMINATE_Q (pfile);
2541 pfile->only_seen_white = 0;
2542 return CPP_3DOTS;
2544 goto randomchar;
2546 op2:
2547 token = CPP_OTHER;
2548 pfile->only_seen_white = 0;
2549 CPP_RESERVE(pfile, 3);
2550 CPP_PUTC_Q (pfile, c);
2551 CPP_PUTC_Q (pfile, GETC ());
2552 CPP_NUL_TERMINATE_Q (pfile);
2553 return token;
2555 case 'L':
2556 c2 = PEEKC ();
2557 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2559 CPP_PUTC (pfile, c);
2560 c = GETC ();
2561 goto string;
2563 goto letter;
2565 case '0': case '1': case '2': case '3': case '4':
2566 case '5': case '6': case '7': case '8': case '9':
2567 number:
2568 c2 = '.';
2569 for (;;)
2571 CPP_RESERVE (pfile, 2);
2572 CPP_PUTC_Q (pfile, c);
2573 c = PEEKC ();
2574 if (c == EOF)
2575 break;
2576 if (!is_idchar[c] && c != '.'
2577 && ((c2 != 'e' && c2 != 'E'
2578 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
2579 || (c != '+' && c != '-')))
2580 break;
2581 FORWARD(1);
2582 c2= c;
2584 CPP_NUL_TERMINATE_Q (pfile);
2585 pfile->only_seen_white = 0;
2586 return CPP_NUMBER;
2587 case 'b': case 'c': case 'd': case 'h': case 'o':
2588 case 'B': case 'C': case 'D': case 'H': case 'O':
2589 if (opts->chill && PEEKC () == '\'')
2591 pfile->only_seen_white = 0;
2592 CPP_RESERVE (pfile, 2);
2593 CPP_PUTC_Q (pfile, c);
2594 CPP_PUTC_Q (pfile, '\'');
2595 FORWARD(1);
2596 for (;;)
2598 c = GETC();
2599 if (c == EOF)
2600 goto chill_number_eof;
2601 if (!is_idchar[c])
2602 break;
2603 CPP_PUTC (pfile, c);
2605 if (c == '\'')
2607 CPP_RESERVE (pfile, 2);
2608 CPP_PUTC_Q (pfile, c);
2609 CPP_NUL_TERMINATE_Q (pfile);
2610 return CPP_STRING;
2612 else
2614 FORWARD(-1);
2615 chill_number_eof:
2616 CPP_NUL_TERMINATE (pfile);
2617 return CPP_NUMBER;
2620 else
2621 goto letter;
2622 case '_':
2623 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2624 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2625 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2626 case 'x': case 'y': case 'z':
2627 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2628 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2629 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2630 case 'Y': case 'Z':
2631 letter:
2633 HASHNODE *hp;
2634 unsigned char *ident;
2635 int before_name_written = CPP_WRITTEN (pfile);
2636 int ident_len;
2637 parse_name (pfile, c);
2638 pfile->only_seen_white = 0;
2639 if (pfile->no_macro_expand)
2640 return CPP_NAME;
2641 ident = pfile->token_buffer + before_name_written;
2642 ident_len = CPP_PWRITTEN (pfile) - ident;
2643 hp = cpp_lookup (pfile, ident, ident_len, -1);
2644 if (!hp)
2645 return CPP_NAME;
2646 if (hp->type == T_DISABLED)
2648 if (pfile->output_escapes)
2649 { /* Return "\r-IDENT", followed by '\0'. */
2650 int i;
2651 CPP_RESERVE (pfile, 3);
2652 ident = pfile->token_buffer + before_name_written;
2653 CPP_ADJUST_WRITTEN (pfile, 2);
2654 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2655 ident[0] = '\r';
2656 ident[1] = '-';
2658 return CPP_NAME;
2661 /* If macro wants an arglist, verify that a '(' follows.
2662 first skip all whitespace, copying it to the output
2663 after the macro name. Then, if there is no '(',
2664 decide this is not a macro call and leave things that way. */
2665 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2667 int is_macro_call, macbuf_whitespace = 0;
2669 parse_set_mark (pfile);
2670 for (;;)
2672 cpp_skip_hspace (pfile);
2673 c = PEEKC ();
2674 is_macro_call = c == '(';
2675 if (c != EOF)
2677 if (c != '\n')
2678 break;
2679 FORWARD (1);
2681 else
2683 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2685 if (CPP_BUFFER (pfile)->mark !=
2686 (CPP_BUFFER (pfile)->cur
2687 - CPP_BUFFER (pfile)->buf))
2688 macbuf_whitespace = 1;
2690 /* The mark goes away automatically when
2691 the buffer is popped. */
2692 cpp_pop_buffer (pfile);
2693 parse_set_mark (pfile);
2695 else
2696 break;
2699 if (!is_macro_call)
2701 parse_goto_mark (pfile);
2702 if (macbuf_whitespace)
2703 CPP_PUTC (pfile, ' ');
2705 else
2706 parse_clear_mark (pfile);
2707 if (!is_macro_call)
2708 return CPP_NAME;
2710 /* This is now known to be a macro call.
2711 Expand the macro, reading arguments as needed,
2712 and push the expansion on the input stack. */
2713 macroexpand (pfile, hp);
2714 CPP_SET_WRITTEN (pfile, before_name_written);
2716 goto get_next;
2718 case ' ': case '\t': case '\v':
2719 for (;;)
2721 CPP_PUTC (pfile, c);
2722 c = PEEKC ();
2723 if (c == EOF || !is_hor_space[c])
2724 break;
2725 FORWARD(1);
2727 return CPP_HSPACE;
2729 case '\r':
2730 if (CPP_BUFFER (pfile)->has_escapes)
2732 c = GETC ();
2733 if (c == '-')
2735 if (pfile->output_escapes)
2736 CPP_PUTS (pfile, "\r-", 2);
2737 parse_name (pfile, GETC ());
2738 return CPP_NAME;
2740 else if (c == ' ')
2742 CPP_RESERVE (pfile, 2);
2743 if (pfile->output_escapes)
2744 CPP_PUTC_Q (pfile, '\r');
2745 CPP_PUTC_Q (pfile, c);
2746 return CPP_HSPACE;
2748 else
2750 cpp_fatal (pfile,
2751 "internal error: unrecognized escape \\r%c", c);
2752 goto get_next;
2755 else
2757 /* Backslash newline is ignored. */
2758 CPP_BUMP_LINE (pfile);
2759 goto get_next;
2762 case '\n':
2763 CPP_PUTC (pfile, c);
2764 if (pfile->only_seen_white == 0)
2765 pfile->only_seen_white = 1;
2766 CPP_BUMP_LINE (pfile);
2767 if (! CPP_OPTIONS (pfile)->no_line_commands)
2769 pfile->lineno++;
2770 if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2771 output_line_command (pfile, same_file);
2773 return CPP_VSPACE;
2775 case '(': token = CPP_LPAREN; goto char1;
2776 case ')': token = CPP_RPAREN; goto char1;
2777 case '{': token = CPP_LBRACE; goto char1;
2778 case '}': token = CPP_RBRACE; goto char1;
2779 case ',': token = CPP_COMMA; goto char1;
2780 case ';': token = CPP_SEMICOLON; goto char1;
2782 randomchar:
2783 default:
2784 token = CPP_OTHER;
2785 char1:
2786 pfile->only_seen_white = 0;
2787 CPP_PUTC (pfile, c);
2788 return token;
2793 /* Like cpp_get_token, but skip spaces and comments. */
2795 enum cpp_token
2796 cpp_get_non_space_token (pfile)
2797 cpp_reader *pfile;
2799 int old_written = CPP_WRITTEN (pfile);
2800 for (;;)
2802 enum cpp_token token = cpp_get_token (pfile);
2803 if (token != CPP_COMMENT && token != CPP_POP
2804 && token != CPP_HSPACE && token != CPP_VSPACE)
2805 return token;
2806 CPP_SET_WRITTEN (pfile, old_written);
2810 /* Parse an identifier starting with C. */
2812 static void
2813 parse_name (pfile, c)
2814 cpp_reader *pfile;
2815 int c;
2817 for (;;)
2819 if (! is_idchar[c])
2821 FORWARD (-1);
2822 break;
2825 if (c == '$' && CPP_PEDANTIC (pfile))
2826 cpp_pedwarn (pfile, "`$' in identifier");
2828 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
2829 CPP_PUTC_Q (pfile, c);
2830 c = GETC();
2831 if (c == EOF)
2832 break;
2834 CPP_NUL_TERMINATE_Q (pfile);
2835 return;
2838 /* Parse a string starting with C. A single quoted string is treated
2839 like a double -- some programs (e.g., troff) are perverse this way.
2840 (However, a single quoted string is not allowed to extend over
2841 multiple lines.) */
2842 static void
2843 parse_string (pfile, c)
2844 cpp_reader *pfile;
2845 int c;
2847 long start_line, start_column;
2849 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2851 CPP_PUTC (pfile, c);
2852 while (1)
2854 int cc = GETC();
2855 if (cc == EOF)
2857 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2859 /* try harder: this string crosses a macro expansion
2860 boundary. This can happen naturally if -traditional.
2861 Otherwise, only -D can make a macro with an unmatched
2862 quote. */
2863 cpp_pop_buffer (pfile);
2864 continue;
2867 cpp_error_with_line (pfile, start_line, start_column,
2868 "unterminated string or character constant");
2869 if (pfile->multiline_string_line != start_line
2870 && pfile->multiline_string_line != 0)
2871 cpp_error_with_line (pfile,
2872 pfile->multiline_string_line, -1,
2873 "possible real start of unterminated constant");
2874 pfile->multiline_string_line = 0;
2875 break;
2877 CPP_PUTC (pfile, cc);
2878 switch (cc)
2880 case '\n':
2881 CPP_BUMP_LINE (pfile);
2882 pfile->lineno++;
2883 /* Character constants may not extend over multiple lines.
2884 In ANSI, neither may strings. We accept multiline strings
2885 as an extension. */
2886 if (c == '\'')
2888 cpp_error_with_line (pfile, start_line, start_column,
2889 "unterminated character constant");
2890 return;
2892 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2894 cpp_pedwarn_with_line (pfile, start_line, start_column,
2895 "string constant runs past end of line");
2897 if (pfile->multiline_string_line == 0)
2898 pfile->multiline_string_line = start_line;
2899 break;
2901 case '\r':
2902 CPP_ADJUST_WRITTEN (pfile, -1);
2903 if (CPP_BUFFER (pfile)->has_escapes)
2905 cpp_fatal (pfile,
2906 "internal error: \\r escape inside string constant");
2907 FORWARD(1);
2909 else
2910 /* Backslash newline is replaced by nothing at all. */
2911 CPP_BUMP_LINE (pfile);
2912 break;
2914 case '\\':
2915 cc = GETC();
2916 if (cc != EOF)
2917 CPP_PUTC (pfile, cc);
2918 break;
2920 case '\"':
2921 case '\'':
2922 if (cc == c)
2923 return;
2924 break;
2929 /* Read an assertion into the token buffer, converting to
2930 canonical form: `#predicate(a n swe r)' The next non-whitespace
2931 character to read should be the first letter of the predicate.
2932 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2933 with answer (see callers for why). In case of 0, an error has been
2934 printed. */
2935 static int
2936 parse_assertion (pfile)
2937 cpp_reader *pfile;
2939 int c, dropwhite;
2940 cpp_skip_hspace (pfile);
2941 c = PEEKC();
2942 if (! is_idstart[c])
2944 cpp_error (pfile, "assertion predicate is not an identifier");
2945 return 0;
2947 CPP_PUTC(pfile, '#');
2948 FORWARD(1);
2949 parse_name(pfile, c);
2951 c = PEEKC();
2952 if (c != '(')
2954 if (is_hor_space[c] || c == '\r')
2955 cpp_skip_hspace (pfile);
2956 c = PEEKC();
2958 if (c != '(')
2959 return 1;
2961 CPP_PUTC(pfile, '(');
2962 FORWARD(1);
2963 dropwhite = 1;
2964 while ((c = GETC()) != ')')
2966 if (is_hor_space[c])
2968 if (! dropwhite)
2970 CPP_PUTC(pfile, ' ');
2971 dropwhite = 1;
2974 else if (c == '\n' || c == EOF)
2976 if (c == '\n') FORWARD(-1);
2977 cpp_error (pfile, "un-terminated assertion answer");
2978 return 0;
2980 else if (c == '\r')
2981 /* \r cannot be a macro escape here. */
2982 CPP_BUMP_LINE (pfile);
2983 else
2985 CPP_PUTC (pfile, c);
2986 dropwhite = 0;
2990 if (pfile->limit[-1] == ' ')
2991 pfile->limit[-1] = ')';
2992 else if (pfile->limit[-1] == '(')
2994 cpp_error (pfile, "empty token sequence in assertion");
2995 return 0;
2997 else
2998 CPP_PUTC (pfile, ')');
3000 CPP_NUL_TERMINATE (pfile);
3001 return 2;
3004 static int
3005 do_assert (pfile, keyword)
3006 cpp_reader *pfile;
3007 const struct directive *keyword ATTRIBUTE_UNUSED;
3009 char *sym;
3010 int ret, c;
3011 HASHNODE *base, *this;
3012 int baselen, thislen;
3014 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3015 && !CPP_BUFFER (pfile)->system_header_p)
3016 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
3018 cpp_skip_hspace (pfile);
3019 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
3020 ret = parse_assertion (pfile);
3021 if (ret == 0)
3022 goto error;
3023 else if (ret == 1)
3025 cpp_error (pfile, "missing token-sequence in `#assert'");
3026 goto error;
3029 cpp_skip_hspace (pfile);
3030 c = PEEKC();
3031 if (c != EOF && c != '\n')
3033 cpp_error (pfile, "junk at end of `#assert'");
3034 goto error;
3037 thislen = strlen (sym);
3038 baselen = index (sym, '(') - sym;
3039 this = cpp_lookup (pfile, sym, thislen, -1);
3040 if (this)
3042 cpp_warning (pfile, "`%s' re-asserted", sym);
3043 goto error;
3046 base = cpp_lookup (pfile, sym, baselen, -1);
3047 if (! base)
3048 base = cpp_install (pfile, sym, baselen, T_ASSERT, 0, -1);
3049 else if (base->type != T_ASSERT)
3051 /* Token clash - but with what?! */
3052 cpp_fatal (pfile,
3053 "cpp internal error: base->type != T_ASSERT in do_assert");
3054 goto error;
3057 this = cpp_install (pfile, sym, thislen, T_ASSERT,
3058 (char *)base->value.aschain, -1);
3059 base->value.aschain = this;
3061 pfile->limit = (unsigned char *) sym; /* Pop */
3062 return 0;
3064 error:
3065 skip_rest_of_line (pfile);
3066 pfile->limit = (unsigned char *) sym; /* Pop */
3067 return 0;
3070 static int
3071 do_unassert (pfile, keyword)
3072 cpp_reader *pfile;
3073 const struct directive *keyword ATTRIBUTE_UNUSED;
3075 int c, ret;
3076 char *sym;
3077 long baselen, thislen;
3078 HASHNODE *base, *this, *next;
3080 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3081 && !CPP_BUFFER (pfile)->system_header_p)
3082 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
3084 cpp_skip_hspace (pfile);
3086 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
3087 ret = parse_assertion (pfile);
3088 if (ret == 0)
3089 goto error;
3091 cpp_skip_hspace (pfile);
3092 c = PEEKC ();
3093 if (c != EOF && c != '\n')
3094 cpp_error (pfile, "junk at end of `#unassert'");
3096 thislen = strlen (sym);
3097 if (ret == 1)
3099 base = cpp_lookup (pfile, sym, thislen, -1);
3100 if (! base)
3101 goto error; /* It isn't an error to #undef what isn't #defined,
3102 so it isn't an error to #unassert what isn't
3103 #asserted either. */
3105 for (this = base->value.aschain; this; this = next)
3107 next = this->value.aschain;
3108 delete_macro (this);
3110 delete_macro (base);
3112 else
3114 baselen = index (sym, '(') - sym;
3115 base = cpp_lookup (pfile, sym, baselen, -1);
3116 if (! base) goto error;
3117 this = cpp_lookup (pfile, sym, thislen, -1);
3118 if (! this) goto error;
3120 next = base;
3121 while (next->value.aschain != this)
3122 next = next->value.aschain;
3124 next->value.aschain = this->value.aschain;
3125 delete_macro (this);
3127 if (base->value.aschain == NULL)
3128 delete_macro (base); /* Last answer for this predicate deleted. */
3131 pfile->limit = (unsigned char *) sym; /* Pop */
3132 return 0;
3133 error:
3134 skip_rest_of_line (pfile);
3135 pfile->limit = (unsigned char *) sym; /* Pop */
3136 return 0;
3139 /* Process STR as if it appeared as the body of an #unassert. */
3140 void
3141 cpp_unassert (pfile, str)
3142 cpp_reader *pfile;
3143 unsigned char *str;
3145 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
3147 do_assert (pfile, NULL);
3148 cpp_pop_buffer (pfile);
3153 cpp_read_check_assertion (pfile)
3154 cpp_reader *pfile;
3156 U_CHAR *name = CPP_PWRITTEN (pfile);
3157 int result;
3158 HASHNODE *hp;
3160 FORWARD (1); /* Skip '#' */
3161 cpp_skip_hspace (pfile);
3162 if (! parse_assertion (pfile))
3163 result = 0;
3164 else
3166 hp = cpp_lookup (pfile, name, CPP_PWRITTEN (pfile) - name, -1);
3167 result = (hp != 0);
3170 pfile->limit = name;
3171 return result;
3174 /* Remember the current position of PFILE. */
3176 void
3177 parse_set_mark (pfile)
3178 cpp_reader *pfile;
3180 cpp_buffer *ip = CPP_BUFFER (pfile);
3181 if (ip->mark != -1)
3182 cpp_fatal (pfile,
3183 "cpp internal error: ip->mark != -1 in parse_set_mark");
3185 ip->mark = ip->cur - ip->buf;
3188 /* Clear the current mark - we no longer need it. */
3190 void
3191 parse_clear_mark (pfile)
3192 cpp_reader *pfile;
3194 cpp_buffer *ip = CPP_BUFFER (pfile);
3195 if (ip->mark == -1)
3196 cpp_fatal (pfile,
3197 "cpp internal error: ip->mark == -1 in parse_clear_mark");
3199 ip->mark = -1;
3202 /* Backup the current position of PFILE to that saved in its mark,
3203 and clear the mark. */
3205 void
3206 parse_goto_mark (pfile)
3207 cpp_reader *pfile;
3209 cpp_buffer *ip = CPP_BUFFER (pfile);
3210 if (ip->mark == -1)
3211 cpp_fatal (pfile,
3212 "cpp internal error: ip->mark == -1 in parse_goto_mark");
3214 ip->cur = ip->buf + ip->mark;
3215 ip->mark = -1;
3218 static void
3219 cpp_print_file_and_line (pfile)
3220 cpp_reader *pfile;
3222 cpp_buffer *ip = cpp_file_buffer (pfile);
3224 if (ip != NULL)
3226 long line, col;
3227 cpp_buf_line_and_col (ip, &line, &col);
3228 cpp_file_line_for_message (pfile, ip->nominal_fname,
3229 line, pfile->show_column ? col : -1);
3233 static void
3234 v_cpp_error (pfile, msgid, ap)
3235 cpp_reader *pfile;
3236 const char *msgid;
3237 va_list ap;
3239 cpp_print_containing_files (pfile);
3240 cpp_print_file_and_line (pfile);
3241 v_cpp_message (pfile, 1, msgid, ap);
3244 void
3245 cpp_error VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3247 #ifndef ANSI_PROTOTYPES
3248 cpp_reader *pfile;
3249 const char *msgid;
3250 #endif
3251 va_list ap;
3253 VA_START(ap, msgid);
3255 #ifndef ANSI_PROTOTYPES
3256 pfile = va_arg (ap, cpp_reader *);
3257 msgid = va_arg (ap, const char *);
3258 #endif
3260 v_cpp_error (pfile, msgid, ap);
3261 va_end(ap);
3264 /* Print error message but don't count it. */
3266 static void
3267 v_cpp_warning (pfile, msgid, ap)
3268 cpp_reader *pfile;
3269 const char *msgid;
3270 va_list ap;
3272 if (CPP_OPTIONS (pfile)->inhibit_warnings)
3273 return;
3275 if (CPP_OPTIONS (pfile)->warnings_are_errors)
3276 pfile->errors++;
3278 cpp_print_containing_files (pfile);
3279 cpp_print_file_and_line (pfile);
3280 v_cpp_message (pfile, 0, msgid, ap);
3283 void
3284 cpp_warning VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3286 #ifndef ANSI_PROTOTYPES
3287 cpp_reader *pfile;
3288 const char *msgid;
3289 #endif
3290 va_list ap;
3292 VA_START (ap, msgid);
3294 #ifndef ANSI_PROTOTYPES
3295 pfile = va_arg (ap, cpp_reader *);
3296 msgid = va_arg (ap, const char *);
3297 #endif
3299 v_cpp_warning (pfile, msgid, ap);
3300 va_end(ap);
3303 /* Print an error message and maybe count it. */
3305 void
3306 cpp_pedwarn VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3308 #ifndef ANSI_PROTOTYPES
3309 cpp_reader *pfile;
3310 const char *msgid;
3311 #endif
3312 va_list ap;
3314 VA_START (ap, msgid);
3316 #ifndef ANSI_PROTOTYPES
3317 pfile = va_arg (ap, cpp_reader *);
3318 msgid = va_arg (ap, const char *);
3319 #endif
3321 if (CPP_OPTIONS (pfile)->pedantic_errors)
3322 v_cpp_error (pfile, msgid, ap);
3323 else
3324 v_cpp_warning (pfile, msgid, ap);
3325 va_end(ap);
3328 static void
3329 v_cpp_error_with_line (pfile, line, column, msgid, ap)
3330 cpp_reader * pfile;
3331 int line;
3332 int column;
3333 const char * msgid;
3334 va_list ap;
3336 cpp_buffer *ip = cpp_file_buffer (pfile);
3338 cpp_print_containing_files (pfile);
3340 if (ip != NULL)
3341 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
3343 v_cpp_message (pfile, 1, msgid, ap);
3346 void
3347 cpp_error_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3348 const char *msgid, ...))
3350 #ifndef ANSI_PROTOTYPES
3351 cpp_reader *pfile;
3352 int line;
3353 int column;
3354 const char *msgid;
3355 #endif
3356 va_list ap;
3358 VA_START (ap, msgid);
3360 #ifndef ANSI_PROTOTYPES
3361 pfile = va_arg (ap, cpp_reader *);
3362 line = va_arg (ap, int);
3363 column = va_arg (ap, int);
3364 msgid = va_arg (ap, const char *);
3365 #endif
3367 v_cpp_error_with_line(pfile, line, column, msgid, ap);
3368 va_end(ap);
3371 static void
3372 v_cpp_warning_with_line (pfile, line, column, msgid, ap)
3373 cpp_reader * pfile;
3374 int line;
3375 int column;
3376 const char *msgid;
3377 va_list ap;
3379 cpp_buffer *ip;
3381 if (CPP_OPTIONS (pfile)->inhibit_warnings)
3382 return;
3384 if (CPP_OPTIONS (pfile)->warnings_are_errors)
3385 pfile->errors++;
3387 cpp_print_containing_files (pfile);
3389 ip = cpp_file_buffer (pfile);
3391 if (ip != NULL)
3392 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
3394 v_cpp_message (pfile, 0, msgid, ap);
3397 void
3398 cpp_warning_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3399 const char *msgid, ...))
3401 #ifndef ANSI_PROTOTYPES
3402 cpp_reader *pfile;
3403 int line;
3404 int column;
3405 const char *msgid;
3406 #endif
3407 va_list ap;
3409 VA_START (ap, msgid);
3411 #ifndef ANSI_PROTOTYPES
3412 pfile = va_arg (ap, cpp_reader *);
3413 line = va_arg (ap, int);
3414 column = va_arg (ap, int);
3415 msgid = va_arg (ap, const char *);
3416 #endif
3418 v_cpp_warning_with_line (pfile, line, column, msgid, ap);
3419 va_end(ap);
3422 void
3423 cpp_pedwarn_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3424 const char *msgid, ...))
3426 #ifndef ANSI_PROTOTYPES
3427 cpp_reader *pfile;
3428 int line;
3429 int column;
3430 const char *msgid;
3431 #endif
3432 va_list ap;
3434 VA_START (ap, msgid);
3436 #ifndef ANSI_PROTOTYPES
3437 pfile = va_arg (ap, cpp_reader *);
3438 line = va_arg (ap, int);
3439 column = va_arg (ap, int);
3440 msgid = va_arg (ap, const char *);
3441 #endif
3443 if (CPP_OPTIONS (pfile)->pedantic_errors)
3444 v_cpp_error_with_line (pfile, column, line, msgid, ap);
3445 else
3446 v_cpp_warning_with_line (pfile, line, column, msgid, ap);
3447 va_end(ap);
3450 /* Report a warning (or an error if pedantic_errors)
3451 giving specified file name and line number, not current. */
3453 void
3454 cpp_pedwarn_with_file_and_line VPROTO ((cpp_reader *pfile, const char *file,
3455 int line, const char *msgid, ...))
3457 #ifndef ANSI_PROTOTYPES
3458 cpp_reader *pfile;
3459 const char *file;
3460 int line;
3461 const char *msgid;
3462 #endif
3463 va_list ap;
3465 VA_START (ap, msgid);
3467 #ifndef ANSI_PROTOTYPES
3468 pfile = va_arg (ap, cpp_reader *);
3469 file = va_arg (ap, const char *);
3470 line = va_arg (ap, int);
3471 msgid = va_arg (ap, const char *);
3472 #endif
3474 if (!CPP_OPTIONS (pfile)->pedantic_errors
3475 && CPP_OPTIONS (pfile)->inhibit_warnings)
3476 return;
3477 if (file != NULL)
3478 cpp_file_line_for_message (pfile, file, line, -1);
3479 v_cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, msgid, ap);
3480 va_end(ap);
3483 /* my_strerror - return the descriptive text associated with an
3484 `errno' code. */
3486 static const char *
3487 my_strerror (errnum)
3488 int errnum;
3490 const char *result;
3492 #ifndef VMS
3493 #ifndef HAVE_STRERROR
3494 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
3495 #else
3496 result = strerror (errnum);
3497 #endif
3498 #else /* VMS */
3499 /* VAXCRTL's strerror() takes an optional second argument, which only
3500 matters when the first argument is EVMSERR. However, it's simplest
3501 just to pass it unconditionally. `vaxc$errno' is declared in
3502 <errno.h>, and maintained by the library in parallel with `errno'.
3503 We assume that caller's `errnum' either matches the last setting of
3504 `errno' by the library or else does not have the value `EVMSERR'. */
3506 result = strerror (errnum, vaxc$errno);
3507 #endif
3509 if (!result)
3510 result = "errno = ?";
3512 return result;
3515 /* Error including a message from `errno'. */
3517 void
3518 cpp_error_from_errno (pfile, name)
3519 cpp_reader *pfile;
3520 const char *name;
3522 cpp_message_from_errno (pfile, 1, name);
3525 void
3526 cpp_message_from_errno (pfile, is_error, name)
3527 cpp_reader *pfile;
3528 int is_error;
3529 const char *name;
3531 int e = errno;
3532 cpp_buffer *ip = cpp_file_buffer (pfile);
3534 cpp_print_containing_files (pfile);
3536 if (ip != NULL)
3537 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
3539 cpp_message (pfile, is_error, "%s: %s", name, my_strerror (e));
3542 void
3543 cpp_perror_with_name (pfile, name)
3544 cpp_reader *pfile;
3545 const char *name;
3547 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
3550 /* TODO:
3551 * No pre-compiled header file support.
3553 * Possibly different enum token codes for each C/C++ token.
3555 * Find and cleanup remaining uses of static variables,
3557 * Support -dM flag (dump_all_macros).
3559 * Support for_lint flag.