Daily bump.
[official-gcc.git] / gcc / cpplib.c
blobefd87e1ccaa22814dad1c0612fafdc12e926c55b
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 return 1;
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)
524 /* A line of just `#' becomes blank. */
525 if (PEEKC() == '\n')
526 return 1;
527 else
528 return 0;
532 * Decode the keyword and call the appropriate expansion
533 * routine, after moving the input pointer up to the next line.
535 for (kt = directive_table; ; kt++)
537 if (kt->length <= 0)
538 return 0;
539 if (kt->length == ident_length
540 && !strncmp (kt->name, ident, ident_length))
541 break;
544 CPP_SET_WRITTEN (pfile, old_written);
545 (*kt->func) (pfile, kt);
547 return 1;
550 /* Pass a directive through to the output file.
551 BUF points to the contents of the directive, as a contiguous string.
552 LEN is the length of the string pointed to by BUF.
553 KEYWORD is the keyword-table entry for the directive. */
555 static void
556 pass_thru_directive (buf, len, pfile, keyword)
557 U_CHAR *buf;
558 size_t len;
559 cpp_reader *pfile;
560 struct directive *keyword;
562 register unsigned keyword_length = keyword->length;
564 CPP_RESERVE (pfile, 1 + keyword_length + len);
565 CPP_PUTC_Q (pfile, '#');
566 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
567 if (len != 0 && buf[0] != ' ')
568 CPP_PUTC_Q (pfile, ' ');
569 CPP_PUTS_Q (pfile, buf, len);
572 /* Check a purported macro name SYMNAME, and yield its length.
573 ASSERTION is nonzero if this is really for an assertion name. */
576 check_macro_name (pfile, symname, assertion)
577 cpp_reader *pfile;
578 U_CHAR *symname;
579 int assertion;
581 U_CHAR *p;
582 int sym_length;
584 for (p = symname; is_idchar[*p]; p++)
586 sym_length = p - symname;
587 if (sym_length == 0
588 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
589 cpp_error (pfile,
590 assertion ? "invalid assertion name" : "invalid macro name");
591 else if (!is_idstart[*symname]
592 || (! strncmp (symname, "defined", 7) && sym_length == 7)) {
593 U_CHAR *msg; /* what pain... */
594 msg = (U_CHAR *) alloca (sym_length + 1);
595 bcopy (symname, msg, sym_length);
596 msg[sym_length] = 0;
597 cpp_error (pfile,
598 (assertion
599 ? "invalid assertion name `%s'"
600 : "invalid macro name `%s'"),
601 msg);
603 return sym_length;
606 /* Process a #define command.
607 KEYWORD is the keyword-table entry for #define,
608 or NULL for a "predefined" macro. */
610 static int
611 do_define (pfile, keyword)
612 cpp_reader *pfile;
613 struct directive *keyword;
615 int hashcode;
616 MACRODEF mdef;
617 HASHNODE *hp;
618 long here;
619 U_CHAR *macro, *buf, *end;
621 here = CPP_WRITTEN (pfile);
622 copy_rest_of_line (pfile);
624 /* Copy out the line so we can pop the token buffer. */
625 buf = pfile->token_buffer + here;
626 end = CPP_PWRITTEN (pfile);
627 macro = (U_CHAR *) alloca (end - buf + 1);
628 bcopy (buf, macro, end - buf + 1);
629 end = macro + (end - buf);
631 CPP_SET_WRITTEN (pfile, here);
633 mdef = create_definition (macro, end, pfile, keyword == NULL);
634 if (mdef.defn == 0)
635 return 0;
637 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
639 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
641 int ok = 0;
642 /* Redefining a precompiled key is ok. */
643 if (hp->type == T_PCSTRING)
644 ok = 1;
645 /* Redefining a macro is ok if the definitions are the same. */
646 else if (hp->type == T_MACRO)
647 ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
648 /* Redefining a constant is ok with -D. */
649 else if (hp->type == T_CONST || hp->type == T_STDC)
650 ok = ! CPP_OPTIONS (pfile)->done_initializing;
651 /* Print the warning if it's not ok. */
652 if (!ok)
654 cpp_pedwarn (pfile, "`%.*s' redefined", mdef.symlen, mdef.symnam);
655 if (hp->type == T_MACRO)
656 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file,
657 hp->value.defn->line,
658 "this is the location of the previous definition");
660 /* Replace the old definition. */
661 hp->type = T_MACRO;
662 hp->value.defn = mdef.defn;
664 else
665 cpp_install (pfile, mdef.symnam, mdef.symlen, T_MACRO,
666 (char *) mdef.defn, hashcode);
668 if (keyword)
670 if (CPP_OPTIONS (pfile)->debug_output
671 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
672 dump_definition (pfile, mdef);
673 else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
674 pass_thru_directive (mdef.symnam, mdef.symlen, pfile, keyword);
677 return 0;
681 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
682 If BUFFER != NULL, then use the LENGTH characters in BUFFER
683 as the new input buffer.
684 Return the new buffer, or NULL on failure. */
686 cpp_buffer *
687 cpp_push_buffer (pfile, buffer, length)
688 cpp_reader *pfile;
689 U_CHAR *buffer;
690 long length;
692 cpp_buffer *buf = CPP_BUFFER (pfile);
693 cpp_buffer *new;
694 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
696 cpp_fatal (pfile, "macro or `#include' recursion too deep");
697 return NULL;
700 new = (cpp_buffer *) xcalloc (sizeof (cpp_buffer), 1);
702 new->if_stack = pfile->if_stack;
703 new->cleanup = null_cleanup;
704 new->underflow = null_underflow;
705 new->buf = new->cur = buffer;
706 new->alimit = new->rlimit = buffer + length;
707 new->prev = buf;
708 new->mark = -1;
710 CPP_BUFFER (pfile) = new;
711 return new;
714 cpp_buffer *
715 cpp_pop_buffer (pfile)
716 cpp_reader *pfile;
718 cpp_buffer *buf = CPP_BUFFER (pfile);
719 (*buf->cleanup) (buf, pfile);
720 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
721 free (buf);
722 pfile->buffer_stack_depth--;
723 return CPP_BUFFER (pfile);
726 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
727 Pop the buffer when done. */
729 void
730 cpp_scan_buffer (pfile)
731 cpp_reader *pfile;
733 cpp_buffer *buffer = CPP_BUFFER (pfile);
734 for (;;)
736 enum cpp_token token = cpp_get_token (pfile);
737 if (token == CPP_EOF) /* Should not happen ... */
738 break;
739 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
741 cpp_pop_buffer (pfile);
742 break;
748 * Rescan a string (which may have escape marks) into pfile's buffer.
749 * Place the result in pfile->token_buffer.
751 * The input is copied before it is scanned, so it is safe to pass
752 * it something from the token_buffer that will get overwritten
753 * (because it follows CPP_WRITTEN). This is used by do_include.
756 void
757 cpp_expand_to_buffer (pfile, buf, length)
758 cpp_reader *pfile;
759 U_CHAR *buf;
760 int length;
762 register cpp_buffer *ip;
763 #if 0
764 cpp_buffer obuf;
765 #endif
766 U_CHAR *buf1;
767 #if 0
768 int odepth = indepth;
769 #endif
771 if (length < 0)
773 cpp_fatal (pfile, "internal error: length < 0 in cpp_expand_to_buffer");
774 return;
777 /* Set up the input on the input stack. */
779 buf1 = (U_CHAR *) alloca (length + 1);
780 memcpy (buf1, buf, length);
781 buf1[length] = 0;
783 ip = cpp_push_buffer (pfile, buf1, length);
784 if (ip == NULL)
785 return;
786 ip->has_escapes = 1;
787 #if 0
788 ip->lineno = obuf.lineno = 1;
789 #endif
791 /* Scan the input, create the output. */
792 cpp_scan_buffer (pfile);
794 CPP_NUL_TERMINATE (pfile);
797 void
798 cpp_buf_line_and_col (pbuf, linep, colp)
799 register cpp_buffer *pbuf;
800 long *linep, *colp;
802 if (pbuf)
804 *linep = pbuf->lineno;
805 if (colp)
806 *colp = pbuf->cur - pbuf->line_base;
808 else
810 *linep = 0;
811 if (colp)
812 *colp = 0;
816 /* Return the cpp_buffer that corresponds to a file (not a macro). */
818 cpp_buffer *
819 cpp_file_buffer (pfile)
820 cpp_reader *pfile;
822 cpp_buffer *ip = CPP_BUFFER (pfile);
824 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
825 if (ip->fname != NULL)
826 return ip;
827 return NULL;
831 * write out a #line command, for instance, after an #include file.
832 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
835 void
836 output_line_command (pfile, file_change)
837 cpp_reader *pfile;
838 enum file_change_code file_change;
840 long line;
841 cpp_buffer *ip = CPP_BUFFER (pfile);
843 if (ip->fname == NULL)
844 return;
846 if (CPP_OPTIONS (pfile)->no_line_commands
847 || CPP_OPTIONS (pfile)->no_output)
848 return;
850 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, NULL);
852 /* If the current file has not changed, we omit the #line if it would
853 appear to be a no-op, and we output a few newlines instead
854 if we want to increase the line number by a small amount.
855 We cannot do this if pfile->lineno is zero, because that means we
856 haven't output any line commands yet. (The very first line command
857 output is a `same_file' command.) */
858 if (file_change == same_file && pfile->lineno != 0)
860 if (line == pfile->lineno)
861 return;
863 /* If the inherited line number is a little too small,
864 output some newlines instead of a #line command. */
865 if (line > pfile->lineno && line < pfile->lineno + 8)
867 CPP_RESERVE (pfile, 20);
868 while (line > pfile->lineno)
870 CPP_PUTC_Q (pfile, '\n');
871 pfile->lineno++;
873 return;
877 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
878 CPP_PUTS_Q (pfile, "# ", 2);
880 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
881 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
883 quote_string (pfile, ip->nominal_fname);
884 if (file_change != same_file)
886 CPP_PUTC_Q (pfile, ' ');
887 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
889 /* Tell cc1 if following text comes from a system header file. */
890 if (ip->system_header_p)
892 CPP_PUTC_Q (pfile, ' ');
893 CPP_PUTC_Q (pfile, '3');
895 #ifndef NO_IMPLICIT_EXTERN_C
896 /* Tell cc1plus if following text should be treated as C. */
897 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
899 CPP_PUTC_Q (pfile, ' ');
900 CPP_PUTC_Q (pfile, '4');
902 #endif
903 CPP_PUTC_Q (pfile, '\n');
904 pfile->lineno = line;
908 /* Like cpp_get_token, except that it does not read past end-of-line.
909 Also, horizontal space is skipped, and macros are popped. */
911 static enum cpp_token
912 get_directive_token (pfile)
913 cpp_reader *pfile;
915 for (;;)
917 long old_written = CPP_WRITTEN (pfile);
918 enum cpp_token token;
919 cpp_skip_hspace (pfile);
920 if (PEEKC () == '\n')
921 return CPP_VSPACE;
922 token = cpp_get_token (pfile);
923 switch (token)
925 case CPP_POP:
926 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
927 return token;
928 /* ... else fall though ... */
929 case CPP_HSPACE: case CPP_COMMENT:
930 CPP_SET_WRITTEN (pfile, old_written);
931 break;
932 default:
933 return token;
938 /* Handle #include and #import.
939 This function expects to see "fname" or <fname> on the input.
941 The input is normally in part of the output_buffer following
942 CPP_WRITTEN, and will get overwritten by output_line_command.
943 I.e. in input file specification has been popped by handle_directive.
944 This is safe. */
946 static int
947 do_include (pfile, keyword)
948 cpp_reader *pfile;
949 struct directive *keyword;
951 int importing = (keyword->type == T_IMPORT);
952 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
953 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
954 int before; /* included before? */
955 long flen;
956 unsigned char *ftok;
957 cpp_buffer *fp;
959 enum cpp_token token;
961 /* Chain of dirs to search */
962 struct include_hash *ihash;
963 struct file_name_list *search_start;
965 long old_written = CPP_WRITTEN (pfile);
967 int fd;
969 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
971 if (importing)
972 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
973 if (skip_dirs)
974 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
977 if (importing && CPP_OPTIONS (pfile)->warn_import
978 && !CPP_OPTIONS (pfile)->inhibit_warnings
979 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
981 pfile->import_warning = 1;
982 cpp_warning (pfile,
983 "#import is obsolete, use an #ifndef wrapper in the header file");
986 pfile->parsing_include_directive++;
987 token = get_directive_token (pfile);
988 pfile->parsing_include_directive--;
990 if (token == CPP_STRING)
992 if (pfile->token_buffer[old_written] == '<')
993 angle_brackets = 1;
995 #ifdef VMS
996 else if (token == CPP_NAME)
998 /* Support '#include xyz' like VAX-C. It is taken as
999 '#include <xyz.h>' and generates a warning. */
1000 cpp_warning (pfile,
1001 "`#include filename' is obsolete, use `#include <filename.h>'");
1002 angle_brackets = 1;
1004 /* Append the missing `.h' to the name. */
1005 CPP_PUTS (pfile, ".h", 2);
1007 #endif
1008 else
1010 cpp_error (pfile,
1011 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1012 CPP_SET_WRITTEN (pfile, old_written);
1013 skip_rest_of_line (pfile);
1014 return 0;
1017 flen = CPP_WRITTEN (pfile) - old_written;
1018 ftok = (unsigned char *) alloca (flen + 1);
1019 memcpy (ftok, pfile->token_buffer + old_written, flen);
1020 ftok[flen] = '\0';
1022 if (get_directive_token (pfile) != CPP_VSPACE)
1024 cpp_error (pfile, "junk at end of `#include'");
1025 skip_rest_of_line (pfile);
1028 CPP_SET_WRITTEN (pfile, old_written);
1030 if (flen == 0)
1032 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
1033 return 0;
1036 if (CPP_OPTIONS (pfile)->dump_includes)
1037 pass_thru_directive (ftok,
1038 flen
1039 #ifdef VMS
1040 - ((token == CPP_NAME) ? 2 : 0)
1041 #endif
1042 , pfile, keyword);
1044 #ifdef VMS
1045 if (token == CPP_STRING)
1046 #endif
1048 ftok++;
1049 flen -= 2;
1050 ftok[flen] = '\0';
1053 search_start = 0;
1055 for (fp = CPP_BUFFER (pfile);
1056 fp != CPP_NULL_BUFFER (pfile);
1057 fp = CPP_PREV_BUFFER (fp))
1058 if (fp->fname != NULL)
1059 break;
1061 if (fp == CPP_NULL_BUFFER (pfile))
1063 cpp_fatal (pfile, "cpp internal error: fp == NULL_BUFFER in do_include");
1064 return 0;
1067 /* For #include_next, skip in the search path past the dir in which the
1068 containing file was found. Treat files specified using an absolute path
1069 as if there are no more directories to search. Treat the primary source
1070 file like any other included source, but generate a warning. */
1071 if (skip_dirs && CPP_PREV_BUFFER(fp) != CPP_NULL_BUFFER (pfile))
1073 if (fp->ihash->foundhere != ABSOLUTE_PATH)
1074 search_start = fp->ihash->foundhere->next;
1076 else
1078 if (skip_dirs)
1079 cpp_warning (pfile, "#include_next in primary source file");
1081 if (angle_brackets)
1082 search_start = CPP_OPTIONS (pfile)->bracket_include;
1083 else
1085 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
1087 if (fp)
1088 search_start = fp->actual_dir;
1090 else
1091 search_start = CPP_OPTIONS (pfile)->quote_include;
1095 if (!search_start)
1097 cpp_error (pfile, "No include path in which to find %s", ftok);
1098 return 0;
1101 fd = find_include_file (pfile, ftok, search_start, &ihash, &before);
1103 if (fd == -2)
1104 return 0;
1106 if (fd == -1)
1108 if (CPP_OPTIONS (pfile)->print_deps_missing_files
1109 && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
1110 (pfile->system_include_depth > 0)))
1112 if (!angle_brackets)
1113 deps_output (pfile, ftok, ' ');
1114 else
1116 char *p;
1117 struct file_name_list *ptr;
1118 /* If requested as a system header, assume it belongs in
1119 the first system header directory. */
1120 if (CPP_OPTIONS (pfile)->bracket_include)
1121 ptr = CPP_OPTIONS (pfile)->bracket_include;
1122 else
1123 ptr = CPP_OPTIONS (pfile)->quote_include;
1125 p = (char *) alloca (strlen (ptr->name)
1126 + strlen (ftok) + 2);
1127 if (*ptr->name != '\0')
1129 strcpy (p, ptr->name);
1130 strcat (p, "/");
1132 strcat (p, ftok);
1133 deps_output (pfile, p, ' ');
1136 /* If -M was specified, and this header file won't be added to
1137 the dependency list, then don't count this as an error,
1138 because we can still produce correct output. Otherwise, we
1139 can't produce correct output, because there may be
1140 dependencies we need inside the missing file, and we don't
1141 know what directory this missing file exists in. */
1142 else if (CPP_PRINT_DEPS (pfile)
1143 && (CPP_PRINT_DEPS (pfile)
1144 <= (angle_brackets || (pfile->system_include_depth > 0))))
1145 cpp_warning (pfile, "No include path in which to find %s", ftok);
1146 else
1147 cpp_error_from_errno (pfile, ftok);
1149 return 0;
1152 /* For -M, add the file to the dependencies on its first inclusion. */
1153 if (!before && (CPP_PRINT_DEPS (pfile)
1154 > (angle_brackets || (pfile->system_include_depth > 0))))
1155 deps_output (pfile, ihash->name, ' ');
1157 /* Handle -H option. */
1158 if (CPP_OPTIONS(pfile)->print_include_names)
1160 fp = CPP_BUFFER (pfile);
1161 while ((fp = CPP_PREV_BUFFER (fp)) != CPP_NULL_BUFFER (pfile))
1162 putc ('.', stderr);
1163 fprintf (stderr, " %s\n", ihash->name);
1166 /* Actually process the file */
1168 if (importing)
1169 ihash->control_macro = "";
1171 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
1173 close (fd);
1174 return 0;
1177 if (angle_brackets)
1178 pfile->system_include_depth++; /* Decremented in file_cleanup. */
1180 if (finclude (pfile, fd, ihash))
1182 output_line_command (pfile, enter_file);
1183 pfile->only_seen_white = 2;
1186 return 0;
1189 /* Interpret #line command.
1190 Note that the filename string (if any) is treated as if it were an
1191 include filename. That means no escape handling. */
1193 static int
1194 do_line (pfile, keyword)
1195 cpp_reader *pfile;
1196 struct directive *keyword ATTRIBUTE_UNUSED;
1198 cpp_buffer *ip = CPP_BUFFER (pfile);
1199 int new_lineno;
1200 long old_written = CPP_WRITTEN (pfile);
1201 enum file_change_code file_change = same_file;
1202 enum cpp_token token;
1203 char *x;
1205 token = get_directive_token (pfile);
1207 if (token != CPP_NUMBER)
1209 cpp_error (pfile, "token after `#line' is not an integer");
1210 goto bad_line_directive;
1213 new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
1214 if (x[0] != '\0')
1216 cpp_error (pfile, "token after `#line' is not an integer");
1217 goto bad_line_directive;
1219 CPP_SET_WRITTEN (pfile, old_written);
1221 if (CPP_PEDANTIC (pfile) && new_lineno <= 0)
1222 cpp_pedwarn (pfile, "line number out of range in `#line' command");
1224 token = get_directive_token (pfile);
1226 if (token == CPP_STRING)
1228 U_CHAR *fname = pfile->token_buffer + old_written + 1;
1229 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
1230 long num_start = CPP_WRITTEN (pfile);
1232 token = get_directive_token (pfile);
1233 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
1235 U_CHAR *p = pfile->token_buffer + num_start;
1236 if (CPP_PEDANTIC (pfile))
1237 cpp_pedwarn (pfile, "garbage at end of `#line' command");
1239 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
1241 cpp_error (pfile, "invalid format `#line' command");
1242 goto bad_line_directive;
1244 if (*p == '1')
1245 file_change = enter_file;
1246 else if (*p == '2')
1247 file_change = leave_file;
1248 else if (*p == '3')
1249 ip->system_header_p = 1;
1250 else /* if (*p == '4') */
1251 ip->system_header_p = 2;
1253 CPP_SET_WRITTEN (pfile, num_start);
1254 token = get_directive_token (pfile);
1255 p = pfile->token_buffer + num_start;
1256 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4'))
1258 ip->system_header_p = *p == '3' ? 1 : 2;
1259 token = get_directive_token (pfile);
1261 if (token != CPP_VSPACE)
1263 cpp_error (pfile, "invalid format `#line' command");
1264 goto bad_line_directive;
1268 *end_name = '\0';
1270 if (strcmp (fname, ip->nominal_fname))
1272 char *newname, *oldname;
1273 if (!strcmp (fname, ip->fname))
1274 newname = ip->fname;
1275 else if (ip->last_nominal_fname
1276 && !strcmp (fname, ip->last_nominal_fname))
1277 newname = ip->last_nominal_fname;
1278 else
1279 newname = xstrdup (fname);
1281 oldname = ip->nominal_fname;
1282 ip->nominal_fname = newname;
1284 if (ip->last_nominal_fname
1285 && ip->last_nominal_fname != oldname
1286 && ip->last_nominal_fname != newname
1287 && ip->last_nominal_fname != ip->fname)
1288 free (ip->last_nominal_fname);
1290 if (newname == ip->fname)
1291 ip->last_nominal_fname = NULL;
1292 else
1293 ip->last_nominal_fname = oldname;
1296 else if (token != CPP_VSPACE && token != CPP_EOF)
1298 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1299 goto bad_line_directive;
1302 /* The Newline at the end of this line remains to be processed.
1303 To put the next line at the specified line number,
1304 we must store a line number now that is one less. */
1305 ip->lineno = new_lineno - 1;
1306 CPP_SET_WRITTEN (pfile, old_written);
1307 output_line_command (pfile, file_change);
1308 return 0;
1310 bad_line_directive:
1311 skip_rest_of_line (pfile);
1312 CPP_SET_WRITTEN (pfile, old_written);
1313 return 0;
1316 /* Remove the definition of a symbol from the symbol table.
1317 According to the C standard, it is not an error to undef
1318 something that has no definitions. */
1319 static int
1320 do_undef (pfile, keyword)
1321 cpp_reader *pfile;
1322 struct directive *keyword;
1324 int sym_length;
1325 HASHNODE *hp;
1326 U_CHAR *buf, *name, *limit;
1327 int c;
1328 long here = CPP_WRITTEN (pfile);
1329 enum cpp_token token;
1331 cpp_skip_hspace (pfile);
1332 c = GETC();
1333 if (! is_idstart[c])
1335 cpp_error (pfile, "token after #undef is not an identifier");
1336 skip_rest_of_line (pfile);
1337 return 1;
1340 parse_name (pfile, c);
1341 buf = pfile->token_buffer + here;
1342 limit = CPP_PWRITTEN(pfile);
1344 /* Copy out the token so we can pop the token buffer. */
1345 name = (U_CHAR *) alloca (limit - buf + 1);
1346 bcopy(buf, name, limit - buf);
1347 name[limit - buf] = '\0';
1349 token = get_directive_token (pfile);
1350 if (token != CPP_VSPACE && token != CPP_POP)
1352 cpp_pedwarn (pfile, "junk on line after #undef");
1353 skip_rest_of_line (pfile);
1356 CPP_SET_WRITTEN (pfile, here);
1358 sym_length = check_macro_name (pfile, buf, 0);
1360 while ((hp = cpp_lookup (pfile, name, sym_length, -1)) != NULL)
1362 /* If we are generating additional info for debugging (with -g) we
1363 need to pass through all effective #undef commands. */
1364 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1365 pass_thru_directive (name, sym_length, pfile, keyword);
1366 if (hp->type != T_MACRO)
1367 cpp_warning (pfile, "undefining `%s'", hp->name);
1368 delete_macro (hp);
1371 return 0;
1374 /* Wrap do_undef for -U processing. */
1375 void
1376 cpp_undef (pfile, macro)
1377 cpp_reader *pfile;
1378 U_CHAR *macro;
1380 if (cpp_push_buffer (pfile, macro, strlen (macro)))
1382 do_undef (pfile, NULL);
1383 cpp_pop_buffer (pfile);
1389 * Report an error detected by the program we are processing.
1390 * Use the text of the line in the error message.
1391 * (We use error because it prints the filename & line#.)
1394 static int
1395 do_error (pfile, keyword)
1396 cpp_reader *pfile;
1397 struct directive *keyword ATTRIBUTE_UNUSED;
1399 long here = CPP_WRITTEN (pfile);
1400 U_CHAR *text;
1401 copy_rest_of_line (pfile);
1402 text = pfile->token_buffer + here;
1403 SKIP_WHITE_SPACE(text);
1405 cpp_error (pfile, "#error %s", text);
1406 CPP_SET_WRITTEN (pfile, here);
1407 return 0;
1411 * Report a warning detected by the program we are processing.
1412 * Use the text of the line in the warning message, then continue.
1415 static int
1416 do_warning (pfile, keyword)
1417 cpp_reader *pfile;
1418 struct directive *keyword ATTRIBUTE_UNUSED;
1420 U_CHAR *text;
1421 long here = CPP_WRITTEN(pfile);
1422 copy_rest_of_line (pfile);
1423 text = pfile->token_buffer + here;
1424 SKIP_WHITE_SPACE(text);
1426 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1427 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1429 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
1430 if -pedantic-errors is given, #warning should cause an error. */
1431 cpp_pedwarn (pfile, "#warning %s", text);
1432 CPP_SET_WRITTEN (pfile, here);
1433 return 0;
1436 /* Report program identification.
1437 This is not precisely what cccp does with #ident, however I believe
1438 it matches `closely enough' (behavior is identical as long as there
1439 are no macros on the #ident line, which is pathological in my opinion). */
1441 static int
1442 do_ident (pfile, keyword)
1443 cpp_reader *pfile;
1444 struct directive *keyword ATTRIBUTE_UNUSED;
1446 /* Allow #ident in system headers, since that's not user's fault. */
1447 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1448 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1450 CPP_PUTS (pfile, "#ident ", 7);
1451 cpp_skip_hspace (pfile);
1452 copy_rest_of_line (pfile);
1454 return 0;
1457 /* Just check for some recognized pragmas that need validation here,
1458 and leave the text in the token buffer to be output. */
1460 static int
1461 do_pragma (pfile, keyword)
1462 cpp_reader *pfile;
1463 struct directive *keyword ATTRIBUTE_UNUSED;
1465 long here;
1466 U_CHAR *buf;
1468 CPP_PUTS (pfile, "#pragma ", 8);
1469 cpp_skip_hspace (pfile);
1471 here = CPP_WRITTEN (pfile);
1472 copy_rest_of_line (pfile);
1473 buf = pfile->token_buffer + here;
1475 if (!strncmp (buf, "once", 4))
1477 cpp_buffer *ip = NULL;
1479 /* Allow #pragma once in system headers, since that's not the user's
1480 fault. */
1481 if (!CPP_BUFFER (pfile)->system_header_p)
1482 cpp_warning (pfile, "`#pragma once' is obsolete");
1484 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
1486 if (ip == CPP_NULL_BUFFER (pfile))
1487 return 0;
1488 if (ip->fname != NULL)
1489 break;
1492 if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
1493 cpp_warning (pfile, "`#pragma once' outside include file");
1494 else
1495 ip->ihash->control_macro = ""; /* never repeat */
1497 else if (!strncmp (buf, "implementation", 14))
1499 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1500 been included yet. */
1501 struct include_hash *ptr;
1502 U_CHAR *p = buf + 14, *fname, *fcopy;
1503 SKIP_WHITE_SPACE (p);
1504 if (*p == '\n' || *p != '\"')
1505 return 0;
1507 fname = p + 1;
1508 p = (U_CHAR *) index (fname, '\"');
1510 fcopy = (U_CHAR *) alloca (p - fname + 1);
1511 bcopy (fname, fcopy, p - fname);
1512 fcopy[p-fname] = '\0';
1514 ptr = include_hash (pfile, fcopy, 0);
1515 if (ptr)
1516 cpp_warning (pfile,
1517 "`#pragma implementation' for `%s' appears after file is included",
1518 fcopy);
1521 return 0;
1524 #ifdef SCCS_DIRECTIVE
1525 /* Just ignore #sccs, on systems where we define it at all. */
1527 static int
1528 do_sccs (pfile, keyword)
1529 cpp_reader *pfile;
1530 struct directive *keyword ATTRIBUTE_UNUSED;
1532 if (CPP_PEDANTIC (pfile))
1533 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1534 skip_rest_of_line (pfile);
1535 return 0;
1537 #endif
1540 * handle #if command by
1541 * 1) inserting special `defined' keyword into the hash table
1542 * that gets turned into 0 or 1 by special_symbol (thus,
1543 * if the luser has a symbol called `defined' already, it won't
1544 * work inside the #if command)
1545 * 2) rescan the input into a temporary output buffer
1546 * 3) pass the output buffer to the yacc parser and collect a value
1547 * 4) clean up the mess left from steps 1 and 2.
1548 * 5) call conditional_skip to skip til the next #endif (etc.),
1549 * or not, depending on the value from step 3.
1552 static int
1553 do_if (pfile, keyword)
1554 cpp_reader *pfile;
1555 struct directive *keyword ATTRIBUTE_UNUSED;
1557 HOST_WIDEST_INT value = eval_if_expression (pfile);
1558 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
1559 return 0;
1563 * handle a #elif directive by not changing if_stack either.
1564 * see the comment above do_else.
1567 static int
1568 do_elif (pfile, keyword)
1569 cpp_reader *pfile;
1570 struct directive *keyword ATTRIBUTE_UNUSED;
1572 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
1573 cpp_error (pfile, "`#elif' not within a conditional");
1574 return 0;
1575 } else {
1576 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
1577 cpp_error (pfile, "`#elif' after `#else'");
1578 #if 0
1579 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
1580 #endif
1581 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
1582 && strcmp (pfile->if_stack->fname,
1583 CPP_BUFFER (pfile)->nominal_fname) != 0)
1584 fprintf (stderr, ", file %s", pfile->if_stack->fname);
1585 fprintf (stderr, ")\n");
1587 pfile->if_stack->type = T_ELIF;
1590 if (pfile->if_stack->if_succeeded)
1591 skip_if_group (pfile);
1592 else {
1593 HOST_WIDEST_INT value = eval_if_expression (pfile);
1594 if (value == 0)
1595 skip_if_group (pfile);
1596 else {
1597 ++pfile->if_stack->if_succeeded; /* continue processing input */
1598 output_line_command (pfile, same_file);
1601 return 0;
1605 * evaluate a #if expression in BUF, of length LENGTH,
1606 * then parse the result as a C expression and return the value as an int.
1609 static HOST_WIDEST_INT
1610 eval_if_expression (pfile)
1611 cpp_reader *pfile;
1613 HOST_WIDEST_INT value;
1614 long old_written = CPP_WRITTEN (pfile);
1616 pfile->pcp_inside_if = 1;
1617 value = cpp_parse_expr (pfile);
1618 pfile->pcp_inside_if = 0;
1620 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1622 return value;
1626 * routine to handle ifdef/ifndef. Try to look up the symbol,
1627 * then do or don't skip to the #endif/#else/#elif depending
1628 * on what directive is actually being processed.
1631 static int
1632 do_xifdef (pfile, keyword)
1633 cpp_reader *pfile;
1634 struct directive *keyword;
1636 int skip;
1637 cpp_buffer *ip = CPP_BUFFER (pfile);
1638 U_CHAR *ident;
1639 int ident_length;
1640 enum cpp_token token;
1641 int start_of_file = 0;
1642 U_CHAR *control_macro = 0;
1643 int old_written = CPP_WRITTEN (pfile);
1645 /* Detect a #ifndef at start of file (not counting comments). */
1646 if (ip->fname != 0 && keyword->type == T_IFNDEF)
1647 start_of_file = pfile->only_seen_white == 2;
1649 pfile->no_macro_expand++;
1650 token = get_directive_token (pfile);
1651 pfile->no_macro_expand--;
1653 ident = pfile->token_buffer + old_written;
1654 ident_length = CPP_WRITTEN (pfile) - old_written;
1655 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1657 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1659 skip = (keyword->type == T_IFDEF);
1660 if (! CPP_TRADITIONAL (pfile))
1661 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
1663 else if (token == CPP_NAME)
1665 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
1666 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
1667 if (start_of_file && !skip)
1669 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
1670 bcopy (ident, control_macro, ident_length + 1);
1673 else
1675 skip = (keyword->type == T_IFDEF);
1676 if (! CPP_TRADITIONAL (pfile))
1677 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
1680 if (!CPP_TRADITIONAL (pfile))
1681 { int c;
1682 cpp_skip_hspace (pfile);
1683 c = PEEKC ();
1684 if (c != EOF && c != '\n')
1685 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
1687 skip_rest_of_line (pfile);
1689 #if 0
1690 if (pcp_outfile) {
1691 /* Output a precondition for this macro. */
1692 if (hp && hp->value.defn->predefined)
1693 fprintf (pcp_outfile, "#define %s\n", hp->name);
1694 else {
1695 U_CHAR *cp = buf;
1696 fprintf (pcp_outfile, "#undef ");
1697 while (is_idchar[*cp]) /* Ick! */
1698 fputc (*cp++, pcp_outfile);
1699 putc ('\n', pcp_outfile);
1701 #endif
1703 conditional_skip (pfile, skip, T_IF, control_macro);
1704 return 0;
1707 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1708 If this is a #ifndef starting at the beginning of a file,
1709 CONTROL_MACRO is the macro name tested by the #ifndef.
1710 Otherwise, CONTROL_MACRO is 0. */
1712 static void
1713 conditional_skip (pfile, skip, type, control_macro)
1714 cpp_reader *pfile;
1715 int skip;
1716 enum node_type type;
1717 U_CHAR *control_macro;
1719 IF_STACK_FRAME *temp;
1721 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
1722 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
1723 #if 0
1724 temp->lineno = CPP_BUFFER (pfile)->lineno;
1725 #endif
1726 temp->next = pfile->if_stack;
1727 temp->control_macro = control_macro;
1728 pfile->if_stack = temp;
1730 pfile->if_stack->type = type;
1732 if (skip != 0) {
1733 skip_if_group (pfile);
1734 return;
1735 } else {
1736 ++pfile->if_stack->if_succeeded;
1737 output_line_command (pfile, same_file);
1741 /* Subroutine of skip_if_group. Examine one preprocessing directive and
1742 return 0 if skipping should continue, 1 if it should halt. Also
1743 adjusts the if_stack as appropriate.
1744 The `#' has been read, but not the identifier. */
1746 static int
1747 consider_directive_while_skipping (pfile, stack)
1748 cpp_reader *pfile;
1749 IF_STACK_FRAME *stack;
1751 long ident_len, ident;
1752 struct directive *kt;
1753 IF_STACK_FRAME *temp;
1755 cpp_skip_hspace (pfile);
1757 ident = CPP_WRITTEN (pfile);
1758 parse_name (pfile, GETC());
1759 ident_len = CPP_WRITTEN (pfile) - ident;
1761 CPP_SET_WRITTEN (pfile, ident);
1763 for (kt = directive_table; kt->length >= 0; kt++)
1764 if (kt->length == ident_len
1765 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
1766 switch (kt->type)
1768 case T_IF:
1769 case T_IFDEF:
1770 case T_IFNDEF:
1771 temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
1772 temp->next = pfile->if_stack;
1773 pfile->if_stack = temp;
1774 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
1775 temp->type = kt->type;
1776 return 0;
1778 case T_ELSE:
1779 if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
1780 validate_else (pfile, "#else");
1781 /* fall through */
1782 case T_ELIF:
1783 if (pfile->if_stack->type == T_ELSE)
1784 cpp_error (pfile, "`%s' after `#else'", kt->name);
1786 if (pfile->if_stack == stack)
1787 return 1;
1788 else
1790 pfile->if_stack->type = kt->type;
1791 return 0;
1794 case T_ENDIF:
1795 if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
1796 validate_else (pfile, "#endif");
1798 if (pfile->if_stack == stack)
1799 return 1;
1801 temp = pfile->if_stack;
1802 pfile->if_stack = temp->next;
1803 free (temp);
1804 return 0;
1806 default:
1807 return 0;
1810 /* Don't let erroneous code go by. */
1811 if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
1812 cpp_pedwarn (pfile, "invalid preprocessor directive name");
1813 return 0;
1816 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
1817 * leaves input ptr at the sharp sign found.
1819 static void
1820 skip_if_group (pfile)
1821 cpp_reader *pfile;
1823 int c;
1824 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
1825 U_CHAR *beg_of_line;
1826 long old_written;
1828 if (CPP_OPTIONS (pfile)->output_conditionals)
1830 CPP_PUTS (pfile, "#failed\n", 8);
1831 pfile->lineno++;
1832 output_line_command (pfile, same_file);
1835 old_written = CPP_WRITTEN (pfile);
1837 for (;;)
1839 beg_of_line = CPP_BUFFER (pfile)->cur;
1841 if (! CPP_TRADITIONAL (pfile))
1842 cpp_skip_hspace (pfile);
1843 c = GETC();
1844 if (c == '\n')
1846 if (CPP_OPTIONS (pfile)->output_conditionals)
1847 CPP_PUTC (pfile, c);
1848 CPP_BUMP_LINE (pfile);
1849 continue;
1851 else if (c == '#')
1853 if (consider_directive_while_skipping (pfile, save_if_stack))
1854 break;
1856 else if (c == EOF)
1857 return; /* Caller will issue error. */
1859 FORWARD(-1);
1860 if (CPP_OPTIONS (pfile)->output_conditionals)
1862 CPP_PUTS (pfile, beg_of_line, CPP_BUFFER (pfile)->cur - beg_of_line);
1863 copy_rest_of_line (pfile);
1865 else
1867 copy_rest_of_line (pfile);
1868 CPP_SET_WRITTEN (pfile, old_written); /* discard it */
1871 c = GETC();
1872 if (c == EOF)
1873 return; /* Caller will issue error. */
1874 else
1876 /* \n */
1877 if (CPP_OPTIONS (pfile)->output_conditionals)
1879 CPP_PUTC (pfile, c);
1880 pfile->lineno++;
1882 CPP_BUMP_LINE (pfile);
1886 /* Back up to the beginning of this line. Caller will process the
1887 directive. */
1888 CPP_BUFFER (pfile)->cur = beg_of_line;
1889 pfile->only_seen_white = 1;
1890 if (CPP_OPTIONS (pfile)->output_conditionals)
1892 CPP_PUTS (pfile, "#endfailed\n", 11);
1893 pfile->lineno++;
1898 * handle a #else directive. Do this by just continuing processing
1899 * without changing if_stack ; this is so that the error message
1900 * for missing #endif's etc. will point to the original #if. It
1901 * is possible that something different would be better.
1904 static int
1905 do_else (pfile, keyword)
1906 cpp_reader *pfile;
1907 struct directive *keyword ATTRIBUTE_UNUSED;
1909 cpp_buffer *ip = CPP_BUFFER (pfile);
1911 if (CPP_PEDANTIC (pfile))
1912 validate_else (pfile, "#else");
1913 skip_rest_of_line (pfile);
1915 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
1916 cpp_error (pfile, "`#else' not within a conditional");
1917 return 0;
1918 } else {
1919 /* #ifndef can't have its special treatment for containing the whole file
1920 if it has a #else clause. */
1921 pfile->if_stack->control_macro = 0;
1923 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
1924 cpp_error (pfile, "`#else' after `#else'");
1925 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
1926 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
1927 fprintf (stderr, ", file %s", pfile->if_stack->fname);
1928 fprintf (stderr, ")\n");
1930 pfile->if_stack->type = T_ELSE;
1933 if (pfile->if_stack->if_succeeded)
1934 skip_if_group (pfile);
1935 else {
1936 ++pfile->if_stack->if_succeeded; /* continue processing input */
1937 output_line_command (pfile, same_file);
1939 return 0;
1943 * unstack after #endif command
1946 static int
1947 do_endif (pfile, keyword)
1948 cpp_reader *pfile;
1949 struct directive *keyword ATTRIBUTE_UNUSED;
1951 if (CPP_PEDANTIC (pfile))
1952 validate_else (pfile, "#endif");
1953 skip_rest_of_line (pfile);
1955 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1956 cpp_error (pfile, "unbalanced `#endif'");
1957 else
1959 IF_STACK_FRAME *temp = pfile->if_stack;
1960 pfile->if_stack = temp->next;
1961 if (temp->control_macro != 0)
1963 /* This #endif matched a #ifndef at the start of the file.
1964 See if it is at the end of the file. */
1965 int c;
1967 parse_set_mark (pfile);
1969 for (;;)
1971 cpp_skip_hspace (pfile);
1972 c = GETC ();
1973 if (c != '\n')
1974 break;
1976 parse_goto_mark (pfile);
1978 if (c == EOF)
1980 /* This #endif ends a #ifndef
1981 that contains all of the file (aside from whitespace).
1982 Arrange not to include the file again
1983 if the macro that was tested is defined. */
1984 struct cpp_buffer *ip;
1985 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
1986 if (ip->fname != NULL)
1987 break;
1988 ip->ihash->control_macro = (char *) temp->control_macro;
1991 free (temp);
1992 output_line_command (pfile, same_file);
1994 return 0;
1997 /* When an #else or #endif is found while skipping failed conditional,
1998 if -pedantic was specified, this is called to warn about text after
1999 the command name. P points to the first char after the command name. */
2001 static void
2002 validate_else (pfile, directive)
2003 cpp_reader *pfile;
2004 char *directive;
2006 int c;
2007 cpp_skip_hspace (pfile);
2008 c = PEEKC ();
2009 if (c != EOF && c != '\n')
2010 cpp_pedwarn (pfile,
2011 "text following `%s' violates ANSI standard", directive);
2014 /* Get the next token, and add it to the text in pfile->token_buffer.
2015 Return the kind of token we got. */
2017 enum cpp_token
2018 cpp_get_token (pfile)
2019 cpp_reader *pfile;
2021 register int c, c2, c3;
2022 enum cpp_token token;
2023 struct cpp_options *opts = CPP_OPTIONS (pfile);
2025 get_next:
2026 c = GETC();
2027 if (c == EOF)
2029 handle_eof:
2030 if (CPP_BUFFER (pfile)->seen_eof)
2032 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile)
2033 /* If we've been reading from redirected input, the
2034 frontend will pop the buffer. */
2035 || CPP_BUFFER (pfile)->manual_pop)
2036 return CPP_EOF;
2038 cpp_pop_buffer (pfile);
2039 goto get_next;
2041 else
2043 cpp_buffer *next_buf
2044 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2045 CPP_BUFFER (pfile)->seen_eof = 1;
2046 if (CPP_BUFFER (pfile)->nominal_fname
2047 && next_buf != CPP_NULL_BUFFER (pfile))
2049 /* We're about to return from an #include file.
2050 Emit #line information now (as part of the CPP_POP) result.
2051 But the #line refers to the file we will pop to. */
2052 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2053 CPP_BUFFER (pfile) = next_buf;
2054 pfile->input_stack_listing_current = 0;
2055 output_line_command (pfile, leave_file);
2056 CPP_BUFFER (pfile) = cur_buffer;
2058 return CPP_POP;
2061 else
2063 switch (c)
2065 case '/':
2066 if (PEEKC () == '=')
2067 goto op2;
2069 comment:
2070 if (opts->put_out_comments)
2071 c = copy_comment (pfile, c);
2072 else
2073 c = skip_comment (pfile, c);
2074 if (c == EOF)
2075 goto handle_eof;
2076 else if (c != ' ')
2077 goto randomchar;
2079 /* Comments are equivalent to spaces.
2080 For -traditional, a comment is equivalent to nothing. */
2081 if (opts->traditional || opts->put_out_comments)
2082 return CPP_COMMENT;
2083 else
2085 CPP_PUTC (pfile, c);
2086 return CPP_HSPACE;
2088 #if 0
2089 if (opts->for_lint) {
2090 U_CHAR *argbp;
2091 int cmdlen, arglen;
2092 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
2094 if (lintcmd != NULL) {
2095 /* I believe it is always safe to emit this newline: */
2096 obp[-1] = '\n';
2097 bcopy ("#pragma lint ", (char *) obp, 13);
2098 obp += 13;
2099 bcopy (lintcmd, (char *) obp, cmdlen);
2100 obp += cmdlen;
2102 if (arglen != 0) {
2103 *(obp++) = ' ';
2104 bcopy (argbp, (char *) obp, arglen);
2105 obp += arglen;
2108 /* OK, now bring us back to the state we were in before we entered
2109 this branch. We need #line because the newline for the pragma
2110 could mess things up. */
2111 output_line_command (pfile, same_file);
2112 *(obp++) = ' '; /* just in case, if comments are copied thru */
2113 *(obp++) = '/';
2116 #endif
2118 case '#':
2119 #if 0
2120 /* If this is expanding a macro definition, don't recognize
2121 preprocessor directives. */
2122 if (ip->macro != 0)
2123 goto randomchar;
2124 /* If this is expand_into_temp_buffer, recognize them
2125 only after an actual newline at this level,
2126 not at the beginning of the input level. */
2127 if (ip->fname == 0 && beg_of_line == ip->buf)
2128 goto randomchar;
2129 if (ident_length)
2130 goto specialchar;
2131 #endif
2133 if (!pfile->only_seen_white)
2134 goto randomchar;
2135 if (handle_directive (pfile))
2136 return CPP_DIRECTIVE;
2137 pfile->only_seen_white = 0;
2138 return CPP_OTHER;
2140 case '\"':
2141 case '\'':
2142 string:
2143 parse_string (pfile, c);
2144 pfile->only_seen_white = 0;
2145 return c == '\'' ? CPP_CHAR : CPP_STRING;
2147 case '$':
2148 if (!opts->dollars_in_ident)
2149 goto randomchar;
2150 goto letter;
2152 case ':':
2153 if (opts->cplusplus && PEEKC () == ':')
2154 goto op2;
2155 goto randomchar;
2157 case '&':
2158 case '+':
2159 case '|':
2160 c2 = PEEKC ();
2161 if (c2 == c || c2 == '=')
2162 goto op2;
2163 goto randomchar;
2165 case '*':
2166 case '!':
2167 case '%':
2168 case '=':
2169 case '^':
2170 if (PEEKC () == '=')
2171 goto op2;
2172 goto randomchar;
2174 case '-':
2175 c2 = PEEKC ();
2176 if (c2 == '-' && opts->chill)
2177 goto comment; /* Chill style comment */
2178 if (c2 == '-' || c2 == '=')
2179 goto op2;
2180 if (c2 == '>')
2182 if (opts->cplusplus && PEEKN (1) == '*')
2184 /* In C++, there's a ->* operator. */
2185 op3:
2186 token = CPP_OTHER;
2187 pfile->only_seen_white = 0;
2188 CPP_RESERVE (pfile, 4);
2189 CPP_PUTC_Q (pfile, c);
2190 CPP_PUTC_Q (pfile, GETC ());
2191 CPP_PUTC_Q (pfile, GETC ());
2192 CPP_NUL_TERMINATE_Q (pfile);
2193 return token;
2195 goto op2;
2197 goto randomchar;
2199 case '<':
2200 if (pfile->parsing_include_directive)
2202 for (;;)
2204 CPP_PUTC (pfile, c);
2205 if (c == '>')
2206 break;
2207 c = GETC ();
2208 if (c == '\n' || c == EOF)
2210 cpp_error (pfile,
2211 "missing '>' in `#include <FILENAME>'");
2212 break;
2214 else if (c == '\r')
2216 if (!CPP_BUFFER (pfile)->has_escapes)
2218 /* Backslash newline is replaced by nothing. */
2219 CPP_ADJUST_WRITTEN (pfile, -1);
2220 CPP_BUMP_LINE (pfile);
2222 else
2224 /* We might conceivably get \r- or \r<space> in
2225 here. Just delete 'em. */
2226 int d = GETC();
2227 if (d != '-' && d != ' ')
2228 cpp_fatal (pfile,
2229 "internal error: unrecognized escape \\r%c",
2231 CPP_ADJUST_WRITTEN (pfile, -1);
2235 return CPP_STRING;
2237 /* else fall through */
2238 case '>':
2239 c2 = PEEKC ();
2240 if (c2 == '=')
2241 goto op2;
2242 /* GNU C++ supports MIN and MAX operators <? and >?. */
2243 if (c2 != c && (!opts->cplusplus || c2 != '?'))
2244 goto randomchar;
2245 FORWARD(1);
2246 CPP_RESERVE (pfile, 4);
2247 CPP_PUTC (pfile, c);
2248 CPP_PUTC (pfile, c2);
2249 c3 = PEEKC ();
2250 if (c3 == '=')
2251 CPP_PUTC_Q (pfile, GETC ());
2252 CPP_NUL_TERMINATE_Q (pfile);
2253 pfile->only_seen_white = 0;
2254 return CPP_OTHER;
2256 case '.':
2257 c2 = PEEKC ();
2258 if (ISDIGIT(c2))
2260 CPP_RESERVE(pfile, 2);
2261 CPP_PUTC_Q (pfile, '.');
2262 c = GETC ();
2263 goto number;
2266 /* In C++ there's a .* operator. */
2267 if (opts->cplusplus && c2 == '*')
2268 goto op2;
2270 if (c2 == '.' && PEEKN(1) == '.')
2272 CPP_RESERVE(pfile, 4);
2273 CPP_PUTC_Q (pfile, '.');
2274 CPP_PUTC_Q (pfile, '.');
2275 CPP_PUTC_Q (pfile, '.');
2276 FORWARD (2);
2277 CPP_NUL_TERMINATE_Q (pfile);
2278 pfile->only_seen_white = 0;
2279 return CPP_3DOTS;
2281 goto randomchar;
2283 op2:
2284 token = CPP_OTHER;
2285 pfile->only_seen_white = 0;
2286 CPP_RESERVE(pfile, 3);
2287 CPP_PUTC_Q (pfile, c);
2288 CPP_PUTC_Q (pfile, GETC ());
2289 CPP_NUL_TERMINATE_Q (pfile);
2290 return token;
2292 case 'L':
2293 c2 = PEEKC ();
2294 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2296 CPP_PUTC (pfile, c);
2297 c = GETC ();
2298 goto string;
2300 goto letter;
2302 case '0': case '1': case '2': case '3': case '4':
2303 case '5': case '6': case '7': case '8': case '9':
2304 number:
2305 c2 = '.';
2306 for (;;)
2308 CPP_RESERVE (pfile, 2);
2309 CPP_PUTC_Q (pfile, c);
2310 c = PEEKC ();
2311 if (c == EOF)
2312 break;
2313 if (!is_idchar[c] && c != '.'
2314 && ((c2 != 'e' && c2 != 'E'
2315 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
2316 || (c != '+' && c != '-')))
2317 break;
2318 FORWARD(1);
2319 c2= c;
2321 CPP_NUL_TERMINATE_Q (pfile);
2322 pfile->only_seen_white = 0;
2323 return CPP_NUMBER;
2324 case 'b': case 'c': case 'd': case 'h': case 'o':
2325 case 'B': case 'C': case 'D': case 'H': case 'O':
2326 if (opts->chill && PEEKC () == '\'')
2328 pfile->only_seen_white = 0;
2329 CPP_RESERVE (pfile, 2);
2330 CPP_PUTC_Q (pfile, c);
2331 CPP_PUTC_Q (pfile, '\'');
2332 FORWARD(1);
2333 for (;;)
2335 c = GETC();
2336 if (c == EOF)
2337 goto chill_number_eof;
2338 if (!is_idchar[c])
2339 break;
2340 CPP_PUTC (pfile, c);
2342 if (c == '\'')
2344 CPP_RESERVE (pfile, 2);
2345 CPP_PUTC_Q (pfile, c);
2346 CPP_NUL_TERMINATE_Q (pfile);
2347 return CPP_STRING;
2349 else
2351 FORWARD(-1);
2352 chill_number_eof:
2353 CPP_NUL_TERMINATE (pfile);
2354 return CPP_NUMBER;
2357 else
2358 goto letter;
2359 case '_':
2360 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2361 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2362 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2363 case 'x': case 'y': case 'z':
2364 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2365 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2366 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2367 case 'Y': case 'Z':
2368 letter:
2370 HASHNODE *hp;
2371 unsigned char *ident;
2372 int before_name_written = CPP_WRITTEN (pfile);
2373 int ident_len;
2374 parse_name (pfile, c);
2375 pfile->only_seen_white = 0;
2376 if (pfile->no_macro_expand)
2377 return CPP_NAME;
2378 ident = pfile->token_buffer + before_name_written;
2379 ident_len = CPP_PWRITTEN (pfile) - ident;
2380 hp = cpp_lookup (pfile, ident, ident_len, -1);
2381 if (!hp)
2382 return CPP_NAME;
2383 if (hp->type == T_DISABLED)
2385 if (pfile->output_escapes)
2386 { /* Return "\r-IDENT", followed by '\0'. */
2387 int i;
2388 CPP_RESERVE (pfile, 3);
2389 ident = pfile->token_buffer + before_name_written;
2390 CPP_ADJUST_WRITTEN (pfile, 2);
2391 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2392 ident[0] = '\r';
2393 ident[1] = '-';
2395 return CPP_NAME;
2398 /* If macro wants an arglist, verify that a '(' follows.
2399 first skip all whitespace, copying it to the output
2400 after the macro name. Then, if there is no '(',
2401 decide this is not a macro call and leave things that way. */
2402 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2404 int is_macro_call, macbuf_whitespace = 0;
2406 parse_set_mark (pfile);
2407 for (;;)
2409 cpp_skip_hspace (pfile);
2410 c = PEEKC ();
2411 is_macro_call = c == '(';
2412 if (c != EOF)
2414 if (c != '\n')
2415 break;
2416 FORWARD (1);
2418 else
2420 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2422 if (CPP_BUFFER (pfile)->mark !=
2423 (CPP_BUFFER (pfile)->cur
2424 - CPP_BUFFER (pfile)->buf))
2425 macbuf_whitespace = 1;
2427 /* The mark goes away automatically when
2428 the buffer is popped. */
2429 cpp_pop_buffer (pfile);
2430 parse_set_mark (pfile);
2432 else
2433 break;
2436 if (!is_macro_call)
2438 parse_goto_mark (pfile);
2439 if (macbuf_whitespace)
2440 CPP_PUTC (pfile, ' ');
2442 else
2443 parse_clear_mark (pfile);
2444 if (!is_macro_call)
2445 return CPP_NAME;
2447 /* This is now known to be a macro call.
2448 Expand the macro, reading arguments as needed,
2449 and push the expansion on the input stack. */
2450 macroexpand (pfile, hp);
2451 CPP_SET_WRITTEN (pfile, before_name_written);
2453 goto get_next;
2455 case ' ': case '\t': case '\v':
2456 for (;;)
2458 CPP_PUTC (pfile, c);
2459 c = PEEKC ();
2460 if (c == EOF || !is_hor_space[c])
2461 break;
2462 FORWARD(1);
2464 return CPP_HSPACE;
2466 case '\r':
2467 if (CPP_BUFFER (pfile)->has_escapes)
2469 c = GETC ();
2470 if (c == '-')
2472 if (pfile->output_escapes)
2473 CPP_PUTS (pfile, "\r-", 2);
2474 parse_name (pfile, GETC ());
2475 return CPP_NAME;
2477 else if (c == ' ')
2479 CPP_RESERVE (pfile, 2);
2480 if (pfile->output_escapes)
2481 CPP_PUTC_Q (pfile, '\r');
2482 CPP_PUTC_Q (pfile, c);
2483 return CPP_HSPACE;
2485 else
2487 cpp_fatal (pfile,
2488 "internal error: unrecognized escape \\r%c", c);
2489 goto get_next;
2492 else
2494 /* Backslash newline is ignored. */
2495 CPP_BUMP_LINE (pfile);
2496 goto get_next;
2499 case '\n':
2500 CPP_PUTC (pfile, c);
2501 if (pfile->only_seen_white == 0)
2502 pfile->only_seen_white = 1;
2503 CPP_BUMP_LINE (pfile);
2504 if (! CPP_OPTIONS (pfile)->no_line_commands)
2506 pfile->lineno++;
2507 if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2508 output_line_command (pfile, same_file);
2510 return CPP_VSPACE;
2512 case '(': token = CPP_LPAREN; goto char1;
2513 case ')': token = CPP_RPAREN; goto char1;
2514 case '{': token = CPP_LBRACE; goto char1;
2515 case '}': token = CPP_RBRACE; goto char1;
2516 case ',': token = CPP_COMMA; goto char1;
2517 case ';': token = CPP_SEMICOLON; goto char1;
2519 randomchar:
2520 default:
2521 token = CPP_OTHER;
2522 char1:
2523 pfile->only_seen_white = 0;
2524 CPP_PUTC (pfile, c);
2525 return token;
2530 /* Like cpp_get_token, but skip spaces and comments. */
2532 enum cpp_token
2533 cpp_get_non_space_token (pfile)
2534 cpp_reader *pfile;
2536 int old_written = CPP_WRITTEN (pfile);
2537 for (;;)
2539 enum cpp_token token = cpp_get_token (pfile);
2540 if (token != CPP_COMMENT && token != CPP_POP
2541 && token != CPP_HSPACE && token != CPP_VSPACE)
2542 return token;
2543 CPP_SET_WRITTEN (pfile, old_written);
2547 /* Parse an identifier starting with C. */
2549 static void
2550 parse_name (pfile, c)
2551 cpp_reader *pfile;
2552 int c;
2554 for (;;)
2556 if (! is_idchar[c])
2558 FORWARD (-1);
2559 break;
2562 if (c == '$' && CPP_PEDANTIC (pfile))
2563 cpp_pedwarn (pfile, "`$' in identifier");
2565 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
2566 CPP_PUTC_Q (pfile, c);
2567 c = GETC();
2568 if (c == EOF)
2569 break;
2571 CPP_NUL_TERMINATE_Q (pfile);
2572 return;
2575 /* Parse a string starting with C. A single quoted string is treated
2576 like a double -- some programs (e.g., troff) are perverse this way.
2577 (However, a single quoted string is not allowed to extend over
2578 multiple lines.) */
2579 static void
2580 parse_string (pfile, c)
2581 cpp_reader *pfile;
2582 int c;
2584 long start_line, start_column;
2586 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2588 CPP_PUTC (pfile, c);
2589 while (1)
2591 int cc = GETC();
2592 if (cc == EOF)
2594 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2596 /* try harder: this string crosses a macro expansion
2597 boundary. This can happen naturally if -traditional.
2598 Otherwise, only -D can make a macro with an unmatched
2599 quote. */
2600 cpp_pop_buffer (pfile);
2601 continue;
2603 if (!CPP_TRADITIONAL (pfile))
2605 cpp_error_with_line (pfile, start_line, start_column,
2606 "unterminated string or character constant");
2607 if (pfile->multiline_string_line != start_line
2608 && pfile->multiline_string_line != 0)
2609 cpp_error_with_line (pfile,
2610 pfile->multiline_string_line, -1,
2611 "possible real start of unterminated constant");
2612 pfile->multiline_string_line = 0;
2614 break;
2616 CPP_PUTC (pfile, cc);
2617 switch (cc)
2619 case '\n':
2620 CPP_BUMP_LINE (pfile);
2621 pfile->lineno++;
2622 /* Traditionally, end of line ends a string constant with
2623 no error. */
2624 if (CPP_TRADITIONAL (pfile))
2625 return;
2626 /* Character constants may not extend over multiple lines. */
2627 if (c == '\'')
2629 cpp_error_with_line (pfile, start_line, start_column,
2630 "unterminated character constant");
2631 return;
2633 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2635 cpp_pedwarn_with_line (pfile, start_line, start_column,
2636 "string constant runs past end of line");
2638 if (pfile->multiline_string_line == 0)
2639 pfile->multiline_string_line = start_line;
2640 break;
2642 case '\r':
2643 CPP_ADJUST_WRITTEN (pfile, -1);
2644 if (CPP_BUFFER (pfile)->has_escapes)
2646 cpp_fatal (pfile,
2647 "internal error: \\r escape inside string constant");
2648 FORWARD(1);
2650 else
2651 /* Backslash newline is replaced by nothing at all. */
2652 CPP_BUMP_LINE (pfile);
2653 break;
2655 case '\\':
2656 cc = GETC();
2657 if (cc != EOF)
2658 CPP_PUTC (pfile, cc);
2659 break;
2661 case '\"':
2662 case '\'':
2663 if (cc == c)
2664 return;
2665 break;
2670 /* Read an assertion into the token buffer, converting to
2671 canonical form: `#predicate(a n swe r)' The next non-whitespace
2672 character to read should be the first letter of the predicate.
2673 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2674 with answer (see callers for why). In case of 0, an error has been
2675 printed. */
2676 static int
2677 parse_assertion (pfile)
2678 cpp_reader *pfile;
2680 int c, dropwhite;
2681 cpp_skip_hspace (pfile);
2682 c = PEEKC();
2683 if (! is_idstart[c])
2685 cpp_error (pfile, "assertion predicate is not an identifier");
2686 return 0;
2688 CPP_PUTC(pfile, '#');
2689 FORWARD(1);
2690 parse_name(pfile, c);
2692 c = PEEKC();
2693 if (c != '(')
2695 if (is_hor_space[c] || c == '\r')
2696 cpp_skip_hspace (pfile);
2697 c = PEEKC();
2699 if (c != '(')
2700 return 1;
2702 CPP_PUTC(pfile, '(');
2703 FORWARD(1);
2704 dropwhite = 1;
2705 while ((c = GETC()) != ')')
2707 if (is_hor_space[c])
2709 if (! dropwhite)
2711 CPP_PUTC(pfile, ' ');
2712 dropwhite = 1;
2715 else if (c == '\n' || c == EOF)
2717 if (c == '\n') FORWARD(-1);
2718 cpp_error (pfile, "un-terminated assertion answer");
2719 return 0;
2721 else if (c == '\r')
2722 /* \r cannot be a macro escape here. */
2723 CPP_BUMP_LINE (pfile);
2724 else
2726 CPP_PUTC (pfile, c);
2727 dropwhite = 0;
2731 if (pfile->limit[-1] == ' ')
2732 pfile->limit[-1] = ')';
2733 else if (pfile->limit[-1] == '(')
2735 cpp_error (pfile, "empty token sequence in assertion");
2736 return 0;
2738 else
2739 CPP_PUTC (pfile, ')');
2741 CPP_NUL_TERMINATE (pfile);
2742 return 2;
2745 static int
2746 do_assert (pfile, keyword)
2747 cpp_reader *pfile;
2748 struct directive *keyword ATTRIBUTE_UNUSED;
2750 char *sym;
2751 int ret, c;
2752 HASHNODE *base, *this;
2753 int baselen, thislen;
2755 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
2756 && !CPP_BUFFER (pfile)->system_header_p)
2757 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
2759 cpp_skip_hspace (pfile);
2760 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
2761 ret = parse_assertion (pfile);
2762 if (ret == 0)
2763 goto error;
2764 else if (ret == 1)
2766 cpp_error (pfile, "missing token-sequence in `#assert'");
2767 goto error;
2770 cpp_skip_hspace (pfile);
2771 c = PEEKC();
2772 if (c != EOF && c != '\n')
2774 cpp_error (pfile, "junk at end of `#assert'");
2775 goto error;
2778 thislen = strlen (sym);
2779 baselen = index (sym, '(') - sym;
2780 this = cpp_lookup (pfile, sym, thislen, -1);
2781 if (this)
2783 cpp_warning (pfile, "`%s' re-asserted", sym);
2784 goto error;
2787 base = cpp_lookup (pfile, sym, baselen, -1);
2788 if (! base)
2789 base = cpp_install (pfile, sym, baselen, T_ASSERT, 0, -1);
2790 else if (base->type != T_ASSERT)
2792 /* Token clash - but with what?! */
2793 cpp_fatal (pfile,
2794 "cpp internal error: base->type != T_ASSERT in do_assert");
2795 goto error;
2798 this = cpp_install (pfile, sym, thislen, T_ASSERT,
2799 (char *)base->value.aschain, -1);
2800 base->value.aschain = this;
2802 pfile->limit = (unsigned char *) sym; /* Pop */
2803 return 0;
2805 error:
2806 skip_rest_of_line (pfile);
2807 pfile->limit = (unsigned char *) sym; /* Pop */
2808 return 0;
2811 static int
2812 do_unassert (pfile, keyword)
2813 cpp_reader *pfile;
2814 struct directive *keyword ATTRIBUTE_UNUSED;
2816 int c, ret;
2817 char *sym;
2818 long baselen, thislen;
2819 HASHNODE *base, *this, *next;
2821 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
2822 && !CPP_BUFFER (pfile)->system_header_p)
2823 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
2825 cpp_skip_hspace (pfile);
2827 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
2828 ret = parse_assertion (pfile);
2829 if (ret == 0)
2830 goto error;
2832 cpp_skip_hspace (pfile);
2833 c = PEEKC ();
2834 if (c != EOF && c != '\n')
2835 cpp_error (pfile, "junk at end of `#unassert'");
2837 thislen = strlen (sym);
2838 if (ret == 1)
2840 base = cpp_lookup (pfile, sym, thislen, -1);
2841 if (! base)
2842 goto error; /* It isn't an error to #undef what isn't #defined,
2843 so it isn't an error to #unassert what isn't
2844 #asserted either. */
2846 for (this = base->value.aschain; this; this = next)
2848 next = this->value.aschain;
2849 delete_macro (this);
2851 delete_macro (base);
2853 else
2855 baselen = index (sym, '(') - sym;
2856 base = cpp_lookup (pfile, sym, baselen, -1);
2857 if (! base) goto error;
2858 this = cpp_lookup (pfile, sym, thislen, -1);
2859 if (! this) goto error;
2861 next = base;
2862 while (next->value.aschain != this)
2863 next = next->value.aschain;
2865 next->value.aschain = this->value.aschain;
2866 delete_macro (this);
2868 if (base->value.aschain == NULL)
2869 delete_macro (base); /* Last answer for this predicate deleted. */
2872 pfile->limit = (unsigned char *) sym; /* Pop */
2873 return 0;
2874 error:
2875 skip_rest_of_line (pfile);
2876 pfile->limit = (unsigned char *) sym; /* Pop */
2877 return 0;
2880 /* Process STR as if it appeared as the body of an #unassert. */
2881 void
2882 cpp_unassert (pfile, str)
2883 cpp_reader *pfile;
2884 unsigned char *str;
2886 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
2888 do_assert (pfile, NULL);
2889 cpp_pop_buffer (pfile);
2894 cpp_read_check_assertion (pfile)
2895 cpp_reader *pfile;
2897 U_CHAR *name = CPP_PWRITTEN (pfile);
2898 int result;
2899 HASHNODE *hp;
2901 FORWARD (1); /* Skip '#' */
2902 cpp_skip_hspace (pfile);
2903 if (! parse_assertion (pfile))
2904 result = 0;
2905 else
2907 hp = cpp_lookup (pfile, name, CPP_PWRITTEN (pfile) - name, -1);
2908 result = (hp != 0);
2911 pfile->limit = name;
2912 return result;
2915 /* Remember the current position of PFILE. */
2917 void
2918 parse_set_mark (pfile)
2919 cpp_reader *pfile;
2921 cpp_buffer *ip = CPP_BUFFER (pfile);
2922 if (ip->mark != -1)
2923 cpp_fatal (pfile,
2924 "cpp internal error: ip->mark != -1 in parse_set_mark");
2926 ip->mark = ip->cur - ip->buf;
2929 /* Clear the current mark - we no longer need it. */
2931 void
2932 parse_clear_mark (pfile)
2933 cpp_reader *pfile;
2935 cpp_buffer *ip = CPP_BUFFER (pfile);
2936 if (ip->mark == -1)
2937 cpp_fatal (pfile,
2938 "cpp internal error: ip->mark == -1 in parse_clear_mark");
2940 ip->mark = -1;
2943 /* Backup the current position of PFILE to that saved in its mark,
2944 and clear the mark. */
2946 void
2947 parse_goto_mark (pfile)
2948 cpp_reader *pfile;
2950 cpp_buffer *ip = CPP_BUFFER (pfile);
2951 if (ip->mark == -1)
2952 cpp_fatal (pfile,
2953 "cpp internal error: ip->mark == -1 in parse_goto_mark");
2955 ip->cur = ip->buf + ip->mark;
2956 ip->mark = -1;
2959 void
2960 cpp_print_file_and_line (pfile)
2961 cpp_reader *pfile;
2963 cpp_buffer *ip = cpp_file_buffer (pfile);
2965 if (ip != NULL)
2967 long line, col;
2968 cpp_buf_line_and_col (ip, &line, &col);
2969 cpp_file_line_for_message (pfile, ip->nominal_fname,
2970 line, pfile->show_column ? col : -1);
2974 static void
2975 v_cpp_error (pfile, msgid, ap)
2976 cpp_reader *pfile;
2977 const char *msgid;
2978 va_list ap;
2980 cpp_print_containing_files (pfile);
2981 cpp_print_file_and_line (pfile);
2982 v_cpp_message (pfile, 1, msgid, ap);
2985 void
2986 cpp_error VPROTO ((cpp_reader * pfile, const char *msgid, ...))
2988 #ifndef ANSI_PROTOTYPES
2989 cpp_reader *pfile;
2990 const char *msgid;
2991 #endif
2992 va_list ap;
2994 VA_START(ap, msgid);
2996 #ifndef ANSI_PROTOTYPES
2997 pfile = va_arg (ap, cpp_reader *);
2998 msgid = va_arg (ap, const char *);
2999 #endif
3001 v_cpp_error (pfile, msgid, ap);
3002 va_end(ap);
3005 /* Print error message but don't count it. */
3007 static void
3008 v_cpp_warning (pfile, msgid, ap)
3009 cpp_reader *pfile;
3010 const char *msgid;
3011 va_list ap;
3013 if (CPP_OPTIONS (pfile)->inhibit_warnings)
3014 return;
3016 if (CPP_OPTIONS (pfile)->warnings_are_errors)
3017 pfile->errors++;
3019 cpp_print_containing_files (pfile);
3020 cpp_print_file_and_line (pfile);
3021 v_cpp_message (pfile, 0, msgid, ap);
3024 void
3025 cpp_warning VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3027 #ifndef ANSI_PROTOTYPES
3028 cpp_reader *pfile;
3029 const char *msgid;
3030 #endif
3031 va_list ap;
3033 VA_START (ap, msgid);
3035 #ifndef ANSI_PROTOTYPES
3036 pfile = va_arg (ap, cpp_reader *);
3037 msgid = va_arg (ap, const char *);
3038 #endif
3040 v_cpp_warning (pfile, msgid, ap);
3041 va_end(ap);
3044 /* Print an error message and maybe count it. */
3046 void
3047 cpp_pedwarn VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3049 #ifndef ANSI_PROTOTYPES
3050 cpp_reader *pfile;
3051 const char *msgid;
3052 #endif
3053 va_list ap;
3055 VA_START (ap, msgid);
3057 #ifndef ANSI_PROTOTYPES
3058 pfile = va_arg (ap, cpp_reader *);
3059 msgid = va_arg (ap, const char *);
3060 #endif
3062 if (CPP_OPTIONS (pfile)->pedantic_errors)
3063 v_cpp_error (pfile, msgid, ap);
3064 else
3065 v_cpp_warning (pfile, msgid, ap);
3066 va_end(ap);
3069 static void
3070 v_cpp_error_with_line (pfile, line, column, msgid, ap)
3071 cpp_reader * pfile;
3072 int line;
3073 int column;
3074 const char * msgid;
3075 va_list ap;
3077 cpp_buffer *ip = cpp_file_buffer (pfile);
3079 cpp_print_containing_files (pfile);
3081 if (ip != NULL)
3082 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
3084 v_cpp_message (pfile, 1, msgid, ap);
3087 void
3088 cpp_error_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3089 const char *msgid, ...))
3091 #ifndef ANSI_PROTOTYPES
3092 cpp_reader *pfile;
3093 int line;
3094 int column;
3095 const char *msgid;
3096 #endif
3097 va_list ap;
3099 VA_START (ap, msgid);
3101 #ifndef ANSI_PROTOTYPES
3102 pfile = va_arg (ap, cpp_reader *);
3103 line = va_arg (ap, int);
3104 column = va_arg (ap, int);
3105 msgid = va_arg (ap, const char *);
3106 #endif
3108 v_cpp_error_with_line(pfile, line, column, msgid, ap);
3109 va_end(ap);
3112 static void
3113 v_cpp_warning_with_line (pfile, line, column, msgid, ap)
3114 cpp_reader * pfile;
3115 int line;
3116 int column;
3117 const char *msgid;
3118 va_list ap;
3120 cpp_buffer *ip;
3122 if (CPP_OPTIONS (pfile)->inhibit_warnings)
3123 return;
3125 if (CPP_OPTIONS (pfile)->warnings_are_errors)
3126 pfile->errors++;
3128 cpp_print_containing_files (pfile);
3130 ip = cpp_file_buffer (pfile);
3132 if (ip != NULL)
3133 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
3135 v_cpp_message (pfile, 0, msgid, ap);
3138 void
3139 cpp_warning_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3140 const char *msgid, ...))
3142 #ifndef ANSI_PROTOTYPES
3143 cpp_reader *pfile;
3144 int line;
3145 int column;
3146 const char *msgid;
3147 #endif
3148 va_list ap;
3150 VA_START (ap, msgid);
3152 #ifndef ANSI_PROTOTYPES
3153 pfile = va_arg (ap, cpp_reader *);
3154 line = va_arg (ap, int);
3155 column = va_arg (ap, int);
3156 msgid = va_arg (ap, const char *);
3157 #endif
3159 v_cpp_warning_with_line (pfile, line, column, msgid, ap);
3160 va_end(ap);
3163 void
3164 cpp_pedwarn_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3165 const char *msgid, ...))
3167 #ifndef ANSI_PROTOTYPES
3168 cpp_reader *pfile;
3169 int line;
3170 int column;
3171 const char *msgid;
3172 #endif
3173 va_list ap;
3175 VA_START (ap, msgid);
3177 #ifndef ANSI_PROTOTYPES
3178 pfile = va_arg (ap, cpp_reader *);
3179 line = va_arg (ap, int);
3180 column = va_arg (ap, int);
3181 msgid = va_arg (ap, const char *);
3182 #endif
3184 if (CPP_OPTIONS (pfile)->pedantic_errors)
3185 v_cpp_error_with_line (pfile, column, line, msgid, ap);
3186 else
3187 v_cpp_warning_with_line (pfile, line, column, msgid, ap);
3188 va_end(ap);
3191 /* Report a warning (or an error if pedantic_errors)
3192 giving specified file name and line number, not current. */
3194 void
3195 cpp_pedwarn_with_file_and_line VPROTO ((cpp_reader *pfile, char *file, int line,
3196 const char *msgid, ...))
3198 #ifndef ANSI_PROTOTYPES
3199 cpp_reader *pfile;
3200 char *file;
3201 int line;
3202 const char *msgid;
3203 #endif
3204 va_list ap;
3206 VA_START (ap, msgid);
3208 #ifndef ANSI_PROTOTYPES
3209 pfile = va_arg (ap, cpp_reader *);
3210 file = va_arg (ap, char *);
3211 line = va_arg (ap, int);
3212 msgid = va_arg (ap, const char *);
3213 #endif
3215 if (!CPP_OPTIONS (pfile)->pedantic_errors
3216 && CPP_OPTIONS (pfile)->inhibit_warnings)
3217 return;
3218 if (file != NULL)
3219 cpp_file_line_for_message (pfile, file, line, -1);
3220 v_cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, msgid, ap);
3221 va_end(ap);
3224 /* my_strerror - return the descriptive text associated with an
3225 `errno' code. */
3227 static char *
3228 my_strerror (errnum)
3229 int errnum;
3231 char *result;
3233 #ifndef VMS
3234 #ifndef HAVE_STRERROR
3235 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
3236 #else
3237 result = strerror (errnum);
3238 #endif
3239 #else /* VMS */
3240 /* VAXCRTL's strerror() takes an optional second argument, which only
3241 matters when the first argument is EVMSERR. However, it's simplest
3242 just to pass it unconditionally. `vaxc$errno' is declared in
3243 <errno.h>, and maintained by the library in parallel with `errno'.
3244 We assume that caller's `errnum' either matches the last setting of
3245 `errno' by the library or else does not have the value `EVMSERR'. */
3247 result = strerror (errnum, vaxc$errno);
3248 #endif
3250 if (!result)
3251 result = "errno = ?";
3253 return result;
3256 /* Error including a message from `errno'. */
3258 void
3259 cpp_error_from_errno (pfile, name)
3260 cpp_reader *pfile;
3261 const char *name;
3263 cpp_message_from_errno (pfile, 1, name);
3266 void
3267 cpp_message_from_errno (pfile, is_error, name)
3268 cpp_reader *pfile;
3269 int is_error;
3270 const char *name;
3272 int e = errno;
3273 cpp_buffer *ip = cpp_file_buffer (pfile);
3275 cpp_print_containing_files (pfile);
3277 if (ip != NULL)
3278 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
3280 cpp_message (pfile, is_error, "%s: %s", name, my_strerror (e));
3283 void
3284 cpp_perror_with_name (pfile, name)
3285 cpp_reader *pfile;
3286 const char *name;
3288 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
3291 /* TODO:
3292 * No pre-compiled header file support.
3294 * Possibly different enum token codes for each C/C++ token.
3296 * Find and cleanup remaining uses of static variables,
3298 * Support -dM flag (dump_all_macros).
3300 * Support for_lint flag.