* java/lang/natSystem.cc (SystemClass): New define.
[official-gcc.git] / gcc / cpplib.c
blob3ed75db1d66a5575b3a0b7feec18e25d972cc875
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 char *my_strerror PROTO ((int));
42 static void validate_else PROTO ((cpp_reader *, 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 *, struct directive *));
63 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 *, struct directive *));
72 static int do_line PARAMS ((cpp_reader *, struct directive *));
73 static int do_include PARAMS ((cpp_reader *, struct directive *));
74 static int do_undef PARAMS ((cpp_reader *, struct directive *));
75 static int do_error PARAMS ((cpp_reader *, struct directive *));
76 static int do_pragma PARAMS ((cpp_reader *, struct directive *));
77 static int do_ident PARAMS ((cpp_reader *, struct directive *));
78 static int do_if PARAMS ((cpp_reader *, struct directive *));
79 static int do_xifdef PARAMS ((cpp_reader *, struct directive *));
80 static int do_else PARAMS ((cpp_reader *, struct directive *));
81 static int do_elif PARAMS ((cpp_reader *, struct directive *));
82 static int do_endif PARAMS ((cpp_reader *, struct directive *));
83 #ifdef SCCS_DIRECTIVE
84 static int do_sccs PARAMS ((cpp_reader *, struct directive *));
85 #endif
86 static int do_assert PARAMS ((cpp_reader *, struct directive *));
87 static int do_unassert PARAMS ((cpp_reader *, struct directive *));
88 static int do_warning PARAMS ((cpp_reader *, struct directive *));
90 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
91 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
93 /* Here is the actual list of #-directives, most-often-used first.
94 The initialize_builtins function assumes #define is the very first. */
96 static struct directive directive_table[] = {
97 { 6, do_define, "define", T_DEFINE },
98 { 5, do_xifdef, "ifdef", T_IFDEF },
99 { 6, do_xifdef, "ifndef", T_IFNDEF },
100 { 7, do_include, "include", T_INCLUDE },
101 { 12, do_include, "include_next", T_INCLUDE_NEXT },
102 { 6, do_include, "import", T_IMPORT },
103 { 5, do_endif, "endif", T_ENDIF },
104 { 4, do_else, "else", T_ELSE },
105 { 2, do_if, "if", T_IF },
106 { 4, do_elif, "elif", T_ELIF },
107 { 5, do_undef, "undef", T_UNDEF },
108 { 5, do_error, "error", T_ERROR },
109 { 7, do_warning, "warning", T_WARNING },
110 { 6, do_pragma, "pragma", T_PRAGMA },
111 { 4, do_line, "line", T_LINE },
112 { 5, do_ident, "ident", T_IDENT },
113 #ifdef SCCS_DIRECTIVE
114 { 4, do_sccs, "sccs", T_SCCS },
115 #endif
116 { 6, do_assert, "assert", T_ASSERT },
117 { 8, do_unassert, "unassert", T_UNASSERT },
118 { -1, 0, "", T_UNUSED }
121 /* Place into PFILE a quoted string representing the string SRC.
122 Caller must reserve enough space in pfile->token_buffer. */
124 void
125 quote_string (pfile, src)
126 cpp_reader *pfile;
127 const char *src;
129 U_CHAR c;
131 CPP_PUTC_Q (pfile, '\"');
132 for (;;)
133 switch ((c = *src++))
135 default:
136 if (ISPRINT (c))
137 CPP_PUTC_Q (pfile, c);
138 else
140 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
141 CPP_ADJUST_WRITTEN (pfile, 4);
143 break;
145 case '\"':
146 case '\\':
147 CPP_PUTC_Q (pfile, '\\');
148 CPP_PUTC_Q (pfile, c);
149 break;
151 case '\0':
152 CPP_PUTC_Q (pfile, '\"');
153 CPP_NUL_TERMINATE_Q (pfile);
154 return;
158 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
160 void
161 cpp_grow_buffer (pfile, n)
162 cpp_reader *pfile;
163 long n;
165 long old_written = CPP_WRITTEN (pfile);
166 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
167 pfile->token_buffer = (U_CHAR *)
168 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
169 CPP_SET_WRITTEN (pfile, old_written);
172 /* Process the string STR as if it appeared as the body of a #define
173 If STR is just an identifier, define it with value 1.
174 If STR has anything after the identifier, then it should
175 be identifier=definition. */
177 void
178 cpp_define (pfile, str)
179 cpp_reader *pfile;
180 U_CHAR *str;
182 U_CHAR *buf, *p;
183 size_t count;
185 /* Copy the entire option so we can modify it. */
186 count = strlen (str) + 3;
187 buf = (U_CHAR *) alloca (count);
188 memcpy (buf, str, count - 2);
189 /* Change the first "=" in the string to a space. If there is none,
190 tack " 1" on the end. */
191 p = (U_CHAR *) strchr (buf, '=');
192 if (p)
194 *p = ' ';
195 count -= 2;
197 else
198 strcpy (&buf[count-3], " 1");
200 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
202 do_define (pfile, NULL);
203 cpp_pop_buffer (pfile);
207 /* Process the string STR as if it appeared as the body of a #assert. */
208 void
209 cpp_assert (pfile, str)
210 cpp_reader *pfile;
211 U_CHAR *str;
213 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
215 do_assert (pfile, NULL);
216 cpp_pop_buffer (pfile);
221 static enum cpp_token
222 null_underflow (pfile)
223 cpp_reader *pfile ATTRIBUTE_UNUSED;
225 return CPP_EOF;
228 static int
229 null_cleanup (pbuf, pfile)
230 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
231 cpp_reader *pfile ATTRIBUTE_UNUSED;
233 return 0;
236 /* Skip a comment - C, C++, or Chill style. M is the first character
237 of the comment marker. If this really is a comment, skip to its
238 end and return ' '. If we hit end-of-file before end-of-comment,
239 return EOF. If this is not a comment, return M (which will be
240 '/' or '-'). */
242 static int
243 skip_comment (pfile, m)
244 cpp_reader *pfile;
245 int m;
247 if (m == '/' && PEEKC() == '*')
249 int c, prev_c = -1;
250 long line, col;
252 FORWARD(1);
253 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
254 for (;;)
256 c = GETC ();
257 if (c == EOF)
259 cpp_error_with_line (pfile, line, col, "unterminated comment");
260 return EOF;
262 else if (c == '\n' || c == '\r')
263 /* \r cannot be a macro escape marker here. */
264 CPP_BUMP_LINE (pfile);
265 else if (c == '/' && prev_c == '*')
266 return ' ';
267 else if (c == '*' && prev_c == '/'
268 && CPP_OPTIONS (pfile)->warn_comments)
269 cpp_warning (pfile, "`/*' within comment");
271 prev_c = c;
274 else if ((m == '/' && PEEKC() == '/'
275 && CPP_OPTIONS (pfile)->cplusplus_comments)
276 || (m == '-' && PEEKC() == '-'
277 && CPP_OPTIONS (pfile)->chill))
279 FORWARD(1);
280 for (;;)
282 int c = GETC ();
283 if (c == EOF)
284 return ' '; /* Allow // to be terminated by EOF. */
285 if (c == '\n')
287 /* Don't consider final '\n' to be part of comment. */
288 FORWARD(-1);
289 return ' ';
291 else if (c == '\r')
292 /* \r cannot be a macro escape marker here. */
293 CPP_BUMP_LINE (pfile);
296 else
297 return m;
300 /* Identical to skip_comment except that it copies the comment into the
301 token_buffer. This is used if put_out_comments. */
302 static int
303 copy_comment (pfile, m)
304 cpp_reader *pfile;
305 int m;
307 if (m == '/' && PEEKC() == '*')
309 int c, prev_c = -1;
310 long line, col;
312 CPP_PUTC (pfile, '/');
313 CPP_PUTC (pfile, '*');
314 FORWARD(1);
315 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
316 for (;;)
318 c = GETC ();
319 if (c == EOF)
321 cpp_error_with_line (pfile, line, col, "unterminated comment");
322 /* We must pretend this was a legitimate comment, so that the
323 output in token_buffer is not passed back tagged CPP_POP. */
324 return ' ';
326 else if (c == '\r')
328 /* \r cannot be a macro escape marker here. */
329 CPP_BUMP_LINE (pfile);
330 continue;
333 CPP_PUTC (pfile, c);
334 if (c == '\n')
336 pfile->lineno++;
337 CPP_BUMP_LINE (pfile);
339 else if (c == '/' && prev_c == '*')
340 return ' ';
341 else if (c == '*' && prev_c == '/'
342 && CPP_OPTIONS (pfile)->warn_comments)
343 cpp_warning (pfile, "`/*' within comment");
345 prev_c = c;
348 else if ((m == '/' && PEEKC() == '/'
349 && CPP_OPTIONS (pfile)->cplusplus_comments)
350 || (m == '-' && PEEKC() == '-'
351 && CPP_OPTIONS (pfile)->chill))
353 CPP_PUTC (pfile, m);
354 CPP_PUTC (pfile, m);
355 FORWARD(1);
356 for (;;)
358 int c = GETC ();
359 if (c == EOF)
360 return ' '; /* Allow line comments to be terminated by EOF. */
361 else if (c == '\n')
363 /* Don't consider final '\n' to be part of comment. */
364 FORWARD(-1);
365 return ' ';
367 else if (c == '\r')
368 /* \r cannot be a macro escape marker here. */
369 CPP_BUMP_LINE (pfile);
371 CPP_PUTC (pfile, c);
374 else
375 return m;
379 /* Skip whitespace \-newline and comments. Does not macro-expand. */
381 void
382 cpp_skip_hspace (pfile)
383 cpp_reader *pfile;
385 int c;
386 while (1)
388 c = GETC();
389 if (c == EOF)
390 return;
391 else if (is_hor_space[c])
393 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
394 cpp_pedwarn (pfile, "%s in preprocessing directive",
395 c == '\f' ? "formfeed" : "vertical tab");
397 else if (c == '\r')
399 /* \r is a backslash-newline marker if !has_escapes, and
400 a deletable-whitespace or no-reexpansion marker otherwise. */
401 if (CPP_BUFFER (pfile)->has_escapes)
403 if (PEEKC() == ' ')
404 FORWARD(1);
405 else
406 break;
408 else
409 CPP_BUFFER (pfile)->lineno++;
411 else if (c == '/' || c == '-')
413 c = skip_comment (pfile, c);
414 if (c == EOF)
415 return;
416 else if (c != ' ')
417 break;
419 else
420 break;
422 FORWARD(-1);
425 /* Read the rest of the current line.
426 The line is appended to PFILE's output buffer. */
428 static void
429 copy_rest_of_line (pfile)
430 cpp_reader *pfile;
432 for (;;)
434 int c = GETC();
435 switch (c)
437 case '\n':
438 FORWARD(-1);
439 case EOF:
440 CPP_NUL_TERMINATE (pfile);
441 return;
443 case '\r':
444 if (CPP_BUFFER (pfile)->has_escapes)
445 break;
446 else
448 CPP_BUFFER (pfile)->lineno++;
449 continue;
451 case '\'':
452 case '\"':
453 parse_string (pfile, c);
454 continue;
455 case '/':
456 if (PEEKC() == '*' && CPP_TRADITIONAL (pfile))
458 CPP_PUTS (pfile, "/**/", 4);
459 skip_comment (pfile, c);
460 continue;
462 /* else fall through */
463 case '-':
464 c = skip_comment (pfile, c);
465 break;
467 case '\f':
468 case '\v':
469 if (CPP_PEDANTIC (pfile))
470 cpp_pedwarn (pfile, "%s in preprocessing directive",
471 c == '\f' ? "formfeed" : "vertical tab");
472 break;
475 CPP_PUTC (pfile, c);
479 /* FIXME: It is almost definitely a performance win to make this do
480 the scan itself. >75% of calls to copy_r_o_l are from here or
481 skip_if_group, which means the common case is to copy stuff into the
482 token_buffer only to discard it. */
483 void
484 skip_rest_of_line (pfile)
485 cpp_reader *pfile;
487 long old = CPP_WRITTEN (pfile);
488 copy_rest_of_line (pfile);
489 CPP_SET_WRITTEN (pfile, old);
492 /* Handle a possible # directive.
493 '#' has already been read. */
495 static int
496 handle_directive (pfile)
497 cpp_reader *pfile;
499 int c;
500 register struct directive *kt;
501 int ident_length;
502 U_CHAR *ident;
503 long old_written = CPP_WRITTEN (pfile);
505 cpp_skip_hspace (pfile);
507 c = PEEKC ();
508 if (c >= '0' && c <= '9')
510 /* Handle # followed by a line number. */
511 if (CPP_PEDANTIC (pfile))
512 cpp_pedwarn (pfile, "`#' followed by integer");
513 do_line (pfile, NULL);
514 goto done_a_directive;
517 /* Now find the directive name. */
518 CPP_PUTC (pfile, '#');
519 parse_name (pfile, GETC());
520 ident = pfile->token_buffer + old_written + 1;
521 ident_length = CPP_PWRITTEN (pfile) - ident;
522 if (ident_length == 0 && PEEKC() == '\n')
524 /* A line of just `#' becomes blank. */
525 goto done_a_directive;
528 #if 0
529 if (ident_length == 0 || !is_idstart[*ident]) {
530 U_CHAR *p = ident;
531 while (is_idchar[*p]) {
532 if (*p < '0' || *p > '9')
533 break;
534 p++;
536 /* Avoid error for `###' and similar cases unless -pedantic. */
537 if (p == ident) {
538 while (*p == '#' || is_hor_space[*p]) p++;
539 if (*p == '\n') {
540 if (pedantic && !lang_asm)
541 cpp_warning (pfile, "invalid preprocessor directive");
542 return 0;
546 if (!lang_asm)
547 cpp_error (pfile, "invalid preprocessor directive name");
549 return 0;
551 #endif
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++) {
557 if (kt->length <= 0)
558 goto not_a_directive;
559 if (kt->length == ident_length
560 && !strncmp (kt->name, ident, ident_length))
561 break;
564 /* We may want to pass through #define, #undef, #pragma, and #include.
565 Other directives may create output, but we don't want the directive
566 itself out, so we pop it now. For example conditionals may emit
567 #failed ... #endfailed stuff. */
569 if (! (kt->type == T_DEFINE
570 || kt->type == T_PRAGMA
571 || (IS_INCLUDE_DIRECTIVE_TYPE (kt->type)
572 && CPP_OPTIONS (pfile)->dump_includes)))
573 CPP_SET_WRITTEN (pfile, old_written);
575 (*kt->func) (pfile, kt);
577 if (kt->type == T_DEFINE)
579 if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
581 /* Skip "#define". */
582 U_CHAR *p = pfile->token_buffer + old_written + 7;
584 SKIP_WHITE_SPACE (p);
585 while (is_idchar[*p]) p++;
586 pfile->limit = p;
587 CPP_PUTC (pfile, '\n');
589 else if (CPP_OPTIONS (pfile)->dump_macros != dump_definitions)
590 CPP_SET_WRITTEN (pfile, old_written);
593 done_a_directive:
594 return 1;
596 not_a_directive:
597 return 0;
600 /* Pass a directive through to the output file.
601 BUF points to the contents of the directive, as a contiguous string.
602 m LIMIT points to the first character past the end of the directive.
603 KEYWORD is the keyword-table entry for the directive. */
605 static void
606 pass_thru_directive (buf, limit, pfile, keyword)
607 U_CHAR *buf, *limit;
608 cpp_reader *pfile;
609 struct directive *keyword;
611 register unsigned keyword_length = keyword->length;
613 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
614 CPP_PUTC_Q (pfile, '#');
615 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
616 if (limit != buf && buf[0] != ' ')
617 CPP_PUTC_Q (pfile, ' ');
618 CPP_PUTS_Q (pfile, buf, limit - buf);
619 #if 0
620 CPP_PUTS_Q (pfile, '\n');
621 /* Count the line we have just made in the output,
622 to get in sync properly. */
623 pfile->lineno++;
624 #endif
627 /* Check a purported macro name SYMNAME, and yield its length.
628 ASSERTION is nonzero if this is really for an assertion name. */
631 check_macro_name (pfile, symname, assertion)
632 cpp_reader *pfile;
633 U_CHAR *symname;
634 int assertion;
636 U_CHAR *p;
637 int sym_length;
639 for (p = symname; is_idchar[*p]; p++)
641 sym_length = p - symname;
642 if (sym_length == 0
643 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
644 cpp_error (pfile,
645 assertion ? "invalid assertion name" : "invalid macro name");
646 else if (!is_idstart[*symname]
647 || (! strncmp (symname, "defined", 7) && sym_length == 7)) {
648 U_CHAR *msg; /* what pain... */
649 msg = (U_CHAR *) alloca (sym_length + 1);
650 bcopy (symname, msg, sym_length);
651 msg[sym_length] = 0;
652 cpp_error (pfile,
653 (assertion
654 ? "invalid assertion name `%s'"
655 : "invalid macro name `%s'"),
656 msg);
658 return sym_length;
662 /* Process a #define command.
663 KEYWORD is the keyword-table entry for #define,
664 or NULL for a "predefined" macro. */
666 static int
667 do_define (pfile, keyword)
668 cpp_reader *pfile;
669 struct directive *keyword;
671 int hashcode;
672 MACRODEF mdef;
673 HASHNODE *hp;
674 long here;
675 U_CHAR *macro, *buf, *end;
677 here = CPP_WRITTEN (pfile);
678 copy_rest_of_line (pfile);
680 /* Copy out the line so we can pop the token buffer. */
681 buf = pfile->token_buffer + here;
682 end = CPP_PWRITTEN (pfile);
683 macro = alloca (end - buf + 1);
684 bcopy (buf, macro, end - buf + 1);
685 end = macro + (end - buf);
687 CPP_SET_WRITTEN (pfile, here);
689 #if 0
690 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
691 if (pcp_outfile && keyword)
692 pass_thru_directive (macro, end, pfile, keyword);
693 #endif
695 mdef = create_definition (macro, end, pfile, keyword == NULL);
696 if (mdef.defn == 0)
697 goto nope;
699 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
701 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
703 int ok = 0;
704 /* Redefining a precompiled key is ok. */
705 if (hp->type == T_PCSTRING)
706 ok = 1;
707 /* Redefining a macro is ok if the definitions are the same. */
708 else if (hp->type == T_MACRO)
709 ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
710 /* Redefining a constant is ok with -D. */
711 else if (hp->type == T_CONST || hp->type == T_STDC)
712 ok = ! CPP_OPTIONS (pfile)->done_initializing;
713 /* Print the warning if it's not ok. */
714 if (!ok)
716 /* If we are passing through #define and #undef directives, do
717 that for this re-definition now. */
718 if (CPP_OPTIONS (pfile)->debug_output && keyword)
719 pass_thru_directive (macro, end, pfile, keyword);
721 cpp_pedwarn (pfile, "`%.*s' redefined", mdef.symlen, mdef.symnam);
722 if (hp->type == T_MACRO)
723 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
724 "this is the location of the previous definition");
726 /* Replace the old definition. */
727 hp->type = T_MACRO;
728 hp->value.defn = mdef.defn;
730 else
732 /* If we are passing through #define and #undef directives, do
733 that for this new definition now. */
734 if (CPP_OPTIONS (pfile)->debug_output && keyword)
735 pass_thru_directive (macro, end, pfile, keyword);
736 cpp_install (pfile, mdef.symnam, mdef.symlen, T_MACRO,
737 (char *) mdef.defn, hashcode);
740 return 0;
742 nope:
744 return 1;
748 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
749 If BUFFER != NULL, then use the LENGTH characters in BUFFER
750 as the new input buffer.
751 Return the new buffer, or NULL on failure. */
753 cpp_buffer *
754 cpp_push_buffer (pfile, buffer, length)
755 cpp_reader *pfile;
756 U_CHAR *buffer;
757 long length;
759 cpp_buffer *buf = CPP_BUFFER (pfile);
760 cpp_buffer *new;
761 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
763 cpp_fatal (pfile, "macro or `#include' recursion too deep");
764 return NULL;
767 new = (cpp_buffer *) xcalloc (sizeof (cpp_buffer), 1);
769 new->if_stack = pfile->if_stack;
770 new->cleanup = null_cleanup;
771 new->underflow = null_underflow;
772 new->buf = new->cur = buffer;
773 new->alimit = new->rlimit = buffer + length;
774 new->prev = buf;
775 new->mark = -1;
777 CPP_BUFFER (pfile) = new;
778 return new;
781 cpp_buffer *
782 cpp_pop_buffer (pfile)
783 cpp_reader *pfile;
785 cpp_buffer *buf = CPP_BUFFER (pfile);
786 (*buf->cleanup) (buf, pfile);
787 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
788 free (buf);
789 pfile->buffer_stack_depth--;
790 return CPP_BUFFER (pfile);
793 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
794 Pop the buffer when done. */
796 void
797 cpp_scan_buffer (pfile)
798 cpp_reader *pfile;
800 cpp_buffer *buffer = CPP_BUFFER (pfile);
801 for (;;)
803 enum cpp_token token = cpp_get_token (pfile);
804 if (token == CPP_EOF) /* Should not happen ... */
805 break;
806 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
808 cpp_pop_buffer (pfile);
809 break;
815 * Rescan a string (which may have escape marks) into pfile's buffer.
816 * Place the result in pfile->token_buffer.
818 * The input is copied before it is scanned, so it is safe to pass
819 * it something from the token_buffer that will get overwritten
820 * (because it follows CPP_WRITTEN). This is used by do_include.
823 void
824 cpp_expand_to_buffer (pfile, buf, length)
825 cpp_reader *pfile;
826 U_CHAR *buf;
827 int length;
829 register cpp_buffer *ip;
830 #if 0
831 cpp_buffer obuf;
832 #endif
833 U_CHAR *buf1;
834 #if 0
835 int odepth = indepth;
836 #endif
838 if (length < 0)
839 abort ();
841 /* Set up the input on the input stack. */
843 buf1 = (U_CHAR *) alloca (length + 1);
844 memcpy (buf1, buf, length);
845 buf1[length] = 0;
847 ip = cpp_push_buffer (pfile, buf1, length);
848 if (ip == NULL)
849 return;
850 ip->has_escapes = 1;
851 #if 0
852 ip->lineno = obuf.lineno = 1;
853 #endif
855 /* Scan the input, create the output. */
856 cpp_scan_buffer (pfile);
858 CPP_NUL_TERMINATE (pfile);
861 void
862 cpp_buf_line_and_col (pbuf, linep, colp)
863 register cpp_buffer *pbuf;
864 long *linep, *colp;
866 if (pbuf)
868 *linep = pbuf->lineno;
869 if (colp)
870 *colp = pbuf->cur - pbuf->line_base;
872 else
874 *linep = 0;
875 if (colp)
876 *colp = 0;
880 /* Return the cpp_buffer that corresponds to a file (not a macro). */
882 cpp_buffer *
883 cpp_file_buffer (pfile)
884 cpp_reader *pfile;
886 cpp_buffer *ip = CPP_BUFFER (pfile);
888 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
889 if (ip->fname != NULL)
890 return ip;
891 return NULL;
895 * write out a #line command, for instance, after an #include file.
896 * If CONDITIONAL is nonzero, we can omit the #line if it would
897 * appear to be a no-op, and we can output a few newlines instead
898 * if we want to increase the line number by a small amount.
899 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
902 void
903 output_line_command (pfile, conditional, file_change)
904 cpp_reader *pfile;
905 int conditional;
906 enum file_change_code file_change;
908 long line, col;
909 cpp_buffer *ip = CPP_BUFFER (pfile);
911 if (ip->fname == NULL)
912 return;
914 if (CPP_OPTIONS (pfile)->no_line_commands
915 || CPP_OPTIONS (pfile)->no_output)
916 return;
918 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
920 if (conditional)
922 if (line == pfile->lineno)
923 return;
925 /* If the inherited line number is a little too small,
926 output some newlines instead of a #line command. */
927 if (line > pfile->lineno && line < pfile->lineno + 8)
929 CPP_RESERVE (pfile, 20);
930 while (line > pfile->lineno)
932 CPP_PUTC_Q (pfile, '\n');
933 pfile->lineno++;
935 return;
939 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
940 CPP_PUTS_Q (pfile, "# ", 2);
942 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
943 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
945 quote_string (pfile, ip->nominal_fname);
946 if (file_change != same_file)
948 CPP_PUTC_Q (pfile, ' ');
949 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
951 /* Tell cc1 if following text comes from a system header file. */
952 if (ip->system_header_p)
954 CPP_PUTC_Q (pfile, ' ');
955 CPP_PUTC_Q (pfile, '3');
957 #ifndef NO_IMPLICIT_EXTERN_C
958 /* Tell cc1plus if following text should be treated as C. */
959 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
961 CPP_PUTC_Q (pfile, ' ');
962 CPP_PUTC_Q (pfile, '4');
964 #endif
965 CPP_PUTC_Q (pfile, '\n');
966 pfile->lineno = line;
970 /* Like cpp_get_token, except that it does not read past end-of-line.
971 Also, horizontal space is skipped, and macros are popped. */
973 static enum cpp_token
974 get_directive_token (pfile)
975 cpp_reader *pfile;
977 for (;;)
979 long old_written = CPP_WRITTEN (pfile);
980 enum cpp_token token;
981 cpp_skip_hspace (pfile);
982 if (PEEKC () == '\n')
983 return CPP_VSPACE;
984 token = cpp_get_token (pfile);
985 switch (token)
987 case CPP_POP:
988 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
989 return token;
990 /* ... else fall though ... */
991 case CPP_HSPACE: case CPP_COMMENT:
992 CPP_SET_WRITTEN (pfile, old_written);
993 break;
994 default:
995 return token;
1000 /* Handle #include and #import.
1001 This function expects to see "fname" or <fname> on the input.
1003 The input is normally in part of the output_buffer following
1004 CPP_WRITTEN, and will get overwritten by output_line_command.
1005 I.e. in input file specification has been popped by handle_directive.
1006 This is safe. */
1008 static int
1009 do_include (pfile, keyword)
1010 cpp_reader *pfile;
1011 struct directive *keyword;
1013 int importing = (keyword->type == T_IMPORT);
1014 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
1015 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
1016 int before; /* included before? */
1017 long flen;
1018 unsigned char *fbeg, *fend;
1019 cpp_buffer *fp;
1021 enum cpp_token token;
1023 /* Chain of dirs to search */
1024 struct include_hash *ihash;
1025 struct file_name_list *search_start;
1027 long old_written = CPP_WRITTEN (pfile);
1029 int fd;
1031 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1033 if (importing)
1034 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
1035 if (skip_dirs)
1036 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
1039 if (importing && CPP_OPTIONS (pfile)->warn_import
1040 && !CPP_OPTIONS (pfile)->inhibit_warnings
1041 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
1043 pfile->import_warning = 1;
1044 cpp_warning (pfile, "`#import' is obsolete, use an #ifndef wrapper in the header file");
1047 pfile->parsing_include_directive++;
1048 token = get_directive_token (pfile);
1049 pfile->parsing_include_directive--;
1051 if (token == CPP_STRING)
1053 fbeg = pfile->token_buffer + old_written + 1;
1054 fend = CPP_PWRITTEN (pfile) - 1;
1055 *fend = '\0';
1056 if (fbeg[-1] == '<')
1057 angle_brackets = 1;
1059 #ifdef VMS
1060 else if (token == CPP_NAME)
1062 /* Support '#include xyz' like VAX-C to allow for easy use of
1063 * all the decwindow include files. It defaults to '#include
1064 * <xyz.h>' and generates a warning. */
1065 cpp_warning (pfile,
1066 "VAX-C-style include specification found, use '#include <filename.h>' !");
1067 angle_brackets = 1;
1069 /* Append the missing `.h' to the name. */
1070 CPP_PUTS (pfile, ".h", 3)
1071 CPP_NUL_TERMINATE_Q (pfile);
1073 fbeg = pfile->token_buffer + old_written;
1074 fend = CPP_PWRITTEN (pfile);
1076 #endif
1077 else
1079 cpp_error (pfile,
1080 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1081 CPP_SET_WRITTEN (pfile, old_written);
1082 skip_rest_of_line (pfile);
1083 return 0;
1086 token = get_directive_token (pfile);
1087 if (token != CPP_VSPACE)
1089 cpp_error (pfile, "junk at end of `#include'");
1090 skip_rest_of_line (pfile);
1093 CPP_SET_WRITTEN (pfile, old_written);
1095 flen = fend - fbeg;
1097 if (flen == 0)
1099 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
1100 return 0;
1103 search_start = 0;
1105 for (fp = CPP_BUFFER (pfile);
1106 fp != CPP_NULL_BUFFER (pfile);
1107 fp = CPP_PREV_BUFFER (fp))
1108 if (fp->fname != NULL)
1109 break;
1111 if (fp == CPP_NULL_BUFFER (pfile))
1113 cpp_fatal (pfile, "cpp internal error: fp == NULL_BUFFER in do_include");
1114 return 1;
1117 /* For #include_next, skip in the search path past the dir in which the
1118 containing file was found. Treat files specified using an absolute path
1119 as if there are no more directories to search. Treat the primary source
1120 file like any other included source, but generate a warning. */
1121 if (skip_dirs && CPP_PREV_BUFFER(fp) != CPP_NULL_BUFFER (pfile))
1123 if (fp->ihash->foundhere != ABSOLUTE_PATH)
1124 search_start = fp->ihash->foundhere->next;
1126 else
1128 if (skip_dirs)
1129 cpp_warning (pfile, "#include_next in primary source file");
1131 if (angle_brackets)
1132 search_start = CPP_OPTIONS (pfile)->bracket_include;
1133 else
1135 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
1137 if (fp)
1138 search_start = fp->actual_dir;
1140 else
1141 search_start = CPP_OPTIONS (pfile)->quote_include;
1145 if (!search_start)
1147 cpp_error (pfile, "No include path in which to find %s", fbeg);
1148 return 0;
1151 fd = find_include_file (pfile, fbeg, search_start, &ihash, &before);
1153 if (fd == -2)
1154 return 0;
1156 if (fd == -1)
1158 if (CPP_OPTIONS (pfile)->print_deps_missing_files
1159 && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
1160 (pfile->system_include_depth > 0)))
1162 if (!angle_brackets)
1163 deps_output (pfile, fbeg, ' ');
1164 else
1166 char *p;
1167 struct file_name_list *ptr;
1168 /* If requested as a system header, assume it belongs in
1169 the first system header directory. */
1170 if (CPP_OPTIONS (pfile)->bracket_include)
1171 ptr = CPP_OPTIONS (pfile)->bracket_include;
1172 else
1173 ptr = CPP_OPTIONS (pfile)->quote_include;
1175 p = (char *) alloca (strlen (ptr->name)
1176 + strlen (fbeg) + 2);
1177 if (*ptr->name != '\0')
1179 strcpy (p, ptr->name);
1180 strcat (p, "/");
1182 strcat (p, fbeg);
1183 deps_output (pfile, p, ' ');
1186 /* If -M was specified, and this header file won't be added to
1187 the dependency list, then don't count this as an error,
1188 because we can still produce correct output. Otherwise, we
1189 can't produce correct output, because there may be
1190 dependencies we need inside the missing file, and we don't
1191 know what directory this missing file exists in. */
1192 else if (CPP_PRINT_DEPS (pfile)
1193 && (CPP_PRINT_DEPS (pfile)
1194 <= (angle_brackets || (pfile->system_include_depth > 0))))
1195 cpp_warning (pfile, "No include path in which to find %s", fbeg);
1196 else
1197 cpp_error_from_errno (pfile, fbeg);
1199 return 0;
1202 /* For -M, add the file to the dependencies on its first inclusion. */
1203 if (!before && (CPP_PRINT_DEPS (pfile)
1204 > (angle_brackets || (pfile->system_include_depth > 0))))
1205 deps_output (pfile, ihash->name, ' ');
1207 /* Handle -H option. */
1208 if (CPP_OPTIONS(pfile)->print_include_names)
1210 fp = CPP_BUFFER (pfile);
1211 while ((fp = CPP_PREV_BUFFER (fp)) != CPP_NULL_BUFFER (pfile))
1212 putc ('.', stderr);
1213 fprintf (stderr, " %s\n", ihash->name);
1216 /* Actually process the file */
1218 if (importing)
1219 ihash->control_macro = "";
1221 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
1223 close (fd);
1224 return 0;
1227 if (angle_brackets)
1228 pfile->system_include_depth++; /* Decremented in file_cleanup. */
1230 if (finclude (pfile, fd, ihash))
1232 output_line_command (pfile, 0, enter_file);
1233 pfile->only_seen_white = 2;
1236 return 0;
1239 /* Interpret #line command.
1240 Note that the filename string (if any) is treated as if it were an
1241 include filename. That means no escape handling. */
1243 static int
1244 do_line (pfile, keyword)
1245 cpp_reader *pfile;
1246 struct directive *keyword ATTRIBUTE_UNUSED;
1248 cpp_buffer *ip = CPP_BUFFER (pfile);
1249 int new_lineno;
1250 long old_written = CPP_WRITTEN (pfile);
1251 enum file_change_code file_change = same_file;
1252 enum cpp_token token;
1253 char *x;
1255 token = get_directive_token (pfile);
1257 if (token != CPP_NUMBER)
1259 cpp_error (pfile, "token after `#line' is not an integer");
1260 goto bad_line_directive;
1263 new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
1264 if (x[0] != '\0')
1266 cpp_error (pfile, "token after `#line' is not an integer");
1267 goto bad_line_directive;
1269 CPP_SET_WRITTEN (pfile, old_written);
1271 if (CPP_PEDANTIC (pfile) && new_lineno <= 0)
1272 cpp_pedwarn (pfile, "line number out of range in `#line' command");
1274 token = get_directive_token (pfile);
1276 if (token == CPP_STRING)
1278 U_CHAR *fname = pfile->token_buffer + old_written + 1;
1279 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
1280 long num_start = CPP_WRITTEN (pfile);
1282 token = get_directive_token (pfile);
1283 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
1285 U_CHAR *p = pfile->token_buffer + num_start;
1286 if (CPP_PEDANTIC (pfile))
1287 cpp_pedwarn (pfile, "garbage at end of `#line' command");
1289 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
1291 cpp_error (pfile, "invalid format `#line' command");
1292 goto bad_line_directive;
1294 if (*p == '1')
1295 file_change = enter_file;
1296 else if (*p == '2')
1297 file_change = leave_file;
1298 else if (*p == '3')
1299 ip->system_header_p = 1;
1300 else /* if (*p == '4') */
1301 ip->system_header_p = 2;
1303 CPP_SET_WRITTEN (pfile, num_start);
1304 token = get_directive_token (pfile);
1305 p = pfile->token_buffer + num_start;
1306 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4'))
1308 ip->system_header_p = *p == '3' ? 1 : 2;
1309 token = get_directive_token (pfile);
1311 if (token != CPP_VSPACE)
1313 cpp_error (pfile, "invalid format `#line' command");
1314 goto bad_line_directive;
1318 *end_name = '\0';
1320 if (strcmp (fname, ip->nominal_fname))
1322 char *newname, *oldname;
1323 if (!strcmp (fname, ip->fname))
1324 newname = ip->fname;
1325 else if (ip->last_nominal_fname
1326 && !strcmp (fname, ip->last_nominal_fname))
1327 newname = ip->last_nominal_fname;
1328 else
1329 newname = xstrdup (fname);
1331 oldname = ip->nominal_fname;
1332 ip->nominal_fname = newname;
1334 if (ip->last_nominal_fname
1335 && ip->last_nominal_fname != oldname
1336 && ip->last_nominal_fname != newname
1337 && ip->last_nominal_fname != ip->fname)
1338 free (ip->last_nominal_fname);
1340 if (newname == ip->fname)
1341 ip->last_nominal_fname = NULL;
1342 else
1343 ip->last_nominal_fname = oldname;
1346 else if (token != CPP_VSPACE && token != CPP_EOF)
1348 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1349 goto bad_line_directive;
1352 /* The Newline at the end of this line remains to be processed.
1353 To put the next line at the specified line number,
1354 we must store a line number now that is one less. */
1355 ip->lineno = new_lineno - 1;
1356 CPP_SET_WRITTEN (pfile, old_written);
1357 output_line_command (pfile, 0, file_change);
1358 return 0;
1360 bad_line_directive:
1361 skip_rest_of_line (pfile);
1362 CPP_SET_WRITTEN (pfile, old_written);
1363 return 0;
1366 /* Remove the definition of a symbol from the symbol table.
1367 According to the C standard, it is not an error to undef
1368 something that has no definitions. */
1369 static int
1370 do_undef (pfile, keyword)
1371 cpp_reader *pfile;
1372 struct directive *keyword;
1374 int sym_length;
1375 HASHNODE *hp;
1376 U_CHAR *buf, *name, *limit;
1377 int c;
1378 long here = CPP_WRITTEN (pfile);
1379 enum cpp_token token;
1381 cpp_skip_hspace (pfile);
1382 c = GETC();
1383 if (! is_idstart[c])
1385 cpp_error (pfile, "token after #undef is not an identifier");
1386 skip_rest_of_line (pfile);
1387 return 1;
1390 parse_name (pfile, c);
1391 buf = pfile->token_buffer + here;
1392 limit = CPP_PWRITTEN(pfile);
1394 /* Copy out the token so we can pop the token buffer. */
1395 name = alloca (limit - buf + 1);
1396 bcopy(buf, name, limit - buf);
1397 name[limit - buf] = '\0';
1399 token = get_directive_token (pfile);
1400 if (token != CPP_VSPACE && token != CPP_POP)
1402 cpp_pedwarn (pfile, "junk on line after #undef");
1403 skip_rest_of_line (pfile);
1406 CPP_SET_WRITTEN (pfile, here);
1408 #if 0
1409 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
1410 if (pcp_outfile && keyword)
1411 pass_thru_directive (buf, limit, pfile, keyword);
1412 #endif
1414 sym_length = check_macro_name (pfile, buf, 0);
1416 while ((hp = cpp_lookup (pfile, name, sym_length, -1)) != NULL)
1418 /* If we are generating additional info for debugging (with -g) we
1419 need to pass through all effective #undef commands. */
1420 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1421 pass_thru_directive (name, name+sym_length, pfile, keyword);
1422 if (hp->type != T_MACRO)
1423 cpp_warning (pfile, "undefining `%s'", hp->name);
1424 delete_macro (hp);
1427 return 0;
1430 /* Wrap do_undef for -U processing. */
1431 void
1432 cpp_undef (pfile, macro)
1433 cpp_reader *pfile;
1434 U_CHAR *macro;
1436 if (cpp_push_buffer (pfile, macro, strlen (macro)))
1438 do_undef (pfile, NULL);
1439 cpp_pop_buffer (pfile);
1445 * Report an error detected by the program we are processing.
1446 * Use the text of the line in the error message.
1447 * (We use error because it prints the filename & line#.)
1450 static int
1451 do_error (pfile, keyword)
1452 cpp_reader *pfile;
1453 struct directive *keyword ATTRIBUTE_UNUSED;
1455 long here = CPP_WRITTEN (pfile);
1456 U_CHAR *text;
1457 copy_rest_of_line (pfile);
1458 text = pfile->token_buffer + here;
1459 SKIP_WHITE_SPACE(text);
1461 cpp_error (pfile, "#error %s", text);
1462 CPP_SET_WRITTEN (pfile, here);
1463 return 0;
1467 * Report a warning detected by the program we are processing.
1468 * Use the text of the line in the warning message, then continue.
1471 static int
1472 do_warning (pfile, keyword)
1473 cpp_reader *pfile;
1474 struct directive *keyword ATTRIBUTE_UNUSED;
1476 U_CHAR *text;
1477 long here = CPP_WRITTEN(pfile);
1478 copy_rest_of_line (pfile);
1479 text = pfile->token_buffer + here;
1480 SKIP_WHITE_SPACE(text);
1482 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1483 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1485 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
1486 if -pedantic-errors is given, #warning should cause an error. */
1487 cpp_pedwarn (pfile, "#warning %s", text);
1488 CPP_SET_WRITTEN (pfile, here);
1489 return 0;
1492 /* Report program identification. */
1494 static int
1495 do_ident (pfile, keyword)
1496 cpp_reader *pfile;
1497 struct directive *keyword ATTRIBUTE_UNUSED;
1499 /* Allow #ident in system headers, since that's not user's fault. */
1500 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1501 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1503 skip_rest_of_line (pfile); /* Correct? Appears to match cccp. */
1505 return 0;
1508 /* Just check for some recognized pragmas that need validation here,
1509 and leave the text in the token buffer to be output. */
1511 static int
1512 do_pragma (pfile, keyword)
1513 cpp_reader *pfile;
1514 struct directive *keyword ATTRIBUTE_UNUSED;
1516 long here = CPP_WRITTEN (pfile);
1517 U_CHAR *buf;
1519 copy_rest_of_line (pfile);
1520 buf = pfile->token_buffer + here;
1521 SKIP_WHITE_SPACE (buf);
1523 if (!strncmp (buf, "once", 4))
1525 cpp_buffer *ip = NULL;
1527 /* Allow #pragma once in system headers, since that's not the user's
1528 fault. */
1529 if (!CPP_BUFFER (pfile)->system_header_p)
1530 cpp_warning (pfile, "`#pragma once' is obsolete");
1532 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
1534 if (ip == CPP_NULL_BUFFER (pfile))
1535 return 0;
1536 if (ip->fname != NULL)
1537 break;
1540 if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
1541 cpp_warning (pfile, "`#pragma once' outside include file");
1542 else
1543 ip->ihash->control_macro = ""; /* never repeat */
1546 if (!strncmp (buf, "implementation", 14))
1548 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1549 been included yet. */
1550 struct include_hash *ptr;
1551 U_CHAR *p = buf + 14, *fname, *fcopy;
1552 SKIP_WHITE_SPACE (p);
1553 if (*p == '\n' || *p != '\"')
1554 return 0;
1556 fname = p + 1;
1557 p = (U_CHAR *) index (fname, '\"');
1559 fcopy = alloca (p - fname + 1);
1560 bcopy (fname, fcopy, p - fname);
1561 fcopy[p-fname] = '\0';
1563 ptr = include_hash (pfile, fcopy, 0);
1564 if (ptr)
1565 cpp_warning (pfile,
1566 "`#pragma implementation' for `%s' appears after file is included",
1567 fcopy);
1570 return 0;
1573 #ifdef SCCS_DIRECTIVE
1574 /* Just ignore #sccs, on systems where we define it at all. */
1576 static int
1577 do_sccs (pfile, keyword)
1578 cpp_reader *pfile;
1579 struct directive *keyword ATTRIBUTE_UNUSED;
1581 if (CPP_PEDANTIC (pfile))
1582 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1583 skip_rest_of_line (pfile);
1584 return 0;
1586 #endif
1589 * handle #if command by
1590 * 1) inserting special `defined' keyword into the hash table
1591 * that gets turned into 0 or 1 by special_symbol (thus,
1592 * if the luser has a symbol called `defined' already, it won't
1593 * work inside the #if command)
1594 * 2) rescan the input into a temporary output buffer
1595 * 3) pass the output buffer to the yacc parser and collect a value
1596 * 4) clean up the mess left from steps 1 and 2.
1597 * 5) call conditional_skip to skip til the next #endif (etc.),
1598 * or not, depending on the value from step 3.
1601 static int
1602 do_if (pfile, keyword)
1603 cpp_reader *pfile;
1604 struct directive *keyword ATTRIBUTE_UNUSED;
1606 HOST_WIDEST_INT value = eval_if_expression (pfile);
1607 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
1608 return 0;
1612 * handle a #elif directive by not changing if_stack either.
1613 * see the comment above do_else.
1616 static int
1617 do_elif (pfile, keyword)
1618 cpp_reader *pfile;
1619 struct directive *keyword ATTRIBUTE_UNUSED;
1621 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
1622 cpp_error (pfile, "`#elif' not within a conditional");
1623 return 0;
1624 } else {
1625 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
1626 cpp_error (pfile, "`#elif' after `#else'");
1627 #if 0
1628 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
1629 #endif
1630 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
1631 && strcmp (pfile->if_stack->fname,
1632 CPP_BUFFER (pfile)->nominal_fname) != 0)
1633 fprintf (stderr, ", file %s", pfile->if_stack->fname);
1634 fprintf (stderr, ")\n");
1636 pfile->if_stack->type = T_ELIF;
1639 if (pfile->if_stack->if_succeeded)
1640 skip_if_group (pfile);
1641 else {
1642 HOST_WIDEST_INT value = eval_if_expression (pfile);
1643 if (value == 0)
1644 skip_if_group (pfile);
1645 else {
1646 ++pfile->if_stack->if_succeeded; /* continue processing input */
1647 output_line_command (pfile, 1, same_file);
1650 return 0;
1654 * evaluate a #if expression in BUF, of length LENGTH,
1655 * then parse the result as a C expression and return the value as an int.
1658 static HOST_WIDEST_INT
1659 eval_if_expression (pfile)
1660 cpp_reader *pfile;
1662 HOST_WIDEST_INT value;
1663 long old_written = CPP_WRITTEN (pfile);
1665 pfile->pcp_inside_if = 1;
1666 value = cpp_parse_expr (pfile);
1667 pfile->pcp_inside_if = 0;
1669 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1671 return value;
1675 * routine to handle ifdef/ifndef. Try to look up the symbol,
1676 * then do or don't skip to the #endif/#else/#elif depending
1677 * on what directive is actually being processed.
1680 static int
1681 do_xifdef (pfile, keyword)
1682 cpp_reader *pfile;
1683 struct directive *keyword;
1685 int skip;
1686 cpp_buffer *ip = CPP_BUFFER (pfile);
1687 U_CHAR *ident;
1688 int ident_length;
1689 enum cpp_token token;
1690 int start_of_file = 0;
1691 U_CHAR *control_macro = 0;
1692 int old_written = CPP_WRITTEN (pfile);
1694 /* Detect a #ifndef at start of file (not counting comments). */
1695 if (ip->fname != 0 && keyword->type == T_IFNDEF)
1696 start_of_file = pfile->only_seen_white == 2;
1698 pfile->no_macro_expand++;
1699 token = get_directive_token (pfile);
1700 pfile->no_macro_expand--;
1702 ident = pfile->token_buffer + old_written;
1703 ident_length = CPP_WRITTEN (pfile) - old_written;
1704 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1706 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1708 skip = (keyword->type == T_IFDEF);
1709 if (! CPP_TRADITIONAL (pfile))
1710 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
1712 else if (token == CPP_NAME)
1714 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
1715 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
1716 if (start_of_file && !skip)
1718 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
1719 bcopy (ident, control_macro, ident_length + 1);
1722 else
1724 skip = (keyword->type == T_IFDEF);
1725 if (! CPP_TRADITIONAL (pfile))
1726 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
1729 if (!CPP_TRADITIONAL (pfile))
1730 { int c;
1731 cpp_skip_hspace (pfile);
1732 c = PEEKC ();
1733 if (c != EOF && c != '\n')
1734 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
1736 skip_rest_of_line (pfile);
1738 #if 0
1739 if (pcp_outfile) {
1740 /* Output a precondition for this macro. */
1741 if (hp && hp->value.defn->predefined)
1742 fprintf (pcp_outfile, "#define %s\n", hp->name);
1743 else {
1744 U_CHAR *cp = buf;
1745 fprintf (pcp_outfile, "#undef ");
1746 while (is_idchar[*cp]) /* Ick! */
1747 fputc (*cp++, pcp_outfile);
1748 putc ('\n', pcp_outfile);
1750 #endif
1752 conditional_skip (pfile, skip, T_IF, control_macro);
1753 return 0;
1756 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1757 If this is a #ifndef starting at the beginning of a file,
1758 CONTROL_MACRO is the macro name tested by the #ifndef.
1759 Otherwise, CONTROL_MACRO is 0. */
1761 static void
1762 conditional_skip (pfile, skip, type, control_macro)
1763 cpp_reader *pfile;
1764 int skip;
1765 enum node_type type;
1766 U_CHAR *control_macro;
1768 IF_STACK_FRAME *temp;
1770 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
1771 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
1772 #if 0
1773 temp->lineno = CPP_BUFFER (pfile)->lineno;
1774 #endif
1775 temp->next = pfile->if_stack;
1776 temp->control_macro = control_macro;
1777 pfile->if_stack = temp;
1779 pfile->if_stack->type = type;
1781 if (skip != 0) {
1782 skip_if_group (pfile);
1783 return;
1784 } else {
1785 ++pfile->if_stack->if_succeeded;
1786 output_line_command (pfile, 1, same_file);
1790 /* Subroutine of skip_if_group. Examine one preprocessing directive and
1791 return 0 if skipping should continue, 1 if it should halt. Also
1792 adjusts the if_stack as appropriate.
1793 The `#' has been read, but not the identifier. */
1795 static int
1796 consider_directive_while_skipping (pfile, stack)
1797 cpp_reader *pfile;
1798 IF_STACK_FRAME *stack;
1800 long ident_len, ident;
1801 struct directive *kt;
1802 IF_STACK_FRAME *temp;
1804 cpp_skip_hspace (pfile);
1806 ident = CPP_WRITTEN (pfile);
1807 parse_name (pfile, GETC());
1808 ident_len = CPP_WRITTEN (pfile) - ident;
1810 CPP_SET_WRITTEN (pfile, ident);
1812 for (kt = directive_table; kt->length >= 0; kt++)
1813 if (kt->length == ident_len
1814 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
1815 switch (kt->type)
1817 case T_IF:
1818 case T_IFDEF:
1819 case T_IFNDEF:
1820 temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
1821 temp->next = pfile->if_stack;
1822 pfile->if_stack = temp;
1823 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
1824 temp->type = kt->type;
1825 return 0;
1827 case T_ELSE:
1828 if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
1829 validate_else (pfile, "#else");
1830 /* fall through */
1831 case T_ELIF:
1832 if (pfile->if_stack->type == T_ELSE)
1833 cpp_error (pfile, "`%s' after `#else'", kt->name);
1835 if (pfile->if_stack == stack)
1836 return 1;
1837 else
1839 pfile->if_stack->type = kt->type;
1840 return 0;
1843 case T_ENDIF:
1844 if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
1845 validate_else (pfile, "#endif");
1847 if (pfile->if_stack == stack)
1848 return 1;
1850 temp = pfile->if_stack;
1851 pfile->if_stack = temp->next;
1852 free (temp);
1853 return 0;
1855 default:
1856 return 0;
1859 /* Don't let erroneous code go by. */
1860 if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
1861 cpp_pedwarn (pfile, "invalid preprocessor directive name");
1862 return 0;
1865 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
1866 * leaves input ptr at the sharp sign found.
1868 static void
1869 skip_if_group (pfile)
1870 cpp_reader *pfile;
1872 int c;
1873 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
1874 U_CHAR *beg_of_line;
1875 long old_written;
1877 if (CPP_OPTIONS (pfile)->output_conditionals)
1879 CPP_PUTS (pfile, "#failed\n", 8);
1880 pfile->lineno++;
1881 output_line_command (pfile, 1, same_file);
1884 old_written = CPP_WRITTEN (pfile);
1886 for (;;)
1888 beg_of_line = CPP_BUFFER (pfile)->cur;
1890 if (! CPP_TRADITIONAL (pfile))
1891 cpp_skip_hspace (pfile);
1892 c = GETC();
1893 if (c == '\n')
1895 if (CPP_OPTIONS (pfile)->output_conditionals)
1896 CPP_PUTC (pfile, c);
1897 CPP_BUMP_LINE (pfile);
1898 continue;
1900 else if (c == '#')
1902 if (consider_directive_while_skipping (pfile, save_if_stack))
1903 break;
1905 else if (c == EOF)
1906 return; /* Caller will issue error. */
1908 FORWARD(-1);
1909 if (CPP_OPTIONS (pfile)->output_conditionals)
1911 CPP_PUTS (pfile, beg_of_line, CPP_BUFFER (pfile)->cur - beg_of_line);
1912 copy_rest_of_line (pfile);
1914 else
1916 copy_rest_of_line (pfile);
1917 CPP_SET_WRITTEN (pfile, old_written); /* discard it */
1920 c = GETC();
1921 if (c == EOF)
1922 return; /* Caller will issue error. */
1923 else
1925 /* \n */
1926 if (CPP_OPTIONS (pfile)->output_conditionals)
1928 CPP_PUTC (pfile, c);
1929 pfile->lineno++;
1931 CPP_BUMP_LINE (pfile);
1935 /* Back up to the beginning of this line. Caller will process the
1936 directive. */
1937 CPP_BUFFER (pfile)->cur = beg_of_line;
1938 pfile->only_seen_white = 1;
1939 if (CPP_OPTIONS (pfile)->output_conditionals)
1941 CPP_PUTS (pfile, "#endfailed\n", 11);
1942 pfile->lineno++;
1947 * handle a #else directive. Do this by just continuing processing
1948 * without changing if_stack ; this is so that the error message
1949 * for missing #endif's etc. will point to the original #if. It
1950 * is possible that something different would be better.
1953 static int
1954 do_else (pfile, keyword)
1955 cpp_reader *pfile;
1956 struct directive *keyword ATTRIBUTE_UNUSED;
1958 cpp_buffer *ip = CPP_BUFFER (pfile);
1960 if (CPP_PEDANTIC (pfile))
1961 validate_else (pfile, "#else");
1962 skip_rest_of_line (pfile);
1964 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
1965 cpp_error (pfile, "`#else' not within a conditional");
1966 return 0;
1967 } else {
1968 /* #ifndef can't have its special treatment for containing the whole file
1969 if it has a #else clause. */
1970 pfile->if_stack->control_macro = 0;
1972 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
1973 cpp_error (pfile, "`#else' after `#else'");
1974 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
1975 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
1976 fprintf (stderr, ", file %s", pfile->if_stack->fname);
1977 fprintf (stderr, ")\n");
1979 pfile->if_stack->type = T_ELSE;
1982 if (pfile->if_stack->if_succeeded)
1983 skip_if_group (pfile);
1984 else {
1985 ++pfile->if_stack->if_succeeded; /* continue processing input */
1986 output_line_command (pfile, 1, same_file);
1988 return 0;
1992 * unstack after #endif command
1995 static int
1996 do_endif (pfile, keyword)
1997 cpp_reader *pfile;
1998 struct directive *keyword ATTRIBUTE_UNUSED;
2000 if (CPP_PEDANTIC (pfile))
2001 validate_else (pfile, "#endif");
2002 skip_rest_of_line (pfile);
2004 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2005 cpp_error (pfile, "unbalanced `#endif'");
2006 else
2008 IF_STACK_FRAME *temp = pfile->if_stack;
2009 pfile->if_stack = temp->next;
2010 if (temp->control_macro != 0)
2012 /* This #endif matched a #ifndef at the start of the file.
2013 See if it is at the end of the file. */
2014 int c;
2016 parse_set_mark (pfile);
2018 for (;;)
2020 cpp_skip_hspace (pfile);
2021 c = GETC ();
2022 if (c != '\n')
2023 break;
2025 parse_goto_mark (pfile);
2027 if (c == EOF)
2029 /* This #endif ends a #ifndef
2030 that contains all of the file (aside from whitespace).
2031 Arrange not to include the file again
2032 if the macro that was tested is defined. */
2033 struct cpp_buffer *ip;
2034 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2035 if (ip->fname != NULL)
2036 break;
2037 ip->ihash->control_macro = (char *) temp->control_macro;
2040 free (temp);
2041 output_line_command (pfile, 1, same_file);
2043 return 0;
2046 /* When an #else or #endif is found while skipping failed conditional,
2047 if -pedantic was specified, this is called to warn about text after
2048 the command name. P points to the first char after the command name. */
2050 static void
2051 validate_else (pfile, directive)
2052 cpp_reader *pfile;
2053 char *directive;
2055 int c;
2056 cpp_skip_hspace (pfile);
2057 c = PEEKC ();
2058 if (c != EOF && c != '\n')
2059 cpp_pedwarn (pfile,
2060 "text following `%s' violates ANSI standard", directive);
2063 /* Get the next token, and add it to the text in pfile->token_buffer.
2064 Return the kind of token we got. */
2066 enum cpp_token
2067 cpp_get_token (pfile)
2068 cpp_reader *pfile;
2070 register int c, c2, c3;
2071 enum cpp_token token;
2072 struct cpp_options *opts = CPP_OPTIONS (pfile);
2074 get_next:
2075 c = GETC();
2076 if (c == EOF)
2078 handle_eof:
2079 if (CPP_BUFFER (pfile)->seen_eof)
2081 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile))
2082 return CPP_EOF;
2084 cpp_pop_buffer (pfile);
2085 goto get_next;
2087 else
2089 cpp_buffer *next_buf
2090 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2091 CPP_BUFFER (pfile)->seen_eof = 1;
2092 if (CPP_BUFFER (pfile)->nominal_fname
2093 && next_buf != CPP_NULL_BUFFER (pfile))
2095 /* We're about to return from an #include file.
2096 Emit #line information now (as part of the CPP_POP) result.
2097 But the #line refers to the file we will pop to. */
2098 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2099 CPP_BUFFER (pfile) = next_buf;
2100 pfile->input_stack_listing_current = 0;
2101 output_line_command (pfile, 0, leave_file);
2102 CPP_BUFFER (pfile) = cur_buffer;
2104 return CPP_POP;
2107 else
2109 switch (c)
2111 case '/':
2112 if (PEEKC () == '=')
2113 goto op2;
2115 comment:
2116 if (opts->put_out_comments)
2117 c = copy_comment (pfile, c);
2118 else
2119 c = skip_comment (pfile, c);
2120 if (c == EOF)
2121 goto handle_eof;
2122 else if (c != ' ')
2123 goto randomchar;
2125 /* Comments are equivalent to spaces.
2126 For -traditional, a comment is equivalent to nothing. */
2127 if (opts->traditional || opts->put_out_comments)
2128 return CPP_COMMENT;
2129 else
2131 CPP_PUTC (pfile, c);
2132 return CPP_HSPACE;
2134 #if 0
2135 if (opts->for_lint) {
2136 U_CHAR *argbp;
2137 int cmdlen, arglen;
2138 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
2140 if (lintcmd != NULL) {
2141 /* I believe it is always safe to emit this newline: */
2142 obp[-1] = '\n';
2143 bcopy ("#pragma lint ", (char *) obp, 13);
2144 obp += 13;
2145 bcopy (lintcmd, (char *) obp, cmdlen);
2146 obp += cmdlen;
2148 if (arglen != 0) {
2149 *(obp++) = ' ';
2150 bcopy (argbp, (char *) obp, arglen);
2151 obp += arglen;
2154 /* OK, now bring us back to the state we were in before we entered
2155 this branch. We need #line because the newline for the pragma
2156 could mess things up. */
2157 output_line_command (pfile, 0, same_file);
2158 *(obp++) = ' '; /* just in case, if comments are copied thru */
2159 *(obp++) = '/';
2162 #endif
2164 case '#':
2165 #if 0
2166 /* If this is expanding a macro definition, don't recognize
2167 preprocessor directives. */
2168 if (ip->macro != 0)
2169 goto randomchar;
2170 /* If this is expand_into_temp_buffer, recognize them
2171 only after an actual newline at this level,
2172 not at the beginning of the input level. */
2173 if (ip->fname == 0 && beg_of_line == ip->buf)
2174 goto randomchar;
2175 if (ident_length)
2176 goto specialchar;
2177 #endif
2179 if (!pfile->only_seen_white)
2180 goto randomchar;
2181 if (handle_directive (pfile))
2182 return CPP_DIRECTIVE;
2183 pfile->only_seen_white = 0;
2184 return CPP_OTHER;
2186 case '\"':
2187 case '\'':
2188 string:
2189 parse_string (pfile, c);
2190 pfile->only_seen_white = 0;
2191 return c == '\'' ? CPP_CHAR : CPP_STRING;
2193 case '$':
2194 if (!opts->dollars_in_ident)
2195 goto randomchar;
2196 goto letter;
2198 case ':':
2199 if (opts->cplusplus && PEEKC () == ':')
2200 goto op2;
2201 goto randomchar;
2203 case '&':
2204 case '+':
2205 case '|':
2206 c2 = PEEKC ();
2207 if (c2 == c || c2 == '=')
2208 goto op2;
2209 goto randomchar;
2211 case '*':
2212 case '!':
2213 case '%':
2214 case '=':
2215 case '^':
2216 if (PEEKC () == '=')
2217 goto op2;
2218 goto randomchar;
2220 case '-':
2221 c2 = PEEKC ();
2222 if (c2 == '-' && opts->chill)
2223 goto comment; /* Chill style comment */
2224 if (c2 == '-' || c2 == '=' || c2 == '>')
2225 goto op2;
2226 goto randomchar;
2228 case '<':
2229 if (pfile->parsing_include_directive)
2231 for (;;)
2233 CPP_PUTC (pfile, c);
2234 if (c == '>')
2235 break;
2236 c = GETC ();
2237 if (c == '\n' || c == EOF)
2239 cpp_error (pfile,
2240 "missing '>' in `#include <FILENAME>'");
2241 break;
2243 else if (c == '\r')
2245 if (!CPP_BUFFER (pfile)->has_escapes)
2247 /* Backslash newline is replaced by nothing. */
2248 CPP_ADJUST_WRITTEN (pfile, -1);
2249 CPP_BUMP_LINE (pfile);
2251 else
2253 /* We might conceivably get \r- or \r<space> in
2254 here. Just delete 'em. */
2255 int d = GETC();
2256 if (d != '-' && d != ' ')
2257 cpp_fatal (pfile,
2258 "internal error: unrecognized escape \\r%c",
2260 CPP_ADJUST_WRITTEN (pfile, -1);
2264 return CPP_STRING;
2266 /* else fall through */
2267 case '>':
2268 c2 = PEEKC ();
2269 if (c2 == '=')
2270 goto op2;
2271 if (c2 != c)
2272 goto randomchar;
2273 FORWARD(1);
2274 CPP_RESERVE (pfile, 4);
2275 CPP_PUTC (pfile, c);
2276 CPP_PUTC (pfile, c2);
2277 c3 = PEEKC ();
2278 if (c3 == '=')
2279 CPP_PUTC_Q (pfile, GETC ());
2280 CPP_NUL_TERMINATE_Q (pfile);
2281 pfile->only_seen_white = 0;
2282 return CPP_OTHER;
2284 case '.':
2285 c2 = PEEKC ();
2286 if (ISDIGIT(c2))
2288 CPP_RESERVE(pfile, 2);
2289 CPP_PUTC_Q (pfile, '.');
2290 c = GETC ();
2291 goto number;
2293 if (c2 == '.' && PEEKN(1) == '.')
2295 CPP_RESERVE(pfile, 4);
2296 CPP_PUTC_Q (pfile, '.');
2297 CPP_PUTC_Q (pfile, '.');
2298 CPP_PUTC_Q (pfile, '.');
2299 FORWARD (2);
2300 CPP_NUL_TERMINATE_Q (pfile);
2301 pfile->only_seen_white = 0;
2302 return CPP_3DOTS;
2304 goto randomchar;
2306 op2:
2307 token = CPP_OTHER;
2308 pfile->only_seen_white = 0;
2309 CPP_RESERVE(pfile, 3);
2310 CPP_PUTC_Q (pfile, c);
2311 CPP_PUTC_Q (pfile, GETC ());
2312 CPP_NUL_TERMINATE_Q (pfile);
2313 return token;
2315 case 'L':
2316 c2 = PEEKC ();
2317 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2319 CPP_PUTC (pfile, c);
2320 c = GETC ();
2321 goto string;
2323 goto letter;
2325 case '0': case '1': case '2': case '3': case '4':
2326 case '5': case '6': case '7': case '8': case '9':
2327 number:
2328 c2 = '.';
2329 for (;;)
2331 CPP_RESERVE (pfile, 2);
2332 CPP_PUTC_Q (pfile, c);
2333 c = PEEKC ();
2334 if (c == EOF)
2335 break;
2336 if (!is_idchar[c] && c != '.'
2337 && ((c2 != 'e' && c2 != 'E'
2338 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
2339 || (c != '+' && c != '-')))
2340 break;
2341 FORWARD(1);
2342 c2= c;
2344 CPP_NUL_TERMINATE_Q (pfile);
2345 pfile->only_seen_white = 0;
2346 return CPP_NUMBER;
2347 case 'b': case 'c': case 'd': case 'h': case 'o':
2348 case 'B': case 'C': case 'D': case 'H': case 'O':
2349 if (opts->chill && PEEKC () == '\'')
2351 pfile->only_seen_white = 0;
2352 CPP_RESERVE (pfile, 2);
2353 CPP_PUTC_Q (pfile, c);
2354 CPP_PUTC_Q (pfile, '\'');
2355 FORWARD(1);
2356 for (;;)
2358 c = GETC();
2359 if (c == EOF)
2360 goto chill_number_eof;
2361 if (!is_idchar[c])
2362 break;
2363 CPP_PUTC (pfile, c);
2365 if (c == '\'')
2367 CPP_RESERVE (pfile, 2);
2368 CPP_PUTC_Q (pfile, c);
2369 CPP_NUL_TERMINATE_Q (pfile);
2370 return CPP_STRING;
2372 else
2374 FORWARD(-1);
2375 chill_number_eof:
2376 CPP_NUL_TERMINATE (pfile);
2377 return CPP_NUMBER;
2380 else
2381 goto letter;
2382 case '_':
2383 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2384 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2385 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2386 case 'x': case 'y': case 'z':
2387 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2388 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2389 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2390 case 'Y': case 'Z':
2391 letter:
2393 HASHNODE *hp;
2394 unsigned char *ident;
2395 int before_name_written = CPP_WRITTEN (pfile);
2396 int ident_len;
2397 parse_name (pfile, c);
2398 pfile->only_seen_white = 0;
2399 if (pfile->no_macro_expand)
2400 return CPP_NAME;
2401 ident = pfile->token_buffer + before_name_written;
2402 ident_len = CPP_PWRITTEN (pfile) - ident;
2403 hp = cpp_lookup (pfile, ident, ident_len, -1);
2404 if (!hp)
2405 return CPP_NAME;
2406 if (hp->type == T_DISABLED)
2408 if (pfile->output_escapes)
2409 { /* Return "\r-IDENT", followed by '\0'. */
2410 int i;
2411 CPP_RESERVE (pfile, 3);
2412 ident = pfile->token_buffer + before_name_written;
2413 CPP_ADJUST_WRITTEN (pfile, 2);
2414 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2415 ident[0] = '\r';
2416 ident[1] = '-';
2418 return CPP_NAME;
2421 /* If macro wants an arglist, verify that a '(' follows.
2422 first skip all whitespace, copying it to the output
2423 after the macro name. Then, if there is no '(',
2424 decide this is not a macro call and leave things that way. */
2425 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2427 int is_macro_call, macbuf_whitespace = 0;
2429 parse_set_mark (pfile);
2430 for (;;)
2432 cpp_skip_hspace (pfile);
2433 c = PEEKC ();
2434 is_macro_call = c == '(';
2435 if (c != EOF)
2437 if (c != '\n')
2438 break;
2439 FORWARD (1);
2441 else
2443 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2445 if (CPP_BUFFER (pfile)->mark !=
2446 (CPP_BUFFER (pfile)->cur
2447 - CPP_BUFFER (pfile)->buf))
2448 macbuf_whitespace = 1;
2450 /* The mark goes away automatically when
2451 the buffer is popped. */
2452 cpp_pop_buffer (pfile);
2453 parse_set_mark (pfile);
2455 else
2456 break;
2459 if (!is_macro_call)
2461 parse_goto_mark (pfile);
2462 if (macbuf_whitespace)
2463 CPP_PUTC (pfile, ' ');
2465 else
2466 parse_clear_mark (pfile);
2467 if (!is_macro_call)
2468 return CPP_NAME;
2470 /* This is now known to be a macro call.
2471 Expand the macro, reading arguments as needed,
2472 and push the expansion on the input stack. */
2473 macroexpand (pfile, hp);
2474 CPP_SET_WRITTEN (pfile, before_name_written);
2476 goto get_next;
2478 case ' ': case '\t': case '\v':
2479 for (;;)
2481 CPP_PUTC (pfile, c);
2482 c = PEEKC ();
2483 if (c == EOF || !is_hor_space[c])
2484 break;
2485 FORWARD(1);
2487 return CPP_HSPACE;
2489 case '\r':
2490 if (CPP_BUFFER (pfile)->has_escapes)
2492 c = GETC ();
2493 if (c == '-')
2495 if (pfile->output_escapes)
2496 CPP_PUTS (pfile, "\r-", 2);
2497 parse_name (pfile, GETC ());
2498 return CPP_NAME;
2500 else if (c == ' ')
2502 CPP_RESERVE (pfile, 2);
2503 if (pfile->output_escapes)
2504 CPP_PUTC_Q (pfile, '\r');
2505 CPP_PUTC_Q (pfile, c);
2506 return CPP_HSPACE;
2508 else
2510 cpp_fatal (pfile,
2511 "internal error: unrecognized escape \\r%c", c);
2512 goto get_next;
2515 else
2517 /* Backslash newline is ignored. */
2518 CPP_BUMP_LINE (pfile);
2519 goto get_next;
2522 case '\n':
2523 CPP_PUTC (pfile, c);
2524 if (pfile->only_seen_white == 0)
2525 pfile->only_seen_white = 1;
2526 CPP_BUMP_LINE (pfile);
2527 pfile->lineno++;
2528 if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2529 output_line_command (pfile, 1, same_file);
2530 return CPP_VSPACE;
2532 case '(': token = CPP_LPAREN; goto char1;
2533 case ')': token = CPP_RPAREN; goto char1;
2534 case '{': token = CPP_LBRACE; goto char1;
2535 case '}': token = CPP_RBRACE; goto char1;
2536 case ',': token = CPP_COMMA; goto char1;
2537 case ';': token = CPP_SEMICOLON; goto char1;
2539 randomchar:
2540 default:
2541 token = CPP_OTHER;
2542 char1:
2543 pfile->only_seen_white = 0;
2544 CPP_PUTC (pfile, c);
2545 return token;
2550 /* Like cpp_get_token, but skip spaces and comments. */
2552 enum cpp_token
2553 cpp_get_non_space_token (pfile)
2554 cpp_reader *pfile;
2556 int old_written = CPP_WRITTEN (pfile);
2557 for (;;)
2559 enum cpp_token token = cpp_get_token (pfile);
2560 if (token != CPP_COMMENT && token != CPP_POP
2561 && token != CPP_HSPACE && token != CPP_VSPACE)
2562 return token;
2563 CPP_SET_WRITTEN (pfile, old_written);
2567 /* Parse an identifier starting with C. */
2569 static void
2570 parse_name (pfile, c)
2571 cpp_reader *pfile;
2572 int c;
2574 for (;;)
2576 if (! is_idchar[c])
2578 FORWARD (-1);
2579 break;
2582 if (c == '$' && CPP_PEDANTIC (pfile))
2583 cpp_pedwarn (pfile, "`$' in identifier");
2585 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
2586 CPP_PUTC_Q (pfile, c);
2587 c = GETC();
2588 if (c == EOF)
2589 break;
2591 CPP_NUL_TERMINATE_Q (pfile);
2592 return;
2595 /* Parse a string starting with C. A single quoted string is treated
2596 like a double -- some programs (e.g., troff) are perverse this way.
2597 (However, a single quoted string is not allowed to extend over
2598 multiple lines. */
2599 static void
2600 parse_string (pfile, c)
2601 cpp_reader *pfile;
2602 int c;
2604 long start_line, start_column;
2606 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2608 CPP_PUTC (pfile, c);
2609 while (1)
2611 int cc = GETC();
2612 if (cc == EOF)
2614 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2616 /* try harder: this string crosses a macro expansion
2617 boundary. This can happen naturally if -traditional.
2618 Otherwise, only -D can make a macro with an unmatched
2619 quote. */
2620 cpp_pop_buffer (pfile);
2621 continue;
2623 if (!CPP_TRADITIONAL (pfile))
2625 cpp_error_with_line (pfile, start_line, start_column,
2626 "unterminated string or character constant");
2627 if (pfile->multiline_string_line != start_line
2628 && pfile->multiline_string_line != 0)
2629 cpp_error_with_line (pfile,
2630 pfile->multiline_string_line, -1,
2631 "possible real start of unterminated constant");
2632 pfile->multiline_string_line = 0;
2634 break;
2636 CPP_PUTC (pfile, cc);
2637 switch (cc)
2639 case '\n':
2640 CPP_BUMP_LINE (pfile);
2641 pfile->lineno++;
2642 /* Traditionally, end of line ends a string constant with
2643 no error. */
2644 if (CPP_TRADITIONAL (pfile))
2645 return;
2646 /* Character constants may not extend over multiple lines. */
2647 if (c == '\'')
2649 cpp_error_with_line (pfile, start_line, start_column,
2650 "unterminated character constant");
2651 return;
2653 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2655 cpp_pedwarn_with_line (pfile, start_line, start_column,
2656 "string constant runs past end of line");
2658 if (pfile->multiline_string_line == 0)
2659 pfile->multiline_string_line = start_line;
2660 break;
2662 case '\r':
2663 CPP_ADJUST_WRITTEN (pfile, -1);
2664 if (CPP_BUFFER (pfile)->has_escapes)
2666 cpp_fatal (pfile,
2667 "internal error: \\r escape inside string constant");
2668 FORWARD(1);
2670 else
2671 /* Backslash newline is replaced by nothing at all. */
2672 CPP_BUMP_LINE (pfile);
2673 break;
2675 case '\\':
2676 cc = GETC();
2677 if (cc != EOF)
2678 CPP_PUTC (pfile, cc);
2679 break;
2681 case '\"':
2682 case '\'':
2683 if (cc == c)
2684 return;
2685 break;
2690 /* Read an assertion into the token buffer, converting to
2691 canonical form: `#predicate(a n swe r)' The next non-whitespace
2692 character to read should be the first letter of the predicate.
2693 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2694 with answer (see callers for why). In case of 0, an error has been
2695 printed. */
2696 static int
2697 parse_assertion (pfile)
2698 cpp_reader *pfile;
2700 int c, dropwhite;
2701 cpp_skip_hspace (pfile);
2702 c = PEEKC();
2703 if (! is_idstart[c])
2705 cpp_error (pfile, "assertion predicate is not an identifier");
2706 return 0;
2708 CPP_PUTC(pfile, '#');
2709 FORWARD(1);
2710 parse_name(pfile, c);
2712 c = PEEKC();
2713 if (c != '(')
2715 if (is_hor_space[c] || c == '\r')
2716 cpp_skip_hspace (pfile);
2717 c = PEEKC();
2719 if (c != '(')
2720 return 1;
2722 CPP_PUTC(pfile, '(');
2723 FORWARD(1);
2724 dropwhite = 1;
2725 while ((c = GETC()) != ')')
2727 if (is_hor_space[c])
2729 if (! dropwhite)
2731 CPP_PUTC(pfile, ' ');
2732 dropwhite = 1;
2735 else if (c == '\n' || c == EOF)
2737 if (c == '\n') FORWARD(-1);
2738 cpp_error (pfile, "un-terminated assertion answer");
2739 return 0;
2741 else if (c == '\r')
2742 /* \r cannot be a macro escape here. */
2743 CPP_BUMP_LINE (pfile);
2744 else
2746 CPP_PUTC (pfile, c);
2747 dropwhite = 0;
2751 if (pfile->limit[-1] == ' ')
2752 pfile->limit[-1] = ')';
2753 else if (pfile->limit[-1] == '(')
2755 cpp_error (pfile, "empty token sequence in assertion");
2756 return 0;
2758 else
2759 CPP_PUTC (pfile, ')');
2761 CPP_NUL_TERMINATE (pfile);
2762 return 2;
2765 static int
2766 do_assert (pfile, keyword)
2767 cpp_reader *pfile;
2768 struct directive *keyword ATTRIBUTE_UNUSED;
2770 char *sym;
2771 int ret, c;
2772 HASHNODE *base, *this;
2773 int baselen, thislen;
2775 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
2776 && !CPP_BUFFER (pfile)->system_header_p)
2777 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
2779 cpp_skip_hspace (pfile);
2780 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
2781 ret = parse_assertion (pfile);
2782 if (ret == 0)
2783 goto error;
2784 else if (ret == 1)
2786 cpp_error (pfile, "missing token-sequence in `#assert'");
2787 goto error;
2790 cpp_skip_hspace (pfile);
2791 c = PEEKC();
2792 if (c != EOF && c != '\n')
2794 cpp_error (pfile, "junk at end of `#assert'");
2795 goto error;
2798 thislen = strlen (sym);
2799 baselen = index (sym, '(') - sym;
2800 this = cpp_lookup (pfile, sym, thislen, -1);
2801 if (this)
2803 cpp_warning (pfile, "`%s' re-asserted", sym);
2804 goto error;
2807 base = cpp_lookup (pfile, sym, baselen, -1);
2808 if (! base)
2809 base = cpp_install (pfile, sym, baselen, T_ASSERT, 0, -1);
2810 else if (base->type != T_ASSERT)
2812 /* Token clash - but with what?! */
2813 cpp_fatal (pfile,
2814 "cpp internal error: base->type != T_ASSERT in do_assert");
2815 goto error;
2818 this = cpp_install (pfile, sym, thislen, T_ASSERT,
2819 (char *)base->value.aschain, -1);
2820 base->value.aschain = this;
2822 pfile->limit = (unsigned char *) sym; /* Pop */
2823 return 0;
2825 error:
2826 pfile->limit = (unsigned char *) sym; /* Pop */
2827 skip_rest_of_line (pfile);
2828 return 1;
2831 static int
2832 do_unassert (pfile, keyword)
2833 cpp_reader *pfile;
2834 struct directive *keyword ATTRIBUTE_UNUSED;
2836 int c, ret;
2837 char *sym;
2838 long baselen, thislen;
2839 HASHNODE *base, *this, *next;
2841 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
2842 && !CPP_BUFFER (pfile)->system_header_p)
2843 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
2845 cpp_skip_hspace (pfile);
2847 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
2848 ret = parse_assertion (pfile);
2849 if (ret == 0)
2850 goto error;
2852 cpp_skip_hspace (pfile);
2853 c = PEEKC ();
2854 if (c != EOF && c != '\n')
2855 cpp_error (pfile, "junk at end of `#unassert'");
2857 thislen = strlen (sym);
2858 if (ret == 1)
2860 base = cpp_lookup (pfile, sym, thislen, -1);
2861 if (! base)
2862 goto error; /* It isn't an error to #undef what isn't #defined,
2863 so it isn't an error to #unassert what isn't
2864 #asserted either. */
2866 for (this = base->value.aschain; this; this = next)
2868 next = this->value.aschain;
2869 delete_macro (this);
2871 delete_macro (base);
2873 else
2875 baselen = index (sym, '(') - sym;
2876 base = cpp_lookup (pfile, sym, baselen, -1);
2877 if (! base) goto error;
2878 this = cpp_lookup (pfile, sym, thislen, -1);
2879 if (! this) goto error;
2881 next = base;
2882 while (next->value.aschain != this)
2883 next = next->value.aschain;
2885 next->value.aschain = this->value.aschain;
2886 delete_macro (this);
2888 if (base->value.aschain == NULL)
2889 delete_macro (base); /* Last answer for this predicate deleted. */
2892 pfile->limit = (unsigned char *) sym; /* Pop */
2893 return 0;
2894 error:
2895 pfile->limit = (unsigned char *) sym; /* Pop */
2896 skip_rest_of_line (pfile);
2897 return 1;
2900 /* Process STR as if it appeared as the body of an #unassert. */
2901 void
2902 cpp_unassert (pfile, str)
2903 cpp_reader *pfile;
2904 unsigned char *str;
2906 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
2908 do_assert (pfile, NULL);
2909 cpp_pop_buffer (pfile);
2914 cpp_read_check_assertion (pfile)
2915 cpp_reader *pfile;
2917 U_CHAR *name = CPP_PWRITTEN (pfile);
2918 int result;
2919 HASHNODE *hp;
2921 FORWARD (1); /* Skip '#' */
2922 cpp_skip_hspace (pfile);
2923 if (! parse_assertion (pfile))
2924 result = 0;
2925 else
2927 hp = cpp_lookup (pfile, name, CPP_PWRITTEN (pfile) - name, -1);
2928 result = (hp != 0);
2931 pfile->limit = name;
2932 return result;
2935 /* Remember the current position of PFILE. */
2937 void
2938 parse_set_mark (pfile)
2939 cpp_reader *pfile;
2941 cpp_buffer *ip = CPP_BUFFER (pfile);
2942 if (ip->mark != -1)
2943 cpp_fatal (pfile,
2944 "cpp internal error: ip->mark != -1 in parse_set_mark");
2946 ip->mark = ip->cur - ip->buf;
2949 /* Clear the current mark - we no longer need it. */
2951 void
2952 parse_clear_mark (pfile)
2953 cpp_reader *pfile;
2955 cpp_buffer *ip = CPP_BUFFER (pfile);
2956 if (ip->mark == -1)
2957 cpp_fatal (pfile,
2958 "cpp internal error: ip->mark == -1 in parse_clear_mark");
2960 ip->mark = -1;
2963 /* Backup the current position of PFILE to that saved in its mark,
2964 and clear the mark. */
2966 void
2967 parse_goto_mark (pfile)
2968 cpp_reader *pfile;
2970 cpp_buffer *ip = CPP_BUFFER (pfile);
2971 if (ip->mark == -1)
2972 cpp_fatal (pfile,
2973 "cpp internal error: ip->mark == -1 in parse_goto_mark");
2975 ip->cur = ip->buf + ip->mark;
2976 ip->mark = -1;
2979 void
2980 cpp_print_file_and_line (pfile)
2981 cpp_reader *pfile;
2983 cpp_buffer *ip = cpp_file_buffer (pfile);
2985 if (ip != NULL)
2987 long line, col;
2988 cpp_buf_line_and_col (ip, &line, &col);
2989 cpp_file_line_for_message (pfile, ip->nominal_fname,
2990 line, pfile->show_column ? col : -1);
2994 static void
2995 v_cpp_error (pfile, msgid, ap)
2996 cpp_reader *pfile;
2997 const char *msgid;
2998 va_list ap;
3000 cpp_print_containing_files (pfile);
3001 cpp_print_file_and_line (pfile);
3002 v_cpp_message (pfile, 1, msgid, ap);
3005 void
3006 cpp_error VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3008 #ifndef ANSI_PROTOTYPES
3009 cpp_reader *pfile;
3010 const char *msgid;
3011 #endif
3012 va_list ap;
3014 VA_START(ap, msgid);
3016 #ifndef ANSI_PROTOTYPES
3017 pfile = va_arg (ap, cpp_reader *);
3018 msgid = va_arg (ap, const char *);
3019 #endif
3021 v_cpp_error (pfile, msgid, ap);
3022 va_end(ap);
3025 /* Print error message but don't count it. */
3027 static void
3028 v_cpp_warning (pfile, msgid, ap)
3029 cpp_reader *pfile;
3030 const char *msgid;
3031 va_list ap;
3033 if (CPP_OPTIONS (pfile)->inhibit_warnings)
3034 return;
3036 if (CPP_OPTIONS (pfile)->warnings_are_errors)
3037 pfile->errors++;
3039 cpp_print_containing_files (pfile);
3040 cpp_print_file_and_line (pfile);
3041 v_cpp_message (pfile, 0, msgid, ap);
3044 void
3045 cpp_warning VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3047 #ifndef ANSI_PROTOTYPES
3048 cpp_reader *pfile;
3049 const char *msgid;
3050 #endif
3051 va_list ap;
3053 VA_START (ap, msgid);
3055 #ifndef ANSI_PROTOTYPES
3056 pfile = va_arg (ap, cpp_reader *);
3057 msgid = va_arg (ap, const char *);
3058 #endif
3060 v_cpp_warning (pfile, msgid, ap);
3061 va_end(ap);
3064 /* Print an error message and maybe count it. */
3066 void
3067 cpp_pedwarn VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3069 #ifndef ANSI_PROTOTYPES
3070 cpp_reader *pfile;
3071 const char *msgid;
3072 #endif
3073 va_list ap;
3075 VA_START (ap, msgid);
3077 #ifndef ANSI_PROTOTYPES
3078 pfile = va_arg (ap, cpp_reader *);
3079 msgid = va_arg (ap, const char *);
3080 #endif
3082 if (CPP_OPTIONS (pfile)->pedantic_errors)
3083 v_cpp_error (pfile, msgid, ap);
3084 else
3085 v_cpp_warning (pfile, msgid, ap);
3086 va_end(ap);
3089 static void
3090 v_cpp_error_with_line (pfile, line, column, msgid, ap)
3091 cpp_reader * pfile;
3092 int line;
3093 int column;
3094 const char * msgid;
3095 va_list ap;
3097 cpp_buffer *ip = cpp_file_buffer (pfile);
3099 cpp_print_containing_files (pfile);
3101 if (ip != NULL)
3102 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
3104 v_cpp_message (pfile, 1, msgid, ap);
3107 void
3108 cpp_error_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3109 const char *msgid, ...))
3111 #ifndef ANSI_PROTOTYPES
3112 cpp_reader *pfile;
3113 int line;
3114 int column;
3115 const char *msgid;
3116 #endif
3117 va_list ap;
3119 VA_START (ap, msgid);
3121 #ifndef ANSI_PROTOTYPES
3122 pfile = va_arg (ap, cpp_reader *);
3123 line = va_arg (ap, int);
3124 column = va_arg (ap, int);
3125 msgid = va_arg (ap, const char *);
3126 #endif
3128 v_cpp_error_with_line(pfile, line, column, msgid, ap);
3129 va_end(ap);
3132 static void
3133 v_cpp_warning_with_line (pfile, line, column, msgid, ap)
3134 cpp_reader * pfile;
3135 int line;
3136 int column;
3137 const char *msgid;
3138 va_list ap;
3140 cpp_buffer *ip;
3142 if (CPP_OPTIONS (pfile)->inhibit_warnings)
3143 return;
3145 if (CPP_OPTIONS (pfile)->warnings_are_errors)
3146 pfile->errors++;
3148 cpp_print_containing_files (pfile);
3150 ip = cpp_file_buffer (pfile);
3152 if (ip != NULL)
3153 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
3155 v_cpp_message (pfile, 0, msgid, ap);
3158 void
3159 cpp_warning_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3160 const char *msgid, ...))
3162 #ifndef ANSI_PROTOTYPES
3163 cpp_reader *pfile;
3164 int line;
3165 int column;
3166 const char *msgid;
3167 #endif
3168 va_list ap;
3170 VA_START (ap, msgid);
3172 #ifndef ANSI_PROTOTYPES
3173 pfile = va_arg (ap, cpp_reader *);
3174 line = va_arg (ap, int);
3175 column = va_arg (ap, int);
3176 msgid = va_arg (ap, const char *);
3177 #endif
3179 v_cpp_warning_with_line (pfile, line, column, msgid, ap);
3180 va_end(ap);
3183 void
3184 cpp_pedwarn_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3185 const char *msgid, ...))
3187 #ifndef ANSI_PROTOTYPES
3188 cpp_reader *pfile;
3189 int line;
3190 int column;
3191 const char *msgid;
3192 #endif
3193 va_list ap;
3195 VA_START (ap, msgid);
3197 #ifndef ANSI_PROTOTYPES
3198 pfile = va_arg (ap, cpp_reader *);
3199 line = va_arg (ap, int);
3200 column = va_arg (ap, int);
3201 msgid = va_arg (ap, const char *);
3202 #endif
3204 if (CPP_OPTIONS (pfile)->pedantic_errors)
3205 v_cpp_error_with_line (pfile, column, line, msgid, ap);
3206 else
3207 v_cpp_warning_with_line (pfile, line, column, msgid, ap);
3208 va_end(ap);
3211 /* Report a warning (or an error if pedantic_errors)
3212 giving specified file name and line number, not current. */
3214 void
3215 cpp_pedwarn_with_file_and_line VPROTO ((cpp_reader *pfile, char *file, int line,
3216 const char *msgid, ...))
3218 #ifndef ANSI_PROTOTYPES
3219 cpp_reader *pfile;
3220 char *file;
3221 int line;
3222 const char *msgid;
3223 #endif
3224 va_list ap;
3226 VA_START (ap, msgid);
3228 #ifndef ANSI_PROTOTYPES
3229 pfile = va_arg (ap, cpp_reader *);
3230 file = va_arg (ap, char *);
3231 line = va_arg (ap, int);
3232 msgid = va_arg (ap, const char *);
3233 #endif
3235 if (!CPP_OPTIONS (pfile)->pedantic_errors
3236 && CPP_OPTIONS (pfile)->inhibit_warnings)
3237 return;
3238 if (file != NULL)
3239 cpp_file_line_for_message (pfile, file, line, -1);
3240 v_cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, msgid, ap);
3241 va_end(ap);
3244 /* my_strerror - return the descriptive text associated with an
3245 `errno' code. */
3247 static char *
3248 my_strerror (errnum)
3249 int errnum;
3251 char *result;
3253 #ifndef VMS
3254 #ifndef HAVE_STRERROR
3255 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
3256 #else
3257 result = strerror (errnum);
3258 #endif
3259 #else /* VMS */
3260 /* VAXCRTL's strerror() takes an optional second argument, which only
3261 matters when the first argument is EVMSERR. However, it's simplest
3262 just to pass it unconditionally. `vaxc$errno' is declared in
3263 <errno.h>, and maintained by the library in parallel with `errno'.
3264 We assume that caller's `errnum' either matches the last setting of
3265 `errno' by the library or else does not have the value `EVMSERR'. */
3267 result = strerror (errnum, vaxc$errno);
3268 #endif
3270 if (!result)
3271 result = "errno = ?";
3273 return result;
3276 /* Error including a message from `errno'. */
3278 void
3279 cpp_error_from_errno (pfile, name)
3280 cpp_reader *pfile;
3281 const char *name;
3283 cpp_message_from_errno (pfile, 1, name);
3286 void
3287 cpp_message_from_errno (pfile, is_error, name)
3288 cpp_reader *pfile;
3289 int is_error;
3290 const char *name;
3292 int e = errno;
3293 cpp_buffer *ip = cpp_file_buffer (pfile);
3295 cpp_print_containing_files (pfile);
3297 if (ip != NULL)
3298 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
3300 cpp_message (pfile, is_error, "%s: %s", name, my_strerror (e));
3303 void
3304 cpp_perror_with_name (pfile, name)
3305 cpp_reader *pfile;
3306 const char *name;
3308 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
3311 /* TODO:
3312 * No pre-compiled header file support.
3314 * Possibly different enum token codes for each C/C++ token.
3316 * Find and cleanup remaining uses of static variables,
3318 * Support -dM flag (dump_all_macros).
3320 * Support for_lint flag.